Example #1
0
            public TxReceipt[] GetReceipts(ValidatorContract validatorContract, Block block, Address contractAddress, IAbiEncoder encoder, Func <Address[], byte[]> dataFunc)
            {
                var validators = Current.Validators?.FirstOrDefault(v => v.InitializeBlock == block.Number)?.Addresses;

                if (validators == null)
                {
                    return(Array.Empty <TxReceipt>());
                }
                else
                {
                    var logs = new[]
                    {
                        new LogEntry(contractAddress,
                                     dataFunc(validators),
                                     new[] { validatorContract.AbiDefinition.Events[ValidatorContract.InitiateChange].GetHash(), block.ParentHash })
                    };

                    return(new TxReceipt[]
                    {
                        new TxReceipt()
                        {
                            Logs = logs,
                            Bloom = new Bloom(logs)
                        }
                    });
                }
            }
        public IEnumerable <Transaction> GetTransactions(BlockHeader parent, long gasLimit)
        {
            foreach (var transaction in _contractValidator.GetTransactions(parent, gasLimit))
            {
                if (gasLimit >= transaction.GasLimit)
                {
                    gasLimit -= transaction.GasLimit;
                    yield return(transaction);
                }
            }

            if (_contractValidator.ForSealing && IsPosdao(parent.Number + 1))
            {
                FilterReports(parent);
                foreach (var persistentReport in _persistentReports.Take(MaxReportsPerBlock))
                {
                    var transaction = ValidatorContract.ReportMalicious(persistentReport.ValidatorAddress, persistentReport.BlockNumber, persistentReport.Proof);
                    if (transaction != null && gasLimit >= transaction.GasLimit)
                    {
                        gasLimit -= transaction.GasLimit;
                        yield return(transaction);
                    }
                }
            }
        }
Example #3
0
        public void finalize_change_should_call_correct_transaction()
        {
            var expectation = new SystemTransaction()
            {
                Value         = 0,
                Data          = new byte[] { 0x75, 0x28, 0x62, 0x11 },
                Hash          = new Keccak("0x0652461cead47b6e1436fc631debe06bde8bcdd2dad3b9d21df5cf092078c6d3"),
                To            = _contractAddress,
                SenderAddress = Address.SystemUser,
                GasLimit      = Blockchain.Contracts.CallableContract.UnlimitedGas,
                GasPrice      = 0,
                Nonce         = 0
            };

            expectation.Hash = expectation.CalculateHash();

            var contract = new ValidatorContract(
                _transactionProcessor,
                new AbiEncoder(),
                _contractAddress,
                _stateProvider,
                _readOnlyTransactionProcessorSource,
                new Signer(0, TestItem.PrivateKeyD, LimboLogs.Instance));

            contract.FinalizeChange(_block.Header);

            _transactionProcessor.Received().Execute(
                Arg.Is <Transaction>(t => IsEquivalentTo(expectation, t)), _block.Header, Arg.Any <ITxTracer>());
        }
 private IEnumerable <Transaction> GetPersistentReportsTransactions(long currentBlockNumber)
 {
     foreach (PersistentReport persistentReport in _persistentReports)
     {
         // we want to wait at least 1 block with persistent reports to avoid duplicates
         if (persistentReport.BlockNumber + ReportsSkipBlocks < currentBlockNumber)
         {
             yield return(ValidatorContract.ReportMalicious(persistentReport.MaliciousValidator, persistentReport.BlockNumber, persistentReport.Proof));
         }
     }
 }
Example #5
0
 private IEnumerable <Transaction> GetPersistentReportsTransactions(long gasLimit, long currentBlockNumber)
 {
     foreach (var persistentReport in _persistentReports)
     {
         // we want to wait at least 1 block with persistent reports to avoid duplicates
         if (persistentReport.BlockNumber + ReportsSkipBlocks < currentBlockNumber)
         {
             var tx = ValidatorContract.ReportMalicious(persistentReport.MaliciousValidator, persistentReport.BlockNumber, persistentReport.Proof);
             if (tx != null && gasLimit >= tx.GasLimit)
             {
                 gasLimit -= tx.GasLimit;
                 yield return(tx);
             }
         }
     }
 }
        public IEnumerable <Transaction> GetTransactions(BlockHeader parent, long gasLimit)
        {
            if (ForSealing)
            {
                var newBlockNumber = parent.Number + 1;
                if (newBlockNumber < _posdaoTransition)
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace("Skipping a call to emitInitiateChange");
                    }
                }
                else
                {
                    bool emitInitChangeCallable = false;

                    try
                    {
                        emitInitChangeCallable = ValidatorContract.EmitInitiateChangeCallable(parent);
                    }
                    catch (AuRaException e)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Call to {nameof(ValidatorContract.EmitInitiateChangeCallable)} failed.", e);
                        }
                    }

                    if (emitInitChangeCallable)
                    {
                        if (_logger.IsTrace)
                        {
                            _logger.Trace($"New block #{newBlockNumber} issued ― calling emitInitiateChange()");
                        }
                        Metrics.EmitInitiateChange++;
                        yield return(ValidatorContract.EmitInitiateChange());
                    }
                    else
                    {
                        if (_logger.IsTrace)
                        {
                            _logger.Trace($"New block #{newBlockNumber} issued ― no need to call emitInitiateChange()");
                        }
                    }
                }
            }
        }
        public void finalize_change_should_return_valid_transaction_when_validator_available()
        {
            var expectation = new Transaction()
            {
                Value         = 0,
                Data          = new byte[] { 0x75, 0x28, 0x62, 0x11 },
                Hash          = new Keccak("0xa43db3bf833748d98eb99453bc933f313f9f6a7471fed0018190f0d5b0f863a1"),
                To            = _contractAddress,
                SenderAddress = Address.SystemUser,
                GasLimit      = long.MaxValue,
                GasPrice      = 0,
                Nonce         = 0
            };

            var contract = new ValidatorContract(new AbiEncoder(), _contractAddress);

            var transaction = contract.FinalizeChange();

            transaction.Should().BeEquivalentTo(expectation);
        }