Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TxStatus rpc endpoint #1178

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
mempl "github.com/cometbft/cometbft/mempool"
"github.com/cometbft/cometbft/privval"
cmtstate "github.com/cometbft/cometbft/proto/tendermint/state"
cmtstore "github.com/cometbft/cometbft/proto/tendermint/store"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cometbft/cometbft/proxy"
sm "github.com/cometbft/cometbft/state"
Expand Down Expand Up @@ -1221,6 +1222,7 @@ func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
}
func (bs *mockBlockStore) LoadTxStatus(hash []byte) *cmtstore.TxStatus { return &cmtstore.TxStatus{} }

func (bs *mockBlockStore) LoadBlockCommit(height int64) *types.Commit {
return bs.commits[height-1]
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
203 changes: 197 additions & 6 deletions proto/tendermint/store/types.pb.go

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

5 changes: 5 additions & 0 deletions proto/tendermint/store/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ message BlockStoreState {
int64 base = 1;
int64 height = 2;
}

message TxStatus {
int64 height = 1;
int64 index = 2;
}
ninabarbakadze marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions rpc/core/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ func BlockByHash(ctx *rpctypes.Context, hash []byte) (*ctypes.ResultBlock, error
return &ctypes.ResultBlock{BlockID: blockMeta.BlockID, Block: block}, nil
}

// TxStatus retrieves the status of a transaction given its hash. It returns a ResultTxStatus
// containing the height and index of the transaction within the block.
func TxStatus(hash []byte) (*ctypes.ResultTxStatus, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An explanation of the components of the hash would be beneficial. For example, is this related to the transaction key? What fields are included in the digest?

env := GetEnvironment()
txStatus := env.BlockStore.LoadTxStatus(hash)
if txStatus == nil {
return &ctypes.ResultTxStatus{}, nil
}
return &ctypes.ResultTxStatus{Height: txStatus.Height, Index: txStatus.Index}, nil
}

// Commit gets block commit at a given height.
// If no height is provided, it will fetch the commit for the latest block.
// More: https://docs.cometbft.com/v0.34/rpc/#/Info/commit
Expand Down
56 changes: 56 additions & 0 deletions rpc/core/blocks_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package core

import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"testing"
Expand All @@ -17,6 +19,7 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
cmtstate "github.com/cometbft/cometbft/proto/tendermint/state"
cmtstore "github.com/cometbft/cometbft/proto/tendermint/store"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
rpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
sm "github.com/cometbft/cometbft/state"
Expand Down Expand Up @@ -123,6 +126,31 @@ func TestBlockResults(t *testing.T) {
}
}

func TestTxStatus(t *testing.T) {
env := &Environment{}
height := int64(50)

blocks := randomBlocks(height)
blockStore := mockBlockStore{
height: height,
blocks: blocks,
}
env.BlockStore = blockStore

SetEnvironment(env)

// Iterate over each block
for _, block := range blocks {
// Iterate over each transaction in the block
for i, tx := range block.Data.Txs {
txStatus, _ := TxStatus(tx.Hash())
assert.Equal(t, block.Height, txStatus.Height)
assert.Equal(t, int64(i), txStatus.Index)
}
}

}

func TestEncodeDataRootTuple(t *testing.T) {
height := uint64(2)
dataRoot, err := hex.DecodeString("82dc1607d84557d3579ce602a45f5872e821c36dbda7ec926dfa17ebc8d5c013")
Expand Down Expand Up @@ -318,6 +346,21 @@ func (store mockBlockStore) LoadBlock(height int64) *types.Block {
return store.blocks[height]
}

func (store mockBlockStore) LoadTxStatus(hash []byte) *cmtstore.TxStatus {
for _, block := range store.blocks {
for i, tx := range block.Data.Txs {
// Check if transaction hash matches
if bytes.Equal(tx.Hash(), hash) {
return &cmtstore.TxStatus{
Height: block.Header.Height,
Index: int64(i),
}
}
}
}
return nil
}

// mockBlockIndexer used to mock the set of indexed blocks and return a predefined one.
type mockBlockIndexer struct {
height int64
Expand Down Expand Up @@ -349,12 +392,25 @@ func randomBlocks(height int64) []*types.Block {
return blocks
}

func makeTxs(height int64) (txs []types.Tx) {
for i := 0; i < 10; i++ {
numBytes := make([]byte, 8)
binary.BigEndian.PutUint64(numBytes, uint64(height))

txs = append(txs, types.Tx(append(numBytes, byte(i))))
}
return txs
}

// randomBlock generates a Block with a certain height and random data hash.
func randomBlock(height int64) *types.Block {
return &types.Block{
Header: types.Header{
Height: height,
DataHash: cmtrand.Bytes(32),
},
Data: types.Data{
Txs: makeTxs(height),
},
}
}
Loading
Loading