Ejemplo n.º 1
0
        public void Setup()
        {
            IDbProvider                  memDbProvider            = TestMemDbProvider.Init();
            TrieStore                    trieStore                = new (new MemDb(), LimboLogs.Instance);
            StateProvider                stateProvider            = new (trieStore, memDbProvider.CodeDb, LimboLogs.Instance);
            StorageProvider              storageProvider          = new (trieStore, stateProvider, LimboLogs.Instance);
            ChainLevelInfoRepository     chainLevelInfoRepository = new (memDbProvider);
            ISpecProvider                specProvider             = MainnetSpecProvider.Instance;
            IBloomStorage                bloomStorage             = NullBloomStorage.Instance;
            EthereumEcdsa                ecdsa = new (1, LimboLogs.Instance);
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(specProvider, _blockTree);

            _blockTree = new BlockTree(
                memDbProvider,
                chainLevelInfoRepository,
                specProvider,
                bloomStorage,
                new SyncConfig(),
                LimboLogs.Instance);
            TxPool.TxPool txPool = new (
                ecdsa,
                new ChainHeadInfoProvider(specProvider, _blockTree, stateProvider),
                new TxPoolConfig(),
                new TxValidator(specProvider.ChainId),
                LimboLogs.Instance,
                transactionComparerProvider.GetDefaultComparer());
            BlockhashProvider blockhashProvider = new (_blockTree, LimboLogs.Instance);
            VirtualMachine    virtualMachine    = new(
                blockhashProvider,
                specProvider,
                LimboLogs.Instance);
            TransactionProcessor transactionProcessor = new (
                specProvider,
                stateProvider,
                storageProvider,
                virtualMachine,
                LimboLogs.Instance);

            BlockProcessor blockProcessor = new (
                MainnetSpecProvider.Instance,
                Always.Valid,
                new RewardCalculator(specProvider),
                new BlockProcessor.BlockValidationTransactionsExecutor(transactionProcessor, stateProvider),
                stateProvider,
                storageProvider,
                NullReceiptStorage.Instance,
                new WitnessCollector(memDbProvider.StateDb, LimboLogs.Instance),
                LimboLogs.Instance);

            _blockchainProcessor = new BlockchainProcessor(
                _blockTree,
                blockProcessor,
                new RecoverSignatures(
                    ecdsa,
                    txPool,
                    specProvider,
                    LimboLogs.Instance),
                LimboLogs.Instance, BlockchainProcessor.Options.Default);
        }
Ejemplo n.º 2
0
        public void parity_netPeers_null_ActivePeers()
        {
            LimboLogs           logger        = LimboLogs.Instance;
            MainnetSpecProvider specProvider  = MainnetSpecProvider.Instance;
            EthereumEcdsa       ethereumEcdsa = new EthereumEcdsa(specProvider.ChainId, logger);
            InMemoryTxStorage   txStorage     = new InMemoryTxStorage();

            IDb        blockDb     = new MemDb();
            IDb        headerDb    = new MemDb();
            IDb        blockInfoDb = new MemDb();
            IBlockTree blockTree   = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, NullBloomStorage.Instance, LimboLogs.Instance);
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(specProvider, blockTree);

            TxPool.TxPool txPool = new TxPool.TxPool(txStorage, ethereumEcdsa, new ChainHeadInfoProvider(specProvider, blockTree, new StateReader(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance)), new TxPoolConfig(),
                                                     new TxValidator(specProvider.ChainId), LimboLogs.Instance, transactionComparerProvider.GetDefaultComparer());

            new OnChainTxWatcher(blockTree, txPool, specProvider, LimboLogs.Instance);
            IReceiptStorage receiptStorage = new InMemoryReceiptStorage();

            IPeerManager peerManager = Substitute.For <IPeerManager>();

            IParityRpcModule parityRpcModule = new ParityRpcModule(ethereumEcdsa, txPool, blockTree, receiptStorage, new Enode(TestItem.PublicKeyA, IPAddress.Loopback, 8545),
                                                                   _signerStore, new MemKeyStore(new[] { TestItem.PrivateKeyA }), logger, peerManager);
            string serialized     = RpcTest.TestSerializedRequest(parityRpcModule, "parity_netPeers");
            string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":{\"active\":0,\"connected\":0,\"max\":0,\"peers\":[]},\"id\":67}";

            Assert.AreEqual(expectedResult, serialized);
        }
Ejemplo n.º 3
0
 protected virtual TxPool.TxPool CreateTxPool(ITxStorage txStorage) =>
 new TxPool.TxPool(
     txStorage,
     EthereumEcdsa,
     new ChainHeadInfoProvider(new FixedBlockChainHeadSpecProvider(SpecProvider), BlockTree, State),
     new TxPoolConfig(),
     new TxValidator(SpecProvider.ChainId),
     LogManager,
     TransactionComparerProvider.GetDefaultComparer());
Ejemplo n.º 4
0
        private TxPool.TxPool CreatePool(ITxStorage txStorage, ITxPoolConfig config = null, ISpecProvider specProvider = null)
        {
            specProvider ??= RopstenSpecProvider.Instance;
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(specProvider, _blockTree);

            return(new TxPool.TxPool(txStorage, _ethereumEcdsa, new ChainHeadInfoProvider(specProvider, _blockFinder, _stateProvider),
                                     config ?? new TxPoolConfig()
            {
                GasLimit = _txGasLimit
            },
                                     new TxValidator(_specProvider.ChainId), _logManager, transactionComparerProvider.GetDefaultComparer()));
        }
Ejemplo n.º 5
0
        public static INdmBlockchainBridge BuildABridge()
        {
            IDbProvider memDbProvider = TestMemDbProvider.Init();
            StateReader stateReader   = new StateReader(
                new TrieStore(memDbProvider.StateDb, LimboLogs.Instance), memDbProvider.CodeDb, LimboLogs.Instance);
            var                          trieStore     = new TrieStore(memDbProvider.StateDb, LimboLogs.Instance);
            StateProvider                stateProvider = new StateProvider(trieStore, memDbProvider.CodeDb, LimboLogs.Instance);
            IEthereumEcdsa               ecdsa         = new EthereumEcdsa(ChainId.Mainnet, LimboLogs.Instance);
            BlockTree                    blockTree     = Build.A.BlockTree().OfChainLength(1).TestObject;
            MainnetSpecProvider          specProvider  = MainnetSpecProvider.Instance;
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(MainnetSpecProvider.Instance, blockTree);
            ITxPool txPool = new TxPool.TxPool(
                new InMemoryTxStorage(), ecdsa,
                new ChainHeadInfoProvider(specProvider, blockTree, stateProvider),
                new TxPoolConfig(),
                new TxValidator(specProvider.ChainId),
                LimboLogs.Instance,
                transactionComparerProvider.GetDefaultComparer());
            IWallet          wallet           = new DevWallet(new WalletConfig(), LimboLogs.Instance);
            ReceiptsRecovery receiptsRecovery = new ReceiptsRecovery(ecdsa, specProvider);
            LogFinder        logFinder        = new LogFinder(blockTree, new InMemoryReceiptStorage(), NullBloomStorage.Instance,
                                                              LimboLogs.Instance, receiptsRecovery, 1024);

            ReadOnlyTxProcessingEnv processingEnv = new ReadOnlyTxProcessingEnv(
                new ReadOnlyDbProvider(memDbProvider, false),
                new TrieStore(memDbProvider.StateDb, LimboLogs.Instance).AsReadOnly(memDbProvider.StateDb),
                new ReadOnlyBlockTree(blockTree),
                specProvider, LimboLogs.Instance);
            BlockchainBridge blockchainBridge = new BlockchainBridge(
                processingEnv,
                txPool,
                new InMemoryReceiptStorage(),
                NullFilterStore.Instance,
                NullFilterManager.Instance,
                ecdsa,
                Timestamper.Default,
                logFinder,
                specProvider,
                false,
                false);

            WalletTxSigner txSigner  = new WalletTxSigner(wallet, ChainId.Mainnet);
            ITxSealer      txSealer0 = new TxSealer(txSigner, Timestamper.Default);
            ITxSealer      txSealer1 = new NonceReservingTxSealer(txSigner, Timestamper.Default, txPool);
            ITxSender      txSender  = new TxPoolSender(txPool, txSealer0, txSealer1);

            return(new NdmBlockchainBridge(blockchainBridge, blockTree, stateReader, txSender));
        }
