Skip to content

Commit 413d02b

Browse files
committed
feat: collect clock time offset from network peers
1 parent bb83898 commit 413d02b

6 files changed

Lines changed: 359 additions & 3 deletions

File tree

protocol/src/builder.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::protocol_generated::ckb::protocol::{
88
IndexTransactionBuilder, OutPoint as FbsOutPoint, OutPointBuilder,
99
ProposalShortId as FbsProposalShortId, RelayMessage, RelayMessageBuilder, RelayPayload,
1010
Script as FbsScript, ScriptBuilder, SyncMessage, SyncMessageBuilder, SyncPayload,
11-
Transaction as FbsTransaction, TransactionBuilder, UncleBlock as FbsUncleBlock,
12-
UncleBlockBuilder, H256 as FbsH256,
11+
Time as FbsTime, TimeBuilder, TimeMessage, TimeMessageBuilder, Transaction as FbsTransaction,
12+
TransactionBuilder, UncleBlock as FbsUncleBlock, UncleBlockBuilder, H256 as FbsH256,
1313
};
1414
use crate::{short_transaction_id, short_transaction_id_keys};
1515
use ckb_core::block::Block;
@@ -293,6 +293,14 @@ impl<'a> FbsGetBlocks<'a> {
293293
}
294294
}
295295

296+
impl<'a> FbsTime<'a> {
297+
pub fn build<'b>(fbb: &mut FlatBufferBuilder<'b>, timestamp: u64) -> WIPOffset<FbsTime<'b>> {
298+
let mut builder = TimeBuilder::new(fbb);
299+
builder.add_timestamp(timestamp);
300+
builder.finish()
301+
}
302+
}
303+
296304
impl<'a> SyncMessage<'a> {
297305
pub fn build_get_headers<'b>(
298306
fbb: &mut FlatBufferBuilder<'b>,
@@ -579,6 +587,19 @@ impl<'a> RelayMessage<'a> {
579587
builder.finish()
580588
}
581589
}
590+
591+
impl<'a> TimeMessage<'a> {
592+
pub fn build_time<'b>(
593+
fbb: &mut FlatBufferBuilder<'b>,
594+
timestamp: u64,
595+
) -> WIPOffset<TimeMessage<'b>> {
596+
let fbs_time = FbsTime::build(fbb, timestamp);
597+
let mut builder = TimeMessageBuilder::new(fbb);
598+
builder.add_payload(fbs_time);
599+
builder.finish()
600+
}
601+
}
602+
582603
#[cfg(test)]
583604
mod tests {
584605
use super::*;

protocol/src/protocol.fbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,12 @@ table FilteredBlock {
208208
transactions: [IndexTransaction];
209209
hashes: [H256];
210210
}
211+
212+
table TimeMessage {
213+
payload: Time;
214+
}
215+
216+
table Time {
217+
timestamp: uint64;
218+
}
219+

protocol/src/protocol_generated.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,6 +2997,158 @@ impl<'a: 'b, 'b> FilteredBlockBuilder<'a, 'b> {
29972997
}
29982998
}
29992999

