Skip to content

Commit

Permalink
Make PutTransaction<T>() idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Aug 8, 2019
1 parent 62c716b commit d6b028c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ To be released.
if a preloading process suddenly gets shutdown. [[#417]]
- `FileStore` and `LiteDBStore` became to guarantee atomicity of storing
transactions. [[#413]]
- `IStore.PutTransaction<T>()` became to do nothing when it takes
the `Transaction<T>` more than once. [[#413]]

### Bug fixes

Expand Down
7 changes: 7 additions & 0 deletions Libplanet/Store/IStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public interface IStore
Transaction<T> GetTransaction<T>(TxId txid)
where T : IAction, new();

/// <summary>
/// Puts a given <see cref="Transaction{T}"/> to the store. If the same transaction
/// already exists in the store it does nothing.
/// </summary>
/// <param name="tx">A transaction to put into the store.</param>
/// <typeparam name="T">An <see cref="IAction"/> type. It should match
/// to <see cref="Transaction{T}"/>'s type parameter.</typeparam>
void PutTransaction<T>(Transaction<T> tx)
where T : IAction, new();

Expand Down
21 changes: 18 additions & 3 deletions Libplanet/Store/LiteDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,29 @@ public override void PutTransaction<T>(Transaction<T> tx)
string fileId = TxFileId(tx.Id);
string filename = tx.Id.ToHex();
byte[] txBytes = tx.ToBencodex(true);
_txLock.EnterWriteLock();
_txLock.EnterUpgradeableReadLock();
try
{
UploadFile(fileId, filename, txBytes);
LiteFileInfo file = _db.FileStorage.FindById(fileId);
if (file is LiteFileInfo)
{
// No-op if already exists.
return;
}

_txLock.EnterWriteLock();
try
{
UploadFile(fileId, filename, txBytes);
}
finally
{
_txLock.ExitWriteLock();
}
}
finally
{
_txLock.ExitWriteLock();
_txLock.ExitUpgradeableReadLock();
}
}

Expand Down

0 comments on commit d6b028c

Please sign in to comment.