Skip to content

Fix/5205 #5206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 35 additions & 82 deletions stackslib/src/net/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3560,8 +3560,8 @@ impl PeerNetwork {
let prune = if cur_epoch.epoch_id >= StacksEpochId::Epoch30 {
debug!("{:?}: run Nakamoto work loop", self.get_local_peer());

// in Nakamoto epoch, so do Nakamoto things
let prune = self.do_network_work_nakamoto(
// in Nakamoto epoch, so we can always prune
self.do_network_work_nakamoto(
burnchain_height,
sortdb,
chainstate,
Expand Down Expand Up @@ -3593,9 +3593,10 @@ impl PeerNetwork {
"{:?}: ran Epoch 2.x work loop in Nakamoto epoch",
self.get_local_peer()
);
prune || epoch2_prune
epoch2_prune
} else {
prune
// we can always prune in Nakamoto, since all state machines pin their connections
true
}
} else {
// in epoch 2.x, so do epoch 2.x things
Expand Down Expand Up @@ -3623,89 +3624,41 @@ impl PeerNetwork {
chainstate: &mut StacksChainState,
ibd: bool,
network_result: &mut NetworkResult,
) -> bool {
// do some Actual Work(tm)
let mut do_prune = false;
let mut did_cycle = false;

while !did_cycle {
// always do an inv sync
let learned = self.do_network_inv_sync_nakamoto(sortdb, ibd);
debug!(
"{:?}: network work state is {:?}",
self.get_local_peer(),
&self.nakamoto_work_state;
"learned_new_blocks?" => learned
);

// always do block download
let new_blocks = self
.do_network_block_sync_nakamoto(burnchain_height, sortdb, chainstate, ibd)
.map_err(|e| {
warn!(
"{:?}: Failed to perform Nakamoto block sync: {:?}",
&self.get_local_peer(),
&e
);
e
})
.unwrap_or(HashMap::new());

network_result.consume_nakamoto_blocks(new_blocks);

let cur_state = self.nakamoto_work_state;
match self.nakamoto_work_state {
PeerNetworkWorkState::GetPublicIP => {
if cfg!(test) && self.connection_opts.disable_natpunch {
self.nakamoto_work_state = PeerNetworkWorkState::BlockDownload;
} else {
// (re)determine our public IP address
let done = self.do_get_public_ip();
if done {
self.nakamoto_work_state = PeerNetworkWorkState::BlockDownload;
}
}
}
PeerNetworkWorkState::BlockInvSync => {
// this state is useless in Nakamoto since we're always doing inv-syncs
self.nakamoto_work_state = PeerNetworkWorkState::BlockDownload;
}
PeerNetworkWorkState::BlockDownload => {
// this state is useless in Nakamoto since we're always doing download-syncs
self.nakamoto_work_state = PeerNetworkWorkState::AntiEntropy;
}
PeerNetworkWorkState::AntiEntropy => {
debug!(
"{:?}: Block anti-entropy for Nakamoto is not yet implemented",
self.get_local_peer()
);
self.nakamoto_work_state = PeerNetworkWorkState::Prune;
}
PeerNetworkWorkState::Prune => {
// did one pass
did_cycle = true;
do_prune = true;
) {
// always do an inv sync
let learned = self.do_network_inv_sync_nakamoto(sortdb, ibd);
debug!(
"{:?}: network work state is {:?}",
self.get_local_peer(),
&self.nakamoto_work_state;
"learned_new_blocks?" => learned
);

// restart
self.nakamoto_work_state = PeerNetworkWorkState::GetPublicIP;
}
}
// always do block download
let new_blocks = self
.do_network_block_sync_nakamoto(burnchain_height, sortdb, chainstate, ibd)
.map_err(|e| {
warn!(
"{:?}: Failed to perform Nakamoto block sync: {:?}",
&self.get_local_peer(),
&e
);
e
})
.unwrap_or(HashMap::new());

if self.nakamoto_work_state == cur_state {
// only break early if we can't make progress
break;
}
}
network_result.consume_nakamoto_blocks(new_blocks);

if did_cycle {
self.num_state_machine_passes += 1;
debug!(
"{:?}: Finished full p2p state-machine pass for Nakamoto ({})",
&self.local_peer, self.num_state_machine_passes
);
// make sure our public IP is fresh (this self-throttles if we recently learned it).
if !self.connection_opts.disable_natpunch {
self.do_get_public_ip();
}

do_prune
self.num_state_machine_passes += 1;
debug!(
"{:?}: Finished full p2p state-machine pass for Nakamoto ({})",
&self.local_peer, self.num_state_machine_passes
);
}

/// Do the actual work in the state machine.
Expand Down
44 changes: 7 additions & 37 deletions testnet/stacks-node/src/tests/nakamoto_integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5185,51 +5185,21 @@ fn clarity_burn_state() {
vec![&Value::UInt(burn_block_height)],
);
result.expect_result_ok().expect("Read-only call failed");

// Submit a tx for the next block (the next block will be a new tenure, so the burn block height will increment)
let call_tx = tests::make_contract_call(
&sender_sk,
sender_nonce,
tx_fee,
&sender_addr,
contract_name,
"bar",
&[Value::UInt(burn_block_height + 1)],
);
sender_nonce += 1;
submit_tx(&http_origin, &call_tx);
}

let commits_before = commits_submitted.load(Ordering::SeqCst);
next_block_and_process_new_stacks_block(&mut btc_regtest_controller, 60, &coord_channel)
.unwrap();
next_block_and_mine_commit(
&mut btc_regtest_controller,
60,
&coord_channel,
&commits_submitted,
)
.unwrap();

let info = get_chain_info(&naka_conf);
burn_block_height = info.burn_block_height as u128;
info!("Expecting burn block height to be {}", burn_block_height);

// Assert that the contract call was successful
test_observer::get_mined_nakamoto_blocks()
.last()
.unwrap()
.tx_events
.iter()
.for_each(|event| match event {
TransactionEvent::Success(TransactionSuccessEvent { result, fee, .. }) => {
// Ignore coinbase and tenure transactions
if *fee == 0 {
return;
}

info!("Contract call result: {}", result);
result.clone().expect_result_ok().expect("Ok result");
}
_ => {
info!("Unsuccessful event: {:?}", event);
panic!("Expected a successful transaction");
}
});

// mine the interim blocks
for interim_block_ix in 0..inter_blocks_per_tenure {
info!("Mining interim block {interim_block_ix}");
Expand Down