Skip to content

Commit

Permalink
Separate emitter contract into new and old versions
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
fselmo committed Jan 13, 2022
1 parent df0e3b0 commit 693592b
Show file tree
Hide file tree
Showing 5 changed files with 800 additions and 4 deletions.
20 changes: 18 additions & 2 deletions tests/core/contracts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
CONTRACT_EMITTER_CODE,
CONTRACT_EMITTER_RUNTIME,
)
from web3._utils.module_testing.emitter_contract_old import (
CONTRACT_EMITTER_ABI_OLD,
CONTRACT_EMITTER_CODE_OLD,
CONTRACT_EMITTER_RUNTIME_OLD,
)
from web3._utils.module_testing.event_contract import (
EVNT_CONTRACT_ABI,
EVNT_CONTRACT_CODE,
Expand Down Expand Up @@ -422,9 +427,20 @@ def EMITTER(EMITTER_CODE,


@pytest.fixture()
def StrictEmitter(w3_strict_abi, EMITTER):
def STRICT_EMITTER():
# Uses an older version of solidity to compile for strict bytes checking.
# See: https://github.com/ethereum/web3.py/issues/2301
return {
'bytecode': CONTRACT_EMITTER_CODE_OLD,
'bytecode_runtime': CONTRACT_EMITTER_RUNTIME_OLD,
'abi': CONTRACT_EMITTER_ABI_OLD,
}


@pytest.fixture()
def StrictEmitter(w3_strict_abi, STRICT_EMITTER):
w3 = w3_strict_abi
return w3.eth.contract(**EMITTER)
return w3.eth.contract(**STRICT_EMITTER)


@pytest.fixture()
Expand Down
106 changes: 106 additions & 0 deletions tests/core/contracts/contract_sources/Emitter_old.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// This older version of the Emitter contract can be use to keep the strict bytes test against it while we work on
// updating the strict bytes checking to be compatible with newer solidity versions.
// # See: https://github.com/ethereum/web3.py/issues/2301

pragma solidity ^0.4.21;


contract Emitter {
event LogAnonymous() anonymous;
event LogNoArguments();
event LogSingleArg(uint arg0);
event LogDoubleArg(uint arg0, uint arg1);
event LogTripleArg(uint arg0, uint arg1, uint arg2);
event LogQuadrupleArg(uint arg0, uint arg1, uint arg2, uint arg3);
event LogString(string v);
event LogBytes(bytes v);

// Indexed
event LogSingleWithIndex(uint indexed arg0);
event LogSingleAnonymous(uint indexed arg0) anonymous;
event LogDoubleWithIndex(uint arg0, uint indexed arg1);
event LogDoubleAnonymous(uint arg0, uint indexed arg1) anonymous;
event LogTripleWithIndex(uint arg0, uint indexed arg1, uint indexed arg2);
event LogQuadrupleWithIndex(uint arg0, uint arg1, uint indexed arg2, uint indexed arg3);
event LogDynamicArgs(string indexed arg0, string arg1);
event LogListArgs(bytes2[] indexed arg0, bytes2[] arg1);
event LogAddressIndexed(address indexed arg0, address arg1);
event LogAddressNotIndexed(address arg0, address arg1);

enum WhichEvent {
LogAnonymous,
LogNoArguments,
LogSingleArg,
LogDoubleArg,
LogTripleArg,
LogQuadrupleArg,
LogSingleAnonymous,
LogSingleWithIndex,
LogDoubleAnonymous,
LogDoubleWithIndex,
LogTripleWithIndex,
LogQuadrupleWithIndex,
LogBytes,
LogString,
LogDynamicArgs,
LogListArgs,
LogAddressIndexed,
LogAddressNotIndexed
}

function logNoArgs(WhichEvent which) public {
if (which == WhichEvent.LogNoArguments) emit LogNoArguments();
else if (which == WhichEvent.LogAnonymous) emit LogAnonymous();
else revert("Didn't match any allowable event index");
}

function logSingle(WhichEvent which, uint arg0) public {
if (which == WhichEvent.LogSingleArg) emit LogSingleArg(arg0);
else if (which == WhichEvent.LogSingleWithIndex) emit LogSingleWithIndex(arg0);
else if (which == WhichEvent.LogSingleAnonymous) emit LogSingleAnonymous(arg0);
else revert("Didn't match any allowable event index");
}

function logDouble(WhichEvent which, uint arg0, uint arg1) public {
if (which == WhichEvent.LogDoubleArg) emit LogDoubleArg(arg0, arg1);
else if (which == WhichEvent.LogDoubleWithIndex) emit LogDoubleWithIndex(arg0, arg1);
else if (which == WhichEvent.LogDoubleAnonymous) emit LogDoubleAnonymous(arg0, arg1);
else revert("Didn't match any allowable event index");
}

function logTriple(WhichEvent which, uint arg0, uint arg1, uint arg2) public {
if (which == WhichEvent.LogTripleArg) emit LogTripleArg(arg0, arg1, arg2);
else if (which == WhichEvent.LogTripleWithIndex) emit LogTripleWithIndex(arg0, arg1, arg2);
else revert("Didn't match any allowable event index");
}

function logQuadruple(WhichEvent which, uint arg0, uint arg1, uint arg2, uint arg3) public {
if (which == WhichEvent.LogQuadrupleArg) emit LogQuadrupleArg(arg0, arg1, arg2, arg3);
else if (which == WhichEvent.LogQuadrupleWithIndex) emit LogQuadrupleWithIndex(arg0, arg1, arg2, arg3);
else revert("Didn't match any allowable event index");
}

function logDynamicArgs(string arg0, string arg1) public {
emit LogDynamicArgs(arg0, arg1);
}

function logListArgs(bytes2[] arg0, bytes2[] arg1) public {
emit LogListArgs(arg0, arg1);
}

function logAddressIndexedArgs(address arg0, address arg1) public {
emit LogAddressIndexed(arg0, arg1);
}

function logAddressNotIndexedArgs(address arg0, address arg1) public {
emit LogAddressNotIndexed(arg0, arg1);
}

function logBytes(bytes v) public {
emit LogBytes(v);
}

function logString(string v) public {
emit LogString(v);
}
}
2 changes: 1 addition & 1 deletion tests/core/contracts/test_extracting_event_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def test_argument_extraction_strict_bytes_types(w3_strict_abi,
emitter_log_topics):
arg_0 = [b'12']
arg_1 = [b'12']
txn_hash = strict_emitter.functions.logListArgs(arg_0, arg_1).transact({'gas': 25000})
txn_hash = strict_emitter.functions.logListArgs(arg_0, arg_1).transact()
txn_receipt = wait_for_transaction(w3_strict_abi, txn_hash)

assert len(txn_receipt['logs']) == 1
Expand Down
4 changes: 3 additions & 1 deletion web3/_utils/module_testing/emitter_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@
}
]


EMITTER_ENUM = {
'LogAnonymous': 0,
'LogNoArguments': 1,
Expand All @@ -828,5 +829,6 @@
'LogDoubleAnonymous': 8,
'LogDoubleWithIndex': 9,
'LogTripleWithIndex': 10,
'LogQuadrupleWithInde': 11,
'LogQuadrupleWithIndex': 11,
'LogStructArg': 12
}
Loading

0 comments on commit 693592b

Please sign in to comment.