-
Notifications
You must be signed in to change notification settings - Fork 326
/
message_update_state.go
84 lines (70 loc) · 2.6 KB
/
message_update_state.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const TypeMsgUpdateState = "update_state"
var _ sdk.Msg = &MsgUpdateState{}
func NewMsgUpdateState(creator string, rollappId string, startHeight uint64, numBlocks uint64, dAPath string, version uint64, bDs *BlockDescriptors) *MsgUpdateState {
return &MsgUpdateState{
Creator: creator,
RollappId: rollappId,
StartHeight: startHeight,
NumBlocks: numBlocks,
DAPath: dAPath,
Version: version,
BDs: *bDs,
}
}
func (msg *MsgUpdateState) Route() string {
return RouterKey
}
func (msg *MsgUpdateState) Type() string {
return TypeMsgUpdateState
}
func (msg *MsgUpdateState) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgUpdateState) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgUpdateState) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
// an update cann't be with no BDs
if msg.NumBlocks == uint64(0) {
return sdkerrors.Wrap(ErrInvalidNumBlocks, "number of blocks can not be zero")
}
// check to see that update contains all BDs
if len(msg.BDs.BD) != int(msg.NumBlocks) {
return sdkerrors.Wrapf(ErrInvalidNumBlocks, "number of blocks (%d) != number of block descriptors(%d)", msg.NumBlocks, len(msg.BDs.BD))
}
// check to see that startHeight is not zaro
if msg.StartHeight == 0 {
return sdkerrors.Wrapf(ErrWrongBlockHeight, "StartHeight must be greater than zero")
}
// check that the blocks are sequential by height
for bdIndex := uint64(0); bdIndex < msg.NumBlocks; bdIndex += 1 {
if msg.BDs.BD[bdIndex].Height != msg.StartHeight+bdIndex {
return ErrInvalidBlockSequence
}
// check to see stateRoot is a 32 byte array
if len(msg.BDs.BD[bdIndex].StateRoot) != 32 {
return sdkerrors.Wrapf(ErrInvalidStateRoot, "StateRoot of block high (%d) must be 32 byte array. But received (%d) bytes",
msg.BDs.BD[bdIndex].Height, len(msg.BDs.BD[bdIndex].StateRoot))
}
// check to see IntermediateStatesRoot is a 32 byte array
if len(msg.BDs.BD[bdIndex].IntermediateStatesRoot) != 32 {
return sdkerrors.Wrapf(ErrInvalidIntermediateStatesRoot, "IntermediateStatesRoot of block high (%d) must be 32 byte array. But received (%d) bytes",
msg.BDs.BD[bdIndex].Height, len(msg.BDs.BD[bdIndex].IntermediateStatesRoot))
}
}
return nil
}