Skip to content

Commit

Permalink
blockchain, client -> refactor execution: added getIteratorHead(), se…
Browse files Browse the repository at this point in the history
…tIteratorHead() functions, deprecated getHead(), setHead() functions, reactivated VM execution tests
  • Loading branch information
holgerd77 committed Jan 27, 2021
1 parent 3e86029 commit a5421b8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
43 changes: 42 additions & 1 deletion packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,36 @@ export default class Blockchain implements BlockchainInterface {
/**
* Returns the specified iterator head.
*
* @param name - Optional name of the state root head (default: 'vm')
* This function replaces the old `getHead()` method. Note that
* the function deviates from the old behavior and returns the
* genesis hash instead of the current head block if an iterator
* has not been run. This matches the behavior of the `iterator()`
* method.
*
* @param name - Optional name of the iterator head (default: 'vm')
*/
async getIteratorHead(name = 'vm'): Promise<Block> {
return await this.initAndLock<Block>(async () => {
// if the head is not found return the genesis hash
const hash = this._heads[name] || this._genesis
if (!hash) {
throw new Error('No head found.')
}

const block = await this._getBlock(hash)
return block
})
}

/**
* Returns the specified iterator head.
*
* @param name - Optional name of the iterator head (default: 'vm')
*
* @deprecated use `getIteratorHead()` instead. Note that `getIteratorHead()`
* doesn't return the `headHeader` but the genesis hash as an initial
* iterator head value (now matching the behavior of the `iterator()`
* method on a first run)
*/
async getHead(name = 'vm'): Promise<Block> {
return await this.initAndLock<Block>(async () => {
Expand Down Expand Up @@ -813,6 +842,18 @@ export default class Blockchain implements BlockchainInterface {
* @param tag - The tag to save the headHash to
* @param headHash - The head hash to save
*/
async setIteratorHead(tag: string, headHash: Buffer) {
return await this.setHead(tag, headHash)
}

/**
* Set header hash of a certain `tag`.
* When calling the iterator, the iterator will start running the first child block after the header hash currenntly stored.
* @param tag - The tag to save the headHash to
* @param headHash - The head hash to save
*
* @deprecated use `setIteratorHead()` instead
*/
async setHead(tag: string, headHash: Buffer) {
await this.initAndLock<void>(async () => {
this._heads[tag] = headHash
Expand Down
16 changes: 13 additions & 3 deletions packages/blockchain/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ tape('blockchain test', (t) => {
const blockchain = new Blockchain({ common })

const head = await blockchain.getHead()
const iteratorHead = await blockchain.getIteratorHead()

st.equals(head.hash().toString('hex'), common.genesis().hash.slice(2), 'correct genesis hash')
st.equals(
head.hash().toString('hex'),
common.genesis().hash.slice(2),
'correct genesis hash (getHead())'
)
st.equals(
iteratorHead.hash().toString('hex'),
common.genesis().hash.slice(2),
'correct genesis hash (getIteratorHead())'
)
st.end()
})

Expand Down Expand Up @@ -430,14 +440,14 @@ tape('blockchain test', (t) => {
// Note: if st.end() is not called (Promise did not throw), then this test fails, as it does not end.
})

t.test('should test setHead method', async (st) => {
t.test('should test setHead (@deprecated)/setIteratorHead method', async (st) => {
const { blockchain, blocks, error } = await generateBlockchain(25)
st.error(error, 'no error')

const headBlockIndex = 5

const headHash = blocks[headBlockIndex].hash()
await blockchain.setHead('myHead', headHash)
await blockchain.setIteratorHead('myHead', headHash)
const currentHeadBlock = await blockchain.getHead('myHead')

st.ok(headHash.equals(currentHeadBlock.hash()), 'head hash equals the provided head hash')
Expand Down
3 changes: 1 addition & 2 deletions packages/client/lib/sync/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ export class VMExecution extends Execution {
let numExecuted: number | undefined

const blockchain = this.vm.blockchain
let startHeadBlock = await this.vm.blockchain.getHead()
let startHeadBlock = await this.vm.blockchain.getIteratorHead()
let canonicalHead = await this.vm.blockchain.getLatestBlock()

let headBlock: Block | undefined
let parentState: Buffer | undefined

while (
(numExecuted === undefined || numExecuted === this.NUM_BLOCKS_PER_ITERATION) &&
!startHeadBlock.hash().equals(canonicalHead.hash()) &&
Expand Down
6 changes: 3 additions & 3 deletions packages/client/test/sync/execution/vmexecution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ tape('[VMExecution]', async (t) => {
config,
chain,
})
t.equals(exec.vm, vm, 'should use provided')
t.equals(exec.vm, vm, 'should use vm provided')
t.end()
})

async function testSetup(blockchain: Blockchain, common?: Common) {
const vm = new VM()
const config = new Config({ common, vm, loglevel: 'error', transports: [] })
const config = new Config({ common, loglevel: 'error', transports: [] })

const chain = new Chain({ config, blockchain })
const exec = new VMExecution({
config,
Expand Down

0 comments on commit a5421b8

Please sign in to comment.