-
Notifications
You must be signed in to change notification settings - Fork 39
/
accountcache.go
115 lines (104 loc) · 2.89 KB
/
accountcache.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
/*
* Copyright 2020 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package icstate
import (
"github.com/icon-project/goloop/common"
"github.com/icon-project/goloop/common/containerdb"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/icon/iiss/icobject"
"github.com/icon-project/goloop/icon/iiss/icutils"
"github.com/icon-project/goloop/module"
)
type AccountCache struct {
dict *containerdb.DictDB
accounts map[string]*AccountState
}
func (c *AccountCache) Get(owner module.Address, createIfNotExist bool) *AccountState {
key := icutils.ToKey(owner)
account, ok := c.accounts[key]
if ok {
return account
}
o := c.dict.Get(owner)
if o == nil {
if createIfNotExist {
account = newAccountStateWithSnapshot(nil)
} else {
return nil
}
} else {
account = newAccountStateWithSnapshot(ToAccount(o.Object()))
}
c.accounts[key] = account
return account
}
func (c *AccountCache) Clear() {
c.Flush()
c.accounts = make(map[string]*AccountState)
}
func (c *AccountCache) GetSnapshot(owner module.Address) *AccountSnapshot {
key := icutils.ToKey(owner)
account, ok := c.accounts[key]
if ok {
return account.GetSnapshot()
}
o := c.dict.Get(owner)
if o == nil {
return nil
} else {
return ToAccount(o.Object())
}
}
func (c *AccountCache) Reset() {
for key, account := range c.accounts {
addr, err := common.NewAddress([]byte(key))
if err != nil {
panic(errors.Errorf("Address convert error"))
}
value := c.dict.Get(addr)
if value == nil {
account.Reset(emptyAccountSnapshot)
} else {
account.Reset(ToAccount(value.Object()))
}
}
}
func (c *AccountCache) Flush() {
for k, account := range c.accounts {
key, err := common.BytesToAddress([]byte(k))
if err != nil {
panic(errors.Errorf("AccountCache is broken: %s", k))
}
ass := account.GetSnapshot()
if ass.IsEmpty() {
if err = c.dict.Delete(key); err != nil {
log.Errorf("Failed to delete Account key %x, err+%+v", key, err)
}
} else {
o := icobject.New(TypeAccount, ass)
if err := c.dict.Set(key, o); err != nil {
log.Errorf("Failed to set state for %x, err+%+v", key, err)
}
}
}
}
func newAccountCache(store containerdb.ObjectStoreState) *AccountCache {
return &AccountCache{
accounts: make(map[string]*AccountState),
dict: containerdb.NewDictDB(store, 1, AccountDictPrefix),
}
}