From 831475571fdc6008303c208c77c4207718ac9df7 Mon Sep 17 00:00:00 2001 From: striderDM <51991544+StriderDM@users.noreply.github.com> Date: Tue, 31 Aug 2021 17:45:59 +0200 Subject: [PATCH] fix: update cucumber tests for walletffi.feature Made types more explicit for ffi interface and easier to maintain going forwards. Initialized InterfaceFFI in world rather than WalletFFIClient. Updated steps in WalletFFI.feature Updated some const variables in wrapper classes to be let. Better handling of callback function pointer variables in wallet. Added additional steps to tests. Update WalletFFI.feature Update wallet.js Fixed leak in wallet Fixed leak for transport in walletFFI client. Renamed pointer variable for each struct wrapper to ptr. Update walletFFIClient.js --- .circleci/config.yml | 2 +- integration_tests/features/WalletFFI.feature | 34 +- integration_tests/features/support/steps.js | 155 +++-- integration_tests/features/support/world.js | 5 +- integration_tests/helpers/ffi/byteVector.js | 20 +- integration_tests/helpers/ffi/commsConfig.js | 12 +- .../helpers/ffi/completedTransaction.js | 62 +- .../helpers/ffi/completedTransactions.js | 21 +- integration_tests/helpers/ffi/contact.js | 26 +- integration_tests/helpers/ffi/contacts.js | 16 +- integration_tests/helpers/ffi/emojiSet.js | 14 +- integration_tests/helpers/ffi/ffiInterface.js | 529 +++++++++++------- .../helpers/ffi/pendingInboundTransaction.js | 42 +- .../helpers/ffi/pendingInboundTransactions.js | 21 +- .../helpers/ffi/pendingOutboundTransaction.js | 46 +- .../ffi/pendingOutboundTransactions.js | 21 +- integration_tests/helpers/ffi/privateKey.js | 23 +- integration_tests/helpers/ffi/publicKey.js | 24 +- integration_tests/helpers/ffi/seedWords.js | 25 +- .../helpers/ffi/transportType.js | 16 +- integration_tests/helpers/ffi/wallet.js | 206 +++---- integration_tests/helpers/walletFFIClient.js | 8 +- 22 files changed, 731 insertions(+), 597 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f665baf02c6..3410c2b033a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -129,7 +129,7 @@ commands: when: always - run: name: Run ffi cucumber scenarios - command: cd integration_tests && mkdir -p cucumber_output && node_modules/.bin/cucumber-js --tags "not @long-running and not @broken and not @flaky and @wallet-ffi" --format json:cucumber_output/tests_ffi.cucumber --exit + command: cd integration_tests && mkdir -p cucumber_output && touch cucumber_output/tests_ffi.cucumber && node_modules/.bin/cucumber-js --tags "not @long-running and not @broken and not @flaky and @wallet-ffi" --format json:cucumber_output/tests_ffi.cucumber --exit - run: name: Generate report (ffi) command: cd integration_tests && node ./generate_report.js "cucumber_output/tests_ffi.cucumber" "temp/reports/cucumber_ffi_report.html" diff --git a/integration_tests/features/WalletFFI.feature b/integration_tests/features/WalletFFI.feature index 94329892705..2ba709d065f 100644 --- a/integration_tests/features/WalletFFI.feature +++ b/integration_tests/features/WalletFFI.feature @@ -1,7 +1,12 @@ @wallet-ffi Feature: Wallet FFI - # Increase heap memory available to nodejs if frequent crashing occurs with - # error being be similar to this: `0x1a32cd5 V8_Fatal(char const*, ...)` + # Increase heap memory available to nodejs if frequent crashing occurs. + + # Note, there is a known bug in napi-ffi that intermittently causes crashes which has the following output: + # + # Fatal error in , line 0 + # Check failed: result.second. + # # It's just calling the encrypt function, we don't test if it's actually encrypted Scenario: As a client I want to be able to protect my wallet with a passphrase @@ -35,11 +40,12 @@ Feature: Wallet FFI And I stop ffi wallet FFI_WALLET And I stop node BASE1 And I wait 5 seconds - And I restart ffi wallet FFI_WALLET - # Possibly check SAF messages, no way to get current connected base node peer from the library itself afaik - # Good idea just to add a fn to do this to the library. - # Then I wait for ffi wallet FFI_WALLET to receive 1 SAF message - And I wait 5 seconds + # Broken step with reason base node is not persisted + # See details on: + # Scenario: As a client I want to receive Tari via my Public Key sent while I am offline when I come back online + # And I restart ffi wallet FFI_WALLET + And I restart ffi wallet FFI_WALLET connected to base node BASE2 + Then I wait for ffi wallet FFI_WALLET to receive at least 1 SAF message And I stop ffi wallet FFI_WALLET Scenario: As a client I want to cancel a transaction @@ -101,12 +107,18 @@ Feature: Wallet FFI And I wait 10 seconds And I send 2000000 uT from wallet SENDER to wallet FFI_WALLET at fee 100 And I wait 5 seconds - And I restart ffi wallet FFI_WALLET + # Broken step with reason base node is not persisted + # Log: + # [wallet::transaction_service::callback_handler] DEBUG Calling Received Finalized Transaction callback function for TxId: 7595706993517535281 + # [wallet::transaction_service::service] WARN Error broadcasting completed transaction TxId: 7595706993517535281 to mempool: NoBaseNodeKeysProvided + # And I restart ffi wallet FFI_WALLET + And I restart ffi wallet FFI_WALLET connected to base node BASE Then I wait for ffi wallet FFI_WALLET to receive 1 transaction Then I wait for ffi wallet FFI_WALLET to receive 1 finalization - # Assume tx will be mined to reduce time taken for test, balance is tested in later scenarios. - # And mining node MINER mines 10 blocks - # Then I wait for ffi wallet FFI_WALLET to have at least 1000000 uT + Then I wait for ffi wallet FFI_WALLET to receive 1 broadcast + And mining node MINER mines 10 blocks + Then I wait for ffi wallet FFI_WALLET to receive 1 mined + Then I wait for ffi wallet FFI_WALLET to have at least 1000000 uT And I stop ffi wallet FFI_WALLET # Scenario: As a client I want to get my balance diff --git a/integration_tests/features/support/steps.js b/integration_tests/features/support/steps.js index ef93992b5e5..23c8e065472 100644 --- a/integration_tests/features/support/steps.js +++ b/integration_tests/features/support/steps.js @@ -3826,15 +3826,15 @@ Then( "Waiting for " + wallet_name + " to receive " + amount + " transaction(s)" ); - await waitFor( - async () => { - return wallet.getCounters().received >= amount; - }, - true, - 700 * 1000, - 5 * 1000, - 5 - ); + let count = 0; + + while (!(wallet.getCounters().received >= amount)) { + await sleep(1000); + count++; + if (count > 700) { + break; + } + } if (!(wallet.getCounters().received >= amount)) { console.log("Counter not adequate!"); @@ -3860,15 +3860,15 @@ Then( " transaction finalization(s)" ); - await waitFor( - async () => { - return wallet.getCounters().finalized >= amount; - }, - true, - 700 * 1000, - 5 * 1000, - 5 - ); + let count = 0; + + while (!(wallet.getCounters().finalized >= amount)) { + await sleep(1000); + count++; + if (count > 700) { + break; + } + } if (!(wallet.getCounters().finalized >= amount)) { console.log("Counter not adequate!"); @@ -3880,7 +3880,7 @@ Then( ); Then( - /I wait for ffi wallet (.*) to receive (.*) SAF message/, + /I wait for ffi wallet (.*) to receive (.*) broadcast/, { timeout: 710 * 1000 }, async function (wallet_name, amount) { let wallet = this.getWallet(wallet_name); @@ -3891,19 +3891,88 @@ Then( wallet_name + " to receive " + amount + - " SAF messages(s)" + " transaction broadcast(s)" ); - await waitFor( - async () => { - return wallet.getCounters().saf >= amount; - }, - true, - 700 * 1000, - 5 * 1000, - 5 + let count = 0; + + while (!(wallet.getCounters().broadcast >= amount)) { + await sleep(1000); + count++; + if (count > 700) { + break; + } + } + + if (!(wallet.getCounters().broadcast >= amount)) { + console.log("Counter not adequate!"); + } else { + console.log(wallet.getCounters()); + } + expect(wallet.getCounters().broadcast >= amount).to.equal(true); + } +); + +Then( + /I wait for ffi wallet (.*) to receive (.*) mined/, + { timeout: 710 * 1000 }, + async function (wallet_name, amount) { + let wallet = this.getWallet(wallet_name); + + console.log("\n"); + console.log( + "Waiting for " + + wallet_name + + " to receive " + + amount + + " transaction mined" ); + let count = 0; + + while (!(wallet.getCounters().mined >= amount)) { + console.log(wallet.getBalance()); + await sleep(1000); + count++; + if (count > 700) { + break; + } + } + + if (!(wallet.getCounters().mined >= amount)) { + console.log("Counter not adequate!"); + } else { + console.log(wallet.getCounters()); + } + expect(wallet.getCounters().mined >= amount).to.equal(true); + } +); + +Then( + /I wait for ffi wallet (.*) to receive at least (.*) SAF message/, + { timeout: 710 * 1000 }, + async function (wallet_name, amount) { + let wallet = this.getWallet(wallet_name); + + console.log("\n"); + console.log( + "Waiting for " + + wallet_name + + " to receive at least " + + amount + + " SAF messages(s)" + ); + + let count = 0; + + while (!(wallet.getCounters().saf >= amount)) { + await sleep(1000); + count++; + if (count > 700) { + break; + } + } + if (!(wallet.getCounters().saf >= amount)) { console.log("Counter not adequate!"); } else { @@ -3927,6 +3996,7 @@ Then( let count = 0; while (!(wallet.getBalance().available >= amount)) { + console.log(wallet.getBalance()); await sleep(1000); count++; if (count > 700) { @@ -3938,9 +4008,8 @@ Then( if (!(balance >= amount)) { console.log("Balance not adequate!"); - } else { - console.log(wallet.getBalance()); } + expect(balance >= amount).to.equal(true); } ); @@ -3961,10 +4030,28 @@ When(/I start ffi wallet (.*)/, async function (walletName) { await wallet.startNew(null, null); }); -When(/I restart ffi wallet (.*)/, async function (walletName) { - let wallet = this.getWallet(walletName); - await wallet.restart(); -}); +When( + /I restart ffi wallet (.*) connected to base node (.*)/, + async function (walletName, node) { + let wallet = this.getWallet(walletName); + await wallet.restart(); + let peer = this.nodes[node].peerAddress().split("::"); + wallet.addBaseNodePeer(peer[0], peer[1]); + } +); + +Then( + "I want to get public key of ffi wallet {word}", + { timeout: 20 * 1000 }, + function (name) { + let wallet = this.getWallet(name); + let public_key = wallet.identify(); + expect(public_key.length).to.be.equal( + 64, + `Public key has wrong length : ${public_key}` + ); + } +); When(/I stop ffi wallet (.*)/, function (walletName) { let wallet = this.getWallet(walletName); diff --git a/integration_tests/features/support/world.js b/integration_tests/features/support/world.js index 91e8b45f167..44c7dddd449 100644 --- a/integration_tests/features/support/world.js +++ b/integration_tests/features/support/world.js @@ -9,6 +9,8 @@ const TransactionBuilder = require("../../helpers/transactionBuilder"); const glob = require("glob"); const fs = require("fs"); const archiver = require("archiver"); +const InterfaceFFI = require("../../helpers/ffi/ffiInterface"); + class CustomWorld { constructor({ attach, parameters }) { // this.variable = 0; @@ -381,7 +383,8 @@ BeforeAll({ timeout: 1200000 }, async function () { await miningNode.compile(); console.log("Compiling wallet FFI..."); - await WalletFFIClient.Init(); + await InterfaceFFI.compile(); + await InterfaceFFI.init(); console.log("Finished compilation."); }); diff --git a/integration_tests/helpers/ffi/byteVector.js b/integration_tests/helpers/ffi/byteVector.js index 245cb4320ec..5e2f8076f53 100644 --- a/integration_tests/helpers/ffi/byteVector.js +++ b/integration_tests/helpers/ffi/byteVector.js @@ -1,15 +1,15 @@ const InterfaceFFI = require("./ffiInterface"); class ByteVector { - #byte_vector_ptr; + #ptr; pointerAssign(ptr) { // Prevent pointer from being leaked in case of re-assignment - if (this.#byte_vector_ptr) { + if (this.#ptr) { this.destroy(); - this.#byte_vector_ptr = ptr; + this.#ptr = ptr; } else { - this.#byte_vector_ptr = ptr; + this.#ptr = ptr; } } @@ -30,21 +30,21 @@ class ByteVector { } getLength() { - return InterfaceFFI.byteVectorGetLength(this.#byte_vector_ptr); + return InterfaceFFI.byteVectorGetLength(this.#ptr); } getAt(position) { - return InterfaceFFI.byteVectorGetAt(this.#byte_vector_ptr, position); + return InterfaceFFI.byteVectorGetAt(this.#ptr, position); } getPtr() { - return this.#byte_vector_ptr; + return this.#ptr; } destroy() { - if (this.#byte_vector_ptr) { - InterfaceFFI.byteVectorDestroy(this.#byte_vector_ptr); - this.#byte_vector_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.byteVectorDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/commsConfig.js b/integration_tests/helpers/ffi/commsConfig.js index 9bb9ddcb7ad..c9c913a7b59 100644 --- a/integration_tests/helpers/ffi/commsConfig.js +++ b/integration_tests/helpers/ffi/commsConfig.js @@ -2,7 +2,7 @@ const InterfaceFFI = require("./ffiInterface"); const utf8 = require("utf8"); class CommsConfig { - #comms_config_ptr; + #ptr; constructor( public_address, @@ -17,7 +17,7 @@ class CommsConfig { let sanitize_db_name = utf8.encode(database_name); let sanitize_db_path = utf8.encode(datastore_path); let sanitize_network = utf8.encode(network); - this.#comms_config_ptr = InterfaceFFI.commsConfigCreate( + this.#ptr = InterfaceFFI.commsConfigCreate( sanitize_address, transport_ptr, sanitize_db_name, @@ -29,13 +29,13 @@ class CommsConfig { } getPtr() { - return this.#comms_config_ptr; + return this.#ptr; } destroy() { - if (this.#comms_config_ptr) { - InterfaceFFI.commsConfigDestroy(this.#comms_config_ptr); - this.#comms_config_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.commsConfigDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/completedTransaction.js b/integration_tests/helpers/ffi/completedTransaction.js index cc23f22ecff..93e0146a5ad 100644 --- a/integration_tests/helpers/ffi/completedTransaction.js +++ b/integration_tests/helpers/ffi/completedTransaction.js @@ -2,33 +2,29 @@ const InterfaceFFI = require("./ffiInterface"); const PublicKey = require("./publicKey"); class CompletedTransaction { - #tari_completed_transaction_ptr; + #ptr; pointerAssign(ptr) { - if (this.#tari_completed_transaction_ptr) { + if (this.#ptr) { this.destroy(); - this.#tari_completed_transaction_ptr = ptr; + this.#ptr = ptr; } else { - this.#tari_completed_transaction_ptr = ptr; + this.#ptr = ptr; } } getPtr() { - return this.#tari_completed_transaction_ptr; + return this.#ptr; } isOutbound() { - return InterfaceFFI.completedTransactionIsOutbound( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionIsOutbound(this.#ptr); } getDestinationPublicKey() { let result = new PublicKey(); result.pointerAssign( - InterfaceFFI.completedTransactionGetDestinationPublicKey( - this.#tari_completed_transaction_ptr - ) + InterfaceFFI.completedTransactionGetDestinationPublicKey(this.#ptr) ); return result; } @@ -36,67 +32,47 @@ class CompletedTransaction { getSourcePublicKey() { let result = new PublicKey(); result.pointerAssign( - InterfaceFFI.completedTransactionGetSourcePublicKey( - this.#tari_completed_transaction_ptr - ) + InterfaceFFI.completedTransactionGetSourcePublicKey(this.#ptr) ); return result; } getAmount() { - return InterfaceFFI.completedTransactionGetAmount( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionGetAmount(this.#ptr); } getFee() { - return InterfaceFFI.completedTransactionGetFee( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionGetFee(this.#ptr); } getMessage() { - return InterfaceFFI.completedTransactionGetMessage( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionGetMessage(this.#ptr); } getStatus() { - return InterfaceFFI.completedTransactionGetStatus( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionGetStatus(this.#ptr); } getTransactionID() { - return InterfaceFFI.completedTransactionGetTransactionId( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionGetTransactionId(this.#ptr); } getTimestamp() { - return InterfaceFFI.completedTransactionGetTimestamp( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionGetTimestamp(this.#ptr); } isValid() { - return InterfaceFFI.completedTransactionIsValid( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionIsValid(this.#ptr); } getConfirmations() { - return InterfaceFFI.completedTransactionGetConfirmations( - this.#tari_completed_transaction_ptr - ); + return InterfaceFFI.completedTransactionGetConfirmations(this.#ptr); } destroy() { - if (this.#tari_completed_transaction_ptr) { - InterfaceFFI.completedTransactionDestroy( - this.#tari_completed_transaction_ptr - ); - this.#tari_completed_transaction_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.completedTransactionDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/completedTransactions.js b/integration_tests/helpers/ffi/completedTransactions.js index 2b8387bb72d..1611ac61ed4 100644 --- a/integration_tests/helpers/ffi/completedTransactions.js +++ b/integration_tests/helpers/ffi/completedTransactions.js @@ -2,35 +2,28 @@ const CompletedTransaction = require("./completedTransaction"); const InterfaceFFI = require("./ffiInterface"); class CompletedTransactions { - #tari_completed_transactions_ptr; + #ptr; constructor(ptr) { - this.#tari_completed_transactions_ptr = ptr; + this.#ptr = ptr; } getLength() { - return InterfaceFFI.completedTransactionsGetLength( - this.#tari_completed_transactions_ptr - ); + return InterfaceFFI.completedTransactionsGetLength(this.#ptr); } getAt(position) { let result = new CompletedTransaction(); result.pointerAssign( - InterfaceFFI.completedTransactionsGetAt( - this.#tari_completed_transactions_ptr, - position - ) + InterfaceFFI.completedTransactionsGetAt(this.#ptr, position) ); return result; } destroy() { - if (this.#tari_completed_transactions_ptr) { - InterfaceFFI.completedTransactionsDestroy( - this.#tari_completed_transactions_ptr - ); - this.#tari_completed_transactions_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.completedTransactionsDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/contact.js b/integration_tests/helpers/ffi/contact.js index ea72376e752..8937fdf1ad9 100644 --- a/integration_tests/helpers/ffi/contact.js +++ b/integration_tests/helpers/ffi/contact.js @@ -2,50 +2,48 @@ const PublicKey = require("./publicKey"); const InterfaceFFI = require("./ffiInterface"); class Contact { - #tari_contact_ptr; + #ptr; pointerAssign(ptr) { // Prevent pointer from being leaked in case of re-assignment - if (this.#tari_contact_ptr) { + if (this.#ptr) { this.destroy(); - this.#tari_contact_ptr = ptr; + this.#ptr = ptr; } else { - this.#tari_contact_ptr = ptr; + this.#ptr = ptr; } } getPtr() { - return this.#tari_contact_ptr; + return this.#ptr; } getAlias() { - const alias = InterfaceFFI.contactGetAlias(this.#tari_contact_ptr); - const result = alias.readCString(); + let alias = InterfaceFFI.contactGetAlias(this.#ptr); + let result = alias.readCString(); InterfaceFFI.stringDestroy(alias); return result; } getPubkey() { let result = new PublicKey(); - result.pointerAssign( - InterfaceFFI.contactGetPublicKey(this.#tari_contact_ptr) - ); + result.pointerAssign(InterfaceFFI.contactGetPublicKey(this.#ptr)); return result; } getPubkeyHex() { let result = ""; let pk = new PublicKey(); - pk.pointerAssign(InterfaceFFI.contactGetPublicKey(this.#tari_contact_ptr)); + pk.pointerAssign(InterfaceFFI.contactGetPublicKey(this.#ptr)); result = pk.getHex(); pk.destroy(); return result; } destroy() { - if (this.#tari_contact_ptr) { - InterfaceFFI.contactDestroy(this.#tari_contact_ptr); - this.#tari_contact_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.contactDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/contacts.js b/integration_tests/helpers/ffi/contacts.js index 1f7db81fcc5..25adcc67115 100644 --- a/integration_tests/helpers/ffi/contacts.js +++ b/integration_tests/helpers/ffi/contacts.js @@ -2,28 +2,26 @@ const Contact = require("./contact"); const InterfaceFFI = require("./ffiInterface"); class Contacts { - #tari_contacts_ptr; + #ptr; constructor(ptr) { - this.#tari_contacts_ptr = ptr; + this.#ptr = ptr; } getLength() { - return InterfaceFFI.contactsGetLength(this.#tari_contacts_ptr); + return InterfaceFFI.contactsGetLength(this.#ptr); } getAt(position) { let result = new Contact(); - result.pointerAssign( - InterfaceFFI.contactsGetAt(this.#tari_contacts_ptr, position) - ); + result.pointerAssign(InterfaceFFI.contactsGetAt(this.#ptr, position)); return result; } destroy() { - if (this.#tari_contacts_ptr) { - InterfaceFFI.contactsDestroy(this.#tari_contacts_ptr); - this.#tari_contacts_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.contactsDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/emojiSet.js b/integration_tests/helpers/ffi/emojiSet.js index f94e2ae7465..9f18a802d51 100644 --- a/integration_tests/helpers/ffi/emojiSet.js +++ b/integration_tests/helpers/ffi/emojiSet.js @@ -1,18 +1,18 @@ const InterfaceFFI = require("./ffiInterface"); class EmojiSet { - #emoji_set_ptr; + #ptr; constructor() { - this.#emoji_set_ptr = InterfaceFFI.getEmojiSet(); + this.#ptr = InterfaceFFI.getEmojiSet(); } getLength() { - return InterfaceFFI.emojiSetGetLength(this.#emoji_set_ptr); + return InterfaceFFI.emojiSetGetLength(this.#ptr); } getAt(position) { - return InterfaceFFI.emojiSetGetAt(this.#emoji_set_ptr, position); + return InterfaceFFI.emojiSetGetAt(this.#ptr, position); } list() { @@ -26,9 +26,9 @@ class EmojiSet { } destroy() { - if (this.#emoji_set_ptr) { - InterfaceFFI.byteVectorDestroy(this.#emoji_set_ptr); - this.#emoji_set_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.byteVectorDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/ffiInterface.js b/integration_tests/helpers/ffi/ffiInterface.js index e39632cd059..0cd4476fe09 100644 --- a/integration_tests/helpers/ffi/ffiInterface.js +++ b/integration_tests/helpers/ffi/ffiInterface.js @@ -10,6 +10,19 @@ const { spawn } = require("child_process"); const fs = require("fs"); class InterfaceFFI { + static void = ref.types.void; + static bool = ref.types.bool; + static int = "int"; + static uint64 = "uint64"; + static uchar = "uchar"; + static uint = "uint"; + static string = "string"; + static ucharPtr = ref.refType(this.uchar); // uchar* + static ptr = ref.refType(this.void); //pointer is opaque + static stringPtr = ref.refType(ref.types.CString); + static intPtr = ref.refType(this.int); // int* + static boolPtr = ref.refType(this.bool); // bool* + //region Compile static compile() { return new Promise((resolve, _reject) => { @@ -56,269 +69,366 @@ class InterfaceFFI { static #loaded = false; static #ps = null; + static library = null; - static async Init() { - if (this.#loaded) { - return; - } - - this.#loaded = true; - await this.compile(); - const outputProcess = `${process.cwd()}/temp/out/${ + static async init() { + this.library = `${process.cwd()}/temp/out/${ process.platform === "win32" ? "" : "lib" }tari_wallet_ffi`; - // Load the library - this.#fn = ffi.Library(outputProcess, { - transport_memory_create: ["pointer", ["void"]], - transport_tcp_create: ["pointer", ["string", "int*"]], + this.#fn = ffi.Library(this.#loaded ? null : this.library, { + transport_memory_create: [this.ptr, []], + transport_tcp_create: [this.ptr, [this.string, this.intPtr]], transport_tor_create: [ - "pointer", - ["string", "pointer", "ushort", "string", "string", "int*"], + this.ptr, + [ + this.string, + this.ptr, + "ushort", + this.string, + this.string, + this.intPtr, + ], ], - transport_memory_get_address: ["char*", ["pointer", "int*"]], - transport_type_destroy: ["void", ["pointer"]], - string_destroy: ["void", ["string"]], - byte_vector_create: ["pointer", ["uchar*", "uint", "int*"]], - byte_vector_get_at: ["uchar", ["pointer", "uint", "int*"]], - byte_vector_get_length: ["uint", ["pointer", "int*"]], - byte_vector_destroy: ["void", ["pointer"]], - public_key_create: ["pointer", ["pointer", "int*"]], - public_key_get_bytes: ["pointer", ["pointer", "int*"]], - public_key_from_private_key: ["pointer", ["pointer", "int*"]], - public_key_from_hex: ["pointer", ["string", "int*"]], - public_key_destroy: ["void", ["pointer"]], - public_key_to_emoji_id: ["char*", ["pointer", "int*"]], - emoji_id_to_public_key: ["pointer", ["string", "int*"]], - private_key_create: ["pointer", ["pointer", "int*"]], - private_key_generate: ["pointer", ["void"]], - private_key_get_bytes: ["pointer", ["pointer", "int*"]], - private_key_from_hex: ["pointer", ["string", "int*"]], - private_key_destroy: ["void", ["pointer"]], - seed_words_create: ["pointer", ["void"]], - seed_words_get_length: ["uint", ["pointer", "int*"]], - seed_words_get_at: ["char*", ["pointer", "uint", "int*"]], - seed_words_push_word: ["uchar", ["pointer", "string", "int*"]], - seed_words_destroy: ["void", ["pointer"]], - contact_create: ["pointer", ["string", "pointer", "int*"]], - contact_get_alias: ["char*", ["pointer", "int*"]], - contact_get_public_key: ["pointer", ["pointer", "int*"]], - contact_destroy: ["void", ["pointer"]], - contacts_get_length: ["uint", ["pointer", "int*"]], - contacts_get_at: ["pointer", ["pointer", "uint", "int*"]], - contacts_destroy: ["void", ["pointer"]], + transport_memory_get_address: [this.stringPtr, [this.ptr, this.intPtr]], + transport_type_destroy: [this.void, [this.ptr]], + string_destroy: [this.void, [this.string]], + byte_vector_create: [this.ptr, [this.ucharPtr, this.uint, this.intPtr]], + byte_vector_get_at: [this.uchar, [this.ptr, this.uint, this.intPtr]], + byte_vector_get_length: [this.uint, [this.ptr, this.intPtr]], + byte_vector_destroy: [this.void, [this.ptr]], + public_key_create: [this.ptr, [this.ptr, this.intPtr]], + public_key_get_bytes: [this.ptr, [this.ptr, this.intPtr]], + public_key_from_private_key: [this.ptr, [this.ptr, this.intPtr]], + public_key_from_hex: [this.ptr, [this.string, this.intPtr]], + public_key_destroy: [this.void, [this.ptr]], + public_key_to_emoji_id: [this.stringPtr, [this.ptr, this.intPtr]], + emoji_id_to_public_key: [this.ptr, [this.string, this.intPtr]], + private_key_create: [this.ptr, [this.ptr, this.intPtr]], + private_key_generate: [this.ptr, []], + private_key_get_bytes: [this.ptr, [this.ptr, this.intPtr]], + private_key_from_hex: [this.ptr, [this.string, this.intPtr]], + private_key_destroy: [this.void, [this.ptr]], + seed_words_create: [this.ptr, []], + seed_words_get_length: [this.uint, [this.ptr, this.intPtr]], + seed_words_get_at: [this.stringPtr, [this.ptr, this.uint, this.intPtr]], + seed_words_push_word: [this.uchar, [this.ptr, this.string, this.intPtr]], + seed_words_destroy: [this.void, [this.ptr]], + contact_create: [this.ptr, [this.string, this.ptr, this.intPtr]], + contact_get_alias: [this.stringPtr, [this.ptr, this.intPtr]], + contact_get_public_key: [this.ptr, [this.ptr, this.intPtr]], + contact_destroy: [this.void, [this.ptr]], + contacts_get_length: [this.uint, [this.ptr, this.intPtr]], + contacts_get_at: [this.ptr, [this.ptr, this.uint, this.intPtr]], + contacts_destroy: [this.void, [this.ptr]], completed_transaction_get_destination_public_key: [ - "pointer", - ["pointer", "int*"], + this.ptr, + [this.ptr, this.intPtr], ], completed_transaction_get_source_public_key: [ - "pointer", - ["pointer", "int*"], + this.ptr, + [this.ptr, this.intPtr], + ], + completed_transaction_get_amount: [this.uint64, [this.ptr, this.intPtr]], + completed_transaction_get_fee: [this.uint64, [this.ptr, this.intPtr]], + completed_transaction_get_message: [ + this.stringPtr, + [this.ptr, this.intPtr], + ], + completed_transaction_get_status: [this.int, [this.ptr, this.intPtr]], + completed_transaction_get_transaction_id: [ + this.uint64, + [this.ptr, this.intPtr], + ], + completed_transaction_get_timestamp: [ + this.uint64, + [this.ptr, this.intPtr], + ], + completed_transaction_is_valid: [this.bool, [this.ptr, this.intPtr]], + completed_transaction_is_outbound: [this.bool, [this.ptr, this.intPtr]], + completed_transaction_get_confirmations: [ + this.uint64, + [this.ptr, this.intPtr], ], - completed_transaction_get_amount: ["uint64", ["pointer", "int*"]], - completed_transaction_get_fee: ["uint64", ["pointer", "int*"]], - completed_transaction_get_message: ["char*", ["pointer", "int*"]], - completed_transaction_get_status: ["int", ["pointer", "int*"]], - completed_transaction_get_transaction_id: ["uint64", ["pointer", "int*"]], - completed_transaction_get_timestamp: ["uint64", ["pointer", "int*"]], - completed_transaction_is_valid: ["bool", ["pointer", "int*"]], - completed_transaction_is_outbound: ["bool", ["pointer", "int*"]], - completed_transaction_get_confirmations: ["uint64", ["pointer", "int*"]], - completed_transaction_destroy: ["void", ["pointer"]], + completed_transaction_destroy: [this.void, [this.ptr]], //completed_transaction_get_excess: [ //this.tari_excess_ptr, - // [this.tari_completed_transaction_ptr, "int*"], + // [this.tari_completed_transaction_ptr, this.intPtr], //], //completed_transaction_get_public_nonce: [ // this.tari_excess_public_nonce_ptr, - // [this.tari_completed_transaction_ptr, "int*"], + // [this.tari_completed_transaction_ptr, this.intPtr], //], //completed_transaction_get_signature: [ // this.tari_excess_signature_ptr, - // [this.tari_completed_transaction_ptr, "int*"], + // [this.tari_completed_transaction_ptr, this.intPtr], //], - // excess_destroy: ["void", [this.tari_excess_ptr]], - // nonce_destroy: ["void", [this.tari_excess_public_nonce_ptr]], - // signature_destroy: ["void", [this.tari_excess_signature_ptr]], - completed_transactions_get_length: ["uint", ["pointer", "int*"]], - completed_transactions_get_at: ["pointer", ["pointer", "uint", "int*"]], - completed_transactions_destroy: ["void", ["pointer"]], + // excess_destroy: [this.void, [this.tari_excess_ptr]], + // nonce_destroy: [this.void, [this.tari_excess_public_nonce_ptr]], + // signature_destroy: [this.void, [this.tari_excess_signature_ptr]], + completed_transactions_get_length: [this.uint, [this.ptr, this.intPtr]], + completed_transactions_get_at: [ + this.ptr, + [this.ptr, this.uint, this.intPtr], + ], + completed_transactions_destroy: [this.void, [this.ptr]], pending_outbound_transaction_get_transaction_id: [ - "uint64", - ["pointer", "int*"], + this.uint64, + [this.ptr, this.intPtr], ], pending_outbound_transaction_get_destination_public_key: [ - "pointer", - ["pointer", "int*"], + this.ptr, + [this.ptr, this.intPtr], + ], + pending_outbound_transaction_get_amount: [ + this.uint64, + [this.ptr, this.intPtr], + ], + pending_outbound_transaction_get_fee: [ + this.uint64, + [this.ptr, this.intPtr], + ], + pending_outbound_transaction_get_message: [ + this.stringPtr, + [this.ptr, this.intPtr], ], - pending_outbound_transaction_get_amount: ["uint64", ["pointer", "int*"]], - pending_outbound_transaction_get_fee: ["uint64", ["pointer", "int*"]], - pending_outbound_transaction_get_message: ["char*", ["pointer", "int*"]], pending_outbound_transaction_get_timestamp: [ - "uint64", - ["pointer", "int*"], + this.uint64, + [this.ptr, this.intPtr], + ], + pending_outbound_transaction_get_status: [ + this.int, + [this.ptr, this.intPtr], + ], + pending_outbound_transaction_destroy: [this.void, [this.ptr]], + pending_outbound_transactions_get_length: [ + this.uint, + [this.ptr, this.intPtr], ], - pending_outbound_transaction_get_status: ["int", ["pointer", "int*"]], - pending_outbound_transaction_destroy: ["void", ["pointer"]], - pending_outbound_transactions_get_length: ["uint", ["pointer", "int*"]], pending_outbound_transactions_get_at: [ - "pointer", - ["pointer", "uint", "int*"], + this.ptr, + [this.ptr, this.uint, this.intPtr], ], - pending_outbound_transactions_destroy: ["void", ["pointer"]], + pending_outbound_transactions_destroy: [this.void, [this.ptr]], pending_inbound_transaction_get_transaction_id: [ - "uint64", - ["pointer", "int*"], + this.uint64, + [this.ptr, this.intPtr], ], pending_inbound_transaction_get_source_public_key: [ - "pointer", - ["pointer", "int*"], + this.ptr, + [this.ptr, this.intPtr], + ], + pending_inbound_transaction_get_message: [ + this.stringPtr, + [this.ptr, this.intPtr], + ], + pending_inbound_transaction_get_amount: [ + this.uint64, + [this.ptr, this.intPtr], ], - pending_inbound_transaction_get_message: ["char*", ["pointer", "int*"]], - pending_inbound_transaction_get_amount: ["uint64", ["pointer", "int*"]], pending_inbound_transaction_get_timestamp: [ - "uint64", - ["pointer", "int*"], + this.uint64, + [this.ptr, this.intPtr], + ], + pending_inbound_transaction_get_status: [ + this.int, + [this.ptr, this.intPtr], + ], + pending_inbound_transaction_destroy: [this.void, [this.ptr]], + pending_inbound_transactions_get_length: [ + this.uint, + [this.ptr, this.intPtr], ], - pending_inbound_transaction_get_status: ["int", ["pointer", "int*"]], - pending_inbound_transaction_destroy: ["void", ["pointer"]], - pending_inbound_transactions_get_length: ["uint", ["pointer", "int*"]], pending_inbound_transactions_get_at: [ - "pointer", - ["pointer", "uint", "int*"], + this.ptr, + [this.ptr, this.uint, this.intPtr], ], - pending_inbound_transactions_destroy: ["void", ["pointer"]], + pending_inbound_transactions_destroy: [this.void, [this.ptr]], comms_config_create: [ - "pointer", + this.ptr, [ - "string", - "pointer", - "string", - "string", - "uint64", - "uint64", - "string", - "int*", + this.string, + this.ptr, + this.string, + this.string, + this.uint64, + this.uint64, + this.string, + this.intPtr, ], ], - comms_config_destroy: ["void", ["pointer"]], + comms_config_destroy: [this.void, [this.ptr]], wallet_create: [ - "pointer", + this.ptr, [ - "pointer", - "string", - "uint", - "uint", - "string", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "pointer", - "bool*", - "int*", + this.ptr, + this.string, + this.uint, + this.uint, + this.string, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.ptr, + this.boolPtr, + this.intPtr, ], ], - wallet_sign_message: ["char*", ["pointer", "string", "int*"]], + wallet_sign_message: [ + this.stringPtr, + [this.ptr, this.string, this.intPtr], + ], wallet_verify_message_signature: [ - "bool", - ["pointer", "pointer", "string", "string", "int*"], + this.bool, + [this.ptr, this.ptr, this.string, this.string, this.intPtr], ], wallet_add_base_node_peer: [ - "bool", - ["pointer", "pointer", "string", "int*"], + this.bool, + [this.ptr, this.ptr, this.string, this.intPtr], + ], + wallet_upsert_contact: [this.bool, [this.ptr, this.ptr, this.intPtr]], + wallet_remove_contact: [this.bool, [this.ptr, this.ptr, this.intPtr]], + wallet_get_available_balance: [this.uint64, [this.ptr, this.intPtr]], + wallet_get_pending_incoming_balance: [ + this.uint64, + [this.ptr, this.intPtr], + ], + wallet_get_pending_outgoing_balance: [ + this.uint64, + [this.ptr, this.intPtr], ], - wallet_upsert_contact: ["bool", ["pointer", "pointer", "int*"]], - wallet_remove_contact: ["bool", ["pointer", "pointer", "int*"]], - wallet_get_available_balance: ["uint64", ["pointer", "int*"]], - wallet_get_pending_incoming_balance: ["uint64", ["pointer", "int*"]], - wallet_get_pending_outgoing_balance: ["uint64", ["pointer", "int*"]], wallet_get_fee_estimate: [ - "uint64", - ["pointer", "uint64", "uint64", "uint64", "uint64", "int*"], + this.uint64, + [ + this.ptr, + this.uint64, + this.uint64, + this.uint64, + this.uint64, + this.intPtr, + ], + ], + wallet_get_num_confirmations_required: [ + this.uint64, + [this.ptr, this.intPtr], ], - wallet_get_num_confirmations_required: ["uint64", ["pointer", "int*"]], wallet_set_num_confirmations_required: [ - "void", - ["pointer", "uint64", "int*"], + this.void, + [this.ptr, this.uint64, this.intPtr], ], wallet_send_transaction: [ - "uint64", - ["pointer", "pointer", "uint64", "uint64", "string", "int*"], + this.uint64, + [ + this.ptr, + this.ptr, + this.uint64, + this.uint64, + this.string, + this.intPtr, + ], ], - wallet_get_contacts: ["pointer", ["pointer", "int*"]], - wallet_get_completed_transactions: ["pointer", ["pointer", "int*"]], + wallet_get_contacts: [this.ptr, [this.ptr, this.intPtr]], + wallet_get_completed_transactions: [this.ptr, [this.ptr, this.intPtr]], wallet_get_pending_outbound_transactions: [ - "pointer", - ["pointer", "int*"], + this.ptr, + [this.ptr, this.intPtr], + ], + wallet_get_public_key: [this.ptr, [this.ptr, this.intPtr]], + wallet_get_pending_inbound_transactions: [ + this.ptr, + [this.ptr, this.intPtr], ], - wallet_get_public_key: ["pointer", ["pointer", "int*"]], - wallet_get_pending_inbound_transactions: ["pointer", ["pointer", "int*"]], - wallet_get_cancelled_transactions: ["pointer", ["pointer", "int*"]], + wallet_get_cancelled_transactions: [this.ptr, [this.ptr, this.intPtr]], wallet_get_completed_transaction_by_id: [ - "pointer", - ["pointer", "uint64", "int*"], + this.ptr, + [this.ptr, this.uint64, this.intPtr], ], wallet_get_pending_outbound_transaction_by_id: [ - "pointer", - ["pointer", "uint64", "int*"], + this.ptr, + [this.ptr, this.uint64, this.intPtr], ], wallet_get_pending_inbound_transaction_by_id: [ - "pointer", - ["pointer", "uint64", "int*"], + this.ptr, + [this.ptr, this.uint64, this.intPtr], ], wallet_get_cancelled_transaction_by_id: [ - "pointer", - ["pointer", "uint64", "int*"], + this.ptr, + [this.ptr, this.uint64, this.intPtr], ], wallet_import_utxo: [ - "uint64", - ["pointer", "uint64", "pointer", "pointer", "string", "int*"], + this.uint64, + [this.ptr, this.uint64, this.ptr, this.ptr, this.string, this.intPtr], ], - wallet_start_utxo_validation: ["uint64", ["pointer", "int*"]], - wallet_start_stxo_validation: ["uint64", ["pointer", "int*"]], - wallet_start_invalid_txo_validation: ["uint64", ["pointer", "int*"]], - wallet_start_transaction_validation: ["uint64", ["pointer", "int*"]], - wallet_restart_transaction_broadcast: ["bool", ["pointer", "int*"]], - wallet_set_low_power_mode: ["void", ["pointer", "int*"]], - wallet_set_normal_power_mode: ["void", ["pointer", "int*"]], + wallet_start_utxo_validation: [this.uint64, [this.ptr, this.intPtr]], + wallet_start_stxo_validation: [this.uint64, [this.ptr, this.intPtr]], + wallet_start_invalid_txo_validation: [ + this.uint64, + [this.ptr, this.intPtr], + ], + wallet_start_transaction_validation: [ + this.uint64, + [this.ptr, this.intPtr], + ], + wallet_restart_transaction_broadcast: [ + this.bool, + [this.ptr, this.intPtr], + ], + wallet_set_low_power_mode: [this.void, [this.ptr, this.intPtr]], + wallet_set_normal_power_mode: [this.void, [this.ptr, this.intPtr]], wallet_cancel_pending_transaction: [ - "bool", - ["pointer", "uint64", "int*"], + this.bool, + [this.ptr, this.uint64, this.intPtr], ], wallet_coin_split: [ - "uint64", - ["pointer", "uint64", "uint64", "uint64", "string", "uint64", "int*"], + this.uint64, + [ + this.ptr, + this.uint64, + this.uint64, + this.uint64, + this.string, + this.uint64, + this.intPtr, + ], ], - wallet_get_seed_words: ["pointer", ["pointer", "int*"]], - wallet_apply_encryption: ["void", ["pointer", "string", "int*"]], - wallet_remove_encryption: ["void", ["pointer", "int*"]], - wallet_set_key_value: ["bool", ["pointer", "string", "string", "int*"]], - wallet_get_value: ["char*", ["pointer", "string", "int*"]], - wallet_clear_value: ["bool", ["pointer", "string", "int*"]], - wallet_is_recovery_in_progress: ["bool", ["pointer", "int*"]], + wallet_get_seed_words: [this.ptr, [this.ptr, this.intPtr]], + wallet_apply_encryption: [ + this.void, + [this.ptr, this.string, this.intPtr], + ], + wallet_remove_encryption: [this.void, [this.ptr, this.intPtr]], + wallet_set_key_value: [ + this.bool, + [this.ptr, this.string, this.string, this.intPtr], + ], + wallet_get_value: [this.stringPtr, [this.ptr, this.string, this.intPtr]], + wallet_clear_value: [this.bool, [this.ptr, this.string, this.intPtr]], + wallet_is_recovery_in_progress: [this.bool, [this.ptr, this.intPtr]], wallet_start_recovery: [ - "bool", - ["pointer", "pointer", "pointer", "int*"], + this.bool, + [this.ptr, this.ptr, this.ptr, this.intPtr], ], - wallet_destroy: ["void", ["pointer"]], - file_partial_backup: ["void", ["string", "string", "int*"]], - log_debug_message: ["void", ["string"]], - get_emoji_set: ["pointer", ["void"]], - emoji_set_destroy: ["void", ["pointer"]], - emoji_set_get_at: ["pointer", ["pointer", "uint", "int*"]], - emoji_set_get_length: ["uint", ["pointer", "int*"]], + wallet_destroy: [this.void, [this.ptr]], + file_partial_backup: [this.void, [this.string, this.string, this.intPtr]], + log_debug_message: [this.void, [this.string]], + get_emoji_set: [this.ptr, []], + emoji_set_destroy: [this.void, [this.ptr]], + emoji_set_get_at: [this.ptr, [this.ptr, this.uint, this.intPtr]], + emoji_set_get_length: [this.uint, [this.ptr, this.intPtr]], }); + + this.#loaded = true; } //endregion @@ -328,9 +438,7 @@ class InterfaceFFI { //region Helpers static initError() { - let error = Buffer.alloc(4); - error.writeInt32LE(-1, 0); - error.type = ref.types.int; + let error = ref.alloc(ref.types.int); return error; } @@ -993,57 +1101,57 @@ class InterfaceFFI { //region Callbacks static createCallbackReceivedTransaction(fn) { - return ffi.Callback("void", ["pointer"], fn); + return ffi.Callback(this.void, [this.ptr], fn); } static createCallbackReceivedTransactionReply(fn) { - return ffi.Callback("void", ["pointer"], fn); + return ffi.Callback(this.void, [this.ptr], fn); } static createCallbackReceivedFinalizedTransaction(fn) { - return ffi.Callback("void", ["pointer"], fn); + return ffi.Callback(this.void, [this.ptr], fn); } static createCallbackTransactionBroadcast(fn) { - return ffi.Callback("void", ["pointer"], fn); + return ffi.Callback(this.void, [this.ptr], fn); } static createCallbackTransactionMined(fn) { - return ffi.Callback("void", ["pointer"], fn); + return ffi.Callback(this.void, [this.ptr], fn); } static createCallbackTransactionMinedUnconfirmed(fn) { - return ffi.Callback("void", ["pointer", "uint64"], fn); + return ffi.Callback(this.void, [this.ptr, this.uint64], fn); } static createCallbackDirectSendResult(fn) { - return ffi.Callback("void", ["uint64", "bool"], fn); + return ffi.Callback(this.void, [this.uint64, this.bool], fn); } static createCallbackStoreAndForwardSendResult(fn) { - return ffi.Callback("void", ["uint64", "bool"], fn); + return ffi.Callback(this.void, [this.uint64, this.bool], fn); } static createCallbackTransactionCancellation(fn) { - return ffi.Callback("void", ["pointer"], fn); + return ffi.Callback(this.void, [this.ptr], fn); } static createCallbackUtxoValidationComplete(fn) { - return ffi.Callback("void", ["uint64", "uchar"], fn); + return ffi.Callback(this.void, [this.uint64, this.uchar], fn); } static createCallbackStxoValidationComplete(fn) { - return ffi.Callback("void", ["uint64", "uchar"], fn); + return ffi.Callback(this.void, [this.uint64, this.uchar], fn); } static createCallbackInvalidTxoValidationComplete(fn) { - return ffi.Callback("void", ["uint64", "uchar"], fn); + return ffi.Callback(this.void, [this.uint64, this.uchar], fn); } static createCallbackTransactionValidationComplete(fn) { - return ffi.Callback("void", ["uint64", "uchar"], fn); + return ffi.Callback(this.void, [this.uint64, this.uchar], fn); } static createCallbackSafMessageReceived(fn) { - return ffi.Callback("void", ["void"], fn); + return ffi.Callback(this.void, [], fn); } static createRecoveryProgressCallback(fn) { - return ffi.Callback("void", ["uchar", "uint64", "uint64"], fn); + return ffi.Callback(this.void, [this.uchar, this.uint64, this.uint64], fn); } //endregion @@ -1097,9 +1205,6 @@ class InterfaceFFI { error ); this.checkErrorResult(error, `walletCreate`); - if (recovery_in_progress) { - console.log("Wallet recovery is in progress"); - } return result; } diff --git a/integration_tests/helpers/ffi/pendingInboundTransaction.js b/integration_tests/helpers/ffi/pendingInboundTransaction.js index dc2071e24bb..6df175985e0 100644 --- a/integration_tests/helpers/ffi/pendingInboundTransaction.js +++ b/integration_tests/helpers/ffi/pendingInboundTransaction.js @@ -2,67 +2,53 @@ const InterfaceFFI = require("./ffiInterface"); const PublicKey = require("./publicKey"); class PendingInboundTransaction { - #tari_pending_inbound_transaction_ptr; + #ptr; pointerAssign(ptr) { - if (this.#tari_pending_inbound_transaction_ptr) { + if (this.#ptr) { this.destroy(); - this.#tari_pending_inbound_transaction_ptr = ptr; + this.#ptr = ptr; } else { - this.#tari_pending_inbound_transaction_ptr = ptr; + this.#ptr = ptr; } } getPtr() { - return this.#tari_pending_inbound_transaction_ptr; + return this.#ptr; } getSourcePublicKey() { let result = new PublicKey(); result.pointerAssign( - InterfaceFFI.pendingInboundTransactionGetSourcePublicKey( - this.#tari_pending_inbound_transaction_ptr - ) + InterfaceFFI.pendingInboundTransactionGetSourcePublicKey(this.#ptr) ); return result; } getAmount() { - return InterfaceFFI.pendingInboundTransactionGetAmount( - this.#tari_pending_inbound_transaction_ptr - ); + return InterfaceFFI.pendingInboundTransactionGetAmount(this.#ptr); } getMessage() { - return InterfaceFFI.pendingInboundTransactionGetMessage( - this.#tari_pending_inbound_transaction_ptr - ); + return InterfaceFFI.pendingInboundTransactionGetMessage(this.#ptr); } getStatus() { - return InterfaceFFI.pendingInboundTransactionGetStatus( - this.#tari_pending_inbound_transaction_ptr - ); + return InterfaceFFI.pendingInboundTransactionGetStatus(this.#ptr); } getTransactionID() { - return InterfaceFFI.pendingInboundTransactionGetTransactionId( - this.#tari_pending_inbound_transaction_ptr - ); + return InterfaceFFI.pendingInboundTransactionGetTransactionId(this.#ptr); } getTimestamp() { - return InterfaceFFI.pendingInboundTransactionGetTimestamp( - this.#tari_pending_inbound_transaction_ptr - ); + return InterfaceFFI.pendingInboundTransactionGetTimestamp(this.#ptr); } destroy() { - if (this.#tari_pending_inbound_transaction_ptr) { - InterfaceFFI.pendingInboundTransactionDestroy( - this.#tari_pending_inbound_transaction_ptr - ); - this.#tari_pending_inbound_transaction_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.pendingInboundTransactionDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/pendingInboundTransactions.js b/integration_tests/helpers/ffi/pendingInboundTransactions.js index 2500b41a047..7089b211f11 100644 --- a/integration_tests/helpers/ffi/pendingInboundTransactions.js +++ b/integration_tests/helpers/ffi/pendingInboundTransactions.js @@ -2,35 +2,28 @@ const PendingInboundTransaction = require("./pendingInboundTransaction"); const InterfaceFFI = require("./ffiInterface"); class PendingInboundTransactions { - #tari_pending_inbound_transactions_ptr; + #ptr; constructor(ptr) { - this.#tari_pending_inbound_transactions_ptr = ptr; + this.#ptr = ptr; } getLength() { - return InterfaceFFI.pendingInboundTransactionsGetLength( - this.#tari_pending_inbound_transactions_ptr - ); + return InterfaceFFI.pendingInboundTransactionsGetLength(this.#ptr); } getAt(position) { let result = new PendingInboundTransaction(); result.pointerAssign( - InterfaceFFI.pendingInboundTransactionsGetAt( - this.#tari_pending_inbound_transactions_ptr, - position - ) + InterfaceFFI.pendingInboundTransactionsGetAt(this.#ptr, position) ); return result; } destroy() { - if (this.#tari_pending_inbound_transactions_ptr) { - InterfaceFFI.pendingInboundTransactionsDestroy( - this.#tari_pending_inbound_transactions_ptr - ); - this.#tari_pending_inbound_transactions_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.pendingInboundTransactionsDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/pendingOutboundTransaction.js b/integration_tests/helpers/ffi/pendingOutboundTransaction.js index 0fc2ca47b96..1e21a1767c9 100644 --- a/integration_tests/helpers/ffi/pendingOutboundTransaction.js +++ b/integration_tests/helpers/ffi/pendingOutboundTransaction.js @@ -2,73 +2,57 @@ const InterfaceFFI = require("./ffiInterface"); const PublicKey = require("./publicKey"); class PendingOutboundTransaction { - #tari_pending_outbound_transaction_ptr; + #ptr; pointerAssign(ptr) { - if (this.#tari_pending_outbound_transaction_ptr) { - this.#tari_pending_outbound_transaction_ptr = ptr; + if (this.#ptr) { + this.#ptr = ptr; this.destroy(); } else { - this.#tari_pending_outbound_transaction_ptr = ptr; + this.#ptr = ptr; } } getPtr() { - return this.#tari_pending_outbound_transaction_ptr; + return this.#ptr; } getDestinationPublicKey() { let result = new PublicKey(); result.pointerAssign( - InterfaceFFI.pendingOutboundTransactionGetDestinationPublicKey( - this.#tari_pending_outbound_transaction_ptr - ) + InterfaceFFI.pendingOutboundTransactionGetDestinationPublicKey(this.#ptr) ); return result; } getAmount() { - return InterfaceFFI.pendingOutboundTransactionGetAmount( - this.#tari_pending_outbound_transaction_ptr - ); + return InterfaceFFI.pendingOutboundTransactionGetAmount(this.#ptr); } getFee() { - return InterfaceFFI.pendingOutboundTransactionGetFee( - this.#tari_pending_outbound_transaction_ptr - ); + return InterfaceFFI.pendingOutboundTransactionGetFee(this.#ptr); } getMessage() { - return InterfaceFFI.pendingOutboundTransactionGetMessage( - this.#tari_pending_outbound_transaction_ptr - ); + return InterfaceFFI.pendingOutboundTransactionGetMessage(this.#ptr); } getStatus() { - return InterfaceFFI.pendingOutboundTransactionGetStatus( - this.#tari_pending_outbound_transaction_ptr - ); + return InterfaceFFI.pendingOutboundTransactionGetStatus(this.#ptr); } getTransactionID() { - return InterfaceFFI.pendingOutboundTransactionGetTransactionId( - this.#tari_pending_outbound_transaction_ptr - ); + return InterfaceFFI.pendingOutboundTransactionGetTransactionId(this.#ptr); } getTimestamp() { - return InterfaceFFI.pendingOutboundTransactionGetTimestamp( - this.#tari_pending_outbound_transaction_ptr - ); + return InterfaceFFI.pendingOutboundTransactionGetTimestamp(this.#ptr); } destroy() { - if (this.#tari_pending_outbound_transaction_ptr) { - InterfaceFFI.pendingOutboundTransactionDestroy( - this.#tari_pending_outbound_transaction_ptr - ); - this.#tari_pending_outbound_transaction_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.pendingOutboundTransactionDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/pendingOutboundTransactions.js b/integration_tests/helpers/ffi/pendingOutboundTransactions.js index 45de06033b7..a849218fc4c 100644 --- a/integration_tests/helpers/ffi/pendingOutboundTransactions.js +++ b/integration_tests/helpers/ffi/pendingOutboundTransactions.js @@ -2,35 +2,28 @@ const PendingOutboundTransaction = require("./pendingOutboundTransaction"); const InterfaceFFI = require("./ffiInterface"); class PendingOutboundTransactions { - #tari_pending_outbound_transactions_ptr; + #ptr = undefined; constructor(ptr) { - this.#tari_pending_outbound_transactions_ptr = ptr; + this.#ptr = ptr; } getLength() { - return InterfaceFFI.pendingOutboundTransactionsGetLength( - this.#tari_pending_outbound_transactions_ptr - ); + return InterfaceFFI.pendingOutboundTransactionsGetLength(this.#ptr); } getAt(position) { let result = new PendingOutboundTransaction(); result.pointerAssign( - InterfaceFFI.pendingOutboundTransactionsGetAt( - this.#tari_pending_outbound_transactions_ptr, - position - ) + InterfaceFFI.pendingOutboundTransactionsGetAt(this.#ptr, position) ); return result; } destroy() { - if (this.#tari_pending_outbound_transactions_ptr) { - InterfaceFFI.pendingOutboundTransactionsDestroy( - this.#tari_pending_outbound_transactions_ptr - ); - this.#tari_pending_outbound_transactions_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.pendingOutboundTransactionsDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/privateKey.js b/integration_tests/helpers/ffi/privateKey.js index 7115ab8a1d4..34ec3d7dd5d 100644 --- a/integration_tests/helpers/ffi/privateKey.js +++ b/integration_tests/helpers/ffi/privateKey.js @@ -3,20 +3,19 @@ const ByteVector = require("./byteVector"); const utf8 = require("utf8"); class PrivateKey { - #tari_private_key_ptr; - + #ptr; pointerAssign(ptr) { // Prevent pointer from being leaked in case of re-assignment - if (this.#tari_private_key_ptr) { - this.#tari_private_key_ptr = ptr; + if (this.#ptr) { + this.#ptr = ptr; this.destroy(); } else { - this.#tari_private_key_ptr = ptr; + this.#ptr = ptr; } } generate() { - this.#tari_private_key_ptr = InterfaceFFI.privateKeyGenerate(); + this.#ptr = InterfaceFFI.privateKeyGenerate(); } fromHexString(hex) { @@ -33,14 +32,12 @@ class PrivateKey { } getPtr() { - return this.#tari_private_key_ptr; + return this.#ptr; } getBytes() { let result = new ByteVector(); - result.pointerAssign( - InterfaceFFI.privateKeyGetBytes(this.#tari_private_key_ptr) - ); + result.pointerAssign(InterfaceFFI.privateKeyGetBytes(this.#ptr)); return result; } @@ -57,9 +54,9 @@ class PrivateKey { } destroy() { - if (this.#tari_private_key_ptr) { - InterfaceFFI.privateKeyDestroy(this.#tari_private_key_ptr); - this.#tari_private_key_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.privateKeyDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/publicKey.js b/integration_tests/helpers/ffi/publicKey.js index 7e1476c3d58..51706ca0cda 100644 --- a/integration_tests/helpers/ffi/publicKey.js +++ b/integration_tests/helpers/ffi/publicKey.js @@ -3,15 +3,15 @@ const ByteVector = require("./byteVector"); const utf8 = require("utf8"); class PublicKey { - #tari_public_key_ptr; + #ptr; pointerAssign(ptr) { // Prevent pointer from being leaked in case of re-assignment - if (this.#tari_public_key_ptr) { + if (this.#ptr) { this.destroy(); - this.#tari_public_key_ptr = ptr; + this.#ptr = ptr; } else { - this.#tari_public_key_ptr = ptr; + this.#ptr = ptr; } } @@ -42,14 +42,12 @@ class PublicKey { } getPtr() { - return this.#tari_public_key_ptr; + return this.#ptr; } getBytes() { let result = new ByteVector(); - result.pointerAssign( - InterfaceFFI.publicKeyGetBytes(this.#tari_public_key_ptr) - ); + result.pointerAssign(InterfaceFFI.publicKeyGetBytes(this.#ptr)); return result; } @@ -66,16 +64,16 @@ class PublicKey { } getEmojiId() { - const emoji_id = InterfaceFFI.publicKeyToEmojiId(this.#tari_public_key_ptr); - const result = emoji_id.readCString(); + let emoji_id = InterfaceFFI.publicKeyToEmojiId(this.#ptr); + let result = emoji_id.readCString(); InterfaceFFI.stringDestroy(emoji_id); return result; } destroy() { - if (this.#tari_public_key_ptr) { - InterfaceFFI.publicKeyDestroy(this.#tari_public_key_ptr); - this.#tari_public_key_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.publicKeyDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/seedWords.js b/integration_tests/helpers/ffi/seedWords.js index e191bc38a9e..f0fba891aa5 100644 --- a/integration_tests/helpers/ffi/seedWords.js +++ b/integration_tests/helpers/ffi/seedWords.js @@ -2,15 +2,15 @@ const InterfaceFFI = require("./ffiInterface"); const utf8 = require("utf8"); class SeedWords { - #tari_seed_words_ptr; + #ptr; pointerAssign(ptr) { // Prevent pointer from being leaked in case of re-assignment - if (this.#tari_seed_words_ptr) { + if (this.#ptr) { this.destroy(); - this.#tari_seed_words_ptr = ptr; + this.#ptr = ptr; } else { - this.#tari_seed_words_ptr = ptr; + this.#ptr = ptr; } } @@ -28,27 +28,24 @@ class SeedWords { } getLength() { - return InterfaceFFI.seedWordsGetLength(this.#tari_seed_words_ptr); + return InterfaceFFI.seedWordsGetLength(this.#ptr); } getPtr() { - return this.#tari_seed_words_ptr; + return this.#ptr; } getAt(position) { - const seed_word = InterfaceFFI.seedWordsGetAt( - this.#tari_seed_words_ptr, - position - ); - const result = seed_word.readCString(); + let seed_word = InterfaceFFI.seedWordsGetAt(this.#ptr, position); + let result = seed_word.readCString(); InterfaceFFI.stringDestroy(seed_word); return result; } destroy() { - if (this.#tari_seed_words_ptr) { - InterfaceFFI.seedWordsDestroy(this.#tari_seed_words_ptr); - this.#tari_seed_words_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.seedWordsDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/transportType.js b/integration_tests/helpers/ffi/transportType.js index 0826c423b50..bd21e45a93d 100644 --- a/integration_tests/helpers/ffi/transportType.js +++ b/integration_tests/helpers/ffi/transportType.js @@ -2,23 +2,23 @@ const InterfaceFFI = require("./ffiInterface"); const utf8 = require("utf8"); class TransportType { - #tari_transport_type_ptr; + #ptr; #type = "None"; pointerAssign(ptr, type) { // Prevent pointer from being leaked in case of re-assignment - if (this.#tari_transport_type_ptr) { + if (this.#ptr) { this.destroy(); - this.#tari_transport_type_ptr = ptr; + this.#ptr = ptr; this.#type = type; } else { - this.#tari_transport_type_ptr = ptr; + this.#ptr = ptr; this.#type = type; } } getPtr() { - return this.#tari_transport_type_ptr; + return this.#ptr; } getType() { @@ -75,9 +75,9 @@ class TransportType { destroy() { this.#type = "None"; - if (this.#tari_transport_type_ptr) { - InterfaceFFI.transportTypeDestroy(this.#tari_transport_type_ptr); - this.#tari_transport_type_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.transportTypeDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault } } } diff --git a/integration_tests/helpers/ffi/wallet.js b/integration_tests/helpers/ffi/wallet.js index fea21fe6825..297059894d7 100644 --- a/integration_tests/helpers/ffi/wallet.js +++ b/integration_tests/helpers/ffi/wallet.js @@ -11,7 +11,7 @@ const Contacts = require("./contacts"); const utf8 = require("utf8"); class Wallet { - #wallet_ptr; + #ptr; #log_path = ""; receivedTransaction = 0; receivedTransactionReply = 0; @@ -24,6 +24,22 @@ class Wallet { stxo_validation_complete = false; stxo_validation_result = 0; + #callback_received_transaction; + #callback_received_transaction_reply; + #callback_received_finalized_transaction; + #callback_transaction_broadcast; + #callback_transaction_mined; + #callback_transaction_mined_unconfirmed; + #callback_direct_send_result; + #callback_store_and_forward_send_result; + #callback_transaction_cancellation; + #callback_utxo_validation_complete; + #callback_stxo_validation_complete; + #callback_invalid_txo_validation_complete; + #callback_transaction_validation_complete; + #callback_saf_message_received; + #recoveryProgressCallback; + getUtxoValidationStatus() { return { utxo_validation_complete: this.utxo_validation_complete, @@ -71,6 +87,61 @@ class Wallet { num_rolling_log_file = 50, log_size_bytes = 102400 ) { + //region Callbacks + this.#callback_received_transaction = + InterfaceFFI.createCallbackReceivedTransaction( + this.#onReceivedTransaction + ); + this.#callback_received_transaction_reply = + InterfaceFFI.createCallbackReceivedTransactionReply( + this.#onReceivedTransactionReply + ); + this.#callback_received_finalized_transaction = + InterfaceFFI.createCallbackReceivedFinalizedTransaction( + this.#onReceivedFinalizedTransaction + ); + this.#callback_transaction_broadcast = + InterfaceFFI.createCallbackTransactionBroadcast( + this.#onTransactionBroadcast + ); + this.#callback_transaction_mined = + InterfaceFFI.createCallbackTransactionMined(this.#onTransactionMined); + this.#callback_transaction_mined_unconfirmed = + InterfaceFFI.createCallbackTransactionMinedUnconfirmed( + this.#onTransactionMinedUnconfirmed + ); + this.#callback_direct_send_result = + InterfaceFFI.createCallbackDirectSendResult(this.#onDirectSendResult); + this.#callback_store_and_forward_send_result = + InterfaceFFI.createCallbackStoreAndForwardSendResult( + this.#onStoreAndForwardSendResult + ); + this.#callback_transaction_cancellation = + InterfaceFFI.createCallbackTransactionCancellation( + this.#onTransactionCancellation + ); + this.#callback_utxo_validation_complete = + InterfaceFFI.createCallbackUtxoValidationComplete( + this.#onUtxoValidationComplete + ); + this.#callback_stxo_validation_complete = + InterfaceFFI.createCallbackStxoValidationComplete( + this.#onStxoValidationComplete + ); + this.#callback_invalid_txo_validation_complete = + InterfaceFFI.createCallbackInvalidTxoValidationComplete( + this.#onInvalidTxoValidationComplete + ); + this.#callback_transaction_validation_complete = + InterfaceFFI.createCallbackTransactionValidationComplete( + this.#onTransactionValidationComplete + ); + this.#callback_saf_message_received = + InterfaceFFI.createCallbackSafMessageReceived(this.#onSafMessageReceived); + this.#recoveryProgressCallback = + InterfaceFFI.createRecoveryProgressCallback(this.#onRecoveryProgress); + //endregion + this.receivedTransaction = 0; this.receivedTransactionReply = 0; this.transactionBroadcast = 0; @@ -89,7 +160,7 @@ class Wallet { words = seed_words_ptr; } this.#log_path = log_path; - this.#wallet_ptr = InterfaceFFI.walletCreate( + this.#ptr = InterfaceFFI.walletCreate( comms_config_ptr, utf8.encode(this.#log_path), //`${this.baseDir}/log/wallet.log`, num_rolling_log_file, @@ -113,7 +184,6 @@ class Wallet { ); } - //region Callbacks #onReceivedTransaction = (ptr) => { // refer to outer scope in callback function otherwise this is null let tx = new PendingInboundTransaction(); @@ -243,65 +313,10 @@ class Wallet { } }; - #callback_received_transaction = - InterfaceFFI.createCallbackReceivedTransaction(this.#onReceivedTransaction); - #callback_received_transaction_reply = - InterfaceFFI.createCallbackReceivedTransactionReply( - this.#onReceivedTransactionReply - ); - #callback_received_finalized_transaction = - InterfaceFFI.createCallbackReceivedFinalizedTransaction( - this.#onReceivedFinalizedTransaction - ); - #callback_transaction_broadcast = - InterfaceFFI.createCallbackTransactionBroadcast( - this.#onTransactionBroadcast - ); - #callback_transaction_mined = InterfaceFFI.createCallbackTransactionMined( - this.#onTransactionMined - ); - #callback_transaction_mined_unconfirmed = - InterfaceFFI.createCallbackTransactionMinedUnconfirmed( - this.#onTransactionMinedUnconfirmed - ); - #callback_direct_send_result = InterfaceFFI.createCallbackDirectSendResult( - this.#onDirectSendResult - ); - #callback_store_and_forward_send_result = - InterfaceFFI.createCallbackStoreAndForwardSendResult( - this.#onStoreAndForwardSendResult - ); - #callback_transaction_cancellation = - InterfaceFFI.createCallbackTransactionCancellation( - this.#onTransactionCancellation - ); - #callback_utxo_validation_complete = - InterfaceFFI.createCallbackUtxoValidationComplete( - this.#onUtxoValidationComplete - ); - #callback_stxo_validation_complete = - InterfaceFFI.createCallbackStxoValidationComplete( - this.#onStxoValidationComplete - ); - #callback_invalid_txo_validation_complete = - InterfaceFFI.createCallbackInvalidTxoValidationComplete( - this.#onInvalidTxoValidationComplete - ); - #callback_transaction_validation_complete = - InterfaceFFI.createCallbackTransactionValidationComplete( - this.#onTransactionValidationComplete - ); - #callback_saf_message_received = - InterfaceFFI.createCallbackSafMessageReceived(this.#onSafMessageReceived); - #recoveryProgressCallback = InterfaceFFI.createRecoveryProgressCallback( - this.#onRecoveryProgress - ); - //endregion - startRecovery(base_node_pubkey) { let node_pubkey = PublicKey.fromHexString(utf8.encode(base_node_pubkey)); InterfaceFFI.walletStartRecovery( - this.#wallet_ptr, + this.#ptr, node_pubkey.getPtr(), this.#recoveryProgressCallback ); @@ -309,11 +324,11 @@ class Wallet { } recoveryInProgress() { - return InterfaceFFI.walletIsRecoveryInProgress(this.#wallet_ptr); + return InterfaceFFI.walletIsRecoveryInProgress(this.#ptr); } getPublicKey() { - let ptr = InterfaceFFI.walletGetPublicKey(this.#wallet_ptr); + let ptr = InterfaceFFI.walletGetPublicKey(this.#ptr); let pk = new PublicKey(); pk.pointerAssign(ptr); let result = pk.getHex(); @@ -322,7 +337,7 @@ class Wallet { } getEmojiId() { - let ptr = InterfaceFFI.walletGetPublicKey(this.#wallet_ptr); + let ptr = InterfaceFFI.walletGetPublicKey(this.#ptr); let pk = new PublicKey(); pk.pointerAssign(ptr); let result = pk.getEmojiId(); @@ -331,12 +346,12 @@ class Wallet { } getBalance() { - let available = InterfaceFFI.walletGetAvailableBalance(this.#wallet_ptr); + let available = InterfaceFFI.walletGetAvailableBalance(this.#ptr); let pendingIncoming = InterfaceFFI.walletGetPendingIncomingBalance( - this.#wallet_ptr + this.#ptr ); let pendingOutgoing = InterfaceFFI.walletGetPendingOutgoingBalance( - this.#wallet_ptr + this.#ptr ); return { pendingIn: pendingIncoming, @@ -348,7 +363,7 @@ class Wallet { addBaseNodePeer(public_key_hex, address) { let public_key = PublicKey.fromHexString(utf8.encode(public_key_hex)); let result = InterfaceFFI.walletAddBaseNodePeer( - this.#wallet_ptr, + this.#ptr, public_key.getPtr(), utf8.encode(address) ); @@ -359,7 +374,7 @@ class Wallet { sendTransaction(destination, amount, fee_per_gram, message) { let dest_public_key = PublicKey.fromHexString(utf8.encode(destination)); let result = InterfaceFFI.walletSendTransaction( - this.#wallet_ptr, + this.#ptr, dest_public_key.getPtr(), amount, fee_per_gram, @@ -370,35 +385,26 @@ class Wallet { } applyEncryption(passphrase) { - InterfaceFFI.walletApplyEncryption( - this.#wallet_ptr, - utf8.encode(passphrase) - ); + InterfaceFFI.walletApplyEncryption(this.#ptr, utf8.encode(passphrase)); } getCompletedTransactions() { - let list_ptr = InterfaceFFI.walletGetCompletedTransactions( - this.#wallet_ptr - ); + let list_ptr = InterfaceFFI.walletGetCompletedTransactions(this.#ptr); return new CompletedTransactions(list_ptr); } getInboundTransactions() { - let list_ptr = InterfaceFFI.walletGetPendingInboundTransactions( - this.#wallet_ptr - ); + let list_ptr = InterfaceFFI.walletGetPendingInboundTransactions(this.#ptr); return new PendingInboundTransactions(list_ptr); } getOutboundTransactions() { - let list_ptr = InterfaceFFI.walletGetPendingOutboundTransactions( - this.#wallet_ptr - ); + let list_ptr = InterfaceFFI.walletGetPendingOutboundTransactions(this.#ptr); return new PendingOutboundTransactions(list_ptr); } getContacts() { - let list_ptr = InterfaceFFI.walletGetContacts(this.#wallet_ptr); + let list_ptr = InterfaceFFI.walletGetContacts(this.#ptr); return new Contacts(list_ptr); } @@ -408,40 +414,50 @@ class Wallet { contact.pointerAssign( InterfaceFFI.contactCreate(utf8.encode(alias), public_key.getPtr()) ); - let result = InterfaceFFI.walletUpsertContact( - this.#wallet_ptr, - contact.getPtr() - ); + let result = InterfaceFFI.walletUpsertContact(this.#ptr, contact.getPtr()); contact.destroy(); public_key.destroy(); return result; } removeContact(contact) { - let result = InterfaceFFI.walletRemoveContact( - this.#wallet_ptr, - contact.getPtr() - ); + let result = InterfaceFFI.walletRemoveContact(this.#ptr, contact.getPtr()); contact.destroy(); return result; } cancelPendingTransaction(tx_id) { - return InterfaceFFI.walletCancelPendingTransaction(this.#wallet_ptr, tx_id); + return InterfaceFFI.walletCancelPendingTransaction(this.#ptr, tx_id); } startUtxoValidation() { - return InterfaceFFI.walletStartUtxoValidation(this.#wallet_ptr); + return InterfaceFFI.walletStartUtxoValidation(this.#ptr); } startStxoValidation() { - return InterfaceFFI.walletStartStxoValidation(this.#wallet_ptr); + return InterfaceFFI.walletStartStxoValidation(this.#ptr); } destroy() { - if (this.#wallet_ptr) { - InterfaceFFI.walletDestroy(this.#wallet_ptr); - this.#wallet_ptr = undefined; //prevent double free segfault + if (this.#ptr) { + InterfaceFFI.walletDestroy(this.#ptr); + this.#ptr = undefined; //prevent double free segfault + this.#callback_received_transaction = + this.#callback_received_transaction_reply = + this.#callback_received_finalized_transaction = + this.#callback_transaction_broadcast = + this.#callback_transaction_mined = + this.#callback_transaction_mined_unconfirmed = + this.#callback_direct_send_result = + this.#callback_store_and_forward_send_result = + this.#callback_transaction_cancellation = + this.#callback_utxo_validation_complete = + this.#callback_stxo_validation_complete = + this.#callback_invalid_txo_validation_complete = + this.#callback_transaction_validation_complete = + this.#callback_saf_message_received = + this.#recoveryProgressCallback = + undefined; // clear callback function pointers } } } diff --git a/integration_tests/helpers/walletFFIClient.js b/integration_tests/helpers/walletFFIClient.js index 8835f03deee..5239358b3d4 100644 --- a/integration_tests/helpers/walletFFIClient.js +++ b/integration_tests/helpers/walletFFIClient.js @@ -4,7 +4,6 @@ const CommsConfig = require("./ffi/commsConfig"); const Wallet = require("./ffi/wallet"); const { getFreePort } = require("./util"); const dateFormat = require("dateformat"); -const InterfaceFFI = require("./ffi/ffiInterface"); class WalletFFIClient { #name; @@ -20,10 +19,6 @@ class WalletFFIClient { this.#name = name; } - static async Init() { - await InterfaceFFI.Init(); - } - async startNew(seed_words_text, pass_phrase) { this.#port = await getFreePort(19000, 25000); const name = `WalletFFI${this.#port}-${this.#name}`; @@ -179,6 +174,9 @@ class WalletFFIClient { if (this.#comms_config) { this.#comms_config.destroy(); } + if (this.#transport) { + this.#transport.destroy(); + } if (this.#seed_words) { this.#seed_words.destroy(); }