Skip to content

Commit

Permalink
use sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Dec 27, 2023
1 parent 633225c commit 6740833
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
4 changes: 2 additions & 2 deletions x/opchild/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Keeper struct {
consensusAddressCodec address.Codec

Schema collections.Schema
NextL2Sequence collections.Item[uint64]
NextL2Sequence collections.Sequence
Params collections.Item[types.Params]
FinalizedL1Sequence collections.Map[uint64, bool]
LastValidatorPowers collections.Map[[]byte, int64]
Expand Down Expand Up @@ -78,7 +78,7 @@ func NewKeeper(
authority: authority,
validatorAddressCodec: validatorAddressCodec,
consensusAddressCodec: consensusAddressCodec,
NextL2Sequence: collections.NewItem(sb, types.NextL2SequenceKey, "next_l2_sequence", collections.Uint64Value),
NextL2Sequence: collections.NewSequence(sb, types.NextL2SequenceKey, "next_l2_sequence"),
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](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),
Expand Down
23 changes: 13 additions & 10 deletions x/opchild/keeper/sequences.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package keeper

import (
"context"
"errors"

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

func (k Keeper) RecordFinalizedL1Sequence(ctx context.Context, l1Sequence uint64) error {
Expand All @@ -26,27 +26,30 @@ func (k Keeper) SetNextL2Sequence(ctx context.Context, l2Sequence uint64) error
}

func (k Keeper) GetNextL2Sequence(ctx context.Context) (uint64, error) {
nextL2Sequence, err := k.NextL2Sequence.Get(ctx)
nextL2Sequence, err := k.NextL2Sequence.Peek(ctx)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return 1, nil
}

return 0, err
}

Check warning on line 32 in x/opchild/keeper/sequences.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/sequences.go#L31-L32

Added lines #L31 - L32 were not covered by tests

if nextL2Sequence == collections.DefaultSequenceStart {
return types.DefaultL2SequenceStart, nil
}

return nextL2Sequence, nil
}

func (k Keeper) IncreaseNextL2Sequence(ctx context.Context) (uint64, error) {
nextL2Sequence, err := k.GetNextL2Sequence(ctx)
nextL2Sequence, err := k.NextL2Sequence.Next(ctx)
if err != nil {
return 0, err

Check warning on line 44 in x/opchild/keeper/sequences.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/sequences.go#L44

Added line #L44 was not covered by tests
}

// increase NextL2Sequence
if err = k.NextL2Sequence.Set(ctx, nextL2Sequence+1); err != nil {
return 0, err
if nextL2Sequence == collections.DefaultSequenceStart {
if err := k.NextL2Sequence.Set(ctx, types.DefaultL2SequenceStart+1); err != nil {
return 0, err
}

Check warning on line 50 in x/opchild/keeper/sequences.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/sequences.go#L49-L50

Added lines #L49 - L50 were not covered by tests

return types.DefaultL2SequenceStart, nil
}

return nextL2Sequence, nil
Expand Down
8 changes: 7 additions & 1 deletion x/opchild/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
)

const DefaultL2SequenceStart = 1

// NewGenesisState creates a new GenesisState instance
func NewGenesisState(params Params, validators []Validator) *GenesisState {
return &GenesisState{
Expand All @@ -26,7 +28,7 @@ func DefaultGenesisState() *GenesisState {
LastValidatorPowers: []LastValidatorPower{},
Validators: []Validator{},
Exported: false,
NextL2Sequence: 1,
NextL2Sequence: DefaultL2SequenceStart,
FinalizedL1Sequences: []uint64{},
}
}
Expand All @@ -38,6 +40,10 @@ func ValidateGenesis(data *GenesisState, ac address.Codec) error {
return err
}

if data.NextL2Sequence < DefaultL2SequenceStart {
return ErrInvalidSequence
}

return data.Params.Validate(ac)
}

Expand Down
25 changes: 14 additions & 11 deletions x/ophost/keeper/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (k Keeper) GetNextL1Sequence(ctx context.Context, bridgeId uint64) (uint64,
nextSequence, err := k.NextL1Sequences.Get(ctx, bridgeId)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
nextSequence = 1
nextSequence = types.DefaultL1SequenceStart
} else {
return 0, err
}

Check warning on line 51 in x/ophost/keeper/bridge.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/bridge.go#L50-L51

Added lines #L50 - L51 were not covered by tests
Expand Down Expand Up @@ -76,27 +76,30 @@ func (k Keeper) SetNextBridgeId(ctx context.Context, nextBridgeId uint64) error
}

