Skip to content

Commit e004184

Browse files
committed
unsafe nonce store
1 parent 121fa42 commit e004184

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

chains/ethereum/chain.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type Connection interface {
5252
CallOpts() *bind.CallOpts
5353
LockAndUpdateOpts() error
5454
UnlockOpts()
55+
IncreaseNonce()
5556
Client() *ethclient.Client
5657
EnsureHasBytecode(address common.Address) error
5758
LatestBlock() (*big.Int, error)

chains/ethereum/writer_methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"math/big"
1010
"time"
1111

12+
log "github.com/ChainSafe/log15"
1213
utils "github.com/Phala-Network/ChainBridge/shared/ethereum"
1314
"github.com/Phala-Network/chainbridge-utils/msg"
14-
log "github.com/ChainSafe/log15"
1515
)
1616

1717
// Number of blocks to wait for an finalization event
@@ -265,6 +265,7 @@ func (w *writer) voteProposal(m msg.Message, dataHash [32]byte) {
265265
w.conn.UnlockOpts()
266266

267267
if err == nil {
268+
w.conn.IncreaseNonce()
268269
w.log.Info("Submitted proposal vote", "tx", tx.Hash(), "src", m.Source, "depositNonce", m.DepositNonce)
269270
if w.metrics != nil {
270271
w.metrics.VotesSubmitted.Inc()
@@ -312,6 +313,7 @@ func (w *writer) executeProposal(m msg.Message, data []byte, dataHash [32]byte)
312313
w.conn.UnlockOpts()
313314

314315
if err == nil {
316+
w.conn.IncreaseNonce()
315317
w.log.Info("Submitted proposal execution", "tx", tx.Hash(), "src", m.Source, "dst", m.Destination, "nonce", m.DepositNonce)
316318
return
317319
} else if err.Error() == ErrNonceTooLow.Error() || err.Error() == ErrTxUnderpriced.Error() {

connections/ethereum/connection.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"sync"
1212
"time"
1313

14-
"github.com/Phala-Network/chainbridge-utils/crypto/secp256k1"
1514
"github.com/ChainSafe/log15"
15+
"github.com/Phala-Network/chainbridge-utils/crypto/secp256k1"
1616
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1717
ethcommon "github.com/ethereum/go-ethereum/common"
1818
ethcrypto "github.com/ethereum/go-ethereum/crypto"
@@ -31,12 +31,13 @@ type Connection struct {
3131
gasMultiplier *big.Float
3232
conn *ethclient.Client
3333
// signer ethtypes.Signer
34-
opts *bind.TransactOpts
35-
callOpts *bind.CallOpts
36-
nonce uint64
37-
optsLock sync.Mutex
38-
log log15.Logger
39-
stop chan int // All routines should exit when this channel is closed
34+
opts *bind.TransactOpts
35+
callOpts *bind.CallOpts
36+
nonce uint64
37+
nonceLock sync.Mutex
38+
optsLock sync.Mutex
39+
log log15.Logger
40+
stop chan int // All routines should exit when this channel is closed
4041
}
4142

4243
// NewConnection returns an uninitialized connection, must call Connection.Connect() before using.
@@ -80,12 +81,34 @@ func (c *Connection) Connect() error {
8081
return nil
8182
}
8283

84+
func (c *Connection) unsafeNonce(address ethcommon.Address) (uint64, error) {
85+
// Sync on-chain nonce
86+
nonce, err := c.conn.NonceAt(context.Background(), address, nil)
87+
if err != nil {
88+
return 0, err
89+
}
90+
if c.nonce >= nonce {
91+
return c.nonce, nil
92+
} else {
93+
c.nonce = nonce
94+
return nonce, nil
95+
}
96+
}
97+
98+
// LockAndIncreaseNonce acquires a lock on the opts before increase nonce by 1
99+
// and gas price.
100+
func (c *Connection) IncreaseNonce() {
101+
c.nonceLock.Lock()
102+
c.nonce += 1
103+
c.nonceLock.Unlock()
104+
}
105+
83106
// newTransactOpts builds the TransactOpts for the connection's keypair.
84107
func (c *Connection) newTransactOpts(value, gasLimit, gasPrice *big.Int) (*bind.TransactOpts, uint64, error) {
85108
privateKey := c.kp.PrivateKey()
86109
address := ethcrypto.PubkeyToAddress(privateKey.PublicKey)
87110

88-
nonce, err := c.conn.PendingNonceAt(context.Background(), address)
111+
nonce, err := c.unsafeNonce(address)
89112
if err != nil {
90113
return nil, 0, err
91114
}
@@ -167,12 +190,13 @@ func (c *Connection) LockAndUpdateOpts() error {
167190
}
168191
c.opts.GasPrice = gasPrice
169192

170-
nonce, err := c.conn.PendingNonceAt(context.Background(), c.opts.From)
193+
nonce, err := c.unsafeNonce(c.opts.From)
171194
if err != nil {
172195
c.optsLock.Unlock()
173196
return err
174197
}
175198
c.opts.Nonce.SetUint64(nonce)
199+
c.log.Info("LockAndUpdateOpts: update opts nonce to: ", nonce)
176200
return nil
177201
}
178202

0 commit comments

Comments
 (0)