Skip to content

Commit

Permalink
add altvalue codec
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Sep 7, 2023
1 parent 46c1138 commit 5e06fd4
Show file tree
Hide file tree
Showing 11 changed files with 2,652 additions and 1,699 deletions.
416 changes: 208 additions & 208 deletions api/cosmos/staking/v1beta1/query.pulsar.go

Large diffs are not rendered by default.

1,606 changes: 1,124 additions & 482 deletions api/cosmos/staking/v1beta1/staking.pulsar.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion proto/cosmos/staking/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ message QueryHistoricalInfoRequest {
// method.
message QueryHistoricalInfoResponse {
// hist defines the historical info at the given height.
HistoricalInfo hist = 1;
Historical hist = 1;
}

// QueryPoolRequest is request type for the Query/Pool RPC method.
Expand Down
6 changes: 3 additions & 3 deletions proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ message HistoricalInfo {
// Hsitorical is a set of minimum values needed for ibc.
message Historical {
bytes apphash = 1;
google.protobuf.Timestamp update_time = 2 [(gogoproto.stdtime) = true];
google.protobuf.Timestamp time = 2 [(gogoproto.stdtime) = true];
bytes next_validator_hash = 3;
}

Expand Down Expand Up @@ -227,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 @@ -300,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
9 changes: 3 additions & 6 deletions tests/integration/staking/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,14 @@ func TestGRPCHistoricalInfo(t *testing.T) {
vals.Validators = append(vals.Validators, validator)
}

historicalInfo := stakingtypes.HistoricalInfo{
Header: cmtproto.Header{},
Valset: vals.Validators,
}
historical := stakingtypes.Historical{}

height := rapid.Int64Min(0).Draw(rt, "height")

assert.NilError(t, f.stakingKeeper.HistoricalInfo.Set(
f.ctx,
uint64(height),
historicalInfo,
historical,
))

req := &stakingtypes.QueryHistoricalInfoRequest{
Expand All @@ -700,7 +697,7 @@ func TestGRPCHistoricalInfo(t *testing.T) {

height := int64(127)

assert.NilError(t, f.stakingKeeper.HistoricalInfo.Set(
assert.NilError(t, f.stakingKeeper.Historical.Set(
f.ctx,
uint64(height),
historicalInfo,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createValidatorAccs(t *testing.T, f *fixture) ([]sdk.AccAddress, []types.Va
// have its order changed
sortedVals := make([]types.Validator, len(validators))
copy(sortedVals, validators)
hi := types.NewHistoricalInfo(header, types.Validators{Validators: sortedVals}, f.stakingKeeper.PowerReduction(f.sdkCtx))
hi := types.Historical{Time: &header.Time, Apphash: header.AppHash, NextValidatorHash: header.NextValidatorsHash}
assert.NilError(t, f.stakingKeeper.HistoricalInfo.Set(f.sdkCtx, uint64(5), hi))

return addrs, validators
Expand Down Expand Up @@ -732,7 +732,7 @@ func TestGRPCQueryHistoricalInfo(t *testing.T) {
qr := f.app.QueryHelper()
queryClient := types.NewQueryClient(qr)

hi, found := f.stakingKeeper.HistoricalInfo.Get(ctx, uint64(5))
hi, found := f.stakingKeeper.Historical.Get(ctx, uint64(5))
assert.Assert(t, found)

var req *types.QueryHistoricalInfoRequest
Expand Down
19 changes: 5 additions & 14 deletions x/staking/keeper/historical_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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 @@ -44,21 +42,14 @@ func (k Keeper) TrackHistoricalInfo(ctx context.Context) error {
return nil
}

// Create HistoricalInfo struct
lastVals, err := k.GetLastValidators(ctx)
if err != nil {
return err
}
time := sdkCtx.HeaderInfo().Time

h := cmtproto.Header{
AppHash: sdkCtx.HeaderInfo().AppHash,
Time: sdkCtx.HeaderInfo().Time,
ChainID: sdkCtx.HeaderInfo().ChainID,
NextValidatorsHash: sdkCtx.CometInfo().GetValidatorsHash(),
historicalEntry := types.Historical{
Time: &time,
NextValidatorHash: sdkCtx.CometInfo().GetValidatorsHash(),
Apphash: sdkCtx.HeaderInfo().AppHash,
}

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)
}
56 changes: 36 additions & 20 deletions x/staking/keeper/historical_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ func (s *KeeperTestSuite) TestHistoricalInfo() {
validators[i] = testutil.NewValidator(s.T(), valAddr, PKs[i])
}

hi := stakingtypes.NewHistoricalInfo(ctx.BlockHeader(), stakingtypes.Validators{Validators: validators}, keeper.PowerReduction(ctx))
time := ctx.BlockTime()
hi := stakingtypes.Historical{
Time: &time,
NextValidatorHash: ctx.CometInfo().GetValidatorsHash(),
Apphash: ctx.HeaderInfo().AppHash,
}
require.NoError(keeper.HistoricalInfo.Set(ctx, uint64(2), hi))

recv, err := keeper.HistoricalInfo.Get(ctx, uint64(2))
require.NoError(err, "HistoricalInfo found after set")
require.Equal(hi, recv, "HistoricalInfo not equal")
require.True(IsValSetSorted(recv.Valset, keeper.PowerReduction(ctx)), "HistoricalInfo validators is not sorted")

require.NoError(keeper.HistoricalInfo.Remove(ctx, uint64(2)))

Expand Down Expand Up @@ -70,12 +74,19 @@ func (s *KeeperTestSuite) TestTrackHistoricalInfo() {
ChainID: "HelloChain",
Height: 5,
}
valSet := []stakingtypes.Validator{
testutil.NewValidator(s.T(), addrVals[0], PKs[0]),
testutil.NewValidator(s.T(), addrVals[1], PKs[1]),

hi4 := stakingtypes.Historical{
Time: &h4.Time,
NextValidatorHash: h4.NextValidatorsHash,
Apphash: h4.AppHash,
}

hi5 := stakingtypes.Historical{
Time: &h5.Time,
NextValidatorHash: h5.NextValidatorsHash,
Apphash: h5.AppHash,
}
hi4 := stakingtypes.NewHistoricalInfo(h4, stakingtypes.Validators{Validators: valSet}, keeper.PowerReduction(ctx))
hi5 := stakingtypes.NewHistoricalInfo(h5, stakingtypes.Validators{Validators: valSet}, keeper.PowerReduction(ctx))

require.NoError(keeper.HistoricalInfo.Set(ctx, uint64(4), hi4))
require.NoError(keeper.HistoricalInfo.Set(ctx, uint64(5), hi5))
recv, err := keeper.HistoricalInfo.Get(ctx, uint64(4))
Expand Down Expand Up @@ -143,29 +154,34 @@ func (s *KeeperTestSuite) TestGetAllHistoricalInfo() {
ctx, keeper := s.ctx, s.stakingKeeper
require := s.Require()

_, addrVals := createValAddrs(50)

valSet := []stakingtypes.Validator{
testutil.NewValidator(s.T(), addrVals[0], PKs[0]),
testutil.NewValidator(s.T(), addrVals[1], PKs[1]),
}

header1 := cmtproto.Header{ChainID: "HelloChain", Height: 9}
header2 := cmtproto.Header{ChainID: "HelloChain", Height: 10}
header3 := cmtproto.Header{ChainID: "HelloChain", Height: 11}

hist1 := stakingtypes.HistoricalInfo{Header: header1, Valset: valSet}
hist2 := stakingtypes.HistoricalInfo{Header: header2, Valset: valSet}
hist3 := stakingtypes.HistoricalInfo{Header: header3, Valset: valSet}
hist1 := stakingtypes.Historical{
Time: &header1.Time,
NextValidatorHash: header1.NextValidatorsHash,
Apphash: header1.AppHash,
}
hist2 := stakingtypes.Historical{
Time: &header2.Time,
NextValidatorHash: header2.NextValidatorsHash,
Apphash: header2.AppHash,
}
hist3 := stakingtypes.Historical{
Time: &header3.Time,
NextValidatorHash: header3.NextValidatorsHash,
Apphash: header3.AppHash,
}

expHistInfos := []stakingtypes.HistoricalInfo{hist1, hist2, hist3}
expHistInfos := []stakingtypes.Historical{hist1, hist2, hist3}

for i, hi := range expHistInfos {
require.NoError(keeper.HistoricalInfo.Set(ctx, uint64(int64(9+i)), hi))
}

var infos []stakingtypes.HistoricalInfo
err := keeper.HistoricalInfo.Walk(ctx, nil, func(key uint64, info stakingtypes.HistoricalInfo) (stop bool, err error) {
var infos []stakingtypes.Historical
err := keeper.HistoricalInfo.Walk(ctx, nil, func(key uint64, info stakingtypes.Historical) (stop bool, err error) {
infos = append(infos, info)
return false, nil
})
Expand Down
21 changes: 19 additions & 2 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ var _ types.ValidatorSet = Keeper{}
// Implements DelegationSet interface
var _ types.DelegationSet = Keeper{}

func HistoricalInfoCodec(cdc codec.BinaryCodec) collcodec.ValueCodec[types.Historical] {
return collcodec.NewAltValueCodec(codec.CollValue[types.Historical](cdc), func(b []byte) (types.Historical, error) {
historicalinfo := types.HistoricalInfo{}
err := historicalinfo.Unmarshal(b)
if err != nil {
return types.Historical{}, err
}

return types.Historical{
Apphash: historicalinfo.Header.AppHash,
Time: &historicalinfo.Header.Time,
NextValidatorHash: historicalinfo.Header.NextValidatorsHash,
}, nil
})
}

// Keeper of the x/staking store
type Keeper struct {
storeService storetypes.KVStoreService
Expand All @@ -37,8 +53,9 @@ type Keeper struct {
consensusAddressCodec addresscodec.Codec

Schema collections.Schema

// HistoricalInfo key: Height | value: HistoricalInfo
HistoricalInfo collections.Map[uint64, types.HistoricalInfo]
HistoricalInfo collections.Map[uint64, types.Historical]
// LastTotalPower value: LastTotalPower
LastTotalPower collections.Item[math.Int]
// ValidatorUpdates value: ValidatorUpdates
Expand Down Expand Up @@ -111,7 +128,7 @@ func NewKeeper(
validatorAddressCodec: validatorAddressCodec,
consensusAddressCodec: consensusAddressCodec,
LastTotalPower: collections.NewItem(sb, types.LastTotalPowerKey, "last_total_power", sdk.IntValue),
HistoricalInfo: collections.NewMap(sb, types.HistoricalInfoKey, "historical_info", collections.Uint64Key, codec.CollValue[types.HistoricalInfo](cdc)),
HistoricalInfo: collections.NewMap(sb, types.HistoricalInfoKey, "historical_info", collections.Uint64Key, HistoricalInfoCodec(cdc)),
ValidatorUpdates: collections.NewItem(sb, types.ValidatorUpdatesKey, "validator_updates", codec.CollValue[types.ValidatorUpdates](cdc)),
Delegations: collections.NewMap(
sb, types.DelegationKey, "delegations",
Expand Down
Loading

0 comments on commit 5e06fd4

Please sign in to comment.