From 8310bcda6100cc703f5c1e22e0122ad7476d689d Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 14 Apr 2015 12:47:54 +0200 Subject: [PATCH] state: fixed mutex lockes --- core/state/managed_state.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/core/state/managed_state.go b/core/state/managed_state.go index 97d098039..9e6be9980 100644 --- a/core/state/managed_state.go +++ b/core/state/managed_state.go @@ -37,7 +37,7 @@ func (ms *ManagedState) SetState(statedb *StateDB) { // RemoveNonce removed the nonce from the managed state and all future pending nonces func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) { - if ms.HasAccount(addr) { + if ms.hasAccount(addr) { ms.mu.Lock() defer ms.mu.Unlock() @@ -67,7 +67,10 @@ func (ms *ManagedState) NewNonce(addr common.Address) uint64 { // GetNonce returns the canonical nonce for the managed or unmanged account func (ms *ManagedState) GetNonce(addr common.Address) uint64 { - if ms.HasAccount(addr) { + ms.mu.RLock() + defer ms.mu.RUnlock() + + if ms.hasAccount(addr) { account := ms.getAccount(addr) return uint64(len(account.nonces)) + account.nstart } else { @@ -77,6 +80,9 @@ func (ms *ManagedState) GetNonce(addr common.Address) uint64 { // SetNonce sets the new canonical nonce for the managed state func (ms *ManagedState) SetNonce(addr common.Address, nonce uint64) { + ms.mu.Lock() + defer ms.mu.Unlock() + so := ms.GetOrNewStateObject(addr) so.SetNonce(nonce) @@ -85,6 +91,12 @@ func (ms *ManagedState) SetNonce(addr common.Address, nonce uint64) { // HasAccount returns whether the given address is managed or not func (ms *ManagedState) HasAccount(addr common.Address) bool { + ms.mu.RLock() + defer ms.mu.RUnlock() + return ms.hasAccount(addr) +} + +func (ms *ManagedState) hasAccount(addr common.Address) bool { _, ok := ms.accounts[addr.Str()] return ok }