WengQiang's Blog

where there is a will, there is a way!

go grammer

  1. the standard Go compiler and gccgo both don't allow local variables declared but not used. Package-level variables have no such limit. If a local variable is only ever used as destination values, it will also be viewed as unused.

  2. Overflows are not allowed for typed constant values but are allowed for non-constant and untyped constant values.
    • Non-constant integer values can be converted to strings.
    • Non-constant floating-point and integer values can be explicitly converted to any other floating-point and integer types.
    • Non-constant complex values can be explicitly converted to any other complex types.
  3. The name of the folder of a package is not required to be the same as the package name. However, for a library package, it will make package users confused if the name of the package is different from the name of the folder of the package.

    • A package-level variable is initialized after all of its depended variables.
    • All package-level variables declared in a package are initialized before any init function declared in the same package is invoked.
    • All init functions in all involved packages in a program will be invoked sequentially. An init function in an importing package will be invoked after all the init functions declared in the dependency packages of the importing package for sure. All init functions will be invoked before invoking the main entry function.
  4. The expressions enclosed within the body of an anonymous function call, whether the call is a general call or a deferred/goroutine call, will not be evaluated at the moment when the anonymous function call is invoked.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package main

    import "fmt"

    func main() {
    func() {
    for i := 0; i < 3; i++ {
    defer fmt.Println("a:", i)
    }
    }()
    fmt.Println()
    func() {
    for i := 0; i < 3; i++ {
    defer func() {
    fmt.Println("b:", i)
    }()
    }
    }()
    }

    output is: a: 2 a: 1 a: 0

            b: 3
            b: 3
            b: 3

    to get b: 2, b: 1, b: 0

    1
    2
    3
    4
    5
    6
    7
    func() {
    for i := 0; i < 3; i++ {
    defer func(i int) {
    fmt.Println("b:", i)
    }(i)
    }
    }()

go 数组大小不能超过2GB

需要针对数组元素的类型大小计算数组的最大长度范围 ([]uint8 最大2GB, []uint16 最大1GB,以此类推),但是 []struct{} 数组的长度可以超过2GB

goroutine

  1. 首先系统级的线程创建会创建默认大小的栈(一般默认可能为2Mb),主要用于保存递归函数调用的参数以及局部变量,默认大小的栈存在可能空间浪费或者栈不足的问题;而goroutine采用可以动态增减的栈,初始创建栈大小为(2kb或者4kb),根据需要动态增减(主流实现的栈大小可以达到1gb)
    阅读全文 »

segwit相关

segwit bips

see bitcoin bip141 bitcoin bip143 bitcoin bip144 bitcoin bip147 (chain activated on August 24, 2017)

block cost calculation

cost = (stripped_size * 4) + witness_size formula,
using only serialization with and without witness data. As witness_size
is equal to total_size - stripped_size, this formula is identical to:
cost = (stripped_size * 3) + total_size.

conditional probablity

P(A|B) = P(A ∩ B) / P(B)

The conditional probability means "what is the probability of event A given that we know event B occurred"

docker中运行ethereum go节点

  1. 安装ethereum(go version)的docker

docker pull ethereum:client-go

  1. 运行docker

sudo docker run -d --name wens-geth -v /mnt/main/wq/ethereum:/root/.ethereum -p 8588:8545 -p 30308:30303 ethereum/client-go --rpc --rpcaddr 0.0.0.0 --syncmode "fast" --cache=10240 --txpool.pricelimit 1 console

  1. 进入运行着的docker go节点

sudo docker run -it -v /mnt/main/wq/ethereum:/root/.ethereum ethereum/client-go attach

阅读全文 »

contract creation transaction: the target account address is 0. the payload of such transaction is taken to be EVM bytecode and executed.The output of this execution is permanently stored as the code of the contract

each account has its own persistant memory area called storage.

the second memory area is called memory, of which a contract obtains a freshly cleared instance for each message call.

EVM is not a register machine but a stack machine.

Calls are limited to a depth of 1024.

library

use the feature delegatecall: the contract can call the code of target address

fallback function

  1. A contract can have exactly one unnamed function. This function cannot have arguments and cannot return anything. It is executed on a call to the contract if none of the other functions match the given function identifier (or if no data was supplied at all).

  2. Furthermore, this function is executed whenever the contract receives plain Ether (without data). Additionally, in order to receive Ether, the fallback function must be marked payable.

  3. If no such function exists, the contract cannot receive Ether through regular transactions.

  4. Please ensure you test your fallback function thoroughly to ensure the execution cost is less than 2300 gas before deploying a contract.

0%