private string SendTransaction(JObject opts) { var from = opts["from"]?.ToString().HexToBytes().ToUInt160() ?? _systemContractReader.NodeAddress(); var to = opts["to"]?.ToString().HexToBytes().ToUInt160() ?? throw new Exception($"\"to\" {opts["to"]} is not valid"); var value = Money.Parse(opts["amount"]?.ToString() ?? throw new Exception($"\"amount\" {opts["amount"]} is not valid") ); var invocation = opts["data"]?.ToString().HexToBytes(); var nonce = _transactionPool.GetNextNonceForAddress(from); var tx = new Transaction { To = to, From = from, GasPrice = (ulong)_stateManager.CurrentSnapshot.NetworkGasPrice, /* TODO: "calculate gas limit for input size" */ GasLimit = 10000000, Nonce = nonce, Value = value.ToUInt256(), Invocation = ByteString.CopyFrom(invocation) }; return(AddTxToPool(tx)); }
public void TestTxPoolAdding() { bool useNewChainId = HardforkHeights.IsHardfork_9Active(_blockManager.GetHeight() + 1); var tx = TestUtils.GetRandomTransaction(useNewChainId); var result = _transactionPool.Add(tx); Assert.AreEqual(OperatingError.InsufficientBalance, result); _stateManager.LastApprovedSnapshot.Balances.AddBalance(tx.Transaction.From, Money.Parse("1000")); result = _transactionPool.Add(tx); Assert.AreEqual(OperatingError.Ok, result); result = _transactionPool.Add(tx); Assert.AreEqual(OperatingError.AlreadyExists, result); var tx2 = TestUtils.GetRandomTransaction(useNewChainId); tx2.Transaction.Nonce++; result = _transactionPool.Add(tx2); Assert.AreEqual(OperatingError.InvalidNonce, result); /* TODO: maybe we should fix this strange behaviour */ var tx3 = TestUtils.GetRandomTransaction(useNewChainId); tx3.Transaction.From = UInt160Utils.Zero; tx3.Transaction.Nonce = _transactionPool.GetNextNonceForAddress(UInt160Utils.Zero); result = _transactionPool.Add(tx3); Assert.AreEqual(OperatingError.Ok, result); }
private void _Bench_Execute_Blocks( ITransactionBuilder transactionBuilder, ITransactionSigner transactionSigner, EcdsaKeyPair keyPair) { const int txGenerate = 50; const int txPerBlock = 10; Logger.LogInformation($"Setting initial balance for the 'From' address"); _stateManager.LastApprovedSnapshot.Balances.AddBalance(keyPair.PublicKey.GetAddress(), Money.Parse("2000000")); for (var k = 0; k < txGenerate / txPerBlock; k++) { var txReceipts = new List <TransactionReceipt>(); var watch = System.Diagnostics.Stopwatch.StartNew(); for (int i = 0; i < txPerBlock; i++) { var randomValue = new Random().Next(1, 100); var amount = Money.Parse($"{randomValue}.0").ToUInt256(); byte[] random = new byte[32]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetBytes(random); var tx = new Transaction { To = random.Slice(0, 20).ToUInt160(), From = keyPair.PublicKey.GetAddress(), GasPrice = (ulong)Money.Parse("0.0000001").ToWei(), GasLimit = 100000000, Nonce = _transactionPool.GetNextNonceForAddress(keyPair.PublicKey.GetAddress()) + (ulong)i, Value = amount }; txReceipts.Add(transactionSigner.Sign(tx, keyPair, true)); } watch.Stop(); Console.WriteLine($"Building TXs Time: {watch.ElapsedMilliseconds} ms"); Block block = null !; watch.Restart(); block = BuildBlock(txReceipts.ToArray()); watch.Stop(); Console.WriteLine($"Building Block Time: {watch.ElapsedMilliseconds} ms"); watch.Restart(); ExecuteBlock(block, txReceipts.ToArray()); watch.Stop(); Console.WriteLine($"Block Emulation + Execution Time: {watch.ElapsedMilliseconds} ms"); var executedBlock = _stateManager.LastApprovedSnapshot.Blocks.GetBlockByHeight(block !.Header.Index); Console.WriteLine($"Executed Transactions: {executedBlock!.TransactionHashes.Count}"); Console.WriteLine( $"Balance After Transaction {_stateManager.LastApprovedSnapshot.Balances.GetBalance(keyPair.PublicKey.GetAddress())}"); } }
private TransactionReceipt TopUpBalanceTx(UInt160 to, UInt256 value, int nonceInc, bool useNewChainId) { var keyPair = new EcdsaKeyPair("0xd95d6db65f3e2223703c5d8e205d98e3e6b470f067b0f94f6c6bf73d4301ce48" .HexToBytes().ToPrivateKey()); var tx = new Transaction { To = to, From = keyPair.PublicKey.GetAddress(), GasPrice = (ulong)Money.Parse("0.0000001").ToWei(), GasLimit = 4_000_000, Nonce = _transactionPool.GetNextNonceForAddress(keyPair.PublicKey.GetAddress()) + (ulong)nonceInc, Value = value }; return(Signer.Sign(tx, keyPair, useNewChainId)); }
public string GetTransactionCount(string from, string blockId) { ulong nonce; if (blockId.Equals("pending")) { nonce = _transactionPool.GetNextNonceForAddress(from.HexToUInt160()); } else { nonce = GetSnapshotByTag(blockId) !.Transactions.GetTotalTransactionCount(from.HexToUInt160()); } return(Web3DataFormatUtils.Web3Number(nonce)); }
public Transaction TransferTransaction(UInt160 from, UInt160 to, Money value, ulong gasPrice, byte[]?input) { var nonce = _transactionPool.GetNextNonceForAddress(from); if (gasPrice == 0) { gasPrice = (ulong)_stateManager.CurrentSnapshot.NetworkGasPrice; } var tx = new Transaction { To = to, Value = value.ToUInt256(), From = from, GasPrice = gasPrice, GasLimit = GasMetering.DefaultBlockGasLimit, Nonce = nonce }; if (input != null) { tx.Invocation = ByteString.CopyFrom(input); } return(tx); }