Skip to content

Commit

Permalink
Merge pull request #3563 from iron-fish/staging
Browse files Browse the repository at this point in the history
STAGING -> MASTER (2/25/23)
  • Loading branch information
NullSoldier committed Feb 25, 2023
2 parents 46295bc + 815b64b commit 6fb75f4
Show file tree
Hide file tree
Showing 44 changed files with 1,630 additions and 42 deletions.
4 changes: 2 additions & 2 deletions ironfish-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ironfish",
"version": "0.1.69",
"version": "0.1.70",
"description": "CLI for running and interacting with an Iron Fish node",
"author": "Iron Fish <[email protected]> (https://ironfish.network)",
"main": "build/src/index.js",
Expand Down Expand Up @@ -61,7 +61,7 @@
"@aws-sdk/client-secrets-manager": "3.276.0",
"@aws-sdk/s3-request-presigner": "3.127.0",
"@ironfish/rust-nodejs": "0.1.27",
"@ironfish/sdk": "0.0.46",
"@ironfish/sdk": "0.0.47",
"@oclif/core": "1.23.1",
"@oclif/plugin-help": "5.1.12",
"@oclif/plugin-not-found": "2.3.1",
Expand Down
8 changes: 4 additions & 4 deletions ironfish-cli/src/commands/service/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ export default class Faucet extends IronfishCommand {

const response = await client.getAccountBalance({ account })

if (BigInt(response.content.confirmed) < BigInt(FAUCET_AMOUNT + FAUCET_FEE)) {
if (BigInt(response.content.available) < BigInt(FAUCET_AMOUNT + FAUCET_FEE)) {
if (!this.warnedFund) {
this.log(
`Faucet has insufficient funds. Needs ${FAUCET_AMOUNT + FAUCET_FEE} but has ${
response.content.confirmed
}. Waiting on more funds.`,
response.content.available
} available to spend. Waiting on more funds.`,
)

this.warnedFund = true
Expand All @@ -167,7 +167,7 @@ export default class Faucet extends IronfishCommand {
this.warnedFund = false

const maxPossibleRecipients = Math.min(
Number(BigInt(response.content.confirmed) / BigInt(FAUCET_AMOUNT + FAUCET_FEE)),
Number(BigInt(response.content.available) / BigInt(FAUCET_AMOUNT + FAUCET_FEE)),
MAX_RECIPIENTS_PER_TRANSACTION,
)

Expand Down
18 changes: 16 additions & 2 deletions ironfish-cli/src/commands/wallet/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export class BalanceCommand extends IronfishCommand {
this.log(`Account: ${response.content.account}`)
this.log(`Head Hash: ${response.content.blockHash || 'NULL'}`)
this.log(`Head Sequence: ${response.content.sequence || 'NULL'}`)
this.log(
`Available: ${CurrencyUtils.renderIron(response.content.available, true, assetId)}`,
)
this.log(
`Confirmed: ${CurrencyUtils.renderIron(response.content.confirmed, true, assetId)}`,
)
Expand All @@ -77,13 +80,20 @@ export class BalanceCommand extends IronfishCommand {
}

this.log(`Account: ${response.content.account}`)
this.log(`Balance: ${CurrencyUtils.renderIron(response.content.confirmed, true, assetId)}`)
this.log(
`Available Balance: ${CurrencyUtils.renderIron(
response.content.available,
true,
assetId,
)}`,
)
}

explainBalance(response: GetBalanceResponse, assetId: string): void {
const unconfirmed = CurrencyUtils.decode(response.unconfirmed)
const confirmed = CurrencyUtils.decode(response.confirmed)
const pending = CurrencyUtils.decode(response.pending)
const available = CurrencyUtils.decode(response.available)

const unconfirmedDelta = unconfirmed - confirmed
const pendingDelta = pending - unconfirmed
Expand All @@ -97,7 +107,11 @@ export class BalanceCommand extends IronfishCommand {
)
this.log('')

this.log(`Your confirmed balance is made of notes on the chain that are safe to spend`)
this.log(`Your available balance is made of notes on the chain that are safe to spend`)
this.log(`Available: ${CurrencyUtils.renderIron(available, true, assetId)}`)
this.log('')

this.log('Your confirmed balance includes all notes from transactions on the chain')
this.log(`Confirmed: ${CurrencyUtils.renderIron(confirmed, true, assetId)}`)
this.log('')

Expand Down
10 changes: 7 additions & 3 deletions ironfish-cli/src/commands/wallet/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,19 @@ export class BalancesCommand extends IronfishCommand {
assetId: {
header: 'Asset Id',
},
confirmed: {
header: 'Confirmed Balance',
get: (row) => CurrencyUtils.renderIron(row.confirmed),
available: {
header: 'Available Balance',
get: (row) => CurrencyUtils.renderIron(row.available),
},
}

if (flags.all) {
columns = {
...columns,
confirmed: {
header: 'Confirmed Balance',
get: (row) => CurrencyUtils.renderIron(row.confirmed),
},
unconfirmed: {
header: 'Unconfirmed Balance',
get: (row) => CurrencyUtils.renderIron(row.unconfirmed),
Expand Down
26 changes: 22 additions & 4 deletions ironfish-cli/src/commands/wallet/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class ImportCommand extends IronfishCommand {
}
}
CliUx.ux.error(
`Detected mnemonic input, but the import failed.
`Detected mnemonic input, but the import failed.
Please verify the input text or use a different method to import wallet`,
)
}
Expand All @@ -101,7 +101,16 @@ export class ImportCommand extends IronfishCommand {
// bech32 encoded json
const [decoded, _] = Bech32m.decode(data)
if (decoded) {
return JSONUtils.parse<AccountImport>(decoded)
let data = JSONUtils.parse<AccountImport>(decoded)

if (data.spendingKey) {
data = {
...data,
...generateKeyFromPrivateKey(data.spendingKey),
}
}

return data
}

// mnemonic or explicit spending key
Expand All @@ -118,7 +127,16 @@ export class ImportCommand extends IronfishCommand {

// raw json
try {
return JSONUtils.parse<AccountImport>(data)
let json = JSONUtils.parse<AccountImport>(data)

if (json.spendingKey) {
json = {
...json,
...generateKeyFromPrivateKey(json.spendingKey),
}
}

return json
} catch (e) {
CliUx.ux.error(`Import failed for the given input: ${data}`)
}
Expand All @@ -127,7 +145,7 @@ export class ImportCommand extends IronfishCommand {
async importFile(path: string): Promise<AccountImport> {
const resolved = this.sdk.fileSystem.resolve(path)
const data = await this.sdk.fileSystem.readFile(resolved)
return this.stringToAccountImport(data)
return this.stringToAccountImport(data.trim())
}

async importPipe(): Promise<AccountImport> {
Expand Down
2 changes: 1 addition & 1 deletion ironfish-cli/src/utils/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function selectAsset(
const choices = balances.map((balance) => {
const assetName = BufferUtils.toHuman(Buffer.from(balance.assetName, 'hex'))
const name = `${balance.assetId} (${assetName}) (${CurrencyUtils.renderIron(
balance.confirmed,
balance.available,
)})`

const value = {
Expand Down
12 changes: 3 additions & 9 deletions ironfish-cli/src/utils/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { Asset } from '@ironfish/rust-nodejs'
import { Assert, CurrencyUtils, RpcClient } from '@ironfish/sdk'
import { CurrencyUtils, RpcClient } from '@ironfish/sdk'
import { CliUx } from '@oclif/core'

export async function promptCurrency(options: {
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function promptCurrency(options: {
confirmations: options.balance.confirmations,
})

text += ` (balance ${CurrencyUtils.renderIron(balance.content.confirmed)})`
text += ` (balance ${CurrencyUtils.renderIron(balance.content.available)})`
}

// eslint-disable-next-line no-constant-condition
Expand All @@ -50,13 +50,7 @@ export async function promptCurrency(options: {
return null
}

const [amount, error] = CurrencyUtils.decodeTry(input)

if (error) {
throw error
}

Assert.isNotNull(amount)
const amount = CurrencyUtils.decodeIron(input)

if (options.minimum != null && amount < options.minimum) {
continue
Expand Down
2 changes: 1 addition & 1 deletion ironfish/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/sdk",
"version": "0.0.46",
"version": "0.0.47",
"description": "SDK for running and interacting with an Iron Fish node",
"author": "Iron Fish <[email protected]> (https://ironfish.network)",
"main": "build/src/index.js",
Expand Down
72 changes: 72 additions & 0 deletions ironfish/src/migrations/data/024-unspent-notes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { Logger } from '../../logger'
import { IronfishNode } from '../../node'
import { IDatabase, IDatabaseTransaction } from '../../storage'
import { createDB } from '../../storage/utils'
import { Account } from '../../wallet'
import { Migration } from '../migration'
import { GetStores } from './024-unspent-notes/stores'

export class Migration024 extends Migration {
path = __filename

prepare(node: IronfishNode): IDatabase {
return createDB({ location: node.config.walletDatabasePath })
}

async forward(
node: IronfishNode,
db: IDatabase,
tx: IDatabaseTransaction | undefined,
logger: Logger,
): Promise<void> {
const stores = GetStores(db)

const accounts = []

for await (const accountValue of stores.old.accounts.getAllValuesIter()) {
accounts.push(
new Account({
...accountValue,
walletDb: node.wallet.walletDb,
}),
)
}

logger.info(`Indexing unspent notes for ${accounts.length} accounts`)

for (const account of accounts) {
let unspentNotes = 0

logger.info(` Indexing unspent notes for account ${account.name}`)
for await (const [[, noteHash], note] of stores.old.decryptedNotes.getAllIter(
undefined,
account.prefixRange,
)) {
if (note.sequence === null || note.spent) {
continue
}

await stores.new.unspentNoteHashes.put(
[
account.prefix,
[note.note.assetId(), [note.sequence, [note.note.value(), noteHash]]],
],
null,
)
unspentNotes++
}

logger.info(` Indexed ${unspentNotes} unspent notes for account ${account.name}`)
}
}

async backward(node: IronfishNode, db: IDatabase): Promise<void> {
const stores = GetStores(db)

await stores.new.unspentNoteHashes.clear()
}
}
47 changes: 47 additions & 0 deletions ironfish/src/migrations/data/024-unspent-notes/new/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import {
BigU64BEEncoding,
BufferEncoding,
IDatabase,
IDatabaseStore,
NULL_ENCODING,
PrefixEncoding,
U32_ENCODING_BE,
} from '../../../../storage'
import { Account } from '../../../../wallet'

export function GetNewStores(db: IDatabase): {
unspentNoteHashes: IDatabaseStore<{
key: [Account['prefix'], [Buffer, [number, [bigint, Buffer]]]]
value: null
}>
} {
const unspentNoteHashes: IDatabaseStore<{
key: [Account['prefix'], [Buffer, [number, [bigint, Buffer]]]]
value: null
}> = db.addStore({
name: 'un',
keyEncoding: new PrefixEncoding(
new BufferEncoding(), // account prefix
new PrefixEncoding(
new BufferEncoding(), // asset ID
new PrefixEncoding(
U32_ENCODING_BE, // sequence
new PrefixEncoding(
new BigU64BEEncoding(), // value
new BufferEncoding(), // note hash
8,
),
4,
),
32,
),
4,
),
valueEncoding: NULL_ENCODING,
})

return { unspentNoteHashes }
}
Loading

0 comments on commit 6fb75f4

Please sign in to comment.