-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
164 lines (145 loc) · 4.33 KB
/
cache.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package data
import (
"github.com/fletaio/common"
"github.com/fletaio/common/hash"
"github.com/fletaio/core/account"
"github.com/fletaio/core/transaction"
"github.com/fletaio/framework/chain"
)
type cache struct {
ctx *Context
SeqMap map[common.Address]uint64
AccountMap map[common.Address]account.Account
AccountNameMap map[string]common.Address
AccountDataMap map[string][]byte
AccountDataKeysMap map[common.Address][][]byte
UTXOMap map[uint64]*transaction.UTXO
}
// NewCache is used for generating genesis state
func newCache(ctx *Context) *cache {
return &cache{
ctx: ctx,
SeqMap: map[common.Address]uint64{},
AccountMap: map[common.Address]account.Account{},
AccountNameMap: map[string]common.Address{},
AccountDataMap: map[string][]byte{},
AccountDataKeysMap: map[common.Address][][]byte{},
UTXOMap: map[uint64]*transaction.UTXO{},
}
}
// ChainCoord returns the coordinate of the target chain
func (cc *cache) ChainCoord() *common.Coordinate {
return cc.ctx.ChainCoord()
}
// Accounter returns the accounter of the target chain
func (cc *cache) Accounter() *Accounter {
return cc.ctx.Accounter()
}
// Transactor returns the transactor of the target chain
func (cc *cache) Transactor() *Transactor {
return cc.ctx.Transactor()
}
// Provider returns the provider of the target chain
func (cc *cache) Provider() chain.Provider {
return cc.ctx.Provider()
}
// Eventer returns the eventer of the target chain
func (cc *cache) Eventer() *Eventer {
return cc.ctx.Eventer()
}
// TargetHeight returns cached target height when context generation
func (cc *cache) TargetHeight() uint32 {
return cc.ctx.TargetHeight()
}
// LastHash returns the recorded prev hash when context generation
func (cc *cache) LastHash() hash.Hash256 {
return cc.ctx.LastHash()
}
// Seq returns the sequence of the account
func (cc *cache) Seq(addr common.Address) uint64 {
if seq, has := cc.SeqMap[addr]; has {
return seq
} else {
seq := cc.ctx.loader.Seq(addr)
cc.SeqMap[addr] = seq
return seq
}
}
// Account returns the account instance of the address
func (cc *cache) Account(addr common.Address) (account.Account, error) {
if acc, has := cc.AccountMap[addr]; has {
return acc, nil
} else {
if acc, err := cc.ctx.loader.Account(addr); err != nil {
return nil, err
} else {
cc.AccountMap[addr] = acc
return acc, nil
}
}
}
// AddressByName returns the account address of the name
func (cc *cache) AddressByName(Name string) (common.Address, error) {
if addr, has := cc.AccountNameMap[Name]; has {
return addr, nil
} else {
if addr, err := cc.ctx.loader.AddressByName(Name); err != nil {
return common.Address{}, err
} else {
cc.AccountNameMap[Name] = addr
return addr, nil
}
}
}
// IsExistAccount checks that the account of the address is exist or not
func (cc *cache) IsExistAccount(addr common.Address) (bool, error) {
if _, has := cc.AccountMap[addr]; has {
return true, nil
} else {
return cc.ctx.loader.IsExistAccount(addr)
}
}
// IsExistAccountName checks that the account of the name is exist or not
func (cc *cache) IsExistAccountName(Name string) (bool, error) {
if _, has := cc.AccountNameMap[Name]; has {
return true, nil
} else {
return cc.ctx.loader.IsExistAccountName(Name)
}
}
// AccountDataKeys returns all data keys of the account in the context
func (cc *cache) AccountDataKeys(addr common.Address, Prefix []byte) ([][]byte, error) {
return cc.ctx.loader.AccountDataKeys(addr, Prefix)
}
// AccountData returns the account data
func (cc *cache) AccountData(addr common.Address, name []byte) []byte {
key := string(addr[:]) + string(name)
if value, has := cc.AccountDataMap[key]; has {
return value
} else {
value := cc.ctx.loader.AccountData(addr, name)
cc.AccountDataMap[key] = value
return value
}
}
// IsExistUTXO checks that the utxo of the id is exist or not
func (cc *cache) IsExistUTXO(id uint64) (bool, error) {
if _, has := cc.UTXOMap[id]; has {
return true, nil
} else {
return false, nil
}
}
// UTXO returns the UTXO
func (cc *cache) UTXO(id uint64) (*transaction.UTXO, error) {
if utxo, has := cc.UTXOMap[id]; has {
return utxo, nil
} else {
if utxo, err := cc.ctx.loader.UTXO(id); err != nil {
return nil, err
} else {
cc.UTXOMap[id] = utxo
return utxo, nil
}
}
}