diff --git a/lib/ain-ocean/src/indexer/oracle.rs b/lib/ain-ocean/src/indexer/oracle.rs index 343b4831ae..77ab02710f 100644 --- a/lib/ain-ocean/src/indexer/oracle.rs +++ b/lib/ain-ocean/src/indexer/oracle.rs @@ -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 @@ -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 = ( @@ -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 { @@ -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() { @@ -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()?; diff --git a/lib/ain-ocean/src/model/oracle.rs b/lib/ain-ocean/src/model/oracle.rs index c15c3f7f69..6c4c0b9548 100644 --- a/lib/ain-ocean/src/model/oracle.rs +++ b/lib/ain-ocean/src/model/oracle.rs @@ -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")] diff --git a/lib/ain-ocean/src/storage/mod.rs b/lib/ain-ocean/src/storage/mod.rs index 0d6c449636..92ef6cfc9b 100644 --- a/lib/ain-ocean/src/storage/mod.rs +++ b/lib/ain-ocean/src/storage/mod.rs @@ -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> { + 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 { + 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)) + } + }, } }