forked from lesnikutsa/babylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevidence.go
65 lines (58 loc) · 2.23 KB
/
evidence.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
package keeper
import (
"context"
"cosmossdk.io/store/prefix"
bbn "github.com/babylonchain/babylon/types"
"github.com/babylonchain/babylon/x/finality/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k Keeper) SetEvidence(ctx context.Context, evidence *types.Evidence) {
store := k.evidenceStore(ctx, evidence.FpBtcPk)
store.Set(sdk.Uint64ToBigEndian(evidence.BlockHeight), k.cdc.MustMarshal(evidence))
}
func (k Keeper) HasEvidence(ctx context.Context, fpBtcPK *bbn.BIP340PubKey, height uint64) bool {
store := k.evidenceStore(ctx, fpBtcPK)
return store.Has(fpBtcPK.MustMarshal())
}
func (k Keeper) GetEvidence(ctx context.Context, fpBtcPK *bbn.BIP340PubKey, height uint64) (*types.Evidence, error) {
if uint64(sdk.UnwrapSDKContext(ctx).HeaderInfo().Height) < height {
return nil, types.ErrHeightTooHigh
}
store := k.evidenceStore(ctx, fpBtcPK)
evidenceBytes := store.Get(sdk.Uint64ToBigEndian(height))
if len(evidenceBytes) == 0 {
return nil, types.ErrEvidenceNotFound
}
var evidence types.Evidence
k.cdc.MustUnmarshal(evidenceBytes, &evidence)
return &evidence, nil
}
// GetFirstSlashableEvidence gets the first evidence that is slashable,
// i.e., it contains all fields.
// NOTE: it's possible that the CanonicalFinalitySig field is empty for
// an evidence, which happens when the finality provider signed a fork block
// but hasn't signed the canonical block yet.
func (k Keeper) GetFirstSlashableEvidence(ctx context.Context, fpBtcPK *bbn.BIP340PubKey) *types.Evidence {
store := k.evidenceStore(ctx, fpBtcPK)
iter := store.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
evidenceBytes := iter.Value()
var evidence types.Evidence
k.cdc.MustUnmarshal(evidenceBytes, &evidence)
if evidence.IsSlashable() {
return &evidence
}
}
return nil
}
// evidenceStore returns the KVStore of the evidences
// prefix: EvidenceKey
// key: (finality provider PK || height)
// value: Evidence
func (k Keeper) evidenceStore(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
eStore := prefix.NewStore(storeAdapter, types.EvidenceKey)
return prefix.NewStore(eStore, fpBTCPK.MustMarshal())
}