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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::str::FromStr;

use client_contracts::{
    error::BridgeContractError,
    inbound_types::{Message, OutboundLaneData, Payload},
    outbound::MessageAccepted,
    ChainMessageCommitter, Inbound, LaneMessageCommitter, Outbound,
};
use futures::future;
use support_common::error::BridgerError;
use thegraph_liketh::graph::TheGraphLikeEth;
use web3::{
    contract::tokens::Tokenizable,
    ethabi::{encode, RawLog},
    signing::keccak256,
    transports::Http,
    types::{Address, BlockId, BlockNumber, Bytes, FilterBuilder, Proof as Web3Proof, H256, U256},
    Web3,
};

use crate::goerli_client::types::MessagesConfirmationProof;

use super::message_client::{LANE_IDENTIFY_SLOT, LANE_MESSAGE_SLOT, LANE_NONCE_SLOT};

pub async fn build_darwinia_delivery_proof(
    outbound: &Outbound,
    lane_message_committer: &LaneMessageCommitter,
    chain_message_committer: &ChainMessageCommitter,
    block_id: Option<BlockId>,
) -> color_eyre::Result<Bytes> {
    let (_, lane_pos, _, _) = outbound.get_lane_info().await?;

    build_darwinia_proof(
        lane_message_committer,
        chain_message_committer,
        lane_pos,
        block_id,
    )
    .await
}

pub async fn build_darwinia_confirmation_proof(
    inbound: &Inbound,
    lane_message_committer: &LaneMessageCommitter,
    chain_message_committer: &ChainMessageCommitter,
    block_id: Option<BlockId>,
) -> color_eyre::Result<Bytes> {
    let (_, lane_pos, _, _) = inbound.get_lane_info(block_id).await?;
    build_darwinia_proof(
        lane_message_committer,
        chain_message_committer,
        lane_pos,
        block_id,
    )
    .await
}

async fn build_darwinia_proof(
    lane_message_committer: &LaneMessageCommitter,
    chain_message_committer: &ChainMessageCommitter,
    lane_pos: u32,
    block_id: Option<BlockId>,
) -> color_eyre::Result<Bytes> {
    let bridged_chain_pos = lane_message_committer.bridged_chain_position().await?;
    let proof = chain_message_committer
        .prove(bridged_chain_pos, U256::from(lane_pos), block_id)
        .await?
        .into_token();

    Ok(Bytes(encode(&[proof])))
}

pub async fn build_eth_confirmation_proof(
    client: &Web3<Http>,
    inbound: &Inbound,
    begin: u64,
    end: u64,
    block_number: Option<BlockNumber>,
) -> color_eyre::Result<Bytes> {
    let lane_id_proof = build_eth_proof(
        client,
        inbound.contract.address(),
        vec![U256::from(LANE_IDENTIFY_SLOT)],
        block_number,
    )
    .await?
    .ok_or_else(|| BridgerError::Custom("Failed to get lane_id_proof".into()))?;
    let lane_nonce_proof = build_eth_proof(
        client,
        inbound.contract.address(),
        vec![U256::from(LANE_NONCE_SLOT)],
        block_number,
    )
    .await?
    .ok_or_else(|| BridgerError::Custom("Failed to get lane_nonce_proof".into()))?;
    let relayer_keys = build_relayer_keys(begin, end)?;
    let lane_relayers_proof = build_eth_proof(
        client,
        inbound.contract.address(),
        relayer_keys,
        block_number,
    )
    .await?
    .ok_or_else(|| BridgerError::Custom("Failed to get lane_nonce_proof".into()))?;

    let proof = MessagesConfirmationProof {
        account_proof: encode_proof(&lane_id_proof.account_proof),
        lane_nonce_proof: encode_proof(&lane_nonce_proof.storage_proof[0].proof),
        lane_relayers_proof: lane_relayers_proof
            .storage_proof
            .iter()
            .map(|x| encode_proof(&x.proof))
            .collect(),
    };
    Ok(Bytes(encode(&[proof.get_token()?])))
}

async fn build_eth_proof(
    client: &Web3<Http>,
    address: Address,
    storage_keys: Vec<U256>,
    block_number: Option<BlockNumber>,
) -> color_eyre::Result<Option<Web3Proof>> {
    Ok(client
        .eth()
        .proof(address, storage_keys, block_number)
        .await?)
}

pub fn build_relayer_keys(begin: u64, end: u64) -> color_eyre::Result<Vec<U256>> {
    let mut result: Vec<U256> = Vec::new();
    for pos in begin..=end {
        let pos = U256::from(pos);
        let slot = U256::from(LANE_MESSAGE_SLOT);
        let bytes: &mut [u8] = &mut [0u8; 64];
        pos.to_big_endian(&mut bytes[..32]);
        slot.to_big_endian(&mut bytes[32..]);
        let key1 = U256::from(keccak256(bytes));
        let key2 = key1
            .checked_add(U256::from(1u64))
            .ok_or_else(|| BridgerError::Custom("Failed to build relayer keys".into()))?;
        result.push(key1);
        result.push(key2);
    }
    Ok(result)
}

