Skip to content

Commit

Permalink
feat: improve state structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Dec 24, 2023
1 parent 9715caf commit 4856955
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 33 deletions.
10 changes: 1 addition & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@ members = ["crates/*"]
[workspace.dependencies]
anyhow = "1"
async-trait = "0.1"
axum = { version = "0.7", features = [
"http1",
"http2",
"json",
"macros",
"matched-path",
"tokio",
"query",
], default-features = true }
bytes = "1"
base64 = "0.21"
ciborium = "0.2"
ciborium-io = "0.2"
ed25519-dalek = "2"
futures = "0.3"
libflate = "1"
log = "0.4"
mime = "0.3"
Expand Down
10 changes: 9 additions & 1 deletion crates/ns-axum-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ license = "CC0-1.0"

[dependencies]
anyhow = { workspace = true }
axum = { workspace = true }
async-trait = { workspace = true }
bytes = { workspace = true }
base64 = { workspace = true }
Expand All @@ -25,6 +24,15 @@ serde = { workspace = true }
serde_json = { workspace = true }
structured-logger = { workspace = true }
tokio = { workspace = true }
axum = { version = "0.7", features = [
"http1",
"http2",
"json",
"macros",
"matched-path",
"tokio",
"query",
], default-features = true }
scylla = "0.11"
zstd = "0.12"
validator = { version = "0.16", features = ["derive"] }
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.4" }
ns-protocol = { path = "../ns-protocol", version = "0.5" }
anyhow = { workspace = true }
bytes = { workspace = true }
base64 = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions crates/ns-fetcher/src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub type InscriptionState = (
Option<(NameState, ServiceState, Option<ServiceProtocol>)>,
);

