Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(staking): change historicalinfo to contain minimal set of data #17655

Merged
merged 18 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/architecture/adr-017-historical-header-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* 26 November 2019: Start of first version
* 2 December 2019: Final draft of first version
* 7 September 2023: Reduce HistoricalInfo type

## Context

Expand All @@ -16,10 +17,11 @@ The application MUST store the most recent `n` headers in a persistent store. At
The application MUST store this information by storing new headers immediately when handling `abci.RequestBeginBlock`:

```go
func BeginBlock(ctx sdk.Context, keeper HistoricalHeaderKeeper, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
func BeginBlock(ctx sdk.Context, keeper HistoricalHeaderKeeper) error {
info := HistoricalInfo{
Header: ctx.BlockHeader(),
ValSet: keeper.StakingKeeper.GetAllValidators(ctx), // note that this must be stored in a canonical order
apphash: ctx.HeaderInfo().AppHash,
Time: ctx.HeaderInfo().Time,
NextValidatorsHash: ctx.CometInfo().NextValidatorsHash,
}
keeper.SetHistoricalInfo(ctx, ctx.BlockHeight(), info)
n := keeper.GetParamRecentHeadersToStore()
Expand Down
11 changes: 9 additions & 2 deletions proto/cosmos/staking/v1beta1/staking.proto
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ message HistoricalInfo {
repeated Validator valset = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}

// Hsitorical is a set of minimum values needed for ibc.
message Historical {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
bytes apphash = 1;
google.protobuf.Timestamp update_time = 2 [(gogoproto.stdtime) = true];
bytes next_validator_hash = 3;
}

// CommissionRates defines the initial commission rates to be used for creating
// a validator.
message CommissionRates {
Expand Down Expand Up @@ -220,7 +227,7 @@ message UnbondingDelegation {
string validator_address = 2 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
// entries are the unbonding delegation entries.
repeated UnbondingDelegationEntry entries = 3
[(gogoproto.nullable) = false, (amino.dont_omitempty) = true]; // unbonding delegation entries
[(gogoproto.nullable) = false, (amino.dont_omitempty) = true]; // unbonding delegation entries
}

// UnbondingDelegationEntry defines an unbonding object with relevant metadata.
Expand Down Expand Up @@ -293,7 +300,7 @@ message Redelegation {
string validator_dst_address = 3 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
// entries are the redelegation entries.
repeated RedelegationEntry entries = 4
[(gogoproto.nullable) = false, (amino.dont_omitempty) = true]; // redelegation entries
[(gogoproto.nullable) = false, (amino.dont_omitempty) = true]; // redelegation entries
}

// Params defines the parameters for the x/staking module.
Expand Down
10 changes: 5 additions & 5 deletions x/feegrant/basic_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"testing"
"time"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"cosmossdk.io/core/header"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/feegrant"

Expand All @@ -19,23 +19,23 @@ func TestBasicFeeValidAllow(t *testing.T) {
key := storetypes.NewKVStoreKey(feegrant.StoreKey)
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test"))

ctx := testCtx.Ctx.WithBlockHeader(cmtproto.Header{Height: 1})
ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1})

badTime := ctx.BlockTime().AddDate(0, 0, -1)
badTime := ctx.HeaderInfo().Time.AddDate(0, 0, -1)
allowace := &feegrant.BasicAllowance{
Expiration: &badTime,
}
require.Error(t, allowace.ValidateBasic())

ctx = ctx.WithBlockHeader(cmtproto.Header{
ctx = ctx.WithHeaderInfo(header.Info{
Time: time.Now(),
})
eth := sdk.NewCoins(sdk.NewInt64Coin("eth", 10))
atom := sdk.NewCoins(sdk.NewInt64Coin("atom", 555))
smallAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 43))
bigAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 1000))
leftAtom := sdk.NewCoins(sdk.NewInt64Coin("atom", 512))
now := ctx.BlockTime()
now := ctx.HeaderInfo().Time
oneHour := now.Add(1 * time.Hour)

cases := map[string]struct {
Expand Down
11 changes: 10 additions & 1 deletion x/staking/keeper/historical_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"context"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -48,7 +50,14 @@ func (k Keeper) TrackHistoricalInfo(ctx context.Context) error {
return err
}

historicalEntry := types.NewHistoricalInfo(sdkCtx.BlockHeader(), types.Validators{Validators: lastVals, ValidatorCodec: k.validatorAddressCodec}, k.PowerReduction(ctx))
h := cmtproto.Header{
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
Height: sdkCtx.BlockHeight(),
Time: sdkCtx.HeaderInfo().Time,
ChainID: sdkCtx.HeaderInfo().ChainID,
NextValidatorsHash: sdkCtx.CometInfo().GetValidatorsHash(),
}

historicalEntry := types.NewHistoricalInfo(h, types.Validators{Validators: lastVals, ValidatorCodec: k.validatorAddressCodec}, k.PowerReduction(ctx))

// Set latest HistoricalInfo at current height
return k.HistoricalInfo.Set(ctx, uint64(sdkCtx.BlockHeight()), historicalEntry)
Expand Down
11 changes: 10 additions & 1 deletion x/staking/keeper/historical_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/collections"
coreheader "cosmossdk.io/core/header"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/x/staking/testutil"
Expand Down Expand Up @@ -108,7 +109,15 @@ func (s *KeeperTestSuite) TestTrackHistoricalInfo() {
ChainID: "HelloChain",
Height: 10,
}
ctx = ctx.WithBlockHeader(header)
ctx = ctx.WithHeaderInfo(coreheader.Info{
ChainID: "HelloChain",
Height: 10,
})

// ctx = ctx.WithCometInfo(comet.BlockInfo{
// ChainID: "HelloChain",
// Height: 10,
// })

require.NoError(keeper.TrackHistoricalInfo(ctx))

Expand Down