Exemplo n.º 1
0
def add_prefix_nodes_and_verify(state,
                                prefix,
                                keys_suffices=None,
                                extra_nodes=False):
    keys_suffices = keys_suffices if keys_suffices else [
        1, 4, 10, 11, 24, 99, 100
    ]
    key_vals = {
        '{}{}'.format(prefix, k).encode(): str(random.randint(3000,
                                                              5000)).encode()
        for k in keys_suffices
    }
    for k, v in key_vals.items():
        state.set(k, v)

    prefix_prf, val = state.generate_state_proof_for_keys_with_prefix(
        prefix.encode(), get_value=True)
    encoded_key_values = dict(
        PruningState.encode_kv_for_verification(k, v)
        for k, v in key_vals.items())
    if extra_nodes:
        assert val.items() >= encoded_key_values.items()
    else:
        assert val == encoded_key_values

    assert PruningState.verify_state_proof_multi(state.headHash, key_vals,
                                                 prefix_prf)
Exemplo n.º 2
0
 def add_auth_rules_to_config_state(
         state: PruningState, auth_map: dict,
         serializer: AbstractConstraintSerializer):
     for rule_id, auth_constraint in auth_map.items():
         serialized_key = rule_id.encode()
         serialized_value = serializer.serialize(auth_constraint)
         if not state.get(serialized_key, isCommitted=False):
             state.set(serialized_key, serialized_value)
def state(request, tmpdir_factory) -> State:
    if request.param == 'memory':
        db = KeyValueStorageInMemory()
    if request.param == 'leveldb':
        db = KeyValueStorageLeveldb(tmpdir_factory.mktemp('').strpath,
                                    'some_db')
    state = PruningState(db)
    yield state
    state.close()
Exemplo n.º 4
0
    def on_batch_rejected(utxo_cache, tracker: LedgerUncommittedTracker,
                          state: PruningState, ledger: Ledger):
        uncommitted_hash, txn_count = tracker.reject_batch()
        if txn_count == 0:
            return 0
        state.revertToHead(uncommitted_hash)
        ledger.discardTxns(txn_count)

        utxo_cache.reject_batch()
        return txn_count
Exemplo n.º 5
0
def db_manager(tconf, tdir, idr_cache):
    db_manager = DatabaseManager()

    db_manager.register_new_store(IDR_CACHE_LABEL, idr_cache)
    db_manager.register_new_database(DOMAIN_LEDGER_ID, get_fake_ledger(),
                                     PruningState(KeyValueStorageInMemory()))
    db_manager.register_new_database(CONFIG_LEDGER_ID, get_fake_ledger(),
                                     PruningState(KeyValueStorageInMemory()))
    db_manager.register_new_database(POOL_LEDGER_ID, get_fake_ledger(),
                                     PruningState(KeyValueStorageInMemory()))
    return db_manager
Exemplo n.º 6
0
 def _create_state(self, name: str) -> PruningState:
     storage_name = getattr(self.config, "{}StateStorage".format(name))
     db_name = getattr(self.config, "{}StateDbName".format(name))
     if self.data_location is not None:
         return PruningState(
             initKeyValueStorage(storage_name,
                                 self.data_location,
                                 db_name,
                                 db_config=self.config.db_state_config))
     else:
         return PruningState(KeyValueStorageInMemory())
def state(request, tmpdir_factory) -> State:
    if request.param == 'leveldb':
        db = KeyValueStorageLeveldb(tmpdir_factory.mktemp('').strpath,
                                    'some_db')
    elif request.param == 'rocksdb':
        db = KeyValueStorageRocksdb(tmpdir_factory.mktemp('').strpath,
                                    'some_db')
    else:
        db = KeyValueStorageInMemory()
    state = PruningState(db)
    yield state
    state.close()
Exemplo n.º 8
0
 def loadState(self):
     return PruningState(
         initKeyValueStorage(
             self.config.poolStateStorage,
             self.node.dataLocation,
             self.config.poolStateDbName)
     )
