Skip to content

Commit ace150d

Browse files
Implementing EIP-1559
- Fix bug in verifyEip1559Block method - Add testBlockGasLimits test (works only London to London part yet)
1 parent e607b9f commit ace150d

File tree

2 files changed

+74
-107
lines changed

2 files changed

+74
-107
lines changed

Sources/web3swift/Web3/Web3+EIP1559.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public extension Web3 {
4040
/// and thors an error if it isn't.
4141
func verifyEip1559Block(chain version: ChainVersion, parent: Block, current: Block) throws -> Bool {
4242
var parentGasLimit = parent.gasLimit
43-
if case .London = version {
43+
if version != .London {
4444
parentGasLimit = parent.gasLimit * Web3.ElasticityMultiplier
4545
}
4646

Tests/web3swiftTests/localTests/EIP1559Tests.swift

Lines changed: 73 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,32 @@ import web3swift
66

77
class EIP1559Tests: XCTestCase {
88

9+
/// testBlockGasLimits tests the gasLimit checks for blocks both across
10+
/// the EIP-1559 boundary and post-1559 blocks
911
func testBlockGasLimits() throws {
10-
let uselessBlockPart = [
11-
// "number": 3,
12-
13-
"hash": Data(from: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46"),
14-
"parentHash": Data(from: "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88"),
15-
// "baseFeePerGas": 58713056622,
16-
"nonce": "0xfb6e1a62d119228b",
17-
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
18-
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
19-
"transactionsRoot": "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee",
20-
"stateRoot": "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb",
21-
"miner": "0x8888f1f195afa192cfee860698584c030f4c9db1",
22-
"difficulty": "21345678965432",
23-
"totalDifficulty": "324567845321",
24-
"size": 616,
25-
"extraData": "0x",
26-
// "gasLimit": 3141592,
27-
"gasUsed": 21662,
28-
"timestamp": 1429287689,
29-
"transactions": [
30-
"0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b"
31-
],
32-
"uncles": []
33-
]
34-
/*
35-
web3.eth.getBlock(3150).then(console.log);
36-
> {
37-
"number": 3,
38-
"hash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
39-
"parentHash": "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88",
40-
"baseFeePerGas": 58713056622,
41-
"nonce": "0xfb6e1a62d119228b",
42-
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
43-
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
44-
"transactionsRoot": "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee",
45-
"stateRoot": "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb",
46-
"miner": "0x8888f1f195afa192cfee860698584c030f4c9db1",
47-
"difficulty": "21345678965432",
48-
"totalDifficulty": "324567845321",
49-
"size": 616,
50-
"extraData": "0x",
51-
"gasLimit": 3141592,
52-
"gasUsed": 21662,
53-
"timestamp": 1429287689,
54-
"transactions": [
55-
"0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b"
56-
],
57-
"uncles": []
58-
}
59-
*/
12+
let uselessBlockPart = (
13+
number: BigUInt(3),
14+
15+
hash: Data(from: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46")!, // "hash":
16+
parentHash: Data(from: "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88")!, // "parentHash":
17+
// baseFeePerGas: 58713056622, // baseFeePerGas":
18+
nonce: Data(from: "0xfb6e1a62d119228b"), // "nonce":
19+
sha3Uncles: Data(from: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "sha3Uncles":
20+
receiptsRoot: Data(from: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "receiptsRoot":
21+
logsBloom: EthereumBloomFilter(Data(from: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!), // "logsBloom":
22+
transactionsRoot: Data(from: "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee")!, // "transactionsRoot":
23+
stateRoot: Data(from: "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb")!, // "stateRoot":
24+
miner: EthereumAddress( Data(from: "0x8888f1f195afa192cfee860698584c030f4c9db1")!)!, // "miner":
25+
difficulty: BigUInt(21345678965432), // "difficulty":
26+
totalDifficulty: BigUInt(324567845321), // "totalDifficulty":
27+
size: BigUInt(616), // "size":
28+
extraData: Data(from: "0x")!, // extraData":
29+
gasLimit: BigUInt(3141592), // "gasLimit":
30+
gasUsed: BigUInt(21662), // "gasUsed":
31+
timestamp: Date(), // "timestamp":
32+
transactions: [TransactionInBlock](), // "transactions":
33+
uncles: [Data]() // "uncles":
34+
)
6035

6136
// [0] - parentGasLimit
6237
// [1] - parentNumber
@@ -82,18 +57,60 @@ class EIP1559Tests: XCTestCase {
8257
(40000000, 5, 39960938, false), // Lower limit -1
8358
]
8459

85-
headerArray.forEach { ( touple: (parentGasLimit: Int, parentNumber: Int, currentGasLimit: Int, isOk: Bool)) in
86-
let parent = Block(number: touple.parentGasLimit , hash: , parentHash: <#T##Data#>, nonce: <#T##Data?#>, sha3Uncles: <#T##Data#>, logsBloom: <#T##EthereumBloomFilter?#>, transactionsRoot: <#T##Data#>, stateRoot: <#T##Data#>, receiptsRoot: <#T##Data#>, miner: <#T##EthereumAddress?#>, difficulty: <#T##BigUInt#>, totalDifficulty: <#T##BigUInt#>, extraData: <#T##Data#>, size: <#T##BigUInt#>, gasLimit: <#T##BigUInt#>, gasUsed: <#T##BigUInt#>, baseFeePerGas: <#T##BigUInt#>, timestamp: <#T##Date#>, transactions: <#T##[TransactionInBlock]#>, uncles: <#T##[Data]#>)
87-
88-
let current = Block()
60+
try headerArray.forEach { ( touple: (parentGasLimit: BigUInt, parentNumber: BigUInt, currentGasLimit: BigUInt, isOk: Bool)) in
61+
let parent = Block(number: touple.parentGasLimit,
62+
hash: uselessBlockPart.hash,
63+
parentHash: uselessBlockPart.parentHash,
64+
nonce: uselessBlockPart.nonce,
65+
sha3Uncles: uselessBlockPart.sha3Uncles,
66+
logsBloom: uselessBlockPart.logsBloom,
67+
transactionsRoot: uselessBlockPart.transactionsRoot,
68+
stateRoot: uselessBlockPart.stateRoot,
69+
receiptsRoot: uselessBlockPart.receiptsRoot,
70+
miner: uselessBlockPart.miner,
71+
difficulty: uselessBlockPart.difficulty,
72+
totalDifficulty: uselessBlockPart.totalDifficulty,
73+
extraData: uselessBlockPart.extraData,
74+
size: uselessBlockPart.size,
75+
gasLimit: touple.parentGasLimit,
76+
gasUsed: touple.parentGasLimit / 2,
77+
baseFeePerGas: Web3.InitialBaseFee,
78+
timestamp: uselessBlockPart.timestamp,
79+
transactions: uselessBlockPart.transactions,
80+
uncles: uselessBlockPart.uncles)
81+
82+
let current = Block(number: touple.parentGasLimit + 1,
83+
hash: uselessBlockPart.hash,
84+
parentHash: uselessBlockPart.parentHash,
85+
nonce: uselessBlockPart.nonce,
86+
sha3Uncles: uselessBlockPart.sha3Uncles,
87+
logsBloom: uselessBlockPart.logsBloom,
88+
transactionsRoot: uselessBlockPart.transactionsRoot,
89+
stateRoot: uselessBlockPart.stateRoot,
90+
receiptsRoot: uselessBlockPart.receiptsRoot,
91+
miner: uselessBlockPart.miner,
92+
difficulty: uselessBlockPart.difficulty,
93+
totalDifficulty: uselessBlockPart.totalDifficulty,
94+
extraData: uselessBlockPart.extraData,
95+
size: uselessBlockPart.size,
96+
gasLimit: touple.currentGasLimit,
97+
gasUsed: touple.currentGasLimit / 2,
98+
baseFeePerGas: Web3.InitialBaseFee,
99+
timestamp: uselessBlockPart.timestamp,
100+
transactions: uselessBlockPart.transactions,
101+
uncles: uselessBlockPart.uncles)
89102

90103
let web3 = Web3()
91104

92-
web3.verifyEip1559Block()
105+
if touple.isOk {
106+
XCTAssertNoThrow(_ = try web3.verifyEip1559Block(chain: .London, parent: parent, current: current), "Shoult not fail, got parent: \(parent.gasLimit), current: \(current.gasLimit)")
107+
} else {
108+
XCTAssertThrowsError(_ = try web3.verifyEip1559Block(chain: .London, parent: parent, current: current), "Should fail, got parent: \(parent.gasLimit), current: \(current.gasLimit)")
109+
}
93110
}
94-
95111
}
96112

113+
/// testCalcBaseFee assumes all blocks are 1559-blocks
97114
func testCalcBaseFee() throws {
98115

99116
}
@@ -103,56 +120,6 @@ class EIP1559Tests: XCTestCase {
103120
}
104121

105122
/*
106-
// TestBlockGasLimits tests the gasLimit checks for blocks both across
107-
// the EIP-1559 boundary and post-1559 blocks
108-
func TestBlockGasLimits(t *testing.T) {
109-
initial := new(big.Int).SetUint64(params.InitialBaseFee)
110-
111-
for i, tc := range []struct {
112-
pGasLimit uint64
113-
pNum int64
114-
gasLimit uint64
115-
ok bool
116-
}{
117-
// Transitions from non-london to london
118-
{10000000, 4, 20000000, true}, // No change
119-
{10000000, 4, 20019530, true}, // Upper limit
120-
{10000000, 4, 20019531, false}, // Upper +1
121-
{10000000, 4, 19980470, true}, // Lower limit
122-
{10000000, 4, 19980469, false}, // Lower limit -1
123-
// London to London
124-
{20000000, 5, 20000000, true},
125-
{20000000, 5, 20019530, true}, // Upper limit
126-
{20000000, 5, 20019531, false}, // Upper limit +1
127-
{20000000, 5, 19980470, true}, // Lower limit
128-
{20000000, 5, 19980469, false}, // Lower limit -1
129-
{40000000, 5, 40039061, true}, // Upper limit
130-
{40000000, 5, 40039062, false}, // Upper limit +1
131-
{40000000, 5, 39960939, true}, // lower limit
132-
{40000000, 5, 39960938, false}, // Lower limit -1
133-
} {
134-
parent := &types.Header{
135-
GasUsed: tc.pGasLimit / 2,
136-
GasLimit: tc.pGasLimit,
137-
BaseFee: initial,
138-
Number: big.NewInt(tc.pNum),
139-
}
140-
header := &types.Header{
141-
GasUsed: tc.gasLimit / 2,
142-
GasLimit: tc.gasLimit,
143-
BaseFee: initial,
144-
Number: big.NewInt(tc.pNum + 1),
145-
}
146-
err := VerifyEip1559Header(config(), parent, header)
147-
if tc.ok && err != nil {
148-
t.Errorf("test %d: Expected valid header: %s", i, err)
149-
}
150-
if !tc.ok && err == nil {
151-
t.Errorf("test %d: Expected invalid header", i)
152-
}
153-
}
154-
}
155-
156123
// TestCalcBaseFee assumes all blocks are 1559-blocks
157124
func TestCalcBaseFee(t *testing.T) {
158125
tests := []struct {

0 commit comments

Comments
 (0)