Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blockchain UT for parallel TX #1310

Merged
merged 3 commits into from
Dec 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion tests/neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.SmartContract.Native.Tokens;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using System.Linq;
using System.Reflection;

namespace Neo.UnitTests.Ledger
{
Expand Down Expand Up @@ -34,7 +42,7 @@ public static TestHeader Cast(Header input)
}

[TestClass]
public class UT_Blockchain
public class UT_Blockchain : TestKit
{
private NeoSystem system;
Transaction txSample = Blockchain.GenesisBlock.Transactions[0];
Expand Down Expand Up @@ -96,5 +104,72 @@ public void TestGetTransaction()
Blockchain.Singleton.GetTransaction(UInt256.Zero).Should().BeNull();
Blockchain.Singleton.GetTransaction(txSample.Hash).Should().NotBeNull();
}

[TestMethod]
public void TestValidTransaction()
{
var senderProbe = CreateTestProbe();
var snapshot = Blockchain.Singleton.GetSnapshot();
var walletA = TestUtils.GenerateTestWallet();

using (var unlockA = walletA.Unlock("123"))
{
var acc = walletA.CreateAccount();

// Fake balance

var key = NativeContract.GAS.CreateStorageKey(20, acc.ScriptHash);
var entry = snapshot.Storages.GetAndChange(key, () => new StorageItem
{
Value = new Nep5AccountState().ToByteArray()
});

entry.Value = new Nep5AccountState()
{
Balance = 100_000_000 * NativeContract.GAS.Factor
}
.ToByteArray();

snapshot.Commit();

typeof(Blockchain)
.GetMethod("UpdateCurrentSnapshot", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(Blockchain.Singleton, null);

// Make transaction

var tx = CreateValidTx(walletA, acc.ScriptHash, 0);

senderProbe.Send(system.Blockchain, tx);
senderProbe.ExpectMsg(RelayResultReason.Succeed);

senderProbe.Send(system.Blockchain, tx);
senderProbe.ExpectMsg(RelayResultReason.AlreadyExists);
}
}

private Transaction CreateValidTx(NEP6Wallet wallet, UInt160 account, uint nonce)
{
var tx = wallet.MakeTransaction(new TransferOutput[]
{
new TransferOutput()
{
AssetId = NativeContract.GAS.Hash,
ScriptHash = account,
Value = new BigDecimal(1,8)
}
},
account);

tx.Nonce = nonce;

var data = new ContractParametersContext(tx);
Assert.IsTrue(wallet.Sign(data));
Assert.IsTrue(data.Completed);

tx.Witnesses = data.GetWitnesses();

return tx;
}
}
}