Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_monitor_new_balance_proof_event_handler_idempotency(context: Context):
context = setup_state_with_closed_channel(context)
new_balance_event = ReceiveMonitoringNewBalanceProofEvent(
token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS,
channel_identifier=DEFAULT_CHANNEL_IDENTIFIER,
reward_amount=TokenAmount(1),
nonce=Nonce(2),
ms_address=Address(bytes([3] * 20)),
raiden_node_address=DEFAULT_PARTICIPANT2,
block_number=BlockNumber(23),
)
channel = context.database.get_channel(
new_balance_event.token_network_address, new_balance_event.channel_identifier
)
assert channel
assert channel.update_status is None
monitor_new_balance_proof_event_handler(new_balance_event, context)
assert context.database.scheduled_event_count() == 1
assert context.database.channel_count() == 1
channel = context.database.get_channel(
reveal_timeout=BlockTimeout(2),
signature=EMPTY_SIGNATURE,
),
updating_capacity_partner=TokenAmount(0),
other_capacity_partner=TokenAmount(0),
)
token_network_model.handle_channel_balance_update_message(
PFSCapacityUpdate(
canonical_identifier=CanonicalIdentifier(
chain_identifier=ChainID(61),
channel_identifier=channel_id,
token_network_address=TokenNetworkAddress(token_network_model.address),
),
updating_participant=address2,
other_participant=address1,
updating_nonce=Nonce(2),
other_nonce=Nonce(1),
updating_capacity=deposit2,
other_capacity=deposit1,
reveal_timeout=BlockTimeout(2),
signature=EMPTY_SIGNATURE,
),
updating_capacity_partner=TokenAmount(deposit1),
other_capacity_partner=TokenAmount(deposit2),
)
participant1=participant1,
participant2=participant2,
settle_timeout=settle_timeout,
)
token_network.handle_channel_balance_update_message(
PFSCapacityUpdate(
canonical_identifier=CanonicalIdentifier(
chain_identifier=ChainID(61),
channel_identifier=ChannelID(channel_id),
token_network_address=TokenNetworkAddress(token_network.address),
),
updating_participant=addresses[p1_index],
other_participant=addresses[p2_index],
updating_nonce=Nonce(1),
other_nonce=Nonce(1),
updating_capacity=p1_capacity,
other_capacity=p2_capacity,
reveal_timeout=p1_reveal_timeout,
signature=EMPTY_SIGNATURE,
),
updating_capacity_partner=TokenAmount(0),
other_capacity_partner=TokenAmount(0),
)
token_network.handle_channel_balance_update_message(
PFSCapacityUpdate(
canonical_identifier=CanonicalIdentifier(
chain_identifier=ChainID(61),
channel_identifier=ChannelID(channel_id),
token_network_address=TokenNetworkAddress(token_network.address),
),
updating_participant=addresses[p2_index],
assert service_registry.functions.hasValidRegistration(monitoring_service.address).call()
# each client does a transfer
channel_id = create_channel(c1, c2, settle_timeout=5)[0]
shared_bp_args = dict(
channel_identifier=channel_id,
token_network_address=decode_hex(token_network.address),
chain_id=monitoring_service.chain_id,
additional_hash="0x%064x" % 0,
locked_amount=TokenAmount(0),
locksroot=encode_hex(LOCKSROOT_OF_NO_LOCKS),
)
transferred_c1 = 5
balance_proof_c1 = HashedBalanceProof(
nonce=Nonce(1),
transferred_amount=transferred_c1,
priv_key=get_private_key(c1),
**shared_bp_args
)
transferred_c2 = 6
balance_proof_c2 = HashedBalanceProof(
nonce=Nonce(2),
transferred_amount=transferred_c2,
priv_key=get_private_key(c2),
**shared_bp_args
)
ms_greenlet = gevent.spawn(monitoring_service.start)
# need to wait here till the MS has some time to react
gevent.sleep(0.01)
assert service_registry.functions.hasValidRegistration(monitoring_service.address).call()
# each client does a transfer
channel_id = create_channel(c1, c2, settle_timeout=10)[0]
shared_bp_args = dict(
channel_identifier=channel_id,
token_network_address=decode_hex(token_network.address),
chain_id=monitoring_service.chain_id,
additional_hash="0x%064x" % 0,
locked_amount=TokenAmount(0),
locksroot=encode_hex(LOCKSROOT_OF_NO_LOCKS),
)
transferred_c1 = 5
balance_proof_c1 = HashedBalanceProof(
nonce=Nonce(1),
transferred_amount=transferred_c1,
priv_key=get_private_key(c1),
**shared_bp_args
)
transferred_c2 = 6
balance_proof_c2 = HashedBalanceProof(
nonce=Nonce(2),
transferred_amount=transferred_c2,
priv_key=get_private_key(c2),
**shared_bp_args
)
monitoring_service._process_new_blocks(web3.eth.blockNumber)
assert len(monitoring_service.context.database.get_token_network_addresses()) > 0
# c1 asks MS to monitor the channel
reward_amount = TokenAmount(1)
def handle_contract_send_channelclose(
raiden: "RaidenService",
chain_state: ChainState,
channel_close_event: ContractSendChannelClose,
) -> None:
balance_proof = channel_close_event.balance_proof
if balance_proof:
nonce = balance_proof.nonce
balance_hash = balance_proof.balance_hash
signature_in_proof = balance_proof.signature
message_hash = balance_proof.message_hash
canonical_identifier = balance_proof.canonical_identifier
else:
nonce = Nonce(0)
balance_hash = EMPTY_BALANCE_HASH
signature_in_proof = EMPTY_SIGNATURE
message_hash = EMPTY_MESSAGE_HASH
canonical_identifier = channel_close_event.canonical_identifier
closing_data = pack_signed_balance_proof(
msg_type=MessageTypeId.BALANCE_PROOF,
nonce=nonce,
balance_hash=balance_hash,
additional_hash=message_hash,
canonical_identifier=canonical_identifier,
partner_signature=signature_in_proof,
)
our_signature = raiden.signer.sign(data=closing_data)
def get_current_balanceproof(end_state: NettingChannelEndState) -> BalanceProofData:
balance_proof = end_state.balance_proof
if balance_proof:
locksroot = balance_proof.locksroot
nonce = end_state.nonce
transferred_amount = balance_proof.transferred_amount
locked_amount = get_amount_locked(end_state)
else:
locksroot = Locksroot(LOCKSROOT_OF_NO_LOCKS)
nonce = Nonce(0)
transferred_amount = TokenAmount(0)
locked_amount = TokenAmount(0)
return locksroot, nonce, transferred_amount, locked_amount
Locksroot: BytesField,
Secret: BytesField,
SecretHash: BytesField,
Signature: BytesField,
TransactionHash: BytesField,
# Ints
BlockExpiration: IntegerToStringField,
BlockNumber: IntegerToStringField,
BlockTimeout: IntegerToStringField,
TokenAmount: IntegerToStringField,
FeeAmount: IntegerToStringField,
ProportionalFeeAmount: IntegerToStringField,
LockedAmount: IntegerToStringField,
BlockGasLimit: IntegerToStringField,
MessageID: IntegerToStringField,
Nonce: IntegerToStringField,
PaymentAmount: IntegerToStringField,
PaymentID: IntegerToStringField,
PaymentWithFeeAmount: IntegerToStringField,
TransferID: IntegerToStringField,
WithdrawAmount: IntegerToStringField,
Optional[BlockNumber]: OptionalIntegerToStringField,
# Integers which should be converted to strings
# This is done for querying purposes as sqlite
# integer type is smaller than python's.
ChainID: IntegerToStringField,
ChannelID: IntegerToStringField,
# Polymorphic fields
TransferTask: CallablePolyField(
serialization_schema_selector=transfer_task_schema_serialization,
deserialization_schema_selector=transfer_task_schema_deserialization,
),
class Channel:
# pylint: disable=too-many-instance-attributes
token_network_address: TokenNetworkAddress = field(
metadata={"marshmallow_field": ChecksumAddress(required=True)}
)
channel_id: ChannelID
participant1: Address = field(metadata={"marshmallow_field": ChecksumAddress(required=True)})
participant2: Address = field(metadata={"marshmallow_field": ChecksumAddress(required=True)})
settle_timeout: BlockTimeout
fee_schedule1: FeeSchedule = field(default_factory=FeeSchedule)
fee_schedule2: FeeSchedule = field(default_factory=FeeSchedule)
# Set by PFSCapacityUpdate
capacity1: TokenAmount = TokenAmount(0)
capacity2: TokenAmount = TokenAmount(0)
update_nonce1: Nonce = Nonce(0)
update_nonce2: Nonce = Nonce(0)
reveal_timeout1: BlockTimeout = DEFAULT_REVEAL_TIMEOUT
reveal_timeout2: BlockTimeout = DEFAULT_REVEAL_TIMEOUT
Schema: ClassVar[Type[marshmallow.Schema]]
@property
def views(self) -> Tuple["ChannelView", "ChannelView"]:
return ChannelView(channel=self), ChannelView(channel=self, reverse=True)
@dataclass
class ChannelView:
"""
Unidirectional view of a bidirectional channel
def get_next_transaction(self) -> TransactionSlot:
with self._nonce_lock:
slot = TransactionSlot(self, self._available_nonce)
self._available_nonce = Nonce(self._available_nonce + 1)
return slot