dg_xch_core 2.1.0

Core library containing type/error definitions, CLVM tools, Consensus and Pool definitions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
pub mod error;
pub mod farmer;
pub mod full_node;
pub mod harvester;
pub mod introducer;
pub mod pool;
pub mod shared;
pub mod timelord;
pub mod wallet;

use crate::blockchain::sized_bytes::Bytes32;
use crate::utils::await_termination;
use async_trait::async_trait;
use dg_xch_macros::ChiaSerial;
use dg_xch_serialize::ChiaProtocolVersion;
use dg_xch_serialize::ChiaSerialize;
use futures_util::stream::{FusedStream, SplitSink, SplitStream};
use futures_util::SinkExt;
use futures_util::{Sink, Stream, StreamExt};
use hyper::upgrade::Upgraded;
use hyper_util::rt::TokioIo;
use log::{debug, error, info};
use std::collections::HashMap;
use std::fmt;
use std::io::{Cursor, Error, ErrorKind};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::net::TcpStream;
use tokio::select;
use tokio::sync::RwLock;
use tokio_tungstenite::tungstenite::error::ProtocolError;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use uuid::Uuid;

#[repr(u8)]
#[derive(ChiaSerial, Copy, Clone, Debug, PartialEq, Eq)]
pub enum ProtocolMessageTypes {
    Unknown = 0,
    //Shared protocol (all services)
    Handshake = 1,

    //Harvester protocol (harvester < -> farmer)
    HarvesterHandshake = 3,
    //NewSignagePointHarvester = 4 Changed to 66 in new protocol
    NewProofOfSpace = 5,
    RequestSignatures = 6,
    RespondSignatures = 7,

    //Farmer protocol (farmer < -> full_node)
    NewSignagePoint = 8,
    DeclareProofOfSpace = 9,
    RequestSignedValues = 10,
    SignedValues = 11,
    FarmingInfo = 12,

    //Timelord protocol (timelord < -> full_node)
    NewPeakTimelord = 13,
    NewUnfinishedBlockTimelord = 14,
    NewInfusionPointVdf = 15,
    NewSignagePointVdf = 16,
    NewEndOfSubSlotVdf = 17,
    RequestCompactProofOfTime = 18,
    RespondCompactProofOfTime = 19,

    //Full node protocol (full_node < -> full_node)
    NewPeak = 20,
    NewTransaction = 21,
    RequestTransaction = 22,
    RespondTransaction = 23,
    RequestProofOfWeight = 24,
    RespondProofOfWeight = 25,
    RequestBlock = 26,
    RespondBlock = 27,
    RejectBlock = 28,
    RequestBlocks = 29,
    RespondBlocks = 30,
    RejectBlocks = 31,
    NewUnfinishedBlock = 32,
    RequestUnfinishedBlock = 33,
    RespondUnfinishedBlock = 34,
    NewSignagePointOrEndOfSubSlot = 35,
    RequestSignagePointOrEndOfSubSlot = 36,
    RespondSignagePoint = 37,
    RespondEndOfSubSlot = 38,
    RequestMempoolTransactions = 39,
    RequestCompactVdf = 40,
    RespondCompactVdf = 41,
    NewCompactVdf = 42,
    RequestPeers = 43,
    RespondPeers = 44,
    NoneResponse = 91,

    //Wallet protocol (wallet < -> full_node)
    RequestPuzzleSolution = 45,
    RespondPuzzleSolution = 46,
    RejectPuzzleSolution = 47,
    SendTransaction = 48,
    TransactionAck = 49,
    NewPeakWallet = 50,
    RequestBlockHeader = 51,
    RespondBlockHeader = 52,
    RejectHeaderRequest = 53,
    RequestRemovals = 54,
    RespondRemovals = 55,
    RejectRemovalsRequest = 56,
    RequestAdditions = 57,
    RespondAdditions = 58,
    RejectAdditionsRequest = 59,
    RequestHeaderBlocks = 60,
    RejectHeaderBlocks = 61,
    RespondHeaderBlocks = 62,

    //Introducer protocol (introducer < -> full_node)
    RequestPeersIntroducer = 63,
    RespondPeersIntroducer = 64,

    //Simulator protocol
    FarmNewBlock = 65,

