Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Add trace plugin API test #9194

Merged
merged 10 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/plugin_http_api_test.py ${CMAKE_CURRE
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/resource_monitor_plugin_test.py ${CMAKE_CURRENT_BINARY_DIR}/resource_monitor_plugin_test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/rodeos_test.py ${CMAKE_CURRENT_BINARY_DIR}/rodeos_test.py COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test_filter.wasm ${CMAKE_CURRENT_BINARY_DIR}/test_filter.wasm COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/trace_plugin_test.py ${CMAKE_CURRENT_BINARY_DIR}/trace_plugin_test.py COPYONLY)

#To run plugin_test with all log from blockchain displayed, put --verbose after --, i.e. plugin_test -- --verbose
add_test(NAME plugin_test COMMAND plugin_test --report_level=detailed --color_output)
Expand Down Expand Up @@ -172,6 +173,10 @@ add_test(NAME plugin_http_api_test COMMAND tests/plugin_http_api_test.py WORKING
set_tests_properties(plugin_http_api_test PROPERTIES TIMEOUT 40)
set_property(TEST plugin_http_api_test PROPERTY LABELS nonparallelizable_tests)

add_test(NAME trace_plugin_test COMMAND tests/trace_plugin_test.py WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_tests_properties(trace_plugin_test PROPERTIES TIMEOUT 100)
set_property(TEST trace_plugin_test PROPERTY LABELS nonparallelizable_tests)

add_subdirectory(se_tests)

add_test(NAME resource_monitor_plugin_test COMMAND tests/resource_monitor_plugin_test.py WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
Expand Down
106 changes: 106 additions & 0 deletions tests/trace_plugin_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python3
import json
import time
import unittest

from testUtils import Utils
from Cluster import Cluster
from TestHelper import TestHelper
from Node import Node
from WalletMgr import WalletMgr
from core_symbol import CORE_SYMBOL

class TraceApiPluginTest(unittest.TestCase):
brianjohnson5972 marked this conversation as resolved.
Show resolved Hide resolved
sleep_s = 1
cluster=Cluster(walletd=True, defproduceraPrvtKey=None)
walletMgr=WalletMgr(True)
accounts = []
cluster.setWalletMgr(walletMgr)

# kill nodeos and keosd and clean up dir
def cleanEnv(self, shouldCleanup: bool) :
self.cluster.killall(allInstances=True)
if shouldCleanup:
self.cluster.cleanup()
self.walletMgr.killall(allInstances=True)
if shouldCleanup:
self.walletMgr.cleanup()

# start keosd and nodeos
def startEnv(self) :
account_names = ["alice", "bob", "charlie"]
traceNodeosArgs = " --plugin eosio::trace_api_plugin --trace-no-abis --trace-dir=."
self.cluster.launch(totalNodes=1, extraNodeosArgs=traceNodeosArgs)
self.walletMgr.launch()
testWalletName="testwallet"
testWallet=self.walletMgr.create(testWalletName, [self.cluster.eosioAccount, self.cluster.defproduceraAccount])
self.cluster.validateAccounts(None)
self.accounts=Cluster.createAccountKeys(len(account_names))
node = self.cluster.getNode(0)
for idx in range(len(account_names)):
self.accounts[idx].name = account_names[idx]
self.walletMgr.importKey(self.accounts[idx], testWallet)
for account in self.accounts:
node.createInitializeAccount(account, self.cluster.eosioAccount, buyRAM=1000000, stakedDeposit=5000000, waitForTransBlock=True, exitOnError=True)
time.sleep(self.sleep_s)

def get_block(self, params: str, node: Node) -> json:
base_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, node.port)
cmd_str = base_cmd_str + "trace_api/get_block -X POST -d " + ("'{\"block_num\":%s}'") % params
return Utils.runCmdReturnJson(cmd_str)

def test_TraceApi(self) :
node = self.cluster.getNode(0)
for account in self.accounts:
brianjohnson5972 marked this conversation as resolved.
Show resolved Hide resolved
self.assertIsNotNone(node.verifyAccount(account))

expectedAmount = Node.currencyIntToStr(5000000, CORE_SYMBOL)
brianjohnson5972 marked this conversation as resolved.
Show resolved Hide resolved
account_balances = []
for account in self.accounts:
amount = node.getAccountEosBalanceStr(account.name)
self.assertEqual(amount, expectedAmount)
account_balances.append(amount)

xferAmount = Node.currencyIntToStr(123456, CORE_SYMBOL)
trans = node.transferFunds(self.accounts[0], self.accounts[1], xferAmount, "test transfer a->b")
transId = Node.getTransId(trans)
blockNum = Node.getTransBlockNum(trans)

self.assertEqual(node.getAccountEosBalanceStr(self.accounts[0].name), Utils.deduceAmount(expectedAmount, xferAmount))
self.assertEqual(node.getAccountEosBalanceStr(self.accounts[1].name), Utils.addAmount(expectedAmount, xferAmount))
time.sleep(self.sleep_s)

# verify trans via node api before calling trace_api RPC
blockFromNode = node.getBlock(blockNum)
self.assertIn("transactions", blockFromNode)
isTrxInBlockFromNode = False
for trx in blockFromNode["transactions"]:
self.assertIn("trx", trx)
self.assertIn("id", trx["trx"])
if (trx["trx"]["id"] == transId) :
isTrxInBlockFromNode = True
break
self.assertTrue(isTrxInBlockFromNode)

# verify trans via trace_api by calling get_block RPC
blockFromTraceApi = self.get_block(blockNum, node)
self.assertIn("transactions", blockFromTraceApi)
isTrxInBlockFromTraceApi = False
for trx in blockFromTraceApi["transactions"]:
self.assertIn("id", trx)
if (trx["id"] == transId) :
isTrxInBlockFromTraceApi = True
break
self.assertTrue(isTrxInBlockFromTraceApi)

@classmethod
def setUpClass(self):
self.cleanEnv(self, shouldCleanup=True)
self.startEnv(self)

@classmethod
def tearDownClass(self):
self.cleanEnv(self, shouldCleanup=False) # not cleanup to save log in case for further investigation

if __name__ == "__main__":
brianjohnson5972 marked this conversation as resolved.
Show resolved Hide resolved
unittest.main()