Ejemplo n.º 6
0
        protected BlockProducerContext GetProducerChain(IBlockTree blockTree,
                                                        IDbProvider dbProvider,
                                                        IReadOnlyTrieStore readOnlyTrieStore,
                                                        IBlockPreprocessorStep blockPreprocessor,
                                                        ITxPool txPool,
                                                        IBlockValidator blockValidator,
                                                        IRewardCalculatorSource rewardCalculatorSource,
                                                        IReceiptStorage receiptStorage,
                                                        ISpecProvider specProvider,
                                                        IMiningConfig miningConfig,
                                                        ILogManager logManager)
        {
            ReadOnlyDbProvider readOnlyDbProvider = dbProvider.AsReadOnly(false);
            ReadOnlyBlockTree  readOnlyBlockTree  = blockTree.AsReadOnly();

            ReadOnlyTxProcessingEnv txProcessingEnv =
                new(readOnlyDbProvider, readOnlyTrieStore, readOnlyBlockTree, specProvider, logManager);
            ITransactionComparerProvider transactionComparerProvider = new TransactionComparerProvider(specProvider, blockTree);

            BlockProcessor blockProcessor =
                CreateBlockProcessor(txProcessingEnv,
                                     specProvider,
                                     blockValidator,
                                     rewardCalculatorSource,
                                     txPool,
                                     receiptStorage,
                                     logManager);

            IBlockchainProcessor blockchainProcessor =
                new BlockchainProcessor(
                    readOnlyBlockTree,
                    blockProcessor,
                    blockPreprocessor,
                    logManager,
                    BlockchainProcessor.Options.NoReceipts);

            OneTimeChainProcessor chainProcessor = new(
                readOnlyDbProvider,
                blockchainProcessor);

            return(new BlockProducerContext
            {
                ChainProcessor = chainProcessor,
                ReadOnlyStateProvider = txProcessingEnv.StateProvider,
                TxSource = CreateTxSourceForProducer(txProcessingEnv, txPool, specProvider, logManager, miningConfig, transactionComparerProvider),
                ReadOnlyTxProcessingEnv = txProcessingEnv
            });
        }
Ejemplo n.º 7
0
        protected async Task Prepare()
        {
            _wallet          = new DevWallet(new WalletConfig(), _logManager);
            _feeAccount      = _wallet.GetAccounts()[0];
            _consumerAccount = _wallet.GetAccounts()[1];
            _providerAccount = _wallet.GetAccounts()[2];
            _ndmConfig       = new NdmConfig();

            IReleaseSpec  spec         = _releaseSpec;
            ISpecProvider specProvider = new SingleReleaseSpecProvider(spec, 99);
            MemDb         stateDb      = new MemDb();
            TrieStore     trieStore    = new TrieStore(stateDb, _logManager);

            _state = new StateProvider(trieStore, new MemDb(), _logManager);
            StorageProvider storageProvider = new StorageProvider(trieStore, _state, _logManager);

            _state.CreateAccount(_consumerAccount, 1000.Ether());
            _state.CreateAccount(_providerAccount, 1.Ether());
            _state.Commit(spec);
            _state.CommitTree(0);

            VirtualMachine machine =
                new VirtualMachine(Substitute.For <IBlockhashProvider>(), specProvider, _logManager);
            TransactionProcessor processor = new TransactionProcessor(specProvider, _state, storageProvider, machine, _logManager);

            _bridge = new BlockchainBridge(processor);

            TxReceipt receipt = await DeployContract(Bytes.FromHexString(ContractData.GetInitCode(_feeAccount)));

            ((NdmConfig)_ndmConfig).ContractAddress = receipt.ContractAddress.ToString();
            _contractAddress = receipt.ContractAddress;
            IBlockTree blockTree = Substitute.For <IBlockTree>();
            Block      block     = Build.A.Block.WithNumber(0).TestObject;

            blockTree.Head.Returns(block);
            TransactionComparerProvider transactionComparerProvider = new TransactionComparerProvider(specProvider, blockTree);

            _txPool = new TxPool.TxPool(
                new EthereumEcdsa(specProvider.ChainId, _logManager),
                new ChainHeadInfoProvider(specProvider, blockTree, _state),
                new TxPoolConfig(),
                new TxValidator(specProvider.ChainId),
                _logManager,
                transactionComparerProvider.GetDefaultComparer());
            _ndmBridge = new NdmBlockchainBridge(_bridge, _bridge, _bridge, _bridge);
        }
Ejemplo n.º 8
0
        public void Setup()
        {
            ISpecProvider specProvider = Substitute.For <ISpecProvider>();
            IBlockTree    blockTree    = Substitute.For <IBlockTree>();
            Block         block        = Build.A.Block.WithNumber(0).TestObject;

            blockTree.Head.Returns(block);
            specProvider.GetSpec(Arg.Any <long>()).Returns(new ReleaseSpec()
            {
                IsEip1559Enabled = false
            });
            ITransactionComparerProvider transactionComparerProvider = new TransactionComparerProvider(specProvider, blockTree);

            _sortedPool = new TxDistinctSortedPool(Capacity, transactionComparerProvider.GetDefaultComparer(), LimboLogs.Instance);
            for (int i = 0; i < _transactions.Length; i++)
            {
                UInt256 gasPrice = (UInt256)i;
                _transactions[i] = Build.A.Transaction.WithGasPrice(gasPrice)
                                   .WithSenderAddress(Address.FromNumber(gasPrice)).TestObject;
            }
        }
Ejemplo n.º 9
0
            public On CreateNode(PrivateKey privateKey, bool withGenesisAlreadyProcessed = false)
            {
                if (_logger.IsInfo)
                {
                    _logger.Info($"CREATING NODE {privateKey.Address}");
                }
                _logManagers[privateKey] = LimboLogs.Instance;
//                _logManagers[privateKey] = new OneLoggerLogManager(new ConsoleAsyncLogger(LogLevel.Debug, $"{privateKey.Address} "));
                var nodeLogManager = _logManagers[privateKey];

                AutoResetEvent newHeadBlockEvent = new AutoResetEvent(false);

                _blockEvents.Add(privateKey, newHeadBlockEvent);

                MemDb blocksDb    = new MemDb();
                MemDb headersDb   = new MemDb();
                MemDb blockInfoDb = new MemDb();

                MemDb stateDb = new MemDb();
                MemDb codeDb  = new MemDb();

                ISpecProvider specProvider = RinkebySpecProvider.Instance;

                var           trieStore     = new TrieStore(stateDb, nodeLogManager);
                StateReader   stateReader   = new StateReader(trieStore, codeDb, nodeLogManager);
                StateProvider stateProvider = new StateProvider(trieStore, codeDb, nodeLogManager);

                stateProvider.CreateAccount(TestItem.PrivateKeyD.Address, 100.Ether());
                GoerliSpecProvider goerliSpecProvider = GoerliSpecProvider.Instance;

                stateProvider.Commit(goerliSpecProvider.GenesisSpec);
                stateProvider.CommitTree(0);

                BlockTree blockTree = new BlockTree(blocksDb, headersDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), goerliSpecProvider, NullBloomStorage.Instance, nodeLogManager);

                blockTree.NewHeadBlock += (sender, args) => { _blockEvents[privateKey].Set(); };
                ITransactionComparerProvider transactionComparerProvider =
                    new TransactionComparerProvider(specProvider, blockTree);

                TxPool.TxPool txPool = new TxPool.TxPool(new InMemoryTxStorage(), _ethereumEcdsa, new ChainHeadInfoProvider(new FixedBlockChainHeadSpecProvider(GoerliSpecProvider.Instance), blockTree, stateProvider), new TxPoolConfig(), new TxValidator(goerliSpecProvider.ChainId), _logManager, transactionComparerProvider.GetDefaultComparer());
                _pools[privateKey] = txPool;

                BlockhashProvider blockhashProvider = new BlockhashProvider(blockTree, LimboLogs.Instance);

                _blockTrees.Add(privateKey, blockTree);

                SnapshotManager snapshotManager = new SnapshotManager(_cliqueConfig, blocksDb, blockTree, _ethereumEcdsa, nodeLogManager);

                _snapshotManager[privateKey] = snapshotManager;
                CliqueSealer cliqueSealer = new CliqueSealer(new Signer(ChainId.Goerli, privateKey, LimboLogs.Instance), _cliqueConfig, snapshotManager, nodeLogManager);



                _genesis.Header.StateRoot       = _genesis3Validators.Header.StateRoot = stateProvider.StateRoot;
                _genesis.Header.Hash            = _genesis.Header.CalculateHash();
                _genesis3Validators.Header.Hash = _genesis3Validators.Header.CalculateHash();

                StorageProvider      storageProvider      = new StorageProvider(trieStore, stateProvider, nodeLogManager);
                TransactionProcessor transactionProcessor = new TransactionProcessor(goerliSpecProvider, stateProvider, storageProvider, new VirtualMachine(stateProvider, storageProvider, blockhashProvider, specProvider, nodeLogManager), nodeLogManager);

                BlockProcessor blockProcessor = new BlockProcessor(
                    goerliSpecProvider,
                    Always.Valid,
                    NoBlockRewards.Instance,
                    transactionProcessor,
                    stateProvider,
                    storageProvider,
                    txPool,
                    NullReceiptStorage.Instance,
                    NullWitnessCollector.Instance,
                    nodeLogManager);

                BlockchainProcessor processor = new BlockchainProcessor(blockTree, blockProcessor, new AuthorRecoveryStep(snapshotManager), nodeLogManager, BlockchainProcessor.Options.NoReceipts);

                processor.Start();

                var minerTrieStore = trieStore.AsReadOnly();

                StateProvider        minerStateProvider        = new StateProvider(minerTrieStore, codeDb, nodeLogManager);
                StorageProvider      minerStorageProvider      = new StorageProvider(minerTrieStore, minerStateProvider, nodeLogManager);
                VirtualMachine       minerVirtualMachine       = new VirtualMachine(minerStateProvider, minerStorageProvider, blockhashProvider, specProvider, nodeLogManager);
                TransactionProcessor minerTransactionProcessor = new TransactionProcessor(goerliSpecProvider, minerStateProvider, minerStorageProvider, minerVirtualMachine, nodeLogManager);

                BlockProcessor minerBlockProcessor = new BlockProcessor(
                    goerliSpecProvider,
                    Always.Valid,
                    NoBlockRewards.Instance,
                    minerTransactionProcessor,
                    minerStateProvider,
                    minerStorageProvider,
                    txPool,
                    NullReceiptStorage.Instance,
                    NullWitnessCollector.Instance,
                    nodeLogManager);

                BlockchainProcessor minerProcessor = new BlockchainProcessor(blockTree, minerBlockProcessor, new AuthorRecoveryStep(snapshotManager), nodeLogManager, BlockchainProcessor.Options.NoReceipts);

                if (withGenesisAlreadyProcessed)
                {
                    ProcessGenesis(privateKey);
                }

                ITxFilterPipeline   txFilterPipeline = TxFilterPipelineBuilder.CreateStandardFilteringPipeline(nodeLogManager, specProvider);
                TxPoolTxSource      txPoolTxSource   = new TxPoolTxSource(txPool, stateReader, specProvider, transactionComparerProvider, nodeLogManager, txFilterPipeline);
                CliqueBlockProducer blockProducer    = new CliqueBlockProducer(
                    txPoolTxSource,
                    minerProcessor,
                    minerStateProvider,
                    blockTree,
                    _timestamper,
                    new CryptoRandom(),
                    snapshotManager,
                    cliqueSealer,
                    new TargetAdjustedGasLimitCalculator(goerliSpecProvider, new MiningConfig()),
                    MainnetSpecProvider.Instance,
                    _cliqueConfig,
                    nodeLogManager);

                blockProducer.Start();

                _producers.Add(privateKey, blockProducer);

                return(this);
            }
