Skip to content

Instantly share code, notes, and snippets.

View yihuang's full-sized avatar
🏠
Working from home

yihuang yihuang

🏠
Working from home
  • crypto.com
  • Shenzhen, China
View GitHub Profile

tree math

struct LeafSize(u32);
struct ParentSize(u32);
struct NodeSize(u32);

impl LeafSize {
    // returns None when leafs in (0, 1)
    fn sibling(self, leafs: LeafSize) -> Option<LeafSize>;
@yihuang
yihuang / websocket-jsonrpc-multiplex.rs
Last active May 27, 2020 17:01
Multiplex concurrent jsonrpc request on one websocket connection with smol
use std::cell::RefCell;
use std::collections::HashMap;
use std::net::TcpStream;
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use anyhow::Result;
use easy_parallel::Parallel;
use futures::channel::{mpsc, oneshot};

Mock enclave

helpers

// called by client to encrypt tx
fn encrypt(tx: PlainTxAux) -> TxObfuscated {
  TxObfuscated {
    key_from: 0,
    init_vector; [0; 12],
@yihuang
yihuang / api.md
Created March 25, 2020 03:51
api description
@yihuang
yihuang / validator_stat_spec.md
Last active February 5, 2020 09:56
Using relational database concepts to clarify validator stat logic
-- current validator state, in memory.
create table validator (
  staking_address primary key,
  bonded_coins btree_indexed,
  voting_power = bonded_coins / 10000_0000,
  validator_address unique_indexed,
  council_node_info json,
  proposer_stat int,  -- or vote_stat
  punishment json null,

1、Run the docker version of PostgreSQL 12

$ docker run --rm -p 5432:5432 -v /tmp:/data -e POSTGRES_USER=exchange -e POSTGRES_DB=exchange -e POSTGRES_PASSWORD=123456 postgres:12-alpine

2、Download the attached files to /tmp 3、Import the schema and data

$ docker exec -it $CONTAINER psql -U exchange exchange -f /data/t_order_schema.sql
$ docker exec -it $CONTAINER psql -U exchange exchange -f /data/t_order_data.sql
@yihuang
yihuang / cyclic_type_reference.rs
Last active July 26, 2019 14:45
cyclic type reference in rust
use std::fmt;
trait Listener where Self: Sized + fmt::Debug {
fn on_event(&self, obj: &TestObject<Self>, args: &str);
}
#[derive(Debug)]
struct TestObject<F> where F:Listener {
listener: F
}
@yihuang
yihuang / gadt-and-partial-function.hs
Last active May 10, 2019 18:06
gadt and partial function
{-# LANGUAGE GADTs, KindSignatures, DataKinds #-}
module Test where
data Test =
Test1 { test1 :: Int }
| Test2 { test2 :: Int }
data TestBranch = TTest1 | TTest2
data Test' (a :: TestBranch) where
Test'1 :: { test'1 :: Int } -> Test' TTest1
@yihuang
yihuang / cardano-v1-api-demo.py
Created May 30, 2018 06:53
Demonstrate use cardano v1 api to receive funds.
from pyswagger import App, Security
from pyswagger.contrib.client.requests import Client
from pyswagger.primitives import Primitive, create_str, validate_str
from mnemonic import Mnemonic
def validate_hex_base16(obj, ret, val, ctx):
print('validate', val)
return val
prim = Primitive()
@yihuang
yihuang / gist:358e0b1944e8c0cac204d20eb6efd35e
Last active May 30, 2018 05:38
cardano-wallet-swagger_v1.json
{"swagger":"2.0","info":{"version":"cardano-sl:1","title":"Cardano Wallet API","license":{"url":"https://raw.githubusercontent.com/input-output-hk/cardano-sl/develop/lib/LICENSE","name":"MIT"},"description":"This is the specification for the Cardano Wallet API, automatically generated as a [Swagger](https://swagger.io/) spec from the [Servant](http://haskell-servant.readthedocs.io/en/stable/) API of [Cardano](https://github.com/input-output-hk/cardano-sl).\n\nSoftware Version | Git Revision\n-------------------|-------------------\ncardano-sl:1 | c3a292fee627bc9e76bb5b4896df59471a4dbdc4\n\n> **Warning**: This version is currently a **BETA-release** which is still under testing before\n> its final stable release. Should you encounter any issues or have any remarks, please let us\n> know; your feedback is highly appreciated.\n\n\nGetting Started\n===============\n\nIn the following examples, we will use *curl* to illustrate request to an API running on the default port **8090**.\n\nPlease note that wallet web