Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Aug 9, 2019
1 parent 699e3ac commit 6d0e141
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
11 changes: 11 additions & 0 deletions Libplanet.Tests/Store/StoreTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ public void StoreStateReference<T>(
_store.StoreStateReference(@namespace, addresses, block);
}

public void StoreStateReference(
string @namespace,
IImmutableSet<Address> addresses,
HashDigest<SHA256> blockHash,
long blockIndex)
{
// FIXME: Log arguments properly (including @namespace).
_logs.Add((nameof(StoreStateReference), blockHash, null));
_store.StoreStateReference(@namespace, addresses, blockHash, blockIndex);
}

public void ForkStateReferences<T>(
string sourceNamespace,
string destinationNamespace,
Expand Down
23 changes: 20 additions & 3 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,20 +1022,37 @@ await socket.SendMultipartMessageAsync(

int count = 0, totalCount = recentStates.StateReferences.Count;
_logger.Debug("Starts to store state refs received from {0}.", peer);

var d = new Dictionary<HashDigest<SHA256>, ISet<Address>>();
foreach (var pair in recentStates.StateReferences)
{
IImmutableSet<Address> address = ImmutableHashSet.Create(pair.Key);
foreach (HashDigest<SHA256> bHash in pair.Value)
{
Block<T> block = store.GetBlock<T>(bHash);
store.StoreStateReference(ns, address, block);
if (!d.ContainsKey(bHash))
{
d[bHash] = new HashSet<Address>();
}

d[bHash].UnionWith(address);
}
}

totalCount = d.Count;
foreach (KeyValuePair<HashDigest<SHA256>, ISet<Address>> pair in d)
{
HashDigest<SHA256> hash = pair.Key;
IImmutableSet<Address> addresses = pair.Value.ToImmutableHashSet();
if (store.GetBlockIndex(hash) is long index)
{
store.StoreStateReference(ns, addresses, hash, index);
}

progress?.Report(new StateReferenceDownloadState()
{
TotalStateReferenceCount = totalCount,
ReceivedStateReferenceCount = ++count,
ReceivedAddress = pair.Key,
ReceivedAddress = addresses.First(), // Fixme
});
}

Expand Down
13 changes: 11 additions & 2 deletions Libplanet/Store/BaseStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,20 @@ public abstract IEnumerable<Tuple<HashDigest<SHA256>, long>> IterateStateReferen
string @namespace, Address address);

/// <inheritdoc />
public abstract void StoreStateReference<T>(
public void StoreStateReference<T>(
string @namespace,
IImmutableSet<Address> addresses,
Block<T> block)
where T : IAction, new();
where T : IAction, new()
{
StoreStateReference(@namespace, addresses, block.Hash, block.Index);
}

public abstract void StoreStateReference(
string @namespace,
IImmutableSet<Address> addresses,
HashDigest<SHA256> hashDigest,
long index);

/// <inheritdoc />
public abstract void ForkStateReferences<T>(
Expand Down
7 changes: 3 additions & 4 deletions Libplanet/Store/FileStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,12 @@ public override IEnumerable<Tuple<HashDigest<SHA256>, long>> IterateStateReferen
}

/// <inheritdoc/>
public override void StoreStateReference<T>(
public override void StoreStateReference(
string @namespace,
IImmutableSet<Address> addresses,
Block<T> block)
HashDigest<SHA256> blockHash,
long blockIndex)
{
HashDigest<SHA256> blockHash = block.Hash;
long blockIndex = block.Index;
int hashSize = HashDigest<SHA256>.Size;

foreach (Address address in addresses)
Expand Down
6 changes: 6 additions & 0 deletions Libplanet/Store/IStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ void StoreStateReference<T>(
Block<T> block)
where T : IAction, new();

void StoreStateReference(
string @namespace,
IImmutableSet<Address> addresses,
HashDigest<SHA256> blockHash,
long blockIndex);

/// <summary>
/// Forks state references, which are <see cref="Block{T}.Hash"/>es that
/// have the state of the <see cref="Address"/>es, from
Expand Down
9 changes: 5 additions & 4 deletions Libplanet/Store/LiteDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,20 @@ public override IEnumerable<Tuple<HashDigest<SHA256>, long>> IterateStateReferen
}

/// <inheritdoc/>
public override void StoreStateReference<T>(
public override void StoreStateReference(
string @namespace,
IImmutableSet<Address> addresses,
Block<T> block)
HashDigest<SHA256> hash,
long index)
{
string collId = StateRefId(@namespace);
LiteCollection<StateRefDoc> coll = _db.GetCollection<StateRefDoc>(collId);
coll.InsertBulk(
addresses.Select(addr => new StateRefDoc
{
Address = addr,
BlockIndex = block.Index,
BlockHash = block.Hash,
BlockIndex = index,
BlockHash = hash,
})
);
coll.EnsureIndex("AddressString");
Expand Down

0 comments on commit 6d0e141

Please sign in to comment.