forked from hyperledger/indy-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_keys.py
149 lines (119 loc) · 4.66 KB
/
add_keys.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env python3
"""
This script registers new trust anchors (client keys)
To add new key you need to use existing Steward and it's seed
"""
import os
import sys
from itertools import groupby
from stp_core.loop.looper import Looper
from plenum.common.signer_did import DidSigner
from plenum.common.types import HA
from stp_core.common.log import getlogger
from plenum.test.helper import eventually, eventuallyAll
from indy_common.config_util import getConfig
from indy_common.constants import TRUST_ANCHOR
from indy_client.client.client import Client
from indy_client.client.wallet.wallet import Wallet
logger = getlogger()
# loading cluster configuration
config = getConfig()
requestTTL = 10 # seconds
# load test configuration
assert len(sys.argv) >= 3
stewardName = sys.argv[1]
stewardSeed = str.encode(sys.argv[2])
trustAnchorSeeds = sys.argv[3:]
if not trustAnchorSeeds:
seed_file_path = "{}/load_test_clients.list".format(os.getcwd())
trustAnchorSeeds = []
with open(seed_file_path, "r") as file:
trustAnchorSeeds = [line.strip().split(":")[1] for line in file]
def spawnClient(clientName, port, signerSeed, host='0.0.0.0'):
clientAddress = HA(host, port)
# from plenum.client.request_id_store import FileRequestIdStore
# walletFilePath = os.path.join(config.baseDir, "wallet")
# print("Storing request ids in {}".format(walletFilePath))
# store = FileRequestIdStore(walletFilePath)
# wallet = Wallet(clientName, store)
wallet = Wallet(clientName)
wallet.addIdentifier(signer=DidSigner(seed=signerSeed))
client = Client(clientName, ha=clientAddress)
return client, wallet
async def checkReply(client, requestId):
_, status = client.getReply(requestId)
logger.info("Number of received messages {}".format(len(client.inBox)))
groups = groupby(client.inBox, key=lambda x: x[0])
for key, group in groups:
logger.info("Group {}".format(key['op']))
for msg in list(group):
logger.info(" {}".format(msg))
succeeded = status == "CONFIRMED"
return succeeded
async def doRequesting(client, wallet, op):
signedOp = wallet.signOp(op)
logger.info("Client {} sending request {}".format(client, op))
request = client.submitReqs(signedOp)[0][0]
requestId = request.reqId
args = [client, requestId]
await eventually(checkReply, *args, timeout=requestTTL)
def checkIfConnectedToAll(client):
connectedNodes = client.nodestack.connecteds
connectedNodesNum = len(connectedNodes)
totalNodes = len(client.nodeReg)
logger.info("Connected {} / {} nodes".format(connectedNodesNum, totalNodes))
for node in connectedNodes:
logger.info(" {}".format(node))
if connectedNodesNum == 0:
raise Exception("Not connected to any")
elif connectedNodesNum < totalNodes * 0.8:
raise Exception("Not connected fully")
else:
return True
async def ensureConnectedToNodes(client):
wait = 5
logger.info(
"waiting for {} seconds to check client connections to nodes...".format(wait))
await eventuallyAll(lambda: checkIfConnectedToAll(client), retryWait=.5, totalTimeout=wait)
def addNyms():
with Looper(debug=getConfig().LOOPER_DEBUG) as looper:
from indy_client.test.helper import createNym
# Starting clients
print("Spawning client")
client, wallet = spawnClient(stewardName, 5678, stewardSeed)
client.registerObserver(wallet.handleIncomingReply)
print("Adding it to looper")
looper.add(client)
print("Running it")
looper.run(ensureConnectedToNodes(client))
# Creating request
print("Creating request")
bad = []
for seed in trustAnchorSeeds:
signer = DidSigner(seed=seed.encode())
nym = signer.identifier
verkey = signer.verkey
# Sending requests
print("Creating nym for seed {}".format(seed))
try:
createNym(
looper=looper,
nym=nym,
creatorClient=client,
creatorWallet=wallet,
verkey=verkey,
role=TRUST_ANCHOR)
print("Successfully created nym for {}".format(seed))
except Exception as ex:
bad.append(seed)
print("Failed to create nym for {}".format(seed))
print("=======================")
if not bad:
print("All nyms created successfully")
else:
print("Failed to created nyms for:")
for nym in bad:
print("-", nym)
print("=======================")
if __name__ == '__main__':
addNyms()