Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross chain testing scripts. #2033

Merged
merged 22 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 93 additions & 1 deletion contracts/config/networks.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"localGeth": {
"url": "http://127.0.0.1:17000",
StefanIliev545 marked this conversation as resolved.
Show resolved Hide resolved
"url": "http://127.0.0.1:25400",
"deploy": [
"deployment_scripts/core"
],
Expand Down Expand Up @@ -30,6 +30,22 @@
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f8"
]
},
"testObscuro": {
"chainId": 443,
"url": "http://127.0.0.1:11180/v1/",
"useGateway": true,
"companionNetworks" : {
"layer1" : "localGeth"
},
"deploy": [
"deployment_scripts/testing"
],
"accounts": [
"8dfb8083da6275ae3e4f41e3e8a8c19d028d32c9247e24530933782f2a05035b",
"6e384a07a01263518a09a5424c7b6bbfc3604ba7d93f47e3a455cbdd7f9f0682",
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f8"
]
},
"hardhat": {
"deploy": [
"deployment_scripts/core/layer1/",
Expand All @@ -47,5 +63,81 @@
"balance": "174165200000000000"
}
]
},
"uatGeth": {
"url": "http://uat-testnet-eth2network.uksouth.cloudapp.azure.com:8025",
"deploy": [
"deployment_scripts/core"
],
"accounts": [
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f1"
]
},
"testUatObscuro": {
"chainId": 443,
"url": "https://uat-testnet.ten.xyz/v1/",
"useGateway": true,
"companionNetworks" : {
"layer1" : "uatGeth"
},
"deploy": [
"deployment_scripts/testing"
],
"accounts": [
"8dfb8083da6275ae3e4f41e3e8a8c19d028d32c9247e24530933782f2a05035b",
"6e384a07a01263518a09a5424c7b6bbfc3604ba7d93f47e3a455cbdd7f9f0682",
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f8"
]
},
"devGeth": {
"url": "http://dev-testnet-eth2network.uksouth.cloudapp.azure.com:8025",
"deploy": [
"deployment_scripts/core"
],
"accounts": [
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f1"
]
},
"testDevObscuro": {
"chainId": 443,
"url": "https://dev-testnet.ten.xyz/v1/",
"useGateway": true,
"companionNetworks" : {
"layer1" : "uatGeth"
},
"deploy": [
"deployment_scripts/testing"
],
"accounts": [
"8dfb8083da6275ae3e4f41e3e8a8c19d028d32c9247e24530933782f2a05035b",
"6e384a07a01263518a09a5424c7b6bbfc3604ba7d93f47e3a455cbdd7f9f0682",
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f8"
]
},
"localTestnetGeth": {
"url": "http://127.0.0.1:8025",
"deploy": [
"deployment_scripts/core"
],
"accounts": [
"f52e5418e349dccdda29b6ac8b0abe6576bb7713886aa85abea6181ba731f9bb",
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f1"
]
},
"localTestnetTen": {
"chainId": 443,
"url": "http://127.0.0.1:3000/v1/",
"useGateway": true,
"companionNetworks" : {
"layer1" : "localTestnetGeth"
},
"deploy": [
"deployment_scripts/testing"
],
"accounts": [
"8dfb8083da6275ae3e4f41e3e8a8c19d028d32c9247e24530933782f2a05035b",
"6e384a07a01263518a09a5424c7b6bbfc3604ba7d93f47e3a455cbdd7f9f0682",
"4bfe14725e685901c062ccd4e220c61cf9c189897b6c78bd18d7f51291b2b8f8"
]
}
}
2 changes: 1 addition & 1 deletion contracts/deployment_scripts/bridge/001_deploy_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {DeployFunction} from 'hardhat-deploy/types';
*/

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {
const {
deployments,
getNamedAccounts
} = hre;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Receipt } from 'hardhat-deploy/dist/types';


const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
return;
StefanIliev545 marked this conversation as resolved.
Show resolved Hide resolved
const l2Network = hre;
const {deployer} = await hre.getNamedAccounts();

