Skip to content

Commit

Permalink
feat: improve ns-protocol crate
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Dec 22, 2023
1 parent 2a480e8 commit ec6e69f
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 124 deletions.
2 changes: 1 addition & 1 deletion crates/ns-fetcher/src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_stream::try_stream;
use bloomfilter::Bloom;
use futures_core::stream::Stream;

use ns_protocol::index::{Inscription, NameState, ServiceState};
use ns_protocol::state::{Inscription, NameState, ServiceState};

use crate::indexer::Client;

Expand Down
2 changes: 1 addition & 1 deletion crates/ns-fetcher/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use serde::{de::DeserializeOwned, Deserialize};
use tokio::time::{sleep, Duration};

use ns_protocol::{
index::{Inscription, NameState, ServiceState},
ns::Value,
state::{Inscription, NameState, ServiceState},
};

static APP_USER_AGENT: &str = concat!(
Expand Down
2 changes: 1 addition & 1 deletion crates/ns-indexer/src/api/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use axum_web::{
erring::{HTTPError, SuccessResponse},
object::PackObject,
};
use ns_protocol::index::{Inscription, InvalidInscription};
use ns_protocol::state::{Inscription, InvalidInscription};

use crate::api::{IndexerAPI, QueryHeight, QueryName, QueryNamePagination};
use crate::db;
Expand Down
2 changes: 1 addition & 1 deletion crates/ns-indexer/src/api/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use axum_web::{
erring::{HTTPError, SuccessResponse},
object::PackObject,
};
use ns_protocol::index::NameState;
use ns_protocol::state::NameState;

use crate::api::{IndexerAPI, QueryName, QueryPubkey};
use crate::db;
Expand Down
2 changes: 1 addition & 1 deletion crates/ns-indexer/src/api/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use axum_web::{
erring::{HTTPError, SuccessResponse},
object::PackObject,
};
use ns_protocol::index::ServiceState;
use ns_protocol::state::ServiceState;

use crate::api::{IndexerAPI, QueryName};
use crate::db;
Expand Down
30 changes: 15 additions & 15 deletions crates/ns-indexer/src/db/model_inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum_web::erring::HTTPError;
use scylla_orm::{ColumnsMap, CqlValue, ToCqlVal};
use scylla_orm_macros::CqlOrm;

use ns_protocol::index;
use ns_protocol::state;

use crate::db::{self, scylladb, scylladb::filter_single_row_err};

Expand Down Expand Up @@ -142,8 +142,8 @@ impl Inscription {
}
}

