forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,256 additions
and
1,131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Neo.SmartContract | ||
{ | ||
partial class InteropService | ||
{ | ||
public static class Binary | ||
{ | ||
public static readonly InteropDescriptor Serialize = Register("System.Binary.Serialize", Binary_Serialize, 0_00100000, TriggerType.All); | ||
public static readonly InteropDescriptor Deserialize = Register("System.Binary.Deserialize", Binary_Deserialize, 0_00500000, TriggerType.All); | ||
|
||
private static bool Binary_Serialize(ApplicationEngine engine) | ||
{ | ||
byte[] serialized; | ||
try | ||
{ | ||
serialized = BinarySerializer.Serialize(engine.CurrentContext.EvaluationStack.Pop(), engine.MaxItemSize); | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
engine.CurrentContext.EvaluationStack.Push(serialized); | ||
return true; | ||
} | ||
|
||
private static bool Binary_Deserialize(ApplicationEngine engine) | ||
{ | ||
StackItem item; | ||
try | ||
{ | ||
item = BinarySerializer.Deserialize(engine.CurrentContext.EvaluationStack.Pop().GetSpan(), engine.MaxItemSize, engine.ReferenceCounter); | ||
} | ||
catch (FormatException) | ||
{ | ||
return false; | ||
} | ||
catch (IOException) | ||
{ | ||
return false; | ||
} | ||
engine.CurrentContext.EvaluationStack.Push(item); | ||
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using Neo.Ledger; | ||
using Neo.Network.P2P.Payloads; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Numerics; | ||
|
||
namespace Neo.SmartContract | ||
{ | ||
partial class InteropService | ||
{ | ||
public static class Blockchain | ||
{ | ||
public static readonly InteropDescriptor GetHeight = Register("System.Blockchain.GetHeight", Blockchain_GetHeight, 0_00000400, TriggerType.Application); | ||
public static readonly InteropDescriptor GetBlock = Register("System.Blockchain.GetBlock", Blockchain_GetBlock, 0_02500000, TriggerType.Application); | ||
public static readonly InteropDescriptor GetTransaction = Register("System.Blockchain.GetTransaction", Blockchain_GetTransaction, 0_01000000, TriggerType.Application); | ||
public static readonly InteropDescriptor GetTransactionHeight = Register("System.Blockchain.GetTransactionHeight", Blockchain_GetTransactionHeight, 0_01000000, TriggerType.Application); | ||
public static readonly InteropDescriptor GetTransactionFromBlock = Register("System.Blockchain.GetTransactionFromBlock", Blockchain_GetTransactionFromBlock, 0_01000000, TriggerType.Application); | ||
public static readonly InteropDescriptor GetContract = Register("System.Blockchain.GetContract", Blockchain_GetContract, 0_01000000, TriggerType.Application); | ||
|
||
private static bool Blockchain_GetHeight(ApplicationEngine engine) | ||
{ | ||
engine.CurrentContext.EvaluationStack.Push(engine.Snapshot.Height); | ||
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()); | ||
ContractState contract = engine.Snapshot.Contracts.TryGet(hash); | ||
if (contract == null) | ||
engine.CurrentContext.EvaluationStack.Push(StackItem.Null); | ||
else | ||
engine.CurrentContext.EvaluationStack.Push(contract.ToStackItem(engine.ReferenceCounter)); | ||
return true; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.