Exemplo n.º 9
0
def nym_handler(tconf):
    data_manager = DatabaseManager()
    handler = NymHandler(tconf, data_manager)
    state = PruningState(KeyValueStorageInMemory())
    data_manager.register_new_database(handler.ledger_id, FakeSomething(),
                                       state)
    return handler
Exemplo n.º 10
0
def db_manager(tconf):
    _db_manager = DatabaseManager()
    storage = initKeyValueStorage(
        KeyValueStorageType.Memory,
        None,
        "tokenInMemoryStore",
        txn_serializer=serialization.multi_sig_store_serializer)
    ledger = get_fake_ledger()

    def commit_txns(count):
        ledger.committed_root_hash = ledger.uncommitted_root_hash
        return None, [1]

    ledger.commitTxns = commit_txns
    ledger.root_hash = txn_root_serializer.serialize("1")
    ledger.uncommitted_root_hash = "1"
    ledger.uncommitted_size = 1
    ledger.size = 0
    ledger.discardTxns = lambda x: None
    ledger.committed_root_hash = "-1"
    ledger.append_txns_metadata = lambda txns, txn_time: [
        append_txn_metadata(txn, 2, txn_time, 2) for txn in txns
    ]
    ledger.appendTxns = lambda x: (None, x)
    _db_manager.register_new_database(TOKEN_LEDGER_ID, ledger,
                                      PruningState(storage))
    return _db_manager
def add_prefix_nodes_and_verify(state, prefix, keys_suffices=None, extra_nodes=False):
    keys_suffices = keys_suffices if keys_suffices else [1, 4, 10, 11, 24, 99, 100]
    key_vals = {'{}{}'.format(prefix, k).encode():
                str(random.randint(3000, 5000)).encode() for k in keys_suffices}
    for k, v in key_vals.items():
        state.set(k, v)

    prefix_prf, val = state.generate_state_proof_for_keys_with_prefix(prefix.encode(), get_value=True)
    encoded_key_values = dict(PruningState.encode_kv_for_verification(k, v) for k, v in key_vals.items())
    if extra_nodes:
        assert val.items() >= encoded_key_values.items()
    else:
        assert val == encoded_key_values

    assert PruningState.verify_state_proof_multi(state.headHash, key_vals,
                                                 prefix_prf)
Exemplo n.º 12
0
def fees_authorizer(fees):
    authorizer = FeesAuthorizer(
        config_state=PruningState(KeyValueStorageInMemory()),
        utxo_cache=UTXOCache(KeyValueStorageInMemory()))
    authorizer.calculate_fees_from_req = lambda *args, **kwargs: fees.get(
        NYM_FEES_ALIAS)
    return authorizer
Exemplo n.º 13
0
 def init_config_state(self):
     return PruningState(
         initKeyValueStorage(
             self.node.config.configStateStorage,
             self.node.dataLocation,
             self.node.config.configStateDbName,
             db_config=self.node.config.db_state_config)
     )
Exemplo n.º 14
0
def node_handler():
    data_manager = DatabaseManager()
    bls = FakeSomething()
    handler = NodeHandler(data_manager, bls)
    state = PruningState(KeyValueStorageInMemory())
    data_manager.register_new_database(handler.ledger_id, FakeSomething(),
                                       state)
    return handler
