Skip to content

Commit

Permalink
change StartHeight & stateIndex - to start from 1 (dymensionxyz#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
liorzilp authored Aug 29, 2022
1 parent f266da5 commit 7349dc2
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 47 deletions.
30 changes: 30 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21447,6 21447,16 @@ paths:
in: query
required: false
type: boolean
- name: pagination.reverse
description: >-
reverse is set to true if results are to be returned in the
descending order.


Since: cosmos-sdk 0.43
in: query
required: false
type: boolean
tags:
- Query
'/dymensionxyz/dymension/sequencer/scheduler/{sequencerAddress}':
Expand Down Expand Up @@ -22171,6 22181,16 @@ paths:
in: query
required: false
type: boolean
- name: pagination.reverse
description: >-
reverse is set to true if results are to be returned in the
descending order.


Since: cosmos-sdk 0.43
in: query
required: false
type: boolean
tags:
- Query
'/dymensionxyz/dymension/sequencer/sequencer/{sequencerAddress}':
Expand Down Expand Up @@ -22894,6 22914,16 @@ paths:
in: query
required: false
type: boolean
- name: pagination.reverse
description: >-
reverse is set to true if results are to be returned in the
descending order.


Since: cosmos-sdk 0.43
in: query
required: false
type: boolean
tags:
- Query
'/dymensionxyz/dymension/sequencer/sequencers_by_rollapp/{rollappId}':
Expand Down
123 changes: 90 additions & 33 deletions x/rollapp/keeper/keeper_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 184,28 @@ func (suite *IntegrationTestSuite) TestFirstUpdateState() {
}
suite.app.SequencerKeeper.SetScheduler(suite.ctx, scheduler)

// check no index exists
expectedStateIndex, found := suite.app.RollappKeeper.GetStateIndex(suite.ctx, rollapp.GetRollappId())
suite.Require().EqualValues(false, found)

// update state
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 1}, {Height: 2}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
suite.Require().Nil(err)

// check first index is 1
expectedStateIndex, found = suite.app.RollappKeeper.GetStateIndex(suite.ctx, rollapp.GetRollappId())
suite.Require().EqualValues(true, found)
suite.Require().EqualValues(expectedStateIndex.Index, 1)
}

