Skip to content

Commit

Permalink
feat: validator merkle tree
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Sep 5, 2024
1 parent 9b8c319 commit 7782dfb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ func NewApp(
runtime.NewKVStoreService(keys[batchingtypes.StoreKey]),
authtypes.NewModuleAddress(batchingtypes.ModuleName).String(),
app.StakingKeeper,
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
)

/* =================================================== */
Expand Down
65 changes: 63 additions & 2 deletions x/batching/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package keeper

import (
"encoding/binary"
"encoding/hex"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sedaprotocol/seda-chain/x/batching/types"

"github.com/cometbft/cometbft/crypto/merkle"
)

func (k Keeper) EndBlock(ctx sdk.Context) (err error) {
Expand All @@ -19,6 +26,60 @@ func (k Keeper) EndBlock(ctx sdk.Context) (err error) {
err = nil
}()

// TODO Construct batches and persist
return
batch, err := k.ConstructBatch(ctx)
if err != nil {
panic(err)
}
fmt.Println(batch)

err = k.SetBatch(ctx, batch)
if err != nil {
panic(err)
}

return nil
}

func (k Keeper) ConstructBatch(ctx sdk.Context) (types.Batch, error) {
curBatchNum, err := k.GetCurrentBatchNum(ctx)
if err != nil {
return types.Batch{}, err
}

// Construct data result tree.
// Fetch batch-ready data requests.
// TODO: Deal with offset and limits. (#313)
// queryRes, err := k.wasmViewKeeper.QuerySmart(ctx, coreContract, []byte(`{"get_data_requests_by_status":{"status": "tallied", "offset": 0, "limit": 100}}`))
// if err != nil {
// return err
// }
// if string(queryRes) == "[]" {
// return nil
// }

// Leaves are SHA-256 hashes of JSON-serialized data result structs.

// Construct validator tree.
var leaves [][]byte
err = k.stakingKeeper.IterateLastValidatorPowers(ctx, func(addr sdk.ValAddress, power int64) (stop bool) {
buf := make([]byte, len(addr)+8)
copy(buf[:len(addr)], addr)
binary.BigEndian.PutUint64(buf[:len(addr)], uint64(power))

leaves = append(leaves, addr)
return false
})
if err != nil {
return types.Batch{}, err
}
valRoot := merkle.HashFromByteSlices(leaves) // TODO "ordered" merkle tree
valRootHex := hex.EncodeToString(valRoot)

return types.Batch{
BatchNumber: curBatchNum,
BlockHeight: ctx.BlockHeight(),
// DataResultRoot: ,
ValidatorRoot: valRootHex,
BlockTime: ctx.BlockTime(),
}, nil
}
2 changes: 1 addition & 1 deletion x/batching/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
panic(err)
}
for _, batch := range data.Batches {
if err := k.SetBatch(ctx, batch.BatchNumber, batch); err != nil {
if err := k.SetBatch(ctx, batch); err != nil {
panic(err)
}
}
Expand Down
27 changes: 18 additions & 9 deletions x/batching/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"cosmossdk.io/collections"
addresscodec "cosmossdk.io/core/address"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"

Expand All @@ -15,7 +16,8 @@ import (
)

type Keeper struct {
stakingKeeper types.StakingKeeper
stakingKeeper types.StakingKeeper
validatorAddressCodec addresscodec.Codec

// authority is the address capable of executing MsgUpdateParams.
// Typically, this should be the gov module address.
Expand All @@ -27,15 +29,22 @@ type Keeper struct {
params collections.Item[types.Params]
}

func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, authority string, sk types.StakingKeeper) Keeper {
func NewKeeper(
cdc codec.BinaryCodec,
storeService storetypes.KVStoreService,
authority string,
sk types.StakingKeeper,
validatorAddressCodec addresscodec.Codec,
) Keeper {
sb := collections.NewSchemaBuilder(storeService)

k := Keeper{
authority: authority,
stakingKeeper: sk,
currentBatchNumber: collections.NewSequence(sb, types.CurrentBatchNumberKey, "current_batch_number"),
batch: collections.NewMap(sb, types.BatchPrefix, "batch", collections.Uint64Key, codec.CollValue[types.Batch](cdc)),
params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
authority: authority,
stakingKeeper: sk,
validatorAddressCodec: validatorAddressCodec,
currentBatchNumber: collections.NewSequence(sb, types.CurrentBatchNumberKey, "current_batch_number"),
batch: collections.NewMap(sb, types.BatchPrefix, "batch", collections.Uint64Key, codec.CollValue[types.Batch](cdc)),
params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
}

schema, err := sb.Build()
Expand Down Expand Up @@ -63,8 +72,8 @@ func (k Keeper) GetCurrentBatchNum(ctx context.Context) (uint64, error) {
return batchNum, nil
}

func (k Keeper) SetBatch(ctx context.Context, batchNum uint64, batch types.Batch) error {
return k.batch.Set(ctx, batchNum, batch)
func (k Keeper) SetBatch(ctx context.Context, batch types.Batch) error {
return k.batch.Set(ctx, batch.BatchNumber, batch)
}

func (k Keeper) GetBatch(ctx context.Context, batchNum uint64) (types.Batch, error) {
Expand Down
15 changes: 14 additions & 1 deletion x/batching/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
package types

type StakingKeeper interface{}
import (
"context"

abci "github.com/cometbft/cometbft/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type StakingKeeper interface {
GetBondedValidatorsByPower(ctx context.Context) ([]stakingtypes.Validator, error)
GetValidatorUpdates(ctx context.Context) ([]abci.ValidatorUpdate, error)
IterateLastValidatorPowers(ctx context.Context, handler func(operator sdk.ValAddress, power int64) (stop bool)) error
}

0 comments on commit 7782dfb

Please sign in to comment.