chore: migrate sdk.Msg to transaction.type (#20273)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com>
This commit is contained in:
co-authored by
coderabbitai[bot]
Hieu Vu
parent
98471350e4
commit
62b0b26d0c
@@ -5,17 +5,18 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
appmodulev2 "cosmossdk.io/core/appmodule/v2"
|
||||
"cosmossdk.io/core/transaction"
|
||||
"cosmossdk.io/server/v2/stf/branch"
|
||||
"cosmossdk.io/server/v2/stf/gas"
|
||||
"cosmossdk.io/server/v2/stf/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBranchService(t *testing.T) {
|
||||
s := &STF[mock.Tx]{
|
||||
handleMsg: func(ctx context.Context, msg transaction.Type) (msgResp transaction.Type, err error) {
|
||||
handleMsg: func(ctx context.Context, msg transaction.Msg) (msgResp transaction.Msg, err error) {
|
||||
kvSet(t, ctx, "exec")
|
||||
return nil, nil
|
||||
},
|
||||
|
||||
@@ -8,9 +8,7 @@ import (
|
||||
|
||||
var _ header.Service = (*HeaderService)(nil)
|
||||
|
||||
type HeaderService struct {
|
||||
getHeader func() (header.Info, error)
|
||||
}
|
||||
type HeaderService struct{}
|
||||
|
||||
func (h HeaderService) HeaderInfo(ctx context.Context) header.Info {
|
||||
return ctx.(*executionContext).headerInfo
|
||||
|
||||
@@ -23,6 +23,7 @@ require (
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rs/zerolog v1.32.0 // indirect
|
||||
github.com/tendermint/go-amino v0.16.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -3,11 +3,16 @@ cosmossdk.io/log v1.3.1/go.mod h1:2/dIomt8mKdk6vl3OWJcPk2be3pGOS8OQaLUM/3/tCM=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cosmos/gogoproto v1.4.12 h1:vB6Lbe/rtnYGjQuFxkPiPYiCybqFT8QvLipDZP8JpFE=
|
||||
github.com/cosmos/gogoproto v1.4.12/go.mod h1:LnZob1bXRdUoqMMtwYlcR3wjiElmlC+FkjaZRv1/eLY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
|
||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
@@ -28,17 +33,25 @@ github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUz
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
|
||||
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
|
||||
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
|
||||
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
|
||||
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
+12
-15
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
gogoproto "github.com/cosmos/gogoproto/types"
|
||||
|
||||
"cosmossdk.io/core/transaction"
|
||||
)
|
||||
@@ -14,7 +14,7 @@ var _ transaction.Tx = Tx{}
|
||||
|
||||
type Tx struct {
|
||||
Sender []byte
|
||||
Msg transaction.Type
|
||||
Msg transaction.Msg
|
||||
GasLimit uint64
|
||||
}
|
||||
|
||||
@@ -22,11 +22,8 @@ func (t Tx) Hash() [32]byte {
|
||||
return sha256.Sum256(t.Bytes())
|
||||
}
|
||||
|
||||
func (t Tx) GetMessages() ([]transaction.Type, error) {
|
||||
if t.Msg == nil {
|
||||
return nil, errors.New("messages not available or are nil")
|
||||
}
|
||||
return []transaction.Type{t.Msg}, nil
|
||||
func (t Tx) GetMessages() []transaction.Msg {
|
||||
return []transaction.Msg{t.Msg}
|
||||
}
|
||||
|
||||
func (t Tx) GetSenders() ([]transaction.Identity, error) {
|
||||
@@ -41,14 +38,14 @@ func (t Tx) GetGasLimit() (uint64, error) {
|
||||
}
|
||||
|
||||
type encodedTx struct {
|
||||
Sender []byte `json:"sender"`
|
||||
Msg *anypb.Any `json:"message"`
|
||||
GasLimit uint64 `json:"gas_limit"`
|
||||
Sender []byte `json:"sender"`
|
||||
Msg *gogoproto.Any `json:"message"`
|
||||
GasLimit uint64 `json:"gas_limit"`
|
||||
}
|
||||
|
||||
func (t Tx) Bytes() []byte {
|
||||
v2Msg := t.Msg
|
||||
msg, err := anypb.New(v2Msg)
|
||||
msg, err := gogoproto.MarshalAny(v2Msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -69,8 +66,8 @@ func (t *Tx) Decode(b []byte) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
msg, err := rawTx.Msg.UnmarshalNew()
|
||||
if err != nil {
|
||||
var msg transaction.Msg
|
||||
if err := gogoproto.UnmarshalAny(rawTx.Msg, msg); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.Msg = msg
|
||||
@@ -84,8 +81,8 @@ func (t *Tx) DecodeJSON(b []byte) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
msg, err := rawTx.Msg.UnmarshalNew()
|
||||
if err != nil {
|
||||
var msg transaction.Msg
|
||||
if err := gogoproto.UnmarshalAny(rawTx.Msg, msg); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.Msg = msg
|
||||
|
||||
+14
-14
@@ -20,8 +20,8 @@ import (
|
||||
// STF is a struct that manages the state transition component of the app.
|
||||
type STF[T transaction.Tx] struct {
|
||||
logger log.Logger
|
||||
handleMsg func(ctx context.Context, msg transaction.Type) (transaction.Type, error)
|
||||
handleQuery func(ctx context.Context, req transaction.Type) (transaction.Type, error)
|
||||
handleMsg func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error)
|
||||
handleQuery func(ctx context.Context, req transaction.Msg) (transaction.Msg, error)
|
||||
|
||||
doPreBlock func(ctx context.Context, txs []T) error
|
||||
doBeginBlock func(ctx context.Context) error
|
||||
@@ -38,8 +38,8 @@ type STF[T transaction.Tx] struct {
|
||||
|
||||
// NewSTF returns a new STF instance.
|
||||
func NewSTF[T transaction.Tx](
|
||||
handleMsg func(ctx context.Context, msg transaction.Type) (transaction.Type, error),
|
||||
handleQuery func(ctx context.Context, req transaction.Type) (transaction.Type, error),
|
||||
handleMsg func(ctx context.Context, msg transaction.Msg) (transaction.Msg, error),
|
||||
handleQuery func(ctx context.Context, req transaction.Msg) (transaction.Msg, error),
|
||||
doPreBlock func(ctx context.Context, txs []T) error,
|
||||
doBeginBlock func(ctx context.Context) error,
|
||||
doEndBlock func(ctx context.Context) error,
|
||||
@@ -229,7 +229,7 @@ func (s STF[T]) execTx(
|
||||
tx T,
|
||||
execMode corecontext.ExecMode,
|
||||
hi header.Info,
|
||||
) ([]transaction.Type, uint64, []event.Event, error) {
|
||||
) ([]transaction.Msg, uint64, []event.Event, error) {
|
||||
execState := s.branchFn(state)
|
||||
|
||||
msgsResp, gasUsed, runTxMsgsEvents, txErr := s.runTxMsgs(ctx, execState, gasLimit, tx, execMode, hi)
|
||||
@@ -285,16 +285,16 @@ func (s STF[T]) runTxMsgs(
|
||||
tx T,
|
||||
execMode corecontext.ExecMode,
|
||||
hi header.Info,
|
||||
) ([]transaction.Type, uint64, []event.Event, error) {
|
||||
) ([]transaction.Msg, uint64, []event.Event, error) {
|
||||
txSenders, err := tx.GetSenders()
|
||||
if err != nil {
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
msgs, err := tx.GetMessages()
|
||||
msgs := tx.GetMessages()
|
||||
if err != nil {
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
msgResps := make([]transaction.Type, len(msgs))
|
||||
msgResps := make([]transaction.Msg, len(msgs))
|
||||
|
||||
execCtx := s.makeContext(ctx, nil, state, execMode)
|
||||
execCtx.setHeaderInfo(hi)
|
||||
@@ -333,9 +333,9 @@ func (s STF[T]) preBlock(
|
||||
|
||||
func (s STF[T]) runConsensusMessages(
|
||||
ctx *executionContext,
|
||||
messages []transaction.Type,
|
||||
) ([]transaction.Type, error) {
|
||||
responses := make([]transaction.Type, len(messages))
|
||||
messages []transaction.Msg,
|
||||
) ([]transaction.Msg, error) {
|
||||
responses := make([]transaction.Msg, len(messages))
|
||||
for i := range messages {
|
||||
resp, err := s.handleMsg(ctx, messages[i])
|
||||
if err != nil {
|
||||
@@ -477,8 +477,8 @@ func (s STF[T]) Query(
|
||||
ctx context.Context,
|
||||
state store.ReaderMap,
|
||||
gasLimit uint64,
|
||||
req transaction.Type,
|
||||
) (transaction.Type, error) {
|
||||
req transaction.Msg,
|
||||
) (transaction.Msg, error) {
|
||||
queryState := s.branchFn(state)
|
||||
hi, err := s.getHeaderInfo(queryState)
|
||||
if err != nil {
|
||||
@@ -490,7 +490,7 @@ func (s STF[T]) Query(
|
||||
return s.handleQuery(queryCtx, req)
|
||||
}
|
||||
|
||||
func (s STF[T]) Message(ctx context.Context, msg transaction.Type) (response transaction.Type, err error) {
|
||||
func (s STF[T]) Message(ctx context.Context, msg transaction.Msg) (response transaction.Msg, err error) {
|
||||
return s.handleMsg(ctx, msg)
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestSTF(t *testing.T) {
|
||||
sum := sha256.Sum256([]byte("test-hash"))
|
||||
|
||||
s := &STF[mock.Tx]{
|
||||
handleMsg: func(ctx context.Context, msg transaction.Type) (msgResp transaction.Type, err error) {
|
||||
handleMsg: func(ctx context.Context, msg transaction.Msg) (msgResp transaction.Msg, err error) {
|
||||
kvSet(t, ctx, "exec")
|
||||
return nil, nil
|
||||
},
|
||||
@@ -124,7 +124,7 @@ func TestSTF(t *testing.T) {
|
||||
t.Run("fail exec tx", func(t *testing.T) {
|
||||
// update the stf to fail on the handler
|
||||
s := s.clone()
|
||||
s.handleMsg = func(ctx context.Context, msg transaction.Type) (msgResp transaction.Type, err error) {
|
||||
s.handleMsg = func(ctx context.Context, msg transaction.Msg) (msgResp transaction.Msg, err error) {
|
||||
return nil, fmt.Errorf("failure")
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ func TestSTF(t *testing.T) {
|
||||
|
||||
t.Run("tx failed and post tx failed", func(t *testing.T) {
|
||||
s := s.clone()
|
||||
s.handleMsg = func(ctx context.Context, msg transaction.Type) (msgResp transaction.Type, err error) {
|
||||
s.handleMsg = func(ctx context.Context, msg transaction.Msg) (msgResp transaction.Msg, err error) {
|
||||
return nil, fmt.Errorf("exec failure")
|
||||
}
|
||||
s.postTxExec = func(ctx context.Context, tx mock.Tx, success bool) error { return fmt.Errorf("post tx failure") }
|
||||
|
||||
Reference in New Issue
Block a user