Skip to content

Commit

Permalink
fix: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniRamirezM committed Sep 17, 2024
1 parent 7129d46 commit 1af1141
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
33 changes: 20 additions & 13 deletions sequencesender/ethtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type ethTxAdditionalData struct {
}

// sendTx adds transaction to the ethTxManager to send it to L1
func (s *SequenceSender) sendTx(ctx context.Context, resend bool, txOldHash *common.Hash, to *common.Address, fromBatch uint64, toBatch uint64, data []byte, gas uint64) error {
func (s *SequenceSender) sendTx(ctx context.Context, resend bool, txOldHash *common.Hash, to *common.Address,
fromBatch uint64, toBatch uint64, data []byte, gas uint64) error {
// Params if new tx to send or resend a previous tx
var paramTo *common.Address
var paramNonce *uint64
Expand Down Expand Up @@ -164,7 +165,7 @@ func (s *SequenceSender) purgeEthTx(ctx context.Context) {
}

// syncEthTxResults syncs results from L1 for transactions in the memory structure
func (s *SequenceSender) syncEthTxResults(ctx context.Context) (uint64, error) {
func (s *SequenceSender) syncEthTxResults(ctx context.Context) (uint64, error) { //nolint:unparam
s.mutexEthTx.Lock()
var txPending uint64
var txSync uint64
Expand Down Expand Up @@ -240,7 +241,9 @@ func (s *SequenceSender) syncAllEthTxResults(ctx context.Context) error {
}

// copyTxData copies tx data in the internal structure
func (s *SequenceSender) copyTxData(txHash common.Hash, txData []byte, txsResults map[common.Hash]ethtxmanager.TxResult) {
func (s *SequenceSender) copyTxData(
txHash common.Hash, txData []byte, txsResults map[common.Hash]ethtxmanager.TxResult,
) {
s.ethTxData[txHash] = make([]byte, len(txData))
copy(s.ethTxData[txHash], txData)

Expand All @@ -264,12 +267,14 @@ func (s *SequenceSender) updateEthTxResult(txData *ethTxData, txResult ethtxmana
if txData.Status != txResult.Status.String() {
log.Infof("update transaction %v to state %s", txResult.ID, txResult.Status.String())
txData.StatusTimestamp = time.Now()
stTrans := txData.StatusTimestamp.Format("2006-01-02T15:04:05.000-07:00") + ", " + txData.Status + ", " + txResult.Status.String()
stTrans := txData.StatusTimestamp.Format("2006-01-02T15:04:05.000-07:00") +
", " + txData.Status + ", " + txResult.Status.String()
txData.Status = txResult.Status.String()
txData.StateHistory = append(txData.StateHistory, stTrans)

// Manage according to the state
statusConsolidated := txData.Status == ethtxmanager.MonitoredTxStatusSafe.String() || txData.Status == ethtxmanager.MonitoredTxStatusFinalized.String()
statusConsolidated := txData.Status == ethtxmanager.MonitoredTxStatusSafe.String() ||
txData.Status == ethtxmanager.MonitoredTxStatusFinalized.String()
if txData.Status == ethtxmanager.MonitoredTxStatusFailed.String() {
s.logFatalf("transaction %v result failed!")
} else if statusConsolidated && txData.ToBatch >= s.latestVirtualBatchNumber {
Expand All @@ -292,24 +297,26 @@ func (s *SequenceSender) updateEthTxResult(txData *ethTxData, txResult ethtxmana
func (s *SequenceSender) getResultAndUpdateEthTx(ctx context.Context, txHash common.Hash) error {
txData, exists := s.ethTransactions[txHash]
if !exists {
log.Infof("transaction %v not found in memory", txHash)
return nil
s.logger.Errorf("transaction %v not found in memory", txHash)
return errors.New("transaction not found in memory structure")
}

txResult, err := s.ethTxManager.Result(ctx, txHash)
if err == ethtxmanager.ErrNotFound {
log.Infof("transaction %v does not exist in ethtxmanager. Marking it", txHash)
switch {
case errors.Is(err, ethtxmanager.ErrNotFound):
s.logger.Infof("transaction %v does not exist in ethtxmanager. Marking it", txHash)
txData.OnMonitor = false
// Resend tx
errSend := s.sendTx(ctx, true, &txHash, nil, 0, 0, nil, txData.Gas)
if errSend == nil {
txData.OnMonitor = false
}
return errSend
} else if err != nil {
log.Errorf("error getting result for tx %v: %v", txHash, err)

case err != nil:
s.logger.Errorf("error getting result for tx %v: %v", txHash, err)
return err
} else {

default:
s.updateEthTxResult(txData, txResult)
}

Expand Down
13 changes: 7 additions & 6 deletions sequencesender/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,25 @@ func (s *SequenceSender) getBatchFromRPC(batchNumber uint64) (*rpcbatch.RPCBatch

// Check if the response is an error
if response.Error != nil {
return nil, fmt.Errorf("error in the response calling zkevm_getBatchByNumber: %v", response.Error)
return nil, fmt.Errorf("error in the response calling zkevm_getBatchByNumber: %w", response.Error)

Check failure on line 41 in sequencesender/rpc.go

View workflow job for this annotation

GitHub Actions / test-unit (1.22.4, amd64)

fmt.Errorf format %w has arg response.Error of wrong type *github.com/0xPolygon/cdk-rpc/rpc.ErrorObject
}

// Get the batch number from the response hex string
err = json.Unmarshal(response.Result, &zkEVMBatchData)
if err != nil {
return nil, fmt.Errorf("error unmarshalling the batch from the response calling zkevm_getBatchByNumber: %v", err)
return nil, fmt.Errorf("error unmarshalling the batch from the response calling zkevm_getBatchByNumber: %w", err)
}

rpcBatch, err := rpcbatch.New(batchNumber, zkEVMBatchData.Blocks, common.Hex2Bytes(zkEVMBatchData.BatchL2Data), common.HexToHash(zkEVMBatchData.GlobalExitRoot), common.HexToAddress(zkEVMBatchData.Coinbase), zkEVMBatchData.Closed)
rpcBatch, err := rpcbatch.New(batchNumber, zkEVMBatchData.Blocks, common.Hex2Bytes(zkEVMBatchData.BatchL2Data),
common.HexToHash(zkEVMBatchData.GlobalExitRoot), common.HexToAddress(zkEVMBatchData.Coinbase), zkEVMBatchData.Closed)
if err != nil {
return nil, fmt.Errorf("error creating the rpc batch: %v", err)
return nil, fmt.Errorf("error creating the rpc batch: %w", err)
}

if len(zkEVMBatchData.Blocks) > 0 {
lastL2BlockTimestamp, err := s.getL2BlockTimestampFromRPC(zkEVMBatchData.Blocks[len(zkEVMBatchData.Blocks)-1])
if err != nil {
return nil, fmt.Errorf("error getting the last l2 block timestamp from the rpc: %v", err)
return nil, fmt.Errorf("error getting the last l2 block timestamp from the rpc: %w", err)
}
rpcBatch.SetLastL2BLockTimestamp(lastL2BlockTimestamp)
} else {
Expand Down Expand Up @@ -87,7 +88,7 @@ func (s *SequenceSender) getL2BlockTimestampFromRPC(blockHash string) (uint64, e
l2Block := zkeEVML2Block{}
err = json.Unmarshal(response.Result, &l2Block)
if err != nil {
return 0, fmt.Errorf("error unmarshalling the l2 block from the response calling eth_getBlockByHash: %v", err)
return 0, fmt.Errorf("error unmarshalling the l2 block from the response calling eth_getBlockByHash: %w", err)
}

return new(big.Int).SetBytes(common.FromHex(l2Block.Timestamp)).Uint64(), nil
Expand Down
17 changes: 14 additions & 3 deletions sequencesender/seqsendertypes/rpcbatch/rpcbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type RPCBatch struct {
l1InfoTreeIndex uint32 `json:"l1InfoTreeIndex"`
}

func New(batchNumber uint64, blockHashes []string, batchL2Data []byte, globalExitRoot common.Hash, coinbase common.Address, closed bool) (*RPCBatch, error) {
func New(batchNumber uint64, blockHashes []string, batchL2Data []byte, globalExitRoot common.Hash,
coinbase common.Address, closed bool) (*RPCBatch, error) {
return &RPCBatch{
batchNumber: batchNumber,
blockHashes: blockHashes,
Expand Down Expand Up @@ -110,8 +111,18 @@ func (b *RPCBatch) SetL1InfoTreeIndex(index uint32) {

// String
func (b *RPCBatch) String() string {
return fmt.Sprintf("Batch/RPC: LastCoinbase: %s, ForcedBatchTimestamp: %d, ForcedGlobalExitRoot: %x, ForcedBlockHashL1: %x, L2Data: %x, LastL2BLockTimestamp: %d, BatchNumber: %d, GlobalExitRoot: %x, L1InfoTreeIndex: %d",
b.LastCoinbase().String(), b.ForcedBatchTimestamp(), b.ForcedGlobalExitRoot().String(), b.ForcedBlockHashL1().String(), b.L2Data(), b.LastL2BLockTimestamp(), b.BatchNumber(), b.GlobalExitRoot().String(), b.L1InfoTreeIndex(),
return fmt.Sprintf(
"Batch/RPC: LastCoinbase: %s, ForcedBatchTimestamp: %d, ForcedGlobalExitRoot: %x, ForcedBlockHashL1: %x"+
", L2Data: %x, LastL2BLockTimestamp: %d, BatchNumber: %d, GlobalExitRoot: %x, L1InfoTreeIndex: %d",
b.LastCoinbase().String(),
b.ForcedBatchTimestamp(),
b.ForcedGlobalExitRoot().String(),
b.ForcedBlockHashL1().String(),
b.L2Data(),
b.LastL2BLockTimestamp(),
b.BatchNumber(),
b.GlobalExitRoot().String(),
b.L1InfoTreeIndex(),
)
}

Expand Down
29 changes: 21 additions & 8 deletions sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/ethereum/go-ethereum/common"
)

const ten = 10

// SequenceSender represents a sequence sender
type SequenceSender struct {
cfg Config
Expand Down Expand Up @@ -148,7 +150,7 @@ func (s *SequenceSender) batchRetrieval(ctx context.Context) error {
// Try to retrieve batch from RPC
rpcBatch, err := s.getBatchFromRPC(currentBatchNumber)
if err != nil {
if err == state.ErrNotFound {
if errors.Is(err, ethtxmanager.ErrNotFound) {
s.logger.Infof("batch %d not found in RPC", currentBatchNumber)
} else {
s.logger.Errorf("error getting batch %d from RPC: %v", currentBatchNumber, err)
Expand Down Expand Up @@ -307,12 +309,21 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) {
elapsed, waitTime := s.marginTimeElapsed(lastL2BlockTimestamp, lastL1BlockHeader.Time, timeMargin)

if !elapsed {
s.logger.Infof("waiting at least %d seconds to send sequences, time difference between last L1 block %d (ts: %d) and last L2 block %d (ts: %d) in the sequence is lower than %d seconds",
waitTime, lastL1BlockHeader.Number, lastL1BlockHeader.Time, lastBatch.BatchNumber(), lastL2BlockTimestamp, timeMargin)
s.logger.Infof("waiting at least %d seconds to send sequences, time difference between last L1 block %d (ts: %d) "+
"and last L2 block %d (ts: %d) in the sequence is lower than %d seconds",
waitTime, lastL1BlockHeader.Number, lastL1BlockHeader.Time,
lastBatch.BatchNumber(), lastL2BlockTimestamp, timeMargin,
)
time.Sleep(time.Duration(waitTime) * time.Second)
} else {
s.logger.Infof("continuing, time difference between last L1 block %d (ts: %d) and last L2 block %d (ts: %d) in the sequence is greater than %d seconds",
lastL1BlockHeader.Number, lastL1BlockHeader.Time, lastBatch.BatchNumber, lastL2BlockTimestamp, timeMargin)
s.logger.Infof("continuing, time difference between last L1 block %d (ts: %d) and last L2 block %d (ts: %d) "+
"in the sequence is greater than %d seconds",
lastL1BlockHeader.Number,
lastL1BlockHeader.Time,
lastBatch.BatchNumber,
lastL2BlockTimestamp,
timeMargin,
)
break
}
}
Expand All @@ -326,11 +337,13 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) {

// Wait if the time difference is less than L1BlockTimestampMargin
if !elapsed {
s.logger.Infof("waiting at least %d seconds to send sequences, time difference between now (ts: %d) and last L2 block %d (ts: %d) in the sequence is lower than %d seconds",
s.logger.Infof("waiting at least %d seconds to send sequences, time difference between now (ts: %d) "+
"and last L2 block %d (ts: %d) in the sequence is lower than %d seconds",
waitTime, currentTime, lastBatch.BatchNumber, lastL2BlockTimestamp, timeMargin)
time.Sleep(time.Duration(waitTime) * time.Second)
} else {
s.logger.Infof("[SeqSender]sending sequences now, time difference between now (ts: %d) and last L2 block %d (ts: %d) in the sequence is also greater than %d seconds",
s.logger.Infof("sending sequences now, time difference between now (ts: %d) and last L2 block %d (ts: %d) "+
"in the sequence is also greater than %d seconds",
currentTime, lastBatch.BatchNumber, lastL2BlockTimestamp, timeMargin)
break
}
Expand Down Expand Up @@ -500,6 +513,6 @@ func (s *SequenceSender) logFatalf(template string, args ...interface{}) {
for {
s.logger.Errorf(template, args...)
s.logger.Errorf("sequence sending stopped.")
time.Sleep(10 * time.Second)
time.Sleep(ten * time.Second)
}
}

0 comments on commit 1af1141

Please sign in to comment.