Skip to content

Commit

Permalink
Fix bug when ABI encoding/decoding constant-size Cadence arrays and a…
Browse files Browse the repository at this point in the history
…dd tests
  • Loading branch information
m-Peter committed Dec 12, 2023
1 parent dd997fa commit 190df1a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fvm/evm/stdlib/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func encodeABI(
switch arrayStaticType := arrayStaticType.(type) {
case interpreter.ConstantSizedStaticType:
size := int(arrayStaticType.Size)
result = reflect.New(reflect.ArrayOf(size, elementGoType))
result = reflect.Indirect(reflect.New(reflect.ArrayOf(size, elementGoType)))

case interpreter.VariableSizedStaticType:
size := value.Count()
Expand Down
54 changes: 54 additions & 0 deletions fvm/evm/stdlib/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,60 @@ func TestEVMEncodeDecodeABIRoundtrip(t *testing.T) {
assert((values[13] as! [Int256]) == [-5, -10])
assert((values[14] as! [EVM.EVMAddress])[0].bytes == [address][0].bytes)
// Check constant-size array of leaf types encode/decode
data = EVM.encodeABI([
["one", "two"] as [String; 2],
[true, false] as [Bool; 2],
[5, 10] as [UInt8; 2],
[5, 10] as [UInt16; 2],
[5, 10] as [UInt32; 2],
[5, 10] as [UInt64; 2],
[5, 10] as [UInt128; 2],
[5, 10] as [UInt256; 2],
[-5, -10] as [Int8; 2],
[-5, -10] as [Int16; 2],
[-5, -10] as [Int32; 2],
[-5, -10] as [Int64; 2],
[-5, -10] as [Int128; 2],
[-5, -10] as [Int256; 2],
[address] as [EVM.EVMAddress; 1]
])
values = EVM.decodeABI(
types: [
Type<[String; 2]>(),
Type<[Bool; 2]>(),
Type<[UInt8; 2]>(),
Type<[UInt16; 2]>(),
Type<[UInt32; 2]>(),
Type<[UInt64; 2]>(),
Type<[UInt128; 2]>(),
Type<[UInt256; 2]>(),
Type<[Int8; 2]>(),
Type<[Int16; 2]>(),
Type<[Int32; 2]>(),
Type<[Int64; 2]>(),
Type<[Int128; 2]>(),
Type<[Int256; 2]>(),
Type<[EVM.EVMAddress; 1]>()
],
data: data
)
assert((values[0] as! [String; 2]) == ["one", "two"])
assert((values[1] as! [Bool; 2]) == [true, false])
assert((values[2] as! [UInt8; 2]) == [5, 10])
assert((values[3] as! [UInt16; 2]) == [5, 10])
assert((values[4] as! [UInt32; 2]) == [5, 10])
assert((values[5] as! [UInt64; 2]) == [5, 10])
assert((values[6] as! [UInt128; 2]) == [5, 10])
assert((values[7] as! [UInt256; 2]) == [5, 10])
assert((values[8] as! [Int8; 2]) == [-5, -10])
assert((values[9] as! [Int16; 2]) == [-5, -10])
assert((values[10] as! [Int32; 2]) == [-5, -10])
assert((values[11] as! [Int64; 2]) == [-5, -10])
assert((values[12] as! [Int128; 2]) == [-5, -10])
assert((values[13] as! [Int256; 2]) == [-5, -10])
assert((values[14] as! [EVM.EVMAddress; 1])[0].bytes == [address][0].bytes)
// Check partial decoding of encoded data
data = EVM.encodeABI(["Peter", UInt64(9999)])
values = EVM.decodeABI(types: [Type<String>()], data: data)
Expand Down

0 comments on commit 190df1a

Please sign in to comment.