Skip to content

Commit c0bb890

Browse files
authored
Merge pull request #233 from Anthony4548/main
bsc testnet lightNode upgradle for bsc london fork
2 parents 7af1985 + 64feec2 commit c0bb890

File tree

2 files changed

+53
-39
lines changed

2 files changed

+53
-39
lines changed

lightclients/bsc/contracts/LightNode.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ contract LightNode is UUPSUpgradeable, Initializable, Pausable, ILightNode {
205205
}
206206
}
207207

208-
preBlockHash = Verify._getBlockHash(_blockHeaders[i]);
208+
preBlockHash = Verify._getBlockHash(_blockHeaders[i],chainId);
209209

210210
preBlockTime = _blockHeaders[i].timestamp;
211211

lightclients/bsc/contracts/lib/Verify.sol

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ library Verify {
2626

2727
uint256 internal constant TESTNET_LU_BAN_FORK_BLOCK = 29295050;
2828

29+
uint256 internal constant TESTNET_LONDON_FORK_BLOCK = 31103030;
30+
2931
uint256 internal constant MAINNET_LU_BAN_FORK_BLOCK = 29020050;
3032

33+
uint256 internal constant MAINNET_LONDON_FORK_BLOCK = 0;
34+
3135
uint256 internal constant MAINNET_CHAIN_ID = 56;
3236

3337
bytes32 constant SHA3_UNCLES =
@@ -54,6 +58,7 @@ library Verify {
5458
bytes extraData;
5559
bytes mixHash;
5660
bytes nonce;
61+
uint256 baseFeePerGas;
5762
}
5863

5964
struct ReceiptProof {
@@ -81,7 +86,7 @@ library Verify {
8186
uint256 _chainId
8287
) internal pure returns (bool) {
8388
(bytes memory signature, bytes memory extraData) = _splitExtra( _header.extraData);
84-
bytes32 hash = keccak256(_encodeSigHeader(_header, extraData, _chainId));
89+
bytes32 hash = _getSealHash(_header, extraData, _chainId);
8590

8691
bytes32 r;
8792
bytes32 s;
@@ -156,53 +161,54 @@ library Verify {
156161
return true;
157162
}
158163

159-
function _encodeSigHeader(
164+
function _getSealHash(
160165
BlockHeader memory _header,
161166
bytes memory _extraData,
162167
uint256 _chainId
163-
) internal pure returns (bytes memory output) {
168+
) internal pure returns (bytes32) {
164169
bytes[] memory list = new bytes[](16);
165170
list[0] = RLPEncode.encodeUint(_chainId);
166-
list[1] = RLPEncode.encodeBytes(_header.parentHash);
167-
list[2] = RLPEncode.encodeBytes(_header.sha3Uncles);
168-
list[3] = RLPEncode.encodeAddress(_header.miner);
169-
list[4] = RLPEncode.encodeBytes(_header.stateRoot);
170-
list[5] = RLPEncode.encodeBytes(_header.transactionsRoot);
171-
list[6] = RLPEncode.encodeBytes(_header.receiptsRoot);
172-
list[7] = RLPEncode.encodeBytes(_header.logsBloom);
173-
list[8] = RLPEncode.encodeUint(_header.difficulty);
174-
list[9] = RLPEncode.encodeUint(_header.number);
175-
list[10] = RLPEncode.encodeUint(_header.gasLimit);
176-
list[11] = RLPEncode.encodeUint(_header.gasUsed);
177-
list[12] = RLPEncode.encodeUint(_header.timestamp);
178-
list[13] = RLPEncode.encodeBytes(_extraData);
179-
list[14] = RLPEncode.encodeBytes(_header.mixHash);
180-
list[15] = RLPEncode.encodeBytes(_header.nonce);
181-
output = RLPEncode.encodeList(list);
171+
_headerToList(_header,_extraData,list,1);
172+
return keccak256(RLPEncode.encodeList(list));
173+
}
174+
175+
function _getBlockHash(BlockHeader memory _header,uint256 _chainId)
176+
internal
177+
pure
178+
returns (bytes32)
179+
{
180+
bytes[] memory list;
181+
if(_isAfterLondonFork(_chainId,_header.number)) {
182+
list = new bytes[](16);
183+
_headerToList(_header,_header.extraData,list,0);
184+
list[15] = RLPEncode.encodeUint(_header.baseFeePerGas);
185+
} else {
186+
list = new bytes[](15);
187+
_headerToList(_header,_header.extraData,list,0);
188+
}
189+
return keccak256(RLPEncode.encodeList(list));
182190
}
183191

184-
function _getBlockHash(BlockHeader memory _header)
192+
function _headerToList(BlockHeader memory _header,bytes memory _extraData,bytes[] memory _list,uint256 _start)
185193
internal
186194
pure
187-
returns (bytes32)
188195
{
189-
bytes[] memory list = new bytes[](15);
190-
list[0] = RLPEncode.encodeBytes(_header.parentHash);
191-
list[1] = RLPEncode.encodeBytes(_header.sha3Uncles);
192-
list[2] = RLPEncode.encodeAddress(_header.miner);
193-
list[3] = RLPEncode.encodeBytes(_header.stateRoot);
194-
list[4] = RLPEncode.encodeBytes(_header.transactionsRoot);
195-
list[5] = RLPEncode.encodeBytes(_header.receiptsRoot);
196-
list[6] = RLPEncode.encodeBytes(_header.logsBloom);
197-
list[7] = RLPEncode.encodeUint(_header.difficulty);
198-
list[8] = RLPEncode.encodeUint(_header.number);
199-
list[9] = RLPEncode.encodeUint(_header.gasLimit);
200-
list[10] = RLPEncode.encodeUint(_header.gasUsed);
201-
list[11] = RLPEncode.encodeUint(_header.timestamp);
202-
list[12] = RLPEncode.encodeBytes(_header.extraData);
203-
list[13] = RLPEncode.encodeBytes(_header.mixHash);
204-
list[14] = RLPEncode.encodeBytes(_header.nonce);
205-
return keccak256(RLPEncode.encodeList(list));
196+
_list[_start] = RLPEncode.encodeBytes(_header.parentHash);
197+
_list[++_start] = RLPEncode.encodeBytes(_header.sha3Uncles);
198+
_list[++_start] = RLPEncode.encodeAddress(_header.miner);
199+
_list[++_start] = RLPEncode.encodeBytes(_header.stateRoot);
200+
_list[++_start] = RLPEncode.encodeBytes(_header.transactionsRoot);
201+
_list[++_start] = RLPEncode.encodeBytes(_header.receiptsRoot);
202+
_list[++_start] = RLPEncode.encodeBytes(_header.logsBloom);
203+
_list[++_start] = RLPEncode.encodeUint(_header.difficulty);
204+
_list[++_start] = RLPEncode.encodeUint(_header.number);
205+
_list[++_start] = RLPEncode.encodeUint(_header.gasLimit);
206+
_list[++_start] = RLPEncode.encodeUint(_header.gasUsed);
207+
_list[++_start] = RLPEncode.encodeUint(_header.timestamp);
208+
_list[++_start] = RLPEncode.encodeBytes(_extraData);
209+
_list[++_start] = RLPEncode.encodeBytes(_header.mixHash);
210+
_list[++_start] = RLPEncode.encodeBytes(_header.nonce);
211+
206212
}
207213

208214
function _validateProof(
@@ -358,6 +364,14 @@ library Verify {
358364
}
359365
}
360366

367+
function _isAfterLondonFork(uint256 _chainId,uint256 _blockNum) internal pure returns(bool){
368+
if(_chainId == MAINNET_CHAIN_ID) {
369+
return MAINNET_LONDON_FORK_BLOCK > 0 && _blockNum >= MAINNET_LONDON_FORK_BLOCK;
370+
} else {
371+
return _blockNum >= TESTNET_LONDON_FORK_BLOCK;
372+
}
373+
}
374+
361375
function _containsValidator(
362376
bytes memory _validators,
363377
address _miner,

0 commit comments

Comments
 (0)