diff --git a/.circleci/config.yml b/.circleci/config.yml index 856a79507bea..bcac607de97e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,7 +7,7 @@ executors: golang: docker: - image: circleci/golang:1.13 - working_directory: /go/src/github.com/maticnetwork/bor + working_directory: /go/src/github.com/ethereum/go-ethereum jobs: build: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f73cf44c0f2..81d2ba5b1f2d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: - name: Install Go uses: actions/setup-go@v1 with: - go-version: 1.14.7 + go-version: 1.15.5 - name: "Build binaries" run: make all - name: "Run tests" diff --git a/.github/workflows/linuxpackage.yml b/.github/workflows/linuxpackage.yml index 639ac2c91e13..d03e30a3e2d2 100644 --- a/.github/workflows/linuxpackage.yml +++ b/.github/workflows/linuxpackage.yml @@ -13,7 +13,7 @@ jobs: - name: Install Go uses: actions/setup-go@v1 with: - go-version: 1.14.7 + go-version: 1.15.5 - name: Set up Ruby 2.6 uses: actions/setup-ruby@v1 diff --git a/.travis.yml b/.travis.yml index bcc4fa71899f..36673ffab370 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: go -go_import_path: github.com/maticnetwork/bor +go_import_path: github.com/ethereum/go-ethereum sudo: false jobs: allow_failures: @@ -150,7 +150,7 @@ jobs: - mv android-ndk-r19b $ANDROID_HOME/ndk-bundle - mkdir -p $GOPATH/src/github.com/ethereum - - ln -s `pwd` $GOPATH/src/github.com/maticnetwork/bor + - ln -s `pwd` $GOPATH/src/github.com/ethereum/go-ethereum - go run build/ci.go aar -signer ANDROID_SIGNING_KEY -deploy https://oss.sonatype.org -upload gethstore/builds # This builder does the OSX Azure, iOS CocoaPods and iOS Azure uploads diff --git a/Dockerfile b/Dockerfile index d86b77661186..9119dfbc50b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,14 +3,13 @@ FROM golang:1.15-alpine as builder RUN apk add --no-cache make gcc musl-dev linux-headers git -ADD . /go-ethereum -RUN cd /go-ethereum && make geth +ADD . /bor +RUN cd /bor && make bor -# Pull Geth into a second stage deploy alpine container +# Pull Bor into a second stage deploy alpine container FROM alpine:latest RUN apk add --no-cache ca-certificates -COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/ +COPY --from=builder /bor/build/bin/bor /usr/local/bin/ -EXPOSE 8545 8546 30303 30303/udp -ENTRYPOINT ["geth"] +EXPOSE 8545 8546 8547 30303 30303/udp diff --git a/Dockerfile.alltools b/Dockerfile.alltools index 715213c5de0d..9c55918d2cca 100644 --- a/Dockerfile.alltools +++ b/Dockerfile.alltools @@ -3,13 +3,13 @@ FROM golang:1.15-alpine as builder RUN apk add --no-cache make gcc musl-dev linux-headers git -ADD . /go-ethereum -RUN cd /go-ethereum && make all +ADD . /bor +RUN cd /bor && make bor-all # Pull all binaries into a second stage deploy alpine container FROM alpine:latest RUN apk add --no-cache ca-certificates -COPY --from=builder /go-ethereum/build/bin/* /usr/local/bin/ +COPY --from=builder /bor/build/bin/* /usr/local/bin/ EXPOSE 8545 8546 30303 30303/udp diff --git a/Makefile b/Makefile index ecee5f189435..48e7f345282f 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,19 @@ GOBIN = ./build/bin GO ?= latest GORUN = env GO111MODULE=on go run +GOPATH = $(shell go env GOPATH) + +bor: + $(GORUN) build/ci.go install ./cmd/geth + mkdir -p $(GOPATH)/bin/ + cp $(GOBIN)/geth $(GOBIN)/bor + cp $(GOBIN)/* $(GOPATH)/bin/ + +bor-all: + $(GORUN) build/ci.go install + mkdir -p $(GOPATH)/bin/ + cp $(GOBIN)/geth $(GOBIN)/bor + cp $(GOBIN)/* $(GOPATH)/bin/ geth: $(GORUN) build/ci.go install ./cmd/geth diff --git a/README.md b/README.md index ddb885dfdc36..b7dfece8a503 100644 --- a/README.md +++ b/README.md @@ -1,353 +1,22 @@ -## Go Ethereum +## Bor -Official Golang implementation of the Ethereum protocol. - -[![API Reference]( -https://camo.githubusercontent.com/915b7be44ada53c290eb157634330494ebe3e30a/68747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f676f6c616e672f6764646f3f7374617475732e737667 -)](https://pkg.go.dev/github.com/ethereum/go-ethereum?tab=doc) -[![Go Report Card](https://goreportcard.com/badge/github.com/ethereum/go-ethereum)](https://goreportcard.com/report/github.com/ethereum/go-ethereum) -[![Travis](https://travis-ci.org/ethereum/go-ethereum.svg?branch=master)](https://travis-ci.org/ethereum/go-ethereum) -[![Discord](https://img.shields.io/badge/discord-join%20chat-blue.svg)](https://discord.gg/nthXNEv) - -Automated builds are available for stable releases and the unstable master branch. Binary -archives are published at https://geth.ethereum.org/downloads/. +Official Golang implementation of the Matic protocol (fork of Go Ethereum - https://github.com/ethereum/go-ethereum) ## Building the source -For prerequisites and detailed build instructions please read the [Installation Instructions](https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum) on the wiki. - -Building `geth` requires both a Go (version 1.13 or later) and a C compiler. You can install +Building `bor` requires both a Go (version 1.13 or later) and a C compiler. You can install them using your favourite package manager. Once the dependencies are installed, run ```shell -make geth +make bor ``` or, to build the full suite of utilities: ```shell -make all -``` - -## Executables - -The go-ethereum project comes with several wrappers/executables found in the `cmd` -directory. - -| Command | Description | -| :-----------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **`geth`** | Our main Ethereum CLI client. It is the entry point into the Ethereum network (main-, test- or private net), capable of running as a full node (default), archive node (retaining all historical state) or a light node (retrieving data live). It can be used by other processes as a gateway into the Ethereum network via JSON RPC endpoints exposed on top of HTTP, WebSocket and/or IPC transports. `geth --help` and the [CLI Wiki page](https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options) for command line options. | -| `abigen` | Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages. It operates on plain [Ethereum contract ABIs](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI) with expanded functionality if the contract bytecode is also available. However, it also accepts Solidity source files, making development much more streamlined. Please see our [Native DApps](https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts) wiki page for details. | -| `bootnode` | Stripped down version of our Ethereum client implementation that only takes part in the network node discovery protocol, but does not run any of the higher level application protocols. It can be used as a lightweight bootstrap node to aid in finding peers in private networks. | -| `evm` | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow isolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug run`). | -| `gethrpctest` | Developer utility tool to support our [ethereum/rpc-test](https://github.com/ethereum/rpc-tests) test suite which validates baseline conformity to the [Ethereum JSON RPC](https://github.com/ethereum/wiki/wiki/JSON-RPC) specs. Please see the [test suite's readme](https://github.com/ethereum/rpc-tests/blob/master/README.md) for details. | -| `rlpdump` | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://github.com/ethereum/wiki/wiki/RLP)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user-friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). | -| `puppeth` | a CLI wizard that aids in creating a new Ethereum network. | - -## Running `geth` - -Going through all the possible command line flags is out of scope here (please consult our -[CLI Wiki page](https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options)), -but we've enumerated a few common parameter combos to get you up to speed quickly -on how you can run your own `geth` instance. - -### Full node on the main Ethereum network - -By far the most common scenario is people wanting to simply interact with the Ethereum -network: create accounts; transfer funds; deploy and interact with contracts. For this -particular use-case the user doesn't care about years-old historical data, so we can -fast-sync quickly to the current state of the network. To do so: - -```shell -$ geth console -``` - -This command will: - * Start `geth` in fast sync mode (default, can be changed with the `--syncmode` flag), - causing it to download more data in exchange for avoiding processing the entire history - of the Ethereum network, which is very CPU intensive. - * Start up `geth`'s built-in interactive [JavaScript console](https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console), - (via the trailing `console` subcommand) through which you can invoke all official [`web3` methods](https://github.com/ethereum/wiki/wiki/JavaScript-API) - as well as `geth`'s own [management APIs](https://github.com/ethereum/go-ethereum/wiki/Management-APIs). - This tool is optional and if you leave it out you can always attach to an already running - `geth` instance with `geth attach`. - -### A Full node on the Görli test network - -Transitioning towards developers, if you'd like to play around with creating Ethereum -contracts, you almost certainly would like to do that without any real money involved until -you get the hang of the entire system. In other words, instead of attaching to the main -network, you want to join the **test** network with your node, which is fully equivalent to -the main network, but with play-Ether only. - -```shell -$ geth --goerli console -``` - -The `console` subcommand has the exact same meaning as above and they are equally -useful on the testnet too. Please, see above for their explanations if you've skipped here. - -Specifying the `--goerli` flag, however, will reconfigure your `geth` instance a bit: - - * Instead of connecting the main Ethereum network, the client will connect to the Görli - test network, which uses different P2P bootnodes, different network IDs and genesis - states. - * Instead of using the default data directory (`~/.ethereum` on Linux for example), `geth` - will nest itself one level deeper into a `goerli` subfolder (`~/.ethereum/goerli` on - Linux). Note, on OSX and Linux this also means that attaching to a running testnet node - requires the use of a custom endpoint since `geth attach` will try to attach to a - production node endpoint by default, e.g., - `geth attach /goerli/geth.ipc`. Windows users are not affected by - this. - -*Note: Although there are some internal protective measures to prevent transactions from -crossing over between the main network and test network, you should make sure to always -use separate accounts for play-money and real-money. Unless you manually move -accounts, `geth` will by default correctly separate the two networks and will not make any -accounts available between them.* - -### Full node on the Rinkeby test network - -Go Ethereum also supports connecting to the older proof-of-authority based test network -called [*Rinkeby*](https://www.rinkeby.io) which is operated by members of the community. - -```shell -$ geth --rinkeby console -``` - -### Full node on the Ropsten test network - -In addition to Görli and Rinkeby, Geth also supports the ancient Ropsten testnet. The -Ropsten test network is based on the Ethash proof-of-work consensus algorithm. As such, -it has certain extra overhead and is more susceptible to reorganization attacks due to the -network's low difficulty/security. - -```shell -$ geth --ropsten console -``` - -*Note: Older Geth configurations store the Ropsten database in the `testnet` subdirectory.* - -### Configuration - -As an alternative to passing the numerous flags to the `geth` binary, you can also pass a -configuration file via: - -```shell -$ geth --config /path/to/your_config.toml -``` - -To get an idea how the file should look like you can use the `dumpconfig` subcommand to -export your existing configuration: - -```shell -$ geth --your-favourite-flags dumpconfig -``` - -*Note: This works only with `geth` v1.6.0 and above.* - -#### Docker quick start - -One of the quickest ways to get Ethereum up and running on your machine is by using -Docker: - -```shell -docker run -d --name ethereum-node -v /Users/alice/ethereum:/root \ - -p 8545:8545 -p 30303:30303 \ - ethereum/client-go +make bor-all ``` -This will start `geth` in fast-sync mode with a DB memory allowance of 1GB just as the -above command does. It will also create a persistent volume in your home directory for -saving your blockchain as well as map the default ports. There is also an `alpine` tag -available for a slim version of the image. - -Do not forget `--http.addr 0.0.0.0`, if you want to access RPC from other containers -and/or hosts. By default, `geth` binds to the local interface and RPC endpoints is not -accessible from the outside. - -### Programmatically interfacing `geth` nodes - -As a developer, sooner rather than later you'll want to start interacting with `geth` and the -Ethereum network via your own programs and not manually through the console. To aid -this, `geth` has built-in support for a JSON-RPC based APIs ([standard APIs](https://github.com/ethereum/wiki/wiki/JSON-RPC) -and [`geth` specific APIs](https://github.com/ethereum/go-ethereum/wiki/Management-APIs)). -These can be exposed via HTTP, WebSockets and IPC (UNIX sockets on UNIX based -platforms, and named pipes on Windows). - -The IPC interface is enabled by default and exposes all the APIs supported by `geth`, -whereas the HTTP and WS interfaces need to manually be enabled and only expose a -subset of APIs due to security reasons. These can be turned on/off and configured as -you'd expect. - -HTTP based JSON-RPC API options: - - * `--http` Enable the HTTP-RPC server - * `--http.addr` HTTP-RPC server listening interface (default: `localhost`) - * `--http.port` HTTP-RPC server listening port (default: `8545`) - * `--http.api` API's offered over the HTTP-RPC interface (default: `eth,net,web3`) - * `--http.corsdomain` Comma separated list of domains from which to accept cross origin requests (browser enforced) - * `--ws` Enable the WS-RPC server - * `--ws.addr` WS-RPC server listening interface (default: `localhost`) - * `--ws.port` WS-RPC server listening port (default: `8546`) - * `--ws.api` API's offered over the WS-RPC interface (default: `eth,net,web3`) - * `--ws.origins` Origins from which to accept websockets requests - * `--ipcdisable` Disable the IPC-RPC server - * `--ipcapi` API's offered over the IPC-RPC interface (default: `admin,debug,eth,miner,net,personal,shh,txpool,web3`) - * `--ipcpath` Filename for IPC socket/pipe within the datadir (explicit paths escape it) - -You'll need to use your own programming environments' capabilities (libraries, tools, etc) to -connect via HTTP, WS or IPC to a `geth` node configured with the above flags and you'll -need to speak [JSON-RPC](https://www.jsonrpc.org/specification) on all transports. You -can reuse the same connection for multiple requests! - -**Note: Please understand the security implications of opening up an HTTP/WS based -transport before doing so! Hackers on the internet are actively trying to subvert -Ethereum nodes with exposed APIs! Further, all browser tabs can access locally -running web servers, so malicious web pages could try to subvert locally available -APIs!** - -### Operating a private network - -Maintaining your own private network is more involved as a lot of configurations taken for -granted in the official networks need to be manually set up. - -#### Defining the private genesis state - -First, you'll need to create the genesis state of your networks, which all nodes need to be -aware of and agree upon. This consists of a small JSON file (e.g. call it `genesis.json`): - -```json -{ - "config": { - "chainId": , - "homesteadBlock": 0, - "eip150Block": 0, - "eip155Block": 0, - "eip158Block": 0, - "byzantiumBlock": 0, - "constantinopleBlock": 0, - "petersburgBlock": 0, - "istanbulBlock": 0 - }, - "alloc": {}, - "coinbase": "0x0000000000000000000000000000000000000000", - "difficulty": "0x20000", - "extraData": "", - "gasLimit": "0x2fefd8", - "nonce": "0x0000000000000042", - "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "timestamp": "0x00" -} -``` - -The above fields should be fine for most purposes, although we'd recommend changing -the `nonce` to some random value so you prevent unknown remote nodes from being able -to connect to you. If you'd like to pre-fund some accounts for easier testing, create -the accounts and populate the `alloc` field with their addresses. - -```json -"alloc": { - "0x0000000000000000000000000000000000000001": { - "balance": "111111111" - }, - "0x0000000000000000000000000000000000000002": { - "balance": "222222222" - } -} -``` - -With the genesis state defined in the above JSON file, you'll need to initialize **every** -`geth` node with it prior to starting it up to ensure all blockchain parameters are correctly -set: - -```shell -$ geth init path/to/genesis.json -``` - -#### Creating the rendezvous point - -With all nodes that you want to run initialized to the desired genesis state, you'll need to -start a bootstrap node that others can use to find each other in your network and/or over -the internet. The clean way is to configure and run a dedicated bootnode: - -```shell -$ bootnode --genkey=boot.key -$ bootnode --nodekey=boot.key -``` - -With the bootnode online, it will display an [`enode` URL](https://github.com/ethereum/wiki/wiki/enode-url-format) -that other nodes can use to connect to it and exchange peer information. Make sure to -replace the displayed IP address information (most probably `[::]`) with your externally -accessible IP to get the actual `enode` URL. - -*Note: You could also use a full-fledged `geth` node as a bootnode, but it's the less -recommended way.* - -#### Starting up your member nodes - -With the bootnode operational and externally reachable (you can try -`telnet ` to ensure it's indeed reachable), start every subsequent `geth` -node pointed to the bootnode for peer discovery via the `--bootnodes` flag. It will -probably also be desirable to keep the data directory of your private network separated, so -do also specify a custom `--datadir` flag. - -```shell -$ geth --datadir=path/to/custom/data/folder --bootnodes= -``` - -*Note: Since your network will be completely cut off from the main and test networks, you'll -also need to configure a miner to process transactions and create new blocks for you.* - -#### Running a private miner - -Mining on the public Ethereum network is a complex task as it's only feasible using GPUs, -requiring an OpenCL or CUDA enabled `ethminer` instance. For information on such a -setup, please consult the [EtherMining subreddit](https://www.reddit.com/r/EtherMining/) -and the [ethminer](https://github.com/ethereum-mining/ethminer) repository. - -In a private network setting, however a single CPU miner instance is more than enough for -practical purposes as it can produce a stable stream of blocks at the correct intervals -without needing heavy resources (consider running on a single thread, no need for multiple -ones either). To start a `geth` instance for mining, run it with all your usual flags, extended -by: - -```shell -$ geth --mine --miner.threads=1 --etherbase=0x0000000000000000000000000000000000000000 -``` - -Which will start mining blocks and transactions on a single CPU thread, crediting all -proceedings to the account specified by `--etherbase`. You can further tune the mining -by changing the default gas limit blocks converge to (`--targetgaslimit`) and the price -transactions are accepted at (`--gasprice`). - -## Contribution - -Thank you for considering to help out with the source code! We welcome contributions -from anyone on the internet, and are grateful for even the smallest of fixes! - -If you'd like to contribute to go-ethereum, please fork, fix, commit and send a pull request -for the maintainers to review and merge into the main code base. If you wish to submit -more complex changes though, please check up with the core devs first on [our gitter channel](https://gitter.im/ethereum/go-ethereum) -to ensure those changes are in line with the general philosophy of the project and/or get -some early feedback which can make both your efforts much lighter as well as our review -and merge procedures quick and simple. - -Please make sure your contributions adhere to our coding guidelines: - - * Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) - guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)). - * Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) - guidelines. - * Pull requests need to be based on and opened against the `master` branch. - * Commit messages should be prefixed with the package(s) they modify. - * E.g. "eth, rpc: make trace configs optional" - -Please see the [Developers' Guide](https://github.com/ethereum/go-ethereum/wiki/Developers'-Guide) -for more details on configuring your environment, managing project dependencies, and -testing procedures. - ## License The go-ethereum library (i.e. all code outside of the `cmd` directory) is licensed under the diff --git a/accounts/accounts.go b/accounts/accounts.go index dc85cba174b6..237ca44b1c12 100644 --- a/accounts/accounts.go +++ b/accounts/accounts.go @@ -39,6 +39,7 @@ const ( MimetypeDataWithValidator = "data/validator" MimetypeTypedData = "data/typed" MimetypeClique = "application/x-clique-header" + MimetypeBor = "application/x-bor-header" MimetypeTextPlain = "text/plain" ) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 6418f909579a..0e0835cb7260 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -74,7 +74,7 @@ The dumpgenesis command dumps the genesis block configuration in JSON format to Action: utils.MigrateFlags(importChain), Name: "import", Usage: "Import a blockchain file", - ArgsUsage: " ( ... ) ", + ArgsUsage: " ( ... ) ", Flags: []cli.Flag{ utils.DataDirFlag, utils.CacheFlag, @@ -94,6 +94,10 @@ The dumpgenesis command dumps the genesis block configuration in JSON format to utils.MetricsInfluxDBPasswordFlag, utils.MetricsInfluxDBTagsFlag, utils.TxLookupLimitFlag, + + // bor related flags + utils.HeimdallURLFlag, + utils.WithoutHeimdallFlag, }, Category: "BLOCKCHAIN COMMANDS", Description: ` @@ -270,7 +274,7 @@ func dumpGenesis(ctx *cli.Context) error { } func importChain(ctx *cli.Context) error { - if len(ctx.Args()) < 1 { + if len(ctx.Args()) < 2 { utils.Fatalf("This command requires an argument.") } // Start metrics export if enabled @@ -304,13 +308,14 @@ func importChain(ctx *cli.Context) error { var importErr error - if len(ctx.Args()) == 1 { + // ArgsUsage: " ( ... ) ", + if len(ctx.Args()) == 2 { if err := utils.ImportChain(chain, ctx.Args().First()); err != nil { importErr = err log.Error("Import error", "err", err) } } else { - for _, arg := range ctx.Args() { + for _, arg := range ctx.Args()[:len(ctx.Args())-1] { if err := utils.ImportChain(chain, arg); err != nil { importErr = err log.Error("Import error", "file", arg, "err", err) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 6b51843aa456..25a04254291b 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -111,7 +111,7 @@ func defaultNodeConfig() node.Config { cfg.Version = params.VersionWithCommit(gitCommit, gitDate) cfg.HTTPModules = append(cfg.HTTPModules, "eth") cfg.WSModules = append(cfg.WSModules, "eth") - cfg.IPCPath = "geth.ipc" + cfg.IPCPath = clientIdentifier + ".ipc" return cfg } @@ -146,6 +146,9 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) { } utils.SetShhConfig(ctx, stack) + // Set Bor config flags + utils.SetBorConfig(ctx, &cfg.Eth) + return stack, cfg } diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 38e48534dcb1..26664b630ddb 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -46,7 +46,8 @@ import ( ) const ( - clientIdentifier = "geth" // Client identifier to advertise over the network + clientIdentifier = "bor" // Client identifier to advertise over the network + repositoryIdentifier = "go-bor" ) var ( @@ -54,7 +55,7 @@ var ( gitCommit = "" gitDate = "" // The app that holds all commands and flags. - app = flags.NewApp(gitCommit, gitDate, "the go-ethereum command line interface") + app = flags.NewApp(gitCommit, gitDate, fmt.Sprintf("the %s command line interface", repositoryIdentifier)) // flags that configure the node nodeFlags = []cli.Flag{ utils.IdentityFlag, @@ -259,6 +260,9 @@ func init() { app.Flags = append(app.Flags, whisperFlags...) app.Flags = append(app.Flags, metricsFlags...) + // add bor flags + app.Flags = append(app.Flags, utils.BorFlags...) + app.Before = func(ctx *cli.Context) error { return debug.Setup(ctx) } diff --git a/cmd/utils/bor_flags.go b/cmd/utils/bor_flags.go new file mode 100644 index 000000000000..22e1b33d3e64 --- /dev/null +++ b/cmd/utils/bor_flags.go @@ -0,0 +1,88 @@ +package utils + +import ( + "encoding/json" + "io/ioutil" + "os" + + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/node" + "gopkg.in/urfave/cli.v1" +) + +var ( + // + // Bor Specific flags + // + + // HeimdallURLFlag flag for heimdall url + HeimdallURLFlag = cli.StringFlag{ + Name: "bor.heimdall", + Usage: "URL of Heimdall service", + Value: "http://localhost:1317", + } + + // WithoutHeimdallFlag no heimdall (for testing purpose) + WithoutHeimdallFlag = cli.BoolFlag{ + Name: "bor.withoutheimdall", + Usage: "Run without Heimdall service (for testing purpose)", + } + + // BorFlags all bor related flags + BorFlags = []cli.Flag{ + HeimdallURLFlag, + WithoutHeimdallFlag, + } +) + +func getGenesis(genesisPath string) (*core.Genesis, error) { + log.Info("Reading genesis at ", "file", genesisPath) + file, err := os.Open(genesisPath) + if err != nil { + return nil, err + } + defer file.Close() + + genesis := new(core.Genesis) + if err := json.NewDecoder(file).Decode(genesis); err != nil { + return nil, err + } + return genesis, nil +} + +// SetBorConfig sets bor config +func SetBorConfig(ctx *cli.Context, cfg *eth.Config) { + cfg.HeimdallURL = ctx.GlobalString(HeimdallURLFlag.Name) + cfg.WithoutHeimdall = ctx.GlobalBool(WithoutHeimdallFlag.Name) +} + +// CreateBorEthereum Creates bor ethereum object from eth.Config +func CreateBorEthereum(cfg *eth.Config) *eth.Ethereum { + workspace, err := ioutil.TempDir("", "bor-command-node-") + if err != nil { + Fatalf("Failed to create temporary keystore: %v", err) + } + + // Create a networkless protocol stack and start an Ethereum service within + stack, err := node.New(&node.Config{DataDir: workspace, UseLightweightKDF: true, Name: "bor-command-node"}) + if err != nil { + Fatalf("Failed to create node: %v", err) + } + ethereum, err := eth.New(stack, cfg) + if err != nil { + Fatalf("Failed to register Ethereum protocol: %v", err) + } + + // Start the node and assemble the JavaScript console around it + if err = stack.Start(); err != nil { + Fatalf("Failed to start stack: %v", err) + } + _, err = stack.Attach() + if err != nil { + Fatalf("Failed to attach to node: %v", err) + } + + return ethereum +} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index e5ccfd7435d4..f9e6fb16e479 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1801,15 +1801,28 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis { // MakeChain creates a chain manager from set command line flags. func MakeChain(ctx *cli.Context, stack *node.Node, readOnly bool) (chain *core.BlockChain, chainDb ethdb.Database) { - var err error + // expecting the last argument to be the genesis file + genesis, err := getGenesis(ctx.Args().Get(len(ctx.Args()) - 1)) + if err != nil { + Fatalf("Valid genesis file is required as argument: {}", err) + } + chainDb = MakeChainDatabase(ctx, stack) - config, _, err := core.SetupGenesisBlock(chainDb, MakeGenesis(ctx)) + config, _, err := core.SetupGenesisBlock(chainDb, genesis) if err != nil { Fatalf("%v", err) } var engine consensus.Engine + var ethereum *eth.Ethereum if config.Clique != nil { engine = clique.New(config.Clique, chainDb) + } else if config.Bor != nil { + ethereum = CreateBorEthereum(ð.Config{ + Genesis: genesis, + HeimdallURL: ctx.GlobalString(HeimdallURLFlag.Name), + WithoutHeimdall: ctx.GlobalBool(WithoutHeimdallFlag.Name), + }) + engine = ethereum.Engine() } else { engine = ethash.NewFaker() if !ctx.GlobalBool(FakePoWFlag.Name) { @@ -1855,6 +1868,9 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readOnly bool) (chain *core.B if err != nil { Fatalf("Can't create BlockChain: %v", err) } + if ethereum != nil { + ethereum.SetBlockchain(chain) + } return chain, chainDb } diff --git a/consensus/bor/api.go b/consensus/bor/api.go new file mode 100644 index 000000000000..12841290afaf --- /dev/null +++ b/consensus/bor/api.go @@ -0,0 +1,192 @@ +package bor + +import ( + "encoding/hex" + "math" + "math/big" + "strconv" + "sync" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/rpc" + lru "github.com/hashicorp/golang-lru" + "github.com/xsleonard/go-merkle" + "golang.org/x/crypto/sha3" +) + +var ( + // MaxCheckpointLength is the maximum number of blocks that can be requested for constructing a checkpoint root hash + MaxCheckpointLength = uint64(math.Pow(2, 15)) +) + +// API is a user facing RPC API to allow controlling the signer and voting +// mechanisms of the proof-of-authority scheme. +type API struct { + chain consensus.ChainHeaderReader + bor *Bor + rootHashCache *lru.ARCCache +} + +// GetSnapshot retrieves the state snapshot at a given block. +func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) { + // Retrieve the requested block number (or current if none requested) + var header *types.Header + if number == nil || *number == rpc.LatestBlockNumber { + header = api.chain.CurrentHeader() + } else { + header = api.chain.GetHeaderByNumber(uint64(number.Int64())) + } + // Ensure we have an actually valid block and return its snapshot + if header == nil { + return nil, errUnknownBlock + } + return api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) +} + +// GetAuthor retrieves the author a block. +func (api *API) GetAuthor(number *rpc.BlockNumber) (*common.Address, error) { + // Retrieve the requested block number (or current if none requested) + var header *types.Header + if number == nil || *number == rpc.LatestBlockNumber { + header = api.chain.CurrentHeader() + } else { + header = api.chain.GetHeaderByNumber(uint64(number.Int64())) + } + // Ensure we have an actually valid block and return its snapshot + if header == nil { + return nil, errUnknownBlock + } + author, err := api.bor.Author(header) + return &author, err +} + +// GetSnapshotAtHash retrieves the state snapshot at a given block. +func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) { + header := api.chain.GetHeaderByHash(hash) + if header == nil { + return nil, errUnknownBlock + } + return api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) +} + +// GetSigners retrieves the list of authorized signers at the specified block. +func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) { + // Retrieve the requested block number (or current if none requested) + var header *types.Header + if number == nil || *number == rpc.LatestBlockNumber { + header = api.chain.CurrentHeader() + } else { + header = api.chain.GetHeaderByNumber(uint64(number.Int64())) + } + // Ensure we have an actually valid block and return the signers from its snapshot + if header == nil { + return nil, errUnknownBlock + } + snap, err := api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) + if err != nil { + return nil, err + } + return snap.signers(), nil +} + +// GetSignersAtHash retrieves the list of authorized signers at the specified block. +func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) { + header := api.chain.GetHeaderByHash(hash) + if header == nil { + return nil, errUnknownBlock + } + snap, err := api.bor.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) + if err != nil { + return nil, err + } + return snap.signers(), nil +} + +// GetCurrentProposer gets the current proposer +func (api *API) GetCurrentProposer() (common.Address, error) { + snap, err := api.GetSnapshot(nil) + if err != nil { + return common.Address{}, err + } + return snap.ValidatorSet.GetProposer().Address, nil +} + +// GetCurrentValidators gets the current validators +func (api *API) GetCurrentValidators() ([]*Validator, error) { + snap, err := api.GetSnapshot(nil) + if err != nil { + return make([]*Validator, 0), err + } + return snap.ValidatorSet.Validators, nil +} + +// GetRootHash returns the merkle root of the start to end block headers +func (api *API) GetRootHash(start uint64, end uint64) (string, error) { + if err := api.initializeRootHashCache(); err != nil { + return "", err + } + key := getRootHashKey(start, end) + if root, known := api.rootHashCache.Get(key); known { + return root.(string), nil + } + length := uint64(end - start + 1) + if length > MaxCheckpointLength { + return "", &MaxCheckpointLengthExceededError{start, end} + } + currentHeaderNumber := api.chain.CurrentHeader().Number.Uint64() + if start > end || end > currentHeaderNumber { + return "", &InvalidStartEndBlockError{start, end, currentHeaderNumber} + } + blockHeaders := make([]*types.Header, end-start+1) + wg := new(sync.WaitGroup) + concurrent := make(chan bool, 20) + for i := start; i <= end; i++ { + wg.Add(1) + concurrent <- true + go func(number uint64) { + blockHeaders[number-start] = api.chain.GetHeaderByNumber(uint64(number)) + <-concurrent + wg.Done() + }(i) + } + wg.Wait() + close(concurrent) + + headers := make([][32]byte, nextPowerOfTwo(length)) + for i := 0; i < len(blockHeaders); i++ { + blockHeader := blockHeaders[i] + header := crypto.Keccak256(appendBytes32( + blockHeader.Number.Bytes(), + new(big.Int).SetUint64(blockHeader.Time).Bytes(), + blockHeader.TxHash.Bytes(), + blockHeader.ReceiptHash.Bytes(), + )) + + var arr [32]byte + copy(arr[:], header) + headers[i] = arr + } + + tree := merkle.NewTreeWithOpts(merkle.TreeOptions{EnableHashSorting: false, DisableHashLeaves: true}) + if err := tree.Generate(convert(headers), sha3.NewLegacyKeccak256()); err != nil { + return "", err + } + root := hex.EncodeToString(tree.Root().Hash) + api.rootHashCache.Add(key, root) + return root, nil +} + +func (api *API) initializeRootHashCache() error { + var err error + if api.rootHashCache == nil { + api.rootHashCache, err = lru.NewARC(10) + } + return err +} + +func getRootHashKey(start uint64, end uint64) string { + return strconv.FormatUint(start, 10) + "-" + strconv.FormatUint(end, 10) +} diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go new file mode 100644 index 000000000000..6210a6207e3c --- /dev/null +++ b/consensus/bor/bor.go @@ -0,0 +1,1305 @@ +package bor + +import ( + "bytes" + "context" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "math" + "math/big" + "sort" + "strings" + "sync" + "time" + + lru "github.com/hashicorp/golang-lru" + "golang.org/x/crypto/sha3" + + ethereum "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/consensus/misc" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/trie" +) + +const ( + checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database + inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory + inmemorySignatures = 4096 // Number of recent block signatures to keep in memory +) + +// Bor protocol constants. +var ( + defaultSprintLength = uint64(64) // Default number of blocks after which to checkpoint and reset the pending votes + + extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity + extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal + + uncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. + + diffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures + diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures + + validatorHeaderBytesLength = common.AddressLength + 20 // address + power + systemAddress = common.HexToAddress("0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE") +) + +// Various error messages to mark blocks invalid. These should be private to +// prevent engine specific errors from being referenced in the remainder of the +// codebase, inherently breaking if the engine is swapped out. Please put common +// error types into the consensus package. +var ( + // errUnknownBlock is returned when the list of signers is requested for a block + // that is not part of the local blockchain. + errUnknownBlock = errors.New("unknown block") + + // errInvalidCheckpointBeneficiary is returned if a checkpoint/epoch transition + // block has a beneficiary set to non-zeroes. + errInvalidCheckpointBeneficiary = errors.New("beneficiary in checkpoint block non-zero") + + // errInvalidVote is returned if a nonce value is something else that the two + // allowed constants of 0x00..0 or 0xff..f. + errInvalidVote = errors.New("vote nonce not 0x00..0 or 0xff..f") + + // errInvalidCheckpointVote is returned if a checkpoint/epoch transition block + // has a vote nonce set to non-zeroes. + errInvalidCheckpointVote = errors.New("vote nonce in checkpoint block non-zero") + + // errMissingVanity is returned if a block's extra-data section is shorter than + // 32 bytes, which is required to store the signer vanity. + errMissingVanity = errors.New("extra-data 32 byte vanity prefix missing") + + // errMissingSignature is returned if a block's extra-data section doesn't seem + // to contain a 65 byte secp256k1 signature. + errMissingSignature = errors.New("extra-data 65 byte signature suffix missing") + + // errExtraValidators is returned if non-sprint-end block contain validator data in + // their extra-data fields. + errExtraValidators = errors.New("non-sprint-end block contains extra validator list") + + // errInvalidSpanValidators is returned if a block contains an + // invalid list of validators (i.e. non divisible by 40 bytes). + errInvalidSpanValidators = errors.New("invalid validator list on sprint end block") + + // errInvalidMixDigest is returned if a block's mix digest is non-zero. + errInvalidMixDigest = errors.New("non-zero mix digest") + + // errInvalidUncleHash is returned if a block contains an non-empty uncle list. + errInvalidUncleHash = errors.New("non empty uncle hash") + + // errInvalidDifficulty is returned if the difficulty of a block neither 1 or 2. + errInvalidDifficulty = errors.New("invalid difficulty") + + // ErrInvalidTimestamp is returned if the timestamp of a block is lower than + // the previous block's timestamp + the minimum block period. + ErrInvalidTimestamp = errors.New("invalid timestamp") + + // errOutOfRangeChain is returned if an authorization list is attempted to + // be modified via out-of-range or non-contiguous headers. + errOutOfRangeChain = errors.New("out of range or non-contiguous chain") +) + +// SignerFn is a signer callback function to request a header to be signed by a +// backing account. +type SignerFn func(accounts.Account, string, []byte) ([]byte, error) + +// ecrecover extracts the Ethereum account address from a signed header. +func ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, error) { + // If the signature's already cached, return that + hash := header.Hash() + if address, known := sigcache.Get(hash); known { + return address.(common.Address), nil + } + // Retrieve the signature from the header extra-data + if len(header.Extra) < extraSeal { + return common.Address{}, errMissingSignature + } + signature := header.Extra[len(header.Extra)-extraSeal:] + + // Recover the public key and the Ethereum address + pubkey, err := crypto.Ecrecover(SealHash(header).Bytes(), signature) + if err != nil { + return common.Address{}, err + } + var signer common.Address + copy(signer[:], crypto.Keccak256(pubkey[1:])[12:]) + + sigcache.Add(hash, signer) + return signer, nil +} + +// SealHash returns the hash of a block prior to it being sealed. +func SealHash(header *types.Header) (hash common.Hash) { + hasher := sha3.NewLegacyKeccak256() + encodeSigHeader(hasher, header) + hasher.Sum(hash[:0]) + return hash +} + +func encodeSigHeader(w io.Writer, header *types.Header) { + err := rlp.Encode(w, []interface{}{ + header.ParentHash, + header.UncleHash, + header.Coinbase, + header.Root, + header.TxHash, + header.ReceiptHash, + header.Bloom, + header.Difficulty, + header.Number, + header.GasLimit, + header.GasUsed, + header.Time, + header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short + header.MixDigest, + header.Nonce, + }) + if err != nil { + panic("can't encode: " + err.Error()) + } +} + +// CalcProducerDelay is the block delay algorithm based on block time, period, producerDelay and turn-ness of a signer +func CalcProducerDelay(number uint64, succession int, c *params.BorConfig) uint64 { + // When the block is the first block of the sprint, it is expected to be delayed by `producerDelay`. + // That is to allow time for block propagation in the last sprint + delay := c.Period + if number%c.Sprint == 0 { + delay = c.ProducerDelay + } + if succession > 0 { + delay += uint64(succession) * c.BackupMultiplier + } + return delay +} + +// BorRLP returns the rlp bytes which needs to be signed for the bor +// sealing. The RLP to sign consists of the entire header apart from the 65 byte signature +// contained at the end of the extra data. +// +// Note, the method requires the extra data to be at least 65 bytes, otherwise it +// panics. This is done to avoid accidentally using both forms (signature present +// or not), which could be abused to produce different hashes for the same header. +func BorRLP(header *types.Header) []byte { + b := new(bytes.Buffer) + encodeSigHeader(b, header) + return b.Bytes() +} + +// Bor is the matic-bor consensus engine +type Bor struct { + chainConfig *params.ChainConfig // Chain config + config *params.BorConfig // Consensus engine configuration parameters for bor consensus + db ethdb.Database // Database to store and retrieve snapshot checkpoints + + recents *lru.ARCCache // Snapshots for recent block to speed up reorgs + signatures *lru.ARCCache // Signatures of recent blocks to speed up mining + + signer common.Address // Ethereum address of the signing key + signFn SignerFn // Signer function to authorize hashes with + lock sync.RWMutex // Protects the signer fields + + ethAPI *ethapi.PublicBlockChainAPI + GenesisContractsClient *GenesisContractsClient + validatorSetABI abi.ABI + stateReceiverABI abi.ABI + HeimdallClient IHeimdallClient + WithoutHeimdall bool + + stateSyncFeed event.Feed + scope event.SubscriptionScope + // The fields below are for testing only + fakeDiff bool // Skip difficulty verifications +} + +// New creates a Matic Bor consensus engine. +func New( + chainConfig *params.ChainConfig, + db ethdb.Database, + ethAPI *ethapi.PublicBlockChainAPI, + heimdallURL string, + withoutHeimdall bool, +) *Bor { + // get bor config + borConfig := chainConfig.Bor + + // Set any missing consensus parameters to their defaults + if borConfig != nil && borConfig.Sprint == 0 { + borConfig.Sprint = defaultSprintLength + } + + // Allocate the snapshot caches and create the engine + recents, _ := lru.NewARC(inmemorySnapshots) + signatures, _ := lru.NewARC(inmemorySignatures) + vABI, _ := abi.JSON(strings.NewReader(validatorsetABI)) + sABI, _ := abi.JSON(strings.NewReader(stateReceiverABI)) + heimdallClient, _ := NewHeimdallClient(heimdallURL) + genesisContractsClient := NewGenesisContractsClient(chainConfig, borConfig.ValidatorContract, borConfig.StateReceiverContract, ethAPI) + c := &Bor{ + chainConfig: chainConfig, + config: borConfig, + db: db, + ethAPI: ethAPI, + recents: recents, + signatures: signatures, + validatorSetABI: vABI, + stateReceiverABI: sABI, + GenesisContractsClient: genesisContractsClient, + HeimdallClient: heimdallClient, + WithoutHeimdall: withoutHeimdall, + } + + return c +} + +// Author implements consensus.Engine, returning the Ethereum address recovered +// from the signature in the header's extra-data section. +func (c *Bor) Author(header *types.Header) (common.Address, error) { + return ecrecover(header, c.signatures) +} + +// VerifyHeader checks whether a header conforms to the consensus rules. +func (c *Bor) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, seal bool) error { + return c.verifyHeader(chain, header, nil) +} + +// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The +// method returns a quit channel to abort the operations and a results channel to +// retrieve the async verifications (the order is that of the input slice). +func (c *Bor) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { + abort := make(chan struct{}) + results := make(chan error, len(headers)) + + go func() { + for i, header := range headers { + err := c.verifyHeader(chain, header, headers[:i]) + + select { + case <-abort: + return + case results <- err: + } + } + }() + return abort, results +} + +// verifyHeader checks whether a header conforms to the consensus rules.The +// caller may optionally pass in a batch of parents (ascending order) to avoid +// looking those up from the database. This is useful for concurrently verifying +// a batch of new headers. +func (c *Bor) verifyHeader(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { + if header.Number == nil { + return errUnknownBlock + } + number := header.Number.Uint64() + + // Don't waste time checking blocks from the future + if header.Time > uint64(time.Now().Unix()) { + return consensus.ErrFutureBlock + } + + if err := validateHeaderExtraField(header.Extra); err != nil { + return err + } + + // check extr adata + isSprintEnd := (number+1)%c.config.Sprint == 0 + + // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise + signersBytes := len(header.Extra) - extraVanity - extraSeal + if !isSprintEnd && signersBytes != 0 { + return errExtraValidators + } + if isSprintEnd && signersBytes%validatorHeaderBytesLength != 0 { + return errInvalidSpanValidators + } + // Ensure that the mix digest is zero as we don't have fork protection currently + if header.MixDigest != (common.Hash{}) { + return errInvalidMixDigest + } + // Ensure that the block doesn't contain any uncles which are meaningless in PoA + if header.UncleHash != uncleHash { + return errInvalidUncleHash + } + // Ensure that the block's difficulty is meaningful (may not be correct at this point) + if number > 0 { + if header.Difficulty == nil { + return errInvalidDifficulty + } + } + // If all checks passed, validate any special fields for hard forks + if err := misc.VerifyForkHashes(chain.Config(), header, false); err != nil { + return err + } + // All basic checks passed, verify cascading fields + return c.verifyCascadingFields(chain, header, parents) +} + +// validateHeaderExtraField validates that the extra-data contains both the vanity and signature. +// header.Extra = header.Vanity + header.ProducerBytes (optional) + header.Seal +func validateHeaderExtraField(extraBytes []byte) error { + if len(extraBytes) < extraVanity { + return errMissingVanity + } + if len(extraBytes) < extraVanity+extraSeal { + return errMissingSignature + } + return nil +} + +// verifyCascadingFields verifies all the header fields that are not standalone, +// rather depend on a batch of previous headers. The caller may optionally pass +// in a batch of parents (ascending order) to avoid looking those up from the +// database. This is useful for concurrently verifying a batch of new headers. +func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { + // The genesis block is the always valid dead-end + number := header.Number.Uint64() + if number == 0 { + return nil + } + + // Ensure that the block's timestamp isn't too close to it's parent + var parent *types.Header + if len(parents) > 0 { + parent = parents[len(parents)-1] + } else { + parent = chain.GetHeader(header.ParentHash, number-1) + } + + if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { + return consensus.ErrUnknownAncestor + } + + if parent.Time+c.config.Period > header.Time { + return ErrInvalidTimestamp + } + + // Retrieve the snapshot needed to verify this header and cache it + snap, err := c.snapshot(chain, number-1, header.ParentHash, parents) + if err != nil { + return err + } + + // verify the validator list in the last sprint block + if isSprintStart(number, c.config.Sprint) { + parentValidatorBytes := parent.Extra[extraVanity : len(parent.Extra)-extraSeal] + validatorsBytes := make([]byte, len(snap.ValidatorSet.Validators)*validatorHeaderBytesLength) + + currentValidators := snap.ValidatorSet.Copy().Validators + // sort validator by address + sort.Sort(ValidatorsByAddress(currentValidators)) + for i, validator := range currentValidators { + copy(validatorsBytes[i*validatorHeaderBytesLength:], validator.HeaderBytes()) + } + // len(header.Extra) >= extraVanity+extraSeal has already been validated in validateHeaderExtraField, so this won't result in a panic + if !bytes.Equal(parentValidatorBytes, validatorsBytes) { + return &MismatchingValidatorsError{number - 1, validatorsBytes, parentValidatorBytes} + } + } + + // All basic checks passed, verify the seal and return + return c.verifySeal(chain, header, parents) +} + +// snapshot retrieves the authorization snapshot at a given point in time. +func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { + // Search for a snapshot in memory or on disk for checkpoints + var ( + headers []*types.Header + snap *Snapshot + ) + + for snap == nil { + // If an in-memory snapshot was found, use that + if s, ok := c.recents.Get(hash); ok { + snap = s.(*Snapshot) + break + } + + // If an on-disk checkpoint snapshot can be found, use that + if number%checkpointInterval == 0 { + if s, err := loadSnapshot(c.config, c.signatures, c.db, hash, c.ethAPI); err == nil { + log.Trace("Loaded snapshot from disk", "number", number, "hash", hash) + snap = s + break + } + } + + // If we're at the genesis, snapshot the initial state. Alternatively if we're + // at a checkpoint block without a parent (light client CHT), or we have piled + // up more headers than allowed to be reorged (chain reinit from a freezer), + // consider the checkpoint trusted and snapshot it. + // TODO fix this + if number == 0 { + checkpoint := chain.GetHeaderByNumber(number) + if checkpoint != nil { + // get checkpoint data + hash := checkpoint.Hash() + + // get validators and current span + validators, err := c.GetCurrentValidators(number, number+1) + if err != nil { + return nil, err + } + + // new snap shot + snap = newSnapshot(c.config, c.signatures, number, hash, validators, c.ethAPI) + if err := snap.store(c.db); err != nil { + return nil, err + } + log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash) + break + } + } + + // No snapshot for this header, gather the header and move backward + var header *types.Header + if len(parents) > 0 { + // If we have explicit parents, pick from there (enforced) + header = parents[len(parents)-1] + if header.Hash() != hash || header.Number.Uint64() != number { + return nil, consensus.ErrUnknownAncestor + } + parents = parents[:len(parents)-1] + } else { + // No explicit parents (or no more left), reach out to the database + header = chain.GetHeader(hash, number) + if header == nil { + return nil, consensus.ErrUnknownAncestor + } + } + headers = append(headers, header) + number, hash = number-1, header.ParentHash + } + + // check if snapshot is nil + if snap == nil { + return nil, fmt.Errorf("Unknown error while retrieving snapshot at block number %v", number) + } + + // Previous snapshot found, apply any pending headers on top of it + for i := 0; i < len(headers)/2; i++ { + headers[i], headers[len(headers)-1-i] = headers[len(headers)-1-i], headers[i] + } + + snap, err := snap.apply(headers) + if err != nil { + return nil, err + } + c.recents.Add(snap.Hash, snap) + + // If we've generated a new checkpoint snapshot, save to disk + if snap.Number%checkpointInterval == 0 && len(headers) > 0 { + if err = snap.store(c.db); err != nil { + return nil, err + } + log.Trace("Stored snapshot to disk", "number", snap.Number, "hash", snap.Hash) + } + return snap, err +} + +// VerifyUncles implements consensus.Engine, always returning an error for any +// uncles as this consensus mechanism doesn't permit uncles. +func (c *Bor) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { + if len(block.Uncles()) > 0 { + return errors.New("uncles not allowed") + } + return nil +} + +// VerifySeal implements consensus.Engine, checking whether the signature contained +// in the header satisfies the consensus protocol requirements. +func (c *Bor) VerifySeal(chain consensus.ChainHeaderReader, header *types.Header) error { + return c.verifySeal(chain, header, nil) +} + +// verifySeal checks whether the signature contained in the header satisfies the +// consensus protocol requirements. The method accepts an optional list of parent +// headers that aren't yet part of the local blockchain to generate the snapshots +// from. +func (c *Bor) verifySeal(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { + // Verifying the genesis block is not supported + number := header.Number.Uint64() + if number == 0 { + return errUnknownBlock + } + // Retrieve the snapshot needed to verify this header and cache it + snap, err := c.snapshot(chain, number-1, header.ParentHash, parents) + if err != nil { + return err + } + + // Resolve the authorization key and check against signers + signer, err := ecrecover(header, c.signatures) + if err != nil { + return err + } + if !snap.ValidatorSet.HasAddress(signer.Bytes()) { + // Check the UnauthorizedSignerError.Error() msg to see why we pass number-1 + return &UnauthorizedSignerError{number - 1, signer.Bytes()} + } + + succession, err := snap.GetSignerSuccessionNumber(signer) + if err != nil { + return err + } + + var parent *types.Header + if len(parents) > 0 { // if parents is nil, len(parents) is zero + parent = parents[len(parents)-1] + } else if number > 0 { + parent = chain.GetHeader(header.ParentHash, number-1) + } + + if parent != nil && header.Time < parent.Time+CalcProducerDelay(number, succession, c.config) { + return &BlockTooSoonError{number, succession} + } + + // Ensure that the difficulty corresponds to the turn-ness of the signer + if !c.fakeDiff { + difficulty := snap.Difficulty(signer) + if header.Difficulty.Uint64() != difficulty { + return &WrongDifficultyError{number, difficulty, header.Difficulty.Uint64(), signer.Bytes()} + } + } + + return nil +} + +// Prepare implements consensus.Engine, preparing all the consensus fields of the +// header for running the transactions on top. +func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { + // If the block isn't a checkpoint, cast a random vote (good enough for now) + header.Coinbase = common.Address{} + header.Nonce = types.BlockNonce{} + + number := header.Number.Uint64() + // Assemble the validator snapshot to check which votes make sense + snap, err := c.snapshot(chain, number-1, header.ParentHash, nil) + if err != nil { + return err + } + + // Set the correct difficulty + header.Difficulty = new(big.Int).SetUint64(snap.Difficulty(c.signer)) + + // Ensure the extra data has all it's components + if len(header.Extra) < extraVanity { + header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...) + } + header.Extra = header.Extra[:extraVanity] + + // get validator set if number + if (number+1)%c.config.Sprint == 0 { + newValidators, err := c.GetCurrentValidators(snap.Number, number+1) + if err != nil { + return errors.New("unknown validators") + } + + // sort validator by address + sort.Sort(ValidatorsByAddress(newValidators)) + for _, validator := range newValidators { + header.Extra = append(header.Extra, validator.HeaderBytes()...) + } + } + + // add extra seal space + header.Extra = append(header.Extra, make([]byte, extraSeal)...) + + // Mix digest is reserved for now, set to empty + header.MixDigest = common.Hash{} + + // Ensure the timestamp has the correct delay + parent := chain.GetHeader(header.ParentHash, number-1) + if parent == nil { + return consensus.ErrUnknownAncestor + } + + var succession int + // if signer is not empty + if bytes.Compare(c.signer.Bytes(), common.Address{}.Bytes()) != 0 { + succession, err = snap.GetSignerSuccessionNumber(c.signer) + if err != nil { + return err + } + } + + header.Time = parent.Time + CalcProducerDelay(number, succession, c.config) + if header.Time < uint64(time.Now().Unix()) { + header.Time = uint64(time.Now().Unix()) + } + return nil +} + +// Finalize implements consensus.Engine, ensuring no uncles are set, nor block +// rewards given. +func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) { + var err error + headerNumber := header.Number.Uint64() + if headerNumber%c.config.Sprint == 0 { + cx := chainContext{Chain: chain, Bor: c} + // check and commit span + if err := c.checkAndCommitSpan(state, header, cx); err != nil { + log.Error("Error while committing span", "error", err) + return + } + + if !c.WithoutHeimdall { + // commit statees + _, err = c.CommitStates(state, header, cx) + if err != nil { + log.Error("Error while committing states", "error", err) + return + } + } + } + + // No block rewards in PoA, so the state remains as is and uncles are dropped + header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) + header.UncleHash = types.CalcUncleHash(nil) +} + +// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set, +// nor block rewards given, and returns the final block. +func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { + headerNumber := header.Number.Uint64() + if headerNumber%c.config.Sprint == 0 { + cx := chainContext{Chain: chain, Bor: c} + + // check and commit span + err := c.checkAndCommitSpan(state, header, cx) + if err != nil { + log.Error("Error while committing span", "error", err) + return nil, err + } + + if !c.WithoutHeimdall { + // commit states + _, err = c.CommitStates(state, header, cx) + if err != nil { + log.Error("Error while committing states", "error", err) + return nil, err + } + } + } + + // No block rewards in PoA, so the state remains as is and uncles are dropped + header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) + header.UncleHash = types.CalcUncleHash(nil) + + // Assemble block + block := types.NewBlock(header, txs, nil, receipts, new(trie.Trie)) + // return the final block for sealing + return block, nil +} + +// Authorize injects a private key into the consensus engine to mint new blocks +// with. +func (c *Bor) Authorize(signer common.Address, signFn SignerFn) { + c.lock.Lock() + defer c.lock.Unlock() + + c.signer = signer + c.signFn = signFn +} + +// Seal implements consensus.Engine, attempting to create a sealed block using +// the local signing credentials. +func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { + header := block.Header() + // Sealing the genesis block is not supported + number := header.Number.Uint64() + if number == 0 { + return errUnknownBlock + } + // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing) + if c.config.Period == 0 && len(block.Transactions()) == 0 { + log.Info("Sealing paused, waiting for transactions") + return nil + } + // Don't hold the signer fields for the entire sealing procedure + c.lock.RLock() + signer, signFn := c.signer, c.signFn + c.lock.RUnlock() + + snap, err := c.snapshot(chain, number-1, header.ParentHash, nil) + if err != nil { + return err + } + + // Bail out if we're unauthorized to sign a block + if !snap.ValidatorSet.HasAddress(signer.Bytes()) { + // Check the UnauthorizedSignerError.Error() msg to see why we pass number-1 + return &UnauthorizedSignerError{number - 1, signer.Bytes()} + } + + successionNumber, err := snap.GetSignerSuccessionNumber(signer) + if err != nil { + return err + } + + // Sweet, the protocol permits us to sign the block, wait for our time + delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple + // wiggle was already accounted for in header.Time, this is just for logging + wiggle := time.Duration(successionNumber) * time.Duration(c.config.BackupMultiplier) * time.Second + + // Sign all the things! + sighash, err := signFn(accounts.Account{Address: signer}, accounts.MimetypeBor, BorRLP(header)) + if err != nil { + return err + } + copy(header.Extra[len(header.Extra)-extraSeal:], sighash) + + // Wait until sealing is terminated or delay timeout. + log.Trace("Waiting for slot to sign and propagate", "delay", common.PrettyDuration(delay)) + go func() { + select { + case <-stop: + log.Debug("Discarding sealing operation for block", "number", number) + return + case <-time.After(delay): + if wiggle > 0 { + log.Info( + "Sealing out-of-turn", + "number", number, + "wiggle", common.PrettyDuration(wiggle), + "in-turn-signer", snap.ValidatorSet.GetProposer().Address.Hex(), + ) + } + log.Info( + "Sealing successful", + "number", number, + "delay", delay, + "headerDifficulty", header.Difficulty, + ) + } + select { + case results <- block.WithSeal(header): + default: + log.Warn("Sealing result was not read by miner", "number", number, "sealhash", SealHash(header)) + } + }() + return nil +} + +// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty +// that a new block should have based on the previous blocks in the chain and the +// current signer. +func (c *Bor) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { + snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil) + if err != nil { + return nil + } + return new(big.Int).SetUint64(snap.Difficulty(c.signer)) +} + +// SealHash returns the hash of a block prior to it being sealed. +func (c *Bor) SealHash(header *types.Header) common.Hash { + return SealHash(header) +} + +// APIs implements consensus.Engine, returning the user facing RPC API to allow +// controlling the signer voting. +func (c *Bor) APIs(chain consensus.ChainHeaderReader) []rpc.API { + return []rpc.API{{ + Namespace: "bor", + Version: "1.0", + Service: &API{chain: chain, bor: c}, + Public: false, + }} +} + +// Close implements consensus.Engine. It's a noop for bor as there are no background threads. +func (c *Bor) Close() error { + return nil +} + +// GetCurrentSpan get current span from contract +func (c *Bor) GetCurrentSpan(snapshotNumber uint64) (*Span, error) { + // block + blockNr := rpc.BlockNumber(snapshotNumber) + + // method + method := "getCurrentSpan" + + data, err := c.validatorSetABI.Pack(method) + if err != nil { + log.Error("Unable to pack tx for getCurrentSpan", "error", err) + return nil, err + } + + msgData := (hexutil.Bytes)(data) + toAddress := common.HexToAddress(c.config.ValidatorContract) + gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) + result, err := c.ethAPI.Call(context.Background(), ethapi.CallArgs{ + Gas: &gas, + To: &toAddress, + Data: &msgData, + }, rpc.BlockNumberOrHash{BlockNumber: &blockNr}, nil) + if err != nil { + return nil, err + } + + // span result + ret := new(struct { + Number *big.Int + StartBlock *big.Int + EndBlock *big.Int + }) + if err := c.validatorSetABI.UnpackIntoInterface(ret, method, result); err != nil { + return nil, err + } + + // create new span + span := Span{ + ID: ret.Number.Uint64(), + StartBlock: ret.StartBlock.Uint64(), + EndBlock: ret.EndBlock.Uint64(), + } + + return &span, nil +} + +// GetCurrentValidators get current validators +func (c *Bor) GetCurrentValidators(snapshotNumber uint64, blockNumber uint64) ([]*Validator, error) { + // block + blockNr := rpc.BlockNumber(snapshotNumber) + + // method + method := "getBorValidators" + + data, err := c.validatorSetABI.Pack(method, big.NewInt(0).SetUint64(blockNumber)) + if err != nil { + log.Error("Unable to pack tx for getValidator", "error", err) + return nil, err + } + + // call + msgData := (hexutil.Bytes)(data) + toAddress := common.HexToAddress(c.config.ValidatorContract) + gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) + result, err := c.ethAPI.Call(context.Background(), ethapi.CallArgs{ + Gas: &gas, + To: &toAddress, + Data: &msgData, + }, rpc.BlockNumberOrHash{BlockNumber: &blockNr}, nil) + if err != nil { + panic(err) + // return nil, err + } + + var ( + ret0 = new([]common.Address) + ret1 = new([]*big.Int) + ) + out := &[]interface{}{ + ret0, + ret1, + } + + if err := c.validatorSetABI.UnpackIntoInterface(out, method, result); err != nil { + return nil, err + } + + valz := make([]*Validator, len(*ret0)) + for i, a := range *ret0 { + valz[i] = &Validator{ + Address: a, + VotingPower: (*ret1)[i].Int64(), + } + } + + return valz, nil +} + +func (c *Bor) checkAndCommitSpan( + state *state.StateDB, + header *types.Header, + chain core.ChainContext, +) error { + headerNumber := header.Number.Uint64() + span, err := c.GetCurrentSpan(headerNumber - 1) + if err != nil { + return err + } + if c.needToCommitSpan(span, headerNumber) { + err := c.fetchAndCommitSpan(span.ID+1, state, header, chain) + return err + } + return nil +} + +func (c *Bor) needToCommitSpan(span *Span, headerNumber uint64) bool { + // if span is nil + if span == nil { + return false + } + + // check span is not set initially + if span.EndBlock == 0 { + return true + } + + // if current block is first block of last sprint in current span + if span.EndBlock > c.config.Sprint && span.EndBlock-c.config.Sprint+1 == headerNumber { + return true + } + + return false +} + +func (c *Bor) fetchAndCommitSpan( + newSpanID uint64, + state *state.StateDB, + header *types.Header, + chain core.ChainContext, +) error { + var heimdallSpan HeimdallSpan + + if c.WithoutHeimdall { + s, err := c.getNextHeimdallSpanForTest(newSpanID, state, header, chain) + if err != nil { + return err + } + heimdallSpan = *s + } else { + response, err := c.HeimdallClient.FetchWithRetry(fmt.Sprintf("bor/span/%d", newSpanID), "") + if err != nil { + return err + } + + if err := json.Unmarshal(response.Result, &heimdallSpan); err != nil { + return err + } + } + + // check if chain id matches with heimdall span + if heimdallSpan.ChainID != c.chainConfig.ChainID.String() { + return fmt.Errorf( + "Chain id proposed span, %s, and bor chain id, %s, doesn't match", + heimdallSpan.ChainID, + c.chainConfig.ChainID, + ) + } + + // get validators bytes + var validators []MinimalVal + for _, val := range heimdallSpan.ValidatorSet.Validators { + validators = append(validators, val.MinimalVal()) + } + validatorBytes, err := rlp.EncodeToBytes(validators) + if err != nil { + return err + } + + // get producers bytes + var producers []MinimalVal + for _, val := range heimdallSpan.SelectedProducers { + producers = append(producers, val.MinimalVal()) + } + producerBytes, err := rlp.EncodeToBytes(producers) + if err != nil { + return err + } + + // method + method := "commitSpan" + log.Info("✅ Committing new span", + "id", heimdallSpan.ID, + "startBlock", heimdallSpan.StartBlock, + "endBlock", heimdallSpan.EndBlock, + "validatorBytes", hex.EncodeToString(validatorBytes), + "producerBytes", hex.EncodeToString(producerBytes), + ) + + // get packed data + data, err := c.validatorSetABI.Pack(method, + big.NewInt(0).SetUint64(heimdallSpan.ID), + big.NewInt(0).SetUint64(heimdallSpan.StartBlock), + big.NewInt(0).SetUint64(heimdallSpan.EndBlock), + validatorBytes, + producerBytes, + ) + if err != nil { + log.Error("Unable to pack tx for commitSpan", "error", err) + return err + } + + // get system message + msg := getSystemMessage(common.HexToAddress(c.config.ValidatorContract), data) + + // apply message + return applyMessage(msg, state, header, c.chainConfig, chain) +} + +// GetPendingStateProposals get pending state proposals +func (c *Bor) GetPendingStateProposals(snapshotNumber uint64) ([]*big.Int, error) { + // block + blockNr := rpc.BlockNumber(snapshotNumber) + + // method + method := "getPendingStates" + + data, err := c.stateReceiverABI.Pack(method) + if err != nil { + log.Error("Unable to pack tx for getPendingStates", "error", err) + return nil, err + } + + msgData := (hexutil.Bytes)(data) + toAddress := common.HexToAddress(c.config.StateReceiverContract) + gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) + result, err := c.ethAPI.Call(context.Background(), ethapi.CallArgs{ + Gas: &gas, + To: &toAddress, + Data: &msgData, + }, rpc.BlockNumberOrHash{BlockNumber: &blockNr}, nil) + if err != nil { + return nil, err + } + + var ret = new([]*big.Int) + if err = c.stateReceiverABI.UnpackIntoInterface(ret, method, result); err != nil { + return nil, err + } + + return *ret, nil +} + +// CommitStates commit states +func (c *Bor) CommitStates( + state *state.StateDB, + header *types.Header, + chain chainContext, +) ([]*types.StateData, error) { + stateSyncs := make([]*types.StateData, 0) + number := header.Number.Uint64() + _lastStateID, err := c.GenesisContractsClient.LastStateId(number - 1) + if err != nil { + return nil, err + } + + to := time.Unix(int64(chain.Chain.GetHeaderByNumber(number-c.config.Sprint).Time), 0) + lastStateID := _lastStateID.Uint64() + log.Info( + "Fetching state updates from Heimdall", + "fromID", lastStateID+1, + "to", to.Format(time.RFC3339)) + eventRecords, err := c.HeimdallClient.FetchStateSyncEvents(lastStateID+1, to.Unix()) + + chainID := c.chainConfig.ChainID.String() + for _, eventRecord := range eventRecords { + if eventRecord.ID <= lastStateID { + continue + } + if err := validateEventRecord(eventRecord, number, to, lastStateID, chainID); err != nil { + log.Error(err.Error()) + break + } + + stateData := types.StateData{ + Did: eventRecord.ID, + Contract: eventRecord.Contract, + Data: hex.EncodeToString(eventRecord.Data), + TxHash: eventRecord.TxHash, + } + stateSyncs = append(stateSyncs, &stateData) + + if err := c.GenesisContractsClient.CommitState(eventRecord, state, header, chain); err != nil { + return nil, err + } + lastStateID++ + } + return stateSyncs, nil +} + +func validateEventRecord(eventRecord *EventRecordWithTime, number uint64, to time.Time, lastStateID uint64, chainID string) error { + // event id should be sequential and event.Time should lie in the range [from, to) + if lastStateID+1 != eventRecord.ID || eventRecord.ChainID != chainID || !eventRecord.Time.Before(to) { + return &InvalidStateReceivedError{number, lastStateID, &to, eventRecord} + } + return nil +} + +func (c *Bor) SetHeimdallClient(h IHeimdallClient) { + c.HeimdallClient = h +} + +// +// Private methods +// + +func (c *Bor) getNextHeimdallSpanForTest( + newSpanID uint64, + state *state.StateDB, + header *types.Header, + chain core.ChainContext, +) (*HeimdallSpan, error) { + headerNumber := header.Number.Uint64() + span, err := c.GetCurrentSpan(headerNumber - 1) + if err != nil { + return nil, err + } + + // get local chain context object + localContext := chain.(chainContext) + // Retrieve the snapshot needed to verify this header and cache it + snap, err := c.snapshot(localContext.Chain, headerNumber-1, header.ParentHash, nil) + if err != nil { + return nil, err + } + + // new span + span.ID = newSpanID + if span.EndBlock == 0 { + span.StartBlock = 256 + } else { + span.StartBlock = span.EndBlock + 1 + } + span.EndBlock = span.StartBlock + (100 * c.config.Sprint) - 1 + + selectedProducers := make([]Validator, len(snap.ValidatorSet.Validators)) + for i, v := range snap.ValidatorSet.Validators { + selectedProducers[i] = *v + } + heimdallSpan := &HeimdallSpan{ + Span: *span, + ValidatorSet: *snap.ValidatorSet, + SelectedProducers: selectedProducers, + ChainID: c.chainConfig.ChainID.String(), + } + + return heimdallSpan, nil +} + +// +// Chain context +// + +// chain context +type chainContext struct { + Chain consensus.ChainHeaderReader + Bor consensus.Engine +} + +func (c chainContext) Engine() consensus.Engine { + return c.Bor +} + +func (c chainContext) GetHeader(hash common.Hash, number uint64) *types.Header { + return c.Chain.GetHeader(hash, number) +} + +// callmsg implements core.Message to allow passing it as a transaction simulator. +type callmsg struct { + ethereum.CallMsg +} + +func (m callmsg) From() common.Address { return m.CallMsg.From } +func (m callmsg) Nonce() uint64 { return 0 } +func (m callmsg) CheckNonce() bool { return false } +func (m callmsg) To() *common.Address { return m.CallMsg.To } +func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice } +func (m callmsg) Gas() uint64 { return m.CallMsg.Gas } +func (m callmsg) Value() *big.Int { return m.CallMsg.Value } +func (m callmsg) Data() []byte { return m.CallMsg.Data } + +// get system message +func getSystemMessage(toAddress common.Address, data []byte) callmsg { + return callmsg{ + ethereum.CallMsg{ + From: systemAddress, + Gas: math.MaxUint64 / 2, + GasPrice: big.NewInt(0), + Value: big.NewInt(0), + To: &toAddress, + Data: data, + }, + } +} + +// apply message +func applyMessage( + msg callmsg, + state *state.StateDB, + header *types.Header, + chainConfig *params.ChainConfig, + chainContext core.ChainContext, +) error { + // Create a new context to be used in the EVM environment + context := core.NewEVMContext(msg, header, chainContext, &header.Coinbase) + // Create a new environment which holds all relevant information + // about the transaction and calling mechanisms. + vmenv := vm.NewEVM(context, state, chainConfig, vm.Config{}) + // Apply the transaction to the current state (included in the env) + _, _, err := vmenv.Call( + vm.AccountRef(msg.From()), + *msg.To(), + msg.Data(), + msg.Gas(), + msg.Value(), + ) + // Update the state with pending changes + if err != nil { + state.Finalise(true) + } + + return nil +} + +func validatorContains(a []*Validator, x *Validator) (*Validator, bool) { + for _, n := range a { + if bytes.Compare(n.Address.Bytes(), x.Address.Bytes()) == 0 { + return n, true + } + } + return nil, false +} + +func getUpdatedValidatorSet(oldValidatorSet *ValidatorSet, newVals []*Validator) *ValidatorSet { + v := oldValidatorSet + oldVals := v.Validators + + var changes []*Validator + for _, ov := range oldVals { + if f, ok := validatorContains(newVals, ov); ok { + ov.VotingPower = f.VotingPower + } else { + ov.VotingPower = 0 + } + + changes = append(changes, ov) + } + + for _, nv := range newVals { + if _, ok := validatorContains(changes, nv); !ok { + changes = append(changes, nv) + } + } + + v.UpdateWithChangeSet(changes) + return v +} + +func isSprintStart(number, sprint uint64) bool { + return number%sprint == 0 +} diff --git a/consensus/bor/clerk.go b/consensus/bor/clerk.go new file mode 100644 index 000000000000..d7e6982873fb --- /dev/null +++ b/consensus/bor/clerk.go @@ -0,0 +1,49 @@ +package bor + +import ( + "fmt" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" +) + +// EventRecord represents state record +type EventRecord struct { + ID uint64 `json:"id" yaml:"id"` + Contract common.Address `json:"contract" yaml:"contract"` + Data hexutil.Bytes `json:"data" yaml:"data"` + TxHash common.Hash `json:"tx_hash" yaml:"tx_hash"` + LogIndex uint64 `json:"log_index" yaml:"log_index"` + ChainID string `json:"bor_chain_id" yaml:"bor_chain_id"` +} + +type EventRecordWithTime struct { + EventRecord + Time time.Time `json:"record_time" yaml:"record_time"` +} + +// String returns the string representatin of span +func (e *EventRecordWithTime) String() string { + return fmt.Sprintf( + "id %v, contract %v, data: %v, txHash: %v, logIndex: %v, chainId: %v, time %s", + e.ID, + e.Contract.String(), + e.Data.String(), + e.TxHash.Hex(), + e.LogIndex, + e.ChainID, + e.Time.Format(time.RFC3339), + ) +} + +func (e *EventRecordWithTime) BuildEventRecord() *EventRecord { + return &EventRecord{ + ID: e.ID, + Contract: e.Contract, + Data: e.Data, + TxHash: e.TxHash, + LogIndex: e.LogIndex, + ChainID: e.ChainID, + } +} diff --git a/consensus/bor/errors.go b/consensus/bor/errors.go new file mode 100644 index 000000000000..a1e60d1e219e --- /dev/null +++ b/consensus/bor/errors.go @@ -0,0 +1,143 @@ +package bor + +import ( + "fmt" + "time" +) + +// TotalVotingPowerExceededError is returned when the maximum allowed total voting power is exceeded +type TotalVotingPowerExceededError struct { + Sum int64 + Validators []*Validator +} + +func (e *TotalVotingPowerExceededError) Error() string { + return fmt.Sprintf( + "Total voting power should be guarded to not exceed %v; got: %v; for validator set: %v", + MaxTotalVotingPower, + e.Sum, + e.Validators, + ) +} + +type InvalidStartEndBlockError struct { + Start uint64 + End uint64 + CurrentHeader uint64 +} + +func (e *InvalidStartEndBlockError) Error() string { + return fmt.Sprintf( + "Invalid parameters start: %d and end block: %d params", + e.Start, + e.End, + ) +} + +type MaxCheckpointLengthExceededError struct { + Start uint64 + End uint64 +} + +func (e *MaxCheckpointLengthExceededError) Error() string { + return fmt.Sprintf( + "Start: %d and end block: %d exceed max allowed checkpoint length: %d", + e.Start, + e.End, + MaxCheckpointLength, + ) +} + +// MismatchingValidatorsError is returned if a last block in sprint contains a +// list of validators different from the one that local node calculated +type MismatchingValidatorsError struct { + Number uint64 + ValidatorSetSnap []byte + ValidatorSetHeader []byte +} + +func (e *MismatchingValidatorsError) Error() string { + return fmt.Sprintf( + "Mismatching validators at block %d\nValidatorBytes from snapshot: 0x%x\nValidatorBytes in Header: 0x%x\n", + e.Number, + e.ValidatorSetSnap, + e.ValidatorSetHeader, + ) +} + +type BlockTooSoonError struct { + Number uint64 + Succession int +} + +func (e *BlockTooSoonError) Error() string { + return fmt.Sprintf( + "Block %d was created too soon. Signer turn-ness number is %d\n", + e.Number, + e.Succession, + ) +} + +// UnauthorizedProposerError is returned if a header is [being] signed by an unauthorized entity. +type UnauthorizedProposerError struct { + Number uint64 + Proposer []byte +} + +func (e *UnauthorizedProposerError) Error() string { + return fmt.Sprintf( + "Proposer 0x%x is not a part of the producer set at block %d", + e.Proposer, + e.Number, + ) +} + +// UnauthorizedSignerError is returned if a header is [being] signed by an unauthorized entity. +type UnauthorizedSignerError struct { + Number uint64 + Signer []byte +} + +func (e *UnauthorizedSignerError) Error() string { + return fmt.Sprintf( + "Signer 0x%x is not a part of the producer set at block %d", + e.Signer, + e.Number, + ) +} + +// WrongDifficultyError is returned if the difficulty of a block doesn't match the +// turn of the signer. +type WrongDifficultyError struct { + Number uint64 + Expected uint64 + Actual uint64 + Signer []byte +} + +func (e *WrongDifficultyError) Error() string { + return fmt.Sprintf( + "Wrong difficulty at block %d, expected: %d, actual %d. Signer was %x\n", + e.Number, + e.Expected, + e.Actual, + e.Signer, + ) +} + +type InvalidStateReceivedError struct { + Number uint64 + LastStateID uint64 + To *time.Time + Event *EventRecordWithTime +} + +func (e *InvalidStateReceivedError) Error() string { + return fmt.Sprintf( + "Received invalid event %s at block %d. Requested events until %s. Last state id was %d", + e.Event, + e.Number, + e.To.Format(time.RFC3339), + e.LastStateID, + ) +} diff --git a/consensus/bor/genesis_contracts_client.go b/consensus/bor/genesis_contracts_client.go new file mode 100644 index 000000000000..abe3d0270158 --- /dev/null +++ b/consensus/bor/genesis_contracts_client.go @@ -0,0 +1,103 @@ +package bor + +import ( + "context" + "math" + "math/big" + "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/rpc" +) + +type GenesisContractsClient struct { + validatorSetABI abi.ABI + stateReceiverABI abi.ABI + ValidatorContract string + StateReceiverContract string + chainConfig *params.ChainConfig + ethAPI *ethapi.PublicBlockChainAPI +} + +const validatorsetABI = `[{"constant":true,"inputs":[],"name":"SPRINT","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"SYSTEM_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"CHAIN","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"FIRST_END_BLOCK","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"producers","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"uint256","name":"power","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"ROUND_TYPE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"BOR_ID","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"spanNumbers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"VOTE_TYPE","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"validators","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"uint256","name":"power","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"spans","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"startBlock","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"endBlock","type":"uint256"}],"name":"NewSpan","type":"event"},{"constant":true,"inputs":[],"name":"currentSprint","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"}],"name":"getSpan","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getCurrentSpan","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getNextSpan","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"number","type":"uint256"}],"name":"getSpanByBlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"currentSpanNumber","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"}],"name":"getValidatorsTotalStakeBySpan","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"}],"name":"getProducersTotalStakeBySpan","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"name":"getValidatorBySigner","outputs":[{"components":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"uint256","name":"power","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"internalType":"struct BorValidatorSet.Validator","name":"result","type":"tuple"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"name":"isValidator","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"name":"isProducer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"signer","type":"address"}],"name":"isCurrentValidator","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"signer","type":"address"}],"name":"isCurrentProducer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"number","type":"uint256"}],"name":"getBorValidators","outputs":[{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getInitialValidators","outputs":[{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getValidators","outputs":[{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"newSpan","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"},{"internalType":"bytes","name":"validatorBytes","type":"bytes"},{"internalType":"bytes","name":"producerBytes","type":"bytes"}],"name":"commitSpan","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"},{"internalType":"bytes32","name":"dataHash","type":"bytes32"},{"internalType":"bytes","name":"sigs","type":"bytes"}],"name":"getStakePowerBySigs","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"rootHash","type":"bytes32"},{"internalType":"bytes32","name":"leaf","type":"bytes32"},{"internalType":"bytes","name":"proof","type":"bytes"}],"name":"checkMembership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"d","type":"bytes32"}],"name":"leafNode","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"left","type":"bytes32"},{"internalType":"bytes32","name":"right","type":"bytes32"}],"name":"innerNode","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"pure","type":"function"}]` +const stateReceiverABI = `[{"constant":true,"inputs":[],"name":"SYSTEM_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"lastStateId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"syncTime","type":"uint256"},{"internalType":"bytes","name":"recordBytes","type":"bytes"}],"name":"commitState","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]` + +func NewGenesisContractsClient( + chainConfig *params.ChainConfig, + validatorContract, + stateReceiverContract string, + ethAPI *ethapi.PublicBlockChainAPI, +) *GenesisContractsClient { + vABI, _ := abi.JSON(strings.NewReader(validatorsetABI)) + sABI, _ := abi.JSON(strings.NewReader(stateReceiverABI)) + return &GenesisContractsClient{ + validatorSetABI: vABI, + stateReceiverABI: sABI, + ValidatorContract: validatorContract, + StateReceiverContract: stateReceiverContract, + chainConfig: chainConfig, + ethAPI: ethAPI, + } +} + +func (gc *GenesisContractsClient) CommitState( + event *EventRecordWithTime, + state *state.StateDB, + header *types.Header, + chCtx chainContext, +) error { + eventRecord := event.BuildEventRecord() + recordBytes, err := rlp.EncodeToBytes(eventRecord) + if err != nil { + return err + } + method := "commitState" + t := event.Time.Unix() + data, err := gc.stateReceiverABI.Pack(method, big.NewInt(0).SetInt64(t), recordBytes) + if err != nil { + log.Error("Unable to pack tx for commitState", "error", err) + return err + } + log.Info("→ committing new state", "eventRecord", event.String()) + msg := getSystemMessage(common.HexToAddress(gc.StateReceiverContract), data) + if err := applyMessage(msg, state, header, gc.chainConfig, chCtx); err != nil { + return err + } + return nil +} + +func (gc *GenesisContractsClient) LastStateId(snapshotNumber uint64) (*big.Int, error) { + blockNr := rpc.BlockNumber(snapshotNumber) + method := "lastStateId" + data, err := gc.stateReceiverABI.Pack(method) + if err != nil { + log.Error("Unable to pack tx for LastStateId", "error", err) + return nil, err + } + + msgData := (hexutil.Bytes)(data) + toAddress := common.HexToAddress(gc.StateReceiverContract) + gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) + result, err := gc.ethAPI.Call(context.Background(), ethapi.CallArgs{ + Gas: &gas, + To: &toAddress, + Data: &msgData, + }, rpc.BlockNumberOrHash{BlockNumber: &blockNr}, nil) + if err != nil { + return nil, err + } + + var ret = new(*big.Int) + if err := gc.stateReceiverABI.UnpackIntoInterface(ret, method, result); err != nil { + return nil, err + } + return *ret, nil +} diff --git a/consensus/bor/merkle.go b/consensus/bor/merkle.go new file mode 100644 index 000000000000..bdfbaba9834e --- /dev/null +++ b/consensus/bor/merkle.go @@ -0,0 +1,48 @@ +package bor + +func appendBytes32(data ...[]byte) []byte { + var result []byte + for _, v := range data { + paddedV, err := convertTo32(v) + if err == nil { + result = append(result, paddedV[:]...) + } + } + return result +} + +func nextPowerOfTwo(n uint64) uint64 { + if n == 0 { + return 1 + } + // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + n-- + n |= n >> 1 + n |= n >> 2 + n |= n >> 4 + n |= n >> 8 + n |= n >> 16 + n |= n >> 32 + n++ + return n +} + +func convertTo32(input []byte) (output [32]byte, err error) { + l := len(input) + if l > 32 || l == 0 { + return + } + copy(output[32-l:], input[:]) + return +} + +func convert(input []([32]byte)) [][]byte { + var output [][]byte + for _, in := range input { + newInput := make([]byte, len(in[:])) + copy(newInput, in[:]) + output = append(output, newInput) + + } + return output +} diff --git a/consensus/bor/rest.go b/consensus/bor/rest.go new file mode 100644 index 000000000000..4408c1c415e9 --- /dev/null +++ b/consensus/bor/rest.go @@ -0,0 +1,139 @@ +package bor + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "sort" + "time" + + "github.com/ethereum/go-ethereum/log" +) + +var ( + stateFetchLimit = 50 +) + +// ResponseWithHeight defines a response object type that wraps an original +// response with a height. +type ResponseWithHeight struct { + Height string `json:"height"` + Result json.RawMessage `json:"result"` +} + +type IHeimdallClient interface { + Fetch(path string, query string) (*ResponseWithHeight, error) + FetchWithRetry(path string, query string) (*ResponseWithHeight, error) + FetchStateSyncEvents(fromID uint64, to int64) ([]*EventRecordWithTime, error) +} + +type HeimdallClient struct { + urlString string + client http.Client +} + +func NewHeimdallClient(urlString string) (*HeimdallClient, error) { + h := &HeimdallClient{ + urlString: urlString, + client: http.Client{ + Timeout: time.Duration(5 * time.Second), + }, + } + return h, nil +} + +func (h *HeimdallClient) FetchStateSyncEvents(fromID uint64, to int64) ([]*EventRecordWithTime, error) { + eventRecords := make([]*EventRecordWithTime, 0) + for { + queryParams := fmt.Sprintf("from-id=%d&to-time=%d&limit=%d", fromID, to, stateFetchLimit) + log.Info("Fetching state sync events", "queryParams", queryParams) + response, err := h.FetchWithRetry("clerk/event-record/list", queryParams) + if err != nil { + return nil, err + } + var _eventRecords []*EventRecordWithTime + if response.Result == nil { // status 204 + break + } + if err := json.Unmarshal(response.Result, &_eventRecords); err != nil { + return nil, err + } + eventRecords = append(eventRecords, _eventRecords...) + if len(_eventRecords) < stateFetchLimit { + break + } + fromID += uint64(stateFetchLimit) + } + + sort.SliceStable(eventRecords, func(i, j int) bool { + return eventRecords[i].ID < eventRecords[j].ID + }) + return eventRecords, nil +} + +// Fetch fetches response from heimdall +func (h *HeimdallClient) Fetch(rawPath string, rawQuery string) (*ResponseWithHeight, error) { + u, err := url.Parse(h.urlString) + if err != nil { + return nil, err + } + + u.Path = rawPath + u.RawQuery = rawQuery + + return h.internalFetch(u) +} + +// FetchWithRetry returns data from heimdall with retry +func (h *HeimdallClient) FetchWithRetry(rawPath string, rawQuery string) (*ResponseWithHeight, error) { + u, err := url.Parse(h.urlString) + if err != nil { + return nil, err + } + + u.Path = rawPath + u.RawQuery = rawQuery + + for { + res, err := h.internalFetch(u) + if err == nil && res != nil { + return res, nil + } + log.Info("Retrying again in 5 seconds for next Heimdall span", "path", u.Path) + time.Sleep(5 * time.Second) + } +} + +// internal fetch method +func (h *HeimdallClient) internalFetch(u *url.URL) (*ResponseWithHeight, error) { + res, err := h.client.Get(u.String()) + if err != nil { + return nil, err + } + defer res.Body.Close() + + // check status code + if res.StatusCode != 200 && res.StatusCode != 204 { + return nil, fmt.Errorf("Error while fetching data from Heimdall") + } + + // unmarshall data from buffer + var response ResponseWithHeight + if res.StatusCode == 204 { + return &response, nil + } + + // get response + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + + if err := json.Unmarshal(body, &response); err != nil { + return nil, err + } + + return &response, nil +} diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go new file mode 100644 index 000000000000..8405f34fbd91 --- /dev/null +++ b/consensus/bor/snapshot.go @@ -0,0 +1,223 @@ +package bor + +import ( + "bytes" + "encoding/json" + + lru "github.com/hashicorp/golang-lru" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/params" +) + +// Snapshot is the state of the authorization voting at a given point in time. +type Snapshot struct { + config *params.BorConfig // Consensus engine parameters to fine tune behavior + ethAPI *ethapi.PublicBlockChainAPI + sigcache *lru.ARCCache // Cache of recent block signatures to speed up ecrecover + + Number uint64 `json:"number"` // Block number where the snapshot was created + Hash common.Hash `json:"hash"` // Block hash where the snapshot was created + ValidatorSet *ValidatorSet `json:"validatorSet"` // Validator set at this moment + Recents map[uint64]common.Address `json:"recents"` // Set of recent signers for spam protections +} + +// signersAscending implements the sort interface to allow sorting a list of addresses +type signersAscending []common.Address + +func (s signersAscending) Len() int { return len(s) } +func (s signersAscending) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 } +func (s signersAscending) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + +// newSnapshot creates a new snapshot with the specified startup parameters. This +// method does not initialize the set of recent signers, so only ever use if for +// the genesis block. +func newSnapshot( + config *params.BorConfig, + sigcache *lru.ARCCache, + number uint64, + hash common.Hash, + validators []*Validator, + ethAPI *ethapi.PublicBlockChainAPI, +) *Snapshot { + snap := &Snapshot{ + config: config, + ethAPI: ethAPI, + sigcache: sigcache, + Number: number, + Hash: hash, + ValidatorSet: NewValidatorSet(validators), + Recents: make(map[uint64]common.Address), + } + return snap +} + +// loadSnapshot loads an existing snapshot from the database. +func loadSnapshot(config *params.BorConfig, sigcache *lru.ARCCache, db ethdb.Database, hash common.Hash, ethAPI *ethapi.PublicBlockChainAPI) (*Snapshot, error) { + blob, err := db.Get(append([]byte("bor-"), hash[:]...)) + if err != nil { + return nil, err + } + snap := new(Snapshot) + if err := json.Unmarshal(blob, snap); err != nil { + return nil, err + } + snap.config = config + snap.sigcache = sigcache + snap.ethAPI = ethAPI + + // update total voting power + if err := snap.ValidatorSet.updateTotalVotingPower(); err != nil { + return nil, err + } + + return snap, nil +} + +// store inserts the snapshot into the database. +func (s *Snapshot) store(db ethdb.Database) error { + blob, err := json.Marshal(s) + if err != nil { + return err + } + return db.Put(append([]byte("bor-"), s.Hash[:]...), blob) +} + +// copy creates a deep copy of the snapshot, though not the individual votes. +func (s *Snapshot) copy() *Snapshot { + cpy := &Snapshot{ + config: s.config, + ethAPI: s.ethAPI, + sigcache: s.sigcache, + Number: s.Number, + Hash: s.Hash, + ValidatorSet: s.ValidatorSet.Copy(), + Recents: make(map[uint64]common.Address), + } + for block, signer := range s.Recents { + cpy.Recents[block] = signer + } + + return cpy +} + +func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { + // Allow passing in no headers for cleaner code + if len(headers) == 0 { + return s, nil + } + // Sanity check that the headers can be applied + for i := 0; i < len(headers)-1; i++ { + if headers[i+1].Number.Uint64() != headers[i].Number.Uint64()+1 { + return nil, errOutOfRangeChain + } + } + if headers[0].Number.Uint64() != s.Number+1 { + return nil, errOutOfRangeChain + } + // Iterate through the headers and create a new snapshot + snap := s.copy() + + for _, header := range headers { + // Remove any votes on checkpoint blocks + number := header.Number.Uint64() + + // Delete the oldest signer from the recent list to allow it signing again + if number >= s.config.Sprint && number-s.config.Sprint >= 0 { + delete(snap.Recents, number-s.config.Sprint) + } + + // Resolve the authorization key and check against signers + signer, err := ecrecover(header, s.sigcache) + if err != nil { + return nil, err + } + + // check if signer is in validator set + if !snap.ValidatorSet.HasAddress(signer.Bytes()) { + return nil, &UnauthorizedSignerError{number, signer.Bytes()} + } + + if _, err = snap.GetSignerSuccessionNumber(signer); err != nil { + return nil, err + } + + // add recents + snap.Recents[number] = signer + + // change validator set and change proposer + if number > 0 && (number+1)%s.config.Sprint == 0 { + if err := validateHeaderExtraField(header.Extra); err != nil { + return nil, err + } + validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal] + + // get validators from headers and use that for new validator set + newVals, _ := ParseValidators(validatorBytes) + v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals) + v.IncrementProposerPriority(1) + snap.ValidatorSet = v + } + } + snap.Number += uint64(len(headers)) + snap.Hash = headers[len(headers)-1].Hash() + + return snap, nil +} + +// GetSignerSuccessionNumber returns the relative position of signer in terms of the in-turn proposer +func (s *Snapshot) GetSignerSuccessionNumber(signer common.Address) (int, error) { + validators := s.ValidatorSet.Validators + proposer := s.ValidatorSet.GetProposer().Address + proposerIndex, _ := s.ValidatorSet.GetByAddress(proposer) + if proposerIndex == -1 { + return -1, &UnauthorizedProposerError{s.Number, proposer.Bytes()} + } + signerIndex, _ := s.ValidatorSet.GetByAddress(signer) + if signerIndex == -1 { + return -1, &UnauthorizedSignerError{s.Number, signer.Bytes()} + } + + tempIndex := signerIndex + if proposerIndex != tempIndex { + if tempIndex < proposerIndex { + tempIndex = tempIndex + len(validators) + } + } + return tempIndex - proposerIndex, nil +} + +// signers retrieves the list of authorized signers in ascending order. +func (s *Snapshot) signers() []common.Address { + sigs := make([]common.Address, 0, len(s.ValidatorSet.Validators)) + for _, sig := range s.ValidatorSet.Validators { + sigs = append(sigs, sig.Address) + } + return sigs +} + +// Difficulty returns the difficulty for a particular signer at the current snapshot number +func (s *Snapshot) Difficulty(signer common.Address) uint64 { + // if signer is empty + if bytes.Compare(signer.Bytes(), common.Address{}.Bytes()) == 0 { + return 1 + } + + validators := s.ValidatorSet.Validators + proposer := s.ValidatorSet.GetProposer().Address + totalValidators := len(validators) + + proposerIndex, _ := s.ValidatorSet.GetByAddress(proposer) + signerIndex, _ := s.ValidatorSet.GetByAddress(signer) + + // temp index + tempIndex := signerIndex + if tempIndex < proposerIndex { + tempIndex = tempIndex + totalValidators + } + + return uint64(totalValidators - (tempIndex - proposerIndex)) +} diff --git a/consensus/bor/snapshot_test.go b/consensus/bor/snapshot_test.go new file mode 100644 index 000000000000..6bb85478435a --- /dev/null +++ b/consensus/bor/snapshot_test.go @@ -0,0 +1,124 @@ +package bor + +import ( + "math/rand" + "sort" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/ethereum/go-ethereum/common" +) + +const ( + numVals = 100 +) + +func TestGetSignerSuccessionNumber_ProposerIsSigner(t *testing.T) { + validators := buildRandomValidatorSet(numVals) + validatorSet := NewValidatorSet(validators) + snap := Snapshot{ + ValidatorSet: validatorSet, + } + + // proposer is signer + signer := validatorSet.Proposer.Address + successionNumber, err := snap.GetSignerSuccessionNumber(signer) + if err != nil { + t.Fatalf("%s", err) + } + assert.Equal(t, 0, successionNumber) +} + +func TestGetSignerSuccessionNumber_SignerIndexIsLarger(t *testing.T) { + validators := buildRandomValidatorSet(numVals) + + // sort validators by address, which is what NewValidatorSet also does + sort.Sort(ValidatorsByAddress(validators)) + proposerIndex := 32 + signerIndex := 56 + // give highest ProposerPriority to a particular val, so that they become the proposer + validators[proposerIndex].VotingPower = 200 + snap := Snapshot{ + ValidatorSet: NewValidatorSet(validators), + } + + // choose a signer at an index greater than proposer index + signer := snap.ValidatorSet.Validators[signerIndex].Address + successionNumber, err := snap.GetSignerSuccessionNumber(signer) + if err != nil { + t.Fatalf("%s", err) + } + assert.Equal(t, signerIndex-proposerIndex, successionNumber) +} + +func TestGetSignerSuccessionNumber_SignerIndexIsSmaller(t *testing.T) { + validators := buildRandomValidatorSet(numVals) + proposerIndex := 98 + signerIndex := 11 + // give highest ProposerPriority to a particular val, so that they become the proposer + validators[proposerIndex].VotingPower = 200 + snap := Snapshot{ + ValidatorSet: NewValidatorSet(validators), + } + + // choose a signer at an index greater than proposer index + signer := snap.ValidatorSet.Validators[signerIndex].Address + successionNumber, err := snap.GetSignerSuccessionNumber(signer) + if err != nil { + t.Fatalf("%s", err) + } + assert.Equal(t, signerIndex+numVals-proposerIndex, successionNumber) +} + +func TestGetSignerSuccessionNumber_ProposerNotFound(t *testing.T) { + validators := buildRandomValidatorSet(numVals) + snap := Snapshot{ + ValidatorSet: NewValidatorSet(validators), + } + dummyProposerAddress := randomAddress() + snap.ValidatorSet.Proposer = &Validator{Address: dummyProposerAddress} + // choose any signer + signer := snap.ValidatorSet.Validators[3].Address + _, err := snap.GetSignerSuccessionNumber(signer) + assert.NotNil(t, err) + e, ok := err.(*UnauthorizedProposerError) + assert.True(t, ok) + assert.Equal(t, dummyProposerAddress.Bytes(), e.Proposer) +} + +func TestGetSignerSuccessionNumber_SignerNotFound(t *testing.T) { + validators := buildRandomValidatorSet(numVals) + snap := Snapshot{ + ValidatorSet: NewValidatorSet(validators), + } + dummySignerAddress := randomAddress() + _, err := snap.GetSignerSuccessionNumber(dummySignerAddress) + assert.NotNil(t, err) + e, ok := err.(*UnauthorizedSignerError) + assert.True(t, ok) + assert.Equal(t, dummySignerAddress.Bytes(), e.Signer) +} + +func buildRandomValidatorSet(numVals int) []*Validator { + rand.Seed(time.Now().Unix()) + validators := make([]*Validator, numVals) + for i := 0; i < numVals; i++ { + validators[i] = &Validator{ + Address: randomAddress(), + // cannot process validators with voting power 0, hence +1 + VotingPower: int64(rand.Intn(99) + 1), + } + } + + // sort validators by address, which is what NewValidatorSet also does + sort.Sort(ValidatorsByAddress(validators)) + return validators +} + +func randomAddress() common.Address { + bytes := make([]byte, 32) + rand.Read(bytes) + return common.BytesToAddress(bytes) +} diff --git a/consensus/bor/span.go b/consensus/bor/span.go new file mode 100644 index 000000000000..2fd0cf10792d --- /dev/null +++ b/consensus/bor/span.go @@ -0,0 +1,16 @@ +package bor + +// Span represents a current bor span +type Span struct { + ID uint64 `json:"span_id" yaml:"span_id"` + StartBlock uint64 `json:"start_block" yaml:"start_block"` + EndBlock uint64 `json:"end_block" yaml:"end_block"` +} + +// HeimdallSpan represents span from heimdall APIs +type HeimdallSpan struct { + Span + ValidatorSet ValidatorSet `json:"validator_set" yaml:"validator_set"` + SelectedProducers []Validator `json:"selected_producers" yaml:"selected_producers"` + ChainID string `json:"bor_chain_id" yaml:"bor_chain_id"` +} diff --git a/consensus/bor/validator.go b/consensus/bor/validator.go new file mode 100644 index 000000000000..00e9fdc645e9 --- /dev/null +++ b/consensus/bor/validator.go @@ -0,0 +1,154 @@ +package bor + +import ( + "bytes" + // "encoding/json" + "errors" + "fmt" + "math/big" + "sort" + "strings" + + "github.com/ethereum/go-ethereum/common" +) + +// Validator represets Volatile state for each Validator +type Validator struct { + ID uint64 `json:"ID"` + Address common.Address `json:"signer"` + VotingPower int64 `json:"power"` + ProposerPriority int64 `json:"accum"` +} + +// NewValidator creates new validator +func NewValidator(address common.Address, votingPower int64) *Validator { + return &Validator{ + Address: address, + VotingPower: votingPower, + ProposerPriority: 0, + } +} + +// Copy creates a new copy of the validator so we can mutate ProposerPriority. +// Panics if the validator is nil. +func (v *Validator) Copy() *Validator { + vCopy := *v + return &vCopy +} + +// Cmp returns the one validator with a higher ProposerPriority. +// If ProposerPriority is same, it returns the validator with lexicographically smaller address +func (v *Validator) Cmp(other *Validator) *Validator { + // if both of v and other are nil, nil will be returned and that could possibly lead to nil pointer dereference bubbling up the stack + if v == nil { + return other + } + if other == nil { + return v + } + if v.ProposerPriority > other.ProposerPriority { + return v + } else if v.ProposerPriority < other.ProposerPriority { + return other + } else { + result := bytes.Compare(v.Address.Bytes(), other.Address.Bytes()) + if result < 0 { + return v + } else if result > 0 { + return other + } else { + panic("Cannot compare identical validators") + } + } +} + +func (v *Validator) String() string { + if v == nil { + return "nil-Validator" + } + return fmt.Sprintf("Validator{%v Power:%v Priority:%v}", + v.Address.Hex(), + v.VotingPower, + v.ProposerPriority) +} + +// ValidatorListString returns a prettified validator list for logging purposes. +func ValidatorListString(vals []*Validator) string { + chunks := make([]string, len(vals)) + for i, val := range vals { + chunks[i] = fmt.Sprintf("%s:%d", val.Address, val.VotingPower) + } + + return strings.Join(chunks, ",") +} + +// HeaderBytes return header bytes +func (v *Validator) HeaderBytes() []byte { + result := make([]byte, 40) + copy(result[:20], v.Address.Bytes()) + copy(result[20:], v.PowerBytes()) + return result +} + +// PowerBytes return power bytes +func (v *Validator) PowerBytes() []byte { + powerBytes := big.NewInt(0).SetInt64(v.VotingPower).Bytes() + result := make([]byte, 20) + copy(result[20-len(powerBytes):], powerBytes) + return result +} + +// MinimalVal returns block number of last validator update +func (v *Validator) MinimalVal() MinimalVal { + return MinimalVal{ + ID: v.ID, + VotingPower: uint64(v.VotingPower), + Signer: v.Address, + } +} + +// ParseValidators returns validator set bytes +func ParseValidators(validatorsBytes []byte) ([]*Validator, error) { + if len(validatorsBytes)%40 != 0 { + return nil, errors.New("Invalid validators bytes") + } + + result := make([]*Validator, len(validatorsBytes)/40) + for i := 0; i < len(validatorsBytes); i += 40 { + address := make([]byte, 20) + power := make([]byte, 20) + + copy(address, validatorsBytes[i:i+20]) + copy(power, validatorsBytes[i+20:i+40]) + + result[i/40] = NewValidator(common.BytesToAddress(address), big.NewInt(0).SetBytes(power).Int64()) + } + + return result, nil +} + +// --- + +// MinimalVal is the minimal validator representation +// Used to send validator information to bor validator contract +type MinimalVal struct { + ID uint64 `json:"ID"` + VotingPower uint64 `json:"power"` // TODO add 10^-18 here so that we dont overflow easily + Signer common.Address `json:"signer"` +} + +// SortMinimalValByAddress sorts validators +func SortMinimalValByAddress(a []MinimalVal) []MinimalVal { + sort.Slice(a, func(i, j int) bool { + return bytes.Compare(a[i].Signer.Bytes(), a[j].Signer.Bytes()) < 0 + }) + return a +} + +// ValidatorsToMinimalValidators converts array of validators to minimal validators +func ValidatorsToMinimalValidators(vals []Validator) (minVals []MinimalVal) { + for _, val := range vals { + minVals = append(minVals, val.MinimalVal()) + } + return +} diff --git a/consensus/bor/validator_set.go b/consensus/bor/validator_set.go new file mode 100644 index 000000000000..0b5c10ebd020 --- /dev/null +++ b/consensus/bor/validator_set.go @@ -0,0 +1,703 @@ +package bor + +// Tendermint leader selection algorithm + +import ( + "bytes" + "fmt" + "math" + "math/big" + "sort" + "strings" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/log" +) + +// MaxTotalVotingPower - the maximum allowed total voting power. +// It needs to be sufficiently small to, in all cases: +// 1. prevent clipping in incrementProposerPriority() +// 2. let (diff+diffMax-1) not overflow in IncrementProposerPriority() +// (Proof of 1 is tricky, left to the reader). +// It could be higher, but this is sufficiently large for our purposes, +// and leaves room for defensive purposes. +// PriorityWindowSizeFactor - is a constant that when multiplied with the total voting power gives +// the maximum allowed distance between validator priorities. + +const ( + MaxTotalVotingPower = int64(math.MaxInt64) / 8 + PriorityWindowSizeFactor = 2 +) + +// ValidatorSet represent a set of *Validator at a given height. +// The validators can be fetched by address or index. +// The index is in order of .Address, so the indices are fixed +// for all rounds of a given blockchain height - ie. the validators +// are sorted by their address. +// On the other hand, the .ProposerPriority of each validator and +// the designated .GetProposer() of a set changes every round, +// upon calling .IncrementProposerPriority(). +// NOTE: Not goroutine-safe. +// NOTE: All get/set to validators should copy the value for safety. +type ValidatorSet struct { + // NOTE: persisted via reflect, must be exported. + Validators []*Validator `json:"validators"` + Proposer *Validator `json:"proposer"` + + // cached (unexported) + totalVotingPower int64 +} + +// NewValidatorSet initializes a ValidatorSet by copying over the +// values from `valz`, a list of Validators. If valz is nil or empty, +// the new ValidatorSet will have an empty list of Validators. +// The addresses of validators in `valz` must be unique otherwise the +// function panics. +func NewValidatorSet(valz []*Validator) *ValidatorSet { + vals := &ValidatorSet{} + err := vals.updateWithChangeSet(valz, false) + if err != nil { + panic(fmt.Sprintf("cannot create validator set: %s", err)) + } + if len(valz) > 0 { + vals.IncrementProposerPriority(1) + } + return vals +} + +// Nil or empty validator sets are invalid. +func (vals *ValidatorSet) IsNilOrEmpty() bool { + return vals == nil || len(vals.Validators) == 0 +} + +// Increment ProposerPriority and update the proposer on a copy, and return it. +func (vals *ValidatorSet) CopyIncrementProposerPriority(times int) *ValidatorSet { + copy := vals.Copy() + copy.IncrementProposerPriority(times) + return copy +} + +// IncrementProposerPriority increments ProposerPriority of each validator and updates the +// proposer. Panics if validator set is empty. +// `times` must be positive. +func (vals *ValidatorSet) IncrementProposerPriority(times int) { + if vals.IsNilOrEmpty() { + panic("empty validator set") + } + if times <= 0 { + panic("Cannot call IncrementProposerPriority with non-positive times") + } + + // Cap the difference between priorities to be proportional to 2*totalPower by + // re-normalizing priorities, i.e., rescale all priorities by multiplying with: + // 2*totalVotingPower/(maxPriority - minPriority) + diffMax := PriorityWindowSizeFactor * vals.TotalVotingPower() + vals.RescalePriorities(diffMax) + vals.shiftByAvgProposerPriority() + + var proposer *Validator + // Call IncrementProposerPriority(1) times times. + for i := 0; i < times; i++ { + proposer = vals.incrementProposerPriority() + } + + vals.Proposer = proposer +} + +func (vals *ValidatorSet) RescalePriorities(diffMax int64) { + if vals.IsNilOrEmpty() { + panic("empty validator set") + } + // NOTE: This check is merely a sanity check which could be + // removed if all tests would init. voting power appropriately; + // i.e. diffMax should always be > 0 + if diffMax <= 0 { + return + } + + // Calculating ceil(diff/diffMax): + // Re-normalization is performed by dividing by an integer for simplicity. + // NOTE: This may make debugging priority issues easier as well. + diff := computeMaxMinPriorityDiff(vals) + ratio := (diff + diffMax - 1) / diffMax + if diff > diffMax { + for _, val := range vals.Validators { + val.ProposerPriority = val.ProposerPriority / ratio + } + } +} + +func (vals *ValidatorSet) incrementProposerPriority() *Validator { + for _, val := range vals.Validators { + // Check for overflow for sum. + newPrio := safeAddClip(val.ProposerPriority, val.VotingPower) + val.ProposerPriority = newPrio + } + // Decrement the validator with most ProposerPriority. + mostest := vals.getValWithMostPriority() + // Mind the underflow. + mostest.ProposerPriority = safeSubClip(mostest.ProposerPriority, vals.TotalVotingPower()) + + return mostest +} + +// Should not be called on an empty validator set. +func (vals *ValidatorSet) computeAvgProposerPriority() int64 { + n := int64(len(vals.Validators)) + sum := big.NewInt(0) + for _, val := range vals.Validators { + sum.Add(sum, big.NewInt(val.ProposerPriority)) + } + avg := sum.Div(sum, big.NewInt(n)) + if avg.IsInt64() { + return avg.Int64() + } + + // This should never happen: each val.ProposerPriority is in bounds of int64. + panic(fmt.Sprintf("Cannot represent avg ProposerPriority as an int64 %v", avg)) +} + +// Compute the difference between the max and min ProposerPriority of that set. +func computeMaxMinPriorityDiff(vals *ValidatorSet) int64 { + if vals.IsNilOrEmpty() { + panic("empty validator set") + } + max := int64(math.MinInt64) + min := int64(math.MaxInt64) + for _, v := range vals.Validators { + if v.ProposerPriority < min { + min = v.ProposerPriority + } + if v.ProposerPriority > max { + max = v.ProposerPriority + } + } + diff := max - min + if diff < 0 { + return -1 * diff + } else { + return diff + } +} + +func (vals *ValidatorSet) getValWithMostPriority() *Validator { + var res *Validator + for _, val := range vals.Validators { + res = res.Cmp(val) + } + return res +} + +func (vals *ValidatorSet) shiftByAvgProposerPriority() { + if vals.IsNilOrEmpty() { + panic("empty validator set") + } + avgProposerPriority := vals.computeAvgProposerPriority() + for _, val := range vals.Validators { + val.ProposerPriority = safeSubClip(val.ProposerPriority, avgProposerPriority) + } +} + +// Makes a copy of the validator list. +func validatorListCopy(valsList []*Validator) []*Validator { + if valsList == nil { + return nil + } + valsCopy := make([]*Validator, len(valsList)) + for i, val := range valsList { + valsCopy[i] = val.Copy() + } + return valsCopy +} + +// Copy each validator into a new ValidatorSet. +func (vals *ValidatorSet) Copy() *ValidatorSet { + return &ValidatorSet{ + Validators: validatorListCopy(vals.Validators), + Proposer: vals.Proposer, + totalVotingPower: vals.totalVotingPower, + } +} + +// HasAddress returns true if address given is in the validator set, false - +// otherwise. +func (vals *ValidatorSet) HasAddress(address []byte) bool { + idx := sort.Search(len(vals.Validators), func(i int) bool { + return bytes.Compare(address, vals.Validators[i].Address.Bytes()) <= 0 + }) + return idx < len(vals.Validators) && bytes.Equal(vals.Validators[idx].Address.Bytes(), address) +} + +// GetByAddress returns an index of the validator with address and validator +// itself if found. Otherwise, -1 and nil are returned. +func (vals *ValidatorSet) GetByAddress(address common.Address) (index int, val *Validator) { + idx := sort.Search(len(vals.Validators), func(i int) bool { + return bytes.Compare(address.Bytes(), vals.Validators[i].Address.Bytes()) <= 0 + }) + if idx < len(vals.Validators) && bytes.Equal(vals.Validators[idx].Address.Bytes(), address.Bytes()) { + return idx, vals.Validators[idx].Copy() + } + return -1, nil +} + +// GetByIndex returns the validator's address and validator itself by index. +// It returns nil values if index is less than 0 or greater or equal to +// len(ValidatorSet.Validators). +func (vals *ValidatorSet) GetByIndex(index int) (address []byte, val *Validator) { + if index < 0 || index >= len(vals.Validators) { + return nil, nil + } + val = vals.Validators[index] + return val.Address.Bytes(), val.Copy() +} + +// Size returns the length of the validator set. +func (vals *ValidatorSet) Size() int { + return len(vals.Validators) +} + +// Force recalculation of the set's total voting power. +func (vals *ValidatorSet) updateTotalVotingPower() error { + + sum := int64(0) + for _, val := range vals.Validators { + // mind overflow + sum = safeAddClip(sum, val.VotingPower) + if sum > MaxTotalVotingPower { + return &TotalVotingPowerExceededError{sum, vals.Validators} + } + } + vals.totalVotingPower = sum + return nil +} + +// TotalVotingPower returns the sum of the voting powers of all validators. +// It recomputes the total voting power if required. +func (vals *ValidatorSet) TotalVotingPower() int64 { + if vals.totalVotingPower == 0 { + log.Info("invoking updateTotalVotingPower before returning it") + if err := vals.updateTotalVotingPower(); err != nil { + // Can/should we do better? + panic(err) + } + } + return vals.totalVotingPower +} + +// GetProposer returns the current proposer. If the validator set is empty, nil +// is returned. +func (vals *ValidatorSet) GetProposer() (proposer *Validator) { + if len(vals.Validators) == 0 { + return nil + } + if vals.Proposer == nil { + vals.Proposer = vals.findProposer() + } + return vals.Proposer.Copy() +} + +func (vals *ValidatorSet) findProposer() *Validator { + var proposer *Validator + for _, val := range vals.Validators { + if proposer == nil || !bytes.Equal(val.Address.Bytes(), proposer.Address.Bytes()) { + proposer = proposer.Cmp(val) + } + } + return proposer +} + +// Hash returns the Merkle root hash build using validators (as leaves) in the +// set. +// func (vals *ValidatorSet) Hash() []byte { +// if len(vals.Validators) == 0 { +// return nil +// } +// bzs := make([][]byte, len(vals.Validators)) +// for i, val := range vals.Validators { +// bzs[i] = val.Bytes() +// } +// return merkle.SimpleHashFromByteSlices(bzs) +// } + +// Iterate will run the given function over the set. +func (vals *ValidatorSet) Iterate(fn func(index int, val *Validator) bool) { + for i, val := range vals.Validators { + stop := fn(i, val.Copy()) + if stop { + break + } + } +} + +// Checks changes against duplicates, splits the changes in updates and removals, sorts them by address. +// +// Returns: +// updates, removals - the sorted lists of updates and removals +// err - non-nil if duplicate entries or entries with negative voting power are seen +// +// No changes are made to 'origChanges'. +func processChanges(origChanges []*Validator) (updates, removals []*Validator, err error) { + // Make a deep copy of the changes and sort by address. + changes := validatorListCopy(origChanges) + sort.Sort(ValidatorsByAddress(changes)) + + removals = make([]*Validator, 0, len(changes)) + updates = make([]*Validator, 0, len(changes)) + var prevAddr common.Address + + // Scan changes by address and append valid validators to updates or removals lists. + for _, valUpdate := range changes { + if bytes.Equal(valUpdate.Address.Bytes(), prevAddr.Bytes()) { + err = fmt.Errorf("duplicate entry %v in %v", valUpdate, changes) + return nil, nil, err + } + if valUpdate.VotingPower < 0 { + err = fmt.Errorf("voting power can't be negative: %v", valUpdate) + return nil, nil, err + } + if valUpdate.VotingPower > MaxTotalVotingPower { + err = fmt.Errorf("to prevent clipping/ overflow, voting power can't be higher than %v: %v ", + MaxTotalVotingPower, valUpdate) + return nil, nil, err + } + if valUpdate.VotingPower == 0 { + removals = append(removals, valUpdate) + } else { + updates = append(updates, valUpdate) + } + prevAddr = valUpdate.Address + } + return updates, removals, err +} + +// Verifies a list of updates against a validator set, making sure the allowed +// total voting power would not be exceeded if these updates would be applied to the set. +// +// Returns: +// updatedTotalVotingPower - the new total voting power if these updates would be applied +// numNewValidators - number of new validators +// err - non-nil if the maximum allowed total voting power would be exceeded +// +// 'updates' should be a list of proper validator changes, i.e. they have been verified +// by processChanges for duplicates and invalid values. +// No changes are made to the validator set 'vals'. +func verifyUpdates(updates []*Validator, vals *ValidatorSet) (updatedTotalVotingPower int64, numNewValidators int, err error) { + + updatedTotalVotingPower = vals.TotalVotingPower() + + for _, valUpdate := range updates { + address := valUpdate.Address + _, val := vals.GetByAddress(address) + if val == nil { + // New validator, add its voting power the the total. + updatedTotalVotingPower += valUpdate.VotingPower + numNewValidators++ + } else { + // Updated validator, add the difference in power to the total. + updatedTotalVotingPower += valUpdate.VotingPower - val.VotingPower + } + overflow := updatedTotalVotingPower > MaxTotalVotingPower + if overflow { + err = fmt.Errorf( + "failed to add/update validator %v, total voting power would exceed the max allowed %v", + valUpdate, MaxTotalVotingPower) + return 0, 0, err + } + } + + return updatedTotalVotingPower, numNewValidators, nil +} + +// Computes the proposer priority for the validators not present in the set based on 'updatedTotalVotingPower'. +// Leaves unchanged the priorities of validators that are changed. +// +// 'updates' parameter must be a list of unique validators to be added or updated. +// No changes are made to the validator set 'vals'. +func computeNewPriorities(updates []*Validator, vals *ValidatorSet, updatedTotalVotingPower int64) { + + for _, valUpdate := range updates { + address := valUpdate.Address + _, val := vals.GetByAddress(address) + if val == nil { + // add val + // Set ProposerPriority to -C*totalVotingPower (with C ~= 1.125) to make sure validators can't + // un-bond and then re-bond to reset their (potentially previously negative) ProposerPriority to zero. + // + // Contract: updatedVotingPower < MaxTotalVotingPower to ensure ProposerPriority does + // not exceed the bounds of int64. + // + // Compute ProposerPriority = -1.125*totalVotingPower == -(updatedVotingPower + (updatedVotingPower >> 3)). + valUpdate.ProposerPriority = -(updatedTotalVotingPower + (updatedTotalVotingPower >> 3)) + } else { + valUpdate.ProposerPriority = val.ProposerPriority + } + } + +} + +// Merges the vals' validator list with the updates list. +// When two elements with same address are seen, the one from updates is selected. +// Expects updates to be a list of updates sorted by address with no duplicates or errors, +// must have been validated with verifyUpdates() and priorities computed with computeNewPriorities(). +func (vals *ValidatorSet) applyUpdates(updates []*Validator) { + + existing := vals.Validators + merged := make([]*Validator, len(existing)+len(updates)) + i := 0 + + for len(existing) > 0 && len(updates) > 0 { + if bytes.Compare(existing[0].Address.Bytes(), updates[0].Address.Bytes()) < 0 { // unchanged validator + merged[i] = existing[0] + existing = existing[1:] + } else { + // Apply add or update. + merged[i] = updates[0] + if bytes.Equal(existing[0].Address.Bytes(), updates[0].Address.Bytes()) { + // Validator is present in both, advance existing. + existing = existing[1:] + } + updates = updates[1:] + } + i++ + } + + // Add the elements which are left. + for j := 0; j < len(existing); j++ { + merged[i] = existing[j] + i++ + } + // OR add updates which are left. + for j := 0; j < len(updates); j++ { + merged[i] = updates[j] + i++ + } + + vals.Validators = merged[:i] +} + +// Checks that the validators to be removed are part of the validator set. +// No changes are made to the validator set 'vals'. +func verifyRemovals(deletes []*Validator, vals *ValidatorSet) error { + + for _, valUpdate := range deletes { + address := valUpdate.Address + _, val := vals.GetByAddress(address) + if val == nil { + return fmt.Errorf("failed to find validator %X to remove", address) + } + } + if len(deletes) > len(vals.Validators) { + panic("more deletes than validators") + } + return nil +} + +// Removes the validators specified in 'deletes' from validator set 'vals'. +// Should not fail as verification has been done before. +func (vals *ValidatorSet) applyRemovals(deletes []*Validator) { + + existing := vals.Validators + + merged := make([]*Validator, len(existing)-len(deletes)) + i := 0 + + // Loop over deletes until we removed all of them. + for len(deletes) > 0 { + if bytes.Equal(existing[0].Address.Bytes(), deletes[0].Address.Bytes()) { + deletes = deletes[1:] + } else { // Leave it in the resulting slice. + merged[i] = existing[0] + i++ + } + existing = existing[1:] + } + + // Add the elements which are left. + for j := 0; j < len(existing); j++ { + merged[i] = existing[j] + i++ + } + + vals.Validators = merged[:i] +} + +// Main function used by UpdateWithChangeSet() and NewValidatorSet(). +// If 'allowDeletes' is false then delete operations (identified by validators with voting power 0) +// are not allowed and will trigger an error if present in 'changes'. +// The 'allowDeletes' flag is set to false by NewValidatorSet() and to true by UpdateWithChangeSet(). +func (vals *ValidatorSet) updateWithChangeSet(changes []*Validator, allowDeletes bool) error { + + if len(changes) <= 0 { + return nil + } + + // Check for duplicates within changes, split in 'updates' and 'deletes' lists (sorted). + updates, deletes, err := processChanges(changes) + if err != nil { + return err + } + + if !allowDeletes && len(deletes) != 0 { + return fmt.Errorf("cannot process validators with voting power 0: %v", deletes) + } + + // Verify that applying the 'deletes' against 'vals' will not result in error. + if err := verifyRemovals(deletes, vals); err != nil { + return err + } + + // Verify that applying the 'updates' against 'vals' will not result in error. + updatedTotalVotingPower, numNewValidators, err := verifyUpdates(updates, vals) + if err != nil { + return err + } + + // Check that the resulting set will not be empty. + if numNewValidators == 0 && len(vals.Validators) == len(deletes) { + return fmt.Errorf("applying the validator changes would result in empty set") + } + + // Compute the priorities for updates. + computeNewPriorities(updates, vals, updatedTotalVotingPower) + + // Apply updates and removals. + vals.applyUpdates(updates) + vals.applyRemovals(deletes) + + if err := vals.updateTotalVotingPower(); err != nil { + return err + } + + // Scale and center. + vals.RescalePriorities(PriorityWindowSizeFactor * vals.TotalVotingPower()) + vals.shiftByAvgProposerPriority() + + return nil +} + +// UpdateWithChangeSet attempts to update the validator set with 'changes'. +// It performs the following steps: +// - validates the changes making sure there are no duplicates and splits them in updates and deletes +// - verifies that applying the changes will not result in errors +// - computes the total voting power BEFORE removals to ensure that in the next steps the priorities +// across old and newly added validators are fair +// - computes the priorities of new validators against the final set +// - applies the updates against the validator set +// - applies the removals against the validator set +// - performs scaling and centering of priority values +// If an error is detected during verification steps, it is returned and the validator set +// is not changed. +func (vals *ValidatorSet) UpdateWithChangeSet(changes []*Validator) error { + return vals.updateWithChangeSet(changes, true) +} + +//----------------- +// ErrTooMuchChange + +func IsErrTooMuchChange(err error) bool { + switch err.(type) { + case errTooMuchChange: + return true + default: + return false + } +} + +type errTooMuchChange struct { + got int64 + needed int64 +} + +func (e errTooMuchChange) Error() string { + return fmt.Sprintf("Invalid commit -- insufficient old voting power: got %v, needed %v", e.got, e.needed) +} + +//---------------- + +func (vals *ValidatorSet) String() string { + return vals.StringIndented("") +} + +func (vals *ValidatorSet) StringIndented(indent string) string { + if vals == nil { + return "nil-ValidatorSet" + } + var valStrings []string + vals.Iterate(func(index int, val *Validator) bool { + valStrings = append(valStrings, val.String()) + return false + }) + return fmt.Sprintf(`ValidatorSet{ +%s Proposer: %v +%s Validators: +%s %v +%s}`, + indent, vals.GetProposer().String(), + indent, + indent, strings.Join(valStrings, "\n"+indent+" "), + indent) + +} + +//------------------------------------- +// Implements sort for sorting validators by address. + +// Sort validators by address. +type ValidatorsByAddress []*Validator + +func (valz ValidatorsByAddress) Len() int { + return len(valz) +} + +func (valz ValidatorsByAddress) Less(i, j int) bool { + return bytes.Compare(valz[i].Address.Bytes(), valz[j].Address.Bytes()) == -1 +} + +func (valz ValidatorsByAddress) Swap(i, j int) { + it := valz[i] + valz[i] = valz[j] + valz[j] = it +} + +/////////////////////////////////////////////////////////////////////////////// +// safe addition/subtraction + +func safeAdd(a, b int64) (int64, bool) { + if b > 0 && a > math.MaxInt64-b { + return -1, true + } else if b < 0 && a < math.MinInt64-b { + return -1, true + } + return a + b, false +} + +func safeSub(a, b int64) (int64, bool) { + if b > 0 && a < math.MinInt64+b { + return -1, true + } else if b < 0 && a > math.MaxInt64+b { + return -1, true + } + return a - b, false +} + +func safeAddClip(a, b int64) int64 { + c, overflow := safeAdd(a, b) + if overflow { + if b < 0 { + return math.MinInt64 + } + return math.MaxInt64 + } + return c +} + +func safeSubClip(a, b int64) int64 { + c, overflow := safeSub(a, b) + if overflow { + if b > 0 { + return math.MinInt64 + } + return math.MaxInt64 + } + return c +} diff --git a/core/blockchain.go b/core/blockchain.go index 1c8a7fe60a9f..898adf1a7e72 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -341,7 +341,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par // The first thing the node will do is reconstruct the verification data for // the head block (ethash cache or clique voting snapshot). Might as well do // it in advance. - bc.engine.VerifyHeader(bc, bc.CurrentHeader(), true) + // bc.engine.VerifyHeader(bc, bc.CurrentHeader(), true) // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain for hash := range BadHashes { diff --git a/core/bor_fee_log.go b/core/bor_fee_log.go new file mode 100644 index 000000000000..c2851750daf4 --- /dev/null +++ b/core/bor_fee_log.go @@ -0,0 +1,115 @@ +package core + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" +) + +var transferLogSig = common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4") +var transferFeeLogSig = common.HexToHash("0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63") +var feeAddress = common.HexToAddress("0x0000000000000000000000000000000000001010") +var bigZero = big.NewInt(0) + +// AddTransferLog adds transfer log into state +func AddTransferLog( + state vm.StateDB, + + sender, + recipient common.Address, + + amount, + input1, + input2, + output1, + output2 *big.Int, +) { + addTransferLog( + state, + transferLogSig, + + sender, + recipient, + + amount, + input1, + input2, + output1, + output2, + ) +} + +// AddFeeTransferLog adds transfer log into state +func AddFeeTransferLog( + state vm.StateDB, + + sender, + recipient common.Address, + + amount, + input1, + input2, + output1, + output2 *big.Int, +) { + addTransferLog( + state, + transferFeeLogSig, + + sender, + recipient, + + amount, + input1, + input2, + output1, + output2, + ) +} + +// addTransferLog adds transfer log into state +func addTransferLog( + state vm.StateDB, + eventSig common.Hash, + + sender, + recipient common.Address, + + amount, + input1, + input2, + output1, + output2 *big.Int, +) { + // ignore if amount is 0 + if amount.Cmp(bigZero) <= 0 { + return + } + + dataInputs := []*big.Int{ + amount, + input1, + input2, + output1, + output2, + } + + var data []byte + for _, v := range dataInputs { + data = append(data, common.LeftPadBytes(v.Bytes(), 32)...) + } + + // add transfer log + state.AddLog(&types.Log{ + Address: feeAddress, + Topics: []common.Hash{ + eventSig, + feeAddress.Hash(), + sender.Hash(), + recipient.Hash(), + }, + Data: data, + }) +} diff --git a/core/evm.go b/core/evm.go index 8abe5a04771b..1648d95d406a 100644 --- a/core/evm.go +++ b/core/evm.go @@ -100,6 +100,17 @@ func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool { // Transfer subtracts amount from sender and adds amount to recipient using the given Db func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { + // get inputs before + input1 := db.GetBalance(sender) + input2 := db.GetBalance(recipient) + db.SubBalance(sender, amount) db.AddBalance(recipient, amount) + + // get outputs after + output1 := db.GetBalance(sender) + output2 := db.GetBalance(recipient) + + // add transfer log + AddTransferLog(db, sender, recipient, amount, input1, input2, output1, output2) } diff --git a/core/state_transition.go b/core/state_transition.go index 9a9bf475e9aa..5f683f27e5b2 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -214,6 +214,9 @@ func (st *StateTransition) preCheck() error { // However if any consensus issue encountered, return the error directly with // nil evm execution result. func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { + input1 := st.state.GetBalance(st.msg.From()) + input2 := st.state.GetBalance(st.evm.Coinbase) + // First check this message satisfies all consensus rules before // applying the message. The rules include these clauses // @@ -262,6 +265,24 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { st.refundGas() st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice)) + amount := new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice) + output1 := new(big.Int).SetBytes(input1.Bytes()) + output2 := new(big.Int).SetBytes(input2.Bytes()) + + // add transfer log + AddFeeTransferLog( + st.state, + + msg.From(), + st.evm.Coinbase, + + amount, + input1, + input2, + output1.Sub(output1, amount), + output2.Add(output2, amount), + ) + return &ExecutionResult{ UsedGas: st.gasUsed(), Err: vmerr, diff --git a/core/types/state_data.go b/core/types/state_data.go new file mode 100644 index 000000000000..38174461c78d --- /dev/null +++ b/core/types/state_data.go @@ -0,0 +1,11 @@ +package types + +import "github.com/ethereum/go-ethereum/common" + +// StateData represents state received from Ethereum Blockchain +type StateData struct { + Did uint64 + Contract common.Address + Data string + TxHash common.Hash +} diff --git a/eth/backend.go b/eth/backend.go index 3fd027137c7f..694d4c35db32 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus" + "github.com/ethereum/go-ethereum/consensus/bor" "github.com/ethereum/go-ethereum/consensus/clique" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" @@ -129,7 +130,7 @@ func New(stack *node.Node, config *Config) (*Ethereum, error) { chainDb: chainDb, eventMux: stack.EventMux(), accountManager: stack.AccountManager(), - engine: CreateConsensusEngine(stack, chainConfig, &config.Ethash, config.Miner.Notify, config.Miner.Noverify, chainDb), + engine: nil, closeBloomHandler: make(chan struct{}), networkID: config.NetworkId, gasPrice: config.Miner.GasPrice, @@ -139,6 +140,19 @@ func New(stack *node.Node, config *Config) (*Ethereum, error) { p2pServer: stack.Server(), } + // START: Bor changes + eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), eth, nil} + gpoParams := config.GPO + if gpoParams.Default == nil { + gpoParams.Default = config.Miner.GasPrice + } + eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) + + // create eth api and set engine + ethAPI := ethapi.NewPublicBlockChainAPI(eth.APIBackend) + eth.engine = CreateConsensusEngine(stack, chainConfig, config, chainDb, ethAPI) + // END: Bor changes + bcVersion := rawdb.ReadDatabaseVersion(chainDb) var dbVer = "" if bcVersion != nil { @@ -175,6 +189,8 @@ func New(stack *node.Node, config *Config) (*Ethereum, error) { if err != nil { return nil, err } + eth.engine.VerifyHeader(eth.blockchain, eth.blockchain.CurrentHeader(), true) // TODO think on it + // Rewind the chain in case of an incompatible config upgrade. if compat, ok := genesisErr.(*params.ConfigCompatError); ok { log.Warn("Rewinding chain to upgrade configuration", "err", compat) @@ -200,13 +216,6 @@ func New(stack *node.Node, config *Config) (*Ethereum, error) { eth.miner = miner.New(eth, &config.Miner, chainConfig, eth.EventMux(), eth.engine, eth.isLocalBlock) eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData)) - eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), eth, nil} - gpoParams := config.GPO - if gpoParams.Default == nil { - gpoParams.Default = config.Miner.GasPrice - } - eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams) - eth.dialCandidates, err = eth.setupDiscovery(&stack.Config().P2P) if err != nil { return nil, err @@ -227,7 +236,7 @@ func makeExtraData(extra []byte) []byte { // create default extradata extra, _ = rlp.EncodeToBytes([]interface{}{ uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch), - "geth", + "bor", runtime.Version(), runtime.GOOS, }) @@ -240,11 +249,19 @@ func makeExtraData(extra []byte) []byte { } // CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service -func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine { +func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, ethConfig *Config, db ethdb.Database, blockchainAPI *ethapi.PublicBlockChainAPI) consensus.Engine { + config := ðConfig.Ethash + notify := ethConfig.Miner.Notify + noverify := ethConfig.Miner.Noverify + // If proof-of-authority is requested, set it up if chainConfig.Clique != nil { return clique.New(chainConfig.Clique, db) } + // If Matic bor consensus is requested, set it up + if chainConfig.Bor != nil { + return bor.New(chainConfig, db, blockchainAPI, ethConfig.HeimdallURL, ethConfig.WithoutHeimdall) + } // Otherwise assume proof-of-work switch config.PowMode { case ethash.ModeFake: @@ -456,6 +473,14 @@ func (s *Ethereum) StartMining(threads int) error { } clique.Authorize(eb, wallet.SignData) } + if bor, ok := s.engine.(*bor.Bor); ok { + wallet, err := s.accountManager.Find(accounts.Account{Address: eb}) + if wallet == nil || err != nil { + log.Error("Etherbase account unavailable locally", "err", err) + return fmt.Errorf("signer missing: %v", err) + } + bor.Authorize(eb, wallet.SignData) + } // If mining is started, we can disable the transaction rejection mechanism // introduced to speed sync times. atomic.StoreUint32(&s.protocolManager.acceptTxs, 1) @@ -546,3 +571,12 @@ func (s *Ethereum) Stop() error { s.eventMux.Stop() return nil } + +// +// Bor related methods +// + +// SetBlockchain set blockchain while testing +func (s *Ethereum) SetBlockchain(blockchain *core.BlockChain) { + s.blockchain = blockchain +} diff --git a/eth/config.go b/eth/config.go index 0d99c2a3f1e6..5217d4f6e03e 100644 --- a/eth/config.go +++ b/eth/config.go @@ -186,4 +186,10 @@ type Config struct { // CheckpointOracle is the configuration for checkpoint oracle. CheckpointOracle *params.CheckpointOracleConfig `toml:",omitempty"` + + // URL to connect to Heimdall node + HeimdallURL string + + // No heimdall service + WithoutHeimdall bool } diff --git a/go.mod b/go.mod index ae1cf64aaf9c..5a02365520dd 100755 --- a/go.mod +++ b/go.mod @@ -59,6 +59,7 @@ require ( github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 + github.com/xsleonard/go-merkle v1.1.0 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 golang.org/x/mobile v0.0.0-20200801112145-973feb4309de // indirect golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect diff --git a/go.sum b/go.sum index 10bec96411e9..f6f9586d3c07 100755 --- a/go.sum +++ b/go.sum @@ -200,6 +200,7 @@ github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570 h1:gIlAHnH1 github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8OR4w3TdeIHIh1g6EMY5p0gVNOovcWC+1vpc7naMuAw= github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 h1:njlZPzLwU639dk2kqnCPPv+wNjq7Xb6EfUxe/oX0/NM= github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -212,6 +213,8 @@ github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:s github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 h1:1cngl9mPEoITZG8s8cVcUy5CeIBYhEESkOB7m6Gmkrk= github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees= +github.com/xsleonard/go-merkle v1.1.0 h1:fHe1fuhJjGH22ZzVTAH0jqHLhTGhOq3wQjJN+8P0jQg= +github.com/xsleonard/go-merkle v1.1.0/go.mod h1:cW4z+UZ/4f2n9IJgIiyDCdYguchoDyDAPmpuOWGxdGg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= diff --git a/les/client.go b/les/client.go index a2f7c56dfd57..6d51d33ad9a2 100644 --- a/les/client.go +++ b/les/client.go @@ -104,7 +104,7 @@ func New(stack *node.Node, config *eth.Config) (*LightEthereum, error) { eventMux: stack.EventMux(), reqDist: newRequestDistributor(peers, &mclock.System{}), accountManager: stack.AccountManager(), - engine: eth.CreateConsensusEngine(stack, chainConfig, &config.Ethash, nil, false, chainDb), + engine: nil, bloomRequests: make(chan chan *bloombits.Retrieval), bloomIndexer: eth.NewBloomIndexer(chainDb, params.BloomBitsBlocksClient, params.HelperTrieConfirmations), valueTracker: lpc.NewValueTracker(lespayDb, &mclock.System{}, requestList, time.Minute, 1/float64(time.Hour), 1/float64(time.Hour*100), 1/float64(time.Hour*1000)), diff --git a/params/config.go b/params/config.go index ade81408a8ef..bdb7a55e67d7 100644 --- a/params/config.go +++ b/params/config.go @@ -240,16 +240,16 @@ var ( // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil} + AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, nil} // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced // and accepted by the Ethereum core developers into the Clique consensus. // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}} + AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil} - TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil} + TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil, nil} TestRules = TestChainConfig.Rules(new(big.Int)) ) @@ -327,6 +327,7 @@ type ChainConfig struct { // Various consensus engines Ethash *EthashConfig `json:"ethash,omitempty"` Clique *CliqueConfig `json:"clique,omitempty"` + Bor *BorConfig `json:"bor,omitempty"` } // EthashConfig is the consensus engine configs for proof-of-work based sealing. @@ -348,6 +349,21 @@ func (c *CliqueConfig) String() string { return "clique" } +// BorConfig is the consensus engine configs for Matic bor based sealing. +type BorConfig struct { + Period uint64 `json:"period"` // Number of seconds between blocks to enforce + ProducerDelay uint64 `json:"producerDelay"` // Number of seconds delay between two producer interval + Sprint uint64 `json:"sprint"` // Epoch length to proposer + BackupMultiplier uint64 `json:"backupMultiplier"` // Backup multiplier to determine the wiggle time + ValidatorContract string `json:"validatorContract"` // Validator set contract + StateReceiverContract string `json:"stateReceiverContract"` // State receiver contract +} + +// String implements the stringer interface, returning the consensus engine details. +func (b *BorConfig) String() string { + return "bor" +} + // String implements the fmt.Stringer interface. func (c *ChainConfig) String() string { var engine interface{} @@ -356,6 +372,8 @@ func (c *ChainConfig) String() string { engine = c.Ethash case c.Clique != nil: engine = c.Clique + case c.Bor != nil: + engine = c.Bor default: engine = "unknown" } diff --git a/signer/core/signed_data.go b/signer/core/signed_data.go index 19377a521b13..9811178cc78e 100644 --- a/signer/core/signed_data.go +++ b/signer/core/signed_data.go @@ -58,6 +58,10 @@ var ( accounts.MimetypeClique, 0x02, } + ApplicationBor = SigFormat{ + accounts.MimetypeBor, + 0x10, + } TextPlain = SigFormat{ accounts.MimetypeTextPlain, 0x45, diff --git a/tests/bor/bor_test.go b/tests/bor/bor_test.go new file mode 100644 index 000000000000..4270e41999a5 --- /dev/null +++ b/tests/bor/bor_test.go @@ -0,0 +1,270 @@ +package bor + +import ( + "encoding/hex" + "encoding/json" + "math/big" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + + "github.com/ethereum/go-ethereum/consensus/bor" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/tests/bor/mocks" +) + +var ( + spanPath = "bor/span/1" + clerkPath = "clerk/event-record/list" + clerkQueryParams = "from-time=%d&to-time=%d&page=%d&limit=50" +) + +func TestInsertingSpanSizeBlocks(t *testing.T) { + init := buildEthereumInstance(t, rawdb.NewMemoryDatabase()) + chain := init.ethereum.BlockChain() + engine := init.ethereum.Engine() + _bor := engine.(*bor.Bor) + h, heimdallSpan := getMockedHeimdallClient(t) + _bor.SetHeimdallClient(h) + + db := init.ethereum.ChainDb() + block := init.genesis.ToBlock(db) + // to := int64(block.Header().Time) + + // Insert sprintSize # of blocks so that span is fetched at the start of a new sprint + for i := uint64(1); i <= spanSize; i++ { + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor) + insertNewBlock(t, chain, block) + } + + assert.True(t, h.AssertCalled(t, "FetchWithRetry", spanPath, "")) + validators, err := _bor.GetCurrentValidators(sprintSize, spanSize) // check validator set at the first block of new span + if err != nil { + t.Fatalf("%s", err) + } + + assert.Equal(t, 3, len(validators)) + for i, validator := range validators { + assert.Equal(t, validator.Address.Bytes(), heimdallSpan.SelectedProducers[i].Address.Bytes()) + assert.Equal(t, validator.VotingPower, heimdallSpan.SelectedProducers[i].VotingPower) + } +} + +func TestFetchStateSyncEvents(t *testing.T) { + init := buildEthereumInstance(t, rawdb.NewMemoryDatabase()) + chain := init.ethereum.BlockChain() + engine := init.ethereum.Engine() + _bor := engine.(*bor.Bor) + + // A. Insert blocks for 0th sprint + db := init.ethereum.ChainDb() + block := init.genesis.ToBlock(db) + // Insert sprintSize # of blocks so that span is fetched at the start of a new sprint + for i := uint64(1); i < sprintSize; i++ { + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor) + insertNewBlock(t, chain, block) + } + + // B. Before inserting 1st block of the next sprint, mock heimdall deps + // B.1 Mock /bor/span/1 + res, _ := loadSpanFromFile(t) + h := &mocks.IHeimdallClient{} + h.On("FetchWithRetry", spanPath, "").Return(res, nil) + + // B.2 Mock State Sync events + fromID := uint64(1) + // at # sprintSize, events are fetched for [fromID, (block-sprint).Time) + to := int64(chain.GetHeaderByNumber(0).Time) + eventCount := 50 + + sample := getSampleEventRecord(t) + sample.Time = time.Unix(to-int64(eventCount+1), 0) // last event.Time will be just < to + eventRecords := generateFakeStateSyncEvents(sample, eventCount) + h.On("FetchStateSyncEvents", fromID, to).Return(eventRecords, nil) + _bor.SetHeimdallClient(h) + + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor) + insertNewBlock(t, chain, block) + + assert.True(t, h.AssertCalled(t, "FetchWithRetry", spanPath, "")) + assert.True(t, h.AssertCalled(t, "FetchStateSyncEvents", fromID, to)) +} + +func TestFetchStateSyncEvents_2(t *testing.T) { + init := buildEthereumInstance(t, rawdb.NewMemoryDatabase()) + chain := init.ethereum.BlockChain() + engine := init.ethereum.Engine() + _bor := engine.(*bor.Bor) + + // Mock /bor/span/1 + res, _ := loadSpanFromFile(t) + h := &mocks.IHeimdallClient{} + h.On("FetchWithRetry", spanPath, "").Return(res, nil) + + // Mock State Sync events + // at # sprintSize, events are fetched for [fromID, (block-sprint).Time) + fromID := uint64(1) + to := int64(chain.GetHeaderByNumber(0).Time) + sample := getSampleEventRecord(t) + + // First query will be from [id=1, (block-sprint).Time] + // Insert 5 events in this time range + eventRecords := []*bor.EventRecordWithTime{ + buildStateEvent(sample, 1, 3), // id = 1, time = 1 + buildStateEvent(sample, 2, 1), // id = 2, time = 3 + buildStateEvent(sample, 3, 2), // id = 3, time = 2 + // event with id 5 is missing + buildStateEvent(sample, 4, 5), // id = 4, time = 5 + buildStateEvent(sample, 6, 4), // id = 6, time = 4 + } + h.On("FetchStateSyncEvents", fromID, to).Return(eventRecords, nil) + _bor.SetHeimdallClient(h) + + // Insert blocks for 0th sprint + db := init.ethereum.ChainDb() + block := init.genesis.ToBlock(db) + for i := uint64(1); i <= sprintSize; i++ { + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor) + insertNewBlock(t, chain, block) + } + assert.True(t, h.AssertCalled(t, "FetchWithRetry", spanPath, "")) + assert.True(t, h.AssertCalled(t, "FetchStateSyncEvents", fromID, to)) + lastStateID, _ := _bor.GenesisContractsClient.LastStateId(sprintSize) + // state 6 was not written + assert.Equal(t, uint64(4), lastStateID.Uint64()) + + // + fromID = uint64(5) + to = int64(chain.GetHeaderByNumber(sprintSize).Time) + eventRecords = []*bor.EventRecordWithTime{ + buildStateEvent(sample, 5, 7), + buildStateEvent(sample, 6, 4), + } + h.On("FetchStateSyncEvents", fromID, to).Return(eventRecords, nil) + for i := sprintSize + 1; i <= spanSize; i++ { + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor) + insertNewBlock(t, chain, block) + } + assert.True(t, h.AssertCalled(t, "FetchStateSyncEvents", fromID, to)) + lastStateID, _ = _bor.GenesisContractsClient.LastStateId(spanSize) + assert.Equal(t, uint64(6), lastStateID.Uint64()) +} + +func TestOutOfTurnSigning(t *testing.T) { + init := buildEthereumInstance(t, rawdb.NewMemoryDatabase()) + chain := init.ethereum.BlockChain() + engine := init.ethereum.Engine() + _bor := engine.(*bor.Bor) + h, _ := getMockedHeimdallClient(t) + _bor.SetHeimdallClient(h) + + db := init.ethereum.ChainDb() + block := init.genesis.ToBlock(db) + + for i := uint64(1); i < spanSize; i++ { + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor) + insertNewBlock(t, chain, block) + } + + // insert spanSize-th block + // This account is one the out-of-turn validators for 1st (0-indexed) span + signer := "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd9" + signerKey, _ := hex.DecodeString(signer) + key, _ = crypto.HexToECDSA(signer) + addr = crypto.PubkeyToAddress(key.PublicKey) + expectedSuccessionNumber := 2 + + block = buildNextBlock(t, _bor, chain, block, signerKey, init.genesis.Config.Bor) + _, err := chain.InsertChain([]*types.Block{block}) + assert.Equal(t, + *err.(*bor.BlockTooSoonError), + bor.BlockTooSoonError{Number: spanSize, Succession: expectedSuccessionNumber}) + + expectedDifficulty := uint64(3 - expectedSuccessionNumber) // len(validators) - succession + header := block.Header() + header.Time += (bor.CalcProducerDelay(header.Number.Uint64(), expectedSuccessionNumber, init.genesis.Config.Bor) - + bor.CalcProducerDelay(header.Number.Uint64(), 0, init.genesis.Config.Bor)) + sign(t, header, signerKey) + block = types.NewBlockWithHeader(header) + _, err = chain.InsertChain([]*types.Block{block}) + assert.Equal(t, + *err.(*bor.WrongDifficultyError), + bor.WrongDifficultyError{Number: spanSize, Expected: expectedDifficulty, Actual: 3, Signer: addr.Bytes()}) + + header.Difficulty = new(big.Int).SetUint64(expectedDifficulty) + sign(t, header, signerKey) + block = types.NewBlockWithHeader(header) + _, err = chain.InsertChain([]*types.Block{block}) + assert.Nil(t, err) +} + +func TestSignerNotFound(t *testing.T) { + init := buildEthereumInstance(t, rawdb.NewMemoryDatabase()) + chain := init.ethereum.BlockChain() + engine := init.ethereum.Engine() + _bor := engine.(*bor.Bor) + h, _ := getMockedHeimdallClient(t) + _bor.SetHeimdallClient(h) + + db := init.ethereum.ChainDb() + block := init.genesis.ToBlock(db) + + // random signer account that is not a part of the validator set + signer := "3714d99058cd64541433d59c6b391555b2fd9b54629c2b717a6c9c00d1127b6b" + signerKey, _ := hex.DecodeString(signer) + key, _ = crypto.HexToECDSA(signer) + addr = crypto.PubkeyToAddress(key.PublicKey) + + block = buildNextBlock(t, _bor, chain, block, signerKey, init.genesis.Config.Bor) + _, err := chain.InsertChain([]*types.Block{block}) + assert.Equal(t, + *err.(*bor.UnauthorizedSignerError), + bor.UnauthorizedSignerError{Number: 0, Signer: addr.Bytes()}) +} + +func getMockedHeimdallClient(t *testing.T) (*mocks.IHeimdallClient, *bor.HeimdallSpan) { + res, heimdallSpan := loadSpanFromFile(t) + h := &mocks.IHeimdallClient{} + h.On("FetchWithRetry", "bor/span/1", "").Return(res, nil) + h.On( + "FetchStateSyncEvents", + mock.AnythingOfType("uint64"), + mock.AnythingOfType("int64")).Return([]*bor.EventRecordWithTime{getSampleEventRecord(t)}, nil) + return h, heimdallSpan +} + +func generateFakeStateSyncEvents(sample *bor.EventRecordWithTime, count int) []*bor.EventRecordWithTime { + events := make([]*bor.EventRecordWithTime, count) + event := *sample + event.ID = 1 + events[0] = &bor.EventRecordWithTime{} + *events[0] = event + for i := 1; i < count; i++ { + event.ID = uint64(i) + event.Time = event.Time.Add(1 * time.Second) + events[i] = &bor.EventRecordWithTime{} + *events[i] = event + } + return events +} + +func buildStateEvent(sample *bor.EventRecordWithTime, id uint64, timeStamp int64) *bor.EventRecordWithTime { + event := *sample + event.ID = id + event.Time = time.Unix(timeStamp, 0) + return &event +} + +func getSampleEventRecord(t *testing.T) *bor.EventRecordWithTime { + res := stateSyncEventsPayload(t) + var _eventRecords []*bor.EventRecordWithTime + if err := json.Unmarshal(res.Result, &_eventRecords); err != nil { + t.Fatalf("%s", err) + } + _eventRecords[0].Time = time.Unix(1, 0) + return _eventRecords[0] +} diff --git a/tests/bor/helper.go b/tests/bor/helper.go new file mode 100644 index 000000000000..850d82643506 --- /dev/null +++ b/tests/bor/helper.go @@ -0,0 +1,172 @@ +package bor + +import ( + "encoding/hex" + "encoding/json" + "io/ioutil" + "math/big" + "sort" + "testing" + + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus/bor" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/crypto/secp256k1" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/params" +) + +var ( + // The genesis for tests was generated with following parameters + extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal + + // Only this account is a validator for the 0th span + privKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" + key, _ = crypto.HexToECDSA(privKey) + addr = crypto.PubkeyToAddress(key.PublicKey) // 0x71562b71999873DB5b286dF957af199Ec94617F7 + + // This account is one the validators for 1st span (0-indexed) + privKey2 = "9b28f36fbd67381120752d6172ecdcf10e06ab2d9a1367aac00cdcd6ac7855d3" + key2, _ = crypto.HexToECDSA(privKey2) + addr2 = crypto.PubkeyToAddress(key2.PublicKey) // 0x9fB29AAc15b9A4B7F17c3385939b007540f4d791 + + validatorHeaderBytesLength = common.AddressLength + 20 // address + power + sprintSize uint64 = 4 + spanSize uint64 = 8 +) + +type initializeData struct { + genesis *core.Genesis + ethereum *eth.Ethereum +} + +func buildEthereumInstance(t *testing.T, db ethdb.Database) *initializeData { + genesisData, err := ioutil.ReadFile("./testdata/genesis.json") + if err != nil { + t.Fatalf("%s", err) + } + gen := &core.Genesis{} + if err := json.Unmarshal(genesisData, gen); err != nil { + t.Fatalf("%s", err) + } + ethConf := ð.Config{ + Genesis: gen, + } + ethConf.Genesis.MustCommit(db) + + ethereum := utils.CreateBorEthereum(ethConf) + if err != nil { + t.Fatalf("failed to register Ethereum protocol: %v", err) + } + + ethConf.Genesis.MustCommit(ethereum.ChainDb()) + return &initializeData{ + genesis: gen, + ethereum: ethereum, + } +} + +func insertNewBlock(t *testing.T, chain *core.BlockChain, block *types.Block) { + if _, err := chain.InsertChain([]*types.Block{block}); err != nil { + t.Fatalf("%s", err) + } +} + +func buildNextBlock(t *testing.T, _bor *bor.Bor, chain *core.BlockChain, block *types.Block, signer []byte, borConfig *params.BorConfig) *types.Block { + header := block.Header() + header.Number.Add(header.Number, big.NewInt(1)) + number := header.Number.Uint64() + + if signer == nil { + signer = getSignerKey(header.Number.Uint64()) + } + + header.ParentHash = block.Hash() + header.Time += bor.CalcProducerDelay(header.Number.Uint64(), 0, borConfig) + header.Extra = make([]byte, 32+65) // vanity + extraSeal + + currentValidators := []*bor.Validator{bor.NewValidator(addr, 10)} + + isSpanEnd := (number+1)%spanSize == 0 + isSpanStart := number%spanSize == 0 + isSprintEnd := (header.Number.Uint64()+1)%sprintSize == 0 + if isSpanEnd { + _, heimdallSpan := loadSpanFromFile(t) + // this is to stash the validator bytes in the header + currentValidators = heimdallSpan.ValidatorSet.Validators + } else if isSpanStart { + header.Difficulty = new(big.Int).SetInt64(3) + } + if isSprintEnd { + sort.Sort(bor.ValidatorsByAddress(currentValidators)) + validatorBytes := make([]byte, len(currentValidators)*validatorHeaderBytesLength) + header.Extra = make([]byte, 32+len(validatorBytes)+65) // vanity + validatorBytes + extraSeal + for i, val := range currentValidators { + copy(validatorBytes[i*validatorHeaderBytesLength:], val.HeaderBytes()) + } + copy(header.Extra[32:], validatorBytes) + } + + state, err := chain.State() + if err != nil { + t.Fatalf("%s", err) + } + _, err = _bor.FinalizeAndAssemble(chain, header, state, nil, nil, nil) + if err != nil { + t.Fatalf("%s", err) + } + sign(t, header, signer) + return types.NewBlockWithHeader(header) +} + +func sign(t *testing.T, header *types.Header, signer []byte) { + sig, err := secp256k1.Sign(crypto.Keccak256(bor.BorRLP(header)), signer) + if err != nil { + t.Fatalf("%s", err) + } + copy(header.Extra[len(header.Extra)-extraSeal:], sig) +} + +func stateSyncEventsPayload(t *testing.T) *bor.ResponseWithHeight { + stateData, err := ioutil.ReadFile("./testdata/states.json") + if err != nil { + t.Fatalf("%s", err) + } + res := &bor.ResponseWithHeight{} + if err := json.Unmarshal(stateData, res); err != nil { + t.Fatalf("%s", err) + } + return res +} + +func loadSpanFromFile(t *testing.T) (*bor.ResponseWithHeight, *bor.HeimdallSpan) { + spanData, err := ioutil.ReadFile("./testdata/span.json") + if err != nil { + t.Fatalf("%s", err) + } + res := &bor.ResponseWithHeight{} + if err := json.Unmarshal(spanData, res); err != nil { + t.Fatalf("%s", err) + } + + heimdallSpan := &bor.HeimdallSpan{} + if err := json.Unmarshal(res.Result, heimdallSpan); err != nil { + t.Fatalf("%s", err) + } + return res, heimdallSpan +} + +func getSignerKey(number uint64) []byte { + signerKey := privKey + isSpanStart := number%spanSize == 0 + if isSpanStart { + // validator set in the new span has changed + signerKey = privKey2 + } + _key, _ := hex.DecodeString(signerKey) + return _key +} diff --git a/tests/bor/mocks/IHeimdallClient.go b/tests/bor/mocks/IHeimdallClient.go new file mode 100644 index 000000000000..d69b90862ed0 --- /dev/null +++ b/tests/bor/mocks/IHeimdallClient.go @@ -0,0 +1,82 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. + +package mocks + +import ( + bor "github.com/ethereum/go-ethereum/consensus/bor" + mock "github.com/stretchr/testify/mock" +) + +// IHeimdallClient is an autogenerated mock type for the IHeimdallClient type +type IHeimdallClient struct { + mock.Mock +} + +// Fetch provides a mock function with given fields: path, query +func (_m *IHeimdallClient) Fetch(path string, query string) (*bor.ResponseWithHeight, error) { + ret := _m.Called(path, query) + + var r0 *bor.ResponseWithHeight + if rf, ok := ret.Get(0).(func(string, string) *bor.ResponseWithHeight); ok { + r0 = rf(path, query) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*bor.ResponseWithHeight) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(string, string) error); ok { + r1 = rf(path, query) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// FetchStateSyncEvents provides a mock function with given fields: fromID, to +func (_m *IHeimdallClient) FetchStateSyncEvents(fromID uint64, to int64) ([]*bor.EventRecordWithTime, error) { + ret := _m.Called(fromID, to) + + var r0 []*bor.EventRecordWithTime + if rf, ok := ret.Get(0).(func(uint64, int64) []*bor.EventRecordWithTime); ok { + r0 = rf(fromID, to) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*bor.EventRecordWithTime) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(uint64, int64) error); ok { + r1 = rf(fromID, to) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// FetchWithRetry provides a mock function with given fields: path, query +func (_m *IHeimdallClient) FetchWithRetry(path string, query string) (*bor.ResponseWithHeight, error) { + ret := _m.Called(path, query) + + var r0 *bor.ResponseWithHeight + if rf, ok := ret.Get(0).(func(string, string) *bor.ResponseWithHeight); ok { + r0 = rf(path, query) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*bor.ResponseWithHeight) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(string, string) error); ok { + r1 = rf(path, query) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} diff --git a/tests/bor/testdata/genesis.json b/tests/bor/testdata/genesis.json new file mode 100644 index 000000000000..7c3e34bf35b1 --- /dev/null +++ b/tests/bor/testdata/genesis.json @@ -0,0 +1,47 @@ +{ + "config": { + "chainId": 15001, + "homesteadBlock": 0, + "eip150Block": 0, + "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "eip155Block": 0, + "eip158Block": 0, + "byzantiumBlock": 0, + "constantinopleBlock": 0, + "bor": { + "period": 1, + "producerDelay": 4, + "sprint": 4, + "backupMultiplier": 1, + "validatorContract": "0x0000000000000000000000000000000000001000", + "stateReceiverContract": "0x0000000000000000000000000000000000001001" + } + }, + "nonce": "0x0", + "timestamp": "0x5ce28211", + "extraData": "", + "gasLimit": "0x989680", + "difficulty": "0x1", + "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "coinbase": "0x0000000000000000000000000000000000000000", + "alloc": { + "0000000000000000000000000000000000001000": { + "balance": "0x0", + "code": "0x608060405234801561001057600080fd5b50600436106101f05760003560e01c806360c8614d1161010f578063af26aa96116100a2578063d5b844eb11610071578063d5b844eb14610666578063dcf2793a14610684578063e3b7c924146106b6578063f59cf565146106d4576101f0565b8063af26aa96146105c7578063b71d7a69146105e7578063b7ab4db514610617578063c1b3c91914610636576101f0565b806370ba5707116100de57806370ba57071461052b57806398ab2b621461055b5780639d11b80714610579578063ae756451146105a9576101f0565b806360c8614d1461049c57806365b3a1e2146104bc57806366332354146104db578063687a9bd6146104f9576101f0565b80633434735f1161018757806344d6528f1161015657806344d6528f146103ee5780634dbc959f1461041e57806355614fcc1461043c578063582a8d081461046c576101f0565b80633434735f1461035257806335ddfeea1461037057806343ee8213146103a057806344c15cb1146103be576101f0565b806323f2a73f116101c357806323f2a73f146102a45780632bc06564146102d45780632de3a180146102f25780632eddf35214610322576101f0565b8063047a6c5b146101f55780630c35b1cb146102275780631270b5741461025857806323c2a2b414610288575b600080fd5b61020f600480360361020a919081019061290d565b610706565b60405161021e939291906131ec565b60405180910390f35b610241600480360361023c919081019061290d565b61075d565b60405161024f92919061302d565b60405180910390f35b610272600480360361026d9190810190612936565b610939565b60405161027f9190613064565b60405180910390f35b6102a2600480360361029d9190810190612a15565b610a91565b005b6102be60048036036102b99190810190612936565b6110f4565b6040516102cb9190613064565b60405180910390f35b6102dc61124b565b6040516102e9919061319a565b60405180910390f35b61030c6004803603610307919081019061286a565b611250565b604051610319919061307f565b60405180910390f35b61033c6004803603610337919081019061290d565b6112d1565b604051610349919061319a565b60405180910390f35b61035a611401565b6040516103679190613012565b60405180910390f35b61038a600480360361038591908101906128a6565b611419565b6040516103979190613064565b60405180910390f35b6103a86114e4565b6040516103b5919061307f565b60405180910390f35b6103d860048036036103d39190810190612972565b6114fb565b6040516103e5919061319a565b60405180910390f35b61040860048036036104039190810190612936565b6115e3565b604051610415919061317f565b60405180910390f35b61042661174b565b604051610433919061319a565b60405180910390f35b610456600480360361045191908101906127ef565b61175b565b6040516104639190613064565b60405180910390f35b61048660048036036104819190810190612818565b611775565b604051610493919061307f565b60405180910390f35b6104a46117f3565b6040516104b3939291906131ec565b60405180910390f35b6104c4611867565b6040516104d292919061302d565b60405180910390f35b6104e3611957565b6040516104f0919061319a565b60405180910390f35b610513600480360361050e91908101906129d9565b61195c565b604051610522939291906131b5565b60405180910390f35b610545600480360361054091908101906127ef565b6119c0565b6040516105529190613064565b60405180910390f35b6105636119da565b604051610570919061307f565b60405180910390f35b610593600480360361058e919081019061290d565b6119f1565b6040516105a0919061319a565b60405180910390f35b6105b1611b22565b6040516105be919061307f565b60405180910390f35b6105cf611b39565b6040516105de939291906131ec565b60405180910390f35b61060160048036036105fc919081019061290d565b611b9a565b60405161060e919061319a565b60405180910390f35b61061f611c9a565b60405161062d92919061302d565b60405180910390f35b610650600480360361064b919081019061290d565b611cae565b60405161065d919061319a565b60405180910390f35b61066e611ccf565b60405161067b9190613223565b60405180910390f35b61069e600480360361069991908101906129d9565b611cd4565b6040516106ad939291906131b5565b60405180910390f35b6106be611d38565b6040516106cb919061319a565b60405180910390f35b6106ee60048036036106e9919081019061290d565b611d4a565b6040516106fd939291906131ec565b60405180910390f35b60008060006002600085815260200190815260200160002060000154600260008681526020019081526020016000206001015460026000878152602001908152602001600020600201549250925092509193909250565b6060806007831161077957610770611867565b91509150610934565b600061078484611b9a565b9050606060016000838152602001908152602001600020805490506040519080825280602002602001820160405280156107cd5781602001602082028038833980820191505090505b509050606060016000848152602001908152602001600020805490506040519080825280602002602001820160405280156108175781602001602082028038833980820191505090505b50905060008090505b60016000858152602001908152602001600020805490508110156109295760016000858152602001908152602001600020818154811061085c57fe5b906000526020600020906003020160020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1683828151811061089a57fe5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250506001600085815260200190815260200160002081815481106108f257fe5b90600052602060002090600302016001015482828151811061091057fe5b6020026020010181815250508080600101915050610820565b508181945094505050505b915091565b6000606060016000858152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b82821015610a0c578382906000526020600020906003020160405180606001604052908160008201548152602001600182015481526020016002820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152505081526020019060010190610970565b50505050905060008090505b8151811015610a84578373ffffffffffffffffffffffffffffffffffffffff16828281518110610a4457fe5b60200260200101516040015173ffffffffffffffffffffffffffffffffffffffff161415610a7757600192505050610a8b565b8080600101915050610a18565b5060009150505b92915050565b73fffffffffffffffffffffffffffffffffffffffe73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610add57600080fd5b6000610ae761174b565b90506000811415610afb57610afa611d74565b5b610b0f60018261209590919063ffffffff16565b8814610b50576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b47906130ff565b60405180910390fd5b868611610b92576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b899061315f565b60405180910390fd5b6000600460018989030181610ba357fe5b0614610be4576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610bdb9061313f565b60405180910390fd5b8660026000838152602001908152602001600020600101541115610c3d576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610c34906130df565b60405180910390fd5b6000600260008a81526020019081526020016000206000015414610c96576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610c8d9061311f565b60405180910390fd5b604051806060016040528089815260200188815260200187815250600260008a8152602001908152602001600020600082015181600001556020820151816001015560408201518160020155905050600388908060018154018082558091505090600182039060005260206000200160009091929091909150555060008060008a815260200190815260200160002081610d3091906125e9565b506000600160008a815260200190815260200160002081610d5191906125e9565b506060610da9610da487878080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050506120b4565b6120e2565b905060008090505b8151811015610f1b576060610dd8838381518110610dcb57fe5b60200260200101516120e2565b90506000808c81526020019081526020016000208054809190600101610dfe91906125e9565b506040518060600160405280610e2783600081518110610e1a57fe5b60200260200101516121bf565b8152602001610e4983600181518110610e3c57fe5b60200260200101516121bf565b8152602001610e6b83600281518110610e5e57fe5b6020026020010151612230565b73ffffffffffffffffffffffffffffffffffffffff168152506000808d81526020019081526020016000208381548110610ea157fe5b9060005260206000209060030201600082015181600001556020820151816001015560408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550905050508080600101915050610db1565b506060610f73610f6e86868080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050506120b4565b6120e2565b905060008090505b81518110156110e7576060610fa2838381518110610f9557fe5b60200260200101516120e2565b9050600160008d81526020019081526020016000208054809190600101610fc991906125e9565b506040518060600160405280610ff283600081518110610fe557fe5b60200260200101516121bf565b81526020016110148360018151811061100757fe5b60200260200101516121bf565b81526020016110368360028151811061102957fe5b6020026020010151612230565b73ffffffffffffffffffffffffffffffffffffffff16815250600160008e8152602001908152602001600020838154811061106d57fe5b9060005260206000209060030201600082015181600001556020820151816001015560408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550905050508080600101915050610f7b565b5050505050505050505050565b60006060600080858152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b828210156111c6578382906000526020600020906003020160405180606001604052908160008201548152602001600182015481526020016002820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815250508152602001906001019061112a565b50505050905060008090505b815181101561123e578373ffffffffffffffffffffffffffffffffffffffff168282815181106111fe57fe5b60200260200101516040015173ffffffffffffffffffffffffffffffffffffffff16141561123157600192505050611245565b80806001019150506111d2565b5060009150505b92915050565b600481565b60006002600160f81b848460405160200161126d93929190612f7f565b6040516020818303038152906040526040516112899190612fbc565b602060405180830381855afa1580156112a6573d6000803e3d6000fd5b5050506040513d601f19601f820116820180604052506112c99190810190612841565b905092915050565b60006060600080848152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b828210156113a3578382906000526020600020906003020160405180606001604052908160008201548152602001600182015481526020016002820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152505081526020019060010190611307565b505050509050600080905060008090505b82518110156113f6576113e78382815181106113cc57fe5b6020026020010151602001518361209590919063ffffffff16565b915080806001019150506113b4565b508092505050919050565b73fffffffffffffffffffffffffffffffffffffffe81565b600080600080859050600060218087518161143057fe5b0402905060008111156114495761144687611775565b91505b6000602190505b8181116114d35760006001820388015190508188015195508060006020811061147557fe5b1a60f81b9450600060f81b857effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614156114ba576114b38685611250565b93506114c7565b6114c48487611250565b93505b50602181019050611450565b508782149450505050509392505050565b6040516114f090612fe8565b604051809103902081565b60008060009050600080905060008090505b84518167ffffffffffffffff1610156115d6576060611538868367ffffffffffffffff166041612253565b9050600061154f82896122df90919063ffffffff16565b905061155961261b565b6115638a836115e3565b905061156f8a836110f4565b80156115a657508473ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16115b156115c8578194506115c581602001518761209590919063ffffffff16565b95505b50505060418101905061150d565b5081925050509392505050565b6115eb61261b565b6060600080858152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b828210156116bb578382906000526020600020906003020160405180606001604052908160008201548152602001600182015481526020016002820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815250508152602001906001019061161f565b50505050905060008090505b8151811015611743578373ffffffffffffffffffffffffffffffffffffffff168282815181106116f357fe5b60200260200101516040015173ffffffffffffffffffffffffffffffffffffffff1614156117365781818151811061172757fe5b60200260200101519250611743565b80806001019150506116c7565b505092915050565b600061175643611b9a565b905090565b600061176e61176861174b565b836110f4565b9050919050565b60006002600060f81b83604051602001611790929190612f53565b6040516020818303038152906040526040516117ac9190612fbc565b602060405180830381855afa1580156117c9573d6000803e3d6000fd5b5050506040513d601f19601f820116820180604052506117ec9190810190612841565b9050919050565b600080600080611814600161180661174b565b61209590919063ffffffff16565b905060026000828152602001908152602001600020600001546002600083815260200190815260200160002060010154600260008481526020019081526020016000206002015493509350935050909192565b6060806060600160405190808252806020026020018201604052801561189c5781602001602082028038833980820191505090505b5090507371562b71999873db5b286df957af199ec94617f7816000815181106118c157fe5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250506060600160405190808252806020026020018201604052801561192d5781602001602082028038833980820191505090505b509050600a8160008151811061193f57fe5b60200260200101818152505081819350935050509091565b600781565b6001602052816000526040600020818154811061197557fe5b9060005260206000209060030201600091509150508060000154908060010154908060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905083565b60006119d36119cd61174b565b83610939565b9050919050565b6040516119e690612fd3565b604051809103902081565b6000606060016000848152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b82821015611ac4578382906000526020600020906003020160405180606001604052908160008201548152602001600182015481526020016002820160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152505081526020019060010190611a28565b505050509050600080905060008090505b8251811015611b1757611b08838281518110611aed57fe5b6020026020010151602001518361209590919063ffffffff16565b91508080600101915050611ad5565b508092505050919050565b604051611b2e90612ffd565b604051809103902081565b600080600080611b4761174b565b905060026000828152602001908152602001600020600001546002600083815260200190815260200160002060010154600260008481526020019081526020016000206002015493509350935050909192565b60008060038054905090505b6000811115611c5a57611bb7612652565b6002600060036001850381548110611bcb57fe5b906000526020600020015481526020019081526020016000206040518060600160405290816000820154815260200160018201548152602001600282015481525050905083816020015111158015611c2857506000816040015114155b8015611c38575080604001518411155b15611c4b57806000015192505050611c95565b50808060019003915050611ba6565b5060006003805490501115611c9057600360016003805490500381548110611c7e57fe5b90600052602060002001549050611c95565b600090505b919050565b606080611ca64361075d565b915091509091565b60038181548110611cbb57fe5b906000526020600020016000915090505481565b600281565b60006020528160005260406000208181548110611ced57fe5b9060005260206000209060030201600091509150508060000154908060010154908060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905083565b600060044381611d4457fe5b04905090565b60026020528060005260406000206000915090508060000154908060010154908060020154905083565b606080611d7f611867565b809250819350505060008090506040518060600160405280828152602001600081526020016007815250600260008381526020019081526020016000206000820151816000015560208201518160010155604082015181600201559050506003819080600181540180825580915050906001820390600052602060002001600090919290919091505550600080600083815260200190815260200160002081611e2891906125e9565b5060006001600083815260200190815260200160002081611e4991906125e9565b5060008090505b8351811015611f6b576000808381526020019081526020016000208054809190600101611e7d91906125e9565b506040518060600160405280828152602001848381518110611e9b57fe5b60200260200101518152602001858381518110611eb457fe5b602002602001015173ffffffffffffffffffffffffffffffffffffffff168152506000808481526020019081526020016000208281548110611ef257fe5b9060005260206000209060030201600082015181600001556020820151816001015560408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055509050508080600101915050611e50565b5060008090505b835181101561208f57600160008381526020019081526020016000208054809190600101611fa091906125e9565b506040518060600160405280828152602001848381518110611fbe57fe5b60200260200101518152602001858381518110611fd757fe5b602002602001015173ffffffffffffffffffffffffffffffffffffffff1681525060016000848152602001908152602001600020828154811061201657fe5b9060005260206000209060030201600082015181600001556020820151816001015560408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055509050508080600101915050611f72565b50505050565b6000808284019050838110156120aa57600080fd5b8091505092915050565b6120bc612673565b600060208301905060405180604001604052808451815260200182815250915050919050565b60606120ed826123e9565b6120f657600080fd5b600061210183612437565b905060608160405190808252806020026020018201604052801561213f57816020015b61212c61268d565b8152602001906001900390816121245790505b509050600061215185602001516124a8565b8560200151019050600080600090505b848110156121b25761217283612531565b915060405180604001604052808381526020018481525084828151811061219557fe5b602002602001018190525081830192508080600101915050612161565b5082945050505050919050565b60008082600001511180156121d957506021826000015111155b6121e257600080fd5b60006121f183602001516124a8565b9050600081846000015103905060008083866020015101905080519150602083101561222457826020036101000a820491505b81945050505050919050565b6000601582600001511461224357600080fd5b61224c826121bf565b9050919050565b60608183018451101561226557600080fd5b6060821560008114612282576040519150602082016040526122d3565b6040519150601f8416801560200281840101858101878315602002848b0101015b818310156122c057805183526020830192506020810190506122a3565b50868552601f19601f8301166040525050505b50809150509392505050565b60008060008060418551146122fa57600093505050506123e3565b602085015192506040850151915060ff6041860151169050601b8160ff16101561232557601b810190505b601b8160ff161415801561233d5750601c8160ff1614155b1561234e57600093505050506123e3565b600060018783868660405160008152602001604052604051612373949392919061309a565b6020604051602081039080840390855afa158015612395573d6000803e3d6000fd5b505050602060405103519050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614156123db57600080fd5b809450505050505b92915050565b600080826000015114156124005760009050612432565b60008083602001519050805160001a915060c060ff168260ff16101561242b57600092505050612432565b6001925050505b919050565b6000808260000151141561244e57600090506124a3565b6000809050600061246284602001516124a8565b84602001510190506000846000015185602001510190505b8082101561249c5761248b82612531565b82019150828060010193505061247a565b8293505050505b919050565b600080825160001a9050608060ff168110156124c857600091505061252c565b60b860ff168110806124ed575060c060ff1681101580156124ec575060f860ff1681105b5b156124fc57600191505061252c565b60c060ff1681101561251c5760018060b80360ff1682030191505061252c565b60018060f80360ff168203019150505b919050565b6000806000835160001a9050608060ff1681101561255257600191506125df565b60b860ff1681101561256f576001608060ff1682030191506125de565b60c060ff1681101561259f5760b78103600185019450806020036101000a855104600182018101935050506125dd565b60f860ff168110156125bc57600160c060ff1682030191506125dc565b60f78103600185019450806020036101000a855104600182018101935050505b5b5b5b8192505050919050565b8154818355818111156126165760030281600302836000526020600020918201910161261591906126a7565b5b505050565b60405180606001604052806000815260200160008152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b60405180606001604052806000815260200160008152602001600081525090565b604051806040016040528060008152602001600081525090565b604051806040016040528060008152602001600081525090565b6126fa91905b808211156126f65760008082016000905560018201600090556002820160006101000a81549073ffffffffffffffffffffffffffffffffffffffff0219169055506003016126ad565b5090565b90565b60008135905061270c8161341c565b92915050565b60008135905061272181613433565b92915050565b60008151905061273681613433565b92915050565b60008083601f84011261274e57600080fd5b8235905067ffffffffffffffff81111561276757600080fd5b60208301915083600182028301111561277f57600080fd5b9250929050565b600082601f83011261279757600080fd5b81356127aa6127a58261326b565b61323e565b915080825260208301602083018583830111156127c657600080fd5b6127d18382846133c6565b50505092915050565b6000813590506127e98161344a565b92915050565b60006020828403121561280157600080fd5b600061280f848285016126fd565b91505092915050565b60006020828403121561282a57600080fd5b600061283884828501612712565b91505092915050565b60006020828403121561285357600080fd5b600061286184828501612727565b91505092915050565b6000806040838503121561287d57600080fd5b600061288b85828601612712565b925050602061289c85828601612712565b9150509250929050565b6000806000606084860312156128bb57600080fd5b60006128c986828701612712565b93505060206128da86828701612712565b925050604084013567ffffffffffffffff8111156128f757600080fd5b61290386828701612786565b9150509250925092565b60006020828403121561291f57600080fd5b600061292d848285016127da565b91505092915050565b6000806040838503121561294957600080fd5b6000612957858286016127da565b9250506020612968858286016126fd565b9150509250929050565b60008060006060848603121561298757600080fd5b6000612995868287016127da565b93505060206129a686828701612712565b925050604084013567ffffffffffffffff8111156129c357600080fd5b6129cf86828701612786565b9150509250925092565b600080604083850312156129ec57600080fd5b60006129fa858286016127da565b9250506020612a0b858286016127da565b9150509250929050565b600080600080600080600060a0888a031215612a3057600080fd5b6000612a3e8a828b016127da565b9750506020612a4f8a828b016127da565b9650506040612a608a828b016127da565b955050606088013567ffffffffffffffff811115612a7d57600080fd5b612a898a828b0161273c565b9450945050608088013567ffffffffffffffff811115612aa857600080fd5b612ab48a828b0161273c565b925092505092959891949750929550565b6000612ad18383612af5565b60208301905092915050565b6000612ae98383612f26565b60208301905092915050565b612afe8161333b565b82525050565b612b0d8161333b565b82525050565b6000612b1e826132b7565b612b2881856132f2565b9350612b3383613297565b8060005b83811015612b64578151612b4b8882612ac5565b9750612b56836132d8565b925050600181019050612b37565b5085935050505092915050565b6000612b7c826132c2565b612b868185613303565b9350612b91836132a7565b8060005b83811015612bc2578151612ba98882612add565b9750612bb4836132e5565b925050600181019050612b95565b5085935050505092915050565b612bd88161334d565b82525050565b612bef612bea82613359565b613408565b82525050565b612bfe81613385565b82525050565b612c15612c1082613385565b613412565b82525050565b6000612c26826132cd565b612c308185613314565b9350612c408185602086016133d5565b80840191505092915050565b6000612c59600483613330565b91507f766f7465000000000000000000000000000000000000000000000000000000006000830152600482019050919050565b6000612c99602d8361331f565b91507f537461727420626c6f636b206d7573742062652067726561746572207468616e60008301527f2063757272656e74207370616e000000000000000000000000000000000000006020830152604082019050919050565b6000612cff600f8361331f565b91507f496e76616c6964207370616e20696400000000000000000000000000000000006000830152602082019050919050565b6000612d3f60138361331f565b91507f5370616e20616c726561647920657869737473000000000000000000000000006000830152602082019050919050565b6000612d7f60458361331f565b91507f446966666572656e6365206265747765656e20737461727420616e6420656e6460008301527f20626c6f636b206d75737420626520696e206d756c7469706c6573206f66207360208301527f7072696e740000000000000000000000000000000000000000000000000000006040830152606082019050919050565b6000612e0b602a8361331f565b91507f456e6420626c6f636b206d7573742062652067726561746572207468616e207360008301527f7461727420626c6f636b000000000000000000000000000000000000000000006020830152604082019050919050565b6000612e71600e83613330565b91507f6865696d64616c6c2d31353030310000000000000000000000000000000000006000830152600e82019050919050565b6000612eb1600583613330565b91507f31353030310000000000000000000000000000000000000000000000000000006000830152600582019050919050565b606082016000820151612efa6000850182612f26565b506020820151612f0d6020850182612f26565b506040820151612f206040850182612af5565b50505050565b612f2f816133af565b82525050565b612f3e816133af565b82525050565b612f4d816133b9565b82525050565b6000612f5f8285612bde565b600182019150612f6f8284612c04565b6020820191508190509392505050565b6000612f8b8286612bde565b600182019150612f9b8285612c04565b602082019150612fab8284612c04565b602082019150819050949350505050565b6000612fc88284612c1b565b915081905092915050565b6000612fde82612c4c565b9150819050919050565b6000612ff382612e64565b9150819050919050565b600061300882612ea4565b9150819050919050565b60006020820190506130276000830184612b04565b92915050565b600060408201905081810360008301526130478185612b13565b9050818103602083015261305b8184612b71565b90509392505050565b60006020820190506130796000830184612bcf565b92915050565b60006020820190506130946000830184612bf5565b92915050565b60006080820190506130af6000830187612bf5565b6130bc6020830186612f44565b6130c96040830185612bf5565b6130d66060830184612bf5565b95945050505050565b600060208201905081810360008301526130f881612c8c565b9050919050565b6000602082019050818103600083015261311881612cf2565b9050919050565b6000602082019050818103600083015261313881612d32565b9050919050565b6000602082019050818103600083015261315881612d72565b9050919050565b6000602082019050818103600083015261317881612dfe565b9050919050565b60006060820190506131946000830184612ee4565b92915050565b60006020820190506131af6000830184612f35565b92915050565b60006060820190506131ca6000830186612f35565b6131d76020830185612f35565b6131e46040830184612b04565b949350505050565b60006060820190506132016000830186612f35565b61320e6020830185612f35565b61321b6040830184612f35565b949350505050565b60006020820190506132386000830184612f44565b92915050565b6000604051905081810181811067ffffffffffffffff8211171561326157600080fd5b8060405250919050565b600067ffffffffffffffff82111561328257600080fd5b601f19601f8301169050602081019050919050565b6000819050602082019050919050565b6000819050602082019050919050565b600081519050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b600082825260208201905092915050565b600082825260208201905092915050565b600081905092915050565b600082825260208201905092915050565b600081905092915050565b60006133468261338f565b9050919050565b60008115159050919050565b60007fff0000000000000000000000000000000000000000000000000000000000000082169050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600060ff82169050919050565b82818337600083830152505050565b60005b838110156133f35780820151818401526020810190506133d8565b83811115613402576000848401525b50505050565b6000819050919050565b6000819050919050565b6134258161333b565b811461343057600080fd5b50565b61343c81613385565b811461344757600080fd5b50565b613453816133af565b811461345e57600080fd5b5056fea365627a7a723158208af21d8e799fd1406e4b7808af903d8055606c3aa8d3ec80d8e21f0514e9bf676c6578706572696d656e74616cf564736f6c634300050c0040" + }, + "0000000000000000000000000000000000001001": { + "balance": "0x0", + "code": "0x608060405234801561001057600080fd5b50600436106100415760003560e01c806319494a17146100465780633434735f146100e15780635407ca671461012b575b600080fd5b6100c76004803603604081101561005c57600080fd5b81019080803590602001909291908035906020019064010000000081111561008357600080fd5b82018360208201111561009557600080fd5b803590602001918460018302840111640100000000831117156100b757600080fd5b9091929391929390505050610149565b604051808215151515815260200191505060405180910390f35b6100e9610411565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b610133610429565b6040518082815260200191505060405180910390f35b600073fffffffffffffffffffffffffffffffffffffffe73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461019757600080fd5b60606101ee6101e985858080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505061042f565b61045d565b9050600061020f8260008151811061020257fe5b602002602001015161053a565b9050806001600054011461028b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f537461746549647320617265206e6f742073657175656e7469616c000000000081525060200191505060405180910390fd5b600080815480929190600101919050555060006102bb836001815181106102ae57fe5b60200260200101516105ab565b905060606102dc846002815181106102cf57fe5b60200260200101516105ce565b90506102e78261065a565b15610406576000624c4b409050606084836040516024018083815260200180602001828103825283818151815260200191508051906020019080838360005b83811015610341578082015181840152602081019050610326565b50505050905090810190601f16801561036e5780820380516001836020036101000a031916815260200191505b5093505050506040516020818303038152906040527f26c53bea000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050905060008082516020840160008887f1965050505b505050509392505050565b73fffffffffffffffffffffffffffffffffffffffe81565b60005481565b6104376108da565b600060208301905060405180604001604052808451815260200182815250915050919050565b606061046882610673565b61047157600080fd5b600061047c836106c1565b90506060816040519080825280602002602001820160405280156104ba57816020015b6104a76108f4565b81526020019060019003908161049f5790505b50905060006104cc8560200151610732565b8560200151019050600080600090505b8481101561052d576104ed836107bb565b915060405180604001604052808381526020018481525084828151811061051057fe5b6020026020010181905250818301925080806001019150506104dc565b5082945050505050919050565b600080826000015111801561055457506021826000015111155b61055d57600080fd5b600061056c8360200151610732565b9050600081846000015103905060008083866020015101905080519150602083101561059f57826020036101000a820491505b81945050505050919050565b600060158260000151146105be57600080fd5b6105c78261053a565b9050919050565b606060008260000151116105e157600080fd5b60006105f08360200151610732565b905060008184600001510390506060816040519080825280601f01601f1916602001820160405280156106325781602001600182028038833980820191505090505b509050600081602001905061064e848760200151018285610873565b81945050505050919050565b600080823b905060008163ffffffff1611915050919050565b6000808260000151141561068a57600090506106bc565b60008083602001519050805160001a915060c060ff168260ff1610156106b5576000925050506106bc565b6001925050505b919050565b600080826000015114156106d8576000905061072d565b600080905060006106ec8460200151610732565b84602001510190506000846000015185602001510190505b8082101561072657610715826107bb565b820191508280600101935050610704565b8293505050505b919050565b600080825160001a9050608060ff168110156107525760009150506107b6565b60b860ff16811080610777575060c060ff168110158015610776575060f860ff1681105b5b156107865760019150506107b6565b60c060ff168110156107a65760018060b80360ff168203019150506107b6565b60018060f80360ff168203019150505b919050565b6000806000835160001a9050608060ff168110156107dc5760019150610869565b60b860ff168110156107f9576001608060ff168203019150610868565b60c060ff168110156108295760b78103600185019450806020036101000a85510460018201810193505050610867565b60f860ff1681101561084657600160c060ff168203019150610866565b60f78103600185019450806020036101000a855104600182018101935050505b5b5b5b8192505050919050565b6000811415610881576108d5565b5b602060ff1681106108b15782518252602060ff1683019250602060ff1682019150602060ff1681039050610882565b6000600182602060ff16036101000a03905080198451168184511681811785525050505b505050565b604051806040016040528060008152602001600081525090565b60405180604001604052806000815260200160008152509056fea265627a7a723158206e562292874be6a994dcfabfea65957791c1491194eeb7dea6f7eaf1390c036e64736f6c634300050c0032" + }, + "0000000000000000000000000000000000001010": { + "balance": "0x204fce28085b549b31600000", + "code": "0x60806040526004361061019c5760003560e01c806377d32e94116100ec578063acd06cb31161008a578063e306f77911610064578063e306f77914610a7b578063e614d0d614610aa6578063f2fde38b14610ad1578063fc0c546a14610b225761019c565b8063acd06cb31461097a578063b789543c146109cd578063cc79f97b14610a505761019c565b80639025e64c116100c65780639025e64c146107c957806395d89b4114610859578063a9059cbb146108e9578063abceeba21461094f5761019c565b806377d32e94146106315780638da5cb5b146107435780638f32d59b1461079a5761019c565b806347e7ef24116101595780637019d41a116101335780637019d41a1461053357806370a082311461058a578063715018a6146105ef578063771282f6146106065761019c565b806347e7ef2414610410578063485cc9551461046b57806360f96a8f146104dc5761019c565b806306fdde03146101a15780631499c5921461023157806318160ddd1461028257806319d27d9c146102ad5780632e1a7d4d146103b1578063313ce567146103df575b600080fd5b3480156101ad57600080fd5b506101b6610b79565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101f65780820151818401526020810190506101db565b50505050905090810190601f1680156102235780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561023d57600080fd5b506102806004803603602081101561025457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610bb6565b005b34801561028e57600080fd5b50610297610c24565b6040518082815260200191505060405180910390f35b3480156102b957600080fd5b5061036f600480360360a08110156102d057600080fd5b81019080803590602001906401000000008111156102ed57600080fd5b8201836020820111156102ff57600080fd5b8035906020019184600183028401116401000000008311171561032157600080fd5b9091929391929390803590602001909291908035906020019092919080359060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610c3a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6103dd600480360360208110156103c757600080fd5b8101908080359060200190929190505050610e06565b005b3480156103eb57600080fd5b506103f4610f58565b604051808260ff1660ff16815260200191505060405180910390f35b34801561041c57600080fd5b506104696004803603604081101561043357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610f61565b005b34801561047757600080fd5b506104da6004803603604081101561048e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061111d565b005b3480156104e857600080fd5b506104f16111ec565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561053f57600080fd5b50610548611212565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561059657600080fd5b506105d9600480360360208110156105ad57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050611238565b6040518082815260200191505060405180910390f35b3480156105fb57600080fd5b50610604611259565b005b34801561061257600080fd5b5061061b611329565b6040518082815260200191505060405180910390f35b34801561063d57600080fd5b506107016004803603604081101561065457600080fd5b81019080803590602001909291908035906020019064010000000081111561067b57600080fd5b82018360208201111561068d57600080fd5b803590602001918460018302840111640100000000831117156106af57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061132f565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561074f57600080fd5b506107586114b4565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156107a657600080fd5b506107af6114dd565b604051808215151515815260200191505060405180910390f35b3480156107d557600080fd5b506107de611534565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561081e578082015181840152602081019050610803565b50505050905090810190601f16801561084b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561086557600080fd5b5061086e61156d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156108ae578082015181840152602081019050610893565b50505050905090810190601f1680156108db5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610935600480360360408110156108ff57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506115aa565b604051808215151515815260200191505060405180910390f35b34801561095b57600080fd5b506109646115d0565b6040518082815260200191505060405180910390f35b34801561098657600080fd5b506109b36004803603602081101561099d57600080fd5b810190808035906020019092919050505061165d565b604051808215151515815260200191505060405180910390f35b3480156109d957600080fd5b50610a3a600480360360808110156109f057600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001909291908035906020019092919050505061167d565b6040518082815260200191505060405180910390f35b348015610a5c57600080fd5b50610a6561169d565b6040518082815260200191505060405180910390f35b348015610a8757600080fd5b50610a906116a3565b6040518082815260200191505060405180910390f35b348015610ab257600080fd5b50610abb6116a9565b6040518082815260200191505060405180910390f35b348015610add57600080fd5b50610b2060048036036020811015610af457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050611736565b005b348015610b2e57600080fd5b50610b37611753565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60606040518060400160405280600b81526020017f4d6174696320546f6b656e000000000000000000000000000000000000000000815250905090565b6040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260108152602001807f44697361626c656420666561747572650000000000000000000000000000000081525060200191505060405180910390fd5b6000601260ff16600a0a6402540be40002905090565b6000808511610c4857600080fd5b6000831480610c575750824311155b610cc9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f5369676e6174757265206973206578706972656400000000000000000000000081525060200191505060405180910390fd5b6000610cd73387878761167d565b9050600015156005600083815260200190815260200160002060009054906101000a900460ff16151514610d73576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252600f8152602001807f536967206465616374697661746564000000000000000000000000000000000081525060200191505060405180910390fd5b60016005600083815260200190815260200160002060006101000a81548160ff021916908315150217905550610ded8189898080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505061132f565b9150610dfa828488611779565b50509695505050505050565b60003390506000610e1682611238565b9050610e2d83600654611b3690919063ffffffff16565b600681905550600083118015610e4257508234145b610eb4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f496e73756666696369656e7420616d6f756e740000000000000000000000000081525060200191505060405180910390fd5b8173ffffffffffffffffffffffffffffffffffffffff16600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167febff2602b3f468259e1e99f613fed6691f3a6526effe6ef3e768ba7ae7a36c4f8584610f3087611238565b60405180848152602001838152602001828152602001935050505060405180910390a3505050565b60006012905090565b610f696114dd565b610f7257600080fd5b600081118015610faf5750600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614155b611004576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526023815260200180611e626023913960400191505060405180910390fd5b600061100f83611238565b905060008390508073ffffffffffffffffffffffffffffffffffffffff166108fc849081150290604051600060405180830381858888f1935050505015801561105c573d6000803e3d6000fd5b5061107283600654611b5690919063ffffffff16565b6006819055508373ffffffffffffffffffffffffffffffffffffffff16600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f4e2ca0515ed1aef1395f66b5303bb5d6f1bf9d61a353fa53f73f8ac9973fa9f685856110f489611238565b60405180848152602001838152602001828152602001935050505060405180910390a350505050565b600760009054906101000a900460ff1615611183576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526023815260200180611e3f6023913960400191505060405180910390fd5b6001600760006101000a81548160ff02191690831515021790555080600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506111e882611b75565b5050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008173ffffffffffffffffffffffffffffffffffffffff16319050919050565b6112616114dd565b61126a57600080fd5b600073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a360008060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b60065481565b600080600080604185511461134a57600093505050506114ae565b602085015192506040850151915060ff6041860151169050601b8160ff16101561137557601b810190505b601b8160ff161415801561138d5750601c8160ff1614155b1561139e57600093505050506114ae565b60018682858560405160008152602001604052604051808581526020018460ff1660ff1681526020018381526020018281526020019450505050506020604051602081039080840390855afa1580156113fb573d6000803e3d6000fd5b505050602060405103519350600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1614156114aa576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f4572726f7220696e2065637265636f766572000000000000000000000000000081525060200191505060405180910390fd5b5050505b92915050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614905090565b6040518060400160405280600281526020017f3a9900000000000000000000000000000000000000000000000000000000000081525081565b60606040518060400160405280600581526020017f4d41544943000000000000000000000000000000000000000000000000000000815250905090565b60008134146115bc57600090506115ca565b6115c7338484611779565b90505b92915050565b6040518060800160405280605b8152602001611ed7605b91396040516020018082805190602001908083835b6020831061161f57805182526020820191506020810190506020830392506115fc565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040528051906020012081565b60056020528060005260406000206000915054906101000a900460ff1681565b600061169361168e86868686611c6d565b611d43565b9050949350505050565b613a9981565b60015481565b604051806080016040528060528152602001611e85605291396040516020018082805190602001908083835b602083106116f857805182526020820191506020810190506020830392506116d5565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040528051906020012081565b61173e6114dd565b61174757600080fd5b61175081611b75565b50565b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000803073ffffffffffffffffffffffffffffffffffffffff166370a08231866040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b1580156117f957600080fd5b505afa15801561180d573d6000803e3d6000fd5b505050506040513d602081101561182357600080fd5b8101908080519060200190929190505050905060003073ffffffffffffffffffffffffffffffffffffffff166370a08231866040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b1580156118b557600080fd5b505afa1580156118c9573d6000803e3d6000fd5b505050506040513d60208110156118df57600080fd5b810190808051906020019092919050505090506118fd868686611d8d565b8473ffffffffffffffffffffffffffffffffffffffff168673ffffffffffffffffffffffffffffffffffffffff16600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167fe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c48786863073ffffffffffffffffffffffffffffffffffffffff166370a082318e6040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b158015611a0557600080fd5b505afa158015611a19573d6000803e3d6000fd5b505050506040513d6020811015611a2f57600080fd5b81019080805190602001909291905050503073ffffffffffffffffffffffffffffffffffffffff166370a082318e6040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b158015611abd57600080fd5b505afa158015611ad1573d6000803e3d6000fd5b505050506040513d6020811015611ae757600080fd5b8101908080519060200190929190505050604051808681526020018581526020018481526020018381526020018281526020019550505050505060405180910390a46001925050509392505050565b600082821115611b4557600080fd5b600082840390508091505092915050565b600080828401905083811015611b6b57600080fd5b8091505092915050565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415611baf57600080fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6000806040518060800160405280605b8152602001611ed7605b91396040516020018082805190602001908083835b60208310611cbf5780518252602082019150602081019050602083039250611c9c565b6001836020036101000a03801982511681845116808217855250505050505090500191505060405160208183030381529060405280519060200120905060405181815273ffffffffffffffffffffffffffffffffffffffff8716602082015285604082015284606082015283608082015260a0812092505081915050949350505050565b60008060015490506040517f190100000000000000000000000000000000000000000000000000000000000081528160028201528360228201526042812092505081915050919050565b8173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050158015611dd3573d6000803e3d6000fd5b508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a350505056fe54686520636f6e747261637420697320616c726561647920696e697469616c697a6564496e73756666696369656e7420616d6f756e74206f7220696e76616c69642075736572454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429546f6b656e5472616e736665724f726465722861646472657373207370656e6465722c75696e7432353620746f6b656e49644f72416d6f756e742c6279746573333220646174612c75696e743235362065787069726174696f6e29a265627a7a72315820539379fc818a23e69edb3d3a2323efa049847b7691be5bd708c0770ce4be296964736f6c634300050c0032" + }, + "71562b71999873DB5b286dF957af199Ec94617F7": { + "balance": "0x3635c9adc5dea00000" + } + }, + "number": "0x0", + "gasUsed": "0x0", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000" +} diff --git a/tests/bor/testdata/span.json b/tests/bor/testdata/span.json new file mode 100644 index 000000000000..893d3db4a6e9 --- /dev/null +++ b/tests/bor/testdata/span.json @@ -0,0 +1,67 @@ +{ + "height": "42841", + "result": { + "span_id": 1, + "start_block": 256, + "end_block": 6655, + "validator_set": { + "validators": [{ + "ID": 5, + "startEpoch": 0, + "endEpoch": 0, + "power": 30, + "pubKey": "0x04a36f6ed1f93acb0a38f4cacbe2467c72458ac41ce3b12b34d758205b2bc5d930a4e059462da7a0976c32fce766e1f7e8d73933ae72ac2af231fe161187743932", + "signer": "0x9fB29AAc15b9A4B7F17c3385939b007540f4d791", + "last_updated": 0, + "accum": 10000 + }, { + "ID": 1, + "startEpoch": 0, + "endEpoch": 0, + "power": 20, + "pubKey": "0x04a312814042a6655c8e5ecf0c52cba0b6a6f3291c87cc42260a3c0222410c0d0d59b9139d1c56542e5df0ce2fce3a86ce13e93bd9bde0dc8ff664f8dd5294dead", + "signer": "0x96C42C56fdb78294F96B0cFa33c92bed7D75F96a", + "last_updated": 0, + "accum": 10000 + }, { + "ID": 2, + "startEpoch": 0, + "endEpoch": 0, + "power": 10, + "pubKey": "0x0469536ae98030a7e83ec5ef3baffed2d05a32e31d978e58486f6bdb0fbbf240293838325116090190c0639db03f9cbd8b9aecfd269d016f46e3a2287fbf9ad232", + "signer": "0xc787af4624cb3e80ee23ae7faac0f2acea2be34c", + "last_updated": 0, + "accum": 5000 + }] + }, + "selected_producers": [{ + "ID": 5, + "startEpoch": 0, + "endEpoch": 0, + "power": 30, + "pubKey": "0x04a36f6ed1f93acb0a38f4cacbe2467c72458ac41ce3b12b34d758205b2bc5d930a4e059462da7a0976c32fce766e1f7e8d73933ae72ac2af231fe161187743932", + "signer": "0x9fB29AAc15b9A4B7F17c3385939b007540f4d791", + "last_updated": 0, + "accum": 10000 + }, { + "ID": 1, + "startEpoch": 0, + "endEpoch": 0, + "power": 20, + "pubKey": "0x04a312814042a6655c8e5ecf0c52cba0b6a6f3291c87cc42260a3c0222410c0d0d59b9139d1c56542e5df0ce2fce3a86ce13e93bd9bde0dc8ff664f8dd5294dead", + "signer": "0x96C42C56fdb78294F96B0cFa33c92bed7D75F96a", + "last_updated": 0, + "accum": 10000 + }, { + "ID": 2, + "startEpoch": 0, + "endEpoch": 0, + "power": 10, + "pubKey": "0x0469536ae98030a7e83ec5ef3baffed2d05a32e31d978e58486f6bdb0fbbf240293838325116090190c0639db03f9cbd8b9aecfd269d016f46e3a2287fbf9ad232", + "signer": "0xc787af4624cb3e80ee23ae7faac0f2acea2be34c", + "last_updated": 0, + "accum": 5000 + }], + "bor_chain_id": "15001" + } +} diff --git a/tests/bor/testdata/states.json b/tests/bor/testdata/states.json new file mode 100644 index 000000000000..809998ad906d --- /dev/null +++ b/tests/bor/testdata/states.json @@ -0,0 +1,23 @@ +{ + "height": "0", + "result": [ + { + "id": 1, + "contract": "0xb55969a6d60413a63291a1de572269875df541e3", + "data": "0x00000000000000000000000048aa8d4af32551892fcf08ad63be7dd206d46f6500000000000000000000000048aa8d4af32551892fcf08ad63be7dd206d46f65000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000014", + "tx_hash": "0x7b113e09d98b6d4be1dedfbc0746e34876de767f2cb8b58ff00160a160811dd6", + "log_index": 0, + "bor_chain_id": "15001", + "record_time": "2020-05-15T13:36:38.580995Z" + }, + { + "id": 2, + "contract": "0xb55969a6d60413a63291a1de572269875df541e3", + "data": "0x00000000000000000000000048aa8d4af32551892fcf08ad63be7dd206d46f6500000000000000000000000048aa8d4af32551892fcf08ad63be7dd206d46f65000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000015", + "tx_hash": "0xb72358aff8e4d61f4de97a37a40ddda986c081e0de8036e0a78c4b61b067cba9", + "log_index": 0, + "bor_chain_id": "15001", + "record_time": "2020-05-15T13:42:37.319058Z" + } + ] +}