Skip to content

Commit

Permalink
fix(test): making tests type-safe (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerLamTd authored Dec 7, 2022
1 parent 608e3e3 commit 66ec7cc
Show file tree
Hide file tree
Showing 9 changed files with 655 additions and 447 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ library LibBridgeProcess {
uint256 gasLimit = msg.sender == message.owner
? gasleft()
: message.gasLimit;

bool success = LibBridgeInvoke.invokeMessageCall({
state: state,
message: message,
Expand Down
65 changes: 65 additions & 0 deletions packages/protocol/tasks/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as fs from "fs"
import * as log from "./log"
import { Block, BlockHeader, EthGetProofResponse } from "../test/utils/rpc"
import RLP from "rlp"

async function deployContract(
hre: any,
Expand Down Expand Up @@ -84,6 +86,67 @@ const MessageStatus = {
FAILED: 3,
}

async function getLatestBlockHeader(hre: any) {
const block: Block = await hre.ethers.provider.send(
"eth_getBlockByNumber",
["latest", false]
)

const logsBloom = block.logsBloom.toString().substring(2)

const blockHeader: BlockHeader = {
parentHash: block.parentHash,
ommersHash: block.sha3Uncles,
beneficiary: block.miner,
stateRoot: block.stateRoot,
transactionsRoot: block.transactionsRoot,
receiptsRoot: block.receiptsRoot,
logsBloom: logsBloom.match(/.{1,64}/g)!.map((s: string) => "0x" + s),
difficulty: block.difficulty,
height: block.number,
gasLimit: block.gasLimit,
gasUsed: block.gasUsed,
timestamp: block.timestamp,
extraData: block.extraData,
mixHash: block.mixHash,
nonce: block.nonce,
baseFeePerGas: block.baseFeePerGas ? parseInt(block.baseFeePerGas) : 0,
}

return { block, blockHeader }
}

async function getSignalProof(
hre: any,
contractAddress: string,
key: string,
blockNumber: number,
blockHeader: BlockHeader
) {
const proof: EthGetProofResponse = await hre.ethers.provider.send(
"eth_getProof",
[contractAddress, [key], blockNumber]
)

// RLP encode the proof together for LibTrieProof to decode
const encodedProof = hre.ethers.utils.defaultAbiCoder.encode(
["bytes", "bytes"],
[
RLP.encode(proof.accountProof),
RLP.encode(proof.storageProof[0].proof),
]
)
// encode the SignalProof struct from LibBridgeSignal
const signalProof = hre.ethers.utils.defaultAbiCoder.encode(
[
"tuple(tuple(bytes32 parentHash, bytes32 ommersHash, address beneficiary, bytes32 stateRoot, bytes32 transactionsRoot, bytes32 receiptsRoot, bytes32[8] logsBloom, uint256 difficulty, uint128 height, uint64 gasLimit, uint64 gasUsed, uint64 timestamp, bytes extraData, bytes32 mixHash, uint64 nonce, uint256 baseFeePerGas) header, bytes proof)",
],
[{ header: blockHeader, proof: encodedProof }]
)

return signalProof
}

export {
deployContract,
getDeployer,
Expand All @@ -94,4 +157,6 @@ export {
getSlot,
decode,
MessageStatus,
getLatestBlockHeader,
getSignalProof,
}
Loading

0 comments on commit 66ec7cc

Please sign in to comment.