Skip to content

Commit 6d323e4

Browse files
committed
Change to compositional instead of OO
1 parent 2ea34fe commit 6d323e4

21 files changed

+574
-494
lines changed

conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def web3():
103103

104104

105105
@pytest.fixture
106-
def web3_strict_types():
106+
def w3_strict_abi():
107107
w3 = Web3(EthereumTesterProvider())
108108
w3.enable_strict_bytes_type_checking()
109109
return w3

docs/abi_types.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ All addresses must be supplied in one of three ways:
2828
Strict Bytes Type Checking
2929
--------------------------
3030

31+
.. note ::
32+
33+
In version 6, this will be the default behavior
34+
3135
There is a method on web3 that will enable stricter bytes type checking.
3236
The default is to allow Python strings, and to allow bytestrings less
3337
than the specified byte size. To enable stricter checks, use

docs/contracts.rst

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,12 @@ The following values will raise an error by default:
545545
- String is not valid hex
546546

547547
However, you may want to be stricter with acceptable values for bytes types.
548-
For this you can use the ``enable_strict_bytes_type_checking`` method,
548+
For this you can use the :meth:`w3.enable_strict_bytes_type_checking()` method,
549549
which is available on the web3 instance. A web3 instance which has had this method
550550
invoked will enforce a stricter set of rules on which values are accepted.
551551

552552
- A Python string that is not prefixed with ``0x`` will throw an error.
553-
- A bytestring that is less than (or greater than) the specified byte size
553+
- A bytestring whose length not exactly the specified byte size
554554
will raise an error.
555555

556556
.. list-table:: Valid byte and hex strings for a strict bytes4 type
@@ -586,13 +586,75 @@ invoked will enforce a stricter set of rules on which values are accepted.
586586
- Needs to have exactly 4 bytes
587587

588588

589-
For example, the following contract code will generate the abi below and some bytecode:
589+
Taking the following contract code as an example:
590590

591591
.. testsetup::
592592

593593
from web3 import Web3
594594
w3 = Web3(Web3.EthereumTesterProvider())
595595
bytecode = "608060405234801561001057600080fd5b506040516106103803806106108339810180604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185602082028301116401000000008211171561007e57600080fd5b5050929190505050806000908051906020019061009c9291906100a3565b505061019c565b82805482825590600052602060002090600f0160109004810192821561015a5791602002820160005b8382111561012a57835183826101000a81548161ffff02191690837e010000000000000000000000000000000000000000000000000000000000009004021790555092602001926002016020816001010492830192600103026100cc565b80156101585782816101000a81549061ffff021916905560020160208160010104928301926001030261012a565b505b509050610167919061016b565b5090565b61019991905b8082111561019557600081816101000a81549061ffff021916905550600101610171565b5090565b90565b610465806101ab6000396000f3fe608060405260043610610051576000357c0100000000000000000000000000000000000000000000000000000000900480633b3230ee14610056578063d7c8a410146100e7578063dfe3136814610153575b600080fd5b34801561006257600080fd5b5061008f6004803603602081101561007957600080fd5b8101908080359060200190929190505050610218565b60405180827dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff19167dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200191505060405180910390f35b3480156100f357600080fd5b506100fc61026c565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b8381101561013f578082015181840152602081019050610124565b505050509050019250505060405180910390f35b34801561015f57600080fd5b506102166004803603602081101561017657600080fd5b810190808035906020019064010000000081111561019357600080fd5b8201836020820111156101a557600080fd5b803590602001918460208302840111640100000000831117156101c757600080fd5b919080806020026020016040519081016040528093929190818152602001838360200280828437600081840152601f19601f820116905080830192505050505050509192919290505050610326565b005b60008181548110151561022757fe5b9060005260206000209060109182820401919006600202915054906101000a90047e010000000000000000000000000000000000000000000000000000000000000281565b6060600080548060200260200160405190810160405280929190818152602001828054801561031c57602002820191906000526020600020906000905b82829054906101000a90047e01000000000000000000000000000000000000000000000000000000000000027dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200190600201906020826001010492830192600103820291508084116102a95790505b5050505050905090565b806000908051906020019061033c929190610340565b5050565b82805482825590600052602060002090600f016010900481019282156103f75791602002820160005b838211156103c757835183826101000a81548161ffff02191690837e01000000000000000000000000000000000000000000000000000000000000900402179055509260200192600201602081600101049283019260010302610369565b80156103f55782816101000a81549061ffff02191690556002016020816001010492830192600103026103c7565b505b5090506104049190610408565b5090565b61043691905b8082111561043257600081816101000a81549061ffff02191690555060010161040e565b5090565b9056fea165627a7a72305820a8f9f1f4815c1eedfb8df31298a5cd13b198895de878871328b5d96296b69b4e0029"
596+
abi = '''
597+
[
598+
{
599+
"constant": true,
600+
"inputs": [
601+
{
602+
"name": "",
603+
"type": "uint256"
604+
}
605+
],
606+
"name": "bytes2Value",
607+
"outputs": [
608+
{
609+
"name": "",
610+
"type": "bytes2"
611+
}
612+
],
613+
"payable": false,
614+
"stateMutability": "view",
615+
"type": "function"
616+
},
617+
{
618+
"constant": true,
619+
"inputs": [],
620+
"name": "getBytes2Value",
621+
"outputs": [
622+
{
623+
"name": "",
624+
"type": "bytes2[]"
625+
}
626+
],
627+
"payable": false,
628+
"stateMutability": "view",
629+
"type": "function"
630+
},
631+
{
632+
"constant": false,
633+
"inputs": [
634+
{
635+
"name": "_bytes2Value",
636+
"type": "bytes2[]"
637+
}
638+
],
639+
"name": "setBytes2Value",
640+
"outputs": [],
641+
"payable": false,
642+
"stateMutability": "nonpayable",
643+
"type": "function"
644+
},
645+
{
646+
"inputs": [
647+
{
648+
"name": "_bytes2Value",
649+
"type": "bytes2[]"
650+
}
651+
],
652+
"payable": false,
653+
"stateMutability": "nonpayable",
654+
"type": "constructor"
655+
}
656+
]
657+
'''.strip()
596658

