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

Storage abstract #1249

Merged
merged 16 commits into from
Nov 27, 2019
2 changes: 1 addition & 1 deletion neo.UnitTests/Consensus/UT_Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart()
TestProbe subscriber = CreateTestProbe();
var mockWallet = new Mock<Wallet>();
mockWallet.Setup(p => p.GetAccount(It.IsAny<UInt160>())).Returns<UInt160>(p => new TestWalletAccount(p));
ConsensusContext context = new ConsensusContext(mockWallet.Object, TestBlockchain.GetStore());
ConsensusContext context = new ConsensusContext(mockWallet.Object, TestBlockchain.Store);

int timeIndex = 0;
var timeValues = new[] {
Expand Down
2 changes: 1 addition & 1 deletion neo.UnitTests/Consensus/UT_ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void TestSetup()
_validatorKeys[x] = new KeyPair(pk);
}

_context = new ConsensusContext(mockWallet.Object, TestBlockchain.GetStore())
_context = new ConsensusContext(mockWallet.Object, TestBlockchain.Store)
{
Validators = _validatorKeys.Select(u => u.PublicKey).ToArray()
};
Expand Down
5 changes: 3 additions & 2 deletions neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
Expand All @@ -8,12 +9,12 @@ namespace Neo.UnitTests.Extensions
{
public static class NativeContractExtensions
{
public static StackItem Call(this NativeContract contract, Neo.Persistence.Snapshot snapshot, string method, params ContractParameter[] args)
public static StackItem Call(this NativeContract contract, SnapshotView snapshot, string method, params ContractParameter[] args)
{
return Call(contract, snapshot, null, method, args);
}

public static StackItem Call(this NativeContract contract, Neo.Persistence.Snapshot snapshot, IVerifiable container, string method, params ContractParameter[] args)
public static StackItem Call(this NativeContract contract, SnapshotView snapshot, IVerifiable container, string method, params ContractParameter[] args)
{
var engine = new ApplicationEngine(TriggerType.Application, container, snapshot, 0, true);

Expand Down
9 changes: 5 additions & 4 deletions neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentAssertions;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
Expand Down Expand Up @@ -34,14 +35,14 @@ public ManualWitness(params UInt160[] hashForVerify)

public void DeserializeUnsigned(BinaryReader reader) { }

public UInt160[] GetScriptHashesForVerifying(Persistence.Snapshot snapshot) => _hashForVerify;
public UInt160[] GetScriptHashesForVerifying(View snapshot) => _hashForVerify;

public void Serialize(BinaryWriter writer) { }

public void SerializeUnsigned(BinaryWriter writer) { }
}

public static bool Transfer(this NativeContract contract, Persistence.Snapshot snapshot, byte[] from, byte[] to, BigInteger amount, bool signFrom)
public static bool Transfer(this NativeContract contract, SnapshotView snapshot, byte[] from, byte[] to, BigInteger amount, bool signFrom)
{
var engine = new ApplicationEngine(TriggerType.Application,
new ManualWitness(signFrom ? new UInt160(from) : null), snapshot, 0, true);
Expand Down Expand Up @@ -90,7 +91,7 @@ public static string[] SupportedStandards(this NativeContract contract)
.ToArray();
}

public static BigInteger TotalSupply(this NativeContract contract, Persistence.Snapshot snapshot)
public static BigInteger TotalSupply(this NativeContract contract, SnapshotView snapshot)
{
var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);

Expand All @@ -110,7 +111,7 @@ public static BigInteger TotalSupply(this NativeContract contract, Persistence.S
return (result as VM.Types.Integer).GetBigInteger();
}

public static BigInteger BalanceOf(this NativeContract contract, Persistence.Snapshot snapshot, byte[] account)
public static BigInteger BalanceOf(this NativeContract contract, SnapshotView snapshot, byte[] account)
{
var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);

Expand Down
6 changes: 3 additions & 3 deletions neo.UnitTests/IO/Caching/UT_DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class MyDataCache<TKey, TValue> : DataCache<TKey, TValue>
{
public Dictionary<TKey, TValue> InnerDict = new Dictionary<TKey, TValue>();

public override void DeleteInternal(TKey key)
protected override void DeleteInternal(TKey key)
{
InnerDict.Remove(key);
}
Expand All @@ -115,9 +115,9 @@ protected override void AddInternal(TKey key, TValue value)
InnerDict.Add(key, value);
}

protected override IEnumerable<KeyValuePair<TKey, TValue>> FindInternal(byte[] key_prefix)
protected override IEnumerable<(TKey, TValue)> FindInternal(byte[] key_prefix)
{
return InnerDict.Where(kvp => kvp.Key.ToArray().Take(key_prefix.Length).SequenceEqual(key_prefix));
return InnerDict.Where(kvp => kvp.Key.ToArray().Take(key_prefix.Length).SequenceEqual(key_prefix)).Select(p => (p.Key, p.Value));
}

protected override TValue GetInternal(TKey key)
Expand Down
10 changes: 5 additions & 5 deletions neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Neo.UnitTests.Ledger
{
internal class TestBlock : Block
{
public override bool Verify(Snapshot snapshot)
public override bool Verify(SnapshotView snapshot)
{
return true;
}
Expand All @@ -22,7 +22,7 @@ public static TestBlock Cast(Block input)

internal class TestHeader : Header
{
public override bool Verify(Snapshot snapshot)
public override bool Verify(SnapshotView snapshot)
{
return true;
}
Expand All @@ -37,14 +37,14 @@ public static TestHeader Cast(Header input)
public class UT_Blockchain
{
private NeoSystem system;
private Store store;
private IStore store;
Transaction txSample = Blockchain.GenesisBlock.Transactions[0];

[TestInitialize]
public void Initialize()
{
system = TestBlockchain.InitializeMockNeoSystem();
store = TestBlockchain.GetStore();
system = TestBlockchain.TheNeoSystem;
store = TestBlockchain.Store;
Blockchain.Singleton.MemPool.TryAdd(txSample.Hash, txSample);
}

Expand Down
73 changes: 12 additions & 61 deletions neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Moq;
using Neo.Cryptography;
using Neo.IO;
using Neo.IO.Caching;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
Expand Down Expand Up @@ -39,19 +38,19 @@ public void TestSetup()
// protect against external changes on TimeProvider
TimeProvider.ResetToDefault();

NeoSystem TheNeoSystem = TestBlockchain.InitializeMockNeoSystem();
TestBlockchain.InitializeMockNeoSystem();

// Create a MemoryPool with capacity of 100
_unit = new MemoryPool(TheNeoSystem, 100);
_unit.LoadPolicy(TestBlockchain.GetStore().GetSnapshot());
_unit = new MemoryPool(TestBlockchain.TheNeoSystem, 100);
_unit.LoadPolicy(Blockchain.Singleton.GetSnapshot());

// Verify capacity equals the amount specified
_unit.Capacity.Should().Be(100);

_unit.VerifiedCount.Should().Be(0);
_unit.UnVerifiedCount.Should().Be(0);
_unit.Count.Should().Be(0);
_unit2 = new MemoryPool(TheNeoSystem, 0);
_unit2 = new MemoryPool(TestBlockchain.TheNeoSystem, 0);
plugin = new TestIMemoryPoolTxObserverPlugin();
}

Expand All @@ -74,8 +73,8 @@ private Transaction CreateTransactionWithFee(long fee)
var randomBytes = new byte[16];
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
mock.Setup(p => p.Reverify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(true);
mock.Setup(p => p.Verify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(true);
mock.Setup(p => p.Reverify(It.IsAny<SnapshotView>(), It.IsAny<BigInteger>())).Returns(true);
mock.Setup(p => p.Verify(It.IsAny<SnapshotView>(), It.IsAny<BigInteger>())).Returns(true);
mock.Object.Script = randomBytes;
mock.Object.Sender = UInt160.Zero;
mock.Object.NetworkFee = fee;
Expand Down Expand Up @@ -343,10 +342,8 @@ public void TestGetVerifiedTransactions()
[TestMethod]
public void TestReVerifyTopUnverifiedTransactionsIfNeeded()
{
NeoSystem TheNeoSystem = TestBlockchain.InitializeMockNeoSystem();
var s = Blockchain.Singleton.Height;
_unit = new MemoryPool(TheNeoSystem, 600);
_unit.LoadPolicy(TestBlockchain.GetStore().GetSnapshot());
_unit = new MemoryPool(TestBlockchain.TheNeoSystem, 600);
_unit.LoadPolicy(Blockchain.Singleton.GetSnapshot());
AddTransaction(CreateTransaction(100000001));
AddTransaction(CreateTransaction(100000001));
AddTransaction(CreateTransaction(100000001));
Expand Down Expand Up @@ -406,7 +403,7 @@ public void TestTryGetValue()
[TestMethod]
public void TestUpdatePoolForBlockPersisted()
{
var mockSnapshot = new Mock<Snapshot>();
var snapshot = Blockchain.Singleton.GetSnapshot();
byte[] transactionsPerBlock = { 0x18, 0x00, 0x00, 0x00 }; // 24
byte[] feePerByte = { 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00 }; // 1048576
StorageItem item1 = new StorageItem
Expand All @@ -417,14 +414,12 @@ public void TestUpdatePoolForBlockPersisted()
{
Value = feePerByte
};
var myDataCache = new MyDataCache<StorageKey, StorageItem>();
var key1 = CreateStorageKey(Prefix_MaxTransactionsPerBlock);
var key2 = CreateStorageKey(Prefix_FeePerByte);
key1.ScriptHash = NativeContract.Policy.Hash;
key2.ScriptHash = NativeContract.Policy.Hash;
myDataCache.Add(key1, item1);
myDataCache.Add(key2, item2);
mockSnapshot.SetupGet(p => p.Storages).Returns(myDataCache);
snapshot.Storages.Add(key1, item1);
snapshot.Storages.Add(key2, item2);

var tx1 = CreateTransaction();
var tx2 = CreateTransaction();
Expand All @@ -436,7 +431,7 @@ public void TestUpdatePoolForBlockPersisted()
_unit.UnVerifiedCount.Should().Be(0);
_unit.VerifiedCount.Should().Be(1);

_unit.UpdatePoolForBlockPersisted(block, mockSnapshot.Object);
_unit.UpdatePoolForBlockPersisted(block, snapshot);

_unit.UnVerifiedCount.Should().Be(0);
_unit.VerifiedCount.Should().Be(0);
Expand All @@ -455,48 +450,4 @@ public StorageKey CreateStorageKey(byte prefix, byte[] key = null)
return storageKey;
}
}

public class MyDataCache<TKey, TValue> : DataCache<TKey, TValue>
where TKey : IEquatable<TKey>, ISerializable
where TValue : class, ICloneable<TValue>, ISerializable, new()
{
private readonly TValue _defaultValue;

public MyDataCache()
{
_defaultValue = null;
}

public MyDataCache(TValue defaultValue)
{
this._defaultValue = defaultValue;
}
public override void DeleteInternal(TKey key)
{
}

protected override void AddInternal(TKey key, TValue value)
{
Add(key, value);
}

protected override IEnumerable<KeyValuePair<TKey, TValue>> FindInternal(byte[] key_prefix)
{
return Enumerable.Empty<KeyValuePair<TKey, TValue>>();
}

protected override TValue GetInternal(TKey key)
{
return TryGet(key);
}

protected override TValue TryGetInternal(TKey key)
{
return _defaultValue;
}

protected override void UpdateInternal(TKey key, TValue value)
{
}
}
}
4 changes: 2 additions & 2 deletions neo.UnitTests/Ledger/UT_SendersFeeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ private Transaction CreateTransactionWithFee(long networkFee, long systemFee)
var randomBytes = new byte[16];
random.NextBytes(randomBytes);
Mock<Transaction> mock = new Mock<Transaction>();
mock.Setup(p => p.Reverify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(true);
mock.Setup(p => p.Verify(It.IsAny<Snapshot>(), It.IsAny<BigInteger>())).Returns(true);
mock.Setup(p => p.Reverify(It.IsAny<SnapshotView>(), It.IsAny<BigInteger>())).Returns(true);
mock.Setup(p => p.Verify(It.IsAny<SnapshotView>(), It.IsAny<BigInteger>())).Returns(true);
mock.Object.Script = randomBytes;
mock.Object.Sender = UInt160.Zero;
mock.Object.NetworkFee = networkFee;
Expand Down
10 changes: 5 additions & 5 deletions neo.UnitTests/Ledger/UT_TrimmedBlock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FluentAssertions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void TestGetIsBlock()
[TestMethod]
public void TestGetBlock()
{
var cache = new TestDataCache<UInt256, TransactionState>();
var snapshot = Blockchain.Singleton.GetSnapshot();
var tx1 = TestUtils.GetTransaction();
tx1.Script = new byte[] { 0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,
Expand All @@ -63,12 +63,12 @@ public void TestGetBlock()
Transaction = tx2,
BlockIndex = 1
};
cache.Add(tx1.Hash, state1);
cache.Add(tx2.Hash, state2);
snapshot.Transactions.Add(tx1.Hash, state1);
snapshot.Transactions.Add(tx2.Hash, state2);

TrimmedBlock tblock = GetTrimmedBlockWithNoTransaction();
tblock.Hashes = new UInt256[] { tx1.Hash, tx2.Hash };
Block block = tblock.GetBlock(cache);
Block block = tblock.GetBlock(snapshot.Transactions);

block.Index.Should().Be(1);
block.MerkleRoot.Should().Be(UInt256.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff02"));
Expand Down
Loading