Skip to content

Commit

Permalink
remove implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Dec 18, 2024
1 parent 6ee24df commit ad2801e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 114 deletions.
7 changes: 1 addition & 6 deletions simapp/v2/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simapp

import (
"context"
_ "embed"
"fmt"

Expand Down Expand Up @@ -159,11 +158,7 @@ func NewSimApp[T transaction.Tx](
}

var err error
app.App, err = appBuilder.Build(runtime.AppBuilderWithPreblocker(
func(ctx context.Context, txs []T) error {
return nil
},
))
app.App, err = appBuilder.Build()
if err != nil {
return nil, err
}
Expand Down
3 changes: 0 additions & 3 deletions simapp/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ replace (
cosmossdk.io/x/protocolpool => ../../x/protocolpool
cosmossdk.io/x/slashing => ../../x/slashing
cosmossdk.io/x/staking => ../../x/staking
cosmossdk.io/x/tx => ../../x/tx
cosmossdk.io/x/auth => ../../x/auth
cosmossdk.io/x/upgrade => ../../x/upgrade
)
Expand All @@ -300,8 +299,6 @@ replace (

// server v2 integration
replace (
cosmossdk.io/core/testing => ../../core/testing
cosmossdk.io/core => ../../core
cosmossdk.io/indexer/postgres => ../../indexer/postgres
cosmossdk.io/runtime/v2 => ../../runtime/v2
cosmossdk.io/server/v2 => ../../server/v2
Expand Down
109 changes: 4 additions & 105 deletions simapp/v2/simdv2/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

v1 "github.com/cometbft/cometbft/api/cometbft/abci/v1"
cmtcfg "github.com/cometbft/cometbft/config"

"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/server/v2/cometbft"
"cosmossdk.io/server/v2/cometbft/handlers"
staking "cosmossdk.io/x/staking/types"

clientconfig "github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

// initAppConfig helps to override default client config template and configs.
Expand Down Expand Up @@ -100,9 +92,10 @@ func initCometConfig() cometbft.CfgOption {

func initCometOptions[T transaction.Tx]() cometbft.ServerOptions[T] {
serverOptions := cometbft.DefaultServerOptions[T]()
serverOptions.PrepareProposalHandler = CustomPrepareProposal[T]()
serverOptions.ProcessProposalHandler = CustomProcessProposalHandler[T]()
serverOptions.ExtendVoteHandler = CustomExtendVoteHandler[T]()
// Implement custom handlers (e.g. for Vote Extensions)
// serverOptions.PrepareProposalHandler = CustomPrepareProposal[T]()
// serverOptions.ProcessProposalHandler = CustomProcessProposalHandler[T]()
// serverOptions.ExtendVoteHandler = CustomExtendVoteHandler[T]()

// overwrite app mempool, using max-txs option
// serverOptions.Mempool = func(cfg map[string]any) mempool.Mempool[T] {
Expand All @@ -117,97 +110,3 @@ func initCometOptions[T transaction.Tx]() cometbft.ServerOptions[T] {

return serverOptions
}

func CustomExtendVoteHandler[T transaction.Tx]() handlers.ExtendVoteHandler {
return func(ctx context.Context, rm store.ReaderMap, evr *v1.ExtendVoteRequest) (*v1.ExtendVoteResponse, error) {
return &v1.ExtendVoteResponse{
VoteExtension: []byte("BTC=1234567.89;height=" + fmt.Sprint(evr.Height)),
}, nil
}
}

func CustomPrepareProposal[T transaction.Tx]() handlers.PrepareHandler[T] {
return func(ctx context.Context, app handlers.AppManager[T], codec transaction.Codec[T], req *v1.PrepareProposalRequest, chainID string) ([]T, error) {
var txs []T
for _, tx := range req.Txs {
decTx, err := codec.Decode(tx)
if err != nil {
continue
}

txs = append(txs, decTx)
}

// "Process" vote extensions (we'll just inject all votes)
injectedTx, err := json.Marshal(req.LocalLastCommit)
if err != nil {
return nil, err
}

// put the injected tx into the first position
txs = append([]T{cometbft.RawTx(injectedTx).(T)}, txs...)

return txs, nil
}
}

func CustomProcessProposalHandler[T transaction.Tx]() handlers.ProcessHandler[T] {
return func(ctx context.Context, am handlers.AppManager[T], c transaction.Codec[T], req *v1.ProcessProposalRequest, chainID string) error {
// Get all vote extensions from the first tx

injectedTx := req.Txs[0]
var voteExts v1.ExtendedCommitInfo
if err := json.Unmarshal(injectedTx, &voteExts); err != nil {
return err
}

// Get validators from the staking module
res, err := am.Query(
ctx,
0,
&staking.QueryValidatorsRequest{},
)
if err != nil {
return err
}

validatorsResponse := res.(*staking.QueryValidatorsResponse)
consAddrToPubkey := map[string]cryptotypes.PubKey{}

for _, val := range validatorsResponse.GetValidators() {
cv := val.ConsensusPubkey.GetCachedValue()
if cv == nil {
return fmt.Errorf("public key cached value is nil")
}

cpk, ok := cv.(cryptotypes.PubKey)
if ok {
consAddrToPubkey[string(cpk.Address().Bytes())] = cpk
} else {
return fmt.Errorf("invalid public key type")
}
}

// First verify that the vote extensions injected by the proposer are correct
if err := cometbft.ValidateVoteExtensions(
ctx,
am,
chainID,
func(ctx context.Context, b []byte) (cryptotypes.PubKey, error) {
if _, ok := consAddrToPubkey[string(b)]; !ok {
return nil, fmt.Errorf("validator not found")
}
return consAddrToPubkey[string(b)], nil
},
voteExts,
req.Height,
&req.ProposedLastCommit,
); err != nil {
return err
}

// TODO: do something with the vote extensions

return nil
}
}

0 comments on commit ad2801e

Please sign in to comment.