-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UnsignedMessage cbor encoding #174
Changes from 3 commits
e561a0d
645d737
11e345d
4e40d10
07dde7d
ac4833c
8b915cb
d66569c
7048ebb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use address::Address; | ||
// use encoding::{from_slice, to_vec}; | ||
use encoding::to_vec; | ||
use hex::decode; | ||
use message::UnsignedMessage; | ||
use num_bigint::BigUint; | ||
use serde::Deserialize; | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
use std::str::FromStr; | ||
use vm::{MethodNum, TokenAmount}; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct MessageVector { | ||
to: String, | ||
from: String, | ||
nonce: u64, | ||
value: u64, | ||
gas_price: u128, | ||
gas_limit: u128, | ||
method: u64, | ||
params: String, | ||
} | ||
|
||
impl From<MessageVector> for UnsignedMessage { | ||
fn from(vector: MessageVector) -> UnsignedMessage { | ||
UnsignedMessage::builder() | ||
.to(Address::from_str(&vector.to).unwrap()) | ||
.from(Address::from_str(&vector.from).unwrap()) | ||
.sequence(vector.nonce) | ||
.value(TokenAmount::new(vector.value)) | ||
.method_num(MethodNum::new(vector.method)) | ||
// .params( | ||
// from_slice(&decode(vector.params).unwrap()).expect("bytes to MethodParams failed"), | ||
// ) | ||
.gas_limit(BigUint::from(vector.gas_limit)) | ||
.gas_price(BigUint::from(vector.gas_price)) | ||
.build() | ||
.unwrap() | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct TestVector { | ||
message: MessageVector, | ||
hex_cbor: String, | ||
} | ||
|
||
fn encode_assert_cbor(message: &UnsignedMessage, expected: &[u8]) { | ||
let enc_bz: Vec<u8> = to_vec(message).expect("cbor serialization failed"); | ||
|
||
// TODO remove only checking first bytes (after method param encoding format) | ||
assert_eq!(&enc_bz[..81], &expected[..81]); | ||
} | ||
|
||
#[test] | ||
fn unsigned_message_cbor_vectors() { | ||
let mut file = File::open("../../tests/cbor/unsigned_message_vectors.json").unwrap(); | ||
let mut string = String::new(); | ||
file.read_to_string(&mut string).unwrap(); | ||
|
||
let vectors: Vec<TestVector> = | ||
serde_json::from_str(&string).expect("test vector deserialization failed"); | ||
for tv in vectors { | ||
encode_assert_cbor( | ||
&UnsignedMessage::from(tv.message), | ||
&decode(tv.hex_cbor).expect("decoding cbor bytes failed"), | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use encoding::{ser, to_vec, Error as EncodingError}; | ||
use encoding::{de, ser, serde_bytes, to_vec, Error as EncodingError}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
/// Method number indicator for calling actor methods | ||
#[derive(Default, Clone, PartialEq, Debug, Serialize, Deserialize)] | ||
pub struct MethodNum(i32); // TODO: add constraints to this | ||
#[derive(Default, Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] | ||
pub struct MethodNum(u64); // TODO: add constraints to this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this not be an enum? Then you can impl the From traits to convert from a number type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nah, needs to be an integer because methods vary based on actor and needs to be future proofed for when you can initialize own actor with custom code |
||
|
||
// TODO verify format or implement custom serialize/deserialize function (if necessary): | ||
// https://github.com/ChainSafe/ferret/issues/143 | ||
|
||
impl MethodNum { | ||
/// Constructor for new MethodNum | ||
pub fn new(num: i32) -> Self { | ||
pub fn new(num: u64) -> Self { | ||
Self(num) | ||
} | ||
} | ||
|
||
impl From<MethodNum> for i32 { | ||
fn from(method_num: MethodNum) -> i32 { | ||
impl From<MethodNum> for u64 { | ||
fn from(method_num: MethodNum) -> u64 { | ||
method_num.0 | ||
} | ||
} | ||
|
@@ -72,13 +72,39 @@ impl Serialized { | |
} | ||
|
||
/// Method parameters used in Actor execution | ||
#[derive(Default, Clone, PartialEq, Debug, Serialize, Deserialize)] | ||
#[derive(Default, Clone, PartialEq, Debug)] | ||
pub struct MethodParams { | ||
params: Vec<Serialized>, | ||
} | ||
|
||
// TODO verify format or implement custom serialize/deserialize function (if necessary): | ||
// https://github.com/ChainSafe/ferret/issues/143 | ||
impl MethodParams { | ||
/// Constructor for new MethodParams | ||
pub fn new(params: Vec<Serialized>) -> Self { | ||
Self { params } | ||
} | ||
} | ||
|
||
impl ser::Serialize for MethodParams { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: ser::Serializer, | ||
{ | ||
let bz = [0]; // TODO | ||
let value = serde_bytes::Bytes::new(&bz); | ||
serde_bytes::Serialize::serialize(value, s) | ||
} | ||
} | ||
|
||
impl<'de> de::Deserialize<'de> for MethodParams { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: de::Deserializer<'de>, | ||
{ | ||
let bz: Vec<u8> = serde_bytes::Deserialize::deserialize(deserializer)?; | ||
// TODO | ||
Ok(MethodParams::new(vec![Serialized::new(bz)])) | ||
} | ||
} | ||
|
||
impl Deref for MethodParams { | ||
type Target = Vec<Serialized>; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be public, ya?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that was refactored 4 hours ago lol how did you even review that? And no, didn't need to be since it was just used in that file for consistent ser/de