simply working our way forward through the updates is likely the easiest way to describe the changes forthcoming on the SOILnet. ill deal with these changes one or two aspect per post, and try to make it as understandable as possible.
Go 1.4.1 (
Knoxjonesi) introduces
*
Event Subscriptions*, or RPC PUB SUB, wherein an active, connected node can subscribe to blockchain event notifications rather than manually requesting them directly. the node will create a subscription id and provide notifications via RPC as subscription events occur. these subscriptions can be cancelled thru calls or by closing the connection to the gsoil client. these subscriptions are coupled to your connection to the SOIL network, thus if the connection is terminated, all subscriptions are automatically closed
example: creating a subscription
Subscriptions are created via a regular RPC call with
eth_subscribe as method and the subscription name as the parameter. If successful it returns the subscription id (your active wallet address).
>> {"id": 1, "method": "eth_subscribe", "params": ["newBlocks", {}]}
<< {"jsonrpc":"2.0","id":1,"result":"0xcd0c3e8af590364c09d0fa6a1210faf5"}
produces subscribed results to the determined address (0xcd0c3e8af590364c09d0fa6a1210faf5) when
new blocks are set on the blockchain.
<< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0xcd0c3e8af590364c09d0fa6a1210faf5","result":{"difficulty":"0xd9263f42a87",<...>, "uncles":[]}}}
<< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0xcd0c3e8af590364c09d0fa6a1210faf5","result":{"difficulty":"0xd90b1a7ad02", <...>, "uncles":["0x80aacd1ea4c9da32efd8c2cc9ab38f8f70578fcd46a1a4ed73f82f3e0957f936"]}}}
and can be unsubscribed to by
>> {"id": 1, "method": "eth_unsubscribe", "params": ["0xcd0c3e8af590364c09d0fa6a1210faf5"]}
<< {"jsonrpc":"2.0","id":1,"result":true}
currently supported subscriptions are (ie: placed in stead on "newBlocks" in the above example):
newHeads - produces a notification with new block header information, including difficulty, gas limit, miner, nonce, timestamp, etc.
logs - returns logs that are included in new imported blocks and match the given filter criteria, ex: addresses (personal or contract) or arrays of addresses, or topics. only logs that are created from events matching these filters are returned
newPendingTransactions - returns the hash for all transactions that are added to the pending blockchain state
syncing - a boolean return to provide notices of block chain synchronization start/stops
future available subscriptions being worked upon beyond this implementation will also provide subscription notices for current events related to balance changes, account changes, nonce changes and storage changes in contracts.
*
Native Go DApp Development*, introduces a new tool called
abigen (located in cmd/abigen) which enables fully automated creation of Go Bindings to any contract built for deployment on the SOILnet. In order to ensure client implementation for applications in various programming languages that operate on the EVM, and allowing for a streamlined approach to mainstream adoption, RPC interfaces for JavaScript dApps have made it possible for most use cases where manual interaction with the blockchain is necessary. The server side RPC use cases, where other languages are used (i.e.: backend data-access layers written in NodeJs, Python, Ruby or Perl; database administration; security and authentication) these bindings have been elusive.
abigen allows for work on server side native dApps, ensuring that Go-language bindings to contracts on the SOILnet are safe at the time of compiling and deployment, and that they can be independently, automatically and completely generated from a contract ABI or EVM bytecode. Overall, abigen will make it exponentially easier to construct functioning contracts on the SOILnet that are interactive within the DUST brower for SOILsafe.
The available tutorial for ETH regarding abigen relates to using the "official Token contract" (token.sol) as the base for introducing the Go native bindings. This contract source code repo is available at:
https://gist.github.com/karalabe/08f4b780e01c8452d989 and is the ETH foundations representation as the common form of creating a token for oneself. (which can be done directly from within the DUST wallet for SOILsafe).
Interacting with a contract on the SOILcoin blockchain directly from the Go client (gsoil) is already possible via the RPC interfaces exposed by the client. Writing the boilerplate code that translates Go language constructs into RPC calls and back requires a lot of code to perform minimal functions, and as an aspect of application development... is an excessive time consumer and even small changes in Solidity can require a great deal of work to disposition the code to Go. With the introduction of abigen, the SOILcoin implementation of go-ethereum (go-soil) has a source code generator that will convert SOILcoin ABI definitions into ready-made Go packages. Assuming a valid Go development environment is set up, with
godep installed, and an up-to-date build of gsoil, you can build the
Go binding Generator with:
$ cd $GOPATH/src/github.com/SOILcoin/go-soil
$ godep go install ./cmd/abigen
GENERATING BINDINGS: The one prerequisite needed to generate a Go binding to an SOILcoin contract is that contract's ABI definition JSON file. For the example Token contract this is obtained by either compiling the Solidity code yourself (via an online solidity compiler such as at
https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js or
https://etherchain.org/solc), or by using a pre-compiled token.abi available at
https://gist.github.com/karalabe/b8dfdb6d301660f56c1b.
To generate a binding, call:
$ abigen --abi token.abi --pkg main --type Token --out token.go
The flags are:
--abi: (Mandatory) path to the contract ABI to bind to
--pgk: (Mandatory) Go package name to place the Go code into
--type: (Optional) Go type name to assign to the binding struct
--out: (Optional) output path for the generated Go source file (not set = stdout)
This will generate a type-safe Go binding for the Token contract. The generated code will look something like token.go as referrenced by the original tutorial for this update, (
https://gist.github.com/karalabe/5839509295afa4f7e2215bc4116c7a8f), but as the generator itself is upgraded, the structure of the Go bindings may change, and it is recommended to generate the bindings fresh when preparing to deploy this contract (token.sol) on the SOILcoin network.
The Go binding generator also produces ready-built RPC backends which can be attached to an active SOILcoin node via IPC, HTTP or WebSockets, and through which the user can access the SOILcoin network and further connect to, interact, and perform transactions with contracts deployed on the SOILnet. The generator also creates specialized wrappers which can be pre-configured with authorization parameters, allowing all the Solidity defined methods to be called by simple
session commands.
Deploying contracts onto the SOILnet using abigen requires the use of the contract bytecode alongside the contract ABI. This is retrieved via the previously mentioned online compilers, where the token.sol contract code can be compiled, generating the bytecode (similar to the presented example from the original tutorial, found at
https://gist.github.com/karalabe/026548f6a5f5f97b54de) saved as
token.bin$ abigen --abi token.abi --pkg main --type Token --out token.go --bin token.bin
This will generate something similar to the above referenced
token.go with the addition of a DeployToken function.
abigen also allows for Go binding directly from Solidity source code, which first compiles the source code (via --solc) into it's fundamental commands and functions and binds using that. This has the added function that all contracts contained within a given Solidity source file are built and bound, so if the file contains many contract sources, they will all be available from Go code.
$ abigen --sol token.sol --pkg main --out token.go
Much of the Go binding generation process can be automated during contract updating by leveraging the
go:generate binding into a Go source file before the package definition, after which whenever the Solidity contract is modified, instead of needing run the above command, a call go generate on the package will correctly generate the new bindings.
//go:generate abigen --sol token.sol --pkg main --out token.go
The final update that abigen presents is a very useful one, which i think may open up a great deal of possibilities for on-chain token economies going forward. A
simulated blockchain can be built as a backend to native contracts the same way as a live RPC backend could be, and which can be "mined" thru calling Commit. the adjusted code presented in the original tutorial for abigen follows:
package main
import (
"fmt"
"log"
"math/big"
"github.com/SOILcoin/go-soil/accounts/abi/bind"
"github.com/SOILcoin/go-soil/accounts/abi/bind/backends"
"github.com/SOILcoin/go-soil/core"
"github.com/SOILcoin/go-soil/crypto"
)
func main() {
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactor(key)
sim := backends.NewSimulatedBackend(core.GenesisAccount{Address: auth.From, Balance: big.NewInt(10000000000)})
// Deploy a token contract on the simulated blockchain
_, _, token, err := DeployMyToken(auth, sim, new(big.Int), "Simulated blockchain tokens", 0, "SBT")
if err != nil {
log.Fatalf("Failed to deploy new token contract: %v", err)
}
// Print the current (non existent) and pending name of the contract
name, _ := token.Name(nil)
fmt.Println("Pre-mining name:", name)
name, _ = token.Name(&bind.CallOpts{Pending: true})
fmt.Println("Pre-mining pending name:", name)
// Commit all pending transactions in the simulator and print the names again
sim.Commit()
name, _ = token.Name(nil)
fmt.Println("Post-mining name:", name)
name, _ = token.Name(&bind.CallOpts{Pending: true})
fmt.Println("Post-mining pending name:", name)
}
The original tutorial regarding abigen has a number of examples for usage, and when time permits, I'll write a tutorial showing how to use abigen via a gsoil development set-up, connecting to a similar sort of token on SOIL that the tutorial points towards (the Ethereum testnet Unicorn which mints 1 UNICORN to an address for every 2.something ETH sent to the contract address) and will add that information to this post when we set it up on the SOILcoin forum (which in and of itself still needs to be set up, just havent had the hours available to commit to that project yet.)