good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
cf7aba36
Commit
cf7aba36
authored
7 years ago
by
Yondon Fu
Browse files
Options
Downloads
Patches
Plain Diff
accounts/abi: update array type check in method.go. Add more packing tests
parent
3857cdc2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
accounts/abi/abi_test.go
+136
-4
136 additions, 4 deletions
accounts/abi/abi_test.go
accounts/abi/method.go
+2
-2
2 additions, 2 deletions
accounts/abi/method.go
with
138 additions
and
6 deletions
accounts/abi/abi_test.go
+
136
−
4
View file @
cf7aba36
...
...
@@ -351,7 +351,10 @@ func TestInputVariableInputLength(t *testing.T) {
func
TestInputFixedArrayAndVariableInputLength
(
t
*
testing
.
T
)
{
const
definition
=
`[
{ "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
{ "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] }
{ "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
{ "type" : "function", "name" : "mixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type": "uint256[2]" }, { "name" : "dynArr", "type": "uint256[]" } ] },
{ "type" : "function", "name" : "doubleFixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "fixedArr2", "type": "uint256[3]" } ] },
{ "type" : "function", "name" : "multipleMixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] }
]`
abi
,
err
:=
JSON
(
strings
.
NewReader
(
definition
))
...
...
@@ -359,14 +362,15 @@ func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
t
.
Error
(
err
)
}
// test fixed array of uint256 and a string
arrin
:=
[
2
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)}
// test string, fixed array uint256[2]
strin
:=
"hello world"
arrin
:=
[
2
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)}
fixedArrStrPack
,
err
:=
abi
.
Pack
(
"fixedArrStr"
,
strin
,
arrin
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
// generate expected output
offset
:=
make
([]
byte
,
32
)
offset
[
31
]
=
96
length
:=
make
([]
byte
,
32
)
...
...
@@ -384,18 +388,146 @@ func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
t
.
Errorf
(
"expected %x, got %x
\n
"
,
exp
,
fixedArrStrPack
)
}
// test fixed array
of
uint256
and a byte array
// test
byte array,
fixed array uint256
[2]
bytesin
:=
[]
byte
(
strin
)
arrin
=
[
2
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)}
fixedArrBytesPack
,
err
:=
abi
.
Pack
(
"fixedArrBytes"
,
bytesin
,
arrin
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
// generate expected output
offset
=
make
([]
byte
,
32
)
offset
[
31
]
=
96
length
=
make
([]
byte
,
32
)
length
[
31
]
=
byte
(
len
(
strin
))
strvalue
=
common
.
RightPadBytes
([]
byte
(
strin
),
32
)
arrinvalue1
=
common
.
LeftPadBytes
(
arrin
[
0
]
.
Bytes
(),
32
)
arrinvalue2
=
common
.
LeftPadBytes
(
arrin
[
1
]
.
Bytes
(),
32
)
exp
=
append
(
offset
,
arrinvalue1
...
)
exp
=
append
(
exp
,
arrinvalue2
...
)
exp
=
append
(
exp
,
append
(
length
,
strvalue
...
)
...
)
// ignore first 4 bytes of the output. This is the function identifier
fixedArrBytesPack
=
fixedArrBytesPack
[
4
:
]
if
!
bytes
.
Equal
(
fixedArrBytesPack
,
exp
)
{
t
.
Errorf
(
"expected %x, got %x
\n
"
,
exp
,
fixedArrBytesPack
)
}
// test string, fixed array uint256[2], dynamic array uint256[]
strin
=
"hello world"
fixedarrin
:=
[
2
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)}
dynarrin
:=
[]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
),
big
.
NewInt
(
3
)}
mixedArrStrPack
,
err
:=
abi
.
Pack
(
"mixedArrStr"
,
strin
,
fixedarrin
,
dynarrin
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
// generate expected output
stroffset
:=
make
([]
byte
,
32
)
stroffset
[
31
]
=
128
strlength
:=
make
([]
byte
,
32
)
strlength
[
31
]
=
byte
(
len
(
strin
))
strvalue
=
common
.
RightPadBytes
([]
byte
(
strin
),
32
)
fixedarrinvalue1
:=
common
.
LeftPadBytes
(
fixedarrin
[
0
]
.
Bytes
(),
32
)
fixedarrinvalue2
:=
common
.
LeftPadBytes
(
fixedarrin
[
1
]
.
Bytes
(),
32
)
dynarroffset
:=
make
([]
byte
,
32
)
dynarroffset
[
31
]
=
byte
(
160
+
((
len
(
strin
)
/
32
)
+
1
)
*
32
)
dynarrlength
:=
make
([]
byte
,
32
)
dynarrlength
[
31
]
=
byte
(
len
(
dynarrin
))
dynarrinvalue1
:=
common
.
LeftPadBytes
(
dynarrin
[
0
]
.
Bytes
(),
32
)
dynarrinvalue2
:=
common
.
LeftPadBytes
(
dynarrin
[
1
]
.
Bytes
(),
32
)
dynarrinvalue3
:=
common
.
LeftPadBytes
(
dynarrin
[
2
]
.
Bytes
(),
32
)
exp
=
append
(
stroffset
,
fixedarrinvalue1
...
)
exp
=
append
(
exp
,
fixedarrinvalue2
...
)
exp
=
append
(
exp
,
dynarroffset
...
)
exp
=
append
(
exp
,
append
(
strlength
,
strvalue
...
)
...
)
dynarrarg
:=
append
(
dynarrlength
,
dynarrinvalue1
...
)
dynarrarg
=
append
(
dynarrarg
,
dynarrinvalue2
...
)
dynarrarg
=
append
(
dynarrarg
,
dynarrinvalue3
...
)
exp
=
append
(
exp
,
dynarrarg
...
)
// ignore first 4 bytes of the output. This is the function identifier
mixedArrStrPack
=
mixedArrStrPack
[
4
:
]
if
!
bytes
.
Equal
(
mixedArrStrPack
,
exp
)
{
t
.
Errorf
(
"expected %x, got %x
\n
"
,
exp
,
mixedArrStrPack
)
}
// test string, fixed array uint256[2], fixed array uint256[3]
strin
=
"hello world"
fixedarrin1
:=
[
2
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)}
fixedarrin2
:=
[
3
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
),
big
.
NewInt
(
3
)}
doubleFixedArrStrPack
,
err
:=
abi
.
Pack
(
"doubleFixedArrStr"
,
strin
,
fixedarrin1
,
fixedarrin2
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
// generate expected output
stroffset
=
make
([]
byte
,
32
)
stroffset
[
31
]
=
192
strlength
=
make
([]
byte
,
32
)
strlength
[
31
]
=
byte
(
len
(
strin
))
strvalue
=
common
.
RightPadBytes
([]
byte
(
strin
),
32
)
fixedarrin1value1
:=
common
.
LeftPadBytes
(
fixedarrin1
[
0
]
.
Bytes
(),
32
)
fixedarrin1value2
:=
common
.
LeftPadBytes
(
fixedarrin1
[
1
]
.
Bytes
(),
32
)
fixedarrin2value1
:=
common
.
LeftPadBytes
(
fixedarrin2
[
0
]
.
Bytes
(),
32
)
fixedarrin2value2
:=
common
.
LeftPadBytes
(
fixedarrin2
[
1
]
.
Bytes
(),
32
)
fixedarrin2value3
:=
common
.
LeftPadBytes
(
fixedarrin2
[
2
]
.
Bytes
(),
32
)
exp
=
append
(
stroffset
,
fixedarrin1value1
...
)
exp
=
append
(
exp
,
fixedarrin1value2
...
)
exp
=
append
(
exp
,
fixedarrin2value1
...
)
exp
=
append
(
exp
,
fixedarrin2value2
...
)
exp
=
append
(
exp
,
fixedarrin2value3
...
)
exp
=
append
(
exp
,
append
(
strlength
,
strvalue
...
)
...
)
// ignore first 4 bytes of the output. This is the function identifier
doubleFixedArrStrPack
=
doubleFixedArrStrPack
[
4
:
]
if
!
bytes
.
Equal
(
doubleFixedArrStrPack
,
exp
)
{
t
.
Errorf
(
"expected %x, got %x
\n
"
,
exp
,
doubleFixedArrStrPack
)
}
// test string, fixed array uint256[2], dynamic array uint256[], fixed array uint256[3]
strin
=
"hello world"
fixedarrin1
=
[
2
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)}
dynarrin
=
[]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)}
fixedarrin2
=
[
3
]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
),
big
.
NewInt
(
3
)}
multipleMixedArrStrPack
,
err
:=
abi
.
Pack
(
"multipleMixedArrStr"
,
strin
,
fixedarrin1
,
dynarrin
,
fixedarrin2
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
// generate expected output
stroffset
=
make
([]
byte
,
32
)
stroffset
[
31
]
=
224
strlength
=
make
([]
byte
,
32
)
strlength
[
31
]
=
byte
(
len
(
strin
))
strvalue
=
common
.
RightPadBytes
([]
byte
(
strin
),
32
)
fixedarrin1value1
=
common
.
LeftPadBytes
(
fixedarrin1
[
0
]
.
Bytes
(),
32
)
fixedarrin1value2
=
common
.
LeftPadBytes
(
fixedarrin1
[
1
]
.
Bytes
(),
32
)
dynarroffset
=
U256
(
big
.
NewInt
(
int64
(
256
+
((
len
(
strin
)
/
32
)
+
1
)
*
32
)))
dynarrlength
=
make
([]
byte
,
32
)
dynarrlength
[
31
]
=
byte
(
len
(
dynarrin
))
dynarrinvalue1
=
common
.
LeftPadBytes
(
dynarrin
[
0
]
.
Bytes
(),
32
)
dynarrinvalue2
=
common
.
LeftPadBytes
(
dynarrin
[
1
]
.
Bytes
(),
32
)
fixedarrin2value1
=
common
.
LeftPadBytes
(
fixedarrin2
[
0
]
.
Bytes
(),
32
)
fixedarrin2value2
=
common
.
LeftPadBytes
(
fixedarrin2
[
1
]
.
Bytes
(),
32
)
fixedarrin2value3
=
common
.
LeftPadBytes
(
fixedarrin2
[
2
]
.
Bytes
(),
32
)
exp
=
append
(
stroffset
,
fixedarrin1value1
...
)
exp
=
append
(
exp
,
fixedarrin1value2
...
)
exp
=
append
(
exp
,
dynarroffset
...
)
exp
=
append
(
exp
,
fixedarrin2value1
...
)
exp
=
append
(
exp
,
fixedarrin2value2
...
)
exp
=
append
(
exp
,
fixedarrin2value3
...
)
exp
=
append
(
exp
,
append
(
strlength
,
strvalue
...
)
...
)
dynarrarg
=
append
(
dynarrlength
,
dynarrinvalue1
...
)
dynarrarg
=
append
(
dynarrarg
,
dynarrinvalue2
...
)
exp
=
append
(
exp
,
dynarrarg
...
)
// ignore first 4 bytes of the output. This is the function identifier
multipleMixedArrStrPack
=
multipleMixedArrStrPack
[
4
:
]
if
!
bytes
.
Equal
(
multipleMixedArrStrPack
,
exp
)
{
t
.
Errorf
(
"expected %x, got %x
\n
"
,
exp
,
multipleMixedArrStrPack
)
}
}
func
TestDefaultFunctionParsing
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
accounts/abi/method.go
+
2
−
2
View file @
cf7aba36
...
...
@@ -51,8 +51,8 @@ func (method Method) pack(args ...interface{}) ([]byte, error) {
// input offset is the bytes offset for packed output
inputOffset
:=
0
for
_
,
input
:=
range
method
.
Inputs
{
if
input
.
Type
.
Is
Array
{
inputOffset
+=
(
32
*
input
.
Type
.
Slice
Size
)
if
input
.
Type
.
T
==
Array
Ty
{
inputOffset
+=
(
32
*
input
.
Type
.
Size
)
}
else
{
inputOffset
+=
32
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment