Skip to content

Commit

Permalink
Merge branch 'main' into merge-main-into-feat-state-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Dec 12, 2024
2 parents dc0120c + a0e4044 commit 38f5d43
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 383 deletions.
20 changes: 19 additions & 1 deletion data/esdt/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func IsValidPrefixedToken(token string) (string, bool) {
}

tokenRandSeq := tokenSplit[2]
if !(len(tokenRandSeq) >= esdtTickerNumRandChars) {
if !IsRandomSeqValid(tokenRandSeq) {
return "", false
}

Expand Down Expand Up @@ -83,3 +83,21 @@ func IsTickerValid(ticker string) bool {
func IsTokenTickerLenCorrect(tokenTickerLen int) bool {
return !(tokenTickerLen < minLengthForTickerName || tokenTickerLen > maxLengthForTickerName)
}

// IsRandomSeqValid checks if the token random sequence is valid
func IsRandomSeqValid(randomSeq string) bool {
if len(randomSeq) != esdtTickerNumRandChars {
return false
}

for _, ch := range randomSeq {
isSmallCharacter := ch >= 'a' && ch <= 'f'
isNumber := ch >= '0' && ch <= '9'
isReadable := isSmallCharacter || isNumber
if !isReadable {
return false
}
}

return true
}
10 changes: 5 additions & 5 deletions data/esdt/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (
)

func TestIsValidPrefixedToken(t *testing.T) {
prefix, valid := IsValidPrefixedToken("sov1-TKN-coffee")
prefix, valid := IsValidPrefixedToken("sov1-TKN-c0ffee")
require.True(t, valid)
require.Equal(t, "sov1", prefix)

prefix, valid = IsValidPrefixedToken("sOv1-TKN-coffee")
prefix, valid = IsValidPrefixedToken("sOv1-TKN-c0ffee")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("sov1-TkN-coffee")
prefix, valid = IsValidPrefixedToken("sov1-TkN-c0ffee")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("sov1-TKN-coffe")
prefix, valid = IsValidPrefixedToken("sov1-TKN-c0ffe")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("sov1-TKN")
require.False(t, valid)
require.Equal(t, "", prefix)

prefix, valid = IsValidPrefixedToken("TKN-coffee")
prefix, valid = IsValidPrefixedToken("TKN-c0ffee")
require.False(t, valid)
require.Equal(t, "", prefix)
}
Expand Down
2 changes: 0 additions & 2 deletions data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ type TransactionHandler interface {
GetSndAddr() []byte
GetGasLimit() uint64
GetGasPrice() uint64
GetUserTransactions() []TransactionHandler

SetValue(*big.Int)
SetData([]byte)
Expand Down Expand Up @@ -399,7 +398,6 @@ type TransactionWithFeeHandler interface {
GetData() []byte
GetRcvAddr() []byte
GetValue() *big.Int
GetUserTransactions() []TransactionHandler
}

// UserAccountHandler models a user account
Expand Down
5 changes: 0 additions & 5 deletions data/receipt/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ func (_ *Receipt) GetRcvUserName() []byte {
return nil
}

// GetUserTransactions returns nil as Receipt does not have user transactions
func (rpt *Receipt) GetUserTransactions() []data.TransactionHandler {
return nil
}

// CheckIntegrity checks for not nil fields and negative value
func (rpt *Receipt) CheckIntegrity() error {
return nil
Expand Down
1 change: 0 additions & 1 deletion data/receipt/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ 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.GetUserTransactions())
}

func TestReceipt_CheckIntegrityReturnsNil(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions data/rewardTx/rewardTx.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ func (rtx *RewardTx) GetRcvUserName() []byte {
return nil
}

// GetUserTransactions returns nil as RewardTx does not have user transactions
func (rtx *RewardTx) GetUserTransactions() []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: 0 additions & 1 deletion data/rewardTx/rewardTx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ 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.GetUserTransactions())
}

func TestRewardTx_CheckIntegrityShouldWork(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions data/smartContractResult/smartContractResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ func (_ *SmartContractResult) GetRcvUserName() []byte {
return nil
}

// GetUserTransactions returns nil as SmartContractResult does not have user transactions
func (scr *SmartContractResult) GetUserTransactions() []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: 0 additions & 1 deletion data/smartContractResult/smartContractResult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ 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.GetUserTransactions())
}

func TestTrimSlicePtr(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion data/transaction/apiTransactionResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ type ApiTransactionResult struct {
Options uint32 `json:"options"`
GuardianAddr string `json:"guardian,omitempty"`
GuardianSignature string `json:"guardianSignature,omitempty"`
InnerTransactions []*FrontendTransaction `json:"innerTransactions,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: 15 additions & 17 deletions data/transaction/frontendTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ 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"`
Relayer string `json:"relayer,omitempty"`
InnerTransactions []*FrontendTransaction `json:"innerTransactions,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"`
}
96 changes: 25 additions & 71 deletions data/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package transaction

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

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -37,17 +36,6 @@ func (tx *Transaction) SetSndAddr(addr []byte) {
tx.SndAddr = addr
}

// GetUserTransactions returns the inner transactions
func (tx *Transaction) GetUserTransactions() []data.TransactionHandler {
innerTxs := tx.GetInnerTransactions()
txsHandlers := make([]data.TransactionHandler, len(innerTxs))
for i := 0; i < len(innerTxs); i++ {
txsHandlers[i] = innerTxs[i]
}

return txsHandlers
}

// 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 @@ -80,25 +68,38 @@ func (tx *Transaction) GetDataForSigning(encoder data.Encoder, marshaller data.M
return nil, ErrNilHasher
}

ftx, err := tx.prepareTx(encoder)
receiverAddr, err := encoder.Encode(tx.RcvAddr)
if err != nil {
return nil, err
}

numInnerTxs := len(tx.InnerTransactions)
if numInnerTxs > 0 {
ftx.InnerTransactions = make([]*FrontendTransaction, numInnerTxs)
var errPrepare error
for i := 0; i < numInnerTxs; i++ {
ftx.InnerTransactions[i], errPrepare = tx.InnerTransactions[i].prepareTx(encoder)
if errPrepare != nil {
return nil, errPrepare
}
senderAddr, err := encoder.Encode(tx.SndAddr)
if err != nil {
return nil, err
}

ftx.InnerTransactions[i].Signature = hex.EncodeToString(tx.InnerTransactions[i].Signature)
ftx.InnerTransactions[i].GuardianSignature = hex.EncodeToString(tx.InnerTransactions[i].GuardianSignature)
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
}

ftx.GuardianAddr = guardianAddr
}

ftxBytes, err := marshaller.Marshal(ftx)
Expand Down Expand Up @@ -146,50 +147,3 @@ 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

0 comments on commit 38f5d43

Please sign in to comment.