// fetches all inscriptions and states from last accepted to bottom_height
// fetches all inscriptions and states from last accepted to bottom_height.
// The lowest height is 1 (the first inscription).
pub fn fetch_desc(
cli: Client,
bottom_height: u64,
Expand All @@ -29,7 +30,7 @@ pub fn fetch_desc(
yield (last_accepted, Some((name_state, service_state, None)));

loop {
if head_height == 0 || head_height < bottom_height {
if head_height <= 1 || head_height < bottom_height {
break;
}

Expand Down Expand Up @@ -93,7 +94,7 @@ mod tests {

let cli = Client::new(&ClientOptions { endpoint }).await.unwrap();

let s = fetch_desc(cli, 0);
let s = fetch_desc(cli, 1);
pin_mut!(s); // needed for iteration

// first item is always the last accepted inscription
Expand Down
16 changes: 12 additions & 4 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.1.0"
version = "0.2.0"
edition = "2021"
rust-version = "1.64"
description = "Name & Service Protocol indexer service in Rust"
Expand All @@ -15,12 +15,11 @@ name = "ns-indexer"
path = "src/bin/main.rs"

[dependencies]
ns-protocol = { path = "../ns-protocol", version = "0.4" }
ns-protocol = { path = "../ns-protocol", version = "0.5" }
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" }
anyhow = { workspace = true }
axum = { workspace = true }
bytes = { workspace = true }
base64 = { workspace = true }
ciborium = { workspace = true }
Expand All @@ -30,7 +29,16 @@ tokio = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
structured-logger = { workspace = true }
futures = "0.3"
futures = { workspace = true }
axum = { version = "0.7", features = [
"http1",
"http2",
"json",
"macros",
"matched-path",
"tokio",
"query",
], default-features = true }
reqwest = { version = "0.11", features = [
"rustls-tls",
"rustls-tls-webpki-roots",
Expand Down
3 changes: 3 additions & 0 deletions crates/ns-indexer/cql/schema.cql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CREATE TABLE IF NOT EXISTS name_state (
sequence BIGINT, -- name's latest sequence
block_height BIGINT, -- latest update at block height
block_time BIGINT, -- latest update at block time
stale_time BIGINT, -- stale time
expire_time BIGINT, -- expire time
threshold TINYINT, -- verifing threshold
key_kind TINYINT, -- int8, 0: Ed25519
public_keys LIST<BLOB>, -- public keys
Expand Down Expand Up @@ -70,6 +72,7 @@ CREATE TABLE IF NOT EXISTS inscription (
name TEXT, -- unique name
sequence BIGINT, -- name's updating sequence
height BIGINT, -- inscription's global height
name_height BIGINT, -- global name's counter
previous_hash BLOB, -- previous inscription hash
name_hash BLOB, -- current name state hash
service_hash BLOB, -- current service state hash
Expand Down
3 changes: 3 additions & 0 deletions crates/ns-indexer/src/db/model_inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Inscription {
pub name: String,
pub sequence: i64,
pub height: i64,
pub name_height: i64,
pub previous_hash: Vec<u8>,
pub name_hash: Vec<u8>,
pub service_hash: Vec<u8>,
Expand Down Expand Up @@ -148,6 +149,7 @@ impl Inscription {
name: value.name.clone(),
sequence: value.sequence as i64,
height: value.height as i64,
name_height: value.name_height as i64,
previous_hash: value.previous_hash.clone(),
name_hash: value.name_hash.clone(),
service_hash: value.service_hash.clone(),
Expand All @@ -167,6 +169,7 @@ impl Inscription {
name: self.name.clone(),
sequence: self.sequence as u64,
height: self.height as u64,
name_height: self.name_height as u64,
previous_hash: self.previous_hash.clone(),
name_hash: self.name_hash.clone(),
service_hash: self.service_hash.clone(),
Expand Down
6 changes: 6 additions & 0 deletions crates/ns-indexer/src/db/model_name_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct NameState {
pub sequence: i64,
pub block_height: i64,
pub block_time: i64,
pub stale_time: i64,
pub expire_time: i64,
pub threshold: i8,
pub key_kind: i8,
pub public_keys: Vec<Vec<u8>>,
Expand Down Expand Up @@ -51,6 +53,8 @@ impl NameState {
sequence: value.sequence as i64,
block_height: value.block_height as i64,
block_time: value.block_time as i64,
stale_time: value.stale_time as i64,
expire_time: value.expire_time as i64,
threshold: value.threshold as i8,
key_kind: value.key_kind as i8,
public_keys: value.public_keys.clone(),
Expand All @@ -65,6 +69,8 @@ impl NameState {
sequence: self.sequence as u64,
block_height: self.block_height as u64,
block_time: self.block_time as u64,
stale_time: self.stale_time as u64,
expire_time: self.expire_time as u64,
threshold: self.threshold as u8,
key_kind: self.key_kind as u8,
public_keys: self.public_keys.clone(),
Expand Down
21 changes: 20 additions & 1 deletion crates/ns-indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use tokio::sync::RwLock;

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

use crate::db::{
Expand Down Expand Up @@ -113,6 +116,7 @@ impl Indexer {

for envelope in Envelope::from_transaction(&tx) {
for name in envelope.payload {
let is_new_name = name.sequence == 0;
match self.index_name(block_height, block_time, &name).await {
Err(err) => {
if !name.name.is_empty() {
Expand Down Expand Up @@ -141,6 +145,7 @@ impl Indexer {
name: name.name.clone(),
sequence: name.sequence,
height: 0,
name_height: 0,
previous_hash: vec![],
name_hash: name_state_hash,
service_hash: service_state_hash,
Expand All @@ -158,19 +163,31 @@ impl Indexer {
match best_inscriptions_state.back() {
Some(prev_best_inscription) => {
inscription.height = prev_best_inscription.height + 1;
inscription.name_height = if is_new_name {
prev_best_inscription.name_height + 1
} else {
prev_best_inscription.name_height
};
inscription.previous_hash = prev_best_inscription
.hash()
.expect("hash_sha3(inscription) should not fail");
}
None => match *self.state.last_accepted.read().await {
Some(ref last_accepted_state) => {
inscription.height = last_accepted_state.height + 1;
inscription.name_height = if is_new_name {
last_accepted_state.name_height + 1
} else {
last_accepted_state.name_height
};
inscription.previous_hash = last_accepted_state
.hash()
.expect("hash_sha3(inscription) should not fail");
}
None => {
// this is the first inscription
inscription.height = 1;
inscription.name_height = 1;
inscription.previous_hash = [0u8; 32].to_vec();
}
},
Expand Down Expand Up @@ -340,6 +357,8 @@ impl Indexer {
sequence: 0,
block_height,
block_time,
stale_time: block_time + NAME_STALE_SECONDS,
expire_time: block_time + NAME_EXPIRE_SECONDS,
threshold: public_key_params
.threshold
.unwrap_or(public_key_params.public_keys.len() as u8),
Expand Down
8 changes: 4 additions & 4 deletions crates/ns-inscriber/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ns-inscriber"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
rust-version = "1.64"
description = "Name & Service Protocol inscriber service in Rust"
Expand All @@ -15,8 +15,8 @@ name = "ns-inscriber"
path = "src/bin/main.rs"

[dependencies]
ns-protocol = { path = "../ns-protocol", version = "0.4" }
ns-indexer = { path = "../ns-indexer", version = "0.1" }
ns-protocol = { path = "../ns-protocol", version = "0.5" }
ns-indexer = { path = "../ns-indexer", version = "0.2" }
anyhow = { workspace = true }
bytes = { workspace = true }
base64 = { workspace = true }
Expand All @@ -28,7 +28,7 @@ serde_json = { workspace = true }
log = { workspace = true }
structured-logger = { workspace = true }
ed25519-dalek = { workspace = true }
futures = "0.3"
futures = { workspace = true }
reqwest = { version = "0.11", features = [
"rustls-tls",
"rustls-tls-webpki-roots",
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.4.0"
version = "0.5.0"
edition = "2021"
rust-version = "1.64"
description = "Name & Service Protocol in Rust"
Expand Down
Loading

0 comments on commit 4856955

Please sign in to comment.