Skip to content

Commit

Permalink
Turn singular Recipient to plural UpdatedAddresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Mar 14, 2019
1 parent 7571090 commit 31b87ae
Show file tree
Hide file tree
Showing 18 changed files with 394 additions and 346 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Expand Up @@ -22,6 +22,15 @@ To be released.
`AddressStateMap` to `IAccountStateDelta`. `AddressStateMap` to `IAccountStateDelta`.
- Removed `IAction.RequestStates()` method because there is no need for it - Removed `IAction.RequestStates()` method because there is no need for it
and thus it is not used anymore. and thus it is not used anymore.
- `Transaction<T>.Recipient` and `RawTransaction.Recipient` properties were
replaced by `Transaction<T>.UpdatedAddresses` and
`RawTransaction.UpdatedAddresses` properties.
The corresponding parameter names on constructors and methods were
replaced too.
- Since the schema of `RawTransaction` class was changed, the serialization
format of transactions and blocks were also changed. It affects to
the way to generate `Transaction<T>.Signature`, `Transaction<T>.Id`, and
`Block.Hash` values as well.
- The type of `Peer.Urls` property was changed from `Uri` to `IPEndPoint`. - The type of `Peer.Urls` property was changed from `Uri` to `IPEndPoint`.
- Since we decided to depend on TURN ([RFC 5766]) and STUN ([RFC 5389]) to - Since we decided to depend on TURN ([RFC 5766]) and STUN ([RFC 5389]) to
work around NAT so that `Peer`'s endpoints don't have to be multiple, work around NAT so that `Peer`'s endpoints don't have to be multiple,
Expand Down
3 changes: 2 additions & 1 deletion Libplanet.Explorer/Controllers/ExplorerController.cs
Expand Up @@ -136,7 +136,8 @@ public IActionResult getTransaction(string txIdString)
Signature = tx.Signature, Signature = tx.Signature,
Timestamp = tx.Timestamp, Timestamp = tx.Timestamp,
Signer = tx.Signer.ToHex(), Signer = tx.Signer.ToHex(),
Recipient = tx.Recipient.ToHex(), UpdatedAddresses = tx.UpdatedAddresses
.Select(a => a.ToHex()).ToArray(),
Actions = tx.Actions Actions = tx.Actions
.Select(act => new Dictionary<string, object> .Select(act => new Dictionary<string, object>
{ {
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Explorer/ViewModels/TransactionViewModel.cs
Expand Up @@ -11,7 +11,7 @@ public class TransactionViewModel
public byte[] Signature { get; set; } public byte[] Signature { get; set; }
public DateTimeOffset Timestamp { get; set; } public DateTimeOffset Timestamp { get; set; }
public string Signer { get; set; } public string Signer { get; set; }
public string Recipient { get; set; } public string[] UpdatedAddresses { get; set; }
public IEnumerable<Dictionary<string, object>> Actions { get; set; } public IEnumerable<Dictionary<string, object>> Actions { get; set; }
} }
} }
39 changes: 29 additions & 10 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
@@ -1,9 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Drawing.Design;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using Libplanet.Action; using Libplanet.Action;
using Libplanet.Blockchain; using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies; using Libplanet.Blockchain.Policies;
Expand Down Expand Up @@ -110,7 +108,7 @@ public void ProcessActions()
}; };
Transaction<BaseAction> tx1 = Transaction<BaseAction>.Make( Transaction<BaseAction> tx1 = Transaction<BaseAction>.Make(
new PrivateKey(), new PrivateKey(),
_fx.Address1, ImmutableHashSet.Create(_fx.Address1),
actions1, actions1,
DateTimeOffset.UtcNow DateTimeOffset.UtcNow
); );
Expand Down Expand Up @@ -143,7 +141,7 @@ public void ProcessActions()
}; };
Transaction<BaseAction> tx2 = Transaction<BaseAction>.Make( Transaction<BaseAction> tx2 = Transaction<BaseAction>.Make(
new PrivateKey(), new PrivateKey(),
_fx.Address1, ImmutableHashSet.Create(_fx.Address1),
actions2, actions2,
DateTimeOffset.UtcNow DateTimeOffset.UtcNow
); );
Expand Down Expand Up @@ -176,13 +174,31 @@ public void CanStoreAddresses()
{ {
_fx.Transaction1, _fx.Transaction1,
_fx.Transaction2, _fx.Transaction2,
_fx.MakeTransaction(
new[]
{
new Attack
{
Weapon = "sword",
Target = "goblin",
TargetAddress = _fx.Address1,
},
},
new[] { _fx.Address1 }.ToImmutableHashSet()
),
}; };
_blockChain.StageTransactions(txs); _blockChain.StageTransactions(txs);
_blockChain.MineBlock(_fx.Address1); _blockChain.MineBlock(_fx.Address1);


