Skip to content

Commit

Permalink
indexer: drop concept of transaction id, hash is now the primary key
Browse files Browse the repository at this point in the history
 * api: remove endpoint /chain/transactions/reference/index/{index}
  • Loading branch information
altergui committed Aug 27, 2024
1 parent 5086384 commit 3a59d53
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 123 deletions.
38 changes: 0 additions & 38 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,6 @@ func (a *API) enableChainHandlers() error {
); err != nil {
return err
}
if err := a.Endpoint.RegisterMethod(
"/chain/transactions/reference/index/{index}",
"GET",
apirest.MethodAccessTypePublic,
a.chainTxRefByIndexHandler,
); err != nil {
return err
}
if err := a.Endpoint.RegisterMethod(
"/chain/blocks/{height}/transactions/page/{page}",
"GET",
Expand Down Expand Up @@ -705,36 +697,6 @@ func (a *API) chainTxHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) er
return ctx.Send(data, apirest.HTTPstatusOK)
}

// chainTxRefByIndexHandler
//
// @Summary Transaction metadata (by db index)
// @Description Get transaction by its internal index. This is not the transaction hash, and neither the block height and block index. The transaction index is an incremental counter for each transaction. You could use the transaction `block` and `index` to retrieve full info using [transaction by block and index](transaction-by-block-index).
// @Tags Chain
// @Accept json
// @Produce json
// @Param index path int true "Index of the transaction"
// @Success 200 {object} indexertypes.Transaction
// @Success 204 "See [errors](vocdoni-api#errors) section"
// @Router /chain/transactions/reference/index/{index} [get]
func (a *API) chainTxRefByIndexHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
index, err := strconv.ParseUint(ctx.URLParam("index"), 10, 64)
if err != nil {
return err
}
ref, err := a.indexer.GetTxMetadataByID(index)
if err != nil {
if errors.Is(err, indexer.ErrTransactionNotFound) {
return ErrTransactionNotFound
}
return ErrVochainGetTxFailed.WithErr(err)
}
data, err := json.Marshal(ref)
if err != nil {
return err
}
return ctx.Send(data, apirest.HTTPstatusOK)
}

