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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use async_trait::async_trait;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::{All, PublicKey, Secp256k1, SecretKey};
use bitcoin::{Block, BlockHash, Network};
use bitcoin::hash_types::FilterHeader;
use bitcoin::key::KeyPair;
use log::info;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use txoo::filter::BlockSpendFilter;
use txoo::source::Error as TxooSourceError;
use txoo::source::{attestation_path, write_yaml_to_file, FileSource, Source};
use txoo::util::sign_attestation;
use txoo::{Attestation, OracleSetup, SignedAttestation};

/// A dummy TXOO source that can be used for testing.
/// Uses DUMMY_SECRET as the oracle secret key.
///
/// Note that the [`TxooFollower`] will automatically provide blocks to the source
/// via the [`on_new_block`] method, except for the genesis block that must be provided
/// by the caller.
#[derive(Clone)]
pub struct DummyTxooSource {
    setup: OracleSetup,
    secret_key: SecretKey,
    attestations: Arc<Mutex<HashMap<BlockHash, SignedAttestation>>>,
    secp: Secp256k1<All>,
}

/// A dummy oracle secret
pub const DUMMY_SECRET: [u8; 32] = [0xcd; 32];

impl DummyTxooSource {
    /// Create a new source
    pub fn new() -> Self {
        let secp = Secp256k1::new();
        let secret_key =
            SecretKey::from_slice(&DUMMY_SECRET).expect("32 bytes, within curve order");
        let public_key = PublicKey::from_secret_key(&secp, &secret_key);
        Self {
            setup: OracleSetup {
                network: Network::Bitcoin,
                start_block: 0,
                public_key,
            },
            secret_key,
            attestations: Arc::new(Mutex::new(HashMap::new())),
            secp,
        }
    }
}

#[async_trait]
impl Source for DummyTxooSource {
    async fn get_unchecked(
        &self,
        block_height: u32,
        block_hash: &BlockHash,
    ) -> Result<SignedAttestation, TxooSourceError> {
        let attestations = self.attestations.lock().unwrap();
        attestations
            .get(block_hash)
            .cloned()
            .map(|a| {
                if a.attestation.block_height != block_height {
                    panic!(
                        "wrong height {} {}",
                        a.attestation.block_height, block_height
                    );
                } else {
                    a
                }
            })
            .ok_or(TxooSourceError::NotExists)
    }

    async fn oracle_setup(&self) -> &OracleSetup {
        &self.setup
    }

    fn secp(&self) -> &Secp256k1<All> {
        &self.secp
    }

    async fn on_new_block(&self, block_height: u32, block: &Block) {
        let mut attestations = self.attestations.lock().unwrap();
        if attestations.len() != block_height as usize {
            panic!(
                "wrong height to DummyTxooSource::on_new_block stored {} called with {}",
                attestations.len(),
                block_height as usize
            );
        }
        let prev_block_hash = block.header.prev_blockhash;
        let filter_header = if !attestations.is_empty() {
            let prev_attestation = attestations.get(&prev_block_hash).unwrap();
            let prev_filter_header = prev_attestation.attestation.filter_header;
            let filter = BlockSpendFilter::from_block(&block);
            filter.filter_header(&prev_filter_header)
        } else {
            FilterHeader::all_zeros()
        };
        let attestation = Attestation {
            block_hash: block.block_hash(),
            block_height,
            filter_header,
            time: 0,
        };
        let keypair = KeyPair::from_secret_key(&self.secp, &self.secret_key);
        let signed_attestation = sign_attestation(attestation, &keypair, &self.secp);
        attestations.insert(block.block_hash(), signed_attestation);
    }
}

/// A dummy TXOO source that can be used for testing.
/// Uses DUMMY_SECRET as the oracle secret key.
///
/// Note that the [`TxooFollower`] will automatically provide blocks to the source
/// via the [`on_new_block`] method, except for the genesis block that must be provided
/// by the caller.
pub struct DummyPersistentTxooSource {
    file_source: FileSource,
    setup: OracleSetup,
    secret_key: SecretKey,
}

