public BlockHeader GetHeader(ChainedHeader chainedHeader, uint256 hash)
        {
            if (this.headers.TryGetValue(hash, out BlockHeader blockHeader))
            {
                return(blockHeader);
            }

            // TODO: Bring in uint256 span optimisations
            byte[] bytes = hash.ToBytes();

            lock (this.locker)
            {
                bytes = this.rocksDb.Get(DBH.Key(HeaderTableName, bytes));
            }

            if (bytes == null)
            {
                throw new ApplicationException("Header must exist if requested");
            }

            blockHeader = this.network.Consensus.ConsensusFactory.CreateBlockHeader();
            blockHeader.FromBytes(bytes, this.network.Consensus.ConsensusFactory);

            // If the header is 500 blocks behind tip or 100 blocks ahead cache it.
            if ((chainedHeader.Height > this.ChainIndexer.Height - 500) && (chainedHeader.Height <= this.ChainIndexer.Height + 100))
            {
                this.headers.AddOrUpdate(hash, blockHeader);
            }

            return(blockHeader);
        }
Exemple #2
0
        /// <inheritdoc />
        public void PruneAndCompactDatabase(ChainedHeader blockRepositoryTip, Network network, bool nodeInitializing)
        {
            this.logger.LogInformation($"Pruning started...");

            if (this.PrunedTip == null)
            {
                Block genesis = network.GetGenesis();

                this.PrunedTip = new HashHeightPair(genesis.GetHash(), 0);

                this.blockRepository.Leveldb.Put(DBH.Key(BlockRepository.CommonTableName, prunedTipKey), this.dBreezeSerializer.Serialize(this.PrunedTip));
            }

            if (nodeInitializing)
            {
                if (this.IsDatabasePruned())
                {
                    return;
                }

                this.PrepareDatabaseForCompacting(blockRepositoryTip);
            }

            // LevelDB has its own internal compaction logic

            this.logger.LogInformation($"Pruning complete.");

            return;
        }
Exemple #3
0
        public static int UpdateUserCategory(string tableName, int id, int pid, int uid, string name, string pinyin = null, string pinyinabbr = null, string ename = null)
        {
            if (pinyin == null)
            {
                pinyin = HZ.ToPinYin(name, true);
            }
            if (pinyinabbr == null)
            {
                pinyinabbr = PinYin.GetInitial(name);
            }
            if (ename == null)
            {
                ename = pinyin;
            }

            //check pid & uid
            if (pid != 0 && !DBH.GetBoolean(QA.DBCS_CMS, CommandType.Text, "SELECT COUNT(id) FROM " + tableName + " WHERE pid=@pid AND uid=@uid", new SqlParameter("@pid", pid), new SqlParameter("@uid", uid)))
            {
                return(-1);
            }

            return(EB <UserCategoryEntity> .Update(QA.DBCS_CMS, new UserCategoryEntity()
            {
                id = id,
                uid = uid,
                pid = pid,
                name = name,
                pinyin = pinyin,
                pinyinabbr = pinyinabbr,
                ename = ename
            }, tableName));
        }
Exemple #4
0
        /// <inheritdoc />
        public uint256 GetBlockIdByTransactionId(uint256 trxid)
        {
            Guard.NotNull(trxid, nameof(trxid));

            if (!this.TxIndex)
            {
                this.logger.LogTrace("(-)[NO_TXINDEX]:null");
                return(default(uint256));
            }

            if (this.genesisTransactions.ContainsKey(trxid))
            {
                return(this.network.GenesisHash);
            }

            uint256 res = null;

            lock (this.Locker)
            {
                byte[] transactionRow = this.rocksdb.Get(DBH.Key(TransactionTableName, trxid.ToBytes()));
                if (transactionRow != null)
                {
                    res = new uint256(transactionRow);
                }
            }

            return(res);
        }
