-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
93 lines (73 loc) · 2.65 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Threading;
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Util;
using Contract;
namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
ContractLog.Init();
IList<Thread> threads = new List<Thread>();
ConcurrentQueue<ContractTask> tasks = new ConcurrentQueue<ContractTask>();
var web3 = InitWeb3();
if (web3 != null) {
var oracle = new Oracle(web3);
threads.Add(new Thread(() => CreateHostBuilder(args).Build().Run()));
for(int i = 0; i < 3; ++i)
{
threads.Add(new Thread(() => DemoOfContractExecute(oracle, tasks)));
}
foreach (var thread in threads)
{
thread.Start();
}
// generate contracts
for(int i = 0; i < 9; ++i ) {
tasks.Enqueue(new ContractTask(oracle));
}
}
ContractLog.Close();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
public static Web3 InitWeb3() {
Web3 web3 = null;
try
{
// using local chain eg Geth https://github.com/Nethereum/TestChains#geth
var url = "http://localhost:8545";
var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
var account = new Account(privateKey);
web3 = new Web3(account, url);
}
catch (Exception ex)
{
Log.Information("Problem in web3 initialization!");
Console.WriteLine(ex.ToString());
}
return web3;
}
static void DemoOfContractExecute(Oracle oracle, ConcurrentQueue<ContractTask> tasks)
{
ContractTask task;
while(true) {
while (tasks.TryDequeue(out task)) {
task.ExecuteWith(oracle);
}
}
}
}
}