Skip to content

Commit

Permalink
Use Address.Bencoded instead of Address.ByteArray
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Nov 7, 2023
1 parent e96f2e6 commit 390d98a
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/Common/Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Attack : BaseAction
.Add("values", Dictionary.Empty
.Add("weapon", Weapon)
.Add("target", Target)
.Add("target_address", TargetAddress.ByteArray));
.Add("target_address", TargetAddress.Bencoded));

public string Weapon { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/Common/DetectRehearsal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DetectRehearsal : BaseAction
public override IValue PlainValue => Dictionary.Empty
.Add("type_id", TypeId)
.Add("values", Dictionary.Empty
.Add("target_address", TargetAddress.ByteArray));
.Add("target_address", TargetAddress.Bencoded));

public bool ResultState { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Action.Tests/Common/DumbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public IValue PlainValue
new Dictionary<string, IValue>
{
["item"] = (Text)Item,
["target_address"] = new Binary(TargetAddress.ByteArray),
["target_address"] = TargetAddress.Bencoded,
["record_rehearsal"] = new Bencodex.Types.Boolean(RecordRehearsal),
});
}
Expand All @@ -116,8 +116,8 @@ public IValue PlainValue
if (!(Transfer is null))
{
plainValue = plainValue
.Add("transfer_from", Transfer.Item1.ByteArray)
.Add("transfer_to", Transfer.Item2.ByteArray)
.Add("transfer_from", Transfer.Item1.Bencoded)
.Add("transfer_to", Transfer.Item2.Bencoded)
.Add("transfer_amount", Transfer.Item3);
}

Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/Common/SetStatesAtBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SetStatesAtBlock(Address address, IValue value, long blockIndex)
}

public IValue PlainValue => Bencodex.Types.Dictionary.Empty
.Add("address", _address.ByteArray)
.Add("address", _address.Bencoded)
.Add("value", _value)
.Add("block_index", _blockIndex);

Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/Loader/IndexedActionLoaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void LoadAction()
.Add("values", Dictionary.Empty
.Add("weapon", "sword")
.Add("target", "dummy")
.Add("target_address", new PrivateKey().PublicKey.ToAddress().ByteArray)));
.Add("target_address", new PrivateKey().PublicKey.ToAddress().Bencoded)));
var action3 = new RandomAction(new PrivateKey().PublicKey.ToAddress());

var loader = new IndexedActionLoader(
Expand Down
7 changes: 3 additions & 4 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,10 +1323,9 @@ private sealed class EvaluateTestAction : IAction
public Address BlockIndexKey { get; set; }

public IValue PlainValue => new List(
(Binary)SignerKey.ByteArray,
(Binary)MinerKey.ByteArray,
(Binary)BlockIndexKey.ByteArray
);
SignerKey.Bencoded,
MinerKey.Bencoded,
BlockIndexKey.Bencoded);

public void LoadPlainValue(IValue plainValue)
{
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Blocks/BlockMarshalerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void MarshalBlockMetadata()
Dictionary.Empty
.Add(IndexKey, 0L)
.Add(TimestampKey, "2021-09-06T04:46:39.123000Z")
.Add(MinerKey, _content.GenesisContentPv0.Miner.ByteArray),
.Add(MinerKey, _content.GenesisContentPv0.Miner.Bencoded),
BlockMarshaler.MarshalBlockMetadata(_content.GenesisContentPv0)
);
AssertBencodexEqual(
Expand All @@ -143,7 +143,7 @@ public void MarshalBlockMetadata()
PreviousHashKey,
_content.Block1ContentPv1.PreviousHash?.ByteArray ?? default)
.Add(TimestampKey, "2021-09-06T08:01:09.045000Z")
.Add(MinerKey, _content.Block1ContentPv1.Miner.ByteArray)
.Add(MinerKey, _content.Block1ContentPv1.Miner.Bencoded)
.Add(TxHashKey, _content.Block1ContentPv1.TxHash?.ByteArray ?? default),
BlockMarshaler.MarshalBlockMetadata(_content.Block1ContentPv1)
);
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Tests/Tx/TxMarshalerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public void UnmarshalTransactionWithCustomActions()
.Add("values", Dictionary.Empty
.Add("weapon", "wand")
.Add("target", "orc")
.Add("target_address", new Address(publicKey).ByteArray)),
.Add("target_address", new Address(publicKey).Bencoded)),
action0.PlainValue
);

