Exemplo n.º 1
0
        private static readonly Ethash Ethash = new Ethash(); // temporarily keep reusing the same one as otherwise it would recreate cache for each test

        protected void Setup(ILogger logger)
        {
            _logger = logger;
            ILogger stateLogger = ShouldLog.State ? _logger : null;

            _multiDb = new MultiDb(stateLogger);
            _chain   = new BlockStore();

            _blockhashProvider = new BlockhashProvider(_chain);
            _virtualMachines   = new Dictionary <EthereumNetwork, IVirtualMachine>();
            _stateProviders    = new Dictionary <EthereumNetwork, StateProvider>();
            _storageProviders  = new Dictionary <EthereumNetwork, IStorageProvider>();
            _blockValidators   = new Dictionary <EthereumNetwork, IBlockValidator>();
            EthereumNetwork[] networks = { EthereumNetwork.Frontier, EthereumNetwork.Homestead, EthereumNetwork.Byzantium, EthereumNetwork.SpuriousDragon, EthereumNetwork.TangerineWhistle };
            foreach (EthereumNetwork ethereumNetwork in networks)
            {
                IEthereumRelease      spec = _protocolSpecificationProvider.GetSpec(ethereumNetwork, 1);
                ISignatureValidator   signatureValidator   = new SignatureValidator(spec, ChainId.MainNet);
                ITransactionValidator transactionValidator = new TransactionValidator(spec, signatureValidator);
                IBlockHeaderValidator headerValidator      = new BlockHeaderValidator(_chain, Ethash);
                IOmmersValidator      ommersValidator      = new OmmersValidator(_chain, headerValidator);
                IBlockValidator       blockValidator       = new BlockValidator(transactionValidator, headerValidator, ommersValidator, stateLogger);

                _blockValidators[ethereumNetwork]  = blockValidator;
                _stateProviders[ethereumNetwork]   = new StateProvider(new StateTree(_multiDb.CreateDb()), spec, stateLogger);
                _storageProviders[ethereumNetwork] = new StorageProvider(_multiDb, _stateProviders[ethereumNetwork], stateLogger);
                _virtualMachines[ethereumNetwork]  = new VirtualMachine(
                    spec,
                    _stateProviders[ethereumNetwork],
                    _storageProviders[ethereumNetwork],
                    _blockhashProvider,
                    ShouldLog.Evm ? logger : null);
            }
        }
Exemplo n.º 2
0
        protected void RunTest(BlockchainTest test, Stopwatch stopwatch = null)
        {
            LoggingTraceListener traceListener = new LoggingTraceListener(_logger);

            // TODO: not supported in .NET Core, need to replace?
//            Debug.Listeners.Clear();
//            Debug.Listeners.Add(traceListener);

            InitializeTestState(test);

            // TODO: transition...
            _stateProviders[test.Network].EthereumRelease = _protocolSpecificationProvider.GetSpec(test.Network, 0);

            IEthereumRelease spec           = _protocolSpecificationProvider.GetSpec(test.Network, 1);
            IEthereumSigner  signer         = new EthereumSigner(spec, ChainId.MainNet);
            IBlockProcessor  blockProcessor = new BlockProcessor(
                spec,
                _chain,
                _blockValidators[test.Network],
                new ProtocolBasedDifficultyCalculator(spec),
                new RewardCalculator(spec),
                new TransactionProcessor(
                    spec,
                    _stateProviders[test.Network],
                    _storageProviders[test.Network],
                    _virtualMachines[test.Network],
                    signer,
                    ShouldLog.Processing ? _logger : null),
                _multiDb,
                _stateProviders[test.Network],
                _storageProviders[test.Network],
                new TransactionStore(),
                ShouldLog.Processing ? _logger : null);

            IBlockchainProcessor blockchainProcessor = new BlockchainProcessor(
                test.GenesisRlp,
                blockProcessor,
                _chain,
                ShouldLog.Processing ? _logger : null);

            var rlps = test.Blocks.Select(tb => new Rlp(Hex.ToBytes(tb.Rlp))).ToArray();

            for (int i = 0; i < rlps.Length; i++)
            {
                stopwatch?.Start();
                try
                {
                    blockchainProcessor.Process(rlps[i]);
                }
                catch (InvalidBlockException ex)
                {
                }
                catch (Exception ex)
                {
                    _logger?.Log(ex.ToString());
                }

                stopwatch?.Stop();
            }

            RunAssertions(test, blockchainProcessor.HeadBlock);
        }