Skip to content

Commit fbb8de4

Browse files
authored
Merge pull request #386 from eosnetworkfoundation/kayan_1.0_forktest_fix
fix gas param fork test
2 parents 7efa71d + da9a7b1 commit fbb8de4

File tree

1 file changed

+30
-108
lines changed

1 file changed

+30
-108
lines changed

tests/nodeos_eos_evm_gasparam_fork_test.py

+30-108
Original file line numberDiff line numberDiff line change
@@ -214,65 +214,6 @@ def interact_with_storage_contract(dest, nonce):
214214

215215
return nonce
216216

217-
def processUrllibRequest(endpoint, payload={}, silentErrors=False, exitOnError=False, exitMsg=None, returnType=ReturnType.json):
218-
cmd = f"{endpoint}"
219-
req = urllib.request.Request(cmd, method="POST")
220-
req.add_header('Content-Type', 'application/json')
221-
req.add_header('Accept', 'application/json')
222-
data = payload
223-
data = json.dumps(data)
224-
data = data.encode()
225-
if Utils.Debug: Utils.Print("cmd: %s" % (cmd))
226-
rtn=None
227-
start=time.perf_counter()
228-
try:
229-
response = urllib.request.urlopen(req, data=data)
230-
if returnType==ReturnType.json:
231-
rtn = {}
232-
rtn["code"] = response.getcode()
233-
rtn["payload"] = json.load(response)
234-
elif returnType==ReturnType.raw:
235-
rtn = response.read()
236-
else:
237-
unhandledEnumType(returnType)
238-
239-
if Utils.Debug:
240-
end=time.perf_counter()
241-
Utils.Print("cmd Duration: %.3f sec" % (end-start))
242-
printReturn=json.dumps(rtn) if returnType==ReturnType.json else rtn
243-
Utils.Print("cmd returned: %s" % (printReturn[:1024]))
244-
except urllib.error.HTTPError as ex:
245-
if not silentErrors:
246-
end=time.perf_counter()
247-
msg=ex.msg
248-
errorMsg="Exception during \"%s\". %s. cmd Duration=%.3f sec." % (cmd, msg, end-start)
249-
if exitOnError:
250-
Utils.cmdError(errorMsg)
251-
Utils.errorExit(errorMsg)
252-
else:
253-
Utils.Print("ERROR: %s" % (errorMsg))
254-
if returnType==ReturnType.json:
255-
rtn = json.load(ex)
256-
elif returnType==ReturnType.raw:
257-
rtn = ex.read()
258-
else:
259-
unhandledEnumType(returnType)
260-
else:
261-
return None
262-
except:
263-
Utils.Print("Unknown exception occurred during processUrllibRequest")
264-
raise
265-
266-
if exitMsg is not None:
267-
exitMsg=": " + exitMsg
268-
else:
269-
exitMsg=""
270-
if exitOnError and rtn is None:
271-
Utils.cmdError("could not \"%s\" - %s" % (cmd,exitMsg))
272-
Utils.errorExit("Failed to \"%s\"" % (cmd))
273-
274-
return rtn
275-
276217
def getGasPrice():
277218
return 15000000000
278219

@@ -320,10 +261,11 @@ def makeReservedEvmAddress(account):
320261

321262
specificExtraNodeosArgs[2]="--plugin eosio::test_control_api_plugin"
322263

323-
extraNodeosArgs="--contracts-console --resource-monitor-not-shutdown-on-threshold-exceeded"
264+
extraNodeosArgs="--contracts-console --production-pause-vote-timeout-ms 0 --resource-monitor-not-shutdown-on-threshold-exceeded"
324265

325266
Print("Stand up cluster")
326-
if cluster.launch(prodCount=2, pnodes=2, topo="bridge", totalNodes=3, extraNodeosArgs=extraNodeosArgs, totalProducers=3, specificExtraNodeosArgs=specificExtraNodeosArgs,delay=5) is False:
267+
# node 0 (defproducera, defproducerb), node 1 (defproducerc, ...)
268+
if cluster.launch(prodCount=2, pnodes=2, topo="bridge", totalNodes=3, extraNodeosArgs=extraNodeosArgs, totalProducers=4, specificExtraNodeosArgs=specificExtraNodeosArgs,delay=5,activateIF=True,biosFinalizer=False) is False:
327269
errorExit("Failed to stand up eos cluster.")
328270

