public void Dispose() { if (_conn != null) { _conn.Dispose(); _conn = null; } if (_blockchain != null) { _blockchain.Dispose(); _blockchain = null; } if (_cache != null) { _cache.Dispose(); _cache = null; } if (_insertBlock != null) { _insertBlock.Dispose(); _insertBlock = null; } if (_insertTransaction != null) { _insertTransaction.Dispose(); _insertTransaction = null; } if (_readTx != null) { _readTx.Dispose(); _readTx = null; } if (_readGasPrices != null) { _readGasPrices.Dispose(); _readGasPrices = null; } if (_readAverageGasPrice != null) { _readAverageGasPrice.Dispose(); _readAverageGasPrice = null; } if (_writeAverageGasPrice != null) { _writeAverageGasPrice.Dispose(); _writeAverageGasPrice = null; } if (_getHistoryByTxId != null) { _getHistoryByTxId.Dispose(); _getHistoryByTxId = null; } if (_getTxsByAddress != null) { _getTxsByAddress.Dispose(); _getTxsByAddress = null; } if (_getTokenTxsByAddress != null) { _getTokenTxsByAddress.Dispose(); _getTokenTxsByAddress = null; } if (_insertTokenTransaction != null) { _insertTokenTransaction.Dispose(); _insertTokenTransaction = null; } if (_getTransactionsToAddressInBlock != null) { _getTransactionsToAddressInBlock.Dispose(); _getTransactionsToAddressInBlock = null; } if (_inputStore != null) { _inputStore.Dispose(); _inputStore = null; } if (_logsStore != null) { _logsStore.Dispose(); _logsStore = null; } }
public Database(Configuration config) { InitializeDb(config); _conn = OpenDb(config); while (true) { try { _cache = new BigintCache(_conn); break; } catch (InconsistentAddressEncodingException ex) { if (!ex.LastGood.HasValue) { throw; } Console.WriteLine("Warning: Addresses have been inconsistently encoded. Rolling database back."); if (!CleanUpDatabase(ex.LastGood.Value)) { throw; } } } InitBlockchain(); InitializeTimestampIndex(); #region SQLite commands _insertBlock = new SQLiteCommand( @"insert into blocks ( hash, previous_hash, miner, timestamp, first_transaction_id, transaction_count, average_gasprice ) values ( @hash, @previous_hash, @miner, @timestamp, @first_transaction_id, @transaction_count, @average_gasprice );" , _conn); _insertTransaction = new SQLiteCommand( @"insert into txs ( id, hash, sender, receiver, amount_wei, blocks_id, input_offset, input_size, gas_price, gas_limit, contract_address, gas_used, logs_offset, logs_size ) values ( @id, @hash, @sender, @receiver, @amount_wei, @blocks_id, @input_offset, @input_size, @gas_price, @gas_limit, @contract_address, @gas_used, @logs_offset, @logs_size );" , _conn); _readTx = new SQLiteCommand("select txs.hash, txs.sender, txs.receiver, txs.amount_wei, blocks.hash from txs inner join blocks on blocks.id = txs.blocks_id where txs.sender = @id or receiver = @id;", _conn); _readGasPrices = new SQLiteCommand("select gas_price from txs where blocks_id = @blocks_id;", _conn); _readAverageGasPrice = new SQLiteCommand("select average_gasprice from blocks where id = @id;", _conn); _writeAverageGasPrice = new SQLiteCommand("update blocks set average_gasprice = @average_gasprice where id = @id;"); _getHistoryByTxId = new SQLiteCommand("select txs.hash, txs.sender, txs.receiver, txs.amount_wei, blocks.hash from txs inner join blocks on blocks.id = txs.blocks_id where txs.id = @id;", _conn); _getTxsByAddress = new SQLiteCommand("select id from txs where sender = @id or receiver = @id;", _conn); _getTokenTxsByAddress = new SQLiteCommand("select txs_id, amount_wei, sender, receiver from token_txs where tokens_id = @tokens_id and (sender = @id or receiver = @id);", _conn); _insertTokenTransaction = new SQLiteCommand("insert into token_txs (txs_id, tokens_id, function_id, sender, receiver, amount_wei) values (@txs_id, @tokens_id, @function_id, @sender, @receiver, @amount_wei);", _conn); _getTransactionsToAddressInBlock = new SQLiteCommand("select id, hash from txs where receiver = @receiver and blocks_id = @blocks_id;", _conn); #endregion _inputStore = new InputStore(config); _logsStore = new LogsStore(config); _nextTxId = GetNextTxId(); }