Files
grin-node/store/src/codec/tx_test.rs
T
Jacob Payne de4ebdde71 [WIP] Partial Transition from Writeable/Readable to Codecs (#51)
* Sample Signatures for put_enc and get_dec
* Implement put_enc and get_dec
* Implement ChainCodec in grin_chain
* Truncate src only on complete Blocks
* Truncate src only on complete Tip + Check Len
* Move BlockHeader Encoding to BlockHeaderCodec
* Define put_enc for store::Batch
* Replace BlockCodec and BlockHeaderCodec with generic BlockCodec<T>
* Implement Default for BlockCodec Manually
* Replace get_ser/put_ser with get_enc/get_dec for chain::ChainKVStore
* Remove Writeable/Readable for chain::Tip
* Add Tokio-io and Bytes to grin_p2p
* Additional Setup for Message enum + Msg{Encode,Decode} traits
* base msg ping pong encoding and test
* fill out msg-codec tests
* Implement Hand Encoding/Decoding
* msg-encode shake
* msg-encode getpeeraddr
* codec peer-addrs message, SockAddr struct wierdness
* header message codec
* msg encoding finished prelim
* Implement PeerCodec Encoding/Decoding
* Set PeerStore to use PeerCodec for Encoding/Decoding
* Add a DecIterator
* Prune PeerStore
* Replace Decoding and Encoding in handle_payload
* Prune Writeable/Readable methods in store::Store
* Remove Incomplete Frame Testing ( Not Nessesary right now )
* separate block and tx codec tests
* Refactor {Tx,Block}Codec Tests
2017-05-22 19:16:13 +02:00

94 lines
2.3 KiB
Rust

// Copyright 2016 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::io;
use tokio_io::*;
use bytes::{BytesMut, BigEndian, BufMut, Buf, IntoBuf};
use core::core::{Input, Output, Transaction};
use core::core::transaction::OutputFeatures;
use secp::pedersen::{RangeProof, Commitment};
use secp::constants::PEDERSEN_COMMITMENT_SIZE;
use super::tx::*;
#[test]
fn should_have_tx_codec_roundtrip() {
use tokio_io::codec::{Encoder, Decoder};
let input = Input(Commitment([1; PEDERSEN_COMMITMENT_SIZE]));
let output = Output {
features: OutputFeatures::empty(),
commit: Commitment([1; PEDERSEN_COMMITMENT_SIZE]),
proof: RangeProof {
proof: [1; 5134],
plen: 5134,
},
};
let tx = Transaction {
inputs: vec![input],
outputs: vec![output],
fee: 0,
excess_sig: vec![0; 10],
};
let mut buf = BytesMut::with_capacity(0);
let mut codec = TxCodec {};
codec
.encode(tx.clone(), &mut buf)
.expect("Error During Transaction Encoding");
let d_tx = codec
.decode(&mut buf)
.expect("Error During Transaction Decoding")
.expect("Unfinished Transaction");
assert_eq!(tx, d_tx);
}
#[test]
fn should_encode_and_decode_output() {
let output = Output {
features: OutputFeatures::empty(),
commit: Commitment([1; PEDERSEN_COMMITMENT_SIZE]),
proof: RangeProof {
proof: [1; 5134],
plen: 5134,
},
};
let mut buf = BytesMut::with_capacity(0);
output.tx_encode(&mut buf);
let d_output = Output::tx_decode(&mut buf).unwrap().unwrap();
assert_eq!(output, d_output);
}
#[test]
fn should_encode_and_decode_input() {
let input = Input(Commitment([1; PEDERSEN_COMMITMENT_SIZE]));
let mut buf = BytesMut::with_capacity(0);
input.tx_encode(&mut buf);
let d_input = Input::tx_decode(&mut buf).unwrap().unwrap();
assert_eq!(input, d_input);
}