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

add timestamp & tx hash to withdrawals #54

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ linters:
- dogsled
- dupl
- errcheck
- exportloopref
- copyloopvar
- goconst
- gofmt
- goimports
Expand All @@ -23,12 +23,12 @@ linters:
- typecheck
- unconvert
- unused

issues:
exclude-rules:
- path: _test\.go
linters:
- gosec
- unused
- linters:
- stylecheck
text: "ST1003:"
Expand Down
2 changes: 1 addition & 1 deletion executor/batchsubmitter/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSaveGetLocalBatchInfo(t *testing.T) {
localBatchInfo := executortypes.LocalBatchInfo{
Start: 1,
End: 2,
LastSubmissionTime: time.Unix(0, 10000),
LastSubmissionTime: time.Unix(0, 10000).UTC(),
BatchSize: 1000,
}

Expand Down
3 changes: 1 addition & 2 deletions executor/celestia/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

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

btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
"github.com/initia-labs/opinit-bots/txutils"
celestiatypes "github.com/initia-labs/opinit-bots/types/celestia"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ func (c *Celestia) BuildTxWithMessages(
return nil, "", errors.Wrap(err, "failed to marshal blob tx")
}

return blobTxBytes, btypes.TxHash(txBytes), nil
return blobTxBytes, txutils.TxHash(txBytes), nil
}

func (c *Celestia) PendingTxToProcessedMsgs(
Expand Down
3 changes: 3 additions & 0 deletions executor/child/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func (ch Child) QueryWithdrawal(sequence uint64) (executortypes.QueryWithdrawalR
Sequence: sequence,
Amount: amount,
Version: []byte{ch.Version()},
TxHeight: withdrawal.TxHeight,
TxTime: withdrawal.TxTime,
TxHash: withdrawal.TxHash,
}

proofs, outputIndex, outputRoot, extraDataBytes, err := merkle.GetProofs(ch.DB(), sequence)
Expand Down
52 changes: 49 additions & 3 deletions executor/child/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package child

import (
"encoding/json"
"fmt"
"testing"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
Expand Down Expand Up @@ -31,6 +32,9 @@ func saveQueryData(t *testing.T, db types.DB) {
Amount: 100,
BaseDenom: "baseDenom",
WithdrawalHash: []byte("withdrawalHash"),
TxTime: int64(i),
TxHeight: int64(i),
TxHash: fmt.Sprintf("txHash%d", i),
}
err := SaveWithdrawal(db, withdrawal)
require.NoError(t, err)
Expand All @@ -44,12 +48,15 @@ func saveQueryData(t *testing.T, db types.DB) {
Amount: 1000,
BaseDenom: "baseDenom",
WithdrawalHash: []byte("withdrawalHash"),
TxTime: int64(i),
TxHeight: int64(i),
TxHash: fmt.Sprintf("txHash%d", i),
}
err := SaveWithdrawal(db, withdrawal)
require.NoError(t, err)
}

extraData := executortypes.NewTreeExtraData(10, []byte("00000000000000000000000blockid10"))
extraData := executortypes.NewTreeExtraData(10, 10, []byte("00000000000000000000000blockid10"))
extraDataBz, err := json.Marshal(extraData)
require.NoError(t, err)

Expand All @@ -73,7 +80,7 @@ func saveQueryData(t *testing.T, db types.DB) {
}...)
require.NoError(t, err)

