-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
319 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/beacon-state-transition/test/utils/beforeValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export type LazyValue<T> = {value: T}; | ||
|
||
/** | ||
* Register a callback to compute a value in the before() block of mocha tests | ||
* ```ts | ||
* const state = beforeValue(() => getState()) | ||
* it("test", () => { | ||
* doTest(state.value) | ||
* }) | ||
* ``` | ||
*/ | ||
export function beforeValue<T>(fn: () => T | Promise<T>, timeout?: number): LazyValue<T> { | ||
let value: T = (null as unknown) as T; | ||
|
||
before(async function () { | ||
this.timeout(timeout ?? 300_000); | ||
value = await fn(); | ||
}); | ||
|
||
return new Proxy<{value: T}>( | ||
{value}, | ||
{ | ||
get: function (target, prop) { | ||
if (prop === "value") { | ||
if (value === null) { | ||
throw Error("beforeValue has not yet run the before() block"); | ||
} else { | ||
return value; | ||
} | ||
} else { | ||
return undefined; | ||
} | ||
}, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./beforeValue"; | ||
export * from "./testFileCache"; |
File renamed without changes.
116 changes: 116 additions & 0 deletions
116
packages/beacon-state-transition/test/utils/testFileCache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import got from "got"; | ||
import {getClient} from "@chainsafe/lodestar-api"; | ||
import {NetworkName, networksChainConfig} from "@chainsafe/lodestar-config/networks"; | ||
import {createIChainForkConfig, IChainForkConfig} from "@chainsafe/lodestar-config"; | ||
import {CachedBeaconStateAllForks, computeEpochAtSlot} from "../../src"; | ||
import {getInfuraBeaconUrl} from "./infura"; | ||
import {testCachePath} from "../cache"; | ||
import {createCachedBeaconStateTest} from "../utils/state"; | ||
import {allForks} from "@chainsafe/lodestar-types"; | ||
|
||
/** | ||
* Full link example: | ||
* ``` | ||
* https://github.com/dapplion/ethereum-consensus-test-data/releases/download/v0.1.0/block_mainnet_3766821.ssz | ||
* ``` */ | ||
const TEST_FILES_BASE_URL = "https://github.com/dapplion/ethereum-consensus-test-data/releases/download/v0.1.0"; | ||
|
||
/** | ||
* Create a network config from known network params | ||
*/ | ||
export function getNetworkConfig(network: NetworkName): IChainForkConfig { | ||
const configNetwork = networksChainConfig[network]; | ||
return createIChainForkConfig(configNetwork); | ||
} | ||
|
||
/** | ||
* Download a state from Infura. Caches states in local fs by network and slot to only download once. | ||
*/ | ||
export async function getNetworkCachedState( | ||
network: NetworkName, | ||
slot: number, | ||
timeout?: number | ||
): Promise<CachedBeaconStateAllForks> { | ||
const config = getNetworkConfig(network); | ||
const fileId = `state_${network}_${slot}.ssz`; | ||
|
||
const filepath = path.join(testCachePath, fileId); | ||
|
||
if (fs.existsSync(filepath)) { | ||
const stateSsz = fs.readFileSync(filepath); | ||
return createCachedBeaconStateTest(config.getForkTypes(slot).BeaconState.deserializeToViewDU(stateSsz), config); | ||
} else { | ||
const stateSsz = await tryEach([ | ||
() => downloadTestFile(fileId), | ||
() => { | ||
const client = getClient({baseUrl: getInfuraBeaconUrl(network), timeoutMs: timeout ?? 300_000}, {config}); | ||
return computeEpochAtSlot(slot) < config.ALTAIR_FORK_EPOCH | ||
? client.debug.getState(String(slot), "ssz") | ||
: client.debug.getStateV2(String(slot), "ssz"); | ||
}, | ||
]); | ||
|
||
fs.writeFileSync(filepath, stateSsz); | ||
return createCachedBeaconStateTest(config.getForkTypes(slot).BeaconState.deserializeToViewDU(stateSsz), config); | ||
} | ||
} | ||
|
||
/** | ||
* Download a state from Infura. Caches states in local fs by network and slot to only download once. | ||
*/ | ||
export async function getNetworkCachedBlock( | ||
network: NetworkName, | ||
slot: number, | ||
timeout?: number | ||
): Promise<allForks.SignedBeaconBlock> { | ||
const config = getNetworkConfig(network); | ||
const fileId = `block_${network}_${slot}.ssz`; | ||
|
||
const filepath = path.join(testCachePath, fileId); | ||
|
||
if (fs.existsSync(filepath)) { | ||
const blockSsz = fs.readFileSync(filepath); | ||
return config.getForkTypes(slot).SignedBeaconBlock.deserialize(blockSsz); | ||
} else { | ||
const blockSsz = await tryEach([ | ||
() => downloadTestFile(fileId), | ||
async () => { | ||
const client = getClient({baseUrl: getInfuraBeaconUrl(network), timeoutMs: timeout ?? 300_000}, {config}); | ||
|
||
const res = | ||
computeEpochAtSlot(slot) < config.ALTAIR_FORK_EPOCH | ||
? await client.beacon.getBlock(String(slot)) | ||
: await client.beacon.getBlockV2(String(slot)); | ||
return config.getForkTypes(slot).SignedBeaconBlock.serialize(res.data); | ||
}, | ||
]); | ||
|
||
fs.writeFileSync(filepath, blockSsz); | ||
return config.getForkTypes(slot).SignedBeaconBlock.deserialize(blockSsz); | ||
} | ||
} | ||
|
||
async function downloadTestFile(fileId: string): Promise<Buffer> { | ||
const fileUrl = `${TEST_FILES_BASE_URL}/${fileId}`; | ||
// eslint-disable-next-line no-console | ||
console.log(`Downloading file ${fileUrl}`); | ||
|
||
const res = await got(fileUrl, {responseType: "buffer"}); | ||
return res.body; | ||
} | ||
|
||
async function tryEach<T>(promises: (() => Promise<T>)[]): Promise<T> { | ||
const errors: Error[] = []; | ||
|
||
for (let i = 0; i < promises.length; i++) { | ||
try { | ||
return promises[i](); | ||
} catch (e) { | ||
errors.push(e as Error); | ||
} | ||
} | ||
|
||
throw Error(errors.map((e, i) => `Error[${i}] ${e.message}`).join("\n")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.