Skip to content

Commit 1d33d5a

Browse files
feat: add 8 bit-flags into script type args to control features
1 parent 2464c8f commit 1d33d5a

File tree

10 files changed

+55
-18
lines changed

10 files changed

+55
-18
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CARGO := @cargo
22

3-
MOLC := @moleculec
3+
MOLC := moleculec
44
MOLC_VERSION := 0.7.5
55

66
NEXTEST_RUN_ARGS := --no-fail-fast --success-output never --failure-output final

prover/src/tests/service.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn test_spv_client(
8585
let new_client: packed::SpvClient = service.tip_client().pack();
8686

8787
old_client
88-
.verify_new_client(&new_client, update)
88+
.verify_new_client(&new_client, update, 0)
8989
.map_err(|err| err as i8)
9090
.unwrap();
9191
old_client = new_client;

verifier/schemas/types.mol

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct SpvClient {
5353
struct SpvTypeArgs {
5454
type_id: Hash,
5555
clients_count: byte,
56+
flags: byte,
5657
}
5758

5859
//

verifier/src/constants.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Constants.
2+
3+
pub const FLAG_DISABLE_DIFFICULTY_CHECK: u8 = 0b1000_0000;

verifier/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern crate core;
1111
#[macro_use]
1212
mod log;
1313

14+
pub mod constants;
1415
pub mod error;
1516
pub mod types;
1617
pub mod utilities;

verifier/src/types/conversion/pack.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl Pack<packed::SpvTypeArgs> for core::SpvTypeArgs {
104104
packed::SpvTypeArgs::new_builder()
105105
.type_id(self.type_id.pack())
106106
.clients_count(self.clients_count.into())
107+
.flags(self.flags.into())
107108
.build()
108109
}
109110
}

verifier/src/types/conversion/unpack.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl<'r> Unpack<core::SpvTypeArgs> for packed::SpvTypeArgsReader<'r> {
104104
core::SpvTypeArgs {
105105
type_id: self.type_id().unpack(),
106106
clients_count: self.clients_count().into(),
107+
flags: self.flags().into(),
107108
}
108109
}
109110
}

verifier/src/types/core.rs

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ pub struct SpvTypeArgs {
8484
///
8585
/// N.B. Exclude the SPV info cell.
8686
pub clients_count: u8,
87+
/// Bit flags to control features.
88+
///
89+
/// From high to low:
90+
/// - Set 0-th bit to true, to disable difficulty checks.
91+
/// - Other bits are reserved.
92+
pub flags: u8,
8793
}
8894

8995
#[cfg(feature = "std")]

