-
Notifications
You must be signed in to change notification settings - Fork 38
/
message_to_mainnet.rs
108 lines (94 loc) · 3.29 KB
/
message_to_mainnet.rs
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
use ethereum_types::{Address, U256, H256};
use contracts::foreign::events::Withdraw;
use web3::types::Log;
use ethabi;
use error::Error;
/// the message that is relayed from side to main.
/// contains all the information required for the relay.
/// validators sign off on this message.
#[derive(PartialEq, Debug)]
pub struct MessageToMainnet {
pub recipient: Address,
pub value: U256,
pub sidenet_transaction_hash: H256,
pub mainnet_gas_price: U256,
}
/// length of a `MessageToMainnet.to_bytes()` in bytes
pub const MESSAGE_LENGTH: usize = 116;
impl MessageToMainnet {
/// parses message from a byte slice
pub fn from_bytes(bytes: &[u8]) -> Self {
assert_eq!(bytes.len(), MESSAGE_LENGTH);
Self {
recipient: bytes[0..20].into(),
value: (&bytes[20..52]).into(),
sidenet_transaction_hash: bytes[52..84].into(),
mainnet_gas_price: (&bytes[84..MESSAGE_LENGTH]).into(),
}
}
/// construct a message from a `Withdraw` event that was logged on `foreign`
pub fn from_log(web3_log: Log) -> Result<Self, Error> {
let ethabi_raw_log = ethabi::RawLog {
topics: web3_log.topics,
data: web3_log.data.0,
};
let withdraw_log = Withdraw::default().parse_log(ethabi_raw_log)?;
let hash = web3_log.transaction_hash.ok_or_else(|| "`log` must be mined and contain `transaction_hash`")?;
Ok(Self {
recipient: withdraw_log.recipient,
value: withdraw_log.value,
sidenet_transaction_hash: hash,
mainnet_gas_price: withdraw_log.home_gas_price,
})
}
/// serializes message to a byte vector.
/// mainly used to construct the message byte vector that is then signed
/// and passed to `ForeignBridge.submitSignature`
pub fn to_bytes(&self) -> Vec<u8> {
let mut result = vec![0u8; MESSAGE_LENGTH];
result[0..20].copy_from_slice(&self.recipient.0[..]);
result[20..52].copy_from_slice(&H256::from(self.value));
result[52..84].copy_from_slice(&self.sidenet_transaction_hash.0[..]);
result[84..MESSAGE_LENGTH].copy_from_slice(&H256::from(self.mainnet_gas_price));
return result;
}
/// serializes message to an ethabi payload
pub fn to_payload(&self) -> Vec<u8> {
ethabi::encode(&[ethabi::Token::Bytes(self.to_bytes())])
}
}
#[cfg(test)]
mod test {
use quickcheck::TestResult;
use super::*;
quickcheck! {
fn quickcheck_message_to_mainnet_roundtrips_to_bytes(
recipient_raw: Vec<u8>,
value_raw: u64,
sidenet_transaction_hash_raw: Vec<u8>,
mainnet_gas_price_raw: u64
) -> TestResult {
if recipient_raw.len() != 20 || sidenet_transaction_hash_raw.len() != 32 {
return TestResult::discard();
}
let recipient: Address = recipient_raw.as_slice().into();
let value: U256 = value_raw.into();
let sidenet_transaction_hash: H256 = sidenet_transaction_hash_raw.as_slice().into();
let mainnet_gas_price: U256 = mainnet_gas_price_raw.into();
let message = MessageToMainnet {
recipient,
value,
sidenet_transaction_hash,
mainnet_gas_price
};
let bytes = message.to_bytes();
assert_eq!(message, MessageToMainnet::from_bytes(bytes.as_slice()));
let payload = message.to_payload();
let mut tokens = ethabi::decode(&[ethabi::ParamType::Bytes], payload.as_slice())
.unwrap();
let decoded = tokens.pop().unwrap().to_bytes().unwrap();
assert_eq!(message, MessageToMainnet::from_bytes(decoded.as_slice()));
TestResult::passed()
}
}
}