Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

UP-42: Add API calls for frozen ledgers #2356

Merged
merged 12 commits into from
Feb 8, 2021
51 changes: 51 additions & 0 deletions libindy/include/indy_ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,57 @@ extern "C" {
const char* request_json)
);

/// Builds a LEDGERS_FREEZE request. Request to freeze list of ledgers.
///
/// #Params
/// command_handle: command handle to map callback to caller context.
/// submitter_did: (Optional) DID of the read request sender (if not provided then default Libindy DID will be used).
/// ledgers_ids: list of ledgers IDs for freezing ledgers (json format).
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// Request result as json.
///
/// #Errors
/// Common*
extern indy_error_t indy_build_ledgers_freeze_request(indy_handle_t command_handle,
const char * submitter_did,
const char * ledgers_ids,
adenishchenko marked this conversation as resolved.
Show resolved Hide resolved

void (*cb)(indy_handle_t command_handle_,
indy_error_t err,
const char* request_json)
);

/// Builds a GET_FROZEN_LEDGERS request. Request to get list of frozen ledgers.
/// frozen ledgers are defined by LEDGERS_FREEZE request.
///
/// #Params
/// command_handle: command handle to map callback to caller context.
/// submitter_did: (Optional) DID of the read request sender (if not provided then default Libindy DID will be used).
/// cb: Callback that takes command result as parameter.
///
/// #Returns
/// Request result as json.
/// {
/// <ledger_id>: {
/// "ledger": String - Ledger root hash,
/// "state": String - State root hash,
/// "seq_no": u64 - the latest transaction seqNo for particular Node,
/// },
/// ...
/// }
///
/// #Errors
/// Common*
extern indy_error_t indy_build_get_frozen_ledgers_request(indy_handle_t command_handle,
const char * submitter_did,

void (*cb)(indy_handle_t command_handle_,
indy_error_t err,
const char* request_json)
);

/// Builds a GET_TXN_AUTHR_AGRMT_AML request. Request to get a list of acceptance mechanisms from the ledger
/// valid for specified time or the latest one.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ public void TestLedgersFreezeRequest() throws Exception {
List<Integer> ledgersIds = Arrays.asList(0, 1, 28 ,345);
String request = Ledger.buildLedgersFreezeRequest(did, ledgersIds).get();

List<Integer> expectedLedgersIds = Arrays.asList(0, 1, 28 ,345);
JSONObject expectedResult = new JSONObject()
.put("operation", new JSONObject()
.put("type", "9")
.put("ledgers_ids", expectedLedgersIds)
.put("ledgers_ids", ledgersIds)
);

