Skip to content

Commit

Permalink
Store block_height of Color ID in big endian
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Mar 24, 2024
1 parent 4f17079 commit d5ffe82
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
68 changes: 38 additions & 30 deletions src/new_index/color.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use bincode::Options;
use tapyrus::consensus::encode::{deserialize, serialize};
use tapyrus::ColorIdentifier;
use tapyrus::{BlockHash, OutPoint, Transaction, TxOut, Txid};
Expand Down Expand Up @@ -36,15 +37,15 @@ impl ColoredTxHistoryRow {
}

pub fn filter(color_id: &ColorIdentifier) -> Bytes {
bincode::serialize(&(b'C', &serialize_color_id(color_id))).unwrap()
bincode::options().with_big_endian().serialize(&(b'C', &serialize_color_id(color_id))).unwrap()
}

pub fn prefix_height(color_id: &ColorIdentifier, height: u32) -> Bytes {
bincode::serialize(&(b'C', &serialize_color_id(color_id), height)).unwrap()
bincode::options().with_big_endian().serialize(&(b'C', &serialize_color_id(color_id), height.to_be_bytes())).unwrap()
}

pub fn prefix_end(color_id: &ColorIdentifier) -> Bytes {
bincode::serialize(&(b'C', &serialize_color_id(color_id), std::u32::MAX)).unwrap()
bincode::options().with_big_endian().serialize(&(b'C', &serialize_color_id(color_id), std::u32::MAX.to_be_bytes())).unwrap()
}

