Skip to content

Commit 6a94c5e

Browse files
committed
tmp commit
1 parent c69791f commit 6a94c5e

File tree

6 files changed

+189
-37
lines changed

6 files changed

+189
-37
lines changed

src/Plugins/OracleService/OracleService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void Start(Wallet wallet)
133133
return;
134134
}
135135

136-
if (!CheckOracleAvaiblable(_system.StoreView, out ECPoint[] oracles))
136+
if (!CheckOracleAvailable(_system.StoreView, out ECPoint[] oracles))
137137
{
138138
ConsoleHelper.Warning("The oracle service is unavailable");
139139
return;
@@ -180,7 +180,7 @@ void ICommittingHandler.Blockchain_Committing_Handler(NeoSystem system, Block bl
180180
OnStart();
181181
}
182182
if (status != OracleStatus.Running) return;
183-
if (!CheckOracleAvaiblable(snapshot, out ECPoint[] oracles) || !CheckOracleAccount(wallet, oracles))
183+
if (!CheckOracleAvailable(snapshot, out ECPoint[] oracles) || !CheckOracleAccount(wallet, oracles))
184184
OnStop();
185185
}
186186

@@ -553,7 +553,7 @@ private bool CheckTxSign(DataCache snapshot, Transaction tx, ConcurrentDictionar
553553
return false;
554554
}
555555

556-
private static bool CheckOracleAvaiblable(DataCache snapshot, out ECPoint[] oracles)
556+
private static bool CheckOracleAvailable(DataCache snapshot, out ECPoint[] oracles)
557557
{
558558
uint height = NativeContract.Ledger.CurrentIndex(snapshot) + 1;
559559
oracles = NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.Oracle, height);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (C) 2015-2024 The Neo Project.
2+
//
3+
// UT_OracleService.cs file belongs to the neo project and is free
4+
// software distributed under the MIT software license, see the
5+
// accompanying file LICENSE in the main directory of the
6+
// repository or http://www.opensource.org/licenses/mit-license.php
7+
// for more details.
8+
//
9+
// Redistribution and use in source and binary forms with or without
10+
// modifications are permitted.
11+
12+
using Akka.Actor;
13+
using Akka.TestKit.Xunit2;
14+
using Microsoft.VisualStudio.TestTools.UnitTesting;
15+
using Neo.Cryptography;
16+
using Neo.Cryptography.ECC;
17+
using Neo.Network.P2P.Payloads;
18+
using Neo.SmartContract;
19+
using Neo.SmartContract.Native;
20+
using Neo.VM;
21+
using Neo.Wallets;
22+
using System;
23+
using System.Linq;
24+
using System.Threading.Tasks;
25+
using static Neo.Plugins.OracleService.Tests.TestBlockchain;
26+
using static Neo.Plugins.OracleService.Tests.TestUtils;
27+
28+
namespace Neo.Plugins.OracleService.Tests
29+
{
30+
[TestClass]
31+
public class E2E_Https : TestKit
32+
{
33+
UInt160 customContract;
34+
35+
[TestInitialize]
36+
public void TestSetup()
37+
{
38+
customContract = InitializeContract();
39+
}
40+
41+
[TestMethod]
42+
public void TestE2EHttps()
43+
{
44+
byte[] script;
45+
using (ScriptBuilder sb = new())
46+
{
47+
sb.EmitDynamicCall(NativeContract.RoleManagement.Hash, "designateAsRole",
48+
[Role.Oracle,
49+
new ContractParameter()
50+
{
51+
Type = ContractParameterType.Array,
52+
Value = settings.StandbyCommittee.Select(
53+
p => new ContractParameter() { Type = ContractParameterType.PublicKey, Value = p }).ToList()
54+
}]);
55+
// Expected result: 12685221
56+
sb.EmitDynamicCall(customContract, "createRequest",
57+
["https://api.github.com/orgs/neo-project", "id", "callback", new byte[] { }, 1_0000_0000]);
58+
script = sb.ToArray();
59+
}
60+
Transaction[] txs = [
61+
new Transaction
62+
{
63+
Nonce = 233,
64+
ValidUntilBlock = NativeContract.Ledger.CurrentIndex(s_theNeoSystem.GetSnapshotCache()) + s_theNeoSystem.Settings.MaxValidUntilBlockIncrement,
65+
Signers = [new Signer() { Account = MultisigScriptHash, Scopes = WitnessScope.CalledByEntry }],
66+
Attributes = Array.Empty<TransactionAttribute>(),
67+
Script = script,
68+
NetworkFee = 1000_0000,
69+
SystemFee = 2_0000_0000,
70+
}
71+
];
72+
byte[] signature = txs[0].Sign(s_walletAccount.GetKey(), settings.Network);
73+
txs[0].Witnesses = [new Witness
74+
{
75+
InvocationScript = new byte[] { (byte)OpCode.PUSHDATA1, (byte)signature.Length }.Concat(signature).ToArray(),
76+
VerificationScript = MultisigScript,
77+
}];
78+
Block block = new Block
79+
{
80+
Header = new Header
81+
{
82+
Version = 0,
83+
PrevHash = s_theNeoSystem.GenesisBlock.Hash,
84+
Timestamp = s_theNeoSystem.GenesisBlock.Timestamp + 15_000,
85+
Index = 1,
86+
NextConsensus = s_theNeoSystem.GenesisBlock.NextConsensus,
87+
},
88+
Transactions = txs,
89+
};
90+
block.Header.MerkleRoot ??= MerkleTree.ComputeRoot(block.Transactions.Select(t => t.Hash).ToArray());
91+
signature = block.Sign(s_walletAccount.GetKey(), settings.Network);
92+
block.Header.Witness = new Witness
93+
{
94+
InvocationScript = new byte[] { (byte)OpCode.PUSHDATA1, (byte)signature.Length }.Concat(signature).ToArray(),
95+
VerificationScript = MultisigScript,
96+
};
97+
s_theNeoSystem.Blockchain.Ask(block).Wait();
98+
s_oracle.Start(s_wallet);
99+
}
100+
}
101+
}

