Skip to content

Commit

Permalink
Merge branch 'feat-libp2p' into libp2p2-update-peersync
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi authored Nov 19, 2024
2 parents a91b1ef + 316fe77 commit d8cc83b
Show file tree
Hide file tree
Showing 49 changed files with 1,212 additions and 493 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
--release \
--package tari_integration_tests \
-- -t "${{ env.CI_PROFILE }} and (not @wallet-ffi) and (not @chat-ffi) and (not @broken)" \
-c 5 \
-c 1 \
--retry 2
- name: upload artifact
Expand Down
5 changes: 3 additions & 2 deletions applications/minotari_app_grpc/proto/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ message BlockHeader {
bytes prev_hash = 4;
// Timestamp at which the block was built.
uint64 timestamp = 5;
// This is the UTXO merkle root of the outputs
// This is calculated as Hash (txo MMR root || roaring bitmap hash of UTXO indices)
// This is the UTXO merkle root of the outputs in the blockchain
bytes output_mr = 6;
// This is the merkle root of all outputs in this block
bytes block_output_mr = 7;
// This is the MMR root of the kernels
bytes kernel_mr = 8;
// This is the Merkle root of the inputs in this block
Expand Down
10 changes: 10 additions & 0 deletions applications/minotari_app_grpc/proto/network.proto
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ message ListConnectedPeersResponse {
repeated ConnectedPeer connected_peers = 1;
}

message UpsertContactRequest {
string alias = 1;
bytes wallet_address = 2;
}

message UpsertContactResponse {
bool success = 1;
string error_message = 2;
}

message SoftwareUpdate {
bool has_update = 1;
string version = 2;
Expand Down
2 changes: 2 additions & 0 deletions applications/minotari_app_grpc/proto/wallet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ service Wallet {
rpc GetNetworkStatus(Empty) returns (NetworkStatusResponse);
// List currently connected peers
rpc ListConnectedPeers(Empty) returns (ListConnectedPeersResponse);
// Upsert a new contact
rpc UpsertContact(UpsertContactRequest) returns (UpsertContactResponse);
// Cancel pending transaction
rpc CancelTransaction (CancelTransactionRequest) returns (CancelTransactionResponse);
// Will trigger a complete revalidation of all wallet outputs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl From<BlockHeader> for grpc::BlockHeader {
timestamp: h.timestamp.as_u64(),
input_mr: h.input_mr.to_vec(),
output_mr: h.output_mr.to_vec(),
block_output_mr: h.block_output_mr.to_vec(),
output_mmr_size: h.output_smt_size,
kernel_mr: h.kernel_mr.to_vec(),
kernel_mmr_size: h.kernel_mmr_size,
Expand Down Expand Up @@ -76,6 +77,7 @@ impl TryFrom<grpc::BlockHeader> for BlockHeader {
timestamp: EpochTime::from(header.timestamp),
input_mr: FixedHash::try_from(header.input_mr).map_err(|err| err.to_string())?,
output_mr: FixedHash::try_from(header.output_mr).map_err(|err| err.to_string())?,
block_output_mr: FixedHash::try_from(header.block_output_mr).unwrap_or_default(),
output_smt_size: header.output_mmr_size,
kernel_mr: FixedHash::try_from(header.kernel_mr).map_err(|err| err.to_string())?,
kernel_mmr_size: header.kernel_mmr_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ use minotari_app_grpc::tari_rpc::{
TransferRequest,
TransferResponse,
TransferResult,
UpsertContactRequest,
UpsertContactResponse,
ValidateRequest,
ValidateResponse,
};
Expand All @@ -97,6 +99,7 @@ use tari_common_types::{
transaction::TxId,
types::{BlockHash, PublicKey, Signature},
};
use tari_contacts::contacts_service::types::Contact;
use tari_core::{
consensus::{ConsensusBuilderError, ConsensusConstants, ConsensusManager},
transactions::{
Expand Down Expand Up @@ -927,6 +930,30 @@ impl wallet_server::Wallet for WalletGrpcServer {
Ok(Response::new(resp))
}

async fn upsert_contact(
&self,
request: Request<UpsertContactRequest>,
) -> Result<Response<UpsertContactResponse>, Status> {
let message = request.into_inner();
let alias = message.alias;
let wallet_address = TariAddress::from_bytes(&message.wallet_address)
.map_err(|e| Status::invalid_argument(format!("Invalid wallet address: {}", e)))?;

let contact = Contact::new(alias, wallet_address, None, None, false);

let mut wallet = self.wallet.clone();
match wallet.contacts_service.upsert_contact(contact).await {
Ok(_) => Ok(Response::new(UpsertContactResponse {
success: true,
error_message: String::new(),
})),
Err(e) => Ok(Response::new(UpsertContactResponse {
success: false,
error_message: e.to_string(),
})),
}
}

async fn cancel_transaction(
&self,
request: Request<tari_rpc::CancelTransactionRequest>,
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ pub async fn init_wallet(
e => ExitError::new(ExitCode::WalletError, format!("Error creating Wallet Container: {}", e)),
})?;

error!(
info!(
target: LOG_TARGET,
"Wallet started in {}ms", now.elapsed().as_millis()
);
Expand Down
13 changes: 12 additions & 1 deletion base_layer/contacts/src/contacts_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,22 @@ pub const SUBSCRIPTION_LABEL: &str = "Chat";

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContactMessageType {
Ping,
Ping = 0,
Pong,
NoMessage,
}

impl ContactMessageType {
pub fn from_byte(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Ping),
1 => Some(Self::Pong),
2 => Some(Self::NoMessage),
_ => None,
}
}
}

impl Display for ContactMessageType {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
Expand Down
14 changes: 10 additions & 4 deletions base_layer/core/src/base_node/sync/header_sync/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,18 @@ mod test {
test_helpers::blockchain::{create_new_blockchain, TempDatabase},
};

fn setup() -> (BlockHeaderSyncValidator<TempDatabase>, AsyncBlockchainDb<TempDatabase>) {
fn setup() -> (
BlockHeaderSyncValidator<TempDatabase>,
AsyncBlockchainDb<TempDatabase>,
ConsensusManager,
) {
let rules = ConsensusManager::builder(Network::LocalNet).build().unwrap();
let randomx_factory = RandomXFactory::default();
let db = create_new_blockchain();
(
BlockHeaderSyncValidator::new(db.clone().into(), rules, randomx_factory),
BlockHeaderSyncValidator::new(db.clone().into(), rules.clone(), randomx_factory),
db.into(),
rules,
)
}

Expand All @@ -262,10 +267,11 @@ mod test {
AsyncBlockchainDb<TempDatabase>,
ChainHeader,
) {
let (validator, db) = setup();
let (validator, db, cm) = setup();
let mut tip = db.fetch_tip_header().await.unwrap();
for _ in 0..n {
let mut header = BlockHeader::from_previous(tip.header());
header.version = cm.consensus_constants(header.height).blockchain_version();
// Needed to have unique keys for the blockchain db mmr count indexes (MDB_KEY_EXIST error)
header.kernel_mmr_size += 1;
header.output_smt_size += 1;
Expand Down Expand Up @@ -301,7 +307,7 @@ mod test {

#[tokio::test]
async fn it_errors_if_hash_does_not_exist() {
let (mut validator, _) = setup();
let (mut validator, _, _cm) = setup();
let start_hash = vec![0; 32];
let err = validator
.initialize_state(&start_hash.clone().try_into().unwrap())
Expand Down
19 changes: 13 additions & 6 deletions base_layer/core/src/blocks/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ pub struct BlockHeader {
pub timestamp: EpochTime,
/// This is the Merkle root of the inputs in this block
pub input_mr: FixedHash,
/// This is the UTXO merkle root of the outputs
/// This is calculated as Hash (txo MMR root || roaring bitmap hash of UTXO indices)
/// This is the UTXO merkle root of the outputs on the blockchain
pub output_mr: FixedHash,
/// This is the block_output_mr
pub block_output_mr: FixedHash,
/// The size (number of leaves) of the output and range proof MMRs at the time of this header
pub output_smt_size: u64,
/// This is the MMR root of the kernels
Expand Down Expand Up @@ -130,6 +131,7 @@ impl BlockHeader {
prev_hash: FixedHash::zero(),
timestamp: EpochTime::now(),
output_mr: FixedHash::zero(),
block_output_mr: FixedHash::zero(),
output_smt_size: 0,
kernel_mr: FixedHash::zero(),
kernel_mmr_size: 0,
Expand Down Expand Up @@ -164,6 +166,7 @@ impl BlockHeader {
timestamp: EpochTime::now(),
output_mr: FixedHash::zero(),
output_smt_size: prev.output_smt_size,
block_output_mr: FixedHash::zero(),
kernel_mr: FixedHash::zero(),
kernel_mmr_size: prev.kernel_mmr_size,
input_mr: FixedHash::zero(),
Expand Down Expand Up @@ -222,7 +225,7 @@ impl BlockHeader {
/// Provides a mining hash of the header, used for the mining.
/// This differs from the normal hash by not hashing the nonce and kernel pow.
pub fn mining_hash(&self) -> FixedHash {
DomainSeparatedConsensusHasher::<BlocksHashDomain, Blake2b<U32>>::new("block_header")
let incomplete = DomainSeparatedConsensusHasher::<BlocksHashDomain, Blake2b<U32>>::new("block_header")
.chain(&self.version)
.chain(&self.height)
.chain(&self.prev_hash)
Expand All @@ -235,9 +238,12 @@ impl BlockHeader {
.chain(&self.total_kernel_offset)
.chain(&self.total_script_offset)
.chain(&self.validator_node_mr)
.chain(&self.validator_node_size)
.finalize()
.into()
.chain(&self.validator_node_size);

match self.version {
0 => incomplete.finalize().into(),
_ => incomplete.chain(&self.block_output_mr).finalize().into(),
}
}

pub fn merge_mining_hash(&self) -> FixedHash {
Expand Down Expand Up @@ -273,6 +279,7 @@ impl From<NewBlockHeaderTemplate> for BlockHeader {
prev_hash: header_template.prev_hash,
timestamp: EpochTime::now(),
output_mr: FixedHash::zero(),
block_output_mr: FixedHash::zero(),
output_smt_size: 0,
kernel_mr: FixedHash::zero(),
kernel_mmr_size: 0,
Expand Down
47 changes: 44 additions & 3 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ pub fn get_nextnet_genesis_block() -> ChainBlock {
// TODO: Fix this hack with the next nextnet reset!!
block.header.input_mr =
FixedHash::from_hex("0000000000000000000000000000000000000000000000000000000000000000").unwrap();
block.header.block_output_mr =
FixedHash::from_hex("0000000000000000000000000000000000000000000000000000000000000000").unwrap();

// Add pre-mine utxos - enable/disable as required
let add_pre_mine_utxos = false;
Expand Down Expand Up @@ -270,6 +272,8 @@ pub fn get_mainnet_genesis_block() -> ChainBlock {
FixedHash::from_hex("b7b38b76f5832b5b63691a8334dfa67d8c762b77b2b4aa4f648c4eb1dfb25c1e").unwrap();
block.header.output_mr =
FixedHash::from_hex("a77ecf05b20c426d3d400a63397be6c622843c66d5751ecbe3390c8a4885158e").unwrap();
block.header.block_output_mr =
FixedHash::from_hex("91e997520b0eee770914334692080f92d18db434d373561f8842c56d70c11b97").unwrap();
block.header.validator_node_mr =
FixedHash::from_hex("277da65c40b2cf99db86baedb903a3f0a38540f3a94d40c826eecac7e27d5dfc").unwrap();
}
Expand Down Expand Up @@ -371,6 +375,10 @@ pub fn get_esmeralda_genesis_block() -> ChainBlock {
// lets get the block
let mut block = get_esmeralda_genesis_block_raw();

// TODO: Fix this hack with the next esme reset!!
block.header.block_output_mr =
FixedHash::from_hex("0000000000000000000000000000000000000000000000000000000000000000").unwrap();

// Add pre-mine utxos - enable/disable as required
let add_pre_mine_utxos = true;
if add_pre_mine_utxos {
Expand Down Expand Up @@ -482,7 +490,9 @@ fn get_raw_block(genesis_timestamp: &DateTime<FixedOffset>, not_before_proof: &P
height: 0,
prev_hash: FixedHash::zero(),
timestamp: timestamp.into(),
output_mr: FixedHash::from_hex("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
output_mr: FixedHash::zero(),
block_output_mr: FixedHash::from_hex("622720a6571c33d6bf6138d9e737d3468c77f1193640698ad459953d24ec0812")
.unwrap(),
output_smt_size: 0,
kernel_mr: FixedHash::from_hex("c14803066909d6d22abf0d2d2782e8936afc3f713f2af3a4ef5c42e8400c1303").unwrap(),
kernel_mmr_size: 0,
Expand Down Expand Up @@ -518,6 +528,7 @@ mod test {

use super::*;
use crate::{
block_output_mr_hash_from_pruned_mmr,
chain_storage::calculate_validator_node_mr,
consensus::ConsensusManager,
test_helpers::blockchain::create_new_blockchain_with_network,
Expand All @@ -527,8 +538,8 @@ mod test {
},
validation::{ChainBalanceValidator, FinalHorizonStateValidation},
KernelMmr,
PrunedOutputMmr,
};

#[test]
#[serial]
fn esmeralda_genesis_sanity_check() {
Expand Down Expand Up @@ -661,9 +672,15 @@ mod test {
kernel_mmr.push(k.hash().to_vec()).unwrap();
}
let mut output_smt = OutputSmt::new();

let mut block_output_mmr = PrunedOutputMmr::new(PrunedHashSet::default());
let mut normal_output_mmr = PrunedOutputMmr::new(PrunedHashSet::default());
let mut vn_nodes = Vec::new();
for o in block.block().body.outputs() {
if o.features.is_coinbase() {
block_output_mmr.push(o.hash().to_vec()).unwrap();
} else {
normal_output_mmr.push(o.hash().to_vec()).unwrap();
}
let smt_key = NodeKey::try_from(o.commitment.as_bytes()).unwrap();
let smt_node = ValueHash::try_from(o.smt_hash(block.header().height).as_slice()).unwrap();
output_smt.insert(smt_key, smt_node).unwrap();
Expand All @@ -681,6 +698,11 @@ mod test {
));
}
}

block_output_mmr
.push(normal_output_mmr.get_merkle_root().unwrap().to_vec())
.unwrap();

for i in block.block().body.inputs() {
let smt_key = NodeKey::try_from(i.commitment().unwrap().as_bytes()).unwrap();
output_smt.delete(&smt_key).unwrap();
Expand Down Expand Up @@ -718,6 +740,25 @@ mod test {
output_mr_hash_from_smt(&mut output_smt).unwrap().to_vec().to_hex(),
block.header().output_mr.to_vec().to_hex(),
);

// TODO: Fix this hack with the next nextnet/esme release reset!!
if network == Network::NextNet || network == Network::Esmeralda {
assert_eq!(
FixedHash::from_hex("0000000000000000000000000000000000000000000000000000000000000000")
.unwrap()
.to_vec()
.to_hex(),
block.header().block_output_mr.to_vec().to_hex(),
);
} else {
assert_eq!(
block_output_mr_hash_from_pruned_mmr(&block_output_mmr)
.unwrap()
.to_vec()
.to_hex(),
block.header().block_output_mr.to_vec().to_hex(),
);
}
if network == Network::NextNet {
// TODO: Fix this hack with the next nextnet reset!!
assert_eq!(
Expand Down
Loading

0 comments on commit d8cc83b

Please sign in to comment.