-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathmod.rs
More file actions
147 lines (133 loc) · 4.85 KB
/
mod.rs
File metadata and controls
147 lines (133 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
pub mod chains;
use crate::types::{BitcoinBlockData, BlockIdentifier, StacksBlockData, StacksBlockMetadata};
use crate::utils::stacks::PoxInfo;
use rocket::serde::json::Value as JsonValue;
use std::collections::{HashMap, VecDeque};
#[allow(dead_code)]
pub enum BitcoinChainEvent {
ChainUpdatedWithBlock(BitcoinBlockData),
ChainUpdatedWithReorg(Vec<BitcoinBlockData>),
}
#[allow(dead_code)]
pub enum StacksChainEvent {
ChainUpdatedWithBlock(StacksBlockData),
ChainUpdatedWithReorg(Vec<StacksBlockData>),
}
#[derive(Deserialize, Debug, Clone, Default)]
pub struct AssetClassCache {
pub symbol: String,
pub decimals: u8,
}
pub struct StacksChainContext {
asset_class_map: HashMap<String, AssetClassCache>,
pox_info: PoxInfo,
}
impl PoxInfo {
pub fn default() -> PoxInfo {
PoxInfo {
contract_id: "ST000000000000000000002AMW42H.pox".into(),
pox_activation_threshold_ustx: 0,
first_burnchain_block_height: 100,
prepare_phase_block_length: 1,
reward_phase_block_length: 4,
reward_slots: 8,
total_liquid_supply_ustx: 1000000000000000,
..Default::default()
}
}
}
impl StacksChainContext {
pub fn new() -> StacksChainContext {
StacksChainContext {
asset_class_map: HashMap::new(),
pox_info: PoxInfo::default(),
}
}
}
pub struct IndexerConfig {
pub stacks_node_rpc_url: String,
pub bitcoin_node_rpc_url: String,
pub bitcoin_node_rpc_username: String,
pub bitcoin_node_rpc_password: String,
}
pub struct Indexer {
config: IndexerConfig,
stacks_last_7_blocks: VecDeque<(BlockIdentifier, StacksBlockMetadata)>,
bitcoin_last_7_blocks: VecDeque<BlockIdentifier>,
pub stacks_context: StacksChainContext,
}
impl Indexer {
pub fn new(config: IndexerConfig) -> Indexer {
let stacks_last_7_blocks = VecDeque::new();
let bitcoin_last_7_blocks = VecDeque::new();
let stacks_context = StacksChainContext::new();
Indexer {
config,
stacks_last_7_blocks,
bitcoin_last_7_blocks,
stacks_context,
}
}
pub fn handle_bitcoin_block(&mut self, marshalled_block: JsonValue) -> BitcoinChainEvent {
let block = chains::standardize_bitcoin_block(&self.config, marshalled_block);
if let Some(tip) = self.bitcoin_last_7_blocks.back() {
if block.block_identifier.index == tip.index + 1 {
self.bitcoin_last_7_blocks
.push_back(block.block_identifier.clone());
if self.bitcoin_last_7_blocks.len() > 7 {
self.bitcoin_last_7_blocks.pop_front();
}
} else if block.block_identifier.index > tip.index + 1 {
// TODO: we received a block and we don't have the parent
} else if block.block_identifier.index == tip.index {
// TODO: 1 block reorg
} else {
// TODO: deeper reorg
}
} else {
self.bitcoin_last_7_blocks
.push_front(block.block_identifier.clone());
}
BitcoinChainEvent::ChainUpdatedWithBlock(block)
}
pub fn handle_stacks_block(&mut self, marshalled_block: JsonValue) -> StacksChainEvent {
let block = chains::standardize_stacks_block(
&self.config,
marshalled_block,
&mut self.stacks_context,
);
if let Some((tip, _)) = self.stacks_last_7_blocks.back() {
if block.block_identifier.index == tip.index + 1 {
self.stacks_last_7_blocks
.push_back((block.block_identifier.clone(), block.metadata.clone()));
if self.stacks_last_7_blocks.len() > 7 {
self.stacks_last_7_blocks.pop_front();
}
} else if block.block_identifier.index > tip.index + 1 {
// TODO: we received a block and we don't have the parent
} else if block.block_identifier.index == tip.index {
// TODO: 1 block reorg
} else {
// TODO: deeper reorg
}
} else {
self.stacks_last_7_blocks
.push_front((block.block_identifier.clone(), block.metadata.clone()));
}
StacksChainEvent::ChainUpdatedWithBlock(block)
}
pub fn get_pox_info(&mut self) -> PoxInfo {
self.stacks_context.pox_info.clone()
}
pub async fn update_pox_info(&mut self) {
let res: Result<PoxInfo, _> =
reqwest::get(format!("{}/v2/pox", self.config.stacks_node_rpc_url))
.await
.expect("Unable to retrieve pox info")
.json()
.await;
if let Ok(ref pox_info) = res {
self.stacks_context.pox_info = pox_info.clone();
}
}
}