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
use crate::*;
use bitcoin::{Transaction, Txid, TxOut, Block};
use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
use crate::db::utxo::UtxoEntry;
use crate::rocks_db::{Serialize, Deserialize};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxDBKey {
    txid: Txid,
}

impl Serialize for TxDBKey {
    fn serialize(&self) -> Vec<u8> {
        consensus_encode(&self.txid)
    }
}

impl Deserialize for TxDBKey {
    fn deserialize(buf: &[u8]) -> Self {
        Self {
            txid: consensus_decode(buf)
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxDBValue {
    pub confirmed_height: Option<u32>,
    pub tx: Transaction,
    pub previous_txouts: Vec<TxOut>,
}

impl TxDBValue {
    pub fn deserialize_as_rawtx(buf: &[u8]) -> (Option<u32>, Vec<u8>, Vec<TxOut>) {
        let confirmed_height = bytes_to_i32(&buf[0..4]);
        let confirmed_height = if confirmed_height >= 0 {
            Some(confirmed_height as u32)
        } else {
            None
        };
        let tx_len = bytes_to_u32(&buf[4..8]) as usize;
        let tx = buf[8..tx_len+8].to_vec();
        let mut offset: usize = tx_len + 8;
        let mut previous_txouts = Vec::new();
        while offset < buf.len() {
            let txout_len = bytes_to_u32(&buf[offset..offset+4]) as usize;
            offset += 4;
            let txout = consensus_decode(&buf[offset..txout_len+offset]);
            offset += txout_len;
            previous_txouts.push(txout);
        }
        (confirmed_height, tx, previous_txouts)
    }
}

impl Serialize for TxDBValue {
    fn serialize(&self) -> Vec<u8> {
        let mut ret = Vec::new();
        let confirmed_height = self.confirmed_height.map_or_else(|| -1i32, |confirmed_height| confirmed_height as i32);
        ret.push(confirmed_height.to_le_bytes().to_vec());
        let tx = consensus_encode(&self.tx);
        let tx_len = tx.len() as u32;
        ret.push(tx_len.to_le_bytes().to_vec());
        ret.push(tx);
        for txout in self.previous_txouts.iter() {
            let txout = consensus_encode(txout);
            let txout_len = txout.len() as u32;
            ret.push(txout_len.to_le_bytes().to_vec());
            ret.push(txout);
        }
        ret.concat()
    }
}

impl Deserialize for TxDBValue {
    fn deserialize(buf: &[u8]) -> Self {
        let (confirmed_height, tx, previous_txouts) = Self::deserialize_as_rawtx(buf);
        Self {
            confirmed_height,
            tx: consensus_decode(&tx),
            previous_txouts,
        }
    }
}

#[derive(Debug)]
pub struct TxDB {
    db: RocksDB<TxDBKey, TxDBValue>,
}

impl TxDB {
    pub fn path(coin: &str) -> String {
        format!("{}/{}/tx", data_dir(), coin)
    }
    pub fn new(coin: &str, temporary: bool) -> Self {
        let path = Self::path(coin);
        Self {
            db: RocksDB::new(&path, temporary),
        }
    }
    pub fn put(&self, txid: &Txid, value: &TxDBValue) {
        self.db.put(&TxDBKey { txid: *txid }, value);
    }
    pub fn put_tx(&self, tx: &Transaction, confirmed_height: Option<u32>) -> Result<(TxDBValue, Vec<UtxoEntry>), Txid> {
        let mut previous_txouts = Vec::new();
        let mut previous_utxos = Vec::new();
        for vin in tx.input.iter() {
            if !vin.previous_output.is_null() {
                let previous_txid = vin.previous_output.txid;
                match self.get(&previous_txid) {
                    Some(previous_tx) => {
                        let previous_txout = previous_tx.tx.output[vin.previous_output.vout as usize].clone();
                        previous_utxos.push(UtxoEntry {
                            script_pubkey: previous_txout.script_pubkey.clone(),
                            txid: previous_txid,
                            vout: vin.previous_output.vout,
                            value: previous_txout.value,
                        });
                        previous_txouts.push(previous_txout);
                    },
                    None => return Err(previous_txid),
                }
            }
        }
        let value = TxDBValue {
            confirmed_height,
            tx: (*tx).clone(),
            previous_txouts,
        };
        self.put(&tx.txid(), &value);
        Ok((value, previous_utxos))
    }
    pub fn get(&self, txid: &Txid) -> Option<TxDBValue> {
        self.db.get(&TxDBKey { txid: *txid })
    }
    pub fn get_as_rest(&self, txid: &Txid, config: &Config) -> Option<chainseeker::Transaction> {
        //let begin_get = std::time::Instant::now();
        let buf = self.db.get_raw(&TxDBKey { txid: *txid });
        //println!("Transaction got in {}us.", begin_get.elapsed().as_micros());
        buf.map_or_else(|| None, |buf| {
            //let begin_convert = std::time::Instant::now();
            let (confirmed_height, rawtx, previous_txouts) = TxDBValue::deserialize_as_rawtx(&buf);
            let tx: Transaction = consensus_decode(&rawtx);
            let mut input_value = 0;
            let mut vin = Vec::new();
            let mut previous_txout_index = 0;
            for input in tx.input.iter() {
                if input.previous_output.is_null() {
                    vin.push(create_vin(input, &None, config));
                } else {
                    input_value += previous_txouts[previous_txout_index].value;
                    vin.push(create_vin(input, &Some(previous_txouts[previous_txout_index].clone()), config));
                    previous_txout_index += 1;
                }
            }
            let output_value: u64 = tx.output.iter().map(|output| output.value).sum();
            let tx = chainseeker::Transaction {
                confirmed_height,
                hex: hex::encode(&rawtx),
                txid: tx.txid().to_string(),
                hash: tx.wtxid().to_string(),
                size: tx.get_size(),
                // TODO: waiting for upstream merge.
                //vsize: tx.get_vsize(),
                vsize: (tx.get_weight() + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR,
                weight: tx.get_weight(),
                version: tx.version,
                locktime: tx.lock_time,
                vin,
                vout: tx.output.iter().enumerate().map(|(n, vout)| create_vout(vout, n, config)).collect(),
                // TODO: compute for coinbase transactions!
                fee: (input_value as i64) - (output_value as i64),
            };
            //println!("Transaction converted in {}us.", begin_convert.elapsed().as_micros());
            Some(tx)
        })
    }
    /*
    pub fn multi_get<I: IntoIterator<Item = Txid>>(&self, txids: I) -> Vec<Option<TxDBValue>> {
        let txids: Vec<TxDBKey> = txids.into_iter().map(|txid| TxDBKey { txid }).collect();
        self.db.multi_get(txids)
    }
    */
    pub fn process_block(&self, confirmed_height: u32, block: &Block, previous_utxos: &[UtxoEntry]) {
        let mut previous_utxo_index = 0;
        for tx in block.txdata.iter() {
            // Process vins.
            let mut previous_txouts = Vec::new();
            for vin in tx.input.iter() {
                if !vin.previous_output.is_null() {
                    let txout = TxOut {
                        value: previous_utxos[previous_utxo_index].value,
                        script_pubkey: previous_utxos[previous_utxo_index].script_pubkey.clone(),
                    };
                    previous_txouts.push(txout);
                    previous_utxo_index += 1;
                }
            }
            let value = TxDBValue {
                confirmed_height: Some(confirmed_height),
                tx: (*tx).clone(),
                previous_txouts,
            };
            self.put(&tx.txid(), &value);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use crate::db::utxo::UtxoDB;
    use super::*;
    const TXID: &str = "503e4e9824282eb06f1a328484e2b367b5f4f93a405d6e7b97261bafabfb53d5";
    #[test]
    fn key_deserialize() {
        assert_eq!(
            TxDBKey {
                txid: Txid::from_str(TXID).unwrap(),
            },
            TxDBKey::deserialize(&Txid::from_hex(TXID).unwrap()),
        );
    }
    #[test]
    fn put_unconfirmed() {
        let tx = &fixtures::regtest_blocks()[0].txdata[0];
        let tx_db = TxDB::new("test/tx/unconfirmed", true);
        tx_db.put_tx(&tx, None).unwrap();
        assert_eq!(
            tx_db.get(&tx.txid()).unwrap(),
            TxDBValue {
                confirmed_height: None,
                tx: (*tx).clone(),
                previous_txouts: Vec::new(),
            },
        );
    }
    #[test]
    fn put_confirmed() {
        let blocks = fixtures::regtest_blocks();
        let mut utxo_db = UtxoDB::new("test/tx/confirmed", true);
        let tx_db = TxDB::new("test/tx/confirmed", true);
        let mut previous_utxos_vec = Vec::new();
        for (height, block) in blocks.iter().enumerate() {
            let previous_utxos = utxo_db.process_block(&block, true);
            tx_db.process_block(height as u32, &block, &previous_utxos);
            previous_utxos_vec.push(previous_utxos);
        }
        // txid = fe6c48bbfdc025670f4db0340650ba5a50f9307b091d9aaa19aa44291961c69f.
        assert_eq!(
            tx_db.put_tx(&consensus_decode(&hex::decode("01000000000101d553fbabaf1b26977b6e5d403af9f4b567b3e28484321a6fb02e2824984e3e5000000000171600142b2296c588ec413cebd19c3cbc04ea830ead6e78ffffffff01be1611020000000017a91487e4e5a7ff7bf78b8a8972a49381c8a673917f3e870247304402205f39ccbab38b644acea0776d18cb63ce3e37428cbac06dc23b59c61607aef69102206b8610827e9cb853ea0ba38983662034bd3575cc1ab118fb66d6a98066fa0bed01210304c01563d46e38264283b99bb352b46e69bf132431f102d4bd9a9d8dab075e7f00000000").unwrap()), Some(500_000)).unwrap_err(),
            Txid::from_str(TXID).unwrap(),
        );
        for (height, block) in blocks.iter().enumerate() {
            let mut previous_utxo_index = 0;
            for tx in block.txdata.iter() {
                let mut previous_txout_index = 0;
                let value = tx_db.get(&tx.txid()).unwrap();
                assert_eq!(value.confirmed_height, Some(height as u32));
                assert_eq!(value.tx, *tx);
                for vin in tx.input.iter() {
                    if !vin.previous_output.is_null() {
                        let txout = TxOut {
                            value: previous_utxos_vec[height][previous_utxo_index].value,
                            script_pubkey: previous_utxos_vec[height][previous_utxo_index].script_pubkey.clone(),
                        };
                        assert_eq!(value.previous_txouts[previous_txout_index], txout);
                        previous_utxo_index += 1;
                        previous_txout_index += 1;
                    }
                }
            }
        }
    }
}