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 1 commit
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
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
27 changes: 23 additions & 4 deletions executor/child/withdraw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ func TestInitiateWithdrawalHandler(t *testing.T) {
Done: false,
},
eventHandlerArgs: nodetypes.EventHandlerArgs{
BlockHeight: 11,
BlockTime: time.Unix(0, 10000),
Tx: []byte("txbytes"), // EA58654919E6F3E08370DE723D8DA223F1DFE78DD28D0A23E6F18BFA0815BB99
EventAttributes: InitiateWithdrawalEvents("from", "to", "denom", "uinit", sdk.NewInt64Coin("uinit", 10000), 1),
},
expectedStage: []types.KV{
{
Key: append([]byte("/test_child/withdrawal_sequence/"), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}...),
Value: []byte(`{"sequence":1,"from":"from","to":"to","amount":10000,"base_denom":"uinit","withdrawal_hash":"V+7ukqwrq0Ba6kj63TEZ1C7m4Ze7pqERmid/OQtNneY="}`),
Value: []byte(`{"sequence":1,"from":"from","to":"to","amount":10000,"base_denom":"uinit","withdrawal_hash":"V+7ukqwrq0Ba6kj63TEZ1C7m4Ze7pqERmid/OQtNneY=","tx_height":11,"tx_time":10000,"tx_hash":"EA58654919E6F3E08370DE723D8DA223F1DFE78DD28D0A23E6F18BFA0815BB99"}`),
},
{
Key: append([]byte("/test_child/withdrawal_address/to/"), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}...),
Expand All @@ -113,6 +116,8 @@ func TestInitiateWithdrawalHandler(t *testing.T) {
zap.Uint64("amount", 10000),
zap.String("base_denom", "uinit"),
zap.String("withdrawal", "V+7ukqwrq0Ba6kj63TEZ1C7m4Ze7pqERmid/OQtNneY="),
zap.Int64("height", 11),
zap.String("tx_hash", "EA58654919E6F3E08370DE723D8DA223F1DFE78DD28D0A23E6F18BFA0815BB99"),
}
return msg, fields
},
Expand All @@ -132,12 +137,15 @@ func TestInitiateWithdrawalHandler(t *testing.T) {
Done: false,
},
eventHandlerArgs: nodetypes.EventHandlerArgs{
BlockHeight: 11,
BlockTime: time.Unix(0, 10000),
Tx: []byte("txbytes"),
EventAttributes: InitiateWithdrawalEvents("from", "to", "denom", "uinit", sdk.NewInt64Coin("uinit", 10000), 101),
},
expectedStage: []types.KV{
{
Key: append([]byte("/test_child/withdrawal_sequence/"), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65}...),
Value: []byte(`{"sequence":101,"from":"from","to":"to","amount":10000,"base_denom":"uinit","withdrawal_hash":"Hzn58U22rfXK2VZCOIFzjudpdYkw5v0eZ2QnspIFlBs="}`),
Value: []byte(`{"sequence":101,"from":"from","to":"to","amount":10000,"base_denom":"uinit","withdrawal_hash":"Hzn58U22rfXK2VZCOIFzjudpdYkw5v0eZ2QnspIFlBs=","tx_height":11,"tx_time":10000,"tx_hash":"EA58654919E6F3E08370DE723D8DA223F1DFE78DD28D0A23E6F18BFA0815BB99"}`),
},
{
Key: append([]byte("/test_child/withdrawal_address/to/"), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65}...),
Expand All @@ -161,6 +169,8 @@ func TestInitiateWithdrawalHandler(t *testing.T) {
zap.Uint64("amount", 10000),
zap.String("base_denom", "uinit"),
zap.String("withdrawal", "Hzn58U22rfXK2VZCOIFzjudpdYkw5v0eZ2QnspIFlBs="),
zap.Int64("height", 11),
zap.String("tx_hash", "EA58654919E6F3E08370DE723D8DA223F1DFE78DD28D0A23E6F18BFA0815BB99"),
}
return msg, fields
},
Expand All @@ -178,6 +188,9 @@ func TestInitiateWithdrawalHandler(t *testing.T) {
Done: false,
},
eventHandlerArgs: nodetypes.EventHandlerArgs{
BlockHeight: 10,
BlockTime: time.Unix(0, 10000),
Tx: []byte("txbytes"),
EventAttributes: InitiateWithdrawalEvents("from", "to", "denom", "uinit", sdk.NewInt64Coin("uinit", 10000), 101),
},
expectedStage: nil,
Expand Down Expand Up @@ -609,6 +622,7 @@ func TestHandleTree(t *testing.T) {
LeafCount: 0,
StartLeafIndex: 10,
LastSiblings: make(map[uint8][]byte),
Done: false,
},
lastOutputTime: time.Time{},
nextOutputTime: time.Unix(0, 10000),
Expand Down Expand Up @@ -643,6 +657,7 @@ func TestHandleTree(t *testing.T) {
0: {0xf7, 0x58, 0xe5, 0x5d, 0xb1, 0x30, 0x74, 0x4b, 0x05, 0xad, 0x66, 0x94, 0xb2, 0x8b, 0xe4, 0xab, 0x73, 0x0d, 0xe0, 0xdc, 0x09, 0xde, 0x5c, 0x0c, 0x42, 0xab, 0x64, 0x66, 0xc8, 0x06, 0xdc, 0x10},
1: {0x50, 0x26, 0x55, 0x2e, 0x7b, 0x21, 0xca, 0xb5, 0x27, 0xe4, 0x16, 0x9e, 0x66, 0x46, 0x02, 0xb8, 0x5d, 0x03, 0x67, 0x0b, 0xb5, 0x57, 0xe3, 0x29, 0x18, 0xd9, 0x33, 0xe3, 0xd5, 0x92, 0x5c, 0x7e},
},
Done: false,
},
lastOutputTime: time.Time{},
nextOutputTime: time.Unix(0, 10000),
Expand All @@ -659,7 +674,7 @@ func TestHandleTree(t *testing.T) {
},
{
Key: append([]byte("/test_child/finalized_tree/"), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a}...),
Value: []byte(`{"tree_index":3,"tree_height":1,"root":"UCZVLnshyrUn5BaeZkYCuF0DZwu1V+MpGNkz49WSXH4=","start_leaf_index":10,"leaf_count":2,"extra_data":"eyJibG9ja19udW1iZXIiOjUsImJsb2NrX2hhc2giOiJkR1Z6ZEY5aWJHOWphMTlwWkE9PSJ9"}`),
Value: []byte(`{"tree_index":3,"tree_height":1,"root":"UCZVLnshyrUn5BaeZkYCuF0DZwu1V+MpGNkz49WSXH4=","start_leaf_index":10,"leaf_count":2,"extra_data":"eyJibG9ja19udW1iZXIiOjUsImJsb2NrX3RpbWUiOjEwMTAwLCJibG9ja19oYXNoIjoiZEdWemRGOWliRzlqYTE5cFpBPT0ifQ=="}`),
},
},
err: false,
Expand All @@ -681,6 +696,7 @@ func TestHandleTree(t *testing.T) {
0: {0xd9, 0xf8, 0x70, 0xb0, 0x6d, 0x46, 0x43, 0xc5, 0x9f, 0xbd, 0x0a, 0x9a, 0xd1, 0xe5, 0x5c, 0x43, 0x98, 0xdd, 0xae, 0xf1, 0xca, 0xc2, 0xd7, 0xfb, 0xcf, 0xd5, 0xe0, 0x11, 0xb6, 0x83, 0xb8, 0x33},
1: {0x50, 0x26, 0x55, 0x2e, 0x7b, 0x21, 0xca, 0xb5, 0x27, 0xe4, 0x16, 0x9e, 0x66, 0x46, 0x02, 0xb8, 0x5d, 0x03, 0x67, 0x0b, 0xb5, 0x57, 0xe3, 0x29, 0x18, 0xd9, 0x33, 0xe3, 0xd5, 0x92, 0x5c, 0x7e},
},
Done: false,
},
lastOutputTime: time.Time{},
nextOutputTime: time.Unix(0, 10000),
Expand All @@ -697,7 +713,7 @@ func TestHandleTree(t *testing.T) {
},
{
Key: append([]byte("/test_child/finalized_tree/"), []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a}...),
Value: []byte(`{"tree_index":3,"tree_height":2,"root":"/9R6cfY6ilAJVu80sfq71C8HyF53960hJwHgZNq99qM=","start_leaf_index":10,"leaf_count":3,"extra_data":"eyJibG9ja19udW1iZXIiOjUsImJsb2NrX2hhc2giOiJkR1Z6ZEY5aWJHOWphMTlwWkE9PSJ9"}`),
Value: []byte(`{"tree_index":3,"tree_height":2,"root":"/9R6cfY6ilAJVu80sfq71C8HyF53960hJwHgZNq99qM=","start_leaf_index":10,"leaf_count":3,"extra_data":"eyJibG9ja19udW1iZXIiOjUsImJsb2NrX3RpbWUiOjEwMTAwLCJibG9ja19oYXNoIjoiZEdWemRGOWliRzlqYTE5cFpBPT0ifQ=="}`),
},
{ // height 0, index 3
Key: append([]byte("/test_child/node/"), []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3}...),
Expand Down Expand Up @@ -728,6 +744,7 @@ func TestHandleTree(t *testing.T) {
LeafCount: 3,
StartLeafIndex: 10,
LastSiblings: map[uint8][]byte{},
Done: false,
},
finalizingBlockHeight: 5,

