Skip to content

Commit

Permalink
indexer: add raw_tx in transactions table, to index the raw proto.Mar…
Browse files Browse the repository at this point in the history
…shal(tx.Tx)
  • Loading branch information
altergui committed Jul 8, 2024
1 parent 5da65a6 commit fae0ff0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
7 changes: 6 additions & 1 deletion vochain/indexer/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func BenchmarkIndexer(b *testing.B) {
tx := &vochaintx.Tx{
TxID: rnd.Random32(),
TxModelType: "vote",
Tx: &models.Tx{Payload: &models.Tx_Vote{}},
}
idx.OnNewTx(tx, height, txBlockIndex)
curTxs = append(curTxs, tx)
Expand Down Expand Up @@ -138,7 +139,11 @@ func BenchmarkFetchTx(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < numTxs; j++ {
idx.OnNewTx(&vochaintx.Tx{TxID: util.Random32()}, uint32(i), int32(j))
idx.OnNewTx(&vochaintx.Tx{
TxID: util.Random32(),
TxModelType: "vote",
Tx: &models.Tx{Payload: &models.Tx_Vote{}},
}, uint32(i), int32(j))
}
err := idx.Commit(uint32(i))
qt.Assert(b, err, qt.IsNil)
Expand Down
1 change: 1 addition & 0 deletions vochain/indexer/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions vochain/indexer/db/transactions.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vochain/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ func TestTxIndexer(t *testing.T) {
idx.OnNewTx(&vochaintx.Tx{
TxID: getTxID(i, j),
TxModelType: "setAccount",
Tx: &models.Tx{Payload: &models.Tx_SetAccount{}},
}, uint32(i), int32(j))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- +goose Up
ALTER TABLE transactions ADD COLUMN raw_tx BLOB NOT NULL DEFAULT x'';

-- +goose Down
ALTER TABLE transactions DROP COLUMN raw_tx;
4 changes: 2 additions & 2 deletions vochain/indexer/queries/transactions.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- name: CreateTransaction :execresult
INSERT INTO transactions (
hash, block_height, block_index, type
hash, block_height, block_index, type, raw_tx
) VALUES (
?, ?, ?, ?
?, ?, ?, ?, ?
);

-- name: GetTransaction :one
Expand Down
9 changes: 9 additions & 0 deletions vochain/indexer/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
indexerdb "go.vocdoni.io/dvote/vochain/indexer/db"
"go.vocdoni.io/dvote/vochain/indexer/indexertypes"
"go.vocdoni.io/dvote/vochain/transaction/vochaintx"
"google.golang.org/protobuf/proto"
)

// ErrTransactionNotFound is returned if the transaction is not found.
Expand Down Expand Up @@ -84,12 +85,20 @@ func (idx *Indexer) GetLastTransactions(limit, offset int32) ([]*indexertypes.Tr
func (idx *Indexer) OnNewTx(tx *vochaintx.Tx, blockHeight uint32, txIndex int32) {
idx.blockMu.Lock()
defer idx.blockMu.Unlock()

rawtx, err := proto.Marshal(tx.Tx)
if err != nil {
log.Errorw(err, "indexer cannot marshal new transaction")
return
}

queries := idx.blockTxQueries()
if _, err := queries.CreateTransaction(context.TODO(), indexerdb.CreateTransactionParams{
Hash: tx.TxID[:],
BlockHeight: int64(blockHeight),
BlockIndex: int64(txIndex),
Type: tx.TxModelType,
RawTx: rawtx,
}); err != nil {
log.Errorw(err, "cannot index new transaction")
}
Expand Down

0 comments on commit fae0ff0

Please sign in to comment.