|
| 1 | +use { |
| 2 | + crate::{replica, Replica}, |
| 3 | + cluster::{ |
| 4 | + node_operator, |
| 5 | + smart_contract::{self, testing::FakeSmartContract}, |
| 6 | + testing::node_peer_id, |
| 7 | + Cluster, |
| 8 | + Node, |
| 9 | + PeerId, |
| 10 | + }, |
| 11 | + futures::TryStreamExt as _, |
| 12 | + std::{sync::Arc, time::Duration}, |
| 13 | + storage_api::{ |
| 14 | + operation, |
| 15 | + testing::FakeStorage, |
| 16 | + Error, |
| 17 | + Factory, |
| 18 | + Namespace, |
| 19 | + Record, |
| 20 | + RecordVersion, |
| 21 | + StorageApi, |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +#[derive(Clone, Default)] |
| 26 | +struct Config { |
| 27 | + smart_contract_registry: smart_contract::testing::FakeRegistry, |
| 28 | + database: FakeStorage, |
| 29 | +} |
| 30 | + |
| 31 | +impl cluster::Config for Config { |
| 32 | + type SmartContract = FakeSmartContract; |
| 33 | + type KeyspaceShards = (); |
| 34 | + type Node = Node; |
| 35 | + |
| 36 | + fn new_node(&self, _operator_id: node_operator::Id, node: Node) -> Node { |
| 37 | + node |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl super::Config for Config { |
| 42 | + type OutboundDatabaseConnection = FakeStorage; |
| 43 | +} |
| 44 | + |
| 45 | +struct Context { |
| 46 | + config: Config, |
| 47 | + |
| 48 | + replica: Replica<Config>, |
| 49 | + conn: replica::InboundConnection<Config>, |
| 50 | +} |
| 51 | + |
| 52 | +impl Context { |
| 53 | + async fn new() -> Self { |
| 54 | + let cfg = Config::default(); |
| 55 | + |
| 56 | + let cluster = Cluster::deploy( |
| 57 | + cfg.clone(), |
| 58 | + &cfg.smart_contract_registry |
| 59 | + .deployer(smart_contract::testing::signer(42)), |
| 60 | + cluster::Settings { |
| 61 | + max_node_operator_data_bytes: 1024, |
| 62 | + }, |
| 63 | + (0..8) |
| 64 | + .map(|idx| cluster::testing::node_operator(idx as u8)) |
| 65 | + .collect(), |
| 66 | + ) |
| 67 | + .await |
| 68 | + .unwrap(); |
| 69 | + |
| 70 | + let replica = Replica::new(Arc::new(cfg.clone()), cluster, cfg.database.clone()); |
| 71 | + |
| 72 | + Self { |
| 73 | + config: cfg, |
| 74 | + conn: replica.new_inbound_connection(node_peer_id(0, 0)).unwrap(), |
| 75 | + replica, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn namespace(operator_id: u8, idx: u8) -> Namespace { |
| 81 | + let operator_id = cluster::testing::node_operator(operator_id).id; |
| 82 | + format!("{operator_id}/{idx}").parse().unwrap() |
| 83 | +} |
| 84 | + |
| 85 | +#[tokio::test] |
| 86 | +async fn inbound_connections_not_authorized_for_nodes_not_in_cluster() { |
| 87 | + let ctx = Context::new().await; |
| 88 | + let err = ctx.replica.new_storage_api(PeerId::random()).err().unwrap(); |
| 89 | + |
| 90 | + assert_eq!(err, Error::unauthorized()) |
| 91 | +} |
| 92 | + |
| 93 | +#[tokio::test] |
| 94 | +async fn errors_on_keyspace_version_mismatch() { |
| 95 | + let ctx = Context::new().await; |
| 96 | + |
| 97 | + let get = operation::GetBorrowed { |
| 98 | + namespace: namespace(0, 0), |
| 99 | + key: b"test", |
| 100 | + keyspace_version: None, |
| 101 | + }; |
| 102 | + |
| 103 | + let res = ctx.conn.execute(operation::Borrowed::Get(get).into()).await; |
| 104 | + assert_eq!(res, Err(Error::keyspace_version_mismatch())); |
| 105 | + |
| 106 | + let get = operation::GetBorrowed { |
| 107 | + namespace: namespace(0, 0), |
| 108 | + key: b"test", |
| 109 | + keyspace_version: Some(1), |
| 110 | + }; |
| 111 | + |
| 112 | + let res = ctx.conn.execute(operation::Borrowed::Get(get).into()).await; |
| 113 | + assert_eq!(res, Err(Error::keyspace_version_mismatch())); |
| 114 | + |
| 115 | + let err = ctx.conn.read_data(0..=42, 1).await.err(); |
| 116 | + assert_eq!(err, Some(Error::keyspace_version_mismatch())); |
| 117 | +} |
| 118 | + |
| 119 | +#[tokio::test] |
| 120 | +async fn forwards_calls_to_database() { |
| 121 | + let ctx = Context::new().await; |
| 122 | + let ns = namespace(0, 0); |
| 123 | + |
| 124 | + let set = operation::Set { |
| 125 | + namespace: ns, |
| 126 | + key: b"foo".into(), |
| 127 | + record: Record { |
| 128 | + value: b"bar".into(), |
| 129 | + expiration: Duration::from_secs(30).into(), |
| 130 | + version: RecordVersion::now(), |
| 131 | + }, |
| 132 | + keyspace_version: None, |
| 133 | + }; |
| 134 | + |
| 135 | + let res = ctx.config.database.execute(set.clone().into()).await; |
| 136 | + assert_eq!(res, Ok(operation::Output::none())); |
| 137 | + |
| 138 | + let get = operation::GetBorrowed { |
| 139 | + namespace: namespace(0, 0), |
| 140 | + key: b"foo", |
| 141 | + keyspace_version: Some(0), |
| 142 | + }; |
| 143 | + |
| 144 | + let res = ctx.conn.execute(operation::Borrowed::Get(get).into()).await; |
| 145 | + assert_eq!(res, Ok(operation::Output::Record(Some(set.record)))); |
| 146 | + |
| 147 | + let items: Vec<_> = ctx |
| 148 | + .conn |
| 149 | + .read_data(0..=u64::MAX, 0) |
| 150 | + .await |
| 151 | + .unwrap() |
| 152 | + .try_collect() |
| 153 | + .await |
| 154 | + .unwrap(); |
| 155 | + |
| 156 | + assert_eq!(items.len(), 2); // +1 `Done` frame |
| 157 | +} |
0 commit comments