Skip to content

Commit

Permalink
fix sonarCloud code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
chengzhinei committed Nov 9, 2023
1 parent 1f9759a commit cc9f357
Showing 1 changed file with 46 additions and 67 deletions.
113 changes: 46 additions & 67 deletions claimtxman/claimtxman.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,48 @@ func (tm *ClaimTxManager) getDeposits(ger *etherman.GlobalExitRoot) ([]*etherman
return deposits, nil
}

func (tm *ClaimTxManager) handleDeposit(deposit *etherman.Deposit, dbTx pgx.Tx) (error, bool) {
claimHash, err := tm.bridgeService.GetDepositStatus(tm.ctx, deposit.DepositCount, deposit.DestinationNetwork)
if err != nil {
log.Errorf("error getting deposit status for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)
return err, false
}
if len(claimHash) > 0 || deposit.LeafType == LeafTypeMessage && !tm.isDepositMessageAllowed(deposit) {
log.Infof("Ignoring deposit: %d, leafType: %d, claimHash: %s, deposit.OriginalAddress: %s", deposit.DepositCount, deposit.LeafType, claimHash, deposit.OriginalAddress.String())
return nil, true
}
log.Infof("create the claim tx for the deposit %d", deposit.DepositCount)
ger, proves, err := tm.bridgeService.GetClaimProof(deposit.DepositCount, deposit.NetworkID, dbTx)
if err != nil {
log.Errorf("error getting Claim Proof for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)
return err, false
}
var mtProves [mtHeight][keyLen]byte
for i := 0; i < mtHeight; i++ {
mtProves[i] = proves[i]
}
tx, err := tm.l2Node.BuildSendClaim(tm.ctx, deposit, mtProves,
&etherman.GlobalExitRoot{
ExitRoots: []common.Hash{
ger.ExitRoots[0],
ger.ExitRoots[1],
}}, 1, 1, 1,
tm.auth)
if err != nil {
log.Errorf("error BuildSendClaim tx for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)
return err, false
}
if err = tm.addClaimTx(deposit.DepositCount, tm.auth.From, tx.To(), nil, tx.Data(), dbTx); err != nil {
log.Errorf("error adding claim tx for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)
return err, false
}
return nil, false
}

func (tm *ClaimTxManager) processDepositStatusL1(ger *etherman.GlobalExitRoot) error {
deposits, err := tm.getDeposits(ger)
if err != nil {
Expand All @@ -217,44 +259,12 @@ func (tm *ClaimTxManager) processDepositStatusL1(ger *etherman.GlobalExitRoot) e
if err != nil {
return err
}
claimHash, err := tm.bridgeService.GetDepositStatus(tm.ctx, deposit.DepositCount, deposit.DestinationNetwork)
if err != nil {
log.Errorf("error getting deposit status for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)

if err, ctn := tm.handleDeposit(deposit, dbTx); err != nil {
return err
}
if len(claimHash) > 0 || deposit.LeafType == LeafTypeMessage && !tm.isDepositMessageAllowed(deposit) {
log.Infof("Ignoring deposit: %d, leafType: %d, claimHash: %s, deposit.OriginalAddress: %s", deposit.DepositCount, deposit.LeafType, claimHash, deposit.OriginalAddress.String())
} else if ctn {
continue
}
log.Infof("create the claim tx for the deposit %d", deposit.DepositCount)
ger, proves, err := tm.bridgeService.GetClaimProof(deposit.DepositCount, deposit.NetworkID, dbTx)
if err != nil {
log.Errorf("error getting Claim Proof for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)
return err
}
var mtProves [mtHeight][keyLen]byte
for i := 0; i < mtHeight; i++ {
mtProves[i] = proves[i]
}
tx, err := tm.l2Node.BuildSendClaim(tm.ctx, deposit, mtProves,
&etherman.GlobalExitRoot{
ExitRoots: []common.Hash{
ger.ExitRoots[0],
ger.ExitRoots[1],
}}, 1, 1, 1,
tm.auth)
if err != nil {
log.Errorf("error BuildSendClaim tx for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)
return err
}
if err = tm.addClaimTx(deposit.DepositCount, tm.auth.From, tx.To(), nil, tx.Data(), dbTx); err != nil {
log.Errorf("error adding claim tx for deposit %d. Error: %v", deposit.DepositCount, err)
tm.rollbackStore(dbTx)
return err
}

err = tm.storage.Commit(tm.ctx, dbTx)
if err != nil {
Expand Down Expand Up @@ -293,38 +303,7 @@ func (tm *ClaimTxManager) processDepositStatus(ger *etherman.GlobalExitRoot, dbT
return err
}
for _, deposit := range deposits {
claimHash, err := tm.bridgeService.GetDepositStatus(tm.ctx, deposit.DepositCount, deposit.DestinationNetwork)
if err != nil {
log.Errorf("error getting deposit status for deposit %d. Error: %v", deposit.DepositCount, err)
return err
}
if len(claimHash) > 0 || deposit.LeafType == LeafTypeMessage && !tm.isDepositMessageAllowed(deposit) {
log.Infof("Ignoring deposit: %d, leafType: %d, claimHash: %s, deposit.OriginalAddress: %s", deposit.DepositCount, deposit.LeafType, claimHash, deposit.OriginalAddress.String())
continue
}
log.Infof("create the claim tx for the deposit %d", deposit.DepositCount)
ger, proves, err := tm.bridgeService.GetClaimProof(deposit.DepositCount, deposit.NetworkID, dbTx)
if err != nil {
log.Errorf("error getting Claim Proof for deposit %d. Error: %v", deposit.DepositCount, err)
return err
}
var mtProves [mtHeight][keyLen]byte
for i := 0; i < mtHeight; i++ {
mtProves[i] = proves[i]
}
tx, err := tm.l2Node.BuildSendClaim(tm.ctx, deposit, mtProves,
&etherman.GlobalExitRoot{
ExitRoots: []common.Hash{
ger.ExitRoots[0],
ger.ExitRoots[1],
}}, 1, 1, 1,
tm.auth)
if err != nil {
log.Errorf("error BuildSendClaim tx for deposit %d. Error: %v", deposit.DepositCount, err)
return err
}
if err = tm.addClaimTx(deposit.DepositCount, tm.auth.From, tx.To(), nil, tx.Data(), dbTx); err != nil {
log.Errorf("error adding claim tx for deposit %d. Error: %v", deposit.DepositCount, err)
if err, _ := tm.handleDeposit(deposit, dbTx); err != nil {
return err
}
}
Expand Down

0 comments on commit cc9f357

Please sign in to comment.