Skip to content

Commit

Permalink
Automatically fill the Transaction<T>.UpdatedAddresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Mar 14, 2019
1 parent a069f98 commit 5622f3c
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 105 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,10 @@ To be released.
`IActionContext.From` properties to `Signer`.
The corresponding parameter names on constructors and
methods were also renamed too.
- Old `Transaction<T>.Make()` factory method is replaced by
new `Transaction<T>.Create()` factory method. The `timestamp` parameter
became optional, and the new optional `updatedAddresses` parameter was
added.
- Removed `IActionContext.To` property.
- Added `AccountStateGetter` delegate to provide a read-only view to
account states.
Expand Down
22 changes: 6 additions & 16 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Expand Up @@ -109,11 +109,9 @@ public void ProcessActions()
TargetAddress = _fx.Address1,
},
};
Transaction<BaseAction> tx1 = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx1 = Transaction<BaseAction>.Create(
new PrivateKey(),
ImmutableHashSet.Create(_fx.Address1),
actions1,
DateTimeOffset.UtcNow
actions1
);

_blockChain.StageTransactions(new HashSet<Transaction<BaseAction>> { tx1 });
Expand All @@ -137,11 +135,9 @@ public void ProcessActions()
TargetAddress = _fx.Address1,
},
};
Transaction<BaseAction> tx2 = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx2 = Transaction<BaseAction>.Create(
new PrivateKey(),
ImmutableHashSet.Create(_fx.Address1),
actions2,
DateTimeOffset.UtcNow
actions2
);

_blockChain.StageTransactions(new HashSet<Transaction<BaseAction>> { tx2 });
Expand Down Expand Up @@ -266,15 +262,9 @@ public void EvaluateActions()
long blockIndex = 0;

TestEvaluateAction action = new TestEvaluateAction();
Transaction<BaseAction> tx1 = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx1 = Transaction<BaseAction>.Create(
fromPrivateKey,
new[]
{
TestEvaluateAction.SignerKey,
TestEvaluateAction.BlockIndexKey,
}.ToImmutableHashSet(),
new[] { action },
DateTimeOffset.UtcNow
new[] { action }
);

_blockChain.StageTransactions(new HashSet<Transaction<BaseAction>> { tx1 });
Expand Down
52 changes: 31 additions & 21 deletions Libplanet.Tests/Blocks/BlockTest.cs
Expand Up @@ -186,21 +186,17 @@ public void EvaluateActions()
genesis,
new[]
{
Transaction<BaseAction>.Make(
Transaction<BaseAction>.Create(
_fx.TxFixture.PrivateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[]
{
MakeAction(addresses[0], 'A'),
MakeAction(addresses[1], 'B'),
},
DateTimeOffset.UtcNow
}
),
Transaction<BaseAction>.Make(
Transaction<BaseAction>.Create(
_fx.TxFixture.PrivateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[] { MakeAction(addresses[2], 'C') },
DateTimeOffset.UtcNow
new BaseAction[] { MakeAction(addresses[2], 'C') }
),
}
);
Expand Down Expand Up @@ -233,17 +229,13 @@ public void EvaluateActions()
blockIdx1,
new[]
{
Transaction<BaseAction>.Make(
Transaction<BaseAction>.Create(
_fx.TxFixture.PrivateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[] { MakeAction(addresses[0], 'D') },
DateTimeOffset.UtcNow
new BaseAction[] { MakeAction(addresses[0], 'D') }
),
Transaction<BaseAction>.Make(
Transaction<BaseAction>.Create(
_fx.TxFixture.PrivateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[] { MakeAction(addresses[3], 'E') },
DateTimeOffset.UtcNow
new BaseAction[] { MakeAction(addresses[3], 'E') }
),
}
);
Expand Down Expand Up @@ -345,11 +337,29 @@ public void ValidateInvalidTxPublicKey()
[Fact]
public void ValidateInvalidTxUpdatedAddresses()
{
Transaction<BaseAction> invalidTx = Transaction<BaseAction>.Make(
_fx.TxFixture.PrivateKey,
ImmutableHashSet<Address>.Empty,
_fx.TxFixture.TxWithActions.Actions,
DateTimeOffset.UtcNow
ImmutableArray<IDictionary<string, object>> rawActions =
_fx.TxFixture.TxWithActions
.ToRawTransaction(false).Actions.ToImmutableArray();
RawTransaction rawTxWithoutSig = new RawTransaction(
_fx.TxFixture.Address.ToByteArray(),
new byte[][] { },
_fx.TxFixture.PublicKey.Format(false),
DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.ffffffZ"),
rawActions,
new byte[0]
);
byte[] sig = _fx.TxFixture.PrivateKey.Sign(
new Transaction<BaseAction>(rawTxWithoutSig).ToBencodex(false)
);
var invalidTx = new Transaction<BaseAction>(
new RawTransaction(
rawTxWithoutSig.Signer,
rawTxWithoutSig.UpdatedAddresses,
rawTxWithoutSig.PublicKey,
rawTxWithoutSig.Timestamp,
rawTxWithoutSig.Actions,
sig
)
);
Block<BaseAction> invalidBlock = MineNext(
_fx.Next,
Expand Down
6 changes: 5 additions & 1 deletion Libplanet.Tests/Common/Action/Sleep.cs
Expand Up @@ -26,7 +26,11 @@ public override IAccountStateDelta Execute(IActionContext context)
public override void LoadPlainValue(
IImmutableDictionary<string, object> plainValue)
{
ZoneId = (int)(BigInteger)plainValue["zone_id"];
object serialized = plainValue["zone_id"];

// FIXME: The reason why the type of the serialized value is not
// consistent should be analyzed.
ZoneId = serialized is BigInteger v ? (int)v : (int)serialized;
}
}
}
16 changes: 6 additions & 10 deletions Libplanet.Tests/Net/SwarmTest.cs
Expand Up @@ -413,19 +413,17 @@ public async Task CanGetBlock()
}

[Fact]
public async Task CanGetTx()
public async Task GetTx()
{
Swarm swarmA = _swarms[0];
Swarm swarmB = _swarms[1];

BlockChain<BaseAction> chainA = _blockchains[0];
BlockChain<BaseAction> chainB = _blockchains[1];

Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
new PrivateKey(),
ImmutableHashSet<Address>.Empty,
new BaseAction[] { },
DateTimeOffset.UtcNow
new BaseAction[0]
);
chainB.StageTransactions(new[] { tx }.ToHashSet());
chainB.MineBlock(_fx1.Address1);
Expand Down Expand Up @@ -457,7 +455,7 @@ public async Task CanGetTx()
}

