-
Notifications
You must be signed in to change notification settings - Fork 321
/
trie.go
69 lines (61 loc) · 2.13 KB
/
trie.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
// Copyright (c) 2019 IoTeX Foundation
// 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 trie
import (
"context"
"github.com/pkg/errors"
)
var (
// ErrInvalidTrie indicates something wrong causing invalid operation
ErrInvalidTrie = errors.New("invalid trie operation")
// ErrNotExist indicates entry does not exist
ErrNotExist = errors.New("not exist in trie")
// ErrEndOfIterator defines an error which will be returned
ErrEndOfIterator = errors.New("hit the end of the iterator, no more item")
)
type (
// Iterator iterates a trie
Iterator interface {
Next() ([]byte, []byte, error)
}
// Trie is the interface of Merkle Patricia Trie
Trie interface {
// Start starts the trie and the corresponding dependencies
Start(context.Context) error
// Stop stops the trie
Stop(context.Context) error
// Upsert inserts a new entry
Upsert([]byte, []byte) error
// Get retrieves an existing entry
Get([]byte) ([]byte, error)
// Delete deletes an entry
Delete([]byte) error
// RootHash returns trie's root hash
RootHash() ([]byte, error)
// SetRootHash sets a new root to trie
SetRootHash([]byte) error
// IsEmpty returns true is this is an empty trie
IsEmpty() bool
// Clone clones a trie with a new kvstore
Clone(KVStore) (Trie, error)
}
// TwoLayerTrie is a trie data structure with two layers
TwoLayerTrie interface {
// Start starts the layer one trie
Start(context.Context) error
// Stop stops the layer one trie
Stop(context.Context) error
// RootHash returns the layer one trie root
RootHash() ([]byte, error)
// SetRootHash sets root hash for layer one trie
SetRootHash([]byte) error
// Get returns the value in layer two
Get([]byte, []byte) ([]byte, error)
// Upsert upserts an item in layer two
Upsert([]byte, []byte, []byte) error
// Delete deletes an item in layer two
Delete([]byte, []byte) error
}
)