Skip to content

Commit

Permalink
Delete WalletBlockHeader and WalletTransaction
Browse files Browse the repository at this point in the history
I have no idea why these types were introduced, but they were not
neccesary. You can just use the basic primitives we already have and not
introduce brand new types to the code base.
  • Loading branch information
NullSoldier committed Jun 6, 2024
1 parent 584d5ca commit 445ad1b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 71 deletions.
28 changes: 27 additions & 1 deletion ironfish/src/rpc/routes/chain/serializers.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 { getBlockSize, getTransactionSize } from '../../../network/utils/serializers'
import { Block, BlockHeader, Transaction } from '../../../primitives'
import { Block, BlockHeader, Target, Transaction } from '../../../primitives'
import { BufferUtils } from '../../../utils'
import { RpcBlock, RpcBlockHeader, RpcTransaction } from './types'

Expand All @@ -25,6 +25,22 @@ export function serializeRpcBlockHeader(header: BlockHeader): RpcBlockHeader {
}
}

export function deserializeRpcBlockHeader(header: RpcBlockHeader): BlockHeader {
return new BlockHeader(
{
sequence: header.sequence,
previousBlockHash: Buffer.from(header.previousBlockHash, 'hex'),
noteCommitment: Buffer.from(header.noteCommitment, 'hex'),
transactionCommitment: Buffer.from(header.transactionCommitment, 'hex'),
target: new Target(header.target),
randomness: BigInt(header.randomness),
timestamp: new Date(header.timestamp),
graffiti: Buffer.from(header.graffiti, 'hex'),
},
Buffer.from(header.hash, 'hex'),
)
}

export const serializeRpcBlock = (block: Block, serialized?: boolean): RpcBlock => {
const blockHeaderResponse = serializeRpcBlockHeader(block.header)

Expand Down Expand Up @@ -79,3 +95,13 @@ export const serializeRpcTransaction = (
...(serialized ? { serialized: tx.serialize().toString('hex') } : {}),
}
}

export const deserializeRpcTransaction = (tx: RpcTransaction): Transaction => {
if (tx.serialized) {
return new Transaction(Buffer.from(tx.serialized, 'hex'))
}

throw new Error(
`Deserializing transactions that are not serialized are currently not supported.`,
)
}
9 changes: 4 additions & 5 deletions ironfish/src/wallet/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Asset } from '@ironfish/rust-nodejs'
import { BufferMap, BufferSet } from 'buffer-map'
import MurmurHash3 from 'imurmurhash'
import { Assert } from '../../assert'
import { Transaction } from '../../primitives'
import { BlockHeader, Transaction } from '../../primitives'
import { GENESIS_BLOCK_SEQUENCE } from '../../primitives/block'
import { Note } from '../../primitives/note'
import { DatabaseKeyRange, IDatabaseTransaction } from '../../storage'
Expand All @@ -15,7 +15,6 @@ import { WithNonNull, WithRequired } from '../../utils'
import { DecryptedNote } from '../../workerPool/tasks/decryptNotes'
import { AssetBalances } from '../assetBalances'
import { MultisigKeys, MultisigSigner } from '../interfaces/multisigKeys'
import { WalletBlockHeader } from '../scanner/remoteChainProcessor'
import { AccountValue } from '../walletdb/accountValue'
import { AssetValue } from '../walletdb/assetValue'
import { BalanceValue } from '../walletdb/balanceValue'
Expand Down Expand Up @@ -174,7 +173,7 @@ export class Account {
}