def test_state_proof_and_verification(state):
    state.set(b'k1', b'v1')
    state.set(b'k2', b'v2')
    state.set(b'k3', b'v3')
    state.set(b'k4', b'v4')

    p1 = state.generate_state_proof(b'k1')
    p2 = state.generate_state_proof(b'k2')
    p3 = state.generate_state_proof(b'k3')
    p4 = state.generate_state_proof(b'k4')

    # Verify correct proofs and values
    assert PruningState.verify_state_proof(state.headHash, b'k1', b'v1', p1)
    assert PruningState.verify_state_proof(state.headHash, b'k2', b'v2', p2)
    assert PruningState.verify_state_proof(state.headHash, b'k3', b'v3', p3)
    assert PruningState.verify_state_proof(state.headHash, b'k4', b'v4', p4)

    # Incorrect proof
    assert PruningState.verify_state_proof(state.headHash, b'k3', b'v3', p4)

    # Correct proof but incorrect value
    assert not PruningState.verify_state_proof(state.headHash, b'k2', b'v1',
                                               p2)
    assert not PruningState.verify_state_proof(state.headHash, b'k4', b'v2',
                                               p4)
Exemplo n.º 16
0
def config_state(constraint_serializer):
    state = PruningState(KeyValueStorageInMemory())
    Node.add_auth_rules_to_config_state(state=state,
                                        auth_map=auth_map,
                                        serializer=constraint_serializer)
    Node.add_auth_rules_to_config_state(state=state,
                                        auth_map=anyone_can_write_map,
                                        serializer=constraint_serializer)
    return state
Exemplo n.º 17
0
def db_manager_with_config(db_manager, utxo_cache):
    storage = initKeyValueStorage(KeyValueStorageType.Memory,
                                  None,
                                  "configInMemoryStore",
                                  txn_serializer=in_memory_serializer)
    ledger = get_fake_ledger()
    db_manager.register_new_database(CONFIG_LEDGER_ID, ledger,
                                     PruningState(storage))
    return db_manager
def txn_author_agreement_aml_handler(tconf, domain_state):
    data_manager = DatabaseManager()
    handler = TxnAuthorAgreementAmlHandler(data_manager, FakeSomething())
    state = PruningState(KeyValueStorageInMemory())
    data_manager.register_new_database(handler.ledger_id, FakeSomething(),
                                       state)
    data_manager.register_new_database(DOMAIN_LEDGER_ID, FakeSomething(),
                                       domain_state)
    return handler
def ledgers_freeze_handler(tconf, domain_state, audit_ledger):
    data_manager = DatabaseManager()
    handler = LedgersFreezeHandler(data_manager)
    state = PruningState(KeyValueStorageInMemory())
    data_manager.register_new_database(handler.ledger_id, FakeSomething(),
                                       state)
    data_manager.register_new_database(DOMAIN_LEDGER_ID, FakeSomething(),
                                       domain_state)
    data_manager.register_new_database(AUDIT_LEDGER_ID, audit_ledger)
    return handler
def request_handler(bls_store):
    state = PruningState(KeyValueStorageInMemory())
    cache = IdrCache('Cache', KeyValueStorageInMemory())
    attr_store = AttributeStore(KeyValueStorageInMemory())
    return DomainReqHandler(ledger=None,
                            state=state,
                            requestProcessor=None,
                            idrCache=cache,
                            attributeStore=attr_store,
                            bls_store=bls_store)
Exemplo n.º 21
0
def database_manager(tdir_for_func):
    db = DatabaseManager()
    db.register_new_database(
        LEDGER_ID, Ledger(CompactMerkleTree(), dataDir=tdir_for_func),
        PruningState(KeyValueStorageRocksdb(tdir_for_func, 'kv1')))

    db.register_new_store(
        TS_LABEL,
        StateTsDbStorage('test',
                         {1: KeyValueStorageRocksdb(tdir_for_func, 'kv2')}))
    return db
def request_handler(bls_store):
    state = PruningState(KeyValueStorageInMemory())
    config_state = PruningState(KeyValueStorageInMemory())
    state_serializer = ConstraintsSerializer(domain_state_serializer)
    cache = IdrCache('Cache', KeyValueStorageInMemory())
    attr_store = AttributeStore(KeyValueStorageInMemory())
    write_req_validator = WriteRequestValidator(
        config=FakeSomething(authPolicy=CONFIG_LEDGER_AUTH_POLICY),
        auth_map=auth_map,
        cache=cache,
        config_state=config_state,
        state_serializer=state_serializer)
    return DomainReqHandler(ledger=None,
                            state=state,
                            config=None,
                            requestProcessor=None,
                            idrCache=cache,
                            attributeStore=attr_store,
                            bls_store=bls_store,
                            write_req_validator=write_req_validator,
                            ts_store=None)