3000+
pub enum TimeMessageOffset {}
3001+
#[derive(Copy, Clone, Debug, PartialEq)]
3002+
3003+
pub struct TimeMessage<'a> {
3004+
pub _tab: flatbuffers::Table<'a>,
3005+
}
3006+
3007+
impl<'a> flatbuffers::Follow<'a> for TimeMessage<'a> {
3008+
type Inner = TimeMessage<'a>;
3009+
#[inline]
3010+
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
3011+
Self {
3012+
_tab: flatbuffers::Table { buf: buf, loc: loc },
3013+
}
3014+
}
3015+
}
3016+
3017+
impl<'a> TimeMessage<'a> {
3018+
#[inline]
3019+
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
3020+
TimeMessage {
3021+
_tab: table,
3022+
}
3023+
}
3024+
#[allow(unused_mut)]
3025+
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
3026+
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
3027+
args: &'args TimeMessageArgs<'args>) -> flatbuffers::WIPOffset<TimeMessage<'bldr>> {
3028+
let mut builder = TimeMessageBuilder::new(_fbb);
3029+
if let Some(x) = args.payload { builder.add_payload(x); }
3030+
builder.finish()
3031+
}
3032+
3033+
pub const VT_PAYLOAD: flatbuffers::VOffsetT = 4;
3034+
3035+
#[inline]
3036+
pub fn payload(&self) -> Option<Time<'a>> {
3037+
self._tab.get::<flatbuffers::ForwardsUOffset<Time<'a>>>(TimeMessage::VT_PAYLOAD, None)
3038+
}
3039+
}
3040+
3041+
pub struct TimeMessageArgs<'a> {
3042+
pub payload: Option<flatbuffers::WIPOffset<Time<'a >>>,
3043+
}
3044+
impl<'a> Default for TimeMessageArgs<'a> {
3045+
#[inline]
3046+
fn default() -> Self {
3047+
TimeMessageArgs {
3048+
payload: None,
3049+
}
3050+
}
3051+
}
3052+
pub struct TimeMessageBuilder<'a: 'b, 'b> {
3053+
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
3054+
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
3055+
}
3056+
impl<'a: 'b, 'b> TimeMessageBuilder<'a, 'b> {
3057+
#[inline]
3058+
pub fn add_payload(&mut self, payload: flatbuffers::WIPOffset<Time<'b >>) {
3059+
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Time>>(TimeMessage::VT_PAYLOAD, payload);
3060+
}
3061+
#[inline]
3062+
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TimeMessageBuilder<'a, 'b> {
3063+
let start = _fbb.start_table();
3064+
TimeMessageBuilder {
3065+
fbb_: _fbb,
3066+
start_: start,
3067+
}
3068+
}
3069+
#[inline]
3070+
pub fn finish(self) -> flatbuffers::WIPOffset<TimeMessage<'a>> {
3071+
let o = self.fbb_.end_table(self.start_);
3072+
flatbuffers::WIPOffset::new(o.value())
3073+
}
3074+
}
3075+
3076+
pub enum TimeOffset {}
3077+
#[derive(Copy, Clone, Debug, PartialEq)]
3078+
3079+
pub struct Time<'a> {
3080+
pub _tab: flatbuffers::Table<'a>,
3081+
}
3082+
3083+
impl<'a> flatbuffers::Follow<'a> for Time<'a> {
3084+
type Inner = Time<'a>;
3085+
#[inline]
3086+
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
3087+
Self {
3088+
_tab: flatbuffers::Table { buf: buf, loc: loc },
3089+
}
3090+
}
3091+
}
3092+
3093+
impl<'a> Time<'a> {
3094+
#[inline]
3095+
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
3096+
Time {
3097+
_tab: table,
3098+
}
3099+
}
3100+
#[allow(unused_mut)]
3101+
pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
3102+
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
3103+
args: &'args TimeArgs) -> flatbuffers::WIPOffset<Time<'bldr>> {
3104+
let mut builder = TimeBuilder::new(_fbb);
3105+
builder.add_timestamp(args.timestamp);
3106+
builder.finish()
3107+
}
3108+
3109+
pub const VT_TIMESTAMP: flatbuffers::VOffsetT = 4;
3110+
3111+
#[inline]
3112+
pub fn timestamp(&self) -> u64 {
3113+
self._tab.get::<u64>(Time::VT_TIMESTAMP, Some(0)).unwrap()
3114+
}
3115+
}
3116+
3117+
pub struct TimeArgs {
3118+
pub timestamp: u64,
3119+
}
3120+
impl<'a> Default for TimeArgs {
3121+
#[inline]
3122+
fn default() -> Self {
3123+
TimeArgs {
3124+
timestamp: 0,
3125+
}
3126+
}
3127+
}
3128+
pub struct TimeBuilder<'a: 'b, 'b> {
3129+
fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>,
3130+
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
3131+
}
3132+
impl<'a: 'b, 'b> TimeBuilder<'a, 'b> {
3133+
#[inline]
3134+
pub fn add_timestamp(&mut self, timestamp: u64) {
3135+
self.fbb_.push_slot::<u64>(Time::VT_TIMESTAMP, timestamp, 0);
3136+
}
3137+
#[inline]
3138+
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> TimeBuilder<'a, 'b> {
3139+
let start = _fbb.start_table();
3140+
TimeBuilder {
3141+
fbb_: _fbb,
3142+
start_: start,
3143+
}
3144+
}
3145+
#[inline]
3146+
pub fn finish(self) -> flatbuffers::WIPOffset<Time<'a>> {
3147+
let o = self.fbb_.end_table(self.start_);
3148+
flatbuffers::WIPOffset::new(o.value())
3149+
}
3150+
}
3151+
30003152
#[inline]
30013153
pub fn get_root_as_sync_message<'a>(buf: &'a [u8]) -> SyncMessage<'a> {
30023154
flatbuffers::get_root::<SyncMessage<'a>>(buf)

