5
5
from test_framework .script import *
6
6
from test_framework .p2p import *
7
7
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
14
9
15
10
class QtumTransactionReceiptOriginContractAddressTest (BitcoinTestFramework ):
16
11
def add_options (self , parser ):
@@ -24,69 +19,68 @@ def set_test_params(self):
24
19
def skip_test_if_missing_module (self ):
25
20
self .skip_if_no_wallet ()
26
21
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
+
27
43
def run_test (self ):
28
44
self .node = self .nodes [0 ]
29
45
self .nodes [0 ].generate (10 + COINBASE_MATURITY )
30
- """
31
- pragma solidity ^0.5.2;
32
46
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"
57
49
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
59
51
contracts = []
60
- contract_bytecode = "608060405234801561001057600080fd5b506102b8806100206000396000f3fe608060405234801561001057600080fd5b506004361061005e576000357c010000000000000000000000000000000000000000000000000000000090048063afd67ce714610063578063bcb1c3a91461006d578063f8d86e18146100b7575b600080fd5b61006b6100fb565b005b610075610220565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100f9600480360360208110156100cd57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610249565b005b600073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415610182577f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405160405180910390a161021e565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663afd67ce76040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401600060405180830381600087803b15801561020757600080fd5b5060325a03f115801561021957600080fd5b505050505b565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea165627a7a723058203cf61a18e40f6e2bd01b2f7bd607c6e6aff032f12bd5e3eca68212d2e2c80dbf0029"
61
52
for i in range (10 ):
62
53
contracts .append (self .nodes [0 ].createcontract (contract_bytecode )['address' ])
63
54
self .node .generate (1 )
64
55
if len (contracts ) > 1 :
65
56
self .node .sendtocontract (contracts [- 2 ], "f8d86e18" + (contracts [- 1 ].zfill (64 )), 0 , 1000000 )
66
57
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)
69
60
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
72
65
txid = self .node .sendtocontract (contracts [0 ], "afd67ce7" , 0 , 1000000 )['txid' ]
73
66
self .node .generate (7 )
74
- thread .join ()
67
+
68
+ # Wait for logs and verify
69
+ logs = self .wait_for_logs (contract_address , start_block )
75
70
receipt = self .node .gettransactionreceipt (txid )
76
71
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 )
79
73
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 ()
83
76
txid = self .node .sendtocontract (contracts [0 ], "afd67ce7" , 0 , 1000000 )['txid' ]
84
77
self .node .generate (7 )
85
- thread .join ()
78
+
79
+ # Wait for logs and verify
80
+ logs = self .wait_for_logs (contracts [- 1 ], start_block )
86
81
receipt = self .node .gettransactionreceipt (txid )
87
82
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 )
90
84
91
85
if __name__ == '__main__' :
92
- QtumTransactionReceiptOriginContractAddressTest ().main ()
86
+ QtumTransactionReceiptOriginContractAddressTest ().main ()
0 commit comments