-
Notifications
You must be signed in to change notification settings - Fork 73
/
time-travel.ts
42 lines (35 loc) · 1.34 KB
/
time-travel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { BN, u8aToHex } from '@polkadot/util'
import { HexString } from '@polkadot/util/types'
import { Blockchain } from '../blockchain/index.js'
import { compactHex, getSlotDuration } from './index.js'
import { setStorage } from './set-storage.js'
export const timeTravel = async (chain: Blockchain, timestamp: number) => {
const meta = await chain.head.meta
const slotDuration = await getSlotDuration(chain.head)
const newSlot = Math.floor(timestamp / slotDuration)
// new timestamp
const storage: [HexString, HexString][] = [
[compactHex(meta.query.timestamp.now()), u8aToHex(meta.registry.createType('u64', timestamp).toU8a())],
]
if (meta.consts.babe) {
// new slot
storage.push([
compactHex(meta.query.babe.currentSlot()),
u8aToHex(meta.registry.createType('Slot', newSlot).toU8a()),
])
// new epoch
const epochDuration = (meta.consts.babe.epochDuration as any as BN).toNumber()
const newEpoch = Math.floor(timestamp / epochDuration)
storage.push([
compactHex(meta.query.babe.epochIndex()),
u8aToHex(meta.registry.createType('u64', newEpoch).toU8a()),
])
} else if (meta.query.aura) {
// new slot
storage.push([
compactHex(meta.query.aura.currentSlot()),
u8aToHex(meta.registry.createType('Slot', newSlot).toU8a()),
])
}
await setStorage(chain, storage)
}