Skip to content

Commit

Permalink
pr changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarom Swisa authored and Yarom Swisa committed Dec 21, 2022
1 parent b91e8fb commit ed9ee20
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/upgrades/v0.2.0/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func CreateUpgradeHandler(
log.Println("# STARTING UPGRADE #")
log.Println("########################")

//delete all existing conflicts
// delete all existing conflicts
migrator := v020.NewMigrator(keepers.SpecKeeper)
migrator.MigrateToV020(ctx)

Expand Down
18 changes: 4 additions & 14 deletions testutil/keeper/keepers_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
tmdb "github.com/tendermint/tm-db"
)

const BLOCK_TIME = 30 * time.Second

type Keepers struct {
Epochstorage epochstoragekeeper.Keeper
Spec speckeeper.Keeper
Expand Down Expand Up @@ -115,7 +117,7 @@ func InitAllKeepers(t testing.TB) (*Servers, *Keepers, context.Context) {
ks.Pairing = *pairingkeeper.NewKeeper(cdc, pairingStoreKey, pairingMemStoreKey, pairingparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Spec, &ks.Epochstorage)
ks.ParamsKeeper = paramsKeeper
ks.Conflict = *conflictkeeper.NewKeeper(cdc, conflictStoreKey, conflictMemStoreKey, conflictparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, ks.Pairing, ks.Epochstorage, ks.Spec)
ks.BlockStore = MockBlockStore{height: 0, blockHistory: make(map[int64]tenderminttypes.Block)}
ks.BlockStore = MockBlockStore{height: 0, blockHistory: make(map[int64]*tenderminttypes.Block)}

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.TestingLogger())

Expand Down Expand Up @@ -191,7 +193,6 @@ func NewBlock(ctx context.Context, ks *Keepers) {
block := uint64(unwrapedCtx.BlockHeight())

if ks.Epochstorage.IsEpochStart(sdk.UnwrapSDKContext(ctx)) {

ks.Epochstorage.FixateParams(unwrapedCtx, block)
// begin block
ks.Epochstorage.SetEpochDetailsStart(unwrapedCtx, block)
Expand All @@ -208,16 +209,5 @@ func NewBlock(ctx context.Context, ks *Keepers) {

ks.Conflict.CheckAndHandleAllVotes(unwrapedCtx)

// keep block height in mock blockstore
blockInt64 := int64(block)
ks.BlockStore.SetHeight(blockInt64)

// create mock block header
blockHeader := tenderminttypes.Header{}
blockHeader.Height = blockInt64
blockHeader.Time = time.Now()

// update the blockstore's block history with current block
blockCore := tenderminttypes.Block{Header: blockHeader}
ks.BlockStore.SetBlockHistoryEntry(blockInt64, &blockCore)
ks.BlockStore.AdvanceBlock(BLOCK_TIME)
}
25 changes: 22 additions & 3 deletions testutil/keeper/mock_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -112,15 +113,33 @@ func (k *mockBankKeeper) SubFromBalance(addr sdk.AccAddress, amounts sdk.Coins)

type MockBlockStore struct {
height int64
blockHistory map[int64]tenderminttypes.Block
blockHistory map[int64]*tenderminttypes.Block
}

func (b *MockBlockStore) SetHeight(height int64) {
b.height = height
}

func (b *MockBlockStore) AdvanceBlock(blockTimeLen time.Duration) {
// keep block height in mock blockstore
blockInt64 := b.height + 1
b.SetHeight(blockInt64)

// create mock block header
blockHeader := tenderminttypes.Header{}
blockHeader.Height = blockInt64
if prevBlock, ok := b.blockHistory[b.height-1]; ok {
blockHeader.Time = prevBlock.Time.Add(blockTimeLen)
} else {
blockHeader.Time = time.Now()
}

// update the blockstore's block history with current block
b.SetBlockHistoryEntry(blockInt64, &tenderminttypes.Block{Header: blockHeader})
}

func (b *MockBlockStore) SetBlockHistoryEntry(height int64, blockCore *tenderminttypes.Block) {
b.blockHistory[height] = *blockCore
b.blockHistory[height] = blockCore
}

func (b *MockBlockStore) Base() int64 {
Expand All @@ -145,7 +164,7 @@ func (b *MockBlockStore) LoadBlockMeta(height int64) *tenderminttypes.BlockMeta

func (b *MockBlockStore) LoadBlock(height int64) *tenderminttypes.Block {
block := b.blockHistory[height]
return &block
return block
}

func (b *MockBlockStore) SaveBlock(block *tenderminttypes.Block, blockParts *tenderminttypes.PartSet, seenCommit *tenderminttypes.Commit) {
Expand Down
3 changes: 1 addition & 2 deletions x/pairing/keeper/grpc_query_get_pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (

// Gets a client's provider list in a specific chain. Also returns the start block of the current epoch, time (in seconds) until there's a new pairing, the block that the chain in the request's spec was changed
func (k Keeper) GetPairing(goCtx context.Context, req *types.QueryGetPairingRequest) (*types.QueryGetPairingResponse, error) {

if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
//TODO: validate chainID
// TODO: validate chainID
// check client address
clientAddr, err := sdk.AccAddressFromBech32(req.Client)
if err != nil {
Expand Down
17 changes: 4 additions & 13 deletions x/pairing/keeper/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ const (

// Function to calculate how much time (in seconds) is left until the next epoch
func (k Keeper) calculateNextEpochTime(ctx sdk.Context) (uint64, error) {

// Get current epoch
currentEpoch := k.epochStorageKeeper.GetEpochStart(ctx)

Expand Down Expand Up @@ -258,7 +257,6 @@ func (k Keeper) calculateNextEpochTime(ctx sdk.Context) (uint64, error) {

// Function to calculate the average block time (i.e., how much time it takes to create a new block, in average)
func (k Keeper) calculateAverageBlockTime(ctx sdk.Context, epoch uint64) (float64, error) {

// Get epochBlocks (the number of blocks in an epoch)
epochBlocks, err := k.epochStorageKeeper.EpochBlocks(ctx, epoch)
if err != nil {
Expand Down Expand Up @@ -299,18 +297,15 @@ type blockHeightAndTime struct {

// Function to get a list of the timestamps of the blocks in the previous epoch of the input (so it'll be deterministic)
func (k Keeper) getPreviousEpochTimestampsByHeight(ctx sdk.Context, epoch uint64, sampleStep uint64) ([]blockHeightAndTime, error) {

// Check if a previous epoch exists (on the first epoch or after a chain fork, there is no previous epoch. In this case, return an error since this function only calculates the average block time on epochs that fully passed).
prevEpoch, err := k.epochStorageKeeper.GetPreviousEpochStartForBlock(ctx, epoch)
if err != nil {
return nil, fmt.Errorf("can't get previous epoch for current epoch. epoch: %d", epoch)
}

// Get previous epoch timestamps, in sampleStep steps
prevEpochTimestampAndHeightList := make([]blockHeightAndTime, EpochBlocksDivider)
loopCounter := 0
for block := prevEpoch; block < epoch; block = block + sampleStep {

prevEpochTimestampAndHeightList := []blockHeightAndTime{}
for block := prevEpoch; block < epoch; block += sampleStep {
// Get current block's height and timestamp
blockInt64 := int64(block)
blockCore, err := core.Block(nil, &blockInt64)
Expand All @@ -322,29 +317,25 @@ func (k Keeper) getPreviousEpochTimestampsByHeight(ctx sdk.Context, epoch uint64
blockHeightAndTimeStruct := blockHeightAndTime{blockHeight: currentBlockHeight, blockTime: currentBlockTimestamp}

// Append the timestamp to the timestamp list
prevEpochTimestampAndHeightList[loopCounter] = blockHeightAndTimeStruct
loopCounter++
prevEpochTimestampAndHeightList = append(prevEpochTimestampAndHeightList, blockHeightAndTimeStruct)
}

return prevEpochTimestampAndHeightList, nil

}

func calculateAverageBlockTimeFromList(ctx sdk.Context, blockHeightAndTimeList []blockHeightAndTime, sampleStep uint64) (float64, error) {

if blockHeightAndTimeList == nil {
return 0, fmt.Errorf("empty blockHeightAndTimeList")
}

averageBlockTime := math.MaxFloat64
for i := 1; i < len(blockHeightAndTimeList); i++ {

// Calculate time difference (the average block time will be the minimum difference)
currentAverageBlockTime := blockHeightAndTimeList[i].blockTime.Sub(blockHeightAndTimeList[i-1].blockTime).Seconds() / float64(sampleStep)
if currentAverageBlockTime <= 0 {
return 0, fmt.Errorf("calculated average block time is less than or equal to zero. block %v timestamp: %s, block %v timestamp: %s", blockHeightAndTimeList[i].blockHeight, blockHeightAndTimeList[i].blockTime.String(), blockHeightAndTimeList[i-1].blockHeight, blockHeightAndTimeList[i-1].blockTime.String())
}
if averageBlockTime > currentAverageBlockTime || averageBlockTime == 0 {
if averageBlockTime > currentAverageBlockTime {
averageBlockTime = currentAverageBlockTime
}
}
Expand Down

0 comments on commit ed9ee20

Please sign in to comment.