Skip to content

Commit

Permalink
Merge branch 'main' into julien/core-handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Sep 9, 2024
2 parents 14323ec + b92c173 commit da06273
Show file tree
Hide file tree
Showing 83 changed files with 1,282 additions and 1,192 deletions.
172 changes: 95 additions & 77 deletions baseapp/abci_test.go

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions baseapp/abci_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,10 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan
shouldAdd := true
txSignersSeqs := make(map[string]uint64)
for _, signer := range signerData {
seq, ok := selectedTxsSignersSeqs[signer.Signer.String()]
signerKey := string(signer.Signer)
seq, ok := selectedTxsSignersSeqs[signerKey]
if !ok {
txSignersSeqs[signer.Signer.String()] = signer.Sequence
txSignersSeqs[signerKey] = signer.Sequence
continue
}

Expand All @@ -317,7 +318,7 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan
shouldAdd = false
break
}
txSignersSeqs[signer.Signer.String()] = signer.Sequence
txSignersSeqs[signerKey] = signer.Sequence
}
if !shouldAdd {
return true
Expand Down
42 changes: 24 additions & 18 deletions baseapp/abci_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"cosmossdk.io/core/address"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/header"
"cosmossdk.io/log"
Expand Down Expand Up @@ -485,9 +486,11 @@ func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_NoOpMempoolTxSelection()

// build a tx
_, _, addr := testdata.KeyTestPubAddr()
addrStr, err := signingCtx.AddressCodec().BytesToString(addr)
require.NoError(s.T(), err)
builder := txConfig.NewTxBuilder()
s.Require().NoError(builder.SetMsgs(
&baseapptestutil.MsgCounter{Counter: 0, FailOnHandler: false, Signer: addr.String()},
&baseapptestutil.MsgCounter{Counter: 0, FailOnHandler: false, Signer: addrStr},
))
builder.SetGasLimit(100)
setTxSignature(s.T(), builder, 0)
Expand Down Expand Up @@ -592,24 +595,24 @@ func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_PriorityNonceMempoolTxSe

testTxs := []testTx{
// test 1
{tx: buildMsg(s.T(), txConfig, []byte(`0`), [][]byte{secret1}, []uint64{1}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`12345678910`), [][]byte{secret1}, []uint64{2}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`22`), [][]byte{secret1}, []uint64{3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`32`), [][]byte{secret2}, []uint64{1}), priority: 8},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`0`), [][]byte{secret1}, []uint64{1}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`12345678910`), [][]byte{secret1}, []uint64{2}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`22`), [][]byte{secret1}, []uint64{3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`32`), [][]byte{secret2}, []uint64{1}), priority: 8},
// test 2
{tx: buildMsg(s.T(), txConfig, []byte(`4`), [][]byte{secret1, secret2}, []uint64{3, 3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`52345678910`), [][]byte{secret1, secret3}, []uint64{4, 3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`62`), [][]byte{secret1, secret4}, []uint64{5, 3}), priority: 8},
{tx: buildMsg(s.T(), txConfig, []byte(`72`), [][]byte{secret3, secret5}, []uint64{4, 3}), priority: 8},
{tx: buildMsg(s.T(), txConfig, []byte(`82`), [][]byte{secret2, secret6}, []uint64{4, 3}), priority: 8},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`4`), [][]byte{secret1, secret2}, []uint64{3, 3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`52345678910`), [][]byte{secret1, secret3}, []uint64{4, 3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`62`), [][]byte{secret1, secret4}, []uint64{5, 3}), priority: 8},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`72`), [][]byte{secret3, secret5}, []uint64{4, 3}), priority: 8},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`82`), [][]byte{secret2, secret6}, []uint64{4, 3}), priority: 8},
// test 3
{tx: buildMsg(s.T(), txConfig, []byte(`9`), [][]byte{secret3, secret4}, []uint64{3, 3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`1052345678910`), [][]byte{secret1, secret2}, []uint64{4, 4}), priority: 8},
{tx: buildMsg(s.T(), txConfig, []byte(`11`), [][]byte{secret1, secret2}, []uint64{5, 5}), priority: 8},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`9`), [][]byte{secret3, secret4}, []uint64{3, 3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`1052345678910`), [][]byte{secret1, secret2}, []uint64{4, 4}), priority: 8},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`11`), [][]byte{secret1, secret2}, []uint64{5, 5}), priority: 8},
// test 4
{tx: buildMsg(s.T(), txConfig, []byte(`1252345678910`), [][]byte{secret1}, []uint64{3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`13`), [][]byte{secret1}, []uint64{5}), priority: 10},
{tx: buildMsg(s.T(), txConfig, []byte(`14`), [][]byte{secret1}, []uint64{6}), priority: 8},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`1252345678910`), [][]byte{secret1}, []uint64{3}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`13`), [][]byte{secret1}, []uint64{5}), priority: 10},
{tx: buildMsg(s.T(), txConfig, signingCtx.AddressCodec(), []byte(`14`), [][]byte{secret1}, []uint64{6}), priority: 8},
}

