Skip to content

Commit

Permalink
Timestamp estimation now working
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Aug 27, 2023
1 parent 08a128a commit 1804cab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli/src/modules/cos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Cos {
}

let resp = rpc
.get_daa_score_timestamp_estimate_call(GetDaaScoreTimestampEstimateRequest { daa_score: 500 })
.get_daa_score_timestamp_estimate_call(GetDaaScoreTimestampEstimateRequest { daa_score: 42976852 })
.await;
let data = resp.unwrap().timestamp;

Expand Down
25 changes: 25 additions & 0 deletions rpc/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,31 @@ impl RpcApi for RpcCoreService {
&self,
request: GetDaaScoreTimestampEstimateRequest,
) -> RpcResult<GetDaaScoreTimestampEstimateResponse> {
let session = self.consensus_manager.consensus().session().await;
let headers = session.async_pruning_point_headers().await;

// Process from the highest daa_score and timestamp first
for (idx, header) in headers.iter().rev().enumerate() {
if header.daa_score <= request.daa_score {
// For the last element, we estimate excess time as seconds
let mut time_adjustment: u64 = 0;

if idx == 0 {
// Assume difference in daa_score corresponds to seconds since the basis header
time_adjustment = (request.daa_score - header.daa_score) * 1000;
} else {
// "next" header is the one that we processed last iteration
let next_header = headers.get(idx - 1).unwrap();
let time_between_now_and_next = next_header.timestamp - header.timestamp;
let score_between_now_and_request = request.daa_score - header.daa_score;
let score_between_now_and_next = next_header.daa_score - header.daa_score;

time_adjustment = (time_between_now_and_next) * (score_between_now_and_request / score_between_now_and_next);
}

return Ok(GetDaaScoreTimestampEstimateResponse::new(header.timestamp + time_adjustment));
}
}
// TODO: Add the logic here
Ok(GetDaaScoreTimestampEstimateResponse::new(request.daa_score))
}
Expand Down

0 comments on commit 1804cab

Please sign in to comment.