Skip to content

Commit

Permalink
use util methods to access transactions
Browse files Browse the repository at this point in the history
Signed-off-by: ashcherbakov <[email protected]>
  • Loading branch information
ashcherbakov committed May 4, 2018
1 parent dc82a46 commit 8778a1f
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 364 deletions.
11 changes: 5 additions & 6 deletions indy_client/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
from plenum.cli.phrase_word_completer import PhraseWordCompleter
from plenum.common.constants import NAME, VERSION, VERKEY, DATA, TXN_ID, FORCE, RAW
from plenum.common.exceptions import OperationError
from plenum.common.member.member import Member
from plenum.common.signer_did import DidSigner
from plenum.common.txn_util import createGenesisTxnFile
from plenum.common.util import randomString, getWalletFilePath
Expand Down Expand Up @@ -1982,13 +1983,11 @@ def _addGenTxnAction(self, matchedVars):
if matchedVars.get('add_genesis'):
nym = matchedVars.get('dest_id')
role = Identity.correctRole(self._getRole(matchedVars))
txn = {
TXN_TYPE: NYM,
TARGET_NYM: nym,
TXN_ID: sha256(randomString(6).encode()).hexdigest()
}
if role:
txn[ROLE] = role.upper()
role = role.upper()
txn = Member.nym_txn(nym=nym,
role=role,
txn_id=sha256(randomString(6).encode()).hexdigest())
# TODO: need to check if this needs to persist as well
self.genesisTransactions.append(txn)
self.print('Genesis transaction added.')
Expand Down
3 changes: 2 additions & 1 deletion indy_client/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from plenum.client.client import Client as PlenumClient
from plenum.common.error import fault
from plenum.common.txn_util import get_type
from stp_core.common.log import getlogger
from plenum.common.startable import Status

Expand Down Expand Up @@ -184,7 +185,7 @@ def _getDecryptedData(encData, key):

def hasNym(self, nym):
for txn in self.txnLog.getTxnsByType(NYM):
if txn.get(TXN_TYPE) == NYM:
if get_type(txn) == NYM:
return True
return False

Expand Down
4 changes: 2 additions & 2 deletions indy_client/persistence/client_txn_log.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from plenum.common.constants import TXN_TYPE
from plenum.common.txn_util import get_type
from plenum.common.util import updateFieldsWithSeqNo
from plenum.persistence.client_txn_log import ClientTxnLog as PClientTxnLog

Expand All @@ -20,6 +20,6 @@ def getTxnsByType(self, txnType: str) -> List:
include_value=True):
txn = self.serializer.deserialize(
val, fields=self.txnFieldOrdering)
if txn.get(TXN_TYPE) == txnType:
if get_type(txn) == txnType:
txns.append(txn)
return txns
86 changes: 41 additions & 45 deletions indy_client/test/cli/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@
from _sha256 import sha256
from typing import Dict

from ledger.genesis_txn.genesis_txn_file_util import create_genesis_txn_init_ledger
from libnacl import randombytes
from plenum.bls.bls_crypto_factory import create_default_bls_crypto_factory

from indy_client.cli.cli import IndyCli
from indy_client.client.wallet.connection import Connection
from indy_client.test.client.TestClient import TestClient
from indy_common.constants import NYM
from indy_common.roles import Roles
from indy_common.txn_util import getTxnOrderedFields
from ledger.genesis_txn.genesis_txn_file_util import create_genesis_txn_init_ledger
from plenum.bls.bls_crypto_factory import create_default_bls_crypto_factory
from plenum.common.constants import TARGET_NYM, ROLE, TXN_TYPE, ALIAS, TXN_ID, VALIDATOR, STEWARD
from plenum.common.member.steward import Steward
from plenum.common.signer_did import DidSigner
from plenum.common.signer_simple import SimpleSigner
from plenum.common.util import rawToFriendly
from plenum.config import pool_transactions_file_base, domain_transactions_file_base

