Skip to content

Commit 2d612b5

Browse files
authored
fix leading 0 in gasUsed (#142)
1 parent 9e23935 commit 2d612b5

File tree

3 files changed

+190
-15
lines changed

3 files changed

+190
-15
lines changed

src/external/Collector.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { sleep } from '../utils'
1818
import { BaseTrie } from 'merkle-patricia-tree'
1919
import * as RLP from 'rlp'
2020
import { BigNumber } from '@ethersproject/bignumber'
21+
import { calculateBlockGasUsed } from '../utils/gasCalculator'
22+
2123
class Collector extends BaseExternal {
2224
private blockCacheManager: BlockCacheManager
2325

@@ -349,21 +351,7 @@ class Collector extends BaseExternal {
349351

350352
if (txResponse.data.success && txResponse.data.transactions) {
351353
resultBlock.transactions = txResponse.data.transactions.map((tx: any) => this.decodeTransaction(tx))
352-
// Calculate block gas used with proper hex value handling
353-
let blockGasUsed = BigNumber.from(0)
354-
txResponse.data.transactions.forEach((tx: any) => {
355-
const gasUsedHex = tx.wrappedEVMAccount?.readableReceipt?.gasUsed
356-
if (gasUsedHex && gasUsedHex !== '0x' && gasUsedHex !== '0x0') {
357-
try {
358-
// Normalize if necessary before converting to BigNumber
359-
const gasUsed = BigNumber.from(gasUsedHex)
360-
blockGasUsed = blockGasUsed.add(gasUsed)
361-
} catch (gasError) {
362-
console.warn('Invalid gas value in transaction:', tx.hash, gasUsedHex)
363-
}
364-
}
365-
})
366-
resultBlock.gasUsed = blockGasUsed.toHexString()
354+
resultBlock.gasUsed = calculateBlockGasUsed(txResponse.data.transactions)
367355

368356
// Extract transaction hash array
369357
const transactionHashes = resultBlock.transactions.map((tx: any) => tx.hash)

src/utils/gasCalculator.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { BigNumber } from '@ethersproject/bignumber'
2+
3+
/**
4+
* Calculates the total gas used from an array of transactions
5+
* @param transactions Array of transactions with optional gas values
6+
* @returns Hex string of total gas used, without leading zeros (except for 0x0)
7+
*/
8+
export function calculateBlockGasUsed(transactions: any[]): string {
9+
let blockGasUsed = BigNumber.from(0)
10+
transactions.forEach((tx: any) => {
11+
const gasUsedHex = tx.wrappedEVMAccount?.readableReceipt?.gasUsed
12+
if (gasUsedHex && gasUsedHex !== '0x' && gasUsedHex !== '0x0') {
13+
try {
14+
const gasValue = BigNumber.from(gasUsedHex)
15+
blockGasUsed = blockGasUsed.add(gasValue)
16+
} catch (gasError) {
17+
console.warn('Invalid gas value in transaction:', tx.hash, gasUsedHex)
18+
}
19+
}
20+
})
21+
22+
// Handle special case for zero
23+
if (blockGasUsed.isZero()) {
24+
return '0x0'
25+
}
26+
27+
// Convert to hex and remove leading zeros
28+
const hexString = blockGasUsed.toHexString()
29+
return hexString.replace(/0x0+/, '0x')
30+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { calculateBlockGasUsed } from '../../../src/utils/gasCalculator'
2+
3+
describe('gasCalculator', () => {
4+
describe('calculateBlockGasUsed', () => {
5+
it('should return 0x0 for empty transactions', () => {
6+
const result = calculateBlockGasUsed([])
7+
expect(result).toBe('0x0')
8+
})
9+
10+
it('should handle single transaction with valid gas', () => {
11+
const transactions = [
12+
{
13+
wrappedEVMAccount: {
14+
readableReceipt: {
15+
gasUsed: '0x123',
16+
hash: '0xabc',
17+
},
18+
},
19+
},
20+
]
21+
const result = calculateBlockGasUsed(transactions)
22+
expect(result).toBe('0x123')
23+
})
24+
25+
it('should sum multiple transactions with valid gas', () => {
26+
const transactions = [
27+
{
28+
wrappedEVMAccount: {
29+
readableReceipt: {
30+
gasUsed: '0x123',
31+
hash: '0xabc',
32+
},
33+
},
34+
},
35+
{
36+
wrappedEVMAccount: {
37+
readableReceipt: {
38+
gasUsed: '0x456',
39+
hash: '0xdef',
40+
},
41+
},
42+
},
43+
]
44+
const result = calculateBlockGasUsed(transactions)
45+
expect(result).toBe('0x579')
46+
})
47+
48+
it('should handle zero gas values', () => {
49+
const transactions = [
50+
{
51+
wrappedEVMAccount: {
52+
readableReceipt: {
53+
gasUsed: '0x0',
54+
hash: '0xabc',
55+
},
56+
},
57+
},
58+
{
59+
wrappedEVMAccount: {
60+
readableReceipt: {
61+
gasUsed: '0x123',
62+
hash: '0xdef',
63+
},
64+
},
65+
},
66+
]
67+
const result = calculateBlockGasUsed(transactions)
68+
expect(result).toBe('0x123')
69+
})
70+
71+
it('should handle empty gas values', () => {
72+
const transactions = [
73+
{
74+
wrappedEVMAccount: {
75+
readableReceipt: {
76+
gasUsed: '',
77+
hash: '0xabc',
78+
},
79+
},
80+
},
81+
{
82+
wrappedEVMAccount: {
83+
readableReceipt: {
84+
gasUsed: '0x123',
85+
hash: '0xdef',
86+
},
87+
},
88+
},
89+
]
90+
const result = calculateBlockGasUsed(transactions)
91+
expect(result).toBe('0x123')
92+
})
93+
94+
it('should handle invalid gas values', () => {
95+
const transactions = [
96+
{
97+
wrappedEVMAccount: {
98+
readableReceipt: {
99+
gasUsed: 'invalid',
100+
hash: '0xabc',
101+
},
102+
},
103+
},
104+
{
105+
wrappedEVMAccount: {
106+
readableReceipt: {
107+
gasUsed: '0x123',
108+
hash: '0xdef',
109+
},
110+
},
111+
},
112+
]
113+
const result = calculateBlockGasUsed(transactions)
114+
expect(result).toBe('0x123')
115+
})
116+
117+
it('should handle missing gas values', () => {
118+
const transactions = [
119+
{
120+
wrappedEVMAccount: {
121+
readableReceipt: {
122+
hash: '0xabc',
123+
},
124+
},
125+
},
126+
{
127+
wrappedEVMAccount: {
128+
readableReceipt: {
129+
gasUsed: '0x123',
130+
hash: '0xdef',
131+
},
132+
},
133+
},
134+
]
135+
const result = calculateBlockGasUsed(transactions)
136+
expect(result).toBe('0x123')
137+
})
138+
139+
it('should handle missing wrappedEVMAccount', () => {
140+
const transactions = [
141+
{
142+
hash: '0xabc',
143+
},
144+
{
145+
wrappedEVMAccount: {
146+
readableReceipt: {
147+
gasUsed: '0x123',
148+
hash: '0xdef',
149+
},
150+
},
151+
},
152+
]
153+
const result = calculateBlockGasUsed(transactions)
154+
expect(result).toBe('0x123')
155+
})
156+
})
157+
})

0 commit comments

Comments
 (0)