def test_state_proof_and_verification_serialized(state):
    data = {k.encode(): v.encode() for k, v in
            [('k1', 'v1'), ('k2', 'v2'), ('k35', 'v55'), ('k70', 'v99')]}

    for k, v in data.items():
        state.set(k, v)

    proofs = {k: state.generate_state_proof(k, serialize=True) for k in data}

    for k, v in data.items():
        assert PruningState.verify_state_proof(state.headHash, k, v,
                                               proofs[k], serialized=True)
def test_state_proof_and_verification_serialized(state):
    data = {k.encode(): v.encode() for k, v in
            [('k1', 'v1'), ('k2', 'v2'), ('k35', 'v55'), ('k70', 'v99')]}

    for k, v in data.items():
        state.set(k, v)

    proofs = {k: state.generate_state_proof(k, serialize=True) for k in data}

    for k, v in data.items():
        assert PruningState.verify_state_proof(state.headHash, k, v,
                                               proofs[k], serialized=True)
def add_prefix_nodes_and_verify(state, prefix, keys_suffices=None):
    keys_suffices = keys_suffices if keys_suffices else [
        1, 4, 10, 11, 24, 99, 100
    ]
    key_vals = {
        '{}{}'.format(prefix, k).encode(): str(random.randint(3000,
                                                              5000)).encode()
        for k in keys_suffices
    }
    for k, v in key_vals.items():
        state.set(k, v)

    prefix_prf = state.generate_state_proof_for_key_prfx(prefix.encode())
    assert PruningState.verify_state_proof_multi(state.headHash, key_vals,
                                                 prefix_prf)
Exemplo n.º 26
0
def validate_proof(result):
    """
    Validates state proof
    """
    state_root_hash = result[STATE_PROOF]['root_hash']
    state_root_hash = state_roots_serializer.deserialize(state_root_hash)
    proof_nodes = result[STATE_PROOF]['proof_nodes']
    if isinstance(proof_nodes, str):
        proof_nodes = proof_nodes.encode()
    proof_nodes = proof_nodes_serializer.deserialize(proof_nodes)
    key, value = prepare_for_state(result)
    valid = PruningState.verify_state_proof(state_root_hash,
                                            key,
                                            value,
                                            proof_nodes,
                                            serialized=True)
    return valid
Exemplo n.º 27
0
 def validate_proof(self, result):
     """
     Validates state proof
     """
     state_root_hash = result[STATE_PROOF]['root_hash']
     state_root_hash = state_roots_serializer.deserialize(state_root_hash)
     proof_nodes = result[STATE_PROOF]['proof_nodes']
     if isinstance(proof_nodes, str):
         proof_nodes = proof_nodes.encode()
     proof_nodes = proof_nodes_serializer.deserialize(proof_nodes)
     key, value = self.prepare_for_state(result)
     valid = PruningState.verify_state_proof(state_root_hash,
                                             key,
                                             value,
                                             proof_nodes,
                                             serialized=True)
     return valid