Ejemplo n.º 10
0
        public void order_is_correct(Func <IEnumerable <Transaction>, IEnumerable <Transaction> > transactionSelect)
        {
            var sendersWhitelist = Substitute.For <IContractDataStore <Address> >();
            var priorities       = Substitute.For <IDictionaryContractDataStore <TxPriorityContract.Destination> >();
            var blockHeader      = Build.A.BlockHeader.TestObject;

            byte[] p5Signature = { 0, 1, 2, 3 };
            byte[] p6Signature = { 0, 0, 0, 2 };
            byte[] p0signature = { 0, 0, 0, 1 };
            sendersWhitelist.GetItemsFromContractAtBlock(blockHeader).Returns(WhitelistedSenders);

            SetPriority(priorities, blockHeader, TestItem.AddressB, p5Signature, 5);
            SetPriority(priorities, blockHeader, TestItem.AddressB, p6Signature, 6);

            Transaction A_B_0_10_10_P6 = Build.A.NamedTransaction(nameof(A_B_0_10_10_P6))
                                         .WithSenderAddress(TestItem.AddressA)
                                         .To(TestItem.AddressB)
                                         .WithNonce(0)
                                         .WithGasPrice(10)
                                         .WithGasLimit(10)
                                         .WithData(p6Signature)
                                         .TestObject;

            Transaction A_B_0_10_20_P6 = Build.A.NamedTransaction(nameof(A_B_0_10_20_P6))
                                         .WithSenderAddress(TestItem.AddressA)
                                         .To(TestItem.AddressB)
                                         .WithNonce(0)
                                         .WithGasPrice(10)
                                         .WithGasLimit(20)
                                         .WithData(p6Signature)
                                         .TestObject;

            Transaction A_B_0_10_5_P6 = Build.A.NamedTransaction(nameof(A_B_0_10_5_P6))
                                        .WithSenderAddress(TestItem.AddressA)
                                        .To(TestItem.AddressB)
                                        .WithNonce(0)
                                        .WithGasPrice(10)
                                        .WithGasLimit(5)
                                        .WithData(p6Signature)
                                        .TestObject;

            Transaction A_B_0_10_10_P5 = Build.A.NamedTransaction(nameof(A_B_0_10_10_P5))
                                         .WithSenderAddress(TestItem.AddressA)
                                         .To(TestItem.AddressB)
                                         .WithNonce(0)
                                         .WithGasPrice(10)
                                         .WithGasLimit(10)
                                         .WithData(p5Signature)
                                         .TestObject;

            Transaction A_B_0_20_10_P5 = Build.A.NamedTransaction(nameof(A_B_0_20_10_P5))
                                         .WithSenderAddress(TestItem.AddressA)
                                         .To(TestItem.AddressB)
                                         .WithNonce(0)
                                         .WithGasPrice(20)
                                         .WithGasLimit(10)
                                         .WithData(p5Signature)
                                         .TestObject;

            Transaction A_B_1_20_10_P5 = Build.A.NamedTransaction(nameof(A_B_1_20_10_P5))
                                         .WithSenderAddress(TestItem.AddressA)
                                         .To(TestItem.AddressB)
                                         .WithNonce(1)
                                         .WithGasPrice(20)
                                         .WithGasLimit(10)
                                         .WithData(p5Signature)
                                         .TestObject;

            Transaction A_B_1_200_10_P0 = Build.A.NamedTransaction(nameof(A_B_1_200_10_P0))
                                          .WithSenderAddress(TestItem.AddressA)
                                          .To(TestItem.AddressB)
                                          .WithNonce(1)
                                          .WithGasPrice(200)
                                          .WithGasLimit(10)
                                          .WithData(p0signature)
                                          .TestObject;

            Transaction A_A_0_100_100_P0 = Build.A.NamedTransaction(nameof(A_A_0_100_100_P0))
                                           .WithSenderAddress(TestItem.AddressA)
                                           .To(TestItem.AddressA)
                                           .WithNonce(0)
                                           .WithGasPrice(100)
                                           .WithGasLimit(100)
                                           .WithData(p5Signature)
                                           .TestObject;

            Transaction A_A_1_100_100_P0 = Build.A.NamedTransaction(nameof(A_A_1_100_100_P0))
                                           .WithSenderAddress(TestItem.AddressA)
                                           .To(TestItem.AddressA)
                                           .WithNonce(1)
                                           .WithGasPrice(100)
                                           .WithGasLimit(100)
                                           .WithData(p5Signature)
                                           .TestObject;

            Transaction A_A_1_1000_1000_P0 = Build.A.NamedTransaction(nameof(A_A_1_1000_1000_P0))
                                             .WithSenderAddress(TestItem.AddressA)
                                             .To(TestItem.AddressA)
                                             .WithNonce(1)
                                             .WithGasPrice(1000)
                                             .WithGasLimit(1000)
                                             .WithData(p5Signature)
                                             .TestObject;

            Transaction A_A_2_1000_1_P0 = Build.A.NamedTransaction(nameof(A_A_2_1000_1_P0))
                                          .WithSenderAddress(TestItem.AddressA)
                                          .To(TestItem.AddressA)
                                          .WithNonce(2)
                                          .WithGasPrice(1000)
                                          .WithGasLimit(1)
                                          .WithData(p5Signature)
                                          .TestObject;

            Transaction B_B_2_1000_1_P6 = Build.A.NamedTransaction(nameof(B_B_2_1000_1_P6))
                                          .WithSenderAddress(TestItem.AddressB)
                                          .To(TestItem.AddressB)
                                          .WithNonce(2)
                                          .WithGasPrice(1000)
                                          .WithGasLimit(1)
                                          .WithData(p6Signature)
                                          .TestObject;

            Transaction B_B_2_15_1_P5 = Build.A.NamedTransaction(nameof(B_B_2_15_1_P5))
                                        .WithSenderAddress(TestItem.AddressB)
                                        .To(TestItem.AddressB)
                                        .WithNonce(2)
                                        .WithGasPrice(15)
                                        .WithGasLimit(1)
                                        .WithData(p5Signature)
                                        .TestObject;

            Transaction B_B_3_15_1_P6 = Build.A.NamedTransaction(nameof(B_B_3_15_1_P6))
                                        .WithSenderAddress(TestItem.AddressB)
                                        .To(TestItem.AddressB)
                                        .WithNonce(3)
                                        .WithGasPrice(15)
                                        .WithGasLimit(1)
                                        .WithData(p6Signature)
                                        .TestObject;

            Transaction C_B_3_10_1_P6_W = Build.A.NamedTransaction(nameof(C_B_3_10_1_P6_W))
                                          .WithSenderAddress(TestItem.AddressC)
                                          .To(TestItem.AddressB)
                                          .WithNonce(3)
                                          .WithGasPrice(10)
                                          .WithGasLimit(1)
                                          .WithData(p6Signature)
                                          .TestObject;

            Transaction C_B_4_10_1_P0_W = Build.A.NamedTransaction(nameof(C_B_4_10_1_P0_W))
                                          .WithSenderAddress(TestItem.AddressC)
                                          .To(TestItem.AddressB)
                                          .WithNonce(4)
                                          .WithGasPrice(10)
                                          .WithGasLimit(1)
                                          .TestObject;

            Transaction D_B_4_100_1_P0_W = Build.A.NamedTransaction(nameof(D_B_4_100_1_P0_W))
                                           .WithSenderAddress(TestItem.AddressD)
                                           .To(TestItem.AddressB)
                                           .WithNonce(4)
                                           .WithGasPrice(100)
                                           .WithGasLimit(1)
                                           .TestObject;

            IEnumerable <Transaction> transactions = new []
            {
                A_B_1_200_10_P0,
                A_A_2_1000_1_P0,
                A_A_1_1000_1000_P0,
                C_B_4_10_1_P0_W,
                A_A_1_100_100_P0,
                A_A_0_100_100_P0,
                A_B_0_10_10_P5,
                D_B_4_100_1_P0_W,
                C_B_3_10_1_P6_W,
                A_B_0_10_10_P6,
                A_B_0_20_10_P5,
                B_B_2_15_1_P5,
                A_B_0_10_5_P6,
                B_B_3_15_1_P6,
                A_B_0_10_20_P6,
                A_B_1_20_10_P5,
                B_B_2_1000_1_P6
            };

            IEnumerable <Transaction> expectation = new []
            {
                C_B_3_10_1_P6_W,
                D_B_4_100_1_P0_W,
                C_B_4_10_1_P0_W,
                B_B_2_1000_1_P6,
                A_B_0_10_5_P6,
                A_B_0_10_10_P6,
                A_B_0_10_20_P6,
                A_B_0_20_10_P5,
                B_B_2_15_1_P5,
                B_B_3_15_1_P6,
                A_B_0_10_10_P5,
                A_A_0_100_100_P0,
                A_B_1_20_10_P5,
                A_A_1_1000_1000_P0,
                A_B_1_200_10_P0,
                A_A_1_100_100_P0,
                A_A_2_1000_1_P0
            };

            transactions = transactionSelect?.Invoke(transactions) ?? transactions;
            expectation  = transactionSelect?.Invoke(expectation) ?? expectation;

            IBlockTree blockTree = Substitute.For <IBlockTree>();
            Block      block     = Build.A.Block.WithNumber(0).TestObject;

            blockTree.Head.Returns(block);
            ISpecProvider specProvider = Substitute.For <ISpecProvider>();

            specProvider.GetSpec(Arg.Any <long>()).Returns(new ReleaseSpec()
            {
                IsEip1559Enabled = false
            });
            var transactionComparerProvider         = new TransactionComparerProvider(specProvider, blockTree);
            IComparer <Transaction> defaultComparer = transactionComparerProvider.GetDefaultComparer();
            IComparer <Transaction> comparer        = new CompareTxByPriorityOnSpecifiedBlock(sendersWhitelist, priorities, blockHeader)
                                                      .ThenBy(defaultComparer);


            var txBySender = transactions.GroupBy(t => t.SenderAddress)
                             .ToDictionary(
                g => g.Key,
                g => g.OrderBy(t => t,
                               // to simulate order coming from TxPool
                               TxSortedPool.GetPoolUniqueTxComparerByNonce(comparer)).ToArray());


            var orderedTransactions = TxPoolTxSource.Order(txBySender, comparer).ToArray();

            orderedTransactions.Should().BeEquivalentTo(expectation, o => o.WithStrictOrdering());
        }
