Skip to content
Permalink
Browse files

Automatically fill the Transaction<T>.UpdatedAddresses

  • Loading branch information...
dahlia committed Mar 14, 2019
1 parent 5f06d38 commit c4d712acbef0188fbcc17b64541188ab12d9af92
@@ -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.
@@ -106,11 +106,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 });
@@ -134,11 +132,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 });
@@ -263,15 +259,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 });
@@ -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') }
),
}
);
@@ -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') }
),
}
);
@@ -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,
@@ -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;
}
}
}
@@ -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);
@@ -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];
@@ -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());
@@ -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
);
}
@@ -22,7 +22,7 @@ public TransactionTest()
}

[Fact]
public void Make()
public void Create()
{
var privateKey = new PrivateKey(
new byte[]
@@ -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
);

@@ -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
)
);
}
@@ -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,
@@ -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
@@ -536,8 +584,7 @@ public void Evaluate()
Target = "t2",
TargetAddress = addresses[0],
},
},
DateTimeOffset.UtcNow
}
);
IAccountStateDelta delta = tx.EvaluateActions(
default,
@@ -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();
@@ -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(
@@ -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(),
Oops, something went wrong.

0 comments on commit c4d712a

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.