Skip to content

Commit

Permalink
feat: add mined_timestamp to wallet.db (#4267)
Browse files Browse the repository at this point in the history
Description
---
Add mined_timestamp to wallet.db
  • Loading branch information
Cifko authored Jul 7, 2022
1 parent 361a277 commit c6c9832
Show file tree
Hide file tree
Showing 50 changed files with 448 additions and 118 deletions.
9 changes: 9 additions & 0 deletions base_layer/common_types/src/chain_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct ChainMetadata {
pruned_height: u64,
/// The total accumulated proof of work of the longest chain
accumulated_difficulty: u128,
/// Timestamp of the tip block in the longest valid chain
timestamp: u64,
}

impl ChainMetadata {
Expand All @@ -53,13 +55,15 @@ impl ChainMetadata {
pruning_horizon: u64,
pruned_height: u64,
accumulated_difficulty: u128,
timestamp: u64,
) -> ChainMetadata {
ChainMetadata {
height_of_longest_chain: height,
best_block: hash,
pruning_horizon,
pruned_height,
accumulated_difficulty,
timestamp,
}
}

Expand All @@ -70,6 +74,7 @@ impl ChainMetadata {
pruning_horizon: 0,
pruned_height: 0,
accumulated_difficulty: 0,
timestamp: 0,
}
}

Expand Down Expand Up @@ -130,6 +135,10 @@ impl ChainMetadata {
pub fn best_block(&self) -> &BlockHash {
&self.best_block
}

pub fn timestamp(&self) -> u64 {
self.timestamp
}
}

impl Display for ChainMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ mod test {
best_block: Some(vec![]),
pruned_height: 0,
accumulated_difficulty: diff.to_be_bytes().to_vec(),
timestamp: Some(0),
}
}

