Skip to content

Commit

Permalink
enable to query withdrawals that their tree is not finalized yet
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Nov 19, 2024
1 parent 3ec9bfd commit 9644a45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
40 changes: 22 additions & 18 deletions executor/child/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package child

import (
"encoding/json"
"errors"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"

executortypes "github.com/initia-labs/opinit-bots/executor/types"
merkletypes "github.com/initia-labs/opinit-bots/merkle/types"
)

func (ch Child) QueryWithdrawal(sequence uint64) (executortypes.QueryWithdrawalResponse, error) {
Expand All @@ -16,33 +18,35 @@ func (ch Child) QueryWithdrawal(sequence uint64) (executortypes.QueryWithdrawalR
return executortypes.QueryWithdrawalResponse{}, err
}

amount := sdk.NewCoin(withdrawal.BaseDenom, math.NewIntFromUint64(withdrawal.Amount))

res := executortypes.QueryWithdrawalResponse{
BridgeId: ch.BridgeId(),
From: withdrawal.From,
To: withdrawal.To,
Sequence: sequence,
Amount: amount,
Version: []byte{ch.Version()},
}

proofs, outputIndex, outputRoot, extraDataBytes, err := ch.Merkle().GetProofs(sequence)
if err != nil {
if errors.Is(err, merkletypes.ErrUnfinalizedTree) {
// if the tree is not finalized, we just return only withdrawal info
return res, nil
} else if err != nil {
return executortypes.QueryWithdrawalResponse{}, err
}

amount := sdk.NewCoin(withdrawal.BaseDenom, math.NewIntFromUint64(withdrawal.Amount))

treeExtraData := executortypes.TreeExtraData{}
err = json.Unmarshal(extraDataBytes, &treeExtraData)
if err != nil {
return executortypes.QueryWithdrawalResponse{}, err
}

return executortypes.QueryWithdrawalResponse{
BridgeId: ch.BridgeId(),
OutputIndex: outputIndex,
WithdrawalProofs: proofs,
From: withdrawal.From,
To: withdrawal.To,
Sequence: sequence,
Amount: amount,
Version: []byte{ch.Version()},
StorageRoot: outputRoot,
LastBlockHash: treeExtraData.BlockHash,
// BlockNumber: treeExtraData.BlockNumber,
// WithdrawalHash: withdrawal.WithdrawalHash,
}, nil
res.WithdrawalProofs = proofs
res.OutputIndex = outputIndex
res.StorageRoot = outputRoot
res.LastBlockHash = treeExtraData.BlockHash
return res, nil
}

func (ch Child) QueryWithdrawals(address string, offset uint64, limit uint64, descOrder bool) (executortypes.QueryWithdrawalsResponse, error) {
Expand Down
6 changes: 4 additions & 2 deletions merkle/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ func (m *Merkle) InsertLeaf(data []byte) error {
// GetProofs returns the proofs for the leaf with the given index.
func (m *Merkle) GetProofs(leafIndex uint64) (proofs [][]byte, treeIndex uint64, rootData []byte, extraData []byte, err error) {
_, value, err := m.db.SeekPrevInclusiveKey(merkletypes.FinalizedTreeKey, merkletypes.PrefixedFinalizedTreeKey(leafIndex))
if err != nil {
if errors.Is(err, dbtypes.ErrNotFound) {
return nil, 0, nil, nil, merkletypes.ErrUnfinalizedTree
} else if err != nil {
return nil, 0, nil, nil, err
}

Expand All @@ -313,7 +315,7 @@ func (m *Merkle) GetProofs(leafIndex uint64) (proofs [][]byte, treeIndex uint64,
if leafIndex < treeInfo.StartLeafIndex {
return nil, 0, nil, nil, fmt.Errorf("leaf (`%d`) is not found in tree (`%d`)", leafIndex, treeInfo.TreeIndex)
} else if leafIndex-treeInfo.StartLeafIndex >= treeInfo.LeafCount {
return nil, 0, nil, nil, fmt.Errorf("the tree containing the leaf (`%d`) has not been finalized yet", leafIndex)
return nil, 0, nil, nil, merkletypes.ErrUnfinalizedTree
}

height := uint8(0)
Expand Down
5 changes: 5 additions & 0 deletions merkle/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

import "errors"

var ErrUnfinalizedTree = errors.New("unfinalized tree")

0 comments on commit 9644a45

Please sign in to comment.