Skip to content

Commit 7f81aff

Browse files
committed
Update outpoint mapping for ptrs
1 parent 5c26f8d commit 7f81aff

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

client/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use spaces_protocol::{
1515
validate::{TxChangeSet, UpdateKind, Validator},
1616
Bytes, Covenant, FullSpaceOut, RevokeReason, SpaceOut,
1717
};
18-
use spaces_ptr::{CommitmentKey, RegistryKey, RegistrySptrKey};
18+
use spaces_ptr::{CommitmentKey, RegistryKey, RegistrySptrKey, PtrOutpointKey};
1919
use spaces_wallet::bitcoin::{Network, Transaction};
2020

2121
use crate::{
@@ -348,7 +348,7 @@ impl Client {
348348
}
349349

350350
// Outpoint => PtrOut
351-
let outpoint_key = OutpointKey::from_outpoint::<Sha256>(outpoint);
351+
let outpoint_key = PtrOutpointKey::from_outpoint::<Sha256>(outpoint);
352352
state.insert_ptrout(outpoint_key, create);
353353
}
354354
}

client/src/rpc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use tokio::{
4848
task::JoinSet,
4949
};
5050
use spaces_protocol::hasher::Hash;
51-
use spaces_ptr::{PtrSource, FullPtrOut, PtrOut, Commitment, RegistryKey, CommitmentKey, RegistrySptrKey};
51+
use spaces_ptr::{PtrSource, FullPtrOut, PtrOut, Commitment, RegistryKey, CommitmentKey, RegistrySptrKey, PtrOutpointKey};
5252
use spaces_ptr::sptr::Sptr;
5353
use spaces_wallet::bitcoin::hashes::sha256;
5454
use crate::auth::BasicAuthLayer;
@@ -1853,7 +1853,7 @@ impl AsyncChainState {
18531853
outpoint: OutPoint,
18541854
prefer_recent: bool,
18551855
) -> anyhow::Result<ProofResult> {
1856-
let key = OutpointKey::from_outpoint::<Sha256>(outpoint);
1856+
let key = PtrOutpointKey::from_outpoint::<Sha256>(outpoint);
18571857

18581858
let proof = if !prefer_recent {
18591859
let ptrout = match state.get_ptrout(&outpoint)? {

client/src/store/chain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use spaces_protocol::hasher::{BaseHash, BidKey, OutpointKey, SpaceKey};
1010
use spaces_protocol::prepare::SpacesSource;
1111
use spaces_protocol::{FullSpaceOut, SpaceOut};
1212
use spaces_protocol::slabel::SLabel;
13-
use spaces_ptr::{Commitment, CommitmentKey, FullPtrOut, PtrOut, PtrSource, RegistryKey, RegistrySptrKey};
13+
use spaces_ptr::{Commitment, CommitmentKey, FullPtrOut, PtrOut, PtrSource, RegistryKey, RegistrySptrKey, PtrOutpointKey};
1414
use spaces_ptr::sptr::Sptr;
1515
use spaces_wallet::bitcoin::Network;
1616
use crate::client::{BlockMeta, PtrBlockMeta};
@@ -276,7 +276,7 @@ impl Chain {
276276
tip.hash = block_hash;
277277
}
278278

279-
pub(crate) fn insert_ptrout(&self, key: OutpointKey, ptrout: PtrOut) {
279+
pub(crate) fn insert_ptrout(&self, key: PtrOutpointKey, ptrout: PtrOut) {
280280
self.db.pt.state.insert(key, ptrout)
281281
}
282282

client/src/store/ptrs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use spacedb::{
2020
use spaces_protocol::{
2121
bitcoin::{BlockHash, OutPoint},
2222
constants::{ChainAnchor},
23-
hasher::{KeyHash, OutpointKey},
23+
hasher::{KeyHash},
2424
};
2525
use spaces_protocol::slabel::SLabel;
26-
use spaces_ptr::{Commitment, CommitmentKey, FullPtrOut, PtrOut, PtrSource, RegistryKey, RegistrySptrKey};
26+
use spaces_ptr::{Commitment, CommitmentKey, FullPtrOut, PtrOut, PtrSource, RegistryKey, RegistrySptrKey, PtrOutpointKey};
2727
use spaces_ptr::sptr::Sptr;
2828
use crate::store::{EncodableOutpoint, Sha256};
2929

@@ -110,7 +110,7 @@ impl PtrStore {
110110
}
111111

112112
pub trait PtrChainState {
113-
fn insert_ptrout(&self, key: OutpointKey, ptrout: PtrOut);
113+
fn insert_ptrout(&self, key: PtrOutpointKey, ptrout: PtrOut);
114114
fn insert_commitment(&self, key: CommitmentKey, commitment: Commitment);
115115
fn insert_registry(&self, key: RegistryKey, state_root: Hash);
116116
fn insert_registry_delegation(&self, key: RegistrySptrKey, space: SLabel);
@@ -124,7 +124,7 @@ pub trait PtrChainState {
124124
}
125125

126126
impl PtrChainState for PtrLiveSnapshot {
127-
fn insert_ptrout(&self, key: OutpointKey, ptrout: PtrOut) {
127+
fn insert_ptrout(&self, key: PtrOutpointKey, ptrout: PtrOut) {
128128
self.insert(key, ptrout)
129129
}
130130

@@ -351,7 +351,7 @@ impl PtrSource for PtrLiveSnapshot {
351351
&mut self,
352352
outpoint: &OutPoint,
353353
) -> spaces_protocol::errors::Result<Option<PtrOut>> {
354-
let h = OutpointKey::from_outpoint::<Sha256>(*outpoint);
354+
let h = PtrOutpointKey::from_outpoint::<Sha256>(*outpoint);
355355
let result = self.get(h).map_err(|err| {
356356
spaces_protocol::errors::Error::IO(format!("getptrout: {}", err.to_string()))
357357
})?;

ptr/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub enum KeyKind {
143143
Sptr = 0x02,
144144
Registry = 0x03,
145145
RegistrySptr = 0x04,
146+
PtrOutpoint = 0x05,
146147
}
147148

148149
impl KeyKind {
@@ -175,9 +176,15 @@ pub struct RegistrySptrKey([u8; 32]);
175176
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
176177
pub struct CommitmentKey([u8; 32]);
177178

179+
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
180+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
181+
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
182+
pub struct PtrOutpointKey([u8; 32]);
183+
178184
impl KeyHash for RegistryKey {}
179185
impl KeyHash for RegistrySptrKey {}
180186
impl KeyHash for CommitmentKey {}
187+
impl KeyHash for PtrOutpointKey {}
181188

182189
impl Commitment {
183190
pub fn is_finalized(&self, height: u32) -> bool {
@@ -205,6 +212,21 @@ impl From<CommitmentKey> for Hash {
205212
}
206213
}
207214

215+
impl From<PtrOutpointKey> for Hash {
216+
fn from(value: PtrOutpointKey) -> Self {
217+
value.0
218+
}
219+
}
220+
221+
impl PtrOutpointKey {
222+
pub fn from_outpoint<H: KeyHasher>(outpoint: OutPoint) -> Self {
223+
let mut buffer = [0u8; 36];
224+
buffer[0..32].copy_from_slice(outpoint.txid.as_ref());
225+
buffer[32..36].copy_from_slice(&outpoint.vout.to_le_bytes());
226+
Self(ns_hash::<H>(KeyKind::PtrOutpoint, H::hash(&buffer)))
227+
}
228+
}
229+
208230
impl CommitmentKey {
209231
pub fn new<H: KeyHasher>(space: &SLabel, root: [u8; 32]) -> Self {
210232
let mut data = [0u8; 64];

0 commit comments

Comments
 (0)