github.com/immesys/bw2bc@v1.1.0/core/state/managed_state.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package state
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  )
    24  
    25  type account struct {
    26  	stateObject *StateObject
    27  	nstart      uint64
    28  	nonces      []bool
    29  }
    30  
    31  type ManagedState struct {
    32  	*StateDB
    33  
    34  	mu sync.RWMutex
    35  
    36  	accounts map[string]*account
    37  }
    38  
    39  // ManagedState returns a new managed state with the statedb as it's backing layer
    40  func ManageState(statedb *StateDB) *ManagedState {
    41  	return &ManagedState{
    42  		StateDB:  statedb.Copy(),
    43  		accounts: make(map[string]*account),
    44  	}
    45  }
    46  
    47  // SetState sets the backing layer of the managed state
    48  func (ms *ManagedState) SetState(statedb *StateDB) {
    49  	ms.mu.Lock()
    50  	defer ms.mu.Unlock()
    51  	ms.StateDB = statedb
    52  }
    53  
    54  // RemoveNonce removed the nonce from the managed state and all future pending nonces
    55  func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) {
    56  	if ms.hasAccount(addr) {
    57  		ms.mu.Lock()
    58  		defer ms.mu.Unlock()
    59  
    60  		account := ms.getAccount(addr)
    61  		if n-account.nstart <= uint64(len(account.nonces)) {
    62  			reslice := make([]bool, n-account.nstart)
    63  			copy(reslice, account.nonces[:n-account.nstart])
    64  			account.nonces = reslice
    65  		}
    66  	}
    67  }
    68  
    69  // NewNonce returns the new canonical nonce for the managed account
    70  func (ms *ManagedState) NewNonce(addr common.Address) uint64 {
    71  	ms.mu.Lock()
    72  	defer ms.mu.Unlock()
    73  
    74  	account := ms.getAccount(addr)
    75  	for i, nonce := range account.nonces {
    76  		if !nonce {
    77  			return account.nstart + uint64(i)
    78  		}
    79  	}
    80  	account.nonces = append(account.nonces, true)
    81  
    82  	return uint64(len(account.nonces)-1) + account.nstart
    83  }
    84  
    85  // GetNonce returns the canonical nonce for the managed or unmanged account
    86  func (ms *ManagedState) GetNonce(addr common.Address) uint64 {
    87  	ms.mu.RLock()
    88  	defer ms.mu.RUnlock()
    89  
    90  	if ms.hasAccount(addr) {
    91  		account := ms.getAccount(addr)
    92  		return uint64(len(account.nonces)) + account.nstart
    93  	} else {
    94  		return ms.StateDB.GetNonce(addr)
    95  	}
    96  }
    97  
    98  // SetNonce sets the new canonical nonce for the managed state
    99  func (ms *ManagedState) SetNonce(addr common.Address, nonce uint64) {
   100  	ms.mu.Lock()
   101  	defer ms.mu.Unlock()
   102  
   103  	so := ms.GetOrNewStateObject(addr)
   104  	so.SetNonce(nonce)
   105  
   106  	ms.accounts[addr.Str()] = newAccount(so)
   107  }
   108  
   109  // HasAccount returns whether the given address is managed or not
   110  func (ms *ManagedState) HasAccount(addr common.Address) bool {
   111  	ms.mu.RLock()
   112  	defer ms.mu.RUnlock()
   113  	return ms.hasAccount(addr)
   114  }
   115  
   116  func (ms *ManagedState) hasAccount(addr common.Address) bool {
   117  	_, ok := ms.accounts[addr.Str()]
   118  	return ok
   119  }
   120  
   121  // populate the managed state
   122  func (ms *ManagedState) getAccount(addr common.Address) *account {
   123  	straddr := addr.Str()
   124  	if account, ok := ms.accounts[straddr]; !ok {
   125  		so := ms.GetOrNewStateObject(addr)
   126  		ms.accounts[straddr] = newAccount(so)
   127  	} else {
   128  		// Always make sure the state account nonce isn't actually higher
   129  		// than the tracked one.
   130  		so := ms.StateDB.GetStateObject(addr)
   131  		if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce {
   132  			ms.accounts[straddr] = newAccount(so)
   133  		}
   134  
   135  	}
   136  
   137  	return ms.accounts[straddr]
   138  }
   139  
   140  func newAccount(so *StateObject) *account {
   141  	return &account{so, so.nonce, nil}
   142  }