Ejemplo n.º 11
0
        protected virtual async Task <TestBlockchain> Build(ISpecProvider specProvider = null, UInt256?initialValues = null)
        {
            Timestamper    = new ManualTimestamper(new DateTime(2020, 2, 15, 12, 50, 30, DateTimeKind.Utc));
            JsonSerializer = new EthereumJsonSerializer();
            SpecProvider   = specProvider ?? MainnetSpecProvider.Instance;
            EthereumEcdsa  = new EthereumEcdsa(ChainId.Mainnet, LogManager);
            ITxStorage txStorage = new InMemoryTxStorage();

            DbProvider = await TestMemDbProvider.InitAsync();

            TrieStore = new TrieStore(StateDb.Innermost, LogManager);
            State     = new StateProvider(TrieStore, DbProvider.CodeDb, LogManager);
            State.CreateAccount(TestItem.AddressA, (initialValues ?? InitialValue));
            State.CreateAccount(TestItem.AddressB, (initialValues ?? InitialValue));
            State.CreateAccount(TestItem.AddressC, (initialValues ?? InitialValue));
            byte[] code     = Bytes.FromHexString("0xabcd");
            Keccak codeHash = Keccak.Compute(code);

            State.UpdateCode(code);
            State.UpdateCodeHash(TestItem.AddressA, codeHash, SpecProvider.GenesisSpec);

            Storage = new StorageProvider(TrieStore, State, LogManager);
            Storage.Set(new StorageCell(TestItem.AddressA, UInt256.One), Bytes.FromHexString("0xabcdef"));
            Storage.Commit();

            State.Commit(SpecProvider.GenesisSpec);
            State.CommitTree(0);



            IDb blockDb     = new MemDb();
            IDb headerDb    = new MemDb();
            IDb blockInfoDb = new MemDb();

            BlockTree = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), SpecProvider, NullBloomStorage.Instance, LimboLogs.Instance);
            TransactionComparerProvider = new TransactionComparerProvider(specProvider, BlockTree);
            TxPool = CreateTxPool(txStorage);

            new OnChainTxWatcher(BlockTree, TxPool, SpecProvider, LimboLogs.Instance);

            ReceiptStorage = new InMemoryReceiptStorage();
            VirtualMachine virtualMachine = new VirtualMachine(State, Storage, new BlockhashProvider(BlockTree, LogManager), SpecProvider, LogManager);

            TxProcessor           = new TransactionProcessor(SpecProvider, State, Storage, virtualMachine, LogManager);
            BlockProcessor        = CreateBlockProcessor();
            BlockPreprocessorStep = new RecoverSignatures(EthereumEcdsa, TxPool, SpecProvider, LogManager);
            BlockchainProcessor chainProcessor = new BlockchainProcessor(BlockTree, BlockProcessor, BlockPreprocessorStep, LogManager, Nethermind.Blockchain.Processing.BlockchainProcessor.Options.Default);

            BlockchainProcessor  = chainProcessor;
            BlockProcessingQueue = chainProcessor;
            chainProcessor.Start();

            ReadOnlyTrieStore = TrieStore.AsReadOnly();

            StateReader = new StateReader(ReadOnlyTrieStore, CodeDb, LogManager);
            TxPoolTxSource txPoolTxSource        = CreateTxPoolTxSource();
            ISealer        sealer                = new NethDevSealEngine(TestItem.AddressD);
            IStateProvider producerStateProvider = new StateProvider(ReadOnlyTrieStore, CodeDb, LogManager);

            BlockProducer = CreateTestBlockProducer(txPoolTxSource, chainProcessor, producerStateProvider, sealer);
            BlockProducer.Start();

            _resetEvent = new SemaphoreSlim(0);
            _suggestedBlockResetEvent = new ManualResetEvent(true);
            BlockTree.NewHeadBlock   += (s, e) =>
            {
                _resetEvent.Release(1);
            };
            BlockProducer.LastProducedBlockChanged += (s, e) =>
            {
                _suggestedBlockResetEvent.Set();
            };

            var genesis = GetGenesisBlock();

            BlockTree.SuggestBlock(genesis);
            await _resetEvent.WaitAsync();

            //if (!await _resetEvent.WaitAsync(1000))
            // {
            //     throw new InvalidOperationException("Failed to process genesis in 1s.");
            // }

            await AddBlocksOnStart();

            return(this);
        }