[Fact]
public async Task CanBroadcastTx()
public async Task BroadcastTx()
{
Swarm swarmA = _swarms[0];
Swarm swarmB = _swarms[1];
Expand All @@ -467,11 +465,9 @@ public async Task CanBroadcastTx()
BlockChain<BaseAction> chainB = _blockchains[1];
BlockChain<BaseAction> chainC = _blockchains[2];

Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
new PrivateKey(),
ImmutableHashSet<Address>.Empty,
new BaseAction[] { },
DateTimeOffset.UtcNow
new BaseAction[] { }
);

chainA.StageTransactions(new[] { tx }.ToHashSet());
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Tests/Store/FileStoreFixture.cs
Expand Up @@ -130,10 +130,10 @@ public void Dispose()
var privateKey = new PrivateKey();
var timestamp =
new DateTimeOffset(2018, 11, 21, 0, 0, 0, TimeSpan.Zero);
return Transaction<BaseAction>.Make(
return Transaction<BaseAction>.Create(
privateKey,
updatedAddresses ?? ImmutableHashSet<Address>.Empty,
actions ?? new BaseAction[0],
updatedAddresses,
timestamp
);
}
Expand Down
95 changes: 69 additions & 26 deletions Libplanet.Tests/Tx/TransactionTest.cs
Expand Up @@ -22,7 +22,7 @@ public TransactionTest()
}

[Fact]
public void Make()
public void Create()
{
var privateKey = new PrivateKey(
new byte[]
Expand All @@ -33,11 +33,12 @@ public void Make()
0x7b, 0x76,
}
);
var timestamp = new DateTimeOffset(2018, 11, 21, 0, 0, 0, TimeSpan.Zero);
Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
var timestamp =
new DateTimeOffset(2018, 11, 21, 0, 0, 0, TimeSpan.Zero);
Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
privateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[0],
ImmutableHashSet<Address>.Empty,
timestamp
);

Expand Down Expand Up @@ -73,24 +74,72 @@ public void Make()
),
tx.Id
);
}