597659
.. code-block::
598660
@@ -614,72 +676,12 @@ For example, the following contract code will generate the abi below and some by
614676
... # }
615677
... # }
616678
617-
.. doctest::
618679
619-
>>> abi = '''
620-
... [
621-
... {
622-
... "constant": true,
623-
... "inputs": [
624-
... {
625-
... "name": "",
626-
... "type": "uint256"
627-
... }
628-
... ],
629-
... "name": "bytes2Value",
630-
... "outputs": [
631-
... {
632-
... "name": "",
633-
... "type": "bytes2"
634-
... }
635-
... ],
636-
... "payable": false,
637-
... "stateMutability": "view",
638-
... "type": "function"
639-
... },
640-
... {
641-
... "constant": true,
642-
... "inputs": [],
643-
... "name": "getBytes2Value",
644-
... "outputs": [
645-
... {
646-
... "name": "",
647-
... "type": "bytes2[]"
648-
... }
649-
... ],
650-
... "payable": false,
651-
... "stateMutability": "view",
652-
... "type": "function"
653-
... },
654-
... {
655-
... "constant": false,
656-
... "inputs": [
657-
... {
658-
... "name": "_bytes2Value",
659-
... "type": "bytes2[]"
660-
... }
661-
... ],
662-
... "name": "setBytes2Value",
663-
... "outputs": [],
664-
... "payable": false,
665-
... "stateMutability": "nonpayable",
666-
... "type": "function"
667-
... },
668-
... {
669-
... "inputs": [
670-
... {
671-
... "name": "_bytes2Value",
672-
... "type": "bytes2[]"
673-
... }
674-
... ],
675-
... "payable": false,
676-
... "stateMutability": "nonpayable",
677-
... "type": "constructor"
678-
... }
679-
... ]
680-
... '''.strip()
680+
>>> # abi = "..."
681681
>>> # bytecode = "6080..."
682682
683+
.. doctest::
684+
683685
>>> ArraysContract = w3.eth.contract(abi=abi, bytecode=bytecode)
684686

685687
>>> tx_hash = ArraysContract.constructor([b'b']).transact()