Exemplo n.º 28
0
def check_state_proof(result,
                      expected_key: Optional = None,
                      expected_value: Optional = None):
    # TODO: This was copy-pasted from indy node (and extended), probably there should be better place for it
    assert STATE_PROOF in result

    state_proof = result[STATE_PROOF]
    assert ROOT_HASH in state_proof
    assert state_proof[ROOT_HASH]
    assert PROOF_NODES in state_proof
    assert state_proof[PROOF_NODES]
    assert MULTI_SIGNATURE in state_proof

    multi_sig = state_proof[MULTI_SIGNATURE]
    assert multi_sig
    assert multi_sig[MULTI_SIGNATURE_PARTICIPANTS]
    assert multi_sig[MULTI_SIGNATURE_SIGNATURE]
    assert MULTI_SIGNATURE_VALUE in multi_sig

    multi_sig_value = multi_sig[MULTI_SIGNATURE_VALUE]
    assert MULTI_SIGNATURE_VALUE_LEDGER_ID in multi_sig_value
    assert multi_sig_value[MULTI_SIGNATURE_VALUE_LEDGER_ID]
    assert MULTI_SIGNATURE_VALUE_STATE_ROOT in multi_sig_value
    assert multi_sig_value[MULTI_SIGNATURE_VALUE_STATE_ROOT]
    assert MULTI_SIGNATURE_VALUE_TXN_ROOT in multi_sig_value
    assert multi_sig_value[MULTI_SIGNATURE_VALUE_TXN_ROOT]
    assert MULTI_SIGNATURE_VALUE_POOL_STATE_ROOT in multi_sig_value
    assert multi_sig_value[MULTI_SIGNATURE_VALUE_POOL_STATE_ROOT]
    assert MULTI_SIGNATURE_VALUE_TIMESTAMP in multi_sig_value
    assert multi_sig_value[MULTI_SIGNATURE_VALUE_TIMESTAMP]

    if expected_key is not None:
        proof_nodes = base64.b64decode(state_proof[PROOF_NODES])
        root_hash = base58.b58decode(state_proof[ROOT_HASH])
        assert PruningState.verify_state_proof(root_hash,
                                               expected_key,
                                               expected_value,
                                               proof_nodes,
                                               serialized=True)
def test_state_proof_and_verification(state):
    state.set(b'k1', b'v1')
    state.set(b'k2', b'v2')
    state.set(b'k3', b'v3')
    state.set(b'k4', b'v4')

    p1 = state.generate_state_proof(b'k1')
    p2 = state.generate_state_proof(b'k2')
    p3 = state.generate_state_proof(b'k3')
    p4 = state.generate_state_proof(b'k4')

    # Verify correct proofs and values
    assert PruningState.verify_state_proof(state.headHash, b'k1', b'v1', p1)
    assert PruningState.verify_state_proof(state.headHash, b'k2', b'v2', p2)
    assert PruningState.verify_state_proof(state.headHash, b'k3', b'v3', p3)
    assert PruningState.verify_state_proof(state.headHash, b'k4', b'v4', p4)

    # Incorrect proof
    assert PruningState.verify_state_proof(state.headHash, b'k3', b'v3', p4)

    # Correct proof but incorrect value
    assert not PruningState.verify_state_proof(state.headHash, b'k2', b'v1', p2)
    assert not PruningState.verify_state_proof(state.headHash, b'k4', b'v2', p4)
Exemplo n.º 30
0
def init_contract_state(node):
    return PruningState(
        initKeyValueStorage(node.config.contractStateStorage,
                            node.dataLocation,
                            node.config.contractStateDbName,
                            db_config=node.config.db_state_config))
def domain_state(tconf):
    return PruningState(KeyValueStorageInMemory())
Exemplo n.º 32
0
def state2() -> State:
    state = PruningState(KeyValueStorageInMemory())
    yield state
    state.close()
def test_state_proof_for_missing_data(state):
    p1 = state.generate_state_proof(b'k1')
    assert PruningState.verify_state_proof(state.headHash, b'k1', None, p1)
def test_state_proof_for_missing_data(state):
    p1 = state.generate_state_proof(b'k1')
    assert PruningState.verify_state_proof(state.headHash, b'k1', None, p1)
Exemplo n.º 35
0
def config_state():
    return PruningState(KeyValueStorageInMemory())
Exemplo n.º 36
0
def state2(db) -> State:
    state = PruningState(db)
    yield state
    state.close()