Skip to content

Commit 4f14f4e

Browse files
committed
Remove wsts from stacks-signer
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 82be35a commit 4f14f4e

File tree

17 files changed

+74
-2484
lines changed

17 files changed

+74
-2484
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libsigner/src/libsigner.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ mod session;
4444
mod signer_set;
4545
/// v0 signer related code
4646
pub mod v0;
47-
/// v1 signer related code
48-
pub mod v1;
4947

5048
use std::cmp::Eq;
5149
use std::fmt::Debug;

libsigner/src/runloop.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,15 @@ const STDERR: i32 = 2;
4141
/// Trait describing the needful components of a top-level runloop.
4242
/// This is where the signer business logic would go.
4343
/// Implement this, and you get all the multithreaded setup for free.
44-
pub trait SignerRunLoop<R: Send, CMD: Send, T: SignerEventTrait> {
44+
pub trait SignerRunLoop<R: Send, T: SignerEventTrait> {
4545
/// Hint to set how long to wait for new events
4646
fn set_event_timeout(&mut self, timeout: Duration);
4747
/// Getter for the event poll timeout
4848
fn get_event_timeout(&self) -> Duration;
4949
/// Run one pass of the event loop, given new Signer events discovered since the last pass.
5050
/// Returns Some(R) if this is the final pass -- the runloop evaluated to R
5151
/// Returns None to keep running.
52-
fn run_one_pass(
53-
&mut self,
54-
event: Option<SignerEvent<T>>,
55-
cmd: Option<CMD>,
56-
res: &Sender<R>,
57-
) -> Option<R>;
52+
fn run_one_pass(&mut self, event: Option<SignerEvent<T>>, res: &Sender<R>) -> Option<R>;
5853

5954
/// This is the main loop body for the signer. It continuously receives events from
6055
/// `event_recv`, polling for up to `self.get_event_timeout()` units of time. Once it has
@@ -66,7 +61,6 @@ pub trait SignerRunLoop<R: Send, CMD: Send, T: SignerEventTrait> {
6661
fn main_loop<EVST: EventStopSignaler>(
6762
&mut self,
6863
event_recv: Receiver<SignerEvent<T>>,
69-
command_recv: Receiver<CMD>,
7064
result_send: Sender<R>,
7165
mut event_stop_signaler: EVST,
7266
) -> Option<R> {
@@ -81,11 +75,7 @@ pub trait SignerRunLoop<R: Send, CMD: Send, T: SignerEventTrait> {
8175
return None;
8276
}
8377
};
84-
// Do not block for commands
85-
let next_command_opt = command_recv.try_recv().ok();
86-
if let Some(final_state) =
87-
self.run_one_pass(next_event_opt, next_command_opt, &result_send)
88-
{
78+
if let Some(final_state) = self.run_one_pass(next_event_opt, &result_send) {
8979
info!("Runloop exit; signaling event-receiver to stop");
9080
event_stop_signaler.send();
9181
return Some(final_state);
@@ -95,13 +85,11 @@ pub trait SignerRunLoop<R: Send, CMD: Send, T: SignerEventTrait> {
9585
}
9686

9787
/// The top-level signer implementation
98-
pub struct Signer<CMD, R, SL, EV, T> {
88+
pub struct Signer<R, SL, EV, T> {
9989
/// the runloop itself
10090
signer_loop: Option<SL>,
10191
/// the event receiver to use
10292
event_receiver: Option<EV>,
103-
/// the command receiver to use
104-
command_receiver: Option<Receiver<CMD>>,
10593
/// the result sender to use
10694
result_sender: Option<Sender<R>>,
10795
/// phantom data for the codec
@@ -193,31 +181,24 @@ pub fn set_runloop_signal_handler<ST: EventStopSignaler + Send + 'static>(mut st
193181
}).expect("FATAL: failed to set signal handler");
194182
}
195183

196-
impl<CMD, R, SL, EV, T> Signer<CMD, R, SL, EV, T> {
184+
impl<R, SL, EV, T> Signer<R, SL, EV, T> {
197185
/// Create a new signer with the given runloop and event receiver.
198-
pub fn new(
199-
runloop: SL,
200-
event_receiver: EV,
201-
command_receiver: Receiver<CMD>,
202-
result_sender: Sender<R>,
203-
) -> Signer<CMD, R, SL, EV, T> {
186+
pub fn new(runloop: SL, event_receiver: EV, result_sender: Sender<R>) -> Signer<R, SL, EV, T> {
204187
Signer {
205188
signer_loop: Some(runloop),
206189
event_receiver: Some(event_receiver),
207-
command_receiver: Some(command_receiver),
208190
result_sender: Some(result_sender),
209191
phantom_data: PhantomData,
210192
}
211193
}
212194
}
213195

214196
impl<
215-
CMD: Send + 'static,
216197
R: Send + 'static,
217198
T: SignerEventTrait + 'static,
218-
SL: SignerRunLoop<R, CMD, T> + Send + 'static,
199+
SL: SignerRunLoop<R, T> + Send + 'static,
219200
EV: EventReceiver<T> + Send + 'static,
220-
> Signer<CMD, R, SL, EV, T>
201+
> Signer<R, SL, EV, T>
221202
{
222203
/// This is a helper function to spawn both the runloop and event receiver in their own
223204
/// threads. Advanced signers may not need this method, and instead opt to run the receiver
@@ -234,10 +215,6 @@ impl<
234215
.event_receiver
235216
.take()
236217
.ok_or(EventError::AlreadyRunning)?;
237-
let command_receiver = self
238-
.command_receiver
239-
.take()
240-
.ok_or(EventError::AlreadyRunning)?;
241218
let result_sender = self
242219
.result_sender
243220
.take()
@@ -266,9 +243,7 @@ impl<
266243
let runloop_thread = thread::Builder::new()
267244
.name(format!("signer_runloop:{bind_port}"))
268245
.stack_size(THREAD_STACK_SIZE)
269-
.spawn(move || {
270-
signer_loop.main_loop(event_recv, command_receiver, result_sender, stop_signaler)
271-
})
246+
.spawn(move || signer_loop.main_loop(event_recv, result_sender, stop_signaler))
272247
.map_err(|e| {
273248
error!("SignerRunLoop failed to start: {:?}", &e);
274249
ret_stop_signaler.send();

libsigner/src/tests/http.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::http::{decode_http_body, decode_http_request, decode_http_response, r
2525

2626
#[test]
2727
fn test_decode_http_request_ok() {
28-
let tests = vec![
28+
let tests = [
2929
("GET /foo HTTP/1.1\r\nHost: localhost:6270\r\n\r\n",
3030
("GET", "/foo", vec![("host", "localhost:6270")])),
3131
("POST asdf HTTP/1.1\r\nHost: core.blockstack.org\r\nFoo: Bar\r\n\r\n",
@@ -61,7 +61,7 @@ fn test_decode_http_request_ok() {
6161

6262
#[test]
6363
fn test_decode_http_request_err() {
64-
let tests = vec![
64+
let tests = [
6565
(
6666
"GET /foo HTTP/1.1\r\n",
6767
EventError::Deserialize("".to_string()),
@@ -99,7 +99,7 @@ fn test_decode_http_request_err() {
9999

100100
#[test]
101101
fn test_decode_http_response_ok() {
102-
let tests = vec![
102+
let tests = [
103103
("HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: 123\r\nX-Request-ID: 0\r\n\r\n",
104104
vec![("content-type", "application/octet-stream"), ("content-length", "123"), ("x-request-id", "0")]),
105105
("HTTP/1.1 200 Ok\r\nContent-Type: application/octet-stream\r\nTransfer-encoding: chunked\r\nX-Request-ID: 0\r\n\r\n",
@@ -123,7 +123,7 @@ fn test_decode_http_response_ok() {
123123

124124
#[test]
125125
fn test_decode_http_response_err() {
126-
let tests = vec![
126+
let tests = [
127127
("HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: 456\r\nFoo: Bar\r\nX-Request-ID: 0\r\n\r\n",
128128
RPCError::HttpError(400)),
129129
("HTTP/1.1 200",
@@ -223,7 +223,7 @@ impl Write for MockHTTPSocket {
223223

224224
#[test]
225225
fn test_run_http_request_with_body() {
226-
let tests = vec![
226+
let tests = [
227227
("GET", "/test-no-content-type-and-no-body", None, vec![]),
228228
(
229229
"GET",
@@ -288,7 +288,7 @@ fn test_run_http_request_with_body() {
288288

289289
#[test]
290290
fn test_run_http_request_no_body() {
291-
let tests = vec![
291+
let tests = [
292292
("GET", "/test-no-content-type-and-no-body", None, vec![]),
293293
(
294294
"GET",

libsigner/src/tests/mod.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ use std::time::Duration;
2424
use std::{mem, thread};
2525

2626
use blockstack_lib::chainstate::nakamoto::signer_set::NakamotoSigners;
27+
use blockstack_lib::chainstate::nakamoto::{NakamotoBlock, NakamotoBlockHeader};
2728
use blockstack_lib::chainstate::stacks::boot::SIGNERS_NAME;
2829
use blockstack_lib::chainstate::stacks::events::StackerDBChunksEvent;
2930
use blockstack_lib::util_lib::boot::boot_code_id;
31+
use clarity::types::chainstate::{ConsensusHash, StacksBlockId, TrieHash};
32+
use clarity::util::hash::Sha512Trunc256Sum;
33+
use clarity::util::secp256k1::MessageSignature;
3034
use clarity::vm::types::QualifiedContractIdentifier;
3135
use libstackerdb::StackerDBChunkData;
36+
use stacks_common::bitvec::BitVec;
3237
use stacks_common::codec::{
3338
read_next, read_next_at_most, read_next_exact, write_next, Error as CodecError,
3439
StacksMessageCodec,
@@ -38,8 +43,8 @@ use stacks_common::util::sleep_ms;
3843
use wsts::net::{DkgBegin, Packet};
3944

4045
use crate::events::{SignerEvent, SignerEventTrait};
41-
use crate::v1::messages::SignerMessage;
42-
use crate::{Signer, SignerEventReceiver, SignerRunLoop};
46+
use crate::v0::messages::{BlockRejection, SignerMessage};
47+
use crate::{BlockProposal, Signer, SignerEventReceiver, SignerRunLoop};
4348

4449
/// Simple runloop implementation. It receives `max_events` events and returns `events` from the
4550
/// last call to `run_one_pass` as its final state.
@@ -63,7 +68,7 @@ enum Command {
6368
Empty,
6469
}
6570

66-
impl<T: SignerEventTrait> SignerRunLoop<Vec<SignerEvent<T>>, Command, T> for SimpleRunLoop<T> {
71+
impl<T: SignerEventTrait> SignerRunLoop<Vec<SignerEvent<T>>, T> for SimpleRunLoop<T> {
6772
fn set_event_timeout(&mut self, timeout: Duration) {
6873
self.poll_timeout = timeout;
6974
}
@@ -75,7 +80,6 @@ impl<T: SignerEventTrait> SignerRunLoop<Vec<SignerEvent<T>>, Command, T> for Sim
7580
fn run_one_pass(
7681
&mut self,
7782
event: Option<SignerEvent<T>>,
78-
_cmd: Option<Command>,
7983
_res: &Sender<Vec<SignerEvent<T>>>,
8084
) -> Option<Vec<SignerEvent<T>>> {
8185
debug!("Got event: {:?}", &event);
@@ -99,16 +103,34 @@ impl<T: SignerEventTrait> SignerRunLoop<Vec<SignerEvent<T>>, Command, T> for Sim
99103
fn test_simple_signer() {
100104
let contract_id = NakamotoSigners::make_signers_db_contract_id(0, 0, false);
101105
let ev = SignerEventReceiver::new(false);
102-
let (_cmd_send, cmd_recv) = channel();
103106
let (res_send, _res_recv) = channel();
104107
let max_events = 5;
105-
let mut signer = Signer::new(SimpleRunLoop::new(max_events), ev, cmd_recv, res_send);
108+
let mut signer = Signer::new(SimpleRunLoop::new(max_events), ev, res_send);
106109
let endpoint: SocketAddr = "127.0.0.1:30000".parse().unwrap();
107110
let mut chunks = vec![];
111+
let block_proposal = BlockProposal {
112+
block: NakamotoBlock {
113+
header: NakamotoBlockHeader {
114+
version: 1,
115+
chain_length: 10,
116+
burn_spent: 10,
117+
consensus_hash: ConsensusHash([0; 20]),
118+
parent_block_id: StacksBlockId([0; 32]),
119+
tx_merkle_root: Sha512Trunc256Sum([0; 32]),
120+
state_index_root: TrieHash([0; 32]),
121+
timestamp: 11,
122+
miner_signature: MessageSignature::empty(),
123+
signer_signature: vec![],
124+
pox_treatment: BitVec::ones(1).unwrap(),
125+
},
126+
txs: vec![],
127+
},
128+
burn_height: 2,
129+
reward_cycle: 1,
130+
};
108131
for i in 0..max_events {
109132
let privk = Secp256k1PrivateKey::new();
110-
let msg = wsts::net::Message::DkgBegin(DkgBegin { dkg_id: 0 });
111-
let message = SignerMessage::Packet(Packet { msg, sig: vec![] });
133+
let message = SignerMessage::BlockProposal(block_proposal.clone());
112134
let message_bytes = message.serialize_to_vec();
113135
let mut chunk = StackerDBChunkData::new(i as u32, 1, message_bytes);
114136
chunk.sign(&privk).unwrap();
@@ -178,10 +200,9 @@ fn test_simple_signer() {
178200
#[test]
179201
fn test_status_endpoint() {
180202
let ev = SignerEventReceiver::new(false);
181-
let (_cmd_send, cmd_recv) = channel();
182203
let (res_send, _res_recv) = channel();
183204
let max_events = 1;
184-
let mut signer = Signer::new(SimpleRunLoop::new(max_events), ev, cmd_recv, res_send);
205+
let mut signer = Signer::new(SimpleRunLoop::new(max_events), ev, res_send);
185206
let endpoint: SocketAddr = "127.0.0.1:31000".parse().unwrap();
186207

187208
// simulate a node that's trying to push data

0 commit comments

Comments
 (0)