assert (new JSONObject(request).toMap().entrySet()
Expand All @@ -58,11 +57,10 @@ public void TestLedgersFreezeRequestWithEmptyData() throws Exception {
List<Integer> ledgersIds = Arrays.asList();
String request = Ledger.buildLedgersFreezeRequest(did, ledgersIds).get();

List<Integer> expectedLedgersIds = Arrays.asList();
JSONObject expectedResult = new JSONObject()
.put("operation", new JSONObject()
.put("type", "9")
.put("ledgers_ids", expectedLedgersIds)
.put("ledgers_ids", ledgersIds)
);

assert (new JSONObject(request).toMap().entrySet()
Expand Down
12 changes: 12 additions & 0 deletions wrappers/nodejs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,18 @@ indy.buildGetAcceptanceMechanismsRequest = function buildGetAcceptanceMechanisms
return cb.promise
}

indy.buildLedgersFreezeRequest = function buildLedgersFreezeRequest (submitterDid, ledgersIds, cb) {
cb = wrapIndyCallback(cb, fromJson)
capi.buildLedgersFreezeRequest(submitterDid, JSON.stringify(ledgersIds), cb)
return cb.promise
}

indy.buildGetFrozenLedgersRequest = function buildGetFrozenLedgersRequest (submitterDid, cb) {
cb = wrapIndyCallback(cb, fromJson)
capi.buildGetFrozenLedgersRequest(submitterDid, cb)
return cb.promise
}

indy.appendTxnAuthorAgreementAcceptanceToRequest = function appendTxnAuthorAgreementAcceptanceToRequest (request, text, version, taaDigest, accMechType, timeOfAcceptance, cb) {
cb = wrapIndyCallback(cb, fromJson)
capi.appendTxnAuthorAgreementAcceptanceToRequest(toJson(request), text, version, taaDigest, accMechType, timeOfAcceptance, cb)
Expand Down
37 changes: 37 additions & 0 deletions wrappers/nodejs/src/indy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,41 @@ NAN_METHOD(buildGetAcceptanceMechanismsRequest) {
delete arg2;
}

void buildLedgersFreezeRequest_cb(indy_handle_t handle, indy_error_t xerr, const char* arg0) {
IndyCallback* icb = IndyCallback::getCallback(handle);
if(icb != nullptr){
icb->cbString(xerr, arg0);
}
}
NAN_METHOD(buildLedgersFreezeRequest) {
INDY_ASSERT_NARGS(buildLedgersFreezeRequest, 3)
INDY_ASSERT_STRING(buildLedgersFreezeRequest, 0, submitterDid)
INDY_ASSERT_STRING(buildLedgersFreezeRequest, 1, ledgersIds)
INDY_ASSERT_FUNCTION(buildLedgersFreezeRequest, 2)
const char* arg0 = argToCString(info[0]);
const char* arg1 = argToCString(info[1]);
IndyCallback* icb = argToIndyCb(info[2]);
indyCalled(icb, indy_build_ledgers_freeze_request(icb->handle, arg0, arg1, buildLedgersFreezeRequest_cb));
delete arg0;
delete arg1;
}

void buildGetFrozenLedgersRequest_cb(indy_handle_t handle, indy_error_t xerr, const char* arg0) {
IndyCallback* icb = IndyCallback::getCallback(handle);
if(icb != nullptr){
icb->cbString(xerr, arg0);
}
}
NAN_METHOD(buildGetFrozenLedgersRequest) {
INDY_ASSERT_NARGS(buildGetFrozenLedgersRequest, 2)
INDY_ASSERT_STRING(buildGetFrozenLedgersRequest, 0, submitterDid)
INDY_ASSERT_FUNCTION(buildGetFrozenLedgersRequest, 1)
const char* arg0 = argToCString(info[0]);
IndyCallback* icb = argToIndyCb(info[1]);
indyCalled(icb, indy_build_get_frozen_ledgers_request(icb->handle, arg0, buildGetFrozenLedgersRequest_cb));
delete arg0;
}

void appendTxnAuthorAgreementAcceptanceToRequest_cb(indy_handle_t handle, indy_error_t xerr, const char* arg0) {
IndyCallback* icb = IndyCallback::getCallback(handle);
if(icb != nullptr){
Expand Down Expand Up @@ -3732,6 +3767,8 @@ NAN_MODULE_INIT(InitAll) {
Nan::Export(target, "buildGetTxnAuthorAgreementRequest", buildGetTxnAuthorAgreementRequest);
Nan::Export(target, "buildAcceptanceMechanismsRequest", buildAcceptanceMechanismsRequest);
Nan::Export(target, "buildGetAcceptanceMechanismsRequest", buildGetAcceptanceMechanismsRequest);
Nan::Export(target, "buildLedgersFreezeRequest", buildLedgersFreezeRequest);
Nan::Export(target, "buildGetFrozenLedgersRequest", buildGetFrozenLedgersRequest);
Nan::Export(target, "appendTxnAuthorAgreementAcceptanceToRequest", appendTxnAuthorAgreementAcceptanceToRequest);
Nan::Export(target, "appendRequestEndorser", appendRequestEndorser);
Nan::Export(target, "getResponseMetadata", getResponseMetadata);
Expand Down
19 changes: 19 additions & 0 deletions wrappers/nodejs/test/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,25 @@ test('ledger', async function (t) {
req = await indy.buildGetAcceptanceMechanismsRequest(null, 123379200, null)
t.deepEqual(req['operation'], { 'type': '7', 'timestamp': 123379200 })

// ledgers freeze
var ledgersIds = [0, 3, 76, 874]
req = await indy.buildLedgersFreezeRequest(trusteeDid, ledgersIds)
t.deepEqual(req['operation'], { 'type': '9', 'ledgers_ids': ledgersIds })

ledgersIds = []
req = await indy.buildLedgersFreezeRequest(trusteeDid, ledgersIds)
t.deepEqual(req['operation'], { 'type': '9', 'ledgers_ids': ledgersIds })

var err = await t.throwsAsync(indy.buildLedgersFreezeRequest(trusteeDid, ['0', '3', '76', '874']))
t.is(err.indyName, 'CommonInvalidStructure')

err = await t.throwsAsync(indy.buildLedgersFreezeRequest(trusteeDid, '[0, 3, 76, 874]'))
t.is(err.indyName, 'CommonInvalidStructure')

// frozen ledgers
req = await indy.buildGetFrozenLedgersRequest(trusteeDid)
t.deepEqual(req['operation'], { 'type': '10' })

// author agreement acceptance data
req = await indy.appendTxnAuthorAgreementAcceptanceToRequest(req, 'indy agreement', '1.0.0', null, 'acceptance mechanism label 1', 123379200)
var expectedMeta = {
Expand Down
3 changes: 2 additions & 1 deletion wrappers/python/indy/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ctypes import *

import logging
import json


async def sign_and_submit_request(pool_handle: int,
Expand Down Expand Up @@ -1861,7 +1862,7 @@ async def build_ledgers_freeze_request(submitter_did: str, ledgers_ids: List[int
build_ledgers_freeze_request.cb = create_cb(CFUNCTYPE(None, c_int32, c_int32, c_char_p))

c_submitter_did = c_char_p(submitter_did.encode('utf-8'))
json_ledgers_ids = '[' + ','.join(str(e) for e in ledgers_ids) + ']'
json_ledgers_ids = json.dumps(ledgers_ids)
c_ledgers_ids = c_char_p(json_ledgers_ids.encode('utf-8'))

request_json = await do_call('indy_build_ledgers_freeze_request',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@

@pytest.mark.asyncio
async def test_build_get_frozen_ledgers_request(did_trustee):
expected_response = {
"identifier": did_trustee,
"operation": {
"type": "10"
}
}
response = json.loads(await ledger.build_get_frozen_ledgers_request(did_trustee))
assert expected_response.items() <= response.items()
38 changes: 35 additions & 3 deletions wrappers/python/tests/ledger/test_build_ledgers_freeze_request.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
from indy import ledger
from typing import List
from indy.error import CommonInvalidStructure

import json
import pytest
from typing import List

@pytest.mark.asyncio
async def test_build_ledgers_freeze_request(did_trustee):
ledgers_ids: List[int] = [0, 6, 78, 751]
json.loads(await ledger.build_ledgers_freeze_request(did_trustee, ledgers_ids))
expected_response = {
"identifier": did_trustee,
"operation": {
"type": "9",
'ledgers_ids': ledgers_ids
}
}
response = json.loads(await ledger.build_ledgers_freeze_request(did_trustee, ledgers_ids))
assert expected_response.items() <= response.items()

@pytest.mark.asyncio
async def test_build_ledgers_freeze_request_with_empty_data(did_trustee):
ledgers_ids: List[int] = []
json.loads(await ledger.build_ledgers_freeze_request(did_trustee, ledgers_ids))
expected_response = {
"identifier": did_trustee,
"operation": {
"type": "9",
'ledgers_ids': ledgers_ids
}
}

response = json.loads(await ledger.build_ledgers_freeze_request(did_trustee, ledgers_ids))
assert expected_response.items() <= response.items()

@pytest.mark.asyncio
async def test_build_ledgers_freeze_request_with_str_data(did_trustee):
ledgers_ids = ["0", "6", "78", "751"]

with pytest.raises(CommonInvalidStructure):
json.loads(await ledger.build_ledgers_freeze_request(did_trustee, ledgers_ids))

@pytest.mark.asyncio
async def test_build_ledgers_freeze_request_with_no_data(did_trustee):
ledgers_ids = None

with pytest.raises(CommonInvalidStructure):
json.loads(await ledger.build_ledgers_freeze_request(did_trustee, ledgers_ids))