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
use std::collections::HashMap;
use async_trait::async_trait;
use fuel_storage::Storage;
use fuel_types::{Address, AssetId, Bytes32, Word};
use tokio::sync::oneshot;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct DepositCoin {
pub owner: Address,
pub amount: Word,
pub asset_id: AssetId,
pub deposited_da_height: u64,
pub fuel_block_spend: Option<u64>,
}
#[async_trait]
pub trait RelayerDb:
Storage<Bytes32, DepositCoin, Error = KvStoreError>
+ Storage<Address, u64,Error = KvStoreError>
+ Storage<u64, HashMap<Address, u64>,Error = KvStoreError>
+ Send
+ Sync
{
async fn insert_token_deposit(
&mut self,
deposit_nonce: Bytes32,
deposited_da_height: u64,
owner: Address,
asset_id: AssetId,
amount: Word,
) {
let coin = DepositCoin {
owner,
amount,
asset_id,
deposited_da_height,
fuel_block_spend: None,
};
let _ = Storage::<Bytes32, DepositCoin>::insert(self,&deposit_nonce,&coin);
}
async fn insert_validators_diff(&mut self, da_height: u64, stakes: &HashMap<Address, u64>) {
let _ = Storage::<u64,HashMap<Address,u64>>::insert(self, &da_height,stakes);
}
async fn get_validator_diffs(
&self,
from_da_height: u64,
to_da_height: Option<u64>,
) -> Vec<(u64,HashMap<Address, u64>)>;
async fn apply_validator_diffs(&mut self, changes: &HashMap<Address,u64>, da_height: u64) {
for ( address, stake) in changes {
let _ = Storage::<Address,u64>::insert(self,address,stake);
}
self.set_validators_da_height(da_height).await;
}
async fn get_block_height(&self) -> u64;
async fn get_validators(&self) -> HashMap<Address,u64>;
async fn set_validators_da_height(&self, block: u64);
async fn get_validators_da_height(&self) -> u64;
async fn set_finalized_da_height(&self, block: u64);
async fn get_finalized_da_height(&self) -> u64;
}
#[derive(Debug)]
pub enum RelayerEvent {
GetValidatorSet {
da_height: u64,
response_channel: oneshot::Sender<Result<HashMap<Address, u64>, RelayerError>>,
},
GetStatus {
response: oneshot::Sender<RelayerStatus>,
},
Stop,
}
pub use thiserror::Error;
use crate::db::KvStoreError;
#[derive(Error, Debug, PartialEq, Eq, Copy, Clone)]
pub enum RelayerError {
#[error("Temp stopped")]
Stopped,
#[error("Temp ProviderError")]
ProviderError,
#[error("Validator Set not returned, waiting for eth client sync")]
ValidatorSetEthClientSyncing,
#[error("Asked for unknown eth block")]
InitialSyncAskedForUnknownBlock,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DaSyncState {
RelayerSyncing,
OverlapingSync,
Synced,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RelayerStatus {
DaClientNotConnected,
DaClientIsSyncing,
DaClientSynced(DaSyncState),
Stop,
}