From 4e8ac2c9189f95586c34e74f99e3a8b345b27b01 Mon Sep 17 00:00:00 2001 From: Hoon <48665813+sh-cha@users.noreply.github.com> Date: Sun, 29 Sep 2024 11:08:36 +0900 Subject: [PATCH] Fix/reset height pending txs (#24) * delete pending txs & processed msgs * send tx immediately even if node is syncing * l1startheight starts from the given height * search tx from the exact l1sequence --- executor/db.go | 12 ++++++++++++ executor/executor.go | 18 ++++++++++-------- executor/host/handler.go | 3 --- node/db.go | 21 +++++++++++++++++++++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/executor/db.go b/executor/db.go index 020d703..5e8f73b 100644 --- a/executor/db.go +++ b/executor/db.go @@ -19,6 +19,12 @@ func ResetHeights(db types.DB) error { if err := node.DeleteSyncInfo(db); err != nil { return err } + if err := node.DeletePendingTxs(db); err != nil { + return err + } + if err := node.DeleteProcessedMsgs(db); err != nil { + return err + } fmt.Printf("reset height to 0 for node %s\n", string(db.GetPrefix())) } return nil @@ -35,6 +41,12 @@ func ResetHeight(db types.DB, nodeName string) error { if err != nil { return err } + if err := node.DeletePendingTxs(db); err != nil { + return err + } + if err := node.DeleteProcessedMsgs(db); err != nil { + return err + } fmt.Printf("reset height to 0 for node %s\n", string(nodeDB.GetPrefix())) return nil } diff --git a/executor/executor.go b/executor/executor.go index d733144..5f0719c 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -236,14 +236,13 @@ func (ex *Executor) getStartHeights(ctx context.Context, bridgeId uint64) (l1Sta if err != nil { return 0, 0, 0, 0, err } - if l1Sequence > 1 { - depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1) - if err != nil { - return 0, 0, 0, 0, err - } - if l1StartHeight > depositTxHeight { - l1StartHeight = depositTxHeight - } + + depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence) + if err != nil { + return 0, 0, 0, 0, err + } + if l1StartHeight > depositTxHeight { + l1StartHeight = depositTxHeight } if l2StartHeight == 0 { @@ -252,5 +251,8 @@ func (ex *Executor) getStartHeights(ctx context.Context, bridgeId uint64) (l1Sta if ex.cfg.BatchStartHeight > 0 { batchStartHeight = ex.cfg.BatchStartHeight - 1 } + if l1StartHeight > 0 { + l1StartHeight-- + } return l1StartHeight, l2StartHeight, startOutputIndex, batchStartHeight, err } diff --git a/executor/host/handler.go b/executor/host/handler.go index e75765c..e4e68b7 100644 --- a/executor/host/handler.go +++ b/executor/host/handler.go @@ -22,9 +22,6 @@ func (h *Host) endBlockHandler(_ context.Context, args nodetypes.EndBlockArgs) e // collect more msgs if block height is not latest blockHeight := args.Block.Header.Height msgQueue := h.GetMsgQueue() - if blockHeight != args.LatestHeight && len(msgQueue) > 0 && len(msgQueue) <= 10 { - return nil - } batchKVs := []types.RawKV{ h.Node().SyncInfoToRawKV(blockHeight), diff --git a/node/db.go b/node/db.go index 4ded461..b1c4da3 100644 --- a/node/db.go +++ b/node/db.go @@ -2,6 +2,7 @@ package node import ( dbtypes "github.com/initia-labs/opinit-bots/db/types" + btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types" nodetypes "github.com/initia-labs/opinit-bots/node/types" "github.com/initia-labs/opinit-bots/types" "go.uber.org/zap" @@ -54,3 +55,23 @@ func (n Node) DeleteSyncInfo() error { func DeleteSyncInfo(db types.DB) error { return db.Delete(nodetypes.LastProcessedBlockHeightKey) } + +func DeleteProcessedMsgs(db types.DB) error { + return db.PrefixedIterate(btypes.ProcessedMsgsKey, func(key, _ []byte) (stop bool, err error) { + err = db.Delete(key) + if err != nil { + return stop, err + } + return false, nil + }) +} + +func DeletePendingTxs(db types.DB) error { + return db.PrefixedIterate(btypes.PendingTxsKey, func(key, _ []byte) (stop bool, err error) { + err = db.Delete(key) + if err != nil { + return stop, err + } + return false, nil + }) +}