Expand Down
2 changes: 2 additions & 0 deletions base_layer/core/src/base_node/proto/chain_metadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ message ChainMetadata {
// If `pruned_height` is equal to the `height_of_longest_chain` no blocks can be provided.
// Archival nodes wil always have an `pruned_height` of zero.
uint64 pruned_height = 6;
// Timestamp of the last block in the chain, or `None` if there is no chain
google.protobuf.UInt64Value timestamp = 7;
}
2 changes: 2 additions & 0 deletions base_layer/core/src/base_node/proto/chain_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl TryFrom<proto::ChainMetadata> for ChainMetadata {
pruning_horizon,
metadata.pruned_height,
accumulated_difficulty,
metadata.timestamp.unwrap_or_default(),
))
}
}
Expand All @@ -69,6 +70,7 @@ impl From<ChainMetadata> for proto::ChainMetadata {
best_block: Some(metadata.best_block().clone()),
pruned_height: metadata.pruned_height(),
accumulated_difficulty,
timestamp: Some(metadata.timestamp()),
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions base_layer/core/src/base_node/proto/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@ message BlockBodyResponse {
message SyncHeadersRequest {
// Start sending from this hash (exclusive)
bytes start_hash = 1;
// Number of blocks to send. If this is zero (empty) the peer SHOULD send to their tip height
// Number of blocks to send. If this is zero (empty) the peer SHOULD send to
// their tip height
uint64 count = 2;
}

// Find at which point the chain splits.
message FindChainSplitRequest {
// A set of block hashes ordered in height descending order from the chain tip.
// A set of block hashes ordered in height descending order from the chain
// tip.
repeated bytes block_hashes = 1;
// The maximum number of headers to return starting at the first header after the matched height
uint64 header_count = 2;}
// The maximum number of headers to return starting at the first header after
// the matched height
uint64 header_count = 2;
}

message FindChainSplitResponse {
// An ordered list of headers starting from next header after the matching hash, up until `FindChainSplitRequest::count`
// An ordered list of headers starting from next header after the matching
// hash, up until `FindChainSplitRequest::count`
repeated tari.core.BlockHeader headers = 1;
// The index of the hash that matched from `FindChainSplitRequest::block_hashes`. This value could also be used to know how far back a split occurs.
// The index of the hash that matched from
// `FindChainSplitRequest::block_hashes`. This value could also be used to
// know how far back a split occurs.
uint64 fork_hash_index = 2;
/// The current header height of this node
uint64 tip_height = 3;
Expand All @@ -51,13 +58,13 @@ message SyncKernelsRequest {
bytes end_header_hash = 2;
}

message SyncUtxosRequest {
message SyncUtxosRequest {
uint64 start = 1;
bytes end_header_hash = 2;
bool include_pruned_utxos = 3;
bool include_deleted_bitmaps = 4;
}
message SyncUtxosResponse {
message SyncUtxosResponse {
oneof utxo_or_deleted {
SyncUtxo utxo = 1;
bytes deleted_diff = 2;
Expand All @@ -79,15 +86,16 @@ message PrunedOutput {
bytes witness_hash = 2;
}

message SyncUtxosByBlockRequest {
message SyncUtxosByBlockRequest {
bytes start_header_hash = 1;
bytes end_header_hash = 2;
}

message SyncUtxosByBlockResponse {
message SyncUtxosByBlockResponse {
repeated tari.types.TransactionOutput outputs = 1;
uint64 height = 2;
bytes header_hash = 3;
uint64 mined_timestamp = 4;
}

message GetMempoolFeePerGramStatsRequest {
Expand Down
112 changes: 56 additions & 56 deletions base_layer/core/src/base_node/proto/wallet_rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,97 +11,97 @@ import "transaction.proto";
package tari.base_node;

enum TxSubmissionRejectionReason {
TxSubmissionRejectionReasonNone = 0;
TxSubmissionRejectionReasonAlreadyMined = 1;
TxSubmissionRejectionReasonDoubleSpend = 2;
TxSubmissionRejectionReasonOrphan = 3;
TxSubmissionRejectionReasonTimeLocked = 4;
TxSubmissionRejectionReasonValidationFailed = 5;
TxSubmissionRejectionReasonNone = 0;
TxSubmissionRejectionReasonAlreadyMined = 1;
TxSubmissionRejectionReasonDoubleSpend = 2;
TxSubmissionRejectionReasonOrphan = 3;
TxSubmissionRejectionReasonTimeLocked = 4;
TxSubmissionRejectionReasonValidationFailed = 5;
}

message TxSubmissionResponse {
bool accepted = 1;
TxSubmissionRejectionReason rejection_reason = 2;
bool is_synced = 3;
bool accepted = 1;
TxSubmissionRejectionReason rejection_reason = 2;
bool is_synced = 3;
}

enum TxLocation {
TxLocationNone = 0;
TxLocationNotStored = 1;
TxLocationInMempool = 2;
TxLocationMined = 3;
TxLocationNone = 0;
TxLocationNotStored = 1;
TxLocationInMempool = 2;
TxLocationMined = 3;
}

message TxQueryResponse {
TxLocation location = 1;
google.protobuf.BytesValue block_hash = 2;
uint64 confirmations = 3;
bool is_synced = 4;
uint64 height_of_longest_chain = 5;
TxLocation location = 1;
google.protobuf.BytesValue block_hash = 2;
uint64 confirmations = 3;
bool is_synced = 4;
uint64 height_of_longest_chain = 5;
google.protobuf.UInt64Value mined_timestamp = 6;
}

message TxQueryBatchResponse {
tari.types.Signature signature = 1;
TxLocation location = 2;
google.protobuf.BytesValue block_hash = 3;
uint64 confirmations = 4;
uint64 block_height = 5;
tari.types.Signature signature = 1;
TxLocation location = 2;
google.protobuf.BytesValue block_hash = 3;
uint64 confirmations = 4;
uint64 block_height = 5;
google.protobuf.UInt64Value mined_timestamp = 6;
}

message TxQueryBatchResponses {
repeated TxQueryBatchResponse responses = 1;
bool is_synced = 2;
google.protobuf.BytesValue tip_hash = 3;
uint64 height_of_longest_chain = 4;

repeated TxQueryBatchResponse responses = 1;
bool is_synced = 2;
google.protobuf.BytesValue tip_hash = 3;
uint64 height_of_longest_chain = 4;
google.protobuf.UInt64Value tip_mined_timestamp = 5;
}

message FetchMatchingUtxos {
repeated bytes output_hashes = 1;
repeated bytes output_hashes = 1;
}

message FetchUtxosResponse {
repeated tari.types.TransactionOutput outputs = 1;
bool is_synced = 2;
repeated tari.types.TransactionOutput outputs = 1;
bool is_synced = 2;
}


message QueryDeletedRequest{
repeated uint64 mmr_positions = 1;
google.protobuf.BytesValue chain_must_include_header = 2;
bool include_deleted_block_data = 3;
message QueryDeletedRequest {
repeated uint64 mmr_positions = 1;
google.protobuf.BytesValue chain_must_include_header = 2;
bool include_deleted_block_data = 3;
}

message QueryDeletedResponse {
repeated uint64 deleted_positions = 1;
repeated uint64 not_deleted_positions = 2;
bytes best_block = 3;
uint64 height_of_longest_chain = 4;
repeated bytes blocks_deleted_in = 5;
repeated uint64 heights_deleted_at = 6;
repeated uint64 deleted_positions = 1;
repeated uint64 not_deleted_positions = 2;
bytes best_block = 3;
uint64 height_of_longest_chain = 4;
repeated bytes blocks_deleted_in = 5;
repeated uint64 heights_deleted_at = 6;
}

message UtxoQueryRequest{
repeated bytes output_hashes =1;
message UtxoQueryRequest {
repeated bytes output_hashes = 1;
}

message UtxoQueryResponses {
repeated UtxoQueryResponse responses =1;
bytes best_block = 3;
uint64 height_of_longest_chain = 4;
repeated UtxoQueryResponse responses = 1;
bytes best_block = 3;
uint64 height_of_longest_chain = 4;
}

message UtxoQueryResponse {
tari.types.TransactionOutput output = 1;
uint64 mmr_position = 2;
uint64 mined_height =3;
bytes mined_in_block = 4;
bytes output_hash = 5;

tari.types.TransactionOutput output = 1;
uint64 mmr_position = 2;
uint64 mined_height = 3;
bytes mined_in_block = 4;
bytes output_hash = 5;
uint64 mined_timestamp = 6;
}

message TipInfoResponse {
ChainMetadata metadata = 1;
bool is_synced = 2;
ChainMetadata metadata = 1;
bool is_synced = 2;
}

5 changes: 5 additions & 0 deletions base_layer/core/src/base_node/proto/wallet_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub struct TxQueryResponse {
pub confirmations: u64,
pub is_synced: bool,
pub height_of_longest_chain: u64,
pub mined_timestamp: Option<u64>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand All @@ -137,6 +138,7 @@ pub struct TxQueryBatchResponse {
pub block_hash: Option<BlockHash>,
pub confirmations: u64,
pub block_height: u64,
pub mined_timestamp: Option<u64>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -195,6 +197,7 @@ impl TryFrom<proto::TxQueryResponse> for TxQueryResponse {
confirmations: proto_response.confirmations,
is_synced: proto_response.is_synced,
height_of_longest_chain: proto_response.height_of_longest_chain,
mined_timestamp: proto_response.mined_timestamp,
})
}
}
Expand All @@ -207,6 +210,7 @@ impl From<TxQueryResponse> for proto::TxQueryResponse {
confirmations: response.confirmations,
is_synced: response.is_synced,
height_of_longest_chain: response.height_of_longest_chain,
mined_timestamp: response.mined_timestamp,
}
}
}
Expand All @@ -229,6 +233,7 @@ impl TryFrom<proto::TxQueryBatchResponse> for TxQueryBatchResponse {
block_hash: proto_response.block_hash,
block_height: proto_response.block_height,
confirmations: proto_response.confirmations,
mined_timestamp: proto_response.mined_timestamp,
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions base_layer/core/src/base_node/rpc/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletRpcService<B> {
confirmations,
is_synced: true,
height_of_longest_chain: chain_metadata.height_of_longest_chain(),
mined_timestamp: Some(header.timestamp.as_u64()),
};
return Ok(response);
},
Expand All @@ -141,6 +142,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletRpcService<B> {
confirmations: 0,
is_synced: true,
height_of_longest_chain: chain_metadata.height_of_longest_chain(),
mined_timestamp: None,
},
TxStorageResponse::ReorgPool |
TxStorageResponse::NotStoredOrphan |
Expand All @@ -153,6 +155,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletRpcService<B> {
confirmations: 0,
is_synced: true,
height_of_longest_chain: chain_metadata.height_of_longest_chain(),
mined_timestamp: None,
},
};
Ok(mempool_response)
Expand Down Expand Up @@ -292,13 +295,15 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletService for BaseNodeWalletRpc
block_hash: response.block_hash,
confirmations: response.confirmations,
block_height: response.height_of_longest_chain - response.confirmations,
mined_timestamp: response.mined_timestamp,
});
}
Ok(Response::new(TxQueryBatchResponses {
responses,
is_synced,
tip_hash: Some(metadata.best_block().clone()),
height_of_longest_chain: metadata.height_of_longest_chain(),
tip_mined_timestamp: Some(metadata.timestamp()),
}))
}

Expand Down Expand Up @@ -392,6 +397,7 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletService for BaseNodeWalletRpc
PrunedOutput::Pruned { .. } => None,
PrunedOutput::NotPruned { output } => Some(output.into()),
},
mined_timestamp: utxo.mined_timestamp,
})
.collect(),
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ where B: BlockchainBackend + 'static
outputs: utxos,
height: current_header.height,
header_hash: current_header_hash,
mined_timestamp: current_header.timestamp.as_u64(),
};
// Ensure task stops if the peer prematurely stops their RPC session
if tx.send(Ok(utxo_block_response)).await.is_err() {
Expand Down
Loading

0 comments on commit c6c9832

Please sign in to comment.