Ejemplo n.º 12
0
        public Task InitBlockProducer()
        {
            if (_nethermindApi !.SealEngineType != Nethermind.Core.SealEngineType.Clique)
            {
                return(Task.CompletedTask);
            }

            var(getFromApi, setInApi) = _nethermindApi !.ForProducer;

            _miningConfig = getFromApi.Config <IMiningConfig>();
            if (!_miningConfig.Enabled)
            {
                throw new InvalidOperationException("Request to start block producer while mining disabled.");
            }

            setInApi.Sealer = new CliqueSealer(
                getFromApi.EngineSigner !,
                _cliqueConfig !,
                _snapshotManager !,
                getFromApi.LogManager);

            ReadOnlyDbProvider           readOnlyDbProvider          = getFromApi.DbProvider.AsReadOnly(false);
            ReadOnlyBlockTree            readOnlyBlockTree           = getFromApi.BlockTree.AsReadOnly();
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(getFromApi.SpecProvider, readOnlyBlockTree);

            ReadOnlyTxProcessingEnv producerEnv = new ReadOnlyTxProcessingEnv(
                readOnlyDbProvider,
                getFromApi.ReadOnlyTrieStore,
                readOnlyBlockTree,
                getFromApi.SpecProvider,
                getFromApi.LogManager);

            BlockProcessor producerProcessor = new BlockProcessor(
                getFromApi !.SpecProvider,
                getFromApi !.BlockValidator,
                NoBlockRewards.Instance,
                producerEnv.TransactionProcessor,
                producerEnv.StateProvider,
                producerEnv.StorageProvider,
                NullTxPool.Instance, // do not remove transactions from the pool when preprocessing
                NullReceiptStorage.Instance,
                NullWitnessCollector.Instance,
                getFromApi.LogManager);

            IBlockchainProcessor producerChainProcessor = new BlockchainProcessor(
                readOnlyBlockTree,
                producerProcessor,
                getFromApi.BlockPreprocessor,
                getFromApi.LogManager,
                BlockchainProcessor.Options.NoReceipts);

            OneTimeChainProcessor chainProcessor = new OneTimeChainProcessor(
                readOnlyDbProvider,
                producerChainProcessor);

            ITxFilterPipeline txFilterPipeline =
                TxFilterPipelineBuilder.CreateStandardFilteringPipeline(_nethermindApi.LogManager,
                                                                        getFromApi.SpecProvider);

            ITxSource txSource = new TxPoolTxSource(
                getFromApi.TxPool,
                getFromApi.StateReader,
                getFromApi.SpecProvider,
                transactionComparerProvider,
                getFromApi.LogManager,
                txFilterPipeline);

            var gasLimitCalculator = new TargetAdjustedGasLimitCalculator(getFromApi.SpecProvider, _miningConfig);

            setInApi.BlockProducer = new CliqueBlockProducer(
                txSource,
                chainProcessor,
                producerEnv.StateProvider,
                getFromApi.BlockTree !,
                getFromApi.Timestamper,
                getFromApi.CryptoRandom,
                _snapshotManager !,
                getFromApi.Sealer !,
                gasLimitCalculator,
                getFromApi.SpecProvider,
                _cliqueConfig !,
                getFromApi.LogManager);

            return(Task.CompletedTask);
        }
Ejemplo n.º 13
0
        private SyncTestContext CreateSyncManager(int index)
        {
            NoErrorLimboLogs   logManager = NoErrorLimboLogs.Instance;
            ConsoleAsyncLogger logger     = new(LogLevel.Debug, "PEER " + index + " ");
//            var logManager = new OneLoggerLogManager(logger);
            SingleReleaseSpecProvider specProvider =
                new(ConstantinopleFix.Instance, MainnetSpecProvider.Instance.ChainId);

            IDbProvider dbProvider  = TestMemDbProvider.Init();
            IDb         blockDb     = dbProvider.BlocksDb;
            IDb         headerDb    = dbProvider.HeadersDb;
            IDb         blockInfoDb = dbProvider.BlockInfosDb;
            IDb         codeDb      = dbProvider.CodeDb;
            IDb         stateDb     = dbProvider.StateDb;

            TrieStore     trieStore     = new(stateDb, LimboLogs.Instance);
            StateReader   stateReader   = new(trieStore, codeDb, logManager);
            StateProvider stateProvider = new(trieStore, codeDb, logManager);

            stateProvider.CreateAccount(TestItem.AddressA, 10000.Ether());
            stateProvider.Commit(specProvider.GenesisSpec);
            stateProvider.CommitTree(0);
            stateProvider.RecalculateStateRoot();

            StorageProvider        storageProvider = new(trieStore, stateProvider, logManager);
            InMemoryReceiptStorage receiptStorage  = new();

            EthereumEcdsa ecdsa = new(specProvider.ChainId, logManager);
            BlockTree     tree  = new(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb),
                                      specProvider, NullBloomStorage.Instance, logManager);
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(specProvider, tree);

            TxPool.TxPool txPool = new(new InMemoryTxStorage(), ecdsa, new ChainHeadInfoProvider(specProvider, tree, stateReader),
                                       new TxPoolConfig(), new TxValidator(specProvider.ChainId), logManager, transactionComparerProvider.GetDefaultComparer());
            BlockhashProvider blockhashProvider = new(tree, LimboLogs.Instance);
            VirtualMachine    virtualMachine    =
                new(stateProvider, storageProvider, blockhashProvider, specProvider, logManager);

            Always          sealValidator   = Always.Valid;
            HeaderValidator headerValidator = new(tree, sealValidator, specProvider, logManager);
            Always          txValidator     = Always.Valid;
            OmmersValidator ommersValidator = new(tree, headerValidator, logManager);
            BlockValidator  blockValidator  =
                new(txValidator, headerValidator, ommersValidator, specProvider, logManager);

            ISyncConfig syncConfig = _synchronizerType == SynchronizerType.Fast
                ? SyncConfig.WithFastSync
                : SyncConfig.WithFullSyncOnly;

            RewardCalculator     rewardCalculator = new(specProvider);
            TransactionProcessor txProcessor      =
                new(specProvider, stateProvider, storageProvider, virtualMachine, logManager);

            BlockProcessor blockProcessor = new(
                specProvider,
                blockValidator,
                rewardCalculator,
                txProcessor,
                stateProvider,
                storageProvider,
                txPool,
                receiptStorage,
                NullWitnessCollector.Instance,
                logManager);

            RecoverSignatures   step      = new(ecdsa, txPool, specProvider, logManager);
            BlockchainProcessor processor = new(tree, blockProcessor, step, logManager,
                                                BlockchainProcessor.Options.Default);

            ITimerFactory    timerFactory     = Substitute.For <ITimerFactory>();
            NodeStatsManager nodeStatsManager = new(timerFactory, logManager);
            SyncPeerPool     syncPeerPool     = new(tree, nodeStatsManager, 25, logManager);

            StateProvider        devState       = new(trieStore, codeDb, logManager);
            StorageProvider      devStorage     = new(trieStore, devState, logManager);
            VirtualMachine       devEvm         = new(devState, devStorage, blockhashProvider, specProvider, logManager);
            TransactionProcessor devTxProcessor = new(specProvider, devState, devStorage, devEvm, logManager);

            BlockProcessor devBlockProcessor = new(
                specProvider,
                blockValidator,
                rewardCalculator,
                devTxProcessor,
                devState,
                devStorage,
                txPool,
                receiptStorage,
                NullWitnessCollector.Instance,
                logManager);

            BlockchainProcessor devChainProcessor = new(tree, devBlockProcessor, step, logManager,
                                                        BlockchainProcessor.Options.NoReceipts);
            ITxFilterPipeline txFilterPipeline    = TxFilterPipelineBuilder.CreateStandardFilteringPipeline(LimboLogs.Instance, specProvider);
            TxPoolTxSource    transactionSelector = new(txPool, stateReader, specProvider, transactionComparerProvider, logManager, txFilterPipeline);
            DevBlockProducer  producer            = new(
                transactionSelector,
                devChainProcessor,
                stateProvider, tree,
                processor,
                new BuildBlocksRegularly(TimeSpan.FromMilliseconds(50)).IfPoolIsNotEmpty(txPool),
                Timestamper.Default,
                specProvider,
                new MiningConfig(),
                logManager);

            SyncProgressResolver resolver = new(
                tree, receiptStorage, stateDb, new MemDb(), NullTrieNodeResolver.Instance, syncConfig, logManager);
            MultiSyncModeSelector selector     = new(resolver, syncPeerPool, syncConfig, logManager);
            Synchronizer          synchronizer = new(
                dbProvider,
                MainnetSpecProvider.Instance,
                tree,
                NullReceiptStorage.Instance,
                blockValidator,
                sealValidator,
                syncPeerPool,
                nodeStatsManager,
                StaticSelector.Full,
                syncConfig,
                logManager);
            SyncServer syncServer = new(
                stateDb,
                codeDb,
                tree,
                receiptStorage,
                Always.Valid,
                Always.Valid,
                syncPeerPool,
                selector,
                syncConfig,
                NullWitnessCollector.Instance,
                logManager);

            ManualResetEventSlim waitEvent = new();

            tree.NewHeadBlock += (_, _) => waitEvent.Set();

            if (index == 0)
            {
                _genesis = Build.A.Block.Genesis.WithStateRoot(stateProvider.StateRoot).TestObject;
                producer.Start();
            }

            syncPeerPool.Start();
            synchronizer.Start();
            processor.Start();
            tree.SuggestBlock(_genesis);

            if (!waitEvent.Wait(1000))
            {
                throw new Exception("No genesis");
            }

            SyncTestContext context = new();

            context.Ecdsa = ecdsa;
            context.BlockchainProcessor = processor;
            context.PeerPool            = syncPeerPool;
            context.StateProvider       = stateProvider;
            context.Synchronizer        = synchronizer;
            context.SyncServer          = syncServer;
            context.Tree          = tree;
            context.BlockProducer = producer;
            context.TxPool        = txPool;
            context.Logger        = logger;
            return(context);
        }
