From ee20bc944ca2c8d6512cfbd5b50dafa94ef12ddb Mon Sep 17 00:00:00 2001 From: Charis Date: Tue, 16 Jul 2019 12:49:12 +0800 Subject: [PATCH 01/10] add internal to DB and WriteBatch --- neo/IO/Data/LevelDB/DB.cs | 16 ++++++++-------- neo/IO/Data/LevelDB/WriteBatch.cs | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/neo/IO/Data/LevelDB/DB.cs b/neo/IO/Data/LevelDB/DB.cs index 559a109594..5f4a9f359a 100644 --- a/neo/IO/Data/LevelDB/DB.cs +++ b/neo/IO/Data/LevelDB/DB.cs @@ -16,7 +16,7 @@ private DB(IntPtr handle) this.handle = handle; } - public void Dispose() + public virtual void Dispose() { if (handle != IntPtr.Zero) { @@ -25,14 +25,14 @@ public void Dispose() } } - public void Delete(WriteOptions options, Slice key) + public virtual void Delete(WriteOptions options, Slice key) { IntPtr error; Native.leveldb_delete(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out error); NativeHelper.CheckError(error); } - public Slice Get(ReadOptions options, Slice key) + public virtual Slice Get(ReadOptions options, Slice key) { UIntPtr length; IntPtr error; @@ -50,12 +50,12 @@ public Slice Get(ReadOptions options, Slice key) } } - public Snapshot GetSnapshot() + public virtual Snapshot GetSnapshot() { return new Snapshot(handle); } - public Iterator NewIterator(ReadOptions options) + public virtual Iterator NewIterator(ReadOptions options) { return new Iterator(Native.leveldb_create_iterator(handle, options.handle)); } @@ -73,14 +73,14 @@ public static DB Open(string name, Options options) return new DB(handle); } - public void Put(WriteOptions options, Slice key, Slice value) + public virtual void Put(WriteOptions options, Slice key, Slice value) { IntPtr error; Native.leveldb_put(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, value.buffer, (UIntPtr)value.buffer.Length, out error); NativeHelper.CheckError(error); } - public bool TryGet(ReadOptions options, Slice key, out Slice value) + public virtual bool TryGet(ReadOptions options, Slice key, out Slice value) { UIntPtr length; IntPtr error; @@ -101,7 +101,7 @@ public bool TryGet(ReadOptions options, Slice key, out Slice value) return true; } - public void Write(WriteOptions options, WriteBatch write_batch) + public virtual void Write(WriteOptions options, WriteBatch write_batch) { // There's a bug in .Net Core. // When calling DB.Write(), it will throw LevelDBException sometimes. diff --git a/neo/IO/Data/LevelDB/WriteBatch.cs b/neo/IO/Data/LevelDB/WriteBatch.cs index eaa3e08bd6..29d04643a1 100644 --- a/neo/IO/Data/LevelDB/WriteBatch.cs +++ b/neo/IO/Data/LevelDB/WriteBatch.cs @@ -11,17 +11,17 @@ public class WriteBatch Native.leveldb_writebatch_destroy(handle); } - public void Clear() + public virtual void Clear() { Native.leveldb_writebatch_clear(handle); } - public void Delete(Slice key) + public virtual void Delete(Slice key) { Native.leveldb_writebatch_delete(handle, key.buffer, (UIntPtr)key.buffer.Length); } - public void Put(Slice key, Slice value) + public virtual void Put(Slice key, Slice value) { Native.leveldb_writebatch_put(handle, key.buffer, (UIntPtr)key.buffer.Length, value.buffer, (UIntPtr)value.buffer.Length); } From a4fa636f7a56721ff3cfd5c5dffe56cb0123ab58 Mon Sep 17 00:00:00 2001 From: luchuan Date: Thu, 26 Sep 2019 10:27:06 +0800 Subject: [PATCH 02/10] add vmstate field in rpc getrawtransaction result --- neo.UnitTests/Network/RPC/UT_RpcClient.cs | 2 ++ neo/Network/RPC/Models/RpcTransaction.cs | 4 ++++ neo/Network/RPC/RpcServer.cs | 8 +++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/neo.UnitTests/Network/RPC/UT_RpcClient.cs b/neo.UnitTests/Network/RPC/UT_RpcClient.cs index 0875a5a793..deaddf1bdf 100644 --- a/neo.UnitTests/Network/RPC/UT_RpcClient.cs +++ b/neo.UnitTests/Network/RPC/UT_RpcClient.cs @@ -355,11 +355,13 @@ public void TestGetRawTransaction() json["blockhash"] = UInt256.Zero.ToString(); json["confirmations"] = 100; json["blocktime"] = 10; + json["vmState"] = "HALT"; MockResponse(response.ToString()); result = rpc.GetRawTransaction("0x9786cce0dddb524c40ddbdd5e31a41ed1f6b5c8a683c122f627ca4a007a7cf4e"); Assert.AreEqual(transaction.Hash, result.Transaction.Hash); Assert.AreEqual(100, result.Confirmations); + Assert.AreEqual("HALT", result.VMState); Assert.AreEqual(json.ToString(), result.ToJson().ToString()); } diff --git a/neo/Network/RPC/Models/RpcTransaction.cs b/neo/Network/RPC/Models/RpcTransaction.cs index f41c04710a..1ec5e69b00 100644 --- a/neo/Network/RPC/Models/RpcTransaction.cs +++ b/neo/Network/RPC/Models/RpcTransaction.cs @@ -13,6 +13,8 @@ public class RpcTransaction public uint? BlockTime { get; set; } + public string VMState { get; set; } + public JObject ToJson() { JObject json = Transaction.ToJson(); @@ -21,6 +23,7 @@ public JObject ToJson() json["blockhash"] = BlockHash.ToString(); json["confirmations"] = Confirmations; json["blocktime"] = BlockTime; + json["vmState"] = VMState; } return json; } @@ -34,6 +37,7 @@ public static RpcTransaction FromJson(JObject json) transaction.BlockHash = UInt256.Parse(json["blockhash"].AsString()); transaction.Confirmations = (int)json["confirmations"].AsNumber(); transaction.BlockTime = (uint)json["blocktime"].AsNumber(); + transaction.VMState = json["vmState"].AsString(); } return transaction; } diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index c47e6644e7..8f0d506adb 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -564,13 +564,15 @@ private JObject GetRawTransaction(UInt256 hash, bool verbose) if (verbose) { JObject json = tx.ToJson(); - uint? height = Blockchain.Singleton.Store.GetTransactions().TryGet(hash)?.BlockIndex; - if (height != null) + TransactionState txState = Blockchain.Singleton.Store.GetTransactions().TryGet(hash); + if(txState != null) { - Header header = Blockchain.Singleton.Store.GetHeader((uint)height); + uint height = txState.BlockIndex; + Header header = Blockchain.Singleton.Store.GetHeader(height); json["blockhash"] = header.Hash.ToString(); json["confirmations"] = Blockchain.Singleton.Height - header.Index + 1; json["blocktime"] = header.Timestamp; + json["vmState"] = Enum.GetName(typeof(VMState), txState.VMState); } return json; } From d23109c1e80be942a54f41a5d7d5be2f15c65e00 Mon Sep 17 00:00:00 2001 From: luchuan Date: Thu, 26 Sep 2019 10:30:22 +0800 Subject: [PATCH 03/10] reset db.cs writebatch.cs --- neo/IO/Data/LevelDB/DB.cs | 16 ++++++++-------- neo/IO/Data/LevelDB/WriteBatch.cs | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/neo/IO/Data/LevelDB/DB.cs b/neo/IO/Data/LevelDB/DB.cs index b446f0eb56..04e8f50e8f 100644 --- a/neo/IO/Data/LevelDB/DB.cs +++ b/neo/IO/Data/LevelDB/DB.cs @@ -16,7 +16,7 @@ private DB(IntPtr handle) this.handle = handle; } - public virtual void Dispose() + public void Dispose() { if (handle != IntPtr.Zero) { @@ -25,14 +25,14 @@ public virtual void Dispose() } } - public virtual void Delete(WriteOptions options, Slice key) + public void Delete(WriteOptions options, Slice key) { IntPtr error; Native.leveldb_delete(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, out error); NativeHelper.CheckError(error); } - public virtual Slice Get(ReadOptions options, Slice key) + public Slice Get(ReadOptions options, Slice key) { UIntPtr length; IntPtr error; @@ -50,12 +50,12 @@ public virtual Slice Get(ReadOptions options, Slice key) } } - public virtual Snapshot GetSnapshot() + public Snapshot GetSnapshot() { return new Snapshot(handle); } - public virtual Iterator NewIterator(ReadOptions options) + public Iterator NewIterator(ReadOptions options) { return new Iterator(Native.leveldb_create_iterator(handle, options.handle)); } @@ -73,14 +73,14 @@ public static DB Open(string name, Options options) return new DB(handle); } - public virtual void Put(WriteOptions options, Slice key, Slice value) + public void Put(WriteOptions options, Slice key, Slice value) { IntPtr error; Native.leveldb_put(handle, options.handle, key.buffer, (UIntPtr)key.buffer.Length, value.buffer, (UIntPtr)value.buffer.Length, out error); NativeHelper.CheckError(error); } - public virtual bool TryGet(ReadOptions options, Slice key, out Slice value) + public bool TryGet(ReadOptions options, Slice key, out Slice value) { UIntPtr length; IntPtr error; @@ -101,7 +101,7 @@ public virtual bool TryGet(ReadOptions options, Slice key, out Slice value) return true; } - public virtual void Write(WriteOptions options, WriteBatch write_batch) + public void Write(WriteOptions options, WriteBatch write_batch) { // There's a bug in .Net Core. // When calling DB.Write(), it will throw LevelDBException sometimes. diff --git a/neo/IO/Data/LevelDB/WriteBatch.cs b/neo/IO/Data/LevelDB/WriteBatch.cs index 03dcab1f3d..b3a9782108 100644 --- a/neo/IO/Data/LevelDB/WriteBatch.cs +++ b/neo/IO/Data/LevelDB/WriteBatch.cs @@ -11,17 +11,17 @@ public class WriteBatch Native.leveldb_writebatch_destroy(handle); } - public virtual void Clear() + public void Clear() { Native.leveldb_writebatch_clear(handle); } - public virtual void Delete(Slice key) + public void Delete(Slice key) { Native.leveldb_writebatch_delete(handle, key.buffer, (UIntPtr)key.buffer.Length); } - public virtual void Put(Slice key, Slice value) + public void Put(Slice key, Slice value) { Native.leveldb_writebatch_put(handle, key.buffer, (UIntPtr)key.buffer.Length, value.buffer, (UIntPtr)value.buffer.Length); } From fbac1a2fecf47e96bc573a7f1960cbb3c77a324d Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 26 Sep 2019 10:02:35 +0200 Subject: [PATCH 04/10] Optimize --- neo/Network/RPC/RpcServer.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index 8f0d506adb..1b078993dc 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -567,8 +567,7 @@ private JObject GetRawTransaction(UInt256 hash, bool verbose) TransactionState txState = Blockchain.Singleton.Store.GetTransactions().TryGet(hash); if(txState != null) { - uint height = txState.BlockIndex; - Header header = Blockchain.Singleton.Store.GetHeader(height); + Header header = Blockchain.Singleton.Store.GetHeader(txState.BlockIndex); json["blockhash"] = header.Hash.ToString(); json["confirmations"] = Blockchain.Singleton.Height - header.Index + 1; json["blocktime"] = header.Timestamp; From 4bb71a992575e9482400fbf615a7914691fa89a9 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 26 Sep 2019 10:03:19 +0200 Subject: [PATCH 05/10] Update RpcServer.cs --- neo/Network/RPC/RpcServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index 1b078993dc..1709cfdc97 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -571,7 +571,7 @@ private JObject GetRawTransaction(UInt256 hash, bool verbose) json["blockhash"] = header.Hash.ToString(); json["confirmations"] = Blockchain.Singleton.Height - header.Index + 1; json["blocktime"] = header.Timestamp; - json["vmState"] = Enum.GetName(typeof(VMState), txState.VMState); + json["vmState"] = txState.VMState.ToString(); } return json; } From b8c62491a978a66a3bfa2837ec63489da34cac44 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 26 Sep 2019 10:12:53 +0200 Subject: [PATCH 06/10] Format --- neo/Network/RPC/RpcServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index 1709cfdc97..ea72c72c76 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -565,7 +565,7 @@ private JObject GetRawTransaction(UInt256 hash, bool verbose) { JObject json = tx.ToJson(); TransactionState txState = Blockchain.Singleton.Store.GetTransactions().TryGet(hash); - if(txState != null) + if (txState != null) { Header header = Blockchain.Singleton.Store.GetHeader(txState.BlockIndex); json["blockhash"] = header.Hash.ToString(); From 5453cc1da8a81c0184e9e4003e6647fa2868c0d5 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 26 Sep 2019 20:10:45 +0800 Subject: [PATCH 07/10] Update RpcServer.cs --- neo/Network/RPC/RpcServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Network/RPC/RpcServer.cs b/neo/Network/RPC/RpcServer.cs index ea72c72c76..7414d54d10 100644 --- a/neo/Network/RPC/RpcServer.cs +++ b/neo/Network/RPC/RpcServer.cs @@ -571,7 +571,7 @@ private JObject GetRawTransaction(UInt256 hash, bool verbose) json["blockhash"] = header.Hash.ToString(); json["confirmations"] = Blockchain.Singleton.Height - header.Index + 1; json["blocktime"] = header.Timestamp; - json["vmState"] = txState.VMState.ToString(); + json["vmState"] = txState.VMState; } return json; } From 40a36346b8109b05cf44aef6aca7bbea86f74014 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 26 Sep 2019 20:15:39 +0800 Subject: [PATCH 08/10] Update RpcTransaction.cs --- neo/Network/RPC/Models/RpcTransaction.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/neo/Network/RPC/Models/RpcTransaction.cs b/neo/Network/RPC/Models/RpcTransaction.cs index 1ec5e69b00..11a0c96476 100644 --- a/neo/Network/RPC/Models/RpcTransaction.cs +++ b/neo/Network/RPC/Models/RpcTransaction.cs @@ -1,5 +1,6 @@ using Neo.IO.Json; using Neo.Network.P2P.Payloads; +using Neo.VM; namespace Neo.Network.RPC.Models { @@ -13,7 +14,7 @@ public class RpcTransaction public uint? BlockTime { get; set; } - public string VMState { get; set; } + public VMState VMState { get; set; } public JObject ToJson() { @@ -37,7 +38,7 @@ public static RpcTransaction FromJson(JObject json) transaction.BlockHash = UInt256.Parse(json["blockhash"].AsString()); transaction.Confirmations = (int)json["confirmations"].AsNumber(); transaction.BlockTime = (uint)json["blocktime"].AsNumber(); - transaction.VMState = json["vmState"].AsString(); + transaction.VMState = json["vmState"].TryGetEnum(); } return transaction; } From 7db1c475ea84e3325efd08c95ff351849c0bdffc Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 26 Sep 2019 20:17:10 +0800 Subject: [PATCH 09/10] Update UT_RpcClient.cs --- neo.UnitTests/Network/RPC/UT_RpcClient.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo.UnitTests/Network/RPC/UT_RpcClient.cs b/neo.UnitTests/Network/RPC/UT_RpcClient.cs index deaddf1bdf..7294d3ccb3 100644 --- a/neo.UnitTests/Network/RPC/UT_RpcClient.cs +++ b/neo.UnitTests/Network/RPC/UT_RpcClient.cs @@ -355,13 +355,13 @@ public void TestGetRawTransaction() json["blockhash"] = UInt256.Zero.ToString(); json["confirmations"] = 100; json["blocktime"] = 10; - json["vmState"] = "HALT"; + json["vmState"] = VMState.HALT; MockResponse(response.ToString()); result = rpc.GetRawTransaction("0x9786cce0dddb524c40ddbdd5e31a41ed1f6b5c8a683c122f627ca4a007a7cf4e"); Assert.AreEqual(transaction.Hash, result.Transaction.Hash); Assert.AreEqual(100, result.Confirmations); - Assert.AreEqual("HALT", result.VMState); + Assert.AreEqual(VMState.HALT, result.VMState); Assert.AreEqual(json.ToString(), result.ToJson().ToString()); } From 278c566d6a2dbb05040ca7f046e692938270081c Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 26 Sep 2019 20:19:34 +0800 Subject: [PATCH 10/10] Fixes `RpcTransaction.VMState` --- neo/Network/RPC/Models/RpcTransaction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Network/RPC/Models/RpcTransaction.cs b/neo/Network/RPC/Models/RpcTransaction.cs index 11a0c96476..f96179c358 100644 --- a/neo/Network/RPC/Models/RpcTransaction.cs +++ b/neo/Network/RPC/Models/RpcTransaction.cs @@ -14,7 +14,7 @@ public class RpcTransaction public uint? BlockTime { get; set; } - public VMState VMState { get; set; } + public VMState? VMState { get; set; } public JObject ToJson() {