impl DummyPersistentTxooSource {
    /// Create a new source
    pub fn new(
        datadir: PathBuf,
        network: Network,
        start_block: u32,
        block: &Block,
        prev_filter_header: &FilterHeader,
    ) -> Self {
        let secp = Secp256k1::new();
        let secret_key =
            SecretKey::from_slice(&DUMMY_SECRET).expect("32 bytes, within curve order");
        let public_key = PublicKey::from_secret_key(&secp, &secret_key);
        let config = OracleSetup {
            network,
            start_block,
            public_key,
        };

        fs::create_dir_all(datadir.join("public")).expect("create datadir/public");

        write_yaml_to_file(&datadir, "public/config", &config);
        let file_source = FileSource::new(datadir);
        let signed_attestation = make_signed_attestation_from_block(
            &secret_key,
            start_block,
            &block,
            prev_filter_header,
            &secp,
        );
        do_write_attestation(file_source.datadir(), &signed_attestation);

        info!(
            "dummy persistent source, start block {}, datadir {}",
            start_block,
            file_source.datadir().display()
        );

        Self {
            file_source,
            setup: OracleSetup {
                network,
                start_block,
                public_key,
            },
            secret_key,
        }
    }

    /// Create a new source at a checkpoint, where the filter_header is known
    pub fn from_checkpoint(
        datadir: PathBuf,
        network: Network,
        start_block: u32,
        block_hash: BlockHash,
        filter_header: FilterHeader,
    ) -> Self {
        let secp = Secp256k1::new();
        let secret_key =
            SecretKey::from_slice(&DUMMY_SECRET).expect("32 bytes, within curve order");
        let public_key = PublicKey::from_secret_key(&secp, &secret_key);
        let config = OracleSetup {
            network,
            start_block,
            public_key,
        };

        fs::create_dir_all(datadir.join("public")).expect("create datadir/public");

        write_yaml_to_file(&datadir, "public/config", &config);
        let file_source = FileSource::new(datadir);
        let signed_attestation =
            make_signed_attestation(&secret_key, start_block, block_hash, filter_header, &secp);
        do_write_attestation(file_source.datadir(), &signed_attestation);

        info!(
            "dummy persistent source, start block {}, datadir {}",
            start_block,
            file_source.datadir().display()
        );

        Self {
            file_source,
            setup: OracleSetup {
                network,
                start_block,
                public_key,
            },
            secret_key,
        }
    }
}

fn make_signed_attestation_from_block(
    secret_key: &SecretKey,
    block_height: u32,
    block: &Block,
    prev_filter_header: &FilterHeader,
    secp: &Secp256k1<All>,
) -> SignedAttestation {
    let filter = BlockSpendFilter::from_block(&block);
    let filter_header = filter.filter_header(&prev_filter_header);
    let block_hash = block.block_hash();
    make_signed_attestation(secret_key, block_height, block_hash, filter_header, secp)
}

fn make_signed_attestation(
    secret_key: &SecretKey,
    block_height: u32,
    block_hash: BlockHash,
    filter_header: FilterHeader,
    secp: &Secp256k1<All>,
) -> SignedAttestation {
    let attestation = Attestation {
        block_hash,
        block_height,
        filter_header,
        time: 0,
    };
    let keypair = KeyPair::from_secret_key(&secp, secret_key);
    sign_attestation(attestation, &keypair, &secp)
}

fn do_write_attestation(datadir: &PathBuf, signed_attestation: &SignedAttestation) {
    let attestation = &signed_attestation.attestation;
    write_yaml_to_file(
        datadir,
        &attestation_path(attestation.block_height, &attestation.block_hash),
        &signed_attestation,
    )
}

