From f64dd9e44d4e98b4a6272929f7ed26a3ec0cb444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 19 Aug 2024 15:10:28 +0300 Subject: [PATCH] Add unit tests. --- ...ocks_with_direct_sc_deploy_with_value.json | 51 +++++++++++++++ server/services/transactionsTransformer.go | 6 +- .../services/transactionsTransformer_test.go | 60 ++++++++++++++++++ systemtests/broadcast_samples.py | 18 ++++++ systemtests/dummy.wasm | Bin 0 -> 115 bytes 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 server/services/testdata/blocks_with_direct_sc_deploy_with_value.json create mode 100755 systemtests/dummy.wasm diff --git a/server/services/testdata/blocks_with_direct_sc_deploy_with_value.json b/server/services/testdata/blocks_with_direct_sc_deploy_with_value.json new file mode 100644 index 00000000..070f6d84 --- /dev/null +++ b/server/services/testdata/blocks_with_direct_sc_deploy_with_value.json @@ -0,0 +1,51 @@ +[ + { + "miniBlocks": [ + { + "transactions": [ + { + "type": "normal", + "processingTypeOnSource": "SCDeployment", + "processingTypeOnDestination": "SCDeployment", + "hash": "2459bb2b9a64c1c920777ecbdaf0fa33d7fe8bcd24d7164562f341b2e4f702da", + "value": "10000000000000000", + "receiver": "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu", + "sender": "erd1tn62hjp72rznp8vq0lplva5csav6rccpqqdungpxtqz0g2hcq6uq9k4cc6", + "logs": { + "address": "erd1tn62hjp72rznp8vq0lplva5csav6rccpqqdungpxtqz0g2hcq6uq9k4cc6", + "events": [ + { + "address": "erd1qqqqqqqqqqqqqpgqc4vdnqgc48ww26ljxqe2flgl86jewg0nq6uqna2ymj", + "identifier": "SCDeploy", + "topics": [ + "AAAAAAAAAAAFAMVY2YEYqdzla/IwMqT9Hz6llyHzBrg=", + "XPSryD5QxTCdgH/D9naYh1mh4wEAG8mgJlgE9Cr4Brg=", + "v5gJ+IK1KdPGJfGYibrKij1SO0bzAAhfUVsODJ8midg=" + ], + "data": null, + "additionalData": null + }, + { + "address": "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu", + "identifier": "writeLog", + "topics": [ + "XPSryD5QxTCdgH/D9naYh1mh4wEAG8mgJlgE9Cr4Brg=", + "QHRvbyBtdWNoIGdhcyBwcm92aWRlZCBmb3IgcHJvY2Vzc2luZzogZ2FzIHByb3ZpZGVkID0gNDU4ODUwMCwgZ2FzIHVzZWQgPSAzMzQ1MDU=" + ], + "data": "QDZmNmI=", + "additionalData": ["QDZmNmI="] + } + ] + }, + "status": "success", + "operation": "scDeploy", + "initiallyPaidFee": "457385000000000", + "chainID": "T", + "version": 2, + "options": 0 + } + ] + } + ] + } +] diff --git a/server/services/transactionsTransformer.go b/server/services/transactionsTransformer.go index 782616e9..045afa77 100644 --- a/server/services/transactionsTransformer.go +++ b/server/services/transactionsTransformer.go @@ -445,11 +445,11 @@ func (transformer *transactionsTransformer) addOperationsGivenTransactionEvents( } for _, event := range eventsSCDeploy { - // Handle deployments with transfer of value + // Handle direct deployments with transfer of value (indirect deployments are currently excluded to prevent any potential misinterpretations).. if tx.Receiver == systemContractDeployAddress { operations := []*types.Operation{ - // Deployer's balance change is already captured in non-events-based operations. - // Let's simulate the transfer from the System deployment address to the contract address. + // Deployer's balance change is already captured in operations recovered not from logs / events, but from the transaction itself. + // It remains to "simulate" the transfer from the System deployment address to the contract address. { Type: opTransfer, Account: addressToAccountIdentifier(tx.Receiver), diff --git a/server/services/transactionsTransformer_test.go b/server/services/transactionsTransformer_test.go index e6b7fbe8..4b1cd7a8 100644 --- a/server/services/transactionsTransformer_test.go +++ b/server/services/transactionsTransformer_test.go @@ -287,6 +287,66 @@ func TestTransactionsTransformer_InvalidTxToRosettaTx(t *testing.T) { require.Equal(t, expectedTx, rosettaTx) } +func TestTransactionsTransformer_TransformBlockTxsHavingDirectSCDeployWithValue(t *testing.T) { + networkProvider := testscommon.NewNetworkProviderMock() + networkProvider.MockObservedActualShard = 0 + + extension := newNetworkProviderExtension(networkProvider) + transformer := newTransactionsTransformer(networkProvider) + + blocks, err := readTestBlocks("testdata/blocks_with_direct_sc_deploy_with_value.json") + require.Nil(t, err) + + txs, err := transformer.transformBlockTxs(blocks[0]) + require.Nil(t, err) + require.Len(t, txs, 1) + require.Len(t, txs[0].Operations, 5) + + expectedTx := &types.Transaction{ + TransactionIdentifier: hashToTransactionIdentifier("2459bb2b9a64c1c920777ecbdaf0fa33d7fe8bcd24d7164562f341b2e4f702da"), + Operations: []*types.Operation{ + { + Type: opTransfer, + OperationIdentifier: indexToOperationIdentifier(0), + Account: addressToAccountIdentifier("erd1tn62hjp72rznp8vq0lplva5csav6rccpqqdungpxtqz0g2hcq6uq9k4cc6"), + Amount: extension.valueToNativeAmount("-10000000000000000"), + Status: &opStatusSuccess, + }, + { + Type: opTransfer, + OperationIdentifier: indexToOperationIdentifier(1), + Account: addressToAccountIdentifier("erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"), + Amount: extension.valueToNativeAmount("10000000000000000"), + Status: &opStatusSuccess, + }, + { + Type: opFee, + OperationIdentifier: indexToOperationIdentifier(2), + Account: addressToAccountIdentifier("erd1tn62hjp72rznp8vq0lplva5csav6rccpqqdungpxtqz0g2hcq6uq9k4cc6"), + Amount: extension.valueToNativeAmount("-457385000000000"), + Status: &opStatusSuccess, + }, + { + Type: opTransfer, + OperationIdentifier: indexToOperationIdentifier(3), + Account: addressToAccountIdentifier("erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu"), + Amount: extension.valueToNativeAmount("-10000000000000000"), + Status: &opStatusSuccess, + }, + { + Type: opTransfer, + OperationIdentifier: indexToOperationIdentifier(4), + Account: addressToAccountIdentifier("erd1qqqqqqqqqqqqqpgqc4vdnqgc48ww26ljxqe2flgl86jewg0nq6uqna2ymj"), + Amount: extension.valueToNativeAmount("10000000000000000"), + Status: &opStatusSuccess, + }, + }, + Metadata: extractTransactionMetadata(blocks[0].MiniBlocks[0].Transactions[0]), + } + + require.Equal(t, expectedTx, txs[0]) +} + func TestTransactionsTransformer_TransformBlockTxsHavingESDTIssue(t *testing.T) { networkProvider := testscommon.NewNetworkProviderMock() networkProvider.MockCustomCurrencies = []resources.Currency{{Symbol: "FOO-6d28db"}} diff --git a/systemtests/broadcast_samples.py b/systemtests/broadcast_samples.py index 5d85e1ce..62d3dd19 100644 --- a/systemtests/broadcast_samples.py +++ b/systemtests/broadcast_samples.py @@ -13,6 +13,7 @@ from systemtests.config import CONFIGURATIONS, Configuration CONTRACT_PATH_ADDER = Path(__file__).parent / "adder.wasm" +CONTRACT_PATH_DUMMY = Path(__file__).parent / "dummy.wasm" def main(): @@ -147,6 +148,12 @@ def main(): amount=1000000000000000000 )) + # Direct contract deployment with MoveBalance + controller.send(controller.create_contract_deployment_with_move_balance( + sender=accounts.get_user(shard=0, index=0), + amount=10000000000000000 + )) + class BunchOfAccounts: def __init__(self, configuration: Configuration) -> None: @@ -362,6 +369,17 @@ def create_relayed_v3_with_a_few_inner_move_balances(self, relayer: "Account", s return transaction + def create_contract_deployment_with_move_balance(self, sender: "Account", amount: int) -> Transaction: + transaction = self.contracts_transactions_factory.create_transaction_for_deploy( + sender=sender.address, + bytecode=CONTRACT_PATH_DUMMY, + gas_limit=5000000, + arguments=[0], + native_transfer_amount=amount + ) + + return transaction + def send_multiple(self, transactions: List[Transaction]): for transaction in transactions: self._apply_nonce(transaction) diff --git a/systemtests/dummy.wasm b/systemtests/dummy.wasm new file mode 100755 index 0000000000000000000000000000000000000000..3c36df962a52b73fa104b51e38b89b635f769a43 GIT binary patch literal 115 zcmWN{u?~VT6ouh)?yW%RLi7n-oejG_2_zKl(3Sw2uqdOCZU21ZkA4XRz)CuP&;fFy zy7;y}uI+X7(JxOxjgmYlAw|V6Z~M>K@@$IWLYg;$$>R!^ujw)OgM;Xp60|<5z-Be> JmuAnw{sGKc6}kWb literal 0 HcmV?d00001