Decouple client/tx from x/auth (#5992)

As part of #5864 part 2), this PR decouples the new client/tx
package from x/auth (as an incremental step to deprecating
all the tx logic in x/auth) and adds StdFee, StdSignature,
StdSignMsgBase and StdTxBase to codec/std, while
deprecating the corresponding types in x/auth.
This commit is contained in:
Aaron Craelius
2020-04-15 23:55:02 +02:00
committed by GitHub
parent aeee097b2f
commit bb4642ad0d
14 changed files with 1470 additions and 1353 deletions
+1212 -98
View File
File diff suppressed because it is too large Load Diff
+42 -4
View File
@@ -3,6 +3,7 @@ package cosmos_sdk.codec.std.v1;
import "third_party/proto/cosmos-proto/cosmos.proto";
import "third_party/proto/gogoproto/gogo.proto";
import "types/types.proto";
import "x/auth/types/types.proto";
import "x/auth/vesting/types/types.proto";
import "x/bank/types/types.proto";
@@ -106,8 +107,7 @@ message Content {
message Transaction {
option (gogoproto.goproto_getters) = false;
cosmos_sdk.x.auth.v1.StdTxBase base = 1
[(gogoproto.jsontag) = "", (gogoproto.embed) = true, (gogoproto.nullable) = false];
StdTxBase base = 1 [(gogoproto.jsontag) = "", (gogoproto.embed) = true, (gogoproto.nullable) = false];
repeated Message msgs = 2 [(gogoproto.nullable) = false];
}
@@ -141,7 +141,45 @@ message Message {
// SignDoc defines a standard application-level signing document to compose
// signatures for a Transaction.
message SignDoc {
cosmos_sdk.x.auth.v1.StdSignDocBase base = 1
[(gogoproto.jsontag) = "", (gogoproto.embed) = true, (gogoproto.nullable) = false];
StdSignDocBase base = 1 [(gogoproto.jsontag) = "", (gogoproto.embed) = true, (gogoproto.nullable) = false];
repeated Message msgs = 2 [(gogoproto.nullable) = false];
}
// StdFee includes the amount of coins paid in fees and the maximum
// gas to be used by the transaction. The ratio yields an effective "gasprice",
// which must be above some miminum to be accepted into the mempool.
message StdFee {
option (gogoproto.goproto_getters) = false;
option (gogoproto.equal) = true;
repeated cosmos_sdk.v1.Coin amount = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
uint64 gas = 2;
}
// StdSignature defines a signature structure that contains the signature of a
// transaction and an optional public key.
message StdSignature {
option (gogoproto.goproto_getters) = false;
bytes pub_key = 1 [(gogoproto.jsontag) = "public_key,omitempty", (gogoproto.moretags) = "yaml:\"public_key\""];
bytes signature = 2;
}
// StdTxBase defines a transaction base which application-level concrete transaction
// types can extend.
message StdTxBase {
StdFee fee = 1 [(gogoproto.nullable) = false];
repeated StdSignature signatures = 2 [(gogoproto.nullable) = false];
string memo = 3;
}
// StdSignDocBase defines the base structure for which applications can extend
// to define the concrete structure that signers sign over.
message StdSignDocBase {
string chain_id = 1 [(gogoproto.customname) = "ChainID", (gogoproto.moretags) = "yaml:\"chain_id\""];
uint64 account_number = 2 [(gogoproto.moretags) = "yaml:\"account_number\""];
uint64 sequence = 3;
string memo = 4;
StdFee fee = 5 [(gogoproto.nullable) = false];
}
+101 -12
View File
@@ -1,6 +1,9 @@
package std
import (
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
clientx "github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@@ -8,23 +11,33 @@ import (
)
var (
_ sdk.Tx = (*Transaction)(nil)
_ clientx.ClientTx = (*Transaction)(nil)
_ clientx.Generator = TxGenerator{}
_ sdk.Tx = (*Transaction)(nil)
_ clientx.ClientTx = (*Transaction)(nil)
_ clientx.Generator = TxGenerator{}
_ clientx.ClientFee = &StdFee{}
_ clientx.ClientSignature = &StdSignature{}
)
// TxGenerator defines a transaction generator that allows clients to construct
// transactions.
type TxGenerator struct{}
func (g TxGenerator) NewFee() clientx.ClientFee {
return &StdFee{}
}
func (g TxGenerator) NewSignature() clientx.ClientSignature {
return &StdSignature{}
}
// NewTx returns a reference to an empty Transaction type.
func (TxGenerator) NewTx() clientx.ClientTx {
return &Transaction{}
}
func NewTransaction(fee auth.StdFee, memo string, sdkMsgs []sdk.Msg) (*Transaction, error) {
func NewTransaction(fee StdFee, memo string, sdkMsgs []sdk.Msg) (*Transaction, error) {
tx := &Transaction{
StdTxBase: auth.NewStdTxBase(fee, nil, memo),
StdTxBase: NewStdTxBase(fee, nil, memo),
}
if err := tx.SetMsgs(sdkMsgs...); err != nil {
@@ -121,15 +134,16 @@ func (tx Transaction) GetSignatures() []sdk.Signature {
// SetSignatures sets the transaction's signatures. It will overwrite any
// existing signatures set.
func (tx *Transaction) SetSignatures(sdkSigs ...sdk.Signature) {
sigs := make([]auth.StdSignature, len(sdkSigs))
func (tx *Transaction) SetSignatures(sdkSigs ...clientx.ClientSignature) error {
sigs := make([]StdSignature, len(sdkSigs))
for i, sig := range sdkSigs {
if sig != nil {
sigs[i] = auth.NewStdSignature(sig.GetPubKey(), sig.GetSignature())
sigs[i] = NewStdSignature(sig.GetPubKey(), sig.GetSignature())
}
}
tx.Signatures = sigs
return nil
}
// GetFee returns the transaction's fee.
@@ -138,8 +152,9 @@ func (tx Transaction) GetFee() sdk.Fee {
}
// SetFee sets the transaction's fee. It will overwrite any existing fee set.
func (tx *Transaction) SetFee(fee sdk.Fee) {
tx.Fee = auth.NewStdFee(fee.GetGas(), fee.GetAmount())
func (tx *Transaction) SetFee(fee clientx.ClientFee) error {
tx.Fee = NewStdFee(fee.GetGas(), fee.GetAmount())
return nil
}
// GetMemo returns the transaction's memo.
@@ -160,9 +175,9 @@ func (tx Transaction) CanonicalSignBytes(cid string, num, seq uint64) ([]byte, e
return NewSignDoc(num, seq, cid, tx.Memo, tx.Fee, tx.Msgs...).CanonicalSignBytes()
}
func NewSignDoc(num, seq uint64, cid, memo string, fee auth.StdFee, msgs ...Message) *SignDoc {
func NewSignDoc(num, seq uint64, cid, memo string, fee StdFee, msgs ...Message) *SignDoc {
return &SignDoc{
StdSignDocBase: auth.NewStdSignDocBase(num, seq, cid, memo, fee),
StdSignDocBase: NewStdSignDocBase(num, seq, cid, memo, fee),
Msgs: msgs,
}
}
@@ -174,3 +189,77 @@ func NewSignDoc(num, seq uint64, cid, memo string, fee auth.StdFee, msgs ...Mess
func (sd *SignDoc) CanonicalSignBytes() ([]byte, error) {
return sdk.CanonicalSignBytes(sd)
}
// NewStdFee returns a new instance of StdFee
func NewStdFee(gas uint64, amount sdk.Coins) StdFee {
return StdFee{
Amount: amount,
Gas: gas,
}
}
func NewStdSignature(pk crypto.PubKey, sig []byte) StdSignature {
var pkBz []byte
if pk != nil {
pkBz = pk.Bytes()
}
return StdSignature{PubKey: pkBz, Signature: sig}
}
func NewStdTxBase(fee StdFee, sigs []StdSignature, memo string) StdTxBase {
return StdTxBase{
Fee: fee,
Signatures: sigs,
Memo: memo,
}
}
func NewStdSignDocBase(num, seq uint64, cid, memo string, fee StdFee) StdSignDocBase {
return StdSignDocBase{
ChainID: cid,
AccountNumber: num,
Sequence: seq,
Memo: memo,
Fee: fee,
}
}
func (m StdFee) GetGas() uint64 {
return m.Gas
}
func (m StdFee) GetAmount() sdk.Coins {
return m.Amount
}
func (m *StdFee) SetGas(gas uint64) {
m.Gas = gas
}
func (m *StdFee) SetAmount(amount sdk.Coins) {
m.Amount = amount
}
func (m StdSignature) GetPubKey() crypto.PubKey {
var pk crypto.PubKey
if len(m.PubKey) == 0 {
return nil
}
amino.MustUnmarshalBinaryBare(m.PubKey, &pk)
return pk
}
func (m StdSignature) GetSignature() []byte {
return m.Signature
}
func (m *StdSignature) SetPubKey(pk crypto.PubKey) error {
m.PubKey = pk.Bytes()
return nil
}
func (m *StdSignature) SetSignature(signature []byte) {
m.Signature = signature
}
+1 -2
View File
@@ -7,12 +7,11 @@ import (
"github.com/cosmos/cosmos-sdk/codec/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
)
func TestTransaction(t *testing.T) {
f := auth.NewStdFee(100, sdk.NewCoins(sdk.NewInt64Coin("stake", 50)))
f := std.NewStdFee(100, sdk.NewCoins(sdk.NewInt64Coin("stake", 50)))
m := "hello world"
acc1 := sdk.AccAddress("from")
acc2 := sdk.AccAddress("to")