Exemple #1
0
        /// <summary>
        /// Initializes a new instance of <see cref="TransactionVerifier"/> using given parameters.
        /// </summary>
        /// <exception cref="ArgumentNullException"/>
        /// <param name="isMempool">Indicates whether this instance is used by the memory pool or block</param>
        /// <param name="utxoDatabase">UTXO database</param>
        /// <param name="memoryPool">Memory pool</param>
        /// <param name="consensus">Consensus rules</param>
        public TransactionVerifier(bool isMempool, IUtxoDatabase utxoDatabase, IMemoryPool memoryPool, IConsensus consensus)
        {
            if (utxoDatabase is null)
            {
                throw new ArgumentNullException(nameof(utxoDatabase), "UTXO database can not be null.");
            }
            if (memoryPool is null)
            {
                throw new ArgumentNullException(nameof(memoryPool), "Memory pool can not be null.");
            }
            if (consensus is null)
            {
                throw new ArgumentNullException(nameof(consensus), "Consensus rules can not be null.");
            }

            this.isMempool = isMempool;
            utxoDb         = utxoDatabase;
            mempool        = memoryPool;
            this.consensus = consensus;

            calc    = new EllipticCurveCalculator();
            scrSer  = new ScriptSerializer();
            hash160 = new Ripemd160Sha256();
            sha256  = new Sha256();
        }
Exemple #2
0
        public void TxTest(IUtxoDatabase utxoDb, IMemoryPool mempool, IConsensus consensus, ITransaction tx, bool isCoinbase)
        {
            var verifier = new TransactionVerifier(false, utxoDb, mempool, consensus);

            bool b = isCoinbase ?
                     verifier.VerifyCoinbasePrimary(tx, out string error) :
                     verifier.Verify(tx, out error);

            Assert.False(b, TxValidTests.BuildErrorStr(tx, error));
            Assert.NotNull(error);
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of <see cref="ClientSettings"/> with the given parameters.
        /// </summary>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <param name="listen">True to open a listening socket; false otherwise</param>
        /// <param name="netType">Network type</param>
        /// <param name="servs">Services supported by this node</param>
        /// <param name="nodes">List of peers (can be null)</param>
        /// <param name="fileMan">File manager</param>
        /// <param name="utxoDb">UTXO database</param>
        /// <param name="memPool">Memory pool</param>
        /// <param name="maxConnection">Maximum number of connections</param>
        public ClientSettings(bool listen, NetworkType netType, int maxConnection, NodeServiceFlags servs,
                              NodePool nodes, IFileManager fileMan, IUtxoDatabase utxoDb, IMemoryPool memPool)
        {
            // TODO: add AcceptSAEAPool here based on listen
            AcceptIncomingConnections = listen;
            Network            = netType;
            MaxConnectionCount = maxConnection;
            Services           = servs;
            AllNodes           = nodes ?? new NodePool(maxConnection);
            FileMan            = fileMan ?? throw new ArgumentNullException();

            DefaultPort = Network switch
            {
                NetworkType.MainNet => Constants.MainNetPort,
                NetworkType.TestNet => Constants.TestNetPort,
                NetworkType.RegTest => Constants.RegTestPort,
                _ => throw new ArgumentException("Undefined network"),
            };

            ListenPort = DefaultPort;

            // TODO: the following values are for testing, they should be set by the caller
            //       they need more checks for correct and optimal values
            BufferLength = 16384; // 16 KB
            int totalBytes = BufferLength * MaxConnectionCount * 2;

            MaxConnectionEnforcer = new Semaphore(MaxConnectionCount, MaxConnectionCount);
            SendReceivePool       = new SocketAsyncEventArgsPool(MaxConnectionCount * 2);
            // TODO: can Memory<byte> be used here instead of byte[]?
            byte[] bufferBlock = new byte[totalBytes];
            for (int i = 0; i < MaxConnectionCount * 2; i++)
            {
                var sArg = new SocketAsyncEventArgs();
                sArg.SetBuffer(bufferBlock, i * BufferLength, BufferLength);
                SendReceivePool.Push(sArg);
            }

            // TODO: find a better way for this
            supportsIpV6 = NetworkInterface.GetAllNetworkInterfaces().All(x => x.Supports(NetworkInterfaceComponent.IPv6));

            var c     = new Consensus(netType);
            var txVer = new TransactionVerifier(false, utxoDb, memPool, c);

            Blockchain = new Blockchain.Blockchain(FileMan, new BlockVerifier(txVer, c), c)
            {
                Time  = Time,
                State = BlockchainState.None
            };
        }
Exemple #4
0
        public void VerifyTest(IUtxoDatabase utxoDB, IMemoryPool memPool, IConsensus c, ITransaction tx, bool expB, string expErr,
                               int expSigOp, ulong expFee, bool expSeg)
        {
            // An initial amount is set for both TotalFee and TotalSigOpCount to make sure Verify()
            // method always adds to previous values instead of setting them
            ulong initialFee   = 10;
            int   initialSigOp = 50;

            var verifier = new TransactionVerifier(false, utxoDB, memPool, c)
            {
                TotalFee        = initialFee,
                TotalSigOpCount = initialSigOp
            };

            bool actualB = verifier.Verify(tx, out string error);

            Assert.Equal(expB, actualB);
            Assert.Equal(expErr, error);
            Assert.Equal(expSigOp + initialSigOp, verifier.TotalSigOpCount);
            Assert.Equal(expFee + initialFee, verifier.TotalFee);
            Assert.Equal(expSeg, verifier.AnySegWit);
        }