Skip to content

Commit 4fee6a2

Browse files
committed
refactor(contractbackend):
- Change SharedMutex to SharedExpectedNoncesMutex. refactor(erc20_depositor): - Return directly if Approval returns error. - Fix comments. Signed-off-by: sophia1ch <sophia.koehler@outlook.com>
1 parent d81f135 commit 4fee6a2

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

channel/contractbackend.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ const (
4646
var errTxTimedOut = errors.New("")
4747

4848
var (
49-
// SharedExpectedNonces is a map of each expected next nonce of all clients.
50-
SharedExpectedNoncesMutex &sync.Mutex{}
51-
SharedExpectedNonces map[ChainID]map[common.Address]uint64
52-
)
49+
// SharedExpectedNonces is a map of each expected next nonce of all clients.
50+
SharedExpectedNonces map[ChainID]map[common.Address]uint64
51+
// SharedExpectedNoncesMutex is a mutex to protect the shared expected nonces map.
52+
SharedExpectedNoncesMutex = &sync.Mutex{}
53+
)
5354

5455
// ContractInterface provides all functions needed by an ethereum backend.
5556
// Both test.SimulatedBackend and ethclient.Client implement this interface.
@@ -178,8 +179,8 @@ func (c *ContractBackend) nonce(ctx context.Context, sender common.Address) (uin
178179
err = cherrors.CheckIsChainNotReachableError(err)
179180
return 0, errors.WithMessage(err, "fetching nonce")
180181
}
181-
SharedMutex.Lock()
182-
defer SharedMutex.Unlock()
182+
SharedExpectedNoncesMutex.Lock()
183+
defer SharedExpectedNoncesMutex.Unlock()
183184
expectedNextNonce, found := c.expectedNextNonce[sender]
184185
if !found {
185186
c.expectedNextNonce[sender] = 0

channel/erc20_depositor.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ func (d *ERC20Depositor) Deposit(ctx context.Context, req DepositReq) (types.Tra
7474
Pending: false,
7575
Context: ctx,
7676
}
77-
// variables for the return value.
7877
var depResult DepositResult
7978
txApproval, approvalReceived, errApproval := Approve(ctx, lock, req, token, callOpts)
8079
if errApproval != nil {
81-
depResult.Error = errApproval
80+
return nil, errors.WithMessagef(errApproval, "approving asset: %x", req.Asset)
8281
}
8382
if approvalReceived {
8483
txDeposit, err := d.DepositOnly(ctx, req)
@@ -104,16 +103,16 @@ func (d *ERC20Depositor) DepositOnly(ctx context.Context, req DepositReq) (*type
104103
return nil, errors.WithMessagef(err, "creating transactor for asset: %x", req.Asset)
105104
}
106105

107-
tx2, err := assetholder.Deposit(opts, req.FundingID, req.Balance)
108-
return tx2, err
106+
tx, err := assetholder.Deposit(opts, req.FundingID, req.Balance)
107+
return tx, err
109108
}
110109

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

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

0 commit comments

Comments
 (0)