-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathlight_client.rs
171 lines (144 loc) · 4.9 KB
/
light_client.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
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
use tendermint_light_client::{
components::{
clock::SystemClock,
io::{AtHeight, Io, ProdIo},
scheduler,
verifier::ProdVerifier,
},
fork_detector::ProdForkDetector,
light_client::{self, LightClient},
peer_list::PeerList,
state::State,
store::{sled::SledStore, LightStore, VerifiedStatus},
supervisor::{Instance, Supervisor},
types::{Height, PeerId, Time, TrustThreshold},
};
use gumdrop::Options;
use std::collections::HashMap;
use std::{
path::{Path, PathBuf},
time::Duration,
};
#[derive(Debug, Options)]
struct CliOptions {
#[options(help = "print this help message")]
help: bool,
#[options(help = "enable verbose output")]
verbose: bool,
#[options(command)]
command: Option<Command>,
}
#[derive(Debug, Options)]
enum Command {
#[options(help = "run the light client and continuously sync up to the latest block")]
Sync(SyncOpts),
}
#[derive(Debug, Options)]
struct SyncOpts {
#[options(help = "show help for this command")]
help: bool,
#[options(
help = "address of the Tendermint node to connect to",
meta = "ADDR",
default = "tcp://127.0.0.1:26657"
)]
address: tendermint::net::Address,
#[options(
help = "height of the initial trusted state (optional if store already initialized)",
meta = "HEIGHT"
)]
trusted_height: Option<Height>,
#[options(
help = "path to the database folder",
meta = "PATH",
default = "./lightstore"
)]
db_path: PathBuf,
}
fn main() {
let opts = CliOptions::parse_args_default_or_exit();
match opts.command {
None => {
eprintln!("Please specify a command:");
eprintln!("{}\n", CliOptions::command_list().unwrap());
eprintln!("{}\n", CliOptions::usage());
std::process::exit(1);
}
Some(Command::Sync(sync_opts)) => sync_cmd(sync_opts),
}
}
fn make_instance(
peer_id: PeerId,
addr: tendermint::net::Address,
db_path: impl AsRef<Path>,
opts: &SyncOpts,
) -> Instance {
let mut peer_map = HashMap::new();
peer_map.insert(peer_id, addr);
let timeout = Duration::from_secs(10);
let io = ProdIo::new(peer_map, Some(timeout));
let db = sled::open(db_path).unwrap_or_else(|e| {
println!("[ error ] could not open database: {}", e);
std::process::exit(1);
});
let mut light_store = SledStore::new(db);
if let Some(height) = opts.trusted_height {
let trusted_state = io
.fetch_light_block(peer_id, AtHeight::At(height))
.unwrap_or_else(|e| {
println!("[ error ] could not retrieve trusted header: {}", e);
std::process::exit(1);
});
light_store.insert(trusted_state, VerifiedStatus::Verified);
} else {
if light_store.highest(VerifiedStatus::Verified).is_none() {
println!("[ error ] no trusted state in database, please specify a trusted header");
std::process::exit(1);
}
}
let state = State {
light_store: Box::new(light_store),
verification_trace: HashMap::new(),
};
let options = light_client::Options {
trust_threshold: TrustThreshold {
numerator: 1,
denominator: 3,
},
trusting_period: Duration::from_secs(36000),
clock_drift: Duration::from_secs(1),
now: Time::now(),
};
let verifier = ProdVerifier::default();
let clock = SystemClock;
let scheduler = scheduler::basic_bisecting_schedule;
let light_client = LightClient::new(peer_id, options, clock, scheduler, verifier, io);
Instance::new(light_client, state)
}
fn sync_cmd(opts: SyncOpts) {
let addr = opts.address.clone();
let primary: PeerId = "BADFADAD0BEFEEDC0C0ADEADBEEFC0FFEEFACADE".parse().unwrap();
let witness: PeerId = "CEFEEDBADFADAD0C0CEEFACADE0ADEADBEEFC0FF".parse().unwrap();
let primary_path = opts.db_path.clone().join(primary.to_string());
let witness_path = opts.db_path.clone().join(witness.to_string());
let primary_instance = make_instance(primary, addr.clone(), primary_path, &opts);
let witness_instance = make_instance(witness, addr.clone(), witness_path, &opts);
let peer_list = PeerList::builder()
.primary(primary, primary_instance)
.witness(witness, witness_instance)
.build();
let mut supervisor = Supervisor::new(peer_list, ProdForkDetector::default());
let mut handle = supervisor.handle();
std::thread::spawn(|| supervisor.run());
loop {
handle.verify_to_highest_async(|result| match result {
Ok(light_block) => {
println!("[ info ] synced to block {}", light_block.height());
}
Err(e) => {
println!("[ error ] sync failed: {}", e);
}
});
std::thread::sleep(Duration::from_millis(800));
}
}