RLP encoder refactoring

This commit is contained in:
arkpar
2016-01-27 12:14:57 +01:00
parent e61d1f810e
commit 40314614f7
15 changed files with 234 additions and 178 deletions

View File

@@ -124,11 +124,10 @@ pub struct CapabilityInfo {
}
impl Encodable for CapabilityInfo {
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
encoder.emit_list(|e| {
self.protocol.encode(e);
(self.version as u32).encode(e);
});
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(2);
s.append(&self.protocol);
s.append(&self.version);
}
}

View File

@@ -199,10 +199,10 @@ impl Session {
fn write_hello(&mut self, host: &HostInfo) -> Result<(), UtilError> {
let mut rlp = RlpStream::new();
rlp.append_raw(&[PACKET_HELLO as u8], 0);
rlp.append_list(5)
rlp.begin_list(5)
.append(&host.protocol_version)
.append(&host.client_version)
.append(&host.capabilities)
.append_list(&host.capabilities)
.append(&host.listen_port)
.append(host.id());
self.connection.send_packet(&rlp.out())
@@ -267,7 +267,7 @@ impl Session {
fn disconnect(&mut self, reason: DisconnectReason) -> NetworkError {
let mut rlp = RlpStream::new();
rlp.append(&(PACKET_DISCONNECT as u32));
rlp.append_list(1);
rlp.begin_list(1);
rlp.append(&(reason.clone() as u32));
self.connection.send_packet(&rlp.out()).ok();
NetworkError::Disconnect(reason)
@@ -276,7 +276,7 @@ impl Session {
fn prepare(packet_id: u8) -> Result<RlpStream, UtilError> {
let mut rlp = RlpStream::new();
rlp.append(&(packet_id as u32));
rlp.append_list(0);
rlp.begin_list(0);
Ok(rlp)
}

View File

@@ -75,6 +75,7 @@ fn test_net_service() {
#[test]
fn test_net_connect() {
::env_logger::init().ok();
let key1 = KeyPair::create().unwrap();
let mut config1 = NetworkConfiguration::new_with_port(30344);
config1.use_secret = Some(key1.secret().clone());