extraData = executortypes.NewTreeExtraData(100, []byte("0000000000000000000000blockid100"))
extraData = executortypes.NewTreeExtraData(100, 100, []byte("0000000000000000000000blockid100"))
extraDataBz, err = json.Marshal(extraData)
require.NoError(t, err)
err = merkle.SaveFinalizedTree(db, merkletypes.FinalizedTreeInfo{
Expand Down Expand Up @@ -104,7 +111,7 @@ func saveQueryData(t *testing.T, db types.DB) {
}...)
require.NoError(t, err)

extraData = executortypes.NewTreeExtraData(1000, []byte("000000000000000000000blockid1000"))
extraData = executortypes.NewTreeExtraData(1000, 1000, []byte("000000000000000000000blockid1000"))
extraDataBz, err = json.Marshal(extraData)
require.NoError(t, err)
err = merkle.SaveFinalizedTree(db, merkletypes.FinalizedTreeInfo{
Expand Down Expand Up @@ -161,6 +168,9 @@ func TestQueryWithdrawal(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("000000000000000000000000hash1234"),
LastBlockHash: []byte("00000000000000000000000blockid10"),
TxTime: 1,
TxHeight: 1,
TxHash: "txHash1",
},
expected: true,
},
Expand All @@ -182,6 +192,9 @@ func TestQueryWithdrawal(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("00000000000000000000hash56789999"),
LastBlockHash: []byte("0000000000000000000000blockid100"),
TxTime: 5,
TxHeight: 5,
TxHash: "txHash5",
},
expected: true,
},
Expand Down Expand Up @@ -209,6 +222,9 @@ func TestQueryWithdrawal(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("00000000000000000000hash56789999"),
LastBlockHash: []byte("0000000000000000000000blockid100"),
TxTime: 7,
TxHeight: 7,
TxHash: "txHash7",
},
expected: true,
},
Expand All @@ -226,6 +242,9 @@ func TestQueryWithdrawal(t *testing.T) {
Version: []byte{0},
StorageRoot: nil,
LastBlockHash: nil,
TxTime: 11,
TxHeight: 11,
TxHash: "txHash11",
},
expected: true,
},
Expand Down Expand Up @@ -301,6 +320,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("000000000000000000000000hash1234"),
LastBlockHash: []byte("00000000000000000000000blockid10"),
TxTime: 1,
TxHeight: 1,
TxHash: "txHash1",
},
{
Sequence: 2,
Expand All @@ -316,6 +338,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("000000000000000000000000hash1234"),
LastBlockHash: []byte("00000000000000000000000blockid10"),
TxTime: 2,
TxHeight: 2,
TxHash: "txHash2",
},
{
Sequence: 3,
Expand All @@ -331,6 +356,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("000000000000000000000000hash1234"),
LastBlockHash: []byte("00000000000000000000000blockid10"),
TxTime: 3,
TxHeight: 3,
TxHash: "txHash3",
},
},
Next: uint64Ptr(4),
Expand Down Expand Up @@ -371,6 +399,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("000000000000000000000000hash1234"),
LastBlockHash: []byte("00000000000000000000000blockid10"),
TxTime: 1,
TxHeight: 1,
TxHash: "txHash1",
},
},
},
Expand All @@ -395,6 +426,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: nil,
LastBlockHash: nil,
TxTime: 11,
TxHeight: 11,
TxHash: "txHash11",
},
{
Sequence: 10,
Expand All @@ -409,6 +443,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("000000000000000000000000hash1010"),
LastBlockHash: []byte("000000000000000000000blockid1000"),
TxTime: 10,
TxHeight: 10,
TxHash: "txHash10",
},
{
Sequence: 9,
Expand All @@ -425,6 +462,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("00000000000000000000hash56789999"),
LastBlockHash: []byte("0000000000000000000000blockid100"),
TxTime: 9,
TxHeight: 9,
TxHash: "txHash9",
},
{
Sequence: 8,
Expand All @@ -441,6 +481,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("00000000000000000000hash56789999"),
LastBlockHash: []byte("0000000000000000000000blockid100"),
TxTime: 8,
TxHeight: 8,
TxHash: "txHash8",
},
{
Sequence: 7,
Expand All @@ -457,6 +500,9 @@ func TestQueryWithdrawals(t *testing.T) {
Version: []byte{0},
StorageRoot: []byte("00000000000000000000hash56789999"),
LastBlockHash: []byte("0000000000000000000000blockid100"),
TxTime: 7,
TxHeight: 7,
TxHash: "txHash7",
},
},
},
Expand Down
12 changes: 8 additions & 4 deletions executor/child/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/base64"
"fmt"
"strings"
"time"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
executortypes "github.com/initia-labs/opinit-bots/executor/types"
"github.com/initia-labs/opinit-bots/merkle"
"github.com/initia-labs/opinit-bots/txutils"
"github.com/initia-labs/opinit-bots/types"
"github.com/pkg/errors"
"go.uber.org/zap"
Expand All @@ -24,16 +26,16 @@ func (ch *Child) initiateWithdrawalHandler(ctx types.Context, args nodetypes.Eve
if err != nil {
return errors.Wrap(err, "failed to parse initiate withdrawal event")
}
err = ch.handleInitiateWithdrawal(ctx, l2Sequence, from, to, baseDenom, amount)
err = ch.handleInitiateWithdrawal(ctx, l2Sequence, from, to, baseDenom, amount, args.BlockTime, args.BlockHeight, txutils.TxHash(args.Tx))
if err != nil {
return errors.Wrap(err, "failed to handle initiate withdrawal")
}
return nil
}

func (ch *Child) handleInitiateWithdrawal(ctx types.Context, l2Sequence uint64, from string, to string, baseDenom string, amount uint64) error {
func (ch *Child) handleInitiateWithdrawal(ctx types.Context, l2Sequence uint64, from string, to string, baseDenom string, amount uint64, blockTime time.Time, blockHeight int64, txHash string) error {
withdrawalHash := ophosttypes.GenerateWithdrawalHash(ch.BridgeId(), l2Sequence, from, to, baseDenom, amount)
data := executortypes.NewWithdrawalData(l2Sequence, from, to, amount, baseDenom, withdrawalHash[:])
data := executortypes.NewWithdrawalData(l2Sequence, from, to, amount, baseDenom, withdrawalHash[:], blockHeight, blockTime.UnixNano(), txHash)

// store to database
err := SaveWithdrawal(ch.stage, data)
Expand Down Expand Up @@ -63,6 +65,8 @@ func (ch *Child) handleInitiateWithdrawal(ctx types.Context, l2Sequence uint64,
zap.Uint64("amount", amount),
zap.String("base_denom", baseDenom),
zap.String("withdrawal", base64.StdEncoding.EncodeToString(withdrawalHash[:])),
zap.Int64("height", blockHeight),
zap.String("tx_hash", txHash),
)

return nil
Expand Down Expand Up @@ -132,7 +136,7 @@ func (ch *Child) handleTree(ctx types.Context, blockHeight int64, latestHeight i
blockHeight == latestHeight &&
blockHeader.Time.After(ch.nextOutputTime)) {

treeExtraData := executortypes.NewTreeExtraData(blockHeight, blockId)
treeExtraData := executortypes.NewTreeExtraData(blockHeight, blockHeader.Time.UnixNano(), blockId)
data, err := treeExtraData.Marshal()
if err != nil {
return nil, err
Expand Down
Loading
Loading