Skip to content

Commit

Permalink
Merge pull request #281 from multiversx/change-indexing-mechanism-for…
Browse files Browse the repository at this point in the history
…-cross-shard-esdt-transfer

New indexing mechanism for cross-shard esdt transfer
  • Loading branch information
miiu96 authored Jun 6, 2024
2 parents d125bb0 + 6c5d2db commit 6608ee8
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 4 deletions.
200 changes: 200 additions & 0 deletions integrationtests/esdtTransfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,203 @@ func TestESDTTransferTooMuchGasProvided(t *testing.T) {

require.JSONEq(t, readExpectedResult("./testdata/esdtTransfer/esdt-transfer.json"), string(genericResponse.Docs[0].Source))
}

func TestESDTTransferCrossShardWithRefundOnSourceShard(t *testing.T) {
esClient, err := createESClient(esURL)
require.Nil(t, err)

esProc, err := CreateElasticProcessor(esClient)
require.Nil(t, err)

txHash := []byte("esdtTransferCross")
header := &dataBlock.Header{
Round: 50,
TimeStamp: 10101,
ShardID: 2,
}

scrHash1 := []byte("scrHash1ESDTTransferOnSourceShard")
body := &dataBlock.Body{
MiniBlocks: dataBlock.MiniBlockSlice{
{
Type: dataBlock.TxBlock,
SenderShardID: 2,
ReceiverShardID: 1,
TxHashes: [][]byte{txHash},
},
{
Type: dataBlock.SmartContractResultBlock,
SenderShardID: 2,
ReceiverShardID: 2,
TxHashes: [][]byte{scrHash1},
},
},
}

address1 := "erd1ef6470tjdtlgpa9f6g3ae4nsedmjg0gv6w73v32xtvhkfff993hq750xl9"
address2 := "erd13u7zyekzvdvzek8768r5gau9p6677ufppsjuklu9e6t7yx7rhg4s68e2ze"

txESDT := outport.TxInfo{
Transaction: &transaction.Transaction{
Nonce: 1,
SndAddr: decodeAddress(address1),
RcvAddr: decodeAddress(address2),
GasLimit: 500_000,
GasPrice: 1000000000,
Data: []byte("ESDTTransfer@54474e2d383862383366@0a"),
Value: big.NewInt(0),
},
FeeInfo: &outport.FeeInfo{
GasUsed: 334_000,
Fee: big.NewInt(136000000000000),
InitialPaidFee: big.NewInt(137660000000000),
},
}

refundValueBig, _ := big.NewInt(0).SetString("1660000000000", 10)
scrRefund := &smartContractResult.SmartContractResult{
Nonce: 20,
SndAddr: decodeAddress(address2),
RcvAddr: decodeAddress(address1),
PrevTxHash: txHash,
OriginalTxHash: txHash,
Value: refundValueBig,
Data: []byte("@6f6b"),
}

pool := &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
hex.EncodeToString(txHash): &txESDT,
},
SmartContractResults: map[string]*outport.SCRInfo{
hex.EncodeToString(scrHash1): {SmartContractResult: scrRefund, FeeInfo: &outport.FeeInfo{}},
},
}
err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

ids := []string{hex.EncodeToString(txHash)}
genericResponse := &GenericResponse{}
err = esClient.DoMultiGet(context.Background(), ids, indexerdata.OperationsIndex, true, genericResponse)
require.Nil(t, err)

require.JSONEq(t, readExpectedResult("./testdata/esdtTransfer/esdt-transfer-cross-shard-on-source.json"), string(genericResponse.Docs[0].Source))

header = &dataBlock.Header{
Round: 55,
TimeStamp: 10102,
ShardID: 1,
}
txESDT.FeeInfo = &outport.FeeInfo{
GasUsed: 500_000,
Fee: big.NewInt(137660000000000),
InitialPaidFee: big.NewInt(137660000000000),
}

err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

err = esClient.DoMultiGet(context.Background(), ids, indexerdata.OperationsIndex, true, genericResponse)
require.Nil(t, err)

require.JSONEq(t, readExpectedResult("./testdata/esdtTransfer/esdt-transfer-cross-shard-on-destination.json"), string(genericResponse.Docs[0].Source))
}