for i := range testTxs {
Expand Down Expand Up @@ -718,7 +721,7 @@ func marshalDelimitedFn(msg proto.Message) ([]byte, error) {
return buf.Bytes(), nil
}

func buildMsg(t *testing.T, txConfig client.TxConfig, value []byte, secrets [][]byte, nonces []uint64) sdk.Tx {
func buildMsg(t *testing.T, txConfig client.TxConfig, ac address.Codec, value []byte, secrets [][]byte, nonces []uint64) sdk.Tx {
t.Helper()
builder := txConfig.NewTxBuilder()

Expand All @@ -735,9 +738,12 @@ func buildMsg(t *testing.T, txConfig client.TxConfig, value []byte, secrets [][]
})
}

addr, err := ac.BytesToString(signatures[0].PubKey.Bytes())
require.NoError(t, err)

_ = builder.SetMsgs(
&baseapptestutil.MsgKeyValue{
Signer: sdk.AccAddress(signatures[0].PubKey.Bytes()).String(),
Signer: addr,
Value: value,
},
)
Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package baseapp

import (
"context"
"cosmossdk.io/core/server"
"errors"
"fmt"
"maps"
Expand All @@ -18,6 +17,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/core/header"
"cosmossdk.io/core/server"
corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
Expand Down
30 changes: 18 additions & 12 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

"cosmossdk.io/core/address"
corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
Expand Down Expand Up @@ -50,6 +51,7 @@ type (
cdc *codec.ProtoCodec
txConfig client.TxConfig
logBuffer *bytes.Buffer
ac address.Codec
}

SnapshotsConfig struct {
Expand Down Expand Up @@ -91,6 +93,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
cdc: cdc,
txConfig: txConfig,
logBuffer: logBuffer,
ac: cdc.InterfaceRegistry().SigningContext().AddressCodec(),
}
}

Expand Down Expand Up @@ -152,7 +155,10 @@ func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...fun
_, err := r.Read(value)
require.NoError(t, err)

msgs = append(msgs, &baseapptestutil.MsgKeyValue{Key: key, Value: value, Signer: addr.String()})
addrStr, err := suite.ac.BytesToString(addr)
require.NoError(t, err)

msgs = append(msgs, &baseapptestutil.MsgKeyValue{Key: key, Value: value, Signer: addrStr})
keyCounter++
}

Expand Down Expand Up @@ -226,7 +232,7 @@ func TestAnteHandlerGasMeter(t *testing.T) {
deliverKey := []byte("deliver-key")
baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, deliverKey})

tx := newTxCounter(t, suite.txConfig, 0, 0)
tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0)
txBytes, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
_, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: 1, Txs: [][]byte{txBytes}})
Expand Down Expand Up @@ -508,7 +514,7 @@ func TestTxDecoder(t *testing.T) {
// patch in TxConfig instead of using an output from x/auth/tx
txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)

tx := newTxCounter(t, txConfig, 1, 0)
tx := newTxCounter(t, txConfig, signingCtx.AddressCodec(), 1, 0)
txBytes, err := txConfig.TxEncoder()(tx)
require.NoError(t, err)