docs/examples.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,19 @@ Given the following solidity source file stored at ``contract.sol``.
202202
.. code-block:: javascript
203203
204204
contract StoreVar {
205-
205+
206206
uint8 public _myVar;
207207
event MyEvent(uint indexed _var);
208-
208+
209209
function setVar(uint8 _var) public {
210210
_myVar = _var;
211211
MyEvent(_var);
212212
}
213-
213+
214214
function getVar() public view returns (uint8) {
215215
return _myVar;
216216
}
217-
217+
218218
}
219219
220220
The following example demonstrates a few things:
@@ -229,45 +229,45 @@ The following example demonstrates a few things:
229229
import sys
230230
import time
231231
import pprint
232-
232+
233233
from web3.providers.eth_tester import EthereumTesterProvider
234234
from web3 import Web3
235235
from solc import compile_source
236-
236+
237237
238238
def compile_source_file(file_path):
239239
with open(file_path, 'r') as f:
240240
source = f.read()
241-
241+
242242
return compile_source(source)
243-
244-
243+
244+
245245
def deploy_contract(w3, contract_interface):
246246
tx_hash = w3.eth.contract(
247247
abi=contract_interface['abi'],
248248
bytecode=contract_interface['bin']).deploy()
249-
249+
250250
address = w3.eth.getTransactionReceipt(tx_hash)['contractAddress']
251251
return address
252-
253-
252+
253+
254254
w3 = Web3(EthereumTesterProvider())
255-
255+
256256
contract_source_path = 'contract.sol'
257257
compiled_sol = compile_source_file('contract.sol')
258-
258+
259259
contract_id, contract_interface = compiled_sol.popitem()
260-
260+
261261
address = deploy_contract(w3, contract_interface)
262262
print("Deployed {0} to: {1}\n".format(contract_id, address))
263-
263+
264264
store_var_contract = w3.eth.contract(
265265
address=address,
266266
abi=contract_interface['abi'])
267-
267+
268268
gas_estimate = store_var_contract.functions.setVar(255).estimateGas()
269269
print("Gas estimate to transact with setVar: {0}\n".format(gas_estimate))
270-
270+
271271
if gas_estimate < 100000:
272272
print("Sending transaction to setVar(255)\n")
273273
tx_hash = store_var_contract.functions.setVar(255).transact()

docs/overview.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,38 @@ Cryptographic Hashing
341341
>>> Web3.soliditySha3(['address'], ["ethereumfoundation.eth"])
342342
HexBytes("0x913c99ea930c78868f1535d34cd705ab85929b2eaaf70fcd09677ecd6e5d75e9")
343343
344+
Check Encodability
345+
~~~~~~~~~~~~~~~~~~~~
346+
347+
.. py:method:: w3.is_encodable(_type, value)
348+
349+
Returns ``True`` if a value can be encoded as the given type. Otherwise returns ``False``.
350+
351+
.. code-block:: python
352+
353+
>>> from web3.auto.gethdev import w3
354+
>>> w3.is_encodable('bytes2', b'12')
355+
True
356+
>>> w3.is_encodable('bytes2', b'1')
357+
True
358+
>>> w3.is_encodable('bytes2', '0x1234')
359+
True
360+
>>> w3.is_encodable('bytes2', b'123')
361+
False
362+
363+
.. py:method:: w3.enable_strict_bytes_type_checking()
364+
365+
Enables stricter bytes type checking. For more examples see :ref:`enable-strict-byte-check`
366+
367+
.. doctest::
368+
369+
>>> from web3.auto.gethdev import w3
370+
>>> w3.enable_strict_bytes_type_checking()
371+
>>> w3.is_encodable('bytes2', b'12')
372+
True
373+
>>> w3.is_encodable('bytes2', b'1')
374+
False
375+
344376
Modules
345377
-------
346378

