1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use chrono::Utc;
use raiden_blockchain::keys::PrivateKey;
use raiden_primitives::{
	serializers::u256_to_str,
	traits::ToBytes,
	types::{
		Address,
		CanonicalIdentifier,
		Nonce,
		RevealTimeout,
		Signature,
		TokenAmount,
		U256,
	},
};
use raiden_state_machine::{
	types::{
		ChannelState,
		FeeScheduleState,
	},
	views,
};
use serde::{
	Deserialize,
	Serialize,
};
use serde_rlp::ser::to_bytes as rlp_to_bytes;
use web3::signing::SigningError;

use super::SignedMessage;

/// Message to inform a pathfinding service about a capacity change.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub struct PFSCapacityUpdate {
	pub canonical_identifier: CanonicalIdentifier,
	pub updating_participant: Address,
	pub other_participant: Address,
	#[serde(serialize_with = "u256_to_str")]
	pub updating_nonce: Nonce,
	#[serde(serialize_with = "u256_to_str")]
	pub other_nonce: Nonce,
	#[serde(serialize_with = "u256_to_str")]
	pub updating_capacity: TokenAmount,
	#[serde(serialize_with = "u256_to_str")]
	pub other_capacity: TokenAmount,
	pub reveal_timeout: RevealTimeout,
	pub signature: Signature,
}

impl From<ChannelState> for PFSCapacityUpdate {
	fn from(channel: ChannelState) -> Self {
		Self {
			canonical_identifier: channel.canonical_identifier,
			updating_participant: channel.our_state.address,
			other_participant: channel.partner_state.address,
			updating_nonce: channel.our_state.nonce,
			other_nonce: channel.partner_state.nonce,
			updating_capacity: views::channel_distributable(
				&channel.our_state,
				&channel.partner_state,
			),
			other_capacity: views::channel_distributable(
				&channel.partner_state,
				&channel.our_state,
			),
			reveal_timeout: channel.reveal_timeout,
			signature: Signature::default(),
		}
	}
}

impl SignedMessage for PFSCapacityUpdate {
	fn bytes_to_sign(&self) -> Vec<u8> {
		let chain_id: U256 = self.canonical_identifier.chain_identifier.into();

		let mut channel_identifier = [0u8; 32];
		self.canonical_identifier
			.channel_identifier
			.to_big_endian(&mut channel_identifier);

		let updating_nonce = self.updating_nonce.as_u64().to_be_bytes();
		let other_nonce = self.other_nonce.as_u64().to_be_bytes();

		let mut updating_capacity = [0u8; 32];
		self.updating_capacity.to_big_endian(&mut updating_capacity);

		let mut other_capacity = [0u8; 32];
		self.other_capacity.to_big_endian(&mut other_capacity);

		let reveal_timeout: U256 = self.reveal_timeout.into();

		let mut bytes = vec![];
		bytes.extend_from_slice(&chain_id.to_bytes());
		bytes.extend_from_slice(self.canonical_identifier.token_network_address.as_bytes());
		bytes.extend_from_slice(&channel_identifier);
		bytes.extend_from_slice(self.updating_participant.as_bytes());
		bytes.extend_from_slice(self.other_participant.as_bytes());
		bytes.extend_from_slice(&updating_nonce);
		bytes.extend_from_slice(&other_nonce);
		bytes.extend_from_slice(&updating_capacity);
		bytes.extend_from_slice(&other_capacity);
		bytes.extend_from_slice(&reveal_timeout.to_bytes());

		bytes
	}

	fn sign(&mut self, key: PrivateKey) -> Result<(), SigningError> {
		self.signature = self.sign_message(key)?.to_bytes().into();
		Ok(())
	}
}

/// Informs the PFS of mediation fees demanded by the client.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub struct PFSFeeUpdate {
	pub canonical_identifier: CanonicalIdentifier,
	pub updating_participant: Address,
	pub fee_schedule: FeeScheduleState,
	pub timestamp: String,
	pub signature: Signature,
}

impl From<ChannelState> for PFSFeeUpdate {
	fn from(channel: ChannelState) -> Self {
		let timestamp = Utc::now().naive_local().format("%Y-%m-%dT%H:%M:%S").to_string();

		Self {
			canonical_identifier: channel.canonical_identifier,
			updating_participant: channel.our_state.address,
			fee_schedule: channel.fee_schedule,
			timestamp,
			signature: Signature::default(),
		}
	}
}

impl SignedMessage for PFSFeeUpdate {
	fn bytes_to_sign(&self) -> Vec<u8> {
		let chain_id: U256 = self.canonical_identifier.chain_identifier.into();
		let mut channel_identifier = [0u8; 32];
		self.canonical_identifier
			.channel_identifier
			.to_big_endian(&mut channel_identifier);

		let imbalance_penalty =
			if let Some(imbalance_penalty) = &self.fee_schedule.imbalance_penalty {
				let imbalance_penalty: Vec<_> =
					imbalance_penalty.iter().map(|(a, b)| (a.as_u128(), b.as_u128())).collect();
				rlp_to_bytes(&imbalance_penalty).expect("Should be able to serialize")
			} else {
				vec![128]
			};

		let mut bytes = vec![];
		bytes.extend_from_slice(&chain_id.to_bytes());
		bytes.extend_from_slice(self.canonical_identifier.token_network_address.as_bytes());
		bytes.extend_from_slice(&channel_identifier);
		bytes.extend_from_slice(self.updating_participant.as_bytes());
		bytes.push(self.fee_schedule.cap_fees as u8);
		bytes.extend_from_slice(&self.fee_schedule.flat.to_bytes());
		bytes.extend_from_slice(&self.fee_schedule.proportional.to_bytes());
		bytes.extend_from_slice(&imbalance_penalty);
		bytes.extend_from_slice(&self.timestamp.clone().into_bytes());
		bytes
	}

	fn sign(&mut self, key: PrivateKey) -> Result<(), SigningError> {
		self.signature = self.sign_message(key)?.to_bytes().into();
		Ok(())
	}
}