Expand Down
16 changes: 8 additions & 8 deletions Libplanet.Tests/Tx/TxMetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void Deserialize()
{
Bencodex.Types.Dictionary dict1 = Dictionary.Empty
.Add(new byte[] { 0x6e }, 123L)
.Add(new byte[] { 0x73 }, _key1.ToAddress().ByteArray)
.Add(new byte[] { 0x73 }, _key1.ToAddress().Bencoded)
.Add(new byte[] { 0x75 }, new List())
.Add(new byte[] { 0x74 }, "2022-05-23T10:02:00.000000Z")
.Add(new byte[] { 0x70 }, _key1.PublicKey.ToImmutableArray(compress: false));
Expand All @@ -115,12 +115,12 @@ public void Deserialize()

Bencodex.Types.Dictionary dict2 = Dictionary.Empty
.Add(new byte[] { 0x6e }, 0L)
.Add(new byte[] { 0x73 }, _key2.ToAddress().ByteArray)
.Add(new byte[] { 0x73 }, _key2.ToAddress().Bencoded)
.Add(
new byte[] { 0x75 },
Bencodex.Types.List.Empty
.Add(_key1.ToAddress().ToByteArray())
.Add(_key2.ToAddress().ToByteArray()))
.Add(_key1.ToAddress().Bencoded)
.Add(_key2.ToAddress().Bencoded))
.Add(new byte[] { 0x74 }, "2022-01-12T04:56:07.890000Z")
.Add(new byte[] { 0x70 }, _key2.PublicKey.ToImmutableArray(compress: false))
.Add(
Expand Down Expand Up @@ -155,7 +155,7 @@ public void ToBencodex(DateTimeOffset timestamp)
};
Bencodex.Types.Dictionary expected1 = Dictionary.Empty
.Add(new byte[] { 0x6e }, 123L)
.Add(new byte[] { 0x73 }, _key1.ToAddress().ByteArray)
.Add(new byte[] { 0x73 }, _key1.ToAddress().Bencoded)
.Add(new byte[] { 0x75 }, new List())
.Add(new byte[] { 0x74 }, "2022-05-23T10:02:00.000000Z")
.Add(new byte[] { 0x70 }, _key1.PublicKey.ToImmutableArray(compress: false));
Expand All @@ -177,12 +177,12 @@ public void ToBencodex(DateTimeOffset timestamp)
};
Bencodex.Types.Dictionary expected2 = Dictionary.Empty
.Add(new byte[] { 0x6e }, 0L)
.Add(new byte[] { 0x73 }, _key2.ToAddress().ByteArray)
.Add(new byte[] { 0x73 }, _key2.ToAddress().Bencoded)
.Add(
new byte[] { 0x75 },
Bencodex.Types.List.Empty
.Add(_key1.ToAddress().ToByteArray())
.Add(_key2.ToAddress().ToByteArray()))
.Add(_key1.ToAddress().Bencoded)
.Add(_key2.ToAddress().Bencoded))
.Add(new byte[] { 0x74 }, "2022-01-12T04:56:07.890000Z")
.Add(new byte[] { 0x70 }, _key2.PublicKey.ToImmutableArray(compress: false))
.Add(
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Types/Assets/Currency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public bool Equals(Currency other) =>
public IValue Serialize()
{
IValue minters = Minters is IImmutableSet<Address> m
? new Bencodex.Types.List(m.Select<Address, IValue>(a => new Binary(a.ByteArray)))
? new Bencodex.Types.List(m.Select<Address, IValue>(a => a.Bencoded))
: (IValue)Null.Value;
var serialized = Bencodex.Types.Dictionary.Empty
.Add("ticker", Ticker)
Expand Down Expand Up @@ -727,7 +727,7 @@ private static SHA1 GetSHA1()
private IValue SerializeForHash()
{
IValue minters = Minters is ImmutableHashSet<Address> a
? new List(a.OrderBy(m => m).Select(m => m.ByteArray))
? new List(a.OrderBy(m => m).Select(m => m.Bencoded))
: (IValue)Null.Value;

var serialized = Dictionary.Empty
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Types/Blocks/BlockMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static Dictionary MarshalBlockMetadata(IBlockMetadata metadata)

dict = metadata.PublicKey is { } pubKey
? dict.Add(PublicKeyKey, pubKey.Format(compress: true))
: dict.Add(MinerKey, metadata.Miner.ByteArray);
: dict.Add(MinerKey, metadata.Miner.Bencoded);

if (metadata.LastCommit is { } commit)
{
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Types/Blocks/BlockMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public Bencodex.Types.Dictionary MakeCandidateData(Nonce nonce)
// and its ProtocolVersion is >= 2 when it is not null:
dict = PublicKey is { } pubKey && ProtocolVersion >= 2
? dict.Add("public_key", pubKey.Format(compress: true)) // ProtocolVersion >= 2
: dict.Add("reward_beneficiary", Miner.ByteArray); /////// ProtocolVersion <= 1
: dict.Add("reward_beneficiary", Miner.Bencoded); /////// ProtocolVersion <= 1

return dict;
}
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Types/Tx/TxMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class TxMarshaler
public static Bencodex.Types.Dictionary MarshalTxInvoice(this ITxInvoice invoice)
{
Bencodex.Types.List updatedAddresses = new Bencodex.Types.List(
invoice.UpdatedAddresses.Select<Address, IValue>(addr => new Binary(addr.ByteArray))
invoice.UpdatedAddresses.Select<Address, IValue>(addr => addr.Bencoded)
);
string timestamp = invoice.Timestamp
.ToUniversalTime()
Expand Down Expand Up @@ -70,7 +70,7 @@ public static Bencodex.Types.Dictionary MarshalTxSigningMetadata(
this ITxSigningMetadata metadata
) => Dictionary.Empty
.Add(NonceKey, metadata.Nonce)
.Add(SignerKey, metadata.Signer.ByteArray)
.Add(SignerKey, metadata.Signer.Bencoded)
.Add(PublicKeyKey, metadata.PublicKey.ToImmutableArray(compress: false));

[Pure]
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Types/Tx/TxMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ public TxMetadata(Bencodex.Types.Dictionary dictionary)
public Bencodex.Types.Dictionary ToBencodex()
{
List updatedAddresses = new List(
UpdatedAddresses.Select<Address, IValue>(addr => new Binary(addr.ByteArray)));
UpdatedAddresses.Select<Address, IValue>(addr => addr.Bencoded));
string timestamp = Timestamp
.ToUniversalTime()
.ToString(TimestampFormat, CultureInfo.InvariantCulture);
Bencodex.Types.Dictionary dict = Dictionary.Empty
.Add(NonceKey, Nonce)
.Add(SignerKey, Signer.ByteArray)
.Add(SignerKey, Signer.Bencoded)
.Add(UpdatedAddressesKey, updatedAddresses)
.Add(PublicKeyKey, PublicKey.ToImmutableArray(compress: false))
.Add(TimestampKey, timestamp);
Expand Down

0 comments on commit 390d98a

Please sign in to comment.