Skip to content

Commit

Permalink
Remove some SYSCALLs
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang committed Mar 21, 2020
1 parent a056f25 commit 0e11137
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 222 deletions.
26 changes: 1 addition & 25 deletions src/neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Array = Neo.VM.Types.Array;

namespace Neo.Network.P2P.Payloads
{
public class Block : BlockBase, IInventory, IEquatable<Block>, IInteroperable
public class Block : BlockBase, IInventory, IEquatable<Block>
{
public const int MaxContentsPerBlock = ushort.MaxValue;
public const int MaxTransactionsPerBlock = MaxContentsPerBlock - 1;
Expand Down Expand Up @@ -133,25 +129,5 @@ public TrimmedBlock Trim()
ConsensusData = ConsensusData
};
}

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Array(referenceCounter, new StackItem[]
{
// Computed properties
Hash.ToArray(),

// BlockBase properties
Version,
PrevHash.ToArray(),
MerkleRoot.ToArray(),
Timestamp,
Index,
NextConsensus.ToArray(),

// Block properties
Transactions.Length
});
}
}
}
75 changes: 0 additions & 75 deletions src/neo/SmartContract/InteropService.Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Numerics;

namespace Neo.SmartContract
{
Expand All @@ -12,10 +9,6 @@ partial class InteropService
public static class Blockchain
{
public static readonly InteropDescriptor GetHeight = Register("System.Blockchain.GetHeight", Blockchain_GetHeight, 0_00000400, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetBlock = Register("System.Blockchain.GetBlock", Blockchain_GetBlock, 0_02500000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetTransaction = Register("System.Blockchain.GetTransaction", Blockchain_GetTransaction, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetTransactionHeight = Register("System.Blockchain.GetTransactionHeight", Blockchain_GetTransactionHeight, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetTransactionFromBlock = Register("System.Blockchain.GetTransactionFromBlock", Blockchain_GetTransactionFromBlock, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetContract = Register("System.Blockchain.GetContract", Blockchain_GetContract, 0_01000000, TriggerType.Application, CallFlags.None);

private static bool Blockchain_GetHeight(ApplicationEngine engine)
Expand All @@ -24,74 +17,6 @@ private static bool Blockchain_GetHeight(ApplicationEngine engine)
return true;
}

private static bool Blockchain_GetBlock(ApplicationEngine engine)
{
ReadOnlySpan<byte> data = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
UInt256 hash;
if (data.Length <= 5)
hash = Ledger.Blockchain.Singleton.GetBlockHash((uint)new BigInteger(data));
else if (data.Length == 32)
hash = new UInt256(data);
else
return false;

Block block = hash != null ? engine.Snapshot.GetBlock(hash) : null;
if (block == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(block.ToStackItem(engine.ReferenceCounter));
return true;
}

private static bool Blockchain_GetTransaction(ApplicationEngine engine)
{
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
Transaction tx = engine.Snapshot.GetTransaction(new UInt256(hash));
if (tx == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem(engine.ReferenceCounter));
return true;
}

private static bool Blockchain_GetTransactionHeight(ApplicationEngine engine)
{
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
var tx = engine.Snapshot.Transactions.TryGet(new UInt256(hash));
engine.CurrentContext.EvaluationStack.Push(tx != null ? new BigInteger(tx.BlockIndex) : BigInteger.MinusOne);
return true;
}

private static bool Blockchain_GetTransactionFromBlock(ApplicationEngine engine)
{
ReadOnlySpan<byte> data = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
UInt256 hash;
if (data.Length <= 5)
hash = Ledger.Blockchain.Singleton.GetBlockHash((uint)new BigInteger(data));
else if (data.Length == 32)
hash = new UInt256(data);
else
return false;

TrimmedBlock block = hash != null ? engine.Snapshot.Blocks.TryGet(hash) : null;
if (block == null)
{
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
}
else
{
int index = (int)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();
if (index < 0 || index >= block.Hashes.Length - 1) return false;

Transaction tx = engine.Snapshot.GetTransaction(block.Hashes[index + 1]);
if (tx == null)
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
else
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem(engine.ReferenceCounter));
}
return true;
}

private static bool Blockchain_GetContract(ApplicationEngine engine)
{
UInt160 hash = new UInt160(engine.CurrentContext.EvaluationStack.Pop().GetSpan());
Expand Down
48 changes: 0 additions & 48 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,54 +402,6 @@ public void TestBlockchain_GetHeight()
engine.CurrentContext.EvaluationStack.Pop().GetBigInteger().Should().Be(0);
}

[TestMethod]
public void TestBlockchain_GetBlock()
{
var engine = GetEngine(true, true);

engine.CurrentContext.EvaluationStack.Push(new byte[] { 0x01 });
InteropService.Invoke(engine, InteropService.Blockchain.GetBlock).Should().BeTrue();
engine.CurrentContext.EvaluationStack.Pop().Should().Be(StackItem.Null);

byte[] data1 = new byte[] { 0x01, 0x01, 0x01 ,0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
engine.CurrentContext.EvaluationStack.Push(data1);
InteropService.Invoke(engine, InteropService.Blockchain.GetBlock).Should().BeTrue();
engine.CurrentContext.EvaluationStack.Pop().ToBoolean().Should().BeFalse();

byte[] data2 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
engine.CurrentContext.EvaluationStack.Push(data2);
InteropService.Invoke(engine, InteropService.Blockchain.GetBlock).Should().BeFalse();
}

[TestMethod]
public void TestBlockchain_GetTransaction()
{
var engine = GetEngine(true, true);
byte[] data1 = new byte[] { 0x01, 0x01, 0x01 ,0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
engine.CurrentContext.EvaluationStack.Push(data1);
InteropService.Invoke(engine, InteropService.Blockchain.GetTransaction).Should().BeTrue();
engine.CurrentContext.EvaluationStack.Pop().ToBoolean().Should().BeFalse();
}

[TestMethod]
public void TestBlockchain_GetTransactionHeight()
{
var engine = GetEngine(true, true);
byte[] data1 = new byte[] { 0x01, 0x01, 0x01 ,0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
engine.CurrentContext.EvaluationStack.Push(data1);
InteropService.Invoke(engine, InteropService.Blockchain.GetTransactionHeight).Should().BeTrue();
engine.CurrentContext.EvaluationStack.Pop().GetBigInteger().Should().Be(-1);
}

[TestMethod]
public void TestBlockchain_GetContract()
{
Expand Down
74 changes: 0 additions & 74 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,6 @@ public void TestSetup()
TestBlockchain.InitializeMockNeoSystem();
}

[TestMethod]
public void System_Blockchain_GetBlock()
{
var tx = new Transaction()
{
Script = new byte[] { 0x01 },
Attributes = new TransactionAttribute[0],
Cosigners = new Cosigner[0],
NetworkFee = 0x02,
SystemFee = 0x03,
Nonce = 0x04,
ValidUntilBlock = 0x05,
Version = 0x06,
Witnesses = new Witness[] { new Witness() { VerificationScript = new byte[] { 0x07 } } },
Sender = UInt160.Parse("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
};

var block = new Block()
{
Index = 1,
Timestamp = 2,
Version = 3,
Witness = new Witness()
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
},
PrevHash = UInt256.Zero,
MerkleRoot = UInt256.Zero,
NextConsensus = UInt160.Zero,
ConsensusData = new ConsensusData() { Nonce = 1, PrimaryIndex = 1 },
Transactions = new Transaction[] { tx }
};

var snapshot = Blockchain.Singleton.GetSnapshot();

using (var script = new ScriptBuilder())
{
script.EmitPush(block.Hash.ToArray());
script.EmitSysCall(InteropService.Blockchain.GetBlock);

// Without block

var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsTrue(engine.ResultStack.Peek().IsNull);

// With block

var blocks = snapshot.Blocks;
var txs = snapshot.Transactions;
blocks.Add(block.Hash, block.Trim());
txs.Add(tx.Hash, new TransactionState() { Transaction = tx, BlockIndex = block.Index, VMState = VMState.HALT });

script.EmitSysCall(InteropService.Json.Serialize);
engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteArray));
Assert.AreEqual(engine.ResultStack.Pop().GetSpan().ToHexString(),
"5b22556168352f4b6f446d39723064555950636353714346745a30594f726b583164646e7334366e676e3962383d222c332c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c322c312c224141414141414141414141414141414141414141414141414141413d222c315d");
Assert.AreEqual(0, engine.ResultStack.Count);

// Clean
blocks.Delete(block.Hash);
txs.Delete(tx.Hash);
}
}

[TestMethod]
public void Json_Deserialize()
{
Expand Down

0 comments on commit 0e11137

Please sign in to comment.