Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumulative block event #3840

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6500,6 +6500,7 @@ mod tests {
block_height: 2,
charms: expected_charms,
parent_inscription_ids: Vec::new(),
content_hash: inscription.content_hash(),
}
);

Expand Down
30 changes: 30 additions & 0 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,36 @@ impl Entry for Txid {
}
}

pub type BlockEventHashValue = [u8; 64];

pub type BlockEventHash = (String, String);

impl Entry for BlockEventHash {
type Value = BlockEventHashValue;

fn load(value: Self::Value) -> Self {
let block_event_hash = &value[0..32];

let cumulative_block_event_hash = &value[32..64];

(
hex::encode(block_event_hash),
hex::encode(cumulative_block_event_hash),
)
}

fn store(self) -> Self::Value {
let block_event_hash = hex::decode(self.0).unwrap();

let cumulative_block_event_hash = hex::decode(self.1).unwrap();

[block_event_hash, cumulative_block_event_hash]
.concat()
.try_into()
.unwrap()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
81 changes: 81 additions & 0 deletions src/index/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum Event {
location: Option<SatPoint>,
parent_inscription_ids: Vec<InscriptionId>,
sequence_number: u32,
content_hash: String,
},
InscriptionTransferred {
block_height: u32,
Expand All @@ -17,6 +18,12 @@ pub enum Event {
old_location: SatPoint,
sequence_number: u32,
},
BlockStart {
block_height: u32,
},
BlockEnd {
block_height: u32,
},
RuneBurned {
amount: u128,
block_height: u32,
Expand All @@ -42,3 +49,77 @@ pub enum Event {
txid: Txid,
},
}

pub trait EventHash {
fn hash(&self) -> String;
}

fn loc_to_bytes(loc: &SatPoint) -> Vec<u8> {
let mut bytes = Vec::new();

bytes.extend(loc.outpoint.txid.to_byte_array());

bytes.extend(loc.outpoint.vout.to_be_bytes());

bytes.extend(loc.offset.to_be_bytes());

bytes
}

impl EventHash for Event {
fn hash(&self) -> String {
match self {
Event::InscriptionCreated {
block_height,
charms,
inscription_id,
location,
parent_inscription_ids,
sequence_number,
content_hash,
} => {
let mut bytes = Vec::new();

bytes.extend(block_height.to_be_bytes());

bytes.extend(charms.to_be_bytes());

bytes.extend(inscription_id.value());

if let Some(loc) = location {
bytes.extend(loc_to_bytes(loc));
}

for id in parent_inscription_ids.iter() {
bytes.extend(id.value());
}

bytes.extend(sequence_number.to_be_bytes());
bytes.extend(content_hash.as_bytes());

let digest = bitcoin::hashes::sha256::Hash::hash(&bytes);
return hex::encode(&digest[0..32]);
}

Event::InscriptionTransferred {
block_height,
inscription_id,
new_location,
old_location,
sequence_number,
} => {
let mut bytes = Vec::new();
bytes.extend(block_height.to_be_bytes());
bytes.extend(inscription_id.value());
bytes.extend(loc_to_bytes(new_location));
bytes.extend(loc_to_bytes(old_location));
bytes.extend(sequence_number.to_be_bytes());

let digest = bitcoin::hashes::sha256::Hash::hash(&bytes);
return hex::encode(&digest[0..32]);
}
_ => {}
}
"not impl".to_string()
}
}
16 changes: 16 additions & 0 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ impl<'index> Updater<'index> {
let mut uncommitted = 0;
let mut utxo_cache = HashMap::new();
while let Ok(block) = rx.recv() {
let event_sender = self.index.event_sender.as_ref();

if let Some(sender) = event_sender {
let evt = Event::BlockStart {
block_height: self.height,
};
sender.blocking_send(evt)?;
}

self.index_block(
&mut output_sender,
&mut address_txout_receiver,
Expand All @@ -90,6 +99,13 @@ impl<'index> Updater<'index> {
&mut utxo_cache,
)?;

if let Some(sender) = event_sender {
let evt = Event::BlockEnd {
block_height: self.height - 1,
};
sender.blocking_send(evt)?;
}

if let Some(progress_bar) = &mut progress_bar {
progress_bar.inc(1);

Expand Down
18 changes: 13 additions & 5 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum Origin {
reinscription: bool,
unbound: bool,
vindicated: bool,
content_hash: String,
},
Old {
old_satpoint: SatPoint,
Expand Down Expand Up @@ -98,7 +99,7 @@ impl<'a, 'tx> InscriptionUpdater<'a, 'tx> {
self.sequence_number_to_entry,
txin.previous_output,
)? {
let offset = total_input_value + old_satpoint.offset;
let offset: u64 = total_input_value + old_satpoint.offset;
floating_inscriptions.push(Flotsam {
offset,
inscription_id,
Expand Down Expand Up @@ -214,6 +215,7 @@ impl<'a, 'tx> InscriptionUpdater<'a, 'tx> {
hidden: inscription.payload.hidden(),
parents: inscription.payload.parents(),
pointer: inscription.payload.pointer(),
content_hash: inscription.payload.content_hash(),
reinscription: inscribed_offsets.contains_key(&offset),
unbound: txout.value == 0
|| curse == Some(Curse::UnrecognizedEvenField)
Expand Down Expand Up @@ -429,13 +431,15 @@ impl<'a, 'tx> InscriptionUpdater<'a, 'tx> {
}

if let Some(sender) = self.event_sender {
sender.blocking_send(Event::InscriptionTransferred {
let evt = Event::InscriptionTransferred {
block_height: self.height,
inscription_id,
new_location: new_satpoint,
old_location: old_satpoint,
sequence_number,
})?;
};

sender.blocking_send(evt)?;
}

(false, sequence_number)
Expand All @@ -449,6 +453,7 @@ impl<'a, 'tx> InscriptionUpdater<'a, 'tx> {
reinscription,
unbound,
vindicated,
content_hash,
} => {
let inscription_number = if cursed {
let number: i32 = self.cursed_inscription_count.try_into().unwrap();
Expand Down Expand Up @@ -525,14 +530,17 @@ impl<'a, 'tx> InscriptionUpdater<'a, 'tx> {
.collect::<Result<Vec<u32>>>()?;

if let Some(sender) = self.event_sender {
sender.blocking_send(Event::InscriptionCreated {
let evt = Event::InscriptionCreated {
block_height: self.height,
charms,
inscription_id,
location: (!unbound).then_some(new_satpoint),
parent_inscription_ids: parents,
sequence_number,
})?;
content_hash,
};

sender.blocking_send(evt)?;
}

self.sequence_number_to_entry.insert(
Expand Down
15 changes: 15 additions & 0 deletions src/inscriptions/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ impl Inscription {
})
}

pub fn content_hash(&self) -> String {
let mut bytes = Vec::new();

if let Some(content_type) = &self.content_type {
bytes.extend(content_type);
}

if let Some(body) = &self.body {
bytes.extend(body);
}

let digest = bitcoin::hashes::sha256::Hash::hash(&bytes);
hex::encode(&digest[0..20])
}

pub fn pointer_value(pointer: u64) -> Vec<u8> {
let mut bytes = pointer.to_le_bytes().to_vec();

Expand Down
33 changes: 31 additions & 2 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use event_hasher::EventHasher;

use super::*;

pub mod balances;
Expand All @@ -16,6 +18,7 @@ pub mod supply;
pub mod teleburn;
pub mod traits;
pub mod wallet;
pub mod event_hasher;

#[derive(Debug, Parser)]
pub(crate) enum Subcommand {
Expand Down Expand Up @@ -66,10 +69,36 @@ impl Subcommand {
Self::Parse(parse) => parse.run(),
Self::Runes => runes::run(settings),
Self::Server(server) => {
let index = Arc::new(Index::open(&settings)?);
let (event_sender, mut event_receiver) = tokio::sync::mpsc::channel(1024);

let index = Arc::new(Index::open_with_event_sender(&settings, Some(event_sender)).unwrap());
let handle = axum_server::Handle::new();
LISTENERS.lock().unwrap().push(handle.clone());
server.run(settings, index, handle)
log::info!("server run ...");


let setting_clone = settings.clone();
let receiver_thread = thread::spawn(move || {

let hasher = EventHasher::create(&setting_clone).unwrap();
log::info!("Hashing start");
if let Err(error) = hasher.run(&mut event_receiver) {
log::warn!("Hashing error: {error}");
}

log::info!("Hashing end");

});

let result = server.run(settings, index, handle);

log::info!("server end ...");

if receiver_thread.join().is_err() {
log::warn!("Receiver thread panicked; join failed");
}

result
}
Self::Settings => settings::run(settings),
Self::Subsidy(subsidy) => subsidy.run(),
Expand Down
Loading