verifier/src/types/extension/packed.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use bitcoin::{
99
use molecule::bytes::Bytes;
1010

1111
use crate::{
12+
constants,
1213
core::result::Result,
1314
error::{BootstrapError, UpdateError, VerifyTxError},
1415
types::{core, packed, prelude::*},
@@ -67,6 +68,7 @@ impl packed::SpvBootstrap {
6768
let header: core::Header =
6869
deserialize(&self.header().raw_data()).map_err(|_| BootstrapError::DecodeHeader)?;
6970
// Verify POW: just trust the input header.
71+
// TODO Check constants::FLAG_DISABLE_DIFFICULTY_CHECK before return errors.
7072
let block_hash = header
7173
.validate_pow(header.target())
7274
.map_err(|_| BootstrapError::Pow)?
@@ -106,6 +108,7 @@ impl packed::SpvClient {
106108
&self,
107109
packed_new_client: &Self,
108110
update: packed::SpvUpdate,
111+
flags: u8,
109112
) -> Result<(), UpdateError> {
110113
let old_client = self.unpack();
111114
let new_client = packed_new_client.unpack();
@@ -146,13 +149,19 @@ impl packed::SpvClient {
146149
expect {expected} but got {actual}"
147150
);
148151
});
149-
return Err(UpdateError::Difficulty);
152+
if flags & constants::FLAG_DISABLE_DIFFICULTY_CHECK == 0 {
153+
return Err(UpdateError::Difficulty);
154+
}
150155
}
151156
// Check POW.
152-
new_tip_block_hash = header
153-
.validate_pow(new_info.1.into())
154-
.map_err(|_| UpdateError::Pow)?
155-
.into();
157+
new_tip_block_hash = if flags & constants::FLAG_DISABLE_DIFFICULTY_CHECK == 0 {
158+
header
159+
.validate_pow(new_info.1.into())
160+
.map_err(|_| UpdateError::Pow)?
161+
} else {
162+
header.block_hash()
163+
}
164+
.into();
156165
// Update the target adjust info.
157166
{
158167
match (new_max_height + 1) % DIFFCHANGE_INTERVAL {

verifier/src/types/generated/types.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -3499,6 +3499,7 @@ impl ::core::fmt::Display for SpvTypeArgs {
34993499
write!(f, "{} {{ ", Self::NAME)?;
35003500
write!(f, "{}: {}", "type_id", self.type_id())?;
35013501
write!(f, ", {}: {}", "clients_count", self.clients_count())?;
3502+
write!(f, ", {}: {}", "flags", self.flags())?;
35023503
write!(f, " }}")
35033504
}
35043505
}
@@ -3509,19 +3510,22 @@ impl ::core::default::Default for SpvTypeArgs {
35093510
}
35103511
}
35113512
impl SpvTypeArgs {
3512-
const DEFAULT_VALUE: [u8; 33] = [
3513+
const DEFAULT_VALUE: [u8; 34] = [
35133514
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3514-
0, 0, 0,
3515+
0, 0, 0, 0,
35153516
];
3516-
pub const TOTAL_SIZE: usize = 33;
3517-
pub const FIELD_SIZES: [usize; 2] = [32, 1];
3518-
pub const FIELD_COUNT: usize = 2;
3517+
pub const TOTAL_SIZE: usize = 34;
3518+
pub const FIELD_SIZES: [usize; 3] = [32, 1, 1];
3519+
pub const FIELD_COUNT: usize = 3;
35193520
pub fn type_id(&self) -> Hash {
35203521
Hash::new_unchecked(self.0.slice(0..32))
35213522
}
35223523
pub fn clients_count(&self) -> Byte {
35233524
Byte::new_unchecked(self.0.slice(32..33))
35243525
}
3526+
pub fn flags(&self) -> Byte {
3527+
Byte::new_unchecked(self.0.slice(33..34))
3528+
}
35253529
pub fn as_reader<'r>(&'r self) -> SpvTypeArgsReader<'r> {
35263530
SpvTypeArgsReader::new_unchecked(self.as_slice())
35273531
}
@@ -3551,6 +3555,7 @@ impl molecule::prelude::Entity for SpvTypeArgs {
35513555
Self::new_builder()
35523556
.type_id(self.type_id())
35533557
.clients_count(self.clients_count())
3558+
.flags(self.flags())
35543559
}
35553560
}
35563561
#[derive(Clone, Copy)]
@@ -3574,19 +3579,23 @@ impl<'r> ::core::fmt::Display for SpvTypeArgsReader<'r> {
35743579
write!(f, "{} {{ ", Self::NAME)?;
35753580
write!(f, "{}: {}", "type_id", self.type_id())?;
35763581
write!(f, ", {}: {}", "clients_count", self.clients_count())?;
3582+
write!(f, ", {}: {}", "flags", self.flags())?;
35773583
write!(f, " }}")
35783584
}
35793585
}
35803586
impl<'r> SpvTypeArgsReader<'r> {
3581-
pub const TOTAL_SIZE: usize = 33;
3582-
pub const FIELD_SIZES: [usize; 2] = [32, 1];
3583-
pub const FIELD_COUNT: usize = 2;
3587+
pub const TOTAL_SIZE: usize = 34;
3588+
pub const FIELD_SIZES: [usize; 3] = [32, 1, 1];
3589+
pub const FIELD_COUNT: usize = 3;
35843590
pub fn type_id(&self) -> HashReader<'r> {
35853591
HashReader::new_unchecked(&self.as_slice()[0..32])
35863592
}
35873593
pub fn clients_count(&self) -> ByteReader<'r> {
35883594
ByteReader::new_unchecked(&self.as_slice()[32..33])
35893595
}
3596+
pub fn flags(&self) -> ByteReader<'r> {
3597+
ByteReader::new_unchecked(&self.as_slice()[33..34])
3598+
}
35903599
}
35913600
impl<'r> molecule::prelude::Reader<'r> for SpvTypeArgsReader<'r> {
35923601
type Entity = SpvTypeArgs;
@@ -3613,11 +3622,12 @@ impl<'r> molecule::prelude::Reader<'r> for SpvTypeArgsReader<'r> {
36133622
pub struct SpvTypeArgsBuilder {
36143623
pub(crate) type_id: Hash,
36153624
pub(crate) clients_count: Byte,
3625+
pub(crate) flags: Byte,
36163626
}
36173627
impl SpvTypeArgsBuilder {
3618-
pub const TOTAL_SIZE: usize = 33;
3619-
pub const FIELD_SIZES: [usize; 2] = [32, 1];
3620-
pub const FIELD_COUNT: usize = 2;
3628+
pub const TOTAL_SIZE: usize = 34;
3629+
pub const FIELD_SIZES: [usize; 3] = [32, 1, 1];
3630+
pub const FIELD_COUNT: usize = 3;
36213631
pub fn type_id(mut self, v: Hash) -> Self {
36223632
self.type_id = v;
36233633
self
@@ -3626,6 +3636,10 @@ impl SpvTypeArgsBuilder {
36263636
self.clients_count = v;
36273637
self
36283638
}
3639+
pub fn flags(mut self, v: Byte) -> Self {
3640+
self.flags = v;
3641+
self
3642+
}
36293643
}
36303644
impl molecule::prelude::Builder for SpvTypeArgsBuilder {
36313645
type Entity = SpvTypeArgs;
@@ -3636,6 +3650,7 @@ impl molecule::prelude::Builder for SpvTypeArgsBuilder {
36363650
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
36373651
writer.write_all(self.type_id.as_slice())?;
36383652
writer.write_all(self.clients_count.as_slice())?;
3653+
writer.write_all(self.flags.as_slice())?;
36393654
Ok(())
36403655
}
36413656
fn build(&self) -> Self::Entity {

0 commit comments

Comments
 (0)