示例#1
0
        public void CanReIndex()
        {
            var        source = new BlockStore(TestDataLocations.DataFolder(@"blocks"), Network.StratisMain);
            BlockStore store  = CreateBlockStore("CanReIndexFolder");

            store.AppendAll(source.Enumerate(false).Take(100).Select(b => b.Item));

            var test      = new IndexedBlockStore(new InMemoryNoSqlRepository(), store);
            var reIndexed = test.ReIndex();

            Assert.Equal(100, reIndexed);

            int i = 0;

            foreach (StoredBlock b in store.Enumerate(true))
            {
                Block result = test.Get(b.Item.GetHash());
                Assert.Equal(result.GetHash(), b.Item.GetHash());
                i++;
            }
            Assert.Equal(100, i);

            StoredBlock last = source.Enumerate(false).Skip(100).FirstOrDefault();

            store.Append(last.Item);

            reIndexed = test.ReIndex();
            Assert.Equal(1, reIndexed);

            reIndexed = test.ReIndex();
            Assert.Equal(0, reIndexed);
        }
示例#2
0
        public void CanStoreBlocksInMultipleFiles()
        {
            BlockStore store = CreateBlockStore();

            store.MaxFileSize = 10; // Verify break all block in one respective file with extreme settings.

            var allBlocks = StoredBlock.EnumerateFile(TestDataLocations.DataBlockFolder("blk0001.dat"), network: Network.StratisMain).Take(10).ToList();

            foreach (StoredBlock s in allBlocks)
            {
                store.Append(s.Item);
            }

            var storedBlocks = store.Enumerate(true).ToList();

            Assert.Equal(allBlocks.Count, storedBlocks.Count);

            Assert.Equal(11, store.Folder.GetFiles().Length); //10 files + lock file
        }
示例#3
0
        public void CanStoreBlocks()
        {
            BlockStore store = CreateBlockStore();

            var allBlocks = StoredBlock.EnumerateFile(TestDataLocations.DataBlockFolder("blk0001.dat")).Take(50).ToList();

            foreach (StoredBlock s in allBlocks)
            {
                store.Append(s.Item);
            }

            var storedBlocks = store.Enumerate(true).ToList();

            Assert.Equal(allBlocks.Count, storedBlocks.Count);

            foreach (StoredBlock s in allBlocks)
            {
                StoredBlock retrieved = store.Enumerate(true).First(b => b.Item.GetHash() == s.Item.GetHash());
                Assert.True(retrieved.Item.HeaderOnly);
            }
        }
示例#4
0
        public void CheckBlockProofOfStake()
        {
            var totalblocks = 5000;  // fill only a small portion so test wont be too long
            var mainStore   = new BlockStore(TestDataLocations.BlockFolderLocation, Network.StratisMain);

            // create the stores
            BlockStore store = CreateBlockStore();

            var index      = 0;
            var blockStore = new NoSqlBlockRepository();

            foreach (StoredBlock storedBlock in mainStore.Enumerate(false).Take(totalblocks))
            {
                store.Append(storedBlock.Item);
                blockStore.PutAsync(storedBlock.Item);
                index++;
            }

            // build the chain
            ConcurrentChain chain = store.GetChain();

            // fill the transaction store
            var trxStore = new NoSqlTransactionRepository();
            var mapStore = new BlockTransactionMapStore();

            foreach (ChainedBlock chainedBlock in chain.ToEnumerable(false).Take(totalblocks))
            {
                Block block = blockStore.GetBlock(chainedBlock.HashBlock);
                foreach (Transaction blockTransaction in block.Transactions)
                {
                    trxStore.Put(blockTransaction);
                    mapStore.PutAsync(blockTransaction.GetHash(), block.GetHash());
                }
            }

            RPCClient client = null;

            if (!pos_RPCClientTests.noClient)
            {
                client = new RPCClient(new NetworkCredential("rpcuser", "rpcpassword"),
                                       new Uri("http://127.0.0.1:" + Network.StratisMain.RPCPort), Network.StratisMain);
            }

            var stakeChain = new MemoryStakeChain(Network.StratisMain);

            // validate the stake trasnaction
            foreach (ChainedBlock item in chain.ToEnumerable(false).Take(totalblocks).ToList())
            {
                Block      block = blockStore.GetBlock(item.HashBlock);
                BlockStake blockStake;
                Assert.True(BlockValidator.CheckAndComputeStake(blockStore, trxStore, mapStore, stakeChain, chain, item, block, out blockStake));
                stakeChain.Set(item.HashBlock, blockStake);
                if (item.Height == 1125)
                {
                    var g = block.ToHex();
                }

                if (client != null)
                {
                    RPCBlock fetched = client.GetRPCBlockAsync(item.HashBlock).Result;
                    Assert.Equal(uint256.Parse(fetched.modifierv2), blockStake.StakeModifierV2);
                    Assert.Equal(uint256.Parse(fetched.proofhash), blockStake.HashProof);
                }
            }
        }
        private Block PushStore(BlockStore store, Transaction tx, Block prev = null)
        {
            if(prev == null)
                prev = Network.Main.GetGenesis();
            var b = new Block()
               {
                   Header =
                   {
                       Nonce = RandomUtils.GetUInt32(),
                       HashPrevBlock = prev.GetHash()
                   },
                   Transactions =
					{
						tx
					}
               };
            store.Append(b);
            return b;
        }