Exemple #5
0
        public static int RenameUserCategory(string tableName, int id, string name)
        {
            string pinyin     = HZ.ToPinYin(name, true);
            string pinyinabbr = PinYin.GetInitial(name);

            return(DBH.ExecuteText(QA.DBCS_CMS, "UPDATE " + tableName + " SET name=@name,pinyin=@pinyin,pinyinabbr=@pinyinabbr,ename=@ename WHERE id=@id",
                                   new SqlParameter("@name", name), new SqlParameter("@pinyin", pinyin), new SqlParameter("@pinyinabbr", pinyinabbr), new SqlParameter("@ename", pinyin), new SqlParameter("@id", id)));
        }
        /// <inheritdoc />
        public void UpdatePrunedTip(ChainedHeader tip)
        {
            this.PrunedTip = new HashHeightPair(tip);

            lock (this.blockRepository.Locker)
            {
                this.blockRepository.Leveldb.Put(DBH.Key(BlockRepository.CommonTableName, prunedTipKey), this.dataStoreSerializer.Serialize(this.PrunedTip));
            }
        }
        /// <summary>
        /// Set's the hash and height tip of the new <see cref="ProvenBlockHeader"/>.
        /// </summary>
        /// <param name="transaction"> Open DBreeze transaction.</param>
        /// <param name="newTip"> Hash height pair of the new block tip.</param>
        private void SetTip(HashHeightPair newTip)
        {
            Guard.NotNull(newTip, nameof(newTip));

            lock (this.locker)
            {
                this.leveldb.Put(DBH.Key(blockHashHeightTable, blockHashHeightKey), this.dataStoreSerializer.Serialize(newTip));
            }
        }
Exemple #8
0
        /// <summary>
        /// Set's the hash and height tip of the new <see cref="ProvenBlockHeader"/>.
        /// </summary>
        /// <param name="newTip"> Hash height pair of the new block tip.</param>
        private void SetTip(HashHeightPair newTip)
        {
            Guard.NotNull(newTip, nameof(newTip));

            lock (this.locker)
            {
                this.rocksDb.Put(DBH.Key(BlockHeaderRepositoryConstants.BlockHashHeightTable, BlockHeaderRepositoryConstants.BlockHashHeightKey), this.dBreezeSerializer.Serialize(newTip));
            }
        }
Exemple #9
0
        /// <inheritdoc/>
        public Transaction[] GetTransactionsByIds(uint256[] trxids, CancellationToken cancellation = default(CancellationToken))
        {
            if (!this.TxIndex)
            {
                this.logger.LogTrace("(-)[TX_INDEXING_DISABLED]:null");
                return(null);
            }

            Transaction[] txes = new Transaction[trxids.Length];

            lock (this.Locker)
            {
                for (int i = 0; i < trxids.Length; i++)
                {
                    cancellation.ThrowIfCancellationRequested();

                    bool alreadyFetched = trxids.Take(i).Any(x => x == trxids[i]);

                    if (alreadyFetched)
                    {
                        this.logger.LogDebug("Duplicated transaction encountered. Tx id: '{0}'.", trxids[i]);

                        txes[i] = txes.First(x => x.GetHash() == trxids[i]);
                        continue;
                    }

                    if (this.genesisTransactions.TryGetValue(trxids[i], out Transaction genesisTransaction))
                    {
                        txes[i] = genesisTransaction;
                        continue;
                    }

                    byte[] transactionRow = this.leveldb.Get(DBH.Key(TransactionTableName, trxids[i].ToBytes()));
                    if (transactionRow == null)
                    {
                        this.logger.LogTrace("(-)[NO_TX_ROW]:null");
                        return(null);
                    }

                    byte[] blockRow = this.leveldb.Get(DBH.Key(BlockTableName, transactionRow));

                    if (blockRow != null)
                    {
                        this.logger.LogTrace("(-)[NO_BLOCK]:null");
                        return(null);
                    }

                    var         block = this.dataStoreSerializer.Deserialize <Block>(blockRow);
                    Transaction tx    = block.Transactions.FirstOrDefault(t => t.GetHash() == trxids[i]);

                    txes[i] = tx;
                }
            }

            return(txes);
        }
