Пример #1
0
        public static void CanCalculatePowPosCorrectly()
        {
            var store      = new BlockStore(TestDataLocations.BlockFolderLocation, Network.Main);
            var chain      = store.GetChain();
            var stakeChain = new MemoryStakeChain(Network.Main);
            var indexStore = new IndexedBlockStore(new InMemoryNoSqlRepository(), store);
            var reindexed  = indexStore.ReIndex();

            Assert.Equal(reindexed, 103952);

            foreach (var chainedBlock in chain.EnumerateAfter(chain.Genesis))
            {
                var block      = indexStore.Get(chainedBlock.HashBlock);
                var blockstake = new BlockStake(block);
                stakeChain.Set(chainedBlock.HashBlock, blockstake);

                Assert.True(stakeChain.CheckPowPosAndTarget(chainedBlock, blockstake, Network.Main));
            }
        }
Пример #2
0
        public void CanCalculateDifficulty()
        {
            var histories = File.ReadAllLines(TestDataLocations.DataFolder(@"targethistory.csv"));

            var store = new BlockStore(TestDataLocations.BlockFolderLocation, Network.Main);
            // todo: load the chain with a header only file
            ConcurrentChain chain = store.GetChain();

            var stakeChain = new MemoryStakeChain(Network.Main);
            var indexStore = new IndexedBlockStore(new InMemoryNoSqlRepository(), store);
            var reindexed  = indexStore.ReIndex();

            Assert.Equal(reindexed, 103952);

            var lastIndex = 0;

            foreach (var history in histories)
            {
                var height         = int.Parse(history.Split(',')[0]);
                var expectedTarget = new Target(new BigInteger(history.Split(',')[1].Trim(), 10));

                var chainedBlock = chain.GetBlock(height);
                for (int i = height; i > lastIndex; i--)
                {
                    var g     = chain.GetBlock(i);
                    var block = indexStore.Get(g.HashBlock);
                    stakeChain.Set(g.HashBlock, new BlockStake(block));
                }
                lastIndex = height;

                Assert.Equal(expectedTarget, chainedBlock.Header.Bits);
                var target = stakeChain.GetWorkRequired(chainedBlock, stakeChain.Get(chainedBlock.HashBlock), Network.Main.Consensus);
                //var target = chain.GetWorkRequired(Network.Main, height);
                Assert.Equal(expectedTarget, target);
            }
        }
Пример #3
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);
                }
            }
        }