Skip to content

Commit 9e52310

Browse files
committed
New: block trees support within blockchain
Allows to store different blockTypes within a single blockchain
1 parent c25e837 commit 9e52310

File tree

4 files changed

+89
-34
lines changed

4 files changed

+89
-34
lines changed

source/jaindb/BlockChain.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,28 @@ public static string ByteArrayToString(byte[] input)
209209
return sb.ToString();
210210
}
211211

212-
public block GetLastBlock()
212+
public block GetLastBlock(string blockType = "")
213213
{
214-
return Chain.FirstOrDefault(t =>Chain.Count(q=> ByteArrayToString(q.previous_hash) == ByteArrayToString(t.hash)) == 0);
214+
if (string.IsNullOrEmpty(blockType))
215+
return Chain.FirstOrDefault(t => Chain.Count(q => ByteArrayToString(q.previous_hash) == ByteArrayToString(t.hash)) == 0);
216+
else
217+
{
218+
var oBlock = Chain.FirstOrDefault(t => Chain.Count(q => ByteArrayToString(q.previous_hash) == ByteArrayToString(t.hash)) == 0 && (t.blocktype == blockType));
219+
220+
//return genesis block if no other block was found
221+
if (oBlock == null)
222+
return GetBlock(0);
223+
else
224+
return oBlock;
225+
}
215226
}
216227

217-
public block GetBlock(int index)
228+
public block GetBlock(int index, string blockType = "")
218229
{
219-
return Chain.FirstOrDefault(t => t.index == index);
230+
if (string.IsNullOrEmpty(blockType))
231+
return Chain.FirstOrDefault(t => t.index == index);
232+
else
233+
return Chain.FirstOrDefault(t => t.index == index && t.blocktype == blockType);
220234
}
221235
}
222236

source/jaindb/Controllers/HomeController.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ public ActionResult get()
4343

4444
[HttpPost]
4545
[Route("upload/{Id}")]
46-
public string Upload(string JSON, string Id)
46+
public string Upload(string JSON, string Id, string blockType = "INV")
4747
{
4848
var oGet = new StreamReader(Request.Body, true).ReadToEndAsync();
4949

50-
return jDB.UploadFull(oGet.Result.ToString(), Id);
50+
return jDB.UploadFull(oGet.Result.ToString(), Id, blockType);
5151
}
5252

5353
[HttpPost]
5454
[Route("uploadxml/{Id}")]
55-
public string UploadXML(string XML, string Id)
55+
public string UploadXML(string XML, string Id, string blockType = "INV")
5656
{
5757
var oGet = new StreamReader(Request.Body, true).ReadToEndAsync();
5858

@@ -61,7 +61,7 @@ public string UploadXML(string XML, string Id)
6161
//Check if we have a value in sJSON
6262
if (!string.IsNullOrEmpty(sJSON))
6363
{
64-
return jDB.UploadFull(sJSON, Id);
64+
return jDB.UploadFull(sJSON, Id, blockType);
6565
}
6666

6767
return "";
@@ -156,7 +156,7 @@ public string GetPS()
156156
[HttpGet]
157157
[Authorize]
158158
[Route("full")]
159-
public JObject Full()
159+
public JObject Full(string blockType = "INV")
160160
{
161161
string sPath = ((Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest)this.Request).Path;
162162
string sQuery = ((Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest)this.Request).QueryString.ToString();
@@ -170,13 +170,13 @@ public JObject Full()
170170
if (!int.TryParse(query.FirstOrDefault(t => t.Key.ToLower() == "index").Value, out int index))
171171
index = -1;
172172

173-
return jDB.GetFull(sKey, index);
173+
return jDB.GetFull(sKey, index, blockType);
174174
}
175175

176176
[HttpGet]
177177
[Authorize]
178178
[Route("diff")]
179-
public JObject Diff()
179+
public JObject Diff(string blockType = "INV")
180180
{
181181
this.Url.ToString();
182182
string sPath = ((Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest)this.Request).Path;
@@ -204,7 +204,8 @@ public JObject Diff()
204204
if (!int.TryParse(query.FirstOrDefault(t => t.Key.ToLower() == "mode").Value, out int mode))
205205
mode = 0;
206206

207-
return jDB.GetDiff(sKey, index, mode, rindex);
207+
208+
return jDB.GetDiff(sKey, index, mode, rindex, blockType);
208209
}
209210
return null;
210211
}

source/jaindb/jaindb.cs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,14 @@ public static Blockchain GetChain(string DeviceID)
716716
return oChain;
717717
}
718718

