From 5443e48e3de83451c285148bf32821b965f7c795 Mon Sep 17 00:00:00 2001 From: Shuai Date: Thu, 12 Oct 2023 16:32:19 +0800 Subject: [PATCH 1/7] init --- src/RpcClient/RpcClient.cs | 11 +++++++ src/RpcServer/RpcServer.Wallet.cs | 54 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/RpcClient/RpcClient.cs b/src/RpcClient/RpcClient.cs index 957025f1a..62633ae41 100644 --- a/src/RpcClient/RpcClient.cs +++ b/src/RpcClient/RpcClient.cs @@ -645,6 +645,17 @@ public async Task SendToAddressAsync(string assetId, string address, st .ConfigureAwait(false); } + /// + /// Cancel Tx. + /// + /// This function returns Signed Transaction JSON if successful, ContractParametersContext JSON if signing failed. + public async Task CancelTransactionAsync(UInt256 txId, string[] signers, string extraFee) + { + JToken[] parameters = signers.Select(s => (JString)s.AsScriptHash()).ToArray(); + return (JObject)await RpcSendAsync(GetRpcName(), txId.ToString(), new JArray(parameters), extraFee).ConfigureAwait(false); + } + + #endregion Wallet #region Plugins diff --git a/src/RpcServer/RpcServer.Wallet.cs b/src/RpcServer/RpcServer.Wallet.cs index 7ced0421a..ed955746a 100644 --- a/src/RpcServer/RpcServer.Wallet.cs +++ b/src/RpcServer/RpcServer.Wallet.cs @@ -319,6 +319,60 @@ protected virtual JToken SendToAddress(JArray _params) return SignAndRelay(snapshot, tx); } + [RpcMethod] + protected virtual JToken CancelTransaction(JArray _params) + { + CheckWallet(); + var txid = UInt256.Parse(_params[0].AsString()); + TransactionState state = NativeContract.Ledger.GetTransactionState(system.StoreView, txid); + if (state != null) + { + throw new RpcException(32700, "This tx is already confirmed, can't be cancelled."); + } + + var conflict = new TransactionAttribute[] { new Conflicts() { Hash = txid } }; + Signer[] signers = _params.Count >= 2 ? ((JArray)_params[1]).Select(j => new Signer() { Account = UInt160.Parse(j.AsString()), Scopes = WitnessScope.CalledByEntry }).ToArray() : Array.Empty(); + if (!signers.Any()) + { + throw new RpcException(32701, "no signers"); + } + + Transaction tx = new Transaction + { + Signers = signers, + Attributes = conflict, + Witnesses = Array.Empty(), + }; + + try + { + using ScriptBuilder scriptBuilder = new(); + scriptBuilder.Emit(OpCode.RET); + tx = wallet.MakeTransaction(system.StoreView, scriptBuilder.ToArray(), signers[0].Account, signers, conflict); + } + catch (InvalidOperationException e) + { + throw new RpcException(-500, GetExceptionMessage(e)); + } + + if (system.MemPool.TryGetValue(txid, out Transaction conflictTx)) + { + tx.NetworkFee = Math.Max(tx.NetworkFee, conflictTx.NetworkFee) + 1; + } + else if (_params.Count >= 3) + { + var extracFee = _params[2].AsString(); + AssetDescriptor descriptor = new(system.StoreView, system.Settings, NativeContract.GAS.Hash); + if (!BigDecimal.TryParse(extracFee, descriptor.Decimals, out BigDecimal decimalExtraFee) || decimalExtraFee.Sign <= 0) + { + throw new RpcException(32702, "Incorrect Amount Format"); + } + tx.NetworkFee += (long)decimalExtraFee.Value; + }; + return SignAndRelay(system.StoreView, tx); + } + + [RpcMethod] protected virtual JToken InvokeContractVerify(JArray _params) { From 67ac2e505faa4fe2609c008ede919d2c5ce01fd7 Mon Sep 17 00:00:00 2001 From: Shine Li Date: Thu, 12 Oct 2023 18:08:59 +0800 Subject: [PATCH 2/7] Update src/RpcServer/RpcServer.Wallet.cs Co-authored-by: Christopher Schuchardt --- src/RpcServer/RpcServer.Wallet.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/RpcServer/RpcServer.Wallet.cs b/src/RpcServer/RpcServer.Wallet.cs index ed955746a..a9bdd2cf7 100644 --- a/src/RpcServer/RpcServer.Wallet.cs +++ b/src/RpcServer/RpcServer.Wallet.cs @@ -346,9 +346,7 @@ protected virtual JToken CancelTransaction(JArray _params) try { - using ScriptBuilder scriptBuilder = new(); - scriptBuilder.Emit(OpCode.RET); - tx = wallet.MakeTransaction(system.StoreView, scriptBuilder.ToArray(), signers[0].Account, signers, conflict); + tx = wallet.MakeTransaction(system.StoreView, new[] { (byte)OpCode.RET }, signers[0].Account, signers, conflict); } catch (InvalidOperationException e) { From 38c7e7a367629c2c1b5d62394ddaa8be05808cd0 Mon Sep 17 00:00:00 2001 From: Shine Li Date: Fri, 13 Oct 2023 15:32:25 +0800 Subject: [PATCH 3/7] Update src/RpcServer/RpcServer.Wallet.cs Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com> --- src/RpcServer/RpcServer.Wallet.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RpcServer/RpcServer.Wallet.cs b/src/RpcServer/RpcServer.Wallet.cs index a9bdd2cf7..2bdf62185 100644 --- a/src/RpcServer/RpcServer.Wallet.cs +++ b/src/RpcServer/RpcServer.Wallet.cs @@ -359,9 +359,9 @@ protected virtual JToken CancelTransaction(JArray _params) } else if (_params.Count >= 3) { - var extracFee = _params[2].AsString(); + var extraFee = _params[2].AsString(); AssetDescriptor descriptor = new(system.StoreView, system.Settings, NativeContract.GAS.Hash); - if (!BigDecimal.TryParse(extracFee, descriptor.Decimals, out BigDecimal decimalExtraFee) || decimalExtraFee.Sign <= 0) + if (!BigDecimal.TryParse(extraFee, descriptor.Decimals, out BigDecimal decimalExtraFee) || decimalExtraFee.Sign <= 0) { throw new RpcException(32702, "Incorrect Amount Format"); } From 6c956515a27668b7e9e9385d74040e7a606dc206 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 13 Oct 2023 01:24:28 -0700 Subject: [PATCH 4/7] Update src/RpcServer/RpcServer.Wallet.cs Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com> --- src/RpcServer/RpcServer.Wallet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RpcServer/RpcServer.Wallet.cs b/src/RpcServer/RpcServer.Wallet.cs index 2bdf62185..d290da63e 100644 --- a/src/RpcServer/RpcServer.Wallet.cs +++ b/src/RpcServer/RpcServer.Wallet.cs @@ -331,7 +331,7 @@ protected virtual JToken CancelTransaction(JArray _params) } var conflict = new TransactionAttribute[] { new Conflicts() { Hash = txid } }; - Signer[] signers = _params.Count >= 2 ? ((JArray)_params[1]).Select(j => new Signer() { Account = UInt160.Parse(j.AsString()), Scopes = WitnessScope.CalledByEntry }).ToArray() : Array.Empty(); + Signer[] signers = _params.Count >= 2 ? ((JArray)_params[1]).Select(j => new Signer() { Account = AddressToScriptHash(j.AsString(), system.Settings.AddressVersion), Scopes = WitnessScope.CalledByEntry }).ToArray() : Array.Empty(); if (!signers.Any()) { throw new RpcException(32701, "no signers"); From dc16443774a36ab820b881209cfcc63881776f92 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 13 Oct 2023 01:25:17 -0700 Subject: [PATCH 5/7] Clean empty lines --- src/RpcClient/RpcClient.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/RpcClient/RpcClient.cs b/src/RpcClient/RpcClient.cs index 62633ae41..6291ce044 100644 --- a/src/RpcClient/RpcClient.cs +++ b/src/RpcClient/RpcClient.cs @@ -655,7 +655,6 @@ public async Task CancelTransactionAsync(UInt256 txId, string[] signers return (JObject)await RpcSendAsync(GetRpcName(), txId.ToString(), new JArray(parameters), extraFee).ConfigureAwait(false); } - #endregion Wallet #region Plugins From 8b84dae2f32ce90c0cc1d458c869fef0fb274ce1 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 13 Oct 2023 01:25:40 -0700 Subject: [PATCH 6/7] Clean empty lines --- src/RpcServer/RpcServer.Wallet.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/RpcServer/RpcServer.Wallet.cs b/src/RpcServer/RpcServer.Wallet.cs index d290da63e..14095f300 100644 --- a/src/RpcServer/RpcServer.Wallet.cs +++ b/src/RpcServer/RpcServer.Wallet.cs @@ -370,7 +370,6 @@ protected virtual JToken CancelTransaction(JArray _params) return SignAndRelay(system.StoreView, tx); } - [RpcMethod] protected virtual JToken InvokeContractVerify(JArray _params) { From 040e0adfb45201b8aade3995178eb2648d977fd3 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 13 Oct 2023 04:09:14 -0700 Subject: [PATCH 7/7] Update src/RpcServer/RpcServer.Wallet.cs --- src/RpcServer/RpcServer.Wallet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RpcServer/RpcServer.Wallet.cs b/src/RpcServer/RpcServer.Wallet.cs index 14095f300..097dafcd3 100644 --- a/src/RpcServer/RpcServer.Wallet.cs +++ b/src/RpcServer/RpcServer.Wallet.cs @@ -331,7 +331,7 @@ protected virtual JToken CancelTransaction(JArray _params) } var conflict = new TransactionAttribute[] { new Conflicts() { Hash = txid } }; - Signer[] signers = _params.Count >= 2 ? ((JArray)_params[1]).Select(j => new Signer() { Account = AddressToScriptHash(j.AsString(), system.Settings.AddressVersion), Scopes = WitnessScope.CalledByEntry }).ToArray() : Array.Empty(); + Signer[] signers = _params.Count >= 2 ? ((JArray)_params[1]).Select(j => new Signer() { Account = AddressToScriptHash(j.AsString(), system.Settings.AddressVersion), Scopes = WitnessScope.None }).ToArray() : Array.Empty(); if (!signers.Any()) { throw new RpcException(32701, "no signers");