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
2126d814
Commit
2126d814
authored
Jan 05, 2017
by
RJ
Committed by
Felix Lange
Jan 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
accounts/abi: add support for function types (#3405)
parent
06b381d1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
4 deletions
+24
-4
abi.go
accounts/abi/abi.go
+2
-2
abi_test.go
accounts/abi/abi_test.go
+13
-0
packing.go
accounts/abi/packing.go
+1
-1
type.go
accounts/abi/type.go
+8
-1
No files found.
accounts/abi/abi.go
View file @
2126d814
...
...
@@ -169,7 +169,7 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) {
// argument in T.
func
toGoType
(
i
int
,
t
Argument
,
output
[]
byte
)
(
interface
{},
error
)
{
// we need to treat slices differently
if
(
t
.
Type
.
IsSlice
||
t
.
Type
.
IsArray
)
&&
t
.
Type
.
T
!=
BytesTy
&&
t
.
Type
.
T
!=
StringTy
&&
t
.
Type
.
T
!=
FixedBytesTy
{
if
(
t
.
Type
.
IsSlice
||
t
.
Type
.
IsArray
)
&&
t
.
Type
.
T
!=
BytesTy
&&
t
.
Type
.
T
!=
StringTy
&&
t
.
Type
.
T
!=
FixedBytesTy
&&
t
.
Type
.
T
!=
FunctionTy
{
return
toGoSlice
(
i
,
t
,
output
)
}
...
...
@@ -233,7 +233,7 @@ func toGoType(i int, t Argument, output []byte) (interface{}, error) {
return
common
.
BytesToAddress
(
returnOutput
),
nil
case
HashTy
:
return
common
.
BytesToHash
(
returnOutput
),
nil
case
BytesTy
,
FixedBytesTy
:
case
BytesTy
,
FixedBytesTy
,
FunctionTy
:
return
returnOutput
,
nil
case
StringTy
:
return
string
(
returnOutput
),
nil
...
...
accounts/abi/abi_test.go
View file @
2126d814
...
...
@@ -81,6 +81,7 @@ func TestTypeCheck(t *testing.T) {
{
"bytes"
,
common
.
Hash
{
1
},
""
},
{
"string"
,
"hello world"
,
""
},
{
"bytes32[]"
,
[][
32
]
byte
{[
32
]
byte
{}},
""
},
{
"function"
,
[
24
]
byte
{},
""
},
}
{
typ
,
err
:=
NewType
(
test
.
typ
)
if
err
!=
nil
{
...
...
@@ -197,6 +198,13 @@ func TestSimpleMethodUnpack(t *testing.T) {
"interface"
,
""
,
},
{
`[ { "type": "function" } ]`
,
pad
([]
byte
{
1
},
32
,
false
),
[
24
]
byte
{
1
},
"function"
,
""
,
},
}
{
abiDefinition
:=
fmt
.
Sprintf
(
`[{ "name" : "method", "outputs": %s}]`
,
test
.
def
)
abi
,
err
:=
JSON
(
strings
.
NewReader
(
abiDefinition
))
...
...
@@ -255,6 +263,10 @@ func TestSimpleMethodUnpack(t *testing.T) {
var
v
common
.
Hash
err
=
abi
.
Unpack
(
&
v
,
"method"
,
test
.
marshalledOutput
)
outvar
=
v
case
"function"
:
var
v
[
24
]
byte
err
=
abi
.
Unpack
(
&
v
,
"method"
,
test
.
marshalledOutput
)
outvar
=
v
case
"interface"
:
err
=
abi
.
Unpack
(
&
outvar
,
"method"
,
test
.
marshalledOutput
)
default
:
...
...
@@ -333,6 +345,7 @@ func TestPack(t *testing.T) {
{
"uint256[]"
,
[]
*
big
.
Int
{
big
.
NewInt
(
1
),
big
.
NewInt
(
2
)},
formatSliceOutput
([]
byte
{
1
},
[]
byte
{
2
})},
{
"address[]"
,
[]
common
.
Address
{
common
.
Address
{
1
},
common
.
Address
{
2
}},
formatSliceOutput
(
pad
([]
byte
{
1
},
20
,
false
),
pad
([]
byte
{
2
},
20
,
false
))},
{
"bytes32[]"
,
[]
common
.
Hash
{
common
.
Hash
{
1
},
common
.
Hash
{
2
}},
formatSliceOutput
(
pad
([]
byte
{
1
},
32
,
false
),
pad
([]
byte
{
2
},
32
,
false
))},
{
"function"
,
[
24
]
byte
{
1
},
pad
([]
byte
{
1
},
32
,
false
)},
}
{
typ
,
err
:=
NewType
(
test
.
typ
)
if
err
!=
nil
{
...
...
accounts/abi/packing.go
View file @
2126d814
...
...
@@ -54,7 +54,7 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
reflectValue
=
mustArrayToByteSlice
(
reflectValue
)
}
return
packBytesSlice
(
reflectValue
.
Bytes
(),
reflectValue
.
Len
())
case
FixedBytesTy
:
case
FixedBytesTy
,
FunctionTy
:
if
reflectValue
.
Kind
()
==
reflect
.
Array
{
reflectValue
=
mustArrayToByteSlice
(
reflectValue
)
}
...
...
accounts/abi/type.go
View file @
2126d814
...
...
@@ -34,6 +34,7 @@ const (
BytesTy
HashTy
FixedpointTy
FunctionTy
)
// Type is the reflection of the supported argument type
...
...
@@ -148,6 +149,12 @@ func NewType(t string) (typ Type, err error) {
typ
.
T
=
FixedBytesTy
typ
.
SliceSize
=
varSize
}
case
"function"
:
sliceType
,
_
:=
NewType
(
"uint8"
)
typ
.
Elem
=
&
sliceType
typ
.
IsArray
=
true
typ
.
T
=
FunctionTy
typ
.
SliceSize
=
24
default
:
return
Type
{},
fmt
.
Errorf
(
"unsupported arg type: %s"
,
t
)
}
...
...
@@ -168,7 +175,7 @@ func (t Type) pack(v reflect.Value) ([]byte, error) {
return
nil
,
err
}
if
(
t
.
IsSlice
||
t
.
IsArray
)
&&
t
.
T
!=
BytesTy
&&
t
.
T
!=
FixedBytesTy
{
if
(
t
.
IsSlice
||
t
.
IsArray
)
&&
t
.
T
!=
BytesTy
&&
t
.
T
!=
FixedBytesTy
&&
t
.
T
!=
FunctionTy
{
var
packed
[]
byte
for
i
:=
0
;
i
<
v
.
Len
();
i
++
{
...
...
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