func (suite *IntegrationTestSuite) TestUpdateState() {
Expand Down Expand Up @@ -229,19 238,19 @@ func (suite *IntegrationTestSuite) TestUpdateState() {
// set initial stateIndex & StateInfo
stateIndex := types.StateIndex{
RollappId: "rollapp1",
Index: 0,
Index: 1,
}
stateInfo := types.StateInfo{
RollappId: "rollapp1",
StateIndex: 0,
StateIndex: 1,
Sequencer: sequencer.SequencerAddress,
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
CreationHeight: 0,
Status: types.STATE_STATUS_RECEIVED,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}
suite.app.RollappKeeper.SetStateIndex(suite.ctx, stateIndex)
suite.app.RollappKeeper.SetStateInfo(suite.ctx, stateInfo)
Expand All @@ -255,6 264,9 @@ func (suite *IntegrationTestSuite) TestUpdateState() {
// calc new updateState
expectedStateIndex, found := suite.app.RollappKeeper.GetStateIndex(suite.ctx, rollapp.GetRollappId())
suite.Require().EqualValues(true, found)
// verify index
suite.Require().EqualValues(i 1, expectedStateIndex.Index)
// load last state info
expectedStateInfo, found := suite.app.RollappKeeper.GetStateInfo(suite.ctx, rollapp.GetRollappId(), expectedStateIndex.GetIndex())
suite.Require().EqualValues(true, found)
updateState := types.MsgUpdateState{
Expand All @@ -281,11 293,11 @@ func (suite *IntegrationTestSuite) TestUpdateStateUnknownRollappId() {
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: "rollapp1",
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 0,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand All @@ -309,11 321,11 @@ func (suite *IntegrationTestSuite) TestUpdateStateVersionMismatch() {
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 0,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand All @@ -337,11 349,11 @@ func (suite *IntegrationTestSuite) TestUpdateStateUnknownSequencer() {
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand Down Expand Up @@ -373,11 385,11 @@ func (suite *IntegrationTestSuite) TestUpdateStateSequencerRollappMismatch() {
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand Down Expand Up @@ -412,17 424,62 @@ func (suite *IntegrationTestSuite) TestUpdateStateErrLogicUnpermissioned() {
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
suite.ErrorIs(err, sdkerrors.ErrLogic)
}

func (suite *IntegrationTestSuite) TestFirstUpdateStateErrWrongBlockHeightInitial() {
suite.SetupTest()
goCtx := sdk.WrapSDKContext(suite.ctx)

// set rollapp
rollapp := types.Rollapp{
RollappId: "rollapp1",
Creator: alice,
Version: 3,
MaxSequencers: 1,
PermissionedAddresses: sharedtypes.Sequencers{
Addresses: []string{},
},
}
suite.app.RollappKeeper.SetRollapp(suite.ctx, rollapp)

// set sequencer
sequencer := sequencertypes.Sequencer{
Creator: alice,
SequencerAddress: bob,
RollappId: "rollapp1",
}
suite.app.SequencerKeeper.SetSequencer(suite.ctx, sequencer)
// register sequncer in sequencer as Proposer
scheduler := sequencertypes.Scheduler{
SequencerAddress: bob,
Status: sequencertypes.Proposer,
}
suite.app.SequencerKeeper.SetScheduler(suite.ctx, scheduler)

// update state
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
suite.ErrorIs(err, types.ErrWrongBlockHeight)
}

func (suite *IntegrationTestSuite) TestFirstUpdateStateErrWrongBlockHeight() {
suite.SetupTest()
goCtx := sdk.WrapSDKContext(suite.ctx)
Expand Down Expand Up @@ -457,11 514,11 @@ func (suite *IntegrationTestSuite) TestFirstUpdateStateErrWrongBlockHeight() {
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 1,
StartHeight: 2,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand Down Expand Up @@ -501,19 558,19 @@ func (suite *IntegrationTestSuite) TestUpdateStateErrWrongBlockHeight() {
// set initial stateIndex & StateInfo
stateIndex := types.StateIndex{
RollappId: "rollapp1",
Index: 0,
Index: 1,
}
stateInfo := types.StateInfo{
RollappId: "rollapp1",
StateIndex: 0,
StateIndex: 1,
Sequencer: sequencer.SequencerAddress,
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 0,
CreationHeight: 0,
Status: types.STATE_STATUS_RECEIVED,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}
suite.app.RollappKeeper.SetStateIndex(suite.ctx, stateIndex)
suite.app.RollappKeeper.SetStateInfo(suite.ctx, stateInfo)
Expand Down Expand Up @@ -564,19 621,19 @@ func (suite *IntegrationTestSuite) TestUpdateStateErrLogicMissingStateInfo() {
// set initial stateIndex
stateIndex := types.StateIndex{
RollappId: "rollapp1",
Index: 0,
Index: 1,
}
suite.app.RollappKeeper.SetStateIndex(suite.ctx, stateIndex)

// update state
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand Down Expand Up @@ -616,19 673,19 @@ func (suite *IntegrationTestSuite) TestUpdateStateErrMultiUpdateStateInBlock() {
// set initial stateIndex & StateInfo
stateIndex := types.StateIndex{
RollappId: "rollapp1",
Index: 0,
Index: 1,
}
stateInfo := types.StateInfo{
RollappId: "rollapp1",
StateIndex: 0,
StateIndex: 1,
Sequencer: sequencer.SequencerAddress,
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 0,
CreationHeight: 0,
Status: types.STATE_STATUS_RECEIVED,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}
suite.app.RollappKeeper.SetStateIndex(suite.ctx, stateIndex)
suite.app.RollappKeeper.SetStateInfo(suite.ctx, stateInfo)
Expand Down Expand Up @@ -679,11 736,11 @@ func (suite *IntegrationTestSuite) TestUpdateStateErrLogicNotRegisteredInSchedul
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand Down Expand Up @@ -725,11 782,11 @@ func (suite *IntegrationTestSuite) TestUpdateStateErrNotActiveSequencer() {
updateState := types.MsgUpdateState{
Creator: bob,
RollappId: rollapp.GetRollappId(),
StartHeight: 0,
StartHeight: 1,
NumBlocks: 3,
DAPath: "",
Version: 3,
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 0}, {Height: 1}, {Height: 2}}},
BDs: types.BlockDescriptors{BD: []types.BlockDescriptor{{Height: 1}, {Height: 2}, {Height: 3}}},
}

_, err := suite.msgServer.UpdateState(goCtx, &updateState)
Expand Down
6 changes: 3 additions & 3 deletions x/rollapp/keeper/msg_server_update_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 58,14 @@ func (k msgServer) UpdateState(goCtx context.Context, msg *types.MsgUpdateState)
var newIndex uint64
if !isFound {
// check to see if it's the first update
if msg.StartHeight != 0 {
if msg.StartHeight != 1 {
// if not, it's an error
return nil, sdkerrors.Wrapf(types.ErrWrongBlockHeight,
"expected height 0, but received (%d)",
"expected height 1, but received (%d)",
msg.StartHeight)
}
// else, it's the first update
newIndex = 0
newIndex = 1
} else {
// retrieve last updating index
stateInfo, isFound := k.GetStateInfo(ctx, msg.RollappId, stateIndex.Index)
Expand Down
5 changes: 5 additions & 0 deletions x/rollapp/types/message_update_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 58,11 @@ func (msg *MsgUpdateState) ValidateBasic() error {
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 {
Expand Down
Loading

0 comments on commit 7349dc2

Please sign in to comment.