Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable WAL mode in SQLite #53

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ public class IronfishNativeModule: Module {
}
}

AsyncFunction("createTransaction") { (spendComponents: SpendComponentsInput, outputs: OutputsInput, spendingKey: Data) throws -> Data in
AsyncFunction("createTransaction") { (transactionVersion: UInt8, spendComponents: SpendComponentsInput, outputs: OutputsInput, spendingKey: Data) throws -> Data in
let spendComponentsConverted = spendComponents.components.map { spendComponent in
let witnessAuthPath: [WitnessNode] = spendComponent.witnessAuthPath.map { WitnessNode(side: $0.side, hashOfSibling: Data(hexString: $0.hashOfSibling)!) }
return SpendComponents(note: Data(hexString: spendComponent.note)!, witnessRootHash: Data(hexString: spendComponent.witnessRootHash)!, witnessTreeSize: UInt64(spendComponent.witnessTreeSize)!, witnessAuthPath: witnessAuthPath)
}
do {
let transaction = try createTransaction(spendComponents: spendComponentsConverted, outputs: outputs.outputs.map {Data(hexString: $0)!}, spendingKey: spendingKey)
let transaction = try createTransaction(transactionVersion: transactionVersion, spendComponents: spendComponentsConverted, outputs: outputs.outputs.map {Data(hexString: $0)!}, spendingKey: spendingKey)
return transaction
} catch let error as NSError {
print("Unexpected error: \(error.debugDescription)")
Expand Down
9 changes: 7 additions & 2 deletions packages/ironfish-native-module/rust_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,17 @@ pub fn create_note(params: NoteParams) -> Result<Vec<u8>, EnumError> {

#[uniffi::export]
pub fn create_transaction(
transaction_version: u8,
spend_components: Vec<SpendComponents>,
outputs: Vec<Vec<u8>>,
spending_key: Vec<u8>,
) -> Result<Vec<u8>, EnumError> {
let mut transaction =
ironfish::ProposedTransaction::new(ironfish::transaction::TransactionVersion::V2);
let version = ironfish::transaction::TransactionVersion::from_u8(transaction_version)
.ok_or_else(|| EnumError::Error {
msg: "Invalid transaction version".to_string(),
})?;

let mut transaction = ironfish::ProposedTransaction::new(version);
for spend_component in spend_components {
let note_data = Cursor::new(spend_component.note);
let note =
Expand Down
2 changes: 2 additions & 0 deletions packages/ironfish-native-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export function createNote({
}

export function createTransaction(
transactionVersion: number,
spendComponents: {
note: string;
witnessRootHash: string;
Expand All @@ -134,6 +135,7 @@ export function createTransaction(
spendingKey: Uint8Array,
): Promise<Uint8Array> {
return IronfishNativeModule.createTransaction(
transactionVersion,
{ components: spendComponents },
{ outputs },
spendingKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/mobile-app/app/send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function Send() {
{getAccountResult.data?.balances.custom.map((b) => (
<Button
key={b.assetId}
title={`${b.assetId} (${getAccountResult.data?.balances.iron.confirmed ?? 0}) ${selectedAssetId === b.assetId ? "(selected)" : ""}`}
title={`${b.assetId} (${b.confirmed ?? 0}) ${selectedAssetId === b.assetId ? "(selected)" : ""}`}
onPress={() => setSelectedAssetId(b.assetId)}
/>
))}
Expand Down
6 changes: 6 additions & 0 deletions packages/mobile-app/data/wallet/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ export class WalletDb {
}),
});

// WAL mode is generally faster, and I don't think any of the listed
// downsides apply: https://www.sqlite.org/draft/wal.html
// So opting to default to enabling WAL mode and can disable if we run
// into issues on Android/iOS.
sql`PRAGMA journal_mode=WAL`.execute(db);

const migrator = new Migrator({
db: db,
provider: new ExpoMigrationProvider({
Expand Down
19 changes: 19 additions & 0 deletions packages/mobile-app/data/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AssetLoader } from "./assetLoader";
import { Blockchain } from "../blockchain";
import { Output } from "../facades/wallet/types";
import { WalletServerApi } from "../walletServerApi/walletServer";
import { Consensus, MAINNET, TESTNET } from "@ironfish/sdk";

type StartedState = { type: "STARTED"; db: WalletDb; assetLoader: AssetLoader };
type WalletState = { type: "STOPPED" } | { type: "LOADING" } | StartedState;
Expand Down Expand Up @@ -683,6 +684,21 @@ export class Wallet {
return this.state.assetLoader.getAsset(network, assetId);
}

private async getActiveTransactionVersion(network: Network) {
// TODO IFL-2898 Consider fetching the active transaction version from the API
// so we don't have to deploy a new version when setting the activation sequence.
const latestBlock = await Blockchain.getLatestBlock(network);
let consensus;
if (network === Network.MAINNET) {
consensus = new Consensus(MAINNET.consensus);
} else if (network === Network.TESTNET) {
consensus = new Consensus(TESTNET.consensus);
} else {
throw new Error("Unsupported network");
}
return consensus.getActiveTransactionVersion(latestBlock.sequence);
}

async sendTransaction(
network: Network,
accountName: string,
Expand Down Expand Up @@ -775,6 +791,8 @@ export class Wallet {
throw new Error("witnesses don't match");
}

const txnVersion = await this.getActiveTransactionVersion(network);

console.log(
`Witnesses and notes prepared in ${performance.now() - lastTime}ms`,
);
Expand All @@ -795,6 +813,7 @@ export class Wallet {
throw new Error("Spending key not found");
}
const result = await IronfishNativeModule.createTransaction(
txnVersion,
spendComponents,
notes.map((note) => Uint8ArrayUtils.toHex(note)),
Uint8ArrayUtils.fromHex(spendingKey),
Expand Down
Loading