tests/Neo.Plugins.OracleService.Tests/TestBlockchain.cs

+57-5
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,83 @@
1111

1212
using Akka.Actor;
1313
using Neo.Ledger;
14+
using Neo.Network.P2P.Payloads;
1415
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;
1522
using System;
1623

1724
namespace Neo.Plugins.OracleService.Tests
1825
{
1926
public static class TestBlockchain
2027
{
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;
2333

2434
private class StoreProvider : IStoreProvider
2535
{
2636
public string Name => "TestProvider";
27-
2837
public IStore GetStore(string path) => s_store;
2938
}
3039

3140
static TestBlockchain()
3241
{
3342
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");
3548
}
3649

37-
public static void InitializeMockNeoSystem()
50+
public static UInt160 InitializeContract()
3851
{
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());
3991
}
4092

4193
internal static void ResetStore()

tests/Neo.Plugins.OracleService.Tests/TestUtils.cs

+25
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,27 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
using FluentAssertions;
1213
using Neo.IO;
14+
using Neo.Json;
1315
using Neo.SmartContract;
1416
using Neo.SmartContract.Native;
17+
using Neo.Wallets;
18+
using Neo.Wallets.NEP6;
19+
using System;
1520

1621
namespace Neo.Plugins.OracleService.Tests
1722
{
1823
public static class TestUtils
1924
{
25+
public static readonly ProtocolSettings settings = ProtocolSettings.Load("config.json");
26+
public static readonly byte[] ValidatorScript = Contract.CreateSignatureRedeemScript(settings.StandbyCommittee[0]);
27+
public static readonly UInt160 ValidatorScriptHash = ValidatorScript.ToScriptHash();
28+
public static readonly string ValidatorAddress = ValidatorScriptHash.ToAddress(ProtocolSettings.Default.AddressVersion);
29+
public static readonly byte[] MultisigScript = Contract.CreateMultiSigRedeemScript(1, settings.StandbyCommittee);
30+
public static readonly UInt160 MultisigScriptHash = MultisigScript.ToScriptHash();
31+
public static readonly string MultisigAddress = MultisigScriptHash.ToAddress(ProtocolSettings.Default.AddressVersion);
32+
2033
public static StorageKey CreateStorageKey(this NativeContract contract, byte prefix, ISerializable key)
2134
{
2235
var k = new KeyBuilder(contract.Id, prefix);
@@ -28,5 +41,17 @@ public static StorageKey CreateStorageKey(this NativeContract contract, byte pre
2841
{
2942
return new KeyBuilder(contract.Id, prefix).AddBigEndian(value);
3043
}
44+
45+
public static NEP6Wallet GenerateTestWallet(string password)
46+
{
47+
JObject wallet = new JObject();
48+
wallet["name"] = "noname";
49+
wallet["version"] = new Version("1.0").ToString();
50+
wallet["scrypt"] = new ScryptParameters(2, 1, 1).ToJson();
51+
wallet["accounts"] = new JArray();
52+
wallet["extra"] = null;
53+
wallet.ToString().Should().Be("{\"name\":\"noname\",\"version\":\"1.0\",\"scrypt\":{\"n\":2,\"r\":1,\"p\":1},\"accounts\":[],\"extra\":null}");
54+
return new NEP6Wallet(null, password, settings, wallet);
55+
}
3156
}
3257
}

tests/Neo.Plugins.OracleService.Tests/UT_OracleService.cs

-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ namespace Neo.Plugins.OracleService.Tests
2121
[TestClass]
2222
public class UT_OracleService : TestKit
2323
{
24-
[TestInitialize]
25-
public void TestSetup()
26-
{
27-
TestBlockchain.InitializeMockNeoSystem();
28-
}
29-
3024
[TestMethod]
3125
public void TestFilter()
3226
{

tests/Neo.Plugins.OracleService.Tests/config.json

+3-23
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
},
3030
"ProtocolConfiguration": {
31-
"Network": 860833102,
31+
"Network": 5195086,
3232
"AddressVersion": 53,
3333
"MillisecondsPerBlock": 15000,
3434
"MaxTransactionsPerBlock": 512,
@@ -39,29 +39,9 @@
3939
"HF_Basilisk": 4120000
4040
},
4141
"InitialGasDistribution": 5200000000000000,
42-
"ValidatorsCount": 7,
42+
"ValidatorsCount": 1,
4343
"StandbyCommittee": [
44-
"03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c",
45-
"02df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e895093",
46-
"03b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a",
47-
"02ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba554",
48-
"024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d",
49-
"02aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e",
50-
"02486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a70",
51-
"023a36c72844610b4d34d1968662424011bf783ca9d984efa19a20babf5582f3fe",
52-
"03708b860c1de5d87f5b151a12c2a99feebd2e8b315ee8e7cf8aa19692a9e18379",
53-
"03c6aa6e12638b36e88adc1ccdceac4db9929575c3e03576c617c49cce7114a050",
54-
"03204223f8c86b8cd5c89ef12e4f0dbb314172e9241e30c9ef2293790793537cf0",
55-
"02a62c915cf19c7f19a50ec217e79fac2439bbaad658493de0c7d8ffa92ab0aa62",
56-
"03409f31f0d66bdc2f70a9730b66fe186658f84a8018204db01c106edc36553cd0",
57-
"0288342b141c30dc8ffcde0204929bb46aed5756b41ef4a56778d15ada8f0c6654",
58-
"020f2887f41474cfeb11fd262e982051c1541418137c02a0f4961af911045de639",
59-
"0222038884bbd1d8ff109ed3bdef3542e768eef76c1247aea8bc8171f532928c30",
60-
"03d281b42002647f0113f36c7b8efb30db66078dfaaa9ab3ff76d043a98d512fde",
61-
"02504acbc1f4b3bdad1d86d6e1a08603771db135a73e61c9d565ae06a1938cd2ad",
62-
"0226933336f1b75baa42d42b71d9091508b638046d19abd67f4e119bf64a7cfb4d",
63-
"03cdcea66032b82f5c30450e381e5295cae85c5e6943af716cc6b646352a6067dc",
64-
"02cd5a5547119e24feaa7c2a0f37b8c9366216bab7054de0065c9be42084003c8a"
44+
"0278ed78c917797b637a7ed6e7a9d94e8c408444c41ee4c0a0f310a256b9271eda"
6545
],
6646
"SeedList": [
6747
"seed1.neo.org:10333",

0 commit comments

Comments
 (0)