def fake_pp_with_fees(monkeypatch, three_phase_handler): pp = PP.fake_pp() def mock_get_state_root(ledger_id): if ledger_id == TOKEN_LEDGER_ID: return PP.plugin_data[FEES][f.STATE_ROOT.nm] else: return 'Pulled state root from a different ledger than sovtoken' def mock_get_txn_root(ledger_id): if ledger_id == TOKEN_LEDGER_ID: return PP.plugin_data[FEES][f.TXN_ROOT.nm] else: return 'Pulled txn root from a different ledger than sovtoken' state_root_deserialized = state_roots_serializer.deserialize( PP.plugin_data[FEES][f.STATE_ROOT.nm]) txn_root_deserialized = state_roots_serializer.deserialize( PP.plugin_data[FEES][f.TXN_ROOT.nm]) monkeypatch.setattr(three_phase_handler.fees_req_handler, 'fee_txns_in_current_batch', 1) monkeypatch.setattr(three_phase_handler.master_replica, 'stateRootHash', mock_get_state_root) monkeypatch.setattr(three_phase_handler.master_replica, 'txnRootHash', mock_get_txn_root) monkeypatch.setattr( three_phase_handler.fees_req_handler.token_state._trie, 'root_hash', state_root_deserialized) monkeypatch.setattr(three_phase_handler.fees_req_handler.token_ledger, 'uncommittedRootHash', txn_root_deserialized) return three_phase_handler.add_to_pre_prepare(pp)
def commit_batch(self, three_pc_batch, prev_handler_result=None): """ :param txn_count: The number of requests to commit (The actual requests are picked up from the uncommitted list from the ledger) :param state_root: The state trie root after the txns are committed :param txn_root: The txn merkle root after the txns are committed :return: list of committed transactions """ state_root = state_roots_serializer.deserialize(three_pc_batch.state_root.encode()) \ if isinstance(three_pc_batch.state_root, str) else three_pc_batch.state_root self.database_manager.ts_store.set(three_pc_batch.pp_time, state_root, three_pc_batch.ledger_id)
def test_get_fees_with_proof(helpers, fees_set, fees): """ Get the sovtokenfees from the ledger """ result = helpers.general.do_get_fees() fees = result[FEES] state_proof = result[STATE_PROOF] assert state_proof proof_nodes = decode_proof(result[STATE_PROOF][PROOF_NODES]) client_trie = Trie(PersistentDB(KeyValueStorageInMemory())) fees = rlp_encode([StaticFeesReqHandler.state_serializer.serialize(fees)]) assert client_trie.verify_spv_proof( state_roots_serializer.deserialize(result[STATE_PROOF][ROOT_HASH]), StaticFeesReqHandler.fees_state_key, fees, proof_nodes)
def _commit(ledger, state, txn_count, state_root, txn_root): _, committedTxns = ledger.commitTxns(txn_count) state_root = state_roots_serializer.deserialize( state_root.encode()) if isinstance(state_root, str) else state_root # TODO test for that if ledger.root_hash != txn_root: # Probably the following fail should trigger catchup # TODO add repr / str for Ledger class and dump it here as well raise PlenumValueError( 'txnRoot', txn_root, ("equal to current ledger root hash {}".format( ledger.root_hash))) state.commit(rootHash=state_root) return committedTxns
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
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
def _commit(ledger, state, txnCount, stateRoot, txnRoot, ppTime, ts_store=None): _, committedTxns = ledger.commitTxns(txnCount) stateRoot = state_roots_serializer.deserialize(stateRoot.encode()) if isinstance( stateRoot, str) else stateRoot # TODO test for that if ledger.root_hash != txnRoot: # Probably the following fail should trigger catchup # TODO add repr / str for Ledger class and dump it here as well raise PlenumValueError( 'txnRoot', txnRoot, ("equal to current ledger root hash {}" .format(ledger.root_hash)) ) state.commit(rootHash=stateRoot) if ts_store: ts_store.set(ppTime, stateRoot) return committedTxns
def test_state_proof(public_minting, looper, # noqa sdk_pool_handle, sdk_wallet_client, seller_token_wallet, seller_address, user1_token_wallet, user1_address): res = send_get_utxo(looper, seller_address, sdk_wallet_client, sdk_pool_handle) update_token_wallet_with_result(seller_token_wallet, res) # Do 20 XFER txns for _ in range(20): utxos = [_ for lst in seller_token_wallet.get_all_wallet_utxos().values() for _ in lst] seq_no, amount = utxos[0] inputs = [[seller_token_wallet, seller_address, seq_no]] outputs = [{"address": user1_address, "amount": 1}, {"address": seller_address, "amount": amount-1}] res = send_xfer(looper, inputs, outputs, sdk_pool_handle) update_token_wallet_with_result(seller_token_wallet, res) res = send_get_utxo(looper, seller_address, sdk_wallet_client, sdk_pool_handle) update_token_wallet_with_result(seller_token_wallet, res) res = send_get_utxo(looper, user1_address, sdk_wallet_client, sdk_pool_handle) update_token_wallet_with_result(user1_token_wallet, res) res = send_get_utxo(looper, user1_address, sdk_wallet_client, sdk_pool_handle) # Check presence of state proof assert res[STATE_PROOF] encoded = {} outputs = res[OUTPUTS] for out in outputs: state_key = TokenStaticHelper.create_state_key(out["address"], out["seqNo"]) encoded[state_key] = rlp_encode([str(out["amount"])]) proof_nodes = decode_proof(res[STATE_PROOF][PROOF_NODES]) client_trie = Trie(PersistentDB(KeyValueStorageInMemory())) assert client_trie.verify_spv_proof_multi( state_roots_serializer.deserialize(res[STATE_PROOF][ROOT_HASH]), encoded, proof_nodes)