Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a57f532

Browse files
committedJan 13, 2022
Separate emitter contract into new and old versions
`enable_strict_bytes_type_checking()` appears to be broken with solidity versions `0.5.0` and above. This commit separates a new emitter contract, compiled with solidity `0.8.11`, and the older emitter contract, compiled with solidity `0.4.21`. This way, we can update the tests using the `emitter` pytest modules while the strict bytes test can use the old emitter contract until we can update `enable_strict_bytes_type_checking()` to be compatible with newer solidity versions.
1 parent 1c9ec1e commit a57f532

File tree

5 files changed

+800
-4
lines changed

5 files changed

+800
-4
lines changed
 

‎tests/core/contracts/conftest.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
CONTRACT_EMITTER_CODE,
1212
CONTRACT_EMITTER_RUNTIME,
1313
)
14+
from web3._utils.module_testing.emitter_contract_old import (
15+
CONTRACT_EMITTER_ABI_OLD,
16+
CONTRACT_EMITTER_CODE_OLD,
17+
CONTRACT_EMITTER_RUNTIME_OLD,
18+
)
1419
from web3._utils.module_testing.event_contract import (
1520
EVNT_CONTRACT_ABI,
1621
EVNT_CONTRACT_CODE,
@@ -422,9 +427,20 @@ def EMITTER(EMITTER_CODE,
422427

423428

424429
@pytest.fixture()
425-
def StrictEmitter(w3_strict_abi, EMITTER):
430+
def STRICT_EMITTER():
431+
# Uses an older version of solidity to compile for strict bytes checking.
432+
# See: https://github.com/ethereum/web3.py/issues/2301
433+
return {
434+
'bytecode': CONTRACT_EMITTER_CODE_OLD,
435+
'bytecode_runtime': CONTRACT_EMITTER_RUNTIME_OLD,
436+
'abi': CONTRACT_EMITTER_ABI_OLD,
437+
}
438+
439+
440+
@pytest.fixture()
441+
def StrictEmitter(w3_strict_abi, STRICT_EMITTER):
426442
w3 = w3_strict_abi
427-
return w3.eth.contract(**EMITTER)
443+
return w3.eth.contract(**STRICT_EMITTER)
428444

429445

430446
@pytest.fixture()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// This older version of the Emitter contract can be use to keep the strict bytes test against it while we work on
2+
// updating the strict bytes checking to be compatible with newer solidity versions.
3+
// # See: https://github.com/ethereum/web3.py/issues/2301
4+
5+
pragma solidity ^0.4.21;
6+
7+
8+
contract Emitter {
9+
event LogAnonymous() anonymous;
10+
event LogNoArguments();
11+
event LogSingleArg(uint arg0);
12+
event LogDoubleArg(uint arg0, uint arg1);
13+
event LogTripleArg(uint arg0, uint arg1, uint arg2);
14+
event LogQuadrupleArg(uint arg0, uint arg1, uint arg2, uint arg3);
15+
event LogString(string v);
16+
event LogBytes(bytes v);
17+
18+
// Indexed
19+
event LogSingleWithIndex(uint indexed arg0);
20+
event LogSingleAnonymous(uint indexed arg0) anonymous;
21+
event LogDoubleWithIndex(uint arg0, uint indexed arg1);
22+
event LogDoubleAnonymous(uint arg0, uint indexed arg1) anonymous;
23+
event LogTripleWithIndex(uint arg0, uint indexed arg1, uint indexed arg2);
24+
event LogQuadrupleWithIndex(uint arg0, uint arg1, uint indexed arg2, uint indexed arg3);
25+
event LogDynamicArgs(string indexed arg0, string arg1);
26+
event LogListArgs(bytes2[] indexed arg0, bytes2[] arg1);
27+
event LogAddressIndexed(address indexed arg0, address arg1);
28+
event LogAddressNotIndexed(address arg0, address arg1);
29+
30+
enum WhichEvent {
31+
LogAnonymous,
32+
LogNoArguments,
33+
LogSingleArg,
34+
LogDoubleArg,
35+
LogTripleArg,
36+
LogQuadrupleArg,
37+
LogSingleAnonymous,
38+
LogSingleWithIndex,
39+
LogDoubleAnonymous,
40+
LogDoubleWithIndex,
41+
LogTripleWithIndex,
42+
LogQuadrupleWithIndex,
43+
LogBytes,
44+
LogString,
45+
LogDynamicArgs,
46+
LogListArgs,
47+
LogAddressIndexed,
48+
LogAddressNotIndexed
49+
}
50+
51+
function logNoArgs(WhichEvent which) public {
52+
if (which == WhichEvent.LogNoArguments) emit LogNoArguments();
53+
else if (which == WhichEvent.LogAnonymous) emit LogAnonymous();
54+
else revert("Didn't match any allowable event index");
55+
}
56+
57+
function logSingle(WhichEvent which, uint arg0) public {
58+
if (which == WhichEvent.LogSingleArg) emit LogSingleArg(arg0);
59+
else if (which == WhichEvent.LogSingleWithIndex) emit LogSingleWithIndex(arg0);
60+
else if (which == WhichEvent.LogSingleAnonymous) emit LogSingleAnonymous(arg0);
61+
else revert("Didn't match any allowable event index");
62+
}
63+
64+
function logDouble(WhichEvent which, uint arg0, uint arg1) public {
65+
if (which == WhichEvent.LogDoubleArg) emit LogDoubleArg(arg0, arg1);
66+
else if (which == WhichEvent.LogDoubleWithIndex) emit LogDoubleWithIndex(arg0, arg1);
67+
else if (which == WhichEvent.LogDoubleAnonymous) emit LogDoubleAnonymous(arg0, arg1);
68+
else revert("Didn't match any allowable event index");
69+
}
70+
71+
function logTriple(WhichEvent which, uint arg0, uint arg1, uint arg2) public {
72+
if (which == WhichEvent.LogTripleArg) emit LogTripleArg(arg0, arg1, arg2);
73+
else if (which == WhichEvent.LogTripleWithIndex) emit LogTripleWithIndex(arg0, arg1, arg2);
74+
else revert("Didn't match any allowable event index");
75+
}
76+
77+
function logQuadruple(WhichEvent which, uint arg0, uint arg1, uint arg2, uint arg3) public {
78+
if (which == WhichEvent.LogQuadrupleArg) emit LogQuadrupleArg(arg0, arg1, arg2, arg3);
79+
else if (which == WhichEvent.LogQuadrupleWithIndex) emit LogQuadrupleWithIndex(arg0, arg1, arg2, arg3);
80+
else revert("Didn't match any allowable event index");
81+
}
82+
83+
function logDynamicArgs(string arg0, string arg1) public {
84+
emit LogDynamicArgs(arg0, arg1);
85+
}
86+
87+
function logListArgs(bytes2[] arg0, bytes2[] arg1) public {
88+
emit LogListArgs(arg0, arg1);
89+
}
90+
91+
function logAddressIndexedArgs(address arg0, address arg1) public {
92+
emit LogAddressIndexed(arg0, arg1);
93+
}
94+
95+
function logAddressNotIndexedArgs(address arg0, address arg1) public {
96+
emit LogAddressNotIndexed(arg0, arg1);
97+
}
98+
99+
function logBytes(bytes v) public {
100+
emit LogBytes(v);
101+
}
102+
103+
function logString(string v) public {
104+
emit LogString(v);
105+
}
106+
}

‎tests/core/contracts/test_extracting_event_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def test_argument_extraction_strict_bytes_types(w3_strict_abi,
330330
emitter_log_topics):
331331
arg_0 = [b'12']
332332
arg_1 = [b'12']
333-
txn_hash = strict_emitter.functions.logListArgs(arg_0, arg_1).transact({'gas': 25000})
333+
txn_hash = strict_emitter.functions.logListArgs(arg_0, arg_1).transact()
334334
txn_receipt = wait_for_transaction(w3_strict_abi, txn_hash)
335335

336336
assert len(txn_receipt['logs']) == 1

‎web3/_utils/module_testing/emitter_contract.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@
816816
}
817817
]
818818

819+
819820
EMITTER_ENUM = {
820821
'LogAnonymous': 0,
821822
'LogNoArguments': 1,
@@ -828,5 +829,6 @@
828829
'LogDoubleAnonymous': 8,
829830
'LogDoubleWithIndex': 9,
830831
'LogTripleWithIndex': 10,
831-
'LogQuadrupleWithInde': 11,
832+
'LogQuadrupleWithIndex': 11,
833+
'LogStructArg': 12
832834
}

0 commit comments

Comments
 (0)