Skip to content

Commit 272c839

Browse files
authored
Merge pull request #2670 from blockstack/fix/2667
Fix/2667
2 parents 7801c47 + c0367ef commit 272c839

File tree

7 files changed

+40
-19
lines changed

7 files changed

+40
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ This release's chainstate directory is compatible with chainstate directories fr
3434
- Fix trait rpc lookups for implicitly implemented traits (#2602).
3535
- Fix `v2/pox` endpoint, broken on Mocknet (#2634).
3636
- Align cost limits on mocknet, testnet and mainnet (#2660).
37+
- Log peer addresses in the HTTP server (#2667)
38+
- Mine microblocks if there are no recent unprocessed Stacks blocks
3739

3840
## [2.0.11.0.0]
3941

net-test/tests/test_2_nat_miners_microblocks.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ with_miner_1
8282
RC=$?
8383
if [ $RC -ne 0 ]; then
8484
logln "Failed to make a token transfer to $STX_DEST_ADDR at attempt $i: rc $RC"
85-
DONE=1
86-
break
85+
86+
# keep trying
87+
continue
8788
fi
8889

8990
TXID="$(echo "$TX" | send_tx "http://localhost:21443")"

net-test/tests/testlib.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,21 @@ easy_token_transfer() {
195195
local ADDR="$(blockstack-cli --testnet addresses "$PRIVKEY" | jq -r '.STX')"
196196
local NONCE="$(get_unconfirmed_account_nonce "$STACKS_NODE_URL" "$ADDR")"
197197
local RC=$?
198-
if [ $RC -ne 0 ]; then
199-
logln "Failed to query unconfirmed account nonce: rc $RC"
198+
if [ $RC -ne 0 ] || [ -z "$NONCE" ]; then
199+
logln "Failed to query unconfirmed account nonce: rc '$RC' nonce '$NONCE'"
200200
return 1
201201
fi
202202

203203
local MEMO="test $NONCE"
204+
logln "blockstack-cli --testnet token-transfer '$PRIVKEY' '$FEE_RATE' '$NONCE' '$DEST' '$AMOUNT' '$MEMO' '$OPTS'"
204205
local TX="$(blockstack-cli --testnet token-transfer "$PRIVKEY" "$FEE_RATE" "$NONCE" "$DEST" "$AMOUNT" "$MEMO" "$OPTS" 2>&1)"
205206
RC=$?
206207
if [ $RC -ne 0 ]; then
207208
logln "Failed to generate tx: blockstack-cli --testnet token-transfer $PRIVKEY $FEE_RATE $NONCE $DEST $AMOUNT \"$MEMO\""
208209
return 1
209210
fi
210211

212+
logln "Generated tx: $TX"
211213
printf "$TX"
212214
return 0
213215
}

src/chainstate/stacks/db/unconfirmed.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ impl UnconfirmedState {
345345
self.last_mblock.is_some()
346346
}
347347

348+
/// Does the unconfirmed microblock state represent any transactions?
349+
pub fn num_mined_txs(&self) -> usize {
350+
self.mined_txs.len()
351+
}
352+
348353
/// Get information about an unconfirmed transaction
349354
pub fn get_unconfirmed_transaction(
350355
&self,

src/net/http.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ impl HttpRequestType {
15681568
} else {
15691569
"".to_string()
15701570
};
1571-
info!("Handle {} {}{}", verb, decoded_path, query);
1571+
info!("Handle HTTPRequest"; "verb" => %verb, "peer_addr" => %protocol.peer_addr, "path" => %decoded_path, "query" => %query);
15721572
return Ok(request);
15731573
}
15741574
None => {
@@ -3964,6 +3964,8 @@ struct HttpReplyData {
39643964
/// There can be at most one HTTP request in-flight (i.e. we don't do pipelining)
39653965
#[derive(Debug, Clone, PartialEq)]
39663966
pub struct StacksHttp {
3967+
/// Address of peer
3968+
peer_addr: SocketAddr,
39673969
/// Version of client
39683970
request_version: Option<HttpVersion>,
39693971
/// Path we requested
@@ -3977,8 +3979,9 @@ pub struct StacksHttp {
39773979
}
39783980

39793981
impl StacksHttp {
3980-
pub fn new() -> StacksHttp {
3982+
pub fn new(peer_addr: SocketAddr) -> StacksHttp {
39813983
StacksHttp {
3984+
peer_addr,
39823985
reply: None,
39833986
request_version: None,
39843987
request_path: None,
@@ -4084,19 +4087,21 @@ impl StacksHttp {
40844087
}
40854088

40864089
/// Given a HTTP request, serialize it out
4090+
#[cfg(test)]
40874091
pub fn serialize_request(req: &HttpRequestType) -> Result<Vec<u8>, net_error> {
4088-
let mut http = StacksHttp::new();
4092+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
40894093
let mut ret = vec![];
40904094
req.send(&mut http, &mut ret)?;
40914095
Ok(ret)
40924096
}
40934097

40944098
/// Given a fully-formed single HTTP response, parse it (used by clients).
4099+
#[cfg(test)]
40954100
pub fn parse_response(
40964101
request_path: &str,
40974102
response_buf: &[u8],
40984103
) -> Result<StacksHttpMessage, net_error> {
4099-
let mut http = StacksHttp::new();
4104+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
41004105
http.reset();
41014106
http.begin_request(HttpVersion::Http11, request_path.to_string());
41024107

@@ -5437,7 +5442,7 @@ mod test {
54375442
expected_bytes.append(&mut expected_http_body.clone());
54385443

54395444
let mut bytes = vec![];
5440-
let mut http = StacksHttp::new();
5445+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
54415446
http.write_message(&mut bytes, &StacksHttpMessage::Request(test.clone()))
54425447
.unwrap();
54435448

@@ -5454,7 +5459,7 @@ mod test {
54545459
"POST /v2/transactions HTTP/1.1\r\nUser-Agent: stacks/2.0\r\nHost: bad:123\r\nContent-Length: 0\r\n\r\n",
54555460
];
54565461
for bad_content_length in bad_content_lengths {
5457-
let mut http = StacksHttp::new();
5462+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
54585463
let (preamble, offset) = http.read_preamble(bad_content_length.as_bytes()).unwrap();
54595464
let e = http.read_payload(&preamble, &bad_content_length.as_bytes()[offset..]);
54605465
let estr = format!("{:?}", &e);
@@ -5473,7 +5478,7 @@ mod test {
54735478
"POST /v2/transactions HTTP/1.1\r\nUser-Agent: stacks/2.0\r\nHost: bad:123\r\nContent-Length: 1\r\n\r\nb",
54745479
];
54755480
for bad_content_type in bad_content_types {
5476-
let mut http = StacksHttp::new();
5481+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
54775482
let (preamble, offset) = http.read_preamble(bad_content_type.as_bytes()).unwrap();
54785483
let e = http.read_payload(&preamble, &bad_content_type.as_bytes()[offset..]);
54795484
assert!(e.is_err());
@@ -5871,7 +5876,7 @@ mod test {
58715876
.zip(expected_http_bodies.iter()),
58725877
)
58735878
{
5874-
let mut http = StacksHttp::new();
5879+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
58755880
let mut bytes = vec![];
58765881
test_debug!("write body:\n{:?}\n", test);
58775882

@@ -5963,7 +5968,7 @@ mod test {
59635968
expected_error
59645969
);
59655970

5966-
let mut http = StacksHttp::new();
5971+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
59675972
http.begin_request(HttpVersion::Http11, request_path.to_string());
59685973

59695974
let (preamble, offset) = http.read_preamble(test.as_bytes()).unwrap();
@@ -6084,7 +6089,7 @@ mod test {
60846089
let invalid_neighbors_response = "HTTP/1.1 200 OK\r\nServer: stacks/v2.0\r\nX-Request-Id: 123\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\n\r\n10\r\nxxxxxxxxxxxxxxxx\r\n0\r\n\r\n";
60856090
let invalid_chunked_response = "HTTP/1.1 200 OK\r\nServer: stacks/v2.0\r\nX-Request-Id: 123\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\n\r\n29\r\n{\"sample\":[],\"inbound\":[],\"outbound\":[]}\r\n0\r\n\r\n";
60866091

6087-
let mut http = StacksHttp::new();
6092+
let mut http = StacksHttp::new("127.0.0.1:20443".parse().unwrap());
60886093

60896094
http.begin_request(HttpVersion::Http11, "/v2/neighbors".to_string());
60906095
let (preamble, offset) = http

src/net/rpc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl RPCPeerInfoData {
207207
let stacks_tip_height = burnchain_tip.canonical_stacks_tip_height;
208208
let (unconfirmed_tip, unconfirmed_seq) = match chainstate.unconfirmed_state {
209209
Some(ref unconfirmed) => {
210-
if unconfirmed.is_readable() {
210+
if unconfirmed.num_mined_txs() > 0 {
211211
(
212212
unconfirmed.unconfirmed_chain_tip.clone(),
213213
unconfirmed.last_mblock_seq,
@@ -481,7 +481,7 @@ impl ConversationHttp {
481481
conn_opts: &ConnectionOptions,
482482
conn_id: usize,
483483
) -> ConversationHttp {
484-
let mut stacks_http = StacksHttp::new();
484+
let mut stacks_http = StacksHttp::new(peer_addr.clone());
485485
stacks_http.maximum_call_argument_size = conn_opts.maximum_call_argument_size;
486486
ConversationHttp {
487487
network_id: network_id,

testnet/stacks-node/src/neon_node.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,13 @@ fn try_mine_microblock(
436436
if microblock_miner.last_mined + (microblock_miner.frequency as u128)
437437
< get_epoch_time_ms()
438438
{
439-
// opportunistically try and mine, but only if there's no attachable blocks
440-
let num_attachable =
441-
StacksChainState::count_attachable_staging_blocks(chainstate.db(), 1, 0)?;
439+
// opportunistically try and mine, but only if there are no attachable blocks in
440+
// recent history (i.e. in the last 10 minutes)
441+
let num_attachable = StacksChainState::count_attachable_staging_blocks(
442+
chainstate.db(),
443+
1,
444+
get_epoch_time_secs() - 600,
445+
)?;
442446
if num_attachable == 0 {
443447
match mine_one_microblock(&mut microblock_miner, sortdb, chainstate, &mem_pool)
444448
{
@@ -453,6 +457,8 @@ fn try_mine_microblock(
453457
warn!("Failed to mine one microblock: {:?}", &e);
454458
}
455459
}
460+
} else {
461+
debug!("Will not mine microblocks yet -- have {} attachable blocks that arrived in the last 10 minutes", num_attachable);
456462
}
457463
}
458464
microblock_miner.last_mined = get_epoch_time_ms();

0 commit comments

Comments
 (0)