Exemple #10
0
 /// <summary>
 ///  创建一个事件
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public static int CreateEvent(byte type, byte spread, int uid, int touid, string tmplName, string data = null)
 {
     return(DBH.ExecuteSP(QA.DBCS_MAIN, "usp_event_create",
                          new SqlParameter("@type", type),
                          new SqlParameter("@spread", spread),
                          new SqlParameter("@uid", uid),
                          new SqlParameter("@touid", touid),
                          new SqlParameter("@tmplname", tmplName),
                          new SqlParameter("@data", data)));
 }
Exemple #11
0
 public static int CreateThread(int bid, byte type, int uid, string uname, string uip, string name, string story)
 {
     return(DBH.GetInt32(QA.DBCS_MAIN, CommandType.StoredProcedure, "usp_thread_create",
                         new SqlParameter("@bid", bid),
                         new SqlParameter("@type", type),
                         new SqlParameter("@uid", uid),
                         new SqlParameter("@uname", uname),
                         new SqlParameter("@uip", uip),
                         new SqlParameter("@name", name),
                         new SqlParameter("@story", story)));
 }
Exemple #12
0
        public bool PutHeader(BlockHeader blockHeader)
        {
            ConsensusFactory consensusFactory = this.network.Consensus.ConsensusFactory;

            lock (this.locker)
            {
                this.leveldb.Put(DBH.Key(HeaderTableName, blockHeader.GetHash().ToReadOnlySpan()), blockHeader.ToBytes(consensusFactory));
            }

            return(true);
        }
 private void LoadPrunedTip(DB leveldb)
 {
     if (this.PrunedTip == null)
     {
         byte[] row = leveldb.Get(DBH.Key(BlockRepository.CommonTableName, prunedTipKey));
         if (row != null)
         {
             this.PrunedTip = this.dataStoreSerializer.Deserialize <HashHeightPair>(row);
         }
     }
 }
Exemple #14
0
        public static void DeleteUserCategory(int id, string categoryTable, string entityTable)
        {
            int pid = GetUserCategoryPid(id, categoryTable);

            if (pid >= 0)
            {
                if (DBH.ExecuteText(QA.DBCS_CMS, "UPDATE " + entityTable + " SET ucid=@pid WHERE ucid=@id", new SqlParameter("@pid", pid), new SqlParameter("@id", id)) >= 0)
                {
                    DBH.ExecuteText(QA.DBCS_CMS, "DELETE " + categoryTable + " WHERE id=@id", new SqlParameter("@id", id));
                }
            }
        }
Exemple #15
0
 private void LoadPrunedTip(RocksDbSharp.RocksDb rocksdb)
 {
     if (this.PrunedTip == null)
     {
         lock (this.blockRepository.Locker)
         {
             byte[] row = rocksdb.Get(DBH.Key(BlockRepository.CommonTableName, prunedTipKey));
             if (row != null)
             {
                 this.PrunedTip = this.dataStoreSerializer.Deserialize <HashHeightPair>(row);
             }
         }
     }
 }