Assert.NotEmpty(_blockChain.Addresses); Assert.Contains(_fx.Address1, _blockChain.Addresses);
Assert.Contains(_fx.Transaction1, _blockChain.Addresses[_fx.Transaction1.Recipient]); foreach (Address a in _fx.Transaction1.UpdatedAddresses)
Assert.DoesNotContain(_fx.Transaction2, _blockChain.Addresses[_fx.Transaction1.Recipient]); {
Assert.Contains(_fx.Transaction1, _blockChain.Addresses[a]);
Assert.DoesNotContain(
_fx.Transaction2,
_blockChain.Addresses[a]
);
}
} }


[Fact] [Fact]
Expand Down Expand Up @@ -249,17 +265,20 @@ public void CanGetBlockLocator()
} }


[Fact] [Fact]
public void CanEvaluateActions() public void EvaluateActions()
{ {
PrivateKey fromPrivateKey = new PrivateKey(); PrivateKey fromPrivateKey = new PrivateKey();
Address fromAddress = fromPrivateKey.PublicKey.ToAddress(); Address fromAddress = fromPrivateKey.PublicKey.ToAddress();
Address toAddress = _fx.Address1;
long blockIndex = 0; long blockIndex = 0;


TestEvaluateAction action = new TestEvaluateAction(); TestEvaluateAction action = new TestEvaluateAction();
Transaction<BaseAction> tx1 = Transaction<BaseAction>.Make( Transaction<BaseAction> tx1 = Transaction<BaseAction>.Make(
fromPrivateKey, fromPrivateKey,
toAddress, new[]
{
TestEvaluateAction.SignerKey,
TestEvaluateAction.BlockIndexKey,
}.ToImmutableHashSet(),
new List<BaseAction> { action }, new List<BaseAction> { action },
DateTimeOffset.UtcNow DateTimeOffset.UtcNow
); );
Expand Down
47 changes: 24 additions & 23 deletions Libplanet.Tests/Blocks/BlockTest.cs
Expand Up @@ -81,14 +81,14 @@ public void ToBencodex()
[Fact] [Fact]
public void FromBencodex() public void FromBencodex()
{ {
var encoded = new byte[] byte[] encoded =
{ {
0x64, 0x31, 0x30, 0x3a, 0x64, 0x69, 0x66, 0x66, 0x69, 0x63, 0x64, 0x31, 0x30, 0x3a, 0x64, 0x69, 0x66, 0x66, 0x69, 0x63,
0x75, 0x6c, 0x74, 0x79, 0x69, 0x31, 0x65, 0x34, 0x3a, 0x68, 0x75, 0x6c, 0x74, 0x79, 0x69, 0x31, 0x65, 0x34, 0x3a, 0x68,
0x61, 0x73, 0x68, 0x33, 0x32, 0x3a, 0xc1, 0x04, 0xda, 0xf5, 0x61, 0x73, 0x68, 0x33, 0x32, 0x3a, 0xfd, 0x53, 0xab, 0x50,
0xcc, 0x56, 0x6d, 0x81, 0x3d, 0xfc, 0xae, 0xba, 0x32, 0x65, 0xb0, 0x64, 0xd2, 0xf9, 0x31, 0x08, 0x13, 0xa4, 0x36, 0x27,
0x2d, 0x98, 0x87, 0x07, 0x9a, 0x7a, 0x5f, 0x12, 0x22, 0xd7, 0x39, 0xce, 0x1a, 0x8d, 0x1e, 0xbe, 0x84, 0xd2, 0xdc, 0x7f,
0xe0, 0x1f, 0xea, 0x6a, 0xbb, 0x91, 0x4e, 0x50, 0x35, 0x3a, 0xea, 0x98, 0x32, 0xe3, 0x2a, 0x68, 0x35, 0xf9, 0x35, 0x3a,
0x69, 0x6e, 0x64, 0x65, 0x78, 0x69, 0x31, 0x65, 0x35, 0x3a, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x69, 0x31, 0x65, 0x35, 0x3a,
0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x34, 0x3a, 0x02, 0x00, 0x00, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x34, 0x3a, 0x02, 0x00, 0x00,
0x00, 0x31, 0x33, 0x3a, 0x70, 0x72, 0x65, 0x76, 0x69, 0x6f, 0x00, 0x31, 0x33, 0x3a, 0x70, 0x72, 0x65, 0x76, 0x69, 0x6f,
Expand Down Expand Up @@ -130,26 +130,27 @@ public void FromBencodex()
0x09, 0xf7, 0x32, 0x80, 0x87, 0x11, 0xec, 0x97, 0xaf, 0x6e, 0x09, 0xf7, 0x32, 0x80, 0x87,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0x36, 0x3a, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x72, 0x32, 0x30,
0x3a, 0xc2, 0xa8, 0x60, 0x14, 0x07, 0x3d, 0x66, 0x2a, 0x4a, 0x3a, 0xc2, 0xa8, 0x60, 0x14, 0x07, 0x3d, 0x66, 0x2a, 0x4a,
0x9b, 0xfc, 0xf9, 0xcb, 0x54, 0x26, 0x3d, 0xfa, 0x4f, 0x5c, 0x9b, 0xfc, 0xf9, 0xcb, 0x54, 0x26, 0x3d, 0xfa, 0x4f, 0x5c,
0xbc, 0x39, 0x3a, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0xbc, 0x39, 0x3a, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61,
0x72, 0x65, 0x37, 0x30, 0x3a, 0x30, 0x44, 0x02, 0x20, 0x19, 0x6d, 0x70, 0x75, 0x32, 0x37, 0x3a, 0x32, 0x30, 0x31, 0x38,
0x18, 0xb2, 0x19, 0x3e, 0x4f, 0x84, 0x8b, 0xa9, 0x40, 0xce, 0x2d, 0x31, 0x31, 0x2d, 0x32, 0x31, 0x54, 0x30, 0x30, 0x3a,
0x63, 0xdb, 0xf8, 0xbc, 0xe0, 0x6e, 0x23, 0x96, 0xc2, 0x81, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30,
0x06, 0xe2, 0xbd, 0x48, 0x93, 0xec, 0xdb, 0xbd, 0x7d, 0x9b, 0x30, 0x30, 0x5a, 0x31, 0x37, 0x3a, 0x75, 0x70, 0x64, 0x61,
0x98, 0x02, 0x20, 0x09, 0x8a, 0x02, 0x27, 0x4c, 0x76, 0x30, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73,
0xee, 0x19, 0x6d, 0x26, 0x48, 0x0b, 0xb5, 0x10, 0x7a, 0x3d, 0x73, 0x65, 0x73, 0x32, 0x30, 0x3a, 0xc2, 0xa8, 0x60, 0x14,
0xf5, 0x6b, 0x80, 0x3d, 0x55, 0x20, 0xcb, 0x4b, 0x23, 0x63, 0x07, 0x3d, 0x66, 0x2a, 0x4a, 0x9b, 0xfc, 0xf9, 0xcb, 0x54,
0xc2, 0x11, 0xda, 0x14, 0x8d, 0x39, 0x3a, 0x74, 0x69, 0x6d, 0x26, 0x3d, 0xfa, 0x4f, 0x5c, 0xbc, 0x65, 0x65, 0x65,
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, 0x65, 0x65, 0x65,
}; };
Block<BaseAction> actual = Block<BaseAction>.FromBencodex(encoded); Block<BaseAction> actual = Block<BaseAction>.FromBencodex(encoded);
Assert.Equal(_fx.HasTx, actual); Assert.Equal(_fx.HasTx, actual);
Expand Down
22 changes: 0 additions & 22 deletions Libplanet.Tests/Common/Action/DummyAction.cs

This file was deleted.

3 changes: 2 additions & 1 deletion Libplanet.Tests/Common/Action/Sleep.cs
Expand Up @@ -19,7 +19,8 @@ public class Sleep : BaseAction


public override IAccountStateDelta Execute(IActionContext context) public override IAccountStateDelta Execute(IActionContext context)
{ {
throw new NotSupportedException(); // No-op.
return context.PreviousStates;
} }


public override void LoadPlainValue( public override void LoadPlainValue(
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Tests/Libplanet.Tests.csproj
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net461</TargetFramework> <TargetFramework>net461</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Net/SwarmTest.cs
Expand Up @@ -423,7 +423,7 @@ public async Task CanGetTx()


Transaction<BaseAction> tx = Transaction<BaseAction>.Make( Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
new PrivateKey(), new PrivateKey(),
new PrivateKey().PublicKey.ToAddress(), ImmutableHashSet<Address>.Empty,
new BaseAction[] { }, new BaseAction[] { },
DateTimeOffset.UtcNow DateTimeOffset.UtcNow
); );
Expand Down Expand Up @@ -469,7 +469,7 @@ public async Task CanBroadcastTx()


Transaction<BaseAction> tx = Transaction<BaseAction>.Make( Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
new PrivateKey(), new PrivateKey(),
new PrivateKey().PublicKey.ToAddress(), ImmutableHashSet<Address>.Empty,
new BaseAction[] { }, new BaseAction[] { },
DateTimeOffset.UtcNow DateTimeOffset.UtcNow
); );
Expand Down
39 changes: 23 additions & 16 deletions Libplanet.Tests/Store/AddressTransactionSetTest.cs
Expand Up @@ -56,26 +56,33 @@ public void CanIterateItems()
Assert.Empty(_set.Keys); Assert.Empty(_set.Keys);
Assert.Empty(_set.Values); Assert.Empty(_set.Values);


_fx.Store.AppendAddressTransactionId( foreach (Address a in _fx.Transaction1.UpdatedAddresses)
_fx.StoreNamespace, {
_fx.Transaction1.Recipient, _fx.Store.AppendAddressTransactionId(
_fx.Transaction1.Id _fx.StoreNamespace,
); a,
_fx.Store.AppendAddressTransactionId( _fx.Transaction1.Id
_fx.StoreNamespace, );
_fx.Transaction2.Recipient, }
_fx.Transaction2.Id
); foreach (Address a in _fx.Transaction2.UpdatedAddresses)
{
_fx.Store.AppendAddressTransactionId(
_fx.StoreNamespace,
a,
_fx.Transaction2.Id
);
}

_fx.Store.PutTransaction(_fx.StoreNamespace, _fx.Transaction1); _fx.Store.PutTransaction(_fx.StoreNamespace, _fx.Transaction1);
_fx.Store.PutTransaction(_fx.StoreNamespace, _fx.Transaction2); _fx.Store.PutTransaction(_fx.StoreNamespace, _fx.Transaction2);


Assert.Equal( Assert.Equal(
new HashSet<Address>() _fx.Transaction1.UpdatedAddresses.Union(
{ _fx.Transaction2.UpdatedAddresses
_fx.Transaction1.Recipient, ),
_fx.Transaction2.Recipient, _set.Keys.ToHashSet()
}, );
_set.Keys.ToHashSet());
} }


[Fact] [Fact]
Expand Down
18 changes: 11 additions & 7 deletions Libplanet.Tests/Store/FileStoreFixture.cs
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using Libplanet.Blocks; using Libplanet.Blocks;
Expand Down Expand Up @@ -77,8 +78,8 @@ public FileStoreFixture()
Block2 = TestUtils.MineNext(Block1); Block2 = TestUtils.MineNext(Block1);
Block3 = TestUtils.MineNext(Block2); Block3 = TestUtils.MineNext(Block2);


Transaction1 = MakeTransaction(); Transaction1 = MakeTransaction(new List<BaseAction>(), ImmutableHashSet<Address>.Empty);
Transaction2 = MakeTransaction(); Transaction2 = MakeTransaction(new List<BaseAction>(), ImmutableHashSet<Address>.Empty);
} }


public string Path { get; } public string Path { get; }
Expand Down Expand Up @@ -121,15 +122,18 @@ public void Dispose()
} }
} }


private Transaction<BaseAction> MakeTransaction() public Transaction<BaseAction> MakeTransaction(
IEnumerable<BaseAction> actions = null,
ImmutableHashSet<Address> updatedAddresses = null
)
{ {
var privateKey = new PrivateKey(); var privateKey = new PrivateKey();
Address recipient = privateKey.PublicKey.ToAddress(); var timestamp =
var timestamp = new DateTimeOffset(2018, 11, 21, 0, 0, 0, TimeSpan.Zero); new DateTimeOffset(2018, 11, 21, 0, 0, 0, TimeSpan.Zero);
return Transaction<BaseAction>.Make( return Transaction<BaseAction>.Make(
privateKey, privateKey,
recipient, updatedAddresses ?? ImmutableHashSet<Address>.Empty,
new List<BaseAction>(), new List<BaseAction>(actions ?? new BaseAction[0]),
timestamp timestamp
); );
} }
Expand Down

0 comments on commit 31b87ae

Please sign in to comment.