    //New harvester protocol
    NewSignagePointHarvester = 66,
    RequestPlots = 67,
    RespondPlots = 68,
    PlotSyncStart = 78,
    PlotSyncLoaded = 79,
    PlotSyncRemoved = 80,
    PlotSyncInvalid = 81,
    PlotSyncKeysMissing = 82,
    PlotSyncDuplicates = 83,
    PlotSyncDone = 84,
    PlotSyncResponse = 85,

    //More wallet protocol
    CoinStateUpdate = 69,
    RegisterInterestInPuzzleHash = 70,
    RespondToPhUpdate = 71,
    RegisterInterestInCoin = 72,
    RespondToCoinUpdate = 73,
    RequestChildren = 74,
    RespondChildren = 75,
    RequestSesHashes = 76,
    RespondSesHashes = 77,
    RequestBlockHeaders = 86,
    RejectBlockHeaders = 87,
    RespondBlockHeaders = 88,
    RequestFeeEstimates = 89,
    RespondFeeEstimates = 90,
}
impl From<u8> for ProtocolMessageTypes {
    #[allow(clippy::too_many_lines)]
    fn from(byte: u8) -> Self {
        match byte {
            i if i == ProtocolMessageTypes::Handshake as u8 => ProtocolMessageTypes::Handshake,
            i if i == ProtocolMessageTypes::HarvesterHandshake as u8 => {
                ProtocolMessageTypes::HarvesterHandshake
            }
            i if i == ProtocolMessageTypes::NewProofOfSpace as u8 => {
                ProtocolMessageTypes::NewProofOfSpace
            }
            i if i == ProtocolMessageTypes::RequestSignatures as u8 => {
                ProtocolMessageTypes::RequestSignatures
            }
            i if i == ProtocolMessageTypes::RespondSignatures as u8 => {
                ProtocolMessageTypes::RespondSignatures
            }
            i if i == ProtocolMessageTypes::NewSignagePoint as u8 => {
                ProtocolMessageTypes::NewSignagePoint
            }
            i if i == ProtocolMessageTypes::DeclareProofOfSpace as u8 => {
                ProtocolMessageTypes::DeclareProofOfSpace
            }
            i if i == ProtocolMessageTypes::RequestSignedValues as u8 => {
                ProtocolMessageTypes::RequestSignedValues
            }
            i if i == ProtocolMessageTypes::SignedValues as u8 => {
                ProtocolMessageTypes::SignedValues
            }
            i if i == ProtocolMessageTypes::FarmingInfo as u8 => ProtocolMessageTypes::FarmingInfo,
            i if i == ProtocolMessageTypes::NewPeakTimelord as u8 => {
                ProtocolMessageTypes::NewPeakTimelord
            }
            i if i == ProtocolMessageTypes::NewUnfinishedBlockTimelord as u8 => {
                ProtocolMessageTypes::NewUnfinishedBlockTimelord
            }
            i if i == ProtocolMessageTypes::NewInfusionPointVdf as u8 => {
                ProtocolMessageTypes::NewInfusionPointVdf
            }
            i if i == ProtocolMessageTypes::NewSignagePointVdf as u8 => {
                ProtocolMessageTypes::NewSignagePointVdf
            }
            i if i == ProtocolMessageTypes::NewEndOfSubSlotVdf as u8 => {
                ProtocolMessageTypes::NewEndOfSubSlotVdf
            }
            i if i == ProtocolMessageTypes::RequestCompactProofOfTime as u8 => {
                ProtocolMessageTypes::RequestCompactProofOfTime
            }
            i if i == ProtocolMessageTypes::RespondCompactProofOfTime as u8 => {
                ProtocolMessageTypes::RespondCompactProofOfTime
            }
            i if i == ProtocolMessageTypes::NewPeak as u8 => ProtocolMessageTypes::NewPeak,
            i if i == ProtocolMessageTypes::NewTransaction as u8 => {
                ProtocolMessageTypes::NewTransaction
            }
            i if i == ProtocolMessageTypes::RequestTransaction as u8 => {
                ProtocolMessageTypes::RequestTransaction
            }
            i if i == ProtocolMessageTypes::RespondTransaction as u8 => {
                ProtocolMessageTypes::RespondTransaction
            }
            i if i == ProtocolMessageTypes::RequestProofOfWeight as u8 => {
                ProtocolMessageTypes::RequestProofOfWeight
            }
            i if i == ProtocolMessageTypes::RespondProofOfWeight as u8 => {
                ProtocolMessageTypes::RespondProofOfWeight
            }
            i if i == ProtocolMessageTypes::RequestBlock as u8 => {
                ProtocolMessageTypes::RequestBlock
            }
            i if i == ProtocolMessageTypes::RespondBlock as u8 => {
                ProtocolMessageTypes::RespondBlock
            }
            i if i == ProtocolMessageTypes::RejectBlock as u8 => ProtocolMessageTypes::RejectBlock,
            i if i == ProtocolMessageTypes::RequestBlocks as u8 => {
                ProtocolMessageTypes::RequestBlocks
            }
            i if i == ProtocolMessageTypes::RespondBlocks as u8 => {
                ProtocolMessageTypes::RespondBlocks
            }
            i if i == ProtocolMessageTypes::RejectBlocks as u8 => {
                ProtocolMessageTypes::RejectBlocks
            }
            i if i == ProtocolMessageTypes::NewUnfinishedBlock as u8 => {
                ProtocolMessageTypes::NewUnfinishedBlock
            }
            i if i == ProtocolMessageTypes::RequestUnfinishedBlock as u8 => {
                ProtocolMessageTypes::RequestUnfinishedBlock
            }
            i if i == ProtocolMessageTypes::RespondUnfinishedBlock as u8 => {
                ProtocolMessageTypes::RespondUnfinishedBlock
            }
            i if i == ProtocolMessageTypes::NewSignagePointOrEndOfSubSlot as u8 => {
                ProtocolMessageTypes::NewSignagePointOrEndOfSubSlot
            }
            i if i == ProtocolMessageTypes::RequestSignagePointOrEndOfSubSlot as u8 => {
                ProtocolMessageTypes::RequestSignagePointOrEndOfSubSlot
            }
            i if i == ProtocolMessageTypes::RespondSignagePoint as u8 => {
                ProtocolMessageTypes::RespondSignagePoint
            }
            i if i == ProtocolMessageTypes::RespondEndOfSubSlot as u8 => {
                ProtocolMessageTypes::RespondEndOfSubSlot
            }
            i if i == ProtocolMessageTypes::RequestMempoolTransactions as u8 => {
                ProtocolMessageTypes::RequestMempoolTransactions
            }
            i if i == ProtocolMessageTypes::RequestCompactVdf as u8 => {
                ProtocolMessageTypes::RequestCompactVdf
            }
            i if i == ProtocolMessageTypes::RespondCompactVdf as u8 => {
                ProtocolMessageTypes::RespondCompactVdf
            }
            i if i == ProtocolMessageTypes::NewCompactVdf as u8 => {
                ProtocolMessageTypes::NewCompactVdf
            }
            i if i == ProtocolMessageTypes::RequestPeers as u8 => {
                ProtocolMessageTypes::RequestPeers
            }
            i if i == ProtocolMessageTypes::RespondPeers as u8 => {
                ProtocolMessageTypes::RespondPeers
            }
            i if i == ProtocolMessageTypes::NoneResponse as u8 => {
                ProtocolMessageTypes::NoneResponse
            }
            i if i == ProtocolMessageTypes::RequestPuzzleSolution as u8 => {
                ProtocolMessageTypes::RequestPuzzleSolution
            }
            i if i == ProtocolMessageTypes::RespondPuzzleSolution as u8 => {
                ProtocolMessageTypes::RespondPuzzleSolution
            }
            i if i == ProtocolMessageTypes::RejectPuzzleSolution as u8 => {
                ProtocolMessageTypes::RejectPuzzleSolution
            }
            i if i == ProtocolMessageTypes::SendTransaction as u8 => {
                ProtocolMessageTypes::SendTransaction
            }
            i if i == ProtocolMessageTypes::TransactionAck as u8 => {
                ProtocolMessageTypes::TransactionAck
            }
            i if i == ProtocolMessageTypes::NewPeakWallet as u8 => {
                ProtocolMessageTypes::NewPeakWallet
            }
            i if i == ProtocolMessageTypes::RequestBlockHeader as u8 => {
                ProtocolMessageTypes::RequestBlockHeader
            }
            i if i == ProtocolMessageTypes::RespondBlockHeader as u8 => {
                ProtocolMessageTypes::RespondBlockHeader
            }
            i if i == ProtocolMessageTypes::RejectHeaderRequest as u8 => {
                ProtocolMessageTypes::RejectHeaderRequest
            }
            i if i == ProtocolMessageTypes::RequestRemovals as u8 => {
                ProtocolMessageTypes::RequestRemovals
            }
            i if i == ProtocolMessageTypes::RespondRemovals as u8 => {
                ProtocolMessageTypes::RespondRemovals
            }
            i if i == ProtocolMessageTypes::RejectRemovalsRequest as u8 => {
                ProtocolMessageTypes::RejectRemovalsRequest
            }
            i if i == ProtocolMessageTypes::RequestAdditions as u8 => {
                ProtocolMessageTypes::RequestAdditions
            }
            i if i == ProtocolMessageTypes::RespondAdditions as u8 => {
                ProtocolMessageTypes::RespondAdditions
            }
            i if i == ProtocolMessageTypes::RejectAdditionsRequest as u8 => {
                ProtocolMessageTypes::RejectAdditionsRequest
            }
            i if i == ProtocolMessageTypes::RequestHeaderBlocks as u8 => {
                ProtocolMessageTypes::RequestHeaderBlocks
            }
            i if i == ProtocolMessageTypes::RejectHeaderBlocks as u8 => {
                ProtocolMessageTypes::RejectHeaderBlocks
            }
            i if i == ProtocolMessageTypes::RespondHeaderBlocks as u8 => {
                ProtocolMessageTypes::RespondHeaderBlocks
            }
            i if i == ProtocolMessageTypes::RequestPeersIntroducer as u8 => {
                ProtocolMessageTypes::RequestPeersIntroducer
            }
            i if i == ProtocolMessageTypes::RespondPeersIntroducer as u8 => {
                ProtocolMessageTypes::RespondPeersIntroducer
            }
            i if i == ProtocolMessageTypes::FarmNewBlock as u8 => {
                ProtocolMessageTypes::FarmNewBlock
            }
            i if i == ProtocolMessageTypes::NewSignagePointHarvester as u8 => {
                ProtocolMessageTypes::NewSignagePointHarvester
            }
            i if i == ProtocolMessageTypes::RequestPlots as u8 => {
                ProtocolMessageTypes::RequestPlots
            }
            i if i == ProtocolMessageTypes::RespondPlots as u8 => {
                ProtocolMessageTypes::RespondPlots
            }
            i if i == ProtocolMessageTypes::PlotSyncStart as u8 => {
                ProtocolMessageTypes::PlotSyncStart
            }
            i if i == ProtocolMessageTypes::PlotSyncLoaded as u8 => {
                ProtocolMessageTypes::PlotSyncLoaded
            }
            i if i == ProtocolMessageTypes::PlotSyncRemoved as u8 => {
                ProtocolMessageTypes::PlotSyncRemoved
            }
            i if i == ProtocolMessageTypes::PlotSyncInvalid as u8 => {
                ProtocolMessageTypes::PlotSyncInvalid
            }
            i if i == ProtocolMessageTypes::PlotSyncKeysMissing as u8 => {
                ProtocolMessageTypes::PlotSyncKeysMissing
            }
            i if i == ProtocolMessageTypes::PlotSyncDuplicates as u8 => {
                ProtocolMessageTypes::PlotSyncDuplicates
            }
            i if i == ProtocolMessageTypes::PlotSyncDone as u8 => {
                ProtocolMessageTypes::PlotSyncDone
            }
            i if i == ProtocolMessageTypes::PlotSyncResponse as u8 => {
                ProtocolMessageTypes::PlotSyncResponse
            }
            i if i == ProtocolMessageTypes::CoinStateUpdate as u8 => {
                ProtocolMessageTypes::CoinStateUpdate
            }
            i if i == ProtocolMessageTypes::RegisterInterestInPuzzleHash as u8 => {
                ProtocolMessageTypes::RegisterInterestInPuzzleHash
            }
            i if i == ProtocolMessageTypes::RespondToPhUpdate as u8 => {
                ProtocolMessageTypes::RespondToPhUpdate
            }
            i if i == ProtocolMessageTypes::RegisterInterestInCoin as u8 => {
                ProtocolMessageTypes::RegisterInterestInCoin
            }
            i if i == ProtocolMessageTypes::RespondToCoinUpdate as u8 => {
                ProtocolMessageTypes::RespondToCoinUpdate
            }
            i if i == ProtocolMessageTypes::RequestChildren as u8 => {
                ProtocolMessageTypes::RequestChildren
            }
            i if i == ProtocolMessageTypes::RespondChildren as u8 => {
                ProtocolMessageTypes::RespondChildren
            }
            i if i == ProtocolMessageTypes::RequestSesHashes as u8 => {
                ProtocolMessageTypes::RequestSesHashes
            }
            i if i == ProtocolMessageTypes::RespondSesHashes as u8 => {
                ProtocolMessageTypes::RespondSesHashes
            }
            i if i == ProtocolMessageTypes::RequestBlockHeaders as u8 => {
                ProtocolMessageTypes::RequestBlockHeaders
            }
            i if i == ProtocolMessageTypes::RejectBlockHeaders as u8 => {
                ProtocolMessageTypes::RejectBlockHeaders
            }
            i if i == ProtocolMessageTypes::RespondBlockHeaders as u8 => {
                ProtocolMessageTypes::RespondBlockHeaders
            }
            i if i == ProtocolMessageTypes::RequestFeeEstimates as u8 => {
                ProtocolMessageTypes::RequestFeeEstimates
            }
            i if i == ProtocolMessageTypes::RespondFeeEstimates as u8 => {
                ProtocolMessageTypes::RespondFeeEstimates
            }
            _ => ProtocolMessageTypes::Unknown,
        }
    }
}

impl fmt::Display for ProtocolMessageTypes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

pub const INVALID_PROTOCOL_BAN_SECONDS: u8 = 10;
pub const API_EXCEPTION_BAN_SECONDS: u8 = 10;
pub const INTERNAL_PROTOCOL_ERROR_BAN_SECONDS: u8 = 10;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum NodeType {
    Unknown = 0,
    FullNode = 1,
    Harvester = 2,
    Farmer = 3,
    Timelord = 4,
    Introducer = 5,
    Wallet = 6,
    DataLayer = 7,
}
impl From<u8> for NodeType {
    fn from(byte: u8) -> Self {
        match byte {
            i if i == NodeType::Unknown as u8 => NodeType::Unknown,
            i if i == NodeType::FullNode as u8 => NodeType::FullNode,
            i if i == NodeType::Harvester as u8 => NodeType::Harvester,
            i if i == NodeType::Farmer as u8 => NodeType::Farmer,
            i if i == NodeType::Timelord as u8 => NodeType::Timelord,
            i if i == NodeType::Introducer as u8 => NodeType::Introducer,
            i if i == NodeType::Wallet as u8 => NodeType::Wallet,
            i if i == NodeType::DataLayer as u8 => NodeType::DataLayer,
            _ => NodeType::Unknown,
        }
    }
}