719-
public static string UploadFull(string JSON, string DeviceID)
719+
public static string UploadFull(string JSON, string DeviceID, string blockType = "")
720720
{
721721
if (ReadOnly)
722722
return "";
723723

724+
if (string.IsNullOrEmpty(blockType))
725+
blockType = BlockType;
726+
724727
try
725728
{
726729
JObject oObj = JObject.Parse(JSON);
@@ -894,6 +897,8 @@ public static string UploadFull(string JSON, string DeviceID)
894897
oStatic.AddFirst(new JProperty("_date", new DateTime(oNew.timestamp).ToUniversalTime()));
895898
if (oStatic["_index"] == null)
896899
oStatic.AddFirst(new JProperty("_index", oNew.index));
900+
if (oStatic["_type"] == null)
901+
oStatic.AddFirst(new JProperty("_type", blockType));
897902
if (oStatic["#id"] == null)
898903
oStatic.AddFirst(new JProperty("#id", DeviceID));
899904

@@ -903,12 +908,19 @@ public static string UploadFull(string JSON, string DeviceID)
903908
jTemp.AddFirst(new JProperty("_hash", oNew.data));
904909
if (jTemp["_date"] == null)
905910
jTemp.AddFirst(new JProperty("_date", new DateTime(oNew.timestamp).ToUniversalTime()));
911+
if (jTemp["_type"] == null)
912+
jTemp.AddFirst(new JProperty("_type", blockType));
906913
if (jTemp["#id"] == null)
907914
jTemp.AddFirst(new JProperty("#id", DeviceID));
908915

909916
//JSort(jTemp);
910917
if (!UseCosmosDB)
911-
WriteHashAsync(DeviceID, jTemp.ToString(Formatting.None), "_Full");
918+
{
919+
if (blockType == BlockType)
920+
WriteHashAsync(DeviceID, jTemp.ToString(Formatting.None), "_Full");
921+
else
922+
WriteHashAsync(DeviceID + "_" + blockType, jTemp.ToString(Formatting.None), "_Full");
923+
}
912924
else
913925
{
914926
//No need to save cached document when using CosmosDB
@@ -923,7 +935,12 @@ public static string UploadFull(string JSON, string DeviceID)
923935

924936
//JSort(oStatic);
925937
if (!UseCosmosDB)
926-
WriteHashAsync(sResult, oStatic.ToString(Newtonsoft.Json.Formatting.None), "Assets");
938+
{
939+
if (blockType == BlockType)
940+
WriteHashAsync(sResult, oStatic.ToString(Newtonsoft.Json.Formatting.None), "Assets");
941+
else
942+
WriteHashAsync(sResult + "_" + blockType, oStatic.ToString(Newtonsoft.Json.Formatting.None), "Assets");
943+
}
927944
else
928945
{
929946
//On CosmosDB, store full document as Asset
@@ -943,23 +960,36 @@ public static string UploadFull(string JSON, string DeviceID)
943960
return "";
944961
}
945962

946-
public static JObject GetFull(string DeviceID, int Index = -1)
963+
public static JObject GetFull(string DeviceID, int Index = -1, string blockType = "")
947964
{
948965
try
949966
{
950967
JObject oInv = new JObject();
951968

969+
if (string.IsNullOrEmpty(blockType))
970+
blockType = BlockType;
971+
952972
if (Index == -1)
953973
{
954-
string sFull = ReadHash(DeviceID, "_Full");
974+
string sFull = "";
975+
if (blockType == BlockType)
976+
sFull = ReadHash(DeviceID, "_Full");
977+
else
978+
sFull = ReadHash(DeviceID + "_" + blockType, "_Full");
979+
955980
if (!string.IsNullOrEmpty(sFull))
956981
{
957982
return JObject.Parse(sFull);
958983
}
959984
}
960985

961-
JObject oRaw = GetRawId(DeviceID, Index);
962-
string sData = ReadHash(oRaw["_hash"].ToString(), "Assets");
986+
JObject oRaw = GetRawId(DeviceID, Index, blockType);
987+
988+
string sData = "";
989+
if (blockType == BlockType)
990+
sData = ReadHash(oRaw["_hash"].ToString(), "_Assets");
991+
else
992+
sData = ReadHash(oRaw["_hash"].ToString() + "_" + blockType, "_Assets");
963993

964994
if (!UseCosmosDB)
965995
{
@@ -1141,10 +1171,13 @@ public static JObject GetFull(string DeviceID, int Index = -1)
11411171
return new JObject();
11421172
}
11431173

1144-
public static JObject GetRawId(string DeviceID, int Index = -1)
1174+
public static JObject GetRawId(string DeviceID, int Index = -1, string blockType = "")
11451175
{
11461176
JObject jResult = new JObject();
11471177

1178+
if (string.IsNullOrEmpty(blockType))
1179+
blockType = BlockType;
1180+
11481181
try
11491182
{
11501183
Blockchain oChain;
@@ -1154,13 +1187,13 @@ public static JObject GetRawId(string DeviceID, int Index = -1)
11541187
if (Index == -1)
11551188
{
11561189
oChain = GetChain(DeviceID);
1157-
lBlock = oChain.GetLastBlock();
1190+
lBlock = oChain.GetLastBlock(blockType);
11581191

11591192
}
11601193
else
11611194
{
11621195
oChain = GetChain(DeviceID);
1163-
lBlock = oChain.GetBlock(Index);
1196+
lBlock = oChain.GetBlock(Index, blockType);
11641197
}
11651198

11661199

@@ -1177,15 +1210,19 @@ public static JObject GetRawId(string DeviceID, int Index = -1)
11771210
return jResult;
11781211
}
11791212

1180-
public static JObject GetHistory(string DeviceID)
1213+
public static JObject GetHistory(string DeviceID, string blockType = "")
11811214
{
11821215
JObject jResult = new JObject();
1216+
1217+
if (string.IsNullOrEmpty(blockType))
1218+
blockType = BlockType;
1219+
11831220
try
11841221
{
11851222

11861223
string sChain = ReadHash(DeviceID, "Chain");
11871224
var oChain = JsonConvert.DeserializeObject<Blockchain>(sChain);
1188-
foreach (block oBlock in oChain.Chain.Where(t => t.blocktype != "root"))
1225+
foreach (block oBlock in oChain.Chain.Where(t => t.blocktype == blockType))
11891226
{
11901227
try
11911228
{
@@ -1272,8 +1309,11 @@ public static JObject GetRaw(string RawID, string path = "")
12721309
return jResult;
12731310
}
12741311

1275-
public static JObject GetDiff(string DeviceId, int IndexLeft, int mode = -1, int IndexRight = -1)
1312+
public static JObject GetDiff(string DeviceId, int IndexLeft, int mode = -1, int IndexRight = -1, string blockType = "")
12761313
{
1314+
if (string.IsNullOrEmpty(blockType))
1315+
blockType = BlockType;
1316+
12771317
try
12781318
{
12791319
var right = GetFull(DeviceId, IndexRight);

source/jaindb/jaindb.csproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<RepositoryUrl>https://github.com/rzander/jaindb</RepositoryUrl>
1414
<PackageTags>blockchain json</PackageTags>
1515
<Version>1.0.0</Version>
16-
<AssemblyVersion>1.0.0.74</AssemblyVersion>
17-
<FileVersion>1.0.0.74</FileVersion>
16+
<AssemblyVersion>1.0.0.75</AssemblyVersion>
17+
<FileVersion>1.0.0.75</FileVersion>
1818
<StartupObject>jaindb.Program</StartupObject>
1919
<UserSecretsId>fcfd6c0a-e53c-46cb-8a9d-b786c0579861</UserSecretsId>
2020
</PropertyGroup>
@@ -55,21 +55,21 @@
5555

5656
<ItemGroup>
5757
<PackageReference Include="JsonDiffPatch.Net" Version="2.1.0" />
58-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.4.0" />
59-
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.2" />
60-
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.1.1" />
61-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1" />
58+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.4.1" />
59+
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.3" />
60+
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.1.2" />
61+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.2" />
6262
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.1.1" />
6363
<PackageReference Include="Microsoft.AspNetCore.Server.HttpSys" Version="2.1.1" />
6464
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
65-
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="1.9.1" />
65+
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.0.0" />
6666
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
67-
<PackageReference Include="Microsoft.OData.Core" Version="7.5.0" />
67+
<PackageReference Include="Microsoft.OData.Core" Version="7.5.1" />
6868
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.1.1" />
6969
<PackageReference Include="Moon.AspNetCore.Authentication.Basic" Version="4.0.1" />
7070
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
7171
<PackageReference Include="RethinkDb.Driver" Version="2.3.23" />
72-
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
72+
<PackageReference Include="StackExchange.Redis" Version="2.0.505" />
7373
</ItemGroup>
7474

7575
<ItemGroup>

0 commit comments

Comments
 (0)