From 83d9828d93333ab43cabc12fd52885f79806179f Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Thu, 28 Dec 2023 18:38:06 +0800 Subject: [PATCH] chore: rename Name::payload to Name::service --- crates/ns-fetcher/Cargo.toml | 2 +- crates/ns-fetcher/src/fetcher.rs | 4 +-- crates/ns-indexer/Cargo.toml | 4 +-- crates/ns-indexer/src/envelope.rs | 4 +-- crates/ns-indexer/src/indexer.rs | 28 +++++++++---------- crates/ns-inscriber/Cargo.toml | 4 +-- crates/ns-inscriber/src/bin/main.rs | 14 +++++++--- crates/ns-inscriber/src/inscriber.rs | 2 +- crates/ns-protocol/Cargo.toml | 2 +- crates/ns-protocol/src/ns.rs | 22 +++++++-------- crates/ns-protocol/src/state.rs | 40 ++++++++++++++-------------- 11 files changed, 66 insertions(+), 60 deletions(-) diff --git a/crates/ns-fetcher/Cargo.toml b/crates/ns-fetcher/Cargo.toml index a9313f4..d318b81 100644 --- a/crates/ns-fetcher/Cargo.toml +++ b/crates/ns-fetcher/Cargo.toml @@ -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 } diff --git a/crates/ns-fetcher/src/fetcher.rs b/crates/ns-fetcher/src/fetcher.rs index 1107b3a..a3f1b7d 100644 --- a/crates/ns-fetcher/src/fetcher.rs +++ b/crates/ns-fetcher/src/fetcher.rs @@ -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; @@ -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 diff --git a/crates/ns-indexer/Cargo.toml b/crates/ns-indexer/Cargo.toml index 0837818..e1dffcf 100644 --- a/crates/ns-indexer/Cargo.toml +++ b/crates/ns-indexer/Cargo.toml @@ -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" @@ -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" } diff --git a/crates/ns-indexer/src/envelope.rs b/crates/ns-indexer/src/envelope.rs index 8a065e4..0d45a7a 100644 --- a/crates/ns-indexer/src/envelope.rs +++ b/crates/ns-indexer/src/envelope.rs @@ -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, @@ -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, diff --git a/crates/ns-indexer/src/indexer.rs b/crates/ns-indexer/src/indexer.rs index 6b39a45..1cf999f 100644 --- a/crates/ns-indexer/src/indexer.rs +++ b/crates/ns-indexer/src/indexer.rs @@ -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![]) @@ -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 @@ -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()?); } @@ -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())) @@ -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: {}", diff --git a/crates/ns-inscriber/Cargo.toml b/crates/ns-inscriber/Cargo.toml index 0a92e1e..1a66b70 100644 --- a/crates/ns-inscriber/Cargo.toml +++ b/crates/ns-inscriber/Cargo.toml @@ -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 } diff --git a/crates/ns-inscriber/src/bin/main.rs b/crates/ns-inscriber/src/bin/main.rs index cc732bb..c6f3de8 100644 --- a/crates/ns-inscriber/src/bin/main.rs +++ b/crates/ns-inscriber/src/bin/main.rs @@ -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, @@ -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(), @@ -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(()); } @@ -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, diff --git a/crates/ns-inscriber/src/inscriber.rs b/crates/ns-inscriber/src/inscriber.rs index 1d4a6c4..83a6aae 100644 --- a/crates/ns-inscriber/src/inscriber.rs +++ b/crates/ns-inscriber/src/inscriber.rs @@ -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, diff --git a/crates/ns-protocol/Cargo.toml b/crates/ns-protocol/Cargo.toml index 00a6541..945c28c 100644 --- a/crates/ns-protocol/Cargo.toml +++ b/crates/ns-protocol/Cargo.toml @@ -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" diff --git a/crates/ns-protocol/src/ns.rs b/crates/ns-protocol/src/ns.rs index 24756bc..44973bf 100644 --- a/crates/ns-protocol/src/ns.rs +++ b/crates/ns-protocol/src/ns.rs @@ -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, } @@ -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 = Vec::new(); into_writer(&arr, &mut buf).map_err(|err| Error::Custom(err.to_string()))?; @@ -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 {}", @@ -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())); } @@ -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()), ]) } @@ -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(|| { @@ -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, @@ -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, diff --git a/crates/ns-protocol/src/state.rs b/crates/ns-protocol/src/state.rs index 9cdb17e..3c3cbd7 100644 --- a/crates/ns-protocol/src/state.rs +++ b/crates/ns-protocol/src/state.rs @@ -56,7 +56,7 @@ impl NameState { ))); } - if next.payload.code != 0 { + if next.service.code != 0 { next.verify(&self.public_key_params(), ThresholdLevel::Default)?; return Ok(NameState { name: next.name.clone(), @@ -74,7 +74,7 @@ impl NameState { // handle the `0` service code (Name service) let mut next_state = self.clone(); - if next.payload.operations.len() == 1 && next.payload.operations[0].subcode == 0 { + if next.service.operations.len() == 1 && next.service.operations[0].subcode == 0 { // This is the lightweight update operation next.verify(&next_state.public_key_params(), ThresholdLevel::Default)?; next_state.sequence = next.sequence; @@ -86,7 +86,7 @@ impl NameState { return Ok(next_state); } - for op in &next.payload.operations { + for op in &next.service.operations { let public_key_params = PublicKeyParams::try_from(&op.params)?; public_key_params.validate()?; match op.subcode { @@ -169,20 +169,20 @@ impl ServiceState { ))); } - if next.payload.code != self.code { + if next.service.code != self.code { return Err(Error::Custom(format!( "invalid service code, expected: {}, got: {}", - self.code, next.payload.code + self.code, next.service.code ))); } let mut next_state = ServiceState { name: next.name.clone(), - code: next.payload.code, + code: next.service.code, sequence: next.sequence, data: self.data.clone(), }; - for op in &next.payload.operations { + for op in &next.service.operations { if let Some(i) = next_state.data.iter().position(|v| v.0 == op.subcode) { // default to replace operation // we should support other operations in the future @@ -336,7 +336,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 1, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 1, @@ -370,7 +370,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 1, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 2, @@ -419,7 +419,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 2, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 1, @@ -487,7 +487,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 3, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ ns::Operation { @@ -545,7 +545,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 4, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 1, @@ -591,7 +591,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 5, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { // this operation will be overwritten @@ -623,7 +623,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 6, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 0, @@ -660,7 +660,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 7, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { // this operation will be overwritten @@ -692,7 +692,7 @@ mod tests { let mut next_name = ns::Name { name: "test".to_string(), sequence: 8, - payload: ns::Service { + service: ns::Service { code: 123, operations: vec![ns::Operation { subcode: 0, @@ -737,7 +737,7 @@ mod tests { let next_name = ns::Name { name: "test".to_string(), sequence: 1, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 0, @@ -757,7 +757,7 @@ mod tests { let next_name = ns::Name { name: "test".to_string(), sequence: 2, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 0, @@ -780,7 +780,7 @@ mod tests { let next_name = ns::Name { name: "test".to_string(), sequence: 3, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 3, @@ -805,7 +805,7 @@ mod tests { let next_name = ns::Name { name: "test".to_string(), sequence: 4, - payload: ns::Service { + service: ns::Service { code: 0, operations: vec![ns::Operation { subcode: 2,