/
store.go
89 lines (75 loc) · 3.18 KB
/
store.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
package types
import (
"strings"
"github.com/creatachain/creata-sdk/codec"
sdk "github.com/creatachain/creata-sdk/types"
sdkerrors "github.com/creatachain/creata-sdk/types/errors"
clienttypes "github.com/creatachain/creata-sdk/x/icp/core/02-client/types"
host "github.com/creatachain/creata-sdk/x/icp/core/24-host"
"github.com/creatachain/creata-sdk/x/icp/core/exported"
)
// KeyProcessedTime is appended to consensus state key to store the processed time
var KeyProcessedTime = []byte("/processedTime")
// GetConsensusState retrieves the consensus state from the client prefixed
// store. An error is returned if the consensus state does not exist.
func GetConsensusState(store sdk.KVStore, cdc codec.BinaryMarshaler, height exported.Height) (*ConsensusState, error) {
bz := store.Get(host.ConsensusStateKey(height))
if bz == nil {
return nil, sdkerrors.Wrapf(
clienttypes.ErrConsensusStateNotFound,
"consensus state does not exist for height %s", height,
)
}
consensusStateI, err := clienttypes.UnmarshalConsensusState(cdc, bz)
if err != nil {
return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "unmarshal error: %v", err)
}
consensusState, ok := consensusStateI.(*ConsensusState)
if !ok {
return nil, sdkerrors.Wrapf(
clienttypes.ErrInvalidConsensus,
"invalid consensus type %T, expected %T", consensusState, &ConsensusState{},
)
}
return consensusState, nil
}
// IterateProcessedTime iterates through the prefix store and applies the callback.
// If the cb returns true, then iterator will close and stop.
func IterateProcessedTime(store sdk.KVStore, cb func(key, val []byte) bool) {
iterator := sdk.KVStorePrefixIterator(store, []byte(host.KeyConsensusStatePrefix))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
keySplit := strings.Split(string(iterator.Key()), "/")
// processed time key in prefix store has format: "consensusState/<height>/processedTime"
if len(keySplit) != 3 || keySplit[2] != "processedTime" {
// ignore all consensus state keys
continue
}
if cb(iterator.Key(), iterator.Value()) {
break
}
}
}
// ProcessedTime Store code
// ProcessedTimeKey returns the key under which the processed time will be stored in the client store.
func ProcessedTimeKey(height exported.Height) []byte {
return append(host.ConsensusStateKey(height), KeyProcessedTime...)
}
// SetProcessedTime stores the time at which a header was processed and the corresponding consensus state was created.
// This is useful when validating whether a packet has reached the specified delay period in the augusteum client's
// verification functions
func SetProcessedTime(clientStore sdk.KVStore, height exported.Height, timeNs uint64) {
key := ProcessedTimeKey(height)
val := sdk.Uint64ToBigEndian(timeNs)
clientStore.Set(key, val)
}
// GetProcessedTime gets the time (in nanoseconds) at which this chain received and processed a augusteum header.
// This is used to validate that a received packet has passed the delay period.
func GetProcessedTime(clientStore sdk.KVStore, height exported.Height) (uint64, bool) {
key := ProcessedTimeKey(height)
bz := clientStore.Get(key)
if bz == nil {
return 0, false
}
return sdk.BigEndianToUint64(bz), true
}