|
|
|
@ -29,36 +29,53 @@ export class VMProvider { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async resetEnvironment () { |
|
|
|
|
if (this.worker) { |
|
|
|
|
const provider = this.executionContext.getProviderObject() |
|
|
|
|
|
|
|
|
|
this.worker.postMessage({ |
|
|
|
|
cmd: 'init', |
|
|
|
|
fork: this.executionContext.getCurrentFork(), |
|
|
|
|
nodeUrl: provider?.options['nodeUrl'], |
|
|
|
|
blockNumber: provider?.options['blockNumber'] |
|
|
|
|
}) |
|
|
|
|
} else { |
|
|
|
|
async resetEnvironment (stringifiedStateDb?: string) { |
|
|
|
|
if (this.worker) this.worker.terminate() |
|
|
|
|
this.worker = new Worker(new URL('./worker-vm', import.meta.url)) |
|
|
|
|
this.setWorkerEventListeners(this.worker) |
|
|
|
|
const provider = this.executionContext.getProviderObject() |
|
|
|
|
|
|
|
|
|
this.worker.postMessage({ |
|
|
|
|
cmd: 'init', |
|
|
|
|
fork: this.executionContext.getCurrentFork(), |
|
|
|
|
nodeUrl: provider?.options['nodeUrl'], |
|
|
|
|
blockNumber: provider?.options['blockNumber'] |
|
|
|
|
let incr = 0 |
|
|
|
|
const stamps = {} |
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
this.worker.addEventListener('message', (msg) => { |
|
|
|
|
if (msg.data.cmd === 'sendAsyncResult' && stamps[msg.data.stamp]) { |
|
|
|
|
if (stamps[msg.data.stamp].callback) { |
|
|
|
|
stamps[msg.data.stamp].callback(msg.data.error, msg.data.result) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if (msg.data.error) { |
|
|
|
|
stamps[msg.data.stamp].reject(msg.data.error) |
|
|
|
|
} else { |
|
|
|
|
stamps[msg.data.stamp].resolve(msg.data.result) |
|
|
|
|
}
|
|
|
|
|
} else if (msg.data.cmd === 'initiateResult') { |
|
|
|
|
if (!msg.data.error) { |
|
|
|
|
this.provider = { |
|
|
|
|
sendAsync: (query, callback) => { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
const stamp = Date.now() + incr |
|
|
|
|
incr++ |
|
|
|
|
stamps[stamp] = { callback, resolve, reject } |
|
|
|
|
this.worker.postMessage({ cmd: 'sendAsync', query, stamp })
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async loadContext (stringifiedStateDb: string) { |
|
|
|
|
if (this.worker) this.worker.terminate() |
|
|
|
|
this.worker = new Worker(new URL('./worker-vm', import.meta.url)) |
|
|
|
|
this.setWorkerEventListeners(this.worker) |
|
|
|
|
const provider = this.executionContext.getProviderObject() |
|
|
|
|
|
|
|
|
|
this.web3 = new Web3(this.provider as LegacySendAsyncProvider) |
|
|
|
|
this.web3.setConfig({ defaultTransactionType: '0x0' }) |
|
|
|
|
extend(this.web3) |
|
|
|
|
this.executionContext.setWeb3(this.executionContext.getProvider(), this.web3) |
|
|
|
|
resolve({}) |
|
|
|
|
} else { |
|
|
|
|
reject(new Error(msg.data.error)) |
|
|
|
|
} |
|
|
|
|
} else if (msg.data.cmd === 'newAccountResult') { |
|
|
|
|
if (this.newAccountCallback[msg.data.stamp]) { |
|
|
|
|
this.newAccountCallback[msg.data.stamp](msg.data.error, msg.data.result) |
|
|
|
|
delete this.newAccountCallback[msg.data.stamp] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
this.worker.postMessage({ |
|
|
|
|
cmd: 'init', |
|
|
|
|
fork: this.executionContext.getCurrentFork(), |
|
|
|
@ -66,8 +83,10 @@ export class VMProvider { |
|
|
|
|
blockNumber: provider?.options['blockNumber'], |
|
|
|
|
stateDb: stringifiedStateDb |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: is still here because of the plugin API
|
|
|
|
|
// can be removed later when we update the API
|
|
|
|
|
createVMAccount (newAccount) { |
|
|
|
@ -105,12 +124,13 @@ export class VMProvider { |
|
|
|
|
return this.executionContext.getProvider() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private setWorkerEventListeners (worker: Worker) { |
|
|
|
|
if (!worker) throw new Error('Worker not initialized') |
|
|
|
|
private async setWorkerEventListeners () { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
if (!this.worker) throw new Error('Worker not initialized') |
|
|
|
|
let incr = 0 |
|
|
|
|
const stamps = {} |
|
|
|
|
|
|
|
|
|
worker.addEventListener('message', (msg) => { |
|
|
|
|
this.worker.addEventListener('message', (msg) => { |
|
|
|
|
if (msg.data.cmd === 'sendAsyncResult' && stamps[msg.data.stamp]) { |
|
|
|
|
if (stamps[msg.data.stamp].callback) { |
|
|
|
|
stamps[msg.data.stamp].callback(msg.data.error, msg.data.result) |
|
|
|
@ -129,7 +149,7 @@ export class VMProvider { |
|
|
|
|
const stamp = Date.now() + incr |
|
|
|
|
incr++ |
|
|
|
|
stamps[stamp] = { callback, resolve, reject } |
|
|
|
|
worker.postMessage({ cmd: 'sendAsync', query, stamp })
|
|
|
|
|
this.worker.postMessage({ cmd: 'sendAsync', query, stamp })
|
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -137,9 +157,9 @@ export class VMProvider { |
|
|
|
|
this.web3.setConfig({ defaultTransactionType: '0x0' }) |
|
|
|
|
extend(this.web3) |
|
|
|
|
this.executionContext.setWeb3(this.executionContext.getProvider(), this.web3) |
|
|
|
|
resolve({}) |
|
|
|
|
} else { |
|
|
|
|
console.error(msg.data.error) |
|
|
|
|
throw new Error(msg.data.error) |
|
|
|
|
reject(new Error(msg.data.error)) |
|
|
|
|
} |
|
|
|
|
} else if (msg.data.cmd === 'newAccountResult') { |
|
|
|
|
if (this.newAccountCallback[msg.data.stamp]) { |
|
|
|
@ -148,5 +168,6 @@ export class VMProvider { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|