Ejemplo n.º 14
0
        protected async Task <EthereumTestResult> RunTest(BlockchainTest test, Stopwatch?stopwatch = null)
        {
            TestContext.Write($"Running {test.Name} at {DateTime.UtcNow:HH:mm:ss.ffffff}");
            Assert.IsNull(test.LoadFailure, "test data loading failure");

            IDb stateDb = new MemDb();
            IDb codeDb  = new MemDb();

            ISpecProvider specProvider;

            if (test.NetworkAfterTransition != null)
            {
                specProvider = new CustomSpecProvider(1,
                                                      (0, Frontier.Instance),
                                                      (1, test.Network),
                                                      (test.TransitionBlockNumber, test.NetworkAfterTransition));
            }
            else
            {
                specProvider = new CustomSpecProvider(1,
                                                      (0, Frontier.Instance), // TODO: this thing took a lot of time to find after it was removed!, genesis block is always initialized with Frontier
                                                      (1, test.Network));
            }

            if (specProvider.GenesisSpec != Frontier.Instance)
            {
                Assert.Fail("Expected genesis spec to be Frontier for blockchain tests");
            }

            DifficultyCalculator.Wrapped = new DifficultyCalculator(specProvider);
            IRewardCalculator rewardCalculator = new RewardCalculator(specProvider);

            IEthereumEcdsa ecdsa = new EthereumEcdsa(specProvider.ChainId, _logManager);

            TrieStore      trieStore     = new(stateDb, _logManager);
            IStateProvider stateProvider = new StateProvider(trieStore, codeDb, _logManager);
            MemDb          blockInfoDb   = new MemDb();
            IBlockTree     blockTree     = new BlockTree(new MemDb(), new MemDb(), blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, NullBloomStorage.Instance, _logManager);
            ITransactionComparerProvider transactionComparerProvider = new TransactionComparerProvider(specProvider, blockTree);
            ITxPool transactionPool = new TxPool(NullTxStorage.Instance, ecdsa, new ChainHeadInfoProvider(specProvider, blockTree, stateProvider), new TxPoolConfig(), new TxValidator(specProvider.ChainId), _logManager, transactionComparerProvider.GetDefaultComparer());

            IReceiptStorage    receiptStorage    = NullReceiptStorage.Instance;
            IBlockhashProvider blockhashProvider = new BlockhashProvider(blockTree, _logManager);
            ITxValidator       txValidator       = new TxValidator(ChainId.Mainnet);
            IHeaderValidator   headerValidator   = new HeaderValidator(blockTree, Sealer, specProvider, _logManager);
            IOmmersValidator   ommersValidator   = new OmmersValidator(blockTree, headerValidator, _logManager);
            IBlockValidator    blockValidator    = new BlockValidator(txValidator, headerValidator, ommersValidator, specProvider, _logManager);
            IStorageProvider   storageProvider   = new StorageProvider(trieStore, stateProvider, _logManager);
            IVirtualMachine    virtualMachine    = new VirtualMachine(
                stateProvider,
                storageProvider,
                blockhashProvider,
                specProvider,
                _logManager);

            IBlockProcessor blockProcessor = new BlockProcessor(
                specProvider,
                blockValidator,
                rewardCalculator,
                new TransactionProcessor(
                    specProvider,
                    stateProvider,
                    storageProvider,
                    virtualMachine,
                    _logManager),
                stateProvider,
                storageProvider,
                transactionPool,
                receiptStorage,
                NullWitnessCollector.Instance,
                _logManager);

            IBlockchainProcessor blockchainProcessor = new BlockchainProcessor(
                blockTree,
                blockProcessor,
                new RecoverSignatures(ecdsa, NullTxPool.Instance, specProvider, _logManager),
                _logManager,
                BlockchainProcessor.Options.NoReceipts);

            InitializeTestState(test, stateProvider, storageProvider, specProvider);

            List <(Block Block, string ExpectedException)> correctRlp = new();

            for (int i = 0; i < test.Blocks.Length; i++)
            {
                try
                {
                    TestBlockJson testBlockJson  = test.Blocks[i];
                    var           rlpContext     = Bytes.FromHexString(testBlockJson.Rlp).AsRlpStream();
                    Block         suggestedBlock = Rlp.Decode <Block>(rlpContext);
                    suggestedBlock.Header.SealEngineType = test.SealEngineUsed ? SealEngineType.Ethash : SealEngineType.None;

                    Assert.AreEqual(new Keccak(testBlockJson.BlockHeader.Hash), suggestedBlock.Header.Hash, "hash of the block");
                    for (int ommerIndex = 0; ommerIndex < suggestedBlock.Ommers.Length; ommerIndex++)
                    {
                        Assert.AreEqual(new Keccak(testBlockJson.UncleHeaders[ommerIndex].Hash), suggestedBlock.Ommers[ommerIndex].Hash, "hash of the ommer");
                    }

                    correctRlp.Add((suggestedBlock, testBlockJson.ExpectedException));
                }
                catch (Exception)
                {
                    _logger?.Info($"Invalid RLP ({i})");
                }
            }

            if (correctRlp.Count == 0)
            {
                EthereumTestResult result;
                if (test.GenesisBlockHeader is null)
                {
                    result = new EthereumTestResult(test.Name, "Genesis block header missing in the test spec.");
                }
                else if (!new Keccak(test.GenesisBlockHeader.Hash).Equals(test.LastBlockHash))
                {
                    result = new EthereumTestResult(test.Name, "Genesis hash mismatch");
                }
                else
                {
                    result = new EthereumTestResult(test.Name, null, true);
                }

                return(result);
            }

            if (test.GenesisRlp == null)
            {
                test.GenesisRlp = Rlp.Encode(new Block(JsonToEthereumTest.Convert(test.GenesisBlockHeader)));
            }

            Block genesisBlock = Rlp.Decode <Block>(test.GenesisRlp.Bytes);

            Assert.AreEqual(new Keccak(test.GenesisBlockHeader.Hash), genesisBlock.Header.Hash, "genesis header hash");

            ManualResetEvent genesisProcessed = new(false);

            blockTree.NewHeadBlock += (_, args) =>
            {
                if (args.Block.Number == 0)
                {
                    Assert.AreEqual(genesisBlock.Header.StateRoot, stateProvider.StateRoot, "genesis state root");
                    genesisProcessed.Set();
                }
            };

            blockchainProcessor.Start();
            blockTree.SuggestBlock(genesisBlock);

            genesisProcessed.WaitOne();

            for (int i = 0; i < correctRlp.Count; i++)
            {
                stopwatch?.Start();
                try
                {
                    if (correctRlp[i].ExpectedException != null)
                    {
                        _logger.Info($"Expecting block exception: {correctRlp[i].ExpectedException}");
                    }

                    if (correctRlp[i].Block.Hash == null)
                    {
                        throw new Exception($"null hash in {test.Name} block {i}");
                    }

                    // TODO: mimic the actual behaviour where block goes through validating sync manager?
                    if (!test.SealEngineUsed || blockValidator.ValidateSuggestedBlock(correctRlp[i].Block))
                    {
                        blockTree.SuggestBlock(correctRlp[i].Block);
                    }
                    else
                    {
                        Console.WriteLine("Invalid block");
                    }
                }
                catch (InvalidBlockException)
                {
                }
                catch (Exception ex)
                {
                    _logger?.Info(ex.ToString());
                }
            }

            await blockchainProcessor.StopAsync(true);

            stopwatch?.Stop();

            List <string> differences = RunAssertions(test, blockTree.RetrieveHeadBlock(), storageProvider, stateProvider);

//            if (differences.Any())
//            {
//                BlockTrace blockTrace = blockchainProcessor.TraceBlock(blockTree.BestSuggested.Hash);
//                _logger.Info(new UnforgivingJsonSerializer().Serialize(blockTrace, true));
//            }

            Assert.Zero(differences.Count, "differences");

            return(new EthereumTestResult
                   (
                       test.Name,
                       null,
                       differences.Count == 0
                   ));
        }
