diff --git a/data/interface.go b/data/interface.go index 80b039da..259277e4 100644 --- a/data/interface.go +++ b/data/interface.go @@ -271,6 +271,7 @@ type TransactionHandler interface { GetSndAddr() []byte GetGasLimit() uint64 GetGasPrice() uint64 + GetUserTransaction() TransactionHandler SetValue(*big.Int) SetData([]byte) diff --git a/data/receipt/receipt.go b/data/receipt/receipt.go index 992e4932..acff06aa 100644 --- a/data/receipt/receipt.go +++ b/data/receipt/receipt.go @@ -58,6 +58,11 @@ func (_ *Receipt) GetRcvUserName() []byte { return nil } +// GetUserTransaction returns nil as Receipt does not have user transaction +func (rpt *Receipt) GetUserTransaction() data.TransactionHandler { + return nil +} + // CheckIntegrity checks for not nil fields and negative value func (rpt *Receipt) CheckIntegrity() error { return nil diff --git a/data/receipt/receipt_test.go b/data/receipt/receipt_test.go index 84f68941..5621c968 100644 --- a/data/receipt/receipt_test.go +++ b/data/receipt/receipt_test.go @@ -29,6 +29,7 @@ func TestReceipt_SettersAndGetters(t *testing.T) { assert.Equal(t, uint64(0), r.GetNonce()) assert.Equal(t, uint64(0), r.GetGasPrice()) assert.Equal(t, uint64(0), r.GetGasLimit()) + assert.Nil(t, r.GetUserTransaction()) } func TestReceipt_CheckIntegrityReturnsNil(t *testing.T) { diff --git a/data/rewardTx/rewardTx.go b/data/rewardTx/rewardTx.go index 0ccb44a5..35ec718b 100644 --- a/data/rewardTx/rewardTx.go +++ b/data/rewardTx/rewardTx.go @@ -62,6 +62,11 @@ func (rtx *RewardTx) GetRcvUserName() []byte { return nil } +// GetUserTransaction returns nil as RewardTx does not have user transaction +func (rtx *RewardTx) GetUserTransaction() data.TransactionHandler { + return nil +} + // CheckIntegrity checks for not nil fields and negative value func (rtx *RewardTx) CheckIntegrity() error { if len(rtx.RcvAddr) == 0 { diff --git a/data/rewardTx/rewardTx_test.go b/data/rewardTx/rewardTx_test.go index 2f8680e2..70d24bf6 100644 --- a/data/rewardTx/rewardTx_test.go +++ b/data/rewardTx/rewardTx_test.go @@ -27,6 +27,7 @@ func TestRewardTx_GettersAndSetters(t *testing.T) { assert.Equal(t, uint64(0), rwdTx.GetGasLimit()) assert.Equal(t, uint64(0), rwdTx.GetGasPrice()) assert.Equal(t, uint64(0), rwdTx.GetNonce()) + assert.Nil(t, rwdTx.GetUserTransaction()) } func TestRewardTx_CheckIntegrityShouldWork(t *testing.T) { diff --git a/data/smartContractResult/smartContractResult.go b/data/smartContractResult/smartContractResult.go index 2c248176..198aac81 100644 --- a/data/smartContractResult/smartContractResult.go +++ b/data/smartContractResult/smartContractResult.go @@ -39,6 +39,11 @@ func (_ *SmartContractResult) GetRcvUserName() []byte { return nil } +// GetUserTransaction returns nil as SmartContractResult does not have user transaction +func (scr *SmartContractResult) GetUserTransaction() data.TransactionHandler { + return nil +} + // TrimSlicePtr creates a copy of the provided slice without the excess capacity func TrimSlicePtr(in []*SmartContractResult) []*SmartContractResult { if len(in) == 0 { diff --git a/data/smartContractResult/smartContractResult_test.go b/data/smartContractResult/smartContractResult_test.go index 75eba828..d477b630 100644 --- a/data/smartContractResult/smartContractResult_test.go +++ b/data/smartContractResult/smartContractResult_test.go @@ -38,6 +38,7 @@ func TestSmartContractResult_SettersAndGetters(t *testing.T) { assert.Equal(t, gasLimit, scr.GetGasLimit()) assert.Equal(t, gasPrice, scr.GetGasPrice()) assert.Equal(t, nonce, scr.GetNonce()) + assert.Nil(t, scr.GetUserTransaction()) } func TestTrimSlicePtr(t *testing.T) { diff --git a/data/transaction/apiTransactionResult.go b/data/transaction/apiTransactionResult.go index ca9671b1..20e86eb1 100644 --- a/data/transaction/apiTransactionResult.go +++ b/data/transaction/apiTransactionResult.go @@ -69,6 +69,7 @@ type ApiTransactionResult struct { Options uint32 `json:"options"` GuardianAddr string `json:"guardian,omitempty"` GuardianSignature string `json:"guardianSignature,omitempty"` + InnerTransaction data.TransactionHandler `json:"innerTransaction,omitempty"` } // ApiSmartContractResult represents a smart contract result with changed fields' types in order to make it friendly for API's json diff --git a/data/transaction/frontendTransaction.go b/data/transaction/frontendTransaction.go index b5b55c2a..820014f7 100644 --- a/data/transaction/frontendTransaction.go +++ b/data/transaction/frontendTransaction.go @@ -2,19 +2,21 @@ package transaction // FrontendTransaction represents the DTO used in transaction signing/validation. type FrontendTransaction struct { - Nonce uint64 `json:"nonce"` - Value string `json:"value"` - Receiver string `json:"receiver"` - Sender string `json:"sender"` - SenderUsername []byte `json:"senderUsername,omitempty"` - ReceiverUsername []byte `json:"receiverUsername,omitempty"` - GasPrice uint64 `json:"gasPrice"` - GasLimit uint64 `json:"gasLimit"` - Data []byte `json:"data,omitempty"` - Signature string `json:"signature,omitempty"` - ChainID string `json:"chainID"` - Version uint32 `json:"version"` - Options uint32 `json:"options,omitempty"` - GuardianAddr string `json:"guardian,omitempty"` - GuardianSignature string `json:"guardianSignature,omitempty"` + Nonce uint64 `json:"nonce"` + Value string `json:"value"` + Receiver string `json:"receiver"` + Sender string `json:"sender"` + SenderUsername []byte `json:"senderUsername,omitempty"` + ReceiverUsername []byte `json:"receiverUsername,omitempty"` + GasPrice uint64 `json:"gasPrice"` + GasLimit uint64 `json:"gasLimit"` + Data []byte `json:"data,omitempty"` + Signature string `json:"signature,omitempty"` + ChainID string `json:"chainID"` + Version uint32 `json:"version"` + Options uint32 `json:"options,omitempty"` + GuardianAddr string `json:"guardian,omitempty"` + GuardianSignature string `json:"guardianSignature,omitempty"` + Relayer string `json:"relayer,omitempty"` + InnerTransaction *FrontendTransaction `json:"innerTransaction,omitempty"` } diff --git a/data/transaction/transaction.go b/data/transaction/transaction.go index b5b42531..b261a7fa 100644 --- a/data/transaction/transaction.go +++ b/data/transaction/transaction.go @@ -2,6 +2,7 @@ package transaction import ( + "encoding/hex" "math/big" "github.com/multiversx/mx-chain-core-go/core" @@ -36,6 +37,11 @@ func (tx *Transaction) SetSndAddr(addr []byte) { tx.SndAddr = addr } +// GetUserTransaction returns the inner transaction +func (tx *Transaction) GetUserTransaction() data.TransactionHandler { + return tx.GetInnerTransaction() +} + // TrimSlicePtr creates a copy of the provided slice without the excess capacity func TrimSlicePtr(in []*Transaction) []*Transaction { if len(in) == 0 { @@ -68,38 +74,19 @@ func (tx *Transaction) GetDataForSigning(encoder data.Encoder, marshaller data.M return nil, ErrNilHasher } - receiverAddr, err := encoder.Encode(tx.RcvAddr) + ftx, err := tx.prepareTx(encoder) if err != nil { return nil, err } - senderAddr, err := encoder.Encode(tx.SndAddr) - if err != nil { - return nil, err - } - - ftx := &FrontendTransaction{ - Nonce: tx.Nonce, - Value: tx.Value.String(), - Receiver: receiverAddr, - Sender: senderAddr, - GasPrice: tx.GasPrice, - GasLimit: tx.GasLimit, - SenderUsername: tx.SndUserName, - ReceiverUsername: tx.RcvUserName, - Data: tx.Data, - ChainID: string(tx.ChainID), - Version: tx.Version, - Options: tx.Options, - } - - if len(tx.GuardianAddr) > 0 { - guardianAddr, errGuardian := encoder.Encode(tx.GuardianAddr) - if errGuardian != nil { - return nil, errGuardian + if tx.InnerTransaction != nil { + ftx.InnerTransaction, err = tx.InnerTransaction.prepareTx(encoder) + if err != nil { + return nil, err } - ftx.GuardianAddr = guardianAddr + ftx.InnerTransaction.Signature = hex.EncodeToString(tx.InnerTransaction.Signature) + ftx.InnerTransaction.GuardianSignature = hex.EncodeToString(tx.InnerTransaction.GuardianSignature) } ftxBytes, err := marshaller.Marshal(ftx) @@ -147,3 +134,50 @@ func (tx *Transaction) CheckIntegrity() error { return nil } + +func (tx *Transaction) prepareTx(encoder data.Encoder) (*FrontendTransaction, error) { + receiverAddr, err := encoder.Encode(tx.RcvAddr) + if err != nil { + return nil, err + } + + senderAddr, err := encoder.Encode(tx.SndAddr) + if err != nil { + return nil, err + } + + ftx := &FrontendTransaction{ + Nonce: tx.Nonce, + Value: tx.Value.String(), + Receiver: receiverAddr, + Sender: senderAddr, + GasPrice: tx.GasPrice, + GasLimit: tx.GasLimit, + SenderUsername: tx.SndUserName, + ReceiverUsername: tx.RcvUserName, + Data: tx.Data, + ChainID: string(tx.ChainID), + Version: tx.Version, + Options: tx.Options, + } + + if len(tx.RelayerAddr) > 0 { + relayerAddr, errRelayer := encoder.Encode(tx.RelayerAddr) + if errRelayer != nil { + return nil, errRelayer + } + + ftx.Relayer = relayerAddr + } + + if len(tx.GuardianAddr) > 0 { + guardianAddr, errGuardian := encoder.Encode(tx.GuardianAddr) + if errGuardian != nil { + return nil, errGuardian + } + + ftx.GuardianAddr = guardianAddr + } + + return ftx, nil +} diff --git a/data/transaction/transaction.pb.go b/data/transaction/transaction.pb.go index a8b2dd87..b19e9438 100644 --- a/data/transaction/transaction.pb.go +++ b/data/transaction/transaction.pb.go @@ -45,6 +45,8 @@ type Transaction struct { Options uint32 `protobuf:"varint,13,opt,name=Options,proto3" json:"options,omitempty"` GuardianAddr []byte `protobuf:"bytes,14,opt,name=GuardianAddr,proto3" json:"guardian,omitempty"` GuardianSignature []byte `protobuf:"bytes,15,opt,name=GuardianSignature,proto3" json:"guardianSignature,omitempty"` + RelayerAddr []byte `protobuf:"bytes,16,opt,name=RelayerAddr,proto3" json:"relayer,omitempty"` + InnerTransaction *Transaction `protobuf:"bytes,17,opt,name=InnerTransaction,proto3" json:"innerTransaction,omitempty"` } func (m *Transaction) Reset() { *m = Transaction{} } @@ -180,6 +182,20 @@ func (m *Transaction) GetGuardianSignature() []byte { return nil } +func (m *Transaction) GetRelayerAddr() []byte { + if m != nil { + return m.RelayerAddr + } + return nil +} + +func (m *Transaction) GetInnerTransaction() *Transaction { + if m != nil { + return m.InnerTransaction + } + return nil +} + func init() { proto.RegisterType((*Transaction)(nil), "proto.Transaction") } @@ -187,43 +203,47 @@ func init() { func init() { proto.RegisterFile("transaction.proto", fileDescriptor_2cc4e03d2c28c490) } var fileDescriptor_2cc4e03d2c28c490 = []byte{ - // 573 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0x31, 0x6f, 0xd4, 0x3e, - 0x18, 0xc6, 0xe3, 0xff, 0xbf,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 0x84, 0x04, 0x04, 0x00, 0x00, + // 626 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0xcf, 0x6e, 0xd3, 0x3e, + 0x1c, 0xaf, 0x7f, 0xbf, 0x75, 0xd9, 0xdc, 0x6e, 0xac, 0x46, 0x83, 0x30, 0x24, 0xbb, 0x42, 0x30, + 0xf5, 0x40, 0x1b, 0x69, 0x08, 0x21, 0xb1, 0x13, 0xdd, 0xd0, 0x54, 0x09, 0x06, 0xca, 0x60, 0x07, + 0x0e, 0x48, 0x6e, 0x62, 0x52, 0x4b, 0x8b, 0x3d, 0x39, 0x6e, 0xb5, 0x1d, 0x90, 0x78, 0x04, 0x1e, + 0x03, 0xf1, 0x24, 0x88, 0xd3, 0x8e, 0x3b, 0x05, 0x96, 0x5d, 0x50, 0x4e, 0x7b, 0x04, 0x14, 0xa7, + 0x5d, 0xbc, 0xed, 0xc4, 0xa9, 0xf1, 0xe7, 0xef, 0xb7, 0x8e, 0x1d, 0xd8, 0xd2, 0x8a, 0x8a, 0x84, + 0x06, 0x9a, 0x4b, 0xd1, 0x3b, 0x54, 0x52, 0x4b, 0x54, 0x37, 0x3f, 0x6b, 0xdd, 0x88, 0xeb, 0xd1, + 0x78, 0xd8, 0x0b, 0x64, 0xec, 0x45, 0x32, 0x92, 0x9e, 0x81, 0x87, 0xe3, 0x4f, 0x66, 0x65, 0x16, + 0xe6, 0xa9, 0x74, 0x3d, 0xf8, 0xe9, 0xc0, 0xc6, 0xbb, 0x2a, 0x0b, 0x11, 0x58, 0xdf, 0x95, 0x22, + 0x60, 0x2e, 0x68, 0x83, 0xce, 0x5c, 0x7f, 0x31, 0x4f, 0x49, 0x5d, 0x14, 0x80, 0x5f, 0xe2, 0x68, + 0x04, 0xeb, 0xfb, 0xf4, 0x60, 0xcc, 0xdc, 0xff, 0xda, 0xa0, 0xd3, 0xec, 0xfb, 0x85, 0x60,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0xa7, 0x64, 0x8d, 0x5f, 0xd3, + 0x5b, 0xd1, 0x37, 0xb2, 0xfa, 0x9f, 0x4f, 0xce, 0x70, 0xed, 0xf4, 0x0c, 0xd7, 0x2e, 0xce, 0x30, + 0xf8, 0x92, 0x61, 0xf0, 0x2d, 0xc3, 0xe0, 0x47, 0x86, 0xc1, 0x49, 0x86, 0xc1, 0x69, 0x86, 0xc1, + 0xef, 0x0c, 0x83, 0x3f, 0x19, 0xae, 0x5d, 0x64, 0x18, 0x7c, 0x3d, 0xc7, 0xb5, 0x93, 0x73, 0x5c, + 0x3b, 0x3d, 0xc7, 0xb5, 0x0f, 0x5b, 0xff, 0x70, 0x59, 0x3d, 0xeb, 0xdb, 0xb3, 0x69, 0x3d, 0x0f, + 0xe7, 0xcd, 0x7f, 0x78, 0xf2, 0x37, 0x00, 0x00, 0xff, 0xff, 0x2a, 0xdc, 0x62, 0xaa, 0x9d, 0x04, + 0x00, 0x00, } func (this *Transaction) Equal(that interface{}) bool { @@ -293,13 +313,19 @@ func (this *Transaction) Equal(that interface{}) bool { if !bytes.Equal(this.GuardianSignature, that1.GuardianSignature) { return false } + if !bytes.Equal(this.RelayerAddr, that1.RelayerAddr) { + return false + } + if !this.InnerTransaction.Equal(that1.InnerTransaction) { + return false + } return true } func (this *Transaction) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 19) + s := make([]string, 0, 21) s = append(s, "&transaction.Transaction{") s = append(s, "Nonce: "+fmt.Sprintf("%#v", this.Nonce)+",\n") s = append(s, "Value: "+fmt.Sprintf("%#v", this.Value)+",\n") @@ -316,6 +342,10 @@ func (this *Transaction) GoString() string { s = append(s, "Options: "+fmt.Sprintf("%#v", this.Options)+",\n") s = append(s, "GuardianAddr: "+fmt.Sprintf("%#v", this.GuardianAddr)+",\n") s = append(s, "GuardianSignature: "+fmt.Sprintf("%#v", this.GuardianSignature)+",\n") + s = append(s, "RelayerAddr: "+fmt.Sprintf("%#v", this.RelayerAddr)+",\n") + if this.InnerTransaction != nil { + s = append(s, "InnerTransaction: "+fmt.Sprintf("%#v", this.InnerTransaction)+",\n") + } s = append(s, "}") return strings.Join(s, "") } @@ -347,6 +377,29 @@ func (m *Transaction) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.InnerTransaction != nil { + { + size, err := m.InnerTransaction.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTransaction(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x8a + } + if len(m.RelayerAddr) > 0 { + i -= len(m.RelayerAddr) + copy(dAtA[i:], m.RelayerAddr) + i = encodeVarintTransaction(dAtA, i, uint64(len(m.RelayerAddr))) + i-- + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x82 + } if len(m.GuardianSignature) > 0 { i -= len(m.GuardianSignature) copy(dAtA[i:], m.GuardianSignature) @@ -522,6 +575,14 @@ func (m *Transaction) Size() (n int) { if l > 0 { n += 1 + l + sovTransaction(uint64(l)) } + l = len(m.RelayerAddr) + if l > 0 { + n += 2 + l + sovTransaction(uint64(l)) + } + if m.InnerTransaction != nil { + l = m.InnerTransaction.Size() + n += 2 + l + sovTransaction(uint64(l)) + } return n } @@ -551,6 +612,8 @@ func (this *Transaction) String() string { `Options:` + fmt.Sprintf("%v", this.Options) + `,`, `GuardianAddr:` + fmt.Sprintf("%v", this.GuardianAddr) + `,`, `GuardianSignature:` + fmt.Sprintf("%v", this.GuardianSignature) + `,`, + `RelayerAddr:` + fmt.Sprintf("%v", this.RelayerAddr) + `,`, + `InnerTransaction:` + strings.Replace(this.InnerTransaction.String(), "Transaction", "Transaction", 1) + `,`, `}`, }, "") return s @@ -1031,6 +1094,76 @@ func (m *Transaction) Unmarshal(dAtA []byte) error { m.GuardianSignature = []byte{} } iNdEx = postIndex + case 16: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field RelayerAddr", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTransaction + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthTransaction + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthTransaction + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.RelayerAddr = append(m.RelayerAddr[:0], dAtA[iNdEx:postIndex]...) + if m.RelayerAddr == nil { + m.RelayerAddr = []byte{} + } + iNdEx = postIndex + case 17: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field InnerTransaction", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTransaction + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTransaction + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTransaction + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.InnerTransaction == nil { + m.InnerTransaction = &Transaction{} + } + if err := m.InnerTransaction.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTransaction(dAtA[iNdEx:]) diff --git a/data/transaction/transaction.proto b/data/transaction/transaction.proto index 2e8e168d..626e6e43 100644 --- a/data/transaction/transaction.proto +++ b/data/transaction/transaction.proto @@ -15,19 +15,21 @@ import "github.com/gogo/protobuf/gogoproto/gogo.proto"; // Transaction holds all the data needed for a value transfer or SC call message Transaction { - uint64 Nonce = 1 [(gogoproto.jsontag) = "nonce"]; - bytes Value = 2 [(gogoproto.jsontag) = "value", (gogoproto.casttypewith) = "math/big.Int;github.com/multiversx/mx-chain-core-go/data.BigIntCaster"]; - bytes RcvAddr = 3 [(gogoproto.jsontag) = "receiver"]; - bytes RcvUserName = 4 [(gogoproto.jsontag) = "rcvUserName,omitempty"]; - bytes SndAddr = 5 [(gogoproto.jsontag) = "sender"]; - bytes SndUserName = 6 [(gogoproto.jsontag) = "sndUserName,omitempty"]; - uint64 GasPrice = 7 [(gogoproto.jsontag) = "gasPrice,omitempty"]; - uint64 GasLimit = 8 [(gogoproto.jsontag) = "gasLimit,omitempty"]; - bytes Data = 9 [(gogoproto.jsontag) = "data,omitempty"]; - bytes ChainID = 10 [(gogoproto.jsontag) = "chainID"]; - uint32 Version = 11 [(gogoproto.jsontag) = "version"]; - bytes Signature = 12 [(gogoproto.jsontag) = "signature,omitempty"]; - uint32 Options = 13 [(gogoproto.jsontag) = "options,omitempty"]; - bytes GuardianAddr = 14 [(gogoproto.jsontag) = "guardian,omitempty"]; - bytes GuardianSignature = 15 [(gogoproto.jsontag) = "guardianSignature,omitempty"]; + uint64 Nonce = 1 [(gogoproto.jsontag) = "nonce"]; + bytes Value = 2 [(gogoproto.jsontag) = "value", (gogoproto.casttypewith) = "math/big.Int;github.com/multiversx/mx-chain-core-go/data.BigIntCaster"]; + bytes RcvAddr = 3 [(gogoproto.jsontag) = "receiver"]; + bytes RcvUserName = 4 [(gogoproto.jsontag) = "rcvUserName,omitempty"]; + bytes SndAddr = 5 [(gogoproto.jsontag) = "sender"]; + bytes SndUserName = 6 [(gogoproto.jsontag) = "sndUserName,omitempty"]; + uint64 GasPrice = 7 [(gogoproto.jsontag) = "gasPrice,omitempty"]; + uint64 GasLimit = 8 [(gogoproto.jsontag) = "gasLimit,omitempty"]; + bytes Data = 9 [(gogoproto.jsontag) = "data,omitempty"]; + bytes ChainID = 10 [(gogoproto.jsontag) = "chainID"]; + uint32 Version = 11 [(gogoproto.jsontag) = "version"]; + bytes Signature = 12 [(gogoproto.jsontag) = "signature,omitempty"]; + uint32 Options = 13 [(gogoproto.jsontag) = "options,omitempty"]; + bytes GuardianAddr = 14 [(gogoproto.jsontag) = "guardian,omitempty"]; + bytes GuardianSignature = 15 [(gogoproto.jsontag) = "guardianSignature,omitempty"]; + bytes RelayerAddr = 16 [(gogoproto.jsontag) = "relayer,omitempty"]; + Transaction InnerTransaction = 17 [(gogoproto.jsontag) = "innerTransaction,omitempty"]; } diff --git a/data/transaction/transaction_test.go b/data/transaction/transaction_test.go index 02306653..ce8e70ae 100644 --- a/data/transaction/transaction_test.go +++ b/data/transaction/transaction_test.go @@ -23,11 +23,16 @@ func TestTransaction_SettersAndGetters(t *testing.T) { gasLimit := uint64(5) sender := []byte("sndr") receiver := []byte("receiver") + innerTx := &transaction.Transaction{ + Nonce: 123, + RelayerAddr: sender, + } tx := &transaction.Transaction{ - Nonce: nonce, - GasPrice: gasPrice, - GasLimit: gasLimit, + Nonce: nonce, + GasPrice: gasPrice, + GasLimit: gasLimit, + InnerTransaction: innerTx, } assert.False(t, check.IfNil(tx)) @@ -43,6 +48,8 @@ func TestTransaction_SettersAndGetters(t *testing.T) { assert.Equal(t, gasLimit, tx.GetGasLimit()) assert.Equal(t, sender, tx.GetSndAddr()) assert.Equal(t, receiver, tx.GetRcvAddr()) + assert.Equal(t, innerTx, tx.GetUserTransaction()) + assert.Equal(t, sender, tx.GetInnerTransaction().GetRelayerAddr()) } func TestTransaction_MarshalUnmarshalJsonShouldWork(t *testing.T) { @@ -58,6 +65,9 @@ func TestTransaction_MarshalUnmarshalJsonShouldWork(t *testing.T) { GasLimit: 5678, Data: []byte("data"), Signature: []byte("signature"), + InnerTransaction: &transaction.Transaction{ + Nonce: 123, + }, } buff, err := json.Marshal(tx) @@ -182,6 +192,9 @@ func TestTransaction_GetDataForSigningMarshalizerErrShouldErr(t *testing.T) { MarshalCalled: func(obj interface{}) (bytes []byte, err error) { return nil, expectedErr }, + UnmarshalCalled: func(obj interface{}, buff []byte) error { + return nil + }, }, &mock.HasherMock{}, ) @@ -220,7 +233,7 @@ func TestTransaction_GetDataForSigningShouldWork(t *testing.T) { tx := &transaction.Transaction{} numEncodeCalled := 0 - marshalizerWasCalled := false + marshallWasCalled := false hasherWasCalled := false buff, err := tx.GetDataForSigning( &mock.PubkeyConverterStub{ @@ -231,7 +244,7 @@ func TestTransaction_GetDataForSigningShouldWork(t *testing.T) { }, &mock.MarshalizerStub{ MarshalCalled: func(obj interface{}) (bytes []byte, err error) { - marshalizerWasCalled = true + marshallWasCalled = true return make([]byte, 0), nil }, @@ -247,11 +260,59 @@ func TestTransaction_GetDataForSigningShouldWork(t *testing.T) { assert.Equal(t, 0, len(buff)) assert.Nil(t, err) - assert.True(t, marshalizerWasCalled) + assert.True(t, marshallWasCalled) assert.False(t, hasherWasCalled) assert.Equal(t, 2, numEncodeCalled) }) + t.Run("inner tx, without hash sign option set", func(t *testing.T) { + t.Parallel() + + tx1 := &transaction.Transaction{ + Nonce: 1, + } + tx := &transaction.Transaction{ + Nonce: 3, + InnerTransaction: tx1, + } + + numEncodeCalled := 0 + marshallWasCalled := false + hasherWasCalled := false + buff, err := tx.GetDataForSigning( + &mock.PubkeyConverterStub{ + EncodeCalled: func(pkBytes []byte) (string, error) { + numEncodeCalled++ + return "", nil + }, + }, + &mock.MarshalizerStub{ + MarshalCalled: func(obj interface{}) (bytes []byte, err error) { + marshallWasCalled = true + + return json.Marshal(obj) + }, + }, + &mock.HasherStub{ + ComputeCalled: func(s string) []byte { + hasherWasCalled = true + + return make([]byte, 0) + }, + }, + ) + + assert.Nil(t, err) + + var ftx transaction.FrontendTransaction + err = json.Unmarshal(buff, &ftx) + assert.Nil(t, err) + + assert.True(t, marshallWasCalled) + assert.False(t, hasherWasCalled) + assert.Equal(t, 4, numEncodeCalled) + }) + t.Run("with hash sign option set", func(t *testing.T) { t.Parallel() @@ -260,7 +321,7 @@ func TestTransaction_GetDataForSigningShouldWork(t *testing.T) { tx.Options ^= transaction.MaskSignedWithHash numEncodeCalled := 0 - marshalizerWasCalled := false + marshallWasCalled := false hasherWasCalled := false expectedHash := []byte("expectedHash") buff, err := tx.GetDataForSigning( @@ -272,7 +333,7 @@ func TestTransaction_GetDataForSigningShouldWork(t *testing.T) { }, &mock.MarshalizerStub{ MarshalCalled: func(obj interface{}) (bytes []byte, err error) { - marshalizerWasCalled = true + marshallWasCalled = true return make([]byte, 0), nil }, @@ -287,7 +348,7 @@ func TestTransaction_GetDataForSigningShouldWork(t *testing.T) { ) assert.Nil(t, err) - assert.True(t, marshalizerWasCalled) + assert.True(t, marshallWasCalled) assert.True(t, hasherWasCalled) assert.Equal(t, expectedHash, buff) assert.Equal(t, 2, numEncodeCalled)