Skip to content

Commit

Permalink
many fixups (dirty commit, needs squashing)
Browse files Browse the repository at this point in the history
signer field, nonNullBytes,
indexer: add subtype and signer

todo: add new params to api /chain/transactions
  • Loading branch information
altergui committed Aug 26, 2024
1 parent 440d9ac commit 5730d0c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 29 deletions.
2 changes: 1 addition & 1 deletion api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ type GenericTransactionWithInfo struct {
TxContent json.RawMessage `json:"tx"`
TxInfo *indexertypes.Transaction `json:"txInfo"`
Signature types.HexBytes `json:"signature"`
Account types.HexBytes `json:"account"`
Signer types.HexBytes `json:"signer"`
}

type ChainInfo struct {
Expand Down
4 changes: 3 additions & 1 deletion api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ func (a *API) chainTxHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) er
TxContent: []byte(protoFormat(ref.RawTx)),
TxInfo: ref,
Signature: ref.Signature,
Account: ref.Account,
Signer: ref.Signer,
}
data, err := json.Marshal(tx)
if err != nil {
Expand Down Expand Up @@ -849,6 +849,8 @@ func (a *API) transactionList(params *TransactionParams) (*TransactionsList, err
params.Page*params.Limit,
params.Height,
params.Type,
"", // TODO(gui): support new params
"", //
)
if err != nil {
return nil, ErrIndexerQueryFailed.WithErr(err)
Expand Down
2 changes: 2 additions & 0 deletions vochain/indexer/db/models.go

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

34 changes: 27 additions & 7 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: 5 additions & 0 deletions vochain/indexer/indexertypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ type TransactionMetadata struct {
BlockHeight uint32 `json:"blockHeight" format:"int32" example:"64924"`
TxBlockIndex int32 `json:"transactionIndex" format:"int32" example:"0"`
TxType string `json:"transactionType" enums:"vote,newProcess,admin,setProcess,registerKey,mintTokens,sendTokens,setTransactionCosts,setAccount,collectFaucet,setKeykeeper" example:"Vote"`
TxSubtype string `json:"transactionSubtype" enums:"newProcess,setProcessCensus,setProcessDuration" example:"setProcessCensus"`
}

func TransactionMetadataFromDB(dbtx *indexerdb.Transaction) *TransactionMetadata {
Expand All @@ -192,6 +193,7 @@ func TransactionMetadataFromDB(dbtx *indexerdb.Transaction) *TransactionMetadata
BlockHeight: uint32(dbtx.BlockHeight),
TxBlockIndex: int32(dbtx.BlockIndex),
TxType: dbtx.Type,
TxSubtype: dbtx.Subtype,
}
}

Expand All @@ -202,6 +204,7 @@ func TransactionMetadataFromDBRow(dbtx *indexerdb.SearchTransactionsRow) *Transa
BlockHeight: uint32(dbtx.BlockHeight),
TxBlockIndex: int32(dbtx.BlockIndex),
TxType: dbtx.Type,
TxSubtype: dbtx.Subtype,
}
}

Expand All @@ -210,6 +213,7 @@ type Transaction struct {
*TransactionMetadata
RawTx types.HexBytes `json:"-"`
Signature types.HexBytes `json:"-"`
Signer types.HexBytes `json:"-"`
}

// TransactionFromDB converts an indexerdb.Transaction into a Transaction
Expand All @@ -218,6 +222,7 @@ func TransactionFromDB(dbtx *indexerdb.Transaction) *Transaction {
TransactionMetadata: TransactionMetadataFromDB(dbtx),
RawTx: dbtx.RawTx,
Signature: dbtx.Signature,
Signer: dbtx.Signer,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +goose Up
ALTER TABLE transactions ADD COLUMN subtype TEXT NOT NULL DEFAULT '';
ALTER TABLE transactions ADD COLUMN signer BLOB NOT NULL DEFAULT x'';

-- +goose Down
ALTER TABLE transactions DROP COLUMN signer;
ALTER TABLE transactions DROP COLUMN subtype;
6 changes: 4 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, raw_tx, signature
hash, block_height, block_index, type, subtype, raw_tx, signature, signer
) VALUES (
?, ?, ?, ?, ?, ?
?, ?, ?, ?, ?, ?, ?, ?
);

