diff options
author | Meng-Ying Yang <garfield@dexon.org> | 2018-12-25 16:51:08 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:20 +0800 |
commit | a96cea486d4a74b7bd8ada1f004b219e7d8203c4 (patch) | |
tree | 9c91a0919037eab407e23ef462e006258c239dfd /core/types/block.go | |
parent | 9437d34cb57503309b25ea92808705da1d78ac44 (diff) | |
download | go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar.gz go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.tar.zst go-tangerine-a96cea486d4a74b7bd8ada1f004b219e7d8203c4.zip |
core: add database/sql support for more types (#102)
* core: types: add database/sql support for BlockNonce
* common: add database/sql support with Big
New Big type is declared to let big.Int support database/sql by
implementing Scan() and Value() on new type.
Diffstat (limited to 'core/types/block.go')
-rw-r--r-- | core/types/block.go | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/core/types/block.go b/core/types/block.go index 23da3ea77..96df245b7 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -18,7 +18,9 @@ package types import ( + "database/sql/driver" "encoding/binary" + "fmt" "io" "math/big" "sort" @@ -37,10 +39,15 @@ var ( EmptyUncleHash = CalcUncleHash(nil) ) +const ( + // Length of block nonce in bytes. + BlockNonceLength = 8 +) + // A BlockNonce is a 64-bit hash which proves (combined with the // mix-hash) that a sufficient amount of computation has been carried // out on a block. -type BlockNonce [8]byte +type BlockNonce [BlockNonceLength]byte // EncodeNonce converts the given integer to a block nonce. func EncodeNonce(i uint64) BlockNonce { @@ -64,6 +71,24 @@ func (n *BlockNonce) UnmarshalText(input []byte) error { return hexutil.UnmarshalFixedText("BlockNonce", input, n[:]) } +// Scan implements Scanner for database/sql. +func (n *BlockNonce) Scan(src interface{}) error { + srcB, ok := src.([]byte) + if !ok { + return fmt.Errorf("can't scan %T into BlockNonce", src) + } + if len(srcB) != BlockNonceLength { + return fmt.Errorf("can't scan []byte of len %d into BlockNonce, want %d", len(srcB), BlockNonceLength) + } + copy(n[:], srcB) + return nil +} + +// Value implements valuer for database/sql. +func (n BlockNonce) Value() (driver.Value, error) { + return n[:], nil +} + // WitnessData represents the witness data. type WitnessData struct { Root common.Hash |