Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
1ede6835
Unverified
Commit
1ede6835
authored
7 years ago
by
Martin Holst Swende
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
accounts/abi: add another unpack interface
parent
5603715c
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
395 additions
and
14 deletions
+395
-14
abi.go
accounts/abi/abi.go
+3
-3
abi_test.go
accounts/abi/abi_test.go
+5
-1
argument.go
accounts/abi/argument.go
+50
-9
unpack_test.go
accounts/abi/unpack_test.go
+1
-1
unpackv2_test.go
accounts/abi/unpackv2_test.go
+336
-0
No files found.
accounts/abi/abi.go
View file @
1ede6835
...
...
@@ -136,11 +136,11 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
// MethodById looks up a method by the 4-byte id
// returns nil if none found
func
(
abi
*
ABI
)
MethodById
(
sigdata
[]
byte
)
*
Method
{
func
(
abi
*
ABI
)
MethodById
(
sigdata
[]
byte
)
(
*
Method
,
error
)
{
for
_
,
method
:=
range
abi
.
Methods
{
if
bytes
.
Equal
(
method
.
Id
(),
sigdata
[
:
4
])
{
return
&
method
return
&
method
,
nil
}
}
return
nil
return
nil
,
fmt
.
Errorf
(
"ABI spec does not contain method signature in data: 0x%x"
,
sigdata
[
:
4
])
}
This diff is collapsed.
Click to expand it.
accounts/abi/abi_test.go
View file @
1ede6835
...
...
@@ -689,7 +689,11 @@ func TestABI_MethodById(t *testing.T) {
}
for
name
,
m
:=
range
abi
.
Methods
{
a
:=
fmt
.
Sprintf
(
"%v"
,
m
)
b
:=
fmt
.
Sprintf
(
"%v"
,
abi
.
MethodById
(
m
.
Id
()))
m2
,
err
:=
abi
.
MethodById
(
m
.
Id
())
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
b
:=
fmt
.
Sprintf
(
"%v"
,
m2
)
if
a
!=
b
{
t
.
Errorf
(
"Method %v (id %v) not 'findable' by id in ABI"
,
name
,
common
.
ToHex
(
m
.
Id
()))
}
...
...
This diff is collapsed.
Click to expand it.
accounts/abi/argument.go
View file @
1ede6835
...
...
@@ -67,6 +67,16 @@ func (arguments Arguments) LengthNonIndexed() int {
return
out
}
func
(
arguments
Arguments
)
NonIndexed
()
Arguments
{
var
ret
[]
Argument
for
_
,
arg
:=
range
arguments
{
if
!
arg
.
Indexed
{
ret
=
append
(
ret
,
arg
)
}
}
return
ret
}
// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[]
func
(
arguments
Arguments
)
isTuple
()
bool
{
return
len
(
arguments
)
>
1
...
...
@@ -114,14 +124,9 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
// `j` counts the number of complex types.
// both `i` and `j` are used to to correctly compute `data` offset.
i
,
j
:=
-
1
,
0
for
_
,
arg
:=
range
arguments
{
j
:=
0
for
i
,
arg
:=
range
arguments
.
NonIndexed
()
{
if
arg
.
Indexed
{
// can't read, continue
continue
}
i
++
marshalledValue
,
err
:=
toGoType
((
i
+
j
)
*
32
,
arg
.
Type
,
output
)
if
err
!=
nil
{
return
err
...
...
@@ -178,7 +183,6 @@ func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
}
value
:=
valueOf
.
Elem
()
marshalledValue
,
err
:=
toGoType
(
0
,
arg
.
Type
,
output
)
if
err
!=
nil
{
return
err
...
...
@@ -186,7 +190,44 @@ func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
return
set
(
value
,
reflect
.
ValueOf
(
marshalledValue
),
arg
)
}
// Unpack performs the operation Go format -> Hexdata
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
// without supplying a struct to unpack into. Instead, this method returns a list containing the
// values. An atomic argument will be a list with one element.
func
(
arguments
Arguments
)
UnpackValues
(
data
[]
byte
)
([]
interface
{},
error
){
retval
:=
make
([]
interface
{},
0
,
arguments
.
LengthNonIndexed
())
virtualArgs
:=
0
for
index
,
arg
:=
range
arguments
.
NonIndexed
(){
marshalledValue
,
err
:=
toGoType
((
index
+
virtualArgs
)
*
32
,
arg
.
Type
,
data
)
if
arg
.
Type
.
T
==
ArrayTy
{
//If we have a static array, like [3]uint256, these are coded as
// just like uint256,uint256,uint256.
// This means that we need to add two 'virtual' arguments when
// we count the index from now on
virtualArgs
+=
arg
.
Type
.
Size
-
1
}
if
err
!=
nil
{
return
nil
,
err
}
retval
=
append
(
retval
,
marshalledValue
)
}
return
retval
,
nil
}
// UnpackValues performs the operation Go format -> Hexdata
// It is the semantic opposite of UnpackValues
func
(
arguments
Arguments
)
PackValues
(
args
[]
interface
{})
([]
byte
,
error
)
{
return
arguments
.
Pack
(
args
...
)
}
// Pack performs the operation Go format -> Hexdata
func
(
arguments
Arguments
)
Pack
(
args
...
interface
{})
([]
byte
,
error
)
{
// Make sure arguments match up and pack them
abiArgs
:=
arguments
...
...
This diff is collapsed.
Click to expand it.
accounts/abi/unpack_test.go
View file @
1ede6835
...
...
@@ -130,7 +130,7 @@ var unpackTests = []unpackTest{
{
def
:
`[{"type": "bytes32"}]`
,
enc
:
"0100000000000000000000000000000000000000000000000000000000000000"
,
want
:
common
.
HexToHash
(
"0100000000000000000000000000000000000000000000000000000000000000"
)
,
want
:
[
32
]
byte
{
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
}
,
},
{
def
:
`[{"type": "function"}]`
,
...
...
This diff is collapsed.
Click to expand it.
accounts/abi/unpackv2_test.go
0 → 100644
View file @
1ede6835
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment