Skip to content

Commit

Permalink
Merge pull request #3480 from greymistcube/refactor/updated-addresses
Browse files Browse the repository at this point in the history
♻️ Disallow creation of new `Transaction`s with a non-empty `UpdatedAddresses`
  • Loading branch information
greymistcube authored Nov 15, 2023
2 parents 06d7357 + 7fd7327 commit 2129953
Show file tree
Hide file tree
Showing 26 changed files with 291 additions and 242 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ To be released.

### Backward-incompatible API changes

- Removed `updatedAddresses` parameter from `BlockChain.MakeTransaction()`
[[#3480]]
- Removed `updatedAddresses` parameter from `Transaction.Create()`. [[#3480]]
- Removed `updatedAddresses` parameter from all `TxInvoice()`. [[#3480]]
- Removed `Rehearsal` property from `IActionContext` and
`ICommittedActionContext`. [[#3485]]
- (Libplanet.Crypto) Removed `ToAddress()` extension method for
Expand All @@ -31,6 +35,7 @@ To be released.

### CLI tools

[#3480]: https://github.com/planetarium/libplanet/pull/3480
[#3485]: https://github.com/planetarium/libplanet/pull/3485
[#3486]: https://github.com/planetarium/libplanet/pull/3486

Expand Down
24 changes: 14 additions & 10 deletions Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,20 @@ private Transaction
var random = new System.Random(seed);
var addr = pk.Address;
var bal = (int)(Chain.GetBalance(addr, TestCurrency).MajorUnit & int.MaxValue);
return Transaction.Create(
nonce,
pk,
Chain.Genesis.Hash,
random.Next() % 2 == 0
? GetRandomActions(random.Next()).ToPlainValues()
: ImmutableHashSet<SimpleAction>.Empty.ToPlainValues(),
null,
null,
GetRandomAddresses(random.Next()));
return
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: Chain.Genesis.Hash,
updatedAddresses: GetRandomAddresses(random.Next()),
timestamp: DateTimeOffset.UtcNow,
actions: new TxActionList(random.Next() % 2 == 0
? GetRandomActions(random.Next()).ToPlainValues()
: ImmutableHashSet<SimpleAction>.Empty.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(pk.PublicKey, nonce)),
pk);
}

private ImmutableArray<SimpleAction> GetRandomActions(int seed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,6 @@ private Transaction DummyTransaction()
new[] { new Utils.DummyAction() }.ToPlainValues(),
null,
null,
null,
DateTimeOffset.UtcNow
);
DateTimeOffset.UtcNow);
}
}
2 changes: 0 additions & 2 deletions Libplanet.Net.Tests/Consensus/ContextNonProposerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Security.Cryptography;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -367,7 +366,6 @@ message is ConsensusPreCommitMsg commit &&
var unsignedInvalidTx = new UnsignedTx(
new TxInvoice(
blockChain.Genesis.Hash,
ImmutableHashSet<Address>.Empty,
DateTimeOffset.UtcNow,
new TxActionList((IValue)List.Empty.Add(new Text("Foo")))), // Invalid action
new TxSigningMetadata(txSigner.PublicKey, 0));
Expand Down
5 changes: 1 addition & 4 deletions Libplanet.Net.Tests/Consensus/ContextTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -362,9 +361,7 @@ void BroadcastMessage(ConsensusMsg message) =>
nonce: 0,
privateKey: TestUtils.PrivateKeys[1],
genesisHash: blockChain.Genesis.Hash,
actions: new[] { action }.ToPlainValues(),
updatedAddresses: ImmutableHashSet.Create(DelayAction.TrivialUpdatedAddress)
);
actions: new[] { action }.ToPlainValues());
blockChain.StageTransaction(tx);
var block = blockChain.ProposeBlock(TestUtils.PrivateKeys[1]);

Expand Down
4 changes: 1 addition & 3 deletions Libplanet.Net.Tests/SwarmTest.Preload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,7 @@ public async Task PreloadWithFailedActions()
new[] { action }.ToPlainValues(),
null,
null,
ImmutableHashSet<Address>.Empty,
DateTimeOffset.UtcNow
);
DateTimeOffset.UtcNow);

Block block = minerChain.ProposeBlock(
ChainPrivateKey,
Expand Down
178 changes: 109 additions & 69 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,50 @@ DumbAction MakeAction(Address address, char identifier, Address? transferTo = nu

Transaction[] block1Txs =
{
Transaction.Create(
nonce: 0,
privateKey: _txFx.PrivateKey1,
genesisHash: genesis.Hash,
actions: new[]
{
MakeAction(addresses[0], 'A', addresses[1]),
MakeAction(addresses[1], 'B', addresses[2]),
}.ToPlainValues(),
updatedAddresses: new[] { addresses[0], addresses[1] }.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(2)),
Transaction.Create(
nonce: 0,
privateKey: _txFx.PrivateKey2,
genesisHash: genesis.Hash,
actions: new[]
{
MakeAction(addresses[2], 'C', addresses[3]),
}.ToPlainValues(),
timestamp: DateTimeOffset.MinValue.AddSeconds(4)),
Transaction.Create(
nonce: 0,
privateKey: _txFx.PrivateKey3,
genesisHash: genesis.Hash,
actions: Array.Empty<DumbAction>().ToPlainValues(),
timestamp: DateTimeOffset.MinValue.AddSeconds(7)),
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: genesis.Hash,
updatedAddresses: new[]
{
addresses[0],
addresses[1],
}.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(2),
actions: new TxActionList(new[]
{
MakeAction(addresses[0], 'A', addresses[1]),
MakeAction(addresses[1], 'B', addresses[2]),
}.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(_txFx.PrivateKey1.PublicKey, 0)),
_txFx.PrivateKey1),
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: genesis.Hash,
updatedAddresses: ImmutableHashSet<Address>.Empty,
timestamp: DateTimeOffset.MinValue.AddSeconds(4),
actions: new TxActionList(new[]
{
MakeAction(addresses[2], 'C', addresses[3]),
}.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(_txFx.PrivateKey2.PublicKey, 0)),
_txFx.PrivateKey2),
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: genesis.Hash,
updatedAddresses: ImmutableHashSet<Address>.Empty,
timestamp: DateTimeOffset.MinValue.AddSeconds(7),
actions: TxActionList.Empty,
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(_txFx.PrivateKey3.PublicKey, 0)),
_txFx.PrivateKey3),
};
foreach ((var tx, var i) in block1Txs.Zip(
Enumerable.Range(0, block1Txs.Length), (x, y) => (x, y)))
Expand Down Expand Up @@ -384,37 +402,55 @@ DumbAction MakeAction(Address address, char identifier, Address? transferTo = nu
// Note that these timestamps in themselves does not have any meanings but are
// only arbitrary. These purpose to make their evaluation order in a block
// equal to the order we (the test) intend:
Transaction.Create(
0,
_txFx.PrivateKey1,
genesis.Hash,
new[] { MakeAction(addresses[0], 'D') }.ToPlainValues(),
updatedAddresses: new[] { addresses[0] }.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(1)),
Transaction.Create(
0,
_txFx.PrivateKey2,
genesis.Hash,
new[] { MakeAction(addresses[3], 'E') }.ToPlainValues(),
updatedAddresses: new[] { addresses[3] }.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(2)),
Transaction.Create(
0,
_txFx.PrivateKey3,
genesis.Hash,
new[]
{
new DumbAction(
addresses[4],
"RecordRehearsal",
transferFrom: addresses[0],
transferTo: addresses[4],
transferAmount: 8,
recordRehearsal: true,
recordRandom: true),
}.ToPlainValues(),
updatedAddresses: new[] { addresses[4] }.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(4)),
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: genesis.Hash,
updatedAddresses: new[] { addresses[0] }.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(1),
actions: new TxActionList(new[]
{
MakeAction(addresses[0], 'D'),
}.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(_txFx.PrivateKey1.PublicKey, 0)),
_txFx.PrivateKey1),
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: genesis.Hash,
updatedAddresses: new[] { addresses[3] }.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(2),
actions: new TxActionList(new[]
{
MakeAction(addresses[3], 'E'),
}.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(_txFx.PrivateKey2.PublicKey, 0)),
_txFx.PrivateKey2),
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: genesis.Hash,
updatedAddresses: new[] { addresses[4] }.ToImmutableHashSet(),
timestamp: DateTimeOffset.MinValue.AddSeconds(4),
actions: new TxActionList(new[]
{
new DumbAction(
addresses[4],
"RecordRehearsal",
transferFrom: addresses[0],
transferTo: addresses[4],
transferAmount: 8,
recordRehearsal: true,
recordRandom: true),
}.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(_txFx.PrivateKey3.PublicKey, 0)),
_txFx.PrivateKey3),
};
foreach ((var tx, var i) in block2Txs.Zip(
Enumerable.Range(0, block2Txs.Length), (x, y) => (x, y)))
Expand Down Expand Up @@ -623,7 +659,6 @@ public void EvaluateTxResultThrowingException()
new[] { action }.ToPlainValues(),
null,
null,
ImmutableHashSet<Address>.Empty,
DateTimeOffset.UtcNow);
var txs = new Transaction[] { tx };
var hash = new BlockHash(GetRandomBytes(BlockHash.Size));
Expand Down Expand Up @@ -838,17 +873,22 @@ public void OrderTxsForEvaluation(
.Select(signerNoncePair =>
{
Address targetAddress = signerNoncePair.signer.Address;
return Transaction.Create(
nonce: signerNoncePair.nonce,
privateKey: signerNoncePair.signer,
genesisHash: null,
actions: new[]
{
new RandomAction(signerNoncePair.signer.Address),
}.ToPlainValues(),
updatedAddresses: ImmutableHashSet.Create(targetAddress),
timestamp: epoch
);
return new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: null,
updatedAddresses: ImmutableHashSet.Create(targetAddress),
timestamp: epoch,
actions: new TxActionList(new[]
{
new RandomAction(signerNoncePair.signer.Address),
}.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(
signerNoncePair.signer.PublicKey,
signerNoncePair.nonce)),
signerNoncePair.signer);
}).ToImmutableArray();

