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
970f4c06
Commit
970f4c06
authored
Jun 14, 2016
by
Péter Szilágyi
Committed by
GitHub
Jun 14, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2680 from karalabe/abi-fix-uints
accounts/abi: fix uint64 upper range encoding.
parents
22ef7370
0f9539e1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
21 deletions
+33
-21
numbers.go
accounts/abi/numbers.go
+3
-9
numbers_test.go
accounts/abi/numbers_test.go
+30
-12
No files found.
accounts/abi/numbers.go
View file @
970f4c06
...
@@ -56,27 +56,21 @@ var (
...
@@ -56,27 +56,21 @@ var (
big_ts
=
reflect
.
TypeOf
([]
*
big
.
Int
(
nil
))
big_ts
=
reflect
.
TypeOf
([]
*
big
.
Int
(
nil
))
)
)
// U256
will ensure unsigned 256bit on big nums
// U256
converts a big Int into a 256bit EVM number.
func
U256
(
n
*
big
.
Int
)
[]
byte
{
func
U256
(
n
*
big
.
Int
)
[]
byte
{
return
common
.
LeftPadBytes
(
common
.
U256
(
n
)
.
Bytes
(),
32
)
return
common
.
LeftPadBytes
(
common
.
U256
(
n
)
.
Bytes
(),
32
)
}
}
// S256 will ensure signed 256bit on big nums
func
U2U256
(
n
uint64
)
[]
byte
{
return
U256
(
big
.
NewInt
(
int64
(
n
)))
}
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
func
packNum
(
value
reflect
.
Value
)
[]
byte
{
func
packNum
(
value
reflect
.
Value
)
[]
byte
{
switch
kind
:=
value
.
Kind
();
kind
{
switch
kind
:=
value
.
Kind
();
kind
{
case
reflect
.
Uint
,
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
:
case
reflect
.
Uint
,
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
:
return
U2
U256
(
value
.
Uint
(
))
return
U2
56
(
new
(
big
.
Int
)
.
SetUint64
(
value
.
Uint
()
))
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
,
reflect
.
Int64
:
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
,
reflect
.
Int64
:
return
U2
U256
(
uint64
(
value
.
Int
()))
return
U2
56
(
big
.
NewInt
(
value
.
Int
()))
case
reflect
.
Ptr
:
case
reflect
.
Ptr
:
return
U256
(
value
.
Interface
()
.
(
*
big
.
Int
))
return
U256
(
value
.
Interface
()
.
(
*
big
.
Int
))
}
}
return
nil
return
nil
}
}
...
...
accounts/abi/numbers_test.go
View file @
970f4c06
...
@@ -18,6 +18,7 @@ package abi
...
@@ -18,6 +18,7 @@ package abi
import
(
import
(
"bytes"
"bytes"
"math"
"math/big"
"math/big"
"reflect"
"reflect"
"testing"
"testing"
...
@@ -34,21 +35,38 @@ func TestNumberTypes(t *testing.T) {
...
@@ -34,21 +35,38 @@ func TestNumberTypes(t *testing.T) {
}
}
func
TestPackNumber
(
t
*
testing
.
T
)
{
func
TestPackNumber
(
t
*
testing
.
T
)
{
ubytes
:=
make
([]
byte
,
32
)
tests
:=
[]
struct
{
ubytes
[
31
]
=
1
value
reflect
.
Value
maxunsigned
:=
[]
byte
{
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
}
packed
[]
byte
}{
// Protocol limits
{
reflect
.
ValueOf
(
0
),
[]
byte
{
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
,
0
}},
{
reflect
.
ValueOf
(
1
),
[]
byte
{
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
,
1
}},
{
reflect
.
ValueOf
(
-
1
),
[]
byte
{
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
}},
// Type corner cases
{
reflect
.
ValueOf
(
uint8
(
math
.
MaxUint8
)),
[]
byte
{
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
,
255
}},
{
reflect
.
ValueOf
(
uint16
(
math
.
MaxUint16
)),
[]
byte
{
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
,
255
,
255
}},
{
reflect
.
ValueOf
(
uint32
(
math
.
MaxUint32
)),
[]
byte
{
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
,
255
,
255
,
255
,
255
}},
{
reflect
.
ValueOf
(
uint64
(
math
.
MaxUint64
)),
[]
byte
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
}},
packed
:=
packNum
(
reflect
.
ValueOf
(
1
))
{
reflect
.
ValueOf
(
int8
(
math
.
MaxInt8
)),
[]
byte
{
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
,
127
}},
if
!
bytes
.
Equal
(
packed
,
ubytes
)
{
{
reflect
.
ValueOf
(
int16
(
math
.
MaxInt16
)),
[]
byte
{
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
,
127
,
255
}},
t
.
Errorf
(
"expected %x got %x"
,
ubytes
,
packed
)
{
reflect
.
ValueOf
(
int32
(
math
.
MaxInt32
)),
[]
byte
{
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
,
127
,
255
,
255
,
255
}},
{
reflect
.
ValueOf
(
int64
(
math
.
MaxInt64
)),
[]
byte
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
127
,
255
,
255
,
255
,
255
,
255
,
255
,
255
}},
{
reflect
.
ValueOf
(
int8
(
math
.
MinInt8
)),
[]
byte
{
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
128
}},
{
reflect
.
ValueOf
(
int16
(
math
.
MinInt16
)),
[]
byte
{
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
128
,
0
}},
{
reflect
.
ValueOf
(
int32
(
math
.
MinInt32
)),
[]
byte
{
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
128
,
0
,
0
,
0
}},
{
reflect
.
ValueOf
(
int64
(
math
.
MinInt64
)),
[]
byte
{
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
255
,
128
,
0
,
0
,
0
,
0
,
0
,
0
,
0
}},
}
}
packed
=
packNum
(
reflect
.
ValueOf
(
-
1
))
for
i
,
tt
:=
range
tests
{
if
!
bytes
.
Equal
(
packed
,
maxunsigned
)
{
packed
:=
packNum
(
tt
.
value
)
t
.
Errorf
(
"expected %x got %x"
,
maxunsigned
,
packed
)
if
!
bytes
.
Equal
(
packed
,
tt
.
packed
)
{
t
.
Errorf
(
"test %d: pack mismatch: have %x, want %x"
,
i
,
packed
,
tt
.
packed
)
}
}
}
if
packed
:=
packNum
(
reflect
.
ValueOf
(
"string"
));
packed
!=
nil
{
packed
=
packNum
(
reflect
.
ValueOf
(
"string"
))
if
packed
!=
nil
{
t
.
Errorf
(
"expected 'string' to pack to nil. got %x instead"
,
packed
)
t
.
Errorf
(
"expected 'string' to pack to nil. got %x instead"
,
packed
)
}
}
}
}
...
...
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