#[async_trait]
pub trait MessageHandler {
    async fn handle(
        &self,
        msg: Arc<ChiaMessage>,
        peer_id: Arc<Bytes32>,
        peers: PeerMap,
    ) -> Result<(), Error>;
}

#[derive(ChiaSerial, Debug, Clone)]
pub struct ChiaMessage {
    pub msg_type: ProtocolMessageTypes,
    pub id: Option<u16>,
    pub data: Vec<u8>,
}
impl ChiaMessage {
    pub fn new<T: ChiaSerialize>(
        msg_type: ProtocolMessageTypes,
        version: ChiaProtocolVersion,
        msg: &T,
        id: Option<u16>,
    ) -> Self {
        ChiaMessage {
            msg_type,
            id,
            data: msg.to_bytes(version),
        }
    }
}
impl From<ChiaMessage> for Message {
    fn from(val: ChiaMessage) -> Self {
        Message::Binary(val.to_bytes(ChiaProtocolVersion::default()).into())
    }
}

pub type FilterFunction = Box<dyn Fn(&ChiaMessage) -> bool + Sync + Send + 'static>;

pub struct ChiaMessageFilter {
    pub msg_type: Option<ProtocolMessageTypes>,
    pub id: Option<u16>,
    pub custom_fn: Option<FilterFunction>,
}
impl ChiaMessageFilter {
    #[must_use]
    pub fn matches(&self, msg: &ChiaMessage) -> bool {
        if self.id.is_some() && self.id != msg.id {
            return false;
        }
        if let Some(s) = &self.msg_type {
            if *s != msg.msg_type {
                return false;
            }
        }
        if let Some(func) = &self.custom_fn {
            func(msg)
        } else {
            true
        }
    }
}

