Skip to content

Commit

Permalink
no operation for finalized l1 sequence (#90)
Browse files Browse the repository at this point in the history
* no operation for finalized l1 sequence

* remove unused error

* change variable name to NextL1Sequencd

* updatkey g

---------

Co-authored-by: beer-1 <[email protected]>
  • Loading branch information
sh-cha and beer-1 authored Jul 7, 2024
1 parent 4caa252 commit 69fee5f
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 431 deletions.
292 changes: 80 additions & 212 deletions api/opinit/opchild/v1/genesis.pulsar.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions proto/opinit/opchild/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ message GenesisState {
// delegations defines the validator set at genesis.
repeated Validator validators = 3 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];

bool exported = 5;
uint64 next_l2_sequence = 6;
repeated uint64 finalized_l1_sequences = 7;
BridgeInfo bridge_info = 8;
bool exported = 5;
uint64 next_l2_sequence = 6;
uint64 next_l1_sequence = 7;
BridgeInfo bridge_info = 8;
}

// LastValidatorPower required for validator set update logic.
Expand Down
26 changes: 10 additions & 16 deletions x/opchild/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res
}
}

for _, finalizedL1Sequence := range data.FinalizedL1Sequences {
if err := k.RecordFinalizedL1Sequence(ctx, finalizedL1Sequence); err != nil {
panic(err)
}
if err := k.SetNextL1Sequence(ctx, data.NextL1Sequence); err != nil {
panic(err)
}

if err := k.SetNextL2Sequence(ctx, data.NextL2Sequence); err != nil {
Expand Down Expand Up @@ -107,11 +105,7 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
panic(err)
}

var finalizedL1Sequences []uint64
err = k.IterateFinalizedL1Sequences(ctx, func(l1Sequence uint64) (bool, error) {
finalizedL1Sequences = append(finalizedL1Sequences, l1Sequence)
return false, nil
})
finalizedL1Sequence, err := k.GetNextL1Sequence(ctx)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -144,12 +138,12 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
}

return &types.GenesisState{
Params: params,
LastValidatorPowers: lastValidatorPowers,
Validators: validators,
Exported: true,
FinalizedL1Sequences: finalizedL1Sequences,
NextL2Sequence: nextL2Sequence,
BridgeInfo: bridgeInfo,
Params: params,
LastValidatorPowers: lastValidatorPowers,
Validators: validators,
Exported: true,
NextL1Sequence: finalizedL1Sequence,
NextL2Sequence: nextL2Sequence,
BridgeInfo: bridgeInfo,
}
}
4 changes: 2 additions & 2 deletions x/opchild/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func Test_GenesisImportExport(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(2), seq)

input.OPChildKeeper.RecordFinalizedL1Sequence(ctx, 1)
input.OPChildKeeper.RecordFinalizedL1Sequence(ctx, 2)
input.OPChildKeeper.IncreaseNextL1Sequence(ctx) // 2
input.OPChildKeeper.IncreaseNextL1Sequence(ctx) // 3

