mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
WIP
This commit is contained in:
@@ -1,41 +1,15 @@
|
|||||||
//import {ethers} from 'https://unpkg.com/ethers/dist/ethers.js'
|
|
||||||
import {ethers} from 'https://unpkg.com/ethers@5.7.2/dist/ethers.esm.js'
|
import {ethers} from 'https://unpkg.com/ethers@5.7.2/dist/ethers.esm.js'
|
||||||
|
|
||||||
const URL = 'https://ethereum-goerli-rpc.allthatnode.com'
|
const URL = 'https://rpc.ankr.com/eth_goerli'
|
||||||
|
|
||||||
const p = ethers.getDefaultProvider(URL)
|
const p = ethers.getDefaultProvider(URL)
|
||||||
await p._networkPromise
|
|
||||||
|
|
||||||
|
|
||||||
const latest = await p.getBlock()
|
const latest = await p.getBlock()
|
||||||
latest
|
|
||||||
|
|
||||||
|
|
||||||
const txs = await Promise.all(latest.transactions.map(t =>
|
const txs = await Promise.all(latest.transactions.map(t =>
|
||||||
p.getTransactionReceipt(t)
|
p.getTransactionReceipt(t)
|
||||||
))
|
))
|
||||||
|
|
||||||
|
const totalGas = txs
|
||||||
const totalGas = txs.reduce((gas,tx) =>
|
.filter(tx => tx != null)
|
||||||
gas.add(tx.gasUsed), ethers.BigNumber.from(0))
|
.reduce((gas,tx) => gas.add(tx.gasUsed), ethers.BigNumber.from(0))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
totalGas.add(21)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
const totalGas = txs.reduce((gas,tx) =>
|
|
||||||
gas + tx.gasUsed, BigInt(0))
|
|
||||||
|
|
||||||
totalGas + 1
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -610,6 +610,7 @@ export const find_call = (state, index) => {
|
|||||||
if(call != null) {
|
if(call != null) {
|
||||||
if(call.has_more_children) {
|
if(call.has_more_children) {
|
||||||
active_calltree_node = eval_expand_calltree_node(
|
active_calltree_node = eval_expand_calltree_node(
|
||||||
|
// TODO copy eval_cxt?
|
||||||
state.eval_cxt,
|
state.eval_cxt,
|
||||||
state.parse_result,
|
state.parse_result,
|
||||||
call
|
call
|
||||||
@@ -625,6 +626,7 @@ export const find_call = (state, index) => {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const find_result = eval_find_call(
|
const find_result = eval_find_call(
|
||||||
|
// TODO copy eval_cxt?
|
||||||
state.eval_cxt,
|
state.eval_cxt,
|
||||||
state.parse_result,
|
state.parse_result,
|
||||||
state.calltree,
|
state.calltree,
|
||||||
|
|||||||
@@ -216,13 +216,6 @@ const io_patch = (path, use_context = false) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const apply_io_patches = () => {
|
export const apply_io_patches = () => {
|
||||||
// TODO remove, only for dev
|
|
||||||
// TODO test open_run_window
|
|
||||||
if(cxt.window.__io_patched) {
|
|
||||||
throw new Error('illegal state')
|
|
||||||
}
|
|
||||||
cxt.window.__io_patched = true
|
|
||||||
|
|
||||||
io_patch(['Math', 'random'])
|
io_patch(['Math', 'random'])
|
||||||
|
|
||||||
io_patch(['setTimeout'])
|
io_patch(['setTimeout'])
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ export const do_eval_expand_calltree_node = (cxt, node) => {
|
|||||||
synchronous for simplicity
|
synchronous for simplicity
|
||||||
*/
|
*/
|
||||||
export const do_eval_find_call = (cxt, calltree, location) => {
|
export const do_eval_find_call = (cxt, calltree, location) => {
|
||||||
// TODO remove
|
// Sanity check
|
||||||
if(cxt.children != null) {
|
if(cxt.children != null) {
|
||||||
throw new Error('illegal state')
|
throw new Error('illegal state')
|
||||||
}
|
}
|
||||||
|
|||||||
10
test/test.js
10
test/test.js
@@ -2882,7 +2882,7 @@ const y = x()`
|
|||||||
test('async/await logs out of order timeout', async () => {
|
test('async/await logs out of order timeout', async () => {
|
||||||
const i = await test_initial_state_async(`
|
const i = await test_initial_state_async(`
|
||||||
const delay = async time => {
|
const delay = async time => {
|
||||||
await new Promise(res => globalThis.setTimeout(res, time*10))
|
await new Promise(res => setTimeout(res, time*10))
|
||||||
console.log(time)
|
console.log(time)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3091,10 +3091,8 @@ const y = x()`
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
test('record io fetch rejects', async () => {
|
test('record io fetch rejects', async () => {
|
||||||
const original_fetch = globalThis.run_window.fetch
|
|
||||||
|
|
||||||
// Patch fetch
|
// Patch fetch
|
||||||
Object.assign(globalThis.run_window, {fetch: () => Promise.reject('fail')})
|
patch_builtin('fetch', () => Promise.reject('fail'))
|
||||||
|
|
||||||
const initial = await test_initial_state_async(`
|
const initial = await test_initial_state_async(`
|
||||||
await fetch('url', {method: 'GET'})
|
await fetch('url', {method: 'GET'})
|
||||||
@@ -3102,7 +3100,7 @@ const y = x()`
|
|||||||
assert_equal(root_calltree_node(initial).error, 'fail')
|
assert_equal(root_calltree_node(initial).error, 'fail')
|
||||||
|
|
||||||
// Patch fetch again
|
// Patch fetch again
|
||||||
Object.assign(globalThis.run_window, {fetch: async () => 'result'})
|
patch_builtin('fetch', () => async () => 'result')
|
||||||
|
|
||||||
const with_cache = await command_input_async(initial, `
|
const with_cache = await command_input_async(initial, `
|
||||||
await fetch('url', {method: 'GET'})
|
await fetch('url', {method: 'GET'})
|
||||||
@@ -3110,7 +3108,7 @@ const y = x()`
|
|||||||
assert_equal(root_calltree_node(initial).error, 'fail')
|
assert_equal(root_calltree_node(initial).error, 'fail')
|
||||||
|
|
||||||
// Remove patch
|
// Remove patch
|
||||||
Object.assign(globalThis.run_window, {fetch: original_fetch})
|
patch_builtin('fetch', null)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
test('record io preserve promise resolution order', async () => {
|
test('record io preserve promise resolution order', async () => {
|
||||||
|
|||||||
@@ -12,36 +12,39 @@ Object.assign(globalThis,
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
export const original_setTimeout = globalThis.setTimeout
|
|
||||||
|
|
||||||
export const patch_builtin = new Function(`
|
export const patch_builtin = new Function(`
|
||||||
// Substitute some builtin functions: fetch, setTimeout, Math.random to be
|
let originals = globalThis.run_window.__builtins_originals
|
||||||
// able to patch them in tests
|
let patched = globalThis.run_window.__builtins_patched
|
||||||
|
if(originals == null) {
|
||||||
const originals = {
|
globalThis.run_window.__original_setTimeout = globalThis.setTimeout
|
||||||
random: Math.random,
|
// This code can execute twice when tests are run in self-hosted mode.
|
||||||
fetch: globalThis.fetch,
|
// Ensure that patches will be applied only once
|
||||||
setTimeout: globalThis.setTimeout,
|
originals = globalThis.run_window.__builtins_originals = {}
|
||||||
}
|
patched = globalThis.run_window.__builtins_patched = {}
|
||||||
|
|
||||||
const patched = {}
|
|
||||||
|
|
||||||
const patch = (obj, name) => {
|
const patch = (obj, name) => {
|
||||||
originals[name] = obj[name]
|
originals[name] = obj[name]
|
||||||
obj[name] = (...args) => patched[name] == null
|
obj[name] = (...args) => {
|
||||||
? originals[name](...args)
|
return patched[name] == null
|
||||||
: patched[name](...args)
|
? originals[name].apply(null, args)
|
||||||
|
: patched[name].apply(null, args)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
patch(globalThis, 'fetch')
|
// Substitute some builtin functions: fetch, setTimeout, Math.random to be
|
||||||
patch(globalThis, 'setTimeout')
|
// able to patch them in tests
|
||||||
patch(Math, 'random')
|
patch(globalThis.run_window, 'fetch')
|
||||||
|
patch(globalThis.run_window, 'setTimeout')
|
||||||
|
patch(globalThis.run_window.Math, 'random')
|
||||||
|
}
|
||||||
|
|
||||||
return (name, fn) => {
|
return (name, fn) => {
|
||||||
patched[name] = fn
|
patched[name] = fn
|
||||||
}
|
}
|
||||||
`)()
|
`)()
|
||||||
|
|
||||||
|
export const original_setTimeout = globalThis.run_window.__original_setTimeout
|
||||||
|
|
||||||
export const parse_modules = (entry, modules) =>
|
export const parse_modules = (entry, modules) =>
|
||||||
load_modules(entry, module_name => modules[module_name])
|
load_modules(entry, module_name => modules[module_name])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user