#[async_trait]
impl Source for DummyPersistentTxooSource {
    async fn get_unchecked(
        &self,
        block_height: u32,
        block_hash: &BlockHash,
    ) -> Result<SignedAttestation, TxooSourceError> {
        self.file_source
            .get_unchecked(block_height, block_hash)
            .await
    }

    async fn oracle_setup(&self) -> &OracleSetup {
        &self.setup
    }

    fn secp(&self) -> &Secp256k1<All> {
        self.file_source.secp()
    }

    async fn on_new_block(&self, block_height: u32, block: &Block) {
        info!("new block {}-{}", block_height, block.block_hash());
        let prev_block_hash = block.header.prev_blockhash;
        let prev_attestation = self
            .file_source
            .get_unchecked(block_height - 1, &prev_block_hash)
            .await
            .unwrap_or_else(|e| {
                panic!(
                    "could not get attestation for prev {}-{}: {:?}",
                    block_height - 1,
                    prev_block_hash,
                    e
                )
            });
        let prev_filter_header = prev_attestation.attestation.filter_header;
        let signed_attestation = make_signed_attestation_from_block(
            &self.secret_key,
            block_height,
            block,
            &prev_filter_header,
            self.secp(),
        );
        do_write_attestation(&self.file_source.datadir(), &signed_attestation);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use txoo::source::Error;
    use bitcoin::block::Version;
    use bitcoin::blockdata::constants::genesis_block;
    use bitcoin::blockdata::block::Header as BlockHeader;
    use bitcoin::hash_types::TxMerkleNode;
    use bitcoin::CompactTarget;

    #[tokio::test]
    async fn dummy_source_test() {
        let tmpdir = tempfile::tempdir().unwrap();
        let network = Network::Regtest;
        let block = genesis_block(network);
        let source = DummyPersistentTxooSource::new(
            tmpdir.path().to_path_buf(),
            network,
            0,
            &block,
            &FilterHeader::all_zeros(),
        );
        let attestation = source
            .get_unchecked(0, &block.block_hash())
            .await
            .expect("attestation exists");
        assert_eq!(attestation.attestation.block_height, 0);
        assert_eq!(attestation.attestation.block_hash, block.block_hash());

        let block1 = Block {
            header: BlockHeader {
                version: Version::from_consensus(0),
                prev_blockhash: block.block_hash(),
                merkle_root: TxMerkleNode::all_zeros(),
                time: 0,
                bits: CompactTarget::from_consensus(0),
                nonce: 0,
            },
            txdata: vec![],
        };
        source.on_new_block(1, &block1).await;
        let attestation = source
            .get_unchecked(1, &block1.block_hash())
            .await
            .expect("attestation exists");
        assert_eq!(attestation.attestation.block_height, 1);

        source.get(1, &block1).await.expect("get 1");
    }

    #[tokio::test]
    async fn dummy_source_genesis_test() {
        let tmpdir = tempfile::tempdir().unwrap();
        let network = Network::Regtest;
        let block0 = genesis_block(network);
        let block1 = Block {
            header: BlockHeader {
                version: Version::ONE,
                prev_blockhash: block0.block_hash(),
                merkle_root: TxMerkleNode::all_zeros(),
                time: 0,
                bits: CompactTarget::from_consensus(0),
                nonce: 0,
            },
            txdata: vec![],
        };
        // create source from block height 1, genesis block attestation wasn't created
        let source = DummyPersistentTxooSource::new(
            tmpdir.path().to_path_buf(),
            network,
            1,
            &block1,
            &FilterHeader::all_zeros(),
        );

        let genesis_result = source.get_unchecked(0, &block0.block_hash()).await;
        assert!(genesis_result.is_err());
        assert_eq!(genesis_result.err().unwrap(), Error::NotExists);

        let (_, prev_filter_header) = source.get(1, &block1).await.expect("get 1");
        assert_eq!(prev_filter_header, FilterHeader::all_zeros());
    }
}