// chainTxListHandler
//
// @Summary List transactions
Expand Down
4 changes: 2 additions & 2 deletions vochain/indexer/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ func BenchmarkFetchTx(b *testing.B) {

startTime := time.Now()
for j := 0; j < numTxs; j++ {
_, err = idx.GetTxMetadataByID(uint64((i * numTxs) + j + 1))
_, err = idx.GetTransactionByHeightAndIndex(int64(i), int64(j))
qt.Assert(b, err, qt.IsNil)
}
log.Infof("fetched %d transactions (out of %d total) by index, took %s",
log.Infof("fetched %d transactions (out of %d total) by height+index, took %s",
numTxs, (i+1)*numTxs, time.Since(startTime))
startTime = time.Now()
for j := 0; j < numTxs; j++ {
Expand Down
10 changes: 5 additions & 5 deletions vochain/indexer/db/blocks.sql.go

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

10 changes: 0 additions & 10 deletions vochain/indexer/db/db.go

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

3 changes: 1 addition & 2 deletions vochain/indexer/db/models.go

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

45 changes: 9 additions & 36 deletions vochain/indexer/db/transactions.sql.go

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

5 changes: 1 addition & 4 deletions vochain/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ func TestTxIndexer(t *testing.T) {

for i := 0; i < totalBlocks; i++ {
for j := 0; j < txsPerBlock; j++ {
ref, err := idx.GetTxMetadataByID(uint64(i*txsPerBlock + j + 1))
ref, err := idx.GetTransactionByHeightAndIndex(int64(i), int64(j))
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, ref.BlockHeight, qt.Equals, uint32(i))
qt.Assert(t, ref.TxBlockIndex, qt.Equals, int32(j))
Expand All @@ -1423,8 +1423,6 @@ func TestTxIndexer(t *testing.T) {
txs, _, err := idx.SearchTransactions(15, 0, 0, "", "", "")
qt.Assert(t, err, qt.IsNil)
for i, tx := range txs {
// Index is between 1 and totalCount.
qt.Assert(t, tx.Index, qt.Equals, uint64(totalTxs-i))
// BlockIndex and TxBlockIndex start at 0, so subtract 1.
qt.Assert(t, tx.BlockHeight, qt.Equals, uint32(totalTxs-i-1)/txsPerBlock)
qt.Assert(t, tx.TxBlockIndex, qt.Equals, int32(totalTxs-i-1)%txsPerBlock)
Expand All @@ -1434,7 +1432,6 @@ func TestTxIndexer(t *testing.T) {
txs, _, err = idx.SearchTransactions(1, 5, 0, "", "", "")
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, txs, qt.HasLen, 1)
qt.Assert(t, txs[0].Index, qt.Equals, uint64(95))
}

func TestCensusUpdate(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions vochain/indexer/indexertypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ type TxPackage struct {

// TransactionMetadata contains tx information for the TransactionList api
type TransactionMetadata struct {
Index uint64 `json:"transactionNumber" format:"int64" example:"944"`
Hash types.HexBytes `json:"transactionHash" swaggertype:"string" example:"75e8f822f5dd13973ac5158d600f0a2a5fea4bfefce9712ab5195bf17884cfad"`
BlockHeight uint32 `json:"blockHeight" format:"int32" example:"64924"`
TxBlockIndex int32 `json:"transactionIndex" format:"int32" example:"0"`
Expand All @@ -189,7 +188,6 @@ type TransactionMetadata struct {

func TransactionMetadataFromDB(dbtx *indexerdb.Transaction) *TransactionMetadata {
return &TransactionMetadata{
Index: uint64(dbtx.ID),
Hash: dbtx.Hash,
BlockHeight: uint32(dbtx.BlockHeight),
TxBlockIndex: int32(dbtx.BlockIndex),
Expand All @@ -201,7 +199,6 @@ func TransactionMetadataFromDB(dbtx *indexerdb.Transaction) *TransactionMetadata

func TransactionMetadataFromDBRow(dbtx *indexerdb.SearchTransactionsRow) *TransactionMetadata {
return &TransactionMetadata{
Index: uint64(dbtx.ID),
Hash: dbtx.Hash,
BlockHeight: uint32(dbtx.BlockHeight),
TxBlockIndex: int32(dbtx.BlockIndex),
Expand Down
62 changes: 62 additions & 0 deletions vochain/indexer/migrations/0015_recreate_table_transactions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- +goose Up
PRAGMA foreign_keys = OFF;

-- Create a new table with hash as primary key
CREATE TABLE transactions_new (
hash BLOB NOT NULL PRIMARY KEY,
block_height INTEGER NOT NULL,
block_index INTEGER NOT NULL,
type TEXT NOT NULL
);

-- Copy data from the old table to the new table
INSERT INTO transactions_new (hash, block_height, block_index, type)
SELECT hash, block_height, block_index, type
FROM transactions;

-- Drop the old table
DROP TABLE transactions;

-- Rename the new table to the old table name
ALTER TABLE transactions_new RENAME TO transactions;

-- Recreate necessary indexes
CREATE INDEX transactions_block_height_index
ON transactions(block_height, block_index);

-- Add new columns
ALTER TABLE transactions ADD COLUMN subtype TEXT NOT NULL DEFAULT '';
ALTER TABLE transactions ADD COLUMN raw_tx BLOB NOT NULL DEFAULT x'';
ALTER TABLE transactions ADD COLUMN signature BLOB NOT NULL DEFAULT x'';
ALTER TABLE transactions ADD COLUMN signer BLOB NOT NULL DEFAULT x'';

PRAGMA foreign_keys = ON;

-- +goose Down
PRAGMA foreign_keys = OFF;

-- Recreate the old table structure
CREATE TABLE transactions (
id INTEGER NOT NULL PRIMARY KEY,
hash BLOB NOT NULL,
block_height INTEGER NOT NULL,
block_index INTEGER NOT NULL,
type TEXT NOT NULL
);

-- Copy data back from the new table to the old table
INSERT INTO transactions (hash, block_height, block_index, type)
SELECT hash, block_height, block_index, type
FROM transactions_new;

-- Drop the new table
DROP TABLE transactions_new;

-- Recreate the old indexes
CREATE INDEX transactions_hash
ON transactions(hash);

CREATE INDEX transactions_block_height_index
ON transactions(block_height, block_index);

PRAGMA foreign_keys = ON;
10 changes: 5 additions & 5 deletions vochain/indexer/queries/blocks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ INSERT INTO blocks(
?, ?, ?, ?, ?, ?
)
ON CONFLICT(height) DO UPDATE
SET chain_id = sqlc.arg(chain_id),
time = sqlc.arg(time),
hash = sqlc.arg(hash),
proposer_address = sqlc.arg(proposer_address),
last_block_hash = sqlc.arg(last_block_hash);
SET chain_id = excluded.chain_id,
time = excluded.time,
hash = excluded.hash,
proposer_address = excluded.proposer_address,
last_block_hash = excluded.last_block_hash;

-- name: GetBlockByHeight :one
SELECT * FROM blocks
Expand Down
Loading

0 comments on commit 3a59d53

Please sign in to comment.