|
| 1 | +use crate::chain_forward::client_state::find_headers_between; |
| 2 | +use crate::consensus::store::{ChainStore, Nonces, StoreError}; |
| 3 | +use amaru_kernel::{Hash, Header}; |
| 4 | +use pallas_network::miniprotocols::chainsync::Tip; |
| 5 | +use pallas_network::miniprotocols::Point; |
| 6 | +use std::{collections::HashMap, fs::File, path::Path, str::FromStr}; |
| 7 | + |
| 8 | +impl ChainStore<Header> for HashMap<Hash<32>, Header> { |
| 9 | + fn load_header(&self, hash: &Hash<32>) -> Option<Header> { |
| 10 | + self.get(hash).cloned() |
| 11 | + } |
| 12 | + |
| 13 | + fn store_header(&mut self, hash: &Hash<32>, header: &Header) -> Result<(), StoreError> { |
| 14 | + self.insert(*hash, header.clone()); |
| 15 | + Ok(()) |
| 16 | + } |
| 17 | + |
| 18 | + fn get_nonces(&self, _header: &Hash<32>) -> Option<Nonces> { |
| 19 | + todo!() |
| 20 | + } |
| 21 | + |
| 22 | + fn put_nonces(&mut self, _header: &Hash<32>, _nonces: Nonces) -> Result<(), StoreError> { |
| 23 | + todo!() |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn mk_store(path: impl AsRef<Path>) -> HashMap<Hash<32>, Header> { |
| 28 | + let f = File::open(path).unwrap(); |
| 29 | + let json: serde_json::Value = serde_json::from_reader(f).unwrap(); |
| 30 | + let headers = json |
| 31 | + .pointer("/stakePools/chains") |
| 32 | + .unwrap() |
| 33 | + .as_array() |
| 34 | + .unwrap(); |
| 35 | + |
| 36 | + let mut store = HashMap::new(); |
| 37 | + |
| 38 | + for header in headers { |
| 39 | + let hash = header.pointer("/hash").unwrap().as_str().unwrap(); |
| 40 | + let header = header.pointer("/header").unwrap().as_str().unwrap(); |
| 41 | + let header = hex::decode(header).unwrap(); |
| 42 | + store.insert(hash.parse().unwrap(), minicbor::decode(&header).unwrap()); |
| 43 | + } |
| 44 | + |
| 45 | + store |
| 46 | +} |
| 47 | + |
| 48 | +fn hash(s: &str) -> Hash<32> { |
| 49 | + Hash::<32>::from_str(s).unwrap() |
| 50 | +} |
| 51 | + |
| 52 | +fn hex(s: &str) -> Vec<u8> { |
| 53 | + hex::decode(s).unwrap() |
| 54 | +} |
| 55 | + |
| 56 | +const CHAIN_41: &str = "tests/data/chain41.json"; |
| 57 | + |
| 58 | +const TIP_41: &str = "fcb4a51804f14f3f5b5ad841199b557aed0187280f7855736bdb153b0d202bb6"; |
| 59 | +const TIP_41_SLOT: u64 = 990; |
| 60 | +const TIP_41_HEIGHT: u64 = 47; |
| 61 | + |
| 62 | +const LOST_41: &str = "bd41b102018a21e068d504e64b282512a3b7d5c3883b743aa070ad9244691125"; |
| 63 | +const LOST_41_SLOT: u64 = 188; |
| 64 | + |
| 65 | +const WINNER_41: &str = "66c90f54f9073cfc03a334f5b15b1617f6bf6fe6c892fad8368e16abe20b0f4f"; |
| 66 | +const WINNER_41_SLOT: u64 = 187; |
| 67 | +const WINNER_41_HEIGHT: u64 = 8; |
| 68 | + |
| 69 | +const BRANCH_41: &str = "64565f22fb23476baaa6f82e0e2d68636ceadabded697099fb376c23226bdf03"; |
| 70 | +const BRANCH_41_SLOT: u64 = 142; |
| 71 | +const BRANCH_41_HEIGHT: u64 = 7; |
| 72 | + |
| 73 | +const ROOT_41_SLOT: u64 = 31; |
| 74 | + |
| 75 | +fn point(slot: u64, hash: &str) -> Point { |
| 76 | + Point::Specific(slot, hex(hash)) |
| 77 | +} |
| 78 | + |
| 79 | +#[test] |
| 80 | +fn test_mk_store() { |
| 81 | + let store = mk_store(CHAIN_41); |
| 82 | + assert_eq!(store.len(), 48); |
| 83 | + |
| 84 | + let mut current = hash(TIP_41); |
| 85 | + let mut chain = store.get(¤t).unwrap().clone(); |
| 86 | + |
| 87 | + assert_eq!(chain.header_body.slot, TIP_41_SLOT); |
| 88 | + |
| 89 | + while chain.header_body.prev_hash.is_some() { |
| 90 | + current = chain.header_body.prev_hash.unwrap(); |
| 91 | + chain = store.get(¤t).unwrap().clone(); |
| 92 | + } |
| 93 | + |
| 94 | + assert_eq!(chain.header_body.slot, ROOT_41_SLOT); |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn find_headers_between_tip_and_tip() { |
| 99 | + let store = mk_store(CHAIN_41); |
| 100 | + |
| 101 | + let tip = point(TIP_41_SLOT, TIP_41); |
| 102 | + let points = [point(TIP_41_SLOT, TIP_41)]; |
| 103 | + |
| 104 | + let (ops, Tip(p, h)) = find_headers_between(&store, &tip, &points).unwrap(); |
| 105 | + assert_eq!((ops, p, h), (vec![], tip, 47)); |
| 106 | +} |
| 107 | + |
| 108 | +#[test] |
| 109 | +fn find_headers_between_tip_and_branch() { |
| 110 | + let store = mk_store(CHAIN_41); |
| 111 | + |
| 112 | + let tip = point(TIP_41_SLOT, TIP_41); |
| 113 | + let points = [point(BRANCH_41_SLOT, BRANCH_41)]; |
| 114 | + let peer = point(BRANCH_41_SLOT, BRANCH_41); |
| 115 | + |
| 116 | + let (ops, Tip(p, h)) = find_headers_between(&store, &tip, &points).unwrap(); |
| 117 | + assert_eq!( |
| 118 | + (ops.len() as u64, p, h), |
| 119 | + (TIP_41_HEIGHT - BRANCH_41_HEIGHT, peer, BRANCH_41_HEIGHT) |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +#[test] |
| 124 | +fn find_headers_between_tip_and_branches() { |
| 125 | + let store = mk_store(CHAIN_41); |
| 126 | + |
| 127 | + let tip = point(TIP_41_SLOT, TIP_41); |
| 128 | + // Note that the below scheme does not match the documented behaviour, which shall pick the first from |
| 129 | + // the list that is on the same chain. But that doesn't make sense to me at all. |
| 130 | + let points = [ |
| 131 | + point(BRANCH_41_SLOT, BRANCH_41), // this will lose to the (taller) winner |
| 132 | + point(LOST_41_SLOT, LOST_41), // this is not on the same chain |
| 133 | + point(WINNER_41_SLOT, WINNER_41), // this is the winner after the branch |
| 134 | + ]; |
| 135 | + let peer = point(WINNER_41_SLOT, WINNER_41); |
| 136 | + |
| 137 | + let (ops, Tip(p, h)) = find_headers_between(&store, &tip, &points).unwrap(); |
| 138 | + assert_eq!( |
| 139 | + (ops.len() as u64, p, h), |
| 140 | + (TIP_41_HEIGHT - WINNER_41_HEIGHT, peer, WINNER_41_HEIGHT) |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +#[test] |
| 145 | +fn find_headers_between_tip_and_lost() { |
| 146 | + let store = mk_store(CHAIN_41); |
| 147 | + |
| 148 | + let tip = point(TIP_41_SLOT, TIP_41); |
| 149 | + let points = [point(LOST_41_SLOT, LOST_41)]; |
| 150 | + |
| 151 | + let result = find_headers_between(&store, &tip, &points); |
| 152 | + assert!(result.is_none(), "{result:?}"); |
| 153 | +} |
0 commit comments