mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
common: add Hash Scan and Value
This commit is contained in:
parent
9f7cd75682
commit
372d7124d3
2 changed files with 140 additions and 0 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
@ -120,6 +121,33 @@ func EmptyHash(h Hash) bool {
|
||||||
return h == Hash{}
|
return h == Hash{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Value converts the Hash into a SQL driver value which can be used to
|
||||||
|
// directly use the HASH as parameter to a SQL query.
|
||||||
|
func (h Hash) Value() (driver.Value, error) {
|
||||||
|
return h.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan allows scanning from (byte slice) to *Hash
|
||||||
|
func (h *Hash) Scan(src interface{}) error {
|
||||||
|
switch v := src.(type) {
|
||||||
|
case []byte:
|
||||||
|
return h.scan(v)
|
||||||
|
case string:
|
||||||
|
return h.scan([]byte(v))
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Scan: unable to scan type %T into Hash", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hash) scan(b []byte) error {
|
||||||
|
if len(b) != HashLength {
|
||||||
|
return fmt.Errorf("Scan: unable to scan into Hash, wrong size %d", len(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
*h = BytesToHash(b)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// UnprefixedHash allows marshaling a Hash without 0x prefix.
|
// UnprefixedHash allows marshaling a Hash without 0x prefix.
|
||||||
type UnprefixedHash Hash
|
type UnprefixedHash Hash
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -125,3 +125,115 @@ func BenchmarkAddressHex(b *testing.B) {
|
||||||
testAddr.Hex()
|
testAddr.Hex()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHash_Scan(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
src interface{}
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "working scan",
|
||||||
|
src: []byte{
|
||||||
|
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0x10, 0x00,
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "non working scan, int",
|
||||||
|
src: int64(1234567890),
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "non working scan, wrong size",
|
||||||
|
src: []byte{
|
||||||
|
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0x10,
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
h := &Hash{}
|
||||||
|
if err := h.Scan(tt.src); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !tt.wantErr {
|
||||||
|
for i := range h {
|
||||||
|
if h[i] != tt.src.([]byte)[i] {
|
||||||
|
t.Errorf(
|
||||||
|
"Hash.Scan() didn't scan the %d src correctly (have %X, want %X)",
|
||||||
|
i, h[i], tt.src.([]byte)[i],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkHash_Scan(b *testing.B) {
|
||||||
|
tst := []byte{
|
||||||
|
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0x10, 0x00,
|
||||||
|
}
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
h := &Hash{}
|
||||||
|
_ = h.Scan(tst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHash_Value(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
h Hash
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Working hash",
|
||||||
|
h: Hash([HashLength]byte{
|
||||||
|
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0x10, 0x00,
|
||||||
|
}),
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := tt.h.Value()
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i, gotI := range got.([]byte) {
|
||||||
|
if gotI != tt.h[i] {
|
||||||
|
t.Errorf("Hash.Value() non matching data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkHash_Value(b *testing.B) {
|
||||||
|
h := Hash([HashLength]byte{
|
||||||
|
0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
|
||||||
|
0x10, 0x00,
|
||||||
|
})
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _ = h.Value()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue