Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[StateService] get historical state #638

Merged
merged 23 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/StateService/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# StateService

## RPC API

### GetStateRoot
#### Params
|Name|Type|Summary|Required|
|-|-|-|-|
|Index|uint|index|true|
#### Result
StateRoot Object
|Name|Type|Summary|
|-|-|-|
|version|number|version|
|index|number|index|
|roothash|string|version|
|witness|Object|witness from validators|

### GetProof
#### Params
|Name|Type|Summary|Required|
|-|-|-|-|
|RootHash|UInt256|state root|true|
|ScriptHash|UInt160|contract script hash|true|
|Key|base64 string|key|true|
#### Result
Proof in base64 string

### VerifyProof
#### Params
|Name|Type|Summary|
|-|-|-|
|RootHash|UInt256|state root|true|
|Proof|base64 string|proof|true|
#### Result
Value in base64 string

### GetStateheight
#### Result
|Name|Type|Summary|
|-|-|-|
|localrootindex|number|root hash index calculated locally|
|validatedrootindex|number|root hash index verified by validators|

### GetState
ZhangTao1596 marked this conversation as resolved.
Show resolved Hide resolved
#### Params
|Name|Type|Summary|Required|
|-|-|-|-|
|RootHash|UInt256|specify state|true|
|ScriptHash|UInt160|contract script hash|true|
|Key|base64 string|key|true|
#### Result
Value in base64 string or `null`

### FindState
#### Params
|Name|Type|Summary|Required|
|-|-|-|-|
|RootHash|UInt256|specify state|true|
|ScriptHash|UInt160|contract script hash|true|
|Prefix|base64 string|key prefix|true|
|PageNumber|number|page number, default `0`|optional|
#### Result
key-value results in array
4 changes: 4 additions & 0 deletions src/StateService/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ internal class Settings
public bool FullState { get; }
public uint Network { get; }
public bool AutoVerify { get; }
public int MaxFindResultItems { get; }
public int MaxPageNumber { get; }

public static Settings Default { get; private set; }

Expand All @@ -17,6 +19,8 @@ private Settings(IConfigurationSection section)
FullState = section.GetValue("FullState", false);
Network = section.GetValue("Network", 5195086u);
AutoVerify = section.GetValue("AutoVerify", false);
MaxFindResultItems = section.GetValue("MaxFindResultItems", 100);
MaxPageNumber = section.GetValue("MaxPageNumber", 100);
}

public static void Load(IConfigurationSection section)
Expand Down
64 changes: 63 additions & 1 deletion src/StateService/StatePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ private string GetProof(UInt256 root_hash, UInt160 script_hash, byte[] key)
Id = contract.Id,
Key = key,
};
HashSet<byte[]> proof = StateStore.Singleton.GetProof(root_hash, skey);
using ISnapshot store = StateStore.Singleton.GetStoreSnapshot();
var trie = new MPTTrie<StorageKey, StorageItem>(store, root_hash);
var proof = trie.GetProof(skey);
if (proof is null) throw new RpcException(-100, "Unknown value");

using MemoryStream ms = new MemoryStream();
Expand Down Expand Up @@ -239,5 +241,65 @@ public JObject GetStateHeight(JArray _params)
json["validatedrootindex"] = StateStore.Singleton.ValidatedRootIndex;
return json;
}

private ContractState GetHistoricalContractState(MPTTrie<StorageKey, StorageItem> trie, UInt160 script_hash)
{
const byte prefix = 8;
StorageKey skey = new KeyBuilder(NativeContract.ContractManagement.Id, prefix).Add(script_hash);
return trie[skey]?.GetInteroperable<ContractState>();
}

private void PrepareStateParams(JArray _params, out MPTTrie<StorageKey, StorageItem> trie, out StorageKey skey)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
var root_hash = UInt256.Parse(_params[0].AsString());
var script_hash = UInt160.Parse(_params[1].AsString());
var prefix = Convert.FromBase64String(_params[2].AsString());
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (!Settings.Default.FullState && StateStore.Singleton.CurrentLocalRootHash != root_hash)
throw new RpcException(-100, "Old state not supported");
shargon marked this conversation as resolved.
Show resolved Hide resolved
using var store = StateStore.Singleton.GetStoreSnapshot();
trie = new MPTTrie<StorageKey, StorageItem>(store, root_hash);

var contract = GetHistoricalContractState(trie, script_hash);
if (contract is null) throw new RpcException(-100, "Unknown contract");
skey = new()
{
Id = contract.Id,
Key = prefix,
};
}

[RpcMethod]
public JObject FindState(JArray _params)
{
PrepareStateParams(_params, out var trie, out var skey);
int pageNumber = 0;
if (3 < _params.Count)
pageNumber = int.Parse(_params[3].AsString());
if (Settings.Default.MaxPageNumber <= pageNumber)
ZhangTao1596 marked this conversation as resolved.
Show resolved Hide resolved
throw new RpcException(-100, "Page number exceed limit");
var start = Settings.Default.MaxFindResultItems * pageNumber;
JArray array = new();
int i = 0;
foreach (var (ikey, ivalue) in trie.Find(skey.ToArray()))
{
if (start + Settings.Default.MaxFindResultItems <= i) break;
if (start <= i)
{
JObject j = new();
j["key"] = Convert.ToBase64String(ikey.Key);
j["value"] = Convert.ToBase64String(ivalue.Value);
array.Add(j);
}
i++;
};
return array;
shargon marked this conversation as resolved.
Show resolved Hide resolved
}

[RpcMethod]
public JObject GetState(JArray _params)
{
PrepareStateParams(_params, out var trie, out var skey);
return trie[skey]?.Value is null ? null : Convert.ToBase64String(trie[skey].Value);
}
}
}
4 changes: 3 additions & 1 deletion src/StateService/StateService/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"Path": "Data_MPT_{0}",
"FullState": false,
"Network": 860833102,
"AutoVerify": false
"AutoVerify": false,
"MaxFindResultItems": 100,
"MaxPageNumber": 100
ZhangTao1596 marked this conversation as resolved.
Show resolved Hide resolved
},
"Dependency": [
"RpcServer"
Expand Down
6 changes: 2 additions & 4 deletions src/StateService/Storage/StateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ public StateSnapshot GetSnapshot()
return new StateSnapshot(store);
}

public HashSet<byte[]> GetProof(UInt256 root, StorageKey skey)
public ISnapshot GetStoreSnapshot()
{
using ISnapshot snapshot = store.GetSnapshot();
var trie = new MPTTrie<StorageKey, StorageItem>(snapshot, root);
return trie.GetProof(skey);
return store.GetSnapshot();
}

protected override void OnReceive(object message)
Expand Down