Skip to content

Commit

Permalink
refactor: sdk.Tx implements transaction.Tx (#19588)
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 authored and kocubinski committed May 16, 2024
1 parent faba9d1 commit 35fd591
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 13 deletions.
5 changes: 5 additions & 0 deletions contrib/images/simd-env/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ COPY x/bank/go.mod x/bank/go.sum /work/x/bank/
COPY x/mint/go.mod x/mint/go.sum /work/x/mint/
COPY x/consensus/go.mod x/consensus/go.sum /work/x/consensus/
COPY x/accounts/go.mod x/accounts/go.sum /work/x/accounts/
COPY runtime/v2/go.mod runtime/v2/go.sum /work/runtime/v2/
COPY server/v2/appmanager/go.mod server/v2/appmanager/go.sum /work/server/v2/appmanager/
COPY server/v2/core/go.mod server/v2/core/go.sum /work/server/v2/core/
COPY server/v2/stf/go.mod server/v2/stf/go.sum /work/server/v2/stf/

RUN go mod download

COPY ./ /work
Expand Down
22 changes: 19 additions & 3 deletions server/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"fmt"

"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/proto"

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -95,6 +95,22 @@ func NewTx(key, value string, accAddress sdk.AccAddress) *KVStoreTx {
}
}

func (msg *KVStoreTx) Hash() [32]byte {
return [32]byte{}
}

func (msg *KVStoreTx) GetGasLimit() (uint64, error) {
return 0, nil
}

func (msg *KVStoreTx) GetMessages() ([]proto.Message, error) {
return nil, nil
}

func (msg *KVStoreTx) GetSenders() ([][]byte, error) {
return nil, nil
}

func (msg *KVStoreTx) Type() string {
return "kvstore_tx"
}
Expand All @@ -103,8 +119,8 @@ func (msg *KVStoreTx) GetMsgs() []sdk.Msg {
return []sdk.Msg{msg}
}

