Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
PrathyushaLakkireddy committed Sep 20, 2024
1 parent 60329da commit bbb9818
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 345 deletions.
2 changes: 1 addition & 1 deletion keeper/status.go → keeper/blob_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func (k *Keeper) SetBlobStatusPending(ctx sdk.Context, startHeight, endHeight uint64) bool {
store := ctx.KVStore(k.storeKey)

if !CanUpdateStatusToPending(store) { // TOodo: we should check for expiration too
if !CanUpdateStatusToPending(store) { // Todo: we should check for expiration too (what if the status was pending for too long)
return false
}

Expand Down
183 changes: 36 additions & 147 deletions keeper/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@ package keeper

import (
"context"
"fmt"
"time"

"cosmossdk.io/collections"
"github.com/vitwit/avail-da-module/types"
)

const (
// Window for a transaction to be committed
ResubmissionTime = 75 * time.Second

// Buffer for relayer polling logic to retrieve a proof
RelayerPollingBuffer = 15 * time.Second
)

func (k *Keeper) SetValidatorAvailAddress(ctx context.Context, validator types.Validator) error {
return k.Validators.Set(ctx, validator.ValidatorAddress, validator.AvailAddress)
}
Expand Down Expand Up @@ -50,146 +39,46 @@ func (k *Keeper) GetAllValidators(ctx context.Context) (types.Validators, error)
return validators, nil
}

func (k *Keeper) SetProvenHeight(ctx context.Context, height uint64) error {
return k.ProvenHeight.Set(ctx, height)
}
// func (k *Keeper) SetProvenHeight(ctx context.Context, height uint64) error {
// return k.ProvenHeight.Set(ctx, height)
// }

func (k *Keeper) GetProvenHeight(ctx context.Context) (uint64, error) {
return k.ProvenHeight.Get(ctx)
}
// func (k *Keeper) GetProvenHeight(ctx context.Context) (uint64, error) {
// return k.ProvenHeight.Get(ctx)
// }

func (k *Keeper) SetClientID(ctx context.Context, clientID string) error {
return k.ClientID.Set(ctx, clientID)
}
// func (k *Keeper) SetClientID(ctx context.Context, clientID string) error {
// return k.ClientID.Set(ctx, clientID)
// }

func (k *Keeper) GetClientID(ctx context.Context) (string, error) {
return k.ClientID.Get(ctx)
}
// func (k *Keeper) GetClientID(ctx context.Context) (string, error) {
// return k.ClientID.Get(ctx)
// }

// IsBlockPending return true if a block height is already pending
func (k Keeper) IsBlockPending(ctx context.Context, blockHeight int64) bool {
found, err := k.PendingBlocksToTimeouts.Has(ctx, blockHeight)
if err != nil {
return false
}
return found
}
// func (k Keeper) IsBlockPending(ctx context.Context, blockHeight int64) bool {
// found, err := k.PendingBlocksToTimeouts.Has(ctx, blockHeight)
// if err != nil {
// return false
// }
// return found
// }

// IsBlockExpired will return true if a block is pending and expired, otherwise it returns false
func (k *Keeper) IsBlockExpired(ctx context.Context, currentBlockTime time.Time, blockHeight int64) bool {
currentBlockTimeNs := currentBlockTime.UnixNano()
found, err := k.PendingBlocksToTimeouts.Has(ctx, blockHeight)
if err != nil {
return false
}
if found {
expiration, err := k.PendingBlocksToTimeouts.Get(ctx, blockHeight)
if err != nil {
return false
}
if currentBlockTimeNs >= expiration {
return true
}
}
return false
}

// AddUpdatePendingBlock will add a new pending block or update an existing pending block
func (k *Keeper) AddUpdatePendingBlock(ctx context.Context, pendingBlock int64, currentBlockTime time.Time) error {
found, err := k.PendingBlocksToTimeouts.Has(ctx, pendingBlock)
if err != nil {
return fmt.Errorf("remove pending blocks, block %d error", pendingBlock)
}
if found {
if err = k.RemovePendingBlock(ctx, pendingBlock); err != nil {
return err
}
}
expiration := currentBlockTime.Add(ResubmissionTime + RelayerPollingBuffer).UnixNano()
if err = k.PendingBlocksToTimeouts.Set(ctx, pendingBlock, expiration); err != nil {
return fmt.Errorf("add/update pending block, set pending block (%d) to timeout (%d)", pendingBlock, expiration)
}
if err = k.AddPendingBlockToTimeoutsMap(ctx, pendingBlock, expiration); err != nil {
return fmt.Errorf("add/update pending block, add pending block to timeouts map, %v", err)
}
return nil
}

func (k *Keeper) AddPendingBlockToTimeoutsMap(ctx context.Context, height, expiration int64) error {
found, err := k.TimeoutsToPendingBlocks.Has(ctx, expiration)
if err != nil {
return err
}
var pendingBlocks types.PendingBlocks
if found {
pendingBlocks, err = k.TimeoutsToPendingBlocks.Get(ctx, expiration)
if err != nil {
return err
}
}
pendingBlocks.BlockHeights = append(pendingBlocks.BlockHeights, height)
if err = k.TimeoutsToPendingBlocks.Set(ctx, expiration, pendingBlocks); err != nil {
return err
}
return nil
}

// // RemovePendingBlock removes proven block from pending state
// This function will remove the proven block from the PendingBlocksToTimeouts map and TimeoutsToPendingBlocks map
func (k *Keeper) RemovePendingBlock(ctx context.Context, provenBlock int64) error {
found, err := k.PendingBlocksToTimeouts.Has(ctx, provenBlock)
if err != nil {
return fmt.Errorf("remove pending blocks, block %d error", provenBlock)
}
if found {
expiration, err := k.PendingBlocksToTimeouts.Get(ctx, provenBlock)
if err != nil {
return fmt.Errorf("remove pending blocks, getting pending block %d", provenBlock)
}
if err = k.PendingBlocksToTimeouts.Remove(ctx, provenBlock); err != nil {
return fmt.Errorf("remove pending blocks, removing block %d", provenBlock)
}
pendingBlocks, err := k.TimeoutsToPendingBlocks.Get(ctx, expiration)
if err != nil {
return fmt.Errorf("remove pending blocks, getting expiration %d", expiration)
}
var newPendingBlocks []int64
for _, blockHeight := range pendingBlocks.BlockHeights {
if blockHeight != provenBlock {
newPendingBlocks = append(newPendingBlocks, blockHeight)
}
}
if len(newPendingBlocks) > 0 {
pendingBlocks.BlockHeights = newPendingBlocks
if err = k.TimeoutsToPendingBlocks.Set(ctx, expiration, pendingBlocks); err != nil {
return fmt.Errorf("remove pending block, set new pending blocks")
}
} else {
if err = k.TimeoutsToPendingBlocks.Remove(ctx, expiration); err != nil {
return fmt.Errorf("remove pending blocks, removing timeout set %d", expiration)
}
}
}
return nil
}

// GetExpiredBlocks returns all expired blocks, proposer will propose publishing based on this set
func (k Keeper) GetExpiredBlocks(ctx context.Context, currentBlockTime time.Time) []int64 {
currentBlockTimeNs := currentBlockTime.UnixNano()
iterator, err := k.TimeoutsToPendingBlocks.
Iterate(ctx, (&collections.Range[int64]{}).StartInclusive(0).EndInclusive(currentBlockTimeNs))
if err != nil {
return nil
}
defer iterator.Close()

var expiredBlocks []int64
for ; iterator.Valid(); iterator.Next() {
pendingBlocks, err := iterator.Value()
if err != nil {
return nil
}
expiredBlocks = append(expiredBlocks, pendingBlocks.BlockHeights...)
}
return expiredBlocks
}
// func (k *Keeper) IsBlockExpired(ctx context.Context, currentBlockTime time.Time, blockHeight int64) bool {
// currentBlockTimeNs := currentBlockTime.UnixNano()
// found, err := k.PendingBlocksToTimeouts.Has(ctx, blockHeight)
// if err != nil {
// return false
// }
// if found {
// expiration, err := k.PendingBlocksToTimeouts.Get(ctx, blockHeight)
// if err != nil {
// return false
// }
// if currentBlockTimeNs >= expiration {
// return true
// }
// }
// return false
// }
24 changes: 6 additions & 18 deletions keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) error {
}
}

