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

Allow to obtain the current tx #1432

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions src/neo/SmartContract/InteropService.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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 GetCurrentTransaction = Register("System.Blockchain.GetCurrentTransaction", Blockchain_GetCurrentTransaction, 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);
Expand Down Expand Up @@ -43,6 +44,15 @@ private static bool Blockchain_GetBlock(ApplicationEngine engine)
return true;
}

private static bool Blockchain_GetCurrentTransaction(ApplicationEngine engine)
{
if (engine.ScriptContainer is Transaction tx)
engine.CurrentContext.EvaluationStack.Push(tx.ToStackItem(engine.ReferenceCounter));
else
engine.CurrentContext.EvaluationStack.Push(StackItem.Null);
return true;
}
shargon marked this conversation as resolved.
Show resolved Hide resolved

private static bool Blockchain_GetTransaction(ApplicationEngine engine)
{
ReadOnlySpan<byte> hash = engine.CurrentContext.EvaluationStack.Pop().GetSpan();
Expand Down
47 changes: 47 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,53 @@ public void System_Blockchain_GetBlock()
}
}

[TestMethod]
public void System_Blockchain_GetCurrentTransaction()
{
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 snapshot = Blockchain.Singleton.GetSnapshot();

using (var script = new ScriptBuilder())
{
script.EmitSysCall(InteropService.Blockchain.GetCurrentTransaction);

// Without tx

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 tx

script.EmitSysCall(InteropService.Json.Serialize);
engine = new ApplicationEngine(TriggerType.Application, tx, 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(),
"5b225c75303032426b53415959527a4c4b69685a676464414b50596f754655737a63544d7867445a6572584a3172784c37303d222c362c342c222f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f383d222c332c322c352c2241513d3d225d");
Assert.AreEqual(0, engine.ResultStack.Count);
}
}

[TestMethod]
public void Json_Deserialize()
{
Expand Down