@@ -444,100 +444,76 @@ and the arguments are ambiguous.
444
444
1
445
445
446
446
447
- .. _ enable -strict-byte-check :
447
+ .. _ disable -strict-byte-check :
448
448
449
- Enabling Strict Checks for Bytes Types
450
- --------------------------------------
449
+ Disabling Strict Checks for Bytes Types
450
+ ---------------------------------------
451
451
452
- By default, web3 is not very strict when it comes to hex and bytes values.
453
- A bytes type will take a hex string, a bytestring, or a regular python
454
- string that can be decoded as a hex.
455
- Additionally, if an abi specifies a byte size, but the value that gets
456
- passed in is less than the specified size, web3 will automatically pad the value.
457
- For example, if an abi specifies a type of ``bytes4 ``, web3 will handle all of the following values:
452
+ By default, web3 is strict when it comes to hex and bytes values, as of ``v6 ``.
453
+ If an abi specifies a byte size, but the value that gets passed in is not the specified
454
+ size, web3 will invalidate the value. For example, if an abi specifies a type of
455
+ ``bytes4 ``, web3 will invalidate the following values:
458
456
459
- .. list-table :: Valid byte and hex strings for a bytes4 type
460
- :widths: 25 75
461
- :header-rows: 1
462
-
463
- * - Input
464
- - Normalizes to
465
- * - ``'' ``
466
- - ``b'\x00\x00\x00\x00' ``
467
- * - ``'0x' ``
468
- - ``b'\x00\x00\x00\x00' ``
469
- * - ``b'' ``
470
- - ``b'\x00\x00\x00\x00' ``
471
- * - ``b'ab' ``
472
- - ``b'ab\x00\x00' ``
473
- * - ``'0xab' ``
474
- - ``b'\xab\x00\x00\x00' ``
475
- * - ``'1234' ``
476
- - ``b'\x124\x00\x00' ``
477
- * - ``'0x61626364' ``
478
- - ``b'abcd' ``
479
- * - ``'1234' ``
480
- - ``b'1234' ``
481
-
482
-
483
- The following values will raise an error by default:
484
-
485
- .. list-table :: Invalid byte and hex strings for a bytes4 type
457
+ .. list-table :: Invalid byte and hex strings with strict (default) bytes4 type checking
486
458
:widths: 25 75
487
459
:header-rows: 1
488
460
489
461
* - Input
490
462
- Reason
491
- * - ``b'abcde' ``
492
- - Bytestring with more than 4 bytes
493
- * - ``'0x6162636423' ``
494
- - Hex string with more than 4 bytes
463
+ * - ``'' ``
464
+ - Needs to be prefixed with a "0x" to be interpreted as an empty hex string
495
465
* - ``2 ``
496
466
- Wrong type
497
467
* - ``'ah' ``
498
468
- String is not valid hex
469
+ * - ``'1234' ``
470
+ - Needs to either be a bytestring (b'1234') or be a hex value of the right size, prefixed with 0x (in this case: '0x31323334')
471
+ * - ``b'' ``
472
+ - Needs to have exactly 4 bytes
473
+ * - ``b'ab' ``
474
+ - Needs to have exactly 4 bytes
475
+ * - ``'0xab' ``
476
+ - Needs to have exactly 4 bytes
477
+ * - ``'0x6162636464' ``
478
+ - Needs to have exactly 4 bytes
479
+
499
480
500
- However, you may want to be stricter with acceptable values for bytes types.
501
- For this you can use the :meth: `w3.enable_strict_bytes_type_checking() ` method,
502
- which is available on the web3 instance. A web3 instance which has had this method
503
- invoked will enforce a stricter set of rules on which values are accepted.
481
+ However, you may want to be less strict with acceptable values for bytes types.
482
+ This may prove useful if you trust that values coming through are what they are
483
+ meant to be with respects to the respective ABI. In this case, the automatic padding
484
+ might be convenient for inferred types. For this, you can set the
485
+ :meth: `w3.strict_bytes_type_checking ` flag to ``False ``, which is available on the
486
+ Web3 instance. A Web3 instance which has this flag set to ``False `` will have a less
487
+ strict set of rules on which values are accepted. A ``bytes `` type will allow values as
488
+ a hex string, a bytestring, or a regular Python string that can be decoded as a hex.
489
+ 0x-prefixed hex strings are also not required.
504
490
505
- - A Python string that is not prefixed with ``0x `` will throw an error.
506
- - A bytestring whose length is not exactly the specified byte size
507
- will raise an error.
491
+ - A Python string that is not prefixed with ``0x `` is valid.
492
+ - A bytestring whose length is less than the specified byte size is valid.
508
493
509
- .. list-table :: Valid byte and hex strings for a strict bytes4 type
494
+ .. list-table :: Valid byte and hex strings for a non- strict bytes4 type
510
495
:widths: 25 75
511
496
:header-rows: 1
512
497
513
498
* - Input
514
499
- Normalizes to
500
+ * - ``'' ``
501
+ - ``b'\x00\x00\x00\x00' ``
515
502
* - ``'0x' ``
516
503
- ``b'\x00\x00\x00\x00' ``
504
+ * - ``b'' ``
505
+ - ``b'\x00\x00\x00\x00' ``
506
+ * - ``b'ab' ``
507
+ - ``b'ab\x00\x00' ``
508
+ * - ``'0xab' ``
509
+ - ``b'\xab\x00\x00\x00' ``
510
+ * - ``'1234' ``
511
+ - ``b'\x124\x00\x00' ``
517
512
* - ``'0x61626364' ``
518
513
- ``b'abcd' ``
519
514
* - ``'1234' ``
520
515
- ``b'1234' ``
521
516
522
- .. list-table :: Invalid byte and hex strings with strict bytes4 type checking
523
- :widths: 25 75
524
- :header-rows: 1
525
-
526
- * - Input
527
- - Reason
528
- * - ``'' ``
529
- - Needs to be prefixed with a "0x" to be interpreted as an empty hex string
530
- * - ``'1234' ``
531
- - Needs to either be a bytestring (b'1234') or be a hex value of the right size, prefixed with 0x (in this case: '0x31323334')
532
- * - ``b'' ``
533
- - Needs to have exactly 4 bytes
534
- * - ``b'ab' ``
535
- - Needs to have exactly 4 bytes
536
- * - ``'0xab' ``
537
- - Needs to have exactly 4 bytes
538
- * - ``'0x6162636464' ``
539
- - Needs to have exactly 4 bytes
540
-
541
517
542
518
Taking the following contract code as an example:
543
519
@@ -636,69 +612,53 @@ Taking the following contract code as an example:
636
612
637
613
>>> ArraysContract = w3.eth.contract(abi = abi, bytecode = bytecode)
638
614
639
- >>> tx_hash = ArraysContract.constructor([b ' b' ]).transact()
640
- >>> tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
641
-
642
- >>> array_contract = w3.eth.contract(
643
- ... address= tx_receipt.contractAddress,
644
- ... abi= abi
645
- ... )
646
-
647
- >>> array_contract.functions.getBytes2Value().call()
648
- [b'b\x00']
649
- >>> array_contract.functions.setBytes2Value([b ' a' ]).transact({' gas' : 420000 , ' gasPrice' : Web3.to_wei(1 , ' gwei' )})
650
- HexBytes('0xc5377ba25224bd763ceedc0ee455cc14fc57b23dbc6b6409f40a557a009ff5f4')
651
- >>> array_contract.functions.getBytes2Value().call()
652
- [b'a\x00']
653
- >>> w3.disable_strict_bytes_type_checking()
654
- >>> array_contract.functions.setBytes2Value([b ' a' ]).transact()
655
- Traceback (most recent call last):
656
- ...
657
- ValidationError:
658
- Could not identify the intended function with name
659
-
660
- >>> tx_hash = ArraysContract.constructor([b ' b' ]).transact()
661
- >>> tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
662
-
663
- >>> array_contract = w3.eth.contract(
664
- ... address= tx_receipt.contractAddress,
665
- ... abi= abi
666
- ... )
667
-
668
- >>> array_contract.functions.getBytes2Value().call()
669
- [b'b\x00']
670
- >>> array_contract.functions.setBytes2Value([b ' a' ]).transact({' gas' : 420000 , ' gasPrice' : Web3.to_wei(1 , ' gwei' )})
671
- HexBytes('0xc5377ba25224bd763ceedc0ee455cc14fc57b23dbc6b6409f40a557a009ff5f4')
672
- >>> array_contract.functions.getBytes2Value().call()
673
- [b'a\x00']
674
- >>> w3.disable_strict_bytes_type_checking()
675
- >>> array_contract.functions.setBytes2Value([b ' a' ]).transact()
676
- Traceback (most recent call last):
677
- ...
678
- ValidationError:
679
- Could not identify the intended function with name
615
+ >>> tx_hash = ArraysContract.constructor([b ' bb' ]).transact()
616
+ >>> tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
617
+ >>> array_contract = w3.eth.contract(
618
+ ... address= tx_receipt.contractAddress,
619
+ ... abi= abi
620
+ ... )
621
+ >>> array_contract.functions.getBytes2Value().call()
622
+ [b'bb']
623
+
624
+ >>> # set value with appropriate byte size
625
+ >>> array_contract.functions.setBytes2Value([b ' aa' ]).transact({' gas' : 420000 , " maxPriorityFeePerGas" : 10 ** 9 , " maxFeePerGas" : 10 ** 9 })
626
+ HexBytes('0xcb95151142ea56dbf2753d70388aef202a7bb5a1e323d448bc19f1d2e1fe3dc9')
627
+ >>> # check value
628
+ >>> array_contract.functions.getBytes2Value().call()
629
+ [b'aa']
630
+
631
+ >>> # trying to set value without appropriate size (bytes2) is not valid
632
+ >>> array_contract.functions.setBytes2Value([b ' b' ]).transact()
633
+ Traceback (most recent call last):
634
+ ...
635
+ web3.exceptions.Web3ValidationError:
636
+ Could not identify the intended function with name
637
+ >>> # check value is still b'aa'
638
+ >>> array_contract.functions.getBytes2Value().call()
639
+ [b'aa']
640
+
641
+ >>> # disabling strict byte checking...
642
+ >>> w3.strict_bytes_type_checking = False
680
643
681
644
>>> tx_hash = ArraysContract.constructor([b ' b' ]).transact()
682
645
>>> tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
683
-
684
646
>>> array_contract = w3.eth.contract(
685
647
... address= tx_receipt.contractAddress,
686
648
... abi= abi
687
649
... )
688
-
650
+ >>> # check value is zero-padded... i.e. b'b\x00'
689
651
>>> array_contract.functions.getBytes2Value().call()
690
652
[b'b\x00']
691
- >>> array_contract.functions.setBytes2Value([b ' a' ]).transact({' gas' : 420000 , ' gasPrice' : Web3.to_wei(1 , ' gwei' )})
692
- HexBytes('0xc5377ba25224bd763ceedc0ee455cc14fc57b23dbc6b6409f40a557a009ff5f4')
693
- >>> array_contract.functions.getBytes2Value().call()
694
- [b'a\x00']
695
- >>> w3.enable_strict_bytes_type_checking()
653
+
654
+ >>> # set the flag back to True
655
+ >>> w3.strict_bytes_type_checking = True
656
+
696
657
>>> array_contract.functions.setBytes2Value([b ' a' ]).transact()
697
658
Traceback (most recent call last):
698
659
...
699
- Web3ValidationError:
700
- Could not identify the intended function with name `setBytes2Value`
701
-
660
+ web3.exceptions.Web3ValidationError:
661
+ Could not identify the intended function with name
702
662
703
663
.. _contract-functions :
704
664
0 commit comments