def node_txn(steward_nym, node_name, nym, ip, node_port, client_port, client_ip=None, blskey=None, services=None, txn_id=None, seq_no=None, protocol_version=CURRENT_PROTOCOL_VERSION, bls_key_proof=None): txn = init_empty_txn(txn_type=NODE, protocol_version=protocol_version) txn = append_payload_metadata(txn, frm=steward_nym) txn_data = { TARGET_NYM: nym, DATA: { CLIENT_IP: client_ip or ip, ALIAS: node_name, CLIENT_PORT: client_port, NODE_IP: ip, NODE_PORT: node_port, SERVICES: services or [VALIDATOR], BLS_KEY: blskey, BLS_KEY_PROOF: bls_key_proof }, } txn = set_payload_data(txn, txn_data) txn_id = txn_id or sha256(node_name.encode()).hexdigest() txn = append_txn_metadata(txn, txn_id=txn_id) if seq_no: txn = append_txn_metadata(txn, seq_no=seq_no) return txn
def node_txn(steward_nym, node_name, nym, ip, node_port, client_port, client_ip=None, blskey=None, services=None, txn_id=None, seq_no=None): txn = init_empty_txn(txn_type=NODE) txn = append_payload_metadata(txn, frm=steward_nym) txn_data = { TARGET_NYM: nym, DATA: { CLIENT_IP: client_ip or ip, ALIAS: node_name, CLIENT_PORT: client_port, NODE_IP: ip, NODE_PORT: node_port, SERVICES: services or [VALIDATOR], BLS_KEY: blskey }, } txn = set_payload_data(txn, txn_data) txn_id = txn_id or sha256(node_name.encode()).hexdigest() txn = append_txn_metadata(txn, txn_id=txn_id) if seq_no: txn = append_txn_metadata(txn, seq_no=seq_no) return txn
def nym_txn(nym, name=None, verkey=None, role=None, creator=None, txn_id=None, seq_no=None): txn = init_empty_txn(NYM) txn_data = { TARGET_NYM: nym, } if verkey is not None: txn_data[VERKEY] = verkey if role is not None: txn_data[ROLE] = role if name is not None: txn_data[ALIAS] = name set_payload_data(txn, txn_data) txn = append_payload_metadata(txn, frm=creator) if txn_id: txn = append_txn_metadata(txn, txn_id=txn_id) if seq_no: txn = append_txn_metadata(txn, seq_no=seq_no) return txn
def test_append_payload_metadata(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") set_payload_data(txn, {"somekey": "somevalue"}) append_payload_metadata(txn, frm="DID1", req_id=12345, digest="random req digest", payload_digest="random payload") expected = SortedDict({ "reqSignature": {}, "txn": { "data": { "somekey": "somevalue" }, "metadata": { "from": "DID1", "reqId": 12345, "digest": "random req digest", "payloadDigest": "random payload" }, "protocolVersion": "3", "type": NODE, }, "txnMetadata": {}, "ver": "1" }) assert SortedDict(expected) == SortedDict(txn)
def test_append_txn_metadata(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") set_payload_data(txn, {"somekey": "somevalue"}) append_payload_metadata(txn, frm="DID1", req_id=12345) append_txn_metadata(txn, seq_no=144, txn_time=12345678, txn_id="dddd") expected = SortedDict({ "reqSignature": {}, "txn": { "data": { "somekey": "somevalue" }, "metadata": { "from": "DID1", "reqId": 12345, }, "protocolVersion": "3", "type": NODE, }, "txnMetadata": { "seqNo": 144, "txnId": "dddd", "txnTime": 12345678, }, "ver": "1" }) assert SortedDict(expected) == SortedDict(txn)
def test_init_empty_txn_no_protocol_ver(): txn = init_empty_txn(txn_type=NYM) expected = { "reqSignature": {}, "txn": { "data": {}, "metadata": {}, "type": NYM, "protocolVersion": CURRENT_PROTOCOL_VERSION }, "txnMetadata": {}, "ver": "1" } assert SortedDict(expected) == SortedDict(txn)
def test_init_empty_txn_with_protocol_ver(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") expected = { "reqSignature": {}, "txn": { "data": {}, "metadata": {}, "protocolVersion": "3", "type": NODE, }, "txnMetadata": {}, "ver": "1" } assert SortedDict(expected) == SortedDict(txn)
def test_init_empty_txn_no_protocol_ver(): txn = init_empty_txn(txn_type=NYM) expected = { "reqSignature": {}, "txn": { "data": {}, "metadata": { }, "type": NYM, }, "txnMetadata": { }, "ver": "1" } assert SortedDict(expected) == SortedDict(txn)
def test_init_empty_txn_with_protocol_ver(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") expected = { "reqSignature": {}, "txn": { "data": {}, "metadata": { }, "protocolVersion": "3", "type": NODE, }, "txnMetadata": { }, "ver": "1" } assert SortedDict(expected) == SortedDict(txn)
def test_init_empty_txn_no_protocol_ver(): txn = init_empty_txn(txn_type=NYM) expected = { "reqSignature": {}, "txn": { "data": {}, "metadata": { }, "type": NYM, "protocolVersion": CURRENT_PROTOCOL_VERSION }, "txnMetadata": { }, "ver": "1" } assert SortedDict(expected) == SortedDict(txn)
def test_set_payload_metadata(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") set_payload_data(txn, {"somekey": "somevalue"}) expected = SortedDict({ "reqSignature": {}, "txn": { "data": { "somekey": "somevalue" }, "metadata": {}, "protocolVersion": "3", "type": NODE, }, "txnMetadata": {}, "ver": "1" }) assert SortedDict(expected) == SortedDict(txn)
def _add_to_ledger(self, three_pc_batch: ThreePcBatch): # if PRE-PREPARE doesn't have audit txn (probably old code) - do nothing # TODO: remove this check after all nodes support audit ledger if not three_pc_batch.has_audit_txn: return # 1. prepare AUDIT txn txn_data = self._create_audit_txn_data(three_pc_batch, self.ledger.get_last_txn()) txn = init_empty_txn(txn_type=PlenumTransactions.AUDIT.value) txn = set_payload_data(txn, txn_data) # 2. Append txn metadata self.ledger.append_txns_metadata([txn], three_pc_batch.pp_time) # 3. Add to the Ledger self.ledger.appendTxns([txn])
def test_init_empty_txn_with_payload_ver(): txn = init_empty_txn(txn_type=NODE, protocol_version="3", txn_payload_version="10") expected = { "reqSignature": {}, "txn": { "data": {}, "metadata": {}, "protocolVersion": "3", "type": NODE, "ver": "10" }, "txnMetadata": {}, "ver": CURRENT_TXN_VERSION } assert SortedDict(expected) == SortedDict(txn)
def test_set_payload_metadata(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") set_payload_data(txn, {"somekey": "somevalue"}) expected = SortedDict({ "reqSignature": {}, "txn": { "data": {"somekey": "somevalue"}, "metadata": { }, "protocolVersion": "3", "type": NODE, }, "txnMetadata": { }, "ver": "1" }) assert SortedDict(expected) == SortedDict(txn)
def test_append_payload_metadata(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") set_payload_data(txn, {"somekey": "somevalue"}) append_payload_metadata(txn, frm="DID1", req_id=12345, digest="random req digest") expected = SortedDict({ "reqSignature": {}, "txn": { "data": {"somekey": "somevalue"}, "metadata": { "from": "DID1", "reqId": 12345, "digest": "random req digest" }, "protocolVersion": "3", "type": NODE, }, "txnMetadata": { }, "ver": "1" }) assert SortedDict(expected) == SortedDict(txn)
def nym_txn(nym, name=None, verkey=None, role=None, creator=None, txn_id=None, seq_no=None, protocol_version=CURRENT_PROTOCOL_VERSION): txn = init_empty_txn(NYM, protocol_version=protocol_version) txn_data = { TARGET_NYM: nym, } if verkey is not None: txn_data[VERKEY] = verkey if role is not None: txn_data[ROLE] = role if name is not None: txn_data[ALIAS] = name set_payload_data(txn, txn_data) txn = append_payload_metadata(txn, frm=creator) if txn_id: txn = append_txn_metadata(txn, txn_id=txn_id) if seq_no: txn = append_txn_metadata(txn, seq_no=seq_no) return txn
def test_append_txn_metadata(): txn = init_empty_txn(txn_type=NODE, protocol_version="3") set_payload_data(txn, {"somekey": "somevalue"}) append_payload_metadata(txn, frm="DID1", req_id=12345) append_txn_metadata(txn, seq_no=144, txn_time=12345678, txn_id="dddd") expected = SortedDict({ "reqSignature": {}, "txn": { "data": {"somekey": "somevalue"}, "metadata": { "from": "DID1", "reqId": 12345, }, "protocolVersion": "3", "type": NODE, }, "txnMetadata": { "seqNo": 144, "txnId": "dddd", "txnTime": 12345678, }, "ver": "1" }) assert SortedDict(expected) == SortedDict(txn)
def add_txns(node, num): ledger = node.getLedger(ledger_id) for i in range(num): ledger.append(init_empty_txn(NYM))