from plenum.test import waits
from stp_core.loop.eventually import eventually
from stp_core.loop.looper import Looper

from stp_core.common.log import getlogger

from plenum.common.signer_simple import SimpleSigner
from plenum.common.constants import TARGET_NYM, ROLE, NODE, TXN_TYPE, DATA, \
CLIENT_PORT, NODE_PORT, NODE_IP, ALIAS, CLIENT_IP, TXN_ID, SERVICES, \
VALIDATOR, STEWARD, BLS_KEY
from plenum.common.types import f
from plenum.test.cli.helper import TestCliCore, assertAllNodesCreated, \
waitAllNodesStarted, newCLI as newPlenumCLI
from plenum.test.helper import initDirWithGenesisTxns
from plenum.test.testable import spyable
from indy_client.cli.cli import IndyCli
from indy_client.client.wallet.connection import Connection
from indy_common.constants import Environment
from stp_core.common.log import getlogger
from stp_core.loop.eventually import eventually
from stp_core.loop.looper import Looper
from stp_core.network.port_dispenser import genHa
from indy_common.constants import NYM
from indy_client.test.client.TestClient import TestClient
from indy_common.txn_util import getTxnOrderedFields
from indy_common.roles import Roles

logger = getlogger()

Expand Down Expand Up @@ -117,7 +110,7 @@ def ensureNymAdded(be, do, cli, nym, role=None):


def ensureNodesCreated(cli, nodeNames):
#cli.enterCmd("new node all")
# cli.enterCmd("new node all")
# TODO: Why 2 different interfaces one with list and one with varags
assertAllNodesCreated(cli, nodeNames)
waitAllNodesStarted(cli, *nodeNames)
Expand Down Expand Up @@ -167,26 +160,24 @@ def getPoolTxnData(poolId, newPoolTxnNodeNames):
nodeSeed = (newNodeAlias + "0" * (32 - len(newNodeAlias))).encode()
data["seeds"][newNodeAlias] = nodeSeed
nodeSigner = SimpleSigner(seed=nodeSeed)
node_txn = {
TARGET_NYM: nodeSigner.verkey,
TXN_TYPE: NODE,
f.IDENTIFIER.nm: stewardSigner.verkey,
DATA: {
CLIENT_IP: "127.0.0.1",
ALIAS: newNodeAlias,
NODE_IP: "127.0.0.1",
NODE_PORT: genHa()[1],
CLIENT_PORT: genHa()[1],
SERVICES: [VALIDATOR],
},
TXN_ID: sha256("{}".format(nodeSigner.verkey).encode()).hexdigest()
}

_, bls_key = create_default_bls_crypto_factory().generate_bls_keys(
seed=data['seeds'][n])
node_txn[DATA][BLS_KEY] = bls_key
data['nodesWithBls'][n] = True

node_txn = Steward.node_txn(
steward_nym=stewardSigner.verkey,
node_name=newNodeAlias,
nym=nodeSigner.verkey,
ip="127.0.0.1",
node_port=genHa()[1],
client_port=genHa()[1],
client_ip="127.0.0.1",
blskey=bls_key,
services=[VALIDATOR],
txn_id=sha256("{}".format(nodeSigner.verkey).encode()).hexdigest()
)

data["txns"].append(node_txn)

return data
Expand All @@ -195,8 +186,9 @@ def getPoolTxnData(poolId, newPoolTxnNodeNames):
def prompt_is(prompt):
def x(cli):
assert cli.currPromptText == prompt, \
"expected prompt: {}, actual prompt: {}".\
format(prompt, cli.currPromptText)
"expected prompt: {}, actual prompt: {}". \
format(prompt, cli.currPromptText)

return x


Expand Down Expand Up @@ -283,13 +275,15 @@ def new():
agent=agent,
nodes_chroot=tdir)
return c

if not looper:
looper = def_looper
if looper:
yield new()
else:
with Looper(debug=False) as looper:
yield new()

return _


