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
use serde_json;
use DataStore;
use crateRelay;
use crateTransactionGenerator;
use crateTransaction;
use TestConstants;
// use crate::schema::transaction::rounded_amount;
// TODO: Invest in KeyPair class?
// TODO Proof verification unit tests.
//const GENESIS_HEX_STR =
//
// impl TransactionTestContext {
// #[allow(dead_code)]
// pub async fn default() -> Self {
// let relay = Relay::default().await;
// let tc = TestConstants::new();
// let ds = relay.ds.clone();
// info!("Data store path in test context {}", ds.connection_path);
// let c = ds
// .create_all_err_info()
// //.await
// .expect("create");
// let g = genesis::create_genesis_transaction();
//
// ds.transaction_store.insert_transaction(&g, EARLIEST_TIME, true, None)
// .await.expect("Insert fail");
// info!(
// "Data store immediate genesis query {:?}",
// ds.transaction_store.utxo_all_debug().await.unwrap()
// );
//
// let tx_gen = TransactionGenerator::default(vec![]).with_genesis();
//
// let vec = g.to_utxo_entries(0 as u64);
// let source = vec.get(0).unwrap();
// let t = Transaction::new(source, &tc.addr, 20000, &tc.secret, &tc.public);
// let t2 = Transaction::new(source, &tc.addr2, 20000, &tc.secret, &tc.public);
// Self {
// tc,
// ds,
// c,
// g,
// t,
// t2,
// relay,
// tx_gen,
// }
// }
// }
// Re-enable later.
// // TODO: assertions about bad proofs and insufficent balance and so on.
// #[test]
// fn test_validation() {
// let ttc = TransactionTestContext::default();
// let t = ttc.t;
// let result = t.validate_utxo(&ttc.ds);
// assert!(result.is_ok());
//
// let mut t_invalid_sig = t.clone();
// let p2 = Proof::new(&ttc.tc.hash_vec, &ttc.tc.secret2, &ttc.tc.public);
// t_invalid_sig
// .inputs
// .get_mut(0)
// .unwrap()
// .proof
// .get_mut(0)
// .unwrap()
// .signature = p2.signature;
// assert!(t_invalid_sig.validate_utxo(&ttc.ds).is_err());
//
// let mut t_invalid_amount = t.clone();
// t_invalid_amount.outputs.get_mut(0).unwrap().amount() == redgold_to_amount(REWARD_AMOUNT * 100);
// assert!(t_invalid_amount.validate_utxo(&ttc.ds).is_err());
// }
//
// #[test]
// fn test_decoding() {
// let t = genesis::create_test_genesis_transaction();
// let bytes = t.proto_serialize();
// let t2 = Transaction::proto_deserialize(bytes).unwrap();
// assert_eq!(t, t2);
// assert_eq!(t.hash_or(), t2.hash_or());
// }
//
// pub fn as_base64<S>(key: &Vec<u8>, serializer: &mut S) -> Result<(), S::Error>
// where
// S: Serializer,
// {
// serializer.serialize_str(&base64::encode(key))?;
// Ok(())
// }
//
// pub fn from_base64<'a, D>(deserializer: &mut D) -> Result<Vec<u8>, D::Error>
// where
// &mut D: Deserializer<'a>,
// {
// use serde::de::Error;
// let res = String::deserialize(deserializer)
// .and_then(|string| base64::decode(&string).map_err(|err| Error::custom(err.to_string())))
// .and_then(|opt| opt.ok_or_else(|| Error::custom("failed to deserialize public key")))?;
// Ok(res)
// }
// https://github.com/serde-rs/serde/issues/661
//
// #[test]
// fn test_serialization_obs() {
// let t = ObservationMetadata {
// observed_hash: Some(dhash_str("asdf").to_vec().into()),
// state: 0 as i32,
// validation_confidence: None,
// struct_metadata: None,
// observation_type: 0
// };
// let j = serde_json::to_string(&t).unwrap();
// println!("{}", j);
// let t2 = serde_json::from_str::<ObservationMetadata>(&j).unwrap();
// assert_eq!(t, t2);
// }