Ejemplo n.º 15
0
        public void Initialize()
        {
            LimboLogs           logger        = LimboLogs.Instance;
            MainnetSpecProvider specProvider  = MainnetSpecProvider.Instance;
            EthereumEcdsa       ethereumEcdsa = new EthereumEcdsa(specProvider.ChainId, logger);
            InMemoryTxStorage   txStorage     = new InMemoryTxStorage();

            Peer         peerA       = SetUpPeerA(); //standard case
            Peer         peerB       = SetUpPeerB(); //Session is null
            Peer         peerC       = SetUpPeerC(); //Node is null, Caps are empty
            IPeerManager peerManager = Substitute.For <IPeerManager>();

            peerManager.ActivePeers.Returns(new List <Peer> {
                peerA, peerB, peerC
            });
            peerManager.ConnectedPeers.Returns(new List <Peer> {
                peerA, peerB, peerA, peerC, peerB
            });
            peerManager.MaxActivePeers.Returns(15);

            StateProvider stateProvider = new StateProvider(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance);
            StateReader   stateReader   = new StateReader(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance);

            IDb        blockDb     = new MemDb();
            IDb        headerDb    = new MemDb();
            IDb        blockInfoDb = new MemDb();
            IBlockTree blockTree   = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, NullBloomStorage.Instance, LimboLogs.Instance);

            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(specProvider, blockTree);

            TxPool.TxPool txPool = new TxPool.TxPool(txStorage, ethereumEcdsa, new ChainHeadInfoProvider(new FixedBlockChainHeadSpecProvider(specProvider), blockTree, stateProvider), new TxPoolConfig(),
                                                     new TxValidator(specProvider.ChainId), LimboLogs.Instance, transactionComparerProvider.GetDefaultComparer());

            new OnChainTxWatcher(blockTree, txPool, specProvider, LimboLogs.Instance);

            IReceiptStorage receiptStorage = new InMemoryReceiptStorage();

            _signerStore     = new Signer(specProvider.ChainId, TestItem.PrivateKeyB, logger);
            _parityRpcModule = new ParityRpcModule(ethereumEcdsa, txPool, blockTree, receiptStorage, new Enode(TestItem.PublicKeyA, IPAddress.Loopback, 8545),
                                                   _signerStore, new MemKeyStore(new[] { TestItem.PrivateKeyA }), logger, peerManager);

            int         blockNumber        = 2;
            Transaction pendingTransaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false)
                                             .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)).TestObject;

            pendingTransaction.Signature.V = 37;
            stateProvider.CreateAccount(pendingTransaction.SenderAddress, UInt256.UInt128MaxValue);
            txPool.AddTransaction(pendingTransaction, TxHandlingOptions.None);

            blockNumber = 1;
            Transaction transaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false)
                                      .WithSenderAddress(Address.FromNumber((UInt256)blockNumber))
                                      .WithNonce(100).TestObject;

            transaction.Signature.V = 37;
            stateProvider.CreateAccount(transaction.SenderAddress, UInt256.UInt128MaxValue);
            txPool.AddTransaction(transaction, TxHandlingOptions.None);


            Block genesis = Build.A.Block.Genesis
                            .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f"))
                            .TestObject;

            blockTree.SuggestBlock(genesis);
            blockTree.UpdateMainChain(new[] { genesis }, true);

            Block previousBlock = genesis;
            Block block         = Build.A.Block.WithNumber(blockNumber).WithParent(previousBlock)
                                  .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f"))
                                  .WithTransactions(transaction)
                                  .TestObject;

            blockTree.SuggestBlock(block);
            blockTree.UpdateMainChain(new[] { block }, true);

            LogEntry[] logEntries = new[] { Build.A.LogEntry.TestObject };
            receiptStorage.Insert(block, new TxReceipt()
            {
                Bloom           = new Bloom(logEntries),
                Index           = 1,
                Recipient       = TestItem.AddressA,
                Sender          = TestItem.AddressB,
                BlockHash       = TestItem.KeccakA,
                BlockNumber     = 1,
                ContractAddress = TestItem.AddressC,
                GasUsed         = 1000,
                TxHash          = transaction.Hash,
                StatusCode      = 0,
                GasUsedTotal    = 2000,
                Logs            = logEntries
            });
        }
Ejemplo n.º 16
0
        private async Task Initialize(bool auRa = false)
        {
            IDbProvider dbProvider = await TestMemDbProvider.InitAsync();

            ISpecProvider specProvider = MainnetSpecProvider.Instance;

            _jsonRpcConfig = new JsonRpcConfig();
            IEthereumEcdsa ethereumEcdsa = new EthereumEcdsa(specProvider.ChainId, LimboLogs.Instance);
            ITxStorage     txStorage     = new InMemoryTxStorage();

            _stateDb = new MemDb();
            ITrieStore trieStore = new TrieStore(_stateDb, LimboLogs.Instance);
            MemDb      codeDb    = new MemDb();

            _stateProvider = new StateProvider(trieStore, codeDb, LimboLogs.Instance);
            _stateReader   = new StateReader(trieStore, codeDb, LimboLogs.Instance);

            _stateProvider.CreateAccount(TestItem.AddressA, 1000.Ether());
            _stateProvider.CreateAccount(TestItem.AddressB, 1000.Ether());
            _stateProvider.CreateAccount(TestItem.AddressC, 1000.Ether());
            byte[] code     = Bytes.FromHexString("0xabcd");
            Keccak codeHash = Keccak.Compute(code);

            _stateProvider.UpdateCode(code);
            _stateProvider.UpdateCodeHash(TestItem.AddressA, codeHash, specProvider.GenesisSpec);

            IStorageProvider storageProvider = new StorageProvider(trieStore, _stateProvider, LimboLogs.Instance);

            storageProvider.Set(new StorageCell(TestItem.AddressA, UInt256.One), Bytes.FromHexString("0xabcdef"));
            storageProvider.Commit();

            _stateProvider.Commit(specProvider.GenesisSpec);
            _stateProvider.CommitTree(0);

            IChainLevelInfoRepository chainLevels = new ChainLevelInfoRepository(dbProvider);
            IBlockTree blockTree = new BlockTree(dbProvider, chainLevels, specProvider, NullBloomStorage.Instance, LimboLogs.Instance);
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(specProvider, blockTree);
            ITxPool txPool = new TxPool.TxPool(txStorage, ethereumEcdsa, new ChainHeadInfoProvider(specProvider, blockTree, _stateReader),
                                               new TxPoolConfig(), new TxValidator(specProvider.ChainId), LimboLogs.Instance, transactionComparerProvider.GetDefaultComparer());

            IReceiptStorage      receiptStorage = new InMemoryReceiptStorage();
            VirtualMachine       virtualMachine = new VirtualMachine(_stateProvider, storageProvider, new BlockhashProvider(blockTree, LimboLogs.Instance), specProvider, LimboLogs.Instance);
            TransactionProcessor txProcessor    = new TransactionProcessor(specProvider, _stateProvider, storageProvider, virtualMachine, LimboLogs.Instance);
            IBlockProcessor      blockProcessor = new BlockProcessor(
                specProvider,
                Always.Valid,
                new RewardCalculator(specProvider),
                txProcessor,
                _stateProvider,
                storageProvider,
                txPool,
                receiptStorage,
                NullWitnessCollector.Instance,
                LimboLogs.Instance);

            RecoverSignatures   signatureRecovery   = new RecoverSignatures(ethereumEcdsa, txPool, specProvider, LimboLogs.Instance);
            BlockchainProcessor blockchainProcessor = new BlockchainProcessor(blockTree, blockProcessor, signatureRecovery, LimboLogs.Instance, BlockchainProcessor.Options.Default);

            blockchainProcessor.Start();

            ManualResetEventSlim resetEvent = new ManualResetEventSlim(false);

            blockTree.NewHeadBlock += (s, e) =>
            {
                Console.WriteLine(e.Block.Header.Hash);
                if (e.Block.Number == 9)
                {
                    resetEvent.Set();
                }
            };

            var genesisBlockBuilder = Build.A.Block.Genesis.WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f"));

            if (auRa)
            {
                genesisBlockBuilder.WithAura(0, new byte[65]);
            }

            Block genesis = genesisBlockBuilder.TestObject;

            blockTree.SuggestBlock(genesis);

            Block previousBlock = genesis;

            for (int i = 1; i < 10; i++)
            {
                List <Transaction> transactions = new List <Transaction>();
                for (int j = 0; j < i; j++)
                {
                    transactions.Add(Build.A.Transaction.WithNonce((UInt256)j).SignedAndResolved().TestObject);
                }

                BlockBuilder builder = Build.A.Block.WithNumber(i).WithParent(previousBlock).WithTransactions(transactions.ToArray()).WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f"));
                if (auRa)
                {
                    builder.WithAura(i, i.ToByteArray());
                }

                Block block = builder.TestObject;
                blockTree.SuggestBlock(block);
                previousBlock = block;
            }

            ReceiptsRecovery receiptsRecovery = new ReceiptsRecovery(new EthereumEcdsa(specProvider.ChainId, LimboLogs.Instance), specProvider);
            IReceiptFinder   receiptFinder    = new FullInfoReceiptFinder(receiptStorage, receiptsRecovery, blockTree);

            resetEvent.Wait(2000);

            _traceRpcModule = new TraceRpcModule(receiptFinder, new Tracer(_stateProvider, blockchainProcessor), blockTree, _jsonRpcConfig);
        }
