2
2
import pytest
3
3
4
4
from ethereum .utils import (
5
- big_endian_to_int , int_to_big_endian , rzpad , sha3 , zpad ,
5
+ big_endian_to_int , int_to_big_endian , decode_hex , encode_hex , rzpad , sha3 , zpad ,
6
6
)
7
7
from ethereum .abi import (
8
8
_canonical_type , decode_abi , decode_single , decint , encode_abi ,
@@ -37,10 +37,10 @@ def test_canonical_types():
37
37
38
38
def test_function_selector ():
39
39
# https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#function-selector-and-argument-encoding
40
- baz_selector = big_endian_to_int ('CDCD77C0' . decode ( 'hex ' ))
41
- bar_selector = big_endian_to_int ('AB55044D' . decode ( 'hex ' ))
42
- sam_selector = big_endian_to_int ('A5643BF2' . decode ( 'hex ' ))
43
- f_selector = big_endian_to_int ('8BE65246' . decode ( 'hex ' ))
40
+ baz_selector = big_endian_to_int (decode_hex ( 'CDCD77C0 ' ))
41
+ bar_selector = big_endian_to_int (decode_hex ( 'AB55044D ' ))
42
+ sam_selector = big_endian_to_int (decode_hex ( 'A5643BF2 ' ))
43
+ f_selector = big_endian_to_int (decode_hex ( '8BE65246 ' ))
44
44
45
45
assert big_endian_to_int (sha3 ('baz(uint32,bool)' )[:4 ]) == baz_selector
46
46
assert big_endian_to_int (sha3 ('bar(fixed128x128[2])' )[:4 ]) == bar_selector
@@ -79,7 +79,7 @@ def test_event():
79
79
80
80
result = contract_abi .decode_event (topics , data )
81
81
82
- assert result ['_event_type' ] == 'Test'
82
+ assert result ['_event_type' ] == b 'Test'
83
83
assert result ['a' ] == 1
84
84
assert result ['b' ] == 2
85
85
@@ -126,55 +126,55 @@ def test_encode_int():
126
126
int256 = ('int' , '256' , [])
127
127
128
128
int256_maximum = (
129
- '\x7f \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
130
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
129
+ b '\x7f \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
130
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
131
131
)
132
132
int256_minimum = (
133
- '\x80 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
134
- '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
133
+ b '\x80 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
134
+ b '\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
135
135
)
136
136
int256_128 = (
137
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
138
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \x80 '
137
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
138
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \x80 '
139
139
)
140
140
int256_2_to_31 = (
141
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
142
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \x80 \x00 \x00 \x00 '
141
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
142
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \x80 \x00 \x00 \x00 '
143
143
)
144
144
int256_negative_one = (
145
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
146
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
145
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
146
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
147
147
)
148
148
149
149
assert encode_single (int256 , int256_minimum ) == int256_minimum
150
150
151
- assert encode_single (int8 , 0 ) == zpad ('\x00 ' , 32 )
152
- assert encode_single (int8 , 2 ** 7 - 1 ) == zpad ('\x7f ' , 32 )
153
- assert encode_single (int8 , - 1 ) == zpad ('\xff ' , 32 )
154
- assert encode_single (int8 , - 2 ** 7 ) == zpad ('\x80 ' , 32 )
151
+ assert encode_single (int8 , 0 ) == zpad (b '\x00 ' , 32 )
152
+ assert encode_single (int8 , 2 ** 7 - 1 ) == zpad (b '\x7f ' , 32 )
153
+ assert encode_single (int8 , - 1 ) == zpad (b '\xff ' , 32 )
154
+ assert encode_single (int8 , - 2 ** 7 ) == zpad (b '\x80 ' , 32 )
155
155
156
156
with pytest .raises (ValueOutOfBounds ):
157
157
encode_single (int8 , 128 )
158
158
159
159
with pytest .raises (ValueOutOfBounds ):
160
160
encode_single (int8 , - 129 )
161
161
162
- assert encode_single (int32 , 0 ) == zpad ('\x00 ' , 32 )
163
- assert encode_single (int32 , 2 ** 7 - 1 ) == zpad ('\x7f ' , 32 )
164
- assert encode_single (int32 , 2 ** 31 - 1 ) == zpad ('\x7f \xff \xff \xff ' , 32 )
165
- assert encode_single (int32 , - 1 ) == zpad ('\xff \xff \xff \xff ' , 32 )
166
- assert encode_single (int32 , - 2 ** 7 ) == zpad ('\xff \xff \xff \x80 ' , 32 )
167
- assert encode_single (int32 , - 2 ** 31 ) == zpad ('\x80 \x00 \x00 \x00 ' , 32 )
162
+ assert encode_single (int32 , 0 ) == zpad (b '\x00 ' , 32 )
163
+ assert encode_single (int32 , 2 ** 7 - 1 ) == zpad (b '\x7f ' , 32 )
164
+ assert encode_single (int32 , 2 ** 31 - 1 ) == zpad (b '\x7f \xff \xff \xff ' , 32 )
165
+ assert encode_single (int32 , - 1 ) == zpad (b '\xff \xff \xff \xff ' , 32 )
166
+ assert encode_single (int32 , - 2 ** 7 ) == zpad (b '\xff \xff \xff \x80 ' , 32 )
167
+ assert encode_single (int32 , - 2 ** 31 ) == zpad (b '\x80 \x00 \x00 \x00 ' , 32 )
168
168
169
169
with pytest .raises (ValueOutOfBounds ):
170
170
encode_single (int32 , 2 ** 32 )
171
171
172
172
with pytest .raises (ValueOutOfBounds ):
173
173
encode_single (int32 , - (2 ** 32 ))
174
174
175
- assert encode_single (int256 , 0 ) == zpad ('\x00 ' , 32 )
176
- assert encode_single (int256 , 2 ** 7 - 1 ) == zpad ('\x7f ' , 32 )
177
- assert encode_single (int256 , 2 ** 31 - 1 ) == zpad ('\x7f \xff \xff \xff ' , 32 )
175
+ assert encode_single (int256 , 0 ) == zpad (b '\x00 ' , 32 )
176
+ assert encode_single (int256 , 2 ** 7 - 1 ) == zpad (b '\x7f ' , 32 )
177
+ assert encode_single (int256 , 2 ** 31 - 1 ) == zpad (b '\x7f \xff \xff \xff ' , 32 )
178
178
assert encode_single (int256 , 2 ** 255 - 1 ) == int256_maximum
179
179
assert encode_single (int256 , - 1 ) == int256_negative_one
180
180
assert encode_single (int256 , - 2 ** 7 ) == int256_128
@@ -202,24 +202,24 @@ def test_encode_uint():
202
202
with pytest .raises (ValueOutOfBounds ):
203
203
encode_single (uint256 , - 1 )
204
204
205
- assert encode_single (uint8 , 0 ) == zpad ('\x00 ' , 32 )
206
- assert encode_single (uint32 , 0 ) == zpad ('\x00 ' , 32 )
207
- assert encode_single (uint256 , 0 ) == zpad ('\x00 ' , 32 )
205
+ assert encode_single (uint8 , 0 ) == zpad (b '\x00 ' , 32 )
206
+ assert encode_single (uint32 , 0 ) == zpad (b '\x00 ' , 32 )
207
+ assert encode_single (uint256 , 0 ) == zpad (b '\x00 ' , 32 )
208
208
209
- assert encode_single (uint8 , 1 ) == zpad ('\x01 ' , 32 )
210
- assert encode_single (uint32 , 1 ) == zpad ('\x01 ' , 32 )
211
- assert encode_single (uint256 , 1 ) == zpad ('\x01 ' , 32 )
209
+ assert encode_single (uint8 , 1 ) == zpad (b '\x01 ' , 32 )
210
+ assert encode_single (uint32 , 1 ) == zpad (b '\x01 ' , 32 )
211
+ assert encode_single (uint256 , 1 ) == zpad (b '\x01 ' , 32 )
212
212
213
- assert encode_single (uint8 , 2 ** 8 - 1 ) == zpad ('\xff ' , 32 )
214
- assert encode_single (uint32 , 2 ** 8 - 1 ) == zpad ('\xff ' , 32 )
215
- assert encode_single (uint256 , 2 ** 8 - 1 ) == zpad ('\xff ' , 32 )
213
+ assert encode_single (uint8 , 2 ** 8 - 1 ) == zpad (b '\xff ' , 32 )
214
+ assert encode_single (uint32 , 2 ** 8 - 1 ) == zpad (b '\xff ' , 32 )
215
+ assert encode_single (uint256 , 2 ** 8 - 1 ) == zpad (b '\xff ' , 32 )
216
216
217
- assert encode_single (uint32 , 2 ** 32 - 1 ) == zpad ('\xff \xff \xff \xff ' , 32 )
218
- assert encode_single (uint256 , 2 ** 32 - 1 ) == zpad ('\xff \xff \xff \xff ' , 32 )
217
+ assert encode_single (uint32 , 2 ** 32 - 1 ) == zpad (b '\xff \xff \xff \xff ' , 32 )
218
+ assert encode_single (uint256 , 2 ** 32 - 1 ) == zpad (b '\xff \xff \xff \xff ' , 32 )
219
219
220
220
uint256_maximum = (
221
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
222
- '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
221
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
222
+ b '\xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff \xff '
223
223
)
224
224
assert encode_single (uint256 , 2 ** 256 - 1 ) == uint256_maximum
225
225
@@ -228,8 +228,8 @@ def test_encode_bool():
228
228
bool_ = ('bool' , '' , [])
229
229
uint8 = ('uint' , '8' , [])
230
230
231
- assert encode_single (bool_ , True ) == zpad ('\x01 ' , 32 )
232
- assert encode_single (bool_ , False ) == zpad ('\x00 ' , 32 )
231
+ assert encode_single (bool_ , True ) == zpad (b '\x01 ' , 32 )
232
+ assert encode_single (bool_ , False ) == zpad (b '\x00 ' , 32 )
233
233
234
234
assert encode_single (bool_ , True ) == encode_single (uint8 , 1 )
235
235
assert encode_single (bool_ , False ) == encode_single (uint8 , 0 )
@@ -238,21 +238,21 @@ def test_encode_bool():
238
238
def test_encode_fixed ():
239
239
fixed128x128 = ('fixed' , '128x128' , [])
240
240
241
- _2_125 = (
242
- '00000000000000000000000000000002'
243
- '20000000000000000000000000000000'
244
- ). decode ( 'hex' )
241
+ _2_125 = decode_hex (
242
+ b '00000000000000000000000000000002'
243
+ b '20000000000000000000000000000000'
244
+ )
245
245
246
- _8_5 = (
247
- '00000000000000000000000000000008'
248
- '80000000000000000000000000000000'
249
- ). decode ( 'hex' )
246
+ _8_5 = decode_hex (
247
+ b '00000000000000000000000000000008'
248
+ b '80000000000000000000000000000000'
249
+ )
250
250
251
251
assert encode_single (fixed128x128 , 2.125 ) == _2_125
252
252
assert encode_single (fixed128x128 , 8.5 ) == _8_5
253
253
254
- assert encode_single (fixed128x128 , 1.125 ) == (b'\x00 ' * 15 + b'\x01 \x20 ' + b'\x00 ' * 15 )
255
- assert encode_single (fixed128x128 , - 1.125 ) == (b'\xff ' * 15 + b'\xfe ' + b'\xe0 ' + b'\x00 ' * 15 )
254
+ assert encode_single (fixed128x128 , 1.125 ) == (b'\x00 ' * 15 + b'\x01 \x20 ' + b'\x00 ' * 15 )
255
+ assert encode_single (fixed128x128 , - 1.125 ) == (b'\xff ' * 15 + b'\xfe ' + b'\xe0 ' + b'\x00 ' * 15 )
256
256
257
257
with pytest .raises (ValueOutOfBounds ):
258
258
encode_single (fixed128x128 , 2 ** 127 )
@@ -264,22 +264,22 @@ def test_encode_fixed():
264
264
def test_encoded_ufixed ():
265
265
ufixed128x128 = ('ufixed' , '128x128' , [])
266
266
267
- _2_125 = (
268
- '00000000000000000000000000000002'
269
- '20000000000000000000000000000000'
270
- ). decode ( 'hex' )
267
+ _2_125 = decode_hex (
268
+ b '00000000000000000000000000000002'
269
+ b '20000000000000000000000000000000'
270
+ )
271
271
272
- _8_5 = (
273
- '00000000000000000000000000000008'
274
- '80000000000000000000000000000000'
275
- ). decode ( 'hex' )
272
+ _8_5 = decode_hex (
273
+ b '00000000000000000000000000000008'
274
+ b '80000000000000000000000000000000'
275
+ )
276
276
277
277
assert encode_single (ufixed128x128 , 2.125 ) == _2_125
278
278
assert encode_single (ufixed128x128 , 8.5 ) == _8_5
279
279
280
- assert encode_single (ufixed128x128 , 0 ) == (b'\x00 ' * 32 )
281
- assert encode_single (ufixed128x128 , 1.125 ) == (b'\x00 ' * 15 + b'\x01 \x20 ' + '\x00 ' * 15 )
282
- assert encode_single (ufixed128x128 , 2 ** 127 - 1 ) == (b'\x7f ' + b'\xff ' * 15 + b'\x00 ' * 16 )
280
+ assert encode_single (ufixed128x128 , 0 ) == (b'\x00 ' * 32 )
281
+ assert encode_single (ufixed128x128 , 1.125 ) == (b'\x00 ' * 15 + b'\x01 \x20 ' + b '\x00 ' * 15 )
282
+ assert encode_single (ufixed128x128 , 2 ** 127 - 1 ) == (b'\x7f ' + b'\xff ' * 15 + b'\x00 ' * 16 )
283
283
284
284
with pytest .raises (ValueOutOfBounds ):
285
285
encode_single (ufixed128x128 , 2 ** 128 + 1 )
@@ -293,18 +293,18 @@ def test_encode_dynamic_bytes():
293
293
uint256 = ('uint' , '256' , [])
294
294
295
295
# only the size of the bytes
296
- assert encode_single (dynamic_bytes , '' ) == zpad ('\x00 ' , 32 )
296
+ assert encode_single (dynamic_bytes , b '' ) == zpad (b '\x00 ' , 32 )
297
297
298
- a = encode_single (uint256 , 1 ) + rzpad ('\x61 ' , 32 )
299
- assert encode_single (dynamic_bytes , '\x61 ' ) == a
298
+ a = encode_single (uint256 , 1 ) + rzpad (b '\x61 ' , 32 )
299
+ assert encode_single (dynamic_bytes , b '\x61 ' ) == a
300
300
301
- dave_bin = (
302
- '0000000000000000000000000000000000000000000000000000000000000004'
303
- '6461766500000000000000000000000000000000000000000000000000000000'
304
- ). decode ( 'hex' )
305
- dave = encode_single (uint256 , 4 ) + rzpad ('\x64 \x61 \x76 \x65 ' , 32 )
306
- assert encode_single (dynamic_bytes , '\x64 \x61 \x76 \x65 ' ) == dave_bin
307
- assert encode_single (dynamic_bytes , '\x64 \x61 \x76 \x65 ' ) == dave
301
+ dave_bin = decode_hex (
302
+ b '0000000000000000000000000000000000000000000000000000000000000004'
303
+ b '6461766500000000000000000000000000000000000000000000000000000000'
304
+ )
305
+ dave = encode_single (uint256 , 4 ) + rzpad (b '\x64 \x61 \x76 \x65 ' , 32 )
306
+ assert encode_single (dynamic_bytes , b '\x64 \x61 \x76 \x65 ' ) == dave_bin
307
+ assert encode_single (dynamic_bytes , b '\x64 \x61 \x76 \x65 ' ) == dave
308
308
309
309
310
310
def test_encode_dynamic_string ():
@@ -325,7 +325,7 @@ def test_encode_hash():
325
325
hash8 = ('hash' , '8' , [])
326
326
327
327
assert encode_single (hash8 , b'\x00 ' * 8 ) == b'\x00 ' * 32
328
- assert encode_single (hash8 , '00' * 8 ) == b'\x00 ' * 32
328
+ assert encode_single (hash8 , b '00' * 8 ) == b'\x00 ' * 32
329
329
330
330
331
331
def test_encode_address ():
@@ -378,8 +378,8 @@ def test_encode_decode_fixed():
378
378
fixed_data = encode_single (fixed128x128 , 1 )
379
379
assert decode_single (fixed128x128 , fixed_data ) == 1
380
380
381
- fixed_data = encode_single (fixed128x128 , 2 ** 127 - 1 )
382
- assert decode_single (fixed128x128 , fixed_data ) == (2 ** 127 - 1 ) * 1.0
381
+ fixed_data = encode_single (fixed128x128 , 2 ** 127 - 1 )
382
+ assert decode_single (fixed128x128 , fixed_data ) == (2 ** 127 - 1 ) * 1.0
383
383
384
384
fixed_data = encode_single (fixed128x128 , - 1 )
385
385
assert decode_single (fixed128x128 , fixed_data ) == - 1
@@ -414,12 +414,12 @@ def test_encode_decode_address():
414
414
address3 ,
415
415
]
416
416
all_addresses_encoded = [
417
- address1 . encode ( 'hex' ),
418
- address2 . encode ( 'hex' ),
419
- address3 . encode ( 'hex' ),
417
+ encode_hex ( address1 ),
418
+ encode_hex ( address2 ),
419
+ encode_hex ( address3 ),
420
420
]
421
421
422
- assert decode_abi (['address' ], encode_abi (['address' ], [address1 ]))[0 ] == address1 . encode ( 'hex' )
422
+ assert decode_abi (['address' ], encode_abi (['address' ], [address1 ]))[0 ] == encode_hex ( address1 )
423
423
424
424
addresses_encoded_together = encode_abi (['address[]' ], [all_addresses ])
425
425
assert decode_abi (['address[]' ], addresses_encoded_together )[0 ] == all_addresses_encoded
0 commit comments