|
| 1 | +mod auction; |
| 2 | +mod masternode; |
| 3 | +mod oracle; |
| 4 | +mod pool; |
| 5 | + |
| 6 | +use dftx_rs::Transaction; |
| 7 | + |
| 8 | +pub(crate) type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; |
| 9 | + |
| 10 | +pub(crate) trait Index { |
| 11 | + fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()>; |
| 12 | + fn invalidate(&self); |
| 13 | +} |
| 14 | + |
| 15 | +use bitcoin::BlockHash; |
| 16 | +use dftx_rs::{deserialize, Block, DfTx}; |
| 17 | + |
| 18 | +pub(crate) struct BlockContext { |
| 19 | + height: u32, |
| 20 | + hash: BlockHash, |
| 21 | + time: u64, |
| 22 | + median_time: u64, |
| 23 | +} |
| 24 | + |
| 25 | +pub fn index_block(block: String, block_height: u32) -> Result<()> { |
| 26 | + let hex = hex::decode(block)?; |
| 27 | + let block = deserialize::<Block>(&hex)?; |
| 28 | + |
| 29 | + let context = BlockContext { |
| 30 | + height: block_height, |
| 31 | + hash: block.block_hash(), |
| 32 | + time: 0, // TODO |
| 33 | + median_time: 0, // TODO |
| 34 | + }; |
| 35 | + |
| 36 | + for tx in block.txdata { |
| 37 | + let bytes = tx.output[0].script_pubkey.as_bytes(); |
| 38 | + if bytes.len() > 2 && bytes[0] == 0x6a && bytes[1] <= 0x4e { |
| 39 | + let offset = 1 + match bytes[1] { |
| 40 | + 0x4c => 2, |
| 41 | + 0x4d => 3, |
| 42 | + 0x4e => 4, |
| 43 | + _ => 1, |
| 44 | + }; |
| 45 | + |
| 46 | + let raw_tx = &bytes[offset..]; |
| 47 | + |
| 48 | + let dftx = deserialize::<DfTx>(raw_tx)?; |
| 49 | + |
| 50 | + match dftx { |
| 51 | + DfTx::CreateMasternode(data) => data.index(&context, tx)?, |
| 52 | + DfTx::UpdateMasternode(data) => data.index(&context, tx)?, |
| 53 | + DfTx::ResignMasternode(data) => data.index(&context, tx)?, |
| 54 | + DfTx::AppointOracle(data) => data.index(&context, tx)?, |
| 55 | + DfTx::RemoveOracle(data) => data.index(&context, tx)?, |
| 56 | + DfTx::UpdateOracle(data) => data.index(&context, tx)?, |
| 57 | + DfTx::SetOracleData(data) => data.index(&context, tx)?, |
| 58 | + DfTx::PoolSwap(data) => data.index(&context, tx)?, |
| 59 | + DfTx::CompositeSwap(data) => data.index(&context, tx)?, |
| 60 | + DfTx::PlaceAuctionBid(data) => data.index(&context, tx)?, |
| 61 | + _ => (), |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
| 68 | +pub fn invalidate_block() { |
| 69 | + todo!() |
| 70 | +} |
0 commit comments