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
37e5816b
Commit
37e5816b
authored
Nov 27, 2016
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: use package hexutil for fixed size type encoding
parent
1609df32
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
60 deletions
+30
-60
types.go
common/types.go
+9
-49
types_test.go
common/types_test.go
+21
-11
No files found.
common/types.go
View file @
37e5816b
...
@@ -17,14 +17,12 @@
...
@@ -17,14 +17,12 @@
package
common
package
common
import
(
import
(
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"fmt"
"math/big"
"math/big"
"math/rand"
"math/rand"
"reflect"
"reflect"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
)
)
const
(
const
(
...
@@ -32,8 +30,6 @@ const (
...
@@ -32,8 +30,6 @@ const (
AddressLength
=
20
AddressLength
=
20
)
)
var
hashJsonLengthErr
=
errors
.
New
(
"common: unmarshalJSON failed: hash must be exactly 32 bytes"
)
type
(
type
(
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
Hash
[
HashLength
]
byte
Hash
[
HashLength
]
byte
...
@@ -57,30 +53,16 @@ func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
...
@@ -57,30 +53,16 @@ func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
func
(
h
Hash
)
Str
()
string
{
return
string
(
h
[
:
])
}
func
(
h
Hash
)
Str
()
string
{
return
string
(
h
[
:
])
}
func
(
h
Hash
)
Bytes
()
[]
byte
{
return
h
[
:
]
}
func
(
h
Hash
)
Bytes
()
[]
byte
{
return
h
[
:
]
}
func
(
h
Hash
)
Big
()
*
big
.
Int
{
return
Bytes2Big
(
h
[
:
])
}
func
(
h
Hash
)
Big
()
*
big
.
Int
{
return
Bytes2Big
(
h
[
:
])
}
func
(
h
Hash
)
Hex
()
string
{
return
"0x"
+
Bytes2Hex
(
h
[
:
])
}
func
(
h
Hash
)
Hex
()
string
{
return
hexutil
.
Encode
(
h
[
:
])
}
// UnmarshalJSON parses a hash in its hex from to a hash.
// UnmarshalJSON parses a hash in its hex from to a hash.
func
(
h
*
Hash
)
UnmarshalJSON
(
input
[]
byte
)
error
{
func
(
h
*
Hash
)
UnmarshalJSON
(
input
[]
byte
)
error
{
length
:=
len
(
input
)
return
hexutil
.
UnmarshalJSON
(
"Hash"
,
input
,
h
[
:
])
if
length
>=
2
&&
input
[
0
]
==
'"'
&&
input
[
length
-
1
]
==
'"'
{
input
=
input
[
1
:
length
-
1
]
}
// strip "0x" for length check
if
len
(
input
)
>
1
&&
strings
.
ToLower
(
string
(
input
[
:
2
]))
==
"0x"
{
input
=
input
[
2
:
]
}
// validate the length of the input hash
if
len
(
input
)
!=
HashLength
*
2
{
return
hashJsonLengthErr
}
h
.
SetBytes
(
FromHex
(
string
(
input
)))
return
nil
}
}
// Serialize given hash to JSON
// Serialize given hash to JSON
func
(
h
Hash
)
MarshalJSON
()
([]
byte
,
error
)
{
func
(
h
Hash
)
MarshalJSON
()
([]
byte
,
error
)
{
return
json
.
Marshal
(
h
.
Hex
()
)
return
hexutil
.
Bytes
(
h
[
:
])
.
MarshalJSON
(
)
}
}
// Sets the hash to the value of b. If b is larger than len(h) it will panic
// Sets the hash to the value of b. If b is larger than len(h) it will panic
...
@@ -142,7 +124,7 @@ func (a Address) Str() string { return string(a[:]) }
...
@@ -142,7 +124,7 @@ func (a Address) Str() string { return string(a[:]) }
func
(
a
Address
)
Bytes
()
[]
byte
{
return
a
[
:
]
}
func
(
a
Address
)
Bytes
()
[]
byte
{
return
a
[
:
]
}
func
(
a
Address
)
Big
()
*
big
.
Int
{
return
Bytes2Big
(
a
[
:
])
}
func
(
a
Address
)
Big
()
*
big
.
Int
{
return
Bytes2Big
(
a
[
:
])
}
func
(
a
Address
)
Hash
()
Hash
{
return
BytesToHash
(
a
[
:
])
}
func
(
a
Address
)
Hash
()
Hash
{
return
BytesToHash
(
a
[
:
])
}
func
(
a
Address
)
Hex
()
string
{
return
"0x"
+
Bytes2Hex
(
a
[
:
])
}
func
(
a
Address
)
Hex
()
string
{
return
hexutil
.
Encode
(
a
[
:
])
}
// Sets the address to the value of b. If b is larger than len(a) it will panic
// Sets the address to the value of b. If b is larger than len(a) it will panic
func
(
a
*
Address
)
SetBytes
(
b
[]
byte
)
{
func
(
a
*
Address
)
SetBytes
(
b
[]
byte
)
{
...
@@ -164,34 +146,12 @@ func (a *Address) Set(other Address) {
...
@@ -164,34 +146,12 @@ func (a *Address) Set(other Address) {
// Serialize given address to JSON
// Serialize given address to JSON
func
(
a
Address
)
MarshalJSON
()
([]
byte
,
error
)
{
func
(
a
Address
)
MarshalJSON
()
([]
byte
,
error
)
{
return
json
.
Marshal
(
a
.
Hex
()
)
return
hexutil
.
Bytes
(
a
[
:
])
.
MarshalJSON
(
)
}
}
// Parse address from raw json data
// Parse address from raw json data
func
(
a
*
Address
)
UnmarshalJSON
(
data
[]
byte
)
error
{
func
(
a
*
Address
)
UnmarshalJSON
(
input
[]
byte
)
error
{
if
len
(
data
)
>
2
&&
data
[
0
]
==
'"'
&&
data
[
len
(
data
)
-
1
]
==
'"'
{
return
hexutil
.
UnmarshalJSON
(
"Address"
,
input
,
a
[
:
])
data
=
data
[
1
:
len
(
data
)
-
1
]
}
if
len
(
data
)
>
2
&&
data
[
0
]
==
'0'
&&
data
[
1
]
==
'x'
{
data
=
data
[
2
:
]
}
if
len
(
data
)
!=
2
*
AddressLength
{
return
fmt
.
Errorf
(
"Invalid address length, expected %d got %d bytes"
,
2
*
AddressLength
,
len
(
data
))
}
n
,
err
:=
hex
.
Decode
(
a
[
:
],
data
)
if
err
!=
nil
{
return
err
}
if
n
!=
AddressLength
{
return
fmt
.
Errorf
(
"Invalid address"
)
}
a
.
Set
(
HexToAddress
(
string
(
data
)))
return
nil
}
}
// PP Pretty Prints a byte slice in the following format:
// PP Pretty Prints a byte slice in the following format:
...
...
common/types_test.go
View file @
37e5816b
...
@@ -18,7 +18,10 @@ package common
...
@@ -18,7 +18,10 @@ package common
import
(
import
(
"math/big"
"math/big"
"strings"
"testing"
"testing"
"github.com/ethereum/go-ethereum/common/hexutil"
)
)
func
TestBytesConversion
(
t
*
testing
.
T
)
{
func
TestBytesConversion
(
t
*
testing
.
T
)
{
...
@@ -38,19 +41,26 @@ func TestHashJsonValidation(t *testing.T) {
...
@@ -38,19 +41,26 @@ func TestHashJsonValidation(t *testing.T) {
var
tests
=
[]
struct
{
var
tests
=
[]
struct
{
Prefix
string
Prefix
string
Size
int
Size
int
Error
error
Error
string
}{
}{
{
""
,
2
,
hashJsonLengthErr
},
{
""
,
62
,
hexutil
.
ErrMissingPrefix
.
Error
()},
{
""
,
62
,
hashJsonLengthErr
},
{
"0x"
,
66
,
"hex string has length 66, want 64 for Hash"
},
{
""
,
66
,
hashJsonLengthErr
},
{
"0x"
,
63
,
hexutil
.
ErrOddLength
.
Error
()},
{
""
,
65
,
hashJsonLengthErr
},
{
"0x"
,
0
,
"hex string has length 0, want 64 for Hash"
},
{
"0X"
,
64
,
nil
},
{
"0x"
,
64
,
""
},
{
"0x"
,
64
,
nil
},
{
"0X"
,
64
,
""
},
{
"0x"
,
62
,
hashJsonLengthErr
},
}
}
for
i
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
if
err
:=
h
.
UnmarshalJSON
(
append
([]
byte
(
test
.
Prefix
),
make
([]
byte
,
test
.
Size
)
...
));
err
!=
test
.
Error
{
input
:=
`"`
+
test
.
Prefix
+
strings
.
Repeat
(
"0"
,
test
.
Size
)
+
`"`
t
.
Errorf
(
"test #%d: error mismatch: have %v, want %v"
,
i
,
err
,
test
.
Error
)
err
:=
h
.
UnmarshalJSON
([]
byte
(
input
))
if
err
==
nil
{
if
test
.
Error
!=
""
{
t
.
Errorf
(
"%s: error mismatch: have nil, want %q"
,
input
,
test
.
Error
)
}
}
else
{
if
err
.
Error
()
!=
test
.
Error
{
t
.
Errorf
(
"%s: error mismatch: have %q, want %q"
,
input
,
err
,
test
.
Error
)
}
}
}
}
}
}
}
...
...
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