Exemple #16
0
        protected virtual void OnInsertBlocks(List <Block> blocks)
        {
            var transactions     = new List <(Transaction, Block)>();
            var byteListComparer = new ByteListComparer();
            var blockDict        = new Dictionary <uint256, Block>();

            // Gather blocks.
            foreach (Block block in blocks)
            {
                uint256 blockId = block.GetHash();
                blockDict[blockId] = block;
            }

            // Sort blocks. Be consistent in always converting our keys to byte arrays using the ToBytes method.
            List <KeyValuePair <uint256, Block> > blockList = blockDict.ToList();

            blockList.Sort((pair1, pair2) => byteListComparer.Compare(pair1.Key.ToBytes(), pair2.Key.ToBytes()));

            using (var batch = new WriteBatch())
            {
                // Index blocks.
                foreach (KeyValuePair <uint256, Block> kv in blockList)
                {
                    uint256 blockId = kv.Key;
                    Block   block   = kv.Value;

                    // If the block is already in store don't write it again.
                    byte[] blockRow = this.leveldb.Get(DBH.Key(BlockTableName, blockId.ToBytes()));
                    if (blockRow == null)
                    {
                        batch.Put(DBH.Key(BlockTableName, blockId.ToBytes()), this.dBreezeSerializer.Serialize(block));

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

                this.leveldb.Write(batch);
            }

            if (this.TxIndex)
            {
                this.OnInsertTransactions(transactions);
            }
        }
Exemple #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="www"></param>
        /// <param name="id"></param>
        /// <param name="sid"></param>
        /// <param name="code"></param>
        internal static void Initialize(string name, string www, int id, string sid, string code)
        {
            if (!string.IsNullOrEmpty(code))
            {
                //更新好友数据
                string sql = "UPDATE user_status SET invited=invited+1 WHERE id=@id1;UPDATE inviteinfo SET invited_id=@id2 WHERE id=@id1 AND code=@code;"; //id1 邀请 id2
                DBH.ExecuteText(QA.DBCS_MAIN, sql, new SqlParameter("@id1", sid), new SqlParameter("@id2", id), new SqlParameter("@code", code));
            }

            //写入用户信息
            EB <UserStatusEntity> .Create(QA.DBCS_MAIN, new UserStatusEntity()
            {
                id = id,
            });
        }
Exemple #18
0
        public void GetTrxBlockIdAsyncWithoutExistingTransactionReturnsNull()
        {
            string dir = CreateTestDir(this);

            using (var engine = RocksDb.Open(new DbOptions().SetCreateIfMissing(true), dir))
            {
                engine.Put(DBH.Key(RocksdbBlockRepository.CommonTableName, new byte[0]), this.DataStoreSerializer.Serialize(new HashHeightPair(uint256.Zero, 1)));
                engine.Put(DBH.Key(RocksdbBlockRepository.CommonTableName, new byte[1]), BitConverter.GetBytes(true));
            }

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
                Assert.Null(repository.GetBlockIdByTransactionId(new uint256(26)));
            }
        }
Exemple #19
0
        /// <summary>
        /// Inserts <see cref="ProvenBlockHeader"/> items into to the database.
        /// </summary>
        /// <param name="headers"> List of <see cref="ProvenBlockHeader"/> items to save.</param>
        private void InsertHeaders(SortedDictionary <int, ProvenBlockHeader> headers)
        {
            using var batch = new WriteBatch();
            {
                foreach (KeyValuePair <int, ProvenBlockHeader> header in headers)
                {
                    batch.Put(DBH.Key(BlockHeaderRepositoryConstants.ProvenBlockHeaderTable, BitConverter.GetBytes(header.Key)), this.dBreezeSerializer.Serialize(header.Value));
                }

                lock (this.locker)
                {
                    this.rocksDb.Write(batch);
                }
            }
        }
Exemple #20
0
        public void GetTrxAsyncWithoutTransactionIndexReturnsNewTransaction()
        {
            string dir = CreateTestDir(this);

            using (var engine = RocksDb.Open(new DbOptions().SetCreateIfMissing(true), dir))
            {
                engine.Put(DBH.Key(RocksdbBlockRepository.CommonTableName, new byte[0]), this.DataStoreSerializer.Serialize(new HashHeightPair(uint256.Zero, 1)));
                engine.Put(DBH.Key(RocksdbBlockRepository.CommonTableName, new byte[1]), BitConverter.GetBytes(false));
            }

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
                Assert.Equal(default(Transaction), repository.GetTransactionById(uint256.Zero));
            }
        }
