Skip to content

Commit

Permalink
fix receiver shard of transaction from an invalid miniblock
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed Jan 25, 2023
1 parent 53391ec commit b330365
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
8 changes: 7 additions & 1 deletion process/elasticproc/transactions/transactionDBBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/sharding"
coreData "github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/receipt"
Expand Down Expand Up @@ -46,6 +47,11 @@ func (dtb *dbTransactionBuilder) prepareTransaction(
isScCall := core.IsSmartContractAddress(tx.RcvAddr)
res := dtb.dataFieldParser.Parse(tx.Data, tx.SndAddr, tx.RcvAddr, numOfShards)

receiverShardID := mb.ReceiverShardID
if mb.Type == block.InvalidBlock {
receiverShardID = sharding.ComputeShardID(tx.RcvAddr, numOfShards)
}

return &data.Transaction{
Hash: hex.EncodeToString(txHash),
MBHash: hex.EncodeToString(mbHash),
Expand All @@ -54,7 +60,7 @@ func (dtb *dbTransactionBuilder) prepareTransaction(
Value: tx.Value.String(),
Receiver: dtb.addressPubkeyConverter.Encode(tx.RcvAddr),
Sender: dtb.addressPubkeyConverter.Encode(tx.SndAddr),
ReceiverShard: mb.ReceiverShardID,
ReceiverShard: receiverShardID,
SenderShard: mb.SenderShardID,
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
Expand Down
57 changes: 57 additions & 0 deletions process/elasticproc/transactions/transactionDBBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,60 @@ func TestGetTransactionByType_RewardTx(t *testing.T) {

require.Equal(t, expectedTx, resultTx)
}

func TestGetMoveBalanceTransactionInvalid(t *testing.T) {
t.Parallel()

txHash := []byte("txHash")
mbHash := []byte("mbHash")
mb := &block.MiniBlock{TxHashes: [][]byte{txHash}, Type: block.InvalidBlock}
header := &block.Header{Nonce: 2}
status := transaction.TxStatusInvalid.String()
gasPrice := uint64(1000)
gasLimit := uint64(1000)
cp := createCommonProcessor()

tx := &transaction.Transaction{
Nonce: 1,
Value: big.NewInt(1000),
RcvAddr: []byte("receiver"),
SndAddr: []byte("sender"),
GasPrice: gasPrice,
GasLimit: gasLimit,
Data: []byte("data"),
ChainID: []byte("1"),
Version: 1,
Signature: []byte("signature"),
RcvUserName: []byte("rcv"),
SndUserName: []byte("snd"),
}

expectedTx := &data.Transaction{
Hash: hex.EncodeToString(txHash),
MBHash: hex.EncodeToString(mbHash),
Nonce: tx.Nonce,
Round: header.Round,
Value: tx.Value.String(),
Receiver: cp.addressPubkeyConverter.Encode(tx.RcvAddr),
Sender: cp.addressPubkeyConverter.Encode(tx.SndAddr),
ReceiverShard: uint32(2),
SenderShard: mb.SenderShardID,
GasPrice: gasPrice,
GasLimit: gasLimit,
GasUsed: uint64(500),
InitialPaidFee: "100",
Data: tx.Data,
Signature: hex.EncodeToString(tx.Signature),
Timestamp: time.Duration(header.GetTimeStamp()),
Status: status,
Fee: "100",
ReceiverUserName: []byte("rcv"),
SenderUserName: []byte("snd"),
Operation: "transfer",
Version: 1,
Receivers: []string{},
}

dbTx := cp.prepareTransaction(tx, txHash, mbHash, mb, header, status, big.NewInt(100), 500, big.NewInt(100), 3)
require.Equal(t, expectedTx, dbTx)
}

0 comments on commit b330365

Please sign in to comment.