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
f15849cf
Unverified
Commit
f15849cf
authored
Apr 01, 2020
by
Marius van der Wijden
Committed by
GitHub
Apr 01, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
accounts/abi faster unpacking of int256 (#20850)
parent
bf35e27e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
14 deletions
+10
-14
unpack.go
accounts/abi/unpack.go
+9
-13
unpack_test.go
accounts/abi/unpack_test.go
+1
-1
No files found.
accounts/abi/unpack.go
View file @
f15849cf
...
...
@@ -27,13 +27,9 @@ import (
var
(
// MaxUint256 is the maximum value that can be represented by a uint256
MaxUint256
=
big
.
NewInt
(
0
)
.
Add
(
big
.
NewInt
(
0
)
.
Exp
(
big
.
NewInt
(
2
),
big
.
NewInt
(
256
),
nil
),
big
.
NewInt
(
-
1
))
MaxUint256
=
new
(
big
.
Int
)
.
Sub
(
new
(
big
.
Int
)
.
Lsh
(
common
.
Big1
,
256
),
common
.
Big1
)
// MaxInt256 is the maximum value that can be represented by a int256
MaxInt256
=
big
.
NewInt
(
0
)
.
Add
(
big
.
NewInt
(
0
)
.
Exp
(
big
.
NewInt
(
2
),
big
.
NewInt
(
255
),
nil
),
big
.
NewInt
(
-
1
))
MaxInt256
=
new
(
big
.
Int
)
.
Sub
(
new
(
big
.
Int
)
.
Lsh
(
common
.
Big1
,
255
),
common
.
Big1
)
)
// ReadInteger reads the integer based on its kind and returns the appropriate value
...
...
@@ -56,17 +52,17 @@ func ReadInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
case
reflect
.
Int64
:
return
int64
(
binary
.
BigEndian
.
Uint64
(
b
[
len
(
b
)
-
8
:
]))
default
:
// the only case lefts for integer is int256/uint256.
// big.SetBytes can't tell if a number is negative, positive on itself.
// On EVM, if the returned number > max int256, it is negative.
// the only case left for integer is int256/uint256.
ret
:=
new
(
big
.
Int
)
.
SetBytes
(
b
)
if
typ
==
UintTy
{
return
ret
}
if
ret
.
Cmp
(
MaxInt256
)
>
0
{
ret
.
Add
(
MaxUint256
,
big
.
NewInt
(
0
)
.
Neg
(
ret
))
ret
.
Add
(
ret
,
big
.
NewInt
(
1
))
// big.SetBytes can't tell if a number is negative or positive in itself.
// On EVM, if the returned number > max int256, it is negative.
// A number is > max int256 if the bit at position 255 is set.
if
ret
.
Bit
(
255
)
==
1
{
ret
.
Add
(
MaxUint256
,
new
(
big
.
Int
)
.
Neg
(
ret
))
ret
.
Add
(
ret
,
common
.
Big1
)
ret
.
Neg
(
ret
)
}
return
ret
...
...
accounts/abi/unpack_test.go
View file @
f15849cf
...
...
@@ -1009,7 +1009,7 @@ func TestUnpackTuple(t *testing.T) {
t
.
Errorf
(
"unexpected value unpacked: want %x, got %x"
,
1
,
v
.
A
)
}
if
v
.
B
.
Cmp
(
big
.
NewInt
(
-
1
))
!=
0
{
t
.
Errorf
(
"unexpected value unpacked: want %x, got %x"
,
v
.
B
,
-
1
)
t
.
Errorf
(
"unexpected value unpacked: want %x, got %x"
,
-
1
,
v
.
B
)
}
}
...
...
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