Skip to content

Commit 432c8cb

Browse files
author
Neil
committed
Fix qtum_transaction_receipt_origin_contract_address.py
1 parent ced9380 commit 432c8cb

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

test/functional/qtum_transaction_receipt_origin_contract_address.py

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
from test_framework.script import *
66
from test_framework.p2p import *
77
from test_framework.address import *
8-
import threading
9-
10-
def waitforlogs(node, contract_address):
11-
logs = node.cli.waitforlogs(node.cli.getblockcount()-1, COINBASE_MATURITY+500, '{"addresses": ["'+contract_address+'"]}')
12-
node.result = logs
13-
8+
import time
149

1510
class QtumTransactionReceiptOriginContractAddressTest(BitcoinTestFramework):
1611
def add_options(self, parser):
@@ -24,69 +19,68 @@ def set_test_params(self):
2419
def skip_test_if_missing_module(self):
2520
self.skip_if_no_wallet()
2621

22+
def wait_for_logs(self, contract_address, start_block, timeout=30):
23+
"""
24+
Poll for logs with timeout
25+
"""
26+
end_time = time.time() + timeout
27+
while time.time() < end_time:
28+
try:
29+
current_block = self.node.getblockcount()
30+
logs = self.node.waitforlogs(
31+
fromblock=start_block,
32+
toblock=current_block,
33+
filter={"addresses": [contract_address]}
34+
)
35+
return logs
36+
except Exception as e:
37+
if "waitforlogs timeout" not in str(e):
38+
raise
39+
time.sleep(1)
40+
41+
raise TimeoutError(f"No logs found for contract {contract_address} after {timeout} seconds")
42+
2743
def run_test(self):
2844
self.node = self.nodes[0]
2945
self.nodes[0].generate(10 + COINBASE_MATURITY)
30-
"""
31-
pragma solidity ^0.5.2;
3246

33-
contract Test {
34-
event TestEvent();
35-
address private child;
36-
function setChildContract(address childContractAddress) external {
37-
child = childContractAddress;
38-
}
39-
function doEvent() external {
40-
if(child == address(0x0)) {
41-
emit TestEvent();
42-
} else {
43-
Test(child).doEvent();
44-
}
45-
}
46-
function getChildAddress() public view returns(address) {
47-
return child;
48-
}
49-
}
50-
"""
51-
"""
52-
Function signatures:
53-
afd67ce7: doEvent()
54-
bcb1c3a9: getChildAddress()
55-
f8d86e18: setChildContract(address)
56-
"""
47+
# Contract bytecode (unchanged)
48+
contract_bytecode = "608060405234801561001057600080fd5b506102b8806100206000396000f3fe608060405234801561001057600080fd5b506004361061005e576000357c010000000000000000000000000000000000000000000000000000000090048063afd67ce714610063578063bcb1c3a91461006d578063f8d86e18146100b7575b600080fd5b61006b6100fb565b005b610075610220565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100f9600480360360208110156100cd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610249565b005b600073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415610182577f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405160405180910390a161021e565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663afd67ce76040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401600060405180830381600087803b15801561020757600080fd5b5060325a03f115801561021957600080fd5b505050505b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea165627a7a723058203cf61a18e40f6e2bd01b2f7bd607c6e6aff032f12bd5e3eca68212d2e2c80dbf0029"
5749

58-
# Set up a chain of 10 contracts that reference their child contract. I.e. the tenth contract is the leaf
50+
# Set up contract chain
5951
contracts = []
60-
contract_bytecode = "608060405234801561001057600080fd5b506102b8806100206000396000f3fe608060405234801561001057600080fd5b506004361061005e576000357c010000000000000000000000000000000000000000000000000000000090048063afd67ce714610063578063bcb1c3a91461006d578063f8d86e18146100b7575b600080fd5b61006b6100fb565b005b610075610220565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100f9600480360360208110156100cd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610249565b005b600073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415610182577f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405160405180910390a161021e565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663afd67ce76040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401600060405180830381600087803b15801561020757600080fd5b5060325a03f115801561021957600080fd5b505050505b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea165627a7a723058203cf61a18e40f6e2bd01b2f7bd607c6e6aff032f12bd5e3eca68212d2e2c80dbf0029"
6152
for i in range(10):
6253
contracts.append(self.nodes[0].createcontract(contract_bytecode)['address'])
6354
self.node.generate(1)
6455
if len(contracts) > 1:
6556
self.node.sendtocontract(contracts[-2], "f8d86e18" + (contracts[-1].zfill(64)), 0, 1000000)
6657
self.node.generate(1)
67-
68-
# Run the doEvent function recursively starting at the root contract and make sure that no event entries is in the returndata for waitforlogs for the first 9 contracts
58+
59+
# Test non-leaf contracts (first 9)
6960
for contract_address in contracts[:-1]:
70-
thread = threading.Thread(target=waitforlogs, args=(self.node, contract_address))
71-
thread.start()
61+
# Get current block before transaction
62+
start_block = self.node.getblockcount()
63+
64+
# Send contract transaction
7265
txid = self.node.sendtocontract(contracts[0], "afd67ce7", 0, 1000000)['txid']
7366
self.node.generate(7)
74-
thread.join()
67+
68+
# Wait for logs and verify
69+
logs = self.wait_for_logs(contract_address, start_block)
7570
receipt = self.node.gettransactionreceipt(txid)
7671
assert_equal(receipt[0]['log'][0]['address'], contracts[-1])
77-
assert_equal(len(self.node.result['entries']), 0)
78-
72+
assert_equal(len(logs['entries']), 0)
7973

80-
# Do the same thing again but make sure that the event triggers for the "leaf" (10th) contract
81-
thread = threading.Thread(target=waitforlogs, args=(self.node, contracts[-1]))
82-
thread.start()
74+
# Test leaf contract (10th)
75+
start_block = self.node.getblockcount()
8376
txid = self.node.sendtocontract(contracts[0], "afd67ce7", 0, 1000000)['txid']
8477
self.node.generate(7)
85-
thread.join()
78+
79+
# Wait for logs and verify
80+
logs = self.wait_for_logs(contracts[-1], start_block)
8681
receipt = self.node.gettransactionreceipt(txid)
8782
assert_equal(receipt[0]['log'][0]['address'], contracts[-1])
88-
assert_equal(len(self.node.result['entries']), 1)
89-
83+
assert_equal(len(logs['entries']), 1)
9084

9185
if __name__ == '__main__':
92-
QtumTransactionReceiptOriginContractAddressTest().main()
86+
QtumTransactionReceiptOriginContractAddressTest().main()

0 commit comments

Comments
 (0)