Skip to content

Commit 45e7b5d

Browse files
remove async from util
restore skip test in state manager change promisify to util.promisify
1 parent 288572d commit 45e7b5d

File tree

3 files changed

+40
-65
lines changed

3 files changed

+40
-65
lines changed

packages/blockchain/src/index.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,12 @@ import {
1414
numberToHashKey,
1515
tdKey,
1616
} from './util'
17+
const promisify = require('util.promisify')
1718

1819
const Stoplight = require('flow-stoplight')
1920
const level = require('level-mem')
2021
const semaphore = require('semaphore')
2122

22-
// promisify a function: resolves this promise if callback is called
23-
function promisify(func: Function) {
24-
return function () {
25-
return new Promise((resolve, reject) => {
26-
func(function (err: any, value: any) {
27-
if (err) {
28-
reject(err)
29-
} else {
30-
resolve(value)
31-
}
32-
})
33-
})
34-
}
35-
}
36-
3723
export interface BlockchainInterface {
3824
/**
3925
* Adds a block to the blockchain.
@@ -551,7 +537,7 @@ export default class Blockchain implements BlockchainInterface {
551537
cb(err)
552538
})
553539

554-
function verifyPOW(next: any) {
540+
function verifyPOW(value: any, next: any) {
555541
if (!self._validatePow) {
556542
return next()
557543
}
@@ -561,7 +547,7 @@ export default class Blockchain implements BlockchainInterface {
561547
})
562548
}
563549

564-
function getCurrentTd(next: any) {
550+
function getCurrentTd(value: any, next: any) {
565551
if (isGenesis) {
566552
currentTd.header = new BN(0)
567553
currentTd.block = new BN(0)
@@ -592,7 +578,7 @@ export default class Blockchain implements BlockchainInterface {
592578
})
593579
}
594580

595-
function getBlockTd(next: any) {
581+
function getBlockTd(value: any, next: any) {
596582
if (isGenesis) {
597583
return next()
598584
}
@@ -607,7 +593,7 @@ export default class Blockchain implements BlockchainInterface {
607593
})
608594
}
609595

610-
function rebuildInfo(next: any) {
596+
function rebuildInfo(prevValue: any, next: any) {
611597
// save block and total difficulty to the database
612598
let key = tdKey(number, hash)
613599
let value = rlp.encode(td)
@@ -1034,7 +1020,7 @@ export default class Blockchain implements BlockchainInterface {
10341020
}
10351021

10361022
// check if block is in the canonical chain
1037-
function checkCanonical(cb2: any) {
1023+
function checkCanonical(value: any, cb2: any) {
10381024
self._numberToHash(blockNumber, (err?: any, hash?: any) => {
10391025
inCanonical = !err && hash.equals(hash)
10401026
cb2()
@@ -1043,12 +1029,12 @@ export default class Blockchain implements BlockchainInterface {
10431029

10441030
// delete the block, and if block is in the canonical chain, delete all
10451031
// children as well
1046-
function buildDBops(cb2: any) {
1032+
function buildDBops(value: any, cb2: any) {
10471033
self._delChild(hash, blockNumber, inCanonical ? parentHash : null, dbOps, cb2)
10481034
}
10491035

10501036
// delete all number to hash mappings for deleted block number and above
1051-
function deleteStaleAssignments(cb2: any) {
1037+
function deleteStaleAssignments(value: any, cb2: any) {
10521038
if (inCanonical) {
10531039
self._deleteStaleAssignments(blockNumber, parentHash, dbOps, cb2)
10541040
} else {
@@ -1165,7 +1151,7 @@ export default class Blockchain implements BlockchainInterface {
11651151
.then(function () {
11661152
blockNumber.iaddn(1)
11671153
})
1168-
.catch(function (error) {
1154+
.catch(function (error: any) {
11691155
blockNumber = false
11701156
if (error.type !== 'NotFoundError') {
11711157
throw error
@@ -1182,7 +1168,7 @@ export default class Blockchain implements BlockchainInterface {
11821168
})
11831169
}
11841170

1185-
function runFunc(cb3: any) {
1171+
function runFunc(value: any, cb3: any) {
11861172
const reorg = lastBlock ? lastBlock.hash().equals(block.header.parentHash) : false
11871173
lastBlock = block
11881174
func(block, reorg, cb3)

packages/vm/tests/api/state/stateManager.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ tape('StateManager', t => {
178178
})
179179

180180
t.test('should generate the genesis state root correctly for mainnet from common', async st => {
181+
if (isRunningInKarma()) {
182+
st.skip('skip slow test when running in karma')
183+
return st.end()
184+
}
181185
const common = new Common('mainnet', 'petersburg')
182186
const expectedStateRoot = Buffer.from(common.genesis().stateRoot.slice(2), 'hex')
183187
const stateManager = new StateManager({ common: common })

packages/vm/tests/util.js

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const async = require('async')
21
const { BN, rlp, keccak256, stripHexPrefix, setLengthLeft } = require('ethereumjs-util')
32
const Account = require('@ethereumjs/account').default
43
const Transaction = require('@ethereumjs/tx').Transaction
@@ -38,32 +37,23 @@ exports.dumpState = function (state, cb) {
3837
})
3938
}
4039

41-
readAccounts(state).then(function (accounts) {
42-
async.mapSeries(
43-
accounts,
44-
function (account, cb) {
45-
readStorage(state, account).then((storage) => {
46-
account.storage = storage
47-
cb(null, account)
48-
})
49-
},
50-
function (err, results) {
51-
if (err) {
52-
cb(err, null)
53-
}
54-
for (let i = 0; i < results.length; i++) {
55-
console.log("SHA3'd address: " + results[i].address.toString('hex'))
56-
console.log('\tstate root: ' + results[i].stateRoot.toString('hex'))
57-
console.log('\tstorage: ')
58-
for (let storageKey in results[i].storage) {
59-
console.log('\t\t' + storageKey + ': ' + results[i].storage[storageKey])
60-
}
61-
console.log('\tnonce: ' + new BN(results[i].nonce).toString())
62-
console.log('\tbalance: ' + new BN(results[i].balance).toString())
63-
}
64-
return cb()
65-
},
66-
)
40+
readAccounts(state).then(async function (accounts) {
41+
let results = []
42+
for (let key = 0; key < accounts.length; key++) {
43+
let result = await readStorage(state, accounts[key])
44+
results.push(result)
45+
}
46+
for (let i = 0; i < results.length; i++) {
47+
console.log("SHA3'd address: " + results[i].address.toString('hex'))
48+
console.log('\tstate root: ' + results[i].stateRoot.toString('hex'))
49+
console.log('\tstorage: ')
50+
for (let storageKey in results[i].storage) {
51+
console.log('\t\t' + storageKey + ': ' + results[i].storage[storageKey])
52+
}
53+
console.log('\tnonce: ' + new BN(results[i].nonce).toString())
54+
console.log('\tbalance: ' + new BN(results[i].balance).toString())
55+
}
56+
cb()
6757
})
6858
}
6959

@@ -124,9 +114,7 @@ exports.verifyPostConditions = function (state, testData, t, cb) {
124114
keyMap[hash] = key
125115
}
126116

127-
var q = async.queue(function (task, cb2) {
128-
exports.verifyAccountPostConditions(state, task.address, task.account, task.testData, t, cb2)
129-
}, 1)
117+
var q = []
130118

131119
var stream = state.createReadStream()
132120

@@ -138,29 +126,26 @@ exports.verifyPostConditions = function (state, testData, t, cb) {
138126
delete keyMap[key]
139127

140128
if (testData) {
141-
q.push({
142-
address: address,
143-
account: acnt,
144-
testData: testData,
145-
})
129+
q.push(new Promise((resolve, reject) => {
130+
exports.verifyAccountPostConditions(state, address, account, testData, t, function() {
131+
resolve()
132+
})
133+
}))
146134
} else {
147135
t.fail('invalid account in the trie: ' + key)
148136
}
149137
})
150138

151-
stream.on('end', function () {
139+
stream.on('end', async function () {
152140
function onEnd() {
153141
for (hash in keyMap) {
154142
t.fail('Missing account!: ' + keyMap[hash])
155143
}
156144
cb()
157145
}
158146

159-
if (q.length()) {
160-
q.drain = onEnd
161-
} else {
162-
onEnd()
163-
}
147+
await Promise.all(q)
148+
onEnd()
164149
})
165150
}
166151

0 commit comments

Comments
 (0)