forked from planetarium/libplanet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BlockChain<T>.Append() now reject too heavy blocks
Fix planetarium#201
- Loading branch information
Showing
7 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Libplanet.Tests/Blocks/InvalidBlockBytesLengthExceptionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.IO; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using Libplanet.Blocks; | ||
using Xunit; | ||
|
||
namespace Libplanet.Tests.Blocks | ||
{ | ||
public class InvalidBlockBytesLengthExceptionTest | ||
{ | ||
public void Serialization() | ||
{ | ||
var e = new InvalidBlockBytesLengthException(10, 5, "A message."); | ||
var f = new BinaryFormatter(); | ||
InvalidBlockBytesLengthException e2; | ||
|
||
using (var s = new MemoryStream()) | ||
{ | ||
f.Serialize(s, e); | ||
s.Seek(0, SeekOrigin.Begin); | ||
e2 = (InvalidBlockBytesLengthException)f.Deserialize(s); | ||
} | ||
|
||
Assert.Equal(e.Message, e2.Message); | ||
Assert.Equal(e.BlockBytesLength, e2.BlockBytesLength); | ||
Assert.Equal(e.MaxBlockBytesLength, e2.MaxBlockBytesLength); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#nullable enable | ||
using System; | ||
using System.Runtime.Serialization; | ||
using Libplanet.Blockchain.Policies; | ||
|
||
namespace Libplanet.Blocks | ||
{ | ||
/// <summary> | ||
/// The exception that is thrown when a <see cref="Block{T}"/>'s | ||
/// <see cref="Block{T}.BytesLength"/> (i.e., the length of bytes in its serialized format) | ||
/// is too long. | ||
/// </summary> | ||
[Serializable] | ||
public sealed class InvalidBlockBytesLengthException : InvalidBlockException, ISerializable | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="InvalidBlockBytesLengthException"/> class. | ||
/// </summary> | ||
/// <param name="blockBytesLength">The invalid <see cref="Block{T}"/>'s | ||
/// <see cref="Block{T}.BytesLength"/>. It is automatically included to the | ||
/// <see cref="Exception.Message"/> string.</param> | ||
/// <param name="maxBlockBytesLength">The maximum allowed length of bytes. It is | ||
/// automatically included to the <see cref="Exception.Message"/> string.</param> | ||
/// <param name="message">The message that describes the error.</param> | ||
public InvalidBlockBytesLengthException( | ||
int blockBytesLength, | ||
int maxBlockBytesLength, | ||
string message | ||
) | ||
: base( | ||
$"{message}\n" + | ||
$"Actual: #{blockBytesLength} bytes\n" + | ||
$"Allowed (max): #{maxBlockBytesLength}") | ||
{ | ||
BlockBytesLength = blockBytesLength; | ||
MaxBlockBytesLength = maxBlockBytesLength; | ||
} | ||
|
||
private InvalidBlockBytesLengthException(SerializationInfo info, StreamingContext context) | ||
: base(info.GetString(nameof(Message)) ?? string.Empty) | ||
{ | ||
BlockBytesLength = info.GetInt32(nameof(BlockBytesLength)); | ||
MaxBlockBytesLength = info.GetInt32(nameof(MaxBlockBytesLength)); | ||
} | ||
|
||
/// <summary> | ||
/// The bytes length of the actual block. | ||
/// </summary> | ||
public int BlockBytesLength { get; set; } | ||
|
||
/// <summary> | ||
/// The maximum allowed length of a block in bytes. | ||
/// </summary> | ||
/// <seealso cref="IBlockPolicy{T}.GetMaxBlockBytes(long)"/> | ||
public int MaxBlockBytesLength { get; set; } | ||
|
||
public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
base.GetObjectData(info, context); | ||
info.AddValue(nameof(Message), Message); | ||
info.AddValue(nameof(BlockBytesLength), BlockBytesLength); | ||
info.AddValue(nameof(MaxBlockBytesLength), MaxBlockBytesLength); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters