Skip to content

Commit 25f682f

Browse files
committed
Update ptrs testnet4 height
1 parent de9bae6 commit 25f682f

File tree

6 files changed

+76
-29
lines changed

6 files changed

+76
-29
lines changed

client/src/client.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Client {
161161
}
162162
}
163163
// Ptrs tip must connect to block
164-
{
164+
if chain.can_scan_ptrs(height) {
165165
let tip = chain.ptrs_tip();
166166
if tip.hash != block.header.prev_blockhash || tip.height + 1 != height {
167167
return Err(SyncError {
@@ -254,8 +254,14 @@ impl Client {
254254
self.apply_space_tx(chain, &tx, validated_tx);
255255
}
256256

257-
let ptrs_ctx =
258-
{ spaces_ptr::TxContext::from_tx::<Chain, Sha256>(chain, tx, spaceouts.is_some() || spaceouts_input_ctx.is_some())? };
257+
let ptrs_ctx = if chain.can_scan_ptrs(height) {
258+
spaces_ptr::TxContext::from_tx::<Chain, Sha256>(
259+
chain,
260+
tx,
261+
spaceouts.is_some() || spaceouts_input_ctx.is_some())?
262+
} else {
263+
None
264+
};
259265

260266
if let Some(ptrs_ctx) = ptrs_ctx {
261267
let spent_spaceouts = spaceouts_input_ctx.unwrap_or_default().into_iter()
@@ -286,7 +292,9 @@ impl Client {
286292
}
287293

288294
chain.update_spaces_tip(height, block_hash);
289-
chain.update_ptrs_tip(height, block_hash);
295+
if chain.can_scan_ptrs(height) {
296+
chain.update_ptrs_tip(height, block_hash);
297+
}
290298

291299
Ok((spaces_meta, ptr_meta))
292300
}

client/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,12 @@ impl Args {
195195
);
196196

197197
let genesis = Spaced::genesis(args.chain);
198+
let ptr_genesis = Spaced::ptr_genesis(args.chain);
198199

199200
let chain = Chain::load(
200201
args.chain.fallback_network(),
201202
genesis,
203+
ptr_genesis,
202204
&data_dir,
203205
args.block_index || args.block_index_full,
204206
args.block_index || args.block_index_full, // TODO: option to index ptrs

client/src/spaces.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,12 @@ impl Spaced {
172172
_ => panic!("unsupported network"),
173173
}
174174
}
175+
176+
pub fn ptr_genesis(network: ExtendedNetwork) -> ChainAnchor {
177+
match network {
178+
ExtendedNetwork::Testnet4 => ChainAnchor::PTR_TESTNET4(),
179+
ExtendedNetwork::Regtest => ChainAnchor::PTR_TESTNET4(),
180+
_ => panic!("unsupported network"),
181+
}
182+
}
175183
}

client/src/store/chain.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const_assert!(
3939
pub struct Chain {
4040
db: LiveStore,
4141
idx: LiveIndex,
42+
ptrs_genesis: ChainAnchor,
4243
}
4344

4445
#[derive(Clone)]
@@ -94,8 +95,7 @@ impl Chain {
9495
self.db.pt.state.get_ptr_info(key)
9596
}
9697

97-
pub fn load(network: Network, genesis: ChainAnchor, dir: &Path, index_spaces: bool, index_ptrs: bool) -> anyhow::Result<Self> {
98-
let ptrs_genesis = spaces_ptr::constants::ptrs_start_height(&network);
98+
pub fn load(_network: Network, genesis: ChainAnchor, ptrs_genesis: ChainAnchor, dir: &Path, index_spaces: bool, index_ptrs: bool) -> anyhow::Result<Self> {
9999
let proto_db_path = dir.join("protocol.sdb");
100100
let ptrs_db_path = dir.join("ptrs.sdb");
101101
let initial_sp_sync = !proto_db_path.exists();
@@ -109,7 +109,7 @@ impl Chain {
109109

110110
let pt_store = PtrStore::open(ptrs_db_path)?;
111111
let pt = PtrLiveStore {
112-
state: pt_store.begin(&genesis)?,
112+
state: pt_store.begin(&ptrs_genesis)?,
113113
store: pt_store,
114114
};
115115

@@ -130,27 +130,23 @@ impl Chain {
130130
let chain = Chain {
131131
db: LiveStore { sp, pt },
132132
idx: LiveIndex { sp: sp_idx, pt: pt_idx },
133+
ptrs_genesis
133134
};
134135

135136
// If spaces synced past the ptrs point, reset the tip
136137
if initial_pt_sync {
137138
let sp_tip = chain.db.sp.state.tip.read().expect("tip").clone();
138-
if sp_tip.height > ptrs_genesis {
139+
if sp_tip.height > ptrs_genesis.height {
139140
info!("spaces tip = {} > ptrs genesis = {} - rescanning to index ptrs",
140-
sp_tip.height, ptrs_genesis
141+
sp_tip.height, ptrs_genesis.height
141142
);
142143
assert_eq!(
143-
ptrs_genesis % COMMIT_BLOCK_INTERVAL, 0,
144+
ptrs_genesis.height % COMMIT_BLOCK_INTERVAL, 0,
144145
"ptrs genesis must align with commit interval"
145146
);
146-
chain.restore_spaces(|height| {
147-
if height != ptrs_genesis {
148-
// return a dummy hash until we have a checkpoint matching ptrs genesis
149-
return Ok(BlockHash::from_slice(&[0u8; 32]).expect("hash"));
150-
}
151-
152-
Ok(sp_tip.hash)
153-
})?;
147+
chain.restore_spaces(|_| {
148+
return Ok(BlockHash::from_slice(&[0u8; 32]).expect("hash"));
149+
}, Some(ptrs_genesis.height))?;
154150
}
155151
}
156152

@@ -263,6 +259,10 @@ impl Chain {
263259
*self.db.pt.state.tip.read().expect("ptrs tip")
264260
}
265261

262+
pub fn can_scan_ptrs(&self, height: u32) -> bool {
263+
height > self.ptrs_genesis.height
264+
}
265+
266266
pub fn update_ptrs_tip(&self, height: u32, block_hash: BlockHash) {
267267
let mut tip = self.db.pt.state.tip.write().expect("write tip");
268268
tip.height = height;
@@ -349,7 +349,7 @@ impl Chain {
349349
where
350350
F: Fn(u32) -> anyhow::Result<BlockHash>,
351351
{
352-
let point = self.restore_spaces(get_block_hash)?;
352+
let point = self.restore_spaces(get_block_hash, None)?;
353353
self.restore_ptrs(point)
354354
}
355355

@@ -405,22 +405,27 @@ impl Chain {
405405
Ok(())
406406
}
407407

408-
pub fn restore_spaces<F>(&self, get_block_hash: F) -> anyhow::Result<ChainAnchor>
408+
pub fn restore_spaces<F>(&self, get_block_hash: F, restore_to_height: Option<u32>) -> anyhow::Result<ChainAnchor>
409409
where
410410
F: Fn(u32) -> anyhow::Result<BlockHash>,
411411
{
412412
let chain_iter = self.db.sp.store.iter();
413413
for (snapshot_index, snapshot) in chain_iter.enumerate() {
414414
let chain_snapshot = snapshot?;
415415
let chain_checkpoint: ChainAnchor = chain_snapshot.metadata().try_into()?;
416-
let required_hash = get_block_hash(chain_checkpoint.height)?;
417-
418-
if required_hash != chain_checkpoint.hash {
419-
info!(
420-
"Could not restore to block={} height={}",
421-
chain_checkpoint.hash, chain_checkpoint.height
422-
);
423-
continue;
416+
if let Some(restore_to_height) = restore_to_height {
417+
if restore_to_height != chain_checkpoint.height {
418+
continue;
419+
}
420+
} else {
421+
let required_hash = get_block_hash(chain_checkpoint.height)?;
422+
if required_hash != chain_checkpoint.hash {
423+
info!(
424+
"Could not restore to block={} height={}",
425+
chain_checkpoint.hash, chain_checkpoint.height
426+
);
427+
continue;
428+
}
424429
}
425430

426431
info!(

protocol/src/constants.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ impl ChainAnchor {
8484
)
8585
};
8686

87+
pub const PTR_TESTNET4: fn() -> Self = || {
88+
Self::new(
89+
[
90+
0x94, 0x94, 0xe5, 0x15, 0x75, 0xaa, 0xcf, 0x09,
91+
0x45, 0xc1, 0x7a, 0x30, 0xf3, 0x53, 0x20, 0xe8,
92+
0x1d, 0x2b, 0xd0, 0xed, 0x6a, 0xaa, 0xb3, 0xc3,
93+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94+
],
95+
100_008,
96+
)
97+
};
98+
8799
// Testnet activation block
88100
pub const TESTNET: fn() -> Self = || {
89101
Self::new(
@@ -109,6 +121,18 @@ impl ChainAnchor {
109121
0,
110122
)
111123
};
124+
125+
pub const PTR_REGTEST: fn() -> Self = || {
126+
Self::new(
127+
[
128+
0x06, 0x22, 0x6e, 0x46, 0x11, 0x1a, 0x0b, 0x59,
129+
0xca, 0xaf, 0x12, 0x60, 0x43, 0xeb, 0x5b, 0xbf,
130+
0x28, 0xc3, 0x4f, 0x3a, 0x5e, 0x33, 0x2a, 0x1f,
131+
0xc7, 0xb2, 0xb7, 0x3c, 0xf1, 0x88, 0x91, 0x0f,
132+
],
133+
0,
134+
)
135+
};
112136
}
113137

114138
#[cfg(feature = "bincode")]

ptr/src/constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bitcoin::Network;
22

33
pub const PTR_MAINNET_HEIGHT : u32 = 922_777;
4-
pub const PTR_TESTNET4_HEIGHT : u32 = 100_000;
4+
pub const PTR_TESTNET4_HEIGHT : u32 = 100_008;
55
pub const PTR_REGTEST_HEIGHT : u32 = 0;
66

77
pub fn ptrs_start_height(network: &Network) -> u32 {

0 commit comments

Comments
 (0)