Expand All @@ -307,7 +301,7 @@ def check_wallet(cli,
within=None):
async def check():
actualLinks = len(cli.activeWallet._connections)
assert (totalLinks is None or (totalLinks == actualLinks)),\
assert (totalLinks is None or (totalLinks == actualLinks)), \
'connections expected to be {} but is {}'.format(
totalLinks, actualLinks)

Expand All @@ -317,18 +311,18 @@ async def check():

assert (totalAvailableClaims is None or
totalAvailableClaims == tac), \
'available claims {} must be equal to {}'.\
format(tac, totalAvailableClaims)
'available claims {} must be equal to {}'. \
format(tac, totalAvailableClaims)

if cli.agent.prover is None:
assert (totalSchemas + totalClaimsRcvd) == 0
else:
w = cli.agent.prover.wallet
actualSchemas = len(await w.getAllSchemas())
assert (totalSchemas is None or
totalSchemas == actualSchemas),\
'schemas expected to be {} but is {}'.\
format(totalSchemas, actualSchemas)
totalSchemas == actualSchemas), \
'schemas expected to be {} but is {}'. \
format(totalSchemas, actualSchemas)

assert (totalClaimsRcvd is None or
totalClaimsRcvd == len((await w.getAllClaimsSignatures()).keys()))
Expand Down Expand Up @@ -412,13 +406,15 @@ def getTotalSchemas(userCli):
async def getTotalSchemasCoro():
return 0 if userCli.agent.prover is None \
else len(await userCli.agent.prover.wallet.getAllSchemas())

return userCli.looper.run(getTotalSchemasCoro)


def getTotalClaimsRcvd(userCli):
async def getTotalClaimsRcvdCoro():
return 0 if userCli.agent.prover is None \
else len((await userCli.agent.prover.wallet.getAllClaimsSignatures()).keys())

return userCli.looper.run(getTotalClaimsRcvdCoro)


Expand Down
10 changes: 6 additions & 4 deletions indy_client/test/cli/test_add_genesis_transaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from plenum.common.constants import VERKEY, DATA, NODE, TYPE
from plenum.common.txn_util import get_payload_data, get_type
from plenum.test.cli.helper import checkCmdValid

from indy_common.constants import NYM
Expand All @@ -18,13 +19,14 @@ def executeAndCheckGenTxn(cli, cmd, typ, nym, role=None, data=None):

role = Roles[role].value if role else role
for txn in cli.genesisTransactions:
if txn.get(TARGET_NYM) == nym:
txn_data = get_payload_data(txn)
if txn_data.get(TARGET_NYM) == nym:
nymCorrect = True
if txn.get(TYPE) == typ:
if get_type(txn) == typ:
typeCorrect = True
if txn.get(ROLE) == role:
if txn_data.get(ROLE) == role:
roleCorrect = True
if data and txn.get(DATA) == json.loads(data):
if data and txn_data.get(DATA) == json.loads(data):
dataCorrect = True

assert typeCorrect and nymCorrect and roleCorrect and dataCorrect
Expand Down
3 changes: 2 additions & 1 deletion indy_client/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from anoncreds.protocol.utils import randomString

from indy_node.server.node import Node
from plenum.common.txn_util import get_payload_data
from plenum.test.helper import waitForSufficientRepliesForRequests

from plenum.test.test_node import checkNodesConnected
Expand Down Expand Up @@ -106,7 +107,7 @@ def trusteeData(poolTxnTrusteeNames, updatedPoolTxnData):
for name in poolTxnTrusteeNames:
seed = updatedPoolTxnData["seeds"][name]
txn = next(
(txn for txn in updatedPoolTxnData["txns"] if txn[ALIAS] == name),
(txn for txn in updatedPoolTxnData["txns"] if get_payload_data(txn)[ALIAS] == name),
None)
ret.append((name, seed.encode(), txn))
return ret
Expand Down
Loading

0 comments on commit 8778a1f

Please sign in to comment.