Skip to content

Commit 67daf8e

Browse files
committed
rename functions and fix lint
1 parent 4698cbb commit 67daf8e

File tree

6 files changed

+30
-59
lines changed

6 files changed

+30
-59
lines changed

go.mod

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,3 @@ require (
182182
)
183183

184184
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
185-
186-
replace (
187-
github.com/initia-labs/OPinit => ../opinit
188-
github.com/initia-labs/OPinit/api => ../opinit/api
189-
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
476476
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
477477
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
478478
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
479+
github.com/initia-labs/OPinit v0.4.1 h1:g6IVEAOe2X31pgjk/q0zg4R1GfNj2QP3q5s3HMcWm8w=
480+
github.com/initia-labs/OPinit v0.4.1/go.mod h1:n0eqwOnVGE1vuTnW+3jzyEXfE4ndTM0vCRGmPu9VvUc=
481+
github.com/initia-labs/OPinit/api v0.4.1 h1:Q8etW92LiwekKZxzDYVFdiHF3uOpEA4nyajy8zpcxB0=
482+
github.com/initia-labs/OPinit/api v0.4.1/go.mod h1:Xy/Nt3ubXLQ4zKn0m7RuQOM1sj8TVdlNNyek21TGYR0=
479483
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
480484
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
481485
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=

node/broadcaster/account.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ func (b *Broadcaster) GetAccountWithHeight(_ client.Context, addr sdk.AccAddress
6868
return nil, 0, fmt.Errorf("failed to parse block height: %w", err)
6969
}
7070

71-
//nolint:staticcheck
72-
var acc authtypes.AccountI
71+
var acc sdk.AccountI
7372
if err := b.cdc.UnpackAny(res.Account, &acc); err != nil {
7473
return nil, 0, err
7574
}

node/broadcaster/process.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func (b Broadcaster) GetHeight() uint64 {
1818
return b.lastProcessedBlockHeight + 1
1919
}
2020

21-
func (b *Broadcaster) HandleBlock(block *rpccoretypes.ResultBlock, blockResult *rpccoretypes.ResultBlockResults, latestChainHeight uint64) error {
21+
// HandleNewBlock is called when a new block is received.
22+
func (b *Broadcaster) HandleNewBlock(block *rpccoretypes.ResultBlock, blockResult *rpccoretypes.ResultBlockResults, latestChainHeight uint64) error {
2223

2324
// check pending txs first
2425
for _, tx := range block.Block.Txs {
@@ -28,7 +29,7 @@ func (b *Broadcaster) HandleBlock(block *rpccoretypes.ResultBlock, blockResult *
2829

2930
// check if the first pending tx is included in the block
3031
if pendingTx := b.peekLocalPendingTx(); btypes.TxHash(tx) == pendingTx.TxHash {
31-
err := b.MarkPendingTxAsProcessed(block.Block.Height, pendingTx.TxHash, pendingTx.Sequence)
32+
err := b.RemovePendingTx(block.Block.Height, pendingTx.TxHash, pendingTx.Sequence)
3233
if err != nil {
3334
return err
3435
}
@@ -51,19 +52,7 @@ func (b *Broadcaster) HandleBlock(block *rpccoretypes.ResultBlock, blockResult *
5152
return nil
5253
}
5354

54-
func (b *Broadcaster) MarkPendingTxAsProcessed(blockHeight int64, txHash string, sequence uint64) error {
55-
err := b.deletePendingTx(sequence)
56-
if err != nil {
57-
return err
58-
}
59-
60-
b.logger.Debug("tx inserted", zap.Int64("height", blockHeight), zap.Uint64("sequence", sequence), zap.String("txHash", txHash))
61-
b.dequeueLocalPendingTx()
62-
63-
return nil
64-
}
65-
66-
// CheckPendingTx checks if the pending tx is included in the block
55+
// CheckPendingTx query tx info to check if pending tx is processed.
6756
func (b *Broadcaster) CheckPendingTx() (*btypes.PendingTxInfo, *rpccoretypes.ResultTx, error) {
6857
if b.lenLocalPendingTx() == 0 {
6958
return nil, nil, nil
@@ -89,6 +78,20 @@ func (b *Broadcaster) CheckPendingTx() (*btypes.PendingTxInfo, *rpccoretypes.Res
8978
return &pendingTx, res, nil
9079
}
9180

81+
// RemovePendingTx remove pending tx from local pending txs.
82+
// It is called when the pending tx is included in the block.
83+
func (b *Broadcaster) RemovePendingTx(blockHeight int64, txHash string, sequence uint64) error {
84+
err := b.deletePendingTx(sequence)
85+
if err != nil {
86+
return err
87+
}
88+
89+
b.logger.Debug("tx inserted", zap.Int64("height", blockHeight), zap.Uint64("sequence", sequence), zap.String("txHash", txHash))
90+
b.dequeueLocalPendingTx()
91+
92+
return nil
93+
}
94+
9295
// Start broadcaster loop
9396
func (b *Broadcaster) Start(ctx context.Context) error {
9497
defer close(b.txChannelStopped)
@@ -123,10 +126,8 @@ func (b *Broadcaster) Start(ctx context.Context) error {
123126
}
124127

125128
// @dev: these pending processed data is filled at initialization(`NewBroadcaster`).
126-
func (b Broadcaster) BroadcastPendingProcessedMsgs() error {
129+
func (b Broadcaster) BroadcastPendingProcessedMsgs() {
127130
for _, processedMsg := range b.pendingProcessedMsgs {
128131
b.BroadcastMsgs(processedMsg)
129132
}
130-
131-
return nil
132133
}

node/broadcaster/tx.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
"regexp"
88
"strconv"
99
"strings"
10-
"time"
1110

12-
"github.com/pkg/errors"
1311
"go.uber.org/zap"
1412

1513
sdkerrors "cosmossdk.io/errors"
@@ -37,36 +35,6 @@ var ignoringErrors = []error{
3735
var accountSeqRegex = regexp.MustCompile("account sequence mismatch, expected ([0-9]+), got ([0-9]+)")
3836
var outputIndexRegex = regexp.MustCompile("expected ([0-9]+), got ([0-9]+): invalid output index")
3937

40-
func (b *Broadcaster) txBroadcastLooper(ctx context.Context) error {
41-
retry := time.NewTicker(30 * time.Second)
42-
43-
for {
44-
select {
45-
case <-ctx.Done():
46-
return nil
47-
case data := <-b.txChannel:
48-
count := 1
49-
select {
50-
case <-ctx.Done():
51-
return nil
52-
case <-retry.C:
53-
err := b.handleProcessedMsgs(ctx, data)
54-
if err == nil {
55-
break
56-
} else if err = b.handleMsgError(err); err == nil {
57-
break
58-
}
59-
b.logger.Warn("retry", zap.Int("count", count), zap.String("error", err.Error()))
60-
count++
61-
62-
if count >= 10 {
63-
return errors.Wrap(err, "failed to handle processed msgs")
64-
}
65-
}
66-
}
67-
}
68-
}
69-
7038
func (b *Broadcaster) handleMsgError(err error) error {
7139
if strs := accountSeqRegex.FindStringSubmatch(err.Error()); strs != nil {
7240
expected, parseErr := strconv.ParseUint(strs[1], 10, 64)
@@ -116,6 +84,8 @@ func (b *Broadcaster) handleMsgError(err error) error {
11684
return err
11785
}
11886

87+
// HandleProcessedMsgs handles processed messages by broadcasting them to the network.
88+
// It stores the transaction in the database and local memory and keep track of the successful broadcast.
11989
func (b *Broadcaster) handleProcessedMsgs(ctx context.Context, data btypes.ProcessedMsgs) error {
12090
sequence := b.txf.Sequence()
12191
txBytes, txHash, err := b.cfg.BuildTxWithMessages(ctx, data.Msgs)

node/process.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"go.uber.org/zap"
1212
)
1313

14+
// blockProcessLooper fetches new blocks and processes them
1415
func (n *Node) blockProcessLooper(ctx context.Context, processType nodetypes.BlockProcessType) error {
1516
timer := time.NewTicker(nodetypes.POLLING_INTERVAL)
1617
defer timer.Stop()
@@ -92,6 +93,7 @@ func (n *Node) blockProcessLooper(ctx context.Context, processType nodetypes.Blo
9293
}
9394
}
9495

96+
// fetch new block from the chain
9597
func (n *Node) fetchNewBlock(ctx context.Context, height int64) (block *rpccoretypes.ResultBlock, blockResult *rpccoretypes.ResultBlockResults, err error) {
9698
n.logger.Debug("fetch new block", zap.Int64("height", height))
9799
block, err = n.rpcClient.Block(ctx, &height)
@@ -116,7 +118,7 @@ func (n *Node) handleNewBlock(block *rpccoretypes.ResultBlock, blockResult *rpcc
116118

117119
// handle broadcaster first to check pending txs
118120
if n.broadcaster != nil {
119-
err := n.broadcaster.HandleBlock(block, blockResult, latestChainHeight)
121+
err := n.broadcaster.HandleNewBlock(block, blockResult, latestChainHeight)
120122
if err != nil {
121123
return err
122124
}
@@ -230,7 +232,7 @@ func (n *Node) txChecker(ctx context.Context) error {
230232
}
231233
}
232234

233-
err = n.broadcaster.MarkPendingTxAsProcessed(int64(res.Height), pendingTx.TxHash, pendingTx.Sequence)
235+
err = n.broadcaster.RemovePendingTx(int64(res.Height), pendingTx.TxHash, pendingTx.Sequence)
234236
if err != nil {
235237
return err
236238
}

0 commit comments

Comments
 (0)