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
use database::{Database, DatabaseFlags, Environment, ReadTransaction, Transaction, WriteTransaction, FromDatabaseValue, IntoDatabaseValue};
use hash::Blake2bHash;
use block::Block;
use transaction::Transaction as NimiqTransaction;

use beserial::{Serialize, Deserialize};
use std::os::raw::c_uint;
use std::io;
use keys::Address;
use hash::Hash;

pub mod blockchain;

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct TransactionInfo {
    pub transaction_hash: Blake2bHash,
    pub block_hash: Blake2bHash,
    pub block_height: u32,
    pub index: u16,
}

impl FromDatabaseValue for TransactionInfo {
    fn copy_from_database(bytes: &[u8]) -> Result<Self, io::Error> where Self: Sized {
        let mut cursor = io::Cursor::new(bytes);
        Ok(Deserialize::deserialize(&mut cursor)?)
    }
}

impl IntoDatabaseValue for TransactionInfo {
    fn database_byte_size(&self) -> usize {
        self.serialized_size()
    }

    fn copy_into_database(&self, mut bytes: &mut [u8]) {
        Serialize::serialize(&self, &mut bytes).unwrap();
    }
}

impl TransactionInfo {
    pub fn from_block(block: &Block) -> Vec<(&NimiqTransaction, TransactionInfo)> {
        let mut transactions = Vec::with_capacity(
            block.body.as_ref()
                .map(|body| body.transactions.len())
                .unwrap_or_default()
        );

        let block_hash: Blake2bHash = block.header.hash();

        if let Some(ref body) = block.body {
            for (index, tx) in body.transactions.iter().enumerate() {
                transactions.push((tx, TransactionInfo {
                    transaction_hash: tx.hash(),
                    block_hash: block_hash.clone(),
                    block_height: block.header.height,
                    index: index as u16,
                }));
            }
        }

        transactions
    }
}

#[derive(Debug)]
pub struct TransactionStore<'env> {
    env: &'env Environment,
    transaction_db: Database<'env>,
    sender_idx: Database<'env>,
    recipient_idx: Database<'env>,
    transaction_hash_idx: Database<'env>,
}

impl<'env> TransactionStore<'env> {
    const TRANSACTION_DB_NAME: &'static str = "TransactionData";
    const SENDER_IDX_NAME: &'static str = "SenderIdx";
    const RECIPIENT_IDX_NAME: &'static str = "RecipientIdx";
    const TRANSACTION_HASH_IDX_NAME: &'static str = "TransactionHashIdx";
    const HEAD_KEY: c_uint = 0;
    const HEAD_DEFAULT: c_uint = 1;

    pub fn new(env: &'env Environment) -> Self {
        let transaction_db = env.open_database_with_flags(
            Self::TRANSACTION_DB_NAME.to_string(),
            DatabaseFlags::UINT_KEYS
        );
        let sender_idx = env.open_database_with_flags(
            Self::SENDER_IDX_NAME.to_string(),
            DatabaseFlags::DUPLICATE_KEYS | DatabaseFlags::DUP_FIXED_SIZE_VALUES | DatabaseFlags::DUP_UINT_VALUES
        );
        let recipient_idx = env.open_database_with_flags(
            Self::RECIPIENT_IDX_NAME.to_string(),
            DatabaseFlags::DUPLICATE_KEYS | DatabaseFlags::DUP_FIXED_SIZE_VALUES | DatabaseFlags::DUP_UINT_VALUES
        );
        let transaction_hash_idx = env.open_database(
            Self::TRANSACTION_HASH_IDX_NAME.to_string()
        );
        TransactionStore { env, transaction_db, sender_idx, recipient_idx, transaction_hash_idx }
    }

    fn get_head(&self, txn_option: Option<&Transaction>) -> c_uint {
        match txn_option {
            Some(txn) => txn.get(&self.transaction_db, &TransactionStore::HEAD_KEY),
            None => ReadTransaction::new(self.env).get(&self.transaction_db, &TransactionStore::HEAD_KEY)
        }.unwrap_or(Self::HEAD_DEFAULT)
    }

    fn set_head(&self, txn: &mut WriteTransaction, id: c_uint) {
        txn.put(&self.transaction_db, &TransactionStore::HEAD_KEY, &id);
    }

    fn get_id(&self, transaction_hash: &Blake2bHash, txn_option: Option<&Transaction>) -> Option<c_uint> {
        match txn_option {
            Some(txn) => txn.get(&self.transaction_hash_idx, transaction_hash),
            None => ReadTransaction::new(self.env).get(&self.transaction_hash_idx, transaction_hash)
        }
    }

    pub fn get_by_hash(&self, transaction_hash: &Blake2bHash, txn_option: Option<&Transaction>) -> Option<TransactionInfo> {
        let read_txn: ReadTransaction;
        let txn = match txn_option {
            Some(txn) => txn,
            None => {
                read_txn = ReadTransaction::new(self.env);
                &read_txn
            }
        };

        let index = self.get_id(transaction_hash, Some(txn))?;
        txn.get(&self.transaction_db, &index)
    }

