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
dda3bf3c
Commit
dda3bf3c
authored
Nov 03, 2015
by
Jeffrey Wilcke
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1943 from obscuren/abi-fixes
accounts/abi: ABI fixes & added types
parents
6dfbbc3e
1f72952f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
21 deletions
+80
-21
abi.go
accounts/abi/abi.go
+6
-6
abi_test.go
accounts/abi/abi_test.go
+62
-13
type.go
accounts/abi/type.go
+12
-2
No files found.
accounts/abi/abi.go
View file @
dda3bf3c
...
...
@@ -36,7 +36,7 @@ import (
type
Method
struct
{
Name
string
Const
bool
Input
[]
Argument
Input
s
[]
Argument
Return
Type
// not yet implemented
}
...
...
@@ -49,9 +49,9 @@ type Method struct {
// Please note that "int" is substitute for its canonical representation "int256"
func
(
m
Method
)
String
()
(
out
string
)
{
out
+=
m
.
Name
types
:=
make
([]
string
,
len
(
m
.
Input
))
types
:=
make
([]
string
,
len
(
m
.
Input
s
))
i
:=
0
for
_
,
input
:=
range
m
.
Input
{
for
_
,
input
:=
range
m
.
Input
s
{
types
[
i
]
=
input
.
Type
.
String
()
i
++
}
...
...
@@ -104,7 +104,7 @@ func (abi ABI) pack(name string, args ...interface{}) ([]byte, error) {
var
ret
[]
byte
for
i
,
a
:=
range
args
{
input
:=
method
.
Input
[
i
]
input
:=
method
.
Input
s
[
i
]
packed
,
err
:=
input
.
Type
.
pack
(
a
)
if
err
!=
nil
{
...
...
@@ -129,8 +129,8 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
}
// start with argument count match
if
len
(
args
)
!=
len
(
method
.
Input
)
{
return
nil
,
fmt
.
Errorf
(
"argument count mismatch: %d for %d"
,
len
(
args
),
len
(
method
.
Input
))
if
len
(
args
)
!=
len
(
method
.
Input
s
)
{
return
nil
,
fmt
.
Errorf
(
"argument count mismatch: %d for %d"
,
len
(
args
),
len
(
method
.
Input
s
))
}
arguments
,
err
:=
abi
.
pack
(
name
,
args
...
)
...
...
accounts/abi/abi_test.go
View file @
dda3bf3c
...
...
@@ -18,35 +18,38 @@ package abi
import
(
"bytes"
"fmt"
"log"
"math/big"
"reflect"
"strings"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
const
jsondata
=
`
[
{ "name" : "balance", "const" : true },
{ "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] }
{ "name" : "send", "const" : false, "input
s
" : [ { "name" : "amount", "type" : "uint256" } ] }
]`
const
jsondata2
=
`
[
{ "name" : "balance", "const" : true },
{ "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] },
{ "name" : "test", "const" : false, "input" : [ { "name" : "number", "type" : "uint32" } ] },
{ "name" : "string", "const" : false, "input
" : [ { "name" : "input
", "type" : "string" } ] },
{ "name" : "bool", "const" : false, "input
" : [ { "name" : "input
", "type" : "bool" } ] },
{ "name" : "address", "const" : false, "input
" : [ { "name" : "input
", "type" : "address" } ] },
{ "name" : "string32", "const" : false, "input
" : [ { "name" : "input
", "type" : "string32" } ] },
{ "name" : "uint64[2]", "const" : false, "input
" : [ { "name" : "input
", "type" : "uint64[2]" } ] },
{ "name" : "uint64[]", "const" : false, "input
" : [ { "name" : "input
", "type" : "uint64[]" } ] },
{ "name" : "foo", "const" : false, "input
" : [ { "name" : "input
", "type" : "uint32" } ] },
{ "name" : "bar", "const" : false, "input
" : [ { "name" : "input
", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
{ "name" : "slice", "const" : false, "input
" : [ { "name" : "input
", "type" : "uint32[2]" } ] },
{ "name" : "slice256", "const" : false, "input
" : [ { "name" : "input
", "type" : "uint256[2]" } ] }
{ "name" : "send", "const" : false, "input
s
" : [ { "name" : "amount", "type" : "uint256" } ] },
{ "name" : "test", "const" : false, "input
s
" : [ { "name" : "number", "type" : "uint32" } ] },
{ "name" : "string", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "string" } ] },
{ "name" : "bool", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "bool" } ] },
{ "name" : "address", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "address" } ] },
{ "name" : "string32", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "string32" } ] },
{ "name" : "uint64[2]", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "uint64[2]" } ] },
{ "name" : "uint64[]", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "uint64[]" } ] },
{ "name" : "foo", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "uint32" } ] },
{ "name" : "bar", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
{ "name" : "slice", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "uint32[2]" } ] },
{ "name" : "slice256", "const" : false, "input
s" : [ { "name" : "inputs
", "type" : "uint256[2]" } ] }
]`
func
TestType
(
t
*
testing
.
T
)
{
...
...
@@ -344,3 +347,49 @@ func TestPackSliceBig(t *testing.T) {
t
.
Errorf
(
"expected %x got %x"
,
sig
,
packed
)
}
}
func
ExampleJSON
()
{
const
definition
=
`[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
abi
,
err
:=
JSON
(
strings
.
NewReader
(
definition
))
if
err
!=
nil
{
log
.
Fatalln
(
err
)
}
out
,
err
:=
abi
.
Pack
(
"isBar"
,
common
.
HexToAddress
(
"01"
))
if
err
!=
nil
{
log
.
Fatalln
(
err
)
}
fmt
.
Printf
(
"%x
\n
"
,
out
)
// Output:
// 1f2c40920000000000000000000000000000000000000000000000000000000000000001
}
func
TestBytes
(
t
*
testing
.
T
)
{
const
definition
=
`[
{ "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] },
{ "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
]`
abi
,
err
:=
JSON
(
strings
.
NewReader
(
definition
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
ok
:=
make
([]
byte
,
20
)
_
,
err
=
abi
.
Pack
(
"balance"
,
ok
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
toosmall
:=
make
([]
byte
,
19
)
_
,
err
=
abi
.
Pack
(
"balance"
,
toosmall
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
toobig
:=
make
([]
byte
,
21
)
_
,
err
=
abi
.
Pack
(
"balance"
,
toobig
)
if
err
==
nil
{
t
.
Error
(
"expected error"
)
}
}
accounts/abi/type.go
View file @
dda3bf3c
...
...
@@ -43,7 +43,7 @@ type Type struct {
stringKind
string
// holds the unparsed string for deriving signatures
}
// New
t
ype returns a fully parsed Type given by the input string or an error if it can't be parsed.
// New
T
ype returns a fully parsed Type given by the input string or an error if it can't be parsed.
//
// Strings can be in the format of:
//
...
...
@@ -130,6 +130,10 @@ func NewType(t string) (typ Type, err error) {
if
vsize
>
0
{
typ
.
Size
=
32
}
case
"bytes"
:
typ
.
Kind
=
reflect
.
Slice
typ
.
Type
=
byte_ts
typ
.
Size
=
vsize
default
:
return
Type
{},
fmt
.
Errorf
(
"unsupported arg type: %s"
,
t
)
}
...
...
@@ -200,7 +204,13 @@ func (t Type) pack(v interface{}) ([]byte, error) {
}
else
{
return
common
.
LeftPadBytes
(
common
.
Big0
.
Bytes
(),
32
),
nil
}
case
reflect
.
Array
:
if
v
,
ok
:=
value
.
Interface
()
.
(
common
.
Address
);
ok
{
return
t
.
pack
(
v
[
:
])
}
else
if
v
,
ok
:=
value
.
Interface
()
.
(
common
.
Hash
);
ok
{
return
t
.
pack
(
v
[
:
])
}
}
panic
(
"unreached"
)
return
nil
,
fmt
.
Errorf
(
"ABI: bad input given %T"
,
value
.
Kind
()
)
}
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