Skip to content

Commit

Permalink
Merge pull request #751 from limebell/refactor/block-serialize
Browse files Browse the repository at this point in the history
Remove serializer in Block<T>, RawBlock<T>, Transaction<T>, RawTransaction<T>
  • Loading branch information
dahlia committed Jan 13, 2020
2 parents a655e18 + 8a81321 commit 524209d
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 815 deletions.
13 changes: 13 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ To be released.
from `Swarm<T>`. [[#705], [#725]]
- Added `workers` optional parameter into `Swarm<T>()` constructor.
[[#613], [#727]]
- `Block<T>` class became not to implement `ISerializable`. [[#751]]
- `Transaction<T>` class became not to implement `ISerializable`. [[#751]]
- `Block<T>.ToBencodex()` became to return `Bencodex.Types.Dictionary`. [[#751]]
- `Transaction<T>.ToBencodex()` became to return `Bencodex.Types.Dictionary`. [[#751]]
- Removed `Block<T>.FromBencodex(byte[])` method. [[#751]]
- Removed `Transaction<T>.FromBencodex(byte[])` method. [[#751]]

### Backward-incompatible network protocol changes

Expand Down Expand Up @@ -67,6 +73,12 @@ To be released.
- Added `Swarm<T>.Peers` property which returns an enumerable of peers in
`Swarm<T>`'s routing table. [[#739]]
- Added `IStore.LookupStateReference<T>()` method. [[#722]]
- Added `Block<T>.Serialize()` method which returns `byte[]`. [[#751]]
- Added `Transaction<T>.Serialize()` method which returns `byte[]`. [[#751]]
- Added `Block<T>(Bencodex.Types.Dictionary)` constructor. [[#751]]
- Added `Transaction<T>(Bencodex.Types.Dictionary)` constructor. [[#751]]
- Added `Block<T>.Deserialize(byte[])` method. [[#751]]
- Added `Transaction<T>.Deserialize(byte[])` method. [[#751]]
- Added `StoreExtension.Copy(this IStore, IStore)` extension method. [[#753]]

### Behavioral changes
Expand Down Expand Up @@ -163,6 +175,7 @@ To be released.
[#739]: https://github.com/planetarium/libplanet/pull/739
[#744]: https://github.com/planetarium/libplanet/pull/744
[#746]: https://github.com/planetarium/libplanet/pull/746
[#751]: https://github.com/planetarium/libplanet/pull/751
[#753]: https://github.com/planetarium/libplanet/pull/753


Expand Down
100 changes: 50 additions & 50 deletions Libplanet.Tests/Blocks/BlockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void ToBencodex()
0x6e, 0x73, 0x6c, 0x65, 0x65,
};

AssertBytesEqual(expected, _fx.Genesis.ToBencodex(true, true));
AssertBytesEqual(expected, _fx.Genesis.Serialize(true, true));
}

[Fact]
Expand Down Expand Up @@ -158,55 +158,55 @@ public void FromBencodex()
0x30, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2e, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x5a, 0x31, 0x32, 0x3a, 0x74, 0x72,
0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73,
0x6c, 0x64, 0x37, 0x3a, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e,
0x73, 0x6c, 0x64, 0x75, 0x37, 0x3a, 0x74, 0x79, 0x70, 0x65,
0x5f, 0x69, 0x64, 0x75, 0x36, 0x3a, 0x61, 0x74, 0x74, 0x61,
0x63, 0x6b, 0x75, 0x36, 0x3a, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x73, 0x64, 0x75, 0x36, 0x3a, 0x74, 0x61, 0x72, 0x67, 0x65,
0x74, 0x75, 0x33, 0x3a, 0x6f, 0x72, 0x63, 0x75, 0x31, 0x34,
0x3a, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x61, 0x64,
0x64, 0x72, 0x65, 0x73, 0x73, 0x32, 0x30, 0x3a, 0xc2, 0xa8,
0x60, 0x14, 0x07, 0x3d, 0x66, 0x2a, 0x4a, 0x9b, 0xfc, 0xf9,
0xcb, 0x54, 0x26, 0x3d, 0xfa, 0x4f, 0x5c, 0xbc, 0x75, 0x36,
0x3a, 0x77, 0x65, 0x61, 0x70, 0x6f, 0x6e, 0x75, 0x34, 0x3a,
0x77, 0x61, 0x6e, 0x64, 0x65, 0x65, 0x64, 0x75, 0x37, 0x3a,
0x74, 0x79, 0x70, 0x65, 0x5f, 0x69, 0x64, 0x75, 0x35, 0x3a,
0x73, 0x6c, 0x65, 0x65, 0x70, 0x75, 0x36, 0x3a, 0x76, 0x61,
0x6c, 0x75, 0x65, 0x73, 0x64, 0x75, 0x37, 0x3a, 0x7a, 0x6f,
0x6e, 0x65, 0x5f, 0x69, 0x64, 0x69, 0x31, 0x30, 0x65, 0x65,
0x65, 0x65, 0x35, 0x3a, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x69,
0x30, 0x65, 0x31, 0x30, 0x3a, 0x70, 0x75, 0x62, 0x6c, 0x69,
0x63, 0x5f, 0x6b, 0x65, 0x79, 0x36, 0x35, 0x3a, 0x04, 0x46,
0x11, 0x5b, 0x01, 0x31, 0xba, 0xcc, 0xf9, 0x4a, 0x58, 0x56,
0xed, 0xe8, 0x71, 0x29, 0x5f, 0x6f, 0x3d, 0x35, 0x2e, 0x68,
0x47, 0xcd, 0xa9, 0xc0, 0x3e, 0x89, 0xfe, 0x09, 0xf7, 0x32,
0x80, 0x87, 0x11, 0xec, 0x97, 0xaf, 0x6e, 0x34, 0x1f, 0x11,
0x0a, 0x32, 0x6d, 0xa1, 0xbd, 0xb8, 0x1f, 0x5a, 0xe3, 0xba,
0xdf, 0x76, 0xa9, 0x0b, 0x22, 0xc8, 0xc4, 0x91, 0xae, 0xd3,
0xaa, 0xa2, 0x96, 0x39, 0x3a, 0x73, 0x69, 0x67, 0x6e, 0x61,
0x74, 0x75, 0x72, 0x65, 0x37, 0x30, 0x3a, 0x30, 0x44, 0x02,
0x20, 0x6c, 0xad, 0x62, 0xd5, 0x1f, 0x70, 0x21, 0xe7, 0x05,
0x7c, 0xf0, 0x5d, 0xfe, 0xae, 0x0b, 0xbe, 0xcc, 0x7b, 0x35,
0x23, 0xba, 0x36, 0xaf, 0x46, 0xc1, 0x74, 0x00, 0x74, 0x05,
0xda, 0x61, 0x14, 0x02, 0x20, 0x37, 0x5d, 0x2e, 0xd1, 0x5c,
0x91, 0x94, 0xb1, 0xb0, 0x0d, 0xea, 0x25, 0xaf, 0x1e, 0x16,
0x87, 0x1d, 0x34, 0xa6, 0x30, 0x70, 0xea, 0x6b, 0xaa, 0x5e,
0x95, 0x1d, 0x2f, 0xe6, 0xc6, 0x11, 0xba, 0x36, 0x3a, 0x73,
0x69, 0x67, 0x6e, 0x65, 0x72, 0x32, 0x30, 0x3a, 0xc2, 0xa8,
0x60, 0x14, 0x07, 0x3d, 0x66, 0x2a, 0x4a, 0x9b, 0xfc, 0xf9,
0xcb, 0x54, 0x26, 0x3d, 0xfa, 0x4f, 0x5c, 0xbc, 0x39, 0x3a,
0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x75,
0x32, 0x37, 0x3a, 0x32, 0x30, 0x31, 0x38, 0x2d, 0x31, 0x31,
0x2d, 0x32, 0x31, 0x54, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x3a,
0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
0x31, 0x37, 0x3a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x65, 0x73,
0x32, 0x30, 0x3a, 0xc2, 0xa8, 0x60, 0x14, 0x07, 0x3d, 0x66,
0x2a, 0x4a, 0x9b, 0xfc, 0xf9, 0xcb, 0x54, 0x26, 0x3d, 0xfa,
0x4f, 0x5c, 0xbc, 0x65, 0x65, 0x65,
0x6c, 0x34, 0x35, 0x33, 0x3a, 0x64, 0x37, 0x3a, 0x61, 0x63,
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x6c, 0x64, 0x75, 0x37, 0x3a,
0x74, 0x79, 0x70, 0x65, 0x5f, 0x69, 0x64, 0x75, 0x36, 0x3a,
0x61, 0x74, 0x74, 0x61, 0x63, 0x6b, 0x75, 0x36, 0x3a, 0x76,
0x61, 0x6c, 0x75, 0x65, 0x73, 0x64, 0x75, 0x36, 0x3a, 0x74,
0x61, 0x72, 0x67, 0x65, 0x74, 0x75, 0x33, 0x3a, 0x6f, 0x72,
0x63, 0x75, 0x31, 0x34, 0x3a, 0x74, 0x61, 0x72, 0x67, 0x65,
0x74, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x32,
0x30, 0x3a, 0xc2, 0xa8, 0x60, 0x14, 0x07, 0x3d, 0x66, 0x2a,
0x4a, 0x9b, 0xfc, 0xf9, 0xcb, 0x54, 0x26, 0x3d, 0xfa, 0x4f,
0x5c, 0xbc, 0x75, 0x36, 0x3a, 0x77, 0x65, 0x61, 0x70, 0x6f,
0x6e, 0x75, 0x34, 0x3a, 0x77, 0x61, 0x6e, 0x64, 0x65, 0x65,
0x64, 0x75, 0x37, 0x3a, 0x74, 0x79, 0x70, 0x65, 0x5f, 0x69,
0x64, 0x75, 0x35, 0x3a, 0x73, 0x6c, 0x65, 0x65, 0x70, 0x75,
0x36, 0x3a, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x64, 0x75,
0x37, 0x3a, 0x7a, 0x6f, 0x6e, 0x65, 0x5f, 0x69, 0x64, 0x69,
0x31, 0x30, 0x65, 0x65, 0x65, 0x65, 0x35, 0x3a, 0x6e, 0x6f,
0x6e, 0x63, 0x65, 0x69, 0x30, 0x65, 0x31, 0x30, 0x3a, 0x70,
0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x6b, 0x65, 0x79, 0x36,
0x35, 0x3a, 0x04, 0x46, 0x11, 0x5b, 0x01, 0x31, 0xba, 0xcc,
0xf9, 0x4a, 0x58, 0x56, 0xed, 0xe8, 0x71, 0x29, 0x5f, 0x6f,
0x3d, 0x35, 0x2e, 0x68, 0x47, 0xcd, 0xa9, 0xc0, 0x3e, 0x89,
0xfe, 0x09, 0xf7, 0x32, 0x80, 0x87, 0x11, 0xec, 0x97, 0xaf,
0x6e, 0x34, 0x1f, 0x11, 0x0a, 0x32, 0x6d, 0xa1, 0xbd, 0xb8,
0x1f, 0x5a, 0xe3, 0xba, 0xdf, 0x76, 0xa9, 0x0b, 0x22, 0xc8,
0xc4, 0x91, 0xae, 0xd3, 0xaa, 0xa2, 0x96, 0x39, 0x3a, 0x73,
0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x37, 0x30,
0x3a, 0x30, 0x44, 0x02, 0x20, 0x6c, 0xad, 0x62, 0xd5, 0x1f,
0x70, 0x21, 0xe7, 0x05, 0x7c, 0xf0, 0x5d, 0xfe, 0xae, 0x0b,
0xbe, 0xcc, 0x7b, 0x35, 0x23, 0xba, 0x36, 0xaf, 0x46, 0xc1,
0x74, 0x00, 0x74, 0x05, 0xda, 0x61, 0x14, 0x02, 0x20, 0x37,
0x5d, 0x2e, 0xd1, 0x5c, 0x91, 0x94, 0xb1, 0xb0, 0x0d, 0xea,
0x25, 0xaf, 0x1e, 0x16, 0x87, 0x1d, 0x34, 0xa6, 0x30, 0x70,
0xea, 0x6b, 0xaa, 0x5e, 0x95, 0x1d, 0x2f, 0xe6, 0xc6, 0x11,
0xba, 0x36, 0x3a, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x72, 0x32,
0x30, 0x3a, 0xc2, 0xa8, 0x60, 0x14, 0x07, 0x3d, 0x66, 0x2a,
0x4a, 0x9b, 0xfc, 0xf9, 0xcb, 0x54, 0x26, 0x3d, 0xfa, 0x4f,
0x5c, 0xbc, 0x39, 0x3a, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74,
0x61, 0x6d, 0x70, 0x75, 0x32, 0x37, 0x3a, 0x32, 0x30, 0x31,
0x38, 0x2d, 0x31, 0x31, 0x2d, 0x32, 0x31, 0x54, 0x30, 0x30,
0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x5a, 0x31, 0x37, 0x3a, 0x75, 0x70, 0x64,
0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65,
0x73, 0x73, 0x65, 0x73, 0x32, 0x30, 0x3a, 0xc2, 0xa8, 0x60,
0x14, 0x07, 0x3d, 0x66, 0x2a, 0x4a, 0x9b, 0xfc, 0xf9, 0xcb,
0x54, 0x26, 0x3d, 0xfa, 0x4f, 0x5c, 0xbc, 0x65, 0x65, 0x65,
};
Block<PolymorphicAction<BaseAction>> actual =
Block<PolymorphicAction<BaseAction>>.FromBencodex(encoded);
Block<PolymorphicAction<BaseAction>>.Deserialize(encoded);
Assert.Equal(_fx.HasTx, actual);
}

Expand Down Expand Up @@ -424,7 +424,7 @@ public void EvaluateInvalidTxPublicKey()
ImmutableArray<byte>.Empty
);
byte[] sig = _fx.TxFixture.PrivateKey1.Sign(
new Transaction<DumbAction>(rawTxWithoutSig).ToBencodex(false)
new Transaction<DumbAction>(rawTxWithoutSig).Serialize(false)
);
var invalidTx = new Transaction<DumbAction>(
new RawTransaction(
Expand Down Expand Up @@ -464,7 +464,7 @@ public void EvaluateInvalidTxUpdatedAddresses()
byte[] sig = _fx.TxFixture.PrivateKey1.Sign(
new Transaction<PolymorphicAction<BaseAction>>(
rawTxWithoutSig
).ToBencodex(false)
).Serialize(false)
);
var invalidTx = new Transaction<PolymorphicAction<BaseAction>>(
new RawTransaction(
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Tests/Common/Action/Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public override void LoadPlainValue(

public void LoadPlainValue(Bencodex.Types.Dictionary plainValue)
{
Weapon = (Text)plainValue[(Text)"weapon"];
Target = (Text)plainValue[(Text)"target"];
TargetAddress = new Address((Binary)plainValue[(Text)"target_address"]);
Weapon = (Text)plainValue["weapon"];
Target = (Text)plainValue["target"];
TargetAddress = new Address((Binary)plainValue["target_address"]);
}

public override IAccountStateDelta Execute(IActionContext context)
Expand Down
12 changes: 3 additions & 9 deletions Libplanet.Tests/Common/Action/DumbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,12 @@ public IValue PlainValue
{
// In order to avoid changing tx signatures in many test
// fixtures, adds field only if RecordRandom = true.
plainValue =
(Dictionary)plainValue.Add(
(Text)"record_random",
new Bencodex.Types.Boolean(true));
plainValue = plainValue.Add("record_random", true);
}

if (Idempotent)
{
plainValue =
(Dictionary)plainValue.Add(
(Text)"idempotent",
new Bencodex.Types.Boolean(Idempotent));
plainValue = plainValue.Add("idempotent", Idempotent);
}

return plainValue;
Expand Down Expand Up @@ -192,7 +186,7 @@ Dictionary plainValue
RecordRehearsal = plainValue.GetValue<Boolean>("record_rehearsal").Value;
RecordRandom =
plainValue.ContainsKey((Text)"record_random") &&
plainValue[(Text)"record_random"] is Boolean r &&
plainValue["record_random"] is Boolean r &&
r.Value;

if (plainValue.ContainsKey((Text)"idempotent"))
Expand Down
151 changes: 0 additions & 151 deletions Libplanet.Tests/Serialization/Bencodex/BencodexFormatterTest.cs

This file was deleted.

8 changes: 4 additions & 4 deletions Libplanet.Tests/Tx/TransactionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public void ToBencodex()
0x30, 0x3a, 0x65,
};

AssertBytesEqual(expected, _fx.Tx.ToBencodex(true));
AssertBytesEqual(expected, _fx.Tx.Serialize(true));
}

[Fact]
Expand Down Expand Up @@ -404,7 +404,7 @@ public void ToBencodexWithActions()
0x5c, 0xbc, 0x65,
};

AssertBytesEqual(expected, _fx.TxWithActions.ToBencodex(true));
AssertBytesEqual(expected, _fx.TxWithActions.Serialize(true));
}

[Fact]
Expand Down Expand Up @@ -451,7 +451,7 @@ public void FromBencodex()
0x7b, 0x76,
}
).PublicKey;
Transaction<DumbAction> tx = Transaction<DumbAction>.FromBencodex(bytes);
Transaction<DumbAction> tx = Transaction<DumbAction>.Deserialize(bytes);

Assert.Equal(publicKey, tx.PublicKey);
Assert.Equal(ImmutableHashSet<Address>.Empty, tx.UpdatedAddresses);
Expand Down Expand Up @@ -546,7 +546,7 @@ public void FromBencodexWithActions()
}
).PublicKey;
Transaction<PolymorphicAction<BaseAction>> tx =
Transaction<PolymorphicAction<BaseAction>>.FromBencodex(bytes);
Transaction<PolymorphicAction<BaseAction>>.Deserialize(bytes);

Assert.Equal(publicKey, tx.PublicKey);
Assert.Equal(
Expand Down
Loading

0 comments on commit 524209d

Please sign in to comment.