Skip to content

Commit

Permalink
events: generate event data format for usage in sdks (#2458)
Browse files Browse the repository at this point in the history
* events: generate event data format for usage in sdks

* rpc: return max block count instead of error

* fix test
  • Loading branch information
nanne007 authored Apr 28, 2021
1 parent 52f9d1f commit 99f0392
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 11 deletions.
24 changes: 23 additions & 1 deletion dataformat-generator/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ use starcoin_crypto::{
// use starcoin_rpc_api::types::pubsub::Kind;
use starcoin_types::access_path::{AccessPath, DataPath, DataType};
use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_config::AccountResource;
use starcoin_types::account_config::accept_token_payment::AcceptTokenEvent;
use starcoin_types::account_config::block::NewBlockEvent;
use starcoin_types::account_config::{
AccountResource, BlockRewardEvent, BurnEvent, DepositEvent, MintEvent, ProposalCreatedEvent,
VoteChangedEvent, WithdrawEvent,
};
use starcoin_types::block_metadata::BlockMetadata;
use starcoin_types::contract_event::{ContractEvent, ContractEventV0};
use starcoin_types::event::EventKey;
Expand Down Expand Up @@ -91,6 +96,23 @@ fn generate() -> Result<(), Error> {
let registry = tracer.registry()?;
let data = serde_yaml::to_string(&registry).unwrap();
std::fs::write("../etc/starcoin_types.yml", &data).unwrap();

{
let mut tracer = Tracer::new(TracerConfig::default());
let samples = Samples::new();
tracer.trace_type::<WithdrawEvent>(&samples)?;
tracer.trace_type::<DepositEvent>(&samples)?;
tracer.trace_type::<AcceptTokenEvent>(&samples)?;
tracer.trace_type::<BlockRewardEvent>(&samples)?;
tracer.trace_type::<BurnEvent>(&samples)?;
tracer.trace_type::<MintEvent>(&samples)?;
tracer.trace_type::<ProposalCreatedEvent>(&samples)?;
tracer.trace_type::<VoteChangedEvent>(&samples)?;
tracer.trace_type::<NewBlockEvent>(&samples)?;
let registry = tracer.registry()?;
let data = serde_yaml::to_string(&registry).unwrap();
std::fs::write("../etc/onchain_events.yml", &data).unwrap();
}
// println!("{}", data);
Ok(())
}
68 changes: 68 additions & 0 deletions etc/onchain_events.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
AcceptTokenEvent:
STRUCT:
- token_code:
TYPENAME: TokenCode
AccountAddress:
NEWTYPESTRUCT:
TUPLEARRAY:
CONTENT: U8
SIZE: 16
BlockRewardEvent:
STRUCT:
- block_number: U64
- block_reward: U128
- gas_fees: U128
- miner:
TYPENAME: AccountAddress
BurnEvent:
STRUCT:
- amount: U128
- token_code:
TYPENAME: TokenCode
DepositEvent:
STRUCT:
- amount: U128
- token_code:
TYPENAME: TokenCode
- metadata:
SEQ: U8
MintEvent:
STRUCT:
- amount: U128
- token_code:
TYPENAME: TokenCode
NewBlockEvent:
STRUCT:
- number: U64
- author:
TYPENAME: AccountAddress
- timestamp: U64
- uncles: U64
ProposalCreatedEvent:
STRUCT:
- proposal_id: U64
- proposer:
TYPENAME: AccountAddress
TokenCode:
STRUCT:
- address:
TYPENAME: AccountAddress
- module: STR
- name: STR
VoteChangedEvent:
STRUCT:
- proposal_id: U64
- proposer:
TYPENAME: AccountAddress
- voter:
TYPENAME: AccountAddress
- agree: BOOL
- vote: U128
WithdrawEvent:
STRUCT:
- amount: U128
- token_code:
TYPENAME: TokenCode
- metadata:
SEQ: U8
17 changes: 7 additions & 10 deletions rpc/server/src/module/chain_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,13 @@ where
Some(num) => num,
None => service.clone().main_head_header().await?.number(),
};
let max_return_num = end_block_number.min(count);
let block_query_max_range = config.rpc.block_query_max_range();
if max_return_num > block_query_max_range {
return Err(jsonrpc_core::Error::invalid_params(format!(
"would return too many blocks, please decrease count param to {}",
block_query_max_range
))
.into());
}
let block = service.main_blocks_by_number(number, count).await?;

let max_return_num = count
.min(end_block_number + 1)
.min(config.rpc.block_query_max_range());
let block = service
.main_blocks_by_number(number, max_return_num)
.await?;

block
.into_iter()
Expand Down
34 changes: 34 additions & 0 deletions vm/types/src/account_config/events/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::account_address::AccountAddress;
use crate::contract_event::ContractEvent;
use crate::language_storage::TypeTag;
use crate::move_resource::MoveResource;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
pub struct NewBlockEvent {
number: u64,
author: AccountAddress,
timestamp: u64,
uncles: u64,
}
impl NewBlockEvent {
pub fn try_from_bytes(bytes: &[u8]) -> anyhow::Result<Self> {
bcs_ext::from_bytes(bytes).map_err(Into::into)
}
}

impl MoveResource for NewBlockEvent {
const MODULE_NAME: &'static str = "Block";
const STRUCT_NAME: &'static str = "NewBlockEvent";
}

impl TryFrom<&ContractEvent> for NewBlockEvent {
type Error = anyhow::Error;

fn try_from(event: &ContractEvent) -> anyhow::Result<Self> {
if event.type_tag() != &TypeTag::Struct(Self::struct_tag()) {
anyhow::bail!("Expected {}", Self::STRUCT_NAME);
}
Self::try_from_bytes(event.event_data())
}
}
1 change: 1 addition & 0 deletions vm/types/src/account_config/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub mod accept_token_payment;
pub mod account_deposit;
pub mod account_withdraw;
pub mod block;
mod block_reward_event;
pub mod burn;
pub mod dao;
Expand Down

0 comments on commit 99f0392

Please sign in to comment.