-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsimulate-pool.py
More file actions
120 lines (102 loc) · 3.21 KB
/
simulate-pool.py
File metadata and controls
120 lines (102 loc) · 3.21 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import argparse
import asyncio # new module
import base64
import socket
from itertools import cycle
import numpy as np
import simpy
from colorama import Fore, init
from cryptography.hazmat.primitives.asymmetric import x25519
from dissononce.cipher.chachapoly import ChaChaPolyCipher
from dissononce.dh.x25519.x25519 import X25519DH
from dissononce.hash.blake2s import Blake2sHash
from dissononce.processing.handshakepatterns.interactive.NX import NXHandshakePattern
from dissononce.processing.impl.cipherstate import CipherState
from dissononce.processing.impl.handshakestate import HandshakeState
from dissononce.processing.impl.symmetricstate import SymmetricState
from event_bus import EventBus
import primitives.coins as coins
import primitives.mining_params as mining_params
from primitives.connection import Connection
from primitives.messages import SetupConnection, SetupConnectionSuccess
from primitives.miner import Miner
from primitives.pool import Pool
init()
bus = EventBus()
async def connect():
np.random.seed(123)
parser = argparse.ArgumentParser(
prog="mine.py",
description="Simulates interaction of a mining pool and two miners",
)
parser.add_argument(
"--limit",
type=int,
help="simulation time limit in seconds, default = 500",
default=50,
)
parser.add_argument(
"--verbose",
help="display all events (warning: a lot of text is generated)",
action="store_const",
const=True,
)
parser.add_argument(
"--plain-output",
help="Print just values to terminal: accepted shares, accepted submits,"
" stale shares, stale submits, rejected submits",
action="store_true",
)
args = parser.parse_args()
if args.verbose:
@bus.on("pool1")
def subscribe_pool1(ts, conn_uid, message, aux=None):
print(
Fore.LIGHTCYAN_EX,
"T+{0:.3f}:".format(ts),
"(pool1)",
conn_uid if conn_uid is not None else "",
message,
aux,
Fore.RESET,
)
conn1 = Connection("pool", "stratum", pool_host="localhost", pool_port=2000)
pool = Pool(
"pool1",
bus,
default_target=coins.Target.from_difficulty(
100000, mining_params.diff_1_target
),
enable_vardiff=True,
)
await pool.make_handshake(conn1)
if args.plain_output:
print(
pool.accepted_shares,
pool.accepted_submits,
pool.stale_shares,
pool.stale_submits,
pool.rejected_submits,
sep=",",
)
else:
print(
"accepted shares:",
pool.accepted_shares,
"accepted submits:",
pool.accepted_submits,
)
print(
"stale shares:",
pool.stale_shares,
"stale submits:",
pool.stale_submits,
"rejected submits:",
pool.rejected_submits,
)
return pool
async def loop():
pool = await connect()
await asyncio.gather(pool.start_server(), pool.receive_loop(), pool.pow_update())
if __name__ == "__main__":
asyncio.run(loop())