Add storing prevhash to local db and logs
This commit is contained in:
parent
03118ba2d7
commit
1002580cd9
|
@ -202,13 +202,15 @@ func getBlock(rpcClient *rpcclient.Client, height int) (*parser.Block, error) {
|
|||
|
||||
|
||||
func handleBlock(db *sql.DB, block *parser.Block) {
|
||||
|
||||
prevBlockHash := hex.EncodeToString(block.GetPrevHash())
|
||||
blockHash := hex.EncodeToString(block.GetEncodableHash())
|
||||
//blockHash := hex.EncodeToString(block.GetDisplayHash())
|
||||
marshaledBlock, _ := proto.Marshal(block.ToCompact())
|
||||
|
||||
|
||||
err := storage.StoreBlock(
|
||||
db,
|
||||
block.GetHeight(),
|
||||
prevBlockHash,
|
||||
blockHash,
|
||||
block.HasSaplingTransactions(),
|
||||
marshaledBlock,
|
||||
|
|
|
@ -93,12 +93,16 @@ func (b *Block) GetHeight() int {
|
|||
return int(blockHeight)
|
||||
}
|
||||
|
||||
func (b *Block) GetPrevHash() []byte {
|
||||
return b.hdr.HashPrevBlock
|
||||
}
|
||||
|
||||
func (b *Block) ToCompact() *walletrpc.CompactBlock {
|
||||
compactBlock := &walletrpc.CompactBlock{
|
||||
//TODO ProtoVersion: 1,
|
||||
Height: uint64(b.GetHeight()),
|
||||
Hash: b.GetEncodableHash(),
|
||||
PrevHash: b.hdr.HashPrevBlock,
|
||||
Hash: b.GetEncodableHash(),
|
||||
Time: b.hdr.Time,
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ func CreateTables(conn *sql.DB) error {
|
|||
blockTable := `
|
||||
CREATE TABLE IF NOT EXISTS blocks (
|
||||
block_height INTEGER PRIMARY KEY,
|
||||
prev_hash TEXT,
|
||||
block_hash TEXT,
|
||||
sapling BOOL,
|
||||
compact_encoding BLOB
|
||||
|
@ -121,15 +122,15 @@ func GetBlockRange(ctx context.Context, db *sql.DB, blockOut chan<- []byte, errO
|
|||
errOut <- nil
|
||||
}
|
||||
|
||||
func StoreBlock(conn *sql.DB, height int, hash string, sapling bool, encoded []byte) error {
|
||||
insertBlock := "REPLACE INTO blocks (block_height, block_hash, sapling, compact_encoding) values (?, ?, ?, ?)"
|
||||
func StoreBlock(conn *sql.DB, height int, prev_hash string, hash string, sapling bool, encoded []byte) error {
|
||||
insertBlock := "REPLACE INTO blocks (block_height, prev_hash, block_hash, sapling, compact_encoding) values ( ?, ?, ?, ?, ?)"
|
||||
|
||||
tx, err := conn.Begin()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("creating db tx %d", height))
|
||||
}
|
||||
|
||||
_, err = tx.Exec(insertBlock, height, hash, sapling, encoded)
|
||||
_, err = tx.Exec(insertBlock, height, prev_hash, hash, sapling, encoded)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("storing compact block %d", height))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue