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
f0f594d0
Unverified
Commit
f0f594d0
authored
Dec 30, 2017
by
Martin Holst Swende
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
accounts/abi: Deduplicate code in unpacker
parent
1ede6835
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
53 deletions
+33
-53
abi.go
accounts/abi/abi.go
+1
-1
abi_test.go
accounts/abi/abi_test.go
+1
-1
argument.go
accounts/abi/argument.go
+31
-50
unpackv2_test.go
accounts/abi/unpackv2_test.go
+0
-1
No files found.
accounts/abi/abi.go
View file @
f0f594d0
...
...
@@ -136,7 +136,7 @@ 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
,
error
){
func
(
abi
*
ABI
)
MethodById
(
sigdata
[]
byte
)
(
*
Method
,
error
)
{
for
_
,
method
:=
range
abi
.
Methods
{
if
bytes
.
Equal
(
method
.
Id
(),
sigdata
[
:
4
])
{
return
&
method
,
nil
...
...
accounts/abi/abi_test.go
View file @
f0f594d0
...
...
@@ -689,7 +689,7 @@ func TestABI_MethodById(t *testing.T) {
}
for
name
,
m
:=
range
abi
.
Methods
{
a
:=
fmt
.
Sprintf
(
"%v"
,
m
)
m2
,
err
:=
abi
.
MethodById
(
m
.
Id
())
m2
,
err
:=
abi
.
MethodById
(
m
.
Id
())
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
accounts/abi/argument.go
View file @
f0f594d0
...
...
@@ -67,10 +67,10 @@ func (arguments Arguments) LengthNonIndexed() int {
return
out
}
func
(
arguments
Arguments
)
NonIndexed
()
Arguments
{
func
(
arguments
Arguments
)
NonIndexed
()
Arguments
{
var
ret
[]
Argument
for
_
,
arg
:=
range
arguments
{
if
!
arg
.
Indexed
{
for
_
,
arg
:=
range
arguments
{
if
!
arg
.
Indexed
{
ret
=
append
(
ret
,
arg
)
}
}
...
...
@@ -84,21 +84,27 @@ func (arguments Arguments) isTuple() bool {
// Unpack performs the operation hexdata -> Go format
func
(
arguments
Arguments
)
Unpack
(
v
interface
{},
data
[]
byte
)
error
{
if
arguments
.
isTuple
()
{
return
arguments
.
unpackTuple
(
v
,
data
)
}
return
arguments
.
unpackAtomic
(
v
,
data
)
}
func
(
arguments
Arguments
)
unpackTuple
(
v
interface
{},
output
[]
byte
)
error
{
// make sure the passed value is arguments pointer
valueOf
:=
reflect
.
ValueOf
(
v
)
if
reflect
.
Ptr
!=
valueOf
.
Kind
()
{
if
reflect
.
Ptr
!=
reflect
.
ValueOf
(
v
)
.
Kind
()
{
return
fmt
.
Errorf
(
"abi: Unpack(non-pointer %T)"
,
v
)
}
marshalledValues
,
err
:=
arguments
.
UnpackValues
(
data
)
if
err
!=
nil
{
return
err
}
if
arguments
.
isTuple
()
{
return
arguments
.
unpackTuple
(
v
,
marshalledValues
)
}
return
arguments
.
unpackAtomic
(
v
,
marshalledValues
)
}
func
(
arguments
Arguments
)
unpackTuple
(
v
interface
{},
marshalledValues
[]
interface
{})
error
{
var
(
value
=
valueOf
.
Elem
()
value
=
reflect
.
ValueOf
(
v
)
.
Elem
()
typ
=
value
.
Type
()
kind
=
value
.
Kind
()
)
...
...
@@ -120,25 +126,9 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
exists
[
field
]
=
true
}
}
// `i` counts the nonindexed arguments.
// `j` counts the number of complex types.
// both `i` and `j` are used to to correctly compute `data` offset.
j
:=
0
for
i
,
arg
:=
range
arguments
.
NonIndexed
()
{
marshalledValue
,
err
:=
toGoType
((
i
+
j
)
*
32
,
arg
.
Type
,
output
)
if
err
!=
nil
{
return
err
}
if
arg
.
Type
.
T
==
ArrayTy
{
// combined index ('i' + 'j') need to be adjusted only by size of array, thus
// we need to decrement 'j' because 'i' was incremented
j
+=
arg
.
Type
.
Size
-
1
}
reflectValue
:=
reflect
.
ValueOf
(
marshalledValue
)
reflectValue
:=
reflect
.
ValueOf
(
marshalledValues
[
i
])
switch
kind
{
case
reflect
.
Struct
:
...
...
@@ -171,37 +161,29 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
}
// unpackAtomic unpacks ( hexdata -> go ) a single value
func
(
arguments
Arguments
)
unpackAtomic
(
v
interface
{},
output
[]
byte
)
error
{
// make sure the passed value is arguments pointer
valueOf
:=
reflect
.
ValueOf
(
v
)
if
reflect
.
Ptr
!=
valueOf
.
Kind
()
{
return
fmt
.
Errorf
(
"abi: Unpack(non-pointer %T)"
,
v
)
}
arg
:=
arguments
[
0
]
if
arg
.
Indexed
{
return
fmt
.
Errorf
(
"abi: attempting to unpack indexed variable into element."
)
}
func
(
arguments
Arguments
)
unpackAtomic
(
v
interface
{},
marshalledValues
[]
interface
{})
error
{
value
:=
valueOf
.
Elem
()
marshalledValue
,
err
:=
toGoType
(
0
,
arg
.
Type
,
output
)
if
err
!=
nil
{
return
err
if
len
(
marshalledValues
)
!=
1
{
return
fmt
.
Errorf
(
"abi: wrong length, expected single value, got %d"
,
len
(
marshalledValues
))
}
return
set
(
value
,
reflect
.
ValueOf
(
marshalledValue
),
arg
)
elem
:=
reflect
.
ValueOf
(
v
)
.
Elem
()
reflectValue
:=
reflect
.
ValueOf
(
marshalledValues
[
0
])
return
set
(
elem
,
reflectValue
,
arguments
.
NonIndexed
()[
0
])
}
// 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
){
func
(
arguments
Arguments
)
UnpackValues
(
data
[]
byte
)
([]
interface
{},
error
)
{
retval
:=
make
([]
interface
{},
0
,
arguments
.
LengthNonIndexed
())
retval
:=
make
([]
interface
{},
0
,
arguments
.
LengthNonIndexed
())
virtualArgs
:=
0
for
index
,
arg
:=
range
arguments
.
NonIndexed
()
{
for
index
,
arg
:=
range
arguments
.
NonIndexed
()
{
marshalledValue
,
err
:=
toGoType
((
index
+
virtualArgs
)
*
32
,
arg
.
Type
,
data
)
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
...
...
@@ -212,7 +194,7 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error){
virtualArgs
+=
arg
.
Type
.
Size
-
1
}
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
}
retval
=
append
(
retval
,
marshalledValue
)
...
...
@@ -226,7 +208,6 @@ 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
...
...
accounts/abi/unpackv2_test.go
View file @
f0f594d0
...
...
@@ -57,7 +57,6 @@ func TestUnpackV2(t *testing.T) {
}
}
func
TestMultiReturnWithArrayV2
(
t
*
testing
.
T
)
{
const
definition
=
`[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
abi
,
err
:=
JSON
(
strings
.
NewReader
(
definition
))
...
...
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