Expand All @@ -752,6 +769,7 @@ func TestHandleTree(t *testing.T) {
0: {0xd9, 0xf8, 0x70, 0xb0, 0x6d, 0x46, 0x43, 0xc5, 0x9f, 0xbd, 0x0a, 0x9a, 0xd1, 0xe5, 0x5c, 0x43, 0x98, 0xdd, 0xae, 0xf1, 0xca, 0xc2, 0xd7, 0xfb, 0xcf, 0xd5, 0xe0, 0x11, 0xb6, 0x83, 0xb8, 0x33},
1: {0x50, 0x26, 0x55, 0x2e, 0x7b, 0x21, 0xca, 0xb5, 0x27, 0xe4, 0x16, 0x9e, 0x66, 0x46, 0x02, 0xb8, 0x5d, 0x03, 0x67, 0x0b, 0xb5, 0x57, 0xe3, 0x29, 0x18, 0xd9, 0x33, 0xe3, 0xd5, 0x92, 0x5c, 0x7e},
},
Done: false,
},
lastOutputTime: time.Time{},
nextOutputTime: time.Unix(0, 10000),
Expand Down Expand Up @@ -783,6 +801,7 @@ func TestHandleTree(t *testing.T) {
0: {0xd9, 0xf8, 0x70, 0xb0, 0x6d, 0x46, 0x43, 0xc5, 0x9f, 0xbd, 0x0a, 0x9a, 0xd1, 0xe5, 0x5c, 0x43, 0x98, 0xdd, 0xae, 0xf1, 0xca, 0xc2, 0xd7, 0xfb, 0xcf, 0xd5, 0xe0, 0x11, 0xb6, 0x83, 0xb8, 0x33},
1: {0x50, 0x26, 0x55, 0x2e, 0x7b, 0x21, 0xca, 0xb5, 0x27, 0xe4, 0x16, 0x9e, 0x66, 0x46, 0x02, 0xb8, 0x5d, 0x03, 0x67, 0x0b, 0xb5, 0x57, 0xe3, 0x29, 0x18, 0xd9, 0x33, 0xe3, 0xd5, 0x92, 0x5c, 0x7e},
},
Done: false,
},
lastOutputTime: time.Time{},
nextOutputTime: time.Unix(0, 10000),
Expand Down
Loading
Loading