-- name: GetTransaction :one
Expand Down Expand Up @@ -34,6 +34,8 @@ WITH results AS (
WHERE (
(sqlc.arg(block_height) = 0 OR block_height = sqlc.arg(block_height))
AND (sqlc.arg(tx_type) = '' OR LOWER(type) = LOWER(sqlc.arg(tx_type)))
AND (sqlc.arg(tx_subtype) = '' OR LOWER(subtype) = LOWER(sqlc.arg(tx_subtype)))
AND (sqlc.arg(tx_signer) = '' OR LOWER(HEX(signer)) = LOWER(sqlc.arg(tx_signer)))
)
)
SELECT *, COUNT(*) OVER() AS total_count
Expand Down
33 changes: 16 additions & 17 deletions vochain/indexer/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"strings"

"go.vocdoni.io/dvote/crypto/ethereum"
"go.vocdoni.io/dvote/log"
Expand Down Expand Up @@ -69,9 +70,9 @@ func (idx *Indexer) GetTransactionByHeightAndIndex(blockHeight, blockIndex int64
}

// SearchTransactions returns the list of transactions indexed.
// height and txType are optional, if declared as zero-value will be ignored.
// blockHeight, txType, txSubtype and txSigner are optional, if declared as zero-value will be ignored.
// The first one returned is the newest, so they are in descending order.
func (idx *Indexer) SearchTransactions(limit, offset int, blockHeight uint64, txType string) ([]*indexertypes.TransactionMetadata, uint64, error) {
func (idx *Indexer) SearchTransactions(limit, offset int, blockHeight uint64, txType, txSubtype, txSigner string) ([]*indexertypes.TransactionMetadata, uint64, error) {
if offset < 0 {
return nil, 0, fmt.Errorf("invalid value: offset cannot be %d", offset)
}
Expand All @@ -83,19 +84,15 @@ func (idx *Indexer) SearchTransactions(limit, offset int, blockHeight uint64, tx
Offset: int64(offset),
BlockHeight: blockHeight,
TxType: txType,
TxSubtype: txSubtype,
TxSigner: txSigner,
})
if err != nil {
return nil, 0, err
}
list := []*indexertypes.TransactionMetadata{}
for _, row := range results {
list = append(list, &indexertypes.TransactionMetadata{
Index: uint64(row.ID),
Hash: row.Hash,
BlockHeight: uint32(row.BlockHeight),
TxBlockIndex: int32(row.BlockIndex),
TxType: row.Type,
})
list = append(list, indexertypes.TransactionMetadataFromDBRow(&row))
}
if len(results) == 0 {
return list, 0, nil
Expand All @@ -113,12 +110,14 @@ func (idx *Indexer) OnNewTx(tx *vochaintx.Tx, blockHeight uint32, txIndex int32)
return
}

signer, err := ethereum.AddrFromSignature(tx.SignedBody, tx.Signature)
if err != nil {
if len(tx.Signature) > 0 {
log.Warnf("indexing signed tx with empty signer field, can't recover signer from signature: %s", err)
signer := []byte{}
if len(tx.Signature) > 0 { // not all txs are signed, for example zk ones
addr, err := ethereum.AddrFromSignature(tx.SignedBody, tx.Signature)
if err != nil {
log.Errorw(err, "indexer cannot recover signer from signature")
return
}
// otherwise ignore, since some txs are not signed by design, for example zk ones
signer = addr.Bytes()
}

queries := idx.blockTxQueries()
Expand All @@ -127,10 +126,10 @@ func (idx *Indexer) OnNewTx(tx *vochaintx.Tx, blockHeight uint32, txIndex int32)
BlockHeight: int64(blockHeight),
BlockIndex: int64(txIndex),
Type: tx.TxModelType,
Subtype: strings.ToLower(tx.TxSubType()),
RawTx: rawtx,
Signature: tx.Signature,
Subtype: tx.TxSubType(),
Signer: signer.Bytes(),
Signature: nonNullBytes(tx.Signature),
Signer: nonNullBytes(signer),
}); err != nil {
log.Errorw(err, "cannot index new transaction")
}
Expand Down
5 changes: 4 additions & 1 deletion vochain/transaction/vochaintx/vochaintx.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func (tx *Tx) TxSubType() string {
if txtypeFieldDescriptor == nil {
return ""
}
return fieldValue.Message().Get(txtypeFieldDescriptor).String()
// Get the integer value of txtype as protoreflect.EnumNumber
enumNumber := fieldValue.Message().Get(txtypeFieldDescriptor).Enum()
// Convert the EnumNumber to a string using the EnumType descriptor
return string(txtypeFieldDescriptor.Enum().Values().ByNumber(enumNumber).Name())
}

// TxKey computes the checksum of the tx
Expand Down

0 comments on commit 5730d0c

Please sign in to comment.