func (msg *KVStoreTx) GetReflectMessages() ([]protoreflect.Message, error) {
return []protoreflect.Message{(&bankv1beta1.MsgSend{FromAddress: msg.address.String()}).ProtoReflect()}, nil // this is a hack for tests
func (msg *KVStoreTx) GetMsgsV2() ([]proto.Message, error) {
return []proto.Message{&bankv1beta1.MsgSend{FromAddress: msg.address.String()}}, nil // this is a hack for tests
}

func (msg *KVStoreTx) GetSignBytes() []byte {
Expand Down
7 changes: 5 additions & 2 deletions server/v2/stf/mock/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ func (t Tx) Hash() [32]byte {
return sha256.Sum256(t.Bytes())
}

func (t Tx) GetMessages() ([]transaction.Msg, error) {
return []transaction.Msg{t.Msg}, nil
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) GetSenders() ([]transaction.Identity, error) {
Expand Down
40 changes: 40 additions & 0 deletions types/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ var (
_ cryptotypes.PubKey = (*testPubKey)(nil)
)

func (tx testTx) Bytes() []byte {
return []byte{}
}

func (tx testTx) Hash() [32]byte {
return [32]byte{}
}

func (tx testTx) GetGasLimit() (uint64, error) {
return 0, nil
}

func (tx testTx) GetMessages() ([]protov2.Message, error) {
return nil, nil
}

func (tx testTx) GetSenders() ([][]byte, error) {
return nil, nil
}

func (tx testTx) GetMsgs() []sdk.Msg { return nil }

func (tx testTx) GetReflectMessages() ([]protoreflect.Message, error) { return nil, nil }
Expand All @@ -89,6 +109,26 @@ type sigErrTx struct {
getSigs func() ([]txsigning.SignatureV2, error)
}

func (sigErrTx) Bytes() []byte {
return []byte{}
}

func (sigErrTx) Hash() [32]byte {
return [32]byte{}
}

func (sigErrTx) GetGasLimit() (uint64, error) {
return 0, nil
}

func (sigErrTx) GetMessages() ([]protov2.Message, error) {
return nil, nil
}

func (sigErrTx) GetSenders() ([][]byte, error) {
return nil, nil
}

func (sigErrTx) Size() int64 { return 0 }

func (sigErrTx) GetMsgs() []sdk.Msg { return nil }
Expand Down
20 changes: 20 additions & 0 deletions types/mempool/signer_extraction_adapater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ func (n nonVerifiableTx) GetReflectMessages() ([]protoreflect.Message, error) {
panic("not implemented")
}

func (n nonVerifiableTx) Bytes() []byte {
return []byte{}
}

func (n nonVerifiableTx) Hash() [32]byte {
return [32]byte{}
}

func (n nonVerifiableTx) GetGasLimit() (uint64, error) {
return 0, nil
}

func (n nonVerifiableTx) GetMessages() ([]proto.Message, error) {
return nil, nil
}

func (n nonVerifiableTx) GetSenders() ([][]byte, error) {
return nil, nil
}

func TestDefaultSignerExtractor(t *testing.T) {
accounts := simtypes.RandomAccounts(rand.New(rand.NewSource(0)), 1)
sa := accounts[0].Address
Expand Down
53 changes: 53 additions & 0 deletions x/auth/tx/gogotx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tx

import (
"crypto/sha256"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -100,10 +102,61 @@ type gogoTxWrapper struct {
fees sdk.Coins
feePayer []byte
feeGranter []byte

// Cache for hash and full bytes
cachedHash [32]byte
cachedBytes []byte
cachedHashed bool
}

func (w *gogoTxWrapper) String() string { return w.decodedTx.Tx.String() }

func (w *gogoTxWrapper) Bytes() []byte {
if !w.cachedHashed {
w.computeHashAndBytes()
}
return w.cachedBytes
}

func (w *gogoTxWrapper) Hash() [32]byte {
if !w.cachedHashed {
w.computeHashAndBytes()
}
return w.cachedHash
}

func (w *gogoTxWrapper) computeHashAndBytes() {
bz, err := proto.Marshal(w.decodedTx.TxRaw)
if err != nil {
panic(err)
}

w.cachedBytes = bz
w.cachedHash = sha256.Sum256(bz)
w.cachedHashed = true
}

func (w *gogoTxWrapper) GetGasLimit() (uint64, error) {
if w.decodedTx == nil || w.decodedTx.Tx == nil || w.decodedTx.Tx.AuthInfo == nil || w.decodedTx.Tx.AuthInfo.Fee == nil {
return 0, errors.New("gas limit not available, one or more required fields are nil")
}
return w.decodedTx.Tx.AuthInfo.Fee.GasLimit, nil
}

func (w *gogoTxWrapper) GetMessages() ([]protov2.Message, error) {
if w.decodedTx == nil || w.decodedTx.Messages == nil {
return nil, errors.New("messages not available or are nil")
}
return w.decodedTx.Messages, nil
}

func (w *gogoTxWrapper) GetSenders() ([][]byte, error) {
if w.decodedTx == nil || w.decodedTx.Signers == nil {
return nil, errors.New("Senders not available or are nil")
}
return w.decodedTx.Signers, nil
}

var (
_ authsigning.Tx = &gogoTxWrapper{}
_ ante.HasExtensionOptionsTx = &gogoTxWrapper{}
Expand Down
56 changes: 56 additions & 0 deletions x/tx/decode/decode.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package decode

import (
"crypto/sha256"
"errors"

"github.com/cosmos/cosmos-proto/anyutil"
"google.golang.org/protobuf/proto"

v1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
"cosmossdk.io/core/transaction"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/tx/signing"
)
Expand All @@ -18,8 +20,15 @@ type DecodedTx struct {
TxRaw *v1beta1.TxRaw
Signers [][]byte
TxBodyHasUnknownNonCriticals bool

// Cache for hash and full bytes
cachedHash [32]byte
cachedBytes []byte
cachedHashed bool
}

var _ transaction.Tx = &DecodedTx{}

// Decoder contains the dependencies required for decoding transactions.
type Decoder struct {
signingCtx *signing.Context
Expand Down Expand Up @@ -126,3 +135,50 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
Signers: signers,
}, nil
}

// Hash implements the interface for the Tx interface.
func (dtx *DecodedTx) Hash() [32]byte {
if !dtx.cachedHashed {
dtx.computeHashAndBytes()
}
return dtx.cachedHash
}

func (dtx *DecodedTx) GetGasLimit() (uint64, error) {
if dtx == nil || dtx.Tx == nil || dtx.Tx.AuthInfo == nil || dtx.Tx.AuthInfo.Fee == nil {
return 0, errors.New("gas limit not available or one or more required fields are nil")
}
return dtx.Tx.AuthInfo.Fee.GasLimit, nil
}

func (dtx *DecodedTx) GetMessages() ([]proto.Message, error) {
if dtx == nil || dtx.Messages == nil {
return nil, errors.New("messages not available or are nil")
}
return dtx.Messages, nil
}

func (dtx *DecodedTx) GetSenders() ([][]byte, error) {
if dtx == nil || dtx.Signers == nil {
return nil, errors.New("senders not available or are nil")
}
return dtx.Signers, nil
}

func (dtx *DecodedTx) Bytes() []byte {
if !dtx.cachedHashed {
dtx.computeHashAndBytes()
}
return dtx.cachedBytes
}

func (dtx *DecodedTx) computeHashAndBytes() {
bz, err := proto.Marshal(dtx.TxRaw)
if err != nil {
panic(err)
}

dtx.cachedBytes = bz
dtx.cachedHash = sha256.Sum256(bz)
dtx.cachedHashed = true
}
4 changes: 4 additions & 0 deletions x/tx/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.25.0 // indirect
Expand All @@ -33,6 +35,8 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace cosmossdk.io/core => ../../core

// NOTE: we do not want to replace to the development version of cosmossdk.io/api yet
// Until https://github.com/cosmos/cosmos-sdk/issues/19228 is resolved
// We are tagging x/tx from main and must keep using released versions of x/tx dependencies
15 changes: 7 additions & 8 deletions x/tx/go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
cosmossdk.io/api v0.7.5 h1:eMPTReoNmGUm8DeiQL9DyM8sYDjEhWzL1+nLbI9DqtQ=
cosmossdk.io/api v0.7.5/go.mod h1:IcxpYS5fMemZGqyYtErK7OqvdM0C8kdW3dq8Q/XIG38=
cosmossdk.io/core v0.11.0 h1:vtIafqUi+1ZNAE/oxLOQQ7Oek2n4S48SWLG8h/+wdbo=
cosmossdk.io/core v0.11.0/go.mod h1:LaTtayWBSoacF5xNzoF8tmLhehqlA9z1SWiPuNC6X1w=
cosmossdk.io/api v0.7.3 h1:V815i8YOwOAQa1rLCsSMjVG5Gnzs02JLq+l7ks8s1jk=
cosmossdk.io/api v0.7.3/go.mod h1:IcxpYS5fMemZGqyYtErK7OqvdM0C8kdW3dq8Q/XIG38=
cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0=
cosmossdk.io/errors v1.0.1/go.mod h1:MeelVSZThMi4bEakzhhhE/CKqVv3nOJDA25bIqRDu/U=
cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE=
cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k=
github.com/cosmos/cosmos-proto v1.0.0-beta.5 h1:eNcayDLpip+zVLRLYafhzLvQlSmyab+RC5W7ZfmxJLA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5/go.mod h1:hQGLpiIUloJBMdQMMWb/4wRApmI9hjHH05nefC0Ojec=
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/cosmos/cosmos-proto v1.0.0-beta.4 h1:aEL7tU/rLOmxZQ9z4i7mzxcLbSCY48OdY7lIWTLG7oU=
github.com/cosmos/cosmos-proto v1.0.0-beta.4/go.mod h1:oeB+FyVzG3XrQJbJng0EnV8Vljfk9XvTIpGILNU/9Co=
github.com/cosmos/gogoproto v1.4.11 h1:LZcMHrx4FjUgrqQSWeaGC1v/TeuVFqSLa43CC6aWR2g=
github.com/cosmos/gogoproto v1.4.11/go.mod h1:/g39Mh8m17X8Q/GDEs5zYTSNaNnInBSohtaxzQnYq1Y=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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=
Expand Down

0 comments on commit 35fd591

Please sign in to comment.