Sharding represents Spicenet's approach to horizontal scaling through multiple execution shards coordinated by a consensus shard. This enables different product verticals (like spot trading, futures, etc.) to operate independently while maintaining cross-shard communication capabilities.
Architecture Deep Dive
Unified Message Layer (UML)
The UML is crucial for enabling efficient cross-shard communication:
rustCopystruct UnifiedMessageLayer {
priority_queue: PriorityQueue<CrossShardMessage>,
connections: HashMap<ShardId, TcpStream>,
message_handlers: HashMap<MessageType, Box<dyn MessageHandler>>
}
impl UnifiedMessageLayer {
async fn handle_cross_shard_message(&mut self, msg: CrossShardMessage) -> Result<()> {
// Log message receipt
tracing::debug!("Received cross-shard message: {:?}", msg);
// Get appropriate handler
let handler = self.message_handlers.get(&msg.message_type)
.ok_or(Error::UnknownMessageType)?;
// Process message
handler.handle_message(msg).await?;
Ok(())
}
async fn send_priority_message(&mut self, to_shard: ShardId, msg: CrossShardMessage) {
self.priority_queue.push(msg.clone(), msg.priority);
// Process high priority messages immediately
while let Some(high_priority_msg) = self.priority_queue.pop_if_priority_exceeds(PRIORITY_THRESHOLD) {
if let Some(conn) = self.connections.get(&to_shard) {
conn.send_message(high_priority_msg).await?;
}
}
}
}
Example: Cross-Shard Atomic Swap
Here's how the system handles an atomic swap between the spot and futures shards: