private void RunTest(TransactionTest test, IReleaseSpec spec) { //TestContext.CurrentContext.Test.Properties.Set("Category", test.Network); // no longer public ValidTransactionTest validTest = test as ValidTransactionTest; Nethermind.Core.Transaction transaction; try { Rlp rlp = new Rlp(Bytes.FromHexString(test.Rlp)); transaction = Rlp.Decode <Nethermind.Core.Transaction>(rlp); } catch (Exception) { if (validTest == null) { return; } throw; } bool useChainId = transaction.Signature.V > 28; TxValidator validator = new TxValidator(useChainId ? ChainId.MainNet : 0); if (validTest != null) { Assert.AreEqual(validTest.Value, transaction.Value, "value"); Assert.AreEqual(validTest.Data, transaction.Data ?? transaction.Init, "data"); Assert.AreEqual(validTest.GasLimit, transaction.GasLimit, "gasLimit"); Assert.AreEqual(validTest.GasPrice, transaction.GasPrice, "gasPrice"); Assert.AreEqual(validTest.Nonce, transaction.Nonce, "nonce"); Assert.AreEqual(validTest.To, transaction.To, "to"); Assert.True(validator.IsWellFormed(transaction, spec)); Signature expectedSignature = new Signature(validTest.R, validTest.S, validTest.V); Assert.AreEqual(expectedSignature, transaction.Signature, "signature"); // if(useChainId && spec.IsEip155Enabled) // IEthereumEcdsa ecdsa = new EthereumEcdsa(new SingleReleaseSpecProvider(spec, useChainId ? (int)ChainId.MainNet : 0), NullLogManager.Instance); bool verified = ecdsa.Verify( validTest.Sender, transaction, 0); Assert.True(verified); } else { Assert.False(validator.IsWellFormed(transaction, spec)); } }
public void No_chain_id_tx_is_valid() { byte[] sigData = new byte[65]; sigData[31] = 1; // correct r sigData[63] = 1; // correct s Signature signature = new Signature(sigData); var tx = Build.A.Transaction.WithSignature(signature).TestObject; TxValidator txValidator = new TxValidator(1); txValidator.IsWellFormed(tx, MuirGlacier.Instance).Should().BeTrue(); }
public void Zero_s_is_not_valid() { byte[] sigData = new byte[65]; sigData[31] = 1; // correct r // s is zero Signature signature = new Signature(sigData); var tx = Build.A.Transaction.WithSignature(signature).TestObject; TxValidator txValidator = new TxValidator(1); txValidator.IsWellFormed(tx, MuirGlacier.Instance).Should().BeFalse(); }
public void Bad_chain_id_is_not_valid() { byte[] sigData = new byte[65]; sigData[31] = 1; // correct r sigData[63] = 1; // correct s sigData[64] = 39; Signature signature = new Signature(sigData); Transaction tx = Build.A.Transaction.WithSignature(signature).TestObject; TxValidator txValidator = new TxValidator(1); txValidator.IsWellFormed(tx, MuirGlacier.Instance).Should().BeFalse(); }
public void Before_eip_155_has_to_have_valid_chain_id_unless_overridden(bool validateChainId) { byte[] sigData = new byte[65]; sigData[31] = 1; // correct r sigData[63] = 1; // correct s sigData[64] = 41; Signature signature = new Signature(sigData); var tx = Build.A.Transaction.WithSignature(signature).TestObject; IReleaseSpec releaseSpec = Substitute.For <IReleaseSpec>(); releaseSpec.IsEip155Enabled.Returns(false); releaseSpec.ValidateChainId.Returns(validateChainId); TxValidator txValidator = new TxValidator(1); txValidator.IsWellFormed(tx, releaseSpec).Should().Be(!validateChainId); }
public bool Chain_Id_required_for_non_legacy_transactions_after_Berlin(TxType txType) { byte[] sigData = new byte[65]; sigData[31] = 1; // correct r sigData[63] = 1; // correct s sigData[64] = 38; Signature signature = new Signature(sigData); Transaction tx = Build.A.Transaction .WithType(txType > TxType.AccessList ? TxType.Legacy : txType) .WithAccessList(txType == TxType.AccessList ? new AccessList(new Dictionary <Address, IReadOnlySet <UInt256> >()) : null) .WithSignature(signature).TestObject; tx.Type = txType; TxValidator txValidator = new TxValidator(ChainId.Mainnet); return(txValidator.IsWellFormed(tx, Berlin.Instance)); }
public bool Before_eip_2930_has_to_be_legacy_tx(TxType txType, bool eip2930) { byte[] sigData = new byte[65]; sigData[31] = 1; // correct r sigData[63] = 1; // correct s sigData[64] = 38; Signature signature = new Signature(sigData); Transaction tx = Build.A.Transaction .WithType(txType > TxType.AccessList ? TxType.Legacy : txType) .WithChainId(ChainId.Mainnet) .WithAccessList(txType == TxType.AccessList ? new AccessList(new Dictionary <Address, IReadOnlySet <UInt256> >()) : null) .WithSignature(signature).TestObject; tx.Type = txType; TxValidator txValidator = new TxValidator(1); return(txValidator.IsWellFormed(tx, eip2930 ? Berlin.Instance : MuirGlacier.Instance)); }
public bool Before_eip_1559_has_to_be_legacy_or_access_list_tx(TxType txType, bool eip2930, bool eip1559) { byte[] sigData = new byte[65]; sigData[31] = 1; // correct r sigData[63] = 1; // correct s sigData[64] = 38; Signature signature = new Signature(sigData); Transaction tx = Build.A.Transaction .WithType(txType) .WithChainId(ChainId.Mainnet) .WithAccessList(txType == TxType.AccessList || txType == TxType.EIP1559 ? new AccessList(new Dictionary <Address, IReadOnlySet <UInt256> >()) : null) .WithSignature(signature).TestObject; tx.Type = txType; TxValidator txValidator = new TxValidator(1); IReleaseSpec releaseSpec = new ReleaseSpec() { IsEip2930Enabled = eip2930, IsEip1559Enabled = eip1559 }; return(txValidator.IsWellFormed(tx, releaseSpec)); }
protected EthereumTestResult RunTest(GeneralStateTest test, ITxTracer txTracer) { 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 = 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.Fork)); if (specProvider.GenesisSpec != Frontier.Instance) { Assert.Fail("Expected genesis spec to be Frontier for blockchain tests"); } TrieStore trieStore = new(stateDb, _logManager); StateProvider stateProvider = new (trieStore, codeDb, _logManager); IBlockhashProvider blockhashProvider = new TestBlockhashProvider(); IStorageProvider storageProvider = new StorageProvider(trieStore, stateProvider, _logManager); IVirtualMachine virtualMachine = new VirtualMachine( blockhashProvider, specProvider, _logManager); TransactionProcessor transactionProcessor = new( specProvider, stateProvider, storageProvider, virtualMachine, _logManager); InitializeTestState(test, stateProvider, storageProvider, specProvider); BlockHeader header = new(test.PreviousHash, Keccak.OfAnEmptySequenceRlp, test.CurrentCoinbase, test.CurrentDifficulty, test.CurrentNumber, test.CurrentGasLimit, test.CurrentTimestamp, new byte[0]); header.BaseFeePerGas = test.Fork.IsEip1559Enabled ? test.CurrentBaseFee ?? _defaultBaseFeeForStateTest : UInt256.Zero; header.StateRoot = test.PostHash; header.Hash = header.CalculateHash(); Stopwatch stopwatch = Stopwatch.StartNew(); var txValidator = new TxValidator((MainnetSpecProvider.Instance.ChainId)); var spec = specProvider.GetSpec(test.CurrentNumber); if (test.Transaction.ChainId == null) { test.Transaction.ChainId = MainnetSpecProvider.Instance.ChainId; } bool isValid = txValidator.IsWellFormed(test.Transaction, spec); if (isValid) { transactionProcessor.Execute(test.Transaction, header, txTracer); } stopwatch.Stop(); stateProvider.Commit(specProvider.GenesisSpec); stateProvider.CommitTree(1); // '@winsvega added a 0-wei reward to the miner , so we had to add that into the state test execution phase. He needed it for retesteth.' if (!stateProvider.AccountExists(test.CurrentCoinbase)) { stateProvider.CreateAccount(test.CurrentCoinbase, 0); } stateProvider.RecalculateStateRoot(); List <string> differences = RunAssertions(test, stateProvider); EthereumTestResult testResult = new(test.Name, test.ForkName, differences.Count == 0); testResult.TimeInMs = (int)stopwatch.Elapsed.TotalMilliseconds; testResult.StateRoot = stateProvider.StateRoot; // Assert.Zero(differences.Count, "differences"); return(testResult); }