-
Notifications
You must be signed in to change notification settings - Fork 3
Add proofs to rpc response #96
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA new mechanism for generating and returning Merkle proofs for transactions was implemented in the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant RPC_Core
participant RollkitStore
Client->>RPC_Core: Tx / TxSearch (prove=true)
RPC_Core->>RollkitStore: GetBlockData(height)
RollkitStore-->>RPC_Core: BlockData (transactions)
RPC_Core->>RPC_Core: Build Merkle proof for tx at index
RPC_Core-->>Client: Tx result with Merkle proof
Assessment against linked issues
Suggested reviewers
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 Tip ⚡️ Faster reviews with caching
Enjoy the performance boost—your workflow just got faster. ✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
func proofTXExists(txs []rlktypes.Tx, i uint32) cmttypes.TxProof { | ||
hl := hashList(txs) | ||
root, proofs := merkle.ProofsFromByteSlices(hl) | ||
|
||
return cmttypes.TxProof{ | ||
RootHash: root, | ||
Data: cmttypes.Tx(txs[i]), | ||
Proof: *proofs[i], | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function proofTXExists
could panic if the transaction index i
is out of bounds. Consider adding validation to ensure i < len(txs)
before accessing array elements. This is particularly important since the index comes from external input and could be invalid.
func proofTXExists(txs []rlktypes.Tx, i uint32) (cmttypes.TxProof, error) {
if int(i) >= len(txs) {
return cmttypes.TxProof{}, fmt.Errorf("tx index %d out of bounds (len=%d)", i, len(txs))
}
hl := hashList(txs)
root, proofs := merkle.ProofsFromByteSlices(hl)
return cmttypes.TxProof{
RootHash: root,
Data: cmttypes.Tx(txs[i]),
Proof: *proofs[i],
}, nil
}
The calling functions would need to be updated to handle the error return value.
func proofTXExists(txs []rlktypes.Tx, i uint32) cmttypes.TxProof { | |
hl := hashList(txs) | |
root, proofs := merkle.ProofsFromByteSlices(hl) | |
return cmttypes.TxProof{ | |
RootHash: root, | |
Data: cmttypes.Tx(txs[i]), | |
Proof: *proofs[i], | |
} | |
func proofTXExists(txs []rlktypes.Tx, i uint32) (cmttypes.TxProof, error) { | |
if int(i) >= len(txs) { | |
return cmttypes.TxProof{}, fmt.Errorf("tx index %d out of bounds (len=%d)", i, len(txs)) | |
} | |
hl := hashList(txs) | |
root, proofs := merkle.ProofsFromByteSlices(hl) | |
return cmttypes.TxProof{ | |
RootHash: root, | |
Data: cmttypes.Tx(txs[i]), | |
Proof: *proofs[i], | |
}, nil | |
} | |
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (2)
pkg/rpc/core/tx.go (1)
108-113
: RepeatedGetBlockData
calls hurt performance – cache per block height
TxSearch
may contain many transactions belonging to the same block (e.g. the
two height-10 txs in your new test).
At the moment we fetch and rebuild the whole block data once per tx, which is
O(N·B) instead of O(N+B).A lightweight map keyed by
height
(or even the already-filled
proofCache[height]
) would cut redundant store reads and Merkle calculations.pkg/rpc/core/tx_test.go (1)
53-72
: Consider asserting proof RootHash / Data for stronger guaranteesThe current test only checks
Total
,Index
, and thatLeafHash
is non-empty.
VerifyingRootHash
and thatProof.Data
equals the originalsampleTx
would provide end-to-end confidence that the proof is built from the correct
transaction bytes.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
pkg/rpc/core/tx.go
(4 hunks)pkg/rpc/core/tx_test.go
(6 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
pkg/rpc/core/tx_test.go (4)
pkg/rpc/core/mocks_test.go (1)
MockRollkitStore
(113-115)pkg/rpc/core/env.go (1)
Environment
(27-33)pkg/adapter/adapter.go (1)
Adapter
(53-69)pkg/rpc/core/tx.go (2)
Tx
(22-49)TxSearch
(54-125)
pkg/rpc/core/tx.go (1)
pkg/adapter/adapter.go (1)
Adapter
(53-69)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: lint / golangci-lint
- GitHub Check: test / Run Unit Tests
- GitHub Check: Test with Rollkit Chain
func proofTXExists(txs []rlktypes.Tx, i uint32) cmttypes.TxProof { | ||
hl := hashList(txs) | ||
root, proofs := merkle.ProofsFromByteSlices(hl) | ||
|
||
return cmttypes.TxProof{ | ||
RootHash: root, | ||
Data: cmttypes.Tx(txs[i]), | ||
Proof: *proofs[i], | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Return early when txs
is empty to prevent invalid proofs
merkle.ProofsFromByteSlices
panics on an empty slice. A quick guard keeps the
node stable when encountering an unexpected empty block.
func proofTXExists(txs []rlktypes.Tx, i uint32) cmttypes.TxProof {
+ if len(txs) == 0 {
+ return cmttypes.TxProof{}
+ }
hl := hashList(txs)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
func proofTXExists(txs []rlktypes.Tx, i uint32) cmttypes.TxProof { | |
hl := hashList(txs) | |
root, proofs := merkle.ProofsFromByteSlices(hl) | |
return cmttypes.TxProof{ | |
RootHash: root, | |
Data: cmttypes.Tx(txs[i]), | |
Proof: *proofs[i], | |
} | |
} | |
func proofTXExists(txs []rlktypes.Tx, i uint32) cmttypes.TxProof { | |
if len(txs) == 0 { | |
return cmttypes.TxProof{} | |
} | |
hl := hashList(txs) | |
root, proofs := merkle.ProofsFromByteSlices(hl) | |
return cmttypes.TxProof{ | |
RootHash: root, | |
Data: cmttypes.Tx(txs[i]), | |
Proof: *proofs[i], | |
} | |
} |
func buildProof(ctx *rpctypes.Context, blockHeight int64, txIndex uint32) (cmttypes.TxProof, error) { | ||
_, data, err := env.Adapter.RollkitStore.GetBlockData(ctx.Context(), uint64(blockHeight)) | ||
if err != nil { | ||
return cmttypes.TxProof{}, fmt.Errorf("failed to get block data: %w", err) | ||
} | ||
return proofTXExists(data.Txs, txIndex), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate inputs before accessing the slice
proofTXExists(data.Txs, txIndex)
will panic if txIndex
is out of bounds or
if data
is nil
. This can happen with malformed indices or a corrupted
store response.
func buildProof(ctx *rpctypes.Context, blockHeight int64, txIndex uint32) (cmttypes.TxProof, error) {
- _, data, err := env.Adapter.RollkitStore.GetBlockData(ctx.Context(), uint64(blockHeight))
+ _, data, err := env.Adapter.RollkitStore.GetBlockData(ctx.Context(), uint64(blockHeight))
if err != nil {
return cmttypes.TxProof{}, fmt.Errorf("failed to get block data: %w", err)
}
+ if data == nil || int(txIndex) >= len(data.Txs) {
+ return cmttypes.TxProof{}, fmt.Errorf("tx index %d out of range for height %d", txIndex, blockHeight)
+ }
return proofTXExists(data.Txs, txIndex), nil
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
func buildProof(ctx *rpctypes.Context, blockHeight int64, txIndex uint32) (cmttypes.TxProof, error) { | |
_, data, err := env.Adapter.RollkitStore.GetBlockData(ctx.Context(), uint64(blockHeight)) | |
if err != nil { | |
return cmttypes.TxProof{}, fmt.Errorf("failed to get block data: %w", err) | |
} | |
return proofTXExists(data.Txs, txIndex), nil | |
} | |
func buildProof(ctx *rpctypes.Context, blockHeight int64, txIndex uint32) (cmttypes.TxProof, error) { | |
_, data, err := env.Adapter.RollkitStore.GetBlockData(ctx.Context(), uint64(blockHeight)) | |
if err != nil { | |
return cmttypes.TxProof{}, fmt.Errorf("failed to get block data: %w", err) | |
} | |
if data == nil || int(txIndex) >= len(data.Txs) { | |
return cmttypes.TxProof{}, fmt.Errorf("tx index %d out of range for height %d", txIndex, blockHeight) | |
} | |
return proofTXExists(data.Txs, txIndex), nil | |
} |
var proof cmttypes.TxProof | ||
if prove { | ||
if proof, err = buildProof(ctx, height, index); err != nil { | ||
return nil, err | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard against nil Adapter
/ RollkitStore
to avoid panics
prove == true
unconditionally calls buildProof
, which dereferences
env.Adapter.RollkitStore
. In production or other tests env.Adapter
(or its
RollkitStore
) may legitimately be left nil
, leading to a runtime panic that
will bring the whole RPC server down.
Add an early sanity-check (either here or inside buildProof
) and return a
descriptive error instead of crashing.
if prove {
- if proof, err = buildProof(ctx, height, index); err != nil {
+ if env == nil || env.Adapter == nil || env.Adapter.RollkitStore == nil {
+ return nil, fmt.Errorf("proof requested but rollkit store not configured")
+ }
+ if proof, err = buildProof(ctx, height, index); err != nil {
return nil, err
}
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
var proof cmttypes.TxProof | |
if prove { | |
if proof, err = buildProof(ctx, height, index); err != nil { | |
return nil, err | |
} | |
} | |
var proof cmttypes.TxProof | |
if prove { | |
if env == nil || env.Adapter == nil || env.Adapter.RollkitStore == nil { | |
return nil, fmt.Errorf("proof requested but rollkit store not configured") | |
} | |
if proof, err = buildProof(ctx, height, index); err != nil { | |
return nil, err | |
} | |
} |
t.Run("with proofs", func(t *testing.T) { | ||
query := "tx.height >= 10" | ||
orderBy := "asc" | ||
mockTxIndexer.On("Search", mock.Anything, mock.AnythingOfType("*query.Query")).Run(func(args mock.Arguments) { | ||
// Basic check if the query seems right (optional) | ||
q := args.Get(1).(*cmtquery.Query) | ||
require.NotNil(q) | ||
}).Return(searchResults, nil).Once() | ||
mockStore.On("GetBlockData", mock.Anything, uint64(res1.Height)).Return(nil, | ||
&rktypes.Data{Txs: rktypes.Txs{[]byte{0}, []byte{1}}}, nil).Once() | ||
mockStore.On("GetBlockData", mock.Anything, uint64(res2.Height)).Return(nil, | ||
&rktypes.Data{Txs: rktypes.Txs{[]byte{2}}}, nil).Once() | ||
mockStore.On("GetBlockData", mock.Anything, uint64(res3.Height)).Return(nil, | ||
&rktypes.Data{Txs: rktypes.Txs{[]byte{3}, []byte{4}, []byte{5}}}, nil).Once() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mock expectation only set once for height 10 ⇒ second call fails
TxSearch
builds proofs for both res1
and res3
, each at height 10.
The mock is configured with .Once()
, so the second invocation of
GetBlockData
will trigger:
assert: mock: Unexpected Method Call
Result: the test suite fails.
Either expect the call twice…
-mockStore.On("GetBlockData", mock.Anything, uint64(res1.Height)).Return(...).Once()
+mockStore.On("GetBlockData", mock.Anything, uint64(res1.Height)).Return(...).Twice()
…or use .Maybe()
/ On(...).Return(...).Times(2)
depending on your style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good, nice cleanup
Resolves #89
Add proofs to TX get + search response
Overview
Summary by CodeRabbit
New Features
prove
flag in transaction queries.Tests