pub struct ChiaMessageHandler {
    pub filter: Arc<ChiaMessageFilter>,
    pub handle: Arc<dyn MessageHandler + Send + Sync>,
}
impl ChiaMessageHandler {
    pub fn new(
        filter: Arc<ChiaMessageFilter>,
        handle: Arc<dyn MessageHandler + Send + Sync>,
    ) -> Self {
        ChiaMessageHandler { filter, handle }
    }
}

pub type PeerMap = Arc<RwLock<HashMap<Bytes32, Arc<SocketPeer>>>>;

pub struct SocketPeer {
    pub node_type: Arc<RwLock<NodeType>>,
    pub protocol_version: Arc<RwLock<ChiaProtocolVersion>>,
    pub websocket: Arc<RwLock<WebsocketConnection>>,
}

pub enum WebsocketMsgStream {
    TokioIo(WebSocketStream<TokioIo<Upgraded>>),
    Tls(WebSocketStream<MaybeTlsStream<TcpStream>>),
}
impl Stream for WebsocketMsgStream {
    type Item = Result<Message, tokio_tungstenite::tungstenite::error::Error>;
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.get_mut() {
            WebsocketMsgStream::TokioIo(ref mut s) => Pin::new(s).poll_next(cx),
            WebsocketMsgStream::Tls(ref mut s) => Pin::new(s).poll_next(cx),
        }
    }
}
impl FusedStream for WebsocketMsgStream {
    fn is_terminated(&self) -> bool {
        match self {
            WebsocketMsgStream::TokioIo(s) => s.is_terminated(),
            WebsocketMsgStream::Tls(s) => s.is_terminated(),
        }
    }
}
impl Sink<Message> for WebsocketMsgStream {
    type Error = tokio_tungstenite::tungstenite::error::Error;
    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        match self.get_mut() {
            WebsocketMsgStream::TokioIo(ref mut s) => Pin::new(s).poll_ready(cx),
            WebsocketMsgStream::Tls(ref mut s) => Pin::new(s).poll_ready(cx),
        }
    }
    fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
        match self.get_mut() {
            WebsocketMsgStream::TokioIo(ref mut s) => Pin::new(s).start_send(item),
            WebsocketMsgStream::Tls(ref mut s) => Pin::new(s).start_send(item),
        }
    }
    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        match self.get_mut() {
            WebsocketMsgStream::TokioIo(ref mut s) => Pin::new(s).poll_flush(cx),
            WebsocketMsgStream::Tls(ref mut s) => Pin::new(s).poll_flush(cx),
        }
    }
    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        match self.get_mut() {
            WebsocketMsgStream::TokioIo(ref mut s) => Pin::new(s).poll_close(cx),
            WebsocketMsgStream::Tls(ref mut s) => Pin::new(s).poll_close(cx),
        }
    }
}

