Skip to content

Commit

Permalink
Runtime 26 (chainx-org#86)
Browse files Browse the repository at this point in the history
* Log fetch url for http error

* Fix .

* Fix .

* Reduce the cost of submitting preimage

* Skip check total supply for testnet

* Fix tests

---------

Co-authored-by: icodezjb <icodezjb@users.noreply.github.com>
  • Loading branch information
hacpy and icodezjb authored Mar 14, 2024
1 parent 2d7fb4a commit 35e7c06
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 82 deletions.
6 changes: 3 additions & 3 deletions runtime/bevm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 25,
spec_version: 26,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -409,8 +409,8 @@ impl pallet_scheduler::Config for Runtime {
}

parameter_types! {
pub const PreimageBaseDeposit: Balance = 100 * DOLLARS;
pub const PreimageByteDeposit: Balance = 100 * CENTS;
pub const PreimageBaseDeposit: Balance = 1 * DOLLARS;
pub const PreimageByteDeposit: Balance = 1 * CENTS;
pub const PreimageHoldReason: RuntimeHoldReason =
RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 25,
spec_version: 26,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -410,8 +410,8 @@ impl pallet_scheduler::Config for Runtime {
}

parameter_types! {
pub const PreimageBaseDeposit: Balance = 100 * DOLLARS;
pub const PreimageByteDeposit: Balance = 100 * CENTS;
pub const PreimageBaseDeposit: Balance = 1 * DOLLARS;
pub const PreimageByteDeposit: Balance = 1 * CENTS;
pub const PreimageHoldReason: RuntimeHoldReason =
RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 25,
spec_version: 26,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -410,8 +410,8 @@ impl pallet_scheduler::Config for Runtime {
}

parameter_types! {
pub const PreimageBaseDeposit: Balance = 100 * DOLLARS;
pub const PreimageByteDeposit: Balance = 100 * CENTS;
pub const PreimageBaseDeposit: Balance = 1 * DOLLARS;
pub const PreimageByteDeposit: Balance = 1 * CENTS;
pub const PreimageHoldReason: RuntimeHoldReason =
RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
}
Expand Down
53 changes: 34 additions & 19 deletions xpallets/gateway/bitcoin/src/ocw/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T: Config> Pallet<T> {
timeout: u64,
) -> Result<Vec<(OutPoint, u64)>, DispatchError> {
let resp_bytes = Self::fetch_utxos(address, url, timeout).map_err(|e| {
log!(error, "fetch_from_remote error: {:?}", e);
log!(error, "fetch_and_parse_utxos error: {:?}", e);
Error::<T>::HttpFetchingError
})?;

Expand Down Expand Up @@ -339,7 +339,7 @@ impl<T: Config> Pallet<T> {
.deadline(timeout)
.send() // Sending the request out by the host
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?;

Expand All @@ -350,16 +350,21 @@ impl<T: Config> Pallet<T> {
let response = pending
.try_wait(timeout)
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?;

if response.code != 200 {
log!(error, "Unexpected http request status code: {}", response.code);
log!(
error,
"Unexpected http request status code: {}, fetch_url: {:?}",
response.code,
url
);
return Err(Error::<T>::HttpFetchingError.into())
}

Expand All @@ -381,7 +386,7 @@ impl<T: Config> Pallet<T> {
.deadline(timeout)
.send() // Sending the request out by the host
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?;

Expand All @@ -392,16 +397,21 @@ impl<T: Config> Pallet<T> {
let response = pending
.try_wait(timeout)
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?;

if response.code != 200 {
log!(error, "Unexpected http request status code: {}", response.code);
log!(
error,
"Unexpected http request status code: {}, fetch_url: {:?}",
response.code,
url
);
return Err(Error::<T>::HttpFetchingError.into())
}

Expand All @@ -415,14 +425,14 @@ impl<T: Config> Pallet<T> {
) -> Result<Vec<Response>, DispatchError> {
let mut pendings = Vec::new();
let timeout = sp_io::offchain::timestamp().add(Duration::from_millis(timeout));
for url in urls.into_iter() {
let request = http::Request::get(&url);
for url in urls.iter() {
let request = http::Request::get(url);

let pending = request
.deadline(timeout) // Setting the timeout time
.send() // Sending the request out by the host
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?;
pendings.push(pending);
Expand All @@ -431,18 +441,18 @@ impl<T: Config> Pallet<T> {
let mut responses = Vec::new();
for result in http::PendingRequest::try_wait_all(pendings, timeout).into_iter() {
let http_result = result.map_err(|e| {
log!(error, "request http for result error: {:?}", e);
log!(error, "request http for result error: {:?}, fetch_url: {:?}", e, urls);
Error::<T>::BatchHttpFetchError
})?;
let response = http_result.map_err(|e| {
log!(error, "request http for result error: {:?}", e);
log!(error, "request http for result error: {:?}, fetch_url: {:?}", e, urls);
Error::<T>::BatchHttpFetchError
})?;
responses.push(response);
}

if responses.iter().any(|resp| resp.code != 200) {
log!(error, "Unexpected batch http request status code!");
log!(error, "Unexpected batch http request status code! fetch_url: {:?}", urls);
return Err(Error::<T>::BatchHttpFetchError.into())
}

Expand All @@ -458,7 +468,7 @@ impl<T: Config> Pallet<T> {
.deadline(timeout) // Setting the timeout time
.send() // Sending the request out by the host
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?;

Expand All @@ -469,16 +479,21 @@ impl<T: Config> Pallet<T> {
let response = pending
.try_wait(timeout)
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?
.map_err(|e| {
log!(error, "{:?}", e);
log!(error, "{:?}, fetch_url: {:?}", e, url);
Error::<T>::HttpFetchingError
})?;

if response.code != 200 {
log!(error, "Unexpected http request status code: {}", response.code);
log!(
error,
"Unexpected http request status code: {}, fetch_url: {:?}",
response.code,
url
);
return Err(Error::<T>::HttpFetchingError.into())
}

Expand Down
5 changes: 4 additions & 1 deletion xpallets/gateway/bitcoin/src/ocw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use frame_support::{ensure, traits::Currency};
use frame_system::offchain::{Account, SendSignedTransaction, SignMessage, Signer};
use light_bitcoin::{
chain::{OutPoint, Transaction, TransactionOutput},
keys::{self, Address, Message, SchnorrSignature},
keys::{self, Address, Message, Network, SchnorrSignature},
mast,
mast::{
key::{KeyAgg, KeyPair, PrivateKey, PublicKey},
Expand Down Expand Up @@ -432,6 +432,9 @@ impl<T: Config> Pallet<T> {
}

pub(crate) fn check_total_btc_supply() -> Result<(), DispatchError> {
if Self::network_id() != Network::Mainnet {
return Ok(())
}
let total_btc_balance = Self::get_total_btc_balance()?;
let total_btc_supply = Self::total_btc_supply();
let processing_withdrawal_amount = Self::processing_withdrawal_amount();
Expand Down
53 changes: 0 additions & 53 deletions xpallets/gateway/bitcoin/src/tests/ocw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,43 +100,6 @@ fn insert_fee_rate_request(state: &mut Arc<RwLock<OffchainState>>) {
);
}

/// Temporary solution for testing
fn insert_hot_btc_balance_request(state: &mut Arc<RwLock<OffchainState>>) {
insert_expected_request(
state,
PendingRequest {
method: "GET".into(),
uri: "https://mempool.space/testnet/api/address/tb1pn202yeugfa25nssxk2hv902kmxrnp7g9xt487u256n20jgahuwasdcjfdw".into(),
response: Some(br#"{"address":"tb1pn202yeugfa25nssxk2hv902kmxrnp7g9xt487u256n20jgahuwasdcjfdw","chain_stats":{"funded_txo_count":5,"funded_txo_sum":1000376224,"spent_txo_count":3,"spent_txo_sum":1972557,"tx_count":5},"mempool_stats":{"funded_txo_count":0,"funded_txo_sum":0,"spent_txo_count":0,"spent_txo_sum":0,"tx_count":0}}"#.to_vec()),
sent: true,
..Default::default()
});
}

fn insert_cold_btc_balance_request(state: &mut Arc<RwLock<OffchainState>>) {
insert_expected_request(
state,
PendingRequest {
method: "GET".into(),
uri: "https://mempool.space/testnet/api/address/tb1p3x0y2cj9pcz2295v0jlxtpefgxpkfvhply69sa398ku8yz703a2q5rxdw5".into(),
response: Some(br#"{"address":"tb1pn202yeugfa25nssxk2hv902kmxrnp7g9xt487u256n20jgahuwasdcjfdw","chain_stats":{"funded_txo_count":5,"funded_txo_sum":1000376224,"spent_txo_count":3,"spent_txo_sum":1972557,"tx_count":5},"mempool_stats":{"funded_txo_count":0,"funded_txo_sum":0,"spent_txo_count":0,"spent_txo_sum":0,"tx_count":0}}"#.to_vec()),
sent: true,
..Default::default()
});
}

fn insert_group_btc_balance_request(state: &mut Arc<RwLock<OffchainState>>) {
insert_expected_request(
state,
PendingRequest {
method: "GET".into(),
uri: "https://mempool.space/testnet/api/address/tb1p03tyrfs62aa7xay53nurngz7sf2ydz5lwpexv7jum6k0fknphccs58d5tx".into(),
response: Some(br#"{"address":"tb1p03tyrfs62aa7xay53nurngz7sf2ydz5lwpexv7jum6k0fknphccs58d5tx","chain_stats":{"funded_txo_count":5,"funded_txo_sum":1000376224,"spent_txo_count":3,"spent_txo_sum":1972557,"tx_count":5},"mempool_stats":{"funded_txo_count":0,"funded_txo_sum":0,"spent_txo_count":0,"spent_txo_sum":0,"tx_count":0}}"#.to_vec()),
sent: true,
..Default::default()
});
}

fn insert_utxo_request(state: &mut Arc<RwLock<OffchainState>>) {
insert_expected_request(
state,
Expand Down Expand Up @@ -479,8 +442,6 @@ fn alice_create_unsigned_tx(
t: &mut sp_io::TestExternalities,
) {
insert_fee_rate_request(state);
insert_hot_btc_balance_request(state);
insert_cold_btc_balance_request(state);
insert_utxo_request(state);
insert_fee_rate_request(state);

Expand Down Expand Up @@ -805,8 +766,6 @@ fn test_withdraw_with_multi_group() {
t.register_extension(TransactionPoolExt::new(pool));

insert_fee_rate_request(&mut state);
insert_group_btc_balance_request(&mut state);
insert_cold_btc_balance_request(&mut state);
insert_group_utxo_request(&mut state);
insert_fee_rate_request(&mut state);

Expand Down Expand Up @@ -1004,8 +963,6 @@ fn test_withdraw_with_fee() {
});

insert_fee_rate_request(&mut state);
insert_hot_btc_balance_request(&mut state);
insert_cold_btc_balance_request(&mut state);
insert_utxo_request(&mut state);
insert_fee_rate_request(&mut state);

Expand Down Expand Up @@ -1073,8 +1030,6 @@ fn test_hot_to_cold() {
});

insert_fee_rate_request(&mut state);
insert_hot_btc_balance_request(&mut state);
insert_cold_btc_balance_request(&mut state);
insert_utxo_request(&mut state);
insert_fee_rate_request(&mut state);

Expand Down Expand Up @@ -1151,8 +1106,6 @@ fn test_process_multi_withdraw() {
});

insert_fee_rate_request(&mut state);
insert_hot_btc_balance_request(&mut state);
insert_cold_btc_balance_request(&mut state);
insert_multi_utxo_request(&mut state);
insert_fee_rate_request(&mut state);

Expand Down Expand Up @@ -1301,8 +1254,6 @@ fn test_concurrent_withdraw() {
});

insert_fee_rate_request(&mut state);
insert_hot_btc_balance_request(&mut state);
insert_cold_btc_balance_request(&mut state);
insert_concurrent_utxo_request(&mut state);
insert_fee_rate_request(&mut state);

Expand Down Expand Up @@ -1454,8 +1405,6 @@ fn test_max_concurrent_withdraw() {
});

insert_fee_rate_request(&mut state);
insert_hot_btc_balance_request(&mut state);
insert_cold_btc_balance_request(&mut state);

t.execute_with(|| {
MockPublic::set_all_keys(vec![MockPublic(1)]);
Expand Down Expand Up @@ -1646,8 +1595,6 @@ fn test_not_enough_utxo() {
t.register_extension(TransactionPoolExt::new(pool));

insert_fee_rate_request(&mut state);
insert_hot_btc_balance_request(&mut state);
insert_cold_btc_balance_request(&mut state);
insert_utxo_request(&mut state);
insert_fee_rate_request(&mut state);

Expand Down

0 comments on commit 35e7c06

Please sign in to comment.