/
cachedb.go
166 lines (139 loc) · 4.28 KB
/
cachedb.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
165
166
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package storage
import (
comm "github.com/ontio/layer2/node/common"
"github.com/ontio/layer2/node/core/payload"
"github.com/ontio/layer2/node/core/store/common"
"github.com/ontio/layer2/node/core/store/overlaydb"
"github.com/syndtr/goleveldb/leveldb/util"
)
// CacheDB is smart contract execute cache, it contain transaction cache and block cache
// When smart contract execute finish, need to commit transaction cache to block cache
type CacheDB struct {
memdb *overlaydb.MemDB
backend *overlaydb.OverlayDB
keyScratch []byte
}
const initCap = 1024
const initKvNum = 16
// NewCacheDB return a new contract cache
func NewCacheDB(store *overlaydb.OverlayDB) *CacheDB {
return &CacheDB{
backend: store,
memdb: overlaydb.NewMemDB(initCap, initKvNum),
}
}
func (self *CacheDB) Reset() {
self.memdb.Reset()
}
func ensureBuffer(b []byte, n int) []byte {
if cap(b) < n {
return make([]byte, n)
}
return b[:n]
}
func makePrefixedKey(dst []byte, prefix byte, key []byte) []byte {
dst = ensureBuffer(dst, len(key)+1)
dst[0] = prefix
copy(dst[1:], key)
return dst
}
// Commit current transaction cache to block cache
func (self *CacheDB) Commit() {
self.memdb.ForEach(func(key, val []byte) {
if len(val) == 0 {
self.backend.Delete(key)
} else {
self.backend.Put(key, val)
}
})
}
func (self *CacheDB) Put(key []byte, value []byte) {
self.put(common.ST_STORAGE, key, value)
}
func (self *CacheDB) put(prefix common.DataEntryPrefix, key []byte, value []byte) {
self.keyScratch = makePrefixedKey(self.keyScratch, byte(prefix), key)
self.memdb.Put(self.keyScratch, value)
}
func (self *CacheDB) GetContract(addr comm.Address) (*payload.DeployCode, error) {
value, err := self.get(common.ST_CONTRACT, addr[:])
if err != nil {
return nil, err
}
if len(value) == 0 {
return nil, nil
}
contract := new(payload.DeployCode)
if err := contract.Deserialization(comm.NewZeroCopySource(value)); err != nil {
return nil, err
}
return contract, nil
}
func (self *CacheDB) PutContract(contract *payload.DeployCode) {
address := contract.Address()
sink := comm.NewZeroCopySink(nil)
contract.Serialization(sink)
value := sink.Bytes()
self.put(common.ST_CONTRACT, address[:], value)
}
func (self *CacheDB) DeleteContract(address comm.Address) {
self.delete(common.ST_CONTRACT, address[:])
}
func (self *CacheDB) Get(key []byte) ([]byte, error) {
return self.get(common.ST_STORAGE, key)
}
func (self *CacheDB) get(prefix common.DataEntryPrefix, key []byte) ([]byte, error) {
self.keyScratch = makePrefixedKey(self.keyScratch, byte(prefix), key)
value, unknown := self.memdb.Get(self.keyScratch)
if unknown {
v, err := self.backend.Get(self.keyScratch)
if err != nil {
return nil, err
}
value = v
}
return value, nil
}
func (self *CacheDB) Delete(key []byte) {
self.delete(common.ST_STORAGE, key)
}
// Delete item from cache
func (self *CacheDB) delete(prefix common.DataEntryPrefix, key []byte) {
self.keyScratch = makePrefixedKey(self.keyScratch, byte(prefix), key)
self.memdb.Delete(self.keyScratch)
}
func (self *CacheDB) NewIterator(key []byte) common.StoreIterator {
pkey := make([]byte, 1+len(key))
pkey[0] = byte(common.ST_STORAGE)
copy(pkey[1:], key)
prefixRange := util.BytesPrefix(pkey)
backIter := self.backend.NewIterator(pkey)
memIter := self.memdb.NewIterator(prefixRange)
return &Iter{overlaydb.NewJoinIter(memIter, backIter)}
}
type Iter struct {
*overlaydb.JoinIter
}
func (self *Iter) Key() []byte {
key := self.JoinIter.Key()
if len(key) != 0 {
key = key[1:] // remove the first prefix
}
return key
}