Esempio n. 1
0
def test_serialization_merkletree_layers_empty():
    tree = make_empty_merkle_tree()

    data = serialization.serialize_merkletree_layers(tree.layers)
    restored = serialization.deserialize_merkletree_layers(data)

    assert tree.layers == restored
Esempio n. 2
0
def deserialize_merkletree_layers(data: typing.List[str]):
    elements = map_list(deserialize_bytes, data)
    if len(elements) == 0:
        from raiden.transfer.state import make_empty_merkle_tree
        return make_empty_merkle_tree().layers

    return compute_layers(elements)
Esempio n. 3
0
def deserialize_merkletree_layers(data: List[str]) -> List[List[Keccak256]]:
    elements = cast(List[Keccak256], map_list(deserialize_bytes, data))
    if len(elements) == 0:
        from raiden.transfer.state import make_empty_merkle_tree

        return make_empty_merkle_tree().layers

    return compute_layers(elements)
Esempio n. 4
0
def make_receive_expired_lock(
    channel_state: NettingChannelState,
    privkey: bytes,
    nonce: Nonce,
    transferred_amount: TokenAmount,
    lock: HashTimeLockState,
    merkletree_leaves: List[Keccak256] = None,
    locked_amount: LockedAmount = None,
    chain_id: ChainID = None,
) -> ReceiveLockExpired:

    if not isinstance(lock, HashTimeLockState):
        raise ValueError("lock must be of type HashTimeLockState")

    signer = LocalSigner(privkey)
    address = signer.address
    if address not in (channel_state.our_state.address,
                       channel_state.partner_state.address):
        raise ValueError("Private key does not match any of the participants.")

    if merkletree_leaves is None:
        layers = make_empty_merkle_tree().layers
    else:
        assert lock.lockhash not in merkletree_leaves
        layers = compute_layers(merkletree_leaves)

    locksroot = layers[MERKLEROOT][0]

    chain_id = chain_id or channel_state.chain_id
    lock_expired_msg = LockExpired(
        chain_id=chain_id,
        nonce=nonce,
        message_identifier=random.randint(0, UINT64_MAX),
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        locksroot=locksroot,
        channel_identifier=channel_state.identifier,
        token_network_address=channel_state.token_network_identifier,
        recipient=channel_state.partner_state.address,
        secrethash=lock.secrethash,
    )
    lock_expired_msg.sign(signer)

    balance_proof = balanceproof_from_envelope(lock_expired_msg)

    receive_lockedtransfer = ReceiveLockExpired(
        balance_proof=balance_proof,
        secrethash=lock.secrethash,
        message_identifier=random.randint(0, UINT64_MAX),
    )

    return receive_lockedtransfer
Esempio n. 5
0
def assert_locked(from_channel, pending_locks):
    """ Assert the locks created from `from_channel`. """
    # a locked transfer is registered in the _partner_ state
    if pending_locks:
        leaves = [sha3(lock.encoded) for lock in pending_locks]
        layers = compute_layers(leaves)
        tree = MerkleTreeState(layers)
    else:
        tree = make_empty_merkle_tree()

    assert from_channel.our_state.merkletree == tree

    for lock in pending_locks:
        pending = lock.secrethash in from_channel.our_state.secrethashes_to_lockedlocks
        unclaimed = lock.secrethash in from_channel.our_state.secrethashes_to_unlockedlocks
        assert pending or unclaimed
Esempio n. 6
0
 def __init__(self, canonical_identifier):
     self._canonical_identifier = canonical_identifier
     self._merkletree = make_empty_merkle_tree()
     self.properties = None