329271
Print ("Wait for Cluster stabilization")
@@ -352,10 +294,10 @@ def makeReservedEvmAddress(account):
352294
prodNodes.append(node)
353295
producers.extend(node.producers)
354296

355-
node=prodNodes[0] # producers: defproducera, defproducerb
356-
node1=prodNodes[1] # producers: defproducerc
357297
# node2 is the nonProdnode (the bridge node)
358-
prodNode = prodNodes[0]
298+
prodNode = prodNodes[shipNodeNum] # the node the push transactions
299+
node = prodNode
300+
node1 = prodNodes[1 - shipNodeNum]
359301

360302
# *** Identify a block where production is stable ***
361303
#verify nodes are in sync and advancing
@@ -496,16 +438,8 @@ def makeReservedEvmAddress(account):
496438
trans = prodNode.pushMessage(evmAcc.name, "pushtx", json.dumps(actData), '-p {0}'.format(minerAcc.name))
497439
prodNode.waitForTransBlockIfNeeded(trans[1], True)
498440

499-
#
500-
# Test some failure cases
501-
#
502-
503-
# incorrect nonce
504-
Utils.Print("Send balance again, should fail with wrong nonce")
505-
retValue = prodNode.pushMessage(evmAcc.name, "pushtx", json.dumps(actData), '-p {0}'.format(minerAcc.name), silentErrors=True)
506-
assert not retValue[0], f"push trx should have failed: {retValue}"
507-
508441
# correct nonce
442+
time.sleep(1.0)
509443
nonce += 1
510444
gasP = getGasPrice()
511445
signed_trx = w3.eth.account.sign_transaction(dict(
@@ -524,30 +458,6 @@ def makeReservedEvmAddress(account):
524458
time.sleep(1.0)
525459
assert retValue[0], f"push trx should have succeeded: {retValue}"
526460

527-
# incorrect chainid
528-
nonce += 1
529-
evmChainId = 8888
530-
gasP = getGasPrice()
531-
signed_trx = w3.eth.account.sign_transaction(dict(
532-
nonce=nonce,
533-
gas=100000, #100k Gas
534-
gasPrice=gasP,
535-
to=Web3.to_checksum_address(toAdd),
536-
value=amount,
537-
data=b'',
538-
chainId=evmChainId
539-
), evmSendKey)
540-
541-
actData = {"miner":minerAcc.name, "rlptx":Web3.to_hex(get_raw_transaction(signed_trx))[2:]}
542-
Utils.Print("Send balance again, with invalid chainid")
543-
retValue = prodNode.pushMessage(evmAcc.name, "pushtx", json.dumps(actData), '-p {0}'.format(minerAcc.name), silentErrors=True)
544-
time.sleep(1.0)
545-
assert not retValue[0], f"push trx should have failed: {retValue}"
546-
547-
# correct values for continuing
548-
nonce -= 1
549-
evmChainId = 15555
550-
551461
Utils.Print("Simple Solidity contract")
552462
# pragma solidity >=0.7.0 <0.9.0;
553463
# contract Storage {
@@ -758,7 +668,7 @@ def makeReservedEvmAddress(account):
758668
# Verify header.nonce == 1 (evmversion=1)
759669
evm_block = w3.eth.get_block('latest')
760670
Utils.Print("before fork, the latest evm block is:" + str(evm_block))
761-
assert(evm_block["nonce"].hex() == "0000000000000001")
671+
assert(evm_block["nonce"].hex() == "0000000000000001" or evm_block["nonce"].hex() == "0x0000000000000001")
762672
assert("consensusParameter" in evm_block)
763673
assert(evm_block["consensusParameter"]["gasFeeParameters"]["gasCodedeposit"] == 477)
764674
assert(evm_block["consensusParameter"]["gasFeeParameters"]["gasNewaccount"] == 165519)
@@ -823,8 +733,9 @@ def makeReservedEvmAddress(account):
823733
Utils.errorExit("failed to catch a block produced by defproducerb")
824734

825735
blockProducer1=node1.getBlockProducerByNum(blockNum)
826-
Utils.Print("block number %d is producer by %s in node0" % (blockNum, blockProducer))
827-
Utils.Print("block number %d is producer by %s in node1" % (blockNum, blockProducer1))
736+
lib = info["last_irreversible_block_num"]
737+
Utils.Print("before kill, block number %d is producer by %s in node0, LIB %d" % (blockNum, blockProducer, lib))
738+
Utils.Print("before kill, block number %d is producer by %s in node1, LIB %d" % (blockNum, blockProducer1, lib))
828739

829740
# ===== start to make a fork, killing the "bridge" node ====
830741
Print("Sending command to kill \"bridge\" node to separate the 2 producer groups.")
@@ -900,12 +811,11 @@ def makeReservedEvmAddress(account):
900811
prodNode.waitForTransBlockIfNeeded(trans[1], True);
901812
time.sleep(2)
902813

903-
Utils.Print("Transfer funds to trigger gas parameter in minor fork")
904814
# EVM -> EVM
905815
# 0x9E126C57330FA71556628e0aabd6B6B6783d99fA private key: 0xba8c9ff38e4179748925335a9891b969214b37dc3723a1754b8b849d3eea9ac0
906816
toAdd = 0x4ce0ca184bc155a5df5dae2f4643cba60eb1a9ed
907817
evmSendKey = "ba8c9ff38e4179748925335a9891b969214b37dc3723a1754b8b849d3eea9ac0"
908-
Print("Transfer EVM->EVM funds 1Gwei from account %s to %s" % (evmAcc.name, toAdd))
818+
Print("In minor fork, transfer EVM->EVM funds 1Gwei from account %s to %s to trigger gas parameter change" % (evmAcc.name, toAdd))
909819
nonce = 1
910820
gasP = getGasPrice()
911821
signed_trx = w3.eth.account.sign_transaction(dict(
@@ -924,7 +834,7 @@ def makeReservedEvmAddress(account):
924834
time.sleep(1.0)
925835
prodNode.waitForTransBlockIfNeeded(trans[1], True)
926836
row4=prodNode.getTableRow(evmAcc.name, evmAcc.name, "account", 4)
927-
Utils.Print("\taccount row4 in node0: ", row4)
837+
Utils.Print("In minor fork account row4 in node0: ", row4)
928838

929839
assert(row4["eth_address"] == "9e126c57330fa71556628e0aabd6b6b6783d99fa")
930840
assert(row4["balance"] == "0000000000000000000000000000000000000000000000024c8ff6c362545600")
@@ -934,15 +844,15 @@ def makeReservedEvmAddress(account):
934844
time.sleep(1.0)
935845
node1.waitForTransBlockIfNeeded(trans[1], True)
936846
row4_node1=node1.getTableRow(evmAcc.name, evmAcc.name, "account", 4)
937-
Utils.Print("\taccount row4 in node1: ", row4_node1)
847+
Utils.Print("In minor fork, account row4 in node1: ", row4_node1)
938848
assert(row4_node1["eth_address"] == "9e126c57330fa71556628e0aabd6b6b6783d99fa")
939849
assert(row4_node1["balance"] == "0000000000000000000000000000000000000000000000024c91bd38333b1600")
940850
assert(row4["balance"] != row4_node1["balance"])
941851

942852
# verify eos-evm-node get the new gas parameter from the minor fork
943853
evm_block = w3.eth.get_block('latest')
944854
Utils.Print("in minor fork, the latest evm block is:" + str(evm_block))
945-
assert(evm_block["nonce"].hex() == "0000000000000001")
855+
assert(evm_block["nonce"].hex() == "0000000000000001" or evm_block["nonce"].hex() == "0x0000000000000001")
946856
assert("consensusParameter" in evm_block)
947857

948858
assert(evm_block["consensusParameter"]["gasFeeParameters"]["gasCodedeposit"] == 573)
@@ -992,6 +902,10 @@ def makeReservedEvmAddress(account):
992902
# if node1.verifyAlive():
993903
# Utils.errorExit("Expected the node 1 to have shutdown.")
994904

905+
blockNum0 = prodNodes[0].getBlockNum()
906+
blockNum1 = prodNodes[1].getBlockNum()
907+
WaitUntilBlockNum = max(blockNum0, blockNum1) + 20
908+
Print("Before relaunching the bridge node: prod[0] head_block_num %d, prod[1] head_block_num %d" % (blockNum0, blockNum1))
995909
Print("Relaunching the non-producing bridge node to connect the node 0 (defproducera, defproducerb)")
996910
if not nonProdNode.relaunch(chainArg=" --hard-replay "):
997911
errorExit("Failure - (non-production) node %d should have restarted" % (nonProdNode.nodeNum))
@@ -1001,7 +915,7 @@ def makeReservedEvmAddress(account):
1001915
# if not node1.relaunch(chainArg=" --enable-stale-production "):
1002916
# errorExit("Failure - (non-production) node 1 should have restarted")
1003917

1004-
Print("Waiting to allow forks to resolve")
918+
Print("Waiting to allow forks to resolve from block %d" % (killBlockNum))
1005919
time.sleep(3)
1006920

1007921
for prodNode in prodNodes:
@@ -1024,9 +938,11 @@ def makeReservedEvmAddress(account):
1024938
if match:
1025939
if checkHead:
1026940
forkResolved=True
1027-
Print("Great! fork resolved!!!")
941+
Print("Great! fork resolved!!! killBlockNum %d, current head_number %d, producer %s" % (killBlockNum, checkMatchBlock, blockProducer0))
1028942
break
1029943
else:
944+
Print("Block %d has producer %s in both nodes, continue to check head" %(checkMatchBlock, blockProducer0))
945+
assert (blockProducer0 == "defproducerc" or blockProducer0 == "defproducerd"), "node0 must switch fork at %d" % (killBlockNum)
1030946
checkHead=True
1031947
continue
1032948
Print("Fork has not resolved yet, wait a little more. Block %s has producer %s for node_00 and %s for node_01. Original divergence was at block %s. Wait time remaining: %d" % (checkMatchBlock, blockProducer0, blockProducer1, killBlockNum, remainingChecks))
@@ -1036,6 +952,12 @@ def makeReservedEvmAddress(account):
1036952
assert forkResolved, "fork was not resolved in a reasonable time. node_00 lib {} head {}, node_01 lib {} head {}".format(\
1037953
prodNodes[0].getIrreversibleBlockNum(), prodNodes[0].getHeadBlockNum(), \
1038954
prodNodes[1].getIrreversibleBlockNum(), prodNodes[1].getHeadBlockNum())
955+
956+
# wait until the current chain is longer than any minor fork happened in the past
957+
# ensure the EVM oracle to switch to longer fork
958+
blockNum0 = prodNodes[0].getBlockNum()
959+
WaitUntilBlockNum = max(WaitUntilBlockNum, killBlockNum + 30)
960+
prodNodes[0].waitForBlock(WaitUntilBlockNum)
1039961

1040962
row4=prodNode.getTableRow(evmAcc.name, evmAcc.name, "account", 4)
1041963
Utils.Print("\taccount row4 in node0: ", row4)
@@ -1046,7 +968,7 @@ def makeReservedEvmAddress(account):
1046968

1047969
evm_block = w3.eth.get_block('latest')
1048970
Utils.Print("after fork resolved, the latest evm block is:" + str(evm_block))
1049-
assert(evm_block["nonce"].hex() == "0000000000000001")
971+
assert(evm_block["nonce"].hex() == "0000000000000001" or evm_block["nonce"].hex() == "0x0000000000000001")
1050972
assert("consensusParameter" in evm_block)
1051973

1052974
assert(evm_block["consensusParameter"]["gasFeeParameters"]["gasCodedeposit"] == 477)

0 commit comments

Comments
 (0)