pub struct WebsocketConnection {
    write: SplitSink<WebsocketMsgStream, Message>,
    message_handlers: Arc<RwLock<HashMap<Uuid, Arc<ChiaMessageHandler>>>>,
}
impl WebsocketConnection {
    pub fn new(
        websocket: WebsocketMsgStream,
        message_handlers: Arc<RwLock<HashMap<Uuid, Arc<ChiaMessageHandler>>>>,
        peer_id: Arc<Bytes32>,
        peers: PeerMap,
    ) -> (Self, ReadStream) {
        let (write, read) = websocket.split();
        let websocket = WebsocketConnection {
            write,
            message_handlers: message_handlers.clone(),
        };
        let stream = ReadStream {
            read,
            message_handlers,
            peer_id,
            peers,
        };
        (websocket, stream)
    }
    pub async fn send(&mut self, msg: Message) -> Result<(), Error> {
        self.write
            .send(msg)
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e))
    }

    pub async fn subscribe(&self, uuid: Uuid, handle: Arc<ChiaMessageHandler>) {
        self.message_handlers.write().await.insert(uuid, handle);
    }

    pub async fn unsubscribe(&self, uuid: Uuid) -> Option<Arc<ChiaMessageHandler>> {
        self.message_handlers.write().await.remove(&uuid)
    }

    pub async fn close(&mut self, msg: Option<Message>) -> Result<(), Error> {
        if let Some(msg) = msg {
            let _ = self
                .write
                .send(msg)
                .await
                .map_err(|e| Error::new(ErrorKind::Other, e));
            self.write
                .close()
                .await
                .map_err(|e| Error::new(ErrorKind::Other, e))
        } else {
            self.write
                .close()
                .await
                .map_err(|e| Error::new(ErrorKind::Other, e))
        }
    }
    pub async fn clear(&self) {
        self.message_handlers.write().await.clear();
    }
    pub async fn shutdown(&mut self) -> Result<(), Error> {
        self.close(None).await
    }
}