Expand All @@ -16,7 +17,6 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const gasConsumerBalance = await hre.ethers.getContractAt("GasConsumerBalance", gcb.address)
const gasEstimation = await gasConsumerBalance.getFunction('get_balance').estimateGas({
from: deployer,
gasPrice: 2,
});

await hre.deployments.execute("GasConsumerBalance", {
Expand Down
36 changes: 36 additions & 0 deletions contracts/deployment_scripts/testing/002_access_list_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {HardhatRuntimeEnvironment} from 'hardhat/types';
import {DeployFunction} from 'hardhat-deploy/types';
import { Receipt } from 'hardhat-deploy/dist/types';


const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
return;
const l2Network = hre;
const {deployer} = await hre.getNamedAccounts();

const gcb = await l2Network.deployments.deploy("GasConsumerBalance", {
from: deployer,
log: true
})

const signer = await hre.ethers.getSigner(deployer);

const gasConsumerBalance = await hre.ethers.getContractAt("GasConsumerBalance", gcb.address)
const gasConsumer = await gasConsumerBalance.connect(signer)


const tx = await gasConsumer.getFunction("resetOwner").populateTransaction(deployer);
tx.accessList = [
{
address: gcb.address,
storageKeys: []
},
];
const resp = await signer.sendTransaction(tx);
const receipt = await resp.wait();
console.log(`Receipt.Status=${receipt.status}`);
};


export default func;
func.tags = ['GasDebug'];
119 changes: 119 additions & 0 deletions contracts/deployment_scripts/testing/003_simple_withdrawal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {EthereumProvider, HardhatRuntimeEnvironment} from 'hardhat/types';
import {DeployFunction} from 'hardhat-deploy/types';
import { Receipt } from 'hardhat-deploy/dist/types';
import { StandardMerkleTree } from "@openzeppelin/merkle-tree";
import { keccak256 } from 'ethers';
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider';

function process_value_transfer(ethers, value_transfer) {
const abiTypes = ['address', 'address', 'uint256', 'uint64'];
const msg = [
value_transfer['args'].sender, value_transfer['args'].receiver,
value_transfer['args'].amount.toString(), value_transfer['args'].sequence.toString()
]

const abiCoder = ethers.AbiCoder.defaultAbiCoder();
const encodedMsg = abiCoder.encode(abiTypes, msg);
return [msg, ethers.keccak256(encodedMsg)];
}


function decode_base64(base64String) {
let jsonString = atob(base64String);
return JSON.parse(jsonString);
}


async function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function waitForRootPublished(management, msg, proof, root, provider: EthereumProvider, interval = 5000, timeout = 120000) {
var gas_estimate = null
const l1Ethers = new HardhatEthersProvider(provider, "layer1")

const startTime = Date.now();
while (gas_estimate === null) {
try {
console.log(`Extracting native value from cross chain message for root ${root}`)
const tx = await management.getFunction('ExtractNativeValue').populateTransaction(msg, proof, root, {} )
console.log(`Tx to = ${tx.to}`)
gas_estimate = await l1Ethers.estimateGas(tx)
} catch (error) {
console.log(`Estimate gas threw error : ${error}`)
}
if (Date.now() - startTime >= timeout) {
console.log(`Timed out waiting for the estimate gas to return`)
break
}
await sleep(interval)
}
console.log(`Estimation took ${Date.now() - startTime} ms`)
return gas_estimate
}


const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const l2Network = hre;
const {deployer} = await hre.getNamedAccounts();

var mbusBase = await hre.ethers.getContractAt("MessageBus", "0x526c84529b2b8c11f57d93d3f5537aca3aecef9b");
const mbus = mbusBase.connect(await hre.ethers.provider.getSigner(deployer));
const tx = await mbus.getFunction("sendValueToL2").send(deployer, 1000, { value: 1000});
const receipt = await tx.wait()
console.log(`003_simple_withdrawal: Cross Chain send receipt status = ${receipt.status}`);

const block = await hre.ethers.provider.send('eth_getBlockByHash', [receipt.blockHash, true]);
console.log(`Block received: ${block.number}`)


const value_transfer = mbus.interface.parseLog(receipt.logs[0]);
const _processed_value_transfer = process_value_transfer(hre.ethers, value_transfer)
const msg = _processed_value_transfer[0]
const msgHash = _processed_value_transfer[1]
const decoded = decode_base64(block.crossChainTree)

console.log(` Sender: ${value_transfer['args'].sender}`)
console.log(` Receiver: ${value_transfer['args'].receiver}`)
console.log(` Amount: ${value_transfer['args'].amount}`)
console.log(` Sequence: ${value_transfer['args'].sequence}`)
console.log(` VTrans Hash: ${msgHash}`)
console.log(` XChain tree: ${decoded}`)

if (decoded[0][1] != msgHash) {
console.error('Value transfer hash is not in the xchain tree!');
return;
}

const tree = StandardMerkleTree.of(decoded, ["string", "bytes32"]);
const proof = tree.getProof(['v',msgHash])
console.log(` Merkle root: ${tree.root}`)
console.log(` Merkle proof: ${JSON.stringify(proof, null,2)}`)

if (block.crossChainTreeHash != tree.root) {
console.error('Constructed merkle root matches block crossChainTreeHash');
return
}


//const networkConfig : any = await hre.network.provider.request({method: 'net_config'});
const mgmtContractAddress = "0x946600AF6893Ee818CC7CC2dEC4D0A0bF91C9817" // networkConfig.ManagementContractAddress;
const messageBusAddress = "0x68e95924f22Be35386A8aE0240f8885967d452D6" //networkConfig.MessageBusAddress;

const l1Accounts = await hre.companionNetworks.layer1.getNamedAccounts()
const fundTx = await hre.companionNetworks.layer1.deployments.rawTx({
from: l1Accounts.deployer,
to: messageBusAddress,
value: "1000",
})
console.log(`Message bus funding status = ${fundTx.status}`)

var managementContract = await hre.ethers.getContractAt("ManagementContract", mgmtContractAddress);
const estimation = await waitForRootPublished(managementContract, msg, proof, tree.root, hre.companionNetworks.layer1.provider)
console.log(`Estimation for native value extraction = ${estimation}`)
};


