/
kvstorefortrie.go
63 lines (51 loc) · 1.78 KB
/
kvstorefortrie.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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package evm
import (
"context"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/db/trie"
"github.com/iotexproject/iotex-core/state"
)
type kvStoreForTrie struct {
nsOpt protocol.StateOption
sm protocol.StateManager
}
func newKVStoreForTrieWithStateManager(ns string, sm protocol.StateManager) trie.KVStore {
return &kvStoreForTrie{nsOpt: protocol.NamespaceOption(ns), sm: sm}
}
func (kv *kvStoreForTrie) Start(context.Context) error {
return nil
}
func (kv *kvStoreForTrie) Stop(context.Context) error {
return nil
}
func (kv *kvStoreForTrie) Put(key []byte, value []byte) error {
var sb SerializableBytes
if err := sb.Deserialize(value); err != nil {
return err
}
_, err := kv.sm.PutState(sb, protocol.KeyOption(key), kv.nsOpt)
return err
}
func (kv *kvStoreForTrie) Delete(key []byte) error {
_, err := kv.sm.DelState(protocol.KeyOption(key), kv.nsOpt)
if errors.Cause(err) == state.ErrStateNotExist {
return nil
}
return err
}
func (kv *kvStoreForTrie) Get(key []byte) ([]byte, error) {
var value SerializableBytes
_, err := kv.sm.State(&value, protocol.KeyOption(key), kv.nsOpt)
switch errors.Cause(err) {
case state.ErrStateNotExist:
return nil, errors.Wrapf(db.ErrNotExist, "failed to find key %x", key)
}
return value, err
}