Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
c095c87e
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
c095c87e
编写于
12月 21, 2017
作者:
M
Martin Holst Swende
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
accounts/abi: merging of
https://github.com/ethereum/go-ethereum/pull/15452
+ lookup by id
上级
73d4a57d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
128 addition
and
47 deletion
+128
-47
accounts/abi/abi.go
accounts/abi/abi.go
+12
-0
accounts/abi/abi_test.go
accounts/abi/abi_test.go
+59
-6
accounts/abi/argument.go
accounts/abi/argument.go
+36
-35
accounts/abi/event_test.go
accounts/abi/event_test.go
+4
-6
accounts/abi/reflect.go
accounts/abi/reflect.go
+17
-0
未找到文件。
accounts/abi/abi.go
浏览文件 @
c095c87e
...
...
@@ -17,6 +17,7 @@
package
abi
import
(
"bytes"
"encoding/json"
"fmt"
"io"
...
...
@@ -133,3 +134,14 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
return
nil
}
// MethodById looks up a method by the 4-byte id
// returns nil if none found
func
(
abi
*
ABI
)
MethodById
(
sigdata
[]
byte
)
*
Method
{
for
_
,
method
:=
range
abi
.
Methods
{
if
bytes
.
Equal
(
method
.
Id
(),
sigdata
[
:
4
])
{
return
&
method
}
}
return
nil
}
accounts/abi/abi_test.go
浏览文件 @
c095c87e
...
...
@@ -25,6 +25,8 @@ import (
"strings"
"testing"
"reflect"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
...
...
@@ -74,12 +76,24 @@ func TestReader(t *testing.T) {
}
// deep equal fails for some reason
//t.Skip()
// Check with String() instead
expS
:=
fmt
.
Sprintf
(
"%v"
,
exp
)
gotS
:=
fmt
.
Sprintf
(
"%v"
,
abi
)
if
expS
!=
gotS
{
t
.
Errorf
(
"
\n
Got abi:
\n
%v
\n
does not match expected
\n
%v"
,
abi
,
exp
)
for
name
,
expM
:=
range
exp
.
Methods
{
gotM
,
exist
:=
abi
.
Methods
[
name
]
if
!
exist
{
t
.
Errorf
(
"Missing expected method %v"
,
name
)
}
if
!
reflect
.
DeepEqual
(
gotM
,
expM
)
{
t
.
Errorf
(
"
\n
Got abi method:
\n
%v
\n
does not match expected method
\n
%v"
,
gotM
,
expM
)
}
}
for
name
,
gotM
:=
range
abi
.
Methods
{
expM
,
exist
:=
exp
.
Methods
[
name
]
if
!
exist
{
t
.
Errorf
(
"Found extra method %v"
,
name
)
}
if
!
reflect
.
DeepEqual
(
gotM
,
expM
)
{
t
.
Errorf
(
"
\n
Got abi method:
\n
%v
\n
does not match expected method
\n
%v"
,
gotM
,
expM
)
}
}
}
...
...
@@ -643,3 +657,42 @@ func TestUnpackEvent(t *testing.T) {
t
.
Logf
(
"len(data): %d; received event: %+v"
,
len
(
data
),
ev
)
}
}
func
TestABI_MethodById
(
t
*
testing
.
T
)
{
const
abiJSON
=
`[
{"type":"function","name":"receive","constant":false,"inputs":[{"name":"memo","type":"bytes"}],"outputs":[],"payable":true,"stateMutability":"payable"},
{"type":"event","name":"received","anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}]},
{"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":"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]"}]},
{"type":"function","name":"balance","constant":true},
{"type":"function","name":"send","constant":false,"inputs":[{"name":"amount","type":"uint256"}]},
{"type":"function","name":"test","constant":false,"inputs":[{"name":"number","type":"uint32"}]},
{"type":"function","name":"string","constant":false,"inputs":[{"name":"inputs","type":"string"}]},
{"type":"function","name":"bool","constant":false,"inputs":[{"name":"inputs","type":"bool"}]},
{"type":"function","name":"address","constant":false,"inputs":[{"name":"inputs","type":"address"}]},
{"type":"function","name":"uint64[2]","constant":false,"inputs":[{"name":"inputs","type":"uint64[2]"}]},
{"type":"function","name":"uint64[]","constant":false,"inputs":[{"name":"inputs","type":"uint64[]"}]},
{"type":"function","name":"foo","constant":false,"inputs":[{"name":"inputs","type":"uint32"}]},
{"type":"function","name":"bar","constant":false,"inputs":[{"name":"inputs","type":"uint32"},{"name":"string","type":"uint16"}]},
{"type":"function","name":"_slice","constant":false,"inputs":[{"name":"inputs","type":"uint32[2]"}]},
{"type":"function","name":"__slice256","constant":false,"inputs":[{"name":"inputs","type":"uint256[2]"}]},
{"type":"function","name":"sliceAddress","constant":false,"inputs":[{"name":"inputs","type":"address[]"}]},
{"type":"function","name":"sliceMultiAddress","constant":false,"inputs":[{"name":"a","type":"address[]"},{"name":"b","type":"address[]"}]}
]
`
abi
,
err
:=
JSON
(
strings
.
NewReader
(
abiJSON
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
name
,
m
:=
range
abi
.
Methods
{
a
:=
fmt
.
Sprintf
(
"%v"
,
m
)
b
:=
fmt
.
Sprintf
(
"%v"
,
abi
.
MethodById
(
m
.
Id
()))
if
a
!=
b
{
t
.
Errorf
(
"Method %v (id %v) not 'findable' by id in ABI"
,
name
,
common
.
ToHex
(
m
.
Id
()))
}
}
}
accounts/abi/argument.go
浏览文件 @
c095c87e
...
...
@@ -34,7 +34,7 @@ type Argument struct {
type
Arguments
[]
Argument
// UnmarshalJSON implements json.Unmarshaler interface
func
(
a
*
Argument
)
UnmarshalJSON
(
data
[]
byte
)
error
{
func
(
a
rgument
*
Argument
)
UnmarshalJSON
(
data
[]
byte
)
error
{
var
extarg
struct
{
Name
string
Type
string
...
...
@@ -45,38 +45,43 @@ func (a *Argument) UnmarshalJSON(data []byte) error {
return
fmt
.
Errorf
(
"argument json err: %v"
,
err
)
}
a
.
Type
,
err
=
NewType
(
extarg
.
Type
)
a
rgument
.
Type
,
err
=
NewType
(
extarg
.
Type
)
if
err
!=
nil
{
return
err
}
a
.
Name
=
extarg
.
Name
a
.
Indexed
=
extarg
.
Indexed
a
rgument
.
Name
=
extarg
.
Name
a
rgument
.
Indexed
=
extarg
.
Indexed
return
nil
}
func
countNonIndexedArguments
(
args
[]
Argument
)
int
{
// LengthNonIndexed returns the number of arguments when not counting 'indexed' ones. Only events
// can ever have 'indexed' arguments, it should always be false on arguments for method input/output
func
(
arguments
Arguments
)
LengthNonIndexed
()
int
{
out
:=
0
for
i
:=
range
arg
s
{
if
!
arg
s
[
i
]
.
Indexed
{
for
_
,
arg
:=
range
argument
s
{
if
!
arg
.
Indexed
{
out
++
}
}
return
out
}
func
(
a
*
Arguments
)
isTuple
()
bool
{
return
a
!=
nil
&&
len
(
*
a
)
>
1
// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[]
func
(
arguments
Arguments
)
isTuple
()
bool
{
return
len
(
arguments
)
>
1
}
func
(
a
*
Arguments
)
Unpack
(
v
interface
{},
data
[]
byte
)
error
{
if
a
.
isTuple
()
{
return
a
.
unpackTuple
(
v
,
data
)
// 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
a
.
unpackAtomic
(
v
,
data
)
return
a
rguments
.
unpackAtomic
(
v
,
data
)
}
func
(
a
*
Arguments
)
unpackTuple
(
v
interface
{},
output
[]
byte
)
error
{
// make sure the passed value is a pointer
func
(
a
rguments
Arguments
)
unpackTuple
(
v
interface
{},
output
[]
byte
)
error
{
// make sure the passed value is a
rguments
pointer
valueOf
:=
reflect
.
ValueOf
(
v
)
if
reflect
.
Ptr
!=
valueOf
.
Kind
()
{
return
fmt
.
Errorf
(
"abi: Unpack(non-pointer %T)"
,
v
)
...
...
@@ -87,17 +92,16 @@ func (a *Arguments) unpackTuple(v interface{}, output []byte) error {
typ
=
value
.
Type
()
kind
=
value
.
Kind
()
)
/* !TODO add this back
if err := requireUnpackKind(value, typ, kind,
(*a), false
); err != nil {
if
err
:=
requireUnpackKind
(
value
,
typ
,
kind
,
arguments
);
err
!=
nil
{
return
err
}
*/
// `i` counts the nonindexed arguments.
// `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
(
*
a
)
{
for
_
,
arg
:=
range
arguments
{
if
arg
.
Indexed
{
// can't read, continue
...
...
@@ -130,14 +134,16 @@ func (a *Arguments) unpackTuple(v interface{}, output []byte) error {
}
case
reflect
.
Slice
,
reflect
.
Array
:
if
value
.
Len
()
<
i
{
return
fmt
.
Errorf
(
"abi: insufficient number of arguments for unpack, want %d, got %d"
,
len
(
*
a
),
value
.
Len
())
return
fmt
.
Errorf
(
"abi: insufficient number of arguments for unpack, want %d, got %d"
,
len
(
arguments
),
value
.
Len
())
}
v
:=
value
.
Index
(
i
)
if
err
:=
requireAssignable
(
v
,
reflectValue
);
err
!=
nil
{
return
err
}
reflectValue
:=
reflect
.
ValueOf
(
marshalledValue
)
return
set
(
v
.
Elem
(),
reflectValue
,
arg
)
if
err
:=
set
(
v
.
Elem
(),
reflectValue
,
arg
);
err
!=
nil
{
return
err
}
default
:
return
fmt
.
Errorf
(
"abi:[2] cannot unmarshal tuple in to %v"
,
typ
)
}
...
...
@@ -145,13 +151,14 @@ func (a *Arguments) unpackTuple(v interface{}, output []byte) error {
return
nil
}
func
(
a
*
Arguments
)
unpackAtomic
(
v
interface
{},
output
[]
byte
)
error
{
// make sure the passed value is a pointer
// 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
:=
(
*
a
)
[
0
]
arg
:=
arguments
[
0
]
if
arg
.
Indexed
{
return
fmt
.
Errorf
(
"abi: attempting to unpack indexed variable into element."
)
}
...
...
@@ -162,19 +169,13 @@ func (a *Arguments) unpackAtomic(v interface{}, output []byte) error {
if
err
!=
nil
{
return
err
}
if
err
:=
set
(
value
,
reflect
.
ValueOf
(
marshalledValue
),
arg
);
err
!=
nil
{
return
err
}
return
nil
return
set
(
value
,
reflect
.
ValueOf
(
marshalledValue
),
arg
)
}
func
(
arguments
*
Arguments
)
Pack
(
args
...
interface
{})
([]
byte
,
error
)
{
// Unpack performs the operation Go format -> Hexdata
func
(
arguments
Arguments
)
Pack
(
args
...
interface
{})
([]
byte
,
error
)
{
// Make sure arguments match up and pack them
if
arguments
==
nil
{
return
nil
,
fmt
.
Errorf
(
"arguments are nil, programmer error!"
)
}
abiArgs
:=
*
arguments
abiArgs
:=
arguments
if
len
(
args
)
!=
len
(
abiArgs
)
{
return
nil
,
fmt
.
Errorf
(
"argument count mismatch: %d for %d"
,
len
(
args
),
len
(
abiArgs
))
}
...
...
accounts/abi/event_test.go
浏览文件 @
c095c87e
...
...
@@ -225,7 +225,7 @@ func TestEventTupleUnpack(t *testing.T) {
err
:=
unpackTestEventData
(
tc
.
dest
,
tc
.
data
,
tc
.
jsonLog
,
assert
)
if
tc
.
error
==
""
{
assert
.
Nil
(
err
,
"Should be able to unpack event data."
)
assert
.
Equal
(
tc
.
expected
,
tc
.
dest
)
assert
.
Equal
(
tc
.
expected
,
tc
.
dest
,
tc
.
name
)
}
else
{
assert
.
EqualError
(
err
,
tc
.
error
)
}
...
...
@@ -243,11 +243,10 @@ func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, ass
}
/*
!TODO enable these when the fix is in.
Taken from
Taken from
https://github.com/ethereum/go-ethereum/pull/15568
*/
/*
type
testResult
struct
{
Values
[
2
]
*
big
.
Int
Value1
*
big
.
Int
...
...
@@ -309,10 +308,9 @@ func TestEventIndexedWithArrayUnpack(t *testing.T) {
b
.
Write
(
packNum
(
reflect
.
ValueOf
(
32
)))
b
.
Write
(
packNum
(
reflect
.
ValueOf
(
len
(
stringOut
))))
b
.
Write
(
common
.
RightPadBytes
([]
byte
(
stringOut
),
32
))
fmt.Println(b.Bytes())
var
rst
testStruct
require
.
NoError
(
t
,
abi
.
Unpack
(
&
rst
,
"test"
,
b
.
Bytes
()))
require
.
Equal
(
t
,
[
2
]
uint8
{
0
,
0
},
rst
.
Value1
)
require
.
Equal
(
t
,
stringOut
,
rst
.
Value2
)
}
*/
accounts/abi/reflect.go
浏览文件 @
c095c87e
...
...
@@ -93,3 +93,20 @@ func requireAssignable(dst, src reflect.Value) error {
}
return
nil
}
// requireUnpackKind verifies preconditions for unpacking `args` into `kind`
func
requireUnpackKind
(
v
reflect
.
Value
,
t
reflect
.
Type
,
k
reflect
.
Kind
,
args
Arguments
)
error
{
switch
k
{
case
reflect
.
Struct
:
case
reflect
.
Slice
,
reflect
.
Array
:
if
minLen
:=
args
.
LengthNonIndexed
();
v
.
Len
()
<
minLen
{
return
fmt
.
Errorf
(
"abi: insufficient number of elements in the list/array for unpack, want %d, got %d"
,
minLen
,
v
.
Len
())
}
default
:
return
fmt
.
Errorf
(
"abi: cannot unmarshal tuple into %v"
,
t
)
}
return
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录