export default func;
func.tags = ['GasDebug'];
25 changes: 23 additions & 2 deletions contracts/generated/GasConsumerBalance/GasConsumerBalance.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
"dependencies": {
"@openzeppelin/contracts": "^5.0.1",
"@openzeppelin/contracts-upgradeable": "^5.0.1",
"@openzeppelin/merkle-tree": "^1.0.7",
"ethers": "^6.6.0",
"hardhat-ignore-warnings": "^0.2.6",
"ten-hardhat-plugin": "^0.0.9"
},
"peerDependencies": {
"@nomicfoundation/hardhat-verify" : "2.0.8",
"@nomicfoundation/hardhat-ethers":"3.0.6"
"@nomicfoundation/hardhat-ethers":"3.0.5"
}
}
4 changes: 4 additions & 0 deletions contracts/src/testing/GasConsumerBalance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ contract GasConsumerBalance {
require(msg.sender == owner, "You are not the owner");
selfdestruct(payable(address(this)));
}

function resetOwner(address _owner) public {
owner = _owner;
}
}
3 changes: 3 additions & 0 deletions go/common/gethencoding/geth_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
callFieldGasPrice = "gasprice"
callFieldMaxFeePerGas = "maxfeepergas"
callFieldMaxPriorityFeePerGas = "maxpriorityfeepergas"
callFieldAccessList = "accesslist"
)

// EncodingService handles conversion to Geth data structures
Expand Down Expand Up @@ -235,6 +236,8 @@ func ExtractEthCall(param interface{}) (*gethapi.TransactionArgs, error) {
return nil, fmt.Errorf("could not decode value in CallMsg - %w", err)
}
maxPriorityFeePerGas = (*hexutil.Big)(maxPriorityFeePerGasVal)
case callFieldAccessList:
// ignore access list for now
}
}

Expand Down
Loading
Loading