tests/core/contracts/conftest.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ def WithConstructorArgumentsContract(web3,
215215

216216

217217
@pytest.fixture()
218-
def WithConstructorArgumentsContractStrict(web3_strict_types,
218+
def WithConstructorArgumentsContractStrict(w3_strict_abi,
219219
WITH_CONSTRUCTOR_ARGUMENTS_CODE,
220220
WITH_CONSTRUCTOR_ARGUMENTS_RUNTIME,
221221
WITH_CONSTRUCTOR_ARGUMENTS_ABI):
222-
return web3_strict_types.eth.contract(
222+
return w3_strict_abi.eth.contract(
223223
abi=WITH_CONSTRUCTOR_ARGUMENTS_ABI,
224224
bytecode=WITH_CONSTRUCTOR_ARGUMENTS_CODE,
225225
bytecode_runtime=WITH_CONSTRUCTOR_ARGUMENTS_RUNTIME,
@@ -402,27 +402,27 @@ def EMITTER(EMITTER_CODE,
402402

403403

404404
@pytest.fixture()
405-
def StrictEmitter(web3_strict_types, EMITTER):
406-
web3 = web3_strict_types
407-
return web3.eth.contract(**EMITTER)
405+
def StrictEmitter(w3_strict_abi, EMITTER):
406+
w3 = w3_strict_abi
407+
return w3.eth.contract(**EMITTER)
408408

409409

410410
@pytest.fixture()
411-
def strict_emitter(web3_strict_types,
411+
def strict_emitter(w3_strict_abi,
412412
StrictEmitter,
413413
wait_for_transaction,
414414
wait_for_block,
415415
address_conversion_func):
416-
web3 = web3_strict_types
416+
w3 = w3_strict_abi
417417

418-
wait_for_block(web3)
418+
wait_for_block(w3)
419419
deploy_txn_hash = StrictEmitter.constructor().transact(
420-
{'from': web3.eth.coinbase, 'gas': 1000000}
420+
{'from': w3.eth.coinbase, 'gas': 1000000}
421421
)
422-
deploy_receipt = wait_for_transaction(web3, deploy_txn_hash)
422+
deploy_receipt = wait_for_transaction(w3, deploy_txn_hash)
423423
contract_address = address_conversion_func(deploy_receipt['contractAddress'])
424424

425-
bytecode = web3.eth.getCode(contract_address)
425+
bytecode = w3.eth.getCode(contract_address)
426426
assert bytecode == StrictEmitter.bytecode_runtime
427427
emitter_contract = StrictEmitter(address=contract_address)
428428
assert emitter_contract.address == contract_address
@@ -625,8 +625,8 @@ def ArraysContract(web3, ARRAYS_CONTRACT):
625625

626626

627627
@pytest.fixture()
628-
def StrictArraysContract(web3_strict_types, ARRAYS_CONTRACT):
629-
return web3_strict_types.eth.contract(**ARRAYS_CONTRACT)
628+
def StrictArraysContract(w3_strict_abi, ARRAYS_CONTRACT):
629+
return w3_strict_abi.eth.contract(**ARRAYS_CONTRACT)
630630

631631

632632
CONTRACT_PAYABLE_TESTER_SOURCE = """

tests/core/contracts/test_contract_call_interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ def arrays_contract(web3, ArraysContract, address_conversion_func):
7575

7676

7777
@pytest.fixture()
78-
def strict_arrays_contract(web3_strict_types, StrictArraysContract, address_conversion_func):
78+
def strict_arrays_contract(w3_strict_abi, StrictArraysContract, address_conversion_func):
7979
# bytes_32 = [keccak('0'), keccak('1')]
8080
bytes32_array = [
8181
b'\x04HR\xb2\xa6p\xad\xe5@~x\xfb(c\xc5\x1d\xe9\xfc\xb9eB\xa0q\x86\xfe:\xed\xa6\xbb\x8a\x11m', # noqa: E501
8282
b'\xc8\x9e\xfd\xaaT\xc0\xf2\x0cz\xdfa(\x82\xdf\tP\xf5\xa9Qc~\x03\x07\xcd\xcbLg/)\x8b\x8b\xc6', # noqa: E501
8383
]
8484
byte_arr = [b'\xff', b'\xff', b'\xff', b'\xff']
8585
return deploy(
86-
web3_strict_types,
86+
w3_strict_abi,
8787
StrictArraysContract,
8888
address_conversion_func,
8989
args=[bytes32_array, byte_arr]

0 commit comments

Comments
 (0)