Skip to content

Commit

Permalink
refactor(protocol): impl fixed codec trait by proc macro (nervosnetwo…
Browse files Browse the repository at this point in the history
…rk#240)

* refactor: impl fixed codec trait by proc macro

* upfrade fixed codec derive version

* impl Fixedcodec for primitives types

* feat: impl FixedCodec for primitive types

* feat: impl FixedCodec for Bloom

* change: codec Hex

* refactor: encode and decode for Hex

* cargo clippy

* change: primitive codec

* temp

* feat: derive fixedcodec for transaction
  • Loading branch information
Eason Gao authored and zeroqn committed Apr 22, 2020
1 parent 1c1ffe1 commit 49effb5
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 619 deletions.
1 change: 1 addition & 0 deletions built-in-services/asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rlp = "0.4"
bytes = "0.5"
derive_more = "0.15"
byteorder = "1.3"
fixed-codec-derive = { git = "https://github.com/nervosnetwork/muta-utils.git", rev = "d9146fb" }

[dev-dependencies]
cita_trie = "2.0"
Expand Down
79 changes: 16 additions & 63 deletions built-in-services/asset/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use bytes::Bytes;
use fixed_codec_derive::RlpFixedCodec;
use serde::{Deserialize, Serialize};

use protocol::fixed_codec::{FixedCodec, FixedCodecError};
use protocol::types::{Address, Hash};
use protocol::ProtocolResult;

/// Payload
#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct InitGenesisPayload {
pub id: Hash,
pub name: String,
Expand All @@ -18,26 +18,26 @@ pub struct InitGenesisPayload {
pub issuer: Address,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct CreateAssetPayload {
pub name: String,
pub symbol: String,
pub supply: u64,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct GetAssetPayload {
pub id: Hash,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct TransferPayload {
pub asset_id: Hash,
pub to: Address,
pub value: u64,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct TransferEvent {
pub asset_id: Hash,
pub from: Address,
Expand All @@ -47,23 +47,23 @@ pub struct TransferEvent {

pub type ApprovePayload = TransferPayload;

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct ApproveEvent {
pub asset_id: Hash,
pub grantor: Address,
pub grantee: Address,
pub value: u64,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct TransferFromPayload {
pub asset_id: Hash,
pub sender: Address,
pub recipient: Address,
pub value: u64,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct TransferFromEvent {
pub asset_id: Hash,
pub caller: Address,
Expand All @@ -72,35 +72,35 @@ pub struct TransferFromEvent {
pub value: u64,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct GetBalancePayload {
pub asset_id: Hash,
pub user: Address,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug, Default)]
pub struct GetBalanceResponse {
pub asset_id: Hash,
pub user: Address,
pub balance: u64,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct GetAllowancePayload {
pub asset_id: Hash,
pub grantor: Address,
pub grantee: Address,
}

#[derive(Deserialize, Serialize, Clone, Debug, Default)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug, Default)]
pub struct GetAllowanceResponse {
pub asset_id: Hash,
pub grantor: Address,
pub grantee: Address,
pub value: u64,
}

#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug, PartialEq, Default)]
pub struct Asset {
pub id: Hash,
pub name: String,
Expand All @@ -114,59 +114,12 @@ pub struct AssetBalance {
pub allowance: BTreeMap<Address, u64>,
}

#[derive(RlpFixedCodec)]
struct AllowanceCodec {
pub addr: Address,
pub total: u64,
}

impl rlp::Decodable for Asset {
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
Ok(Self {
id: rlp.at(0)?.as_val()?,
name: rlp.at(1)?.as_val()?,
symbol: rlp.at(2)?.as_val()?,
supply: rlp.at(3)?.as_val()?,
issuer: rlp.at(4)?.as_val()?,
})
}
}

impl rlp::Encodable for Asset {
fn rlp_append(&self, s: &mut rlp::RlpStream) {
s.begin_list(5)
.append(&self.id)
.append(&self.name)
.append(&self.symbol)
.append(&self.supply)
.append(&self.issuer);
}
}

impl FixedCodec for Asset {
fn encode_fixed(&self) -> ProtocolResult<Bytes> {
Ok(Bytes::from(rlp::encode(self)))
}

fn decode_fixed(bytes: Bytes) -> ProtocolResult<Self> {
Ok(rlp::decode(bytes.as_ref()).map_err(FixedCodecError::from)?)
}
}

impl rlp::Decodable for AllowanceCodec {
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
Ok(Self {
addr: rlp.at(0)?.as_val()?,
total: rlp.at(1)?.as_val()?,
})
}
}

impl rlp::Encodable for AllowanceCodec {
fn rlp_append(&self, s: &mut rlp::RlpStream) {
s.begin_list(2).append(&self.addr).append(&self.total);
}
}

impl rlp::Decodable for AssetBalance {
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
let value = rlp.at(0)?.as_val()?;
Expand Down
1 change: 1 addition & 0 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rlp = "0.4"
cita_trie = "2.0"
json = "0.12"
byteorder = "1.3"
fixed-codec-derive = { git = "https://github.com/nervosnetwork/muta-utils.git", rev = "d9146fb" }

[dev-dependencies]
num-traits = "0.2"
Expand Down
181 changes: 0 additions & 181 deletions protocol/src/fixed_codec/block.rs

This file was deleted.

Loading

0 comments on commit 49effb5

Please sign in to comment.