Ethereum + IPFS
MindMap
Gas Used by Public and External Function in Solidity
Original
A simple example demostrating this effect looks like this:
1234567891011pragma solidity ^0.4.19;contract Test { function test(uint[20] a) public returns (uint) { return a[10] * 2; } function test2(uint[20] a) external returnss (uint) { return a[10] * 2; }}
Calling each function, the public function uses 496 gas, while the external function uses 261 gas.
The difference is because in public functions, Solidity immediately copies array argument to m ...
智能合约编写注意事项
原文连接
Overflow 与 UnderflowSolidity 可以处理 256 位数字, 最高为 2**256 - 1, 所以对 (2 ** 256 - 1) 加 1 会导致归 0.
同理, 对 unsigned 类型 0 做减 1 运算会得到 (2**256 - 1)
测试代码如下
123456789101112131415pragma solidity 0.4.18;contract OverflowUnderflow { uint public zero = 0; uint public max = 2**256 - 1; // zero will end up at 2 ** 256 - 1 function underflow() public { zero -= 1; } function overflow() public { max += 1; }}
尽管他们同样危险, 但 ...
Start in Protobuf
Protocol Buffers are a language-neutral, platform-neutral, extensible way of serializing structured data for use in commnucations protocols, data storage, and more, originally designed at Google.
Protobuf.js is a pure JS implementation with TypeScript support for node.js and browser. It’s easy to use, blazingly fast and works out of the box with .proto files.
InstallationNode.js
1npm install protobufjs
1var protobuf = require('protobufjs')
Browsers
1<script src="../protobuf.j ...
Get the Most Out of the CommonsChunkPlugin
Original
Use webpack-bundle-analyzer to generate a fancy colorful image of all of yourbundles.
Case 1: Many vendor bundles with duplicate code
Each single-page app is using a new CommonsChunkPlugin that targets just thatentry point, and its vendor code. This creates a bundle with only modules thatcome from node_modules folder, and another bundle with just application code.
The configuration portion was provided:
12345678Object.keys(activeApps).map( app => new webpack.optimize.CommonsChunk ...
Go Web
Original
IntroductionGo is a battery included programming language and has a webserver already builtin.
The net/http package from the standard library contains all functionalitiesabout the HTTP protocol.
This includes an HTTP client and an HTTP server.
Registering a Request HandlerFirst, create a Handler which receives all incoming HTTP connections frombrowsers, HTTP client or API requests. A handler in Go is a function with thissignature.
1func (w http.ResponseWriter, r *http.Request)
The func ...
Generate Ethereum Keys and Wallet Address
Original
This article is a guide on how to generate an ECDSA private key and derivesits Ethereum address.
Use Openssl and keccak-256sum from a terminal.
SHA3 != keccak. Ethereum is using the keccak-256 algorithm and not thestandard sha3.
Ethereum use keccak-256, it should be noted that it does not follow theFIPS-202 based standard(aka. SHA-3), which was finalized in August 2015
web3.utils.sha3 uses keccak-256 web3.sha3(string[, option]): keccak-256
Generate the EC private keyFirst of all w ...
Caution on Int8Array
1234567891011121314151617181920let t = crypto.getRandomValues(new Int8Array(3))// Int8Array(3) [-15, -17, -90]// t is not typeof ArrayArray.isArray(t)// false// element in Int8Array must be type Int8// method map returns array of same type as origin arraylet _t = []t.map(i => { let _tmp = i.toString(16) _t.push(_tmp) return _tmp})// Int8Array(3) [0, -11, 0]// _t: ['-f', '-11', '-5a']t[0] = 'a'// t: Int8Array(3) [0, -17, -90]
React-Lodable
react-lodable is a higher order component for loading components with dynamicimports.
Code-splitting is the process of taking one large bundle containing your entireapp, and splitting them up into multiple smaller bundles which contain seperateparts of your app.
This might seem difficult to do, but tools like Webpack have this built in, andReact Loadable is designed to make is super simple.
Route-based splitting vs. Component-based splittingA common piece of advice you will see is to break your ...
Difference Between Contract Calling in Web3
After we get the instance of Contract(testInstance), we can invoke its method by three ways:
testInstance.testFunc.sendTransaction()
It will make a transaction which will be broadcasted into the net, use gas and return the txHash
testInstance.testFunc.call()
Call the contract function in VM, no broadcast and no gas used, return the response from method
testInstance.testFunc()
If the testFunc is signified constant, which means it won’t change the state on chain, it won’t be executed(web3 ...
Common Patterns in Contract
Withdrawal from ContractThe recommended method of sending funds after an effect is using the withdrawal pattern.
Although the most intuitive method of sending Ether, as a result of an effect, is a direct send call, this is not recommended as it introduces a potential security risk.
This is an example of the withdrawal pattern in practice in a contract where the goal is to send the most money to the contract in order to become the “richest”.
In the following contract, if you are usurped as the ri ...
Solidity Style Guide
Layout4 spaces per indentation level
Use spaces for indentation
Use blank lines
1234567contract A { // ...}contract B { // ...}
Blank lines may be omitted between groups of related one-liners
1234contract A { function spam(); function ham();}
Use UTF-8 or ASCII encoding
Place import statements at the top of the file
Functions should be be grouped according to their visibility and ordered:
constructor
fallback function (if exists)
external
public
internal
...
Notes on Solidity in Depth
Layout of a Solidity Source FileVersion Pragma1pragma solidity ^0.4.11;
Importing other Source File1234567import "filename";import * as symbolName from "filename";import { symbol as alias, symbol2 } from "filename";import "filename" as symbolName;
In the above, filename is always treated as a path with / as directory seperator.
All path names are treated as absolute paths unless thay start with . or ...
RemappingFor example, remap github.com/et ...
Solidity CheatSheet
Global VariablesBlock
block.coinbase: address - current block miner’s address
block.difficulty: uint - current block difficulty
block.gaslimit: uint - current block gaslimit
block.number: uint - current block number
block.timestamp: uint - current block timestamp
Msg
msg.data: bytes - complete calldata
msg.gas: uint - remaining gas
msg.sender: address - sender of the message(current call)
msg.value: uint - number of wei sent with the message
Now
now: uint - current block timestamp(ali ...
HTTP/2 by Node.js
Original
Websites delivered using HTTP/2 enjoys a wide range of new features including -
fully multiplexed connections: all requests and responses to a domain are fully multiplexed via a single connection, making best use of available bandwidth.
header compression: repeated headers are compressed with HPACK compression so that they are not resent with every request and response.
PUSH: resources can be pre-emptively pushed by the server to the client, speeding up page load times.
Node.js jus ...
Rocket of Rust
IntroductionRocket’s design is centered around three core philosophies:
Function declaration and parameter type should contain all necessary information to validate and process a request. This immediately prohibits APIs where request state is retrieved from a global context. As a result, request handling is self-contained in Rocket: handlers are regular functions with regular arguments.
All request handling information should be typed. Because the web and HTTP are themselves untyped(or stringl ...
Fastify Plugin Guide
In Fastify everything is a plugin, your routes, your utilities and so on are all plugins. And to add a new plugin, whatever its functionality is, in Fastify
Registers1fastify.register(require('./my-plugin', opts, callback))
Register creates a new Fastify context, this means that if you do any change to the Fastify instance, that change will not be reflected into the context’s ancestors. In other words, encapsulated!
Required Plugins must expose a single function with the following sig ...
Time to Start Fastify
Getting StartedInstall1yarn add fastify
First Server123456789101112131415// Require the framework and instantiate itconst fastify = require('fastify')()// Declare a routefastify.get('/', (req, reply) => { reply.send({ hello: 'world', })})// Run the serverfastify.listen(3000, (err) => { if (err) throw err console.log(`Server is running on ${fastify.server.address().port}`)})
Schema Serialization123456789101112131 ...