mirror of
https://github.com/0glabs/0g-storage-node.git
synced 2024-12-24 15:25:17 +00:00
Migrate to the new contract interfaces (#29)
* Fix compile warning & Search contract by name automatically Migrate to the new contract interfaces * Fix compile * Fix lint
This commit is contained in:
parent
2ed6bca446
commit
306c43c9dc
@ -1 +1 @@
|
|||||||
Subproject commit 951c02992e2a03eb47e41eb840392ec824639640
|
Subproject commit 1f3f759236b48dd8682deba91011292cb5966f4c
|
@ -29,4 +29,7 @@ resolver = "2"
|
|||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
discv5 = { path = "version-meld/discv5" }
|
discv5 = { path = "version-meld/discv5" }
|
||||||
eth2_ssz = { path = "version-meld/eth2_ssz" }
|
eth2_ssz = { path = "version-meld/eth2_ssz" }
|
||||||
enr = { path = "version-meld/enr" }
|
enr = { path = "version-meld/enr" }
|
||||||
|
|
||||||
|
[profile.bench.package.'storage']
|
||||||
|
debug = true
|
@ -1,30 +1,35 @@
|
|||||||
// use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
// const INSTALL_ERROR_MESSAGE: &str =
|
const INSTALL_ERROR_MESSAGE: &str =
|
||||||
// "Install dependencies for contract fail, try to run `yarn` in folder '0g-storage-contracts'";
|
"Install dependencies for contract fail, try to run `yarn` in folder '0g-storage-contracts'";
|
||||||
// const COMPILE_ERROR_MESSAGE: &str =
|
const COMPILE_ERROR_MESSAGE: &str =
|
||||||
// "Compile solidity contracts fail, try to run `yarn compile` in folder '0g-storage-contracts'";
|
"Compile solidity contracts fail, try to run `yarn compile` in folder '0g-storage-contracts'";
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn compile_contracts() {
|
||||||
|
println!("cargo:rerun-if-changed=../../0g-storage-contracts/contracts/");
|
||||||
|
println!("cargo:rerun-if-changed=../../0g-storage-contracts/hardhat.config.ts");
|
||||||
|
|
||||||
|
let output = Command::new("yarn")
|
||||||
|
.arg("--cwd")
|
||||||
|
.arg("../../0g-storage-contracts")
|
||||||
|
.status()
|
||||||
|
.expect(INSTALL_ERROR_MESSAGE);
|
||||||
|
assert!(output.success(), "{}", INSTALL_ERROR_MESSAGE);
|
||||||
|
|
||||||
|
let output = Command::new("yarn")
|
||||||
|
.arg("--cwd")
|
||||||
|
.arg("../../0g-storage-contracts")
|
||||||
|
.arg("compile")
|
||||||
|
.status()
|
||||||
|
.expect(COMPILE_ERROR_MESSAGE);
|
||||||
|
assert!(output.success(), "{}", COMPILE_ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// if cfg!(feature = "compile-contracts") {
|
if cfg!(feature = "compile-contracts") {
|
||||||
// println!("cargo:rerun-if-changed=../../0g-storage-contracts/contracts/");
|
// compile_contracts();
|
||||||
// println!("cargo:rerun-if-changed=../../0g-storage-contracts/hardhat.config.ts");
|
// return;
|
||||||
|
}
|
||||||
// let output = Command::new("yarn")
|
println!("cargo:rerun-if-changed=../../0g-storage-contracts/artifacts/");
|
||||||
// .arg("--cwd")
|
|
||||||
// .arg("../../0g-storage-contracts")
|
|
||||||
// .status()
|
|
||||||
// .expect(INSTALL_ERROR_MESSAGE);
|
|
||||||
// assert!(output.success(), "{}", INSTALL_ERROR_MESSAGE);
|
|
||||||
|
|
||||||
// let output = Command::new("yarn")
|
|
||||||
// .arg("--cwd")
|
|
||||||
// .arg("../../0g-storage-contracts")
|
|
||||||
// .arg("compile")
|
|
||||||
// .status()
|
|
||||||
// .expect(COMPILE_ERROR_MESSAGE);
|
|
||||||
// assert!(output.success(), "{}", COMPILE_ERROR_MESSAGE);
|
|
||||||
// } else {
|
|
||||||
// println!("cargo:rerun-if-changed=../../0g-storage-contracts/artifacts/");
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,4 @@ criterion = "0.4"
|
|||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "benchmark"
|
name = "benchmark"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[profile.bench]
|
|
||||||
debug = true
|
|
@ -3,8 +3,9 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
import rlp
|
||||||
|
|
||||||
from eth_utils import decode_hex
|
from eth_utils import decode_hex, keccak
|
||||||
from web3 import Web3, HTTPProvider
|
from web3 import Web3, HTTPProvider
|
||||||
from web3.middleware import construct_sign_and_send_raw_middleware
|
from web3.middleware import construct_sign_and_send_raw_middleware
|
||||||
from enum import Enum, unique
|
from enum import Enum, unique
|
||||||
@ -22,6 +23,7 @@ from utility.utils import (
|
|||||||
initialize_config,
|
initialize_config,
|
||||||
wait_until,
|
wait_until,
|
||||||
)
|
)
|
||||||
|
from test_framework.contracts import load_contract_metadata
|
||||||
|
|
||||||
|
|
||||||
@unique
|
@unique
|
||||||
@ -218,15 +220,11 @@ class BlockchainNode(TestNode):
|
|||||||
binary,
|
binary,
|
||||||
local_conf,
|
local_conf,
|
||||||
contract_path,
|
contract_path,
|
||||||
token_contract_path,
|
|
||||||
mine_contract_path,
|
|
||||||
log,
|
log,
|
||||||
blockchain_node_type,
|
blockchain_node_type,
|
||||||
rpc_timeout=10,
|
rpc_timeout=10,
|
||||||
):
|
):
|
||||||
self.contract_path = contract_path
|
self.contract_path = contract_path
|
||||||
self.token_contract_path = token_contract_path
|
|
||||||
self.mine_contract_path = mine_contract_path
|
|
||||||
|
|
||||||
self.blockchain_node_type = blockchain_node_type
|
self.blockchain_node_type = blockchain_node_type
|
||||||
|
|
||||||
@ -261,14 +259,15 @@ class BlockchainNode(TestNode):
|
|||||||
# account = w3.eth.account.from_key(GENESIS_PRIV_KEY1)
|
# account = w3.eth.account.from_key(GENESIS_PRIV_KEY1)
|
||||||
# w3.middleware_onion.add(construct_sign_and_send_raw_middleware(account))
|
# w3.middleware_onion.add(construct_sign_and_send_raw_middleware(account))
|
||||||
|
|
||||||
def deploy_contract(path, args=None):
|
def deploy_contract(name, args=None):
|
||||||
if args is None:
|
if args is None:
|
||||||
args = []
|
args = []
|
||||||
contract_interface = json.load(open(path, "r"))
|
contract_interface = load_contract_metadata(base_path=self.contract_path, name=name)
|
||||||
contract = w3.eth.contract(
|
contract = w3.eth.contract(
|
||||||
abi=contract_interface["abi"],
|
abi=contract_interface["abi"],
|
||||||
bytecode=contract_interface["bytecode"],
|
bytecode=contract_interface["bytecode"],
|
||||||
)
|
)
|
||||||
|
|
||||||
tx_params = TX_PARAMS.copy()
|
tx_params = TX_PARAMS.copy()
|
||||||
del tx_params["gas"]
|
del tx_params["gas"]
|
||||||
tx_hash = contract.constructor(*args).transact(tx_params)
|
tx_hash = contract.constructor(*args).transact(tx_params)
|
||||||
@ -278,38 +277,36 @@ class BlockchainNode(TestNode):
|
|||||||
abi=contract_interface["abi"],
|
abi=contract_interface["abi"],
|
||||||
)
|
)
|
||||||
return contract, tx_hash
|
return contract, tx_hash
|
||||||
|
|
||||||
|
def predict_contract_address(offset):
|
||||||
|
nonce = w3.eth.get_transaction_count(account1.address)
|
||||||
|
rlp_encoded = rlp.encode([decode_hex(account1.address), nonce + offset])
|
||||||
|
contract_address_bytes = keccak(rlp_encoded)[-20:]
|
||||||
|
contract_address = '0x' + contract_address_bytes.hex()
|
||||||
|
return Web3.to_checksum_address(contract_address)
|
||||||
|
|
||||||
|
|
||||||
|
flowAddress = predict_contract_address(1)
|
||||||
|
mineAddress = predict_contract_address(2)
|
||||||
|
|
||||||
|
ZERO = "0x0000000000000000000000000000000000000000"
|
||||||
|
|
||||||
self.log.debug("Start deploy contracts")
|
self.log.debug("Start deploy contracts")
|
||||||
token_contract, _ = deploy_contract(self.token_contract_path)
|
book, _ = deploy_contract("AddressBook", [flowAddress, ZERO, ZERO, mineAddress]);
|
||||||
self.log.debug("ERC20 deployed")
|
self.log.debug("AddressBook deployed")
|
||||||
|
|
||||||
flow_contract, flow_contract_hash = deploy_contract(
|
flow_contract, flow_contract_hash = deploy_contract(
|
||||||
self.contract_path, ["0x0000000000000000000000000000000000000000", 100, 0]
|
"Flow", [book.address, 100, 0]
|
||||||
)
|
)
|
||||||
self.log.debug("Flow deployed")
|
self.log.debug("Flow deployed")
|
||||||
|
|
||||||
mine_contract, _ = deploy_contract(
|
mine_contract, _ = deploy_contract(
|
||||||
self.mine_contract_path,
|
"PoraMineTest",
|
||||||
[flow_contract.address, "0x0000000000000000000000000000000000000000", 7],
|
[book.address, 3],
|
||||||
)
|
)
|
||||||
self.log.debug("Mine deployed")
|
self.log.debug("Mine deployed")
|
||||||
self.log.info("All contracts deployed")
|
self.log.info("All contracts deployed")
|
||||||
|
|
||||||
tx_hash = token_contract.functions.approve(
|
|
||||||
flow_contract.address, int(1e9)
|
|
||||||
).transact(TX_PARAMS)
|
|
||||||
self.wait_for_transaction_receipt(w3, tx_hash)
|
|
||||||
|
|
||||||
# setup second account
|
|
||||||
amount = int(1e8)
|
|
||||||
tx_hash = token_contract.functions.transfer(account2.address, amount).transact(
|
|
||||||
TX_PARAMS
|
|
||||||
)
|
|
||||||
self.wait_for_transaction_receipt(w3, tx_hash)
|
|
||||||
|
|
||||||
tx_hash = token_contract.functions.approve(
|
|
||||||
flow_contract.address, amount
|
|
||||||
).transact(TX_PARAMS1)
|
|
||||||
self.wait_for_transaction_receipt(w3, tx_hash)
|
|
||||||
|
|
||||||
tx_hash = mine_contract.functions.setMiner(decode_hex(MINER_ID)).transact(TX_PARAMS)
|
tx_hash = mine_contract.functions.setMiner(decode_hex(MINER_ID)).transact(TX_PARAMS)
|
||||||
self.wait_for_transaction_receipt(w3, tx_hash)
|
self.wait_for_transaction_receipt(w3, tx_hash)
|
||||||
|
|
||||||
@ -324,7 +321,7 @@ class BlockchainNode(TestNode):
|
|||||||
construct_sign_and_send_raw_middleware([account1, account2])
|
construct_sign_and_send_raw_middleware([account1, account2])
|
||||||
)
|
)
|
||||||
|
|
||||||
contract_interface = json.load(open(self.contract_path, "r"))
|
contract_interface = load_contract_metadata(self.contract_path, "Flow")
|
||||||
return w3.eth.contract(address=contract_address, abi=contract_interface["abi"])
|
return w3.eth.contract(address=contract_address, abi=contract_interface["abi"])
|
||||||
|
|
||||||
def wait_for_transaction(self, tx_hash):
|
def wait_for_transaction(self, tx_hash):
|
||||||
|
@ -26,8 +26,6 @@ class BSCNode(BlockchainNode):
|
|||||||
binary,
|
binary,
|
||||||
updated_config,
|
updated_config,
|
||||||
contract_path,
|
contract_path,
|
||||||
token_contract_path,
|
|
||||||
mine_contract_path,
|
|
||||||
log,
|
log,
|
||||||
rpc_timeout=10,
|
rpc_timeout=10,
|
||||||
):
|
):
|
||||||
@ -71,8 +69,6 @@ class BSCNode(BlockchainNode):
|
|||||||
binary,
|
binary,
|
||||||
local_conf,
|
local_conf,
|
||||||
contract_path,
|
contract_path,
|
||||||
token_contract_path,
|
|
||||||
mine_contract_path,
|
|
||||||
log,
|
log,
|
||||||
BlockChainNodeType.BSC,
|
BlockChainNodeType.BSC,
|
||||||
rpc_timeout,
|
rpc_timeout,
|
||||||
|
@ -29,8 +29,6 @@ class ConfluxNode(BlockchainNode):
|
|||||||
binary,
|
binary,
|
||||||
updated_config,
|
updated_config,
|
||||||
contract_path,
|
contract_path,
|
||||||
token_contract_path,
|
|
||||||
mine_contract_path,
|
|
||||||
log,
|
log,
|
||||||
rpc_timeout=10,
|
rpc_timeout=10,
|
||||||
):
|
):
|
||||||
@ -76,8 +74,6 @@ class ConfluxNode(BlockchainNode):
|
|||||||
binary,
|
binary,
|
||||||
local_conf,
|
local_conf,
|
||||||
contract_path,
|
contract_path,
|
||||||
token_contract_path,
|
|
||||||
mine_contract_path,
|
|
||||||
log,
|
log,
|
||||||
BlockChainNodeType.Conflux,
|
BlockChainNodeType.Conflux,
|
||||||
rpc_timeout,
|
rpc_timeout,
|
||||||
|
14
tests/test_framework/contracts.py
Normal file
14
tests/test_framework/contracts.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from os.path import join
|
||||||
|
from pathlib import Path
|
||||||
|
import json
|
||||||
|
from web3 import Web3
|
||||||
|
|
||||||
|
|
||||||
|
def load_contract_metadata(base_path: str, name: str):
|
||||||
|
path = Path(join(base_path, "artifacts"))
|
||||||
|
try:
|
||||||
|
found_file = next(path.rglob(f"{name}.json"))
|
||||||
|
return json.loads(open(found_file, "r").read())
|
||||||
|
except StopIteration:
|
||||||
|
raise Exception(f"Cannot found contract {name}'s metadata")
|
||||||
|
|
@ -74,8 +74,6 @@ class TestFramework:
|
|||||||
self.blockchain_binary,
|
self.blockchain_binary,
|
||||||
updated_config,
|
updated_config,
|
||||||
self.contract_path,
|
self.contract_path,
|
||||||
self.token_contract_path,
|
|
||||||
self.mine_contract_path,
|
|
||||||
self.log,
|
self.log,
|
||||||
60,
|
60,
|
||||||
)
|
)
|
||||||
@ -86,8 +84,6 @@ class TestFramework:
|
|||||||
self.blockchain_binary,
|
self.blockchain_binary,
|
||||||
updated_config,
|
updated_config,
|
||||||
self.contract_path,
|
self.contract_path,
|
||||||
self.token_contract_path,
|
|
||||||
self.mine_contract_path,
|
|
||||||
self.log,
|
self.log,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@ -212,27 +208,7 @@ class TestFramework:
|
|||||||
dest="contract",
|
dest="contract",
|
||||||
default=os.path.join(
|
default=os.path.join(
|
||||||
__file_path__,
|
__file_path__,
|
||||||
"../../0g-storage-contracts/artifacts/contracts/dataFlow/Flow.sol/Flow.json",
|
"../../0g-storage-contracts/",
|
||||||
),
|
|
||||||
type=str,
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--token-contract-path",
|
|
||||||
dest="token_contract",
|
|
||||||
default=os.path.join(
|
|
||||||
__file_path__,
|
|
||||||
"../config/MockToken.json",
|
|
||||||
),
|
|
||||||
type=str,
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--mine-contract-path",
|
|
||||||
dest="mine_contract",
|
|
||||||
default=os.path.join(
|
|
||||||
__file_path__,
|
|
||||||
"../../0g-storage-contracts/artifacts/contracts/test/PoraMineTest.sol/PoraMineTest.json",
|
|
||||||
),
|
),
|
||||||
type=str,
|
type=str,
|
||||||
)
|
)
|
||||||
@ -414,18 +390,10 @@ class TestFramework:
|
|||||||
self.zgs_binary = os.path.abspath(self.options.zerog_storage)
|
self.zgs_binary = os.path.abspath(self.options.zerog_storage)
|
||||||
self.cli_binary = os.path.abspath(self.options.cli)
|
self.cli_binary = os.path.abspath(self.options.cli)
|
||||||
self.contract_path = os.path.abspath(self.options.contract)
|
self.contract_path = os.path.abspath(self.options.contract)
|
||||||
self.token_contract_path = os.path.abspath(self.options.token_contract)
|
|
||||||
self.mine_contract_path = os.path.abspath(self.options.mine_contract)
|
|
||||||
|
|
||||||
assert os.path.exists(self.contract_path), (
|
assert os.path.exists(self.contract_path), (
|
||||||
"%s should be exist" % self.contract_path
|
"%s should be exist" % self.contract_path
|
||||||
)
|
)
|
||||||
assert os.path.exists(self.token_contract_path), (
|
|
||||||
"%s should be exist" % self.token_contract_path
|
|
||||||
)
|
|
||||||
assert os.path.exists(self.mine_contract_path), (
|
|
||||||
"%s should be exist" % self.mine_contract_path
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.options.random_seed is not None:
|
if self.options.random_seed is not None:
|
||||||
random.seed(self.options.random_seed)
|
random.seed(self.options.random_seed)
|
||||||
|
Loading…
Reference in New Issue
Block a user