Skip to content

Commit

Permalink
Hash block header with strategy class (#4533)
Browse files Browse the repository at this point in the history
  • Loading branch information
danield9tqh authored Jan 11, 2024
1 parent cf5202a commit f08d5bb
Show file tree
Hide file tree
Showing 22 changed files with 176 additions and 130 deletions.
4 changes: 2 additions & 2 deletions ironfish/src/blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ describe('Blockchain', () => {
const genesis = nodeTest.chain.genesis
expect(node.chain.head?.hash).toEqualBuffer(genesis.hash)

const blockB3Invalid = Block.fromRaw({
const blockB3Invalid = nodeTest.strategy.newBlock({
header: {
...blockB3.header,
noteCommitment: Buffer.alloc(32),
Expand Down Expand Up @@ -731,7 +731,7 @@ describe('Blockchain', () => {
valid: true,
})

const invalidBlock = Block.fromRaw({
const invalidBlock = nodeTest.strategy.newBlock({
header: {
...block.header,
timestamp: new Date(0),
Expand Down
6 changes: 3 additions & 3 deletions ironfish/src/blockchain/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class Blockchain {
}

private async seed() {
const genesis = BlockSerde.deserialize(this.seedGenesisBlock)
const genesis = BlockSerde.deserialize(this.seedGenesisBlock, this.strategy)

const result = await this.addBlock(genesis)
Assert.isTrue(result.isAdded, `Could not seed genesis: ${result.reason || 'unknown'}`)
Expand Down Expand Up @@ -230,7 +230,7 @@ export class Blockchain {
if (genesisHeader) {
Assert.isTrue(
genesisHeader.hash.equals(
BlockHeaderSerde.deserialize(this.seedGenesisBlock.header).hash,
BlockHeaderSerde.deserialize(this.seedGenesisBlock.header, this.strategy).hash,
),
'Genesis block in network definition does not match existing chain genesis block',
)
Expand Down Expand Up @@ -948,7 +948,7 @@ export class Blockchain {
graffiti,
}

const header = new BlockHeader(rawHeader, noteSize, BigInt(0))
const header = this.strategy.newBlockHeader(rawHeader, noteSize, BigInt(0))

const block = new Block(header, transactions)
if (verifyBlock && !previousBlockHash.equals(GENESIS_BLOCK_PREVIOUS)) {
Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/blockchain/database/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class HeaderEncoding implements IDatabaseEncoding<HeaderValue> {
timestamp: new Date(timestamp),
graffiti,
}
const header = new BlockHeader(rawHeader, noteSize, work, hash)
const header = new BlockHeader(rawHeader, hash, noteSize, work)

return { header }
}
Expand Down
54 changes: 28 additions & 26 deletions ironfish/src/consensus/verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('Verifier', () => {
const { block } = await useBlockWithTx(nodeTest.node)

const reorderedTransactions = [block.transactions[1], block.transactions[0]]
const invalidBlock = Block.fromRaw({
const invalidBlock = nodeTest.strategy.newBlock({
header: {
...block.header,
transactionCommitment: transactionCommitment(reorderedTransactions),
Expand All @@ -219,7 +219,7 @@ describe('Verifier', () => {
const minersFee = await useMinersTxFixture(nodeTest.node, account, undefined, 0)

const reorderedTransactions = [block.transactions[0], minersFee]
const invalidBlock = Block.fromRaw({
const invalidBlock = nodeTest.strategy.newBlock({
header: {
...block.header,
transactionCommitment: transactionCommitment(reorderedTransactions),
Expand Down Expand Up @@ -272,7 +272,7 @@ describe('Verifier', () => {
},
)

const invalidMinersBlock = Block.fromRaw({
const invalidMinersBlock = nodeTest.strategy.newBlock({
header: {
...minersBlock.header,
transactionCommitment: transactionCommitment([invalidMinersTransaction]),
Expand All @@ -289,7 +289,7 @@ describe('Verifier', () => {
it('rejects a block with no transactions', async () => {
const block = await useMinerBlockFixture(nodeTest.node.chain)

const invalidBlock = Block.fromRaw({
const invalidBlock = nodeTest.strategy.newBlock({
header: {
...block.header,
transactionCommitment: transactionCommitment([]),
Expand All @@ -316,7 +316,7 @@ describe('Verifier', () => {

const invalidTransactions = [...block.transactions, extraTransaction]

const invalidBlock = Block.fromRaw({
const invalidBlock = nodeTest.strategy.newBlock({
header: {
...block.header,
transactionCommitment: transactionCommitment(invalidTransactions),
Expand Down Expand Up @@ -524,9 +524,9 @@ describe('Verifier', () => {
...header,
graffiti: Buffer.alloc(31),
},
header.hash,
header.noteSize,
header.work,
header.hash,
)

expect(nodeTest.verifier.verifyBlockHeader(invalidHeader31Byte)).toMatchObject({
Expand All @@ -539,9 +539,9 @@ describe('Verifier', () => {
...header,
graffiti: Buffer.alloc(33),
},
header.hash,
header.noteSize,
header.work,
header.hash,
)

expect(nodeTest.verifier.verifyBlockHeader(invalidHeader33Byte)).toMatchObject({
Expand Down Expand Up @@ -639,7 +639,7 @@ describe('Verifier', () => {
nodeTest.verifier.enableVerifyTarget = true
const block = await useMinerBlockFixture(nodeTest.chain)

const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...block.header,
target: Target.minTarget(),
})
Expand All @@ -655,7 +655,7 @@ describe('Verifier', () => {
it('Is invalid when the sequence is wrong', async () => {
const block = await useMinerBlockFixture(nodeTest.chain)

const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...block.header,
sequence: 9999,
})
Expand Down Expand Up @@ -692,7 +692,7 @@ describe('Verifier', () => {
})

it('fails validation when timestamp is too low', async () => {
const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(
prevHeader.timestamp.getTime() -
Expand All @@ -713,7 +713,7 @@ describe('Verifier', () => {
.spyOn(global.Date, 'now')
.mockImplementationOnce(() => prevHeader.timestamp.getTime() + 40 * 1000)

const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(
prevHeader.timestamp.getTime() +
Expand All @@ -734,7 +734,7 @@ describe('Verifier', () => {
.spyOn(global.Date, 'now')
.mockImplementationOnce(() => prevHeader.timestamp.getTime() + 1 * 1000)

const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(prevHeader.timestamp.getTime() - 1 * 1000),
})
Expand All @@ -751,7 +751,7 @@ describe('Verifier', () => {
.spyOn(global.Date, 'now')
.mockImplementationOnce(() => prevHeader.timestamp.getTime() + 1 * 1000)

const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(prevHeader.timestamp.getTime() + 1 * 1000),
})
Expand Down Expand Up @@ -792,7 +792,7 @@ describe('Verifier', () => {
})

it('fails validation when timestamp is too low', async () => {
const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(
prevHeader.timestamp.getTime() -
Expand All @@ -813,7 +813,7 @@ describe('Verifier', () => {
.spyOn(global.Date, 'now')
.mockImplementationOnce(() => prevHeader.timestamp.getTime() + 40 * 1000)

const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(
prevHeader.timestamp.getTime() +
Expand All @@ -830,7 +830,7 @@ describe('Verifier', () => {
})

it('fails validation when timestamp is smaller than previous block', async () => {
const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(prevHeader.timestamp.getTime() - 1 * 1000),
})
Expand All @@ -844,7 +844,7 @@ describe('Verifier', () => {
})

it('fails validation when timestamp is same as previous block', async () => {
const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(prevHeader.timestamp.getTime()),
})
Expand All @@ -862,7 +862,7 @@ describe('Verifier', () => {
.spyOn(global.Date, 'now')
.mockImplementationOnce(() => prevHeader.timestamp.getTime() + 1 * 1000)

const invalidHeader = new BlockHeader({
const invalidHeader = nodeTest.strategy.newBlockHeader({
...header,
timestamp: new Date(prevHeader.timestamp.getTime() + 1 * 1000),
})
Expand Down Expand Up @@ -895,15 +895,17 @@ describe('Verifier', () => {
const newBlock = await useMinerBlockFixture(chain)
await expect(chain).toAddBlock(newBlock)

const invalidNewBlock = Block.fromRaw({
header: {
...newBlock.header,
noteCommitment: Buffer.alloc(newBlock.header.noteCommitment.length, 'NOOO'),
const invalidNewBlock = nodeTest.strategy.newBlock(
{
header: {
...newBlock.header,
noteCommitment: Buffer.alloc(newBlock.header.noteCommitment.length, 'NOOO'),
},
transactions: newBlock.transactions,
},
transactions: newBlock.transactions,
})
invalidNewBlock.header.noteSize = newBlock.header.noteSize
invalidNewBlock.header.work = newBlock.header.work
newBlock.header.noteSize,
newBlock.header.work,
)

await expect(
nodeTest.verifier.verifyConnectedBlock(invalidNewBlock),
Expand Down
4 changes: 2 additions & 2 deletions ironfish/src/genesis/addGenesisTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@ironfish/rust-nodejs'
import { Logger } from '../logger'
import { FullNode } from '../node'
import { Block, BlockHeader } from '../primitives'
import { Block } from '../primitives'
import { transactionCommitment } from '../primitives/blockheader'
import { Transaction, TransactionVersion } from '../primitives/transaction'
import { CurrencyUtils } from '../utils'
Expand Down Expand Up @@ -139,7 +139,7 @@ export async function addGenesisTransaction(
graffiti: genesisBlock.header.graffiti,
}

const newGenesisHeader = new BlockHeader(rawHeader, noteSize)
const newGenesisHeader = node.chain.strategy.newBlockHeader(rawHeader, noteSize)

genesisBlock.header = newGenesisHeader

Expand Down
4 changes: 2 additions & 2 deletions ironfish/src/genesis/genesis.test.slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('Create genesis block', () => {

// Deserialize the block and add it to the new chain
const result = IJSON.parse(jsonedBlock) as SerializedBlock
const deserializedBlock = BlockSerde.deserialize(result)
const deserializedBlock = BlockSerde.deserialize(result, nodeTest.strategy)
const addedBlock = await newChain.addBlock(deserializedBlock)
expect(addedBlock.isAdded).toBe(true)

Expand Down Expand Up @@ -274,7 +274,7 @@ describe('addGenesisTransaction', () => {

// Deserialize the block and add it to the new chain
const result = IJSON.parse(jsonedBlock) as SerializedBlock
const deserializedBlock = BlockSerde.deserialize(result)
const deserializedBlock = BlockSerde.deserialize(result, nodeTest.strategy)
const addedBlock = await chain.addBlock(deserializedBlock)
expect(addedBlock.isAdded).toBe(true)

Expand Down
21 changes: 11 additions & 10 deletions ironfish/src/genesis/makeGenesisBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,18 @@ export async function makeGenesisBlock(
GraffitiUtils.fromString('genesis'),
)

const genesisBlock = Block.fromRaw({
header: {
...block.header,
target: info.target,
timestamp: new Date(info.timestamp),
const genesisBlock = chain.strategy.newBlock(
{
header: {
...block.header,
target: info.target,
timestamp: new Date(info.timestamp),
},
transactions: block.transactions,
},
transactions: block.transactions,
})

genesisBlock.header.noteSize = block.header.noteSize
genesisBlock.header.work = block.header.work
block.header.noteSize,
block.header.work,
)

logger.info('Block complete.')
return { block: genesisBlock }
Expand Down
10 changes: 5 additions & 5 deletions ironfish/src/mining/manager.test.slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Assert } from '../assert'
import { VerificationResultReason } from '../consensus'
import { getBlockWithMinersFeeSize, getTransactionSize } from '../network/utils/serializers'
import { Block, Target, Transaction } from '../primitives'
import { Target, Transaction } from '../primitives'
import { TransactionVersion } from '../primitives/transaction'
import { BlockTemplateSerde, SerializedBlockTemplate } from '../serde'
import {
Expand Down Expand Up @@ -626,7 +626,7 @@ describe('Mining manager', () => {
// Create 2 blocks at the same sequence, one with higher difficulty
const blockA1 = await useMinerBlockFixture(chain, undefined, account, wallet)
const blockB1Temp = await useMinerBlockFixture(chain, undefined, account, wallet)
const blockB1 = Block.fromRaw({
const blockB1 = nodeTest.strategy.newBlock({
header: {
...blockB1Temp.header,
target: Target.fromDifficulty(blockA1.header.target.toDifficulty() + 1n),
Expand All @@ -641,8 +641,8 @@ describe('Mining manager', () => {
await expect(chain).toAddBlock(blockB1)

// Increase difficulty of submitted template so it
const blockA2Temp = BlockTemplateSerde.deserialize(templateA2)
const blockA2 = Block.fromRaw({
const blockA2Temp = BlockTemplateSerde.deserialize(templateA2, nodeTest.strategy)
const blockA2 = nodeTest.strategy.newBlock({
header: {
...blockA2Temp.header,
target: Target.fromDifficulty(blockA2Temp.header.target.toDifficulty() + 2n),
Expand Down Expand Up @@ -680,7 +680,7 @@ describe('Mining manager', () => {
MINED_RESULT.SUCCESS,
)

const submittedBlock = BlockTemplateSerde.deserialize(template)
const submittedBlock = BlockTemplateSerde.deserialize(template, nodeTest.strategy)
const newBlock = onNewBlockSpy.mock.calls[0][0]
expect(newBlock.header.hash).toEqual(submittedBlock.header.hash)
})
Expand Down
4 changes: 2 additions & 2 deletions ironfish/src/mining/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class MiningManager {
this.metrics.mining_newBlockTemplate.add(BenchUtils.end(connectedAt))
this.streamBlockTemplate(currentBlock, template)

const block = BlockTemplateSerde.deserialize(template)
const block = BlockTemplateSerde.deserialize(template, this.chain.strategy)
const verification = await this.chain.verifier.verifyBlock(block, {
verifyTarget: false,
})
Expand Down Expand Up @@ -366,7 +366,7 @@ export class MiningManager {
}

async submitBlockTemplate(blockTemplate: SerializedBlockTemplate): Promise<MINED_RESULT> {
const block = BlockTemplateSerde.deserialize(blockTemplate)
const block = BlockTemplateSerde.deserialize(blockTemplate, this.chain.strategy)

const blockDisplay = `${block.header.hash.toString('hex')} (${block.header.sequence})`
if (!block.header.previousBlockHash.equals(this.node.chain.head.hash)) {
Expand Down
6 changes: 3 additions & 3 deletions ironfish/src/network/messages/getBlocks.test.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 { Block, BlockHeader, Target } from '../../primitives'
import { Block, Target } from '../../primitives'
import { transactionCommitment } from '../../primitives/blockheader'
import {
createNodeTest,
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('GetBlocksResponse', () => {
const message = new GetBlocksResponse(
[
new Block(
new BlockHeader({
nodeTest.strategy.newBlockHeader({
sequence: 2,
previousBlockHash: Buffer.alloc(32, 2),
noteCommitment: Buffer.alloc(32, 4),
Expand All @@ -49,7 +49,7 @@ describe('GetBlocksResponse', () => {
[transactionA, transactionB],
),
new Block(
new BlockHeader({
nodeTest.strategy.newBlockHeader({
sequence: 2,
previousBlockHash: Buffer.alloc(32, 1),
noteCommitment: Buffer.alloc(32, 5),
Expand Down
Loading

0 comments on commit f08d5bb

Please sign in to comment.