Skip to content

Commit

Permalink
refactor(contractbackend):
Browse files Browse the repository at this point in the history
- Change SharedMutex to SharedExpectedNoncesMutex.
refactor(erc20_depositor):
- Return directly if Approval returns error.
- Fix comments.

Signed-off-by: sophia1ch <[email protected]>
  • Loading branch information
sophia1ch committed Jan 23, 2024
1 parent d81f135 commit 4fee6a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
13 changes: 7 additions & 6 deletions channel/contractbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ const (
var errTxTimedOut = errors.New("")

var (
// SharedExpectedNonces is a map of each expected next nonce of all clients.
SharedExpectedNoncesMutex &sync.Mutex{}
SharedExpectedNonces map[ChainID]map[common.Address]uint64
)
// SharedExpectedNonces is a map of each expected next nonce of all clients.
SharedExpectedNonces map[ChainID]map[common.Address]uint64
// SharedExpectedNoncesMutex is a mutex to protect the shared expected nonces map.
SharedExpectedNoncesMutex = &sync.Mutex{}
)

Check failure on line 53 in channel/contractbackend.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofmt`-ed with `-s` (gofmt)

// ContractInterface provides all functions needed by an ethereum backend.
// Both test.SimulatedBackend and ethclient.Client implement this interface.
Expand Down Expand Up @@ -178,8 +179,8 @@ func (c *ContractBackend) nonce(ctx context.Context, sender common.Address) (uin
err = cherrors.CheckIsChainNotReachableError(err)
return 0, errors.WithMessage(err, "fetching nonce")
}
SharedMutex.Lock()
defer SharedMutex.Unlock()
SharedExpectedNoncesMutex.Lock()
defer SharedExpectedNoncesMutex.Unlock()
expectedNextNonce, found := c.expectedNextNonce[sender]
if !found {
c.expectedNextNonce[sender] = 0
Expand Down
9 changes: 4 additions & 5 deletions channel/erc20_depositor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ func (d *ERC20Depositor) Deposit(ctx context.Context, req DepositReq) (types.Tra
Pending: false,
Context: ctx,
}
// variables for the return value.
var depResult DepositResult
txApproval, approvalReceived, errApproval := Approve(ctx, lock, req, token, callOpts)
if errApproval != nil {
depResult.Error = errApproval
return nil, errors.WithMessagef(errApproval, "approving asset: %x", req.Asset)
}
if approvalReceived {
txDeposit, err := d.DepositOnly(ctx, req)
Expand All @@ -104,16 +103,16 @@ func (d *ERC20Depositor) DepositOnly(ctx context.Context, req DepositReq) (*type
return nil, errors.WithMessagef(err, "creating transactor for asset: %x", req.Asset)
}

tx2, err := assetholder.Deposit(opts, req.FundingID, req.Balance)
return tx2, err
tx, err := assetholder.Deposit(opts, req.FundingID, req.Balance)
return tx, err
}

// NumTX returns 2 since it does IncreaseAllowance and Deposit.
func (*ERC20Depositor) NumTX() uint32 {
return erc20DepositorNumTx
}

// Create key from account address and asset to only lock the process when hub deposits the same asset at the same time.
// Create key from account address and asset to ensure only one deposit for an asset is performed at the same time.
func lockKey(account common.Address, asset common.Address) string {
return fmt.Sprintf("%s-%s", account.Hex(), asset.Hex())
}
Expand Down

0 comments on commit 4fee6a2

Please sign in to comment.