func TestESDTTransferCrossShardIndexFirstOnDestinationAndAfterSource(t *testing.T) {
esClient, err := createESClient(esURL)
require.Nil(t, err)

esProc, err := CreateElasticProcessor(esClient)
require.Nil(t, err)

txHash := []byte("esdtTransferCross2")
header := &dataBlock.Header{
Round: 55,
TimeStamp: 10102,
ShardID: 1,
}

scrHash1 := []byte("scrHash1ESDTTransferOnSourceShard2")
body := &dataBlock.Body{
MiniBlocks: dataBlock.MiniBlockSlice{
{
Type: dataBlock.TxBlock,
SenderShardID: 2,
ReceiverShardID: 1,
TxHashes: [][]byte{txHash},
},
},
}

address1 := "erd1ef6470tjdtlgpa9f6g3ae4nsedmjg0gv6w73v32xtvhkfff993hq750xl9"
address2 := "erd13u7zyekzvdvzek8768r5gau9p6677ufppsjuklu9e6t7yx7rhg4s68e2ze"

txESDT := outport.TxInfo{
Transaction: &transaction.Transaction{
Nonce: 1,
SndAddr: decodeAddress(address1),
RcvAddr: decodeAddress(address2),
GasLimit: 500_000,
GasPrice: 1000000000,
Data: []byte("ESDTTransfer@54474e2d383862383366@0a"),
Value: big.NewInt(0),
},
FeeInfo: &outport.FeeInfo{
GasUsed: 500_000,
Fee: big.NewInt(137660000000000),
InitialPaidFee: big.NewInt(137660000000000),
},
}

refundValueBig, _ := big.NewInt(0).SetString("1660000000000", 10)
scrRefund := &smartContractResult.SmartContractResult{
Nonce: 20,
SndAddr: decodeAddress(address2),
RcvAddr: decodeAddress(address1),
PrevTxHash: txHash,
OriginalTxHash: txHash,
Value: refundValueBig,
Data: []byte("@6f6b"),
}

pool := &outport.TransactionPool{
Transactions: map[string]*outport.TxInfo{
hex.EncodeToString(txHash): &txESDT,
},
SmartContractResults: map[string]*outport.SCRInfo{
hex.EncodeToString(scrHash1): {SmartContractResult: scrRefund, FeeInfo: &outport.FeeInfo{}},
},
}
err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

ids := []string{hex.EncodeToString(txHash)}
genericResponse := &GenericResponse{}
err = esClient.DoMultiGet(context.Background(), ids, indexerdata.OperationsIndex, true, genericResponse)
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/esdtTransfer/esdt-transfer-first-on-destination.json"), string(genericResponse.Docs[0].Source))

txESDT.FeeInfo = &outport.FeeInfo{
GasUsed: 334_000,
Fee: big.NewInt(136000000000000),
InitialPaidFee: big.NewInt(137660000000000),
}
header = &dataBlock.Header{
Round: 50,
TimeStamp: 10101,
ShardID: 2,
}
body.MiniBlocks = append(body.MiniBlocks, &dataBlock.MiniBlock{
Type: dataBlock.SmartContractResultBlock,
SenderShardID: 2,
ReceiverShardID: 2,
TxHashes: [][]byte{scrHash1},
})

err = esProc.SaveTransactions(createOutportBlockWithHeader(body, header, pool, nil, testNumOfShards))
require.Nil(t, err)

err = esClient.DoMultiGet(context.Background(), ids, indexerdata.OperationsIndex, true, genericResponse)
require.Nil(t, err)
require.JSONEq(t, readExpectedResult("./testdata/esdtTransfer/esdt-transfer-second-on-source.json"), string(genericResponse.Docs[0].Source))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"miniBlockHash": "579fe2f60e8fa9c396254b2173282c6ecb6e736b42e015c3b744c86001be3981",
"nonce": 1,
"round": 55,
"value": "0",
"valueNum": 0,
"receiver": "erd13u7zyekzvdvzek8768r5gau9p6677ufppsjuklu9e6t7yx7rhg4s68e2ze",
"sender": "erd1ef6470tjdtlgpa9f6g3ae4nsedmjg0gv6w73v32xtvhkfff993hq750xl9",
"receiverShard": 1,
"senderShard": 2,
"gasPrice": 1000000000,
"gasLimit": 500000,
"gasUsed": 334000,
"fee": "136000000000000",
"feeNum": 0.000136,
"initialPaidFee": "137660000000000",
"data": "RVNEVFRyYW5zZmVyQDU0NDc0ZTJkMzgzODYyMzgzMzY2QDBh",
"signature": "",
"timestamp": 10102,
"status": "success",
"searchOrder": 0,
"hasScResults": true,
"tokens": [
"TGN-88b83f"
],
"esdtValues": [
"10"
],
"esdtValuesNum": [
1e-17
],
"type": "normal",
"operation": "ESDTTransfer"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"miniBlockHash": "579fe2f60e8fa9c396254b2173282c6ecb6e736b42e015c3b744c86001be3981",
"nonce": 1,
"round": 50,
"value": "0",
"valueNum": 0,
"receiver": "erd13u7zyekzvdvzek8768r5gau9p6677ufppsjuklu9e6t7yx7rhg4s68e2ze",
"sender": "erd1ef6470tjdtlgpa9f6g3ae4nsedmjg0gv6w73v32xtvhkfff993hq750xl9",
"receiverShard": 1,
"senderShard": 2,
"gasPrice": 1000000000,
"gasLimit": 500000,
"gasUsed": 334000,
"fee": "136000000000000",
"feeNum": 0.000136,
"initialPaidFee": "137660000000000",
"data": "RVNEVFRyYW5zZmVyQDU0NDc0ZTJkMzgzODYyMzgzMzY2QDBh",
"signature": "",
"timestamp": 10101,
"status": "pending",
"searchOrder": 0,
"hasScResults": true,
"tokens": [
"TGN-88b83f"
],
"esdtValues": [
"10"
],
"esdtValuesNum": [
1e-17
],
"type": "normal",
"operation": "ESDTTransfer"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"data": "RVNEVFRyYW5zZmVyQDU0NDc0ZTJkMzgzODYyMzgzMzY2QDBh",
"signature": "",
"fee": "137660000000000",
"esdtValues": [
"10"
],
"initialPaidFee": "137660000000000",
"type": "normal",
"gasLimit": 500000,
"esdtValuesNum": [
1e-17
],
"gasUsed": 500000,
"miniBlockHash": "d0f273198e773465908be139872605d93e86115b6320f9b34edb6a5e38668989",
"senderShard": 2,
"tokens": [
"TGN-88b83f"
],
"value": "0",
"gasPrice": 1000000000,
"timestamp": 10102,
"receiver": "erd13u7zyekzvdvzek8768r5gau9p6677ufppsjuklu9e6t7yx7rhg4s68e2ze",
"valueNum": 0,
"feeNum": 0.00013766,
"nonce": 1,
"round": 55,
"hasScResults": true,
"sender": "erd1ef6470tjdtlgpa9f6g3ae4nsedmjg0gv6w73v32xtvhkfff993hq750xl9",
"receiverShard": 1,
"operation": "ESDTTransfer",
"status": "success",
"searchOrder": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"data": "RVNEVFRyYW5zZmVyQDU0NDc0ZTJkMzgzODYyMzgzMzY2QDBh",
"signature": "",
"fee": "136000000000000",
"esdtValues": [
"10"
],
"initialPaidFee": "137660000000000",
"type": "normal",
"gasLimit": 500000,
"esdtValuesNum": [
1e-17
],
"gasUsed": 334000,
"miniBlockHash": "d0f273198e773465908be139872605d93e86115b6320f9b34edb6a5e38668989",
"senderShard": 2,
"tokens": [
"TGN-88b83f"
],
"value": "0",
"gasPrice": 1000000000,
"timestamp": 10102,
"receiver": "erd13u7zyekzvdvzek8768r5gau9p6677ufppsjuklu9e6t7yx7rhg4s68e2ze",
"valueNum": 0,
"feeNum": 0.000136,
"nonce": 1,
"round": 55,
"hasScResults": true,
"sender": "erd1ef6470tjdtlgpa9f6g3ae4nsedmjg0gv6w73v32xtvhkfff993hq750xl9",
"receiverShard": 1,
"operation": "ESDTTransfer",
"status": "success",
"searchOrder": 0
}
Loading

0 comments on commit 6608ee8

Please sign in to comment.