Skip to content

Commit a13bb85

Browse files
devhawkHarry
andauthored
use messagepack for RocksDbCacheClient.GetState (#48)
* dotnet format * rework GetState * update changelog * update changelog Co-authored-by: Harry <[email protected]>
1 parent 77eb69f commit a13bb85

9 files changed

+37
-44
lines changed

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ will not have contiguous patch numbers. Initial major and minor releases will be
1313
in this file without a patch number. Patch version will be included for bug fixes, but
1414
may not exactly match a publicly released version.
1515

16-
## [3.0.10] - 2021-10-11
16+
## [3.0.12] - 2021-10-12
1717

1818
Thanks to @merl111 for his contribution (#41) in this release
1919

2020
### Added
2121

2222
* static function to create a dummy block for testing (#41)
23-
* StateServiceStore (#45)
23+
* StateServiceStore (#45, #46, #48)
2424

2525
### Changes
2626

27-
* Update Neo dependency to 3.0.3
2827
* Persistence Refactoring (#44)
28+
* Update dependencies (#47)
29+
* MessagePack to 2.3.85
30+
* Neo to 3.0.3
31+
* OneOf to 3.0.201
32+
* rocksdb to 6.22.1.20635
33+
* System.IO.Abstractions to 13.2.47
2934

3035
## [3.0.3] - 2021-08-06
3136

src/bctklib/ContractParameterParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ internal ContractParameter ParseStringParameter(string value)
177177
if (value.StartsWith("file://"))
178178
{
179179
var file = fileSystem.NormalizePath(value[7..]);
180-
file = fileSystem.Path.IsPathFullyQualified(file)
180+
file = fileSystem.Path.IsPathFullyQualified(file)
181181
? file
182182
: fileSystem.Path.GetFullPath(file, fileSystem.Directory.GetCurrentDirectory());
183183

src/bctklib/persistence/CheckpointStorageProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Dispose()
3636
public IStore GetStore(string? storeName)
3737
{
3838
if (storeName == null) return defaultStore.Value;
39-
return ImmutableInterlocked.GetOrAdd(ref stores, storeName,
39+
return ImmutableInterlocked.GetOrAdd(ref stores, storeName,
4040
key => new MemoryTrackingStore(GetStorageProviderStore(key)));
4141
}
4242

@@ -47,7 +47,7 @@ IReadOnlyStore GetStorageProviderStore(string? path)
4747
{
4848
roStore = storageProvider?.GetStore(path);
4949
}
50-
catch {}
50+
catch { }
5151

5252
return roStore ?? NullStore.Instance;
5353
}

src/bctklib/persistence/MemoryTrackingStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void AtomicUpdate(ref TrackingMap trackingMap, byte[]? key, OneOf<byte[]?
4848
{
4949
key = key == null ? Array.Empty<byte>() : key.AsSpan().ToArray();
5050
var _value = value.Match<OneOf<ReadOnlyMemory<byte>, None>>(
51-
v => v == null ? default(ReadOnlyMemory<byte>) : v.AsSpan().ToArray(),
51+
v => v == null ? default(ReadOnlyMemory<byte>) : v.AsSpan().ToArray(),
5252
n => n);
5353

5454
var priorCollection = Volatile.Read(ref trackingMap);

src/bctklib/persistence/ReadOnlyMemoryComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Neo.BlockchainToolkit.Persistence
55
{
6-
class ReadOnlyMemoryComparer : IEqualityComparer<ReadOnlyMemory<byte>>, IComparer<ReadOnlyMemory<byte>>
6+
class ReadOnlyMemoryComparer : IEqualityComparer<ReadOnlyMemory<byte>>, IComparer<ReadOnlyMemory<byte>>
77
{
88
public static ReadOnlyMemoryComparer Default { get; } = new ReadOnlyMemoryComparer(false);
99
public static ReadOnlyMemoryComparer Reverse { get; } = new ReadOnlyMemoryComparer(true);

src/bctklib/persistence/RocksDbStorageProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public void Dispose()
3838

3939
public IStore GetStore(string? path)
4040
{
41-
return TryGetStore(path, out var store)
42-
? store
41+
return TryGetStore(path, out var store)
42+
? store
4343
: throw new InvalidOperationException("invalid store path");
4444
}
4545

src/bctklib/persistence/StateServiceStore.RocksDbCacheClient.cs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Buffers;
33
using System.Buffers.Binary;
4-
using System.Diagnostics;
54
using System.IO;
65
using System.Text;
6+
using MessagePack;
7+
using MessagePack.Formatters;
78
using Neo.IO.Json;
89
using Neo.Network.RPC;
910
using Neo.Network.RPC.Models;
@@ -75,45 +76,32 @@ public UInt256 GetBlockHash(uint index)
7576
return UInt256.Parse(json.AsString());
7677
}
7778

79+
static readonly IMessagePackFormatter<byte[]?> byteArrayFormatter =
80+
MessagePackSerializerOptions.Standard.Resolver.GetFormatter<byte[]?>();
81+
7882
public byte[]? GetState(UInt256 rootHash, UInt160 scriptHash, ReadOnlyMemory<byte> key)
7983
{
8084
var familyName = $"{nameof(GetState)}{rootHash}{scriptHash}";
8185
var family = db.GetOrCreateColumnFamily(familyName);
82-
var value = db.Get(key.Span, family);
86+
var cachedState = db.Get(key.Span, family);
8387

84-
JObject jsonValue;
85-
if (value != null)
88+
if (cachedState != null)
8689
{
87-
jsonValue = JObject.Parse(value);
90+
var reader = new MessagePackReader(cachedState);
91+
return byteArrayFormatter.Deserialize(ref reader, MessagePackSerializerOptions.Standard);
8892
}
8993
else
9094
{
91-
try
92-
{
93-
jsonValue = rpcClient.RpcSend("getstate",
94-
rootHash.ToString(),
95-
scriptHash.ToString(),
96-
Convert.ToBase64String(key.Span));
97-
}
98-
catch (RpcException ex)
99-
{
100-
if (ex.HResult == RpcClientExtensions.COR_E_KEYNOTFOUND)
101-
{
102-
jsonValue = JObject.Null;
103-
}
104-
else
105-
{
106-
throw;
107-
}
108-
}
109-
var jsonBytes = jsonValue == null
110-
? Neo.Utility.StrictUTF8.GetBytes("null")
111-
: jsonValue.ToByteArray(false);
112-
db.Put(key.Span, jsonBytes, family);
113-
}
95+
var state = rpcClient.GetState(rootHash, scriptHash, key.Span);
96+
97+
var buffer = new ArrayBufferWriter<byte>();
98+
var writer = new MessagePackWriter(buffer);
99+
writer.Write(state);
100+
writer.Flush();
101+
db.Put(key.Span, buffer.WrittenSpan, family);
114102

115-
return jsonValue == null || jsonValue == JObject.Null ? null
116-
: Convert.FromBase64String(jsonValue.AsString());
103+
return state;
104+
}
117105
}
118106

119107
public RpcStateRoot GetStateRoot(uint index)

src/bctklib/smart-contract/TestApplicationEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ public static Transaction CreateTestTransaction(UInt160 signerAccount, WitnessSc
8282
Witnesses = Array.Empty<Witness>(),
8383
};
8484

85-
public TestApplicationEngine(DataCache snapshot, ProtocolSettings settings)
85+
public TestApplicationEngine(DataCache snapshot, ProtocolSettings settings)
8686
: this(TriggerType.Application, null, snapshot, null, settings, ApplicationEngine.TestModeGas, null)
8787
{
8888
}
8989

90-
public TestApplicationEngine(DataCache snapshot, ProtocolSettings settings, UInt160 signer, WitnessScope witnessScope = WitnessScope.CalledByEntry)
90+
public TestApplicationEngine(DataCache snapshot, ProtocolSettings settings, UInt160 signer, WitnessScope witnessScope = WitnessScope.CalledByEntry)
9191
: this(TriggerType.Application, CreateTestTransaction(signer, witnessScope), snapshot, null, settings, ApplicationEngine.TestModeGas, null)
9292
{
9393
}
9494

95-
public TestApplicationEngine(DataCache snapshot, ProtocolSettings settings, Transaction transaction)
95+
public TestApplicationEngine(DataCache snapshot, ProtocolSettings settings, Transaction transaction)
9696
: this(TriggerType.Application, transaction, snapshot, null, settings, ApplicationEngine.TestModeGas, null)
9797
{
9898
}

src/bctklib/smart-contract/TraceApplicationEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override void Dispose()
3434

3535
private string GetContractName(UInt160 scriptId)
3636
{
37-
return ImmutableInterlocked.GetOrAdd(ref contractNameMap, scriptId,
37+
return ImmutableInterlocked.GetOrAdd(ref contractNameMap, scriptId,
3838
k => NativeContract.ContractManagement.GetContract(Snapshot, k)?.Manifest.Name ?? string.Empty);
3939
}
4040

0 commit comments

Comments
 (0)