Skip to content

Commit

Permalink
Merge branch 'master' into fix_p2p_disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
lock9 committed Dec 2, 2019
2 parents 7821961 + e552255 commit 47e0935
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class NeoSystem : IDisposable
public NeoSystem(string storageEngine = null)
{
Plugin.LoadPlugins(this);
this.store = storageEngine is null ? new MemoryStore() : Plugin.Storages[storageEngine].GetStore();
this.store = string.IsNullOrEmpty(storageEngine) || storageEngine == nameof(MemoryStore)
? new MemoryStore()
: Plugin.Storages[storageEngine].GetStore();
this.Blockchain = ActorSystem.ActorOf(Ledger.Blockchain.Props(this, store));
this.LocalNode = ActorSystem.ActorOf(Network.P2P.LocalNode.Props(this));
this.TaskManager = ActorSystem.ActorOf(Network.P2P.TaskManager.Props(this));
Expand Down
6 changes: 5 additions & 1 deletion src/neo/Network/P2P/Payloads/InvPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public static InvPayload Create(InventoryType type, params UInt256[] hashes)
public static IEnumerable<InvPayload> CreateGroup(InventoryType type, UInt256[] hashes)
{
for (int i = 0; i < hashes.Length; i += MaxHashesCount)
{
int endIndex = i + MaxHashesCount;
if (endIndex > hashes.Length) endIndex = hashes.Length;
yield return new InvPayload
{
Type = type,
Hashes = hashes[i..(i + MaxHashesCount)]
Hashes = hashes[i..endIndex]
};
}
}

void ISerializable.Deserialize(BinaryReader reader)
Expand Down
83 changes: 76 additions & 7 deletions 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 All @@ -46,12 +54,6 @@ public void Initialize()
Blockchain.Singleton.MemPool.TryAdd(txSample.Hash, txSample);
}

[TestMethod]
public void TestConstructor()
{
system.ActorSystem.ActorOf(Blockchain.Props(system, Blockchain.Singleton.Store)).Should().NotBeSameAs(system.Blockchain);
}

[TestMethod]
public void TestContainsBlock()
{
Expand Down Expand Up @@ -96,5 +98,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;
}
}
}

0 comments on commit 47e0935

Please sign in to comment.