Expand Down Expand Up @@ -552,7 +558,7 @@ func TestCustomRunTxPanicHandler(t *testing.T) {

// transaction should panic with custom handler above
{
tx := newTxCounter(t, suite.txConfig, 0, 0)
tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0)

require.PanicsWithValue(t, customPanicMsg, func() {
bz, err := suite.txConfig.TxEncoder()(tx)
Expand Down Expand Up @@ -582,7 +588,7 @@ func TestBaseAppAnteHandler(t *testing.T) {
//
// NOTE: State should not be mutated here. This will be implicitly checked by
// the next txs ante handler execution (anteHandlerTxTest).
tx := newTxCounter(t, suite.txConfig, 0, 0)
tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0)
tx = setFailOnAnte(t, suite.txConfig, tx, true)

txBytes, err := suite.txConfig.TxEncoder()(tx)
Expand All @@ -599,8 +605,8 @@ func TestBaseAppAnteHandler(t *testing.T) {

// execute at tx that will pass the ante handler (the checkTx state should
// mutate) but will fail the message handler
tx = newTxCounter(t, suite.txConfig, 0, 0)
tx = setFailOnHandler(t, suite.txConfig, tx, true)
tx = newTxCounter(t, suite.txConfig, suite.ac, 0, 0)
tx = setFailOnHandler(t, suite.txConfig, suite.ac, tx, true)

txBytes, err = suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
Expand All @@ -617,7 +623,7 @@ func TestBaseAppAnteHandler(t *testing.T) {

// Execute a successful ante handler and message execution where state is
// implicitly checked by previous tx executions.
tx = newTxCounter(t, suite.txConfig, 1, 0)
tx = newTxCounter(t, suite.txConfig, suite.ac, 1, 0)

txBytes, err = suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
Expand Down Expand Up @@ -657,7 +663,7 @@ func TestBaseAppPostHandler(t *testing.T) {
//
// NOTE: State should not be mutated here. This will be implicitly checked by
// the next txs ante handler execution (anteHandlerTxTest).
tx := newTxCounter(t, suite.txConfig, 0, 0)
tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0)
txBytes, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)

Expand All @@ -671,7 +677,7 @@ func TestBaseAppPostHandler(t *testing.T) {

// It should also run on failed message execution
postHandlerRun = false
tx = setFailOnHandler(t, suite.txConfig, tx, true)
tx = setFailOnHandler(t, suite.txConfig, suite.ac, tx, true)
txBytes, err = suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
res, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: 1, Txs: [][]byte{txBytes}})
Expand All @@ -682,7 +688,7 @@ func TestBaseAppPostHandler(t *testing.T) {
require.True(t, postHandlerRun)

// regression test, should not panic when runMsgs fails
tx = wonkyMsg(t, suite.txConfig, tx)
tx = wonkyMsg(t, suite.txConfig, suite.ac, tx)
txBytes, err = suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
_, err = suite.baseApp.FinalizeBlock(&abci.FinalizeBlockRequest{Height: 1, Txs: [][]byte{txBytes}})
Expand Down Expand Up @@ -903,7 +909,7 @@ func TestABCI_FinalizeWithInvalidTX(t *testing.T) {
_, err := suite.baseApp.InitChain(&abci.InitChainRequest{ConsensusParams: &cmtproto.ConsensusParams{}})
require.NoError(t, err)

tx := newTxCounter(t, suite.txConfig, 0, 0)
tx := newTxCounter(t, suite.txConfig, suite.ac, 0, 0)
bz, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)

Expand Down
4 changes: 3 additions & 1 deletion baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ func TestMsgService(t *testing.T) {
require.NoError(t, err)

_, _, addr := testdata.KeyTestPubAddr()
addrStr, err := signingCtx.AddressCodec().BytesToString(addr)
require.NoError(t, err)
msg := testdata.MsgCreateDog{
Dog: &testdata.Dog{Name: "Spot"},
Owner: addr.String(),
Owner: addrStr,
}

txBuilder := txConfig.NewTxBuilder()
Expand Down
2 changes: 1 addition & 1 deletion baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package baseapp

import (
"context"
"cosmossdk.io/core/server"
"errors"
"fmt"
"io"
"math"

"cosmossdk.io/core/server"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/metrics"
pruningtypes "cosmossdk.io/store/pruning/types"
Expand Down
2 changes: 1 addition & 1 deletion baseapp/streaming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestABCI_MultiListener_StateChanges(t *testing.T) {

for i := 0; i < txPerHeight; i++ {
counter := int64(blockN*txPerHeight + i)
tx := newTxCounter(t, suite.txConfig, counter, counter)
tx := newTxCounter(t, suite.txConfig, suite.ac, counter, counter)

txBytes, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
Expand Down
26 changes: 16 additions & 10 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package baseapp_test
import (
"bytes"
"context"
"cosmossdk.io/core/server"
"encoding/binary"
"encoding/json"
"errors"
Expand All @@ -21,6 +20,7 @@ import (
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/server"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -295,17 +295,19 @@ func parseTxMemo(t *testing.T, tx sdk.Tx) (counter int64, failOnAnte bool) {
return counter, failOnAnte
}

func newTxCounter(t *testing.T, cfg client.TxConfig, counter int64, msgCounters ...int64) signing.Tx {
func newTxCounter(t *testing.T, cfg client.TxConfig, ac address.Codec, counter int64, msgCounters ...int64) signing.Tx {
t.Helper()
_, _, addr := testdata.KeyTestPubAddr()
addrStr, err := ac.BytesToString(addr)
require.NoError(t, err)
msgs := make([]sdk.Msg, 0, len(msgCounters))
for _, c := range msgCounters {
msg := &baseapptestutil.MsgCounter{Counter: c, FailOnHandler: false, Signer: addr.String()}
msg := &baseapptestutil.MsgCounter{Counter: c, FailOnHandler: false, Signer: addrStr}
msgs = append(msgs, msg)
}

builder := cfg.NewTxBuilder()
err := builder.SetMsgs(msgs...)
err = builder.SetMsgs(msgs...)
require.NoError(t, err)
builder.SetMemo("counter=" + strconv.FormatInt(counter, 10) + "&failOnAnte=false")
setTxSignature(t, builder, uint64(counter))
Expand Down Expand Up @@ -343,37 +345,41 @@ func setFailOnAnte(t *testing.T, cfg client.TxConfig, tx signing.Tx, failOnAnte
return builder.GetTx()
}

func setFailOnHandler(t *testing.T, cfg client.TxConfig, tx signing.Tx, fail bool) signing.Tx {
func setFailOnHandler(t *testing.T, cfg client.TxConfig, ac address.Codec, tx signing.Tx, fail bool) signing.Tx {
t.Helper()
builder := cfg.NewTxBuilder()
builder.SetMemo(tx.GetMemo())

msgs := tx.GetMsgs()
addr, err := ac.BytesToString(sdk.AccAddress("addr"))
require.NoError(t, err)
for i, msg := range msgs {
msgs[i] = &baseapptestutil.MsgCounter{
Counter: msg.(*baseapptestutil.MsgCounter).Counter,
FailOnHandler: fail,
Signer: sdk.AccAddress("addr").String(),
Signer: addr,
}
}

err := builder.SetMsgs(msgs...)
err = builder.SetMsgs(msgs...)
require.NoError(t, err)
return builder.GetTx()
}

// wonkyMsg is to be used to run a MsgCounter2 message when the MsgCounter2 handler is not registered.
func wonkyMsg(t *testing.T, cfg client.TxConfig, tx signing.Tx) signing.Tx {
func wonkyMsg(t *testing.T, cfg client.TxConfig, ac address.Codec, tx signing.Tx) signing.Tx {
t.Helper()
builder := cfg.NewTxBuilder()
builder.SetMemo(tx.GetMemo())

msgs := tx.GetMsgs()
addr, err := ac.BytesToString(sdk.AccAddress("wonky"))
require.NoError(t, err)
msgs = append(msgs, &baseapptestutil.MsgCounter2{
Signer: sdk.AccAddress("wonky").String(),
Signer: addr,
})

err := builder.SetMsgs(msgs...)
err = builder.SetMsgs(msgs...)
require.NoError(t, err)
return builder.GetTx()
}
Expand Down
7 changes: 4 additions & 3 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ func PubkeyRawCmd() *cobra.Command {
Short: "Decode a ED25519 or secp256k1 pubkey from hex, base64, or bech32",
Long: "Decode a pubkey from hex, base64, or bech32.",
Example: fmt.Sprintf(`
%s debug pubkey-raw TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
%s debug pubkey-raw cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
`, version.AppName, version.AppName),
%s debug pubkey-raw 8FCA9D6D1F80947FD5E9A05309259746F5F72541121766D5F921339DD061174A
%s debug pubkey-raw j8qdbR+AlH/V6aBTCSWXRvX3JUESF2bV+SEzndBhF0o=
%s debug pubkey-raw cosmospub1zcjduepq3l9f6mglsz28l40f5pfsjfvhgm6lwf2pzgtkd40eyyeem5rpza9q47axrz
`, version.AppName, version.AppName, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
Expand Down
Loading

0 comments on commit da06273

Please sign in to comment.