async connectTransaction(
blockHeader: WalletBlockHeader,
blockHeader: BlockHeader,
transaction: Transaction,
decryptedNotes: Array<DecryptedNote>,
tx?: IDatabaseTransaction,
Expand Down Expand Up @@ -488,7 +487,7 @@ export class Account {
}

private async deleteDisconnectedMintsFromAssetsStore(
blockHeader: WalletBlockHeader,
blockHeader: BlockHeader,
transaction: Transaction,
receivedAssets: BufferSet | null,
tx: IDatabaseTransaction,
Expand Down Expand Up @@ -643,7 +642,7 @@ export class Account {
}

async disconnectTransaction(
blockHeader: WalletBlockHeader,
blockHeader: BlockHeader,
transaction: Transaction,
tx?: IDatabaseTransaction,
): Promise<AssetBalances> {
Expand Down
96 changes: 52 additions & 44 deletions ironfish/src/wallet/scanner/remoteChainProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,43 @@
import { Assert } from '../../assert'
import { Event } from '../../event'
import { Logger } from '../../logger'
import { Transaction } from '../../primitives'
import { FollowChainStreamResponse, RpcClient } from '../../rpc'
import { BlockHeader, Transaction } from '../../primitives'
import { RpcClient } from '../../rpc'
import {
deserializeRpcBlockHeader,
deserializeRpcTransaction,
} from '../../rpc/routes/chain/serializers'
import { BufferUtils } from '../../utils'

export type WalletBlockHeader = {
hash: Buffer
previousBlockHash: Buffer
sequence: number
timestamp: Date
}

export type WalletBlockTransaction = {
transaction: Transaction
initialNoteIndex: number
}

export class RemoteChainProcessor {
hash: Buffer | null = null
sequence: number | null = null
logger: Logger
nodeClient: RpcClient | null
maxQueueSize: number

onAdd = new Event<[{ header: WalletBlockHeader; transactions: WalletBlockTransaction[] }]>()
onAdd = new Event<
[
{
header: BlockHeader
transactions: {
transaction: Transaction
initialNoteIndex: number
}[]
},
]
>()

onRemove = new Event<
[{ header: WalletBlockHeader; transactions: WalletBlockTransaction[] }]
[
{
header: BlockHeader
transactions: {
transaction: Transaction
initialNoteIndex: number
}[]
},
]
>()

constructor(options: {
Expand Down Expand Up @@ -68,49 +78,47 @@ export class RemoteChainProcessor {
continue
}

const blockHeader: WalletBlockHeader = {
hash: Buffer.from(block.hash, 'hex'),
previousBlockHash: Buffer.from(block.previousBlockHash, 'hex'),
sequence: block.sequence,
timestamp: new Date(block.timestamp),
}
const header = deserializeRpcBlockHeader(block)

const blockTransactions = this.getBlockTransactions(content)
const transactions = this.mapTransactionWithNoteIndex(
header,
block.transactions.map((tx) => deserializeRpcTransaction(tx)),
)

if (type === 'connected') {
this.hash = blockHeader.hash
this.sequence = blockHeader.sequence
await this.onAdd.emitAsync({ header: blockHeader, transactions: blockTransactions })
this.hash = header.hash
this.sequence = header.sequence

await this.onAdd.emitAsync({ header, transactions })
} else if (type === 'disconnected') {
this.hash = blockHeader.previousBlockHash
this.sequence = blockHeader.sequence - 1
this.hash = header.previousBlockHash
this.sequence = header.sequence - 1

await this.onRemove.emitAsync({
header: blockHeader,
transactions: blockTransactions,
header: header,
transactions: transactions,
})
}
}

return { hashChanged: !BufferUtils.equalsNullable(this.hash, oldHash) }
const hashChanged = !BufferUtils.equalsNullable(this.hash, oldHash)
return { hashChanged }
}

getBlockTransactions(response: FollowChainStreamResponse): WalletBlockTransaction[] {
const transactions = []
mapTransactionWithNoteIndex(
header: BlockHeader,
transactions: Transaction[],
): Array<{ transaction: Transaction; initialNoteIndex: number }> {
Assert.isNotNull(header.noteSize)
let initialNoteIndex = header.noteSize

Assert.isNotNull(response.block.noteSize)
let initialNoteIndex = response.block.noteSize
const result = []

for (const rpcTransaction of response.block.transactions.slice().reverse()) {
Assert.isNotUndefined(rpcTransaction.serialized)
const transaction = new Transaction(Buffer.from(rpcTransaction.serialized, 'hex'))
for (const transaction of transactions.slice().reverse()) {
initialNoteIndex -= transaction.notes.length

transactions.push({
transaction,
initialNoteIndex,
})
result.push({ transaction, initialNoteIndex })
}

return transactions.slice().reverse()
return result.slice().reverse()
}
}
4 changes: 2 additions & 2 deletions ironfish/src/wallet/scanner/scanState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import { Event } from '../../event'
import { Meter } from '../../metrics'
import { BlockHeader } from '../../primitives'
import { PromiseResolve, PromiseUtils } from '../../utils'
import { HeadValue } from '../walletdb/headValue'
import { WalletBlockHeader } from './remoteChainProcessor'

export class ScanState {
hash: Buffer | null = null
Expand Down Expand Up @@ -43,7 +43,7 @@ export class ScanState {
return (remaining / this.speed.rate1m) * 1000
}

signal(header: WalletBlockHeader): void {
signal(header: BlockHeader): void {
this.hash = header.hash
this.sequence = header.sequence
this.speed.add(1)
Expand Down
15 changes: 6 additions & 9 deletions ironfish/src/wallet/scanner/walletScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import { BufferMap } from 'buffer-map'
import { Config } from '../../fileStores'
import { Logger } from '../../logger'
import { Mutex } from '../../mutex'
import { BlockHeader, Transaction } from '../../primitives'
import { RpcClient } from '../../rpc'
import { AsyncUtils, BufferUtils, HashUtils } from '../../utils'
import { DecryptedNote } from '../../workerPool/tasks/decryptNotes'
import {
RemoteChainProcessor,
WalletBlockHeader,
WalletBlockTransaction,
} from './remoteChainProcessor'
import { RemoteChainProcessor } from './remoteChainProcessor'
import { ScanState } from './scanState'

export class WalletScanner {
Expand Down Expand Up @@ -127,8 +124,8 @@ export class WalletScanner {
}

async connectBlock(
blockHeader: WalletBlockHeader,
transactions: WalletBlockTransaction[],
blockHeader: BlockHeader,
transactions: { transaction: Transaction; initialNoteIndex: number }[],
abort?: AbortController,
): Promise<void> {
if (blockHeader.sequence % 100 === 0) {
Expand Down Expand Up @@ -202,8 +199,8 @@ export class WalletScanner {
}

async disconnectBlock(
header: WalletBlockHeader,
transactions: WalletBlockTransaction[],
header: BlockHeader,
transactions: { transaction: Transaction; initialNoteIndex: number }[],
abort?: AbortController,
): Promise<void> {
this.logger.debug(`AccountHead DEL: ${header.sequence} => ${Number(header.sequence) - 1}`)
Expand Down
17 changes: 7 additions & 10 deletions ironfish/src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { NoteHasher } from '../merkletree'
import { Side } from '../merkletree/merkletree'
import { Witness } from '../merkletree/witness'
import { Mutex } from '../mutex'
import { BlockHeader } from '../primitives'
import { GENESIS_BLOCK_PREVIOUS, GENESIS_BLOCK_SEQUENCE } from '../primitives/block'
import { BurnDescription } from '../primitives/burnDescription'
import { MintDescription } from '../primitives/mintDescription'
Expand Down Expand Up @@ -50,7 +51,6 @@ import {
import { AccountImport } from './exporter/accountImport'
import { isMultisigSignerTrustedDealerImport } from './exporter/multisig'
import { MintAssetOptions } from './interfaces/mintAssetOptions'
import { WalletBlockHeader, WalletBlockTransaction } from './scanner/remoteChainProcessor'
import { ScanState } from './scanner/scanState'
import { WalletScanner } from './scanner/walletScanner'
import { validateAccount } from './validator'
Expand Down Expand Up @@ -431,7 +431,7 @@ export class Wallet {

async connectBlockForAccount(
account: Account,
blockHeader: WalletBlockHeader,
blockHeader: BlockHeader,
transactions: { transaction: Transaction; decryptedNotes: DecryptedNote[] }[],
shouldDecrypt: boolean,
): Promise<void> {
Expand Down Expand Up @@ -466,10 +466,7 @@ export class Wallet {
})
}

async shouldDecryptForAccount(
blockHeader: WalletBlockHeader,
account: Account,
): Promise<boolean> {
async shouldDecryptForAccount(blockHeader: BlockHeader, account: Account): Promise<boolean> {
if (account.createdAt === null) {
return true
}
Expand All @@ -495,7 +492,7 @@ export class Wallet {
}

private async connectBlockTransactions(
blockHeader: WalletBlockHeader,
blockHeader: BlockHeader,
transactions: Array<{ transaction: Transaction; decryptedNotes: Array<DecryptedNote> }>,
account: Account,
tx?: IDatabaseTransaction,
Expand All @@ -521,7 +518,7 @@ export class Wallet {
private async upsertAssetsFromDecryptedNotes(
account: Account,
decryptedNotes: DecryptedNote[],
blockHeader: WalletBlockHeader,
blockHeader: BlockHeader,
tx?: IDatabaseTransaction,
): Promise<void> {
for (const { serializedNote } of decryptedNotes) {
Expand Down Expand Up @@ -605,8 +602,8 @@ export class Wallet {

async disconnectBlockForAccount(
account: Account,
header: WalletBlockHeader,
transactions: WalletBlockTransaction[],
header: BlockHeader,
transactions: { transaction: Transaction; initialNoteIndex: number }[],
) {
const assetBalanceDeltas = new AssetBalances()

Expand Down

0 comments on commit 445ad1b

Please sign in to comment.