    fn get_by_address(&self, database: &Database<'env>, address: &Address, limit: usize, txn: &Transaction) -> Vec<TransactionInfo> {
        let mut transactions = Vec::new();

        // Shortcut for a 0 limit.
        if limit == 0 {
            return transactions;
        }

        // Start collecting transactions.
        let mut cursor = txn.cursor(database);

        // Address not found.
        // Move to last transaction of that address.
        if cursor.seek_key::<Address, c_uint>(address).is_none() {
            return transactions;
        }

        let mut id: Option<c_uint> = cursor.last_duplicate();
        while let Some(index) = id {
            let info = txn.get(&self.transaction_db, &index)
                .expect("Corrupted store: TransactionInfo referenced from index not found");
            transactions.push(info);

            // Stop if we have enough transactions.
            if transactions.len() >= limit {
                break;
            }

            id = cursor.prev_duplicate().map(|(_, value): (Address, c_uint)| value);
        }

        transactions
    }

    pub fn get_by_sender(&self, sender: &Address, limit: usize, txn_option: Option<&Transaction>) -> Vec<TransactionInfo> {
        let read_txn: ReadTransaction;
        let txn = match txn_option {
            Some(txn) => txn,
            None => {
                read_txn = ReadTransaction::new(self.env);
                &read_txn
            }
        };

        self.get_by_address(&self.sender_idx, sender, limit, txn)
    }

    pub fn get_by_recipient(&self, recipient: &Address, limit: usize, txn_option: Option<&Transaction>) -> Vec<TransactionInfo> {
        let read_txn: ReadTransaction;
        let txn = match txn_option {
            Some(txn) => txn,
            None => {
                read_txn = ReadTransaction::new(self.env);
                &read_txn
            }
        };

        self.get_by_address(&self.recipient_idx, recipient, limit, txn)
    }

    pub fn put(&self, block: &Block, txn: &mut WriteTransaction<'env>) {
        // Insert all transactions.
        let transactions = TransactionInfo::from_block(block);
        let mut current_id = self.get_head(Some(txn));
        for (tx, info) in transactions.iter() {
            txn.put_reserve(&self.transaction_db, &current_id, info);
            txn.put(&self.transaction_hash_idx, &info.transaction_hash, &current_id);
            txn.put(&self.sender_idx, &tx.sender, &current_id);
            txn.put(&self.recipient_idx, &tx.recipient, &current_id);
            current_id += 1;
        }
        self.set_head(txn, current_id);
    }

    pub fn remove(&self, block: &Block, txn: &mut WriteTransaction<'env>) {
        if let Some(ref body) = block.body {
            // Remove all transactions.
            for tx in body.transactions.iter() {
                let hash = tx.hash();
                // Delete transaction from every store.
                if let Some(id) = self.get_id(&hash, Some(txn)) {
                    txn.remove(&self.transaction_hash_idx, &hash);
                    txn.remove(&self.transaction_db, &id);
                    txn.remove_item(&self.sender_idx, &tx.sender, &id);
                    txn.remove_item(&self.recipient_idx, &tx.recipient, &id);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use database::volatile::VolatileEnvironment;

    #[test]
    fn it_can_store_the_head_id() {
        let env = VolatileEnvironment::new(4).unwrap();
        let store = TransactionStore::new(&env);
        assert_eq!(store.get_head(None), TransactionStore::HEAD_DEFAULT);

        let head = 5;
        let mut txn = WriteTransaction::new(&env);
        store.set_head(&mut txn, head);
        txn.commit();

        assert_eq!(store.get_head(None), head);
    }

    #[test]
    fn it_can_get_an_id() {
        let env = VolatileEnvironment::new(4).unwrap();
        let store = TransactionStore::new(&env);

        let hash = Blake2bHash::default();
        let id = 5;
        let mut txn = WriteTransaction::new(&env);
        txn.put(&store.transaction_hash_idx, &hash, &id);
        txn.commit();

        assert_eq!(store.get_id(&hash, None), Some(id));
    }

    #[test]
    fn it_can_get_by_address() {
        let env = VolatileEnvironment::new(4).unwrap();
        let store = TransactionStore::new(&env);

        let id1 = 5;
        let id2 = 8;
        let address = Address::default();
        let mut info = TransactionInfo {
            transaction_hash: Blake2bHash::default(),
            block_hash: Blake2bHash::default(),
            block_height: 1337,
            index: 12
        };

        {
            let mut txn = WriteTransaction::new(&env);
            // Insert tx 1.
            txn.put_reserve(&store.transaction_db, &id1, &info);
            txn.put(&store.sender_idx, &address, &id1);
            // Insert tx 2.
            info.index = 8;
            txn.put_reserve(&store.transaction_db, &id2, &info);
            txn.put(&store.sender_idx, &address, &id2);
            txn.commit();
        }

        let txn = ReadTransaction::new(&env);
        assert_eq!(store.get_by_address(&store.sender_idx, &address, 0, &txn).len(), 0);

        // 1 transaction.
        let txs = store.get_by_address(&store.sender_idx, &address, 1, &txn);
        assert_eq!(txs.len(), 1);
        assert_eq!(txs[0].index, 8);

        // 2 transaction.
        let txs = store.get_by_address(&store.sender_idx, &address, 3, &txn);
        assert_eq!(txs.len(), 2);
        assert_eq!(txs[0].index, 8);
        assert_eq!(txs[1].index, 12);
    }
}