use chainhash.Hash types from lbcd where appropriate

This commit is contained in:
Jeffrey Picard 2022-04-11 03:19:59 +00:00
parent 167f62c845
commit e2f869f51f
3 changed files with 31 additions and 20 deletions

View file

@ -18,6 +18,7 @@ import (
"strings"
"github.com/lbryio/hub/internal"
"github.com/lbryio/lbcd/chaincfg/chainhash"
)
const (
@ -113,10 +114,10 @@ type DBStateKey struct {
}
type DBStateValue struct {
Genesis []byte
Genesis *chainhash.Hash
Height uint32
TxCount uint32
Tip []byte
Tip *chainhash.Hash
UtxoFlushCount uint32
WallTime uint32
FirstSync bool
@ -129,10 +130,10 @@ type DBStateValue struct {
func NewDBStateValue() *DBStateValue {
return &DBStateValue{
Genesis: make([]byte, 32),
Genesis: new(chainhash.Hash),
Height: 0,
TxCount: 0,
Tip: make([]byte, 32),
Tip: new(chainhash.Hash),
UtxoFlushCount: 0,
WallTime: 0,
FirstSync: true,
@ -220,11 +221,13 @@ func DBStateKeyUnpack(key []byte) *DBStateKey {
}
func DBStateValueUnpack(value []byte) *DBStateValue {
genesis := (*chainhash.Hash)(value[:32])
tip := (*chainhash.Hash)(value[32+4+4 : 32+4+4+32])
x := &DBStateValue{
Genesis: value[:32],
Genesis: genesis,
Height: binary.BigEndian.Uint32(value[32:]),
TxCount: binary.BigEndian.Uint32(value[32+4:]),
Tip: value[32+4+4 : 32+4+4+32],
Tip: tip,
UtxoFlushCount: binary.BigEndian.Uint32(value[32+4+4+32:]),
WallTime: binary.BigEndian.Uint32(value[32+4+4+32+4:]),
FirstSync: value[32+4+4+32+4+4] == 1,
@ -583,7 +586,7 @@ type BlockHashKey struct {
}
type BlockHashValue struct {
BlockHash []byte `json:"block_hash"`
BlockHash *chainhash.Hash `json:"block_hash"`
}
func NewBlockHashKey(height uint32) *BlockHashKey {
@ -670,8 +673,10 @@ func BlockHashKeyUnpack(key []byte) *BlockHashKey {
}
func BlockHashValueUnpack(value []byte) *BlockHashValue {
// hash := chainhash.Hash{}
hash := (*chainhash.Hash)(value)
return &BlockHashValue{
BlockHash: value[:32],
BlockHash: hash,
}
}
@ -681,7 +686,7 @@ type BlockTxsKey struct {
}
type BlockTxsValue struct {
TxHashes [][]byte `json:"tx_hashes"`
TxHashes []*chainhash.Hash `json:"tx_hashes"`
}
func (k *BlockTxsKey) NewBlockTxsKey(height uint32) *BlockTxsKey {
@ -712,7 +717,7 @@ func (v *BlockTxsValue) PackValue() []byte {
log.Println("Warning, txhash not 32 bytes", tx)
return nil
}
copy(value[i*32:], tx)
copy(value[i*32:], tx[:])
}
return value
@ -773,9 +778,9 @@ func BlockTxsKeyUnpack(key []byte) *BlockTxsKey {
func BlockTxsValueUnpack(value []byte) *BlockTxsValue {
numHashes := len(value) / 32
txs := make([][]byte, numHashes)
txs := make([]*chainhash.Hash, numHashes)
for i := 0; i < numHashes; i++ {
txs[i] = value[i*32 : (i+1)*32]
txs[i] = (*chainhash.Hash)(value[i*32 : (i+1)*32])
}
return &BlockTxsValue{
TxHashes: txs,
@ -881,7 +886,7 @@ type TxHashKey struct {
}
type TxHashValue struct {
TxHash []byte `json:"tx_hash"`
TxHash *chainhash.Hash `json:"tx_hash"`
}
func NewTxHashKey(txNum uint32) *TxHashKey {
@ -965,13 +970,13 @@ func TxHashKeyUnpack(key []byte) *TxHashKey {
func TxHashValueUnpack(value []byte) *TxHashValue {
return &TxHashValue{
TxHash: value,
TxHash: (*chainhash.Hash)(value),
}
}
type TxNumKey struct {
Prefix []byte `json:"prefix"`
TxHash []byte `json:"tx_hash"`
Prefix []byte `json:"prefix"`
TxHash *chainhash.Hash `json:"tx_hash"`
}
type TxNumValue struct {
@ -1045,7 +1050,7 @@ func TxNumKeyUnpack(key []byte) *TxNumKey {
prefixLen := 1
return &TxNumKey{
Prefix: key[:prefixLen],
TxHash: key[prefixLen : prefixLen+32],
TxHash: (*chainhash.Hash)(key[prefixLen : prefixLen+32]),
}
}
@ -1056,8 +1061,8 @@ func TxNumValueUnpack(value []byte) *TxNumValue {
}
type TxKey struct {
Prefix []byte `json:"prefix"`
TxHash []byte `json:"tx_hash"`
Prefix []byte `json:"prefix"`
TxHash *chainhash.Hash `json:"tx_hash"`
}
type TxValue struct {
@ -1131,7 +1136,7 @@ func TxKeyUnpack(key []byte) *TxKey {
prefixLen := 1
return &TxKey{
Prefix: key[:prefixLen],
TxHash: key[prefixLen : prefixLen+32],
TxHash: (*chainhash.Hash)(key[prefixLen : prefixLen+32]),
}
}

3
go.mod
View file

@ -20,6 +20,8 @@ require (
gopkg.in/karalabe/cookiejar.v1 v1.0.0-20141109175019-e1490cae028c
)
require golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b // indirect
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
@ -28,6 +30,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/lbryio/lbcd v0.22.201-beta-rc1
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/nlopes/slack v0.6.0 // indirect

3
go.sum
View file

@ -347,6 +347,8 @@ github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL
github.com/lbryio/lbcd v0.22.100-beta/go.mod h1:u8SaFX4xdGMMR5xasBGfgApC8pvD4rnK2OujZnrq5gs=
github.com/lbryio/lbcd v0.22.100-beta-rc5/go.mod h1:9PbFSlHYX7WlnDQwcTxHVf1W35VAnRsattCSyKOO55g=
github.com/lbryio/lbcd v0.22.200-beta/go.mod h1:kNuzGWf808ipTGB0y0WogzsGv5BVM4Qv85Z+JYwC9FA=
github.com/lbryio/lbcd v0.22.201-beta-rc1 h1:FmzzApVj2RBXloLM2w9tLvN2xyTZjeyh+QC7GIw/wwo=
github.com/lbryio/lbcd v0.22.201-beta-rc1/go.mod h1:kNuzGWf808ipTGB0y0WogzsGv5BVM4Qv85Z+JYwC9FA=
github.com/lbryio/lbcutil v1.0.201/go.mod h1:gDHc/b+Rdz3J7+VB8e5/Bl9roVf8Q5/8FQCyuK9dXD0=
github.com/lbryio/lbcutil v1.0.202-rc3/go.mod h1:LGPtVBBzh4cFXfLFb8ginlFcbA2QwumLNFd0yk/as2o=
github.com/lbryio/lbry.go/v2 v2.7.1/go.mod h1:sUhhSKqPNkiwgBqvBzJIqfLLzGH8hkDGrrO/HcaXzFc=
@ -619,6 +621,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b h1:QAqMVf3pSa6eeTsuklijukjXBlj7Es2QQplab+/RbQ4=
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=