-
Notifications
You must be signed in to change notification settings - Fork 199
/
trieStorageManagerInEpoch.go
87 lines (70 loc) · 2.42 KB
/
trieStorageManagerInEpoch.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
package trie
import (
"fmt"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/errors"
)
// numEpochsToVerify needs to be at least 2 due to a snapshotting edge-case.
// The trie nodes modified between when a start of epoch block is committed until it is notarized by meta
// are not copied during snapshot to the new storer. So in order to have access to all trie data related
// to a certain root hash, both current storer and previous storer need to be verified.
const numEpochsToVerify = uint32(2)
type trieStorageManagerInEpoch struct {
*trieStorageManager
mainStorer dbWithGetFromEpoch
epoch uint32
}
func newTrieStorageManagerInEpoch(storageManager common.StorageManager, epoch uint32) (*trieStorageManagerInEpoch, error) {
if check.IfNil(storageManager) {
return nil, ErrNilTrieStorage
}
tsm, ok := storageManager.GetBaseTrieStorageManager().(*trieStorageManager)
if !ok {
return nil, fmt.Errorf("invalid storage manager, type is %T", storageManager.GetBaseTrieStorageManager())
}
storer, ok := tsm.mainStorer.(dbWithGetFromEpoch)
if !ok {
return nil, fmt.Errorf("invalid storer, type is %T", tsm.mainStorer)
}
return &trieStorageManagerInEpoch{
trieStorageManager: tsm,
mainStorer: storer,
epoch: epoch,
}, nil
}
// Get checks all the storers for the given key, and returns it if it is found
func (tsmie *trieStorageManagerInEpoch) Get(key []byte) ([]byte, error) {
tsmie.storageOperationMutex.Lock()
defer tsmie.storageOperationMutex.Unlock()
if tsmie.closed {
log.Debug("trieStorageManagerInEpoch get context closing", "key", key)
return nil, errors.ErrContextClosing
}
for i := uint32(0); i < numEpochsToVerify; i++ {
if i > tsmie.epoch {
break
}
epoch := tsmie.epoch - i
val, err := tsmie.mainStorer.GetFromEpoch(key, epoch)
treatGetFromEpochError(err, epoch)
if len(val) != 0 {
return val, nil
}
}
return nil, ErrKeyNotFound
}
func treatGetFromEpochError(err error, epoch uint32) {
if err == nil {
return
}
if errors.IsClosingError(err) {
log.Debug("trieStorageManagerInEpoch closing err", "error", err.Error(), "epoch", epoch)
return
}
log.Warn("trieStorageManagerInEpoch", "error", err.Error(), "epoch", epoch)
}
// IsInterfaceNil returns true if there is no value under the interface
func (tsmie *trieStorageManagerInEpoch) IsInterfaceNil() bool {
return tsmie == nil
}