/
hook_listener.go
68 lines (56 loc) · 1.79 KB
/
hook_listener.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"github.com/dymensionxyz/dymension/v3/x/sequencer/types"
)
var _ rollapptypes.RollappHooks = rollapphook{}
// Hooks wrapper struct for rollapp keeper.
type rollapphook struct {
k Keeper
}
// Return the wrapper struct.
func (k Keeper) RollappHooks() rollapptypes.RollappHooks {
return rollapphook{
k,
}
}
func (hook rollapphook) BeforeUpdateState(ctx sdk.Context, seqAddr string, rollappId string) error {
// check to see if the sequencer has been registered before
sequencer, found := hook.k.GetSequencer(ctx, seqAddr)
if !found {
return types.ErrUnknownSequencer
}
// check to see if the rollappId matches the one of the sequencer
if sequencer.RollappId != rollappId {
return types.ErrSequencerRollappMismatch
}
// check to see if the sequencer is active and can make the update
if sequencer.Status != types.Bonded {
return types.ErrInvalidSequencerStatus
}
if !sequencer.Proposer {
return types.ErrNotActiveSequencer
}
return nil
}
func (hook rollapphook) AfterStateFinalized(ctx sdk.Context, rollappID string, stateInfo *rollapptypes.StateInfo) error {
return nil
}
// FraudSubmitted implements the RollappHooks interface
// It slashes the sequencer and unbonds all other bonded sequencers
func (hook rollapphook) FraudSubmitted(ctx sdk.Context, rollappID string, height uint64, seqAddr string) error {
err := hook.k.Slashing(ctx, seqAddr)
if err != nil {
return err
}
// unbond all other bonded sequencers
sequencers := hook.k.GetSequencersByRollappByStatus(ctx, rollappID, types.Bonded)
for _, sequencer := range sequencers {
err := hook.k.forceUnbondSequencer(ctx, sequencer.SequencerAddress)
if err != nil {
return err
}
}
return nil
}