Skip to content

Commit

Permalink
Merge pull request #1273 from iron-fish/staging
Browse files Browse the repository at this point in the history
STAGING -> MASTER
  • Loading branch information
dguenther authored May 7, 2022
2 parents a034a37 + 7935c4f commit 4c3b5b9
Show file tree
Hide file tree
Showing 126 changed files with 3,765 additions and 3,884 deletions.
6 changes: 3 additions & 3 deletions ironfish-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ironfish",
"version": "0.1.30",
"version": "0.1.31",
"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 @@ -53,8 +53,8 @@
"oclif:version": "oclif readme && git add README.md"
},
"dependencies": {
"@ironfish/rust-nodejs": "0.1.4",
"@ironfish/sdk": "0.0.8",
"@ironfish/rust-nodejs": "0.1.5",
"@ironfish/sdk": "0.0.9",
"@oclif/core": "1.6.1",
"@oclif/plugin-help": "5.1.12",
"@oclif/plugin-not-found": "2.3.1",
Expand Down
14 changes: 7 additions & 7 deletions ironfish-cli/src/commands/accounts/import.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* 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 { JSONUtils, PromiseUtils, SerializedAccount } from '@ironfish/sdk'
import { AccountsValue, JSONUtils, PromiseUtils } from '@ironfish/sdk'
import { CliUx, Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
Expand Down Expand Up @@ -33,7 +33,7 @@ export class ImportCommand extends IronfishCommand {

const client = await this.sdk.connectRpc()

let account: SerializedAccount | null = null
let account: AccountsValue | null = null
if (importPath) {
account = await this.importFile(importPath)
} else if (process.stdin.isTTY) {
Expand Down Expand Up @@ -62,13 +62,13 @@ export class ImportCommand extends IronfishCommand {
}
}

async importFile(path: string): Promise<SerializedAccount> {
async importFile(path: string): Promise<AccountsValue> {
const resolved = this.sdk.fileSystem.resolve(path)
const data = await this.sdk.fileSystem.readFile(resolved)
return JSONUtils.parse<SerializedAccount>(data)
return JSONUtils.parse<AccountsValue>(data)
}

async importPipe(): Promise<SerializedAccount> {
async importPipe(): Promise<AccountsValue> {
let data = ''

const onData = (dataIn: string): void => {
Expand All @@ -84,10 +84,10 @@ export class ImportCommand extends IronfishCommand {

process.stdin.off('data', onData)

return JSONUtils.parse<SerializedAccount>(data)
return JSONUtils.parse<AccountsValue>(data)
}

async importTTY(): Promise<SerializedAccount> {
async importTTY(): Promise<AccountsValue> {
const accountName = (await CliUx.ux.prompt('Enter the account name', {
required: true,
})) as string
Expand Down
7 changes: 3 additions & 4 deletions ironfish-cli/src/commands/peers/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ export class ShowCommand extends IronfishCommand {
const type = message.brokeringPeerDisplayName
? `(broker: ${message.brokeringPeerDisplayName}) ${message.type}`
: message.type
const messageType = colors.cyan(message.message.type)
const payload = JSON.stringify(
'payload' in message.message ? message.message.payload : null,
)

const messageType = colors.cyan(message.message.type.toString())
const payload = message.message.payload

return `${time} ${direction} ${type}: ${messageType} ${payload}`
}
Expand Down
71 changes: 27 additions & 44 deletions ironfish-cli/src/commands/reset.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* 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 { IronfishNode, NodeUtils, PeerNetwork } from '@ironfish/sdk'
import { IronfishNode, NodeUtils } from '@ironfish/sdk'
import { CliUx, Flags } from '@oclif/core'
import fs from 'fs'
import fsAsync from 'fs/promises'
import path from 'path'
import { IronfishCommand } from '../command'
import {
ConfigFlag,
Expand All @@ -19,7 +17,7 @@ import {
} from '../flags'

export default class Reset extends IronfishCommand {
static description = 'Reset the node to a fresh state but preserve accounts'
static description = 'Reset the node to its initial state'

static flags = {
[VerboseFlagKey]: VerboseFlag,
Expand All @@ -34,66 +32,51 @@ export default class Reset extends IronfishCommand {

node: IronfishNode | null = null

peerNetwork: PeerNetwork | null = null

async start(): Promise<void> {
const { flags } = await this.parse(Reset)

let node = await this.sdk.node({ autoSeed: false })
await NodeUtils.waitForOpen(node, null, { upgrade: false, load: false })
let confirmed = flags.confirm

const backupPath = path.join(this.sdk.config.dataDir, 'accounts.backup.json')
const warningMessage =
`\n/!\\ WARNING: This will permanently delete your accounts. You can back them up by loading the previous version of ironfish and running ironfish export. /!\\\n` +
'\nHave you read the warning? (Y)es / (N)o'

if (fs.existsSync(backupPath)) {
this.log(`There is already an account backup at ${backupPath}`)
confirmed = flags.confirm || (await CliUx.ux.confirm(warningMessage))

const confirmed = await CliUx.ux.confirm(
`\nThis means this failed to run. Delete the accounts backup?\nAre you sure? (Y)es / (N)o`,
)
if (!confirmed) {
this.log('Reset aborted.')
this.exit(0)
}

if (!confirmed) {
this.exit(1)
}
const accountDatabasePath = this.sdk.config.accountDatabasePath
const chainDatabasePath = this.sdk.config.chainDatabasePath

fs.rmSync(backupPath)
}
const message =
'\nYou are about to destroy your node databases. The following directories will be deleted:\n' +
`\nAccounts: ${accountDatabasePath}` +
`\nBlockchain: ${chainDatabasePath}` +
`\n\nAre you sure? (Y)es / (N)o`

const confirmed =
flags.confirm ||
(await CliUx.ux.confirm(
`\nYou are about to destroy your node data at ${node.config.dataDir}\nAre you sure? (Y)es / (N)o`,
))
confirmed = flags.confirm || (await CliUx.ux.confirm(message))

if (!confirmed) {
return
this.log('Reset aborted.')
this.exit(0)
}

const accounts = node.accounts.listAccounts()
this.log(`Backing up ${accounts.length} accounts to ${backupPath}`)
const backup = JSON.stringify(accounts, undefined, ' ')
await fsAsync.writeFile(backupPath, backup)
await node.closeDB()

CliUx.ux.action.start('Deleting databases')
CliUx.ux.action.start('Deleting databases...')

await Promise.all([
fsAsync.rm(node.config.accountDatabasePath, { recursive: true }),
fsAsync.rm(node.config.chainDatabasePath, { recursive: true }),
fsAsync.rm(accountDatabasePath, { recursive: true, force: true }),
fsAsync.rm(chainDatabasePath, { recursive: true, force: true }),
])

CliUx.ux.action.status = `Importing ${accounts.length} accounts`

// We create a new node because the old node has cached account data
node = await this.sdk.node()
await node.openDB()
await Promise.all(accounts.map((a) => node.accounts.importAccount(a)))
await node.closeDB()

// Re-initialize the databases
const node = await this.sdk.node()
await NodeUtils.waitForOpen(node)
node.internal.set('isFirstRun', true)
await node.internal.save()

await fsAsync.rm(backupPath)

CliUx.ux.action.stop('Reset the node successfully.')
}
}
4 changes: 2 additions & 2 deletions ironfish-rust-nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ export class Transaction {
setExpirationSequence(expirationSequence: number): void
}
export class FoundBlockResult {
randomness: number
randomness: string
miningRequestId: number
constructor(randomness: number, miningRequestId: number)
constructor(randomness: string, miningRequestId: number)
}
export class ThreadPoolHandler {
constructor(threadCount: number, batchSize: number)
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs-darwin-arm64",
"version": "0.1.4",
"version": "0.1.5",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs-darwin-x64",
"version": "0.1.4",
"version": "0.1.5",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs-linux-arm64-gnu",
"version": "0.1.4",
"version": "0.1.5",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/npm/linux-arm64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs-linux-arm64-musl",
"version": "0.1.4",
"version": "0.1.5",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs-linux-x64-gnu",
"version": "0.1.4",
"version": "0.1.5",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/npm/linux-x64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs-linux-x64-musl",
"version": "0.1.4",
"version": "0.1.5",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs-win32-x64-msvc",
"version": "0.1.4",
"version": "0.1.5",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion ironfish-rust-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ironfish/rust-nodejs",
"version": "0.1.4",
"version": "0.1.5",
"description": "Node.js bindings for Rust code required by the Iron Fish SDK",
"main": "index.js",
"types": "index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions ironfish-rust-nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn initialize_sapling() {

#[napi(constructor)]
pub struct FoundBlockResult {
pub randomness: f64,
pub randomness: String,
pub mining_request_id: f64,
}

Expand Down Expand Up @@ -98,7 +98,7 @@ impl ThreadPoolHandler {
pub fn get_found_block(&self) -> Option<FoundBlockResult> {
if let Some(result) = self.threadpool.get_found_block() {
return Some(FoundBlockResult {
randomness: result.0 as f64,
randomness: format!("{:016x}", result.0),
mining_request_id: result.1 as f64,
});
}
Expand Down
16 changes: 8 additions & 8 deletions ironfish-rust/src/mining/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ pub(crate) fn bytes_lte(a: &[u8], b: &[u8]) -> bool {
true
}

fn randomize_header(i: usize, mut header_bytes: &mut [u8]) {
header_bytes.write_f64::<BigEndian>(i as f64).unwrap();
fn randomize_header(i: u64, mut header_bytes: &mut [u8]) {
header_bytes.write_u64::<BigEndian>(i).unwrap();
}

pub(crate) fn mine_batch(
header_bytes: &mut [u8],
target: &[u8],
start: usize,
start: u64,
step_size: usize,
batch_size: usize,
) -> Option<usize> {
batch_size: u64,
) -> Option<u64> {
let end = start + batch_size;
for i in (start..end).step_by(step_size) {
randomize_header(i, header_bytes);
Expand Down Expand Up @@ -64,11 +64,11 @@ mod test {
let start = 42;
let step_size = 1;

// Hardcoded target value derived from a randomness of 1, which is lower than 42
// Hardcoded target value derived from a randomness of 43, which is lower than 42
// This allows us to test the looping and target comparison a little better
let target: &[u8; 32] = &[
189, 32, 143, 150, 173, 48, 164, 172, 76, 199, 72, 88, 197, 68, 105, 250, 191, 202,
126, 52, 252, 66, 35, 112, 87, 238, 229, 149, 47, 55, 233, 45,
74, 52, 167, 52, 16, 135, 245, 240, 229, 92, 212, 133, 140, 231, 169, 56, 16, 105, 46,
67, 145, 116, 198, 241, 183, 88, 140, 172, 79, 139, 210, 162,
];

let result = mine_batch(header_bytes, target, start, step_size, batch_size);
Expand Down
Loading

0 comments on commit 4c3b5b9

Please sign in to comment.