/// <summary>
        /// Compacts the block and transaction database by recreating the tables without the deleted references.
        /// </summary>
        private void CompactDataBase()
        {
            Task task = Task.Run(() =>
            {
                using (DBreeze.Transactions.Transaction dbreezeTransaction = this.blockRepository.DBreeze.GetTransaction())
                {
                    dbreezeTransaction.SynchronizeTables(BlockRepository.BlockTableName, BlockRepository.TransactionTableName);

                    var tempBlocks = dbreezeTransaction.SelectDictionary <byte[], byte[]>(BlockRepository.BlockTableName);

                    if (tempBlocks.Count != 0)
                    {
                        this.logger.LogInformation($"{tempBlocks.Count} blocks will be copied to the pruned table.");

                        dbreezeTransaction.RemoveAllKeys(BlockRepository.BlockTableName, true);
                        dbreezeTransaction.InsertDictionary(BlockRepository.BlockTableName, tempBlocks, false);

                        var tempTransactions = dbreezeTransaction.SelectDictionary <byte[], byte[]>(BlockRepository.TransactionTableName);
                        if (tempTransactions.Count != 0)
                        {
                            this.logger.LogInformation($"{tempTransactions.Count} transactions will be copied to the pruned table.");
                            dbreezeTransaction.RemoveAllKeys(BlockRepository.TransactionTableName, true);
                            dbreezeTransaction.InsertDictionary(BlockRepository.TransactionTableName, tempTransactions, false);
                        }

                        // Save the hash and height of where the node was pruned up to.
                        dbreezeTransaction.Insert(BlockRepository.CommonTableName, prunedTipKey, this.dBreezeSerializer.Serialize(this.PrunedTip));
                    }

                    dbreezeTransaction.Commit();
                }

                return(Task.CompletedTask);
            });
        }
예제 #2
0
 /// <summary>
 /// Only use for testing at the moment.
 /// </summary>
 public void Empty()
 {
     using (DBreeze.Transactions.Transaction t = this.engine.GetTransaction())
     {
         t.RemoveAllKeys(this.table, false);
         t.Commit();
     }
 }
        public void Test20DBreeze()
        {
            DBreezeEngine engine = new DBreezeEngine(DbreezeTestLocation);

            using (DBreeze.Transactions.Transaction t = engine.GetTransaction())
            {
                t.RemoveAllKeys(DbreezeTestDb, true);
                t.Commit();
            }
            ISource <byte[], byte[]> stateDB    = new NoDeleteSource <byte[], byte[]>(new DBreezeByteStore(engine, DbreezeTestDb));
            StateRepositoryRoot      repository = new StateRepositoryRoot(stateDB);

            byte[] root = repository.Root;

            uint160 cowAddress   = new uint160(cow);
            uint160 horseAddress = new uint160(horse);

            IStateRepository track2 = repository.StartTracking(); //repository

            track2.SetStorageValue(cowAddress, cowKey1, cowVal1);
            track2.SetStorageValue(horseAddress, horseKey1, horseVal1);
            track2.Commit();
            repository.Commit();

            byte[] root2 = repository.Root;

            track2 = repository.StartTracking(); //repository
            track2.SetStorageValue(cowAddress, cowKey2, cowVal0);
            track2.SetStorageValue(horseAddress, horseKey2, horseVal0);
            track2.Commit();
            repository.Commit();

            byte[] root3 = repository.Root;

            IStateRepository snapshot = new StateRepositoryRoot(stateDB, root);

            Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey1));
            Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2));
            Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey1));
            Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2));

            snapshot = new StateRepositoryRoot(stateDB, root2);
            Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1));
            Assert.Null(snapshot.GetStorageValue(cowAddress, cowKey2));
            Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1));
            Assert.Null(snapshot.GetStorageValue(horseAddress, horseKey2));

            snapshot = new StateRepositoryRoot(stateDB, root3);
            Assert.Equal(cowVal1, snapshot.GetStorageValue(cowAddress, cowKey1));
            Assert.Equal(cowVal0, snapshot.GetStorageValue(cowAddress, cowKey2));
            Assert.Equal(horseVal1, snapshot.GetStorageValue(horseAddress, horseKey1));
            Assert.Equal(horseVal0, snapshot.GetStorageValue(horseAddress, horseKey2));
        }
        private void DropIndex(string name, DBreeze.Transactions.Transaction transaction)
        {
            var indexTableName = IndexTableName(name);

            if (transaction.Select <string, string>("Common", indexTableName).Exists)
            {
                transaction.RemoveAllKeys(indexTableName, true);
                transaction.RemoveKey <string>("Common", indexTableName);
                if (this.tableNames.Contains(indexTableName))
                {
                    this.tableNames.Remove(indexTableName);
                }
            }
        }
