@@ -5,39 +5,38 @@ import BigInt
5
5
import web3swift
6
6
7
7
class EIP1559Tests : XCTestCase {
8
+ let web3 = Web3 ( )
9
+
10
+ let uselessBlockPart = (
11
+ number: BigUInt ( 12_965_000 ) ,
12
+ hash: Data ( from: " 0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46 " ) !, // "hash":
13
+ parentHash: Data ( from: " 0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88 " ) !, // "parentHash":
14
+ nonce: Data ( from: " 0xfb6e1a62d119228b " ) , // "nonce":
15
+ sha3Uncles: Data ( from: " 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347 " ) !, // "sha3Uncles":
16
+ receiptsRoot: Data ( from: " 0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347 " ) !, // "receiptsRoot":
17
+ logsBloom: EthereumBloomFilter ( Data ( from: " 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 " ) !) , // "logsBloom":
18
+ transactionsRoot: Data ( from: " 0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee " ) !, // "transactionsRoot":
19
+ stateRoot: Data ( from: " 0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb " ) !, // "stateRoot":
20
+ miner: EthereumAddress ( Data ( from: " 0x8888f1f195afa192cfee860698584c030f4c9db1 " ) !) !, // "miner":
21
+ difficulty: BigUInt ( 21345678965432 ) , // "difficulty":
22
+ totalDifficulty: BigUInt ( 324567845321 ) , // "totalDifficulty":
23
+ size: BigUInt ( 616 ) , // "size":
24
+ extraData: Data ( from: " 0x " ) !, // extraData":
25
+ gasLimit: BigUInt ( 3141592 ) , // "gasLimit":
26
+ gasUsed: BigUInt ( 21662 ) , // "gasUsed":
27
+ timestamp: Date ( ) , // "timestamp":
28
+ transactions: [ TransactionInBlock] ( ) , // "transactions":
29
+ uncles: [ Data] ( ) // "uncles":
30
+ )
8
31
9
32
/// testBlockGasLimits tests the gasLimit checks for blocks both across
10
33
/// the EIP-1559 boundary and post-1559 blocks
11
34
func testBlockGasLimits( ) throws {
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
- )
35
-
36
35
// [0] - parentGasLimit
37
36
// [1] - parentNumber
38
37
// [2] - currentGasLimit
39
38
// [3] - Should fail or not
40
- let headerArray : [ ( BigUInt , BigUInt , BigUInt , Bool ) ] = [
39
+ let headerArray : [ ( BigUInt , BigUInt , BigUInt , Bool ) ] = [
41
40
// Transitions from non-london to london
42
41
( 10_000_000 , 12_964_999 , 20_000_000 , true ) , // No change
43
42
( 10_000_000 , 12_964_999 , 20_019_530 , true ) , // Upper limit
@@ -55,9 +54,9 @@ class EIP1559Tests: XCTestCase {
55
54
( 40_000_000 , 12_965_000 , 40_039_062 , false ) , // Upper limit +1
56
55
( 40_000_000 , 12_965_000 , 39_960_939 , true ) , // lower limit
57
56
( 40_000_000 , 12_965_000 , 39_960_938 , false ) , // Lower limit -1
58
- ]
57
+ ]
59
58
60
- try headerArray. forEach { ( touple: ( parentGasLimit: BigUInt , parentNumber: BigUInt , currentGasLimit: BigUInt , isOk : Bool ) ) in
59
+ headerArray. forEach { ( touple: ( parentGasLimit: BigUInt , parentNumber: BigUInt , currentGasLimit: BigUInt , is1559 : Bool ) ) in
61
60
let parent = Block ( number: touple. parentNumber,
62
61
hash: uselessBlockPart. hash,
63
62
parentHash: uselessBlockPart. parentHash,
@@ -100,51 +99,55 @@ class EIP1559Tests: XCTestCase {
100
99
transactions: uselessBlockPart. transactions,
101
100
uncles: uselessBlockPart. uncles)
102
101
103
- let web3 = Web3 ( )
104
-
105
- if touple. isOk {
106
- XCTAssertTrue ( web3. verifyEip1559Block ( parent: parent, current: current) ,
102
+ if touple. is1559 {
103
+ XCTAssertTrue ( web3. isEip1559Block ( parent: parent, current: current) ,
107
104
" Shoult not fail, got parent: \( parent. gasLimit) , current: \( current. gasLimit) " )
108
105
} else {
109
- XCTAssertFalse ( web3. verifyEip1559Block ( parent: parent, current: current) ,
106
+ XCTAssertFalse ( web3. isEip1559Block ( parent: parent, current: current) ,
110
107
" Should fail, got parent: \( parent. gasLimit) , current: \( current. gasLimit) " )
111
108
}
112
109
}
113
110
}
114
111
115
112
/// testCalcBaseFee assumes all blocks are 1559-blocks
116
113
func testCalcBaseFee( ) throws {
114
+ // [0] - parentBaseFee
115
+ // [1] - parentNumber
116
+ // [2] - parentGasLimit
117
+ // [3] - parentGasUsed
118
+ // [4] - expectedBaseFee
119
+ let headerArray : [ ( BigUInt , BigUInt , BigUInt , BigUInt , BigUInt ) ] = [
120
+ ( Web3 . InitialBaseFee, 12_964_999 , 20000000 , 10000000 , Web3 . InitialBaseFee) , // parent is not London
121
+ ( Web3 . InitialBaseFee, 12_965_000 , 20000000 , 10000000 , Web3 . InitialBaseFee) , // current == target
122
+ ( Web3 . InitialBaseFee, 12_965_000 , 20000000 , 9000000 , 987500000 ) , // current below target
123
+ ( Web3 . InitialBaseFee, 12_965_000 , 20000000 , 11000000 , 1012500000 ) , // current above target
124
+ ]
125
+
126
+ headerArray. forEach { ( touple: ( parentBaseFee: BigUInt , parentNumber: BigUInt , parentGasLimit: BigUInt , parentGasUsed: BigUInt , expectedBaseFee: BigUInt ) ) in
127
+ let parent = Block ( number: touple. parentNumber,
128
+ hash: uselessBlockPart. hash,
129
+ parentHash: uselessBlockPart. parentHash,
130
+ nonce: uselessBlockPart. nonce,
131
+ sha3Uncles: uselessBlockPart. sha3Uncles,
132
+ logsBloom: uselessBlockPart. logsBloom,
133
+ transactionsRoot: uselessBlockPart. transactionsRoot,
134
+ stateRoot: uselessBlockPart. stateRoot,
135
+ receiptsRoot: uselessBlockPart. receiptsRoot,
136
+ miner: uselessBlockPart. miner,
137
+ difficulty: uselessBlockPart. difficulty,
138
+ totalDifficulty: uselessBlockPart. totalDifficulty,
139
+ extraData: uselessBlockPart. extraData,
140
+ size: uselessBlockPart. size,
141
+ gasLimit: touple. parentGasLimit,
142
+ gasUsed: touple. parentGasUsed,
143
+ baseFeePerGas: Web3 . InitialBaseFee,
144
+ timestamp: uselessBlockPart. timestamp,
145
+ transactions: uselessBlockPart. transactions,
146
+ uncles: uselessBlockPart. uncles)
117
147
118
- }
119
-
120
-
121
-
122
- }
148
+ let calculatedBaseFee = web3. calcBaseFee ( parent)
123
149
124
- /*
125
- // TestCalcBaseFee assumes all blocks are 1559-blocks
126
- func TestCalcBaseFee(t *testing.T) {
127
- tests := []struct {
128
- parentBaseFee int64
129
- parentGasLimit uint64
130
- parentGasUsed uint64
131
- expectedBaseFee int64
132
- }{
133
- {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target
134
- {params.InitialBaseFee, 20000000, 9000000, 987500000}, // usage below target
135
- {params.InitialBaseFee, 20000000, 11000000, 1012500000}, // usage above target
136
- }
137
- for i, test := range tests {
138
- parent := &types.Header{
139
- Number: common.Big32,
140
- GasLimit: test.parentGasLimit,
141
- GasUsed: test.parentGasUsed,
142
- BaseFee: big.NewInt(test.parentBaseFee),
143
- }
144
- if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
145
- t.Errorf("test %d: have %d want %d, ", i, have, want)
150
+ XCTAssertEqual ( calculatedBaseFee, touple. expectedBaseFee, " Base fee calculation fails: should be \( touple. expectedBaseFee) , got: \( calculatedBaseFee) " )
146
151
}
147
152
}
148
153
}
149
-
150
- */
0 commit comments