storage: some minor storage & logging tweaks
This commit is contained in:
parent
a8e099d0fa
commit
8cb238fd60
|
@ -205,14 +205,14 @@ func handleBlock(db *sql.DB, sequence int, blockData []byte) {
|
||||||
"block_hash": blockHash,
|
"block_hash": blockHash,
|
||||||
"block_version": block.GetVersion(),
|
"block_version": block.GetVersion(),
|
||||||
"tx_count": block.GetTxCount(),
|
"tx_count": block.GetTxCount(),
|
||||||
"has_sapling": block.HasSaplingTransactions(),
|
"sapling": block.HasSaplingTransactions(),
|
||||||
"error": err,
|
"error": err,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
entry.Error("error storing block")
|
entry.Error("new block")
|
||||||
} else {
|
} else {
|
||||||
entry.Info("received new block")
|
entry.Info("new block")
|
||||||
}
|
}
|
||||||
|
|
||||||
for index, tx := range block.Transactions() {
|
for index, tx := range block.Transactions() {
|
||||||
|
@ -229,11 +229,12 @@ func handleBlock(db *sql.DB, sequence int, blockData []byte) {
|
||||||
"block_height": block.GetHeight(),
|
"block_height": block.GetHeight(),
|
||||||
"block_hash": blockHash,
|
"block_hash": blockHash,
|
||||||
"tx_index": index,
|
"tx_index": index,
|
||||||
"has_sapling": tx.HasSaplingTransactions(),
|
"tx_size": len(tx.Bytes()),
|
||||||
|
"sapling": tx.HasSaplingTransactions(),
|
||||||
"error": err,
|
"error": err,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
entry.Error("error storing tx")
|
entry.Error("storing tx")
|
||||||
} else {
|
} else {
|
||||||
entry.Debug("storing tx")
|
entry.Debug("storing tx")
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,9 +27,9 @@ func CreateTables(conn *sql.DB) error {
|
||||||
|
|
||||||
blockTable := `
|
blockTable := `
|
||||||
CREATE TABLE IF NOT EXISTS blocks (
|
CREATE TABLE IF NOT EXISTS blocks (
|
||||||
height INTEGER PRIMARY KEY,
|
block_height INTEGER PRIMARY KEY,
|
||||||
hash TEXT,
|
block_hash TEXT,
|
||||||
has_sapling_tx BOOL,
|
sapling BOOL,
|
||||||
compact_encoding BLOB
|
compact_encoding BLOB
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
|
@ -65,7 +65,7 @@ func GetCurrentHeight(ctx context.Context, db *sql.DB) (int, error) {
|
||||||
|
|
||||||
func GetBlock(ctx context.Context, db *sql.DB, height int) ([]byte, error) {
|
func GetBlock(ctx context.Context, db *sql.DB, height int) ([]byte, error) {
|
||||||
var blockBytes []byte // avoid a copy with *RawBytes
|
var blockBytes []byte // avoid a copy with *RawBytes
|
||||||
query := "SELECT compact_encoding from blocks WHERE height = ?"
|
query := "SELECT compact_encoding from blocks WHERE block_height = ?"
|
||||||
err := db.QueryRowContext(ctx, query, height).Scan(&blockBytes)
|
err := db.QueryRowContext(ctx, query, height).Scan(&blockBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -75,7 +75,7 @@ func GetBlock(ctx context.Context, db *sql.DB, height int) ([]byte, error) {
|
||||||
|
|
||||||
func GetBlockByHash(ctx context.Context, db *sql.DB, hash string) ([]byte, error) {
|
func GetBlockByHash(ctx context.Context, db *sql.DB, hash string) ([]byte, error) {
|
||||||
var blockBytes []byte // avoid a copy with *RawBytes
|
var blockBytes []byte // avoid a copy with *RawBytes
|
||||||
query := "SELECT compact_encoding from blocks WHERE hash = ?"
|
query := "SELECT compact_encoding from blocks WHERE block_hash = ?"
|
||||||
err := db.QueryRowContext(ctx, query, hash).Scan(&blockBytes)
|
err := db.QueryRowContext(ctx, query, hash).Scan(&blockBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, fmt.Sprintf("getting block with hash %s", hash))
|
return nil, errors.Wrap(err, fmt.Sprintf("getting block with hash %s", hash))
|
||||||
|
@ -92,7 +92,7 @@ func GetBlockRange(ctx context.Context, db *sql.DB, blockOut chan<- []byte, errO
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
query := "SELECT compact_encoding from blocks WHERE (height BETWEEN ? AND ?)"
|
query := "SELECT compact_encoding from blocks WHERE (block_height BETWEEN ? AND ?)"
|
||||||
result, err := db.QueryContext(ctx, query, start, end)
|
result, err := db.QueryContext(ctx, query, start, end)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errOut <- err
|
errOut <- err
|
||||||
|
@ -122,7 +122,7 @@ func GetBlockRange(ctx context.Context, db *sql.DB, blockOut chan<- []byte, errO
|
||||||
}
|
}
|
||||||
|
|
||||||
func StoreBlock(conn *sql.DB, height int, hash string, sapling bool, encoded []byte) error {
|
func StoreBlock(conn *sql.DB, height int, hash string, sapling bool, encoded []byte) error {
|
||||||
insertBlock := "INSERT INTO blocks (height, hash, has_sapling_tx, compact_encoding) values (?, ?, ?, ?)"
|
insertBlock := "INSERT INTO blocks (block_height, block_hash, sapling, compact_encoding) values (?, ?, ?, ?)"
|
||||||
_, err := conn.Exec(insertBlock, height, hash, sapling, encoded)
|
_, err := conn.Exec(insertBlock, height, hash, sapling, encoded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, fmt.Sprintf("storing compact block %d", height))
|
return errors.Wrap(err, fmt.Sprintf("storing compact block %d", height))
|
||||||
|
@ -130,13 +130,14 @@ func StoreBlock(conn *sql.DB, height int, hash string, sapling bool, encoded []b
|
||||||
|
|
||||||
currentHeight, err := GetCurrentHeight(context.Background(), conn)
|
currentHeight, err := GetCurrentHeight(context.Background(), conn)
|
||||||
if err != nil || height > currentHeight {
|
if err != nil || height > currentHeight {
|
||||||
|
// TODO database transactions!
|
||||||
err = SetCurrentHeight(conn, height)
|
err = SetCurrentHeight(conn, height)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetCurrentHeight(conn *sql.DB, height int) error {
|
func SetCurrentHeight(conn *sql.DB, height int) error {
|
||||||
update := "UPDATE state SET current_height=?, timestamp=CURRENT_TIMESTAMP WHERE rowid = 1"
|
update := "UPDATE state SET current_height=? WHERE rowid = 1"
|
||||||
result, err := conn.Exec(update, height)
|
result, err := conn.Exec(update, height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "updating state row")
|
return errors.Wrap(err, "updating state row")
|
||||||
|
|
|
@ -98,16 +98,17 @@ func TestSqliteStorage(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
lastBlockTest := compactTests[len(compactTests)-1]
|
lastBlockTest := compactTests[len(compactTests)-1]
|
||||||
|
|
||||||
if blockHeight != lastBlockTest.BlockHeight {
|
if blockHeight != lastBlockTest.BlockHeight {
|
||||||
t.Errorf("Wrong block height, got: %d", blockHeight)
|
t.Errorf("Wrong block height, got: %d", blockHeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
retBlock, err := GetBlock(ctx, db, blockHeight)
|
storedBlock, err := GetBlock(ctx, db, blockHeight)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(errors.Wrap(err, "retrieving stored block"))
|
t.Error(errors.Wrap(err, "retrieving stored block"))
|
||||||
}
|
}
|
||||||
cblock := &rpc.CompactBlock{}
|
cblock := &rpc.CompactBlock{}
|
||||||
err = proto.Unmarshal(retBlock, cblock)
|
err = proto.Unmarshal(storedBlock, cblock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -194,6 +195,41 @@ func TestSqliteStorage(t *testing.T) {
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
t.Errorf("got some blocks that shouldn't be there")
|
t.Errorf("got some blocks that shouldn't be there")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transaction storage
|
||||||
|
{
|
||||||
|
blockData, _ := hex.DecodeString(compactTests[0].Full)
|
||||||
|
block := parser.NewBlock()
|
||||||
|
_, _ = block.ParseFromSlice(blockData)
|
||||||
|
tx := block.Transactions()[0]
|
||||||
|
|
||||||
|
blockHash := hex.EncodeToString(block.GetEncodableHash())
|
||||||
|
txHash := hex.EncodeToString(tx.GetEncodableHash())
|
||||||
|
err = StoreTransaction(
|
||||||
|
db,
|
||||||
|
block.GetHeight(),
|
||||||
|
blockHash,
|
||||||
|
0,
|
||||||
|
txHash,
|
||||||
|
tx.Bytes(),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var storedBytes []byte
|
||||||
|
getTx := "SELECT tx_bytes FROM transactions WHERE tx_hash = ?"
|
||||||
|
err = db.QueryRow(getTx, txHash).Scan(&storedBytes)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(errors.Wrap(err, fmt.Sprintf("error getting a full transaction")))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(storedBytes) != len(tx.Bytes()) {
|
||||||
|
t.Errorf("Wrong tx size, want %d got %d", len(tx.Bytes()), storedBytes)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue