protected virtual void OnInsertBlocks(DBreeze.Transactions.Transaction dbreezeTransaction, 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()));

            // 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.
                Row <byte[], byte[]> blockRow = dbreezeTransaction.Select <byte[], byte[]>(BlockTableName, blockId.ToBytes());
                if (!blockRow.Exists)
                {
                    dbreezeTransaction.Insert <byte[], byte[]>(BlockTableName, blockId.ToBytes(), this.dBreezeSerializer.Serialize(block));

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

            if (this.TxIndex)
            {
                this.OnInsertTransactions(dbreezeTransaction, transactions);
            }
        }
Exemple #2
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;

                    batch.Put(DBH.Key(BlockTableName, blockId.ToBytes()), this.dataStoreSerializer.Serialize(block));

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

                this.rocksdb.Write(batch);
            }

            if (this.TxIndex)
            {
                this.OnInsertTransactions(transactions);
            }
        }