func (k Keeper) GetNextBridgeId(ctx context.Context) (uint64, error) {
nextBridgeId, err := k.NextBridgeId.Get(ctx)
nextBridgeId, err := k.NextBridgeId.Peek(ctx)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
nextBridgeId = 1
} else {
return 0, err
}
return 0, err
}

Check warning on line 82 in x/ophost/keeper/bridge.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/bridge.go#L81-L82

Added lines #L81 - L82 were not covered by tests

if nextBridgeId == collections.DefaultSequenceStart {
return types.DefaultBridgeIdStart, nil

Check warning on line 85 in x/ophost/keeper/bridge.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/bridge.go#L85

Added line #L85 was not covered by tests
}

return nextBridgeId, nil
}

func (k Keeper) IncreaseNextBridgeId(ctx context.Context) (uint64, error) {
nextBridgeId, err := k.GetNextBridgeId(ctx)
nextBridgeId, err := k.NextBridgeId.Next(ctx)
if err != nil {
return 0, err
}

Check warning on line 95 in x/ophost/keeper/bridge.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/bridge.go#L94-L95

Added lines #L94 - L95 were not covered by tests

// increase NextBridgeId
if err := k.NextBridgeId.Set(ctx, nextBridgeId+1); err != nil {
return 0, err
if nextBridgeId == collections.DefaultSequenceStart {
if err := k.NextBridgeId.Set(ctx, types.DefaultBridgeIdStart+1); err != nil {
return 0, err
}

Check warning on line 100 in x/ophost/keeper/bridge.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/bridge.go#L99-L100

Added lines #L99 - L100 were not covered by tests

return types.DefaultBridgeIdStart, nil
}

return nextBridgeId, nil

Check warning on line 105 in x/ophost/keeper/bridge.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/keeper/bridge.go#L105

Added line #L105 was not covered by tests
Expand Down
4 changes: 2 additions & 2 deletions x/ophost/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Keeper struct {
authority string

Schema collections.Schema
NextBridgeId collections.Item[uint64]
NextBridgeId collections.Sequence
Params collections.Item[types.Params]
BridgeConfigs collections.Map[uint64, types.BridgeConfig]
NextL1Sequences collections.Map[uint64, uint64]
Expand Down Expand Up @@ -58,7 +58,7 @@ func NewKeeper(
bankKeeper: bk,
bridgeHook: bridgeHook,
authority: authority,
NextBridgeId: collections.NewItem(sb, types.NextBridgeIdKey, "next_bridge_id", collections.Uint64Value),
NextBridgeId: collections.NewSequence(sb, types.NextBridgeIdKey, "next_bridge_id"),
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
BridgeConfigs: collections.NewMap(sb, types.BridgeConfigPrefix, "bridge_configs", collections.Uint64Key, codec.CollValue[types.BridgeConfig](cdc)),
NextL1Sequences: collections.NewMap(sb, types.NextL1SequencePrefix, "next_l1_sequences", collections.Uint64Key, collections.Uint64Value),
Expand Down
11 changes: 8 additions & 3 deletions x/ophost/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
DefaultBridgeIdStart = 1
DefaultL1SequenceStart = 1
)

// NewGenesisState creates a new GenesisState instance
func NewGenesisState(params Params, bridges []Bridge, nextBridgeId uint64) *GenesisState {
return &GenesisState{
Expand All @@ -19,7 +24,7 @@ func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
Bridges: []Bridge{},
NextBridgeId: 1,
NextBridgeId: DefaultBridgeIdStart,
}
}

Expand All @@ -35,7 +40,7 @@ func ValidateGenesis(data *GenesisState, ac address.Codec) error {
return ErrInvalidBridgeId
}

if bridge.NextL1Sequence == 0 {
if bridge.NextL1Sequence < DefaultL1SequenceStart {
return ErrInvalidSequence
}

Expand Down Expand Up @@ -65,7 +70,7 @@ func ValidateGenesis(data *GenesisState, ac address.Codec) error {
}
}

if data.NextBridgeId == 0 {
if data.NextBridgeId < DefaultBridgeIdStart {
return ErrInvalidBridgeId
}

Expand Down

0 comments on commit 6740833

Please sign in to comment.