pub fn from_index(value: &index::Inscription) -> anyhow::Result<Self> {
let data = index::to_bytes(&value.data)?;
pub fn from_index(value: &state::Inscription) -> anyhow::Result<Self> {
let data = state::to_bytes(&value.data)?;
Ok(Self {
name: value.name.clone(),
sequence: value.sequence as i64,
Expand All @@ -161,9 +161,9 @@ impl Inscription {
})
}

pub fn to_index(&self) -> anyhow::Result<index::Inscription> {
let data = index::from_bytes(&self.data)?;
Ok(index::Inscription {
pub fn to_index(&self) -> anyhow::Result<state::Inscription> {
let data = state::from_bytes(&self.data)?;
Ok(state::Inscription {
name: self.name.clone(),
sequence: self.sequence as u64,
height: self.height as u64,
Expand Down Expand Up @@ -252,10 +252,10 @@ impl Inscription {
// save inscriptions and states in a block to db
pub async fn save_checkpoint(
db: &scylladb::ScyllaDB,
name_states: &Vec<index::NameState>,
service_states: &Vec<index::ServiceState>,
protocol_states: &Vec<index::ServiceProtocol>,
inscriptions: &Vec<index::Inscription>,
name_states: &Vec<state::NameState>,
service_states: &Vec<state::ServiceState>,
protocol_states: &Vec<state::ServiceProtocol>,
inscriptions: &Vec<state::Inscription>,
) -> anyhow::Result<()> {
let mut statements: Vec<&str> = Vec::with_capacity(1024);
let mut values: Vec<Vec<CqlValue>> = Vec::with_capacity(1024);
Expand Down Expand Up @@ -486,8 +486,8 @@ impl Inscription {
}

impl InvalidInscription {
pub fn from_index(value: &index::InvalidInscription) -> anyhow::Result<Self> {
let data = index::to_bytes(&value.data)?;
pub fn from_index(value: &state::InvalidInscription) -> anyhow::Result<Self> {
let data = state::to_bytes(&value.data)?;
Ok(Self {
name: value.name.clone(),
block_height: value.block_height as i64,
Expand All @@ -498,9 +498,9 @@ impl InvalidInscription {
})
}

pub fn to_index(&self) -> anyhow::Result<index::InvalidInscription> {
let data = index::from_bytes(&self.data)?;
Ok(index::InvalidInscription {
pub fn to_index(&self) -> anyhow::Result<state::InvalidInscription> {
let data = state::from_bytes(&self.data)?;
Ok(state::InvalidInscription {
name: self.name.clone(),
block_height: self.block_height as u64,
hash: self.hash.clone(),
Expand Down
8 changes: 4 additions & 4 deletions crates/ns-indexer/src/db/model_name_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use scylla_orm::{ColumnsMap, CqlValue, ToCqlVal};
use scylla_orm_macros::CqlOrm;
use std::collections::{BTreeMap, HashSet};

use ns_protocol::index;
use ns_protocol::state;

use crate::db::scylladb;

Expand Down Expand Up @@ -45,7 +45,7 @@ impl NameState {
}
}

pub fn from_index(value: &index::NameState) -> anyhow::Result<Self> {
pub fn from_index(value: &state::NameState) -> anyhow::Result<Self> {
Ok(Self {
name: value.name.clone(),
sequence: value.sequence as i64,
Expand All @@ -59,8 +59,8 @@ impl NameState {
})
}

pub fn to_index(&self) -> anyhow::Result<index::NameState> {
Ok(index::NameState {
pub fn to_index(&self) -> anyhow::Result<state::NameState> {
Ok(state::NameState {
name: self.name.clone(),
sequence: self.sequence as u64,
block_height: self.block_height as u64,
Expand Down
12 changes: 6 additions & 6 deletions crates/ns-indexer/src/db/model_service_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum_web::erring::HTTPError;
use scylla_orm::{ColumnsMap, ToCqlVal};
use scylla_orm_macros::CqlOrm;

use ns_protocol::index;
use ns_protocol::state;

use crate::db::scylladb;

Expand All @@ -26,8 +26,8 @@ impl ServiceProtocol {
}
}

pub fn from_index(value: &index::ServiceProtocol) -> anyhow::Result<Self> {
let protocol = index::to_bytes(&value.protocol)?;
pub fn from_index(value: &state::ServiceProtocol) -> anyhow::Result<Self> {
let protocol = state::to_bytes(&value.protocol)?;
Ok(Self {
code: value.code as i64,
version: value.version as i32,
Expand All @@ -38,9 +38,9 @@ impl ServiceProtocol {
})
}

pub fn to_index(&self) -> anyhow::Result<index::ServiceProtocol> {
let protocol = index::from_bytes(&self.protocol)?;
Ok(index::ServiceProtocol {
pub fn to_index(&self) -> anyhow::Result<state::ServiceProtocol> {
let protocol = state::from_bytes(&self.protocol)?;
Ok(state::ServiceProtocol {
code: self.code as u64,
version: self.version as u16,
protocol,
Expand Down
12 changes: 6 additions & 6 deletions crates/ns-indexer/src/db/model_service_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum_web::erring::HTTPError;
use scylla_orm::{ColumnsMap, ToCqlVal};
use scylla_orm_macros::CqlOrm;

use ns_protocol::index;
use ns_protocol::state;

use crate::db::scylladb;

Expand All @@ -25,8 +25,8 @@ impl ServiceState {
}
}

pub fn from_index(value: &index::ServiceState) -> anyhow::Result<Self> {
let data = index::to_bytes(&value.data)?;
pub fn from_index(value: &state::ServiceState) -> anyhow::Result<Self> {
let data = state::to_bytes(&value.data)?;
Ok(Self {
name: value.name.clone(),
code: value.code as i64,
Expand All @@ -36,9 +36,9 @@ impl ServiceState {
})
}

pub fn to_index(&self) -> anyhow::Result<index::ServiceState> {
let data = index::from_bytes(&self.data)?;
Ok(index::ServiceState {
pub fn to_index(&self) -> anyhow::Result<state::ServiceState> {
let data = state::from_bytes(&self.data)?;
Ok(state::ServiceState {
name: self.name.clone(),
code: self.code as u64,
sequence: self.sequence as u64,
Expand Down
11 changes: 8 additions & 3 deletions crates/ns-indexer/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ mod tests {
use bitcoin::blockdata::script::{Builder, PushBytesBuf};
use ciborium::Value;
use hex_literal::hex;
use ns_protocol::ns::{Operation, PublicKeyParams, Service, ThresholdLevel};
use ns_protocol::{
ed25519,
ns::{Operation, PublicKeyParams, Service, ThresholdLevel},
};

#[test]
fn names_from_witness() {
Expand All @@ -89,6 +92,8 @@ mod tests {
threshold: Some(1),
kind: None,
};
let signer = ed25519::SigningKey::try_from(&secret_key).unwrap();
let signers = vec![signer];

let mut name1 = Name {
name: "a".to_string(),
Expand All @@ -104,7 +109,7 @@ mod tests {
signatures: vec![],
};
name1
.sign(&params, ThresholdLevel::Default, &[secret_key.to_vec()])
.sign(&params, ThresholdLevel::Default, &signers)
.unwrap();
assert!(name1.validate().is_ok());

Expand All @@ -122,7 +127,7 @@ mod tests {
signatures: vec![],
};
name2
.sign(&params, ThresholdLevel::Default, &[secret_key.to_vec()])
.sign(&params, ThresholdLevel::Default, &signers)
.unwrap();
assert!(name2.validate().is_ok());

Expand Down
2 changes: 1 addition & 1 deletion crates/ns-indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::{
use tokio::sync::RwLock;

use ns_protocol::{
index::{hash_sha3, Inscription, InvalidInscription, NameState, ServiceProtocol, ServiceState},
ns::{Name, PublicKeyParams, ThresholdLevel},
state::{hash_sha3, Inscription, InvalidInscription, NameState, ServiceProtocol, ServiceState},
};

use crate::db::{
Expand Down
14 changes: 4 additions & 10 deletions crates/ns-inscriber/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ async fn main() -> anyhow::Result<()> {
kind: None,
};

let signers = vec![signing_key];
let mut ns: Vec<Name> = Vec::with_capacity(names.len());
for name in &names {
let mut n = Name {
Expand All @@ -464,11 +465,7 @@ async fn main() -> anyhow::Result<()> {
},
signatures: vec![],
};
n.sign(
&params,
ThresholdLevel::All,
&[signing_key.as_bytes().to_vec()],
)?;
n.sign(&params, ThresholdLevel::All, &signers)?;
n.validate()?;
ns.push(n);
}
Expand Down Expand Up @@ -521,6 +518,7 @@ async fn main() -> anyhow::Result<()> {
kind: None,
};

let signers = vec![signing_key];
let mut ns: Vec<Name> = Vec::with_capacity(names.len());
for name in &names {
let mut n = Name {
Expand All @@ -536,11 +534,7 @@ async fn main() -> anyhow::Result<()> {
},
signatures: vec![],
};
n.sign(
&params,
ThresholdLevel::All,
&[signing_key.as_bytes().to_vec()],
)?;
n.sign(&params, ThresholdLevel::All, &signers)?;
n.validate()?;
ns.push(n);
}
Expand Down
10 changes: 8 additions & 2 deletions crates/ns-inscriber/src/inscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,10 @@ mod tests {
use serde_json::to_value;

use ns_indexer::envelope::Envelope;
use ns_protocol::ns::{Operation, PublicKeyParams, Service, ThresholdLevel, Value};
use ns_protocol::{
ed25519,
ns::{Operation, PublicKeyParams, Service, ThresholdLevel, Value},
};

fn get_name(name: &str) -> Name {
let secret_key = hex!("7ef3811aabb916dc2f646ef1a371b90adec91bc07992cd4d44c156c42fc1b300");
Expand All @@ -574,6 +577,9 @@ mod tests {
kind: None,
};

let signer = ed25519::SigningKey::try_from(&secret_key).unwrap();
let signers = vec![signer];

let mut name = Name {
name: name.to_string(),
sequence: 0,
Expand All @@ -587,7 +593,7 @@ mod tests {
},
signatures: vec![],
};
name.sign(&params, ThresholdLevel::Default, &[secret_key.to_vec()])
name.sign(&params, ThresholdLevel::Default, &signers)
.unwrap();
assert!(name.validate().is_ok());
name
Expand Down
2 changes: 1 addition & 1 deletion crates/ns-protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ns-protocol"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
rust-version = "1.64"
description = "Name & Service Protocol in Rust"
Expand Down
1 change: 1 addition & 0 deletions crates/ns-protocol/src/ed25519.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
3 changes: 2 additions & 1 deletion crates/ns-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod index;
pub mod ed25519;
pub mod ns;
pub mod state;
Loading

0 comments on commit ec6e69f

Please sign in to comment.