Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner transactions on main transaction #260

Merged
merged 15 commits into from
Oct 11, 2023
1 change: 1 addition & 0 deletions data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ type TransactionHandler interface {
GetSndAddr() []byte
GetGasLimit() uint64
GetGasPrice() uint64
GetUserTransaction() TransactionHandler

SetValue(*big.Int)
SetData([]byte)
Expand Down
5 changes: 5 additions & 0 deletions data/receipt/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions data/receipt/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions data/rewardTx/rewardTx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions data/rewardTx/rewardTx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions data/smartContractResult/smartContractResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions data/smartContractResult/smartContractResult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions data/transaction/apiTransactionResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 17 additions & 15 deletions data/transaction/frontendTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
86 changes: 60 additions & 26 deletions data/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package transaction

import (
"encoding/hex"
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Loading