Skip to content

Commit

Permalink
update caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
3ierratango committed Apr 27, 2023
1 parent 193d3a9 commit 7be38fc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 47 deletions.
14 changes: 7 additions & 7 deletions alice_node_config.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"networks": {
"network_vec": [{
"url": "https://evm.shibuya.astar.network",
"gateway_contract_address": "337ee83622C8Eb69AE7F8C49431BF982e9dE9036",
"id": 81
"url": "https://rpc-mumbai.maticvigil.com/",
"gateway_contract_address": "c7BcC4c1f7bA7fF7A618a8B8e1A1c1007cBD5393",
"id": 80001
},
{
"url": "http://127.0.0.1:8545",
"gateway_contract_address": "92660CBfA6F120dD4343AfD4A74A030506dC9Acb",
"id": 26100
"url": "https://data-seed-prebsc-2-s3.binance.org:8545",
"gateway_contract_address": "c1F13fde5fFDE7B7ae6C95C9190d038A2eEb9e29",
"id": 97
}],
"pair_vec": [[81, 26100], [26100, 81]],
"pair_vec": [[80001, 97], [97, 80001]],
"signer_public_key": "020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1",
"role" : "QP_MINER"
}
Expand Down
4 changes: 2 additions & 2 deletions bob_node_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"networks": {
"network_vec": [{
"url": "https://rpc-mumbai.maticvigil.com/",
"gateway_contract_address": "fE174DC5FF85Ed8871e4f35d86f1BB32A8461A38",
"gateway_contract_address": "ee8F7BB1ED6CFE3f42C815e56bb3cd47D7393e71",
"id": 80001
},
{
"url": "http://127.0.0.1:8545",
"gateway_contract_address": "92660CBfA6F120dD4343AfD4A74A030506dC9Acb",
"gateway_contract_address": "660eB2A65026D18705C4B824Cdbfae2203EcAD81",
"id": 26100
}],
"pair_vec": [[80001, 26100], [26100, 80001]],
Expand Down
36 changes: 21 additions & 15 deletions pallets/quantum-portal/src/contract_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl ContractClient {
Ok(address)
}

pub fn get_miner_manager_address(&self) -> Result<H160, ChainRequestError> {
pub fn get_miner_manager_address(&self) -> Result<(H160, Vec<u8>, Vec<u8>), ChainRequestError> {
let ledger_manager_address = self.get_ledger_manager_address()?;

// no cache, we fetch from the gateway contract
Expand All @@ -112,18 +112,20 @@ impl ContractClient {
let address = ChainUtils::decode_address_response(res.result.as_slice());
log::info!("Miner manager address is : {:?}", address);

let signature = b"VERSION";
let signature = b"VERSION()";
let res: Box<CallResponse> = self.call(signature, &[], Some(address))?;
log::info!("Miner manager version is : {:?}", &res.result.as_slice());
let version = &res.result.as_slice();
log::info!("Miner manager version is : {:?}", version);

let signature = b"NAME";
let signature = b"NAME()";
let res: Box<CallResponse> = self.call(signature, &[], Some(address))?;
log::info!("Miner manager name is : {:?}", &res.result.as_slice());
let name = &res.result.as_slice();
log::info!("Miner manager name is : {:?}", name);

Ok(address)
Ok((address, version.to_vec(), name.to_vec()))
}

pub fn get_authority_manager_address(&self) -> Result<H160, ChainRequestError> {
pub fn get_authority_manager_address(&self) -> Result<(H160, Vec<u8>, Vec<u8>), ChainRequestError> {
let ledger_manager_address = self.get_ledger_manager_address()?;

// no cache, we fetch from the gateway contract
Expand All @@ -133,18 +135,20 @@ impl ContractClient {
let address = ChainUtils::decode_address_response(res.result.as_slice());
log::info!("Authority manager address is : {:?}", address);

let signature = b"VERSION";
let signature = b"VERSION()";
let res: Box<CallResponse> = self.call(signature, &[], Some(address))?;
let version = &res.result.as_slice();
log::info!(
"Authority manager version is : {:?}",
&res.result.as_slice()
version
);

let signature = b"NAME";
let signature = b"NAME()";
let res: Box<CallResponse> = self.call(signature, &[], Some(address))?;
log::info!("Authority manager name is : {:?}", &res.result.as_slice());
let name = &res.result.as_slice();
log::info!("Authority manager name is : {:?}", name);

Ok(address)
Ok((address, version.to_vec(), name.to_vec()))
}

pub fn call<T>(
Expand Down Expand Up @@ -211,6 +215,7 @@ impl ContractClient {
from: Address,
// encoded_bytes: Vec<u8>,
signing: &ContractClientSignature,
recipient_address: Address,
) -> Result<H256, ChainRequestError> {
let encoded_bytes = encoder::encode_function_u8(method_signature, inputs);
let encoded_bytes_0x = ChainUtils::bytes_to_hex(encoded_bytes.as_slice());
Expand All @@ -222,7 +227,7 @@ impl ContractClient {
Some(v) => v,
};
let gas_limit_val = match gas_limit {
None => self.estimate_gas(encoded_bytes_slice.as_slice(), &value, from)?,
None => self.estimate_gas(encoded_bytes_slice.as_slice(), &value, from, recipient_address)?,
Some(v) => v,
};
let gas_price_val = match gas_price {
Expand All @@ -236,7 +241,7 @@ impl ContractClient {
nonce: nonce_val,
gas_price: gas_price_val,
gas_limit: gas_limit_val,
action: TransactionAction::Call(self.gateway_contract_address),
action: TransactionAction::Call(recipient_address),
value,
input: encoded_bytes,
signature: ChainUtils::empty_signature(),
Expand Down Expand Up @@ -296,6 +301,7 @@ impl ContractClient {
encoded: &[u8],
value: &U256,
from: Address,
recipient_address: Address,
) -> Result<U256, ChainRequestError> {
let call_json = JsonSer::new()
.start()
Expand All @@ -307,7 +313,7 @@ impl ContractClient {
.string(
"to",
str::from_utf8(
ChainUtils::address_to_hex(self.gateway_contract_address).as_slice(),
ChainUtils::address_to_hex(recipient_address).as_slice(),
)
.unwrap(),
)
Expand Down
5 changes: 3 additions & 2 deletions pallets/quantum-portal/src/eip_712_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::chain_utils::ChainUtils;
use ethabi_nostd::{encoder, Token, H256, U256}; //vec::{Vec};
use sp_std::prelude::*;
use ethabi_nostd::Address;

pub struct EIP712Utils;

Expand All @@ -10,7 +11,7 @@ impl EIP712Utils {
contract_name: &[u8],
contract_version: &[u8],
chain_id: u64,
contract_address: &[u8],
contract_address: Address,
) -> H256 {
let type_hash = ChainUtils::keccack(
b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)",
Expand All @@ -23,7 +24,7 @@ impl EIP712Utils {
Token::FixedBytes(Vec::from(hashed_name.as_bytes())),
Token::FixedBytes(Vec::from(hashed_version.as_bytes())),
Token::Uint(U256::from(chain_id)),
Token::Address(ChainUtils::hex_to_address(contract_address)),
Token::Address(contract_address),
]);

encoded_domain_seperator
Expand Down
42 changes: 24 additions & 18 deletions pallets/quantum-portal/src/quantum_portal_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,18 @@ impl<T: Config> QuantumPortalClient<T> {
Token::Bytes(multi_sig),
];

let recipient_address = self.contract.get_ledger_manager_address()?;

let res = self.contract.send(
method_signature,
&inputs,
None, //Some(U256::from(1000000 as u64)), // None,
None, // Some(U256::from(10000000000 as u64)), // None,
None, //Some(U256::from(10000000000 as u64)), // None,
U256::zero(),
None,
self.signer.from,
&self.signer,
recipient_address
)?;
Ok(res)
}
Expand All @@ -341,16 +344,16 @@ impl<T: Config> QuantumPortalClient<T> {
salt: Token,
expiry: Token,
) -> Result<Vec<u8>, TransactionCreationError> {
let verifying_contract_address = &self
let (verifying_contract_address, verifying_contract_version, verifying_contract_name) = &self
.contract
.get_authority_manager_address()
.map_err(|_| TransactionCreationError::CannotFindContractAddress)?;
// Generate the domain seperator hash, the hash is generated from the given arguments
let domain_seperator_hash = EIP712Utils::generate_eip_712_domain_seperator_hash(
&ChainUtils::address_to_hex(*verifying_contract_address), // ContractName
&ChainUtils::address_to_hex(*verifying_contract_address), // ContractVersion
verifying_contract_name, // ContractName
verifying_contract_version, // ContractVersion
self.contract.chain_id, // ChainId
&ChainUtils::address_to_hex(*verifying_contract_address), // VerifyingAddress
*verifying_contract_address, // VerifyingAddress
);
log::info!("domain_seperator_hash {:?}", domain_seperator_hash);

Expand Down Expand Up @@ -433,16 +436,16 @@ impl<T: Config> QuantumPortalClient<T> {
) -> ChainRequestResult<H256> {
let method_signature = b"mineRemoteBlock(uint64,uint64,(uint64,address,address,address,address,uint256,bytes,uint256)[],bytes32,uint64,bytes)";

// set a random salt
let (random_hash, _) = T::PalletRandomness::random_seed();
let salt = Token::FixedBytes(Vec::from(random_hash.as_ref()));

// set timestamp 1hr from now
let current_timestamp = source_block.timestamp;
let expiry_buffer = core::time::Duration::from_secs(3600u64);
let expiry_buffer = core::time::Duration::from_secs(259200u64);
let expiry_time = current_timestamp.saturating_add(expiry_buffer.as_secs());
let expiry = Token::Uint(U256::from(expiry_time));

// set a random salt
let (random_hash, _) = T::PalletRandomness::random_seed();
let salt = Token::FixedBytes(Vec::from(random_hash.as_ref()));

let tx_vec: Vec<Token> = txs
.iter()
.map(|t| {
Expand Down Expand Up @@ -473,6 +476,8 @@ impl<T: Config> QuantumPortalClient<T> {
.unwrap()
);

let recipient_address = self.contract.get_ledger_manager_address()?;

let res = self.contract.send(
method_signature,
&[
Expand All @@ -483,12 +488,13 @@ impl<T: Config> QuantumPortalClient<T> {
expiry,
Token::Bytes(multi_sig),
],
None, // Some(U256::from(1000000 as u32)), // None,
None, // Some(U256::from(60000000000 as u64)), // None,
None, //Some(U256::from(1000000 as u32)), // None,
None, //Some(U256::from(60000000000 as u64)), // None,
U256::zero(),
None,
self.signer.from,
&self.signer,
recipient_address
)?;
Ok(res)
}
Expand All @@ -506,17 +512,17 @@ impl<T: Config> QuantumPortalClient<T> {
salt: Token,
expiry: Token,
) -> Result<Vec<u8>, TransactionCreationError> {
let verifying_contract_address = &self
let (verifying_contract_address, verifying_contract_version, verifying_contract_name) = &self
.contract
.get_miner_manager_address()
.map_err(|_| TransactionCreationError::CannotFindContractAddress)?;

// Generate the domain seperator hash, the hash is generated from the given arguments
let domain_seperator_hash = EIP712Utils::generate_eip_712_domain_seperator_hash(
&ChainUtils::address_to_hex(*verifying_contract_address), // ContractName
&ChainUtils::address_to_hex(*verifying_contract_address), // ContractVersion
self.contract.chain_id, // ChainId
&ChainUtils::address_to_hex(*verifying_contract_address), // VerifyingAddress
b"FERRUM_QUANTUM_PORTAL_MINER_MGR", // ContractName
b"000.010", // ContractVersion
self.contract.chain_id, // ChainId
*verifying_contract_address, // VerifyingAddress
);
log::info!("domain_seperator_hash {:?}", domain_seperator_hash);

Expand Down Expand Up @@ -627,7 +633,7 @@ impl<T: Config> QuantumPortalClient<T> {
return Err(ChainRequestError::RemoteBlockAlreadyMined);
}
log::info!("Getting source block?");
let source_block = remote_client.local_block_by_nonce(local_chain, last_block.nonce)?;
let source_block = remote_client.local_block_by_nonce(local_chain, last_mined_block.nonce.saturating_add(1))?;
let default_qp_transaction = QpTransaction::default();
log::info!(
"Source block is GOT\n{:?}\n{:?}",
Expand Down
6 changes: 3 additions & 3 deletions scripts/start-local-testnet.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

# # remove any existing data from chain
# rm -rf ./chain
rm -rf ./chain

# # generate chain spec
# ./target/release/ferrum-network build-spec --disable-default-bootnode > ferrum-local-testnet.json
./target/release/ferrum-network build-spec --disable-default-bootnode > ferrum-local-testnet.json

# # insert the signing keys for alice
# ./target/release/ferrum-network key insert --key-type ofsg --scheme ecdsa --base-path ./chain/alice --chain ferrum-local-testnet.json --suri //Alice
./target/release/ferrum-network key insert --key-type ofsg --scheme ecdsa --base-path ./chain/alice --chain ferrum-local-testnet.json --suri //Alice

# # insert the signing keys for bob
# ./target/release/ferrum-network key insert --key-type ofsg --scheme ecdsa --base-path ./chain/bob --chain ferrum-local-testnet.json --suri //Bob
Expand Down

0 comments on commit 7be38fc

Please sign in to comment.