1 module tagion.testbench.send_contract;
2 import core.thread;
3 import core.time;
4 import std.file;
5 import std.path : buildPath, setExtension;
6 import std.stdio;
7 import tagion.GlobalSignals;
8 import tagion.basic.Types : FileExtension;
9 import tagion.behaviour.Behaviour;
10 import tagion.logger.Logger;
11 import tagion.services.options;
12 import tagion.testbench.services;
13 import tagion.testbench.tools.Environment;
14 import tagion.tools.Basic;
15 import neuewelle = tagion.tools.neuewelle;
16 import tagion.utils.pretend_safe_concurrency;
17 
18 mixin Main!(_main);
19 
20 void wrap_neuewelle(immutable(string)[] args) {
21     neuewelle._main(cast(string[]) args);
22 }
23 
24 int _main(string[] args) {
25     auto module_path = env.bdd_log.buildPath(__MODULE__);
26     if (module_path.exists) {
27         rmdirRecurse(module_path);
28     }
29     mkdirRecurse(module_path);
30     string config_file = buildPath(module_path, "tagionwave.json");
31 
32     scope Options local_options = Options.defaultOptions;
33     local_options.dart.folder_path = buildPath(module_path);
34     local_options.replicator.folder_path = buildPath(module_path, "recorders");
35     local_options.epoch_creator.timeout = 500;
36     local_options.save(config_file);
37 
38     import std.algorithm;
39     import std.array;
40     import std.format;
41     import std.range;
42     import std.stdio;
43     import tagion.crypto.SecureInterfaceNet;
44     import tagion.crypto.SecureNet : StdSecureNet;
45     import tagion.dart.DART;
46     import tagion.dart.DARTFile;
47     import tagion.dart.Recorder;
48     import tagion.script.TagionCurrency;
49     import tagion.script.common : TagionBill;
50     import tagion.testbench.services.sendcontract;
51     import tagion.wallet.SecureWallet;
52 
53     StdSecureWallet[] wallets;
54     // create the wallets
55     foreach (i; 0 .. 5) {
56         StdSecureWallet secure_wallet;
57         secure_wallet = StdSecureWallet(
58                 iota(0, 5).map!(n => format("%dquestion%d", i, n)).array,
59                 iota(0, 5).map!(n => format("%danswer%d", i, n)).array,
60                 4,
61                 format("%04d", i),
62         );
63         wallets ~= secure_wallet;
64     }
65 
66     // bills for the dart on startup
67     TagionBill[] bills;
68     foreach (ref wallet; wallets) {
69         bills ~= wallet.requestBill(1000.TGN);
70         bills ~= wallet.requestBill(2000.TGN);
71     }
72     const start_amount = 3000.TGN;
73 
74     // create the recorder
75     SecureNet net = new StdSecureNet();
76     net.generateKeyPair("very_secret");
77 
78     auto factory = RecordFactory(net);
79     auto recorder = factory.recorder;
80     recorder.insert(bills, Archive.Type.ADD);
81 
82     string dart_interface_sock_addr;
83     string inputvalidator_sock_addr;
84     // create the databases
85     foreach (i; 0 .. local_options.wave.number_of_nodes) {
86         immutable prefix = format(local_options.wave.prefix_format, i);
87 
88         if (i == 0) {
89             auto _opts = Options(local_options);
90             _opts.setPrefix(prefix);
91             dart_interface_sock_addr = _opts.dart_interface.sock_addr;
92             inputvalidator_sock_addr = _opts.inputvalidator.sock_addr;
93         }
94         const path = buildPath(local_options.dart.folder_path, prefix ~ local_options.dart.dart_filename);
95         writeln(path);
96         DARTFile.create(path, net);
97         auto db = new DART(net, path);
98         db.modify(recorder);
99         db.close;
100     }
101 
102     immutable neuewelle_args = ["send_contract_test", config_file]; // ~ args;
103 
104     auto tid = spawn(&wrap_neuewelle, neuewelle_args);
105 
106     Thread.sleep(5.seconds);
107     writeln("going to run test");
108 
109     const task_name = "send_contract_tester";
110     register(task_name, thisTid);
111     log.registerSubscriptionTask(task_name);
112 
113     auto send_contract_feature = automation!(sendcontract);
114     send_contract_feature.SendASingleTransactionFromAWalletToAnotherWallet(local_options, wallets, dart_interface_sock_addr, inputvalidator_sock_addr, start_amount);
115     send_contract_feature.run();
116     writefln("finished test execution");
117 
118     stopsignal.set;
119 
120     return 0;
121 
122 }