chore(wallet)_: proper release of locked nonce (unlock function call)

This commit is contained in:
Sale Djenic 2023-11-17 12:38:12 +01:00 committed by saledjenic
parent 03cdb49c4c
commit e9f11f70dd
4 changed files with 26 additions and 13 deletions

View File

@ -1 +1 @@
0.171.13 0.171.14

View File

@ -400,9 +400,11 @@ func (tm *TransactionManager) ProceedWithTransactionsSignatures(ctx context.Cont
hashes := make(map[uint64][]types.Hash) hashes := make(map[uint64][]types.Hash)
for _, desc := range tm.transactionsForKeycardSingning { for _, desc := range tm.transactionsForKeycardSingning {
hash, err := tm.transactor.SendBuiltTransactionWithSignature(desc.chainID, desc.builtTx, desc.signature) hash, err := tm.transactor.SendBuiltTransactionWithSignature(desc.chainID, desc.builtTx, desc.signature)
defer func() { if desc.unlock != nil {
desc.unlock(err == nil, desc.builtTx.Nonce()) defer func() {
}() desc.unlock(err == nil, desc.builtTx.Nonce())
}()
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -25,6 +25,10 @@ func NewNonce() *Nonce {
func (n *Nonce) Next(rpcWrapper *rpcWrapper, from types.Address) (uint64, UnlockNonceFunc, error) { func (n *Nonce) Next(rpcWrapper *rpcWrapper, from types.Address) (uint64, UnlockNonceFunc, error) {
n.addrLock.LockAddr(from) n.addrLock.LockAddr(from)
current, err := n.GetCurrent(rpcWrapper, from) current, err := n.GetCurrent(rpcWrapper, from)
if err != nil {
return 0, nil, err
}
unlock := func(inc bool, nonce uint64) { unlock := func(inc bool, nonce uint64) {
if inc { if inc {
if _, ok := n.localNonce[rpcWrapper.chainID]; !ok { if _, ok := n.localNonce[rpcWrapper.chainID]; !ok {
@ -36,7 +40,7 @@ func (n *Nonce) Next(rpcWrapper *rpcWrapper, from types.Address) (uint64, Unlock
n.addrLock.UnlockAddr(from) n.addrLock.UnlockAddr(from)
} }
return current, unlock, err return current, unlock, nil
} }
func (n *Nonce) GetCurrent(rpcWrapper *rpcWrapper, from types.Address) (uint64, error) { func (n *Nonce) GetCurrent(rpcWrapper *rpcWrapper, from types.Address) (uint64, error) {

View File

@ -157,9 +157,11 @@ func (t *Transactor) SendTransactionWithSignature(chainID uint64, args SendTxArg
if err != nil { if err != nil {
return hash, err return hash, err
} }
defer func() { if unlock != nil {
unlock(err == nil, expectedNonce) defer func() {
}() unlock(err == nil, expectedNonce)
}()
}
if tx.Nonce() != expectedNonce { if tx.Nonce() != expectedNonce {
return hash, &ErrBadNonce{tx.Nonce(), expectedNonce} return hash, &ErrBadNonce{tx.Nonce(), expectedNonce}
@ -179,7 +181,9 @@ func (t *Transactor) HashTransaction(args SendTxArgs) (validatedArgs SendTxArgs,
if err != nil { if err != nil {
return validatedArgs, hash, err return validatedArgs, hash, err
} }
defer unlock(false, 0) if unlock != nil {
defer unlock(false, 0)
}
gasPrice := (*big.Int)(args.GasPrice) gasPrice := (*big.Int)(args.GasPrice)
gasFeeCap := (*big.Int)(args.MaxFeePerGas) gasFeeCap := (*big.Int)(args.MaxFeePerGas)
@ -273,13 +277,16 @@ func (t *Transactor) validateAndBuildTransaction(rpcWrapper *rpcWrapper, args Se
return tx, nil, ErrInvalidSendTxArgs return tx, nil, ErrInvalidSendTxArgs
} }
nonce, unlock, err := t.nonce.Next(rpcWrapper, args.From) var nonce uint64
if err != nil {
return tx, nil, err
}
if args.Nonce != nil { if args.Nonce != nil {
nonce = uint64(*args.Nonce) nonce = uint64(*args.Nonce)
} else {
nonce, unlock, err = t.nonce.Next(rpcWrapper, args.From)
if err != nil {
return tx, nil, err
}
} }
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout) ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
defer cancel() defer cancel()