// Rearrange transactions so that transactions are not grouped by signers
Expand Down
9 changes: 2 additions & 7 deletions Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,6 @@ public void CannotAppendBlockWithInvalidActions()
var unsignedInvalidTx = new UnsignedTx(
new TxInvoice(
_blockChain.Genesis.Hash,
ImmutableHashSet<Address>.Empty,
DateTimeOffset.UtcNow,
new TxActionList((IValue)List.Empty.Add(new Text("Foo")))), // Invalid action
new TxSigningMetadata(txSigner.PublicKey, 1));
Expand All @@ -640,17 +639,13 @@ public void CannotAppendBlockWithInvalidActions()
nonce: 0,
privateKey: txSigner,
genesisHash: _blockChain.Genesis.Hash,
actions: Array.Empty<DumbAction>().ToPlainValues(),
updatedAddresses: ImmutableHashSet<Address>.Empty
),
actions: Array.Empty<DumbAction>().ToPlainValues()),
invalidTx,
Transaction.Create(
nonce: 2,
privateKey: txSigner,
genesisHash: _blockChain.Genesis.Hash,
actions: Array.Empty<DumbAction>().ToPlainValues(),
updatedAddresses: ImmutableHashSet<Address>.Empty
),
actions: Array.Empty<DumbAction>().ToPlainValues()),
}.OrderBy(tx => tx.Id);

var metadata = new BlockMetadata(
Expand Down
4 changes: 1 addition & 3 deletions Libplanet.Tests/Blockchain/BlockChainTest.Internals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ Transaction MkTx(PrivateKey key, long nonce, DateTimeOffset? ts = null) =>
Array.Empty<DumbAction>().ToPlainValues(),
null,
null,
null,
ts ?? DateTimeOffset.UtcNow
);
ts ?? DateTimeOffset.UtcNow);

PrivateKey a = new PrivateKey();
PrivateKey b = new PrivateKey();
Expand Down
1 change: 0 additions & 1 deletion Libplanet.Tests/Blockchain/BlockChainTest.ProposeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ public void MarkTransactionsToIgnoreWhileProposing()
var unsignedInvalidTx = new UnsignedTx(
new TxInvoice(
_blockChain.Genesis.Hash,
ImmutableHashSet<Address>.Empty,
DateTimeOffset.UtcNow,
new TxActionList((IValue)List.Empty.Add(new Text("Foo")))), // Invalid action
new TxSigningMetadata(keyB.PublicKey, 1));
Expand Down
Loading

0 comments on commit 2129953

Please sign in to comment.