genState := input.OPChildKeeper.ExportGenesis(ctx)
require.Nil(t, genState.BridgeInfo)
Expand Down
4 changes: 2 additions & 2 deletions x/opchild/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ type Keeper struct {
consensusAddressCodec address.Codec

Schema collections.Schema
NextL1Sequence collections.Sequence
NextL2Sequence collections.Sequence
Params collections.Item[types.Params]
BridgeInfo collections.Item[types.BridgeInfo]
FinalizedL1Sequence collections.Map[uint64, bool]
LastValidatorPowers collections.Map[[]byte, int64]
Validators collections.Map[[]byte, types.Validator]
ValidatorsByConsAddr collections.Map[[]byte, []byte]
Expand Down Expand Up @@ -100,10 +100,10 @@ func NewKeeper(
addressCodec: addressCodec,
validatorAddressCodec: validatorAddressCodec,
consensusAddressCodec: consensusAddressCodec,
NextL1Sequence: collections.NewSequence(sb, types.NextL1SequenceKey, "finalized_l1_sequence"),
NextL2Sequence: collections.NewSequence(sb, types.NextL2SequenceKey, "next_l2_sequence"),
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
BridgeInfo: collections.NewItem(sb, types.BridgeInfoKey, "bridge_info", codec.CollValue[types.BridgeInfo](cdc)),
FinalizedL1Sequence: collections.NewMap(sb, types.FinalizedL1SequencePrefix, "finalized_l1_sequence", collections.Uint64Key, collections.BoolValue),
LastValidatorPowers: collections.NewMap(sb, types.LastValidatorPowerPrefix, "last_validator_powers", collections.BytesKey, collections.Int64Value),
Validators: collections.NewMap(sb, types.ValidatorsPrefix, "validators", collections.BytesKey, codec.CollValue[types.Validator](cdc)),
ValidatorsByConsAddr: collections.NewMap(sb, types.ValidatorsByConsAddrPrefix, "validators_by_cons_addr", collections.BytesKey, collections.BytesValue),
Expand Down
15 changes: 10 additions & 5 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,16 @@ func (ms MsgServer) FinalizeTokenDeposit(ctx context.Context, req *types.MsgFina
return nil, err
}

// check already finalized
if ok, err := ms.HasFinalizedL1Sequence(ctx, req.Sequence); err != nil {
finalizedL1Sequence, err := ms.GetNextL1Sequence(ctx)
if err != nil {
return nil, err
} else if ok {
return nil, types.ErrDepositAlreadyFinalized
}

if req.Sequence < finalizedL1Sequence {
// No op instead of returning an error
return &types.MsgFinalizeTokenDepositResponse{}, nil
} else if req.Sequence > finalizedL1Sequence {
return nil, types.ErrInvalidSequence
}

fromAddr, err := ms.authKeeper.AddressCodec().StringToBytes(req.From)
Expand Down Expand Up @@ -408,7 +413,7 @@ func (ms MsgServer) FinalizeTokenDeposit(ctx context.Context, req *types.MsgFina
}
}

if err := ms.RecordFinalizedL1Sequence(ctx, req.Sequence); err != nil {
if _, err := ms.IncreaseNextL1Sequence(ctx); err != nil {
return nil, err
}

Expand Down
26 changes: 6 additions & 20 deletions x/opchild/keeper/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,17 @@ import (
"github.com/stretchr/testify/require"
)

func Test_FinalizedL1Sequence(t *testing.T) {
func Test_NextL1GetNextL1Sequence(t *testing.T) {
ctx, input := createDefaultTestInput(t)

res, err := input.OPChildKeeper.HasFinalizedL1Sequence(ctx, 1)
res, err := input.OPChildKeeper.GetNextL1Sequence(ctx)
require.NoError(t, err)
require.False(t, res)
require.Equal(t, uint64(1), res)

input.OPChildKeeper.RecordFinalizedL1Sequence(ctx, 1)
res, err = input.OPChildKeeper.HasFinalizedL1Sequence(ctx, 1)
input.OPChildKeeper.IncreaseNextL1Sequence(ctx)
res, err = input.OPChildKeeper.GetNextL1Sequence(ctx)
require.NoError(t, err)
require.True(t, res)
}

func Test_IterateFinalizedL1Sequences(t *testing.T) {
ctx, input := createDefaultTestInput(t)

sequences := []uint64{1, 2, 4}
for _, v := range sequences {
input.OPChildKeeper.RecordFinalizedL1Sequence(ctx, v)
}
require.NoError(t, input.OPChildKeeper.IterateFinalizedL1Sequences(ctx, func(l1Sequence uint64) (bool, error) {
require.Equal(t, sequences[0], l1Sequence)
sequences = sequences[1:]
return false, nil
}))
require.Equal(t, uint64(2), res)
}

func Test_SetAndSetNextL2Sequence(t *testing.T) {
Expand Down
37 changes: 29 additions & 8 deletions x/opchild/keeper/sequences.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,41 @@ import (

"cosmossdk.io/collections"
"github.com/initia-labs/OPinit/x/opchild/types"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
)

func (k Keeper) RecordFinalizedL1Sequence(ctx context.Context, l1Sequence uint64) error {
return k.FinalizedL1Sequence.Set(ctx, l1Sequence, true)
func (k Keeper) GetNextL1Sequence(ctx context.Context) (uint64, error) {
finalizedL1Sequence, err := k.NextL1Sequence.Peek(ctx)
if err != nil {
return 0, err
}

if finalizedL1Sequence == collections.DefaultSequenceStart {
return ophosttypes.DefaultL1SequenceStart, nil
}

return finalizedL1Sequence, nil
}

func (k Keeper) HasFinalizedL1Sequence(ctx context.Context, l1Sequence uint64) (bool, error) {
return k.FinalizedL1Sequence.Has(ctx, l1Sequence)
func (k Keeper) SetNextL1Sequence(ctx context.Context, l1Sequence uint64) error {
return k.NextL1Sequence.Set(ctx, l1Sequence)
}

func (k Keeper) IterateFinalizedL1Sequences(ctx context.Context, cb func(l1Sequence uint64) (stop bool, err error)) error {
return k.FinalizedL1Sequence.Walk(ctx, nil, func(l1sequence uint64, _ bool) (stop bool, err error) {
return cb(l1sequence)
})
func (k Keeper) IncreaseNextL1Sequence(ctx context.Context) (uint64, error) {
finalizedL1Sequence, err := k.NextL1Sequence.Next(ctx)
if err != nil {
return 0, err
}

if finalizedL1Sequence == collections.DefaultSequenceStart {
if err := k.NextL1Sequence.Set(ctx, ophosttypes.DefaultL1SequenceStart+1); err != nil {
return 0, err
}

return ophosttypes.DefaultL1SequenceStart, nil
}

return finalizedL1Sequence, nil
}

func (k Keeper) SetNextL2Sequence(ctx context.Context, l2Sequence uint64) error {
Expand Down
29 changes: 14 additions & 15 deletions x/opchild/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ var (
ErrInvalidHistoricalInfo = errorsmod.Register(ModuleName, 6, "invalid historical info")
ErrEmptyValidatorPubKey = errorsmod.Register(ModuleName, 7, "empty validator public key")
ErrInvalidSigner = errorsmod.Register(ModuleName, 8, "expected `opchild` module account as only signer for system message")
ErrDepositAlreadyFinalized = errorsmod.Register(ModuleName, 9, "deposit already finalized")
ErrInvalidAmount = errorsmod.Register(ModuleName, 10, "invalid amount")
ErrInvalidSequence = errorsmod.Register(ModuleName, 11, "invalid sequence")
ErrInvalidBlockHeight = errorsmod.Register(ModuleName, 12, "invalid block height")
ErrZeroMaxValidators = errorsmod.Register(ModuleName, 13, "max validators must be non-zero")
ErrInvalidExecuteMsg = errorsmod.Register(ModuleName, 14, "invalid execute message")
ErrUnroutableExecuteMsg = errorsmod.Register(ModuleName, 15, "unroutable execute message")
ErrInvalidExecutorChangePlan = errorsmod.Register(ModuleName, 16, "invalid executor chane plan")
ErrAlreadyRegisteredHeight = errorsmod.Register(ModuleName, 17, "executor change plan already exists at the height")
ErrInvalidBridgeInfo = errorsmod.Register(ModuleName, 18, "invalid bridge info")
ErrInvalidHeight = errorsmod.Register(ModuleName, 19, "invalid oracle height")
ErrInvalidPrices = errorsmod.Register(ModuleName, 20, "invalid oracle prices")
ErrMaxValidatorsExceeded = errorsmod.Register(ModuleName, 21, "max validators exceeded")
ErrMaxValidatorsLowerThanCurrent = errorsmod.Register(ModuleName, 22, "max validators cannot be lower than current number of validators")
ErrNonL1Token = errorsmod.Register(ModuleName, 23, "token is not from L1")
ErrInvalidAmount = errorsmod.Register(ModuleName, 9, "invalid amount")
ErrInvalidSequence = errorsmod.Register(ModuleName, 10, "invalid sequence")
ErrInvalidBlockHeight = errorsmod.Register(ModuleName, 11, "invalid block height")
ErrZeroMaxValidators = errorsmod.Register(ModuleName, 12, "max validators must be non-zero")
ErrInvalidExecuteMsg = errorsmod.Register(ModuleName, 13, "invalid execute message")
ErrUnroutableExecuteMsg = errorsmod.Register(ModuleName, 14, "unroutable execute message")
ErrInvalidExecutorChangePlan = errorsmod.Register(ModuleName, 15, "invalid executor chane plan")
ErrAlreadyRegisteredHeight = errorsmod.Register(ModuleName, 16, "executor change plan already exists at the height")
ErrInvalidBridgeInfo = errorsmod.Register(ModuleName, 17, "invalid bridge info")
ErrInvalidHeight = errorsmod.Register(ModuleName, 18, "invalid oracle height")
ErrInvalidPrices = errorsmod.Register(ModuleName, 19, "invalid oracle prices")
ErrMaxValidatorsExceeded = errorsmod.Register(ModuleName, 20, "max validators exceeded")
ErrMaxValidatorsLowerThanCurrent = errorsmod.Register(ModuleName, 21, "max validators cannot be lower than current number of validators")
ErrNonL1Token = errorsmod.Register(ModuleName, 22, "token is not from L1")
)
19 changes: 11 additions & 8 deletions x/opchild/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
)

const DefaultL2SequenceStart = 1
const (
DefaultL1SequenceStart = 1
DefaultL2SequenceStart = 1
)

// NewGenesisState creates a new GenesisState instance
func NewGenesisState(params Params, validators []Validator, bridgeInfo *BridgeInfo) *GenesisState {
Expand All @@ -25,13 +28,13 @@ func NewGenesisState(params Params, validators []Validator, bridgeInfo *BridgeIn
// DefaultGenesisState gets the raw genesis raw message for testing
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
LastValidatorPowers: []LastValidatorPower{},
Validators: []Validator{},
Exported: false,
NextL2Sequence: DefaultL2SequenceStart,
FinalizedL1Sequences: []uint64{},
BridgeInfo: nil,
Params: DefaultParams(),
LastValidatorPowers: []LastValidatorPower{},
Validators: []Validator{},
Exported: false,
NextL1Sequence: DefaultL1SequenceStart,
NextL2Sequence: DefaultL2SequenceStart,
BridgeInfo: nil,
}
}

Expand Down
Loading

0 comments on commit 69fee5f

Please sign in to comment.