src/cli/run_impl.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use ckb_shared::cachedb::CacheDB;
1515
use ckb_shared::index::ChainIndex;
1616
use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder};
1717
use ckb_shared::store::ChainKVStore;
18-
use ckb_sync::{Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID};
18+
use ckb_sync::{
19+
NetTimeProtocol, Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID, TIME_PROTOCOL_ID,
20+
};
1921
use crypto::secp::Generator;
2022
use log::info;
2123
use numext_fixed_hash::H256;
@@ -64,6 +66,8 @@ pub fn run(setup: Setup) {
6466
synchronizer.peers(),
6567
));
6668

69+
let net_time_checker = Arc::new(NetTimeProtocol::default());
70+
6771
let network_config = NetworkConfig::from(setup.configs.network);
6872
let protocol_base_name = "ckb";
6973
let protocols = vec![
@@ -79,6 +83,12 @@ pub fn run(setup: Setup) {
7983
RELAY_PROTOCOL_ID,
8084
&[1][..],
8185
),
86+
CKBProtocol::new(
87+
protocol_base_name.to_string(),
88+
net_time_checker as Arc<_>,
89+
TIME_PROTOCOL_ID,
90+
&[1][..],
91+
),
8292
];
8393
let network = Arc::new(
8494
NetworkService::run_in_thread(&network_config, protocols)

sync/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! https://github.com/nervosnetwork/rfcs/tree/master/rfcs/0000-block-sync-protocol
55
66
mod config;
7+
mod net_time_checker;
78
mod relayer;
89
mod synchronizer;
910
mod types;
@@ -12,6 +13,7 @@ mod types;
1213
mod tests;
1314

1415
pub use crate::config::Config;
16+
pub use crate::net_time_checker::NetTimeProtocol;
1517
pub use crate::relayer::Relayer;
1618
pub use crate::synchronizer::Synchronizer;
1719

@@ -29,6 +31,7 @@ pub const BLOCK_DOWNLOAD_WINDOW: u64 = 1024;
2931
pub const PER_FETCH_BLOCK_LIMIT: usize = 128;
3032
pub const SYNC_PROTOCOL_ID: ProtocolId = *b"syn";
3133
pub const RELAY_PROTOCOL_ID: ProtocolId = *b"rel";
34+
pub const TIME_PROTOCOL_ID: ProtocolId = *b"tim";
3235

3336
// Timeout = base + per_header * (expected number of headers)
3437
pub const HEADERS_DOWNLOAD_TIMEOUT_BASE: u64 = 15 * 60 * 1000; // 15 minutes

0 commit comments

Comments
 (0)