public void Test(TransactionTest test) { EthereumEcdsa ethereumEcdsa = new EthereumEcdsa(OlympicSpecProvider.Instance, NullLogManager.Instance); Transaction decodedUnsigned = Rlp.Decode <Transaction>(test.Unsigned); Assert.AreEqual(test.Value, decodedUnsigned.Value, "value"); Assert.AreEqual(test.GasPrice, decodedUnsigned.GasPrice, "gasPrice"); Assert.AreEqual(test.StartGas, decodedUnsigned.GasLimit, "gasLimit"); Assert.AreEqual(test.Data, decodedUnsigned.Data ?? decodedUnsigned.Init, "data"); Assert.AreEqual(test.To, decodedUnsigned.To, "to"); Assert.AreEqual(test.Nonce, decodedUnsigned.Nonce, "nonce"); Transaction decodedSigned = Rlp.Decode <Transaction>(test.Signed); ethereumEcdsa.Sign(test.PrivateKey, decodedUnsigned, 0); Assert.AreEqual(decodedSigned.Signature.R, decodedUnsigned.Signature.R, "R"); BigInteger expectedS = decodedSigned.Signature.S.ToUnsignedBigInteger(); BigInteger actualS = decodedUnsigned.Signature.S.ToUnsignedBigInteger(); BigInteger otherS = EthereumEcdsa.LowSTransform - actualS; // test does not use normalized signature if (otherS != expectedS && actualS != expectedS) { throw new Exception("S is wrong"); } int vToCompare = decodedUnsigned.Signature.V; if (otherS == decodedSigned.Signature.S.ToUnsignedBigInteger()) { vToCompare = vToCompare == 27 ? 28 : 27; } Assert.AreEqual(decodedSigned.Signature.V, vToCompare, "V"); }
private static TransactionTest Convert(TransactionTestJson testJson) { TransactionTest test = new TransactionTest(); test.Value = (UInt256)testJson.Value; test.Data = Bytes.FromHexString(testJson.Data); test.GasPrice = (UInt256)testJson.GasPrice; test.PrivateKey = new PrivateKey(testJson.Key); test.Nonce = (UInt256)testJson.Nonce; test.Signed = new Rlp(Bytes.FromHexString(testJson.Signed)); byte[] unsigned = Bytes.FromHexString(testJson.Unsigned); if (unsigned[0] == 0xf8) { unsigned[1] -= 3; } else { unsigned[0] -= 3; } test.Unsigned = new Rlp(unsigned.Slice(0, unsigned.Length - 3)); test.StartGas = testJson.StartGas; test.To = string.IsNullOrEmpty(testJson.To) ? null : new Address(testJson.To); return(test); }
private static IEnumerable <TransactionTest> LoadTests(string testSet) { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); IEnumerable <string> testDirs = Directory.EnumerateDirectories(".", "tt" + testSet); Dictionary <string, Dictionary <string, TransactionTestJson> > testJsons = new(); foreach (string testDir in testDirs) { testJsons[testDir] = new Dictionary <string, TransactionTestJson>(); IEnumerable <string> testFiles = Directory.EnumerateFiles(testDir).ToList(); foreach (string testFile in testFiles) { string json = File.ReadAllText(testFile); Dictionary <string, TransactionTestJson> testsInFile = JsonConvert.DeserializeObject <Dictionary <string, TransactionTestJson> >(json); foreach (KeyValuePair <string, TransactionTestJson> namedTest in testsInFile) { testJsons[testDir].Add(namedTest.Key, namedTest.Value); } } } List <TransactionTest> tests = new(); foreach (KeyValuePair <string, Dictionary <string, TransactionTestJson> > byDir in testJsons) { foreach (KeyValuePair <string, TransactionTestJson> byName in byDir.Value) { TransactionJson transactionJson = byName.Value.Transaction; TransactionTest test; if (transactionJson != null) { test = new ValidTransactionTest(byDir.Key, byName.Key, byName.Value.Rlp); ValidTransactionTest validTest = (ValidTransactionTest)test; validTest.BlockNumber = Bytes.FromHexString(byName.Value.BlockNumber).ToUInt256(); validTest.Data = Bytes.FromHexString(transactionJson.Data); validTest.GasLimit = Bytes.FromHexString(transactionJson.GasLimit).ToUInt256(); validTest.GasPrice = Bytes.FromHexString(transactionJson.GasPrice).ToUInt256(); validTest.Nonce = Bytes.FromHexString(transactionJson.Nonce).ToUInt256(); validTest.R = Bytes.FromHexString(transactionJson.R).ToUInt256(); validTest.S = Bytes.FromHexString(transactionJson.S).ToUInt256(); validTest.V = Bytes.FromHexString(transactionJson.V)[0]; validTest.Sender = new Address(byName.Value.Sender); validTest.Value = Bytes.FromHexString(transactionJson.Value).ToUInt256(); validTest.To = string.IsNullOrEmpty(transactionJson.To) ? null : new Address(transactionJson.To); } else { test = new TransactionTest(byDir.Key, byName.Key, byName.Value.Rlp); } tests.Add(test); } } return(tests); }
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; SignatureValidator signatureValidator = new SignatureValidator(useChainId ? ChainId.MainNet : 0); TransactionValidator validator = new TransactionValidator(signatureValidator); 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)); } }
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(Bytes.FromHexString(test.Rlp)); transaction = Rlp.Decode <Nethermind.Core.Transaction>(rlp); } catch (Exception) { if (validTest == null) { return; } throw; } bool useChainId = transaction.Signature.V > 28UL; TxValidator validator = new(useChainId ? ChainId.Mainnet : 0UL); if (validTest != null) { Assert.AreEqual(validTest.Value, transaction.Value, "value"); Assert.AreEqual(validTest.Data, transaction.Data, "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(validTest.R, validTest.S, validTest.V); Assert.AreEqual(expectedSignature, transaction.Signature, "signature"); IEthereumEcdsa ecdsa = new EthereumEcdsa(useChainId ? ChainId.Mainnet : 0UL, LimboLogs.Instance); bool verified = ecdsa.Verify( validTest.Sender, transaction); Assert.True(verified); } else { Assert.False(validator.IsWellFormed(transaction, spec)); } }
public void Transaction_Commit() { var ts = new TransactionTest(); ts.Commit(); }
public void Test_Address(TransactionTest test) { RunTest(test, Frontier.Instance); }
public void Test_zero_sig_constantinople(TransactionTest test) { RunTest(test, Byzantium.Instance); }
public void Test_wrong_rlp_homestead(TransactionTest test) { RunTest(test, Homestead.Instance); }
public void Test_wrong_rlp_frontier(TransactionTest test) { RunTest(test, Frontier.Instance); }
public void Test_v_rule_eip158(TransactionTest test) { RunTest(test, SpuriousDragon.Instance); }
public void Test_eip155VitaliksHomesead(TransactionTest test) { RunTest(test, Homestead.Instance); }
public void Test_eip155VitaliksEip158(TransactionTest test) { RunTest(test, SpuriousDragon.Instance); }
public void Transaction_Rolback() { var ts = new TransactionTest(); ts.Rollback(); }
public void Test_WrongRLP(TransactionTest test) { RunTest(test, Frontier.Instance); }
public void Test_Value(TransactionTest test) { RunTest(test, Frontier.Instance); }
public void Test_Signature(TransactionTest test) { RunTest(test, Frontier.Instance); }
public void Test_GasPrice(TransactionTest test) { RunTest(test, Frontier.Instance); }
public void Test_EIP2028(TransactionTest test) { RunTest(test, Frontier.Instance); }