@@ -23,7 +23,6 @@ import (
2323 "fmt"
2424 "math/big"
2525 "strings"
26- "sync"
2726 "time"
2827
2928 "github.com/ethereum/go-ethereum/accounts"
@@ -204,12 +203,13 @@ func (s *PublicAccountAPI) Accounts() []common.Address {
204203// It offers methods to create, (un)lock en list accounts. Some methods accept
205204// passwords and are therefore considered private by default.
206205type PrivateAccountAPI struct {
207- am * accounts.Manager
208- b Backend
206+ am * accounts.Manager
207+ nonceLock * AddrLocker
208+ b Backend
209209}
210210
211211// NewPrivateAccountAPI create a new PrivateAccountAPI.
212- func NewPrivateAccountAPI (b Backend ) * PrivateAccountAPI {
212+ func NewPrivateAccountAPI (b Backend , nonceLock * AddrLocker ) * PrivateAccountAPI {
213213 return & PrivateAccountAPI {
214214 am : b .AccountManager (),
215215 b : b ,
@@ -316,17 +316,25 @@ func (s *PrivateAccountAPI) LockAccount(addr common.Address) bool {
316316// tries to sign it with the key associated with args.To. If the given passwd isn't
317317// able to decrypt the key it fails.
318318func (s * PrivateAccountAPI ) SendTransaction (ctx context.Context , args SendTxArgs , passwd string ) (common.Hash , error ) {
319- // Set some sanity defaults and terminate on failure
320- if err := args .setDefaults (ctx , s .b ); err != nil {
321- return common.Hash {}, err
322- }
323319 // Look up the wallet containing the requested signer
324320 account := accounts.Account {Address : args .From }
325321
326322 wallet , err := s .am .Find (account )
327323 if err != nil {
328324 return common.Hash {}, err
329325 }
326+
327+ if args .Nonce == nil {
328+ // Hold the addresse's mutex around signing to prevent concurrent assignment of
329+ // the same nonce to multiple accounts.
330+ s .nonceLock .LockAddr (args .From )
331+ defer s .nonceLock .UnlockAddr (args .From )
332+ }
333+
334+ // Set some sanity defaults and terminate on failure
335+ if err := args .setDefaults (ctx , s .b ); err != nil {
336+ return common.Hash {}, err
337+ }
330338 // Assemble the transaction and sign with the wallet
331339 tx := args .toTransaction ()
332340
@@ -886,18 +894,13 @@ func newRPCTransaction(b *types.Block, txHash common.Hash) (*RPCTransaction, err
886894
887895// PublicTransactionPoolAPI exposes methods for the RPC interface
888896type PublicTransactionPoolAPI struct {
889- b Backend
897+ b Backend
898+ nonceLock * AddrLocker
890899}
891900
892- // nonceMutex is a global mutex for locking the nonce while a transaction
893- // is being submitted. This should be used when a nonce has not been provided by the user,
894- // and we get a nonce from the pools. The mutex prevents the (an identical nonce) from being
895- // read again during the time that the first transaction is being signed.
896- var nonceMutex sync.RWMutex
897-
898901// NewPublicTransactionPoolAPI creates a new RPC service with methods specific for the transaction pool.
899- func NewPublicTransactionPoolAPI (b Backend ) * PublicTransactionPoolAPI {
900- return & PublicTransactionPoolAPI {b }
902+ func NewPublicTransactionPoolAPI (b Backend , nonceLock * AddrLocker ) * PublicTransactionPoolAPI {
903+ return & PublicTransactionPoolAPI {b , nonceLock }
901904}
902905
903906func getTransaction (chainDb ethdb.Database , b Backend , txHash common.Hash ) (* types.Transaction , bool , error ) {
@@ -1176,24 +1179,25 @@ func submitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c
11761179// transaction pool.
11771180func (s * PublicTransactionPoolAPI ) SendTransaction (ctx context.Context , args SendTxArgs ) (common.Hash , error ) {
11781181
1179- if args .Nonce == nil {
1180- // We'll need to set nonce from pool, and thus we need to lock here
1181- nonceMutex .Lock ()
1182- defer nonceMutex .Unlock ()
1183-
1184- }
1185-
1186- // Set some sanity defaults and terminate on failure
1187- if err := args .setDefaults (ctx , s .b ); err != nil {
1188- return common.Hash {}, err
1189- }
11901182 // Look up the wallet containing the requested signer
11911183 account := accounts.Account {Address : args .From }
11921184
11931185 wallet , err := s .b .AccountManager ().Find (account )
11941186 if err != nil {
11951187 return common.Hash {}, err
11961188 }
1189+
1190+ if args .Nonce == nil {
1191+ // Hold the addresse's mutex around signing to prevent concurrent assignment of
1192+ // the same nonce to multiple accounts.
1193+ s .nonceLock .LockAddr (args .From )
1194+ defer s .nonceLock .UnlockAddr (args .From )
1195+ }
1196+
1197+ // Set some sanity defaults and terminate on failure
1198+ if err := args .setDefaults (ctx , s .b ); err != nil {
1199+ return common.Hash {}, err
1200+ }
11971201 // Assemble the transaction and sign with the wallet
11981202 tx := args .toTransaction ()
11991203
@@ -1270,14 +1274,12 @@ type SignTransactionResult struct {
12701274// The node needs to have the private key of the account corresponding with
12711275// the given from address and it needs to be unlocked.
12721276func (s * PublicTransactionPoolAPI ) SignTransaction (ctx context.Context , args SendTxArgs ) (* SignTransactionResult , error ) {
1273-
12741277 if args .Nonce == nil {
1275- // We'll need to set nonce from pool, and thus we need to lock here
1276- nonceMutex . Lock ()
1277- defer nonceMutex . Unlock ( )
1278-
1278+ // Hold the addresse's mutex around signing to prevent concurrent assignment of
1279+ // the same nonce to multiple accounts.
1280+ s . nonceLock . LockAddr ( args . From )
1281+ defer s . nonceLock . UnlockAddr ( args . From )
12791282 }
1280-
12811283 if err := args .setDefaults (ctx , s .b ); err != nil {
12821284 return nil , err
12831285 }
0 commit comments