From 306bd18f72be5db4ba6c69e22779ca6143d13e9b Mon Sep 17 00:00:00 2001 From: erikzhang Date: Mon, 7 Jan 2019 22:36:04 +0800 Subject: [PATCH 1/7] Add commit phase to consensus algorithm --- neo/Consensus/Commit.cs | 28 +++++++ neo/Consensus/ConsensusContext.cs | 45 +++++------ neo/Consensus/ConsensusMessageType.cs | 2 + neo/Consensus/ConsensusService.cs | 107 ++++++++++++++++---------- neo/Consensus/ConsensusState.cs | 7 +- neo/Consensus/IConsensusContext.cs | 9 ++- neo/Consensus/PrepareRequest.cs | 5 +- neo/Consensus/PrepareResponse.cs | 20 +---- 8 files changed, 129 insertions(+), 94 deletions(-) create mode 100644 neo/Consensus/Commit.cs diff --git a/neo/Consensus/Commit.cs b/neo/Consensus/Commit.cs new file mode 100644 index 0000000000..cdd45d24ef --- /dev/null +++ b/neo/Consensus/Commit.cs @@ -0,0 +1,28 @@ +using System.IO; + +namespace Neo.Consensus +{ + internal class Commit : ConsensusMessage + { + public byte[] Signature; + + public override int Size => base.Size + Signature.Length; + + public Commit() + : base(ConsensusMessageType.Commit) + { + } + + public override void Deserialize(BinaryReader reader) + { + base.Deserialize(reader); + Signature = reader.ReadBytes(64); + } + + public override void Serialize(BinaryWriter writer) + { + base.Serialize(writer); + writer.Write(Signature); + } + } +} diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index d7d9b1bcfd..dcd646965d 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -28,7 +28,8 @@ internal class ConsensusContext : IConsensusContext public UInt160 NextConsensus { get; set; } public UInt256[] TransactionHashes { get; set; } public Dictionary Transactions { get; set; } - public byte[][] Signatures { get; set; } + public bool[] Preparations { get; set; } + public byte[][] Commits { get; set; } public byte[] ExpectedView { get; set; } private Snapshot snapshot; private KeyPair keyPair; @@ -46,14 +47,12 @@ public ConsensusContext(Wallet wallet) public void ChangeView(byte view_number) { - State &= ConsensusState.SignatureSent; + State = ConsensusState.Initial; ViewNumber = view_number; PrimaryIndex = GetPrimaryIndex(view_number); - if (State == ConsensusState.Initial) - { - TransactionHashes = null; - Signatures = new byte[Validators.Length][]; - } + TransactionHashes = null; + Preparations = new bool[Validators.Length]; + Commits = new byte[Validators.Length][]; if (MyIndex >= 0) ExpectedView[MyIndex] = view_number; _header = null; @@ -66,9 +65,9 @@ public Block CreateBlock() Contract contract = Contract.CreateMultiSigContract(M, Validators); ContractParametersContext sc = new ContractParametersContext(block); for (int i = 0, j = 0; i < Validators.Length && j < M; i++) - if (Signatures[i] != null) + if (Commits[i] != null) { - sc.AddSignature(contract, Validators[i], Signatures[i]); + sc.AddSignature(contract, Validators[i], Commits[i]); j++; } sc.Verifiable.Witnesses = sc.GetWitnesses(); @@ -95,6 +94,16 @@ public ConsensusPayload MakeChangeView() }); } + public ConsensusPayload MakeCommit() + { + if (Commits[MyIndex] == null) + Commits[MyIndex] = MakeHeader()?.Sign(keyPair); + return MakeSignedPayload(new Commit + { + Signature = Commits[MyIndex] + }); + } + private Block _header = null; public Block MakeHeader() { @@ -132,11 +141,6 @@ private ConsensusPayload MakeSignedPayload(ConsensusMessage message) return payload; } - public void SignHeader() - { - Signatures[MyIndex] = MakeHeader()?.Sign(keyPair); - } - private void SignPayload(ConsensusPayload payload) { ContractParametersContext sc; @@ -159,17 +163,13 @@ public ConsensusPayload MakePrepareRequest() Nonce = Nonce, NextConsensus = NextConsensus, TransactionHashes = TransactionHashes, - MinerTransaction = (MinerTransaction)Transactions[TransactionHashes[0]], - Signature = Signatures[MyIndex] + MinerTransaction = (MinerTransaction)Transactions[TransactionHashes[0]] }); } - public ConsensusPayload MakePrepareResponse(byte[] signature) + public ConsensusPayload MakePrepareResponse() { - return MakeSignedPayload(new PrepareResponse - { - Signature = signature - }); + return MakeSignedPayload(new PrepareResponse()); } public void Reset() @@ -184,7 +184,8 @@ public void Reset() MyIndex = -1; PrimaryIndex = BlockIndex % (uint)Validators.Length; TransactionHashes = null; - Signatures = new byte[Validators.Length][]; + Preparations = new bool[Validators.Length]; + Commits = new byte[Validators.Length][]; ExpectedView = new byte[Validators.Length]; keyPair = null; for (int i = 0; i < Validators.Length; i++) diff --git a/neo/Consensus/ConsensusMessageType.cs b/neo/Consensus/ConsensusMessageType.cs index b57dbc3214..d134f3bfb2 100644 --- a/neo/Consensus/ConsensusMessageType.cs +++ b/neo/Consensus/ConsensusMessageType.cs @@ -10,5 +10,7 @@ internal enum ConsensusMessageType : byte PrepareRequest = 0x20, [ReflectionCache(typeof(PrepareResponse))] PrepareResponse = 0x21, + [ReflectionCache(typeof(Commit))] + Commit = 0x30, } } diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 77b727e0c8..84fd18555e 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -59,10 +59,10 @@ private bool AddTransaction(Transaction tx, bool verify) if (context.VerifyRequest()) { Log($"send prepare response"); - context.State |= ConsensusState.SignatureSent; - context.SignHeader(); - localNode.Tell(new LocalNode.SendDirectly { Inventory = context.MakePrepareResponse(context.Signatures[context.MyIndex]) }); - CheckSignatures(); + context.State |= ConsensusState.ResponseSent; + context.Preparations[context.MyIndex] = true; + localNode.Tell(new LocalNode.SendDirectly { Inventory = context.MakePrepareResponse() }); + CheckPreparations(); } else { @@ -83,6 +83,17 @@ private void ChangeTimer(TimeSpan delay) }, ActorRefs.NoSender); } + private void CheckCommits() + { + if (context.Commits.Count(p => p != null) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) + { + Block block = context.CreateBlock(); + Log($"relay block: {block.Hash}"); + localNode.Tell(new LocalNode.Relay { Inventory = block }); + context.State |= ConsensusState.BlockSent; + } + } + private void CheckExpectedView(byte view_number) { if (context.ViewNumber == view_number) return; @@ -92,14 +103,15 @@ private void CheckExpectedView(byte view_number) } } - private void CheckSignatures() + private void CheckPreparations() { - if (context.Signatures.Count(p => p != null) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) + if (context.Preparations.Count(p => p) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) { - Block block = context.CreateBlock(); - Log($"relay block: {block.Hash}"); - localNode.Tell(new LocalNode.Relay { Inventory = block }); - context.State |= ConsensusState.BlockSent; + ConsensusPayload payload = context.MakeCommit(); + Log($"send commit"); + localNode.Tell(new LocalNode.SendDirectly { Inventory = payload }); + context.State |= ConsensusState.CommitSent; + CheckCommits(); } } @@ -136,6 +148,8 @@ private void Log(string message, LogLevel level = LogLevel.Info) private void OnChangeViewReceived(ConsensusPayload payload, ChangeView message) { + if (context.State.HasFlag(ConsensusState.CommitSent)) + return; if (message.NewViewNumber <= context.ExpectedView[payload.ValidatorIndex]) return; Log($"{nameof(OnChangeViewReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} nv={message.NewViewNumber}"); @@ -143,6 +157,22 @@ private void OnChangeViewReceived(ConsensusPayload payload, ChangeView message) CheckExpectedView(message.NewViewNumber); } + private void OnCommitReceived(ConsensusPayload payload, Commit commit) + { + if (context.Commits[payload.ValidatorIndex] != null) return; + Log($"{nameof(OnCommitReceived)}: height={payload.BlockIndex} view={commit.ViewNumber} index={payload.ValidatorIndex}"); + byte[] hashData = context.MakeHeader()?.GetHashData(); + if (hashData == null) + { + context.Commits[payload.ValidatorIndex] = commit.Signature; + } + else if (Crypto.Default.VerifySignature(hashData, commit.Signature, context.Validators[payload.ValidatorIndex].EncodePoint(false))) + { + context.Commits[payload.ValidatorIndex] = commit.Signature; + CheckCommits(); + } + } + private void OnConsensusPayload(ConsensusPayload payload) { if (context.State.HasFlag(ConsensusState.BlockSent)) return; @@ -169,16 +199,19 @@ private void OnConsensusPayload(ConsensusPayload payload) } if (message.ViewNumber != context.ViewNumber && message.Type != ConsensusMessageType.ChangeView) return; - switch (message.Type) + switch (message) { - case ConsensusMessageType.ChangeView: - OnChangeViewReceived(payload, (ChangeView)message); + case ChangeView view: + OnChangeViewReceived(payload, view); break; - case ConsensusMessageType.PrepareRequest: - OnPrepareRequestReceived(payload, (PrepareRequest)message); + case PrepareRequest request: + OnPrepareRequestReceived(payload, request); break; - case ConsensusMessageType.PrepareResponse: - OnPrepareResponseReceived(payload, (PrepareResponse)message); + case PrepareResponse response: + OnPrepareResponseReceived(payload, response); + break; + case Commit commit: + OnCommitReceived(payload, commit); break; } } @@ -212,13 +245,12 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m context.NextConsensus = message.NextConsensus; context.TransactionHashes = message.TransactionHashes; context.Transactions = new Dictionary(); + context.Preparations[payload.ValidatorIndex] = true; byte[] hashData = context.MakeHeader().GetHashData(); - if (!Crypto.Default.VerifySignature(hashData, message.Signature, context.Validators[payload.ValidatorIndex].EncodePoint(false))) return; - for (int i = 0; i < context.Signatures.Length; i++) - if (context.Signatures[i] != null) - if (!Crypto.Default.VerifySignature(hashData, context.Signatures[i], context.Validators[i].EncodePoint(false))) - context.Signatures[i] = null; - context.Signatures[payload.ValidatorIndex] = message.Signature; + for (int i = 0; i < context.Commits.Length; i++) + if (context.Commits[i] != null) + if (!Crypto.Default.VerifySignature(hashData, context.Commits[i], context.Validators[i].EncodePoint(false))) + context.Commits[i] = null; Dictionary mempool = Blockchain.Singleton.GetMemoryPool().ToDictionary(p => p.Hash); List unverified = new List(); foreach (UInt256 hash in context.TransactionHashes.Skip(1)) @@ -251,18 +283,11 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m private void OnPrepareResponseReceived(ConsensusPayload payload, PrepareResponse message) { - if (context.Signatures[payload.ValidatorIndex] != null) return; + if (context.Preparations[payload.ValidatorIndex]) return; Log($"{nameof(OnPrepareResponseReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex}"); - byte[] hashData = context.MakeHeader()?.GetHashData(); - if (hashData == null) - { - context.Signatures[payload.ValidatorIndex] = message.Signature; - } - else if (Crypto.Default.VerifySignature(hashData, message.Signature, context.Validators[payload.ValidatorIndex].EncodePoint(false))) - { - context.Signatures[payload.ValidatorIndex] = message.Signature; - CheckSignatures(); - } + context.Preparations[payload.ValidatorIndex] = true; + if (context.State.HasFlag(ConsensusState.RequestSent) || context.State.HasFlag(ConsensusState.RequestReceived)) + CheckPreparations(); } protected override void OnReceive(object message) @@ -304,13 +329,10 @@ private void OnTimer(Timer timer) if (context.State.HasFlag(ConsensusState.Primary) && !context.State.HasFlag(ConsensusState.RequestSent)) { Log($"send prepare request: height={timer.Height} view={timer.ViewNumber}"); - context.State |= ConsensusState.RequestSent; - if (!context.State.HasFlag(ConsensusState.SignatureSent)) - { - context.Fill(); - context.SignHeader(); - } + context.Fill(); localNode.Tell(new LocalNode.SendDirectly { Inventory = context.MakePrepareRequest() }); + context.State |= ConsensusState.RequestSent; + context.Preparations[context.MyIndex] = true; if (context.TransactionHashes.Length > 1) { foreach (InvPayload payload in InvPayload.CreateGroup(InventoryType.TX, context.TransactionHashes.Skip(1).ToArray())) @@ -320,14 +342,15 @@ private void OnTimer(Timer timer) } else if ((context.State.HasFlag(ConsensusState.Primary) && context.State.HasFlag(ConsensusState.RequestSent)) || context.State.HasFlag(ConsensusState.Backup)) { - RequestChangeView(); + if (!context.State.HasFlag(ConsensusState.CommitSent)) + RequestChangeView(); } } private void OnTransaction(Transaction transaction) { if (transaction.Type == TransactionType.MinerTransaction) return; - if (!context.State.HasFlag(ConsensusState.Backup) || !context.State.HasFlag(ConsensusState.RequestReceived) || context.State.HasFlag(ConsensusState.SignatureSent) || context.State.HasFlag(ConsensusState.ViewChanging) || context.State.HasFlag(ConsensusState.BlockSent)) + if (!context.State.HasFlag(ConsensusState.Backup) || !context.State.HasFlag(ConsensusState.RequestReceived) || context.State.HasFlag(ConsensusState.ResponseSent) || context.State.HasFlag(ConsensusState.ViewChanging) || context.State.HasFlag(ConsensusState.BlockSent)) return; if (context.Transactions.ContainsKey(transaction.Hash)) return; if (!context.TransactionHashes.Contains(transaction.Hash)) return; diff --git a/neo/Consensus/ConsensusState.cs b/neo/Consensus/ConsensusState.cs index 7d3d2aa3c6..989822c299 100644 --- a/neo/Consensus/ConsensusState.cs +++ b/neo/Consensus/ConsensusState.cs @@ -10,8 +10,9 @@ public enum ConsensusState : byte Backup = 0x02, RequestSent = 0x04, RequestReceived = 0x08, - SignatureSent = 0x10, - BlockSent = 0x20, - ViewChanging = 0x40, + ResponseSent = 0x10, + CommitSent = 0x20, + BlockSent = 0x40, + ViewChanging = 0x80, } } diff --git a/neo/Consensus/IConsensusContext.cs b/neo/Consensus/IConsensusContext.cs index 893b36085d..0a23daffbb 100644 --- a/neo/Consensus/IConsensusContext.cs +++ b/neo/Consensus/IConsensusContext.cs @@ -20,7 +20,8 @@ public interface IConsensusContext : IDisposable UInt160 NextConsensus { get; set; } UInt256[] TransactionHashes { get; set; } Dictionary Transactions { get; set; } - byte[][] Signatures { get; set; } + bool[] Preparations { get; set; } + byte[][] Commits { get; set; } byte[] ExpectedView { get; set; } int M { get; } @@ -40,13 +41,13 @@ public interface IConsensusContext : IDisposable ConsensusPayload MakeChangeView(); - Block MakeHeader(); + ConsensusPayload MakeCommit(); - void SignHeader(); + Block MakeHeader(); ConsensusPayload MakePrepareRequest(); - ConsensusPayload MakePrepareResponse(byte[] signature); + ConsensusPayload MakePrepareResponse(); void Reset(); diff --git a/neo/Consensus/PrepareRequest.cs b/neo/Consensus/PrepareRequest.cs index 3542055551..9ebbb678b0 100644 --- a/neo/Consensus/PrepareRequest.cs +++ b/neo/Consensus/PrepareRequest.cs @@ -12,9 +12,8 @@ internal class PrepareRequest : ConsensusMessage public UInt160 NextConsensus; public UInt256[] TransactionHashes; public MinerTransaction MinerTransaction; - public byte[] Signature; - public override int Size => base.Size + sizeof(ulong) + NextConsensus.Size + TransactionHashes.GetVarSize() + MinerTransaction.Size + Signature.Length; + public override int Size => base.Size + sizeof(ulong) + NextConsensus.Size + TransactionHashes.GetVarSize() + MinerTransaction.Size; public PrepareRequest() : base(ConsensusMessageType.PrepareRequest) @@ -32,7 +31,6 @@ public override void Deserialize(BinaryReader reader) MinerTransaction = reader.ReadSerializable(); if (MinerTransaction.Hash != TransactionHashes[0]) throw new FormatException(); - Signature = reader.ReadBytes(64); } public override void Serialize(BinaryWriter writer) @@ -42,7 +40,6 @@ public override void Serialize(BinaryWriter writer) writer.Write(NextConsensus); writer.Write(TransactionHashes); writer.Write(MinerTransaction); - writer.Write(Signature); } } } diff --git a/neo/Consensus/PrepareResponse.cs b/neo/Consensus/PrepareResponse.cs index 184e4e7e12..91a6b24b92 100644 --- a/neo/Consensus/PrepareResponse.cs +++ b/neo/Consensus/PrepareResponse.cs @@ -1,28 +1,10 @@ -using System.IO; - -namespace Neo.Consensus +namespace Neo.Consensus { internal class PrepareResponse : ConsensusMessage { - public byte[] Signature; - - public override int Size => base.Size + Signature.Length; - public PrepareResponse() : base(ConsensusMessageType.PrepareResponse) { } - - public override void Deserialize(BinaryReader reader) - { - base.Deserialize(reader); - Signature = reader.ReadBytes(64); - } - - public override void Serialize(BinaryWriter writer) - { - base.Serialize(writer); - writer.Write(Signature); - } } } From 4cc50f5034a76a8d804cf51c68a8c45d4fc3370c Mon Sep 17 00:00:00 2001 From: erikzhang Date: Mon, 7 Jan 2019 22:39:45 +0800 Subject: [PATCH 2/7] fix tests --- neo.UnitTests/UT_Consensus.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neo.UnitTests/UT_Consensus.cs b/neo.UnitTests/UT_Consensus.cs index d9bd62a574..63db887161 100644 --- a/neo.UnitTests/UT_Consensus.cs +++ b/neo.UnitTests/UT_Consensus.cs @@ -99,8 +99,7 @@ public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart() Nonce = mockConsensusContext.Object.Nonce, NextConsensus = mockConsensusContext.Object.NextConsensus, TransactionHashes = new UInt256[0], - MinerTransaction = minerTx, //(MinerTransaction)Transactions[TransactionHashes[0]], - Signature = new byte[64]//Signatures[MyIndex] + MinerTransaction = minerTx //(MinerTransaction)Transactions[TransactionHashes[0]], }; ConsensusMessage mprep = prep; From 5aa40750cc046c185f4ecf312b7459403e05eff8 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Fri, 11 Jan 2019 16:28:16 +0800 Subject: [PATCH 3/7] Prevent repeated sending of `Commit` messages --- neo/Consensus/ConsensusService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 84fd18555e..5a3a2bfd5d 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -285,6 +285,7 @@ private void OnPrepareResponseReceived(ConsensusPayload payload, PrepareResponse { if (context.Preparations[payload.ValidatorIndex]) return; Log($"{nameof(OnPrepareResponseReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex}"); + if (context.State.HasFlag(ConsensusState.CommitSent)) return; context.Preparations[payload.ValidatorIndex] = true; if (context.State.HasFlag(ConsensusState.RequestSent) || context.State.HasFlag(ConsensusState.RequestReceived)) CheckPreparations(); From b9e38fca02c91533772bb3b01ad2e70403469d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Naz=C3=A1rio=20Coelho?= Date: Fri, 11 Jan 2019 09:24:40 -0200 Subject: [PATCH 4/7] RPC call gettransactionheight (#541) * getrawtransactionheight Nowadays two calls are need to get a transaction height, `getrawtransaction` with `verbose` and then use the `blockhash`. Other option is to use `confirmations`, but it can be misleading. * Minnor fix * Shargon's tip * modified --- neo/Network/RPC/RpcServer.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index 5bf4b21f8d..f02cbbe922 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -331,6 +331,13 @@ private JObject Process(string method, JArray _params) }) ?? new StorageItem(); return item.Value?.ToHexString(); } + case "gettransactionheight": + { + UInt256 hash = UInt256.Parse(_params[0].AsString()); + uint? height = Blockchain.Singleton.Store.GetTransactions().TryGet(hash)?.BlockIndex; + if (height.HasValue) return height.Value; + throw new RpcException(-100, "Unknown transaction"); + } case "gettxout": { UInt256 hash = UInt256.Parse(_params[0].AsString()); From 74882737439fea2a874a25acacd17863044adfc6 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 11 Jan 2019 16:59:22 +0100 Subject: [PATCH 5/7] Allow to use the wallet inside a RPC plugin (#536) --- neo/Network/RPC/RpcServer.cs | 85 ++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index f02cbbe922..0c090c770f 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -1,4 +1,14 @@ -using Akka.Actor; +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Net; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; +using Akka.Actor; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -16,30 +26,21 @@ using Neo.VM; using Neo.Wallets; using Neo.Wallets.NEP6; -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Net; -using System.Net.Security; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using System.Threading.Tasks; namespace Neo.Network.RPC { public sealed class RpcServer : IDisposable { - private readonly NeoSystem system; - private Wallet wallet; + public Wallet Wallet; + private IWebHost host; private Fixed8 maxGasInvoke; + private readonly NeoSystem system; public RpcServer(NeoSystem system, Wallet wallet = null, Fixed8 maxGasInvoke = default(Fixed8)) { this.system = system; - this.wallet = wallet; + this.Wallet = wallet; this.maxGasInvoke = maxGasInvoke; } @@ -86,7 +87,7 @@ private JObject GetInvokeResult(byte[] script) { json["stack"] = "error: recursive reference"; } - if (wallet != null) + if (Wallet != null) { InvocationTransaction tx = new InvocationTransaction { @@ -97,11 +98,11 @@ private JObject GetInvokeResult(byte[] script) tx.Gas -= Fixed8.FromDecimal(10); if (tx.Gas < Fixed8.Zero) tx.Gas = Fixed8.Zero; tx.Gas = tx.Gas.Ceiling(); - tx = wallet.MakeTransaction(tx); + tx = Wallet.MakeTransaction(tx); if (tx != null) { ContractParametersContext context = new ContractParametersContext(tx); - wallet.Sign(context); + Wallet.Sign(context); if (context.Completed) tx.Witnesses = context.GetWitnesses(); else @@ -133,7 +134,7 @@ private static JObject GetRelayResult(RelayResultReason reason) public void OpenWallet(Wallet wallet) { - this.wallet = wallet; + this.Wallet = wallet; } private JObject Process(string method, JArray _params) @@ -141,12 +142,12 @@ private JObject Process(string method, JArray _params) switch (method) { case "dumpprivkey": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied"); else { UInt160 scriptHash = _params[0].AsString().ToScriptHash(); - WalletAccount account = wallet.GetAccount(scriptHash); + WalletAccount account = Wallet.GetAccount(scriptHash); return account.GetKey().Export(); } case "getaccountstate": @@ -162,7 +163,7 @@ private JObject Process(string method, JArray _params) return asset?.ToJson() ?? throw new RpcException(-100, "Unknown asset"); } case "getbalance": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied."); else { @@ -170,10 +171,10 @@ private JObject Process(string method, JArray _params) switch (UIntBase.Parse(_params[0].AsString())) { case UInt160 asset_id_160: //NEP-5 balance - json["balance"] = wallet.GetAvailable(asset_id_160).ToString(); + json["balance"] = Wallet.GetAvailable(asset_id_160).ToString(); break; case UInt256 asset_id_256: //Global Assets balance - IEnumerable coins = wallet.GetCoins().Where(p => !p.State.HasFlag(CoinState.Spent) && p.Output.AssetId.Equals(asset_id_256)); + IEnumerable coins = Wallet.GetCoins().Where(p => !p.State.HasFlag(CoinState.Spent) && p.Output.AssetId.Equals(asset_id_256)); json["balance"] = coins.Sum(p => p.Output.Value).ToString(); json["confirmed"] = coins.Where(p => p.State.HasFlag(CoinState.Confirmed)).Sum(p => p.Output.Value).ToString(); break; @@ -267,12 +268,12 @@ private JObject Process(string method, JArray _params) return contract?.ToJson() ?? throw new RpcException(-100, "Unknown contract"); } case "getnewaddress": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied"); else { - WalletAccount account = wallet.CreateAccount(); - if (wallet is NEP6Wallet nep6) + WalletAccount account = Wallet.CreateAccount(); + if (Wallet is NEP6Wallet nep6) nep6.Save(); return account.Address; } @@ -366,10 +367,10 @@ private JObject Process(string method, JArray _params) return json; } case "getwalletheight": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied."); else - return (wallet.WalletHeight > 0) ? wallet.WalletHeight - 1 : 0; + return (Wallet.WalletHeight > 0) ? Wallet.WalletHeight - 1 : 0; case "invoke": { UInt160 script_hash = UInt160.Parse(_params[0].AsString()); @@ -399,10 +400,10 @@ private JObject Process(string method, JArray _params) return GetInvokeResult(script); } case "listaddress": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied."); else - return wallet.GetAccounts().Select(p => + return Wallet.GetAccounts().Select(p => { JObject account = new JObject(); account["address"] = p.Address; @@ -412,7 +413,7 @@ private JObject Process(string method, JArray _params) return account; }).ToArray(); case "sendfrom": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied"); else { @@ -427,7 +428,7 @@ private JObject Process(string method, JArray _params) if (fee < Fixed8.Zero) throw new RpcException(-32602, "Invalid params"); UInt160 change_address = _params.Count >= 6 ? _params[5].AsString().ToScriptHash() : null; - Transaction tx = wallet.MakeTransaction(null, new[] + Transaction tx = Wallet.MakeTransaction(null, new[] { new TransferOutput { @@ -439,11 +440,11 @@ private JObject Process(string method, JArray _params) if (tx == null) throw new RpcException(-300, "Insufficient funds"); ContractParametersContext context = new ContractParametersContext(tx); - wallet.Sign(context); + Wallet.Sign(context); if (context.Completed) { tx.Witnesses = context.GetWitnesses(); - wallet.ApplyTransaction(tx); + Wallet.ApplyTransaction(tx); system.LocalNode.Tell(new LocalNode.Relay { Inventory = tx }); return tx.ToJson(); } @@ -453,7 +454,7 @@ private JObject Process(string method, JArray _params) } } case "sendmany": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied"); else { @@ -478,15 +479,15 @@ private JObject Process(string method, JArray _params) if (fee < Fixed8.Zero) throw new RpcException(-32602, "Invalid params"); UInt160 change_address = _params.Count >= 3 ? _params[2].AsString().ToScriptHash() : null; - Transaction tx = wallet.MakeTransaction(null, outputs, change_address: change_address, fee: fee); + Transaction tx = Wallet.MakeTransaction(null, outputs, change_address: change_address, fee: fee); if (tx == null) throw new RpcException(-300, "Insufficient funds"); ContractParametersContext context = new ContractParametersContext(tx); - wallet.Sign(context); + Wallet.Sign(context); if (context.Completed) { tx.Witnesses = context.GetWitnesses(); - wallet.ApplyTransaction(tx); + Wallet.ApplyTransaction(tx); system.LocalNode.Tell(new LocalNode.Relay { Inventory = tx }); return tx.ToJson(); } @@ -502,7 +503,7 @@ private JObject Process(string method, JArray _params) return GetRelayResult(reason); } case "sendtoaddress": - if (wallet == null) + if (Wallet == null) throw new RpcException(-400, "Access denied"); else { @@ -516,7 +517,7 @@ private JObject Process(string method, JArray _params) if (fee < Fixed8.Zero) throw new RpcException(-32602, "Invalid params"); UInt160 change_address = _params.Count >= 5 ? _params[4].AsString().ToScriptHash() : null; - Transaction tx = wallet.MakeTransaction(null, new[] + Transaction tx = Wallet.MakeTransaction(null, new[] { new TransferOutput { @@ -528,11 +529,11 @@ private JObject Process(string method, JArray _params) if (tx == null) throw new RpcException(-300, "Insufficient funds"); ContractParametersContext context = new ContractParametersContext(tx); - wallet.Sign(context); + Wallet.Sign(context); if (context.Completed) { tx.Witnesses = context.GetWitnesses(); - wallet.ApplyTransaction(tx); + Wallet.ApplyTransaction(tx); system.LocalNode.Tell(new LocalNode.Relay { Inventory = tx }); return tx.ToJson(); } From 27913a88e3da1a53256d64f60ab12f0c180aed2e Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 12 Jan 2019 13:12:12 +0100 Subject: [PATCH 6/7] Clean code --- neo/Consensus/PrepareResponse.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/neo/Consensus/PrepareResponse.cs b/neo/Consensus/PrepareResponse.cs index 91a6b24b92..2f13617feb 100644 --- a/neo/Consensus/PrepareResponse.cs +++ b/neo/Consensus/PrepareResponse.cs @@ -2,9 +2,6 @@ { internal class PrepareResponse : ConsensusMessage { - public PrepareResponse() - : base(ConsensusMessageType.PrepareResponse) - { - } + public PrepareResponse() : base(ConsensusMessageType.PrepareResponse) { } } } From c2a4ce5e5c43f00d347d8aec2cd964de9cc3ac68 Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 12 Jan 2019 13:12:53 +0100 Subject: [PATCH 7/7] Clean code --- neo/Consensus/Commit.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/neo/Consensus/Commit.cs b/neo/Consensus/Commit.cs index cdd45d24ef..cac24aff6f 100644 --- a/neo/Consensus/Commit.cs +++ b/neo/Consensus/Commit.cs @@ -8,10 +8,7 @@ internal class Commit : ConsensusMessage public override int Size => base.Size + Signature.Length; - public Commit() - : base(ConsensusMessageType.Commit) - { - } + public Commit() : base(ConsensusMessageType.Commit) { } public override void Deserialize(BinaryReader reader) {