Skip to content

Commit

Permalink
storage: endianness - OracleHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 22, 2024
1 parent 655c38f commit fbfbf3a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/ain-ocean/src/indexer/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Index for AppointOracle {
};
services.oracle.by_id.put(&oracle_id, &oracle)?;

let oracle_history_id = (oracle_id, ctx.block.height.to_be_bytes());
let oracle_history_id = (oracle_id, ctx.block.height);
services
.oracle_history
.by_id
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Index for AppointOracle {
services
.oracle_history
.by_id
.delete(&(oracle_id, context.block.height.to_be_bytes()))?;
.delete(&(oracle_id, context.block.height))?;

for currency_pair in self.price_feeds.iter().rev() {
let token_currency_id = (
Expand Down Expand Up @@ -184,7 +184,7 @@ impl Index for UpdateOracle {
services
.oracle_history
.by_id
.put(&(oracle_id, ctx.block.height.to_be_bytes()), &oracle)?;
.put(&(oracle_id, ctx.block.height), &oracle)?;

let (_, previous) =
get_previous_oracle(services, oracle_id)?.context(NotFoundIndexSnafu {
Expand Down Expand Up @@ -220,7 +220,7 @@ impl Index for UpdateOracle {
services
.oracle_history
.by_id
.delete(&(oracle_id, context.block.height.to_be_bytes()))?;
.delete(&(oracle_id, context.block.height))?;

let price_feeds = self.price_feeds.as_ref();
for pair in price_feeds.iter().rev() {
Expand Down Expand Up @@ -739,7 +739,7 @@ fn get_previous_oracle(
let previous = services
.oracle_history
.by_id
.list(Some((oracle_id, [0xffu8; 4])), SortOrder::Descending)?
.list(Some((oracle_id, u32::MAX)), SortOrder::Descending)?
.next()
.transpose()?;

Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/model/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

use super::BlockContext;

pub type OracleHistoryId = (Txid, [u8; 4]); //oracleId-height
pub type OracleHistoryId = (Txid, u32); //oracleId-height

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down
22 changes: 22 additions & 0 deletions lib/ain-ocean/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ define_table! {
pub struct OracleHistory {
key_type = model::OracleHistoryId,
value_type = model::Oracle,
custom_key = {
fn key(index: &Self::Index) -> DBResult<Vec<u8>> {
let (txid, height) = index; // txid, u32
let mut vec = txid.as_byte_array().to_vec();
vec.extend_from_slice(&height.to_be_bytes());
Ok(vec)
}

fn get_key(raw_key: Box<[u8]>) -> DBResult<Self::Index> {
if raw_key.len() != 36 {
return Err(DBError::WrongKeyLength);
}
let mut txid_array = [0u8; 32];
txid_array.copy_from_slice(&raw_key[..32]);
let mut height_array = [0u8; 4];
height_array.copy_from_slice(&raw_key[32..]);

let txid = Txid::from_byte_array(txid_array);
let height = u32::from_be_bytes(height_array);
Ok((txid, height))
}
},
}
}

Expand Down

0 comments on commit fbfbf3a

Please sign in to comment.