-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathmod.rs
More file actions
168 lines (152 loc) · 5.81 KB
/
mod.rs
File metadata and controls
168 lines (152 loc) · 5.81 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
pub mod chains;
use crate::types::{
BitcoinChainEvent, BlockIdentifier, ChainUpdatedWithBlockData, ChainUpdatedWithMicroblockData,
StacksBlockData, StacksChainEvent, StacksMicroblocksTrail,
};
use crate::utils::stacks::PoxInfo;
use rocket::serde::json::Value as JsonValue;
use std::collections::{HashMap, VecDeque};
#[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: 5,
reward_phase_block_length: 10,
reward_slots: 20,
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,
current_microblock_trail: StacksMicroblocksTrail,
stacks_last_7_blocks: VecDeque<(BlockIdentifier, StacksBlockData)>,
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 current_microblock_trail = StacksMicroblocksTrail {
microblocks: vec![],
};
let stacks_context = StacksChainContext::new();
Indexer {
config,
stacks_last_7_blocks,
bitcoin_last_7_blocks,
stacks_context,
current_microblock_trail,
}
}
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(lgalabru): we received a block and we don't have the parent
} else if block.block_identifier.index == tip.index {
// TODO(lgalabru): 1 block reorg
} else {
// TODO(lgalabru): 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,
);
let mut anchored_trail = None;
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.clone()));
anchored_trail = Some(self.current_microblock_trail.clone());
self.current_microblock_trail = StacksMicroblocksTrail {
microblocks: vec![],
};
} else if block.block_identifier.index > tip.index + 1 {
// TODO(lgalabru): we received a block and we don't have the parent
} else if block.block_identifier.index == tip.index {
// TODO(lgalabru): 1 block reorg
} else {
// TODO(lgalabru): deeper reorg
}
} else {
self.stacks_last_7_blocks
.push_front((block.block_identifier.clone(), block.clone()));
self.current_microblock_trail = StacksMicroblocksTrail {
microblocks: vec![],
};
}
let (_, confirmed_block) = self.stacks_last_7_blocks.front().unwrap().clone();
if self.stacks_last_7_blocks.len() > 7 {
self.stacks_last_7_blocks.pop_front();
}
let update = ChainUpdatedWithBlockData {
new_block: block,
anchored_trail,
confirmed_block: (confirmed_block, None),
};
StacksChainEvent::ChainUpdatedWithBlock(update)
}
pub fn handle_stacks_microblock(
&mut self,
marshalled_microblock: JsonValue,
) -> StacksChainEvent {
let (_, anchored_block) = self.stacks_last_7_blocks.back().unwrap();
let microblock = chains::standardize_stacks_microblock(
&self.config,
marshalled_microblock,
&anchored_block.block_identifier,
&mut self.stacks_context,
);
self.current_microblock_trail.microblocks.push(microblock);
let update = ChainUpdatedWithMicroblockData {
anchored_block: anchored_block.clone(),
current_trail: self.current_microblock_trail.clone(),
};
StacksChainEvent::ChainUpdatedWithMicroblock(update)
}
pub fn get_pox_info(&mut self) -> PoxInfo {
self.stacks_context.pox_info.clone()
}
}