Exemple #21
0
        public void ExistAsyncWithExistingBlockReturnsTrue()
        {
            string dir   = CreateTestDir(this);
            Block  block = this.Network.Consensus.ConsensusFactory.CreateBlock();

            using (var engine = RocksDb.Open(new DbOptions().SetCreateIfMissing(true), dir))
            {
                engine.Put(DBH.Key(RocksdbBlockRepository.BlockTableName, block.GetHash().ToBytes()), block.ToBytes());
            }

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
                Assert.True(repository.Exist(block.GetHash()));
            }
        }
Exemple #22
0
        public void PutChainData(IEnumerable <ChainDataItem> items)
        {
            using (var batch = new WriteBatch())
            {
                foreach (var item in items)
                {
                    batch.Put(DBH.Key(ChainTableName, BitConverter.GetBytes(item.Height)), item.Data.ToBytes(this.network.Consensus.ConsensusFactory));
                }

                lock (this.locker)
                {
                    this.leveldb.Write(batch);
                }
            }
        }
        /// <inheritdoc />
        public void PrepareDatabase()
        {
            if (this.PrunedTip == null)
            {
                Block genesis = this.network.GetGenesis();

                this.PrunedTip = new HashHeightPair(genesis.GetHash(), 0);

                lock (this.blockRepository.Locker)
                {
                    this.blockRepository.Leveldb.Put(DBH.Key(BlockRepository.CommonTableName, prunedTipKey), this.dataStoreSerializer.Serialize(this.PrunedTip));
                }
            }

            return;
        }
Exemple #24
0
        public static ReposityMeta GetMeta(int mid)
        {
            ReposityMeta rm = CacheService.Get("MID_" + mid) as ReposityMeta;

            if (rm == null)
            {
                string sql = "SELECT * FROM meta_reposity WHERE id=@id;" +
                             "SELECT * FROM meta_reposity_map WHERE rid=@id ORDER BY id";

                using (IDataReader dr = DBH.ExecuteReader(QA.DBCS_MAIN, CommandType.Text, sql, new SqlParameter("@id", mid)))
                {
                    if (dr != null)
                    {
                        if (dr.Read())
                        {
                            rm        = new ReposityMeta();
                            rm.Id     = dr.GetInt32(dr.GetOrdinal("id"));
                            rm.OP     = dr.GetByte(dr.GetOrdinal("op"));
                            rm.Total  = dr.GetInt32(dr.GetOrdinal("total"));
                            rm.Name   = dr.GetString(dr.GetOrdinal("name"));
                            rm.Table1 = dr.GetString(dr.GetOrdinal("table1"));
                            rm.Table2 = (dr["table2"] is DBNull) ? null : dr.GetString(dr.GetOrdinal("table2"));
                            rm.Table3 = (dr["table3"] is DBNull) ? null : dr.GetString(dr.GetOrdinal("table3"));

                            if (dr.NextResult())
                            {
                                rm.Maps = new List <ReposityMap>();
                                while (dr.Read())
                                {
                                    ReposityMap map = new ReposityMap();
                                    map.Id         = dr.GetInt32(dr.GetOrdinal("id"));
                                    map.RId        = dr.GetInt32(dr.GetOrdinal("rid"));
                                    map.Name       = dr.GetString(dr.GetOrdinal("name"));
                                    map.Field      = dr.GetString(dr.GetOrdinal("field"));
                                    map.TableIndex = dr.GetInt32(dr.GetOrdinal("tableindex"));
                                    map.PK         = dr.GetBoolean(dr.GetOrdinal("pk"));
                                    map.AutoId     = dr.GetBoolean(dr.GetOrdinal("autoid"));
                                    rm.Maps.Add(map);
                                }
                            }
                        }
                    }
                }//using
            }

            return(rm);
        }