[Fact]
public void CreateWithDefaultUpdatedAddresses()
{
Transaction<BaseAction> emptyTx = Transaction<BaseAction>.Create(
_fx.PrivateKey,
new BaseAction[0]
);
Assert.Empty(emptyTx.UpdatedAddresses);

Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
_fx.PrivateKey,
_fx.TxWithActions.Actions
);
Assert.Equal(
new[] { _fx.Address }.ToImmutableHashSet(),
tx.UpdatedAddresses
);

Address additionalAddr = new PrivateKey().PublicKey.ToAddress();
Transaction<BaseAction> txWithAddr = Transaction<BaseAction>.Create(
_fx.PrivateKey,
_fx.TxWithActions.Actions,
new[] { additionalAddr }.ToImmutableHashSet()
);
Assert.Equal(
new[] { _fx.Address, additionalAddr }.ToHashSet(),
txWithAddr.UpdatedAddresses.ToHashSet()
);
}

[Fact]
public void CreateWithDefaultTimestamp()
{
DateTimeOffset rightBefore = DateTimeOffset.UtcNow;
Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
_fx.PrivateKey,
new BaseAction[0],
ImmutableHashSet<Address>.Empty
);
DateTimeOffset rightAfter = DateTimeOffset.UtcNow;

Assert.InRange(tx.Timestamp, rightBefore, rightAfter);
}

[Fact]
public void CreateWithMissingRequiredArguments()
{
// The privateKey parameter cannot be null.
Assert.Throws<ArgumentNullException>(() =>
Transaction<BaseAction>.Make(
Transaction<BaseAction>.Create(
null,
ImmutableHashSet<Address>.Empty,
new BaseAction[0],
timestamp
ImmutableHashSet<Address>.Empty,
DateTimeOffset.UtcNow
)
);

// The actions parameter cannot be null.
Assert.Throws<ArgumentNullException>(() =>
Transaction<BaseAction>.Make(
privateKey,
ImmutableHashSet<Address>.Empty,
Transaction<BaseAction>.Create(
_fx.PrivateKey,
null,
timestamp
ImmutableHashSet<Address>.Empty,
DateTimeOffset.UtcNow
)
);
}
Expand Down Expand Up @@ -214,7 +263,7 @@ public void MakeWithSignature()
[Fact]
public void ToBencodex()
{
var expected = new byte[]
byte[] expected =
{
0x64, 0x37, 0x3a, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73,
0x6c, 0x65, 0x31, 0x30, 0x3a, 0x70, 0x75, 0x62, 0x6c, 0x69,
Expand Down Expand Up @@ -513,9 +562,8 @@ public void Evaluate()
new PrivateKey().PublicKey.ToAddress(),
new PrivateKey().PublicKey.ToAddress(),
};
Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
_fx.PrivateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[]
{
new Attack
Expand All @@ -536,8 +584,7 @@ public void Evaluate()
Target = "t2",
TargetAddress = addresses[0],
},
},
DateTimeOffset.UtcNow
}
);
IAccountStateDelta delta = tx.EvaluateActions(
default,
Expand Down Expand Up @@ -573,11 +620,10 @@ public void Validate()
}
);
var timestamp = new DateTimeOffset(2018, 11, 21, 0, 0, 0, TimeSpan.Zero);
Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
privateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[0],
timestamp
timestamp: timestamp
);

tx.Validate();
Expand Down Expand Up @@ -646,11 +692,10 @@ public void ConvertToRaw()
}
);
var timestamp = new DateTimeOffset(2018, 11, 21, 0, 0, 0, TimeSpan.Zero);
Transaction<BaseAction> tx = Transaction<BaseAction>.Make(
Transaction<BaseAction> tx = Transaction<BaseAction>.Create(
privateKey,
ImmutableHashSet<Address>.Empty,
new BaseAction[0],
timestamp
timestamp: timestamp
);

Assert.Equal(
Expand Down Expand Up @@ -706,11 +751,9 @@ public void SignatureBufferIsIsolated()
public void ActionsAreIsolatedFromOutside()
{
var actions = new List<BaseAction>();
Transaction<BaseAction> t1 = Transaction<BaseAction>.Make(
Transaction<BaseAction> t1 = Transaction<BaseAction>.Create(
_fx.PrivateKey,
ImmutableHashSet<Address>.Empty,
actions,
DateTimeOffset.UtcNow
actions
);
var t2 = new Transaction<BaseAction>(
_fx.PrivateKey.PublicKey.ToAddress(),
Expand Down

0 comments on commit 5622f3c

Please sign in to comment.