예제 #5
0
        /// <inheritdoc />
        public override Task <uint256> Rewind()
        {
            Task <uint256> task = Task.Run(() =>
            {
                this.logger.LogTrace("()");

                uint256 res = null;
                using (DBreeze.Transactions.Transaction transaction = this.dbreeze.GetTransaction())
                {
                    transaction.SynchronizeTables("BlockHash", "Coins", "Rewind");
                    if (this.GetRewindIndex(transaction) == -1)
                    {
                        transaction.RemoveAllKeys("Coins", true);
                        this.SetBlockHash(transaction, this.network.GenesisHash);

                        res = this.network.GenesisHash;
                    }
                    else
                    {
                        transaction.ValuesLazyLoadingIsOn = false;

                        Row <int, RewindData> firstRow = transaction.SelectBackward <int, RewindData>("Rewind").FirstOrDefault();
                        transaction.RemoveKey("Rewind", firstRow.Key);
                        this.SetBlockHash(transaction, firstRow.Value.PreviousBlockHash);

                        foreach (uint256 txId in firstRow.Value.TransactionsToRemove)
                        {
                            this.logger.LogTrace("Outputs of transaction ID '{0}' will be removed.", txId);
                            transaction.RemoveKey("Coins", txId.ToBytes(false));
                        }

                        foreach (UnspentOutputs coin in firstRow.Value.OutputsToRestore)
                        {
                            this.logger.LogTrace("Outputs of transaction ID '{0}' will be restored.", coin.TransactionId);
                            transaction.Insert("Coins", coin.TransactionId.ToBytes(false), coin.ToCoins());
                        }

                        res = firstRow.Value.PreviousBlockHash;
                    }

                    transaction.Commit();
                }

                this.logger.LogTrace("(-):'{0}'", res);
                return(res);
            });

            return(task);
        }
예제 #6
0
        private void CreateIndex(DBreeze.Transactions.Transaction dbreezeTransaction, string name, bool multiValue, string builder, string[] dependencies)
        {
            if (this.Indexes.ContainsKey(name))
            {
                throw new IndexStoreException("The '" + name + "' index already exists");
            }

            var index = new Index(this, name, multiValue, builder, dependencies);

            this.Indexes[name] = index;

            Row <string, string> dbIndexRow = dbreezeTransaction.Select <string, string>("Common", index.Table);

            if (dbIndexRow.Exists)
            {
                dbreezeTransaction.RemoveAllKeys(index.Table, true);
            }

            if (!this.tableNames.Contains(index.Table))
            {
                this.tableNames.Add(index.Table);
            }

            var transactions = new List <(Transaction, Block)>();

            foreach (Row <byte[], Block> row in dbreezeTransaction.SelectForward <byte[], Block>("Block"))
            {
                Block  block = row.Value;
                byte[] key   = block.GetHash().ToBytes();

                foreach (Transaction transaction in block.Transactions)
                {
                    transactions.Add((transaction, block));
                }

                if (transactions.Count >= 5000)
                {
                    index.IndexTransactionDetails(dbreezeTransaction, transactions);
                    transactions.Clear();
                }
            }

            index.IndexTransactionDetails(dbreezeTransaction, transactions);
            dbreezeTransaction.Insert <string, string>("Common", index.Table, index.ToString());
        }
        /// <inheritdoc />
        public uint256 Rewind()
        {
            uint256 res = null;

            using (DBreeze.Transactions.Transaction transaction = this.CreateTransaction())
            {
                transaction.SynchronizeTables("BlockHash", "Coins", "Rewind");
                if (this.GetRewindIndex(transaction) == 0)
                {
                    transaction.RemoveAllKeys("Coins", true);
                    this.SetBlockHash(transaction, this.network.GenesisHash);

                    res = this.network.GenesisHash;
                }
                else
                {
                    transaction.ValuesLazyLoadingIsOn = false;

                    Row <int, byte[]> firstRow = transaction.SelectBackward <int, byte[]>("Rewind").FirstOrDefault();
                    transaction.RemoveKey("Rewind", firstRow.Key);
                    var rewindData = this.dBreezeSerializer.Deserialize <RewindData>(firstRow.Value);
                    this.SetBlockHash(transaction, rewindData.PreviousBlockHash);

                    foreach (uint256 txId in rewindData.TransactionsToRemove)
                    {
                        this.logger.LogDebug("Outputs of transaction ID '{0}' will be removed.", txId);
                        transaction.RemoveKey("Coins", txId.ToBytes(false));
                    }

                    foreach (UnspentOutputs coin in rewindData.OutputsToRestore)
                    {
                        this.logger.LogDebug("Outputs of transaction ID '{0}' will be restored.", coin.TransactionId);
                        transaction.Insert("Coins", coin.TransactionId.ToBytes(false), this.dBreezeSerializer.Serialize(coin.ToCoins()));
                    }

                    res = rewindData.PreviousBlockHash;
                }

                transaction.Commit();
            }

            return(res);
        }
예제 #8
0
        private void DropIndex(string name, DBreeze.Transactions.Transaction transaction)
        {
            Guard.NotNull(name, nameof(name));
            Guard.NotNull(transaction, nameof(transaction));

            string indexTableName = GetIndexTableName(name);

            if (transaction.Select <string, string>("Common", indexTableName).Exists)
            {
                transaction.RemoveAllKeys(indexTableName, true);
                transaction.RemoveKey <string>("Common", indexTableName);
                if (this.tableNames.Contains(indexTableName))
                {
                    this.tableNames.Remove(indexTableName);
                }

                if (this.Indexes.ContainsKey(name))
                {
                    this.Indexes.TryRemove(name, out Index index);
                }
            }
        }
 private void ResetLocked(DBreeze.Transactions.Transaction transaction)
 {
     this.highestPollId = -1;
     transaction.RemoveAllKeys(DataTable, true);
     this.CurrentTip = null;
 }
예제 #10
0
 private void DeleteBusinessType(DBreeze.Transactions.Transaction t, BusinessTypeModel _model)
 {
     t.RemoveKey(Datalog._Business_Type, _model.key, out bool wasremoved);
     t.RemoveAllKeys(_model.key, true);
 }