pub struct ReadStream {
    read: SplitStream<WebsocketMsgStream>,
    message_handlers: Arc<RwLock<HashMap<Uuid, Arc<ChiaMessageHandler>>>>,
    peer_id: Arc<Bytes32>,
    peers: PeerMap,
}
impl ReadStream {
    pub async fn run(&mut self, run: Arc<AtomicBool>) {
        loop {
            let peer_self = self.peers.read().await.get(&self.peer_id).cloned();
            let protocol_version = if let Some(peer) = peer_self.as_ref() {
                *peer.protocol_version.read().await
            } else {
                ChiaProtocolVersion::default()
            };
            select! {
                msg = self.read.next() => {
                    match msg {
                        Some(Ok(msg)) => {
                            match msg {
                                Message::Binary(bin_data) => {
                                    let mut cursor = Cursor::new(&bin_data);
                                    match ChiaMessage::from_bytes(&mut cursor, protocol_version) {
                                        Ok(chia_msg) => {
                                            let msg_arc: Arc<ChiaMessage> = Arc::new(chia_msg);
                                            let mut matched = false;
                                            for v in self.message_handlers.read().await.values()
                                                .cloned().collect::<Vec<Arc<ChiaMessageHandler>>>() {
                                                if v.filter.matches(msg_arc.as_ref()) {
                                                    let msg_arc_c = msg_arc.clone();
                                                    let peer_id = self.peer_id.clone();
                                                    let peers = self.peers.clone();
                                                    let v_arc_c = v.handle.clone();
                                                    tokio::spawn(async move {
                                                        if let Err(e) = v_arc_c.handle(msg_arc_c.clone(), peer_id, peers).await {
                                                            error!("Error Handling Message({:#?}): {e:?}", msg_arc_c.msg_type);
                                                        }
                                                    });
                                                    matched = true;
                                                }
                                            }
                                            if !matched{
                                                error!("No Matches for Message: {:?}", &msg_arc);
                                            }
                                            debug!("Processed Message: {:?}", &msg_arc.msg_type);
                                        }
                                        Err(e) => {
                                            error!("Invalid Message: {:?}", e);
                                        }
                                    }
                                }
                                Message::Close(e) => {
                                    debug!("Server Got Close Message: {:?}", e);
                                    return;
                                },
                                _ => {
                                    error!("Invalid Message: {:?}", msg);
                                }
                            }
                        }
                        Some(Err(msg)) => {
                            match msg {
                                tokio_tungstenite::tungstenite::Error::Protocol(ProtocolError::ResetWithoutClosingHandshake) => {
                                    debug!("Server Stream Closed without Handshake");
                                },
                                others => {
                                    error!("Server Stream Error: {:?}", others);
                                }
                            }
                            return;
                        }
                        None => {
                            info!("End of server read Stream");
                            return;
                        }
                    }
                }
                _ = await_termination() => {
                    return;
                }
                () = async {
                    loop {
                        if !run.load(Ordering::Relaxed){
                            debug!("Server is exiting");
                            return;
                        }
                        tokio::time::sleep(Duration::from_millis(100)).await;
                    }
                } => {
                    return;
                }
            }
        }
    }
}