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
Change BlockChain to implement IReadOnlyList #205
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
[assembly: InternalsVisibleTo("Libplanet.Tests")] | ||
namespace Libplanet.Blockchain | ||
{ | ||
public class BlockChain<T> : IEnumerable<Block<T>> | ||
public class BlockChain<T> : IReadOnlyList<Block<T>> | ||
where T : IAction, new() | ||
{ | ||
private readonly ReaderWriterLockSlim _rwlock; | ||
|
@@ -83,8 +83,15 @@ public Block<T> Tip | |
get; private set; | ||
} | ||
|
||
/// <inheritdoc/> | ||
int IReadOnlyCollection<Block<T>>.Count => | ||
checked((int)Store.CountIndex(Id.ToString())); | ||
|
||
internal IStore Store { get; } | ||
|
||
/// <inheritdoc/> | ||
public Block<T> this[int index] => this[(long)index]; | ||
|
||
public Block<T> this[long index] | ||
{ | ||
get | ||
|
@@ -111,7 +118,7 @@ public Block<T> Tip | |
} | ||
|
||
public void Validate( | ||
IEnumerable<Block<T>> blocks, | ||
IReadOnlyList<Block<T>> blocks, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is a |
||
DateTimeOffset currentTime | ||
) | ||
{ | ||
|
@@ -236,8 +243,9 @@ public void Append(Block<T> block, DateTimeOffset currentTime) | |
block.Validate( | ||
currentTime, | ||
a => GetStates(new[] { a }, tip).GetValueOrDefault(a)); | ||
Validate(Enumerable.Append(this, block), currentTime); | ||
Enumerable.Append(this, block); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although it's late, this seems a miss;
To me, the code before changed seems correct. @earlbread Could you fix this in the pull request #210? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this code was changed because the type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems not needed anymore. I'll remove it in #210. |
||
|
||
Validate(this, currentTime); | ||
_rwlock.EnterWriteLock(); | ||
try | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ public interface IBlockPolicy<T> | |
/// <em>invalid</em>, or <c>null</c> if <paramref name="blocks"/> are | ||
/// <em>valid</em>.</returns> | ||
InvalidBlockException ValidateBlocks( | ||
IEnumerable<Block<T>> blocks, | ||
IReadOnlyList<Block<T>> blocks, | ||
DateTimeOffset currentTime | ||
); | ||
|
||
|
@@ -43,6 +43,6 @@ DateTimeOffset currentTime | |
/// followed by a new <see cref="Block{T}"/> to be mined.</param> | ||
/// <returns>A right <see cref="Block{T}.Difficulty"/> | ||
/// for a new <see cref="Block{T}"/> to be mined.</returns> | ||
int GetNextBlockDifficulty(IEnumerable<Block<T>> blocks); | ||
int GetNextBlockDifficulty(IReadOnlyList<Block<T>> blocks); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes in this file also break API backward compatibility, we need to write the corresponding changelogs. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@earlbread Although it's late, we should make it throw
ArgumentOutOfRangeException
instead ofIndexOutOfRangeException
, because collection types inSystem.Collections.Generic
throwArgumentOutOfRangeException
and only arrays throwIndexOutOfRangeException
. See also this SO answer:Could you change this in the next patch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll change it in the next patch!