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

fix: add pow limit in the check_target #84

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions btc-types/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub const BLOCKS_PER_ADJUSTMENT: u64 = 2016;
pub const TARGET_BLOCK_TIME_SECS: u64 = 10 * 60;
pub const EXPECTED_TIME: u64 = BLOCKS_PER_ADJUSTMENT as u64 * TARGET_BLOCK_TIME_SECS;
pub const MAX_ADJUSTMENT_FACTOR: u64 = 4;
pub const POW_LIMIT: U256 = U256::new(
0x0000_0000_ffff_ffff_ffff_ffff_ffff_ffff,
0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff,
);

#[cfg(feature = "testnet")]
pub mod testnet {
Expand Down
4 changes: 4 additions & 0 deletions btc-types/src/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ impl U256 {

pub const ONE: U256 = U256(0, 1);

pub const fn new(a: u128, b: u128) -> Self {
U256(a, b)
}

/// Creates `U256` from a big-endian array of `u8`s.
#[must_use]
pub fn from_be_bytes(a: &[u8; 32]) -> U256 {
Expand Down
6 changes: 5 additions & 1 deletion contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use btc_types::contract_args::{InitArgs, ProofArgs};
use btc_types::hash::H256;
use btc_types::header::{
ExtendedHeader, Header, BLOCKS_PER_ADJUSTMENT, EXPECTED_TIME, MAX_ADJUSTMENT_FACTOR,
ExtendedHeader, Header, BLOCKS_PER_ADJUSTMENT, EXPECTED_TIME, MAX_ADJUSTMENT_FACTOR, POW_LIMIT,
};
use btc_types::u256::U256;
use near_plugins::{
Expand Down Expand Up @@ -500,6 +500,10 @@ impl BtcLightClient {
require!(!new_target_overflow, "new target overflow");
new_target = new_target / U256::from(EXPECTED_TIME);

if new_target > POW_LIMIT {
new_target = POW_LIMIT;
}

let expected_bits = new_target.target_to_bits();

require!(
Expand Down
Binary file modified res/btc_light_client_mainnet.wasm
Binary file not shown.
Binary file modified res/btc_light_client_testnet.wasm
Binary file not shown.
Loading