-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,10 +5,14 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||
"sort" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
"github.com/cometbft/cometbft/crypto/merkle" | ||||||||||||||||||||||||||||||||||||||||||||||||
"github.com/cometbft/cometbft/crypto/tmhash" | ||||||||||||||||||||||||||||||||||||||||||||||||
cmtquery "github.com/cometbft/cometbft/libs/pubsub/query" | ||||||||||||||||||||||||||||||||||||||||||||||||
ctypes "github.com/cometbft/cometbft/rpc/core/types" | ||||||||||||||||||||||||||||||||||||||||||||||||
rpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types" | ||||||||||||||||||||||||||||||||||||||||||||||||
"github.com/cometbft/cometbft/types" | ||||||||||||||||||||||||||||||||||||||||||||||||
cmttypes "github.com/cometbft/cometbft/types" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
rlktypes "github.com/rollkit/rollkit/types" | ||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
// Tx allows you to query the transaction results. `nil` could mean the | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -28,17 +32,12 @@ func Tx(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error | |||||||||||||||||||||||||||||||||||||||||||||||
height := res.Height | ||||||||||||||||||||||||||||||||||||||||||||||||
index := res.Index | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var proof types.TxProof | ||||||||||||||||||||||||||||||||||||||||||||||||
// if prove { | ||||||||||||||||||||||||||||||||||||||||||||||||
// //_, data, _ := env.Adapter.RollkitStore.GetBlockData(unwrappedCtx, uint64(height)) | ||||||||||||||||||||||||||||||||||||||||||||||||
// //blockProof := data.Txs.Proof(int(index)) // TODO: Add proof method to Txs | ||||||||||||||||||||||||||||||||||||||||||||||||
// // proof = types.TxProof{ | ||||||||||||||||||||||||||||||||||||||||||||||||
// // RootHash: blockProof.RootHash, | ||||||||||||||||||||||||||||||||||||||||||||||||
// // Data: types.Tx(blockProof.Data), | ||||||||||||||||||||||||||||||||||||||||||||||||
// // Proof: blockProof.Proof, | ||||||||||||||||||||||||||||||||||||||||||||||||
// // } | ||||||||||||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var proof cmttypes.TxProof | ||||||||||||||||||||||||||||||||||||||||||||||||
if prove { | ||||||||||||||||||||||||||||||||||||||||||||||||
if proof, err = buildProof(ctx, height, index); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
alpe marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||
return &ctypes.ResultTx{ | ||||||||||||||||||||||||||||||||||||||||||||||||
Hash: hash, | ||||||||||||||||||||||||||||||||||||||||||||||||
Height: height, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -106,14 +105,14 @@ func TxSearch( | |||||||||||||||||||||||||||||||||||||||||||||||
for i := skipCount; i < skipCount+pageSize; i++ { | ||||||||||||||||||||||||||||||||||||||||||||||||
r := results[i] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var proof types.TxProof | ||||||||||||||||||||||||||||||||||||||||||||||||
/*if prove { | ||||||||||||||||||||||||||||||||||||||||||||||||
block := nil //env.BlockStore.GetBlock(r.Height) | ||||||||||||||||||||||||||||||||||||||||||||||||
proof = block.Data.Txs.Proof(int(r.Index)) // XXX: overflow on 32-bit machines | ||||||||||||||||||||||||||||||||||||||||||||||||
}*/ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var proof cmttypes.TxProof | ||||||||||||||||||||||||||||||||||||||||||||||||
if prove { | ||||||||||||||||||||||||||||||||||||||||||||||||
if proof, err = buildProof(ctx, r.Height, r.Index); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
apiResults = append(apiResults, &ctypes.ResultTx{ | ||||||||||||||||||||||||||||||||||||||||||||||||
Hash: types.Tx(r.Tx).Hash(), | ||||||||||||||||||||||||||||||||||||||||||||||||
Hash: cmttypes.Tx(r.Tx).Hash(), | ||||||||||||||||||||||||||||||||||||||||||||||||
Height: r.Height, | ||||||||||||||||||||||||||||||||||||||||||||||||
Index: r.Index, | ||||||||||||||||||||||||||||||||||||||||||||||||
TxResult: r.Result, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -124,3 +123,30 @@ func TxSearch( | |||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
return &ctypes.ResultTxSearch{Txs: apiResults, TotalCount: totalCount}, 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) | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
return proofTXExists(data.Txs, txIndex), nil | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
alpe marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
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], | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
alpe marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+135
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Return early when
func proofTXExists(txs []rlktypes.Tx, i uint32) cmttypes.TxProof {
+ if len(txs) == 0 {
+ return cmttypes.TxProof{}
+ }
hl := hashList(txs) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
func hashList(txs []rlktypes.Tx) [][]byte { | ||||||||||||||||||||||||||||||||||||||||||||||||
hl := make([][]byte, len(txs)) | ||||||||||||||||||||||||||||||||||||||||||||||||
for i := 0; i < len(txs); i++ { | ||||||||||||||||||||||||||||||||||||||||||||||||
hl[i] = tmhash.Sum(txs[i]) | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
return hl | ||||||||||||||||||||||||||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.