// Set proven height to genesis height, we do not init any pending block on a genesis init/restart
// if err := k.SetProvenHeight(ctx, ctx.HeaderInfo().Height); err != nil {
// return err
// }

k.relayer.NotifyProvenHeight(ctx.HeaderInfo().Height)

// TODO: client state
k.SetAvailGenesisState(ctx, data)

return nil
Expand All @@ -33,18 +25,14 @@ func (k *Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
panic(err)
}

provenHeight, err := k.GetProvenHeight(ctx)
if err != nil {
panic(err)
}

if err != nil {
panic(err)
}
// provenHeight, err := k.GetProvenHeight(ctx)
// if err != nil {
// panic(err)
// }

return &types.GenesisState{
Validators: vals.Validators,
ProvenHeight: provenHeight,
Validators: vals.Validators,
// ProvenHeight: provenHeight,
}
}

Expand Down
21 changes: 10 additions & 11 deletions keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ import (
"github.com/spf13/cobra"
availblob1 "github.com/vitwit/avail-da-module"
"github.com/vitwit/avail-da-module/relayer"
"github.com/vitwit/avail-da-module/types"
)

type Keeper struct {
// stakingKeeper *stakingkeeper.Keeper
upgradeKeeper *upgradekeeper.Keeper
relayer *relayer.Relayer

Validators collections.Map[string, string]
ClientID collections.Item[string]
ProvenHeight collections.Item[uint64]
PendingBlocksToTimeouts collections.Map[int64, int64]
TimeoutsToPendingBlocks collections.Map[int64, types.PendingBlocks]
Validators collections.Map[string, string]
// ClientID collections.Item[string]
// ProvenHeight collections.Item[uint64]
// PendingBlocksToTimeouts collections.Map[int64, int64]
// TimeoutsToPendingBlocks collections.Map[int64, types.PendingBlocks]
// keyring keyring.Keyring

storeKey storetypes2.StoreKey
Expand All @@ -51,11 +50,11 @@ func NewKeeper(
return &Keeper{
upgradeKeeper: uk,

Validators: collections.NewMap(sb, availblob1.ValidatorsKey, "validators", collections.StringKey, collections.StringValue),
ClientID: collections.NewItem(sb, availblob1.ClientIDKey, "client_id", collections.StringValue),
ProvenHeight: collections.NewItem(sb, availblob1.ProvenHeightKey, "proven_height", collections.Uint64Value),
PendingBlocksToTimeouts: collections.NewMap(sb, availblob1.PendingBlocksToTimeouts, "pending_blocks_to_timeouts", collections.Int64Key, collections.Int64Value),
TimeoutsToPendingBlocks: collections.NewMap(sb, availblob1.TimeoutsToPendingBlocks, "timeouts_to_pending_blocks", collections.Int64Key, codec.CollValue[types.PendingBlocks](cdc)),
Validators: collections.NewMap(sb, availblob1.ValidatorsKey, "validators", collections.StringKey, collections.StringValue),
// ClientID: collections.NewItem(sb, availblob1.ClientIDKey, "client_id", collections.StringValue),
// ProvenHeight: collections.NewItem(sb, availblob1.ProvenHeightKey, "proven_height", collections.Uint64Value),
// PendingBlocksToTimeouts: collections.NewMap(sb, availblob1.PendingBlocksToTimeouts, "pending_blocks_to_timeouts", collections.Int64Key, collections.Int64Value),
// TimeoutsToPendingBlocks: collections.NewMap(sb, availblob1.TimeoutsToPendingBlocks, "timeouts_to_pending_blocks", collections.Int64Key, codec.CollValue[types.PendingBlocks](cdc)),

storeKey: key,

Expand Down
1 change: 1 addition & 0 deletions keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (s msgServer) UpdateBlobStatus(ctx context.Context, req *types.MsgUpdateBlo
UpdateVotingEndHeight(sdkCtx, store, lastVotingEndHeight, true)
}

// update status of the blob range
UpdateBlobStatus(sdkCtx, store, newStatus)

return &types.MsgUpdateBlobStatusResponse{}, nil
Expand Down
6 changes: 3 additions & 3 deletions keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

func (s *TestSuite) TestMsgServer_UpdateBlobStatus() {
err := s.keeper.SetProvenHeight(s.ctx, 10)
s.Require().NoError(err)
// err := s.keeper.SetProvenHeight(s.ctx, 10)
// s.Require().NoError(err)

err = availkeeper.UpdateEndHeight(s.ctx, s.store, uint64(20))
err := availkeeper.UpdateEndHeight(s.ctx, s.store, uint64(20))
s.Require().NoError(err)

testCases := []struct {
Expand Down
Loading

0 comments on commit bbb9818

Please sign in to comment.