|
11 | 11 |
|
12 | 12 | using Akka.Actor;
|
13 | 13 | using Neo.Ledger;
|
| 14 | +using Neo.Network.P2P.Payloads; |
14 | 15 | using Neo.Persistence;
|
| 16 | +using Neo.SmartContract; |
| 17 | +using Neo.SmartContract.Native; |
| 18 | +using Neo.VM; |
| 19 | +using Neo.VM.Types; |
| 20 | +using Neo.Wallets; |
| 21 | +using Neo.Wallets.NEP6; |
15 | 22 | using System;
|
16 | 23 |
|
17 | 24 | namespace Neo.Plugins.OracleService.Tests
|
18 | 25 | {
|
19 | 26 | public static class TestBlockchain
|
20 | 27 | {
|
21 |
| - private static readonly NeoSystem s_theNeoSystem; |
22 |
| - private static readonly MemoryStore s_store = new(); |
| 28 | + public static readonly NeoSystem s_theNeoSystem; |
| 29 | + public static readonly MemoryStore s_store = new(); |
| 30 | + public static readonly NEP6Wallet s_wallet; |
| 31 | + public static readonly WalletAccount s_walletAccount; |
| 32 | + public static readonly OracleService s_oracle; |
23 | 33 |
|
24 | 34 | private class StoreProvider : IStoreProvider
|
25 | 35 | {
|
26 | 36 | public string Name => "TestProvider";
|
27 |
| - |
28 | 37 | public IStore GetStore(string path) => s_store;
|
29 | 38 | }
|
30 | 39 |
|
31 | 40 | static TestBlockchain()
|
32 | 41 | {
|
33 | 42 | Console.WriteLine("initialize NeoSystem");
|
34 |
| - s_theNeoSystem = new NeoSystem(ProtocolSettings.Load("config.json"), new StoreProvider()); |
| 43 | + StoreProvider _memoryStoreProvider = new(); |
| 44 | + s_oracle = new(); |
| 45 | + s_theNeoSystem = new NeoSystem(TestUtils.settings, _memoryStoreProvider); |
| 46 | + s_wallet = TestUtils.GenerateTestWallet("123"); |
| 47 | + s_walletAccount = s_wallet.Import("KxuRSsHgJMb3AMSN6B9P3JHNGMFtxmuimqgR9MmXPcv3CLLfusTd"); |
35 | 48 | }
|
36 | 49 |
|
37 |
| - public static void InitializeMockNeoSystem() |
| 50 | + public static UInt160 InitializeContract() |
38 | 51 | {
|
| 52 | + string _oracleContractSrc = """ |
| 53 | +using System.Numerics;using Neo.SmartContract.Framework;using Neo.SmartContract.Framework.Native;using Neo.SmartContract.Framework.Services; |
| 54 | +namespace oracle_demo{public class OracleDemo:SmartContract{ |
| 55 | +const byte PREFIX_COUNT = 0xcc; |
| 56 | +const byte PREFIX_DATA = 0xdd; |
| 57 | +public static string GetRequstData() => Storage.Get(Storage.CurrentContext, new byte[] { PREFIX_DATA }); |
| 58 | +public static BigInteger GetRequstCount() => (BigInteger)Storage.Get(Storage.CurrentContext, new byte[] { PREFIX_COUNT }); |
| 59 | +public static void CreateRequest(string url, string filter, string callback, byte[] userData, long gasForResponse) => Oracle.Request(url, filter, callback, userData, gasForResponse); |
| 60 | +public static void Callback(string url, byte[] userData, int code, byte[] result) |
| 61 | +{ |
| 62 | + ExecutionEngine.Assert(Runtime.CallingScriptHash == Oracle.Hash, "Unauthorized!"); |
| 63 | + StorageContext currentContext = Storage.CurrentContext; |
| 64 | + Storage.Put(currentContext, new byte[] { PREFIX_DATA }, (ByteString)result); |
| 65 | + Storage.Put(currentContext, new byte[] { PREFIX_COUNT }, |
| 66 | + (BigInteger)Storage.Get(currentContext, new byte[] { PREFIX_DATA }) + 1); |
| 67 | +}}} |
| 68 | +"""; |
| 69 | + string base64NefFile = "TkVGM05lby5Db21waWxlci5DU2hhcnAgMy43LjQrNjAzNGExODIxY2E3MDk0NjBlYzMxMzZjNzBjMmRjYzNiZWEuLi4AAAFYhxcRfgqoEHKvq3HS3Yn+fEuS/gdyZXF1ZXN0BQAADwAAmAwB3dswQZv2Z85Bkl3oMUAMAczbMEGb9mfOQZJd6DFK2CYERRDbIUBXAAV8e3p5eDcAAEBXAQRBOVNuPAwUWIcXEX4KqBByr6tx0t2J/nxLkv6XDA1VbmF1dGhvcml6ZWQh4UGb9mfOcHvbKAwB3dswaEHmPxiEDAHd2zBoQZJd6DFK2CYERRDbIRGeDAHM2zBoQeY/GIRAnIyFhg=="; |
| 70 | + string manifest = """{"name":"OracleDemo","groups":[],"features":{},"supportedstandards":[],"abi":{"methods":[{"name":"getRequstData","parameters":[],"returntype":"String","offset":0,"safe":false},{"name":"getRequstCount","parameters":[],"returntype":"Integer","offset":16,"safe":false},{"name":"createRequest","parameters":[{"name":"url","type":"String"},{"name":"filter","type":"String"},{"name":"callback","type":"String"},{"name":"userData","type":"ByteArray"},{"name":"gasForResponse","type":"Integer"}],"returntype":"Void","offset":40,"safe":false},{"name":"callback","parameters":[{"name":"url","type":"String"},{"name":"userData","type":"ByteArray"},{"name":"code","type":"Integer"},{"name":"result","type":"ByteArray"}],"returntype":"Void","offset":52,"safe":false}],"events":[]},"permissions":[{"contract":"0xfe924b7cfe89ddd271abaf7210a80a7e11178758","methods":["request"]}],"trusts":[],"extra":{"nef":{"optimization":"All"}}}"""; |
| 71 | + byte[] script; |
| 72 | + using (ScriptBuilder sb = new()) |
| 73 | + { |
| 74 | + sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", Convert.FromBase64String(base64NefFile), manifest); |
| 75 | + script = sb.ToArray(); |
| 76 | + } |
| 77 | + SnapshotCache snapshot = s_theNeoSystem.GetSnapshotCache(); |
| 78 | + Transaction? tx = new Transaction |
| 79 | + { |
| 80 | + Nonce = 233, |
| 81 | + ValidUntilBlock = NativeContract.Ledger.CurrentIndex(snapshot) + s_theNeoSystem.Settings.MaxValidUntilBlockIncrement, |
| 82 | + Signers = [new Signer() { Account = TestUtils.ValidatorScriptHash, Scopes = WitnessScope.CalledByEntry }], |
| 83 | + Attributes = System.Array.Empty<TransactionAttribute>(), |
| 84 | + Script = script, |
| 85 | + Witnesses = null, |
| 86 | + }; |
| 87 | + var engine = ApplicationEngine.Run(tx.Script, snapshot, container: tx, settings: s_theNeoSystem.Settings, gas: 1200_0000_0000); |
| 88 | + engine.SnapshotCache.Commit(); |
| 89 | + var result = (Neo.VM.Types.Array)engine.ResultStack.Peek(); |
| 90 | + return new UInt160(result[2].GetSpan()); |
39 | 91 | }
|
40 | 92 |
|
41 | 93 | internal static void ResetStore()
|
|
0 commit comments