Esempio n. 1
0
        private Script GetRandomP2PKScript(out string address)
        {
            var bytes = RandomUtils.GetBytes(33);

            bytes[0] = 0x02;

            Script script = new Script() + Op.GetPushOp(bytes) + OpcodeType.OP_CHECKSIG;

            PubKey[] destinationKeys = script.GetDestinationPublicKeys(this.network);
            address = destinationKeys[0].GetAddress(this.network).ToString();

            return(script);
        }
Esempio n. 2
0
        public void CanIndexAddresses()
        {
            List <ChainedHeader> headers = ChainedHeadersHelper.CreateConsecutiveHeaders(100, null, false, null, this.network);

            this.consensusManagerMock.Setup(x => x.Tip).Returns(() => headers.Last());

            Script p2pk1 = this.GetRandomP2PKScript(out string address1);
            Script p2pk2 = this.GetRandomP2PKScript(out string address2);

            var block1 = new Block()
            {
                Transactions = new List <Transaction>()
                {
                    new Transaction()
                    {
                        Outputs =
                        {
                            new TxOut(new Money(10_000), p2pk1),
                            new TxOut(new Money(20_000), p2pk1),
                            new TxOut(new Money(30_000), p2pk1)
                        }
                    }
                }
            };

            var block5 = new Block()
            {
                Transactions = new List <Transaction>()
                {
                    new Transaction()
                    {
                        Outputs =
                        {
                            new TxOut(new Money(10_000), p2pk1),
                            new TxOut(new Money(1_000),  p2pk2),
                            new TxOut(new Money(1_000),  p2pk2)
                        }
                    }
                }
            };

            var tx = new Transaction();

            tx.Inputs.Add(new TxIn(new OutPoint(block5.Transactions.First().GetHash(), 0)));
            var block10 = new Block()
            {
                Transactions = new List <Transaction>()
                {
                    tx
                }
            };

            this.consensusManagerMock.Setup(x => x.GetBlockData(It.IsAny <uint256>())).Returns((uint256 hash) =>
            {
                ChainedHeader header = headers.SingleOrDefault(x => x.HashBlock == hash);

                switch (header?.Height)
                {
                case 1:
                    return(new ChainedHeaderBlock(block1, header));

                case 5:
                    return(new ChainedHeaderBlock(block5, header));

                case 10:
                    return(new ChainedHeaderBlock(block10, header));
                }

                return(new ChainedHeaderBlock(new Block(), header));
            });

            this.addressIndexer.Initialize();

            TestBase.WaitLoop(() => this.addressIndexer.IndexerTip == headers.Last());

            Assert.Equal(60_000, this.addressIndexer.GetAddressBalances(new[] { address1 }).Balances.First().Balance.Satoshi);
            Assert.Equal(2_000, this.addressIndexer.GetAddressBalances(new[] { address2 }).Balances.First().Balance.Satoshi);

            Assert.Equal(70_000, this.addressIndexer.GetAddressBalances(new[] { address1 }, 93).Balances.First().Balance.Satoshi);

            // Now trigger rewind to see if indexer can handle reorgs.
            ChainedHeader forkPoint = headers.Single(x => x.Height == 8);

            List <ChainedHeader> headersFork = ChainedHeadersHelper.CreateConsecutiveHeaders(100, forkPoint, false, null, this.network);

            this.consensusManagerMock.Setup(x => x.GetBlockData(It.IsAny <uint256>())).Returns((uint256 hash) =>
            {
                ChainedHeader headerFork = headersFork.SingleOrDefault(x => x.HashBlock == hash);

                return(new ChainedHeaderBlock(new Block(), headerFork));
            });

            this.consensusManagerMock.Setup(x => x.Tip).Returns(() => headersFork.Last());
            TestBase.WaitLoop(() => this.addressIndexer.IndexerTip == headersFork.Last());

            Assert.Equal(70_000, this.addressIndexer.GetAddressBalances(new[] { address1 }).Balances.First().Balance.Satoshi);

            this.addressIndexer.Dispose();
        }