-
Notifications
You must be signed in to change notification settings - Fork 850
Expand file tree
/
Copy pathhelpers.ts
More file actions
109 lines (100 loc) · 2.69 KB
/
Copy pathhelpers.ts
File metadata and controls
109 lines (100 loc) · 2.69 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { BlobEIP4844Transaction } from '@ethereumjs/tx'
import { TypeOutput, isHexString, toType } from '@ethereumjs/util'
import type { BlockHeaderBytes, HeaderData } from './types'
import type { TypedTransaction } from '@ethereumjs/tx'
/**
* Returns a 0x-prefixed hex number string from a hex string or string integer.
* @param {string} input string to check, convert, and return
*/
export const numberToHex = function (input?: string) {
if (input === undefined) return undefined
if (!isHexString(input)) {
const regex = new RegExp(/^\d+$/) // test to make sure input contains only digits
if (!regex.test(input)) {
const msg = `Cannot convert string to hex string. numberToHex only supports 0x-prefixed hex or integer strings but the given string was: ${input}`
throw new Error(msg)
}
return '0x' + parseInt(input, 10).toString(16)
}
return input
}
export function valuesArrayToHeaderData(values: BlockHeaderBytes): HeaderData {
const [
parentHash,
uncleHash,
coinbase,
stateRoot,
transactionsTrie,
receiptTrie,
logsBloom,
difficulty,
number,
gasLimit,
gasUsed,
timestamp,
extraData,
mixHash,
nonce,
baseFeePerGas,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
] = values
if (values.length > 19) {
throw new Error('invalid header. More values than expected were received')
}
if (values.length < 15) {
throw new Error('invalid header. Less values than expected were received')
}
return {
parentHash,
uncleHash,
coinbase,
stateRoot,
transactionsTrie,
receiptTrie,
logsBloom,
difficulty,
number,
gasLimit,
gasUsed,
timestamp,
extraData,
mixHash,
nonce,
baseFeePerGas,
withdrawalsRoot,
dataGasUsed,
excessDataGas,
}
}
export function getDifficulty(headerData: HeaderData): bigint | null {
const { difficulty } = headerData
if (difficulty !== undefined) {
return toType(difficulty, TypeOutput.BigInt)
}
return null
}
export const getNumBlobs = (transactions: TypedTransaction[]) => {
let numBlobs = 0
for (const tx of transactions) {
if (tx instanceof BlobEIP4844Transaction) {
numBlobs += tx.versionedHashes.length
}
}
return numBlobs
}
/**
* Approximates `factor * e ** (numerator / denominator)` using Taylor expansion
*/
export const fakeExponential = (factor: bigint, numerator: bigint, denominator: bigint) => {
let i = BigInt(1)
let output = BigInt(0)
let numerator_accum = factor * denominator
while (numerator_accum > BigInt(0)) {
output += numerator_accum
numerator_accum = (numerator_accum * numerator) / (denominator * i)
i++
}
return output / denominator
}