Пример #1
0
        /// <summary>
        /// Loads block index from file into memory.
        /// </summary>
        /// <returns>True if succeeds.</returns>
        public bool Load()
        {
            _indexFile = DbFile.Read <IndexFile>(Config.GetBlockIndexFilePath());
            if (_indexFile == null)
            {
                _indexFile = new IndexFile
                {
                    BlockIndices = new List <BlockIndex>()
                };
                DbFile.Write(Config.GetBlockIndexFilePath(), _indexFile);
            }

            _blockIndices = _indexFile.BlockIndices;

            for (int i = 0, c = _blockIndices.Count; i < c; ++i)
            {
                var idx = _blockIndices[i].Index;
                if (idx != i)
                {
                    return(false);
                }

                var hash = Utils.HexUtils.ByteArrayFromHex(_blockIndices[i].Hash);
                if (!Pow.IsValidHash(hash, Pow.CalculateDifficulty(idx)))
                {
                    return(false);
                }
            }

            _blockFileByIndex = new Dictionary <int, BlockFile>();
            _blocks           = new Dictionary <int, Block>();

            if (_blockIndices.Count == 0)
            {
                AddBlock(Config.GetGenesisBlock());
            }

            IsReady = true;

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Loads transaction database from persistent storage into memory.
        /// </summary>
        /// <returns>True if succeeds.</returns>
        public bool Load()
        {
            _indexFile = DbFile.Read <IndexFile>(Config.GetTransactionIndexFilePath());
            if (_indexFile == null)
            {
                _indexFile = new IndexFile
                {
                    TransactionIndices  = new Dictionary <string, TransactionIndex>(),
                    UnspentTxOuts       = new List <UnspentTxOut>(),
                    PendingTransactions = new List <PendingTransaction>(),
                };
                DbFile.Write(Config.GetTransactionIndexFilePath(), _indexFile);
            }

            _transactionIndices  = _indexFile.TransactionIndices;
            _unspentTxOuts       = _indexFile.UnspentTxOuts;
            _pendingTransactions = _indexFile.PendingTransactions;

            _transactions = new Dictionary <string, Transaction>();

            return(true);
        }