-
Notifications
You must be signed in to change notification settings - Fork 728
/
customConsensus.spec.ts
170 lines (158 loc) · 5.58 KB
/
customConsensus.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Block } from '@ethereumjs/block'
import { Common, Hardfork } from '@ethereumjs/common'
import { bytesToHex } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'
import { Blockchain, EthashConsensus } from '../src/index.js'
import type { Consensus } from '../src/index.js'
import type { BlockHeader } from '@ethereumjs/block'
class fibonacciConsensus implements Consensus {
algorithm: string
constructor() {
this.algorithm = 'fibonacciConsensus'
}
genesisInit(_genesisBlock: Block): Promise<void> {
return new Promise<void>((resolve) => resolve())
}
setup(): Promise<void> {
return new Promise<void>((resolve) => resolve())
}
validateConsensus(_block: Block): Promise<void> {
if (bytesToHex(_block.header.extraData) !== '0x12358d') {
throw new Error(
'header contains invalid extradata - must match first 6 elements of fibonacci sequence'
)
}
return new Promise<void>((resolve) => resolve())
}
validateDifficulty(_header: BlockHeader): Promise<void> {
if (_header.number === 1n && _header.difficulty === 1n) {
return new Promise<void>((resolve) => resolve())
}
if (_header.difficulty !== _header.number - 1n + _header.number - 2n) {
throw new Error('invalid difficulty - must be sum of previous two block numbers')
}
return new Promise<void>((resolve) => resolve())
}
newBlock(): Promise<void> {
return new Promise<void>((resolve) => resolve())
}
}
describe('Optional consensus parameter in blockchain constructor', () => {
it('blockchain constructor should work with custom consensus', async () => {
const common = new Common({ chain: 'mainnet', hardfork: Hardfork.Chainstart })
const consensus = new fibonacciConsensus()
try {
const blockchain = await Blockchain.create({ common, consensus })
assert.equal(
(blockchain.consensus as fibonacciConsensus).algorithm,
'fibonacciConsensus',
'consensus algorithm matches'
)
} catch (err) {
assert.fail('blockchain should instantiate successfully')
}
})
})
describe('Custom consensus validation rules', () => {
it('should validat custom consensus rules', async () => {
const common = new Common({ chain: 'mainnet', hardfork: Hardfork.Chainstart })
const consensus = new fibonacciConsensus()
const blockchain = await Blockchain.create({ common, consensus })
const block = Block.fromBlockData(
{
header: {
number: 1n,
difficulty: 1n,
extraData: '0x12358d',
parentHash: blockchain.genesisBlock.hash(),
timestamp: blockchain.genesisBlock.header.timestamp + 1n,
gasLimit: blockchain.genesisBlock.header.gasLimit + 1n,
},
},
{ common }
)
try {
await blockchain.putBlock(block)
assert.deepEqual(
(await blockchain.getBlock(block.header.number)).header.hash(),
block.header.hash(),
'put block with valid difficulty and extraData'
)
} catch {
assert.fail('should have put block with valid difficulty and extraData')
}
const blockWithBadDifficulty = Block.fromBlockData(
{
header: {
number: 2n,
difficulty: 2n,
extraData: '0x12358d',
parentHash: block.hash(),
timestamp: block.header.timestamp + 1n,
},
},
{ common }
)
try {
await blockchain.putBlock(blockWithBadDifficulty)
assert.fail('should throw')
} catch (err: any) {
assert.ok(
err.message.includes('invalid difficulty'),
'failed to put block with invalid difficulty'
)
}
const blockWithBadExtraData = Block.fromBlockData(
{
header: {
number: 2n,
difficulty: 1n,
extraData: '0x12358c',
parentHash: block.hash(),
timestamp: block.header.timestamp + 1n,
gasLimit: block.header.gasLimit + 1n,
},
},
{ common }
)
try {
await blockchain.putBlock(blockWithBadExtraData)
assert.fail('should throw')
} catch (err: any) {
assert.ok(
err.message ===
'header contains invalid extradata - must match first 6 elements of fibonacci sequence',
'failed to put block with invalid extraData'
)
}
})
})
describe('consensus transition checks', () => {
it('should transition correctly', async () => {
const common = new Common({ chain: 'mainnet', hardfork: Hardfork.Chainstart })
const consensus = new fibonacciConsensus()
const blockchain = await Blockchain.create({ common, consensus })
try {
await blockchain.checkAndTransitionHardForkByNumber(5n)
assert.ok('checkAndTransitionHardForkByNumber does not throw with custom consensus')
} catch (err: any) {
assert.fail(
`checkAndTransitionHardForkByNumber should not throw with custom consensus, error=${err.message}`
)
}
blockchain.consensus = new EthashConsensus()
blockchain.common.consensusAlgorithm = () => 'fibonacci'
try {
await blockchain.checkAndTransitionHardForkByNumber(5n)
assert.fail(
'checkAndTransitionHardForkByNumber should throw when using standard consensus (ethash, clique, casper) but consensus algorithm defined in common is different'
)
} catch (err: any) {
assert.equal(
err.message,
'consensus algorithm fibonacci not supported',
`checkAndTransitionHardForkByNumber correctly throws when using standard consensus (ethash, clique, casper) but consensus algorithm defined in common is different, error=${err.message}`
)
}
})
})