Skip to content

Commit 13139f0

Browse files
authored
Merge pull request #25 from buffrr/structure-update
Structure update
2 parents 8070608 + 9aed182 commit 13139f0

File tree

16 files changed

+721
-715
lines changed

16 files changed

+721
-715
lines changed

node/src/bin/space-cli.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use jsonrpsee::{
1010
};
1111
use protocol::{
1212
bitcoin::{Amount, FeeRate, OutPoint, Txid},
13-
hasher::{KeyHasher, SpaceHash},
13+
hasher::{KeyHasher, SpaceKey},
1414
opcodes::OP_SETALL,
1515
sname::{NameLike, SName},
1616
Covenant, FullSpaceOut,
@@ -372,7 +372,7 @@ async fn main() -> anyhow::Result<()> {
372372
fn space_hash(spaceish: &str) -> anyhow::Result<String> {
373373
let space = normalize_space(&spaceish);
374374
let sname = SName::from_str(&space)?;
375-
let spacehash = SpaceHash::from(Sha256::hash(sname.to_bytes()));
375+
let spacehash = SpaceKey::from(Sha256::hash(sname.to_bytes()));
376376
Ok(hex::encode(spacehash.as_slice()))
377377
}
378378

@@ -394,7 +394,13 @@ async fn handle_commands(
394394

395395
if let Some(outpoint) = outpoint {
396396
if let Some(spaceout) = cli.client.get_spaceout(outpoint).await? {
397-
spaceouts.push((priority, FullSpaceOut { outpoint, spaceout }));
397+
spaceouts.push((
398+
priority,
399+
FullSpaceOut {
400+
txid: outpoint.txid,
401+
spaceout,
402+
},
403+
));
398404
}
399405
}
400406
}

node/src/bin/spaced.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use log::error;
66
use spaced::{
77
config::{safe_exit, Args},
88
rpc::{AsyncChainState, LoadedWallet, RpcServerImpl, WalletManager},
9-
source::BitcoinBlockSource,
9+
source::{BitcoinBlockSource, BitcoinRpc},
1010
store,
1111
sync::Spaced,
1212
wallets::RpcWallet,
@@ -82,6 +82,7 @@ impl Composer {
8282
};
8383

8484
let (async_chain_state, async_chain_state_handle) = create_async_store(
85+
spaced.rpc.clone(),
8586
spaced.chain.state.clone(),
8687
spaced.block_index.as_ref().map(|index| index.state.clone()),
8788
self.shutdown.subscribe(),
@@ -140,15 +141,16 @@ impl Composer {
140141
}
141142

142143
async fn create_async_store(
144+
rpc: BitcoinRpc,
143145
chain_state: LiveSnapshot,
144146
block_index: Option<LiveSnapshot>,
145147
shutdown: broadcast::Receiver<()>,
146148
) -> (AsyncChainState, JoinHandle<()>) {
147149
let (tx, rx) = mpsc::channel(32);
148150
let async_store = AsyncChainState::new(tx);
149-
151+
let client = reqwest::Client::new();
150152
let handle = tokio::spawn(async move {
151-
AsyncChainState::handler(chain_state, block_index, rx, shutdown).await
153+
AsyncChainState::handler(&client, rpc, chain_state, block_index, rx, shutdown).await
152154
});
153155
(async_store, handle)
154156
}

node/src/config.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,41 @@ impl Args {
159159
bitcoin_rpc_auth,
160160
);
161161

162-
std::fs::create_dir_all(data_dir.clone())?;
162+
fs::create_dir_all(data_dir.clone())?;
163163

164-
let chain_store = Store::open(data_dir.join("protocol.sdb"))?;
164+
let proto_db_path = data_dir.join("protocol.sdb");
165+
let initial_sync = !proto_db_path.exists();
166+
167+
let chain_store = Store::open(proto_db_path)?;
165168
let chain = LiveStore {
166169
state: chain_store.begin(&genesis)?,
167170
store: chain_store,
168171
};
169172

170-
let block_index = match args.block_index {
171-
true => {
172-
let block_store = Store::open(data_dir.join("blocks.sdb"))?;
173-
Some(LiveStore {
174-
state: block_store.begin(&genesis).expect("begin block index"),
175-
store: block_store,
176-
})
173+
let block_index = if args.block_index {
174+
let block_db_path = data_dir.join("block_index.sdb");
175+
if !initial_sync && !block_db_path.exists() {
176+
return Err(anyhow::anyhow!(
177+
"Block index must be enabled from the initial sync."
178+
));
177179
}
178-
false => None,
180+
let block_store = Store::open(block_db_path)?;
181+
let index = LiveStore {
182+
state: block_store.begin(&genesis).expect("begin block index"),
183+
store: block_store,
184+
};
185+
{
186+
let tip_1 = index.state.tip.read().expect("index");
187+
let tip_2 = chain.state.tip.read().expect("tip");
188+
if tip_1.height != tip_2.height || tip_1.hash != tip_2.hash {
189+
return Err(anyhow::anyhow!(
190+
"Protocol and block index states don't match."
191+
));
192+
}
193+
}
194+
Some(index)
195+
} else {
196+
None
179197
};
180198

181199
Ok(Spaced {

0 commit comments

Comments
 (0)