pub fn encode_proof(proofs: &[Bytes]) -> Bytes {
    Bytes::from(
        &rlp::encode_list::<Vec<u8>, _>(
            proofs
                .iter()
                .map(|x| x.0.clone())
                .collect::<Vec<Vec<u8>>>()
                .as_slice(),
        )[..],
    )
}

pub async fn build_messages_data(
    indexer: &TheGraphLikeEth,
    outbound: &Outbound,
    begin: u64,
    end: u64,
    at_block: Option<BlockNumber>,
) -> color_eyre::Result<OutboundLaneData> {
    let outbound_data = outbound.data(at_block.map(BlockId::from)).await?;
    let outbound_lane_nonce = outbound
        .outbound_lane_nonce(at_block.map(BlockId::from))
        .await?;
    let (outbound_begin, _outbound_end) = (
        outbound_lane_nonce.latest_received_nonce + 1,
        outbound_lane_nonce.latest_generated_nonce,
    );
    let messages = Vec::from_iter(
        outbound_data.messages[(begin - outbound_begin) as usize..=(end - outbound_begin) as usize]
            .iter()
            .cloned(),
    );

    if (end - begin + 1) as usize != messages.len() {
        return Err(BridgerError::Custom("Build messages data failed".into()).into());
    }

    let accepted_events = query_message_accepted_events_thegraph(indexer, begin, end).await?;
    let messages: Vec<Message> = std::iter::zip(messages, accepted_events)
        .into_iter()
        .map(|(message, event)| Message {
            encoded_key: message.encoded_key,
            payload: Payload {
                source: event.source,
                target: event.target,
                encoded: event.encoded,
            },
        })
        .collect();

    Ok(OutboundLaneData {
        latest_received_nonce: outbound_data.latest_received_nonce,
        messages,
    })
}

#[allow(dead_code)]
pub async fn query_message_accepted(
    client: &Web3<Http>,
    outbound: &Outbound,
    nonce: u64,
) -> color_eyre::Result<Option<MessageAccepted>> {
    let event = outbound.contract.abi().event("MessageAccepted")?;
    let mut filter = FilterBuilder::default();
    filter = filter.from_block(BlockNumber::Earliest);
    filter = filter.address(vec![outbound.contract.address()]);
    filter = filter.topics(
        Some(vec![event.signature()]),
        Some(vec![H256::from_low_u64_be(nonce)]),
        None,
        None,
    );
    let logs = client.eth().logs(filter.build()).await?;

    let events: Vec<MessageAccepted> = logs
        .into_iter()
        .map(|l| {
            let row_log = RawLog {
                topics: l.topics.clone(),
                data: l.data.0.clone(),
            };
            let block_number = l
                .block_number
                .ok_or_else(|| BridgeContractError::Custom("Failed toget block number".into()))?
                .as_u64();
            MessageAccepted::from_log(event.parse_log(row_log)?, block_number)
        })
        .collect::<Result<Vec<MessageAccepted>, BridgeContractError>>()?;
    match events.as_slice() {
        [x] => Ok(Some(x.clone())),
        _ => Ok(None),
    }
}

#[allow(dead_code)]
pub async fn query_message_accepted_events(
    client: &Web3<Http>,
    outbound: &Outbound,
    begin: u64,
    end: u64,
) -> color_eyre::Result<Vec<MessageAccepted>> {
    let logs: Result<Vec<Option<MessageAccepted>>, _> = future::try_join_all(
        (begin..=end).map(|nonce| query_message_accepted(client, outbound, nonce)),
    )
    .await;
    if let Some(logs) = logs?.into_iter().collect::<Option<Vec<_>>>() {
        Ok(logs)
    } else {
        Err(BridgerError::Custom(format!(
            "Failed to get message events from {:?} to {:?}",
            begin, end
        ))
        .into())
    }
}

pub async fn query_message_accepted_thegraph(
    thegraph_client: &TheGraphLikeEth,
    nonce: u64,
) -> color_eyre::Result<Option<MessageAccepted>> {
    thegraph_client
        .query_message_accepted(nonce)
        .await?
        .map(|x| -> color_eyre::Result<MessageAccepted> {
            Ok(MessageAccepted {
                nonce: x.nonce,
                source: Address::from_str(&x.source)?,
                target: Address::from_str(&x.target)?,
                encoded: Bytes(hex::decode(&x.encoded[2..])?),
                block_number: x.block_number,
            })
        })
        .transpose()
}

pub async fn query_message_accepted_events_thegraph(
    client: &TheGraphLikeEth,
    begin: u64,
    end: u64,
) -> color_eyre::Result<Vec<MessageAccepted>> {
    let logs: Result<Vec<Option<MessageAccepted>>, _> = future::try_join_all(
        (begin..=end).map(|nonce| query_message_accepted_thegraph(client, nonce)),
    )
    .await;
    if let Some(logs) = logs?.into_iter().collect::<Option<Vec<_>>>() {
        Ok(logs)
    } else {
        Err(BridgerError::Custom(format!(
            "Failed to get message events from {:?} to {:?}",
            begin, end
        ))
        .into())
    }
}