Ejemplo n.º 17
0
        public void Initialize()
        {
            LimboLogs           logger        = LimboLogs.Instance;
            MainnetSpecProvider specProvider  = MainnetSpecProvider.Instance;
            EthereumEcdsa       ethereumEcdsa = new(specProvider.ChainId, logger);

            Peer         peerA       = SetUpPeerA(); //standard case
            Peer         peerB       = SetUpPeerB(); //Session is null
            Peer         peerC       = SetUpPeerC(); //Node is null, Caps are empty
            IPeerManager peerManager = Substitute.For <IPeerManager>();

            peerManager.ActivePeers.Returns(new List <Peer> {
                peerA, peerB, peerC
            });
            peerManager.ConnectedPeers.Returns(new List <Peer> {
                peerA, peerB, peerA, peerC, peerB
            });
            peerManager.MaxActivePeers.Returns(15);

            StateProvider stateProvider = new(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance);
            StateReader   stateReader   = new(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance);

            IDb        blockDb     = new MemDb();
            IDb        headerDb    = new MemDb();
            IDb        blockInfoDb = new MemDb();
            IBlockTree blockTree   = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, NullBloomStorage.Instance, LimboLogs.Instance);

            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(specProvider, blockTree);

            TxPool.TxPool txPool = new(ethereumEcdsa, new ChainHeadInfoProvider(new FixedBlockChainHeadSpecProvider(specProvider), blockTree, stateProvider), new TxPoolConfig(),
                                       new TxValidator(specProvider.ChainId), LimboLogs.Instance, transactionComparerProvider.GetDefaultComparer());

            IReceiptStorage receiptStorage = new InMemoryReceiptStorage();

            _signerStore     = new Signer(specProvider.ChainId, TestItem.PrivateKeyB, logger);
            _parityRpcModule = new ParityRpcModule(ethereumEcdsa, txPool, blockTree, receiptStorage, new Enode(TestItem.PublicKeyA, IPAddress.Loopback, 8545),
                                                   _signerStore, new MemKeyStore(new[] { TestItem.PrivateKeyA }), MainnetSpecProvider.Instance, peerManager);

            int         blockNumber        = 2;
            Transaction pendingTransaction = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false)
                                             .WithSenderAddress(Address.FromNumber((UInt256)blockNumber)).TestObject;

            pendingTransaction.Signature.V = 37;
            stateProvider.CreateAccount(pendingTransaction.SenderAddress, UInt256.UInt128MaxValue);
            txPool.SubmitTx(pendingTransaction, TxHandlingOptions.None);

            blockNumber = 1;
            Transaction transaction1 = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false)
                                       .WithSenderAddress(Address.FromNumber((UInt256)blockNumber))
                                       .WithNonce(100).TestObject;

            transaction1.Signature.V = 37;
            stateProvider.CreateAccount(transaction1.SenderAddress, UInt256.UInt128MaxValue);

            var transaction2 = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false)
                               .WithSenderAddress(Address.FromNumber((UInt256)blockNumber))
                               .WithNonce(120).TestObject;

            transaction2.Signature.V = 37;

            var transaction3 = Build.A.Transaction.Signed(ethereumEcdsa, TestItem.PrivateKeyD, false)
                               .WithSenderAddress(Address.FromNumber((UInt256)blockNumber))
                               .WithNonce(110).TestObject;

            transaction2.Signature.V = 37;

            Block genesis = Build.A.Block.Genesis
                            .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f"))
                            .TestObject;

            blockTree.SuggestBlock(genesis);
            blockTree.UpdateMainChain(new[] { genesis }, true);

            Block previousBlock = genesis;
            Block block         = Build.A.Block.WithNumber(blockNumber).WithParent(previousBlock)
                                  .WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f"))
                                  .WithTransactions(transaction1, transaction2, transaction3)
                                  .TestObject;

            blockTree.SuggestBlock(block);
            blockTree.UpdateMainChain(new[] { block }, true);

            LogEntry[] logEntries = new[] { Build.A.LogEntry.TestObject };

            TxReceipt receipt1 = new()
            {
                Bloom           = new Bloom(logEntries),
                Index           = 0,
                Recipient       = TestItem.AddressA,
                Sender          = TestItem.AddressB,
                BlockHash       = TestItem.KeccakA,
                BlockNumber     = 1,
                ContractAddress = TestItem.AddressC,
                GasUsed         = 1000,
                TxHash          = transaction1.Hash,
                StatusCode      = 0,
                GasUsedTotal    = 2000,
                Logs            = logEntries
            };

            TxReceipt receipt2 = new()
            {
                Bloom           = new Bloom(logEntries),
                Index           = 1,
                Recipient       = TestItem.AddressA,
                Sender          = TestItem.AddressB,
                BlockHash       = TestItem.KeccakA,
                BlockNumber     = 1,
                ContractAddress = TestItem.AddressC,
                GasUsed         = 1000,
                TxHash          = transaction2.Hash,
                StatusCode      = 0,
                GasUsedTotal    = 2000,
                Logs            = logEntries
            };

            TxReceipt receipt3 = new()
            {
                Bloom           = new Bloom(logEntries),
                Index           = 2,
                Recipient       = TestItem.AddressA,
                Sender          = TestItem.AddressB,
                BlockHash       = TestItem.KeccakA,
                BlockNumber     = 1,
                ContractAddress = TestItem.AddressC,
                GasUsed         = 1000,
                TxHash          = transaction3.Hash,
                StatusCode      = 0,
                GasUsedTotal    = 2000,
                Logs            = logEntries
            };

            receiptStorage.Insert(block, receipt1, receipt2, receipt3);
        }

        private static Peer SetUpPeerA()
        {
            Node node = new(TestItem.PublicKeyA, "127.0.0.1", 30303, true);

            node.ClientId = "Geth/v1.9.21-stable/linux-amd64/go1.15.2";

            Peer peer = new(node);

            peer.InSession  = null;
            peer.OutSession = Substitute.For <ISession>();
            peer.OutSession.RemoteNodeId.Returns(TestItem.PublicKeyA);

            IProtocolHandler protocolHandler = Substitute.For <IProtocolHandler, ISyncPeer>();

            peer.OutSession.TryGetProtocolHandler(Protocol.Eth, out Arg.Any <IProtocolHandler>()).Returns(x =>
            {
                x[1] = protocolHandler;
                return(true);
            });

            byte version = 65;

            protocolHandler.ProtocolVersion.Returns(version);
            if (protocolHandler is ISyncPeer syncPeer)
            {
                UInt256 difficulty = 0x5ea4ed;
                syncPeer.TotalDifficulty.Returns(difficulty);
                syncPeer.HeadHash.Returns(TestItem.KeccakA);
            }

            IProtocolHandler p2PProtocolHandler = Substitute.For <IProtocolHandler, IP2PProtocolHandler>();

            peer.OutSession.TryGetProtocolHandler(Protocol.P2P, out Arg.Any <IProtocolHandler>()).Returns(x =>
            {
                x[1] = p2PProtocolHandler;
                return(true);
            });

            if (p2PProtocolHandler is IP2PProtocolHandler p2PHandler)
            {
                p2PHandler.AgreedCapabilities.Returns(new List <Capability> {
                    new("eth", 65), new("eth", 64)
                });
            }