Exemple #25
0
        /// <summary>
        /// Retrieves the current <see cref="HashHeightPair"/> tip from disk.
        /// </summary>
        /// <returns> Hash of blocks current tip.</returns>
        private HashHeightPair GetTipHash()
        {
            HashHeightPair tipHash = null;

            byte[] row = null;
            lock (this.locker)
            {
                row = this.rocksDb.Get(DBH.Key(BlockHeaderRepositoryConstants.BlockHashHeightTable, BlockHeaderRepositoryConstants.BlockHashHeightKey));
            }

            if (row != null)
            {
                tipHash = this.dBreezeSerializer.Deserialize <HashHeightPair>(row);
            }

            return(tipHash);
        }
Exemple #26
0
        public void InitializesGenesisBlockAndTxIndexOnFirstLoad()
        {
            string dir = CreateTestDir(this);

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
            }

            using (var engine = RocksDb.Open(new DbOptions().SetCreateIfMissing(true), dir))
            {
                byte[] blockRow   = engine.Get(DBH.Key(RocksdbBlockRepository.CommonTableName, new byte[0]));
                bool   txIndexRow = BitConverter.ToBoolean(engine.Get(DBH.Key(RocksdbBlockRepository.CommonTableName, new byte[1])));

                Assert.Equal(this.Network.GetGenesis().GetHash(), this.DataStoreSerializer.Deserialize <HashHeightPair>(blockRow).Hash);
                Assert.False(txIndexRow);
            }
        }
        /// <summary>
        /// Retrieves the current <see cref="HashHeightPair"/> tip from disk.
        /// </summary>
        /// <returns> Hash of blocks current tip.</returns>
        private HashHeightPair GetTipHash()
        {
            HashHeightPair tipHash = null;

            byte[] row = null;
            lock (this.locker)
            {
                row = this.leveldb.Get(DBH.Key(blockHashHeightTable, blockHashHeightKey));
            }

            if (row != null)
            {
                tipHash = this.dataStoreSerializer.Deserialize <HashHeightPair>(row);
            }

            return(tipHash);
        }
        public void GetTrxBlockIdAsyncWithoutTxIndexReturnsDefaultId()
        {
            string dir = CreateTestDir(this);

            using (var engine = new DB(new Options()
            {
                CreateIfMissing = true
            }, dir))
            {
                engine.Put(DBH.Key(BlockRepository.CommonTableName, new byte[0]), this.DataStoreSerializer.Serialize(new HashHeightPair(uint256.Zero, 1)));
                engine.Put(DBH.Key(BlockRepository.CommonTableName, new byte[1]), BitConverter.GetBytes(false));
            }

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
                Assert.Equal(default(uint256), repository.GetBlockIdByTransactionId(new uint256(26)));
            }
        }
        public void GetAsyncWithExistingBlockReturnsBlock()
        {
            string dir   = CreateTestDir(this);
            Block  block = this.Network.Consensus.ConsensusFactory.CreateBlock();

            using (var engine = new DB(new Options()
            {
                CreateIfMissing = true
            }, dir))
            {
                engine.Put(DBH.Key(BlockRepository.BlockTableName, block.GetHash().ToBytes()), block.ToBytes());
            }

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
                Assert.Equal(block.GetHash(), repository.GetBlock(block.GetHash()).GetHash());
            }
        }
        /// <summary>
        /// Inserts <see cref="ProvenBlockHeader"/> items into to the database.
        /// </summary>
        /// <param name="headers"> List of <see cref="ProvenBlockHeader"/> items to save.</param>
        private void InsertHeaders(SortedDictionary <int, ProvenBlockHeader> headers)
        {
            using (var batch = new WriteBatch())
            {
                foreach (KeyValuePair <int, ProvenBlockHeader> header in headers)
                {
                    batch.Put(DBH.Key(provenBlockHeaderTable, BitConverter.GetBytes(header.Key)), this.dataStoreSerializer.Serialize(header.Value));
                }

                lock (this.locker)
                {
                    this.leveldb.Write(batch);
                }
            }

            // Store the latest ProvenBlockHeader in memory.
            this.provenBlockHeaderTip = headers.Last().Value;
        }