pub fn get_txid(&self) -> Txid {
Expand All @@ -53,10 +54,10 @@ impl ColoredTxHistoryRow {

fn into_row(self) -> DBRow {
DBRow {
key: bincode::serialize(&(
key: bincode::options().with_big_endian().serialize(&(
b'C',
&serialize_color_id(&self.key.color_id),
self.key.confirmed_height,
self.key.confirmed_height.to_be_bytes(),
self.key.txinfo,
))
.unwrap(),
Expand All @@ -69,13 +70,13 @@ impl ColoredTxHistoryRow {
u8,
u8,
[u8; 32],
u32,
[u8; 4],
ColoredTxHistoryInfo,
) = bincode::deserialize(&row.key).unwrap();
) = bincode::options().with_big_endian().deserialize(&row.key).unwrap();
ColoredTxHistoryRow {
key: ColoredTxHistoryKey {
color_id: deserialize_color_id(token_type, payload),
confirmed_height,
confirmed_height: u32::from_be_bytes(confirmed_height),
txinfo,
},
}
Expand Down Expand Up @@ -210,9 +211,16 @@ pub fn index_confirmed_colored_tx(
) {
let history = colored_tx_history(tx, previous_txos_map);

info!("---------------------------------------------");
info!("confirmed_height: {}", confirmed_height);
history.into_iter().for_each(|(color_id, info)| {
rows.push(colored_history_row(&color_id, confirmed_height, info).into_row());
rows.push(ColorIdRow::new(confirmed_height, &color_id).into_row());
info!("info: {:?}", info);
let row = colored_history_row(&color_id, confirmed_height, info).into_row();
info!("colored history row: {:?}", row);
rows.push(row);
let row = ColorIdRow::new(confirmed_height, &color_id).into_row();
info!("color id row: {:?}", row);
rows.push(row);
});
}

Expand Down Expand Up @@ -535,87 +543,87 @@ mod tests {
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'B'(0x42) |
// height | 4 | 10(0x0a000000) |
// height | 4 | 10(0x0000000a) |
// code | 1 | 'c'(0x63) |
// color_id | 33 | c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e |
assert_eq!(hex, "420a00000063c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e");
assert_eq!(hex, "420000000a63c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e");

let row1 = rows.get(1).unwrap();
let hex = hex::encode::<Vec<u8>>(row1.key.iter().cloned().collect());
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'B'(0x42) |
// height | 4 | 10(0x0a000000) |
// height | 4 | 10(0x0000000a) |
// code | 1 | 'c'(0x63) |
// color_id | 33 | c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e |
assert_eq!(hex, "420a00000063c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e");
assert_eq!(hex, "420000000a63c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e");

let row2 = rows.get(2).unwrap();
let hex = hex::encode::<Vec<u8>>(row2.key.iter().cloned().collect());
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'B'(0x42) |
// height | 4 | 10(0x0a000000) |
// height | 4 | 10(0x0000000a) |
// code | 1 | 'c'(0x63) |
// color_id | 33 | c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050 |
assert_eq!(hex, "420a00000063c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050");
assert_eq!(hex, "420000000a63c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050");

let row3 = rows.get(3).unwrap();
let hex = hex::encode::<Vec<u8>>(row3.key.iter().cloned().collect());
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'B'(0x42) |
// height | 4 | 10(0x0a000000) |
// height | 4 | 10(0x0000000a) |
// code | 1 | 'c'(0x63) |
// color_id | 33 | c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050 |
assert_eq!(hex, "420a00000063c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050");
assert_eq!(hex, "420000000a63c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050");

let row4 = rows.get(4).unwrap();
let hex = hex::encode::<Vec<u8>>(row4.key.iter().cloned().collect());
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'C'(0x43) |
// color_id | 33 | c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e |
// height | 4 | 10(0x0a000000) |
// Issue/Transfer/Burn | 4 | 'Transfer'(0x010000000) |
// height | 4 | 10(0x0000000a) |
// Issue/Transfer/Burn | 1 | 'Transfer'(0x01) |
// txid | 32 | 59abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c |
// value | 8 | 100(0x64) |
assert_eq!(hex, "43c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e0a0000000100000059abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c6400000000000000");
assert_eq!(hex, "43c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e0000000a0159abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c64");

let row5 = rows.get(5).unwrap();
let hex = hex::encode::<Vec<u8>>(row5.key.iter().cloned().collect());
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'C'(0x43) |
// color_id | 33 | c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e |
// height | 4 | 10(0x0a000000) |
// Issue/Transfer/Burn | 4 | 'Burn'(0x02000000) |
// height | 4 | 10(0x0000000a) |
// Issue/Transfer/Burn | 1 | 'Burn'(0x02) |
// txid | 32 | 59abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c |
// value | 8 | 100(0x64) |
assert_eq!(hex, "43c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e0a0000000200000059abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c6400000000000000");
assert_eq!(hex, "43c12dceb0cedd7c372c838fea8d46ae863a3c47b2ad0fb950e90ac9d531583ad35e0000000a0259abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c64");

let row6 = rows.get(6).unwrap();
let hex = hex::encode::<Vec<u8>>(row6.key.iter().cloned().collect());
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'C'(0x43) |
// color_id | 33 | c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050 |
// height | 4 | 10(0x0a000000) |
// Issue/Transfer/Burn | 4 | 'Issue'(0x00000000) |
// height | 4 | 10(0x0000000a) |
// Issue/Transfer/Burn | 1 | 'Issue'(0x00) |
// txid | 32 | 59abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c |
// value | 8 | 100(0x64) |
assert_eq!(hex, "43c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf110500a0000000000000059abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c6400000000000000");
assert_eq!(hex, "43c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf110500000000a0059abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c64");

let row7 = rows.get(7).unwrap();
let hex = hex::encode::<Vec<u8>>(row7.key.iter().cloned().collect());
// field | size | value |
//---------------------|------|-----------------------------------------------------------------------|
// prefix | 1 | 'C'(0x43) |
// color_id | 33 | c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf11050 |
// height | 4 | 10(0x0a000000) |
// Issue/Transfer/Burn | 4 | 'Transfer'(0x01000000) |
// height | 4 | 10(0x0000000a) |
// Issue/Transfer/Burn | 1 | 'Transfer'(0x01) |
// txid | 32 | 59abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1c |
// value | 8 | 200(0xc8) |
assert_eq!(hex, "43c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf110500a0000000100000059abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1cc800000000000000");
assert_eq!(hex, "43c271c99cc3bc21757feed5b712744ebb0f770d5c41d99189f9457495747bf110500000000a0159abe954f5636c86484e5e2817d29b915e7f9a9f0294e87c438fd060694a8b1cc8");
}
}
30 changes: 21 additions & 9 deletions src/new_index/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1377,34 +1377,46 @@ impl ColorIdRow {
}

fn prefix() -> Bytes {
bincode::serialize(&(b'B'))
bincode::options()
.with_big_endian()
.serialize(&(b'B'))
.unwrap()
}

fn filter_end(block_height: u32, color_id: &Option<ColorIdentifier>) -> Bytes {
let filter = if let Some(color_id) = color_id {
bincode::serialize(&(b'B', block_height, b'c', &serialize_color_id(&color_id)))
.unwrap()
bincode::options()
.with_big_endian()
.serialize(&(b'B', block_height.to_be_bytes(), b'c', &serialize_color_id(&color_id)))
.unwrap()
} else {
bincode::serialize(&(b'B', block_height + 1, b'c'))
.unwrap()
bincode::options()
.with_big_endian()
.serialize(&(b'B', (block_height + 1).to_be_bytes(), b'c'))
.unwrap()
};
filter
}

pub fn into_row(self) -> DBRow {
let key = bincode::options()
.with_big_endian()
.serialize(&(b'B', self.key.block_height.to_be_bytes(), b'c', &serialize_color_id(&self.key.color_id)))
.unwrap();
DBRow {
key: bincode::serialize(&(b'B', self.key.block_height, b'c', &serialize_color_id(&self.key.color_id))).unwrap(),
key: key,
value: vec![],
}
}

pub fn from_row(row: DBRow) -> Self {
let (_prefix, block_height, _prefix2, token_type, payload): (u8, u32, u8, u8, [u8; 32]) =
bincode::deserialize(&row.key).expect("failed to deserialize ColorIdRow");
let (_prefix, block_height, _prefix2, token_type, payload): (u8, [u8; 4], u8, u8, [u8; 32]) =
bincode::options()
.with_big_endian()
.deserialize(&row.key).expect("failed to deserialize ColorIdRow");
ColorIdRow {
key: ColorIdKey {
block_height: block_height,
block_height: u32::from_be_bytes(block_height),
color_id: deserialize_color_id(token_type, payload),
}
}
Expand Down

0 comments on commit d5ffe82

Please sign in to comment.