Skip to content

Commit

Permalink
chore: rename Name::payload to Name::service
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Dec 28, 2023
1 parent efdf6c0 commit 83d9828
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 60 deletions.
2 changes: 1 addition & 1 deletion crates/ns-fetcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "CC0-1.0"
[lib]

[dependencies]
ns-protocol = { path = "../ns-protocol", version = "0.5" }
ns-protocol = { path = "../ns-protocol", version = "0.6" }
anyhow = { workspace = true }
bytes = { workspace = true }
base64 = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-fetcher/src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn fetch_desc(
try_stream! {
let last_accepted: Inscription = cli.get_last_accepted_inscription().await?;
let name_state: NameState = cli.get_name_state(&last_accepted.name).await?;
let service_state: ServiceState = cli.get_service_state(&last_accepted.name, last_accepted.data.payload.code).await?;
let service_state: ServiceState = cli.get_service_state(&last_accepted.name, last_accepted.data.service.code).await?;

let mut bloom = Bloom::new_for_fp_rate(last_accepted.height as usize, 0.0001);
let mut head_height = last_accepted.height;
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn fetch_desc(
}

let name_state: NameState = cli.get_name_state(&inscription.name).await?;
let service_state: ServiceState = cli.get_service_state(&inscription.name, inscription.data.payload.code).await?;
let service_state: ServiceState = cli.get_service_state(&inscription.name, inscription.data.service.code).await?;

bloom.set(&head_inscription.name);
// false positives are possible from bloom, but it's ok
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-indexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ns-indexer"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
rust-version = "1.64"
description = "Name & Service Protocol indexer service in Rust"
Expand All @@ -15,7 +15,7 @@ name = "ns-indexer"
path = "src/bin/main.rs"

[dependencies]
ns-protocol = { path = "../ns-protocol", version = "0.5" }
ns-protocol = { path = "../ns-protocol", version = "0.6" }
ns-axum-web = { path = "../ns-axum-web", version = "0.1" }
ns-scylla-orm = { path = "../ns-scylla-orm", version = "0.1" }
ns-scylla-orm-macros = { path = "../ns-scylla-orm-macros", version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-indexer/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod tests {
let mut name1 = Name {
name: "a".to_string(),
sequence: 0,
payload: Service {
service: Service {
code: 0,
operations: vec![Operation {
subcode: 1,
Expand All @@ -116,7 +116,7 @@ mod tests {
let mut name2 = Name {
name: "aa".to_string(),
sequence: 0,
payload: Service {
service: Service {
code: 0,
operations: vec![Operation {
subcode: 1,
Expand Down
28 changes: 14 additions & 14 deletions crates/ns-indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,23 +219,23 @@ impl Indexer {
name.validate()?;
// default protocol is Name service
let mut service_protocol = ServiceProtocol::default();
if name.payload.code > 0 {
if name.service.code > 0 {
// try to get latest service protocol from db
let protocol =
db::ServiceProtocol::get_latest(&self.scylla, name.payload.code as i64, vec![])
db::ServiceProtocol::get_latest(&self.scylla, name.service.code as i64, vec![])
.await
.map_err(|err| {
anyhow::anyhow!(
"failed to get latest service protocol, code: {}, err: {}",
name.payload.code,
name.service.code,
err
)
})?;
service_protocol = protocol.to_index()?;
};
service_protocol.validate(&name.payload)?;
service_protocol.validate(&name.service)?;

if let Some(ref approver) = name.payload.approver {
if let Some(ref approver) = name.service.approver {
let mut approver_state = db::NameState::with_pk(approver.clone());
approver_state
.get_one(&self.scylla, vec![])
Expand All @@ -259,7 +259,7 @@ impl Indexer {
let prev_service_state = names
.iter()
.filter_map(|(_, service_state, _)| {
if service_state.code == name.payload.code {
if service_state.code == name.service.code {
Some(service_state)
} else {
None
Expand All @@ -283,7 +283,7 @@ impl Indexer {
if prev_state.0.is_some() && prev_state.1.is_none() {
// try to get accepted state from db
let mut service_state =
db::ServiceState::with_pk(name.name.clone(), name.payload.code as i64);
db::ServiceState::with_pk(name.name.clone(), name.service.code as i64);
if service_state.get_one(&self.scylla, vec![]).await.is_ok() {
prev_state.1 = Some(service_state.to_index()?);
}
Expand All @@ -297,10 +297,10 @@ impl Indexer {
} else {
ServiceState {
name: name.name.clone(),
code: name.payload.code,
code: name.service.code,
sequence: name.sequence,
data: name
.payload
.service
.operations
.iter()
.map(|op| (op.subcode, op.params.clone()))
Expand Down Expand Up @@ -330,19 +330,19 @@ impl Indexer {
name.sequence
);
}
if name.payload.code != 0 {
if name.service.code != 0 {
anyhow::bail!(
"invalid code for new name, expected: 0, got: {}",
name.payload.code
name.service.code
);
}
if name.payload.operations.len() != 1 {
if name.service.operations.len() != 1 {
anyhow::bail!(
"invalid operations length for new name, expected: 1, got: {}",
name.payload.operations.len()
name.service.operations.len()
);
}
let op = &name.payload.operations[0];
let op = &name.service.operations[0];
if op.subcode != 1 {
anyhow::bail!(
"invalid operation subcode for new name, expected: 1, got: {}",
Expand Down
4 changes: 2 additions & 2 deletions crates/ns-inscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ name = "ns-inscriber"
path = "src/bin/main.rs"

[dependencies]
ns-protocol = { path = "../ns-protocol", version = "0.5" }
ns-indexer = { path = "../ns-indexer", version = "0.2" }
ns-protocol = { path = "../ns-protocol", version = "0.6" }
ns-indexer = { path = "../ns-indexer", version = "0.3" }
anyhow = { workspace = true }
bytes = { workspace = true }
base64 = { workspace = true }
Expand Down
14 changes: 10 additions & 4 deletions crates/ns-inscriber/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ async fn main() -> anyhow::Result<()> {
let mut n = Name {
name: name.clone(),
sequence: 0,
payload: Service {
service: Service {
code: 0,
operations: vec![Operation {
subcode: 1,
Expand All @@ -472,6 +472,7 @@ async fn main() -> anyhow::Result<()> {

let res = Inscriber::preview_inscription_transactions(&ns, fee_rate)?;

println!("inscriptions: {}", ns.len());
println!(
"commit_tx: {} bytes, {} vBytes",
res.0.total_size(),
Expand All @@ -482,8 +483,13 @@ async fn main() -> anyhow::Result<()> {
res.1.total_size(),
res.1.vsize()
);
println!("total bytes: {}", res.0.total_size() + res.1.total_size());
println!("min cost: {}", res.2);
println!(
"total bytes: {}, {} vBytes",
res.0.total_size() + res.1.total_size(),
res.0.vsize() + res.1.vsize()
);
println!("estimate fee: {}", res.2 - res.1.output[0].value);
println!("min balance (fee + min change): {}", res.2);
return Ok(());
}

Expand Down Expand Up @@ -524,7 +530,7 @@ async fn main() -> anyhow::Result<()> {
let mut n = Name {
name: name.clone(),
sequence: 0,
payload: Service {
service: Service {
code: 0,
operations: vec![Operation {
subcode: 1,
Expand Down
2 changes: 1 addition & 1 deletion crates/ns-inscriber/src/inscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ mod tests {
let mut name = Name {
name: name.to_string(),
sequence: 0,
payload: Service {
service: Service {
code: 0,
operations: vec![Operation {
subcode: 1,
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.5.0"
version = "0.6.0"
edition = "2021"
rust-version = "1.64"
description = "Name & Service Protocol in Rust"
Expand Down
22 changes: 11 additions & 11 deletions crates/ns-protocol/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) const NS_PREFIX: [u8; 3] = [0xd8, 0x35, 0x84]; // d835: tag(53), 84:
pub struct Name {
pub name: String,
pub sequence: u64,
pub payload: Service,
pub service: Service,
pub signatures: Vec<Signature>,
}

Expand Down Expand Up @@ -202,7 +202,7 @@ impl Name {
let arr = Value::Array(vec![
Value::from(self.name.clone()),
Value::from(self.sequence),
Value::from(&self.payload),
Value::from(&self.service),
]);
let mut buf: Vec<u8> = Vec::new();
into_writer(&arr, &mut buf).map_err(|err| Error::Custom(err.to_string()))?;
Expand All @@ -225,15 +225,15 @@ impl Name {
)));
}

if self.payload.code > i64::MAX as u64 {
if self.service.code > i64::MAX as u64 {
return Err(Error::Custom(format!(
"Name: invalid payload code {}, expected less than {}",
self.payload.code,
"Name: invalid service code {}, expected less than {}",
self.service.code,
i64::MAX
)));
}

if let Some(approver) = &self.payload.approver {
if let Some(approver) = &self.service.approver {
if !valid_name(approver) {
return Err(Error::Custom(format!(
"Name: invalid approver {}",
Expand All @@ -242,7 +242,7 @@ impl Name {
}
}

if self.payload.operations.is_empty() {
if self.service.operations.is_empty() {
return Err(Error::Custom("Name: missing operations".to_string()));
}

Expand Down Expand Up @@ -405,7 +405,7 @@ impl From<&Name> for Value {
Value::Array(vec![
Value::from(name.name.clone()),
Value::from(name.sequence),
Value::from(&name.payload),
Value::from(&name.service),
Value::Array(name.signatures.iter().map(Value::from).collect()),
])
}
Expand Down Expand Up @@ -634,7 +634,7 @@ impl TryFrom<&Value> for Name {
.map_err(|err| {
Error::Custom(format!("Name: expected u64, error: {:?}", err))
})?,
payload: Service::try_from(&arr[2])?,
service: Service::try_from(&arr[2])?,
signatures: arr[3]
.as_array()
.ok_or_else(|| {
Expand Down Expand Up @@ -829,7 +829,7 @@ mod tests {
let mut name = Name {
name: "a".to_string(),
sequence: 0,
payload: Service {
service: Service {
code: 0,
operations: vec![Operation {
subcode: 1,
Expand Down Expand Up @@ -891,7 +891,7 @@ mod tests {
let mut name = Name {
name: "道".to_string(),
sequence: 0,
payload: Service {
service: Service {
code: 0,
operations: vec![Operation {
subcode: 1,
Expand Down
Loading

0 comments on commit 83d9828

Please sign in to comment.