aboutsummaryrefslogtreecommitdiffstats
path: root/state
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-03-13 21:37:54 +0800
committerobscuren <geffobscura@gmail.com>2015-03-13 21:37:54 +0800
commitcda88ce31a45af001443c71fb16bdb2bbe1efbc7 (patch)
tree251b3340f4666e153bd347aab0c20e526c359bff /state
parentaa9f981d5f9e45d4f92ad3ccd98454aaa59e87c9 (diff)
downloaddexon-cda88ce31a45af001443c71fb16bdb2bbe1efbc7.tar
dexon-cda88ce31a45af001443c71fb16bdb2bbe1efbc7.tar.gz
dexon-cda88ce31a45af001443c71fb16bdb2bbe1efbc7.tar.bz2
dexon-cda88ce31a45af001443c71fb16bdb2bbe1efbc7.tar.lz
dexon-cda88ce31a45af001443c71fb16bdb2bbe1efbc7.tar.xz
dexon-cda88ce31a45af001443c71fb16bdb2bbe1efbc7.tar.zst
dexon-cda88ce31a45af001443c71fb16bdb2bbe1efbc7.zip
Implemented a manage state for keeping track of nonces
Diffstat (limited to 'state')
-rw-r--r--state/managed_state.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/state/managed_state.go b/state/managed_state.go
new file mode 100644
index 000000000..b0a129af0
--- /dev/null
+++ b/state/managed_state.go
@@ -0,0 +1,56 @@
+package state
+
+import "sync"
+
+type ManagedState struct {
+ *StateDB
+
+ mu sync.RWMutex
+
+ accounts map[string]*StateObject
+}
+
+func ManageState(statedb *StateDB) *ManagedState {
+ return &ManagedState{
+ StateDB: statedb,
+ accounts: make(map[string]*StateObject),
+ }
+}
+
+func (ms *ManagedState) IncrementNonce(addr []byte) {
+ ms.mu.Lock()
+ defer ms.mu.Unlock()
+
+ ms.getAccount(addr).nonce++
+}
+
+func (ms *ManagedState) DecrementNonce(addr []byte) {
+ // Decrementing a nonce does not mean we are interested in the account
+ // incrementing only happens if you control the account, therefor
+ // incrementing behaves differently from decrementing
+ if ms.hasAccount(addr) {
+ ms.mu.Lock()
+ defer ms.mu.Unlock()
+
+ ms.getAccount(addr).nonce--
+ }
+}
+
+func (ms *ManagedState) GetNonce(addr []byte) uint64 {
+ ms.mu.RLock()
+ defer ms.mu.RUnlock()
+ return ms.getAccount(addr).nonce
+}
+
+func (ms *ManagedState) hasAccount(addr []byte) bool {
+ _, ok := ms.accounts[string(addr)]
+ return ok
+}
+
+func (ms *ManagedState) getAccount(addr []byte) *StateObject {
+ if _, ok := ms.accounts[string(addr)]; !ok {
+ ms.accounts[string(addr)] = ms.GetOrNewStateObject(addr)
+ }
+
+ return ms.accounts[string(addr)]
+}