/
block_mem_store.go
139 lines (115 loc) · 3.65 KB
/
block_mem_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package store
import (
"sync"
"github.com/vitelabs/go-vite/interval/common"
)
func newMemoryStore() BlockStore {
self := &blockMemoryStore{}
for _, genesis := range genesisBlocks {
self.PutAccount(genesis.Signer(), genesis)
self.SetAccountHead(genesis.Signer(), &common.HashHeight{Hash: genesis.Hash(), Height: genesis.Height()})
}
return self
}
// thread safe block memory store
type blockMemoryStore struct {
snapshotHeight sync.Map
snapshotHash sync.Map
accountHeight sync.Map
accountHash sync.Map
// key: source hash val: received
sourceHash sync.Map
head sync.Map
sMu sync.Mutex
aMu sync.Mutex
}
var snapshotHeadKey = "s_head_key"
func (store *blockMemoryStore) GetSnapshotHead() *common.HashHeight {
value, ok := store.head.Load(snapshotHeadKey)
if !ok {
return nil
}
return value.(*common.HashHeight)
}
func (store *blockMemoryStore) GetAccountHead(address common.Address) *common.HashHeight {
value, ok := store.head.Load(address)
if !ok {
return nil
}
return value.(*common.HashHeight)
}
func (store *blockMemoryStore) SetSnapshotHead(hashH *common.HashHeight) {
if hashH == nil {
store.head.Delete(snapshotHeadKey)
} else {
store.head.Store(snapshotHeadKey, hashH)
}
}
func (store *blockMemoryStore) SetAccountHead(address common.Address, hashH *common.HashHeight) {
if hashH == nil {
store.head.Delete(address)
} else {
store.head.Store(address, hashH)
}
}
func (store *blockMemoryStore) DeleteSnapshot(hashH common.HashHeight) {
store.snapshotHeight.Delete(hashH.Height)
store.snapshotHash.Delete(hashH.Hash)
}
func (store *blockMemoryStore) DeleteAccount(address common.Address, hashH common.HashHeight) {
store.accountHash.Delete(hashH.Hash)
store.accountHeight.Delete(store.genKey(address, hashH.Height))
}
func (store *blockMemoryStore) PutSnapshot(block *common.SnapshotBlock) {
store.snapshotHash.Store(block.Hash(), block)
store.snapshotHeight.Store(block.Height(), block)
}
func (store *blockMemoryStore) PutAccount(address common.Address, block *common.AccountStateBlock) {
store.accountHash.Store(block.Hash(), block)
store.accountHeight.Store(store.genKey(address, block.Height()), block)
}
func (store *blockMemoryStore) GetSnapshotByHash(hash common.Hash) *common.SnapshotBlock {
value, ok := store.snapshotHash.Load(hash)
if !ok {
return nil
}
return value.(*common.SnapshotBlock)
}
func (store *blockMemoryStore) GetSnapshotByHeight(height common.Height) *common.SnapshotBlock {
value, ok := store.snapshotHeight.Load(height)
if !ok {
return nil
}
return value.(*common.SnapshotBlock)
}
func (store *blockMemoryStore) GetAccountByHash(address common.Address, hash common.Hash) *common.AccountStateBlock {
value, ok := store.accountHash.Load(hash)
if !ok {
return nil
}
return value.(*common.AccountStateBlock)
}
func (store *blockMemoryStore) GetAccountBySourceHash(hash common.Hash) *common.AccountStateBlock {
h, ok := store.sourceHash.Load(hash)
if !ok {
return nil
}
hashH := h.(*common.AccountHashH)
return store.GetAccountByHeight(hashH.Addr, hashH.Height)
}
func (store *blockMemoryStore) PutSourceHash(hash common.Hash, h *common.AccountHashH) {
store.sourceHash.Store(hash, h)
}
func (store *blockMemoryStore) DeleteSourceHash(hash common.Hash) {
store.sourceHash.Delete(hash)
}
func (store *blockMemoryStore) GetAccountByHeight(address common.Address, height common.Height) *common.AccountStateBlock {
value, ok := store.accountHeight.Load(store.genKey(address, height))
if !ok {
return nil
}
return value.(*common.AccountStateBlock)
}
func (store *blockMemoryStore) genKey(address common.Address, height common.Height) string {
return address.String() + "_" + height.String()
}