aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--accounts/abi/abi.go192
-rw-r--r--accounts/abi/argument.go365
-rw-r--r--accounts/abi/bind/auth.go96
-rw-r--r--accounts/abi/bind/backend.go112
-rw-r--r--accounts/abi/bind/backends/simulated.go523
-rw-r--r--accounts/abi/bind/base.go366
-rw-r--r--accounts/abi/bind/bind.go558
-rw-r--r--accounts/abi/bind/template.go616
-rw-r--r--accounts/abi/bind/topics.go241
-rw-r--r--accounts/abi/bind/util.go76
-rw-r--r--accounts/abi/doc.go26
-rw-r--r--accounts/abi/error.go84
-rw-r--r--accounts/abi/event.go77
-rw-r--r--accounts/abi/method.go90
-rw-r--r--accounts/abi/numbers.go44
-rw-r--r--accounts/abi/pack.go81
-rw-r--r--accounts/abi/reflect.go226
-rw-r--r--accounts/abi/type.go348
-rw-r--r--accounts/abi/unpack.go295
-rw-r--r--accounts/accounts.go222
-rw-r--r--accounts/errors.go68
-rw-r--r--accounts/external/backend.go231
-rw-r--r--accounts/hd.go152
-rw-r--r--accounts/keystore/account_cache.go301
-rw-r--r--accounts/keystore/file_cache.go102
-rw-r--r--accounts/keystore/key.go232
-rw-r--r--accounts/keystore/keystore.go495
-rw-r--r--accounts/keystore/passphrase.go356
-rw-r--r--accounts/keystore/plain.go61
-rw-r--r--accounts/keystore/presale.go147
-rw-r--r--accounts/keystore/testdata/dupes/11
-rw-r--r--accounts/keystore/testdata/dupes/21
-rw-r--r--accounts/keystore/testdata/dupes/foo1
-rw-r--r--accounts/keystore/testdata/keystore/.hiddenfile1
-rw-r--r--accounts/keystore/testdata/keystore/README21
-rw-r--r--accounts/keystore/testdata/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef81
-rw-r--r--accounts/keystore/testdata/keystore/aaa1
-rw-r--r--accounts/keystore/testdata/keystore/empty0
-rw-r--r--accounts/keystore/testdata/keystore/foo/fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e1
-rw-r--r--accounts/keystore/testdata/keystore/garbagebin0 -> 300 bytes
-rw-r--r--accounts/keystore/testdata/keystore/no-address1
-rw-r--r--accounts/keystore/testdata/keystore/zero1
-rw-r--r--accounts/keystore/testdata/keystore/zzz1
-rw-r--r--accounts/keystore/testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e1
-rw-r--r--accounts/keystore/testdata/v1_test_vector.json28
-rw-r--r--accounts/keystore/testdata/v3_test_vector.json97
-rw-r--r--accounts/keystore/testdata/very-light-scrypt.json1
-rw-r--r--accounts/keystore/wallet.go148
-rw-r--r--accounts/keystore/watch.go108
-rw-r--r--accounts/keystore/watch_fallback.go28
-rw-r--r--accounts/manager.go229
-rw-r--r--accounts/scwallet/README.md102
-rw-r--r--accounts/scwallet/apdu.go87
-rw-r--r--accounts/scwallet/hub.go302
-rw-r--r--accounts/scwallet/securechannel.go346
-rw-r--r--accounts/scwallet/wallet.go1082
-rw-r--r--accounts/sort.go31
-rw-r--r--accounts/url.go104
-rw-r--r--accounts/usbwallet/hub.go279
-rw-r--r--accounts/usbwallet/ledger.go465
-rw-r--r--accounts/usbwallet/trezor.go365
-rw-r--r--accounts/usbwallet/trezor/messages-common.pb.go811
-rw-r--r--accounts/usbwallet/trezor/messages-common.proto147
-rw-r--r--accounts/usbwallet/trezor/messages-ethereum.pb.go698
-rw-r--r--accounts/usbwallet/trezor/messages-ethereum.proto131
-rw-r--r--accounts/usbwallet/trezor/messages-management.pb.go1621
-rw-r--r--accounts/usbwallet/trezor/messages-management.proto289
-rw-r--r--accounts/usbwallet/trezor/messages.pb.go889
-rw-r--r--accounts/usbwallet/trezor/messages.proto264
-rw-r--r--accounts/usbwallet/trezor/trezor.go70
-rw-r--r--accounts/usbwallet/wallet.go595
-rw-r--r--cmd/geth/chaincmd.go559
-rw-r--r--cmd/geth/config.go211
-rw-r--r--cmd/geth/consolecmd.go220
-rw-r--r--cmd/geth/main.go403
-rw-r--r--cmd/geth/misccmd.go142
-rw-r--r--cmd/geth/retesteth.go891
-rw-r--r--cmd/geth/retesteth_copypaste.go148
-rw-r--r--cmd/geth/usage.go370
-rw-r--r--cmd/utils/cmd.go314
-rw-r--r--cmd/utils/customflags.go240
-rw-r--r--cmd/utils/flags.go1700
-rw-r--r--consensus/clique/api.go119
-rw-r--r--consensus/clique/clique.go738
-rw-r--r--consensus/clique/snapshot.go326
-rw-r--r--consensus/consensus.go125
-rw-r--r--consensus/dummy/consensus.go15
-rw-r--r--consensus/errors.go37
-rw-r--r--consensus/ethash/algorithm.go1148
-rw-r--r--consensus/ethash/api.go118
-rw-r--r--consensus/ethash/consensus.go637
-rw-r--r--consensus/ethash/ethash.go717
-rw-r--r--consensus/ethash/sealer.go371
-rw-r--r--consensus/misc/dao.go85
-rw-r--r--consensus/misc/forks.go43
-rw-r--r--core/block_validator.go8
-rw-r--r--core/blockchain.go12
-rw-r--r--core/blockchain_insert.go2
-rw-r--r--core/bloombits/doc.go18
-rw-r--r--core/bloombits/generator.go93
-rw-r--r--core/bloombits/matcher.go650
-rw-r--r--core/bloombits/scheduler.go181
-rw-r--r--core/chain_indexer.go4
-rw-r--r--core/events.go2
-rw-r--r--core/evm.go60
-rw-r--r--core/genesis.go156
-rw-r--r--core/headerchain.go8
-rw-r--r--core/mkalloc.go2
-rw-r--r--core/rawdb/accessors_chain.go560
-rw-r--r--core/rawdb/accessors_indexes.go131
-rw-r--r--core/rawdb/accessors_metadata.go98
-rw-r--r--core/rawdb/database.go355
-rw-r--r--core/rawdb/freezer.go393
-rw-r--r--core/rawdb/freezer_reinit.go127
-rw-r--r--core/rawdb/freezer_table.go637
-rw-r--r--core/rawdb/schema.go166
-rw-r--r--core/rawdb/table.go204
-rw-r--r--core/state/database.go163
-rw-r--r--core/state/dump.go158
-rw-r--r--core/state/iterator.go154
-rw-r--r--core/state/journal.go245
-rw-r--r--core/state/state_object.go499
-rw-r--r--core/state/statedb.go793
-rw-r--r--core/state/sync.go42
-rw-r--r--core/state_prefetcher.go10
-rw-r--r--core/state_processor.go14
-rw-r--r--core/state_transition.go13
-rw-r--r--core/tx_cacher.go2
-rw-r--r--core/tx_journal.go2
-rw-r--r--core/tx_list.go2
-rw-r--r--core/tx_noncer.go2
-rw-r--r--core/tx_pool.go6
-rw-r--r--core/types.go6
-rw-r--r--core/types/block.go419
-rw-r--r--core/types/bloom9.go136
-rw-r--r--core/types/derive_sha.go41
-rw-r--r--core/types/gen_header_json.go138
-rw-r--r--core/types/gen_log_json.go92
-rw-r--r--core/types/gen_receipt_json.go104
-rw-r--r--core/types/gen_tx_json.go101
-rw-r--r--core/types/log.go143
-rw-r--r--core/types/receipt.go336
-rw-r--r--core/types/transaction.go440
-rw-r--r--core/types/transaction_signing.go260
-rw-r--r--core/vm/analysis.go62
-rw-r--r--core/vm/common.go99
-rw-r--r--core/vm/contract.go184
-rw-r--r--core/vm/contracts.go497
-rw-r--r--core/vm/doc.go24
-rw-r--r--core/vm/eips.go92
-rw-r--r--core/vm/errors.go31
-rw-r--r--core/vm/evm.go557
-rw-r--r--core/vm/gas.go53
-rw-r--r--core/vm/gas_table.go441
-rw-r--r--core/vm/gen_structlog.go111
-rw-r--r--core/vm/instructions.go1011
-rw-r--r--core/vm/int_pool_verifier.go31
-rw-r--r--core/vm/int_pool_verifier_empty.go23
-rw-r--r--core/vm/interface.go87
-rw-r--r--core/vm/interpreter.go300
-rw-r--r--core/vm/intpool.go106
-rw-r--r--core/vm/jump_table.go1183
-rw-r--r--core/vm/logger.go256
-rw-r--r--core/vm/logger_json.go87
-rw-r--r--core/vm/memory.go124
-rw-r--r--core/vm/memory_table.go113
-rw-r--r--core/vm/opcodes.go555
-rw-r--r--core/vm/stack.go95
-rw-r--r--core/vm/stack_table.go42
-rw-r--r--coreth.go6
-rw-r--r--eth/api.go6
-rw-r--r--eth/api_backend.go19
-rw-r--r--eth/api_tracer.go37
-rw-r--r--eth/backend.go42
-rw-r--r--eth/bloombits.go6
-rw-r--r--eth/config.go4
-rw-r--r--eth/filters/api.go4
-rw-r--r--eth/filters/filter.go4
-rw-r--r--eth/filters/filter_system.go25
-rw-r--r--eth/gasprice/gasprice.go2
-rw-r--r--eth/gen_config.go4
-rw-r--r--eth/handler.go844
-rw-r--r--eth/peer.go546
-rw-r--r--eth/protocol.go2
-rw-r--r--eth/sync.go216
-rw-r--r--eth/tracers/internal/tracers/4byte_tracer.js86
-rw-r--r--eth/tracers/internal/tracers/assets.go463
-rw-r--r--eth/tracers/internal/tracers/bigram_tracer.js47
-rw-r--r--eth/tracers/internal/tracers/call_tracer.js246
-rw-r--r--eth/tracers/internal/tracers/evmdis_tracer.js93
-rw-r--r--eth/tracers/internal/tracers/noop_tracer.js29
-rw-r--r--eth/tracers/internal/tracers/opcount_tracer.js32
-rw-r--r--eth/tracers/internal/tracers/prestate_tracer.js108
-rw-r--r--eth/tracers/internal/tracers/tracers.go21
-rw-r--r--eth/tracers/internal/tracers/trigram_tracer.js49
-rw-r--r--eth/tracers/internal/tracers/unigram_tracer.js43
-rw-r--r--eth/tracers/testdata/call_tracer_create.json58
-rw-r--r--eth/tracers/testdata/call_tracer_deep_calls.json415
-rw-r--r--eth/tracers/testdata/call_tracer_delegatecall.json97
-rw-r--r--eth/tracers/testdata/call_tracer_inner_create_oog_outer_throw.json77
-rw-r--r--eth/tracers/testdata/call_tracer_inner_throw_outer_revert.json81
-rw-r--r--eth/tracers/testdata/call_tracer_oog.json60
-rw-r--r--eth/tracers/testdata/call_tracer_revert.json58
-rw-r--r--eth/tracers/testdata/call_tracer_simple.json78
-rw-r--r--eth/tracers/testdata/call_tracer_throw.json62
-rw-r--r--eth/tracers/tracer.go641
-rw-r--r--eth/tracers/tracers.go53
-rw-r--r--ethstats/ethstats.go10
-rw-r--r--examples/chain/main.go4
-rw-r--r--examples/counter/main.go2
-rw-r--r--examples/fc/main.go16
-rw-r--r--examples/payments/main.go2
-rw-r--r--internal/ethapi/api.go58
-rw-r--r--internal/ethapi/backend.go24
-rw-r--r--miner/miner.go13
-rw-r--r--miner/unconfirmed.go2
-rw-r--r--miner/worker.go10
-rw-r--r--node/api.go184
-rw-r--r--node/config.go12
-rw-r--r--node/defaults.go2
-rw-r--r--node/node.go196
-rw-r--r--node/service.go6
-rw-r--r--params/config.go541
-rw-r--r--params/dao.go158
-rw-r--r--params/network_params.go61
-rw-r--r--params/protocol_params.go121
-rw-r--r--params/version.go67
-rw-r--r--plugin/evm/block.go2
-rw-r--r--plugin/evm/service.go2
-rw-r--r--plugin/evm/vm.go4
-rw-r--r--rpc/client.go625
-rw-r--r--rpc/client_example_test.go88
-rw-r--r--rpc/client_test.go569
-rw-r--r--rpc/constants_unix_nocgo.go25
-rw-r--r--rpc/doc.go118
-rw-r--r--rpc/errors.go65
-rw-r--r--rpc/gzip.go66
-rw-r--r--rpc/handler.go397
-rw-r--r--rpc/http.go359
-rw-r--r--rpc/http_test.go54
-rw-r--r--rpc/ipc_js.go37
-rw-r--r--rpc/json.go335
-rw-r--r--rpc/server.go147
-rw-r--r--rpc/server_test.go152
-rw-r--r--rpc/service.go285
-rw-r--r--rpc/subscription.go327
-rw-r--r--rpc/subscription_test.go206
-rw-r--r--rpc/testservice_test.go180
-rw-r--r--rpc/types.go34
-rw-r--r--rpc/types_test.go66
-rw-r--r--rpc/websocket.go175
-rw-r--r--rpc/websocket_test.go259
252 files changed, 43627 insertions, 7264 deletions
diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go
new file mode 100644
index 0000000..7af6685
--- /dev/null
+++ b/accounts/abi/abi.go
@@ -0,0 +1,192 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// The ABI holds information about a contract's context and available
+// invokable methods. It will allow you to type check function calls and
+// packs data accordingly.
+type ABI struct {
+ Constructor Method
+ Methods map[string]Method
+ Events map[string]Event
+}
+
+// JSON returns a parsed ABI interface and error if it failed.
+func JSON(reader io.Reader) (ABI, error) {
+ dec := json.NewDecoder(reader)
+
+ var abi ABI
+ if err := dec.Decode(&abi); err != nil {
+ return ABI{}, err
+ }
+
+ return abi, nil
+}
+
+// Pack the given method name to conform the ABI. Method call's data
+// will consist of method_id, args0, arg1, ... argN. Method id consists
+// of 4 bytes and arguments are all 32 bytes.
+// Method ids are created from the first 4 bytes of the hash of the
+// methods string signature. (signature = baz(uint32,string32))
+func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
+ // Fetch the ABI of the requested method
+ if name == "" {
+ // constructor
+ arguments, err := abi.Constructor.Inputs.Pack(args...)
+ if err != nil {
+ return nil, err
+ }
+ return arguments, nil
+ }
+ method, exist := abi.Methods[name]
+ if !exist {
+ return nil, fmt.Errorf("method '%s' not found", name)
+ }
+ arguments, err := method.Inputs.Pack(args...)
+ if err != nil {
+ return nil, err
+ }
+ // Pack up the method ID too if not a constructor and return
+ return append(method.ID(), arguments...), nil
+}
+
+// Unpack output in v according to the abi specification
+func (abi ABI) Unpack(v interface{}, name string, data []byte) (err error) {
+ if len(data) == 0 {
+ return fmt.Errorf("abi: unmarshalling empty output")
+ }
+ // since there can't be naming collisions with contracts and events,
+ // we need to decide whether we're calling a method or an event
+ if method, ok := abi.Methods[name]; ok {
+ if len(data)%32 != 0 {
+ return fmt.Errorf("abi: improperly formatted output: %s - Bytes: [%+v]", string(data), data)
+ }
+ return method.Outputs.Unpack(v, data)
+ }
+ if event, ok := abi.Events[name]; ok {
+ return event.Inputs.Unpack(v, data)
+ }
+ return fmt.Errorf("abi: could not locate named method or event")
+}
+
+// UnpackIntoMap unpacks a log into the provided map[string]interface{}
+func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error) {
+ if len(data) == 0 {
+ return fmt.Errorf("abi: unmarshalling empty output")
+ }
+ // since there can't be naming collisions with contracts and events,
+ // we need to decide whether we're calling a method or an event
+ if method, ok := abi.Methods[name]; ok {
+ if len(data)%32 != 0 {
+ return fmt.Errorf("abi: improperly formatted output")
+ }
+ return method.Outputs.UnpackIntoMap(v, data)
+ }
+ if event, ok := abi.Events[name]; ok {
+ return event.Inputs.UnpackIntoMap(v, data)
+ }
+ return fmt.Errorf("abi: could not locate named method or event")
+}
+
+// UnmarshalJSON implements json.Unmarshaler interface
+func (abi *ABI) UnmarshalJSON(data []byte) error {
+ var fields []struct {
+ Type string
+ Name string
+ Constant bool
+ Anonymous bool
+ Inputs []Argument
+ Outputs []Argument
+ }
+ if err := json.Unmarshal(data, &fields); err != nil {
+ return err
+ }
+ abi.Methods = make(map[string]Method)
+ abi.Events = make(map[string]Event)
+ for _, field := range fields {
+ switch field.Type {
+ case "constructor":
+ abi.Constructor = Method{
+ Inputs: field.Inputs,
+ }
+ // empty defaults to function according to the abi spec
+ case "function", "":
+ name := field.Name
+ _, ok := abi.Methods[name]
+ for idx := 0; ok; idx++ {
+ name = fmt.Sprintf("%s%d", field.Name, idx)
+ _, ok = abi.Methods[name]
+ }
+ abi.Methods[name] = Method{
+ Name: name,
+ RawName: field.Name,
+ Const: field.Constant,
+ Inputs: field.Inputs,
+ Outputs: field.Outputs,
+ }
+ case "event":
+ name := field.Name
+ _, ok := abi.Events[name]
+ for idx := 0; ok; idx++ {
+ name = fmt.Sprintf("%s%d", field.Name, idx)
+ _, ok = abi.Events[name]
+ }
+ abi.Events[name] = Event{
+ Name: name,
+ RawName: field.Name,
+ Anonymous: field.Anonymous,
+ Inputs: field.Inputs,
+ }
+ }
+ }
+
+ return nil
+}
+
+// MethodById looks up a method by the 4-byte id
+// returns nil if none found
+func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
+ if len(sigdata) < 4 {
+ return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata))
+ }
+ for _, method := range abi.Methods {
+ if bytes.Equal(method.ID(), sigdata[:4]) {
+ return &method, nil
+ }
+ }
+ return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
+}
+
+// EventByID looks an event up by its topic hash in the
+// ABI and returns nil if none found.
+func (abi *ABI) EventByID(topic common.Hash) (*Event, error) {
+ for _, event := range abi.Events {
+ if bytes.Equal(event.ID().Bytes(), topic.Bytes()) {
+ return &event, nil
+ }
+ }
+ return nil, fmt.Errorf("no event with id: %#x", topic.Hex())
+}
diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go
new file mode 100644
index 0000000..4dae586
--- /dev/null
+++ b/accounts/abi/argument.go
@@ -0,0 +1,365 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "encoding/json"
+ "fmt"
+ "reflect"
+ "strings"
+)
+
+// Argument holds the name of the argument and the corresponding type.
+// Types are used when packing and testing arguments.
+type Argument struct {
+ Name string
+ Type Type
+ Indexed bool // indexed is only used by events
+}
+
+type Arguments []Argument
+
+type ArgumentMarshaling struct {
+ Name string
+ Type string
+ Components []ArgumentMarshaling
+ Indexed bool
+}
+
+// UnmarshalJSON implements json.Unmarshaler interface
+func (argument *Argument) UnmarshalJSON(data []byte) error {
+ var arg ArgumentMarshaling
+ err := json.Unmarshal(data, &arg)
+ if err != nil {
+ return fmt.Errorf("argument json err: %v", err)
+ }
+
+ argument.Type, err = NewType(arg.Type, arg.Components)
+ if err != nil {
+ return err
+ }
+ argument.Name = arg.Name
+ argument.Indexed = arg.Indexed
+
+ return nil
+}
+
+// LengthNonIndexed returns the number of arguments when not counting 'indexed' ones. Only events
+// can ever have 'indexed' arguments, it should always be false on arguments for method input/output
+func (arguments Arguments) LengthNonIndexed() int {
+ out := 0
+ for _, arg := range arguments {
+ if !arg.Indexed {
+ out++
+ }
+ }
+ return out
+}
+
+// NonIndexed returns the arguments with indexed arguments filtered out
+func (arguments Arguments) NonIndexed() Arguments {
+ var ret []Argument
+ for _, arg := range arguments {
+ if !arg.Indexed {
+ ret = append(ret, arg)
+ }
+ }
+ return ret
+}
+
+// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[]
+func (arguments Arguments) isTuple() bool {
+ return len(arguments) > 1
+}
+
+// Unpack performs the operation hexdata -> Go format
+func (arguments Arguments) Unpack(v interface{}, data []byte) error {
+ // make sure the passed value is arguments pointer
+ if reflect.Ptr != reflect.ValueOf(v).Kind() {
+ return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
+ }
+ marshalledValues, err := arguments.UnpackValues(data)
+ if err != nil {
+ return err
+ }
+ if arguments.isTuple() {
+ return arguments.unpackTuple(v, marshalledValues)
+ }
+ return arguments.unpackAtomic(v, marshalledValues[0])
+}
+
+// UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value
+func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error {
+ marshalledValues, err := arguments.UnpackValues(data)
+ if err != nil {
+ return err
+ }
+
+ return arguments.unpackIntoMap(v, marshalledValues)
+}
+
+// unpack sets the unmarshalled value to go format.
+// Note the dst here must be settable.
+func unpack(t *Type, dst interface{}, src interface{}) error {
+ var (
+ dstVal = reflect.ValueOf(dst).Elem()
+ srcVal = reflect.ValueOf(src)
+ )
+ tuple, typ := false, t
+ for {
+ if typ.T == SliceTy || typ.T == ArrayTy {
+ typ = typ.Elem
+ continue
+ }
+ tuple = typ.T == TupleTy
+ break
+ }
+ if !tuple {
+ return set(dstVal, srcVal)
+ }
+
+ // Dereferences interface or pointer wrapper
+ dstVal = indirectInterfaceOrPtr(dstVal)
+
+ switch t.T {
+ case TupleTy:
+ if dstVal.Kind() != reflect.Struct {
+ return fmt.Errorf("abi: invalid dst value for unpack, want struct, got %s", dstVal.Kind())
+ }
+ fieldmap, err := mapArgNamesToStructFields(t.TupleRawNames, dstVal)
+ if err != nil {
+ return err
+ }
+ for i, elem := range t.TupleElems {
+ fname := fieldmap[t.TupleRawNames[i]]
+ field := dstVal.FieldByName(fname)
+ if !field.IsValid() {
+ return fmt.Errorf("abi: field %s can't found in the given value", t.TupleRawNames[i])
+ }
+ if err := unpack(elem, field.Addr().Interface(), srcVal.Field(i).Interface()); err != nil {
+ return err
+ }
+ }
+ return nil
+ case SliceTy:
+ if dstVal.Kind() != reflect.Slice {
+ return fmt.Errorf("abi: invalid dst value for unpack, want slice, got %s", dstVal.Kind())
+ }
+ slice := reflect.MakeSlice(dstVal.Type(), srcVal.Len(), srcVal.Len())
+ for i := 0; i < slice.Len(); i++ {
+ if err := unpack(t.Elem, slice.Index(i).Addr().Interface(), srcVal.Index(i).Interface()); err != nil {
+ return err
+ }
+ }
+ dstVal.Set(slice)
+ case ArrayTy:
+ if dstVal.Kind() != reflect.Array {
+ return fmt.Errorf("abi: invalid dst value for unpack, want array, got %s", dstVal.Kind())
+ }
+ array := reflect.New(dstVal.Type()).Elem()
+ for i := 0; i < array.Len(); i++ {
+ if err := unpack(t.Elem, array.Index(i).Addr().Interface(), srcVal.Index(i).Interface()); err != nil {
+ return err
+ }
+ }
+ dstVal.Set(array)
+ }
+ return nil
+}
+
+// unpackIntoMap unpacks marshalledValues into the provided map[string]interface{}
+func (arguments Arguments) unpackIntoMap(v map[string]interface{}, marshalledValues []interface{}) error {
+ // Make sure map is not nil
+ if v == nil {
+ return fmt.Errorf("abi: cannot unpack into a nil map")
+ }
+
+ for i, arg := range arguments.NonIndexed() {
+ v[arg.Name] = marshalledValues[i]
+ }
+ return nil
+}
+
+// unpackAtomic unpacks ( hexdata -> go ) a single value
+func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues interface{}) error {
+ if arguments.LengthNonIndexed() == 0 {
+ return nil
+ }
+ argument := arguments.NonIndexed()[0]
+ elem := reflect.ValueOf(v).Elem()
+
+ if elem.Kind() == reflect.Struct && argument.Type.T != TupleTy {
+ fieldmap, err := mapArgNamesToStructFields([]string{argument.Name}, elem)
+ if err != nil {
+ return err
+ }
+ field := elem.FieldByName(fieldmap[argument.Name])
+ if !field.IsValid() {
+ return fmt.Errorf("abi: field %s can't be found in the given value", argument.Name)
+ }
+ return unpack(&argument.Type, field.Addr().Interface(), marshalledValues)
+ }
+ return unpack(&argument.Type, elem.Addr().Interface(), marshalledValues)
+}
+
+// unpackTuple unpacks ( hexdata -> go ) a batch of values.
+func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interface{}) error {
+ var (
+ value = reflect.ValueOf(v).Elem()
+ typ = value.Type()
+ kind = value.Kind()
+ )
+ if err := requireUnpackKind(value, typ, kind, arguments); err != nil {
+ return err
+ }
+
+ // If the interface is a struct, get of abi->struct_field mapping
+ var abi2struct map[string]string
+ if kind == reflect.Struct {
+ var (
+ argNames []string
+ err error
+ )
+ for _, arg := range arguments.NonIndexed() {
+ argNames = append(argNames, arg.Name)
+ }
+ abi2struct, err = mapArgNamesToStructFields(argNames, value)
+ if err != nil {
+ return err
+ }
+ }
+ for i, arg := range arguments.NonIndexed() {
+ switch kind {
+ case reflect.Struct:
+ field := value.FieldByName(abi2struct[arg.Name])
+ if !field.IsValid() {
+ return fmt.Errorf("abi: field %s can't be found in the given value", arg.Name)
+ }
+ if err := unpack(&arg.Type, field.Addr().Interface(), marshalledValues[i]); err != nil {
+ return err
+ }
+ case reflect.Slice, reflect.Array:
+ if value.Len() < i {
+ return fmt.Errorf("abi: insufficient number of arguments for unpack, want %d, got %d", len(arguments), value.Len())
+ }
+ v := value.Index(i)
+ if err := requireAssignable(v, reflect.ValueOf(marshalledValues[i])); err != nil {
+ return err
+ }
+ if err := unpack(&arg.Type, v.Addr().Interface(), marshalledValues[i]); err != nil {
+ return err
+ }
+ default:
+ return fmt.Errorf("abi:[2] cannot unmarshal tuple in to %v", typ)
+ }
+ }
+ return nil
+
+}
+
+// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
+// without supplying a struct to unpack into. Instead, this method returns a list containing the
+// values. An atomic argument will be a list with one element.
+func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
+ retval := make([]interface{}, 0, arguments.LengthNonIndexed())
+ virtualArgs := 0
+ for index, arg := range arguments.NonIndexed() {
+ marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
+ if arg.Type.T == ArrayTy && !isDynamicType(arg.Type) {
+ // If we have a static array, like [3]uint256, these are coded as
+ // just like uint256,uint256,uint256.
+ // This means that we need to add two 'virtual' arguments when
+ // we count the index from now on.
+ //
+ // Array values nested multiple levels deep are also encoded inline:
+ // [2][3]uint256: uint256,uint256,uint256,uint256,uint256,uint256
+ //
+ // Calculate the full array size to get the correct offset for the next argument.
+ // Decrement it by 1, as the normal index increment is still applied.
+ virtualArgs += getTypeSize(arg.Type)/32 - 1
+ } else if arg.Type.T == TupleTy && !isDynamicType(arg.Type) {
+ // If we have a static tuple, like (uint256, bool, uint256), these are
+ // coded as just like uint256,bool,uint256
+ virtualArgs += getTypeSize(arg.Type)/32 - 1
+ }
+ if err != nil {
+ return nil, err
+ }
+ retval = append(retval, marshalledValue)
+ }
+ return retval, nil
+}
+
+// PackValues performs the operation Go format -> Hexdata
+// It is the semantic opposite of UnpackValues
+func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
+ return arguments.Pack(args...)
+}
+
+// Pack performs the operation Go format -> Hexdata
+func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
+ // Make sure arguments match up and pack them
+ abiArgs := arguments
+ if len(args) != len(abiArgs) {
+ return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs))
+ }
+ // variable input is the output appended at the end of packed
+ // output. This is used for strings and bytes types input.
+ var variableInput []byte
+
+ // input offset is the bytes offset for packed output
+ inputOffset := 0
+ for _, abiArg := range abiArgs {
+ inputOffset += getTypeSize(abiArg.Type)
+ }
+ var ret []byte
+ for i, a := range args {
+ input := abiArgs[i]
+ // pack the input
+ packed, err := input.Type.pack(reflect.ValueOf(a))
+ if err != nil {
+ return nil, err
+ }
+ // check for dynamic types
+ if isDynamicType(input.Type) {
+ // set the offset
+ ret = append(ret, packNum(reflect.ValueOf(inputOffset))...)
+ // calculate next offset
+ inputOffset += len(packed)
+ // append to variable input
+ variableInput = append(variableInput, packed...)
+ } else {
+ // append the packed value to the input
+ ret = append(ret, packed...)
+ }
+ }
+ // append the variable input at the end of the packed input
+ ret = append(ret, variableInput...)
+
+ return ret, nil
+}
+
+// ToCamelCase converts an under-score string to a camel-case string
+func ToCamelCase(input string) string {
+ parts := strings.Split(input, "_")
+ for i, s := range parts {
+ if len(s) > 0 {
+ parts[i] = strings.ToUpper(s[:1]) + s[1:]
+ }
+ }
+ return strings.Join(parts, "")
+}
diff --git a/accounts/abi/bind/auth.go b/accounts/abi/bind/auth.go
new file mode 100644
index 0000000..c916cc4
--- /dev/null
+++ b/accounts/abi/bind/auth.go
@@ -0,0 +1,96 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bind
+
+import (
+ "crypto/ecdsa"
+ "errors"
+ "io"
+ "io/ioutil"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/accounts/external"
+ "github.com/ava-labs/coreth/accounts/keystore"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// NewTransactor is a utility method to easily create a transaction signer from
+// an encrypted json key stream and the associated passphrase.
+func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
+ json, err := ioutil.ReadAll(keyin)
+ if err != nil {
+ return nil, err
+ }
+ key, err := keystore.DecryptKey(json, passphrase)
+ if err != nil {
+ return nil, err
+ }
+ return NewKeyedTransactor(key.PrivateKey), nil
+}
+
+// NewKeyStoreTransactor is a utility method to easily create a transaction signer from
+// an decrypted key from a keystore
+func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
+ return &TransactOpts{
+ From: account.Address,
+ Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
+ if address != account.Address {
+ return nil, errors.New("not authorized to sign this account")
+ }
+ signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
+ if err != nil {
+ return nil, err
+ }
+ return tx.WithSignature(signer, signature)
+ },
+ }, nil
+}
+
+// NewKeyedTransactor is a utility method to easily create a transaction signer
+// from a single private key.
+func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
+ keyAddr := crypto.PubkeyToAddress(key.PublicKey)
+ return &TransactOpts{
+ From: keyAddr,
+ Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
+ if address != keyAddr {
+ return nil, errors.New("not authorized to sign this account")
+ }
+ signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
+ if err != nil {
+ return nil, err
+ }
+ return tx.WithSignature(signer, signature)
+ },
+ }
+}
+
+// NewClefTransactor is a utility method to easily create a transaction signer
+// with a clef backend.
+func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
+ return &TransactOpts{
+ From: account.Address,
+ Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
+ if address != account.Address {
+ return nil, errors.New("not authorized to sign this account")
+ }
+ return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
+ },
+ }
+}
diff --git a/accounts/abi/bind/backend.go b/accounts/abi/bind/backend.go
new file mode 100644
index 0000000..556394f
--- /dev/null
+++ b/accounts/abi/bind/backend.go
@@ -0,0 +1,112 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bind
+
+import (
+ "context"
+ "errors"
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+var (
+ // ErrNoCode is returned by call and transact operations for which the requested
+ // recipient contract to operate on does not exist in the state db or does not
+ // have any code associated with it (i.e. suicided).
+ ErrNoCode = errors.New("no contract code at given address")
+
+ // This error is raised when attempting to perform a pending state action
+ // on a backend that doesn't implement PendingContractCaller.
+ ErrNoPendingState = errors.New("backend does not support pending state")
+
+ // This error is returned by WaitDeployed if contract creation leaves an
+ // empty contract behind.
+ ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
+)
+
+// ContractCaller defines the methods needed to allow operating with contract on a read
+// only basis.
+type ContractCaller interface {
+ // CodeAt returns the code of the given account. This is needed to differentiate
+ // between contract internal errors and the local chain being out of sync.
+ CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
+ // ContractCall executes an Ethereum contract call with the specified data as the
+ // input.
+ CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
+}
+
+// PendingContractCaller defines methods to perform contract calls on the pending state.
+// Call will try to discover this interface when access to the pending state is requested.
+// If the backend does not support the pending state, Call returns ErrNoPendingState.
+type PendingContractCaller interface {
+ // PendingCodeAt returns the code of the given account in the pending state.
+ PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error)
+ // PendingCallContract executes an Ethereum contract call against the pending state.
+ PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
+}
+
+// ContractTransactor defines the methods needed to allow operating with contract
+// on a write only basis. Beside the transacting method, the remainder are helpers
+// used when the user does not provide some needed values, but rather leaves it up
+// to the transactor to decide.
+type ContractTransactor interface {
+ // PendingCodeAt returns the code of the given account in the pending state.
+ PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
+ // PendingNonceAt retrieves the current pending nonce associated with an account.
+ PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
+ // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
+ // execution of a transaction.
+ SuggestGasPrice(ctx context.Context) (*big.Int, error)
+ // EstimateGas tries to estimate the gas needed to execute a specific
+ // transaction based on the current pending state of the backend blockchain.
+ // There is no guarantee that this is the true gas limit requirement as other
+ // transactions may be added or removed by miners, but it should provide a basis
+ // for setting a reasonable default.
+ EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
+ // SendTransaction injects the transaction into the pending pool for execution.
+ SendTransaction(ctx context.Context, tx *types.Transaction) error
+}
+
+// ContractFilterer defines the methods needed to access log events using one-off
+// queries or continuous event subscriptions.
+type ContractFilterer interface {
+ // FilterLogs executes a log filter operation, blocking during execution and
+ // returning all the results in one batch.
+ //
+ // TODO(karalabe): Deprecate when the subscription one can return past data too.
+ FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error)
+
+ // SubscribeFilterLogs creates a background log filtering operation, returning
+ // a subscription immediately, which can be used to stream the found events.
+ SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
+}
+
+// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
+type DeployBackend interface {
+ TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
+ CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
+}
+
+// ContractBackend defines the methods needed to work with contracts on a read-write basis.
+type ContractBackend interface {
+ ContractCaller
+ ContractTransactor
+ ContractFilterer
+}
diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go
new file mode 100644
index 0000000..88610a4
--- /dev/null
+++ b/accounts/abi/bind/backends/simulated.go
@@ -0,0 +1,523 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package backends
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "math/big"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts/abi/bind"
+ "github.com/ava-labs/coreth/consensus/ethash"
+ "github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/bloombits"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+ "github.com/ava-labs/go-ethereum/eth/filters"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/event"
+)
+
+// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
+var _ bind.ContractBackend = (*SimulatedBackend)(nil)
+
+var (
+ errBlockNumberUnsupported = errors.New("simulatedBackend cannot access blocks other than the latest block")
+ errGasEstimationFailed = errors.New("gas required exceeds allowance or always failing transaction")
+)
+
+// SimulatedBackend implements bind.ContractBackend, simulating a blockchain in
+// the background. Its main purpose is to allow easily testing contract bindings.
+type SimulatedBackend struct {
+ database ethdb.Database // In memory database to store our testing data
+ blockchain *core.BlockChain // Ethereum blockchain to handle the consensus
+
+ mu sync.Mutex
+ pendingBlock *types.Block // Currently pending block that will be imported on request
+ pendingState *state.StateDB // Currently pending state that will be the active on on request
+
+ events *filters.EventSystem // Event system for filtering log events live
+
+ config *params.ChainConfig
+}
+
+// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
+// and uses a simulated blockchain for testing purposes.
+func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
+ genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
+ genesis.MustCommit(database)
+ blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{}, nil)
+
+ backend := &SimulatedBackend{
+ database: database,
+ blockchain: blockchain,
+ config: genesis.Config,
+ events: filters.NewEventSystem(new(event.TypeMux), &filterBackend{database, blockchain}, false),
+ }
+ backend.rollback()
+ return backend
+}
+
+// NewSimulatedBackend creates a new binding backend using a simulated blockchain
+// for testing purposes.
+func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
+ return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit)
+}
+
+// Close terminates the underlying blockchain's update loop.
+func (b *SimulatedBackend) Close() error {
+ b.blockchain.Stop()
+ return nil
+}
+
+// Commit imports all the pending transactions as a single block and starts a
+// fresh new state.
+func (b *SimulatedBackend) Commit() {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ if _, err := b.blockchain.InsertChain([]*types.Block{b.pendingBlock}); err != nil {
+ panic(err) // This cannot happen unless the simulator is wrong, fail in that case
+ }
+ b.rollback()
+}
+
+// Rollback aborts all pending transactions, reverting to the last committed state.
+func (b *SimulatedBackend) Rollback() {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ b.rollback()
+}
+
+func (b *SimulatedBackend) rollback() {
+ blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(int, *core.BlockGen) {})
+ statedb, _ := b.blockchain.State()
+
+ b.pendingBlock = blocks[0]
+ b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
+}
+
+// CodeAt returns the code associated with a certain account in the blockchain.
+func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
+ return nil, errBlockNumberUnsupported
+ }
+ statedb, _ := b.blockchain.State()
+ return statedb.GetCode(contract), nil
+}
+
+// BalanceAt returns the wei balance of a certain account in the blockchain.
+func (b *SimulatedBackend) BalanceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (*big.Int, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
+ return nil, errBlockNumberUnsupported
+ }
+ statedb, _ := b.blockchain.State()
+ return statedb.GetBalance(contract), nil
+}
+
+// NonceAt returns the nonce of a certain account in the blockchain.
+func (b *SimulatedBackend) NonceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (uint64, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
+ return 0, errBlockNumberUnsupported
+ }
+ statedb, _ := b.blockchain.State()
+ return statedb.GetNonce(contract), nil
+}
+
+// StorageAt returns the value of key in the storage of an account in the blockchain.
+func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
+ return nil, errBlockNumberUnsupported
+ }
+ statedb, _ := b.blockchain.State()
+ val := statedb.GetState(contract, key)
+ return val[:], nil
+}
+
+// TransactionReceipt returns the receipt of a transaction.
+func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
+ receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash, b.config)
+ return receipt, nil
+}
+
+// TransactionByHash checks the pool of pending transactions in addition to the
+// blockchain. The isPending return value indicates whether the transaction has been
+// mined yet. Note that the transaction may not be part of the canonical chain even if
+// it's not pending.
+func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ tx := b.pendingBlock.Transaction(txHash)
+ if tx != nil {
+ return tx, true, nil
+ }
+ tx, _, _, _ = rawdb.ReadTransaction(b.database, txHash)
+ if tx != nil {
+ return tx, false, nil
+ }
+ return nil, false, ethereum.NotFound
+}
+
+// PendingCodeAt returns the code associated with an account in the pending state.
+func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ return b.pendingState.GetCode(contract), nil
+}
+
+// CallContract executes a contract call.
+func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 {
+ return nil, errBlockNumberUnsupported
+ }
+ state, err := b.blockchain.State()
+ if err != nil {
+ return nil, err
+ }
+ rval, _, _, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), state)
+ return rval, err
+}
+
+// PendingCallContract executes a contract call on the pending state.
+func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot())
+
+ rval, _, _, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
+ return rval, err
+}
+
+// PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving
+// the nonce currently pending for the account.
+func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ return b.pendingState.GetOrNewStateObject(account).Nonce(), nil
+}
+
+// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
+// chain doesn't have miners, we just return a gas price of 1 for any call.
+func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
+ return big.NewInt(1), nil
+}
+
+// EstimateGas executes the requested code against the currently pending block/state and
+// returns the used amount of gas.
+func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ // Determine the lowest and highest possible gas limits to binary search in between
+ var (
+ lo uint64 = params.TxGas - 1
+ hi uint64
+ cap uint64
+ )
+ if call.Gas >= params.TxGas {
+ hi = call.Gas
+ } else {
+ hi = b.pendingBlock.GasLimit()
+ }
+ cap = hi
+
+ // Create a helper to check if a gas allowance results in an executable transaction
+ executable := func(gas uint64) bool {
+ call.Gas = gas
+
+ snapshot := b.pendingState.Snapshot()
+ _, _, failed, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
+ b.pendingState.RevertToSnapshot(snapshot)
+
+ if err != nil || failed {
+ return false
+ }
+ return true
+ }
+ // Execute the binary search and hone in on an executable gas limit
+ for lo+1 < hi {
+ mid := (hi + lo) / 2
+ if !executable(mid) {
+ lo = mid
+ } else {
+ hi = mid
+ }
+ }
+ // Reject the transaction as invalid if it still fails at the highest allowance
+ if hi == cap {
+ if !executable(hi) {
+ return 0, errGasEstimationFailed
+ }
+ }
+ return hi, nil
+}
+
+// callContract implements common code between normal and pending contract calls.
+// state is modified during execution, make sure to copy it if necessary.
+func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, statedb *state.StateDB) ([]byte, uint64, bool, error) {
+ // Ensure message is initialized properly.
+ if call.GasPrice == nil {
+ call.GasPrice = big.NewInt(1)
+ }
+ if call.Gas == 0 {
+ call.Gas = 50000000
+ }
+ if call.Value == nil {
+ call.Value = new(big.Int)
+ }
+ // Set infinite balance to the fake caller account.
+ from := statedb.GetOrNewStateObject(call.From)
+ from.SetBalance(math.MaxBig256)
+ // Execute the call.
+ msg := callmsg{call}
+
+ evmContext := core.NewEVMContext(msg, block.Header(), b.blockchain, nil)
+ // Create a new environment which holds all relevant information
+ // about the transaction and calling mechanisms.
+ vmenv := vm.NewEVM(evmContext, statedb, b.config, vm.Config{})
+ gaspool := new(core.GasPool).AddGas(math.MaxUint64)
+
+ return core.NewStateTransition(vmenv, msg, gaspool).TransitionDb()
+}
+
+// SendTransaction updates the pending block to include the given transaction.
+// It panics if the transaction is invalid.
+func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+
+ sender, err := types.Sender(types.NewEIP155Signer(b.config.ChainID), tx)
+ if err != nil {
+ panic(fmt.Errorf("invalid transaction: %v", err))
+ }
+ nonce := b.pendingState.GetNonce(sender)
+ if tx.Nonce() != nonce {
+ panic(fmt.Errorf("invalid transaction nonce: got %d, want %d", tx.Nonce(), nonce))
+ }
+
+ blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
+ for _, tx := range b.pendingBlock.Transactions() {
+ block.AddTxWithChain(b.blockchain, tx)
+ }
+ block.AddTxWithChain(b.blockchain, tx)
+ })
+ statedb, _ := b.blockchain.State()
+
+ b.pendingBlock = blocks[0]
+ b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
+ return nil
+}
+
+// FilterLogs executes a log filter operation, blocking during execution and
+// returning all the results in one batch.
+//
+// TODO(karalabe): Deprecate when the subscription one can return past data too.
+func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
+ var filter *filters.Filter
+ if query.BlockHash != nil {
+ // Block filter requested, construct a single-shot filter
+ filter = filters.NewBlockFilter(&filterBackend{b.database, b.blockchain}, *query.BlockHash, query.Addresses, query.Topics)
+ } else {
+ // Initialize unset filter boundaried to run from genesis to chain head
+ from := int64(0)
+ if query.FromBlock != nil {
+ from = query.FromBlock.Int64()
+ }
+ to := int64(-1)
+ if query.ToBlock != nil {
+ to = query.ToBlock.Int64()
+ }
+ // Construct the range filter
+ filter = filters.NewRangeFilter(&filterBackend{b.database, b.blockchain}, from, to, query.Addresses, query.Topics)
+ }
+ // Run the filter and return all the logs
+ logs, err := filter.Logs(ctx)
+ if err != nil {
+ return nil, err
+ }
+ res := make([]types.Log, len(logs))
+ for i, log := range logs {
+ res[i] = *log
+ }
+ return res, nil
+}
+
+// SubscribeFilterLogs creates a background log filtering operation, returning a
+// subscription immediately, which can be used to stream the found events.
+func (b *SimulatedBackend) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
+ // Subscribe to contract events
+ sink := make(chan []*types.Log)
+
+ sub, err := b.events.SubscribeLogs(query, sink)
+ if err != nil {
+ return nil, err
+ }
+ // Since we're getting logs in batches, we need to flatten them into a plain stream
+ return event.NewSubscription(func(quit <-chan struct{}) error {
+ defer sub.Unsubscribe()
+ for {
+ select {
+ case logs := <-sink:
+ for _, log := range logs {
+ select {
+ case ch <- *log:
+ case err := <-sub.Err():
+ return err
+ case <-quit:
+ return nil
+ }
+ }
+ case err := <-sub.Err():
+ return err
+ case <-quit:
+ return nil
+ }
+ }
+ }), nil
+}
+
+// AdjustTime adds a time shift to the simulated clock.
+func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
+ for _, tx := range b.pendingBlock.Transactions() {
+ block.AddTx(tx)
+ }
+ block.OffsetTime(int64(adjustment.Seconds()))
+ })
+ statedb, _ := b.blockchain.State()
+
+ b.pendingBlock = blocks[0]
+ b.pendingState, _ = state.New(b.pendingBlock.Root(), statedb.Database())
+
+ return nil
+}
+
+// Blockchain returns the underlying blockchain.
+func (b *SimulatedBackend) Blockchain() *core.BlockChain {
+ return b.blockchain
+}
+
+// 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 }
+
+// filterBackend implements filters.Backend to support filtering for logs without
+// taking bloom-bits acceleration structures into account.
+type filterBackend struct {
+ db ethdb.Database
+ bc *core.BlockChain
+}
+
+func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db }
+func (fb *filterBackend) EventMux() *event.TypeMux { panic("not supported") }
+
+func (fb *filterBackend) HeaderByNumber(ctx context.Context, block rpc.BlockNumber) (*types.Header, error) {
+ if block == rpc.LatestBlockNumber {
+ return fb.bc.CurrentHeader(), nil
+ }
+ return fb.bc.GetHeaderByNumber(uint64(block.Int64())), nil
+}
+
+func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
+ return fb.bc.GetHeaderByHash(hash), nil
+}
+
+func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
+ number := rawdb.ReadHeaderNumber(fb.db, hash)
+ if number == nil {
+ return nil, nil
+ }
+ return rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config()), nil
+}
+
+func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
+ number := rawdb.ReadHeaderNumber(fb.db, hash)
+ if number == nil {
+ return nil, nil
+ }
+ receipts := rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config())
+ if receipts == nil {
+ return nil, nil
+ }
+ logs := make([][]*types.Log, len(receipts))
+ for i, receipt := range receipts {
+ logs[i] = receipt.Logs
+ }
+ return logs, nil
+}
+
+func (fb *filterBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
+ return event.NewSubscription(func(quit <-chan struct{}) error {
+ <-quit
+ return nil
+ })
+}
+func (fb *filterBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
+ return fb.bc.SubscribeChainEvent(ch)
+}
+func (fb *filterBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
+ return fb.bc.SubscribeRemovedLogsEvent(ch)
+}
+func (fb *filterBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
+ return fb.bc.SubscribeLogsEvent(ch)
+}
+
+func (fb *filterBackend) BloomStatus() (uint64, uint64) { return 4096, 0 }
+func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.MatcherSession) {
+ panic("not supported")
+}
diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go
new file mode 100644
index 0000000..3d64f85
--- /dev/null
+++ b/accounts/abi/bind/base.go
@@ -0,0 +1,366 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bind
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "math/big"
+
+ "github.com/ava-labs/coreth/accounts/abi"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/event"
+)
+
+// SignerFn is a signer function callback when a contract requires a method to
+// sign the transaction before submission.
+type SignerFn func(types.Signer, common.Address, *types.Transaction) (*types.Transaction, error)
+
+// CallOpts is the collection of options to fine tune a contract call request.
+type CallOpts struct {
+ Pending bool // Whether to operate on the pending state or the last known one
+ From common.Address // Optional the sender address, otherwise the first account is used
+ BlockNumber *big.Int // Optional the block number on which the call should be performed
+ Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
+}
+
+// TransactOpts is the collection of authorization data required to create a
+// valid Ethereum transaction.
+type TransactOpts struct {
+ From common.Address // Ethereum account to send the transaction from
+ Nonce *big.Int // Nonce to use for the transaction execution (nil = use pending state)
+ Signer SignerFn // Method to use for signing the transaction (mandatory)
+
+ Value *big.Int // Funds to transfer along along the transaction (nil = 0 = no funds)
+ GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
+ GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
+
+ Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
+}
+
+// FilterOpts is the collection of options to fine tune filtering for events
+// within a bound contract.
+type FilterOpts struct {
+ Start uint64 // Start of the queried range
+ End *uint64 // End of the range (nil = latest)
+
+ Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
+}
+
+// WatchOpts is the collection of options to fine tune subscribing for events
+// within a bound contract.
+type WatchOpts struct {
+ Start *uint64 // Start of the queried range (nil = latest)
+ Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
+}
+
+// BoundContract is the base wrapper object that reflects a contract on the
+// Ethereum network. It contains a collection of methods that are used by the
+// higher level contract bindings to operate.
+type BoundContract struct {
+ address common.Address // Deployment address of the contract on the Ethereum blockchain
+ abi abi.ABI // Reflect based ABI to access the correct Ethereum methods
+ caller ContractCaller // Read interface to interact with the blockchain
+ transactor ContractTransactor // Write interface to interact with the blockchain
+ filterer ContractFilterer // Event filtering to interact with the blockchain
+}
+
+// NewBoundContract creates a low level contract interface through which calls
+// and transactions may be made through.
+func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor, filterer ContractFilterer) *BoundContract {
+ return &BoundContract{
+ address: address,
+ abi: abi,
+ caller: caller,
+ transactor: transactor,
+ filterer: filterer,
+ }
+}
+
+// DeployContract deploys a contract onto the Ethereum blockchain and binds the
+// deployment address with a Go wrapper.
+func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (common.Address, *types.Transaction, *BoundContract, error) {
+ // Otherwise try to deploy the contract
+ c := NewBoundContract(common.Address{}, abi, backend, backend, backend)
+
+ input, err := c.abi.Pack("", params...)
+ if err != nil {
+ return common.Address{}, nil, nil, err
+ }
+ tx, err := c.transact(opts, nil, append(bytecode, input...))
+ if err != nil {
+ return common.Address{}, nil, nil, err
+ }
+ c.address = crypto.CreateAddress(opts.From, tx.Nonce())
+ return c.address, tx, c, nil
+}
+
+// Call invokes the (constant) contract method with params as input values and
+// sets the output to result. The result type might be a single field for simple
+// returns, a slice of interfaces for anonymous returns and a struct for named
+// returns.
+func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string, params ...interface{}) error {
+ // Don't crash on a lazy user
+ if opts == nil {
+ opts = new(CallOpts)
+ }
+ // Pack the input, call and unpack the results
+ input, err := c.abi.Pack(method, params...)
+ if err != nil {
+ return err
+ }
+ var (
+ msg = ethereum.CallMsg{From: opts.From, To: &c.address, Data: input}
+ ctx = ensureContext(opts.Context)
+ code []byte
+ output []byte
+ )
+ if opts.Pending {
+ pb, ok := c.caller.(PendingContractCaller)
+ if !ok {
+ return ErrNoPendingState
+ }
+ output, err = pb.PendingCallContract(ctx, msg)
+ if err == nil && len(output) == 0 {
+ // Make sure we have a contract to operate on, and bail out otherwise.
+ if code, err = pb.PendingCodeAt(ctx, c.address); err != nil {
+ return err
+ } else if len(code) == 0 {
+ return ErrNoCode
+ }
+ }
+ } else {
+ output, err = c.caller.CallContract(ctx, msg, opts.BlockNumber)
+ if err == nil && len(output) == 0 {
+ // Make sure we have a contract to operate on, and bail out otherwise.
+ if code, err = c.caller.CodeAt(ctx, c.address, opts.BlockNumber); err != nil {
+ return err
+ } else if len(code) == 0 {
+ return ErrNoCode
+ }
+ }
+ }
+ if err != nil {
+ return err
+ }
+ return c.abi.Unpack(result, method, output)
+}
+
+// Transact invokes the (paid) contract method with params as input values.
+func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
+ // Otherwise pack up the parameters and invoke the contract
+ input, err := c.abi.Pack(method, params...)
+ if err != nil {
+ return nil, err
+ }
+ return c.transact(opts, &c.address, input)
+}
+
+// Transfer initiates a plain transaction to move funds to the contract, calling
+// its default method if one is available.
+func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error) {
+ return c.transact(opts, &c.address, nil)
+}
+
+// transact executes an actual transaction invocation, first deriving any missing
+// authorization fields, and then scheduling the transaction for execution.
+func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
+ var err error
+
+ // Ensure a valid value field and resolve the account nonce
+ value := opts.Value
+ if value == nil {
+ value = new(big.Int)
+ }
+ var nonce uint64
+ if opts.Nonce == nil {
+ nonce, err = c.transactor.PendingNonceAt(ensureContext(opts.Context), opts.From)
+ if err != nil {
+ return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
+ }
+ } else {
+ nonce = opts.Nonce.Uint64()
+ }
+ // Figure out the gas allowance and gas price values
+ gasPrice := opts.GasPrice
+ if gasPrice == nil {
+ gasPrice, err = c.transactor.SuggestGasPrice(ensureContext(opts.Context))
+ if err != nil {
+ return nil, fmt.Errorf("failed to suggest gas price: %v", err)
+ }
+ }
+ gasLimit := opts.GasLimit
+ if gasLimit == 0 {
+ // Gas estimation cannot succeed without code for method invocations
+ if contract != nil {
+ if code, err := c.transactor.PendingCodeAt(ensureContext(opts.Context), c.address); err != nil {
+ return nil, err
+ } else if len(code) == 0 {
+ return nil, ErrNoCode
+ }
+ }
+ // If the contract surely has code (or code is not needed), estimate the transaction
+ msg := ethereum.CallMsg{From: opts.From, To: contract, Value: value, Data: input}
+ gasLimit, err = c.transactor.EstimateGas(ensureContext(opts.Context), msg)
+ if err != nil {
+ return nil, fmt.Errorf("failed to estimate gas needed: %v", err)
+ }
+ }
+ // Create the transaction, sign it and schedule it for execution
+ var rawTx *types.Transaction
+ if contract == nil {
+ rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input)
+ } else {
+ rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input)
+ }
+ if opts.Signer == nil {
+ return nil, errors.New("no signer to authorize the transaction with")
+ }
+ signedTx, err := opts.Signer(types.HomesteadSigner{}, opts.From, rawTx)
+ if err != nil {
+ return nil, err
+ }
+ if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx); err != nil {
+ return nil, err
+ }
+ return signedTx, nil
+}
+
+// FilterLogs filters contract logs for past blocks, returning the necessary
+// channels to construct a strongly typed bound iterator on top of them.
+func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
+ // Don't crash on a lazy user
+ if opts == nil {
+ opts = new(FilterOpts)
+ }
+ // Append the event selector to the query parameters and construct the topic set
+ query = append([][]interface{}{{c.abi.Events[name].ID()}}, query...)
+
+ topics, err := makeTopics(query...)
+ if err != nil {
+ return nil, nil, err
+ }
+ // Start the background filtering
+ logs := make(chan types.Log, 128)
+
+ config := ethereum.FilterQuery{
+ Addresses: []common.Address{c.address},
+ Topics: topics,
+ FromBlock: new(big.Int).SetUint64(opts.Start),
+ }
+ if opts.End != nil {
+ config.ToBlock = new(big.Int).SetUint64(*opts.End)
+ }
+ /* TODO(karalabe): Replace the rest of the method below with this when supported
+ sub, err := c.filterer.SubscribeFilterLogs(ensureContext(opts.Context), config, logs)
+ */
+ buff, err := c.filterer.FilterLogs(ensureContext(opts.Context), config)
+ if err != nil {
+ return nil, nil, err
+ }
+ sub, err := event.NewSubscription(func(quit <-chan struct{}) error {
+ for _, log := range buff {
+ select {
+ case logs <- log:
+ case <-quit:
+ return nil
+ }
+ }
+ return nil
+ }), nil
+
+ if err != nil {
+ return nil, nil, err
+ }
+ return logs, sub, nil
+}
+
+// WatchLogs filters subscribes to contract logs for future blocks, returning a
+// subscription object that can be used to tear down the watcher.
+func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
+ // Don't crash on a lazy user
+ if opts == nil {
+ opts = new(WatchOpts)
+ }
+ // Append the event selector to the query parameters and construct the topic set
+ query = append([][]interface{}{{c.abi.Events[name].ID()}}, query...)
+
+ topics, err := makeTopics(query...)
+ if err != nil {
+ return nil, nil, err
+ }
+ // Start the background filtering
+ logs := make(chan types.Log, 128)
+
+ config := ethereum.FilterQuery{
+ Addresses: []common.Address{c.address},
+ Topics: topics,
+ }
+ if opts.Start != nil {
+ config.FromBlock = new(big.Int).SetUint64(*opts.Start)
+ }
+ sub, err := c.filterer.SubscribeFilterLogs(ensureContext(opts.Context), config, logs)
+ if err != nil {
+ return nil, nil, err
+ }
+ return logs, sub, nil
+}
+
+// UnpackLog unpacks a retrieved log into the provided output structure.
+func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error {
+ if len(log.Data) > 0 {
+ if err := c.abi.Unpack(out, event, log.Data); err != nil {
+ return err
+ }
+ }
+ var indexed abi.Arguments
+ for _, arg := range c.abi.Events[event].Inputs {
+ if arg.Indexed {
+ indexed = append(indexed, arg)
+ }
+ }
+ return parseTopics(out, indexed, log.Topics[1:])
+}
+
+// UnpackLogIntoMap unpacks a retrieved log into the provided map.
+func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error {
+ if len(log.Data) > 0 {
+ if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil {
+ return err
+ }
+ }
+ var indexed abi.Arguments
+ for _, arg := range c.abi.Events[event].Inputs {
+ if arg.Indexed {
+ indexed = append(indexed, arg)
+ }
+ }
+ return parseTopicsIntoMap(out, indexed, log.Topics[1:])
+}
+
+// ensureContext is a helper method to ensure a context is not nil, even if the
+// user specified it as such.
+func ensureContext(ctx context.Context) context.Context {
+ if ctx == nil {
+ return context.TODO()
+ }
+ return ctx
+}
diff --git a/accounts/abi/bind/bind.go b/accounts/abi/bind/bind.go
new file mode 100644
index 0000000..e869eef
--- /dev/null
+++ b/accounts/abi/bind/bind.go
@@ -0,0 +1,558 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package bind generates Ethereum contract Go bindings.
+//
+// Detailed usage document and tutorial available on the go-ethereum Wiki page:
+// https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts
+package bind
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "go/format"
+ "regexp"
+ "strings"
+ "text/template"
+ "unicode"
+
+ "github.com/ava-labs/coreth/accounts/abi"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+// Lang is a target programming language selector to generate bindings for.
+type Lang int
+
+const (
+ LangGo Lang = iota
+ LangJava
+ LangObjC
+)
+
+// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
+// to be used as is in client code, but rather as an intermediate struct which
+// enforces compile time type safety and naming convention opposed to having to
+// manually maintain hard coded strings that break on runtime.
+func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string) (string, error) {
+ // Process each individual contract requested binding
+ contracts := make(map[string]*tmplContract)
+
+ // Map used to flag each encountered library as such
+ isLib := make(map[string]struct{})
+
+ for i := 0; i < len(types); i++ {
+ // Parse the actual ABI to generate the binding for
+ evmABI, err := abi.JSON(strings.NewReader(abis[i]))
+ if err != nil {
+ return "", err
+ }
+ // Strip any whitespace from the JSON ABI
+ strippedABI := strings.Map(func(r rune) rune {
+ if unicode.IsSpace(r) {
+ return -1
+ }
+ return r
+ }, abis[i])
+
+ // Extract the call and transact methods; events, struct definitions; and sort them alphabetically
+ var (
+ calls = make(map[string]*tmplMethod)
+ transacts = make(map[string]*tmplMethod)
+ events = make(map[string]*tmplEvent)
+ structs = make(map[string]*tmplStruct)
+ )
+ for _, original := range evmABI.Methods {
+ // Normalize the method for capital cases and non-anonymous inputs/outputs
+ normalized := original
+ normalized.Name = methodNormalizer[lang](original.Name)
+
+ normalized.Inputs = make([]abi.Argument, len(original.Inputs))
+ copy(normalized.Inputs, original.Inputs)
+ for j, input := range normalized.Inputs {
+ if input.Name == "" {
+ normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
+ }
+ if _, exist := structs[input.Type.String()]; input.Type.T == abi.TupleTy && !exist {
+ bindStructType[lang](input.Type, structs)
+ }
+ }
+ normalized.Outputs = make([]abi.Argument, len(original.Outputs))
+ copy(normalized.Outputs, original.Outputs)
+ for j, output := range normalized.Outputs {
+ if output.Name != "" {
+ normalized.Outputs[j].Name = capitalise(output.Name)
+ }
+ if _, exist := structs[output.Type.String()]; output.Type.T == abi.TupleTy && !exist {
+ bindStructType[lang](output.Type, structs)
+ }
+ }
+ // Append the methods to the call or transact lists
+ if original.Const {
+ calls[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
+ } else {
+ transacts[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
+ }
+ }
+ for _, original := range evmABI.Events {
+ // Skip anonymous events as they don't support explicit filtering
+ if original.Anonymous {
+ continue
+ }
+ // Normalize the event for capital cases and non-anonymous outputs
+ normalized := original
+ normalized.Name = methodNormalizer[lang](original.Name)
+
+ normalized.Inputs = make([]abi.Argument, len(original.Inputs))
+ copy(normalized.Inputs, original.Inputs)
+ for j, input := range normalized.Inputs {
+ // Indexed fields are input, non-indexed ones are outputs
+ if input.Indexed {
+ if input.Name == "" {
+ normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
+ }
+ if _, exist := structs[input.Type.String()]; input.Type.T == abi.TupleTy && !exist {
+ bindStructType[lang](input.Type, structs)
+ }
+ }
+ }
+ // Append the event to the accumulator list
+ events[original.Name] = &tmplEvent{Original: original, Normalized: normalized}
+ }
+
+ // There is no easy way to pass arbitrary java objects to the Go side.
+ if len(structs) > 0 && lang == LangJava {
+ return "", errors.New("java binding for tuple arguments is not supported yet")
+ }
+
+ contracts[types[i]] = &tmplContract{
+ Type: capitalise(types[i]),
+ InputABI: strings.Replace(strippedABI, "\"", "\\\"", -1),
+ InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
+ Constructor: evmABI.Constructor,
+ Calls: calls,
+ Transacts: transacts,
+ Events: events,
+ Libraries: make(map[string]string),
+ Structs: structs,
+ }
+ // Function 4-byte signatures are stored in the same sequence
+ // as types, if available.
+ if len(fsigs) > i {
+ contracts[types[i]].FuncSigs = fsigs[i]
+ }
+ // Parse library references.
+ for pattern, name := range libs {
+ matched, err := regexp.Match("__\\$"+pattern+"\\$__", []byte(contracts[types[i]].InputBin))
+ if err != nil {
+ log.Error("Could not search for pattern", "pattern", pattern, "contract", contracts[types[i]], "err", err)
+ }
+ if matched {
+ contracts[types[i]].Libraries[pattern] = name
+ // keep track that this type is a library
+ if _, ok := isLib[name]; !ok {
+ isLib[name] = struct{}{}
+ }
+ }
+ }
+ }
+ // Check if that type has already been identified as a library
+ for i := 0; i < len(types); i++ {
+ _, ok := isLib[types[i]]
+ contracts[types[i]].Library = ok
+ }
+ // Generate the contract template data content and render it
+ data := &tmplData{
+ Package: pkg,
+ Contracts: contracts,
+ Libraries: libs,
+ }
+ buffer := new(bytes.Buffer)
+
+ funcs := map[string]interface{}{
+ "bindtype": bindType[lang],
+ "bindtopictype": bindTopicType[lang],
+ "namedtype": namedType[lang],
+ "formatmethod": formatMethod,
+ "formatevent": formatEvent,
+ "capitalise": capitalise,
+ "decapitalise": decapitalise,
+ }
+ tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource[lang]))
+ if err := tmpl.Execute(buffer, data); err != nil {
+ return "", err
+ }
+ // For Go bindings pass the code through gofmt to clean it up
+ if lang == LangGo {
+ code, err := format.Source(buffer.Bytes())
+ if err != nil {
+ return "", fmt.Errorf("%v\n%s", err, buffer)
+ }
+ return string(code), nil
+ }
+ // For all others just return as is for now
+ return buffer.String(), nil
+}
+
+// bindType is a set of type binders that convert Solidity types to some supported
+// programming language types.
+var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
+ LangGo: bindTypeGo,
+ LangJava: bindTypeJava,
+}
+
+// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go one.
+func bindBasicTypeGo(kind abi.Type) string {
+ switch kind.T {
+ case abi.AddressTy:
+ return "common.Address"
+ case abi.IntTy, abi.UintTy:
+ parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
+ switch parts[2] {
+ case "8", "16", "32", "64":
+ return fmt.Sprintf("%sint%s", parts[1], parts[2])
+ }
+ return "*big.Int"
+ case abi.FixedBytesTy:
+ return fmt.Sprintf("[%d]byte", kind.Size)
+ case abi.BytesTy:
+ return "[]byte"
+ case abi.FunctionTy:
+ return "[24]byte"
+ default:
+ // string, bool types
+ return kind.String()
+ }
+}
+
+// bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
+// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
+// mapped will use an upscaled type (e.g. BigDecimal).
+func bindTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
+ switch kind.T {
+ case abi.TupleTy:
+ return structs[kind.String()].Name
+ case abi.ArrayTy:
+ return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem, structs)
+ case abi.SliceTy:
+ return "[]" + bindTypeGo(*kind.Elem, structs)
+ default:
+ return bindBasicTypeGo(kind)
+ }
+}
+
+// bindBasicTypeJava converts basic solidity types(except array, slice and tuple) to Java one.
+func bindBasicTypeJava(kind abi.Type) string {
+ switch kind.T {
+ case abi.AddressTy:
+ return "Address"
+ case abi.IntTy, abi.UintTy:
+ // Note that uint and int (without digits) are also matched,
+ // these are size 256, and will translate to BigInt (the default).
+ parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
+ if len(parts) != 3 {
+ return kind.String()
+ }
+ // All unsigned integers should be translated to BigInt since gomobile doesn't
+ // support them.
+ if parts[1] == "u" {
+ return "BigInt"
+ }
+
+ namedSize := map[string]string{
+ "8": "byte",
+ "16": "short",
+ "32": "int",
+ "64": "long",
+ }[parts[2]]
+
+ // default to BigInt
+ if namedSize == "" {
+ namedSize = "BigInt"
+ }
+ return namedSize
+ case abi.FixedBytesTy, abi.BytesTy:
+ return "byte[]"
+ case abi.BoolTy:
+ return "boolean"
+ case abi.StringTy:
+ return "String"
+ case abi.FunctionTy:
+ return "byte[24]"
+ default:
+ return kind.String()
+ }
+}
+
+// pluralizeJavaType explicitly converts multidimensional types to predefined
+// type in go side.
+func pluralizeJavaType(typ string) string {
+ switch typ {
+ case "boolean":
+ return "Bools"
+ case "String":
+ return "Strings"
+ case "Address":
+ return "Addresses"
+ case "byte[]":
+ return "Binaries"
+ case "BigInt":
+ return "BigInts"
+ }
+ return typ + "[]"
+}
+
+// bindTypeJava converts a Solidity type to a Java one. Since there is no clear mapping
+// from all Solidity types to Java ones (e.g. uint17), those that cannot be exactly
+// mapped will use an upscaled type (e.g. BigDecimal).
+func bindTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
+ switch kind.T {
+ case abi.TupleTy:
+ return structs[kind.String()].Name
+ case abi.ArrayTy, abi.SliceTy:
+ return pluralizeJavaType(bindTypeJava(*kind.Elem, structs))
+ default:
+ return bindBasicTypeJava(kind)
+ }
+}
+
+// bindTopicType is a set of type binders that convert Solidity types to some
+// supported programming language topic types.
+var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
+ LangGo: bindTopicTypeGo,
+ LangJava: bindTopicTypeJava,
+}
+
+// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
+// funcionality as for simple types, but dynamic types get converted to hashes.
+func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
+ bound := bindTypeGo(kind, structs)
+ if bound == "string" || bound == "[]byte" {
+ bound = "common.Hash"
+ }
+ return bound
+}
+
+// bindTopicTypeJava converts a Solidity topic type to a Java one. It is almost the same
+// funcionality as for simple types, but dynamic types get converted to hashes.
+func bindTopicTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
+ bound := bindTypeJava(kind, structs)
+ if bound == "String" || bound == "byte[]" {
+ bound = "Hash"
+ }
+ return bound
+}
+
+// bindStructType is a set of type binders that convert Solidity tuple types to some supported
+// programming language struct definition.
+var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
+ LangGo: bindStructTypeGo,
+ LangJava: bindStructTypeJava,
+}
+
+// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
+// in the given map.
+// Notably, this function will resolve and record nested struct recursively.
+func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
+ switch kind.T {
+ case abi.TupleTy:
+ if s, exist := structs[kind.String()]; exist {
+ return s.Name
+ }
+ var fields []*tmplField
+ for i, elem := range kind.TupleElems {
+ field := bindStructTypeGo(*elem, structs)
+ fields = append(fields, &tmplField{Type: field, Name: capitalise(kind.TupleRawNames[i]), SolKind: *elem})
+ }
+ name := fmt.Sprintf("Struct%d", len(structs))
+ structs[kind.String()] = &tmplStruct{
+ Name: name,
+ Fields: fields,
+ }
+ return name
+ case abi.ArrayTy:
+ return fmt.Sprintf("[%d]", kind.Size) + bindStructTypeGo(*kind.Elem, structs)
+ case abi.SliceTy:
+ return "[]" + bindStructTypeGo(*kind.Elem, structs)
+ default:
+ return bindBasicTypeGo(kind)
+ }
+}
+
+// bindStructTypeJava converts a Solidity tuple type to a Java one and records the mapping
+// in the given map.
+// Notably, this function will resolve and record nested struct recursively.
+func bindStructTypeJava(kind abi.Type, structs map[string]*tmplStruct) string {
+ switch kind.T {
+ case abi.TupleTy:
+ if s, exist := structs[kind.String()]; exist {
+ return s.Name
+ }
+ var fields []*tmplField
+ for i, elem := range kind.TupleElems {
+ field := bindStructTypeJava(*elem, structs)
+ fields = append(fields, &tmplField{Type: field, Name: decapitalise(kind.TupleRawNames[i]), SolKind: *elem})
+ }
+ name := fmt.Sprintf("Class%d", len(structs))
+ structs[kind.String()] = &tmplStruct{
+ Name: name,
+ Fields: fields,
+ }
+ return name
+ case abi.ArrayTy, abi.SliceTy:
+ return pluralizeJavaType(bindStructTypeJava(*kind.Elem, structs))
+ default:
+ return bindBasicTypeJava(kind)
+ }
+}
+
+// namedType is a set of functions that transform language specific types to
+// named versions that my be used inside method names.
+var namedType = map[Lang]func(string, abi.Type) string{
+ LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") },
+ LangJava: namedTypeJava,
+}
+
+// namedTypeJava converts some primitive data types to named variants that can
+// be used as parts of method names.
+func namedTypeJava(javaKind string, solKind abi.Type) string {
+ switch javaKind {
+ case "byte[]":
+ return "Binary"
+ case "boolean":
+ return "Bool"
+ default:
+ parts := regexp.MustCompile(`(u)?int([0-9]*)(\[[0-9]*\])?`).FindStringSubmatch(solKind.String())
+ if len(parts) != 4 {
+ return javaKind
+ }
+ switch parts[2] {
+ case "8", "16", "32", "64":
+ if parts[3] == "" {
+ return capitalise(fmt.Sprintf("%sint%s", parts[1], parts[2]))
+ }
+ return capitalise(fmt.Sprintf("%sint%ss", parts[1], parts[2]))
+
+ default:
+ return javaKind
+ }
+ }
+}
+
+// methodNormalizer is a name transformer that modifies Solidity method names to
+// conform to target language naming concentions.
+var methodNormalizer = map[Lang]func(string) string{
+ LangGo: abi.ToCamelCase,
+ LangJava: decapitalise,
+}
+
+// capitalise makes a camel-case string which starts with an upper case character.
+func capitalise(input string) string {
+ return abi.ToCamelCase(input)
+}
+
+// decapitalise makes a camel-case string which starts with a lower case character.
+func decapitalise(input string) string {
+ if len(input) == 0 {
+ return input
+ }
+
+ goForm := abi.ToCamelCase(input)
+ return strings.ToLower(goForm[:1]) + goForm[1:]
+}
+
+// structured checks whether a list of ABI data types has enough information to
+// operate through a proper Go struct or if flat returns are needed.
+func structured(args abi.Arguments) bool {
+ if len(args) < 2 {
+ return false
+ }
+ exists := make(map[string]bool)
+ for _, out := range args {
+ // If the name is anonymous, we can't organize into a struct
+ if out.Name == "" {
+ return false
+ }
+ // If the field name is empty when normalized or collides (var, Var, _var, _Var),
+ // we can't organize into a struct
+ field := capitalise(out.Name)
+ if field == "" || exists[field] {
+ return false
+ }
+ exists[field] = true
+ }
+ return true
+}
+
+// resolveArgName converts a raw argument representation into a user friendly format.
+func resolveArgName(arg abi.Argument, structs map[string]*tmplStruct) string {
+ var (
+ prefix string
+ embedded string
+ typ = &arg.Type
+ )
+loop:
+ for {
+ switch typ.T {
+ case abi.SliceTy:
+ prefix += "[]"
+ case abi.ArrayTy:
+ prefix += fmt.Sprintf("[%d]", typ.Size)
+ default:
+ embedded = typ.String()
+ break loop
+ }
+ typ = typ.Elem
+ }
+ if s, exist := structs[embedded]; exist {
+ return prefix + s.Name
+ } else {
+ return arg.Type.String()
+ }
+}
+
+// formatMethod transforms raw method representation into a user friendly one.
+func formatMethod(method abi.Method, structs map[string]*tmplStruct) string {
+ inputs := make([]string, len(method.Inputs))
+ for i, input := range method.Inputs {
+ inputs[i] = fmt.Sprintf("%v %v", resolveArgName(input, structs), input.Name)
+ }
+ outputs := make([]string, len(method.Outputs))
+ for i, output := range method.Outputs {
+ outputs[i] = resolveArgName(output, structs)
+ if len(output.Name) > 0 {
+ outputs[i] += fmt.Sprintf(" %v", output.Name)
+ }
+ }
+ constant := ""
+ if method.Const {
+ constant = "constant "
+ }
+ return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
+}
+
+// formatEvent transforms raw event representation into a user friendly one.
+func formatEvent(event abi.Event, structs map[string]*tmplStruct) string {
+ inputs := make([]string, len(event.Inputs))
+ for i, input := range event.Inputs {
+ if input.Indexed {
+ inputs[i] = fmt.Sprintf("%v indexed %v", resolveArgName(input, structs), input.Name)
+ } else {
+ inputs[i] = fmt.Sprintf("%v %v", resolveArgName(input, structs), input.Name)
+ }
+ }
+ return fmt.Sprintf("event %v(%v)", event.RawName, strings.Join(inputs, ", "))
+}
diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go
new file mode 100644
index 0000000..e90d02e
--- /dev/null
+++ b/accounts/abi/bind/template.go
@@ -0,0 +1,616 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bind
+
+import "github.com/ava-labs/coreth/accounts/abi"
+
+// tmplData is the data structure required to fill the binding template.
+type tmplData struct {
+ Package string // Name of the package to place the generated file in
+ Contracts map[string]*tmplContract // List of contracts to generate into this file
+ Libraries map[string]string // Map the bytecode's link pattern to the library name
+}
+
+// tmplContract contains the data needed to generate an individual contract binding.
+type tmplContract struct {
+ Type string // Type name of the main contract binding
+ InputABI string // JSON ABI used as the input to generate the binding from
+ InputBin string // Optional EVM bytecode used to denetare deploy code from
+ FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
+ Constructor abi.Method // Contract constructor for deploy parametrization
+ Calls map[string]*tmplMethod // Contract calls that only read state data
+ Transacts map[string]*tmplMethod // Contract calls that write state data
+ Events map[string]*tmplEvent // Contract events accessors
+ Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs
+ Structs map[string]*tmplStruct // Contract struct type definitions
+ Library bool
+}
+
+// tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
+// and cached data fields.
+type tmplMethod struct {
+ Original abi.Method // Original method as parsed by the abi package
+ Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
+ Structured bool // Whether the returns should be accumulated into a struct
+}
+
+// tmplEvent is a wrapper around an a
+type tmplEvent struct {
+ Original abi.Event // Original event as parsed by the abi package
+ Normalized abi.Event // Normalized version of the parsed fields
+}
+
+// tmplField is a wrapper around a struct field with binding language
+// struct type definition and relative filed name.
+type tmplField struct {
+ Type string // Field type representation depends on target binding language
+ Name string // Field name converted from the raw user-defined field name
+ SolKind abi.Type // Raw abi type information
+}
+
+// tmplStruct is a wrapper around an abi.tuple contains a auto-generated
+// struct name.
+type tmplStruct struct {
+ Name string // Auto-generated struct name(We can't obtain the raw struct name through abi)
+ Fields []*tmplField // Struct fields definition depends on the binding language.
+}
+
+// tmplSource is language to template mapping containing all the supported
+// programming languages the package can generate to.
+var tmplSource = map[Lang]string{
+ LangGo: tmplSourceGo,
+ LangJava: tmplSourceJava,
+}
+
+// tmplSourceGo is the Go source template use to generate the contract binding
+// based on.
+const tmplSourceGo = `
+// Code generated - DO NOT EDIT.
+// This file is a generated binding and any manual changes will be lost.
+
+package {{.Package}}
+
+import (
+ "math/big"
+ "strings"
+
+ ethereum "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/coreth/accounts/abi"
+ "github.com/ava-labs/coreth/accounts/abi/bind"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/event"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var (
+ _ = big.NewInt
+ _ = strings.NewReader
+ _ = ethereum.NotFound
+ _ = abi.U256
+ _ = bind.Bind
+ _ = common.Big1
+ _ = types.BloomLookup
+ _ = event.NewSubscription
+)
+
+{{range $contract := .Contracts}}
+ {{$structs := $contract.Structs}}
+ // {{.Type}}ABI is the input ABI used to generate the binding from.
+ const {{.Type}}ABI = "{{.InputABI}}"
+
+ {{if $contract.FuncSigs}}
+ // {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
+ var {{.Type}}FuncSigs = map[string]string{
+ {{range $strsig, $binsig := .FuncSigs}}"{{$binsig}}": "{{$strsig}}",
+ {{end}}
+ }
+ {{end}}
+
+ {{if .InputBin}}
+ // {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
+ var {{.Type}}Bin = "0x{{.InputBin}}"
+
+ // Deploy{{.Type}} deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
+ func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) {
+ parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
+ if err != nil {
+ return common.Address{}, nil, nil, err
+ }
+ {{range $pattern, $name := .Libraries}}
+ {{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend)
+ {{$contract.Type}}Bin = strings.Replace({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:], -1)
+ {{end}}
+ address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
+ if err != nil {
+ return common.Address{}, nil, nil, err
+ }
+ return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
+ }
+ {{end}}
+
+ // {{.Type}} is an auto generated Go binding around an Ethereum contract.
+ type {{.Type}} struct {
+ {{.Type}}Caller // Read-only binding to the contract
+ {{.Type}}Transactor // Write-only binding to the contract
+ {{.Type}}Filterer // Log filterer for contract events
+ }
+
+ // {{.Type}}Caller is an auto generated read-only Go binding around an Ethereum contract.
+ type {{.Type}}Caller struct {
+ contract *bind.BoundContract // Generic contract wrapper for the low level calls
+ }
+
+ // {{.Type}}Transactor is an auto generated write-only Go binding around an Ethereum contract.
+ type {{.Type}}Transactor struct {
+ contract *bind.BoundContract // Generic contract wrapper for the low level calls
+ }
+
+ // {{.Type}}Filterer is an auto generated log filtering Go binding around an Ethereum contract events.
+ type {{.Type}}Filterer struct {
+ contract *bind.BoundContract // Generic contract wrapper for the low level calls
+ }
+
+ // {{.Type}}Session is an auto generated Go binding around an Ethereum contract,
+ // with pre-set call and transact options.
+ type {{.Type}}Session struct {
+ Contract *{{.Type}} // Generic contract binding to set the session for
+ CallOpts bind.CallOpts // Call options to use throughout this session
+ TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
+ }
+
+ // {{.Type}}CallerSession is an auto generated read-only Go binding around an Ethereum contract,
+ // with pre-set call options.
+ type {{.Type}}CallerSession struct {
+ Contract *{{.Type}}Caller // Generic contract caller binding to set the session for
+ CallOpts bind.CallOpts // Call options to use throughout this session
+ }
+
+ // {{.Type}}TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
+ // with pre-set transact options.
+ type {{.Type}}TransactorSession struct {
+ Contract *{{.Type}}Transactor // Generic contract transactor binding to set the session for
+ TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
+ }
+
+ // {{.Type}}Raw is an auto generated low-level Go binding around an Ethereum contract.
+ type {{.Type}}Raw struct {
+ Contract *{{.Type}} // Generic contract binding to access the raw methods on
+ }
+
+ // {{.Type}}CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
+ type {{.Type}}CallerRaw struct {
+ Contract *{{.Type}}Caller // Generic read-only contract binding to access the raw methods on
+ }
+
+ // {{.Type}}TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
+ type {{.Type}}TransactorRaw struct {
+ Contract *{{.Type}}Transactor // Generic write-only contract binding to access the raw methods on
+ }
+
+ // New{{.Type}} creates a new instance of {{.Type}}, bound to a specific deployed contract.
+ func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) {
+ contract, err := bind{{.Type}}(address, backend, backend, backend)
+ if err != nil {
+ return nil, err
+ }
+ return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
+ }
+
+ // New{{.Type}}Caller creates a new read-only instance of {{.Type}}, bound to a specific deployed contract.
+ func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) {
+ contract, err := bind{{.Type}}(address, caller, nil, nil)
+ if err != nil {
+ return nil, err
+ }
+ return &{{.Type}}Caller{contract: contract}, nil
+ }
+
+ // New{{.Type}}Transactor creates a new write-only instance of {{.Type}}, bound to a specific deployed contract.
+ func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) {
+ contract, err := bind{{.Type}}(address, nil, transactor, nil)
+ if err != nil {
+ return nil, err
+ }
+ return &{{.Type}}Transactor{contract: contract}, nil
+ }
+
+ // New{{.Type}}Filterer creates a new log filterer instance of {{.Type}}, bound to a specific deployed contract.
+ func New{{.Type}}Filterer(address common.Address, filterer bind.ContractFilterer) (*{{.Type}}Filterer, error) {
+ contract, err := bind{{.Type}}(address, nil, nil, filterer)
+ if err != nil {
+ return nil, err
+ }
+ return &{{.Type}}Filterer{contract: contract}, nil
+ }
+
+ // bind{{.Type}} binds a generic wrapper to an already deployed contract.
+ func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) {
+ parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
+ if err != nil {
+ return nil, err
+ }
+ return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil
+ }
+
+ // Call invokes the (constant) contract method with params as input values and
+ // sets the output to result. The result type might be a single field for simple
+ // returns, a slice of interfaces for anonymous returns and a struct for named
+ // returns.
+ func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
+ return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...)
+ }
+
+ // Transfer initiates a plain transaction to move funds to the contract, calling
+ // its default method if one is available.
+ func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
+ return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts)
+ }
+
+ // Transact invokes the (paid) contract method with params as input values.
+ func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
+ return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...)
+ }
+
+ // Call invokes the (constant) contract method with params as input values and
+ // sets the output to result. The result type might be a single field for simple
+ // returns, a slice of interfaces for anonymous returns and a struct for named
+ // returns.
+ func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
+ return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...)
+ }
+
+ // Transfer initiates a plain transaction to move funds to the contract, calling
+ // its default method if one is available.
+ func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
+ return _{{$contract.Type}}.Contract.contract.Transfer(opts)
+ }
+
+ // Transact invokes the (paid) contract method with params as input values.
+ func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
+ return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...)
+ }
+
+ {{range .Structs}}
+ // {{.Name}} is an auto generated low-level Go binding around an user-defined struct.
+ type {{.Name}} struct {
+ {{range $field := .Fields}}
+ {{$field.Name}} {{$field.Type}}{{end}}
+ }
+ {{end}}
+
+ {{range .Calls}}
+ // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatmethod .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}Caller) {{.Normalized.Name}}(opts *bind.CallOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} },{{else}}{{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}}{{end}} error) {
+ {{if .Structured}}ret := new(struct{
+ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}}
+ {{end}}
+ }){{else}}var (
+ {{range $i, $_ := .Normalized.Outputs}}ret{{$i}} = new({{bindtype .Type $structs}})
+ {{end}}
+ ){{end}}
+ out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}&[]interface{}{
+ {{range $i, $_ := .Normalized.Outputs}}ret{{$i}},
+ {{end}}
+ }{{end}}{{end}}
+ err := _{{$contract.Type}}.contract.Call(opts, out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
+ return {{if .Structured}}*ret,{{else}}{{range $i, $_ := .Normalized.Outputs}}*ret{{$i}},{{end}}{{end}} err
+ }
+
+ // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatmethod .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) {
+ return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
+ }
+
+ // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatmethod .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}CallerSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) {
+ return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
+ }
+ {{end}}
+
+ {{range .Transacts}}
+ // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatmethod .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
+ return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
+ }
+
+ // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatmethod .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
+ return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
+ }
+
+ // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatmethod .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
+ return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
+ }
+ {{end}}
+
+ {{range .Events}}
+ // {{$contract.Type}}{{.Normalized.Name}}Iterator is returned from Filter{{.Normalized.Name}} and is used to iterate over the raw logs and unpacked data for {{.Normalized.Name}} events raised by the {{$contract.Type}} contract.
+ type {{$contract.Type}}{{.Normalized.Name}}Iterator struct {
+ Event *{{$contract.Type}}{{.Normalized.Name}} // Event containing the contract specifics and raw log
+
+ contract *bind.BoundContract // Generic contract to use for unpacking event data
+ event string // Event name to use for unpacking event data
+
+ logs chan types.Log // Log channel receiving the found contract events
+ sub ethereum.Subscription // Subscription for errors, completion and termination
+ done bool // Whether the subscription completed delivering logs
+ fail error // Occurred error to stop iteration
+ }
+ // Next advances the iterator to the subsequent event, returning whether there
+ // are any more events found. In case of a retrieval or parsing error, false is
+ // returned and Error() can be queried for the exact failure.
+ func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Next() bool {
+ // If the iterator failed, stop iterating
+ if (it.fail != nil) {
+ return false
+ }
+ // If the iterator completed, deliver directly whatever's available
+ if (it.done) {
+ select {
+ case log := <-it.logs:
+ it.Event = new({{$contract.Type}}{{.Normalized.Name}})
+ if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
+ it.fail = err
+ return false
+ }
+ it.Event.Raw = log
+ return true
+
+ default:
+ return false
+ }
+ }
+ // Iterator still in progress, wait for either a data or an error event
+ select {
+ case log := <-it.logs:
+ it.Event = new({{$contract.Type}}{{.Normalized.Name}})
+ if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
+ it.fail = err
+ return false
+ }
+ it.Event.Raw = log
+ return true
+
+ case err := <-it.sub.Err():
+ it.done = true
+ it.fail = err
+ return it.Next()
+ }
+ }
+ // Error returns any retrieval or parsing error occurred during filtering.
+ func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Error() error {
+ return it.fail
+ }
+ // Close terminates the iteration process, releasing any pending underlying
+ // resources.
+ func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Close() error {
+ it.sub.Unsubscribe()
+ return nil
+ }
+
+ // {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract.
+ type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}
+ {{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}}
+ Raw types.Log // Blockchain specific contextual infos
+ }
+
+ // Filter{{.Normalized.Name}} is a free log retrieval operation binding the contract event 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatevent .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Filter{{.Normalized.Name}}(opts *bind.FilterOpts{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (*{{$contract.Type}}{{.Normalized.Name}}Iterator, error) {
+ {{range .Normalized.Inputs}}
+ {{if .Indexed}}var {{.Name}}Rule []interface{}
+ for _, {{.Name}}Item := range {{.Name}} {
+ {{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
+ }{{end}}{{end}}
+
+ logs, sub, err := _{{$contract.Type}}.contract.FilterLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
+ if err != nil {
+ return nil, err
+ }
+ return &{{$contract.Type}}{{.Normalized.Name}}Iterator{contract: _{{$contract.Type}}.contract, event: "{{.Original.Name}}", logs: logs, sub: sub}, nil
+ }
+
+ // Watch{{.Normalized.Name}} is a free log subscription operation binding the contract event 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{formatevent .Original $structs}}
+ func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Watch{{.Normalized.Name}}(opts *bind.WatchOpts, sink chan<- *{{$contract.Type}}{{.Normalized.Name}}{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (event.Subscription, error) {
+ {{range .Normalized.Inputs}}
+ {{if .Indexed}}var {{.Name}}Rule []interface{}
+ for _, {{.Name}}Item := range {{.Name}} {
+ {{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
+ }{{end}}{{end}}
+
+ logs, sub, err := _{{$contract.Type}}.contract.WatchLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
+ if err != nil {
+ return nil, err
+ }
+ return event.NewSubscription(func(quit <-chan struct{}) error {
+ defer sub.Unsubscribe()
+ for {
+ select {
+ case log := <-logs:
+ // New log arrived, parse the event and forward to the user
+ event := new({{$contract.Type}}{{.Normalized.Name}})
+ if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
+ return err
+ }
+ event.Raw = log
+
+ select {
+ case sink <- event:
+ case err := <-sub.Err():
+ return err
+ case <-quit:
+ return nil
+ }
+ case err := <-sub.Err():
+ return err
+ case <-quit:
+ return nil
+ }
+ }
+ }), nil
+ }
+
+ // Parse{{.Normalized.Name}} is a log parse operation binding the contract event 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{.Original.String}}
+ func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
+ event := new({{$contract.Type}}{{.Normalized.Name}})
+ if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
+ return nil, err
+ }
+ return event, nil
+ }
+
+ {{end}}
+{{end}}
+`
+
+// tmplSourceJava is the Java source template use to generate the contract binding
+// based on.
+const tmplSourceJava = `
+// This file is an automatically generated Java binding. Do not modify as any
+// change will likely be lost upon the next re-generation!
+
+package {{.Package}};
+
+import org.ethereum.geth.*;
+import java.util.*;
+
+{{range $contract := .Contracts}}
+{{$structs := $contract.Structs}}
+{{if not .Library}}public {{end}}class {{.Type}} {
+ // ABI is the input ABI used to generate the binding from.
+ public final static String ABI = "{{.InputABI}}";
+ {{if $contract.FuncSigs}}
+ // {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
+ public final static Map<String, String> {{.Type}}FuncSigs;
+ static {
+ Hashtable<String, String> temp = new Hashtable<String, String>();
+ {{range $strsig, $binsig := .FuncSigs}}temp.put("{{$binsig}}", "{{$strsig}}");
+ {{end}}
+ {{.Type}}FuncSigs = Collections.unmodifiableMap(temp);
+ }
+ {{end}}
+ {{if .InputBin}}
+ // BYTECODE is the compiled bytecode used for deploying new contracts.
+ public final static String BYTECODE = "0x{{.InputBin}}";
+
+ // deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
+ public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception {
+ Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}});
+ String bytecode = BYTECODE;
+ {{if .Libraries}}
+
+ // "link" contract to dependent libraries by deploying them first.
+ {{range $pattern, $name := .Libraries}}
+ {{capitalise $name}} {{decapitalise $name}}Inst = {{capitalise $name}}.deploy(auth, client);
+ bytecode = bytecode.replace("__${{$pattern}}$__", {{decapitalise $name}}Inst.Address.getHex().substring(2));
+ {{end}}
+ {{end}}
+ {{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
+ {{end}}
+ return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.decodeFromHex(bytecode), client, args));
+ }
+
+ // Internal constructor used by contract deployment.
+ private {{.Type}}(BoundContract deployment) {
+ this.Address = deployment.getAddress();
+ this.Deployer = deployment.getDeployer();
+ this.Contract = deployment;
+ }
+ {{end}}
+
+ // Ethereum address where this contract is located at.
+ public final Address Address;
+
+ // Ethereum transaction in which this contract was deployed (if known!).
+ public final Transaction Deployer;
+
+ // Contract instance bound to a blockchain address.
+ private final BoundContract Contract;
+
+ // Creates a new instance of {{.Type}}, bound to a specific deployed contract.
+ public {{.Type}}(Address address, EthereumClient client) throws Exception {
+ this(Geth.bindContract(address, ABI, client));
+ }
+
+ {{range .Calls}}
+ {{if gt (len .Normalized.Outputs) 1}}
+ // {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}.
+ public class {{capitalise .Normalized.Name}}Results {
+ {{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type $structs}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}};
+ {{end}}
+ }
+ {{end}}
+
+ // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{.Original.String}}
+ public {{if gt (len .Normalized.Outputs) 1}}{{capitalise .Normalized.Name}}Results{{else}}{{range .Normalized.Outputs}}{{bindtype .Type $structs}}{{end}}{{end}} {{.Normalized.Name}}(CallOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception {
+ Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
+ {{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
+ {{end}}
+
+ Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}});
+ {{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type $structs) .Type}}(); results.set({{$index}}, result{{$index}});
+ {{end}}
+
+ if (opts == null) {
+ opts = Geth.newCallOpts();
+ }
+ this.Contract.call(opts, results, "{{.Original.Name}}", args);
+ {{if gt (len .Normalized.Outputs) 1}}
+ {{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results();
+ {{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type $structs) .Type}}();
+ {{end}}
+ return result;
+ {{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type $structs) .Type}}();{{end}}
+ {{end}}
+ }
+ {{end}}
+
+ {{range .Transacts}}
+ // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
+ //
+ // Solidity: {{.Original.String}}
+ public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception {
+ Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
+ {{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
+ {{end}}
+ return this.Contract.transact(opts, "{{.Original.Name}}" , args);
+ }
+ {{end}}
+}
+{{end}}
+`
diff --git a/accounts/abi/bind/topics.go b/accounts/abi/bind/topics.go
new file mode 100644
index 0000000..58e4ff3
--- /dev/null
+++ b/accounts/abi/bind/topics.go
@@ -0,0 +1,241 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bind
+
+import (
+ "encoding/binary"
+ "errors"
+ "fmt"
+ "math/big"
+ "reflect"
+
+ "github.com/ava-labs/coreth/accounts/abi"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// makeTopics converts a filter query argument list into a filter topic set.
+func makeTopics(query ...[]interface{}) ([][]common.Hash, error) {
+ topics := make([][]common.Hash, len(query))
+ for i, filter := range query {
+ for _, rule := range filter {
+ var topic common.Hash
+
+ // Try to generate the topic based on simple types
+ switch rule := rule.(type) {
+ case common.Hash:
+ copy(topic[:], rule[:])
+ case common.Address:
+ copy(topic[common.HashLength-common.AddressLength:], rule[:])
+ case *big.Int:
+ blob := rule.Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case bool:
+ if rule {
+ topic[common.HashLength-1] = 1
+ }
+ case int8:
+ blob := big.NewInt(int64(rule)).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case int16:
+ blob := big.NewInt(int64(rule)).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case int32:
+ blob := big.NewInt(int64(rule)).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case int64:
+ blob := big.NewInt(rule).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case uint8:
+ blob := new(big.Int).SetUint64(uint64(rule)).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case uint16:
+ blob := new(big.Int).SetUint64(uint64(rule)).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case uint32:
+ blob := new(big.Int).SetUint64(uint64(rule)).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case uint64:
+ blob := new(big.Int).SetUint64(rule).Bytes()
+ copy(topic[common.HashLength-len(blob):], blob)
+ case string:
+ hash := crypto.Keccak256Hash([]byte(rule))
+ copy(topic[:], hash[:])
+ case []byte:
+ hash := crypto.Keccak256Hash(rule)
+ copy(topic[:], hash[:])
+
+ default:
+ // Attempt to generate the topic from funky types
+ val := reflect.ValueOf(rule)
+
+ switch {
+
+ // static byte array
+ case val.Kind() == reflect.Array && reflect.TypeOf(rule).Elem().Kind() == reflect.Uint8:
+ reflect.Copy(reflect.ValueOf(topic[:val.Len()]), val)
+
+ default:
+ return nil, fmt.Errorf("unsupported indexed type: %T", rule)
+ }
+ }
+ topics[i] = append(topics[i], topic)
+ }
+ }
+ return topics, nil
+}
+
+// Big batch of reflect types for topic reconstruction.
+var (
+ reflectHash = reflect.TypeOf(common.Hash{})
+ reflectAddress = reflect.TypeOf(common.Address{})
+ reflectBigInt = reflect.TypeOf(new(big.Int))
+)
+
+// parseTopics converts the indexed topic fields into actual log field values.
+//
+// Note, dynamic types cannot be reconstructed since they get mapped to Keccak256
+// hashes as the topic value!
+func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) error {
+ // Sanity check that the fields and topics match up
+ if len(fields) != len(topics) {
+ return errors.New("topic/field count mismatch")
+ }
+ // Iterate over all the fields and reconstruct them from topics
+ for _, arg := range fields {
+ if !arg.Indexed {
+ return errors.New("non-indexed field in topic reconstruction")
+ }
+ field := reflect.ValueOf(out).Elem().FieldByName(capitalise(arg.Name))
+
+ // Try to parse the topic back into the fields based on primitive types
+ switch field.Kind() {
+ case reflect.Bool:
+ if topics[0][common.HashLength-1] == 1 {
+ field.Set(reflect.ValueOf(true))
+ }
+ case reflect.Int8:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(int8(num.Int64())))
+
+ case reflect.Int16:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(int16(num.Int64())))
+
+ case reflect.Int32:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(int32(num.Int64())))
+
+ case reflect.Int64:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(num.Int64()))
+
+ case reflect.Uint8:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(uint8(num.Uint64())))
+
+ case reflect.Uint16:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(uint16(num.Uint64())))
+
+ case reflect.Uint32:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(uint32(num.Uint64())))
+
+ case reflect.Uint64:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(num.Uint64()))
+
+ default:
+ // Ran out of plain primitive types, try custom types
+ switch field.Type() {
+ case reflectHash: // Also covers all dynamic types
+ field.Set(reflect.ValueOf(topics[0]))
+
+ case reflectAddress:
+ var addr common.Address
+ copy(addr[:], topics[0][common.HashLength-common.AddressLength:])
+ field.Set(reflect.ValueOf(addr))
+
+ case reflectBigInt:
+ num := new(big.Int).SetBytes(topics[0][:])
+ field.Set(reflect.ValueOf(num))
+
+ default:
+ // Ran out of custom types, try the crazies
+ switch {
+
+ // static byte array
+ case arg.Type.T == abi.FixedBytesTy:
+ reflect.Copy(field, reflect.ValueOf(topics[0][:arg.Type.Size]))
+
+ default:
+ return fmt.Errorf("unsupported indexed type: %v", arg.Type)
+ }
+ }
+ }
+ topics = topics[1:]
+ }
+ return nil
+}
+
+// parseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs
+func parseTopicsIntoMap(out map[string]interface{}, fields abi.Arguments, topics []common.Hash) error {
+ // Sanity check that the fields and topics match up
+ if len(fields) != len(topics) {
+ return errors.New("topic/field count mismatch")
+ }
+ // Iterate over all the fields and reconstruct them from topics
+ for _, arg := range fields {
+ if !arg.Indexed {
+ return errors.New("non-indexed field in topic reconstruction")
+ }
+
+ switch arg.Type.T {
+ case abi.BoolTy:
+ out[arg.Name] = topics[0][common.HashLength-1] == 1
+ case abi.IntTy, abi.UintTy:
+ num := new(big.Int).SetBytes(topics[0][:])
+ out[arg.Name] = num
+ case abi.AddressTy:
+ var addr common.Address
+ copy(addr[:], topics[0][common.HashLength-common.AddressLength:])
+ out[arg.Name] = addr
+ case abi.HashTy:
+ out[arg.Name] = topics[0]
+ case abi.FixedBytesTy:
+ out[arg.Name] = topics[0][:]
+ case abi.StringTy, abi.BytesTy, abi.SliceTy, abi.ArrayTy:
+ // Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash
+ // whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash
+ out[arg.Name] = topics[0]
+ case abi.FunctionTy:
+ if garbage := binary.BigEndian.Uint64(topics[0][0:8]); garbage != 0 {
+ return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[0].Bytes())
+ }
+ var tmp [24]byte
+ copy(tmp[:], topics[0][8:32])
+ out[arg.Name] = tmp
+ default: // Not handling tuples
+ return fmt.Errorf("unsupported indexed type: %v", arg.Type)
+ }
+
+ topics = topics[1:]
+ }
+
+ return nil
+}
diff --git a/accounts/abi/bind/util.go b/accounts/abi/bind/util.go
new file mode 100644
index 0000000..b3ec22c
--- /dev/null
+++ b/accounts/abi/bind/util.go
@@ -0,0 +1,76 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bind
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+// WaitMined waits for tx to be mined on the blockchain.
+// It stops waiting when the context is canceled.
+func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
+ queryTicker := time.NewTicker(time.Second)
+ defer queryTicker.Stop()
+
+ logger := log.New("hash", tx.Hash())
+ for {
+ receipt, err := b.TransactionReceipt(ctx, tx.Hash())
+ if receipt != nil {
+ return receipt, nil
+ }
+ if err != nil {
+ logger.Trace("Receipt retrieval failed", "err", err)
+ } else {
+ logger.Trace("Transaction not yet mined")
+ }
+ // Wait for the next round.
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case <-queryTicker.C:
+ }
+ }
+}
+
+// WaitDeployed waits for a contract deployment transaction and returns the on-chain
+// contract address when it is mined. It stops waiting when ctx is canceled.
+func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
+ if tx.To() != nil {
+ return common.Address{}, fmt.Errorf("tx is not contract creation")
+ }
+ receipt, err := WaitMined(ctx, b, tx)
+ if err != nil {
+ return common.Address{}, err
+ }
+ if receipt.ContractAddress == (common.Address{}) {
+ return common.Address{}, fmt.Errorf("zero address")
+ }
+ // Check that code has indeed been deployed at the address.
+ // This matters on pre-Homestead chains: OOG in the constructor
+ // could leave an empty account behind.
+ code, err := b.CodeAt(ctx, receipt.ContractAddress, nil)
+ if err == nil && len(code) == 0 {
+ err = ErrNoCodeAfterDeploy
+ }
+ return receipt.ContractAddress, err
+}
diff --git a/accounts/abi/doc.go b/accounts/abi/doc.go
new file mode 100644
index 0000000..8242068
--- /dev/null
+++ b/accounts/abi/doc.go
@@ -0,0 +1,26 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package abi implements the Ethereum ABI (Application Binary
+// Interface).
+//
+// The Ethereum ABI is strongly typed, known at compile time
+// and static. This ABI will handle basic type casting; unsigned
+// to signed and visa versa. It does not handle slice casting such
+// as unsigned slice to signed slice. Bit size type casting is also
+// handled. ints with a bit size of 32 will be properly cast to int256,
+// etc.
+package abi
diff --git a/accounts/abi/error.go b/accounts/abi/error.go
new file mode 100644
index 0000000..9d8674a
--- /dev/null
+++ b/accounts/abi/error.go
@@ -0,0 +1,84 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "errors"
+ "fmt"
+ "reflect"
+)
+
+var (
+ errBadBool = errors.New("abi: improperly encoded boolean value")
+)
+
+// formatSliceString formats the reflection kind with the given slice size
+// and returns a formatted string representation.
+func formatSliceString(kind reflect.Kind, sliceSize int) string {
+ if sliceSize == -1 {
+ return fmt.Sprintf("[]%v", kind)
+ }
+ return fmt.Sprintf("[%d]%v", sliceSize, kind)
+}
+
+// sliceTypeCheck checks that the given slice can by assigned to the reflection
+// type in t.
+func sliceTypeCheck(t Type, val reflect.Value) error {
+ if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
+ return typeErr(formatSliceString(t.Kind, t.Size), val.Type())
+ }
+
+ if t.T == ArrayTy && val.Len() != t.Size {
+ return typeErr(formatSliceString(t.Elem.Kind, t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
+ }
+
+ if t.Elem.T == SliceTy {
+ if val.Len() > 0 {
+ return sliceTypeCheck(*t.Elem, val.Index(0))
+ }
+ } else if t.Elem.T == ArrayTy {
+ return sliceTypeCheck(*t.Elem, val.Index(0))
+ }
+
+ if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind {
+ return typeErr(formatSliceString(t.Elem.Kind, t.Size), val.Type())
+ }
+ return nil
+}
+
+// typeCheck checks that the given reflection value can be assigned to the reflection
+// type in t.
+func typeCheck(t Type, value reflect.Value) error {
+ if t.T == SliceTy || t.T == ArrayTy {
+ return sliceTypeCheck(t, value)
+ }
+
+ // Check base type validity. Element types will be checked later on.
+ if t.Kind != value.Kind() {
+ return typeErr(t.Kind, value.Kind())
+ } else if t.T == FixedBytesTy && t.Size != value.Len() {
+ return typeErr(t.Type, value.Type())
+ } else {
+ return nil
+ }
+
+}
+
+// typeErr returns a formatted type casting error.
+func typeErr(expected, got interface{}) error {
+ return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected)
+}
diff --git a/accounts/abi/event.go b/accounts/abi/event.go
new file mode 100644
index 0000000..7a41baa
--- /dev/null
+++ b/accounts/abi/event.go
@@ -0,0 +1,77 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// Event is an event potentially triggered by the EVM's LOG mechanism. The Event
+// holds type information (inputs) about the yielded output. Anonymous events
+// don't get the signature canonical representation as the first LOG topic.
+type Event struct {
+ // Name is the event name used for internal representation. It's derived from
+ // the raw name and a suffix will be added in the case of a event overload.
+ //
+ // e.g.
+ // There are two events have same name:
+ // * foo(int,int)
+ // * foo(uint,uint)
+ // The event name of the first one wll be resolved as foo while the second one
+ // will be resolved as foo0.
+ Name string
+ // RawName is the raw event name parsed from ABI.
+ RawName string
+ Anonymous bool
+ Inputs Arguments
+}
+
+func (e Event) String() string {
+ inputs := make([]string, len(e.Inputs))
+ for i, input := range e.Inputs {
+ inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
+ if input.Indexed {
+ inputs[i] = fmt.Sprintf("%v indexed %v", input.Type, input.Name)
+ }
+ }
+ return fmt.Sprintf("event %v(%v)", e.RawName, strings.Join(inputs, ", "))
+}
+
+// Sig returns the event string signature according to the ABI spec.
+//
+// Example
+//
+// event foo(uint32 a, int b) = "foo(uint32,int256)"
+//
+// Please note that "int" is substitute for its canonical representation "int256"
+func (e Event) Sig() string {
+ types := make([]string, len(e.Inputs))
+ for i, input := range e.Inputs {
+ types[i] = input.Type.String()
+ }
+ return fmt.Sprintf("%v(%v)", e.RawName, strings.Join(types, ","))
+}
+
+// ID returns the canonical representation of the event's signature used by the
+// abi definition to identify event names and types.
+func (e Event) ID() common.Hash {
+ return common.BytesToHash(crypto.Keccak256([]byte(e.Sig())))
+}
diff --git a/accounts/abi/method.go b/accounts/abi/method.go
new file mode 100644
index 0000000..9cceaba
--- /dev/null
+++ b/accounts/abi/method.go
@@ -0,0 +1,90 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// Method represents a callable given a `Name` and whether the method is a constant.
+// If the method is `Const` no transaction needs to be created for this
+// particular Method call. It can easily be simulated using a local VM.
+// For example a `Balance()` method only needs to retrieve something
+// from the storage and therefore requires no Tx to be send to the
+// network. A method such as `Transact` does require a Tx and thus will
+// be flagged `false`.
+// Input specifies the required input parameters for this gives method.
+type Method struct {
+ // Name is the method name used for internal representation. It's derived from
+ // the raw name and a suffix will be added in the case of a function overload.
+ //
+ // e.g.
+ // There are two functions have same name:
+ // * foo(int,int)
+ // * foo(uint,uint)
+ // The method name of the first one will be resolved as foo while the second one
+ // will be resolved as foo0.
+ Name string
+ // RawName is the raw method name parsed from ABI.
+ RawName string
+ Const bool
+ Inputs Arguments
+ Outputs Arguments
+}
+
+// Sig returns the methods string signature according to the ABI spec.
+//
+// Example
+//
+// function foo(uint32 a, int b) = "foo(uint32,int256)"
+//
+// Please note that "int" is substitute for its canonical representation "int256"
+func (method Method) Sig() string {
+ types := make([]string, len(method.Inputs))
+ for i, input := range method.Inputs {
+ types[i] = input.Type.String()
+ }
+ return fmt.Sprintf("%v(%v)", method.RawName, strings.Join(types, ","))
+}
+
+func (method Method) String() string {
+ inputs := make([]string, len(method.Inputs))
+ for i, input := range method.Inputs {
+ inputs[i] = fmt.Sprintf("%v %v", input.Type, input.Name)
+ }
+ outputs := make([]string, len(method.Outputs))
+ for i, output := range method.Outputs {
+ outputs[i] = output.Type.String()
+ if len(output.Name) > 0 {
+ outputs[i] += fmt.Sprintf(" %v", output.Name)
+ }
+ }
+ constant := ""
+ if method.Const {
+ constant = "constant "
+ }
+ return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.RawName, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
+}
+
+// ID returns the canonical representation of the method's signature used by the
+// abi definition to identify method names and types.
+func (method Method) ID() []byte {
+ return crypto.Keccak256([]byte(method.Sig()))[:4]
+}
diff --git a/accounts/abi/numbers.go b/accounts/abi/numbers.go
new file mode 100644
index 0000000..c2e844c
--- /dev/null
+++ b/accounts/abi/numbers.go
@@ -0,0 +1,44 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "math/big"
+ "reflect"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+var (
+ bigT = reflect.TypeOf(&big.Int{})
+ derefbigT = reflect.TypeOf(big.Int{})
+ uint8T = reflect.TypeOf(uint8(0))
+ uint16T = reflect.TypeOf(uint16(0))
+ uint32T = reflect.TypeOf(uint32(0))
+ uint64T = reflect.TypeOf(uint64(0))
+ int8T = reflect.TypeOf(int8(0))
+ int16T = reflect.TypeOf(int16(0))
+ int32T = reflect.TypeOf(int32(0))
+ int64T = reflect.TypeOf(int64(0))
+ addressT = reflect.TypeOf(common.Address{})
+)
+
+// U256 converts a big Int into a 256bit EVM number.
+func U256(n *big.Int) []byte {
+ return math.PaddedBigBytes(math.U256(n), 32)
+}
diff --git a/accounts/abi/pack.go b/accounts/abi/pack.go
new file mode 100644
index 0000000..d88dde8
--- /dev/null
+++ b/accounts/abi/pack.go
@@ -0,0 +1,81 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "math/big"
+ "reflect"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+// packBytesSlice packs the given bytes as [L, V] as the canonical representation
+// bytes slice
+func packBytesSlice(bytes []byte, l int) []byte {
+ len := packNum(reflect.ValueOf(l))
+ return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
+}
+
+// packElement packs the given reflect value according to the abi specification in
+// t.
+func packElement(t Type, reflectValue reflect.Value) []byte {
+ switch t.T {
+ case IntTy, UintTy:
+ return packNum(reflectValue)
+ case StringTy:
+ return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len())
+ case AddressTy:
+ if reflectValue.Kind() == reflect.Array {
+ reflectValue = mustArrayToByteSlice(reflectValue)
+ }
+
+ return common.LeftPadBytes(reflectValue.Bytes(), 32)
+ case BoolTy:
+ if reflectValue.Bool() {
+ return math.PaddedBigBytes(common.Big1, 32)
+ }
+ return math.PaddedBigBytes(common.Big0, 32)
+ case BytesTy:
+ if reflectValue.Kind() == reflect.Array {
+ reflectValue = mustArrayToByteSlice(reflectValue)
+ }
+ return packBytesSlice(reflectValue.Bytes(), reflectValue.Len())
+ case FixedBytesTy, FunctionTy:
+ if reflectValue.Kind() == reflect.Array {
+ reflectValue = mustArrayToByteSlice(reflectValue)
+ }
+ return common.RightPadBytes(reflectValue.Bytes(), 32)
+ default:
+ panic("abi: fatal error")
+ }
+}
+
+// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
+func packNum(value reflect.Value) []byte {
+ switch kind := value.Kind(); kind {
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
+ return U256(new(big.Int).SetUint64(value.Uint()))
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+ return U256(big.NewInt(value.Int()))
+ case reflect.Ptr:
+ return U256(value.Interface().(*big.Int))
+ default:
+ panic("abi: fatal error")
+ }
+
+}
diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go
new file mode 100644
index 0000000..73ca8fa
--- /dev/null
+++ b/accounts/abi/reflect.go
@@ -0,0 +1,226 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "fmt"
+ "reflect"
+ "strings"
+)
+
+// indirect recursively dereferences the value until it either gets the value
+// or finds a big.Int
+func indirect(v reflect.Value) reflect.Value {
+ if v.Kind() == reflect.Ptr && v.Elem().Type() != derefbigT {
+ return indirect(v.Elem())
+ }
+ return v
+}
+
+// indirectInterfaceOrPtr recursively dereferences the value until value is not interface.
+func indirectInterfaceOrPtr(v reflect.Value) reflect.Value {
+ if (v.Kind() == reflect.Interface || v.Kind() == reflect.Ptr) && v.Elem().IsValid() {
+ return indirect(v.Elem())
+ }
+ return v
+}
+
+// reflectIntKind returns the reflect using the given size and
+// unsignedness.
+func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) {
+ switch size {
+ case 8:
+ if unsigned {
+ return reflect.Uint8, uint8T
+ }
+ return reflect.Int8, int8T
+ case 16:
+ if unsigned {
+ return reflect.Uint16, uint16T
+ }
+ return reflect.Int16, int16T
+ case 32:
+ if unsigned {
+ return reflect.Uint32, uint32T
+ }
+ return reflect.Int32, int32T
+ case 64:
+ if unsigned {
+ return reflect.Uint64, uint64T
+ }
+ return reflect.Int64, int64T
+ }
+ return reflect.Ptr, bigT
+}
+
+// mustArrayToBytesSlice creates a new byte slice with the exact same size as value
+// and copies the bytes in value to the new slice.
+func mustArrayToByteSlice(value reflect.Value) reflect.Value {
+ slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len())
+ reflect.Copy(slice, value)
+ return slice
+}
+
+// set attempts to assign src to dst by either setting, copying or otherwise.
+//
+// set is a bit more lenient when it comes to assignment and doesn't force an as
+// strict ruleset as bare `reflect` does.
+func set(dst, src reflect.Value) error {
+ dstType, srcType := dst.Type(), src.Type()
+ switch {
+ case dstType.Kind() == reflect.Interface && dst.Elem().IsValid():
+ return set(dst.Elem(), src)
+ case dstType.Kind() == reflect.Ptr && dstType.Elem() != derefbigT:
+ return set(dst.Elem(), src)
+ case srcType.AssignableTo(dstType) && dst.CanSet():
+ dst.Set(src)
+ case dstType.Kind() == reflect.Slice && srcType.Kind() == reflect.Slice:
+ return setSlice(dst, src)
+ default:
+ return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
+ }
+ return nil
+}
+
+// setSlice attempts to assign src to dst when slices are not assignable by default
+// e.g. src: [][]byte -> dst: [][15]byte
+func setSlice(dst, src reflect.Value) error {
+ slice := reflect.MakeSlice(dst.Type(), src.Len(), src.Len())
+ for i := 0; i < src.Len(); i++ {
+ v := src.Index(i)
+ reflect.Copy(slice.Index(i), v)
+ }
+
+ dst.Set(slice)
+ return nil
+}
+
+// requireAssignable assures that `dest` is a pointer and it's not an interface.
+func requireAssignable(dst, src reflect.Value) error {
+ if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface {
+ return fmt.Errorf("abi: cannot unmarshal %v into %v", src.Type(), dst.Type())
+ }
+ return nil
+}
+
+// requireUnpackKind verifies preconditions for unpacking `args` into `kind`
+func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind,
+ args Arguments) error {
+
+ switch k {
+ case reflect.Struct:
+ case reflect.Slice, reflect.Array:
+ if minLen := args.LengthNonIndexed(); v.Len() < minLen {
+ return fmt.Errorf("abi: insufficient number of elements in the list/array for unpack, want %d, got %d",
+ minLen, v.Len())
+ }
+ default:
+ return fmt.Errorf("abi: cannot unmarshal tuple into %v", t)
+ }
+ return nil
+}
+
+// mapArgNamesToStructFields maps a slice of argument names to struct fields.
+// first round: for each Exportable field that contains a `abi:""` tag
+// and this field name exists in the given argument name list, pair them together.
+// second round: for each argument name that has not been already linked,
+// find what variable is expected to be mapped into, if it exists and has not been
+// used, pair them.
+// Note this function assumes the given value is a struct value.
+func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[string]string, error) {
+ typ := value.Type()
+
+ abi2struct := make(map[string]string)
+ struct2abi := make(map[string]string)
+
+ // first round ~~~
+ for i := 0; i < typ.NumField(); i++ {
+ structFieldName := typ.Field(i).Name
+
+ // skip private struct fields.
+ if structFieldName[:1] != strings.ToUpper(structFieldName[:1]) {
+ continue
+ }
+ // skip fields that have no abi:"" tag.
+ var ok bool
+ var tagName string
+ if tagName, ok = typ.Field(i).Tag.Lookup("abi"); !ok {
+ continue
+ }
+ // check if tag is empty.
+ if tagName == "" {
+ return nil, fmt.Errorf("struct: abi tag in '%s' is empty", structFieldName)
+ }
+ // check which argument field matches with the abi tag.
+ found := false
+ for _, arg := range argNames {
+ if arg == tagName {
+ if abi2struct[arg] != "" {
+ return nil, fmt.Errorf("struct: abi tag in '%s' already mapped", structFieldName)
+ }
+ // pair them
+ abi2struct[arg] = structFieldName
+ struct2abi[structFieldName] = arg
+ found = true
+ }
+ }
+ // check if this tag has been mapped.
+ if !found {
+ return nil, fmt.Errorf("struct: abi tag '%s' defined but not found in abi", tagName)
+ }
+ }
+
+ // second round ~~~
+ for _, argName := range argNames {
+
+ structFieldName := ToCamelCase(argName)
+
+ if structFieldName == "" {
+ return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct")
+ }
+
+ // this abi has already been paired, skip it... unless there exists another, yet unassigned
+ // struct field with the same field name. If so, raise an error:
+ // abi: [ { "name": "value" } ]
+ // struct { Value *big.Int , Value1 *big.Int `abi:"value"`}
+ if abi2struct[argName] != "" {
+ if abi2struct[argName] != structFieldName &&
+ struct2abi[structFieldName] == "" &&
+ value.FieldByName(structFieldName).IsValid() {
+ return nil, fmt.Errorf("abi: multiple variables maps to the same abi field '%s'", argName)
+ }
+ continue
+ }
+
+ // return an error if this struct field has already been paired.
+ if struct2abi[structFieldName] != "" {
+ return nil, fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", structFieldName)
+ }
+
+ if value.FieldByName(structFieldName).IsValid() {
+ // pair them
+ abi2struct[argName] = structFieldName
+ struct2abi[structFieldName] = argName
+ } else {
+ // not paired, but annotate as used, to detect cases like
+ // abi : [ { "name": "value" }, { "name": "_value" } ]
+ // struct { Value *big.Int }
+ struct2abi[structFieldName] = argName
+ }
+ }
+ return abi2struct, nil
+}
diff --git a/accounts/abi/type.go b/accounts/abi/type.go
new file mode 100644
index 0000000..597d314
--- /dev/null
+++ b/accounts/abi/type.go
@@ -0,0 +1,348 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "errors"
+ "fmt"
+ "reflect"
+ "regexp"
+ "strconv"
+ "strings"
+)
+
+// Type enumerator
+const (
+ IntTy byte = iota
+ UintTy
+ BoolTy
+ StringTy
+ SliceTy
+ ArrayTy
+ TupleTy
+ AddressTy
+ FixedBytesTy
+ BytesTy
+ HashTy
+ FixedPointTy
+ FunctionTy
+)
+
+// Type is the reflection of the supported argument type
+type Type struct {
+ Elem *Type
+ Kind reflect.Kind
+ Type reflect.Type
+ Size int
+ T byte // Our own type checking
+
+ stringKind string // holds the unparsed string for deriving signatures
+
+ // Tuple relative fields
+ TupleElems []*Type // Type information of all tuple fields
+ TupleRawNames []string // Raw field name of all tuple fields
+}
+
+var (
+ // typeRegex parses the abi sub types
+ typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?")
+)
+
+// NewType creates a new reflection type of abi type given in t.
+func NewType(t string, components []ArgumentMarshaling) (typ Type, err error) {
+ // check that array brackets are equal if they exist
+ if strings.Count(t, "[") != strings.Count(t, "]") {
+ return Type{}, fmt.Errorf("invalid arg type in abi")
+ }
+ typ.stringKind = t
+
+ // if there are brackets, get ready to go into slice/array mode and
+ // recursively create the type
+ if strings.Count(t, "[") != 0 {
+ i := strings.LastIndex(t, "[")
+ // recursively embed the type
+ embeddedType, err := NewType(t[:i], components)
+ if err != nil {
+ return Type{}, err
+ }
+ // grab the last cell and create a type from there
+ sliced := t[i:]
+ // grab the slice size with regexp
+ re := regexp.MustCompile("[0-9]+")
+ intz := re.FindAllString(sliced, -1)
+
+ if len(intz) == 0 {
+ // is a slice
+ typ.T = SliceTy
+ typ.Kind = reflect.Slice
+ typ.Elem = &embeddedType
+ typ.Type = reflect.SliceOf(embeddedType.Type)
+ typ.stringKind = embeddedType.stringKind + sliced
+ } else if len(intz) == 1 {
+ // is a array
+ typ.T = ArrayTy
+ typ.Kind = reflect.Array
+ typ.Elem = &embeddedType
+ typ.Size, err = strconv.Atoi(intz[0])
+ if err != nil {
+ return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err)
+ }
+ typ.Type = reflect.ArrayOf(typ.Size, embeddedType.Type)
+ typ.stringKind = embeddedType.stringKind + sliced
+ } else {
+ return Type{}, fmt.Errorf("invalid formatting of array type")
+ }
+ return typ, err
+ }
+ // parse the type and size of the abi-type.
+ matches := typeRegex.FindAllStringSubmatch(t, -1)
+ if len(matches) == 0 {
+ return Type{}, fmt.Errorf("invalid type '%v'", t)
+ }
+ parsedType := matches[0]
+
+ // varSize is the size of the variable
+ var varSize int
+ if len(parsedType[3]) > 0 {
+ var err error
+ varSize, err = strconv.Atoi(parsedType[2])
+ if err != nil {
+ return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err)
+ }
+ } else {
+ if parsedType[0] == "uint" || parsedType[0] == "int" {
+ // this should fail because it means that there's something wrong with
+ // the abi type (the compiler should always format it to the size...always)
+ return Type{}, fmt.Errorf("unsupported arg type: %s", t)
+ }
+ }
+ // varType is the parsed abi type
+ switch varType := parsedType[1]; varType {
+ case "int":
+ typ.Kind, typ.Type = reflectIntKindAndType(false, varSize)
+ typ.Size = varSize
+ typ.T = IntTy
+ case "uint":
+ typ.Kind, typ.Type = reflectIntKindAndType(true, varSize)
+ typ.Size = varSize
+ typ.T = UintTy
+ case "bool":
+ typ.Kind = reflect.Bool
+ typ.T = BoolTy
+ typ.Type = reflect.TypeOf(bool(false))
+ case "address":
+ typ.Kind = reflect.Array
+ typ.Type = addressT
+ typ.Size = 20
+ typ.T = AddressTy
+ case "string":
+ typ.Kind = reflect.String
+ typ.Type = reflect.TypeOf("")
+ typ.T = StringTy
+ case "bytes":
+ if varSize == 0 {
+ typ.T = BytesTy
+ typ.Kind = reflect.Slice
+ typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0)))
+ } else {
+ typ.T = FixedBytesTy
+ typ.Kind = reflect.Array
+ typ.Size = varSize
+ typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0)))
+ }
+ case "tuple":
+ var (
+ fields []reflect.StructField
+ elems []*Type
+ names []string
+ expression string // canonical parameter expression
+ )
+ expression += "("
+ for idx, c := range components {
+ cType, err := NewType(c.Type, c.Components)
+ if err != nil {
+ return Type{}, err
+ }
+ if ToCamelCase(c.Name) == "" {
+ return Type{}, errors.New("abi: purely anonymous or underscored field is not supported")
+ }
+ fields = append(fields, reflect.StructField{
+ Name: ToCamelCase(c.Name), // reflect.StructOf will panic for any exported field.
+ Type: cType.Type,
+ Tag: reflect.StructTag("json:\"" + c.Name + "\""),
+ })
+ elems = append(elems, &cType)
+ names = append(names, c.Name)
+ expression += cType.stringKind
+ if idx != len(components)-1 {
+ expression += ","
+ }
+ }
+ expression += ")"
+ typ.Kind = reflect.Struct
+ typ.Type = reflect.StructOf(fields)
+ typ.TupleElems = elems
+ typ.TupleRawNames = names
+ typ.T = TupleTy
+ typ.stringKind = expression
+ case "function":
+ typ.Kind = reflect.Array
+ typ.T = FunctionTy
+ typ.Size = 24
+ typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
+ default:
+ return Type{}, fmt.Errorf("unsupported arg type: %s", t)
+ }
+
+ return
+}
+
+// String implements Stringer
+func (t Type) String() (out string) {
+ return t.stringKind
+}
+
+func (t Type) pack(v reflect.Value) ([]byte, error) {
+ // dereference pointer first if it's a pointer
+ v = indirect(v)
+ if err := typeCheck(t, v); err != nil {
+ return nil, err
+ }
+
+ switch t.T {
+ case SliceTy, ArrayTy:
+ var ret []byte
+
+ if t.requiresLengthPrefix() {
+ // append length
+ ret = append(ret, packNum(reflect.ValueOf(v.Len()))...)
+ }
+
+ // calculate offset if any
+ offset := 0
+ offsetReq := isDynamicType(*t.Elem)
+ if offsetReq {
+ offset = getTypeSize(*t.Elem) * v.Len()
+ }
+ var tail []byte
+ for i := 0; i < v.Len(); i++ {
+ val, err := t.Elem.pack(v.Index(i))
+ if err != nil {
+ return nil, err
+ }
+ if !offsetReq {
+ ret = append(ret, val...)
+ continue
+ }
+ ret = append(ret, packNum(reflect.ValueOf(offset))...)
+ offset += len(val)
+ tail = append(tail, val...)
+ }
+ return append(ret, tail...), nil
+ case TupleTy:
+ // (T1,...,Tk) for k >= 0 and any types T1, …, Tk
+ // enc(X) = head(X(1)) ... head(X(k)) tail(X(1)) ... tail(X(k))
+ // where X = (X(1), ..., X(k)) and head and tail are defined for Ti being a static
+ // type as
+ // head(X(i)) = enc(X(i)) and tail(X(i)) = "" (the empty string)
+ // and as
+ // head(X(i)) = enc(len(head(X(1)) ... head(X(k)) tail(X(1)) ... tail(X(i-1))))
+ // tail(X(i)) = enc(X(i))
+ // otherwise, i.e. if Ti is a dynamic type.
+ fieldmap, err := mapArgNamesToStructFields(t.TupleRawNames, v)
+ if err != nil {
+ return nil, err
+ }
+ // Calculate prefix occupied size.
+ offset := 0
+ for _, elem := range t.TupleElems {
+ offset += getTypeSize(*elem)
+ }
+ var ret, tail []byte
+ for i, elem := range t.TupleElems {
+ field := v.FieldByName(fieldmap[t.TupleRawNames[i]])
+ if !field.IsValid() {
+ return nil, fmt.Errorf("field %s for tuple not found in the given struct", t.TupleRawNames[i])
+ }
+ val, err := elem.pack(field)
+ if err != nil {
+ return nil, err
+ }
+ if isDynamicType(*elem) {
+ ret = append(ret, packNum(reflect.ValueOf(offset))...)
+ tail = append(tail, val...)
+ offset += len(val)
+ } else {
+ ret = append(ret, val...)
+ }
+ }
+ return append(ret, tail...), nil
+
+ default:
+ return packElement(t, v), nil
+ }
+}
+
+// requireLengthPrefix returns whether the type requires any sort of length
+// prefixing.
+func (t Type) requiresLengthPrefix() bool {
+ return t.T == StringTy || t.T == BytesTy || t.T == SliceTy
+}
+
+// isDynamicType returns true if the type is dynamic.
+// The following types are called “dynamic”:
+// * bytes
+// * string
+// * T[] for any T
+// * T[k] for any dynamic T and any k >= 0
+// * (T1,...,Tk) if Ti is dynamic for some 1 <= i <= k
+func isDynamicType(t Type) bool {
+ if t.T == TupleTy {
+ for _, elem := range t.TupleElems {
+ if isDynamicType(*elem) {
+ return true
+ }
+ }
+ return false
+ }
+ return t.T == StringTy || t.T == BytesTy || t.T == SliceTy || (t.T == ArrayTy && isDynamicType(*t.Elem))
+}
+
+// getTypeSize returns the size that this type needs to occupy.
+// We distinguish static and dynamic types. Static types are encoded in-place
+// and dynamic types are encoded at a separately allocated location after the
+// current block.
+// So for a static variable, the size returned represents the size that the
+// variable actually occupies.
+// For a dynamic variable, the returned size is fixed 32 bytes, which is used
+// to store the location reference for actual value storage.
+func getTypeSize(t Type) int {
+ if t.T == ArrayTy && !isDynamicType(*t.Elem) {
+ // Recursively calculate type size if it is a nested array
+ if t.Elem.T == ArrayTy {
+ return t.Size * getTypeSize(*t.Elem)
+ }
+ return t.Size * 32
+ } else if t.T == TupleTy && !isDynamicType(t) {
+ total := 0
+ for _, elem := range t.TupleElems {
+ total += getTypeSize(*elem)
+ }
+ return total
+ }
+ return 32
+}
diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go
new file mode 100644
index 0000000..d3cd310
--- /dev/null
+++ b/accounts/abi/unpack.go
@@ -0,0 +1,295 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package abi
+
+import (
+ "encoding/binary"
+ "fmt"
+ "math/big"
+ "reflect"
+
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+var (
+ maxUint256 = big.NewInt(0).Add(
+ big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil),
+ big.NewInt(-1))
+ maxInt256 = big.NewInt(0).Add(
+ big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil),
+ big.NewInt(-1))
+)
+
+// reads the integer based on its kind
+func readInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
+ switch kind {
+ case reflect.Uint8:
+ return b[len(b)-1]
+ case reflect.Uint16:
+ return binary.BigEndian.Uint16(b[len(b)-2:])
+ case reflect.Uint32:
+ return binary.BigEndian.Uint32(b[len(b)-4:])
+ case reflect.Uint64:
+ return binary.BigEndian.Uint64(b[len(b)-8:])
+ case reflect.Int8:
+ return int8(b[len(b)-1])
+ case reflect.Int16:
+ return int16(binary.BigEndian.Uint16(b[len(b)-2:]))
+ case reflect.Int32:
+ return int32(binary.BigEndian.Uint32(b[len(b)-4:]))
+ case reflect.Int64:
+ return int64(binary.BigEndian.Uint64(b[len(b)-8:]))
+ default:
+ // the only case lefts for integer is int256/uint256.
+ // big.SetBytes can't tell if a number is negative, positive on itself.
+ // On EVM, if the returned number > max int256, it is negative.
+ ret := new(big.Int).SetBytes(b)
+ if typ == UintTy {
+ return ret
+ }
+
+ if ret.Cmp(maxInt256) > 0 {
+ ret.Add(maxUint256, big.NewInt(0).Neg(ret))
+ ret.Add(ret, big.NewInt(1))
+ ret.Neg(ret)
+ }
+ return ret
+ }
+}
+
+// reads a bool
+func readBool(word []byte) (bool, error) {
+ for _, b := range word[:31] {
+ if b != 0 {
+ return false, errBadBool
+ }
+ }
+ switch word[31] {
+ case 0:
+ return false, nil
+ case 1:
+ return true, nil
+ default:
+ return false, errBadBool
+ }
+}
+
+// A function type is simply the address with the function selection signature at the end.
+// This enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
+func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
+ if t.T != FunctionTy {
+ return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array")
+ }
+ if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
+ err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
+ } else {
+ copy(funcTy[:], word[0:24])
+ }
+ return
+}
+
+// through reflection, creates a fixed array to be read from
+func readFixedBytes(t Type, word []byte) (interface{}, error) {
+ if t.T != FixedBytesTy {
+ return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
+ }
+ // convert
+ array := reflect.New(t.Type).Elem()
+
+ reflect.Copy(array, reflect.ValueOf(word[0:t.Size]))
+ return array.Interface(), nil
+
+}
+
+// iteratively unpack elements
+func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) {
+ if size < 0 {
+ return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size)
+ }
+ if start+32*size > len(output) {
+ return nil, fmt.Errorf("abi: cannot marshal in to go array: offset %d would go over slice boundary (len=%d)", len(output), start+32*size)
+ }
+
+ // this value will become our slice or our array, depending on the type
+ var refSlice reflect.Value
+
+ if t.T == SliceTy {
+ // declare our slice
+ refSlice = reflect.MakeSlice(t.Type, size, size)
+ } else if t.T == ArrayTy {
+ // declare our array
+ refSlice = reflect.New(t.Type).Elem()
+ } else {
+ return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
+ }
+
+ // Arrays have packed elements, resulting in longer unpack steps.
+ // Slices have just 32 bytes per element (pointing to the contents).
+ elemSize := getTypeSize(*t.Elem)
+
+ for i, j := start, 0; j < size; i, j = i+elemSize, j+1 {
+ inter, err := toGoType(i, *t.Elem, output)
+ if err != nil {
+ return nil, err
+ }
+
+ // append the item to our reflect slice
+ refSlice.Index(j).Set(reflect.ValueOf(inter))
+ }
+
+ // return the interface
+ return refSlice.Interface(), nil
+}
+
+func forTupleUnpack(t Type, output []byte) (interface{}, error) {
+ retval := reflect.New(t.Type).Elem()
+ virtualArgs := 0
+ for index, elem := range t.TupleElems {
+ marshalledValue, err := toGoType((index+virtualArgs)*32, *elem, output)
+ if elem.T == ArrayTy && !isDynamicType(*elem) {
+ // If we have a static array, like [3]uint256, these are coded as
+ // just like uint256,uint256,uint256.
+ // This means that we need to add two 'virtual' arguments when
+ // we count the index from now on.
+ //
+ // Array values nested multiple levels deep are also encoded inline:
+ // [2][3]uint256: uint256,uint256,uint256,uint256,uint256,uint256
+ //
+ // Calculate the full array size to get the correct offset for the next argument.
+ // Decrement it by 1, as the normal index increment is still applied.
+ virtualArgs += getTypeSize(*elem)/32 - 1
+ } else if elem.T == TupleTy && !isDynamicType(*elem) {
+ // If we have a static tuple, like (uint256, bool, uint256), these are
+ // coded as just like uint256,bool,uint256
+ virtualArgs += getTypeSize(*elem)/32 - 1
+ }
+ if err != nil {
+ return nil, err
+ }
+ retval.Field(index).Set(reflect.ValueOf(marshalledValue))
+ }
+ return retval.Interface(), nil
+}
+
+// toGoType parses the output bytes and recursively assigns the value of these bytes
+// into a go type with accordance with the ABI spec.
+func toGoType(index int, t Type, output []byte) (interface{}, error) {
+ if index+32 > len(output) {
+ return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32)
+ }
+
+ var (
+ returnOutput []byte
+ begin, length int
+ err error
+ )
+
+ // if we require a length prefix, find the beginning word and size returned.
+ if t.requiresLengthPrefix() {
+ begin, length, err = lengthPrefixPointsTo(index, output)
+ if err != nil {
+ return nil, err
+ }
+ } else {
+ returnOutput = output[index : index+32]
+ }
+
+ switch t.T {
+ case TupleTy:
+ if isDynamicType(t) {
+ begin, err := tuplePointsTo(index, output)
+ if err != nil {
+ return nil, err
+ }
+ return forTupleUnpack(t, output[begin:])
+ } else {
+ return forTupleUnpack(t, output[index:])
+ }
+ case SliceTy:
+ return forEachUnpack(t, output[begin:], 0, length)
+ case ArrayTy:
+ if isDynamicType(*t.Elem) {
+ offset := int64(binary.BigEndian.Uint64(returnOutput[len(returnOutput)-8:]))
+ return forEachUnpack(t, output[offset:], 0, t.Size)
+ }
+ return forEachUnpack(t, output[index:], 0, t.Size)
+ case StringTy: // variable arrays are written at the end of the return bytes
+ return string(output[begin : begin+length]), nil
+ case IntTy, UintTy:
+ return readInteger(t.T, t.Kind, returnOutput), nil
+ case BoolTy:
+ return readBool(returnOutput)
+ case AddressTy:
+ return common.BytesToAddress(returnOutput), nil
+ case HashTy:
+ return common.BytesToHash(returnOutput), nil
+ case BytesTy:
+ return output[begin : begin+length], nil
+ case FixedBytesTy:
+ return readFixedBytes(t, returnOutput)
+ case FunctionTy:
+ return readFunctionType(t, returnOutput)
+ default:
+ return nil, fmt.Errorf("abi: unknown type %v", t.T)
+ }
+}
+
+// interprets a 32 byte slice as an offset and then determines which indice to look to decode the type.
+func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) {
+ bigOffsetEnd := big.NewInt(0).SetBytes(output[index : index+32])
+ bigOffsetEnd.Add(bigOffsetEnd, common.Big32)
+ outputLength := big.NewInt(int64(len(output)))
+
+ if bigOffsetEnd.Cmp(outputLength) > 0 {
+ return 0, 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %v would go over slice boundary (len=%v)", bigOffsetEnd, outputLength)
+ }
+
+ if bigOffsetEnd.BitLen() > 63 {
+ return 0, 0, fmt.Errorf("abi offset larger than int64: %v", bigOffsetEnd)
+ }
+
+ offsetEnd := int(bigOffsetEnd.Uint64())
+ lengthBig := big.NewInt(0).SetBytes(output[offsetEnd-32 : offsetEnd])
+
+ totalSize := big.NewInt(0)
+ totalSize.Add(totalSize, bigOffsetEnd)
+ totalSize.Add(totalSize, lengthBig)
+ if totalSize.BitLen() > 63 {
+ return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize)
+ }
+
+ if totalSize.Cmp(outputLength) > 0 {
+ return 0, 0, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %v require %v", outputLength, totalSize)
+ }
+ start = int(bigOffsetEnd.Uint64())
+ length = int(lengthBig.Uint64())
+ return
+}
+
+// tuplePointsTo resolves the location reference for dynamic tuple.
+func tuplePointsTo(index int, output []byte) (start int, err error) {
+ offset := big.NewInt(0).SetBytes(output[index : index+32])
+ outputLen := big.NewInt(int64(len(output)))
+
+ if offset.Cmp(big.NewInt(int64(len(output)))) > 0 {
+ return 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %v would go over slice boundary (len=%v)", offset, outputLen)
+ }
+ if offset.BitLen() > 63 {
+ return 0, fmt.Errorf("abi offset larger than int64: %v", offset)
+ }
+ return int(offset.Uint64()), nil
+}
diff --git a/accounts/accounts.go b/accounts/accounts.go
new file mode 100644
index 0000000..bca3223
--- /dev/null
+++ b/accounts/accounts.go
@@ -0,0 +1,222 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package accounts implements high level Ethereum account management.
+package accounts
+
+import (
+ "fmt"
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/types"
+ ethereum "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/event"
+ "golang.org/x/crypto/sha3"
+)
+
+// Account represents an Ethereum account located at a specific location defined
+// by the optional URL field.
+type Account struct {
+ Address common.Address `json:"address"` // Ethereum account address derived from the key
+ URL URL `json:"url"` // Optional resource locator within a backend
+}
+
+const (
+ MimetypeDataWithValidator = "data/validator"
+ MimetypeTypedData = "data/typed"
+ MimetypeClique = "application/x-clique-header"
+ MimetypeTextPlain = "text/plain"
+)
+
+// Wallet represents a software or hardware wallet that might contain one or more
+// accounts (derived from the same seed).
+type Wallet interface {
+ // URL retrieves the canonical path under which this wallet is reachable. It is
+ // user by upper layers to define a sorting order over all wallets from multiple
+ // backends.
+ URL() URL
+
+ // Status returns a textual status to aid the user in the current state of the
+ // wallet. It also returns an error indicating any failure the wallet might have
+ // encountered.
+ Status() (string, error)
+
+ // Open initializes access to a wallet instance. It is not meant to unlock or
+ // decrypt account keys, rather simply to establish a connection to hardware
+ // wallets and/or to access derivation seeds.
+ //
+ // The passphrase parameter may or may not be used by the implementation of a
+ // particular wallet instance. The reason there is no passwordless open method
+ // is to strive towards a uniform wallet handling, oblivious to the different
+ // backend providers.
+ //
+ // Please note, if you open a wallet, you must close it to release any allocated
+ // resources (especially important when working with hardware wallets).
+ Open(passphrase string) error
+
+ // Close releases any resources held by an open wallet instance.
+ Close() error
+
+ // Accounts retrieves the list of signing accounts the wallet is currently aware
+ // of. For hierarchical deterministic wallets, the list will not be exhaustive,
+ // rather only contain the accounts explicitly pinned during account derivation.
+ Accounts() []Account
+
+ // Contains returns whether an account is part of this particular wallet or not.
+ Contains(account Account) bool
+
+ // Derive attempts to explicitly derive a hierarchical deterministic account at
+ // the specified derivation path. If requested, the derived account will be added
+ // to the wallet's tracked account list.
+ Derive(path DerivationPath, pin bool) (Account, error)
+
+ // SelfDerive sets a base account derivation path from which the wallet attempts
+ // to discover non zero accounts and automatically add them to list of tracked
+ // accounts.
+ //
+ // Note, self derivaton will increment the last component of the specified path
+ // opposed to decending into a child path to allow discovering accounts starting
+ // from non zero components.
+ //
+ // Some hardware wallets switched derivation paths through their evolution, so
+ // this method supports providing multiple bases to discover old user accounts
+ // too. Only the last base will be used to derive the next empty account.
+ //
+ // You can disable automatic account discovery by calling SelfDerive with a nil
+ // chain state reader.
+ SelfDerive(bases []DerivationPath, chain ethereum.ChainStateReader)
+
+ // SignData requests the wallet to sign the hash of the given data
+ // It looks up the account specified either solely via its address contained within,
+ // or optionally with the aid of any location metadata from the embedded URL field.
+ //
+ // If the wallet requires additional authentication to sign the request (e.g.
+ // a password to decrypt the account, or a PIN code o verify the transaction),
+ // an AuthNeededError instance will be returned, containing infos for the user
+ // about which fields or actions are needed. The user may retry by providing
+ // the needed details via SignDataWithPassphrase, or by other means (e.g. unlock
+ // the account in a keystore).
+ SignData(account Account, mimeType string, data []byte) ([]byte, error)
+
+ // SignDataWithPassphrase is identical to SignData, but also takes a password
+ // NOTE: there's an chance that an erroneous call might mistake the two strings, and
+ // supply password in the mimetype field, or vice versa. Thus, an implementation
+ // should never echo the mimetype or return the mimetype in the error-response
+ SignDataWithPassphrase(account Account, passphrase, mimeType string, data []byte) ([]byte, error)
+
+ // SignText requests the wallet to sign the hash of a given piece of data, prefixed
+ // by the Ethereum prefix scheme
+ // It looks up the account specified either solely via its address contained within,
+ // or optionally with the aid of any location metadata from the embedded URL field.
+ //
+ // If the wallet requires additional authentication to sign the request (e.g.
+ // a password to decrypt the account, or a PIN code o verify the transaction),
+ // an AuthNeededError instance will be returned, containing infos for the user
+ // about which fields or actions are needed. The user may retry by providing
+ // the needed details via SignHashWithPassphrase, or by other means (e.g. unlock
+ // the account in a keystore).
+ SignText(account Account, text []byte) ([]byte, error)
+
+ // SignTextWithPassphrase is identical to Signtext, but also takes a password
+ SignTextWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
+
+ // SignTx requests the wallet to sign the given transaction.
+ //
+ // It looks up the account specified either solely via its address contained within,
+ // or optionally with the aid of any location metadata from the embedded URL field.
+ //
+ // If the wallet requires additional authentication to sign the request (e.g.
+ // a password to decrypt the account, or a PIN code to verify the transaction),
+ // an AuthNeededError instance will be returned, containing infos for the user
+ // about which fields or actions are needed. The user may retry by providing
+ // the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
+ // the account in a keystore).
+ SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
+
+ // SignTxWithPassphrase is identical to SignTx, but also takes a password
+ SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
+}
+
+// Backend is a "wallet provider" that may contain a batch of accounts they can
+// sign transactions with and upon request, do so.
+type Backend interface {
+ // Wallets retrieves the list of wallets the backend is currently aware of.
+ //
+ // The returned wallets are not opened by default. For software HD wallets this
+ // means that no base seeds are decrypted, and for hardware wallets that no actual
+ // connection is established.
+ //
+ // The resulting wallet list will be sorted alphabetically based on its internal
+ // URL assigned by the backend. Since wallets (especially hardware) may come and
+ // go, the same wallet might appear at a different positions in the list during
+ // subsequent retrievals.
+ Wallets() []Wallet
+
+ // Subscribe creates an async subscription to receive notifications when the
+ // backend detects the arrival or departure of a wallet.
+ Subscribe(sink chan<- WalletEvent) event.Subscription
+}
+
+// TextHash is a helper function that calculates a hash for the given message that can be
+// safely used to calculate a signature from.
+//
+// The hash is calulcated as
+// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
+//
+// This gives context to the signed message and prevents signing of transactions.
+func TextHash(data []byte) []byte {
+ hash, _ := TextAndHash(data)
+ return hash
+}
+
+// TextAndHash is a helper function that calculates a hash for the given message that can be
+// safely used to calculate a signature from.
+//
+// The hash is calulcated as
+// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
+//
+// This gives context to the signed message and prevents signing of transactions.
+func TextAndHash(data []byte) ([]byte, string) {
+ msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), string(data))
+ hasher := sha3.NewLegacyKeccak256()
+ hasher.Write([]byte(msg))
+ return hasher.Sum(nil), msg
+}
+
+// WalletEventType represents the different event types that can be fired by
+// the wallet subscription subsystem.
+type WalletEventType int
+
+const (
+ // WalletArrived is fired when a new wallet is detected either via USB or via
+ // a filesystem event in the keystore.
+ WalletArrived WalletEventType = iota
+
+ // WalletOpened is fired when a wallet is successfully opened with the purpose
+ // of starting any background processes such as automatic key derivation.
+ WalletOpened
+
+ // WalletDropped
+ WalletDropped
+)
+
+// WalletEvent is an event fired by an account backend when a wallet arrival or
+// departure is detected.
+type WalletEvent struct {
+ Wallet Wallet // Wallet instance arrived or departed
+ Kind WalletEventType // Event type that happened in the system
+}
diff --git a/accounts/errors.go b/accounts/errors.go
new file mode 100644
index 0000000..2fed35f
--- /dev/null
+++ b/accounts/errors.go
@@ -0,0 +1,68 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package accounts
+
+import (
+ "errors"
+ "fmt"
+)
+
+// ErrUnknownAccount is returned for any requested operation for which no backend
+// provides the specified account.
+var ErrUnknownAccount = errors.New("unknown account")
+
+// ErrUnknownWallet is returned for any requested operation for which no backend
+// provides the specified wallet.
+var ErrUnknownWallet = errors.New("unknown wallet")
+
+// ErrNotSupported is returned when an operation is requested from an account
+// backend that it does not support.
+var ErrNotSupported = errors.New("not supported")
+
+// ErrInvalidPassphrase is returned when a decryption operation receives a bad
+// passphrase.
+var ErrInvalidPassphrase = errors.New("invalid password")
+
+// ErrWalletAlreadyOpen is returned if a wallet is attempted to be opened the
+// second time.
+var ErrWalletAlreadyOpen = errors.New("wallet already open")
+
+// ErrWalletClosed is returned if a wallet is attempted to be opened the
+// secodn time.
+var ErrWalletClosed = errors.New("wallet closed")
+
+// AuthNeededError is returned by backends for signing requests where the user
+// is required to provide further authentication before signing can succeed.
+//
+// This usually means either that a password needs to be supplied, or perhaps a
+// one time PIN code displayed by some hardware device.
+type AuthNeededError struct {
+ Needed string // Extra authentication the user needs to provide
+}
+
+// NewAuthNeededError creates a new authentication error with the extra details
+// about the needed fields set.
+func NewAuthNeededError(needed string) error {
+ return &AuthNeededError{
+ Needed: needed,
+ }
+}
+
+// Error implements the standard error interface.
+func (err *AuthNeededError) Error() string {
+ return fmt.Sprintf("authentication needed: %s", err.Needed)
+}
diff --git a/accounts/external/backend.go b/accounts/external/backend.go
new file mode 100644
index 0000000..16e201d
--- /dev/null
+++ b/accounts/external/backend.go
@@ -0,0 +1,231 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package external
+
+import (
+ "fmt"
+ "math/big"
+ "sync"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/internal/ethapi"
+ "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/event"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/signer/core"
+)
+
+type ExternalBackend struct {
+ signers []accounts.Wallet
+}
+
+func (eb *ExternalBackend) Wallets() []accounts.Wallet {
+ return eb.signers
+}
+
+func NewExternalBackend(endpoint string) (*ExternalBackend, error) {
+ signer, err := NewExternalSigner(endpoint)
+ if err != nil {
+ return nil, err
+ }
+ return &ExternalBackend{
+ signers: []accounts.Wallet{signer},
+ }, nil
+}
+
+func (eb *ExternalBackend) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription {
+ return event.NewSubscription(func(quit <-chan struct{}) error {
+ <-quit
+ return nil
+ })
+}
+
+// ExternalSigner provides an API to interact with an external signer (clef)
+// It proxies request to the external signer while forwarding relevant
+// request headers
+type ExternalSigner struct {
+ client *rpc.Client
+ endpoint string
+ status string
+ cacheMu sync.RWMutex
+ cache []accounts.Account
+}
+
+func NewExternalSigner(endpoint string) (*ExternalSigner, error) {
+ client, err := rpc.Dial(endpoint)
+ if err != nil {
+ return nil, err
+ }
+ extsigner := &ExternalSigner{
+ client: client,
+ endpoint: endpoint,
+ }
+ // Check if reachable
+ version, err := extsigner.pingVersion()
+ if err != nil {
+ return nil, err
+ }
+ extsigner.status = fmt.Sprintf("ok [version=%v]", version)
+ return extsigner, nil
+}
+
+func (api *ExternalSigner) URL() accounts.URL {
+ return accounts.URL{
+ Scheme: "extapi",
+ Path: api.endpoint,
+ }
+}
+
+func (api *ExternalSigner) Status() (string, error) {
+ return api.status, nil
+}
+
+func (api *ExternalSigner) Open(passphrase string) error {
+ return fmt.Errorf("operation not supported on external signers")
+}
+
+func (api *ExternalSigner) Close() error {
+ return fmt.Errorf("operation not supported on external signers")
+}
+
+func (api *ExternalSigner) Accounts() []accounts.Account {
+ var accnts []accounts.Account
+ res, err := api.listAccounts()
+ if err != nil {
+ log.Error("account listing failed", "error", err)
+ return accnts
+ }
+ for _, addr := range res {
+ accnts = append(accnts, accounts.Account{
+ URL: accounts.URL{
+ Scheme: "extapi",
+ Path: api.endpoint,
+ },
+ Address: addr,
+ })
+ }
+ api.cacheMu.Lock()
+ api.cache = accnts
+ api.cacheMu.Unlock()
+ return accnts
+}
+
+func (api *ExternalSigner) Contains(account accounts.Account) bool {
+ api.cacheMu.RLock()
+ defer api.cacheMu.RUnlock()
+ for _, a := range api.cache {
+ if a.Address == account.Address && (account.URL == (accounts.URL{}) || account.URL == api.URL()) {
+ return true
+ }
+ }
+ return false
+}
+
+func (api *ExternalSigner) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
+ return accounts.Account{}, fmt.Errorf("operation not supported on external signers")
+}
+
+func (api *ExternalSigner) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
+ log.Error("operation SelfDerive not supported on external signers")
+}
+
+func (api *ExternalSigner) signHash(account accounts.Account, hash []byte) ([]byte, error) {
+ return []byte{}, fmt.Errorf("operation not supported on external signers")
+}
+
+// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
+func (api *ExternalSigner) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
+ var res hexutil.Bytes
+ var signAddress = common.NewMixedcaseAddress(account.Address)
+ if err := api.client.Call(&res, "account_signData",
+ mimeType,
+ &signAddress, // Need to use the pointer here, because of how MarshalJSON is defined
+ hexutil.Encode(data)); err != nil {
+ return nil, err
+ }
+ // If V is on 27/28-form, convert to to 0/1 for Clique
+ if mimeType == accounts.MimetypeClique && (res[64] == 27 || res[64] == 28) {
+ res[64] -= 27 // Transform V from 27/28 to 0/1 for Clique use
+ }
+ return res, nil
+}
+
+func (api *ExternalSigner) SignText(account accounts.Account, text []byte) ([]byte, error) {
+ var res hexutil.Bytes
+ var signAddress = common.NewMixedcaseAddress(account.Address)
+ if err := api.client.Call(&res, "account_signData",
+ accounts.MimetypeTextPlain,
+ &signAddress, // Need to use the pointer here, because of how MarshalJSON is defined
+ hexutil.Encode(text)); err != nil {
+ return nil, err
+ }
+ return res, nil
+}
+
+func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ res := ethapi.SignTransactionResult{}
+ data := hexutil.Bytes(tx.Data())
+ var to *common.MixedcaseAddress
+ if tx.To() != nil {
+ t := common.NewMixedcaseAddress(*tx.To())
+ to = &t
+ }
+ args := &core.SendTxArgs{
+ Data: &data,
+ Nonce: hexutil.Uint64(tx.Nonce()),
+ Value: hexutil.Big(*tx.Value()),
+ Gas: hexutil.Uint64(tx.Gas()),
+ GasPrice: hexutil.Big(*tx.GasPrice()),
+ To: to,
+ From: common.NewMixedcaseAddress(account.Address),
+ }
+ if err := api.client.Call(&res, "account_signTransaction", args); err != nil {
+ return nil, err
+ }
+ return res.Tx, nil
+}
+
+func (api *ExternalSigner) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
+ return []byte{}, fmt.Errorf("password-operations not supported on external signers")
+}
+
+func (api *ExternalSigner) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ return nil, fmt.Errorf("password-operations not supported on external signers")
+}
+func (api *ExternalSigner) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
+ return nil, fmt.Errorf("password-operations not supported on external signers")
+}
+
+func (api *ExternalSigner) listAccounts() ([]common.Address, error) {
+ var res []common.Address
+ if err := api.client.Call(&res, "account_list"); err != nil {
+ return nil, err
+ }
+ return res, nil
+}
+
+func (api *ExternalSigner) pingVersion() (string, error) {
+ var v string
+ if err := api.client.Call(&v, "account_version"); err != nil {
+ return "", err
+ }
+ return v, nil
+}
diff --git a/accounts/hd.go b/accounts/hd.go
new file mode 100644
index 0000000..75c4761
--- /dev/null
+++ b/accounts/hd.go
@@ -0,0 +1,152 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package accounts
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "math"
+ "math/big"
+ "strings"
+)
+
+// DefaultRootDerivationPath is the root path to which custom derivation endpoints
+// are appended. As such, the first account will be at m/44'/60'/0'/0, the second
+// at m/44'/60'/0'/1, etc.
+var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
+
+// DefaultBaseDerivationPath is the base path from which custom derivation endpoints
+// are incremented. As such, the first account will be at m/44'/60'/0'/0/0, the second
+// at m/44'/60'/0'/0/1, etc.
+var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}
+
+// LegacyLedgerBaseDerivationPath is the legacy base path from which custom derivation
+// endpoints are incremented. As such, the first account will be at m/44'/60'/0'/0, the
+// second at m/44'/60'/0'/1, etc.
+var LegacyLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
+
+// DerivationPath represents the computer friendly version of a hierarchical
+// deterministic wallet account derivaion path.
+//
+// The BIP-32 spec https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
+// defines derivation paths to be of the form:
+//
+// m / purpose' / coin_type' / account' / change / address_index
+//
+// The BIP-44 spec https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
+// defines that the `purpose` be 44' (or 0x8000002C) for crypto currencies, and
+// SLIP-44 https://github.com/satoshilabs/slips/blob/master/slip-0044.md assigns
+// the `coin_type` 60' (or 0x8000003C) to Ethereum.
+//
+// The root path for Ethereum is m/44'/60'/0'/0 according to the specification
+// from https://github.com/ethereum/EIPs/issues/84, albeit it's not set in stone
+// yet whether accounts should increment the last component or the children of
+// that. We will go with the simpler approach of incrementing the last component.
+type DerivationPath []uint32
+
+// ParseDerivationPath converts a user specified derivation path string to the
+// internal binary representation.
+//
+// Full derivation paths need to start with the `m/` prefix, relative derivation
+// paths (which will get appended to the default root path) must not have prefixes
+// in front of the first element. Whitespace is ignored.
+func ParseDerivationPath(path string) (DerivationPath, error) {
+ var result DerivationPath
+
+ // Handle absolute or relative paths
+ components := strings.Split(path, "/")
+ switch {
+ case len(components) == 0:
+ return nil, errors.New("empty derivation path")
+
+ case strings.TrimSpace(components[0]) == "":
+ return nil, errors.New("ambiguous path: use 'm/' prefix for absolute paths, or no leading '/' for relative ones")
+
+ case strings.TrimSpace(components[0]) == "m":
+ components = components[1:]
+
+ default:
+ result = append(result, DefaultRootDerivationPath...)
+ }
+ // All remaining components are relative, append one by one
+ if len(components) == 0 {
+ return nil, errors.New("empty derivation path") // Empty relative paths
+ }
+ for _, component := range components {
+ // Ignore any user added whitespace
+ component = strings.TrimSpace(component)
+ var value uint32
+
+ // Handle hardened paths
+ if strings.HasSuffix(component, "'") {
+ value = 0x80000000
+ component = strings.TrimSpace(strings.TrimSuffix(component, "'"))
+ }
+ // Handle the non hardened component
+ bigval, ok := new(big.Int).SetString(component, 0)
+ if !ok {
+ return nil, fmt.Errorf("invalid component: %s", component)
+ }
+ max := math.MaxUint32 - value
+ if bigval.Sign() < 0 || bigval.Cmp(big.NewInt(int64(max))) > 0 {
+ if value == 0 {
+ return nil, fmt.Errorf("component %v out of allowed range [0, %d]", bigval, max)
+ }
+ return nil, fmt.Errorf("component %v out of allowed hardened range [0, %d]", bigval, max)
+ }
+ value += uint32(bigval.Uint64())
+
+ // Append and repeat
+ result = append(result, value)
+ }
+ return result, nil
+}
+
+// String implements the stringer interface, converting a binary derivation path
+// to its canonical representation.
+func (path DerivationPath) String() string {
+ result := "m"
+ for _, component := range path {
+ var hardened bool
+ if component >= 0x80000000 {
+ component -= 0x80000000
+ hardened = true
+ }
+ result = fmt.Sprintf("%s/%d", result, component)
+ if hardened {
+ result += "'"
+ }
+ }
+ return result
+}
+
+// MarshalJSON turns a derivation path into its json-serialized string
+func (path DerivationPath) MarshalJSON() ([]byte, error) {
+ return json.Marshal(path.String())
+}
+
+// UnmarshalJSON a json-serialized string back into a derivation path
+func (path *DerivationPath) UnmarshalJSON(b []byte) error {
+ var dp string
+ var err error
+ if err = json.Unmarshal(b, &dp); err != nil {
+ return err
+ }
+ *path, err = ParseDerivationPath(dp)
+ return err
+}
diff --git a/accounts/keystore/account_cache.go b/accounts/keystore/account_cache.go
new file mode 100644
index 0000000..2067ccb
--- /dev/null
+++ b/accounts/keystore/account_cache.go
@@ -0,0 +1,301 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package keystore
+
+import (
+ "bufio"
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/log"
+ mapset "github.com/deckarep/golang-set"
+)
+
+// Minimum amount of time between cache reloads. This limit applies if the platform does
+// not support change notifications. It also applies if the keystore directory does not
+// exist yet, the code will attempt to create a watcher at most this often.
+const minReloadInterval = 2 * time.Second
+
+type accountsByURL []accounts.Account
+
+func (s accountsByURL) Len() int { return len(s) }
+func (s accountsByURL) Less(i, j int) bool { return s[i].URL.Cmp(s[j].URL) < 0 }
+func (s accountsByURL) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+// AmbiguousAddrError is returned when attempting to unlock
+// an address for which more than one file exists.
+type AmbiguousAddrError struct {
+ Addr common.Address
+ Matches []accounts.Account
+}
+
+func (err *AmbiguousAddrError) Error() string {
+ files := ""
+ for i, a := range err.Matches {
+ files += a.URL.Path
+ if i < len(err.Matches)-1 {
+ files += ", "
+ }
+ }
+ return fmt.Sprintf("multiple keys match address (%s)", files)
+}
+
+// accountCache is a live index of all accounts in the keystore.
+type accountCache struct {
+ keydir string
+ watcher *watcher
+ mu sync.Mutex
+ all accountsByURL
+ byAddr map[common.Address][]accounts.Account
+ throttle *time.Timer
+ notify chan struct{}
+ fileC fileCache
+}
+
+func newAccountCache(keydir string) (*accountCache, chan struct{}) {
+ ac := &accountCache{
+ keydir: keydir,
+ byAddr: make(map[common.Address][]accounts.Account),
+ notify: make(chan struct{}, 1),
+ fileC: fileCache{all: mapset.NewThreadUnsafeSet()},
+ }
+ ac.watcher = newWatcher(ac)
+ return ac, ac.notify
+}
+
+func (ac *accountCache) accounts() []accounts.Account {
+ ac.maybeReload()
+ ac.mu.Lock()
+ defer ac.mu.Unlock()
+ cpy := make([]accounts.Account, len(ac.all))
+ copy(cpy, ac.all)
+ return cpy
+}
+
+func (ac *accountCache) hasAddress(addr common.Address) bool {
+ ac.maybeReload()
+ ac.mu.Lock()
+ defer ac.mu.Unlock()
+ return len(ac.byAddr[addr]) > 0
+}
+
+func (ac *accountCache) add(newAccount accounts.Account) {
+ ac.mu.Lock()
+ defer ac.mu.Unlock()
+
+ i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Cmp(newAccount.URL) >= 0 })
+ if i < len(ac.all) && ac.all[i] == newAccount {
+ return
+ }
+ // newAccount is not in the cache.
+ ac.all = append(ac.all, accounts.Account{})
+ copy(ac.all[i+1:], ac.all[i:])
+ ac.all[i] = newAccount
+ ac.byAddr[newAccount.Address] = append(ac.byAddr[newAccount.Address], newAccount)
+}
+
+// note: removed needs to be unique here (i.e. both File and Address must be set).
+func (ac *accountCache) delete(removed accounts.Account) {
+ ac.mu.Lock()
+ defer ac.mu.Unlock()
+
+ ac.all = removeAccount(ac.all, removed)
+ if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 {
+ delete(ac.byAddr, removed.Address)
+ } else {
+ ac.byAddr[removed.Address] = ba
+ }
+}
+
+// deleteByFile removes an account referenced by the given path.
+func (ac *accountCache) deleteByFile(path string) {
+ ac.mu.Lock()
+ defer ac.mu.Unlock()
+ i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Path >= path })
+
+ if i < len(ac.all) && ac.all[i].URL.Path == path {
+ removed := ac.all[i]
+ ac.all = append(ac.all[:i], ac.all[i+1:]...)
+ if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 {
+ delete(ac.byAddr, removed.Address)
+ } else {
+ ac.byAddr[removed.Address] = ba
+ }
+ }
+}
+
+func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account {
+ for i := range slice {
+ if slice[i] == elem {
+ return append(slice[:i], slice[i+1:]...)
+ }
+ }
+ return slice
+}
+
+// find returns the cached account for address if there is a unique match.
+// The exact matching rules are explained by the documentation of accounts.Account.
+// Callers must hold ac.mu.
+func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) {
+ // Limit search to address candidates if possible.
+ matches := ac.all
+ if (a.Address != common.Address{}) {
+ matches = ac.byAddr[a.Address]
+ }
+ if a.URL.Path != "" {
+ // If only the basename is specified, complete the path.
+ if !strings.ContainsRune(a.URL.Path, filepath.Separator) {
+ a.URL.Path = filepath.Join(ac.keydir, a.URL.Path)
+ }
+ for i := range matches {
+ if matches[i].URL == a.URL {
+ return matches[i], nil
+ }
+ }
+ if (a.Address == common.Address{}) {
+ return accounts.Account{}, ErrNoMatch
+ }
+ }
+ switch len(matches) {
+ case 1:
+ return matches[0], nil
+ case 0:
+ return accounts.Account{}, ErrNoMatch
+ default:
+ err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))}
+ copy(err.Matches, matches)
+ sort.Sort(accountsByURL(err.Matches))
+ return accounts.Account{}, err
+ }
+}
+
+func (ac *accountCache) maybeReload() {
+ ac.mu.Lock()
+
+ if ac.watcher.running {
+ ac.mu.Unlock()
+ return // A watcher is running and will keep the cache up-to-date.
+ }
+ if ac.throttle == nil {
+ ac.throttle = time.NewTimer(0)
+ } else {
+ select {
+ case <-ac.throttle.C:
+ default:
+ ac.mu.Unlock()
+ return // The cache was reloaded recently.
+ }
+ }
+ // No watcher running, start it.
+ ac.watcher.start()
+ ac.throttle.Reset(minReloadInterval)
+ ac.mu.Unlock()
+ ac.scanAccounts()
+}
+
+func (ac *accountCache) close() {
+ ac.mu.Lock()
+ ac.watcher.close()
+ if ac.throttle != nil {
+ ac.throttle.Stop()
+ }
+ if ac.notify != nil {
+ close(ac.notify)
+ ac.notify = nil
+ }
+ ac.mu.Unlock()
+}
+
+// scanAccounts checks if any changes have occurred on the filesystem, and
+// updates the account cache accordingly
+func (ac *accountCache) scanAccounts() error {
+ // Scan the entire folder metadata for file changes
+ creates, deletes, updates, err := ac.fileC.scan(ac.keydir)
+ if err != nil {
+ log.Debug("Failed to reload keystore contents", "err", err)
+ return err
+ }
+ if creates.Cardinality() == 0 && deletes.Cardinality() == 0 && updates.Cardinality() == 0 {
+ return nil
+ }
+ // Create a helper method to scan the contents of the key files
+ var (
+ buf = new(bufio.Reader)
+ key struct {
+ Address string `json:"address"`
+ }
+ )
+ readAccount := func(path string) *accounts.Account {
+ fd, err := os.Open(path)
+ if err != nil {
+ log.Trace("Failed to open keystore file", "path", path, "err", err)
+ return nil
+ }
+ defer fd.Close()
+ buf.Reset(fd)
+ // Parse the address.
+ key.Address = ""
+ err = json.NewDecoder(buf).Decode(&key)
+ addr := common.HexToAddress(key.Address)
+ switch {
+ case err != nil:
+ log.Debug("Failed to decode keystore key", "path", path, "err", err)
+ case (addr == common.Address{}):
+ log.Debug("Failed to decode keystore key", "path", path, "err", "missing or zero address")
+ default:
+ return &accounts.Account{
+ Address: addr,
+ URL: accounts.URL{Scheme: KeyStoreScheme, Path: path},
+ }
+ }
+ return nil
+ }
+ // Process all the file diffs
+ start := time.Now()
+
+ for _, p := range creates.ToSlice() {
+ if a := readAccount(p.(string)); a != nil {
+ ac.add(*a)
+ }
+ }
+ for _, p := range deletes.ToSlice() {
+ ac.deleteByFile(p.(string))
+ }
+ for _, p := range updates.ToSlice() {
+ path := p.(string)
+ ac.deleteByFile(path)
+ if a := readAccount(path); a != nil {
+ ac.add(*a)
+ }
+ }
+ end := time.Now()
+
+ select {
+ case ac.notify <- struct{}{}:
+ default:
+ }
+ log.Trace("Handled keystore changes", "time", end.Sub(start))
+ return nil
+}
diff --git a/accounts/keystore/file_cache.go b/accounts/keystore/file_cache.go
new file mode 100644
index 0000000..ac87f0c
--- /dev/null
+++ b/accounts/keystore/file_cache.go
@@ -0,0 +1,102 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package keystore
+
+import (
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+ "time"
+
+ mapset "github.com/deckarep/golang-set"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+// fileCache is a cache of files seen during scan of keystore.
+type fileCache struct {
+ all mapset.Set // Set of all files from the keystore folder
+ lastMod time.Time // Last time instance when a file was modified
+ mu sync.RWMutex
+}
+
+// scan performs a new scan on the given directory, compares against the already
+// cached filenames, and returns file sets: creates, deletes, updates.
+func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) {
+ t0 := time.Now()
+
+ // List all the failes from the keystore folder
+ files, err := ioutil.ReadDir(keyDir)
+ if err != nil {
+ return nil, nil, nil, err
+ }
+ t1 := time.Now()
+
+ fc.mu.Lock()
+ defer fc.mu.Unlock()
+
+ // Iterate all the files and gather their metadata
+ all := mapset.NewThreadUnsafeSet()
+ mods := mapset.NewThreadUnsafeSet()
+
+ var newLastMod time.Time
+ for _, fi := range files {
+ path := filepath.Join(keyDir, fi.Name())
+ // Skip any non-key files from the folder
+ if nonKeyFile(fi) {
+ log.Trace("Ignoring file on account scan", "path", path)
+ continue
+ }
+ // Gather the set of all and fresly modified files
+ all.Add(path)
+
+ modified := fi.ModTime()
+ if modified.After(fc.lastMod) {
+ mods.Add(path)
+ }
+ if modified.After(newLastMod) {
+ newLastMod = modified
+ }
+ }
+ t2 := time.Now()
+
+ // Update the tracked files and return the three sets
+ deletes := fc.all.Difference(all) // Deletes = previous - current
+ creates := all.Difference(fc.all) // Creates = current - previous
+ updates := mods.Difference(creates) // Updates = modified - creates
+
+ fc.all, fc.lastMod = all, newLastMod
+ t3 := time.Now()
+
+ // Report on the scanning stats and return
+ log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
+ return creates, deletes, updates, nil
+}
+
+// nonKeyFile ignores editor backups, hidden files and folders/symlinks.
+func nonKeyFile(fi os.FileInfo) bool {
+ // Skip editor backups and UNIX-style hidden files.
+ if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
+ return true
+ }
+ // Skip misc special files, directories (yes, symlinks too).
+ if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
+ return true
+ }
+ return false
+}
diff --git a/accounts/keystore/key.go b/accounts/keystore/key.go
new file mode 100644
index 0000000..3654daa
--- /dev/null
+++ b/accounts/keystore/key.go
@@ -0,0 +1,232 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package keystore
+
+import (
+ "bytes"
+ "crypto/ecdsa"
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/pborman/uuid"
+)
+
+const (
+ version = 3
+)
+
+type Key struct {
+ Id uuid.UUID // Version 4 "random" for unique id not derived from key data
+ // to simplify lookups we also store the address
+ Address common.Address
+ // we only store privkey as pubkey/address can be derived from it
+ // privkey in this struct is always in plaintext
+ PrivateKey *ecdsa.PrivateKey
+}
+
+type keyStore interface {
+ // Loads and decrypts the key from disk.
+ GetKey(addr common.Address, filename string, auth string) (*Key, error)
+ // Writes and encrypts the key.
+ StoreKey(filename string, k *Key, auth string) error
+ // Joins filename with the key directory unless it is already absolute.
+ JoinPath(filename string) string
+}
+
+type plainKeyJSON struct {
+ Address string `json:"address"`
+ PrivateKey string `json:"privatekey"`
+ Id string `json:"id"`
+ Version int `json:"version"`
+}
+
+type encryptedKeyJSONV3 struct {
+ Address string `json:"address"`
+ Crypto CryptoJSON `json:"crypto"`
+ Id string `json:"id"`
+ Version int `json:"version"`
+}
+
+type encryptedKeyJSONV1 struct {
+ Address string `json:"address"`
+ Crypto CryptoJSON `json:"crypto"`
+ Id string `json:"id"`
+ Version string `json:"version"`
+}
+
+type CryptoJSON struct {
+ Cipher string `json:"cipher"`
+ CipherText string `json:"ciphertext"`
+ CipherParams cipherparamsJSON `json:"cipherparams"`
+ KDF string `json:"kdf"`
+ KDFParams map[string]interface{} `json:"kdfparams"`
+ MAC string `json:"mac"`
+}
+
+type cipherparamsJSON struct {
+ IV string `json:"iv"`
+}
+
+func (k *Key) MarshalJSON() (j []byte, err error) {
+ jStruct := plainKeyJSON{
+ hex.EncodeToString(k.Address[:]),
+ hex.EncodeToString(crypto.FromECDSA(k.PrivateKey)),
+ k.Id.String(),
+ version,
+ }
+ j, err = json.Marshal(jStruct)
+ return j, err
+}
+
+func (k *Key) UnmarshalJSON(j []byte) (err error) {
+ keyJSON := new(plainKeyJSON)
+ err = json.Unmarshal(j, &keyJSON)
+ if err != nil {
+ return err
+ }
+
+ u := new(uuid.UUID)
+ *u = uuid.Parse(keyJSON.Id)
+ k.Id = *u
+ addr, err := hex.DecodeString(keyJSON.Address)
+ if err != nil {
+ return err
+ }
+ privkey, err := crypto.HexToECDSA(keyJSON.PrivateKey)
+ if err != nil {
+ return err
+ }
+
+ k.Address = common.BytesToAddress(addr)
+ k.PrivateKey = privkey
+
+ return nil
+}
+
+func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {
+ id := uuid.NewRandom()
+ key := &Key{
+ Id: id,
+ Address: crypto.PubkeyToAddress(privateKeyECDSA.PublicKey),
+ PrivateKey: privateKeyECDSA,
+ }
+ return key
+}
+
+// NewKeyForDirectICAP generates a key whose address fits into < 155 bits so it can fit
+// into the Direct ICAP spec. for simplicity and easier compatibility with other libs, we
+// retry until the first byte is 0.
+func NewKeyForDirectICAP(rand io.Reader) *Key {
+ randBytes := make([]byte, 64)
+ _, err := rand.Read(randBytes)
+ if err != nil {
+ panic("key generation: could not read from random source: " + err.Error())
+ }
+ reader := bytes.NewReader(randBytes)
+ privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), reader)
+ if err != nil {
+ panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
+ }
+ key := newKeyFromECDSA(privateKeyECDSA)
+ if !strings.HasPrefix(key.Address.Hex(), "0x00") {
+ return NewKeyForDirectICAP(rand)
+ }
+ return key
+}
+
+func newKey(rand io.Reader) (*Key, error) {
+ privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand)
+ if err != nil {
+ return nil, err
+ }
+ return newKeyFromECDSA(privateKeyECDSA), nil
+}
+
+func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Account, error) {
+ key, err := newKey(rand)
+ if err != nil {
+ return nil, accounts.Account{}, err
+ }
+ a := accounts.Account{
+ Address: key.Address,
+ URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.JoinPath(keyFileName(key.Address))},
+ }
+ if err := ks.StoreKey(a.URL.Path, key, auth); err != nil {
+ zeroKey(key.PrivateKey)
+ return nil, a, err
+ }
+ return key, a, err
+}
+
+func writeTemporaryKeyFile(file string, content []byte) (string, error) {
+ // Create the keystore directory with appropriate permissions
+ // in case it is not present yet.
+ const dirPerm = 0700
+ if err := os.MkdirAll(filepath.Dir(file), dirPerm); err != nil {
+ return "", err
+ }
+ // Atomic write: create a temporary hidden file first
+ // then move it into place. TempFile assigns mode 0600.
+ f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
+ if err != nil {
+ return "", err
+ }
+ if _, err := f.Write(content); err != nil {
+ f.Close()
+ os.Remove(f.Name())
+ return "", err
+ }
+ f.Close()
+ return f.Name(), nil
+}
+
+func writeKeyFile(file string, content []byte) error {
+ name, err := writeTemporaryKeyFile(file, content)
+ if err != nil {
+ return err
+ }
+ return os.Rename(name, file)
+}
+
+// keyFileName implements the naming convention for keyfiles:
+// UTC--<created_at UTC ISO8601>-<address hex>
+func keyFileName(keyAddr common.Address) string {
+ ts := time.Now().UTC()
+ return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:]))
+}
+
+func toISO8601(t time.Time) string {
+ var tz string
+ name, offset := t.Zone()
+ if name == "UTC" {
+ tz = "Z"
+ } else {
+ tz = fmt.Sprintf("%03d00", offset/3600)
+ }
+ return fmt.Sprintf("%04d-%02d-%02dT%02d-%02d-%02d.%09d%s",
+ t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
+}
diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go
new file mode 100644
index 0000000..a3ce33f
--- /dev/null
+++ b/accounts/keystore/keystore.go
@@ -0,0 +1,495 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package keystore implements encrypted storage of secp256k1 private keys.
+//
+// Keys are stored as encrypted JSON files according to the Web3 Secret Storage specification.
+// See https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition for more information.
+package keystore
+
+import (
+ "crypto/ecdsa"
+ crand "crypto/rand"
+ "errors"
+ "fmt"
+ "math/big"
+ "os"
+ "path/filepath"
+ "reflect"
+ "runtime"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/event"
+)
+
+var (
+ ErrLocked = accounts.NewAuthNeededError("password or unlock")
+ ErrNoMatch = errors.New("no key for given address or file")
+ ErrDecrypt = errors.New("could not decrypt key with given password")
+)
+
+// KeyStoreType is the reflect type of a keystore backend.
+var KeyStoreType = reflect.TypeOf(&KeyStore{})
+
+// KeyStoreScheme is the protocol scheme prefixing account and wallet URLs.
+const KeyStoreScheme = "keystore"
+
+// Maximum time between wallet refreshes (if filesystem notifications don't work).
+const walletRefreshCycle = 3 * time.Second
+
+// KeyStore manages a key storage directory on disk.
+type KeyStore struct {
+ storage keyStore // Storage backend, might be cleartext or encrypted
+ cache *accountCache // In-memory account cache over the filesystem storage
+ changes chan struct{} // Channel receiving change notifications from the cache
+ unlocked map[common.Address]*unlocked // Currently unlocked account (decrypted private keys)
+
+ wallets []accounts.Wallet // Wallet wrappers around the individual key files
+ updateFeed event.Feed // Event feed to notify wallet additions/removals
+ updateScope event.SubscriptionScope // Subscription scope tracking current live listeners
+ updating bool // Whether the event notification loop is running
+
+ mu sync.RWMutex
+}
+
+type unlocked struct {
+ *Key
+ abort chan struct{}
+}
+
+// NewKeyStore creates a keystore for the given directory.
+func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore {
+ keydir, _ = filepath.Abs(keydir)
+ ks := &KeyStore{storage: &keyStorePassphrase{keydir, scryptN, scryptP, false}}
+ ks.init(keydir)
+ return ks
+}
+
+// NewPlaintextKeyStore creates a keystore for the given directory.
+// Deprecated: Use NewKeyStore.
+func NewPlaintextKeyStore(keydir string) *KeyStore {
+ keydir, _ = filepath.Abs(keydir)
+ ks := &KeyStore{storage: &keyStorePlain{keydir}}
+ ks.init(keydir)
+ return ks
+}
+
+func (ks *KeyStore) init(keydir string) {
+ // Lock the mutex since the account cache might call back with events
+ ks.mu.Lock()
+ defer ks.mu.Unlock()
+
+ // Initialize the set of unlocked keys and the account cache
+ ks.unlocked = make(map[common.Address]*unlocked)
+ ks.cache, ks.changes = newAccountCache(keydir)
+
+ // TODO: In order for this finalizer to work, there must be no references
+ // to ks. addressCache doesn't keep a reference but unlocked keys do,
+ // so the finalizer will not trigger until all timed unlocks have expired.
+ runtime.SetFinalizer(ks, func(m *KeyStore) {
+ m.cache.close()
+ })
+ // Create the initial list of wallets from the cache
+ accs := ks.cache.accounts()
+ ks.wallets = make([]accounts.Wallet, len(accs))
+ for i := 0; i < len(accs); i++ {
+ ks.wallets[i] = &keystoreWallet{account: accs[i], keystore: ks}
+ }
+}
+
+// Wallets implements accounts.Backend, returning all single-key wallets from the
+// keystore directory.
+func (ks *KeyStore) Wallets() []accounts.Wallet {
+ // Make sure the list of wallets is in sync with the account cache
+ ks.refreshWallets()
+
+ ks.mu.RLock()
+ defer ks.mu.RUnlock()
+
+ cpy := make([]accounts.Wallet, len(ks.wallets))
+ copy(cpy, ks.wallets)
+ return cpy
+}
+
+// refreshWallets retrieves the current account list and based on that does any
+// necessary wallet refreshes.
+func (ks *KeyStore) refreshWallets() {
+ // Retrieve the current list of accounts
+ ks.mu.Lock()
+ accs := ks.cache.accounts()
+
+ // Transform the current list of wallets into the new one
+ var (
+ wallets = make([]accounts.Wallet, 0, len(accs))
+ events []accounts.WalletEvent
+ )
+
+ for _, account := range accs {
+ // Drop wallets while they were in front of the next account
+ for len(ks.wallets) > 0 && ks.wallets[0].URL().Cmp(account.URL) < 0 {
+ events = append(events, accounts.WalletEvent{Wallet: ks.wallets[0], Kind: accounts.WalletDropped})
+ ks.wallets = ks.wallets[1:]
+ }
+ // If there are no more wallets or the account is before the next, wrap new wallet
+ if len(ks.wallets) == 0 || ks.wallets[0].URL().Cmp(account.URL) > 0 {
+ wallet := &keystoreWallet{account: account, keystore: ks}
+
+ events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
+ wallets = append(wallets, wallet)
+ continue
+ }
+ // If the account is the same as the first wallet, keep it
+ if ks.wallets[0].Accounts()[0] == account {
+ wallets = append(wallets, ks.wallets[0])
+ ks.wallets = ks.wallets[1:]
+ continue
+ }
+ }
+ // Drop any leftover wallets and set the new batch
+ for _, wallet := range ks.wallets {
+ events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped})
+ }
+ ks.wallets = wallets
+ ks.mu.Unlock()
+
+ // Fire all wallet events and return
+ for _, event := range events {
+ ks.updateFeed.Send(event)
+ }
+}
+
+// Subscribe implements accounts.Backend, creating an async subscription to
+// receive notifications on the addition or removal of keystore wallets.
+func (ks *KeyStore) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription {
+ // We need the mutex to reliably start/stop the update loop
+ ks.mu.Lock()
+ defer ks.mu.Unlock()
+
+ // Subscribe the caller and track the subscriber count
+ sub := ks.updateScope.Track(ks.updateFeed.Subscribe(sink))
+
+ // Subscribers require an active notification loop, start it
+ if !ks.updating {
+ ks.updating = true
+ go ks.updater()
+ }
+ return sub
+}
+
+// updater is responsible for maintaining an up-to-date list of wallets stored in
+// the keystore, and for firing wallet addition/removal events. It listens for
+// account change events from the underlying account cache, and also periodically
+// forces a manual refresh (only triggers for systems where the filesystem notifier
+// is not running).
+func (ks *KeyStore) updater() {
+ for {
+ // Wait for an account update or a refresh timeout
+ select {
+ case <-ks.changes:
+ case <-time.After(walletRefreshCycle):
+ }
+ // Run the wallet refresher
+ ks.refreshWallets()
+
+ // If all our subscribers left, stop the updater
+ ks.mu.Lock()
+ if ks.updateScope.Count() == 0 {
+ ks.updating = false
+ ks.mu.Unlock()
+ return
+ }
+ ks.mu.Unlock()
+ }
+}
+
+// HasAddress reports whether a key with the given address is present.
+func (ks *KeyStore) HasAddress(addr common.Address) bool {
+ return ks.cache.hasAddress(addr)
+}
+
+// Accounts returns all key files present in the directory.
+func (ks *KeyStore) Accounts() []accounts.Account {
+ return ks.cache.accounts()
+}
+
+// Delete deletes the key matched by account if the passphrase is correct.
+// If the account contains no filename, the address must match a unique key.
+func (ks *KeyStore) Delete(a accounts.Account, passphrase string) error {
+ // Decrypting the key isn't really necessary, but we do
+ // it anyway to check the password and zero out the key
+ // immediately afterwards.
+ a, key, err := ks.getDecryptedKey(a, passphrase)
+ if key != nil {
+ zeroKey(key.PrivateKey)
+ }
+ if err != nil {
+ return err
+ }
+ // The order is crucial here. The key is dropped from the
+ // cache after the file is gone so that a reload happening in
+ // between won't insert it into the cache again.
+ err = os.Remove(a.URL.Path)
+ if err == nil {
+ ks.cache.delete(a)
+ ks.refreshWallets()
+ }
+ return err
+}
+
+// SignHash calculates a ECDSA signature for the given hash. The produced
+// signature is in the [R || S || V] format where V is 0 or 1.
+func (ks *KeyStore) SignHash(a accounts.Account, hash []byte) ([]byte, error) {
+ // Look up the key to sign with and abort if it cannot be found
+ ks.mu.RLock()
+ defer ks.mu.RUnlock()
+
+ unlockedKey, found := ks.unlocked[a.Address]
+ if !found {
+ return nil, ErrLocked
+ }
+ // Sign the hash using plain ECDSA operations
+ return crypto.Sign(hash, unlockedKey.PrivateKey)
+}
+
+// SignTx signs the given transaction with the requested account.
+func (ks *KeyStore) SignTx(a accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ // Look up the key to sign with and abort if it cannot be found
+ ks.mu.RLock()
+ defer ks.mu.RUnlock()
+
+ unlockedKey, found := ks.unlocked[a.Address]
+ if !found {
+ return nil, ErrLocked
+ }
+ // Depending on the presence of the chain ID, sign with EIP155 or homestead
+ if chainID != nil {
+ return types.SignTx(tx, types.NewEIP155Signer(chainID), unlockedKey.PrivateKey)
+ }
+ return types.SignTx(tx, types.HomesteadSigner{}, unlockedKey.PrivateKey)
+}
+
+// SignHashWithPassphrase signs hash if the private key matching the given address
+// can be decrypted with the given passphrase. The produced signature is in the
+// [R || S || V] format where V is 0 or 1.
+func (ks *KeyStore) SignHashWithPassphrase(a accounts.Account, passphrase string, hash []byte) (signature []byte, err error) {
+ _, key, err := ks.getDecryptedKey(a, passphrase)
+ if err != nil {
+ return nil, err
+ }
+ defer zeroKey(key.PrivateKey)
+ return crypto.Sign(hash, key.PrivateKey)
+}
+
+// SignTxWithPassphrase signs the transaction if the private key matching the
+// given address can be decrypted with the given passphrase.
+func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ _, key, err := ks.getDecryptedKey(a, passphrase)
+ if err != nil {
+ return nil, err
+ }
+ defer zeroKey(key.PrivateKey)
+
+ // Depending on the presence of the chain ID, sign with EIP155 or homestead
+ if chainID != nil {
+ return types.SignTx(tx, types.NewEIP155Signer(chainID), key.PrivateKey)
+ }
+ return types.SignTx(tx, types.HomesteadSigner{}, key.PrivateKey)
+}
+
+// Unlock unlocks the given account indefinitely.
+func (ks *KeyStore) Unlock(a accounts.Account, passphrase string) error {
+ return ks.TimedUnlock(a, passphrase, 0)
+}
+
+// Lock removes the private key with the given address from memory.
+func (ks *KeyStore) Lock(addr common.Address) error {
+ ks.mu.Lock()
+ if unl, found := ks.unlocked[addr]; found {
+ ks.mu.Unlock()
+ ks.expire(addr, unl, time.Duration(0)*time.Nanosecond)
+ } else {
+ ks.mu.Unlock()
+ }
+ return nil
+}
+
+// TimedUnlock unlocks the given account with the passphrase. The account
+// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
+// until the program exits. The account must match a unique key file.
+//
+// If the account address is already unlocked for a duration, TimedUnlock extends or
+// shortens the active unlock timeout. If the address was previously unlocked
+// indefinitely the timeout is not altered.
+func (ks *KeyStore) TimedUnlock(a accounts.Account, passphrase string, timeout time.Duration) error {
+ a, key, err := ks.getDecryptedKey(a, passphrase)
+ if err != nil {
+ return err
+ }
+
+ ks.mu.Lock()
+ defer ks.mu.Unlock()
+ u, found := ks.unlocked[a.Address]
+ if found {
+ if u.abort == nil {
+ // The address was unlocked indefinitely, so unlocking
+ // it with a timeout would be confusing.
+ zeroKey(key.PrivateKey)
+ return nil
+ }
+ // Terminate the expire goroutine and replace it below.
+ close(u.abort)
+ }
+ if timeout > 0 {
+ u = &unlocked{Key: key, abort: make(chan struct{})}
+ go ks.expire(a.Address, u, timeout)
+ } else {
+ u = &unlocked{Key: key}
+ }
+ ks.unlocked[a.Address] = u
+ return nil
+}
+
+// Find resolves the given account into a unique entry in the keystore.
+func (ks *KeyStore) Find(a accounts.Account) (accounts.Account, error) {
+ ks.cache.maybeReload()
+ ks.cache.mu.Lock()
+ a, err := ks.cache.find(a)
+ ks.cache.mu.Unlock()
+ return a, err
+}
+
+func (ks *KeyStore) getDecryptedKey(a accounts.Account, auth string) (accounts.Account, *Key, error) {
+ a, err := ks.Find(a)
+ if err != nil {
+ return a, nil, err
+ }
+ key, err := ks.storage.GetKey(a.Address, a.URL.Path, auth)
+ return a, key, err
+}
+
+func (ks *KeyStore) expire(addr common.Address, u *unlocked, timeout time.Duration) {
+ t := time.NewTimer(timeout)
+ defer t.Stop()
+ select {
+ case <-u.abort:
+ // just quit
+ case <-t.C:
+ ks.mu.Lock()
+ // only drop if it's still the same key instance that dropLater
+ // was launched with. we can check that using pointer equality
+ // because the map stores a new pointer every time the key is
+ // unlocked.
+ if ks.unlocked[addr] == u {
+ zeroKey(u.PrivateKey)
+ delete(ks.unlocked, addr)
+ }
+ ks.mu.Unlock()
+ }
+}
+
+// NewAccount generates a new key and stores it into the key directory,
+// encrypting it with the passphrase.
+func (ks *KeyStore) NewAccount(passphrase string) (accounts.Account, error) {
+ _, account, err := storeNewKey(ks.storage, crand.Reader, passphrase)
+ if err != nil {
+ return accounts.Account{}, err
+ }
+ // Add the account to the cache immediately rather
+ // than waiting for file system notifications to pick it up.
+ ks.cache.add(account)
+ ks.refreshWallets()
+ return account, nil
+}
+
+// Export exports as a JSON key, encrypted with newPassphrase.
+func (ks *KeyStore) Export(a accounts.Account, passphrase, newPassphrase string) (keyJSON []byte, err error) {
+ _, key, err := ks.getDecryptedKey(a, passphrase)
+ if err != nil {
+ return nil, err
+ }
+ var N, P int
+ if store, ok := ks.storage.(*keyStorePassphrase); ok {
+ N, P = store.scryptN, store.scryptP
+ } else {
+ N, P = StandardScryptN, StandardScryptP
+ }
+ return EncryptKey(key, newPassphrase, N, P)
+}
+
+// Import stores the given encrypted JSON key into the key directory.
+func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (accounts.Account, error) {
+ key, err := DecryptKey(keyJSON, passphrase)
+ if key != nil && key.PrivateKey != nil {
+ defer zeroKey(key.PrivateKey)
+ }
+ if err != nil {
+ return accounts.Account{}, err
+ }
+ return ks.importKey(key, newPassphrase)
+}
+
+// ImportECDSA stores the given key into the key directory, encrypting it with the passphrase.
+func (ks *KeyStore) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (accounts.Account, error) {
+ key := newKeyFromECDSA(priv)
+ if ks.cache.hasAddress(key.Address) {
+ return accounts.Account{}, fmt.Errorf("account already exists")
+ }
+ return ks.importKey(key, passphrase)
+}
+
+func (ks *KeyStore) importKey(key *Key, passphrase string) (accounts.Account, error) {
+ a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.storage.JoinPath(keyFileName(key.Address))}}
+ if err := ks.storage.StoreKey(a.URL.Path, key, passphrase); err != nil {
+ return accounts.Account{}, err
+ }
+ ks.cache.add(a)
+ ks.refreshWallets()
+ return a, nil
+}
+
+// Update changes the passphrase of an existing account.
+func (ks *KeyStore) Update(a accounts.Account, passphrase, newPassphrase string) error {
+ a, key, err := ks.getDecryptedKey(a, passphrase)
+ if err != nil {
+ return err
+ }
+ return ks.storage.StoreKey(a.URL.Path, key, newPassphrase)
+}
+
+// ImportPreSaleKey decrypts the given Ethereum presale wallet and stores
+// a key file in the key directory. The key file is encrypted with the same passphrase.
+func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (accounts.Account, error) {
+ a, _, err := importPreSaleKey(ks.storage, keyJSON, passphrase)
+ if err != nil {
+ return a, err
+ }
+ ks.cache.add(a)
+ ks.refreshWallets()
+ return a, nil
+}
+
+// zeroKey zeroes a private key in memory.
+func zeroKey(k *ecdsa.PrivateKey) {
+ b := k.D.Bits()
+ for i := range b {
+ b[i] = 0
+ }
+}
diff --git a/accounts/keystore/passphrase.go b/accounts/keystore/passphrase.go
new file mode 100644
index 0000000..fa5a09d
--- /dev/null
+++ b/accounts/keystore/passphrase.go
@@ -0,0 +1,356 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+/*
+
+This key store behaves as KeyStorePlain with the difference that
+the private key is encrypted and on disk uses another JSON encoding.
+
+The crypto is documented at https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
+
+*/
+
+package keystore
+
+import (
+ "bytes"
+ "crypto/aes"
+ "crypto/rand"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/pborman/uuid"
+ "golang.org/x/crypto/pbkdf2"
+ "golang.org/x/crypto/scrypt"
+)
+
+const (
+ keyHeaderKDF = "scrypt"
+
+ // StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
+ // memory and taking approximately 1s CPU time on a modern processor.
+ StandardScryptN = 1 << 18
+
+ // StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
+ // memory and taking approximately 1s CPU time on a modern processor.
+ StandardScryptP = 1
+
+ // LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
+ // memory and taking approximately 100ms CPU time on a modern processor.
+ LightScryptN = 1 << 12
+
+ // LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
+ // memory and taking approximately 100ms CPU time on a modern processor.
+ LightScryptP = 6
+
+ scryptR = 8
+ scryptDKLen = 32
+)
+
+type keyStorePassphrase struct {
+ keysDirPath string
+ scryptN int
+ scryptP int
+ // skipKeyFileVerification disables the security-feature which does
+ // reads and decrypts any newly created keyfiles. This should be 'false' in all
+ // cases except tests -- setting this to 'true' is not recommended.
+ skipKeyFileVerification bool
+}
+
+func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) {
+ // Load the key from the keystore and decrypt its contents
+ keyjson, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return nil, err
+ }
+ key, err := DecryptKey(keyjson, auth)
+ if err != nil {
+ return nil, err
+ }
+ // Make sure we're really operating on the requested key (no swap attacks)
+ if key.Address != addr {
+ return nil, fmt.Errorf("key content mismatch: have account %x, want %x", key.Address, addr)
+ }
+ return key, nil
+}
+
+// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
+func StoreKey(dir, auth string, scryptN, scryptP int) (accounts.Account, error) {
+ _, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP, false}, rand.Reader, auth)
+ return a, err
+}
+
+func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error {
+ keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP)
+ if err != nil {
+ return err
+ }
+ // Write into temporary file
+ tmpName, err := writeTemporaryKeyFile(filename, keyjson)
+ if err != nil {
+ return err
+ }
+ if !ks.skipKeyFileVerification {
+ // Verify that we can decrypt the file with the given password.
+ _, err = ks.GetKey(key.Address, tmpName, auth)
+ if err != nil {
+ msg := "An error was encountered when saving and verifying the keystore file. \n" +
+ "This indicates that the keystore is corrupted. \n" +
+ "The corrupted file is stored at \n%v\n" +
+ "Please file a ticket at:\n\n" +
+ "https://github.com/ethereum/go-ethereum/issues." +
+ "The error was : %s"
+ return fmt.Errorf(msg, tmpName, err)
+ }
+ }
+ return os.Rename(tmpName, filename)
+}
+
+func (ks keyStorePassphrase) JoinPath(filename string) string {
+ if filepath.IsAbs(filename) {
+ return filename
+ }
+ return filepath.Join(ks.keysDirPath, filename)
+}
+
+// Encryptdata encrypts the data given as 'data' with the password 'auth'.
+func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error) {
+
+ salt := make([]byte, 32)
+ if _, err := io.ReadFull(rand.Reader, salt); err != nil {
+ panic("reading from crypto/rand failed: " + err.Error())
+ }
+ derivedKey, err := scrypt.Key(auth, salt, scryptN, scryptR, scryptP, scryptDKLen)
+ if err != nil {
+ return CryptoJSON{}, err
+ }
+ encryptKey := derivedKey[:16]
+
+ iv := make([]byte, aes.BlockSize) // 16
+ if _, err := io.ReadFull(rand.Reader, iv); err != nil {
+ panic("reading from crypto/rand failed: " + err.Error())
+ }
+ cipherText, err := aesCTRXOR(encryptKey, data, iv)
+ if err != nil {
+ return CryptoJSON{}, err
+ }
+ mac := crypto.Keccak256(derivedKey[16:32], cipherText)
+
+ scryptParamsJSON := make(map[string]interface{}, 5)
+ scryptParamsJSON["n"] = scryptN
+ scryptParamsJSON["r"] = scryptR
+ scryptParamsJSON["p"] = scryptP
+ scryptParamsJSON["dklen"] = scryptDKLen
+ scryptParamsJSON["salt"] = hex.EncodeToString(salt)
+ cipherParamsJSON := cipherparamsJSON{
+ IV: hex.EncodeToString(iv),
+ }
+
+ cryptoStruct := CryptoJSON{
+ Cipher: "aes-128-ctr",
+ CipherText: hex.EncodeToString(cipherText),
+ CipherParams: cipherParamsJSON,
+ KDF: keyHeaderKDF,
+ KDFParams: scryptParamsJSON,
+ MAC: hex.EncodeToString(mac),
+ }
+ return cryptoStruct, nil
+}
+
+// EncryptKey encrypts a key using the specified scrypt parameters into a json
+// blob that can be decrypted later on.
+func EncryptKey(key *Key, auth string, scryptN, scryptP int) ([]byte, error) {
+ keyBytes := math.PaddedBigBytes(key.PrivateKey.D, 32)
+ cryptoStruct, err := EncryptDataV3(keyBytes, []byte(auth), scryptN, scryptP)
+ if err != nil {
+ return nil, err
+ }
+ encryptedKeyJSONV3 := encryptedKeyJSONV3{
+ hex.EncodeToString(key.Address[:]),
+ cryptoStruct,
+ key.Id.String(),
+ version,
+ }
+ return json.Marshal(encryptedKeyJSONV3)
+}
+
+// DecryptKey decrypts a key from a json blob, returning the private key itself.
+func DecryptKey(keyjson []byte, auth string) (*Key, error) {
+ // Parse the json into a simple map to fetch the key version
+ m := make(map[string]interface{})
+ if err := json.Unmarshal(keyjson, &m); err != nil {
+ return nil, err
+ }
+ // Depending on the version try to parse one way or another
+ var (
+ keyBytes, keyId []byte
+ err error
+ )
+ if version, ok := m["version"].(string); ok && version == "1" {
+ k := new(encryptedKeyJSONV1)
+ if err := json.Unmarshal(keyjson, k); err != nil {
+ return nil, err
+ }
+ keyBytes, keyId, err = decryptKeyV1(k, auth)
+ } else {
+ k := new(encryptedKeyJSONV3)
+ if err := json.Unmarshal(keyjson, k); err != nil {
+ return nil, err
+ }
+ keyBytes, keyId, err = decryptKeyV3(k, auth)
+ }
+ // Handle any decryption errors and return the key
+ if err != nil {
+ return nil, err
+ }
+ key := crypto.ToECDSAUnsafe(keyBytes)
+
+ return &Key{
+ Id: uuid.UUID(keyId),
+ Address: crypto.PubkeyToAddress(key.PublicKey),
+ PrivateKey: key,
+ }, nil
+}
+
+func DecryptDataV3(cryptoJson CryptoJSON, auth string) ([]byte, error) {
+ if cryptoJson.Cipher != "aes-128-ctr" {
+ return nil, fmt.Errorf("Cipher not supported: %v", cryptoJson.Cipher)
+ }
+ mac, err := hex.DecodeString(cryptoJson.MAC)
+ if err != nil {
+ return nil, err
+ }
+
+ iv, err := hex.DecodeString(cryptoJson.CipherParams.IV)
+ if err != nil {
+ return nil, err
+ }
+
+ cipherText, err := hex.DecodeString(cryptoJson.CipherText)
+ if err != nil {
+ return nil, err
+ }
+
+ derivedKey, err := getKDFKey(cryptoJson, auth)
+ if err != nil {
+ return nil, err
+ }
+
+ calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
+ if !bytes.Equal(calculatedMAC, mac) {
+ return nil, ErrDecrypt
+ }
+
+ plainText, err := aesCTRXOR(derivedKey[:16], cipherText, iv)
+ if err != nil {
+ return nil, err
+ }
+ return plainText, err
+}
+
+func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byte, keyId []byte, err error) {
+ if keyProtected.Version != version {
+ return nil, nil, fmt.Errorf("Version not supported: %v", keyProtected.Version)
+ }
+ keyId = uuid.Parse(keyProtected.Id)
+ plainText, err := DecryptDataV3(keyProtected.Crypto, auth)
+ if err != nil {
+ return nil, nil, err
+ }
+ return plainText, keyId, err
+}
+
+func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byte, keyId []byte, err error) {
+ keyId = uuid.Parse(keyProtected.Id)
+ mac, err := hex.DecodeString(keyProtected.Crypto.MAC)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ derivedKey, err := getKDFKey(keyProtected.Crypto, auth)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText)
+ if !bytes.Equal(calculatedMAC, mac) {
+ return nil, nil, ErrDecrypt
+ }
+
+ plainText, err := aesCBCDecrypt(crypto.Keccak256(derivedKey[:16])[:16], cipherText, iv)
+ if err != nil {
+ return nil, nil, err
+ }
+ return plainText, keyId, err
+}
+
+func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) {
+ authArray := []byte(auth)
+ salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string))
+ if err != nil {
+ return nil, err
+ }
+ dkLen := ensureInt(cryptoJSON.KDFParams["dklen"])
+
+ if cryptoJSON.KDF == keyHeaderKDF {
+ n := ensureInt(cryptoJSON.KDFParams["n"])
+ r := ensureInt(cryptoJSON.KDFParams["r"])
+ p := ensureInt(cryptoJSON.KDFParams["p"])
+ return scrypt.Key(authArray, salt, n, r, p, dkLen)
+
+ } else if cryptoJSON.KDF == "pbkdf2" {
+ c := ensureInt(cryptoJSON.KDFParams["c"])
+ prf := cryptoJSON.KDFParams["prf"].(string)
+ if prf != "hmac-sha256" {
+ return nil, fmt.Errorf("Unsupported PBKDF2 PRF: %s", prf)
+ }
+ key := pbkdf2.Key(authArray, salt, c, dkLen, sha256.New)
+ return key, nil
+ }
+
+ return nil, fmt.Errorf("Unsupported KDF: %s", cryptoJSON.KDF)
+}
+
+// TODO: can we do without this when unmarshalling dynamic JSON?
+// why do integers in KDF params end up as float64 and not int after
+// unmarshal?
+func ensureInt(x interface{}) int {
+ res, ok := x.(int)
+ if !ok {
+ res = int(x.(float64))
+ }
+ return res
+}
diff --git a/accounts/keystore/plain.go b/accounts/keystore/plain.go
new file mode 100644
index 0000000..bc5b377
--- /dev/null
+++ b/accounts/keystore/plain.go
@@ -0,0 +1,61 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package keystore
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+type keyStorePlain struct {
+ keysDirPath string
+}
+
+func (ks keyStorePlain) GetKey(addr common.Address, filename, auth string) (*Key, error) {
+ fd, err := os.Open(filename)
+ if err != nil {
+ return nil, err
+ }
+ defer fd.Close()
+ key := new(Key)
+ if err := json.NewDecoder(fd).Decode(key); err != nil {
+ return nil, err
+ }
+ if key.Address != addr {
+ return nil, fmt.Errorf("key content mismatch: have address %x, want %x", key.Address, addr)
+ }
+ return key, nil
+}
+
+func (ks keyStorePlain) StoreKey(filename string, key *Key, auth string) error {
+ content, err := json.Marshal(key)
+ if err != nil {
+ return err
+ }
+ return writeKeyFile(filename, content)
+}
+
+func (ks keyStorePlain) JoinPath(filename string) string {
+ if filepath.IsAbs(filename) {
+ return filename
+ }
+ return filepath.Join(ks.keysDirPath, filename)
+}
diff --git a/accounts/keystore/presale.go b/accounts/keystore/presale.go
new file mode 100644
index 0000000..5122b0d
--- /dev/null
+++ b/accounts/keystore/presale.go
@@ -0,0 +1,147 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package keystore
+
+import (
+ "crypto/aes"
+ "crypto/cipher"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "fmt"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/pborman/uuid"
+ "golang.org/x/crypto/pbkdf2"
+)
+
+// creates a Key and stores that in the given KeyStore by decrypting a presale key JSON
+func importPreSaleKey(keyStore keyStore, keyJSON []byte, password string) (accounts.Account, *Key, error) {
+ key, err := decryptPreSaleKey(keyJSON, password)
+ if err != nil {
+ return accounts.Account{}, nil, err
+ }
+ key.Id = uuid.NewRandom()
+ a := accounts.Account{
+ Address: key.Address,
+ URL: accounts.URL{
+ Scheme: KeyStoreScheme,
+ Path: keyStore.JoinPath(keyFileName(key.Address)),
+ },
+ }
+ err = keyStore.StoreKey(a.URL.Path, key, password)
+ return a, key, err
+}
+
+func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error) {
+ preSaleKeyStruct := struct {
+ EncSeed string
+ EthAddr string
+ Email string
+ BtcAddr string
+ }{}
+ err = json.Unmarshal(fileContent, &preSaleKeyStruct)
+ if err != nil {
+ return nil, err
+ }
+ encSeedBytes, err := hex.DecodeString(preSaleKeyStruct.EncSeed)
+ if err != nil {
+ return nil, errors.New("invalid hex in encSeed")
+ }
+ if len(encSeedBytes) < 16 {
+ return nil, errors.New("invalid encSeed, too short")
+ }
+ iv := encSeedBytes[:16]
+ cipherText := encSeedBytes[16:]
+ /*
+ See https://github.com/ethereum/pyethsaletool
+
+ pyethsaletool generates the encryption key from password by
+ 2000 rounds of PBKDF2 with HMAC-SHA-256 using password as salt (:().
+ 16 byte key length within PBKDF2 and resulting key is used as AES key
+ */
+ passBytes := []byte(password)
+ derivedKey := pbkdf2.Key(passBytes, passBytes, 2000, 16, sha256.New)
+ plainText, err := aesCBCDecrypt(derivedKey, cipherText, iv)
+ if err != nil {
+ return nil, err
+ }
+ ethPriv := crypto.Keccak256(plainText)
+ ecKey := crypto.ToECDSAUnsafe(ethPriv)
+
+ key = &Key{
+ Id: nil,
+ Address: crypto.PubkeyToAddress(ecKey.PublicKey),
+ PrivateKey: ecKey,
+ }
+ derivedAddr := hex.EncodeToString(key.Address.Bytes()) // needed because .Hex() gives leading "0x"
+ expectedAddr := preSaleKeyStruct.EthAddr
+ if derivedAddr != expectedAddr {
+ err = fmt.Errorf("decrypted addr '%s' not equal to expected addr '%s'", derivedAddr, expectedAddr)
+ }
+ return key, err
+}
+
+func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
+ // AES-128 is selected due to size of encryptKey.
+ aesBlock, err := aes.NewCipher(key)
+ if err != nil {
+ return nil, err
+ }
+ stream := cipher.NewCTR(aesBlock, iv)
+ outText := make([]byte, len(inText))
+ stream.XORKeyStream(outText, inText)
+ return outText, err
+}
+
+func aesCBCDecrypt(key, cipherText, iv []byte) ([]byte, error) {
+ aesBlock, err := aes.NewCipher(key)
+ if err != nil {
+ return nil, err
+ }
+ decrypter := cipher.NewCBCDecrypter(aesBlock, iv)
+ paddedPlaintext := make([]byte, len(cipherText))
+ decrypter.CryptBlocks(paddedPlaintext, cipherText)
+ plaintext := pkcs7Unpad(paddedPlaintext)
+ if plaintext == nil {
+ return nil, ErrDecrypt
+ }
+ return plaintext, err
+}
+
+// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes
+func pkcs7Unpad(in []byte) []byte {
+ if len(in) == 0 {
+ return nil
+ }
+
+ padding := in[len(in)-1]
+ if int(padding) > len(in) || padding > aes.BlockSize {
+ return nil
+ } else if padding == 0 {
+ return nil
+ }
+
+ for i := len(in) - 1; i > len(in)-int(padding)-1; i-- {
+ if in[i] != padding {
+ return nil
+ }
+ }
+ return in[:len(in)-int(padding)]
+}
diff --git a/accounts/keystore/testdata/dupes/1 b/accounts/keystore/testdata/dupes/1
new file mode 100644
index 0000000..a3868ec
--- /dev/null
+++ b/accounts/keystore/testdata/dupes/1
@@ -0,0 +1 @@
+{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/dupes/2 b/accounts/keystore/testdata/dupes/2
new file mode 100644
index 0000000..a3868ec
--- /dev/null
+++ b/accounts/keystore/testdata/dupes/2
@@ -0,0 +1 @@
+{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/dupes/foo b/accounts/keystore/testdata/dupes/foo
new file mode 100644
index 0000000..c57060a
--- /dev/null
+++ b/accounts/keystore/testdata/dupes/foo
@@ -0,0 +1 @@
+{"address":"7ef5a6135f1fd6a02593eedc869c6d41d934aef8","crypto":{"cipher":"aes-128-ctr","ciphertext":"1d0839166e7a15b9c1333fc865d69858b22df26815ccf601b28219b6192974e1","cipherparams":{"iv":"8df6caa7ff1b00c4e871f002cb7921ed"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"e5e6ef3f4ea695f496b643ebd3f75c0aa58ef4070e90c80c5d3fb0241bf1595c"},"mac":"6d16dfde774845e4585357f24bce530528bc69f4f84e1e22880d34fa45c273e5"},"id":"950077c7-71e3-4c44-a4a1-143919141ed4","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/keystore/.hiddenfile b/accounts/keystore/testdata/keystore/.hiddenfile
new file mode 100644
index 0000000..d91facc
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/.hiddenfile
@@ -0,0 +1 @@
+{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3}
diff --git a/accounts/keystore/testdata/keystore/README b/accounts/keystore/testdata/keystore/README
new file mode 100644
index 0000000..6af9ac3
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/README
@@ -0,0 +1,21 @@
+This directory contains accounts for testing.
+The password that unlocks them is "foobar".
+
+The "good" key files which are supposed to be loadable are:
+
+- File: UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
+ Address: 0x7ef5a6135f1fd6a02593eedc869c6d41d934aef8
+- File: aaa
+ Address: 0xf466859ead1932d743d622cb74fc058882e8648a
+- File: zzz
+ Address: 0x289d485d9771714cce91d3393d764e1311907acc
+
+The other files (including this README) are broken in various ways
+and should not be picked up by package accounts:
+
+- File: no-address (missing address field, otherwise same as "aaa")
+- File: garbage (file with random data)
+- File: empty (file with no content)
+- File: swapfile~ (should be skipped)
+- File: .hiddenfile (should be skipped)
+- File: foo/... (should be skipped because it is a directory)
diff --git a/accounts/keystore/testdata/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 b/accounts/keystore/testdata/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
new file mode 100644
index 0000000..c57060a
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8
@@ -0,0 +1 @@
+{"address":"7ef5a6135f1fd6a02593eedc869c6d41d934aef8","crypto":{"cipher":"aes-128-ctr","ciphertext":"1d0839166e7a15b9c1333fc865d69858b22df26815ccf601b28219b6192974e1","cipherparams":{"iv":"8df6caa7ff1b00c4e871f002cb7921ed"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"e5e6ef3f4ea695f496b643ebd3f75c0aa58ef4070e90c80c5d3fb0241bf1595c"},"mac":"6d16dfde774845e4585357f24bce530528bc69f4f84e1e22880d34fa45c273e5"},"id":"950077c7-71e3-4c44-a4a1-143919141ed4","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/keystore/aaa b/accounts/keystore/testdata/keystore/aaa
new file mode 100644
index 0000000..a3868ec
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/aaa
@@ -0,0 +1 @@
+{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/keystore/empty b/accounts/keystore/testdata/keystore/empty
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/empty
diff --git a/accounts/keystore/testdata/keystore/foo/fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e b/accounts/keystore/testdata/keystore/foo/fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e
new file mode 100644
index 0000000..309841e
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/foo/fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e
@@ -0,0 +1 @@
+{"address":"fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e","crypto":{"cipher":"aes-128-ctr","ciphertext":"8124d5134aa4a927c79fd852989e4b5419397566f04b0936a1eb1d168c7c68a5","cipherparams":{"iv":"e2febe17176414dd2cda28287947eb2f"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":4096,"p":6,"r":8,"salt":"44b415ede89f3bdd6830390a21b78965f571b347a589d1d943029f016c5e8bd5"},"mac":"5e149ff25bfd9dd45746a84bb2bcd2f015f2cbca2b6d25c5de8c29617f71fe5b"},"id":"d6ac5452-2b2c-4d3c-ad80-4bf0327d971c","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/keystore/garbage b/accounts/keystore/testdata/keystore/garbage
new file mode 100644
index 0000000..ff45091
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/garbage
Binary files differ
diff --git a/accounts/keystore/testdata/keystore/no-address b/accounts/keystore/testdata/keystore/no-address
new file mode 100644
index 0000000..ad51269
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/no-address
@@ -0,0 +1 @@
+{"crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/keystore/zero b/accounts/keystore/testdata/keystore/zero
new file mode 100644
index 0000000..b52617f
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/zero
@@ -0,0 +1 @@
+{"address":"0000000000000000000000000000000000000000","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/keystore/zzz b/accounts/keystore/testdata/keystore/zzz
new file mode 100644
index 0000000..cfd8a47
--- /dev/null
+++ b/accounts/keystore/testdata/keystore/zzz
@@ -0,0 +1 @@
+{"address":"289d485d9771714cce91d3393d764e1311907acc","crypto":{"cipher":"aes-128-ctr","ciphertext":"faf32ca89d286b107f5e6d842802e05263c49b78d46eac74e6109e9a963378ab","cipherparams":{"iv":"558833eec4a665a8c55608d7d503407d"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"d571fff447ffb24314f9513f5160246f09997b857ac71348b73e785aab40dc04"},"mac":"21edb85ff7d0dab1767b9bf498f2c3cb7be7609490756bd32300bb213b59effe"},"id":"3279afcf-55ba-43ff-8997-02dcc46a6525","version":3} \ No newline at end of file
diff --git a/accounts/keystore/testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e b/accounts/keystore/testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e
new file mode 100644
index 0000000..498d813
--- /dev/null
+++ b/accounts/keystore/testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e
@@ -0,0 +1 @@
+{"address":"cb61d5a9c4896fb9658090b597ef0e7be6f7b67e","Crypto":{"cipher":"aes-128-cbc","ciphertext":"6143d3192db8b66eabd693d9c4e414dcfaee52abda451af79ccf474dafb35f1bfc7ea013aa9d2ee35969a1a2e8d752d0","cipherparams":{"iv":"35337770fc2117994ecdcad026bccff4"},"kdf":"scrypt","kdfparams":{"n":262144,"r":8,"p":1,"dklen":32,"salt":"9afcddebca541253a2f4053391c673ff9fe23097cd8555d149d929e4ccf1257f"},"mac":"3f3d5af884b17a100b0b3232c0636c230a54dc2ac8d986227219b0dd89197644","version":"1"},"id":"e25f7c1f-d318-4f29-b62c-687190d4d299","version":"1"} \ No newline at end of file
diff --git a/accounts/keystore/testdata/v1_test_vector.json b/accounts/keystore/testdata/v1_test_vector.json
new file mode 100644
index 0000000..3d09b55
--- /dev/null
+++ b/accounts/keystore/testdata/v1_test_vector.json
@@ -0,0 +1,28 @@
+{
+ "test1": {
+ "json": {
+ "Crypto": {
+ "cipher": "aes-128-cbc",
+ "cipherparams": {
+ "iv": "35337770fc2117994ecdcad026bccff4"
+ },
+ "ciphertext": "6143d3192db8b66eabd693d9c4e414dcfaee52abda451af79ccf474dafb35f1bfc7ea013aa9d2ee35969a1a2e8d752d0",
+ "kdf": "scrypt",
+ "kdfparams": {
+ "dklen": 32,
+ "n": 262144,
+ "p": 1,
+ "r": 8,
+ "salt": "9afcddebca541253a2f4053391c673ff9fe23097cd8555d149d929e4ccf1257f"
+ },
+ "mac": "3f3d5af884b17a100b0b3232c0636c230a54dc2ac8d986227219b0dd89197644",
+ "version": "1"
+ },
+ "address": "cb61d5a9c4896fb9658090b597ef0e7be6f7b67e",
+ "id": "e25f7c1f-d318-4f29-b62c-687190d4d299",
+ "version": "1"
+ },
+ "password": "g",
+ "priv": "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
+ }
+}
diff --git a/accounts/keystore/testdata/v3_test_vector.json b/accounts/keystore/testdata/v3_test_vector.json
new file mode 100644
index 0000000..1e7f790
--- /dev/null
+++ b/accounts/keystore/testdata/v3_test_vector.json
@@ -0,0 +1,97 @@
+{
+ "wikipage_test_vector_scrypt": {
+ "json": {
+ "crypto" : {
+ "cipher" : "aes-128-ctr",
+ "cipherparams" : {
+ "iv" : "83dbcc02d8ccb40e466191a123791e0e"
+ },
+ "ciphertext" : "d172bf743a674da9cdad04534d56926ef8358534d458fffccd4e6ad2fbde479c",
+ "kdf" : "scrypt",
+ "kdfparams" : {
+ "dklen" : 32,
+ "n" : 262144,
+ "r" : 1,
+ "p" : 8,
+ "salt" : "ab0c7876052600dd703518d6fc3fe8984592145b591fc8fb5c6d43190334ba19"
+ },
+ "mac" : "2103ac29920d71da29f15d75b4a16dbe95cfd7ff8faea1056c33131d846e3097"
+ },
+ "id" : "3198bc9c-6672-5ab3-d995-4942343ae5b6",
+ "version" : 3
+ },
+ "password": "testpassword",
+ "priv": "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d"
+ },
+ "wikipage_test_vector_pbkdf2": {
+ "json": {
+ "crypto" : {
+ "cipher" : "aes-128-ctr",
+ "cipherparams" : {
+ "iv" : "6087dab2f9fdbbfaddc31a909735c1e6"
+ },
+ "ciphertext" : "5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46",
+ "kdf" : "pbkdf2",
+ "kdfparams" : {
+ "c" : 262144,
+ "dklen" : 32,
+ "prf" : "hmac-sha256",
+ "salt" : "ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"
+ },
+ "mac" : "517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"
+ },
+ "id" : "3198bc9c-6672-5ab3-d995-4942343ae5b6",
+ "version" : 3
+ },
+ "password": "testpassword",
+ "priv": "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d"
+ },
+ "31_byte_key": {
+ "json": {
+ "crypto" : {
+ "cipher" : "aes-128-ctr",
+ "cipherparams" : {
+ "iv" : "e0c41130a323adc1446fc82f724bca2f"
+ },
+ "ciphertext" : "9517cd5bdbe69076f9bf5057248c6c050141e970efa36ce53692d5d59a3984",
+ "kdf" : "scrypt",
+ "kdfparams" : {
+ "dklen" : 32,
+ "n" : 2,
+ "r" : 8,
+ "p" : 1,
+ "salt" : "711f816911c92d649fb4c84b047915679933555030b3552c1212609b38208c63"
+ },
+ "mac" : "d5e116151c6aa71470e67a7d42c9620c75c4d23229847dcc127794f0732b0db5"
+ },
+ "id" : "fecfc4ce-e956-48fd-953b-30f8b52ed66c",
+ "version" : 3
+ },
+ "password": "foo",
+ "priv": "fa7b3db73dc7dfdf8c5fbdb796d741e4488628c41fc4febd9160a866ba0f35"
+ },
+ "30_byte_key": {
+ "json": {
+ "crypto" : {
+ "cipher" : "aes-128-ctr",
+ "cipherparams" : {
+ "iv" : "3ca92af36ad7c2cd92454c59cea5ef00"
+ },
+ "ciphertext" : "108b7d34f3442fc26ab1ab90ca91476ba6bfa8c00975a49ef9051dc675aa",
+ "kdf" : "scrypt",
+ "kdfparams" : {
+ "dklen" : 32,
+ "n" : 2,
+ "r" : 8,
+ "p" : 1,
+ "salt" : "d0769e608fb86cda848065642a9c6fa046845c928175662b8e356c77f914cd3b"
+ },
+ "mac" : "75d0e6759f7b3cefa319c3be41680ab6beea7d8328653474bd06706d4cc67420"
+ },
+ "id" : "a37e1559-5955-450d-8075-7b8931b392b2",
+ "version" : 3
+ },
+ "password": "foo",
+ "priv": "81c29e8142bb6a81bef5a92bda7a8328a5c85bb2f9542e76f9b0f94fc018"
+ }
+}
diff --git a/accounts/keystore/testdata/very-light-scrypt.json b/accounts/keystore/testdata/very-light-scrypt.json
new file mode 100644
index 0000000..d23b9b2
--- /dev/null
+++ b/accounts/keystore/testdata/very-light-scrypt.json
@@ -0,0 +1 @@
+{"address":"45dea0fb0bba44f4fcf290bba71fd57d7117cbb8","crypto":{"cipher":"aes-128-ctr","ciphertext":"b87781948a1befd247bff51ef4063f716cf6c2d3481163e9a8f42e1f9bb74145","cipherparams":{"iv":"dc4926b48a105133d2f16b96833abf1e"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":2,"p":1,"r":8,"salt":"004244bbdc51cadda545b1cfa43cff9ed2ae88e08c61f1479dbb45410722f8f0"},"mac":"39990c1684557447940d4c69e06b1b82b2aceacb43f284df65c956daf3046b85"},"id":"ce541d8d-c79b-40f8-9f8c-20f59616faba","version":3}
diff --git a/accounts/keystore/wallet.go b/accounts/keystore/wallet.go
new file mode 100644
index 0000000..3be901b
--- /dev/null
+++ b/accounts/keystore/wallet.go
@@ -0,0 +1,148 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package keystore
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/types"
+ ethereum "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// keystoreWallet implements the accounts.Wallet interface for the original
+// keystore.
+type keystoreWallet struct {
+ account accounts.Account // Single account contained in this wallet
+ keystore *KeyStore // Keystore where the account originates from
+}
+
+// URL implements accounts.Wallet, returning the URL of the account within.
+func (w *keystoreWallet) URL() accounts.URL {
+ return w.account.URL
+}
+
+// Status implements accounts.Wallet, returning whether the account held by the
+// keystore wallet is unlocked or not.
+func (w *keystoreWallet) Status() (string, error) {
+ w.keystore.mu.RLock()
+ defer w.keystore.mu.RUnlock()
+
+ if _, ok := w.keystore.unlocked[w.account.Address]; ok {
+ return "Unlocked", nil
+ }
+ return "Locked", nil
+}
+
+// Open implements accounts.Wallet, but is a noop for plain wallets since there
+// is no connection or decryption step necessary to access the list of accounts.
+func (w *keystoreWallet) Open(passphrase string) error { return nil }
+
+// Close implements accounts.Wallet, but is a noop for plain wallets since there
+// is no meaningful open operation.
+func (w *keystoreWallet) Close() error { return nil }
+
+// Accounts implements accounts.Wallet, returning an account list consisting of
+// a single account that the plain kestore wallet contains.
+func (w *keystoreWallet) Accounts() []accounts.Account {
+ return []accounts.Account{w.account}
+}
+
+// Contains implements accounts.Wallet, returning whether a particular account is
+// or is not wrapped by this wallet instance.
+func (w *keystoreWallet) Contains(account accounts.Account) bool {
+ return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
+}
+
+// Derive implements accounts.Wallet, but is a noop for plain wallets since there
+// is no notion of hierarchical account derivation for plain keystore accounts.
+func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
+ return accounts.Account{}, accounts.ErrNotSupported
+}
+
+// SelfDerive implements accounts.Wallet, but is a noop for plain wallets since
+// there is no notion of hierarchical account derivation for plain keystore accounts.
+func (w *keystoreWallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
+}
+
+// signHash attempts to sign the given hash with
+// the given account. If the wallet does not wrap this particular account, an
+// error is returned to avoid account leakage (even though in theory we may be
+// able to sign via our shared keystore backend).
+func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
+ // Make sure the requested account is contained within
+ if !w.Contains(account) {
+ return nil, accounts.ErrUnknownAccount
+ }
+ // Account seems valid, request the keystore to sign
+ return w.keystore.SignHash(account, hash)
+}
+
+// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
+func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
+ return w.signHash(account, crypto.Keccak256(data))
+}
+
+// SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed
+func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
+ // Make sure the requested account is contained within
+ if !w.Contains(account) {
+ return nil, accounts.ErrUnknownAccount
+ }
+ // Account seems valid, request the keystore to sign
+ return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data))
+}
+
+func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
+ return w.signHash(account, accounts.TextHash(text))
+}
+
+// SignTextWithPassphrase implements accounts.Wallet, attempting to sign the
+// given hash with the given account using passphrase as extra authentication.
+func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
+ // Make sure the requested account is contained within
+ if !w.Contains(account) {
+ return nil, accounts.ErrUnknownAccount
+ }
+ // Account seems valid, request the keystore to sign
+ return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text))
+}
+
+// SignTx implements accounts.Wallet, attempting to sign the given transaction
+// with the given account. If the wallet does not wrap this particular account,
+// an error is returned to avoid account leakage (even though in theory we may
+// be able to sign via our shared keystore backend).
+func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ // Make sure the requested account is contained within
+ if !w.Contains(account) {
+ return nil, accounts.ErrUnknownAccount
+ }
+ // Account seems valid, request the keystore to sign
+ return w.keystore.SignTx(account, tx, chainID)
+}
+
+// SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
+// transaction with the given account using passphrase as extra authentication.
+func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ // Make sure the requested account is contained within
+ if !w.Contains(account) {
+ return nil, accounts.ErrUnknownAccount
+ }
+ // Account seems valid, request the keystore to sign
+ return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
+}
diff --git a/accounts/keystore/watch.go b/accounts/keystore/watch.go
new file mode 100644
index 0000000..7fa5b3c
--- /dev/null
+++ b/accounts/keystore/watch.go
@@ -0,0 +1,108 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// +build darwin,!ios,cgo freebsd linux,!arm64 netbsd solaris
+
+package keystore
+
+import (
+ "time"
+
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/rjeczalik/notify"
+)
+
+type watcher struct {
+ ac *accountCache
+ starting bool
+ running bool
+ ev chan notify.EventInfo
+ quit chan struct{}
+}
+
+func newWatcher(ac *accountCache) *watcher {
+ return &watcher{
+ ac: ac,
+ ev: make(chan notify.EventInfo, 10),
+ quit: make(chan struct{}),
+ }
+}
+
+// starts the watcher loop in the background.
+// Start a watcher in the background if that's not already in progress.
+// The caller must hold w.ac.mu.
+func (w *watcher) start() {
+ if w.starting || w.running {
+ return
+ }
+ w.starting = true
+ go w.loop()
+}
+
+func (w *watcher) close() {
+ close(w.quit)
+}
+
+func (w *watcher) loop() {
+ defer func() {
+ w.ac.mu.Lock()
+ w.running = false
+ w.starting = false
+ w.ac.mu.Unlock()
+ }()
+ logger := log.New("path", w.ac.keydir)
+
+ if err := notify.Watch(w.ac.keydir, w.ev, notify.All); err != nil {
+ logger.Trace("Failed to watch keystore folder", "err", err)
+ return
+ }
+ defer notify.Stop(w.ev)
+ logger.Trace("Started watching keystore folder")
+ defer logger.Trace("Stopped watching keystore folder")
+
+ w.ac.mu.Lock()
+ w.running = true
+ w.ac.mu.Unlock()
+
+ // Wait for file system events and reload.
+ // When an event occurs, the reload call is delayed a bit so that
+ // multiple events arriving quickly only cause a single reload.
+ var (
+ debounceDuration = 500 * time.Millisecond
+ rescanTriggered = false
+ debounce = time.NewTimer(0)
+ )
+ // Ignore initial trigger
+ if !debounce.Stop() {
+ <-debounce.C
+ }
+ defer debounce.Stop()
+ for {
+ select {
+ case <-w.quit:
+ return
+ case <-w.ev:
+ // Trigger the scan (with delay), if not already triggered
+ if !rescanTriggered {
+ debounce.Reset(debounceDuration)
+ rescanTriggered = true
+ }
+ case <-debounce.C:
+ w.ac.scanAccounts()
+ rescanTriggered = false
+ }
+ }
+}
diff --git a/accounts/keystore/watch_fallback.go b/accounts/keystore/watch_fallback.go
new file mode 100644
index 0000000..de0e87f
--- /dev/null
+++ b/accounts/keystore/watch_fallback.go
@@ -0,0 +1,28 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// +build darwin,!cgo ios linux,arm64 windows !darwin,!freebsd,!linux,!netbsd,!solaris
+
+// This is the fallback implementation of directory watching.
+// It is used on unsupported platforms.
+
+package keystore
+
+type watcher struct{ running bool }
+
+func newWatcher(*accountCache) *watcher { return new(watcher) }
+func (*watcher) start() {}
+func (*watcher) close() {}
diff --git a/accounts/manager.go b/accounts/manager.go
new file mode 100644
index 0000000..39211da
--- /dev/null
+++ b/accounts/manager.go
@@ -0,0 +1,229 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package accounts
+
+import (
+ "reflect"
+ "sort"
+ "sync"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/event"
+)
+
+// Config contains the settings of the global account manager.
+//
+// TODO(rjl493456442, karalabe, holiman): Get rid of this when account management
+// is removed in favor of Clef.
+type Config struct {
+ InsecureUnlockAllowed bool // Whether account unlocking in insecure environment is allowed
+}
+
+// Manager is an overarching account manager that can communicate with various
+// backends for signing transactions.
+type Manager struct {
+ config *Config // Global account manager configurations
+ backends map[reflect.Type][]Backend // Index of backends currently registered
+ updaters []event.Subscription // Wallet update subscriptions for all backends
+ updates chan WalletEvent // Subscription sink for backend wallet changes
+ wallets []Wallet // Cache of all wallets from all registered backends
+
+ feed event.Feed // Wallet feed notifying of arrivals/departures
+
+ quit chan chan error
+ lock sync.RWMutex
+}
+
+// NewManager creates a generic account manager to sign transaction via various
+// supported backends.
+func NewManager(config *Config, backends ...Backend) *Manager {
+ // Retrieve the initial list of wallets from the backends and sort by URL
+ var wallets []Wallet
+ for _, backend := range backends {
+ wallets = merge(wallets, backend.Wallets()...)
+ }
+ // Subscribe to wallet notifications from all backends
+ updates := make(chan WalletEvent, 4*len(backends))
+
+ subs := make([]event.Subscription, len(backends))
+ for i, backend := range backends {
+ subs[i] = backend.Subscribe(updates)
+ }
+ // Assemble the account manager and return
+ am := &Manager{
+ config: config,
+ backends: make(map[reflect.Type][]Backend),
+ updaters: subs,
+ updates: updates,
+ wallets: wallets,
+ quit: make(chan chan error),
+ }
+ for _, backend := range backends {
+ kind := reflect.TypeOf(backend)
+ am.backends[kind] = append(am.backends[kind], backend)
+ }
+ go am.update()
+
+ return am
+}
+
+// Close terminates the account manager's internal notification processes.
+func (am *Manager) Close() error {
+ errc := make(chan error)
+ am.quit <- errc
+ return <-errc
+}
+
+// Config returns the configuration of account manager.
+func (am *Manager) Config() *Config {
+ return am.config
+}
+
+// update is the wallet event loop listening for notifications from the backends
+// and updating the cache of wallets.
+func (am *Manager) update() {
+ // Close all subscriptions when the manager terminates
+ defer func() {
+ am.lock.Lock()
+ for _, sub := range am.updaters {
+ sub.Unsubscribe()
+ }
+ am.updaters = nil
+ am.lock.Unlock()
+ }()
+
+ // Loop until termination
+ for {
+ select {
+ case event := <-am.updates:
+ // Wallet event arrived, update local cache
+ am.lock.Lock()
+ switch event.Kind {
+ case WalletArrived:
+ am.wallets = merge(am.wallets, event.Wallet)
+ case WalletDropped:
+ am.wallets = drop(am.wallets, event.Wallet)
+ }
+ am.lock.Unlock()
+
+ // Notify any listeners of the event
+ am.feed.Send(event)
+
+ case errc := <-am.quit:
+ // Manager terminating, return
+ errc <- nil
+ return
+ }
+ }
+}
+
+// Backends retrieves the backend(s) with the given type from the account manager.
+func (am *Manager) Backends(kind reflect.Type) []Backend {
+ return am.backends[kind]
+}
+
+// Wallets returns all signer accounts registered under this account manager.
+func (am *Manager) Wallets() []Wallet {
+ am.lock.RLock()
+ defer am.lock.RUnlock()
+
+ cpy := make([]Wallet, len(am.wallets))
+ copy(cpy, am.wallets)
+ return cpy
+}
+
+// Wallet retrieves the wallet associated with a particular URL.
+func (am *Manager) Wallet(url string) (Wallet, error) {
+ am.lock.RLock()
+ defer am.lock.RUnlock()
+
+ parsed, err := parseURL(url)
+ if err != nil {
+ return nil, err
+ }
+ for _, wallet := range am.Wallets() {
+ if wallet.URL() == parsed {
+ return wallet, nil
+ }
+ }
+ return nil, ErrUnknownWallet
+}
+
+// Accounts returns all account addresses of all wallets within the account manager
+func (am *Manager) Accounts() []common.Address {
+ am.lock.RLock()
+ defer am.lock.RUnlock()
+
+ addresses := make([]common.Address, 0) // return [] instead of nil if empty
+ for _, wallet := range am.wallets {
+ for _, account := range wallet.Accounts() {
+ addresses = append(addresses, account.Address)
+ }
+ }
+ return addresses
+}
+
+// Find attempts to locate the wallet corresponding to a specific account. Since
+// accounts can be dynamically added to and removed from wallets, this method has
+// a linear runtime in the number of wallets.
+func (am *Manager) Find(account Account) (Wallet, error) {
+ am.lock.RLock()
+ defer am.lock.RUnlock()
+
+ for _, wallet := range am.wallets {
+ if wallet.Contains(account) {
+ return wallet, nil
+ }
+ }
+ return nil, ErrUnknownAccount
+}
+
+// Subscribe creates an async subscription to receive notifications when the
+// manager detects the arrival or departure of a wallet from any of its backends.
+func (am *Manager) Subscribe(sink chan<- WalletEvent) event.Subscription {
+ return am.feed.Subscribe(sink)
+}
+
+// merge is a sorted analogue of append for wallets, where the ordering of the
+// origin list is preserved by inserting new wallets at the correct position.
+//
+// The original slice is assumed to be already sorted by URL.
+func merge(slice []Wallet, wallets ...Wallet) []Wallet {
+ for _, wallet := range wallets {
+ n := sort.Search(len(slice), func(i int) bool { return slice[i].URL().Cmp(wallet.URL()) >= 0 })
+ if n == len(slice) {
+ slice = append(slice, wallet)
+ continue
+ }
+ slice = append(slice[:n], append([]Wallet{wallet}, slice[n:]...)...)
+ }
+ return slice
+}
+
+// drop is the couterpart of merge, which looks up wallets from within the sorted
+// cache and removes the ones specified.
+func drop(slice []Wallet, wallets ...Wallet) []Wallet {
+ for _, wallet := range wallets {
+ n := sort.Search(len(slice), func(i int) bool { return slice[i].URL().Cmp(wallet.URL()) >= 0 })
+ if n == len(slice) {
+ // Wallet not found, may happen during startup
+ continue
+ }
+ slice = append(slice[:n], slice[n+1:]...)
+ }
+ return slice
+}
diff --git a/accounts/scwallet/README.md b/accounts/scwallet/README.md
new file mode 100644
index 0000000..cfca916
--- /dev/null
+++ b/accounts/scwallet/README.md
@@ -0,0 +1,102 @@
+# Using the smartcard wallet
+
+## Requirements
+
+ * A USB smartcard reader
+ * A keycard that supports the status app
+ * PCSCD version 4.3 running on your system **Only version 4.3 is currently supported**
+
+## Preparing the smartcard
+
+ **WARNING: FOILLOWING THESE INSTRUCTIONS WILL DESTROY THE MASTER KEY ON YOUR CARD. ONLY PROCEED IF NO FUNDS ARE ASSOCIATED WITH THESE ACCOUNTS**
+
+ You can use status' [keycard-cli](https://github.com/status-im/keycard-cli) and you should get _at least_ version 2.1.1 of their [smartcard application](https://github.com/status-im/status-keycard/releases/download/2.2.1/keycard_v2.2.1.cap)
+
+ You also need to make sure that the PCSC daemon is running on your system.
+
+ Then, you can install the application to the card by typing:
+
+ ```
+ keycard install -a keycard_v2.2.1.cap && keycard init
+ ```
+
+ At the end of this process, you will be provided with a PIN, a PUK and a pairing password. Write them down, you'll need them shortly.
+
+ Start `geth` with the `console` command. You will notice the following warning:
+
+ ```
+ WARN [04-09|16:58:38.898] Failed to open wallet url=keycard://044def09 err="smartcard: pairing password needed"
+ ```
+
+ Write down the URL (`keycard://044def09` in this example). Then ask `geth` to open the wallet:
+
+ ```
+ > personal.openWallet("keycard://044def09")
+ Please enter the pairing password:
+ ```
+
+ Enter the pairing password that you have received during card initialization. Same with the PIN that you will subsequently be
+ asked for.
+
+ If everything goes well, you should see your new account when typing `personal` on the console:
+
+ ```
+ > personal
+ WARN [04-09|17:02:07.330] Smartcard wallet account derivation failed url=keycard://044def09 err="Unexpected response status Cla=0x80, Ins=0xd1, Sw=0x6985"
+ {
+ listAccounts: [],
+ listWallets: [{
+ status: "Empty, waiting for initialization",
+ url: "keycard://044def09"
+ }],
+ ...
+ }
+ ```
+
+ So the communication with the card is working, but there is no key associated with this wallet. Let's create it:
+
+ ```
+ > personal.initializeWallet("keycard://044def09")
+ "tilt ... impact"
+ ```
+
+ You should get a list of words, this is your seed so write them down. Your wallet should now be initialized:
+
+ ```
+ > personal.listWallets
+ [{
+ accounts: [{
+ address: "0x678b7cd55c61917defb23546a41803c5bfefbc7a",
+ url: "keycard://044d/m/44'/60'/0'/0/0"
+ }],
+ status: "Online",
+ url: "keycard://044def09"
+ }]
+ ```
+
+ You're all set!
+
+## Usage
+
+ 1. Start `geth` with the `console` command
+ 2. Check the card's URL by checking `personal.listWallets`:
+
+```
+ listWallets: [{
+ status: "Online, can derive public keys",
+ url: "keycard://a4d73015"
+ }]
+```
+
+ 3. Open the wallet, you will be prompted for your pairing password, then PIN:
+
+```
+personal.openWallet("keycard://a4d73015")
+```
+
+ 4. Check that creation was successful by typing e.g. `personal`. Then use it like a regular wallet.
+
+## Known issues
+
+ * Starting geth with a valid card seems to make firefox crash.
+ * PCSC version 4.4 should work, but is currently untested
diff --git a/accounts/scwallet/apdu.go b/accounts/scwallet/apdu.go
new file mode 100644
index 0000000..bd36606
--- /dev/null
+++ b/accounts/scwallet/apdu.go
@@ -0,0 +1,87 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package scwallet
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+)
+
+// commandAPDU represents an application data unit sent to a smartcard.
+type commandAPDU struct {
+ Cla, Ins, P1, P2 uint8 // Class, Instruction, Parameter 1, Parameter 2
+ Data []byte // Command data
+ Le uint8 // Command data length
+}
+
+// serialize serializes a command APDU.
+func (ca commandAPDU) serialize() ([]byte, error) {
+ buf := new(bytes.Buffer)
+
+ if err := binary.Write(buf, binary.BigEndian, ca.Cla); err != nil {
+ return nil, err
+ }
+ if err := binary.Write(buf, binary.BigEndian, ca.Ins); err != nil {
+ return nil, err
+ }
+ if err := binary.Write(buf, binary.BigEndian, ca.P1); err != nil {
+ return nil, err
+ }
+ if err := binary.Write(buf, binary.BigEndian, ca.P2); err != nil {
+ return nil, err
+ }
+ if len(ca.Data) > 0 {
+ if err := binary.Write(buf, binary.BigEndian, uint8(len(ca.Data))); err != nil {
+ return nil, err
+ }
+ if err := binary.Write(buf, binary.BigEndian, ca.Data); err != nil {
+ return nil, err
+ }
+ }
+ if err := binary.Write(buf, binary.BigEndian, ca.Le); err != nil {
+ return nil, err
+ }
+ return buf.Bytes(), nil
+}
+
+// responseAPDU represents an application data unit received from a smart card.
+type responseAPDU struct {
+ Data []byte // response data
+ Sw1, Sw2 uint8 // status words 1 and 2
+}
+
+// deserialize deserializes a response APDU.
+func (ra *responseAPDU) deserialize(data []byte) error {
+ if len(data) < 2 {
+ return fmt.Errorf("can not deserialize data: payload too short (%d < 2)", len(data))
+ }
+
+ ra.Data = make([]byte, len(data)-2)
+
+ buf := bytes.NewReader(data)
+ if err := binary.Read(buf, binary.BigEndian, &ra.Data); err != nil {
+ return err
+ }
+ if err := binary.Read(buf, binary.BigEndian, &ra.Sw1); err != nil {
+ return err
+ }
+ if err := binary.Read(buf, binary.BigEndian, &ra.Sw2); err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/accounts/scwallet/hub.go b/accounts/scwallet/hub.go
new file mode 100644
index 0000000..ceb422c
--- /dev/null
+++ b/accounts/scwallet/hub.go
@@ -0,0 +1,302 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// This package implements support for smartcard-based hardware wallets such as
+// the one written by Status: https://github.com/status-im/hardware-wallet
+//
+// This implementation of smartcard wallets have a different interaction process
+// to other types of hardware wallet. The process works like this:
+//
+// 1. (First use with a given client) Establish a pairing between hardware
+// wallet and client. This requires a secret value called a 'pairing password'.
+// You can pair with an unpaired wallet with `personal.openWallet(URI, pairing password)`.
+// 2. (First use only) Initialize the wallet, which generates a keypair, stores
+// it on the wallet, and returns it so the user can back it up. You can
+// initialize a wallet with `personal.initializeWallet(URI)`.
+// 3. Connect to the wallet using the pairing information established in step 1.
+// You can connect to a paired wallet with `personal.openWallet(URI, PIN)`.
+// 4. Interact with the wallet as normal.
+
+package scwallet
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "sort"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/event"
+ "github.com/ava-labs/go-ethereum/log"
+ pcsc "github.com/gballet/go-libpcsclite"
+)
+
+// Scheme is the URI prefix for smartcard wallets.
+const Scheme = "keycard"
+
+// refreshCycle is the maximum time between wallet refreshes (if USB hotplug
+// notifications don't work).
+const refreshCycle = time.Second
+
+// refreshThrottling is the minimum time between wallet refreshes to avoid thrashing.
+const refreshThrottling = 500 * time.Millisecond
+
+// smartcardPairing contains information about a smart card we have paired with
+// or might pair with the hub.
+type smartcardPairing struct {
+ PublicKey []byte `json:"publicKey"`
+ PairingIndex uint8 `json:"pairingIndex"`
+ PairingKey []byte `json:"pairingKey"`
+ Accounts map[common.Address]accounts.DerivationPath `json:"accounts"`
+}
+
+// Hub is a accounts.Backend that can find and handle generic PC/SC hardware wallets.
+type Hub struct {
+ scheme string // Protocol scheme prefixing account and wallet URLs.
+
+ context *pcsc.Client
+ datadir string
+ pairings map[string]smartcardPairing
+
+ refreshed time.Time // Time instance when the list of wallets was last refreshed
+ wallets map[string]*Wallet // Mapping from reader names to wallet instances
+ updateFeed event.Feed // Event feed to notify wallet additions/removals
+ updateScope event.SubscriptionScope // Subscription scope tracking current live listeners
+ updating bool // Whether the event notification loop is running
+
+ quit chan chan error
+
+ stateLock sync.RWMutex // Protects the internals of the hub from racey access
+}
+
+func (hub *Hub) readPairings() error {
+ hub.pairings = make(map[string]smartcardPairing)
+ pairingFile, err := os.Open(filepath.Join(hub.datadir, "smartcards.json"))
+ if err != nil {
+ if os.IsNotExist(err) {
+ return nil
+ }
+ return err
+ }
+
+ pairingData, err := ioutil.ReadAll(pairingFile)
+ if err != nil {
+ return err
+ }
+ var pairings []smartcardPairing
+ if err := json.Unmarshal(pairingData, &pairings); err != nil {
+ return err
+ }
+
+ for _, pairing := range pairings {
+ hub.pairings[string(pairing.PublicKey)] = pairing
+ }
+ return nil
+}
+
+func (hub *Hub) writePairings() error {
+ pairingFile, err := os.OpenFile(filepath.Join(hub.datadir, "smartcards.json"), os.O_RDWR|os.O_CREATE, 0755)
+ if err != nil {
+ return err
+ }
+ defer pairingFile.Close()
+
+ pairings := make([]smartcardPairing, 0, len(hub.pairings))
+ for _, pairing := range hub.pairings {
+ pairings = append(pairings, pairing)
+ }
+
+ pairingData, err := json.Marshal(pairings)
+ if err != nil {
+ return err
+ }
+
+ if _, err := pairingFile.Write(pairingData); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (hub *Hub) pairing(wallet *Wallet) *smartcardPairing {
+ if pairing, ok := hub.pairings[string(wallet.PublicKey)]; ok {
+ return &pairing
+ }
+ return nil
+}
+
+func (hub *Hub) setPairing(wallet *Wallet, pairing *smartcardPairing) error {
+ if pairing == nil {
+ delete(hub.pairings, string(wallet.PublicKey))
+ } else {
+ hub.pairings[string(wallet.PublicKey)] = *pairing
+ }
+ return hub.writePairings()
+}
+
+// NewHub creates a new hardware wallet manager for smartcards.
+func NewHub(daemonPath string, scheme string, datadir string) (*Hub, error) {
+ context, err := pcsc.EstablishContext(daemonPath, pcsc.ScopeSystem)
+ if err != nil {
+ return nil, err
+ }
+ hub := &Hub{
+ scheme: scheme,
+ context: context,
+ datadir: datadir,
+ wallets: make(map[string]*Wallet),
+ quit: make(chan chan error),
+ }
+ if err := hub.readPairings(); err != nil {
+ return nil, err
+ }
+ hub.refreshWallets()
+ return hub, nil
+}
+
+// Wallets implements accounts.Backend, returning all the currently tracked smart
+// cards that appear to be hardware wallets.
+func (hub *Hub) Wallets() []accounts.Wallet {
+ // Make sure the list of wallets is up to date
+ hub.refreshWallets()
+
+ hub.stateLock.RLock()
+ defer hub.stateLock.RUnlock()
+
+ cpy := make([]accounts.Wallet, 0, len(hub.wallets))
+ for _, wallet := range hub.wallets {
+ cpy = append(cpy, wallet)
+ }
+ sort.Sort(accounts.WalletsByURL(cpy))
+ return cpy
+}
+
+// refreshWallets scans the devices attached to the machine and updates the
+// list of wallets based on the found devices.
+func (hub *Hub) refreshWallets() {
+ // Don't scan the USB like crazy it the user fetches wallets in a loop
+ hub.stateLock.RLock()
+ elapsed := time.Since(hub.refreshed)
+ hub.stateLock.RUnlock()
+
+ if elapsed < refreshThrottling {
+ return
+ }
+ // Retrieve all the smart card reader to check for cards
+ readers, err := hub.context.ListReaders()
+ if err != nil {
+ // This is a perverted hack, the scard library returns an error if no card
+ // readers are present instead of simply returning an empty list. We don't
+ // want to fill the user's log with errors, so filter those out.
+ if err.Error() != "scard: Cannot find a smart card reader." {
+ log.Error("Failed to enumerate smart card readers", "err", err)
+ return
+ }
+ }
+ // Transform the current list of wallets into the new one
+ hub.stateLock.Lock()
+
+ events := []accounts.WalletEvent{}
+ seen := make(map[string]struct{})
+
+ for _, reader := range readers {
+ // Mark the reader as present
+ seen[reader] = struct{}{}
+
+ // If we alreay know about this card, skip to the next reader, otherwise clean up
+ if wallet, ok := hub.wallets[reader]; ok {
+ if err := wallet.ping(); err == nil {
+ continue
+ }
+ wallet.Close()
+ events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped})
+ delete(hub.wallets, reader)
+ }
+ // New card detected, try to connect to it
+ card, err := hub.context.Connect(reader, pcsc.ShareShared, pcsc.ProtocolAny)
+ if err != nil {
+ log.Debug("Failed to open smart card", "reader", reader, "err", err)
+ continue
+ }
+ wallet := NewWallet(hub, card)
+ if err = wallet.connect(); err != nil {
+ log.Debug("Failed to connect to smart card", "reader", reader, "err", err)
+ card.Disconnect(pcsc.LeaveCard)
+ continue
+ }
+ // Card connected, start tracking in amongs the wallets
+ hub.wallets[reader] = wallet
+ events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
+ }
+ // Remove any wallets no longer present
+ for reader, wallet := range hub.wallets {
+ if _, ok := seen[reader]; !ok {
+ wallet.Close()
+ events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped})
+ delete(hub.wallets, reader)
+ }
+ }
+ hub.refreshed = time.Now()
+ hub.stateLock.Unlock()
+
+ for _, event := range events {
+ hub.updateFeed.Send(event)
+ }
+}
+
+// Subscribe implements accounts.Backend, creating an async subscription to
+// receive notifications on the addition or removal of smart card wallets.
+func (hub *Hub) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription {
+ // We need the mutex to reliably start/stop the update loop
+ hub.stateLock.Lock()
+ defer hub.stateLock.Unlock()
+
+ // Subscribe the caller and track the subscriber count
+ sub := hub.updateScope.Track(hub.updateFeed.Subscribe(sink))
+
+ // Subscribers require an active notification loop, start it
+ if !hub.updating {
+ hub.updating = true
+ go hub.updater()
+ }
+ return sub
+}
+
+// updater is responsible for maintaining an up-to-date list of wallets managed
+// by the smart card hub, and for firing wallet addition/removal events.
+func (hub *Hub) updater() {
+ for {
+ // TODO: Wait for a USB hotplug event (not supported yet) or a refresh timeout
+ // <-hub.changes
+ time.Sleep(refreshCycle)
+
+ // Run the wallet refresher
+ hub.refreshWallets()
+
+ // If all our subscribers left, stop the updater
+ hub.stateLock.Lock()
+ if hub.updateScope.Count() == 0 {
+ hub.updating = false
+ hub.stateLock.Unlock()
+ return
+ }
+ hub.stateLock.Unlock()
+ }
+}
diff --git a/accounts/scwallet/securechannel.go b/accounts/scwallet/securechannel.go
new file mode 100644
index 0000000..c1b7cff
--- /dev/null
+++ b/accounts/scwallet/securechannel.go
@@ -0,0 +1,346 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package scwallet
+
+import (
+ "bytes"
+ "crypto/aes"
+ "crypto/cipher"
+ "crypto/rand"
+ "crypto/sha256"
+ "crypto/sha512"
+ "fmt"
+
+ "github.com/ava-labs/go-ethereum/crypto"
+ pcsc "github.com/gballet/go-libpcsclite"
+ "github.com/wsddn/go-ecdh"
+ "golang.org/x/crypto/pbkdf2"
+ "golang.org/x/text/unicode/norm"
+)
+
+const (
+ maxPayloadSize = 223
+ pairP1FirstStep = 0
+ pairP1LastStep = 1
+
+ scSecretLength = 32
+ scBlockSize = 16
+
+ insOpenSecureChannel = 0x10
+ insMutuallyAuthenticate = 0x11
+ insPair = 0x12
+ insUnpair = 0x13
+
+ pairingSalt = "Keycard Pairing Password Salt"
+)
+
+// SecureChannelSession enables secure communication with a hardware wallet.
+type SecureChannelSession struct {
+ card *pcsc.Card // A handle to the smartcard for communication
+ secret []byte // A shared secret generated from our ECDSA keys
+ publicKey []byte // Our own ephemeral public key
+ PairingKey []byte // A permanent shared secret for a pairing, if present
+ sessionEncKey []byte // The current session encryption key
+ sessionMacKey []byte // The current session MAC key
+ iv []byte // The current IV
+ PairingIndex uint8 // The pairing index
+}
+
+// NewSecureChannelSession creates a new secure channel for the given card and public key.
+func NewSecureChannelSession(card *pcsc.Card, keyData []byte) (*SecureChannelSession, error) {
+ // Generate an ECDSA keypair for ourselves
+ gen := ecdh.NewEllipticECDH(crypto.S256())
+ private, public, err := gen.GenerateKey(rand.Reader)
+ if err != nil {
+ return nil, err
+ }
+
+ cardPublic, ok := gen.Unmarshal(keyData)
+ if !ok {
+ return nil, fmt.Errorf("Could not unmarshal public key from card")
+ }
+
+ secret, err := gen.GenerateSharedSecret(private, cardPublic)
+ if err != nil {
+ return nil, err
+ }
+
+ return &SecureChannelSession{
+ card: card,
+ secret: secret,
+ publicKey: gen.Marshal(public),
+ }, nil
+}
+
+// Pair establishes a new pairing with the smartcard.
+func (s *SecureChannelSession) Pair(pairingPassword []byte) error {
+ secretHash := pbkdf2.Key(norm.NFKD.Bytes(pairingPassword), norm.NFKD.Bytes([]byte(pairingSalt)), 50000, 32, sha256.New)
+
+ challenge := make([]byte, 32)
+ if _, err := rand.Read(challenge); err != nil {
+ return err
+ }
+
+ response, err := s.pair(pairP1FirstStep, challenge)
+ if err != nil {
+ return err
+ }
+
+ md := sha256.New()
+ md.Write(secretHash[:])
+ md.Write(challenge)
+
+ expectedCryptogram := md.Sum(nil)
+ cardCryptogram := response.Data[:32]
+ cardChallenge := response.Data[32:64]
+
+ if !bytes.Equal(expectedCryptogram, cardCryptogram) {
+ return fmt.Errorf("Invalid card cryptogram %v != %v", expectedCryptogram, cardCryptogram)
+ }
+
+ md.Reset()
+ md.Write(secretHash[:])
+ md.Write(cardChallenge)
+ response, err = s.pair(pairP1LastStep, md.Sum(nil))
+ if err != nil {
+ return err
+ }
+
+ md.Reset()
+ md.Write(secretHash[:])
+ md.Write(response.Data[1:])
+ s.PairingKey = md.Sum(nil)
+ s.PairingIndex = response.Data[0]
+
+ return nil
+}
+
+// Unpair disestablishes an existing pairing.
+func (s *SecureChannelSession) Unpair() error {
+ if s.PairingKey == nil {
+ return fmt.Errorf("Cannot unpair: not paired")
+ }
+
+ _, err := s.transmitEncrypted(claSCWallet, insUnpair, s.PairingIndex, 0, []byte{})
+ if err != nil {
+ return err
+ }
+ s.PairingKey = nil
+ // Close channel
+ s.iv = nil
+ return nil
+}
+
+// Open initializes the secure channel.
+func (s *SecureChannelSession) Open() error {
+ if s.iv != nil {
+ return fmt.Errorf("Session already opened")
+ }
+
+ response, err := s.open()
+ if err != nil {
+ return err
+ }
+
+ // Generate the encryption/mac key by hashing our shared secret,
+ // pairing key, and the first bytes returned from the Open APDU.
+ md := sha512.New()
+ md.Write(s.secret)
+ md.Write(s.PairingKey)
+ md.Write(response.Data[:scSecretLength])
+ keyData := md.Sum(nil)
+ s.sessionEncKey = keyData[:scSecretLength]
+ s.sessionMacKey = keyData[scSecretLength : scSecretLength*2]
+
+ // The IV is the last bytes returned from the Open APDU.
+ s.iv = response.Data[scSecretLength:]
+
+ return s.mutuallyAuthenticate()
+}
+
+// mutuallyAuthenticate is an internal method to authenticate both ends of the
+// connection.
+func (s *SecureChannelSession) mutuallyAuthenticate() error {
+ data := make([]byte, scSecretLength)
+ if _, err := rand.Read(data); err != nil {
+ return err
+ }
+
+ response, err := s.transmitEncrypted(claSCWallet, insMutuallyAuthenticate, 0, 0, data)
+ if err != nil {
+ return err
+ }
+ if response.Sw1 != 0x90 || response.Sw2 != 0x00 {
+ return fmt.Errorf("Got unexpected response from MUTUALLY_AUTHENTICATE: 0x%x%x", response.Sw1, response.Sw2)
+ }
+
+ if len(response.Data) != scSecretLength {
+ return fmt.Errorf("Response from MUTUALLY_AUTHENTICATE was %d bytes, expected %d", len(response.Data), scSecretLength)
+ }
+
+ return nil
+}
+
+// open is an internal method that sends an open APDU.
+func (s *SecureChannelSession) open() (*responseAPDU, error) {
+ return transmit(s.card, &commandAPDU{
+ Cla: claSCWallet,
+ Ins: insOpenSecureChannel,
+ P1: s.PairingIndex,
+ P2: 0,
+ Data: s.publicKey,
+ Le: 0,
+ })
+}
+
+// pair is an internal method that sends a pair APDU.
+func (s *SecureChannelSession) pair(p1 uint8, data []byte) (*responseAPDU, error) {
+ return transmit(s.card, &commandAPDU{
+ Cla: claSCWallet,
+ Ins: insPair,
+ P1: p1,
+ P2: 0,
+ Data: data,
+ Le: 0,
+ })
+}
+
+// transmitEncrypted sends an encrypted message, and decrypts and returns the response.
+func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*responseAPDU, error) {
+ if s.iv == nil {
+ return nil, fmt.Errorf("Channel not open")
+ }
+
+ data, err := s.encryptAPDU(data)
+ if err != nil {
+ return nil, err
+ }
+ meta := [16]byte{cla, ins, p1, p2, byte(len(data) + scBlockSize)}
+ if err = s.updateIV(meta[:], data); err != nil {
+ return nil, err
+ }
+
+ fulldata := make([]byte, len(s.iv)+len(data))
+ copy(fulldata, s.iv)
+ copy(fulldata[len(s.iv):], data)
+
+ response, err := transmit(s.card, &commandAPDU{
+ Cla: cla,
+ Ins: ins,
+ P1: p1,
+ P2: p2,
+ Data: fulldata,
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ rmeta := [16]byte{byte(len(response.Data))}
+ rmac := response.Data[:len(s.iv)]
+ rdata := response.Data[len(s.iv):]
+ plainData, err := s.decryptAPDU(rdata)
+ if err != nil {
+ return nil, err
+ }
+
+ if err = s.updateIV(rmeta[:], rdata); err != nil {
+ return nil, err
+ }
+ if !bytes.Equal(s.iv, rmac) {
+ return nil, fmt.Errorf("Invalid MAC in response")
+ }
+
+ rapdu := &responseAPDU{}
+ rapdu.deserialize(plainData)
+
+ if rapdu.Sw1 != sw1Ok {
+ return nil, fmt.Errorf("Unexpected response status Cla=0x%x, Ins=0x%x, Sw=0x%x%x", cla, ins, rapdu.Sw1, rapdu.Sw2)
+ }
+
+ return rapdu, nil
+}
+
+// encryptAPDU is an internal method that serializes and encrypts an APDU.
+func (s *SecureChannelSession) encryptAPDU(data []byte) ([]byte, error) {
+ if len(data) > maxPayloadSize {
+ return nil, fmt.Errorf("Payload of %d bytes exceeds maximum of %d", len(data), maxPayloadSize)
+ }
+ data = pad(data, 0x80)
+
+ ret := make([]byte, len(data))
+
+ a, err := aes.NewCipher(s.sessionEncKey)
+ if err != nil {
+ return nil, err
+ }
+ crypter := cipher.NewCBCEncrypter(a, s.iv)
+ crypter.CryptBlocks(ret, data)
+ return ret, nil
+}
+
+// pad applies message padding to a 16 byte boundary.
+func pad(data []byte, terminator byte) []byte {
+ padded := make([]byte, (len(data)/16+1)*16)
+ copy(padded, data)
+ padded[len(data)] = terminator
+ return padded
+}
+
+// decryptAPDU is an internal method that decrypts and deserializes an APDU.
+func (s *SecureChannelSession) decryptAPDU(data []byte) ([]byte, error) {
+ a, err := aes.NewCipher(s.sessionEncKey)
+ if err != nil {
+ return nil, err
+ }
+
+ ret := make([]byte, len(data))
+
+ crypter := cipher.NewCBCDecrypter(a, s.iv)
+ crypter.CryptBlocks(ret, data)
+ return unpad(ret, 0x80)
+}
+
+// unpad strips padding from a message.
+func unpad(data []byte, terminator byte) ([]byte, error) {
+ for i := 1; i <= 16; i++ {
+ switch data[len(data)-i] {
+ case 0:
+ continue
+ case terminator:
+ return data[:len(data)-i], nil
+ default:
+ return nil, fmt.Errorf("Expected end of padding, got %d", data[len(data)-i])
+ }
+ }
+ return nil, fmt.Errorf("Expected end of padding, got 0")
+}
+
+// updateIV is an internal method that updates the initialization vector after
+// each message exchanged.
+func (s *SecureChannelSession) updateIV(meta, data []byte) error {
+ data = pad(data, 0)
+ a, err := aes.NewCipher(s.sessionMacKey)
+ if err != nil {
+ return err
+ }
+ crypter := cipher.NewCBCEncrypter(a, make([]byte, 16))
+ crypter.CryptBlocks(meta, meta)
+ crypter.CryptBlocks(data, data)
+ // The first 16 bytes of the last block is the MAC
+ s.iv = data[len(data)-32 : len(data)-16]
+ return nil
+}
diff --git a/accounts/scwallet/wallet.go b/accounts/scwallet/wallet.go
new file mode 100644
index 0000000..f72e685
--- /dev/null
+++ b/accounts/scwallet/wallet.go
@@ -0,0 +1,1082 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package scwallet
+
+import (
+ "bytes"
+ "context"
+ "crypto/hmac"
+ "crypto/sha256"
+ "crypto/sha512"
+ "encoding/asn1"
+ "encoding/binary"
+ "errors"
+ "fmt"
+ "math/big"
+ "regexp"
+ "sort"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/types"
+ ethereum "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/log"
+ pcsc "github.com/gballet/go-libpcsclite"
+ "github.com/status-im/keycard-go/derivationpath"
+)
+
+// ErrPairingPasswordNeeded is returned if opening the smart card requires pairing with a pairing
+// password. In this case, the calling application should request user input to enter
+// the pairing password and send it back.
+var ErrPairingPasswordNeeded = errors.New("smartcard: pairing password needed")
+
+// ErrPINNeeded is returned if opening the smart card requires a PIN code. In
+// this case, the calling application should request user input to enter the PIN
+// and send it back.
+var ErrPINNeeded = errors.New("smartcard: pin needed")
+
+// ErrPINUnblockNeeded is returned if opening the smart card requires a PIN code,
+// but all PIN attempts have already been exhausted. In this case the calling
+// application should request user input for the PUK and a new PIN code to set
+// fo the card.
+var ErrPINUnblockNeeded = errors.New("smartcard: pin unblock needed")
+
+// ErrAlreadyOpen is returned if the smart card is attempted to be opened, but
+// there is already a paired and unlocked session.
+var ErrAlreadyOpen = errors.New("smartcard: already open")
+
+// ErrPubkeyMismatch is returned if the public key recovered from a signature
+// does not match the one expected by the user.
+var ErrPubkeyMismatch = errors.New("smartcard: recovered public key mismatch")
+
+var (
+ appletAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01, 0x01, 0x01}
+ // DerivationSignatureHash is used to derive the public key from the signature of this hash
+ DerivationSignatureHash = sha256.Sum256(common.Hash{}.Bytes())
+)
+
+// List of APDU command-related constants
+const (
+ claISO7816 = 0
+ claSCWallet = 0x80
+
+ insSelect = 0xA4
+ insGetResponse = 0xC0
+ sw1GetResponse = 0x61
+ sw1Ok = 0x90
+
+ insVerifyPin = 0x20
+ insUnblockPin = 0x22
+ insExportKey = 0xC2
+ insSign = 0xC0
+ insLoadKey = 0xD0
+ insDeriveKey = 0xD1
+ insStatus = 0xF2
+)
+
+// List of ADPU command parameters
+const (
+ P1DeriveKeyFromMaster = uint8(0x00)
+ P1DeriveKeyFromParent = uint8(0x01)
+ P1DeriveKeyFromCurrent = uint8(0x10)
+ statusP1WalletStatus = uint8(0x00)
+ statusP1Path = uint8(0x01)
+ signP1PrecomputedHash = uint8(0x01)
+ signP2OnlyBlock = uint8(0x81)
+ exportP1Any = uint8(0x00)
+ exportP2Pubkey = uint8(0x01)
+)
+
+// Minimum time to wait between self derivation attempts, even it the user is
+// requesting accounts like crazy.
+const selfDeriveThrottling = time.Second
+
+// Wallet represents a smartcard wallet instance.
+type Wallet struct {
+ Hub *Hub // A handle to the Hub that instantiated this wallet.
+ PublicKey []byte // The wallet's public key (used for communication and identification, not signing!)
+
+ lock sync.Mutex // Lock that gates access to struct fields and communication with the card
+ card *pcsc.Card // A handle to the smartcard interface for the wallet.
+ session *Session // The secure communication session with the card
+ log log.Logger // Contextual logger to tag the base with its id
+
+ deriveNextPaths []accounts.DerivationPath // Next derivation paths for account auto-discovery (multiple bases supported)
+ deriveNextAddrs []common.Address // Next derived account addresses for auto-discovery (multiple bases supported)
+ deriveChain ethereum.ChainStateReader // Blockchain state reader to discover used account with
+ deriveReq chan chan struct{} // Channel to request a self-derivation on
+ deriveQuit chan chan error // Channel to terminate the self-deriver with
+}
+
+// NewWallet constructs and returns a new Wallet instance.
+func NewWallet(hub *Hub, card *pcsc.Card) *Wallet {
+ wallet := &Wallet{
+ Hub: hub,
+ card: card,
+ }
+ return wallet
+}
+
+// transmit sends an APDU to the smartcard and receives and decodes the response.
+// It automatically handles requests by the card to fetch the return data separately,
+// and returns an error if the response status code is not success.
+func transmit(card *pcsc.Card, command *commandAPDU) (*responseAPDU, error) {
+ data, err := command.serialize()
+ if err != nil {
+ return nil, err
+ }
+
+ responseData, _, err := card.Transmit(data)
+ if err != nil {
+ return nil, err
+ }
+
+ response := new(responseAPDU)
+ if err = response.deserialize(responseData); err != nil {
+ return nil, err
+ }
+
+ // Are we being asked to fetch the response separately?
+ if response.Sw1 == sw1GetResponse && (command.Cla != claISO7816 || command.Ins != insGetResponse) {
+ return transmit(card, &commandAPDU{
+ Cla: claISO7816,
+ Ins: insGetResponse,
+ P1: 0,
+ P2: 0,
+ Data: nil,
+ Le: response.Sw2,
+ })
+ }
+
+ if response.Sw1 != sw1Ok {
+ return nil, fmt.Errorf("Unexpected insecure response status Cla=0x%x, Ins=0x%x, Sw=0x%x%x", command.Cla, command.Ins, response.Sw1, response.Sw2)
+ }
+
+ return response, nil
+}
+
+// applicationInfo encodes information about the smartcard application - its
+// instance UID and public key.
+type applicationInfo struct {
+ InstanceUID []byte `asn1:"tag:15"`
+ PublicKey []byte `asn1:"tag:0"`
+}
+
+// connect connects to the wallet application and establishes a secure channel with it.
+// must be called before any other interaction with the wallet.
+func (w *Wallet) connect() error {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ appinfo, err := w.doselect()
+ if err != nil {
+ return err
+ }
+
+ channel, err := NewSecureChannelSession(w.card, appinfo.PublicKey)
+ if err != nil {
+ return err
+ }
+
+ w.PublicKey = appinfo.PublicKey
+ w.log = log.New("url", w.URL())
+ w.session = &Session{
+ Wallet: w,
+ Channel: channel,
+ }
+ return nil
+}
+
+// doselect is an internal (unlocked) function to send a SELECT APDU to the card.
+func (w *Wallet) doselect() (*applicationInfo, error) {
+ response, err := transmit(w.card, &commandAPDU{
+ Cla: claISO7816,
+ Ins: insSelect,
+ P1: 4,
+ P2: 0,
+ Data: appletAID,
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ appinfo := new(applicationInfo)
+ if _, err := asn1.UnmarshalWithParams(response.Data, appinfo, "tag:4"); err != nil {
+ return nil, err
+ }
+ return appinfo, nil
+}
+
+// ping checks the card's status and returns an error if unsuccessful.
+func (w *Wallet) ping() error {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ // We can't ping if not paired
+ if !w.session.paired() {
+ return nil
+ }
+ if _, err := w.session.walletStatus(); err != nil {
+ return err
+ }
+ return nil
+}
+
+// release releases any resources held by an open wallet instance.
+func (w *Wallet) release() error {
+ if w.session != nil {
+ return w.session.release()
+ }
+ return nil
+}
+
+// pair is an internal (unlocked) function for establishing a new pairing
+// with the wallet.
+func (w *Wallet) pair(puk []byte) error {
+ if w.session.paired() {
+ return fmt.Errorf("Wallet already paired")
+ }
+ pairing, err := w.session.pair(puk)
+ if err != nil {
+ return err
+ }
+ if err = w.Hub.setPairing(w, &pairing); err != nil {
+ return err
+ }
+ return w.session.authenticate(pairing)
+}
+
+// Unpair deletes an existing wallet pairing.
+func (w *Wallet) Unpair(pin []byte) error {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ if !w.session.paired() {
+ return fmt.Errorf("wallet %x not paired", w.PublicKey)
+ }
+ if err := w.session.verifyPin(pin); err != nil {
+ return fmt.Errorf("failed to verify pin: %s", err)
+ }
+ if err := w.session.unpair(); err != nil {
+ return fmt.Errorf("failed to unpair: %s", err)
+ }
+ if err := w.Hub.setPairing(w, nil); err != nil {
+ return err
+ }
+ return nil
+}
+
+// URL retrieves the canonical path under which this wallet is reachable. It is
+// user by upper layers to define a sorting order over all wallets from multiple
+// backends.
+func (w *Wallet) URL() accounts.URL {
+ return accounts.URL{
+ Scheme: w.Hub.scheme,
+ Path: fmt.Sprintf("%x", w.PublicKey[1:5]), // Byte #0 isn't unique; 1:5 covers << 64K cards, bump to 1:9 for << 4M
+ }
+}
+
+// Status returns a textual status to aid the user in the current state of the
+// wallet. It also returns an error indicating any failure the wallet might have
+// encountered.
+func (w *Wallet) Status() (string, error) {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ // If the card is not paired, we can only wait
+ if !w.session.paired() {
+ return "Unpaired, waiting for pairing password", nil
+ }
+ // Yay, we have an encrypted session, retrieve the actual status
+ status, err := w.session.walletStatus()
+ if err != nil {
+ return fmt.Sprintf("Failed: %v", err), err
+ }
+ switch {
+ case !w.session.verified && status.PinRetryCount == 0 && status.PukRetryCount == 0:
+ return fmt.Sprintf("Bricked, waiting for full wipe"), nil
+ case !w.session.verified && status.PinRetryCount == 0:
+ return fmt.Sprintf("Blocked, waiting for PUK (%d attempts left) and new PIN", status.PukRetryCount), nil
+ case !w.session.verified:
+ return fmt.Sprintf("Locked, waiting for PIN (%d attempts left)", status.PinRetryCount), nil
+ case !status.Initialized:
+ return fmt.Sprintf("Empty, waiting for initialization"), nil
+ default:
+ return fmt.Sprintf("Online"), nil
+ }
+}
+
+// Open initializes access to a wallet instance. It is not meant to unlock or
+// decrypt account keys, rather simply to establish a connection to hardware
+// wallets and/or to access derivation seeds.
+//
+// The passphrase parameter may or may not be used by the implementation of a
+// particular wallet instance. The reason there is no passwordless open method
+// is to strive towards a uniform wallet handling, oblivious to the different
+// backend providers.
+//
+// Please note, if you open a wallet, you must close it to release any allocated
+// resources (especially important when working with hardware wallets).
+func (w *Wallet) Open(passphrase string) error {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ // If the session is already open, bail out
+ if w.session.verified {
+ return ErrAlreadyOpen
+ }
+ // If the smart card is not yet paired, attempt to do so either from a previous
+ // pairing key or form the supplied PUK code.
+ if !w.session.paired() {
+ // If a previous pairing exists, only ever try to use that
+ if pairing := w.Hub.pairing(w); pairing != nil {
+ if err := w.session.authenticate(*pairing); err != nil {
+ return fmt.Errorf("failed to authenticate card %x: %s", w.PublicKey[:4], err)
+ }
+ // Pairing still ok, fall through to PIN checks
+ } else {
+ // If no passphrase was supplied, request the PUK from the user
+ if passphrase == "" {
+ return ErrPairingPasswordNeeded
+ }
+ // Attempt to pair the smart card with the user supplied PUK
+ if err := w.pair([]byte(passphrase)); err != nil {
+ return err
+ }
+ // Pairing succeeded, fall through to PIN checks. This will of course fail,
+ // but we can't return ErrPINNeeded directly here becase we don't know whether
+ // a PIN check or a PIN reset is needed.
+ passphrase = ""
+ }
+ }
+ // The smart card was successfully paired, retrieve its status to check whether
+ // PIN verification or unblocking is needed.
+ status, err := w.session.walletStatus()
+ if err != nil {
+ return err
+ }
+ // Request the appropriate next authentication data, or use the one supplied
+ switch {
+ case passphrase == "" && status.PinRetryCount > 0:
+ return ErrPINNeeded
+ case passphrase == "":
+ return ErrPINUnblockNeeded
+ case status.PinRetryCount > 0:
+ if !regexp.MustCompile(`^[0-9]{6,}$`).MatchString(passphrase) {
+ w.log.Error("PIN needs to be at least 6 digits")
+ return ErrPINNeeded
+ }
+ if err := w.session.verifyPin([]byte(passphrase)); err != nil {
+ return err
+ }
+ default:
+ if !regexp.MustCompile(`^[0-9]{12,}$`).MatchString(passphrase) {
+ w.log.Error("PUK needs to be at least 12 digits")
+ return ErrPINUnblockNeeded
+ }
+ if err := w.session.unblockPin([]byte(passphrase)); err != nil {
+ return err
+ }
+ }
+ // Smart card paired and unlocked, initialize and register
+ w.deriveReq = make(chan chan struct{})
+ w.deriveQuit = make(chan chan error)
+
+ go w.selfDerive()
+
+ // Notify anyone listening for wallet events that a new device is accessible
+ go w.Hub.updateFeed.Send(accounts.WalletEvent{Wallet: w, Kind: accounts.WalletOpened})
+
+ return nil
+}
+
+// Close stops and closes the wallet, freeing any resources.
+func (w *Wallet) Close() error {
+ // Ensure the wallet was opened
+ w.lock.Lock()
+ dQuit := w.deriveQuit
+ w.lock.Unlock()
+
+ // Terminate the self-derivations
+ var derr error
+ if dQuit != nil {
+ errc := make(chan error)
+ dQuit <- errc
+ derr = <-errc // Save for later, we *must* close the USB
+ }
+ // Terminate the device connection
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ w.deriveQuit = nil
+ w.deriveReq = nil
+
+ if err := w.release(); err != nil {
+ return err
+ }
+ return derr
+}
+
+// selfDerive is an account derivation loop that upon request attempts to find
+// new non-zero accounts.
+func (w *Wallet) selfDerive() {
+ w.log.Debug("Smart card wallet self-derivation started")
+ defer w.log.Debug("Smart card wallet self-derivation stopped")
+
+ // Execute self-derivations until termination or error
+ var (
+ reqc chan struct{}
+ errc chan error
+ err error
+ )
+ for errc == nil && err == nil {
+ // Wait until either derivation or termination is requested
+ select {
+ case errc = <-w.deriveQuit:
+ // Termination requested
+ continue
+ case reqc = <-w.deriveReq:
+ // Account discovery requested
+ }
+ // Derivation needs a chain and device access, skip if either unavailable
+ w.lock.Lock()
+ if w.session == nil || w.deriveChain == nil {
+ w.lock.Unlock()
+ reqc <- struct{}{}
+ continue
+ }
+ pairing := w.Hub.pairing(w)
+
+ // Device lock obtained, derive the next batch of accounts
+ var (
+ paths []accounts.DerivationPath
+ nextAcc accounts.Account
+
+ nextPaths = append([]accounts.DerivationPath{}, w.deriveNextPaths...)
+ nextAddrs = append([]common.Address{}, w.deriveNextAddrs...)
+
+ context = context.Background()
+ )
+ for i := 0; i < len(nextAddrs); i++ {
+ for empty := false; !empty; {
+ // Retrieve the next derived Ethereum account
+ if nextAddrs[i] == (common.Address{}) {
+ if nextAcc, err = w.session.derive(nextPaths[i]); err != nil {
+ w.log.Warn("Smartcard wallet account derivation failed", "err", err)
+ break
+ }
+ nextAddrs[i] = nextAcc.Address
+ }
+ // Check the account's status against the current chain state
+ var (
+ balance *big.Int
+ nonce uint64
+ )
+ balance, err = w.deriveChain.BalanceAt(context, nextAddrs[i], nil)
+ if err != nil {
+ w.log.Warn("Smartcard wallet balance retrieval failed", "err", err)
+ break
+ }
+ nonce, err = w.deriveChain.NonceAt(context, nextAddrs[i], nil)
+ if err != nil {
+ w.log.Warn("Smartcard wallet nonce retrieval failed", "err", err)
+ break
+ }
+ // If the next account is empty, stop self-derivation, but add for the last base path
+ if balance.Sign() == 0 && nonce == 0 {
+ empty = true
+ if i < len(nextAddrs)-1 {
+ break
+ }
+ }
+ // We've just self-derived a new account, start tracking it locally
+ path := make(accounts.DerivationPath, len(nextPaths[i]))
+ copy(path[:], nextPaths[i][:])
+ paths = append(paths, path)
+
+ // Display a log message to the user for new (or previously empty accounts)
+ if _, known := pairing.Accounts[nextAddrs[i]]; !known || !empty || nextAddrs[i] != w.deriveNextAddrs[i] {
+ w.log.Info("Smartcard wallet discovered new account", "address", nextAddrs[i], "path", path, "balance", balance, "nonce", nonce)
+ }
+ pairing.Accounts[nextAddrs[i]] = path
+
+ // Fetch the next potential account
+ if !empty {
+ nextAddrs[i] = common.Address{}
+ nextPaths[i][len(nextPaths[i])-1]++
+ }
+ }
+ }
+ // If there are new accounts, write them out
+ if len(paths) > 0 {
+ err = w.Hub.setPairing(w, pairing)
+ }
+ // Shift the self-derivation forward
+ w.deriveNextAddrs = nextAddrs
+ w.deriveNextPaths = nextPaths
+
+ // Self derivation complete, release device lock
+ w.lock.Unlock()
+
+ // Notify the user of termination and loop after a bit of time (to avoid trashing)
+ reqc <- struct{}{}
+ if err == nil {
+ select {
+ case errc = <-w.deriveQuit:
+ // Termination requested, abort
+ case <-time.After(selfDeriveThrottling):
+ // Waited enough, willing to self-derive again
+ }
+ }
+ }
+ // In case of error, wait for termination
+ if err != nil {
+ w.log.Debug("Smartcard wallet self-derivation failed", "err", err)
+ errc = <-w.deriveQuit
+ }
+ errc <- err
+}
+
+// Accounts retrieves the list of signing accounts the wallet is currently aware
+// of. For hierarchical deterministic wallets, the list will not be exhaustive,
+// rather only contain the accounts explicitly pinned during account derivation.
+func (w *Wallet) Accounts() []accounts.Account {
+ // Attempt self-derivation if it's running
+ reqc := make(chan struct{}, 1)
+ select {
+ case w.deriveReq <- reqc:
+ // Self-derivation request accepted, wait for it
+ <-reqc
+ default:
+ // Self-derivation offline, throttled or busy, skip
+ }
+
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ if pairing := w.Hub.pairing(w); pairing != nil {
+ ret := make([]accounts.Account, 0, len(pairing.Accounts))
+ for address, path := range pairing.Accounts {
+ ret = append(ret, w.makeAccount(address, path))
+ }
+ sort.Sort(accounts.AccountsByURL(ret))
+ return ret
+ }
+ return nil
+}
+
+func (w *Wallet) makeAccount(address common.Address, path accounts.DerivationPath) accounts.Account {
+ return accounts.Account{
+ Address: address,
+ URL: accounts.URL{
+ Scheme: w.Hub.scheme,
+ Path: fmt.Sprintf("%x/%s", w.PublicKey[1:3], path.String()),
+ },
+ }
+}
+
+// Contains returns whether an account is part of this particular wallet or not.
+func (w *Wallet) Contains(account accounts.Account) bool {
+ if pairing := w.Hub.pairing(w); pairing != nil {
+ _, ok := pairing.Accounts[account.Address]
+ return ok
+ }
+ return false
+}
+
+// Initialize installs a keypair generated from the provided key into the wallet.
+func (w *Wallet) Initialize(seed []byte) error {
+ go w.selfDerive()
+ // DO NOT lock at this stage, as the initialize
+ // function relies on Status()
+ return w.session.initialize(seed)
+}
+
+// Derive attempts to explicitly derive a hierarchical deterministic account at
+// the specified derivation path. If requested, the derived account will be added
+// to the wallet's tracked account list.
+func (w *Wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ account, err := w.session.derive(path)
+ if err != nil {
+ return accounts.Account{}, err
+ }
+
+ if pin {
+ pairing := w.Hub.pairing(w)
+ pairing.Accounts[account.Address] = path
+ if err := w.Hub.setPairing(w, pairing); err != nil {
+ return accounts.Account{}, err
+ }
+ }
+
+ return account, nil
+}
+
+// SelfDerive sets a base account derivation path from which the wallet attempts
+// to discover non zero accounts and automatically add them to list of tracked
+// accounts.
+//
+// Note, self derivaton will increment the last component of the specified path
+// opposed to decending into a child path to allow discovering accounts starting
+// from non zero components.
+//
+// Some hardware wallets switched derivation paths through their evolution, so
+// this method supports providing multiple bases to discover old user accounts
+// too. Only the last base will be used to derive the next empty account.
+//
+// You can disable automatic account discovery by calling SelfDerive with a nil
+// chain state reader.
+func (w *Wallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ w.deriveNextPaths = make([]accounts.DerivationPath, len(bases))
+ for i, base := range bases {
+ w.deriveNextPaths[i] = make(accounts.DerivationPath, len(base))
+ copy(w.deriveNextPaths[i][:], base[:])
+ }
+ w.deriveNextAddrs = make([]common.Address, len(bases))
+ w.deriveChain = chain
+}
+
+// SignData requests the wallet to sign the hash of the given data.
+//
+// It looks up the account specified either solely via its address contained within,
+// or optionally with the aid of any location metadata from the embedded URL field.
+//
+// If the wallet requires additional authentication to sign the request (e.g.
+// a password to decrypt the account, or a PIN code o verify the transaction),
+// an AuthNeededError instance will be returned, containing infos for the user
+// about which fields or actions are needed. The user may retry by providing
+// the needed details via SignDataWithPassphrase, or by other means (e.g. unlock
+// the account in a keystore).
+func (w *Wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
+ return w.signHash(account, crypto.Keccak256(data))
+}
+
+func (w *Wallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
+ w.lock.Lock()
+ defer w.lock.Unlock()
+
+ path, err := w.findAccountPath(account)
+ if err != nil {
+ return nil, err
+ }
+
+ return w.session.sign(path, hash)
+}
+
+// SignTx requests the wallet to sign the given transaction.
+//
+// It looks up the account specified either solely via its address contained within,
+// or optionally with the aid of any location metadata from the embedded URL field.
+//
+// If the wallet requires additional authentication to sign the request (e.g.
+// a password to decrypt the account, or a PIN code o verify the transaction),
+// an AuthNeededError instance will be returned, containing infos for the user
+// about which fields or actions are needed. The user may retry by providing
+// the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
+// the account in a keystore).
+func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ signer := types.NewEIP155Signer(chainID)
+ hash := signer.Hash(tx)
+ sig, err := w.signHash(account, hash[:])
+ if err != nil {
+ return nil, err
+ }
+ return tx.WithSignature(signer, sig)
+}
+
+// SignDataWithPassphrase requests the wallet to sign the given hash with the
+// given passphrase as extra authentication information.
+//
+// It looks up the account specified either solely via its address contained within,
+// or optionally with the aid of any location metadata from the embedded URL field.
+func (w *Wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
+ return w.signHashWithPassphrase(account, passphrase, crypto.Keccak256(data))
+}
+
+func (w *Wallet) signHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error) {
+ if !w.session.verified {
+ if err := w.Open(passphrase); err != nil {
+ return nil, err
+ }
+ }
+
+ return w.signHash(account, hash)
+}
+
+// SignText requests the wallet to sign the hash of a given piece of data, prefixed
+// by the Ethereum prefix scheme
+// It looks up the account specified either solely via its address contained within,
+// or optionally with the aid of any location metadata from the embedded URL field.
+//
+// If the wallet requires additional authentication to sign the request (e.g.
+// a password to decrypt the account, or a PIN code o verify the transaction),
+// an AuthNeededError instance will be returned, containing infos for the user
+// about which fields or actions are needed. The user may retry by providing
+// the needed details via SignHashWithPassphrase, or by other means (e.g. unlock
+// the account in a keystore).
+func (w *Wallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
+ return w.signHash(account, accounts.TextHash(text))
+}
+
+// SignTextWithPassphrase implements accounts.Wallet, attempting to sign the
+// given hash with the given account using passphrase as extra authentication
+func (w *Wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
+ return w.signHashWithPassphrase(account, passphrase, crypto.Keccak256(accounts.TextHash(text)))
+}
+
+// SignTxWithPassphrase requests the wallet to sign the given transaction, with the
+// given passphrase as extra authentication information.
+//
+// It looks up the account specified either solely via its address contained within,
+// or optionally with the aid of any location metadata from the embedded URL field.
+func (w *Wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ if !w.session.verified {
+ if err := w.Open(passphrase); err != nil {
+ return nil, err
+ }
+ }
+ return w.SignTx(account, tx, chainID)
+}
+
+// findAccountPath returns the derivation path for the provided account.
+// It first checks for the address in the list of pinned accounts, and if it is
+// not found, attempts to parse the derivation path from the account's URL.
+func (w *Wallet) findAccountPath(account accounts.Account) (accounts.DerivationPath, error) {
+ pairing := w.Hub.pairing(w)
+ if path, ok := pairing.Accounts[account.Address]; ok {
+ return path, nil
+ }
+
+ // Look for the path in the URL
+ if account.URL.Scheme != w.Hub.scheme {
+ return nil, fmt.Errorf("Scheme %s does not match wallet scheme %s", account.URL.Scheme, w.Hub.scheme)
+ }
+
+ parts := strings.SplitN(account.URL.Path, "/", 2)
+ if len(parts) != 2 {
+ return nil, fmt.Errorf("Invalid URL format: %s", account.URL)
+ }
+
+ if parts[0] != fmt.Sprintf("%x", w.PublicKey[1:3]) {
+ return nil, fmt.Errorf("URL %s is not for this wallet", account.URL)
+ }
+
+ return accounts.ParseDerivationPath(parts[1])
+}
+
+// Session represents a secured communication session with the wallet.
+type Session struct {
+ Wallet *Wallet // A handle to the wallet that opened the session
+ Channel *SecureChannelSession // A secure channel for encrypted messages
+ verified bool // Whether the pin has been verified in this session.
+}
+
+// pair establishes a new pairing over this channel, using the provided secret.
+func (s *Session) pair(secret []byte) (smartcardPairing, error) {
+ err := s.Channel.Pair(secret)
+ if err != nil {
+ return smartcardPairing{}, err
+ }
+
+ return smartcardPairing{
+ PublicKey: s.Wallet.PublicKey,
+ PairingIndex: s.Channel.PairingIndex,
+ PairingKey: s.Channel.PairingKey,
+ Accounts: make(map[common.Address]accounts.DerivationPath),
+ }, nil
+}
+
+// unpair deletes an existing pairing.
+func (s *Session) unpair() error {
+ if !s.verified {
+ return fmt.Errorf("Unpair requires that the PIN be verified")
+ }
+ return s.Channel.Unpair()
+}
+
+// verifyPin unlocks a wallet with the provided pin.
+func (s *Session) verifyPin(pin []byte) error {
+ if _, err := s.Channel.transmitEncrypted(claSCWallet, insVerifyPin, 0, 0, pin); err != nil {
+ return err
+ }
+ s.verified = true
+ return nil
+}
+
+// unblockPin unblocks a wallet with the provided puk and resets the pin to the
+// new one specified.
+func (s *Session) unblockPin(pukpin []byte) error {
+ if _, err := s.Channel.transmitEncrypted(claSCWallet, insUnblockPin, 0, 0, pukpin); err != nil {
+ return err
+ }
+ s.verified = true
+ return nil
+}
+
+// release releases resources associated with the channel.
+func (s *Session) release() error {
+ return s.Wallet.card.Disconnect(pcsc.LeaveCard)
+}
+
+// paired returns true if a valid pairing exists.
+func (s *Session) paired() bool {
+ return s.Channel.PairingKey != nil
+}
+
+// authenticate uses an existing pairing to establish a secure channel.
+func (s *Session) authenticate(pairing smartcardPairing) error {
+ if !bytes.Equal(s.Wallet.PublicKey, pairing.PublicKey) {
+ return fmt.Errorf("Cannot pair using another wallet's pairing; %x != %x", s.Wallet.PublicKey, pairing.PublicKey)
+ }
+ s.Channel.PairingKey = pairing.PairingKey
+ s.Channel.PairingIndex = pairing.PairingIndex
+ return s.Channel.Open()
+}
+
+// walletStatus describes a smartcard wallet's status information.
+type walletStatus struct {
+ PinRetryCount int // Number of remaining PIN retries
+ PukRetryCount int // Number of remaining PUK retries
+ Initialized bool // Whether the card has been initialized with a private key
+}
+
+// walletStatus fetches the wallet's status from the card.
+func (s *Session) walletStatus() (*walletStatus, error) {
+ response, err := s.Channel.transmitEncrypted(claSCWallet, insStatus, statusP1WalletStatus, 0, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ status := new(walletStatus)
+ if _, err := asn1.UnmarshalWithParams(response.Data, status, "tag:3"); err != nil {
+ return nil, err
+ }
+ return status, nil
+}
+
+// derivationPath fetches the wallet's current derivation path from the card.
+func (s *Session) derivationPath() (accounts.DerivationPath, error) {
+ response, err := s.Channel.transmitEncrypted(claSCWallet, insStatus, statusP1Path, 0, nil)
+ if err != nil {
+ return nil, err
+ }
+ buf := bytes.NewReader(response.Data)
+ path := make(accounts.DerivationPath, len(response.Data)/4)
+ return path, binary.Read(buf, binary.BigEndian, &path)
+}
+
+// initializeData contains data needed to initialize the smartcard wallet.
+type initializeData struct {
+ PublicKey []byte `asn1:"tag:0"`
+ PrivateKey []byte `asn1:"tag:1"`
+ ChainCode []byte `asn1:"tag:2"`
+}
+
+// initialize initializes the card with new key data.
+func (s *Session) initialize(seed []byte) error {
+ // Check that the wallet isn't currently initialized,
+ // otherwise the key would be overwritten.
+ status, err := s.Wallet.Status()
+ if err != nil {
+ return err
+ }
+ if status == "Online" {
+ return fmt.Errorf("card is already initialized, cowardly refusing to proceed")
+ }
+
+ s.Wallet.lock.Lock()
+ defer s.Wallet.lock.Unlock()
+
+ // HMAC the seed to produce the private key and chain code
+ mac := hmac.New(sha512.New, []byte("Bitcoin seed"))
+ mac.Write(seed)
+ seed = mac.Sum(nil)
+
+ key, err := crypto.ToECDSA(seed[:32])
+ if err != nil {
+ return err
+ }
+
+ id := initializeData{}
+ id.PublicKey = crypto.FromECDSAPub(&key.PublicKey)
+ id.PrivateKey = seed[:32]
+ id.ChainCode = seed[32:]
+ data, err := asn1.Marshal(id)
+ if err != nil {
+ return err
+ }
+
+ // Nasty hack to force the top-level struct tag to be context-specific
+ data[0] = 0xA1
+
+ _, err = s.Channel.transmitEncrypted(claSCWallet, insLoadKey, 0x02, 0, data)
+ return err
+}
+
+// derive derives a new HD key path on the card.
+func (s *Session) derive(path accounts.DerivationPath) (accounts.Account, error) {
+ startingPoint, path, err := derivationpath.Decode(path.String())
+ if err != nil {
+ return accounts.Account{}, err
+ }
+
+ var p1 uint8
+ switch startingPoint {
+ case derivationpath.StartingPointMaster:
+ p1 = P1DeriveKeyFromMaster
+ case derivationpath.StartingPointParent:
+ p1 = P1DeriveKeyFromParent
+ case derivationpath.StartingPointCurrent:
+ p1 = P1DeriveKeyFromCurrent
+ default:
+ return accounts.Account{}, fmt.Errorf("invalid startingPoint %d", startingPoint)
+ }
+
+ data := new(bytes.Buffer)
+ for _, segment := range path {
+ if err := binary.Write(data, binary.BigEndian, segment); err != nil {
+ return accounts.Account{}, err
+ }
+ }
+
+ _, err = s.Channel.transmitEncrypted(claSCWallet, insDeriveKey, p1, 0, data.Bytes())
+ if err != nil {
+ return accounts.Account{}, err
+ }
+
+ response, err := s.Channel.transmitEncrypted(claSCWallet, insSign, 0, 0, DerivationSignatureHash[:])
+ if err != nil {
+ return accounts.Account{}, err
+ }
+
+ sigdata := new(signatureData)
+ if _, err := asn1.UnmarshalWithParams(response.Data, sigdata, "tag:0"); err != nil {
+ return accounts.Account{}, err
+ }
+ rbytes, sbytes := sigdata.Signature.R.Bytes(), sigdata.Signature.S.Bytes()
+ sig := make([]byte, 65)
+ copy(sig[32-len(rbytes):32], rbytes)
+ copy(sig[64-len(sbytes):64], sbytes)
+
+ if err := confirmPublicKey(sig, sigdata.PublicKey); err != nil {
+ return accounts.Account{}, err
+ }
+ pub, err := crypto.UnmarshalPubkey(sigdata.PublicKey)
+ if err != nil {
+ return accounts.Account{}, err
+ }
+ return s.Wallet.makeAccount(crypto.PubkeyToAddress(*pub), path), nil
+}
+
+// keyExport contains information on an exported keypair.
+type keyExport struct {
+ PublicKey []byte `asn1:"tag:0"`
+ PrivateKey []byte `asn1:"tag:1,optional"`
+}
+
+// publicKey returns the public key for the current derivation path.
+func (s *Session) publicKey() ([]byte, error) {
+ response, err := s.Channel.transmitEncrypted(claSCWallet, insExportKey, exportP1Any, exportP2Pubkey, nil)
+ if err != nil {
+ return nil, err
+ }
+ keys := new(keyExport)
+ if _, err := asn1.UnmarshalWithParams(response.Data, keys, "tag:1"); err != nil {
+ return nil, err
+ }
+ return keys.PublicKey, nil
+}
+
+// signatureData contains information on a signature - the signature itself and
+// the corresponding public key.
+type signatureData struct {
+ PublicKey []byte `asn1:"tag:0"`
+ Signature struct {
+ R *big.Int
+ S *big.Int
+ }
+}
+
+// sign asks the card to sign a message, and returns a valid signature after
+// recovering the v value.
+func (s *Session) sign(path accounts.DerivationPath, hash []byte) ([]byte, error) {
+ startTime := time.Now()
+ _, err := s.derive(path)
+ if err != nil {
+ return nil, err
+ }
+ deriveTime := time.Now()
+
+ response, err := s.Channel.transmitEncrypted(claSCWallet, insSign, signP1PrecomputedHash, signP2OnlyBlock, hash)
+ if err != nil {
+ return nil, err
+ }
+ sigdata := new(signatureData)
+ if _, err := asn1.UnmarshalWithParams(response.Data, sigdata, "tag:0"); err != nil {
+ return nil, err
+ }
+ // Serialize the signature
+ rbytes, sbytes := sigdata.Signature.R.Bytes(), sigdata.Signature.S.Bytes()
+ sig := make([]byte, 65)
+ copy(sig[32-len(rbytes):32], rbytes)
+ copy(sig[64-len(sbytes):64], sbytes)
+
+ // Recover the V value.
+ sig, err = makeRecoverableSignature(hash, sig, sigdata.PublicKey)
+ if err != nil {
+ return nil, err
+ }
+ log.Debug("Signed using smartcard", "deriveTime", deriveTime.Sub(startTime), "signingTime", time.Since(deriveTime))
+
+ return sig, nil
+}
+
+// confirmPublicKey confirms that the given signature belongs to the specified key.
+func confirmPublicKey(sig, pubkey []byte) error {
+ _, err := makeRecoverableSignature(DerivationSignatureHash[:], sig, pubkey)
+ return err
+}
+
+// makeRecoverableSignature uses a signature and an expected public key to
+// recover the v value and produce a recoverable signature.
+func makeRecoverableSignature(hash, sig, expectedPubkey []byte) ([]byte, error) {
+ var libraryError error
+ for v := 0; v < 2; v++ {
+ sig[64] = byte(v)
+ if pubkey, err := crypto.Ecrecover(hash, sig); err == nil {
+ if bytes.Equal(pubkey, expectedPubkey) {
+ return sig, nil
+ }
+ } else {
+ libraryError = err
+ }
+ }
+ if libraryError != nil {
+ return nil, libraryError
+ }
+ return nil, ErrPubkeyMismatch
+}
diff --git a/accounts/sort.go b/accounts/sort.go
new file mode 100644
index 0000000..f467621
--- /dev/null
+++ b/accounts/sort.go
@@ -0,0 +1,31 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package accounts
+
+// AccountsByURL implements sort.Interface for []Account based on the URL field.
+type AccountsByURL []Account
+
+func (a AccountsByURL) Len() int { return len(a) }
+func (a AccountsByURL) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a AccountsByURL) Less(i, j int) bool { return a[i].URL.Cmp(a[j].URL) < 0 }
+
+// WalletsByURL implements sort.Interface for []Wallet based on the URL field.
+type WalletsByURL []Wallet
+
+func (w WalletsByURL) Len() int { return len(w) }
+func (w WalletsByURL) Swap(i, j int) { w[i], w[j] = w[j], w[i] }
+func (w WalletsByURL) Less(i, j int) bool { return w[i].URL().Cmp(w[j].URL()) < 0 }
diff --git a/accounts/url.go b/accounts/url.go
new file mode 100644
index 0000000..a5add10
--- /dev/null
+++ b/accounts/url.go
@@ -0,0 +1,104 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package accounts
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "strings"
+)
+
+// URL represents the canonical identification URL of a wallet or account.
+//
+// It is a simplified version of url.URL, with the important limitations (which
+// are considered features here) that it contains value-copyable components only,
+// as well as that it doesn't do any URL encoding/decoding of special characters.
+//
+// The former is important to allow an account to be copied without leaving live
+// references to the original version, whereas the latter is important to ensure
+// one single canonical form opposed to many allowed ones by the RFC 3986 spec.
+//
+// As such, these URLs should not be used outside of the scope of an Ethereum
+// wallet or account.
+type URL struct {
+ Scheme string // Protocol scheme to identify a capable account backend
+ Path string // Path for the backend to identify a unique entity
+}
+
+// parseURL converts a user supplied URL into the accounts specific structure.
+func parseURL(url string) (URL, error) {
+ parts := strings.Split(url, "://")
+ if len(parts) != 2 || parts[0] == "" {
+ return URL{}, errors.New("protocol scheme missing")
+ }
+ return URL{
+ Scheme: parts[0],
+ Path: parts[1],
+ }, nil
+}
+
+// String implements the stringer interface.
+func (u URL) String() string {
+ if u.Scheme != "" {
+ return fmt.Sprintf("%s://%s", u.Scheme, u.Path)
+ }
+ return u.Path
+}
+
+// TerminalString implements the log.TerminalStringer interface.
+func (u URL) TerminalString() string {
+ url := u.String()
+ if len(url) > 32 {
+ return url[:31] + "…"
+ }
+ return url
+}
+
+// MarshalJSON implements the json.Marshaller interface.
+func (u URL) MarshalJSON() ([]byte, error) {
+ return json.Marshal(u.String())
+}
+
+// UnmarshalJSON parses url.
+func (u *URL) UnmarshalJSON(input []byte) error {
+ var textURL string
+ err := json.Unmarshal(input, &textURL)
+ if err != nil {
+ return err
+ }
+ url, err := parseURL(textURL)
+ if err != nil {
+ return err
+ }
+ u.Scheme = url.Scheme
+ u.Path = url.Path
+ return nil
+}
+
+// Cmp compares x and y and returns:
+//
+// -1 if x < y
+// 0 if x == y
+// +1 if x > y
+//
+func (u URL) Cmp(url URL) int {
+ if u.Scheme == url.Scheme {
+ return strings.Compare(u.Path, url.Path)
+ }
+ return strings.Compare(u.Scheme, url.Scheme)
+}
diff --git a/accounts/usbwallet/hub.go b/accounts/usbwallet/hub.go
new file mode 100644
index 0000000..b851ffc
--- /dev/null
+++ b/accounts/usbwallet/hub.go
@@ -0,0 +1,279 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package usbwallet
+
+import (
+ "errors"
+ "runtime"
+ "sync"
+ "sync/atomic"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/go-ethereum/event"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/karalabe/usb"
+)
+
+// LedgerScheme is the protocol scheme prefixing account and wallet URLs.
+const LedgerScheme = "ledger"
+
+// TrezorScheme is the protocol scheme prefixing account and wallet URLs.
+const TrezorScheme = "trezor"
+
+// refreshCycle is the maximum time between wallet refreshes (if USB hotplug
+// notifications don't work).
+const refreshCycle = time.Second
+
+// refreshThrottling is the minimum time between wallet refreshes to avoid USB
+// trashing.
+const refreshThrottling = 500 * time.Millisecond
+
+// Hub is a accounts.Backend that can find and handle generic USB hardware wallets.
+type Hub struct {
+ scheme string // Protocol scheme prefixing account and wallet URLs.
+ vendorID uint16 // USB vendor identifier used for device discovery
+ productIDs []uint16 // USB product identifiers used for device discovery
+ usageID uint16 // USB usage page identifier used for macOS device discovery
+ endpointID int // USB endpoint identifier used for non-macOS device discovery
+ makeDriver func(log.Logger) driver // Factory method to construct a vendor specific driver
+
+ refreshed time.Time // Time instance when the list of wallets was last refreshed
+ wallets []accounts.Wallet // List of USB wallet devices currently tracking
+ updateFeed event.Feed // Event feed to notify wallet additions/removals
+ updateScope event.SubscriptionScope // Subscription scope tracking current live listeners
+ updating bool // Whether the event notification loop is running
+
+ quit chan chan error
+
+ stateLock sync.RWMutex // Protects the internals of the hub from racey access
+
+ // TODO(karalabe): remove if hotplug lands on Windows
+ commsPend int // Number of operations blocking enumeration
+ commsLock sync.Mutex // Lock protecting the pending counter and enumeration
+ enumFails uint32 // Number of times enumeration has failed
+}
+
+// NewLedgerHub creates a new hardware wallet manager for Ledger devices.
+func NewLedgerHub() (*Hub, error) {
+ return newHub(LedgerScheme, 0x2c97, []uint16{
+ // Original product IDs
+ 0x0000, /* Ledger Blue */
+ 0x0001, /* Ledger Nano S */
+ 0x0004, /* Ledger Nano X */
+
+ // Upcoming product IDs: https://www.ledger.com/2019/05/17/windows-10-update-sunsetting-u2f-tunnel-transport-for-ledger-devices/
+ 0x0015, /* HID + U2F + WebUSB Ledger Blue */
+ 0x1015, /* HID + U2F + WebUSB Ledger Nano S */
+ 0x4015, /* HID + U2F + WebUSB Ledger Nano X */
+ 0x0011, /* HID + WebUSB Ledger Blue */
+ 0x1011, /* HID + WebUSB Ledger Nano S */
+ 0x4011, /* HID + WebUSB Ledger Nano X */
+ }, 0xffa0, 0, newLedgerDriver)
+}
+
+// NewTrezorHubWithHID creates a new hardware wallet manager for Trezor devices.
+func NewTrezorHubWithHID() (*Hub, error) {
+ return newHub(TrezorScheme, 0x534c, []uint16{0x0001 /* Trezor HID */}, 0xff00, 0, newTrezorDriver)
+}
+
+// NewTrezorHubWithWebUSB creates a new hardware wallet manager for Trezor devices with
+// firmware version > 1.8.0
+func NewTrezorHubWithWebUSB() (*Hub, error) {
+ return newHub(TrezorScheme, 0x1209, []uint16{0x53c1 /* Trezor WebUSB */}, 0xffff /* No usage id on webusb, don't match unset (0) */, 0, newTrezorDriver)
+}
+
+// newHub creates a new hardware wallet manager for generic USB devices.
+func newHub(scheme string, vendorID uint16, productIDs []uint16, usageID uint16, endpointID int, makeDriver func(log.Logger) driver) (*Hub, error) {
+ if !usb.Supported() {
+ return nil, errors.New("unsupported platform")
+ }
+ hub := &Hub{
+ scheme: scheme,
+ vendorID: vendorID,
+ productIDs: productIDs,
+ usageID: usageID,
+ endpointID: endpointID,
+ makeDriver: makeDriver,
+ quit: make(chan chan error),
+ }
+ hub.refreshWallets()
+ return hub, nil
+}
+
+// Wallets implements accounts.Backend, returning all the currently tracked USB
+// devices that appear to be hardware wallets.
+func (hub *Hub) Wallets() []accounts.Wallet {
+ // Make sure the list of wallets is up to date
+ hub.refreshWallets()
+
+ hub.stateLock.RLock()
+ defer hub.stateLock.RUnlock()
+
+ cpy := make([]accounts.Wallet, len(hub.wallets))
+ copy(cpy, hub.wallets)
+ return cpy
+}
+
+// refreshWallets scans the USB devices attached to the machine and updates the
+// list of wallets based on the found devices.
+func (hub *Hub) refreshWallets() {
+ // Don't scan the USB like crazy it the user fetches wallets in a loop
+ hub.stateLock.RLock()
+ elapsed := time.Since(hub.refreshed)
+ hub.stateLock.RUnlock()
+
+ if elapsed < refreshThrottling {
+ return
+ }
+ // If USB enumeration is continually failing, don't keep trying indefinitely
+ if atomic.LoadUint32(&hub.enumFails) > 2 {
+ return
+ }
+ // Retrieve the current list of USB wallet devices
+ var devices []usb.DeviceInfo
+
+ if runtime.GOOS == "linux" {
+ // hidapi on Linux opens the device during enumeration to retrieve some infos,
+ // breaking the Ledger protocol if that is waiting for user confirmation. This
+ // is a bug acknowledged at Ledger, but it won't be fixed on old devices so we
+ // need to prevent concurrent comms ourselves. The more elegant solution would
+ // be to ditch enumeration in favor of hotplug events, but that don't work yet
+ // on Windows so if we need to hack it anyway, this is more elegant for now.
+ hub.commsLock.Lock()
+ if hub.commsPend > 0 { // A confirmation is pending, don't refresh
+ hub.commsLock.Unlock()
+ return
+ }
+ }
+ infos, err := usb.Enumerate(hub.vendorID, 0)
+ if err != nil {
+ failcount := atomic.AddUint32(&hub.enumFails, 1)
+ if runtime.GOOS == "linux" {
+ // See rationale before the enumeration why this is needed and only on Linux.
+ hub.commsLock.Unlock()
+ }
+ log.Error("Failed to enumerate USB devices", "hub", hub.scheme,
+ "vendor", hub.vendorID, "failcount", failcount, "err", err)
+ return
+ }
+ atomic.StoreUint32(&hub.enumFails, 0)
+
+ for _, info := range infos {
+ for _, id := range hub.productIDs {
+ // Windows and Macos use UsageID matching, Linux uses Interface matching
+ if info.ProductID == id && (info.UsagePage == hub.usageID || info.Interface == hub.endpointID) {
+ devices = append(devices, info)
+ break
+ }
+ }
+ }
+ if runtime.GOOS == "linux" {
+ // See rationale before the enumeration why this is needed and only on Linux.
+ hub.commsLock.Unlock()
+ }
+ // Transform the current list of wallets into the new one
+ hub.stateLock.Lock()
+
+ var (
+ wallets = make([]accounts.Wallet, 0, len(devices))
+ events []accounts.WalletEvent
+ )
+
+ for _, device := range devices {
+ url := accounts.URL{Scheme: hub.scheme, Path: device.Path}
+
+ // Drop wallets in front of the next device or those that failed for some reason
+ for len(hub.wallets) > 0 {
+ // Abort if we're past the current device and found an operational one
+ _, failure := hub.wallets[0].Status()
+ if hub.wallets[0].URL().Cmp(url) >= 0 || failure == nil {
+ break
+ }
+ // Drop the stale and failed devices
+ events = append(events, accounts.WalletEvent{Wallet: hub.wallets[0], Kind: accounts.WalletDropped})
+ hub.wallets = hub.wallets[1:]
+ }
+ // If there are no more wallets or the device is before the next, wrap new wallet
+ if len(hub.wallets) == 0 || hub.wallets[0].URL().Cmp(url) > 0 {
+ logger := log.New("url", url)
+ wallet := &wallet{hub: hub, driver: hub.makeDriver(logger), url: &url, info: device, log: logger}
+
+ events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
+ wallets = append(wallets, wallet)
+ continue
+ }
+ // If the device is the same as the first wallet, keep it
+ if hub.wallets[0].URL().Cmp(url) == 0 {
+ wallets = append(wallets, hub.wallets[0])
+ hub.wallets = hub.wallets[1:]
+ continue
+ }
+ }
+ // Drop any leftover wallets and set the new batch
+ for _, wallet := range hub.wallets {
+ events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped})
+ }
+ hub.refreshed = time.Now()
+ hub.wallets = wallets
+ hub.stateLock.Unlock()
+
+ // Fire all wallet events and return
+ for _, event := range events {
+ hub.updateFeed.Send(event)
+ }
+}
+
+// Subscribe implements accounts.Backend, creating an async subscription to
+// receive notifications on the addition or removal of USB wallets.
+func (hub *Hub) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription {
+ // We need the mutex to reliably start/stop the update loop
+ hub.stateLock.Lock()
+ defer hub.stateLock.Unlock()
+
+ // Subscribe the caller and track the subscriber count
+ sub := hub.updateScope.Track(hub.updateFeed.Subscribe(sink))
+
+ // Subscribers require an active notification loop, start it
+ if !hub.updating {
+ hub.updating = true
+ go hub.updater()
+ }
+ return sub
+}
+
+// updater is responsible for maintaining an up-to-date list of wallets managed
+// by the USB hub, and for firing wallet addition/removal events.
+func (hub *Hub) updater() {
+ for {
+ // TODO: Wait for a USB hotplug event (not supported yet) or a refresh timeout
+ // <-hub.changes
+ time.Sleep(refreshCycle)
+
+ // Run the wallet refresher
+ hub.refreshWallets()
+
+ // If all our subscribers left, stop the updater
+ hub.stateLock.Lock()
+ if hub.updateScope.Count() == 0 {
+ hub.updating = false
+ hub.stateLock.Unlock()
+ return
+ }
+ hub.stateLock.Unlock()
+ }
+}
diff --git a/accounts/usbwallet/ledger.go b/accounts/usbwallet/ledger.go
new file mode 100644
index 0000000..528e8da
--- /dev/null
+++ b/accounts/usbwallet/ledger.go
@@ -0,0 +1,465 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// This file contains the implementation for interacting with the Ledger hardware
+// wallets. The wire protocol spec can be found in the Ledger Blue GitHub repo:
+// https://raw.githubusercontent.com/LedgerHQ/blue-app-eth/master/doc/ethapp.asc
+
+package usbwallet
+
+import (
+ "encoding/binary"
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "io"
+ "math/big"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+// ledgerOpcode is an enumeration encoding the supported Ledger opcodes.
+type ledgerOpcode byte
+
+// ledgerParam1 is an enumeration encoding the supported Ledger parameters for
+// specific opcodes. The same parameter values may be reused between opcodes.
+type ledgerParam1 byte
+
+// ledgerParam2 is an enumeration encoding the supported Ledger parameters for
+// specific opcodes. The same parameter values may be reused between opcodes.
+type ledgerParam2 byte
+
+const (
+ ledgerOpRetrieveAddress ledgerOpcode = 0x02 // Returns the public key and Ethereum address for a given BIP 32 path
+ ledgerOpSignTransaction ledgerOpcode = 0x04 // Signs an Ethereum transaction after having the user validate the parameters
+ ledgerOpGetConfiguration ledgerOpcode = 0x06 // Returns specific wallet application configuration
+
+ ledgerP1DirectlyFetchAddress ledgerParam1 = 0x00 // Return address directly from the wallet
+ ledgerP1InitTransactionData ledgerParam1 = 0x00 // First transaction data block for signing
+ ledgerP1ContTransactionData ledgerParam1 = 0x80 // Subsequent transaction data block for signing
+ ledgerP2DiscardAddressChainCode ledgerParam2 = 0x00 // Do not return the chain code along with the address
+)
+
+// errLedgerReplyInvalidHeader is the error message returned by a Ledger data exchange
+// if the device replies with a mismatching header. This usually means the device
+// is in browser mode.
+var errLedgerReplyInvalidHeader = errors.New("ledger: invalid reply header")
+
+// errLedgerInvalidVersionReply is the error message returned by a Ledger version retrieval
+// when a response does arrive, but it does not contain the expected data.
+var errLedgerInvalidVersionReply = errors.New("ledger: invalid version reply")
+
+// ledgerDriver implements the communication with a Ledger hardware wallet.
+type ledgerDriver struct {
+ device io.ReadWriter // USB device connection to communicate through
+ version [3]byte // Current version of the Ledger firmware (zero if app is offline)
+ browser bool // Flag whether the Ledger is in browser mode (reply channel mismatch)
+ failure error // Any failure that would make the device unusable
+ log log.Logger // Contextual logger to tag the ledger with its id
+}
+
+// newLedgerDriver creates a new instance of a Ledger USB protocol driver.
+func newLedgerDriver(logger log.Logger) driver {
+ return &ledgerDriver{
+ log: logger,
+ }
+}
+
+// Status implements usbwallet.driver, returning various states the Ledger can
+// currently be in.
+func (w *ledgerDriver) Status() (string, error) {
+ if w.failure != nil {
+ return fmt.Sprintf("Failed: %v", w.failure), w.failure
+ }
+ if w.browser {
+ return "Ethereum app in browser mode", w.failure
+ }
+ if w.offline() {
+ return "Ethereum app offline", w.failure
+ }
+ return fmt.Sprintf("Ethereum app v%d.%d.%d online", w.version[0], w.version[1], w.version[2]), w.failure
+}
+
+// offline returns whether the wallet and the Ethereum app is offline or not.
+//
+// The method assumes that the state lock is held!
+func (w *ledgerDriver) offline() bool {
+ return w.version == [3]byte{0, 0, 0}
+}
+
+// Open implements usbwallet.driver, attempting to initialize the connection to the
+// Ledger hardware wallet. The Ledger does not require a user passphrase, so that
+// parameter is silently discarded.
+func (w *ledgerDriver) Open(device io.ReadWriter, passphrase string) error {
+ w.device, w.failure = device, nil
+
+ _, err := w.ledgerDerive(accounts.DefaultBaseDerivationPath)
+ if err != nil {
+ // Ethereum app is not running or in browser mode, nothing more to do, return
+ if err == errLedgerReplyInvalidHeader {
+ w.browser = true
+ }
+ return nil
+ }
+ // Try to resolve the Ethereum app's version, will fail prior to v1.0.2
+ if w.version, err = w.ledgerVersion(); err != nil {
+ w.version = [3]byte{1, 0, 0} // Assume worst case, can't verify if v1.0.0 or v1.0.1
+ }
+ return nil
+}
+
+// Close implements usbwallet.driver, cleaning up and metadata maintained within
+// the Ledger driver.
+func (w *ledgerDriver) Close() error {
+ w.browser, w.version = false, [3]byte{}
+ return nil
+}
+
+// Heartbeat implements usbwallet.driver, performing a sanity check against the
+// Ledger to see if it's still online.
+func (w *ledgerDriver) Heartbeat() error {
+ if _, err := w.ledgerVersion(); err != nil && err != errLedgerInvalidVersionReply {
+ w.failure = err
+ return err
+ }
+ return nil
+}
+
+// Derive implements usbwallet.driver, sending a derivation request to the Ledger
+// and returning the Ethereum address located on that derivation path.
+func (w *ledgerDriver) Derive(path accounts.DerivationPath) (common.Address, error) {
+ return w.ledgerDerive(path)
+}
+
+// SignTx implements usbwallet.driver, sending the transaction to the Ledger and
+// waiting for the user to confirm or deny the transaction.
+//
+// Note, if the version of the Ethereum application running on the Ledger wallet is
+// too old to sign EIP-155 transactions, but such is requested nonetheless, an error
+// will be returned opposed to silently signing in Homestead mode.
+func (w *ledgerDriver) SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) {
+ // If the Ethereum app doesn't run, abort
+ if w.offline() {
+ return common.Address{}, nil, accounts.ErrWalletClosed
+ }
+ // Ensure the wallet is capable of signing the given transaction
+ if chainID != nil && w.version[0] <= 1 && w.version[1] <= 0 && w.version[2] <= 2 {
+ return common.Address{}, nil, fmt.Errorf("Ledger v%d.%d.%d doesn't support signing this transaction, please update to v1.0.3 at least", w.version[0], w.version[1], w.version[2])
+ }
+ // All infos gathered and metadata checks out, request signing
+ return w.ledgerSign(path, tx, chainID)
+}
+
+// ledgerVersion retrieves the current version of the Ethereum wallet app running
+// on the Ledger wallet.
+//
+// The version retrieval protocol is defined as follows:
+//
+// CLA | INS | P1 | P2 | Lc | Le
+// ----+-----+----+----+----+---
+// E0 | 06 | 00 | 00 | 00 | 04
+//
+// With no input data, and the output data being:
+//
+// Description | Length
+// ---------------------------------------------------+--------
+// Flags 01: arbitrary data signature enabled by user | 1 byte
+// Application major version | 1 byte
+// Application minor version | 1 byte
+// Application patch version | 1 byte
+func (w *ledgerDriver) ledgerVersion() ([3]byte, error) {
+ // Send the request and wait for the response
+ reply, err := w.ledgerExchange(ledgerOpGetConfiguration, 0, 0, nil)
+ if err != nil {
+ return [3]byte{}, err
+ }
+ if len(reply) != 4 {
+ return [3]byte{}, errLedgerInvalidVersionReply
+ }
+ // Cache the version for future reference
+ var version [3]byte
+ copy(version[:], reply[1:])
+ return version, nil
+}
+
+// ledgerDerive retrieves the currently active Ethereum address from a Ledger
+// wallet at the specified derivation path.
+//
+// The address derivation protocol is defined as follows:
+//
+// CLA | INS | P1 | P2 | Lc | Le
+// ----+-----+----+----+-----+---
+// E0 | 02 | 00 return address
+// 01 display address and confirm before returning
+// | 00: do not return the chain code
+// | 01: return the chain code
+// | var | 00
+//
+// Where the input data is:
+//
+// Description | Length
+// -------------------------------------------------+--------
+// Number of BIP 32 derivations to perform (max 10) | 1 byte
+// First derivation index (big endian) | 4 bytes
+// ... | 4 bytes
+// Last derivation index (big endian) | 4 bytes
+//
+// And the output data is:
+//
+// Description | Length
+// ------------------------+-------------------
+// Public Key length | 1 byte
+// Uncompressed Public Key | arbitrary
+// Ethereum address length | 1 byte
+// Ethereum address | 40 bytes hex ascii
+// Chain code if requested | 32 bytes
+func (w *ledgerDriver) ledgerDerive(derivationPath []uint32) (common.Address, error) {
+ // Flatten the derivation path into the Ledger request
+ path := make([]byte, 1+4*len(derivationPath))
+ path[0] = byte(len(derivationPath))
+ for i, component := range derivationPath {
+ binary.BigEndian.PutUint32(path[1+4*i:], component)
+ }
+ // Send the request and wait for the response
+ reply, err := w.ledgerExchange(ledgerOpRetrieveAddress, ledgerP1DirectlyFetchAddress, ledgerP2DiscardAddressChainCode, path)
+ if err != nil {
+ return common.Address{}, err
+ }
+ // Discard the public key, we don't need that for now
+ if len(reply) < 1 || len(reply) < 1+int(reply[0]) {
+ return common.Address{}, errors.New("reply lacks public key entry")
+ }
+ reply = reply[1+int(reply[0]):]
+
+ // Extract the Ethereum hex address string
+ if len(reply) < 1 || len(reply) < 1+int(reply[0]) {
+ return common.Address{}, errors.New("reply lacks address entry")
+ }
+ hexstr := reply[1 : 1+int(reply[0])]
+
+ // Decode the hex sting into an Ethereum address and return
+ var address common.Address
+ if _, err = hex.Decode(address[:], hexstr); err != nil {
+ return common.Address{}, err
+ }
+ return address, nil
+}
+
+// ledgerSign sends the transaction to the Ledger wallet, and waits for the user
+// to confirm or deny the transaction.
+//
+// The transaction signing protocol is defined as follows:
+//
+// CLA | INS | P1 | P2 | Lc | Le
+// ----+-----+----+----+-----+---
+// E0 | 04 | 00: first transaction data block
+// 80: subsequent transaction data block
+// | 00 | variable | variable
+//
+// Where the input for the first transaction block (first 255 bytes) is:
+//
+// Description | Length
+// -------------------------------------------------+----------
+// Number of BIP 32 derivations to perform (max 10) | 1 byte
+// First derivation index (big endian) | 4 bytes
+// ... | 4 bytes
+// Last derivation index (big endian) | 4 bytes
+// RLP transaction chunk | arbitrary
+//
+// And the input for subsequent transaction blocks (first 255 bytes) are:
+//
+// Description | Length
+// ----------------------+----------
+// RLP transaction chunk | arbitrary
+//
+// And the output data is:
+//
+// Description | Length
+// ------------+---------
+// signature V | 1 byte
+// signature R | 32 bytes
+// signature S | 32 bytes
+func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) {
+ // Flatten the derivation path into the Ledger request
+ path := make([]byte, 1+4*len(derivationPath))
+ path[0] = byte(len(derivationPath))
+ for i, component := range derivationPath {
+ binary.BigEndian.PutUint32(path[1+4*i:], component)
+ }
+ // Create the transaction RLP based on whether legacy or EIP155 signing was requested
+ var (
+ txrlp []byte
+ err error
+ )
+ if chainID == nil {
+ if txrlp, err = rlp.EncodeToBytes([]interface{}{tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data()}); err != nil {
+ return common.Address{}, nil, err
+ }
+ } else {
+ if txrlp, err = rlp.EncodeToBytes([]interface{}{tx.Nonce(), tx.GasPrice(), tx.Gas(), tx.To(), tx.Value(), tx.Data(), chainID, big.NewInt(0), big.NewInt(0)}); err != nil {
+ return common.Address{}, nil, err
+ }
+ }
+ payload := append(path, txrlp...)
+
+ // Send the request and wait for the response
+ var (
+ op = ledgerP1InitTransactionData
+ reply []byte
+ )
+ for len(payload) > 0 {
+ // Calculate the size of the next data chunk
+ chunk := 255
+ if chunk > len(payload) {
+ chunk = len(payload)
+ }
+ // Send the chunk over, ensuring it's processed correctly
+ reply, err = w.ledgerExchange(ledgerOpSignTransaction, op, 0, payload[:chunk])
+ if err != nil {
+ return common.Address{}, nil, err
+ }
+ // Shift the payload and ensure subsequent chunks are marked as such
+ payload = payload[chunk:]
+ op = ledgerP1ContTransactionData
+ }
+ // Extract the Ethereum signature and do a sanity validation
+ if len(reply) != crypto.SignatureLength {
+ return common.Address{}, nil, errors.New("reply lacks signature")
+ }
+ signature := append(reply[1:], reply[0])
+
+ // Create the correct signer and signature transform based on the chain ID
+ var signer types.Signer
+ if chainID == nil {
+ signer = new(types.HomesteadSigner)
+ } else {
+ signer = types.NewEIP155Signer(chainID)
+ signature[64] -= byte(chainID.Uint64()*2 + 35)
+ }
+ signed, err := tx.WithSignature(signer, signature)
+ if err != nil {
+ return common.Address{}, nil, err
+ }
+ sender, err := types.Sender(signer, signed)
+ if err != nil {
+ return common.Address{}, nil, err
+ }
+ return sender, signed, nil
+}
+
+// ledgerExchange performs a data exchange with the Ledger wallet, sending it a
+// message and retrieving the response.
+//
+// The common transport header is defined as follows:
+//
+// Description | Length
+// --------------------------------------+----------
+// Communication channel ID (big endian) | 2 bytes
+// Command tag | 1 byte
+// Packet sequence index (big endian) | 2 bytes
+// Payload | arbitrary
+//
+// The Communication channel ID allows commands multiplexing over the same
+// physical link. It is not used for the time being, and should be set to 0101
+// to avoid compatibility issues with implementations ignoring a leading 00 byte.
+//
+// The Command tag describes the message content. Use TAG_APDU (0x05) for standard
+// APDU payloads, or TAG_PING (0x02) for a simple link test.
+//
+// The Packet sequence index describes the current sequence for fragmented payloads.
+// The first fragment index is 0x00.
+//
+// APDU Command payloads are encoded as follows:
+//
+// Description | Length
+// -----------------------------------
+// APDU length (big endian) | 2 bytes
+// APDU CLA | 1 byte
+// APDU INS | 1 byte
+// APDU P1 | 1 byte
+// APDU P2 | 1 byte
+// APDU length | 1 byte
+// Optional APDU data | arbitrary
+func (w *ledgerDriver) ledgerExchange(opcode ledgerOpcode, p1 ledgerParam1, p2 ledgerParam2, data []byte) ([]byte, error) {
+ // Construct the message payload, possibly split into multiple chunks
+ apdu := make([]byte, 2, 7+len(data))
+
+ binary.BigEndian.PutUint16(apdu, uint16(5+len(data)))
+ apdu = append(apdu, []byte{0xe0, byte(opcode), byte(p1), byte(p2), byte(len(data))}...)
+ apdu = append(apdu, data...)
+
+ // Stream all the chunks to the device
+ header := []byte{0x01, 0x01, 0x05, 0x00, 0x00} // Channel ID and command tag appended
+ chunk := make([]byte, 64)
+ space := len(chunk) - len(header)
+
+ for i := 0; len(apdu) > 0; i++ {
+ // Construct the new message to stream
+ chunk = append(chunk[:0], header...)
+ binary.BigEndian.PutUint16(chunk[3:], uint16(i))
+
+ if len(apdu) > space {
+ chunk = append(chunk, apdu[:space]...)
+ apdu = apdu[space:]
+ } else {
+ chunk = append(chunk, apdu...)
+ apdu = nil
+ }
+ // Send over to the device
+ w.log.Trace("Data chunk sent to the Ledger", "chunk", hexutil.Bytes(chunk))
+ if _, err := w.device.Write(chunk); err != nil {
+ return nil, err
+ }
+ }
+ // Stream the reply back from the wallet in 64 byte chunks
+ var reply []byte
+ chunk = chunk[:64] // Yeah, we surely have enough space
+ for {
+ // Read the next chunk from the Ledger wallet
+ if _, err := io.ReadFull(w.device, chunk); err != nil {
+ return nil, err
+ }
+ w.log.Trace("Data chunk received from the Ledger", "chunk", hexutil.Bytes(chunk))
+
+ // Make sure the transport header matches
+ if chunk[0] != 0x01 || chunk[1] != 0x01 || chunk[2] != 0x05 {
+ return nil, errLedgerReplyInvalidHeader
+ }
+ // If it's the first chunk, retrieve the total message length
+ var payload []byte
+
+ if chunk[3] == 0x00 && chunk[4] == 0x00 {
+ reply = make([]byte, 0, int(binary.BigEndian.Uint16(chunk[5:7])))
+ payload = chunk[7:]
+ } else {
+ payload = chunk[5:]
+ }
+ // Append to the reply and stop when filled up
+ if left := cap(reply) - len(reply); left > len(payload) {
+ reply = append(reply, payload...)
+ } else {
+ reply = append(reply, payload[:left]...)
+ break
+ }
+ }
+ return reply[:len(reply)-2], nil
+}
diff --git a/accounts/usbwallet/trezor.go b/accounts/usbwallet/trezor.go
new file mode 100644
index 0000000..833c7a9
--- /dev/null
+++ b/accounts/usbwallet/trezor.go
@@ -0,0 +1,365 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// This file contains the implementation for interacting with the Trezor hardware
+// wallets. The wire protocol spec can be found on the SatoshiLabs website:
+// https://doc.satoshilabs.com/trezor-tech/api-protobuf.html
+
+package usbwallet
+
+import (
+ "encoding/binary"
+ "errors"
+ "fmt"
+ "io"
+ "math/big"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/accounts/usbwallet/trezor"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/golang/protobuf/proto"
+)
+
+// ErrTrezorPINNeeded is returned if opening the trezor requires a PIN code. In
+// this case, the calling application should display a pinpad and send back the
+// encoded passphrase.
+var ErrTrezorPINNeeded = errors.New("trezor: pin needed")
+
+// ErrTrezorPassphraseNeeded is returned if opening the trezor requires a passphrase
+var ErrTrezorPassphraseNeeded = errors.New("trezor: passphrase needed")
+
+// errTrezorReplyInvalidHeader is the error message returned by a Trezor data exchange
+// if the device replies with a mismatching header. This usually means the device
+// is in browser mode.
+var errTrezorReplyInvalidHeader = errors.New("trezor: invalid reply header")
+
+// trezorDriver implements the communication with a Trezor hardware wallet.
+type trezorDriver struct {
+ device io.ReadWriter // USB device connection to communicate through
+ version [3]uint32 // Current version of the Trezor firmware
+ label string // Current textual label of the Trezor device
+ pinwait bool // Flags whether the device is waiting for PIN entry
+ passphrasewait bool // Flags whether the device is waiting for passphrase entry
+ failure error // Any failure that would make the device unusable
+ log log.Logger // Contextual logger to tag the trezor with its id
+}
+
+// newTrezorDriver creates a new instance of a Trezor USB protocol driver.
+func newTrezorDriver(logger log.Logger) driver {
+ return &trezorDriver{
+ log: logger,
+ }
+}
+
+// Status implements accounts.Wallet, always whether the Trezor is opened, closed
+// or whether the Ethereum app was not started on it.
+func (w *trezorDriver) Status() (string, error) {
+ if w.failure != nil {
+ return fmt.Sprintf("Failed: %v", w.failure), w.failure
+ }
+ if w.device == nil {
+ return "Closed", w.failure
+ }
+ if w.pinwait {
+ return fmt.Sprintf("Trezor v%d.%d.%d '%s' waiting for PIN", w.version[0], w.version[1], w.version[2], w.label), w.failure
+ }
+ return fmt.Sprintf("Trezor v%d.%d.%d '%s' online", w.version[0], w.version[1], w.version[2], w.label), w.failure
+}
+
+// Open implements usbwallet.driver, attempting to initialize the connection to
+// the Trezor hardware wallet. Initializing the Trezor is a two or three phase operation:
+// * The first phase is to initialize the connection and read the wallet's
+// features. This phase is invoked if the provided passphrase is empty. The
+// device will display the pinpad as a result and will return an appropriate
+// error to notify the user that a second open phase is needed.
+// * The second phase is to unlock access to the Trezor, which is done by the
+// user actually providing a passphrase mapping a keyboard keypad to the pin
+// number of the user (shuffled according to the pinpad displayed).
+// * If needed the device will ask for passphrase which will require calling
+// open again with the actual passphrase (3rd phase)
+func (w *trezorDriver) Open(device io.ReadWriter, passphrase string) error {
+ w.device, w.failure = device, nil
+
+ // If phase 1 is requested, init the connection and wait for user callback
+ if passphrase == "" && !w.passphrasewait {
+ // If we're already waiting for a PIN entry, insta-return
+ if w.pinwait {
+ return ErrTrezorPINNeeded
+ }
+ // Initialize a connection to the device
+ features := new(trezor.Features)
+ if _, err := w.trezorExchange(&trezor.Initialize{}, features); err != nil {
+ return err
+ }
+ w.version = [3]uint32{features.GetMajorVersion(), features.GetMinorVersion(), features.GetPatchVersion()}
+ w.label = features.GetLabel()
+
+ // Do a manual ping, forcing the device to ask for its PIN and Passphrase
+ askPin := true
+ askPassphrase := true
+ res, err := w.trezorExchange(&trezor.Ping{PinProtection: &askPin, PassphraseProtection: &askPassphrase}, new(trezor.PinMatrixRequest), new(trezor.PassphraseRequest), new(trezor.Success))
+ if err != nil {
+ return err
+ }
+ // Only return the PIN request if the device wasn't unlocked until now
+ switch res {
+ case 0:
+ w.pinwait = true
+ return ErrTrezorPINNeeded
+ case 1:
+ w.pinwait = false
+ w.passphrasewait = true
+ return ErrTrezorPassphraseNeeded
+ case 2:
+ return nil // responded with trezor.Success
+ }
+ }
+ // Phase 2 requested with actual PIN entry
+ if w.pinwait {
+ w.pinwait = false
+ res, err := w.trezorExchange(&trezor.PinMatrixAck{Pin: &passphrase}, new(trezor.Success), new(trezor.PassphraseRequest))
+ if err != nil {
+ w.failure = err
+ return err
+ }
+ if res == 1 {
+ w.passphrasewait = true
+ return ErrTrezorPassphraseNeeded
+ }
+ } else if w.passphrasewait {
+ w.passphrasewait = false
+ if _, err := w.trezorExchange(&trezor.PassphraseAck{Passphrase: &passphrase}, new(trezor.Success)); err != nil {
+ w.failure = err
+ return err
+ }
+ }
+
+ return nil
+}
+
+// Close implements usbwallet.driver, cleaning up and metadata maintained within
+// the Trezor driver.
+func (w *trezorDriver) Close() error {
+ w.version, w.label, w.pinwait = [3]uint32{}, "", false
+ return nil
+}
+
+// Heartbeat implements usbwallet.driver, performing a sanity check against the
+// Trezor to see if it's still online.
+func (w *trezorDriver) Heartbeat() error {
+ if _, err := w.trezorExchange(&trezor.Ping{}, new(trezor.Success)); err != nil {
+ w.failure = err
+ return err
+ }
+ return nil
+}
+
+// Derive implements usbwallet.driver, sending a derivation request to the Trezor
+// and returning the Ethereum address located on that derivation path.
+func (w *trezorDriver) Derive(path accounts.DerivationPath) (common.Address, error) {
+ return w.trezorDerive(path)
+}
+
+// SignTx implements usbwallet.driver, sending the transaction to the Trezor and
+// waiting for the user to confirm or deny the transaction.
+func (w *trezorDriver) SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) {
+ if w.device == nil {
+ return common.Address{}, nil, accounts.ErrWalletClosed
+ }
+ return w.trezorSign(path, tx, chainID)
+}
+
+// trezorDerive sends a derivation request to the Trezor device and returns the
+// Ethereum address located on that path.
+func (w *trezorDriver) trezorDerive(derivationPath []uint32) (common.Address, error) {
+ address := new(trezor.EthereumAddress)
+ if _, err := w.trezorExchange(&trezor.EthereumGetAddress{AddressN: derivationPath}, address); err != nil {
+ return common.Address{}, err
+ }
+ if addr := address.GetAddressBin(); len(addr) > 0 { // Older firmwares use binary fomats
+ return common.BytesToAddress(addr), nil
+ }
+ if addr := address.GetAddressHex(); len(addr) > 0 { // Newer firmwares use hexadecimal fomats
+ return common.HexToAddress(addr), nil
+ }
+ return common.Address{}, errors.New("missing derived address")
+}
+
+// trezorSign sends the transaction to the Trezor wallet, and waits for the user
+// to confirm or deny the transaction.
+func (w *trezorDriver) trezorSign(derivationPath []uint32, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) {
+ // Create the transaction initiation message
+ data := tx.Data()
+ length := uint32(len(data))
+
+ request := &trezor.EthereumSignTx{
+ AddressN: derivationPath,
+ Nonce: new(big.Int).SetUint64(tx.Nonce()).Bytes(),
+ GasPrice: tx.GasPrice().Bytes(),
+ GasLimit: new(big.Int).SetUint64(tx.Gas()).Bytes(),
+ Value: tx.Value().Bytes(),
+ DataLength: &length,
+ }
+ if to := tx.To(); to != nil {
+ // Non contract deploy, set recipient explicitly
+ hex := to.Hex()
+ request.ToHex = &hex // Newer firmwares (old will ignore)
+ request.ToBin = (*to)[:] // Older firmwares (new will ignore)
+ }
+ if length > 1024 { // Send the data chunked if that was requested
+ request.DataInitialChunk, data = data[:1024], data[1024:]
+ } else {
+ request.DataInitialChunk, data = data, nil
+ }
+ if chainID != nil { // EIP-155 transaction, set chain ID explicitly (only 32 bit is supported!?)
+ id := uint32(chainID.Int64())
+ request.ChainId = &id
+ }
+ // Send the initiation message and stream content until a signature is returned
+ response := new(trezor.EthereumTxRequest)
+ if _, err := w.trezorExchange(request, response); err != nil {
+ return common.Address{}, nil, err
+ }
+ for response.DataLength != nil && int(*response.DataLength) <= len(data) {
+ chunk := data[:*response.DataLength]
+ data = data[*response.DataLength:]
+
+ if _, err := w.trezorExchange(&trezor.EthereumTxAck{DataChunk: chunk}, response); err != nil {
+ return common.Address{}, nil, err
+ }
+ }
+ // Extract the Ethereum signature and do a sanity validation
+ if len(response.GetSignatureR()) == 0 || len(response.GetSignatureS()) == 0 || response.GetSignatureV() == 0 {
+ return common.Address{}, nil, errors.New("reply lacks signature")
+ }
+ signature := append(append(response.GetSignatureR(), response.GetSignatureS()...), byte(response.GetSignatureV()))
+
+ // Create the correct signer and signature transform based on the chain ID
+ var signer types.Signer
+ if chainID == nil {
+ signer = new(types.HomesteadSigner)
+ } else {
+ signer = types.NewEIP155Signer(chainID)
+ signature[64] -= byte(chainID.Uint64()*2 + 35)
+ }
+ // Inject the final signature into the transaction and sanity check the sender
+ signed, err := tx.WithSignature(signer, signature)
+ if err != nil {
+ return common.Address{}, nil, err
+ }
+ sender, err := types.Sender(signer, signed)
+ if err != nil {
+ return common.Address{}, nil, err
+ }
+ return sender, signed, nil
+}
+
+// trezorExchange performs a data exchange with the Trezor wallet, sending it a
+// message and retrieving the response. If multiple responses are possible, the
+// method will also return the index of the destination object used.
+func (w *trezorDriver) trezorExchange(req proto.Message, results ...proto.Message) (int, error) {
+ // Construct the original message payload to chunk up
+ data, err := proto.Marshal(req)
+ if err != nil {
+ return 0, err
+ }
+ payload := make([]byte, 8+len(data))
+ copy(payload, []byte{0x23, 0x23})
+ binary.BigEndian.PutUint16(payload[2:], trezor.Type(req))
+ binary.BigEndian.PutUint32(payload[4:], uint32(len(data)))
+ copy(payload[8:], data)
+
+ // Stream all the chunks to the device
+ chunk := make([]byte, 64)
+ chunk[0] = 0x3f // Report ID magic number
+
+ for len(payload) > 0 {
+ // Construct the new message to stream, padding with zeroes if needed
+ if len(payload) > 63 {
+ copy(chunk[1:], payload[:63])
+ payload = payload[63:]
+ } else {
+ copy(chunk[1:], payload)
+ copy(chunk[1+len(payload):], make([]byte, 63-len(payload)))
+ payload = nil
+ }
+ // Send over to the device
+ w.log.Trace("Data chunk sent to the Trezor", "chunk", hexutil.Bytes(chunk))
+ if _, err := w.device.Write(chunk); err != nil {
+ return 0, err
+ }
+ }
+ // Stream the reply back from the wallet in 64 byte chunks
+ var (
+ kind uint16
+ reply []byte
+ )
+ for {
+ // Read the next chunk from the Trezor wallet
+ if _, err := io.ReadFull(w.device, chunk); err != nil {
+ return 0, err
+ }
+ w.log.Trace("Data chunk received from the Trezor", "chunk", hexutil.Bytes(chunk))
+
+ // Make sure the transport header matches
+ if chunk[0] != 0x3f || (len(reply) == 0 && (chunk[1] != 0x23 || chunk[2] != 0x23)) {
+ return 0, errTrezorReplyInvalidHeader
+ }
+ // If it's the first chunk, retrieve the reply message type and total message length
+ var payload []byte
+
+ if len(reply) == 0 {
+ kind = binary.BigEndian.Uint16(chunk[3:5])
+ reply = make([]byte, 0, int(binary.BigEndian.Uint32(chunk[5:9])))
+ payload = chunk[9:]
+ } else {
+ payload = chunk[1:]
+ }
+ // Append to the reply and stop when filled up
+ if left := cap(reply) - len(reply); left > len(payload) {
+ reply = append(reply, payload...)
+ } else {
+ reply = append(reply, payload[:left]...)
+ break
+ }
+ }
+ // Try to parse the reply into the requested reply message
+ if kind == uint16(trezor.MessageType_MessageType_Failure) {
+ // Trezor returned a failure, extract and return the message
+ failure := new(trezor.Failure)
+ if err := proto.Unmarshal(reply, failure); err != nil {
+ return 0, err
+ }
+ return 0, errors.New("trezor: " + failure.GetMessage())
+ }
+ if kind == uint16(trezor.MessageType_MessageType_ButtonRequest) {
+ // Trezor is waiting for user confirmation, ack and wait for the next message
+ return w.trezorExchange(&trezor.ButtonAck{}, results...)
+ }
+ for i, res := range results {
+ if trezor.Type(res) == kind {
+ return i, proto.Unmarshal(reply, res)
+ }
+ }
+ expected := make([]string, len(results))
+ for i, res := range results {
+ expected[i] = trezor.Name(trezor.Type(res))
+ }
+ return 0, fmt.Errorf("trezor: expected reply types %s, got %s", expected, trezor.Name(kind))
+}
diff --git a/accounts/usbwallet/trezor/messages-common.pb.go b/accounts/usbwallet/trezor/messages-common.pb.go
new file mode 100644
index 0000000..304bec0
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages-common.pb.go
@@ -0,0 +1,811 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: messages-common.proto
+
+package trezor
+
+import (
+ fmt "fmt"
+ math "math"
+
+ proto "github.com/golang/protobuf/proto"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+type Failure_FailureType int32
+
+const (
+ Failure_Failure_UnexpectedMessage Failure_FailureType = 1
+ Failure_Failure_ButtonExpected Failure_FailureType = 2
+ Failure_Failure_DataError Failure_FailureType = 3
+ Failure_Failure_ActionCancelled Failure_FailureType = 4
+ Failure_Failure_PinExpected Failure_FailureType = 5
+ Failure_Failure_PinCancelled Failure_FailureType = 6
+ Failure_Failure_PinInvalid Failure_FailureType = 7
+ Failure_Failure_InvalidSignature Failure_FailureType = 8
+ Failure_Failure_ProcessError Failure_FailureType = 9
+ Failure_Failure_NotEnoughFunds Failure_FailureType = 10
+ Failure_Failure_NotInitialized Failure_FailureType = 11
+ Failure_Failure_PinMismatch Failure_FailureType = 12
+ Failure_Failure_FirmwareError Failure_FailureType = 99
+)
+
+var Failure_FailureType_name = map[int32]string{
+ 1: "Failure_UnexpectedMessage",
+ 2: "Failure_ButtonExpected",
+ 3: "Failure_DataError",
+ 4: "Failure_ActionCancelled",
+ 5: "Failure_PinExpected",
+ 6: "Failure_PinCancelled",
+ 7: "Failure_PinInvalid",
+ 8: "Failure_InvalidSignature",
+ 9: "Failure_ProcessError",
+ 10: "Failure_NotEnoughFunds",
+ 11: "Failure_NotInitialized",
+ 12: "Failure_PinMismatch",
+ 99: "Failure_FirmwareError",
+}
+
+var Failure_FailureType_value = map[string]int32{
+ "Failure_UnexpectedMessage": 1,
+ "Failure_ButtonExpected": 2,
+ "Failure_DataError": 3,
+ "Failure_ActionCancelled": 4,
+ "Failure_PinExpected": 5,
+ "Failure_PinCancelled": 6,
+ "Failure_PinInvalid": 7,
+ "Failure_InvalidSignature": 8,
+ "Failure_ProcessError": 9,
+ "Failure_NotEnoughFunds": 10,
+ "Failure_NotInitialized": 11,
+ "Failure_PinMismatch": 12,
+ "Failure_FirmwareError": 99,
+}
+
+func (x Failure_FailureType) Enum() *Failure_FailureType {
+ p := new(Failure_FailureType)
+ *p = x
+ return p
+}
+
+func (x Failure_FailureType) String() string {
+ return proto.EnumName(Failure_FailureType_name, int32(x))
+}
+
+func (x *Failure_FailureType) UnmarshalJSON(data []byte) error {
+ value, err := proto.UnmarshalJSONEnum(Failure_FailureType_value, data, "Failure_FailureType")
+ if err != nil {
+ return err
+ }
+ *x = Failure_FailureType(value)
+ return nil
+}
+
+func (Failure_FailureType) EnumDescriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{1, 0}
+}
+
+//*
+// Type of button request
+type ButtonRequest_ButtonRequestType int32
+
+const (
+ ButtonRequest_ButtonRequest_Other ButtonRequest_ButtonRequestType = 1
+ ButtonRequest_ButtonRequest_FeeOverThreshold ButtonRequest_ButtonRequestType = 2
+ ButtonRequest_ButtonRequest_ConfirmOutput ButtonRequest_ButtonRequestType = 3
+ ButtonRequest_ButtonRequest_ResetDevice ButtonRequest_ButtonRequestType = 4
+ ButtonRequest_ButtonRequest_ConfirmWord ButtonRequest_ButtonRequestType = 5
+ ButtonRequest_ButtonRequest_WipeDevice ButtonRequest_ButtonRequestType = 6
+ ButtonRequest_ButtonRequest_ProtectCall ButtonRequest_ButtonRequestType = 7
+ ButtonRequest_ButtonRequest_SignTx ButtonRequest_ButtonRequestType = 8
+ ButtonRequest_ButtonRequest_FirmwareCheck ButtonRequest_ButtonRequestType = 9
+ ButtonRequest_ButtonRequest_Address ButtonRequest_ButtonRequestType = 10
+ ButtonRequest_ButtonRequest_PublicKey ButtonRequest_ButtonRequestType = 11
+ ButtonRequest_ButtonRequest_MnemonicWordCount ButtonRequest_ButtonRequestType = 12
+ ButtonRequest_ButtonRequest_MnemonicInput ButtonRequest_ButtonRequestType = 13
+ ButtonRequest_ButtonRequest_PassphraseType ButtonRequest_ButtonRequestType = 14
+ ButtonRequest_ButtonRequest_UnknownDerivationPath ButtonRequest_ButtonRequestType = 15
+)
+
+var ButtonRequest_ButtonRequestType_name = map[int32]string{
+ 1: "ButtonRequest_Other",
+ 2: "ButtonRequest_FeeOverThreshold",
+ 3: "ButtonRequest_ConfirmOutput",
+ 4: "ButtonRequest_ResetDevice",
+ 5: "ButtonRequest_ConfirmWord",
+ 6: "ButtonRequest_WipeDevice",
+ 7: "ButtonRequest_ProtectCall",
+ 8: "ButtonRequest_SignTx",
+ 9: "ButtonRequest_FirmwareCheck",
+ 10: "ButtonRequest_Address",
+ 11: "ButtonRequest_PublicKey",
+ 12: "ButtonRequest_MnemonicWordCount",
+ 13: "ButtonRequest_MnemonicInput",
+ 14: "ButtonRequest_PassphraseType",
+ 15: "ButtonRequest_UnknownDerivationPath",
+}
+
+var ButtonRequest_ButtonRequestType_value = map[string]int32{
+ "ButtonRequest_Other": 1,
+ "ButtonRequest_FeeOverThreshold": 2,
+ "ButtonRequest_ConfirmOutput": 3,
+ "ButtonRequest_ResetDevice": 4,
+ "ButtonRequest_ConfirmWord": 5,
+ "ButtonRequest_WipeDevice": 6,
+ "ButtonRequest_ProtectCall": 7,
+ "ButtonRequest_SignTx": 8,
+ "ButtonRequest_FirmwareCheck": 9,
+ "ButtonRequest_Address": 10,
+ "ButtonRequest_PublicKey": 11,
+ "ButtonRequest_MnemonicWordCount": 12,
+ "ButtonRequest_MnemonicInput": 13,
+ "ButtonRequest_PassphraseType": 14,
+ "ButtonRequest_UnknownDerivationPath": 15,
+}
+
+func (x ButtonRequest_ButtonRequestType) Enum() *ButtonRequest_ButtonRequestType {
+ p := new(ButtonRequest_ButtonRequestType)
+ *p = x
+ return p
+}
+
+func (x ButtonRequest_ButtonRequestType) String() string {
+ return proto.EnumName(ButtonRequest_ButtonRequestType_name, int32(x))
+}
+
+func (x *ButtonRequest_ButtonRequestType) UnmarshalJSON(data []byte) error {
+ value, err := proto.UnmarshalJSONEnum(ButtonRequest_ButtonRequestType_value, data, "ButtonRequest_ButtonRequestType")
+ if err != nil {
+ return err
+ }
+ *x = ButtonRequest_ButtonRequestType(value)
+ return nil
+}
+
+func (ButtonRequest_ButtonRequestType) EnumDescriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{2, 0}
+}
+
+//*
+// Type of PIN request
+type PinMatrixRequest_PinMatrixRequestType int32
+
+const (
+ PinMatrixRequest_PinMatrixRequestType_Current PinMatrixRequest_PinMatrixRequestType = 1
+ PinMatrixRequest_PinMatrixRequestType_NewFirst PinMatrixRequest_PinMatrixRequestType = 2
+ PinMatrixRequest_PinMatrixRequestType_NewSecond PinMatrixRequest_PinMatrixRequestType = 3
+)
+
+var PinMatrixRequest_PinMatrixRequestType_name = map[int32]string{
+ 1: "PinMatrixRequestType_Current",
+ 2: "PinMatrixRequestType_NewFirst",
+ 3: "PinMatrixRequestType_NewSecond",
+}
+
+var PinMatrixRequest_PinMatrixRequestType_value = map[string]int32{
+ "PinMatrixRequestType_Current": 1,
+ "PinMatrixRequestType_NewFirst": 2,
+ "PinMatrixRequestType_NewSecond": 3,
+}
+
+func (x PinMatrixRequest_PinMatrixRequestType) Enum() *PinMatrixRequest_PinMatrixRequestType {
+ p := new(PinMatrixRequest_PinMatrixRequestType)
+ *p = x
+ return p
+}
+
+func (x PinMatrixRequest_PinMatrixRequestType) String() string {
+ return proto.EnumName(PinMatrixRequest_PinMatrixRequestType_name, int32(x))
+}
+
+func (x *PinMatrixRequest_PinMatrixRequestType) UnmarshalJSON(data []byte) error {
+ value, err := proto.UnmarshalJSONEnum(PinMatrixRequest_PinMatrixRequestType_value, data, "PinMatrixRequest_PinMatrixRequestType")
+ if err != nil {
+ return err
+ }
+ *x = PinMatrixRequest_PinMatrixRequestType(value)
+ return nil
+}
+
+func (PinMatrixRequest_PinMatrixRequestType) EnumDescriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{4, 0}
+}
+
+//*
+// Response: Success of the previous request
+// @end
+type Success struct {
+ Message *string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *Success) Reset() { *m = Success{} }
+func (m *Success) String() string { return proto.CompactTextString(m) }
+func (*Success) ProtoMessage() {}
+func (*Success) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{0}
+}
+
+func (m *Success) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_Success.Unmarshal(m, b)
+}
+func (m *Success) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_Success.Marshal(b, m, deterministic)
+}
+func (m *Success) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_Success.Merge(m, src)
+}
+func (m *Success) XXX_Size() int {
+ return xxx_messageInfo_Success.Size(m)
+}
+func (m *Success) XXX_DiscardUnknown() {
+ xxx_messageInfo_Success.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Success proto.InternalMessageInfo
+
+func (m *Success) GetMessage() string {
+ if m != nil && m.Message != nil {
+ return *m.Message
+ }
+ return ""
+}
+
+//*
+// Response: Failure of the previous request
+// @end
+type Failure struct {
+ Code *Failure_FailureType `protobuf:"varint,1,opt,name=code,enum=hw.trezor.messages.common.Failure_FailureType" json:"code,omitempty"`
+ Message *string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *Failure) Reset() { *m = Failure{} }
+func (m *Failure) String() string { return proto.CompactTextString(m) }
+func (*Failure) ProtoMessage() {}
+func (*Failure) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{1}
+}
+
+func (m *Failure) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_Failure.Unmarshal(m, b)
+}
+func (m *Failure) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_Failure.Marshal(b, m, deterministic)
+}
+func (m *Failure) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_Failure.Merge(m, src)
+}
+func (m *Failure) XXX_Size() int {
+ return xxx_messageInfo_Failure.Size(m)
+}
+func (m *Failure) XXX_DiscardUnknown() {
+ xxx_messageInfo_Failure.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Failure proto.InternalMessageInfo
+
+func (m *Failure) GetCode() Failure_FailureType {
+ if m != nil && m.Code != nil {
+ return *m.Code
+ }
+ return Failure_Failure_UnexpectedMessage
+}
+
+func (m *Failure) GetMessage() string {
+ if m != nil && m.Message != nil {
+ return *m.Message
+ }
+ return ""
+}
+
+//*
+// Response: Device is waiting for HW button press.
+// @auxstart
+// @next ButtonAck
+type ButtonRequest struct {
+ Code *ButtonRequest_ButtonRequestType `protobuf:"varint,1,opt,name=code,enum=hw.trezor.messages.common.ButtonRequest_ButtonRequestType" json:"code,omitempty"`
+ Data *string `protobuf:"bytes,2,opt,name=data" json:"data,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *ButtonRequest) Reset() { *m = ButtonRequest{} }
+func (m *ButtonRequest) String() string { return proto.CompactTextString(m) }
+func (*ButtonRequest) ProtoMessage() {}
+func (*ButtonRequest) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{2}
+}
+
+func (m *ButtonRequest) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_ButtonRequest.Unmarshal(m, b)
+}
+func (m *ButtonRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_ButtonRequest.Marshal(b, m, deterministic)
+}
+func (m *ButtonRequest) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_ButtonRequest.Merge(m, src)
+}
+func (m *ButtonRequest) XXX_Size() int {
+ return xxx_messageInfo_ButtonRequest.Size(m)
+}
+func (m *ButtonRequest) XXX_DiscardUnknown() {
+ xxx_messageInfo_ButtonRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ButtonRequest proto.InternalMessageInfo
+
+func (m *ButtonRequest) GetCode() ButtonRequest_ButtonRequestType {
+ if m != nil && m.Code != nil {
+ return *m.Code
+ }
+ return ButtonRequest_ButtonRequest_Other
+}
+
+func (m *ButtonRequest) GetData() string {
+ if m != nil && m.Data != nil {
+ return *m.Data
+ }
+ return ""
+}
+
+//*
+// Request: Computer agrees to wait for HW button press
+// @auxend
+type ButtonAck struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *ButtonAck) Reset() { *m = ButtonAck{} }
+func (m *ButtonAck) String() string { return proto.CompactTextString(m) }
+func (*ButtonAck) ProtoMessage() {}
+func (*ButtonAck) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{3}
+}
+
+func (m *ButtonAck) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_ButtonAck.Unmarshal(m, b)
+}
+func (m *ButtonAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_ButtonAck.Marshal(b, m, deterministic)
+}
+func (m *ButtonAck) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_ButtonAck.Merge(m, src)
+}
+func (m *ButtonAck) XXX_Size() int {
+ return xxx_messageInfo_ButtonAck.Size(m)
+}
+func (m *ButtonAck) XXX_DiscardUnknown() {
+ xxx_messageInfo_ButtonAck.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ButtonAck proto.InternalMessageInfo
+
+//*
+// Response: Device is asking computer to show PIN matrix and awaits PIN encoded using this matrix scheme
+// @auxstart
+// @next PinMatrixAck
+type PinMatrixRequest struct {
+ Type *PinMatrixRequest_PinMatrixRequestType `protobuf:"varint,1,opt,name=type,enum=hw.trezor.messages.common.PinMatrixRequest_PinMatrixRequestType" json:"type,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *PinMatrixRequest) Reset() { *m = PinMatrixRequest{} }
+func (m *PinMatrixRequest) String() string { return proto.CompactTextString(m) }
+func (*PinMatrixRequest) ProtoMessage() {}
+func (*PinMatrixRequest) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{4}
+}
+
+func (m *PinMatrixRequest) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_PinMatrixRequest.Unmarshal(m, b)
+}
+func (m *PinMatrixRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_PinMatrixRequest.Marshal(b, m, deterministic)
+}
+func (m *PinMatrixRequest) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_PinMatrixRequest.Merge(m, src)
+}
+func (m *PinMatrixRequest) XXX_Size() int {
+ return xxx_messageInfo_PinMatrixRequest.Size(m)
+}
+func (m *PinMatrixRequest) XXX_DiscardUnknown() {
+ xxx_messageInfo_PinMatrixRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PinMatrixRequest proto.InternalMessageInfo
+
+func (m *PinMatrixRequest) GetType() PinMatrixRequest_PinMatrixRequestType {
+ if m != nil && m.Type != nil {
+ return *m.Type
+ }
+ return PinMatrixRequest_PinMatrixRequestType_Current
+}
+
+//*
+// Request: Computer responds with encoded PIN
+// @auxend
+type PinMatrixAck struct {
+ Pin *string `protobuf:"bytes,1,req,name=pin" json:"pin,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *PinMatrixAck) Reset() { *m = PinMatrixAck{} }
+func (m *PinMatrixAck) String() string { return proto.CompactTextString(m) }
+func (*PinMatrixAck) ProtoMessage() {}
+func (*PinMatrixAck) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{5}
+}
+
+func (m *PinMatrixAck) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_PinMatrixAck.Unmarshal(m, b)
+}
+func (m *PinMatrixAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_PinMatrixAck.Marshal(b, m, deterministic)
+}
+func (m *PinMatrixAck) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_PinMatrixAck.Merge(m, src)
+}
+func (m *PinMatrixAck) XXX_Size() int {
+ return xxx_messageInfo_PinMatrixAck.Size(m)
+}
+func (m *PinMatrixAck) XXX_DiscardUnknown() {
+ xxx_messageInfo_PinMatrixAck.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PinMatrixAck proto.InternalMessageInfo
+
+func (m *PinMatrixAck) GetPin() string {
+ if m != nil && m.Pin != nil {
+ return *m.Pin
+ }
+ return ""
+}
+
+//*
+// Response: Device awaits encryption passphrase
+// @auxstart
+// @next PassphraseAck
+type PassphraseRequest struct {
+ OnDevice *bool `protobuf:"varint,1,opt,name=on_device,json=onDevice" json:"on_device,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *PassphraseRequest) Reset() { *m = PassphraseRequest{} }
+func (m *PassphraseRequest) String() string { return proto.CompactTextString(m) }
+func (*PassphraseRequest) ProtoMessage() {}
+func (*PassphraseRequest) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{6}
+}
+
+func (m *PassphraseRequest) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_PassphraseRequest.Unmarshal(m, b)
+}
+func (m *PassphraseRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_PassphraseRequest.Marshal(b, m, deterministic)
+}
+func (m *PassphraseRequest) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_PassphraseRequest.Merge(m, src)
+}
+func (m *PassphraseRequest) XXX_Size() int {
+ return xxx_messageInfo_PassphraseRequest.Size(m)
+}
+func (m *PassphraseRequest) XXX_DiscardUnknown() {
+ xxx_messageInfo_PassphraseRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PassphraseRequest proto.InternalMessageInfo
+
+func (m *PassphraseRequest) GetOnDevice() bool {
+ if m != nil && m.OnDevice != nil {
+ return *m.OnDevice
+ }
+ return false
+}
+
+//*
+// Request: Send passphrase back
+// @next PassphraseStateRequest
+type PassphraseAck struct {
+ Passphrase *string `protobuf:"bytes,1,opt,name=passphrase" json:"passphrase,omitempty"`
+ State []byte `protobuf:"bytes,2,opt,name=state" json:"state,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *PassphraseAck) Reset() { *m = PassphraseAck{} }
+func (m *PassphraseAck) String() string { return proto.CompactTextString(m) }
+func (*PassphraseAck) ProtoMessage() {}
+func (*PassphraseAck) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{7}
+}
+
+func (m *PassphraseAck) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_PassphraseAck.Unmarshal(m, b)
+}
+func (m *PassphraseAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_PassphraseAck.Marshal(b, m, deterministic)
+}
+func (m *PassphraseAck) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_PassphraseAck.Merge(m, src)
+}
+func (m *PassphraseAck) XXX_Size() int {
+ return xxx_messageInfo_PassphraseAck.Size(m)
+}
+func (m *PassphraseAck) XXX_DiscardUnknown() {
+ xxx_messageInfo_PassphraseAck.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PassphraseAck proto.InternalMessageInfo
+
+func (m *PassphraseAck) GetPassphrase() string {
+ if m != nil && m.Passphrase != nil {
+ return *m.Passphrase
+ }
+ return ""
+}
+
+func (m *PassphraseAck) GetState() []byte {
+ if m != nil {
+ return m.State
+ }
+ return nil
+}
+
+//*
+// Response: Device awaits passphrase state
+// @next PassphraseStateAck
+type PassphraseStateRequest struct {
+ State []byte `protobuf:"bytes,1,opt,name=state" json:"state,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *PassphraseStateRequest) Reset() { *m = PassphraseStateRequest{} }
+func (m *PassphraseStateRequest) String() string { return proto.CompactTextString(m) }
+func (*PassphraseStateRequest) ProtoMessage() {}
+func (*PassphraseStateRequest) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{8}
+}
+
+func (m *PassphraseStateRequest) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_PassphraseStateRequest.Unmarshal(m, b)
+}
+func (m *PassphraseStateRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_PassphraseStateRequest.Marshal(b, m, deterministic)
+}
+func (m *PassphraseStateRequest) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_PassphraseStateRequest.Merge(m, src)
+}
+func (m *PassphraseStateRequest) XXX_Size() int {
+ return xxx_messageInfo_PassphraseStateRequest.Size(m)
+}
+func (m *PassphraseStateRequest) XXX_DiscardUnknown() {
+ xxx_messageInfo_PassphraseStateRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PassphraseStateRequest proto.InternalMessageInfo
+
+func (m *PassphraseStateRequest) GetState() []byte {
+ if m != nil {
+ return m.State
+ }
+ return nil
+}
+
+//*
+// Request: Send passphrase state back
+// @auxend
+type PassphraseStateAck struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *PassphraseStateAck) Reset() { *m = PassphraseStateAck{} }
+func (m *PassphraseStateAck) String() string { return proto.CompactTextString(m) }
+func (*PassphraseStateAck) ProtoMessage() {}
+func (*PassphraseStateAck) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{9}
+}
+
+func (m *PassphraseStateAck) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_PassphraseStateAck.Unmarshal(m, b)
+}
+func (m *PassphraseStateAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_PassphraseStateAck.Marshal(b, m, deterministic)
+}
+func (m *PassphraseStateAck) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_PassphraseStateAck.Merge(m, src)
+}
+func (m *PassphraseStateAck) XXX_Size() int {
+ return xxx_messageInfo_PassphraseStateAck.Size(m)
+}
+func (m *PassphraseStateAck) XXX_DiscardUnknown() {
+ xxx_messageInfo_PassphraseStateAck.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_PassphraseStateAck proto.InternalMessageInfo
+
+//*
+// Structure representing BIP32 (hierarchical deterministic) node
+// Used for imports of private key into the device and exporting public key out of device
+// @embed
+type HDNodeType struct {
+ Depth *uint32 `protobuf:"varint,1,req,name=depth" json:"depth,omitempty"`
+ Fingerprint *uint32 `protobuf:"varint,2,req,name=fingerprint" json:"fingerprint,omitempty"`
+ ChildNum *uint32 `protobuf:"varint,3,req,name=child_num,json=childNum" json:"child_num,omitempty"`
+ ChainCode []byte `protobuf:"bytes,4,req,name=chain_code,json=chainCode" json:"chain_code,omitempty"`
+ PrivateKey []byte `protobuf:"bytes,5,opt,name=private_key,json=privateKey" json:"private_key,omitempty"`
+ PublicKey []byte `protobuf:"bytes,6,opt,name=public_key,json=publicKey" json:"public_key,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *HDNodeType) Reset() { *m = HDNodeType{} }
+func (m *HDNodeType) String() string { return proto.CompactTextString(m) }
+func (*HDNodeType) ProtoMessage() {}
+func (*HDNodeType) Descriptor() ([]byte, []int) {
+ return fileDescriptor_aaf30d059fdbc38d, []int{10}
+}
+
+func (m *HDNodeType) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_HDNodeType.Unmarshal(m, b)
+}
+func (m *HDNodeType) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_HDNodeType.Marshal(b, m, deterministic)
+}
+func (m *HDNodeType) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_HDNodeType.Merge(m, src)
+}
+func (m *HDNodeType) XXX_Size() int {
+ return xxx_messageInfo_HDNodeType.Size(m)
+}
+func (m *HDNodeType) XXX_DiscardUnknown() {
+ xxx_messageInfo_HDNodeType.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_HDNodeType proto.InternalMessageInfo
+
+func (m *HDNodeType) GetDepth() uint32 {
+ if m != nil && m.Depth != nil {
+ return *m.Depth
+ }
+ return 0
+}
+
+func (m *HDNodeType) GetFingerprint() uint32 {
+ if m != nil && m.Fingerprint != nil {
+ return *m.Fingerprint
+ }
+ return 0
+}
+
+func (m *HDNodeType) GetChildNum() uint32 {
+ if m != nil && m.ChildNum != nil {
+ return *m.ChildNum
+ }
+ return 0
+}
+
+func (m *HDNodeType) GetChainCode() []byte {
+ if m != nil {
+ return m.ChainCode
+ }
+ return nil
+}
+
+func (m *HDNodeType) GetPrivateKey() []byte {
+ if m != nil {
+ return m.PrivateKey
+ }
+ return nil
+}
+
+func (m *HDNodeType) GetPublicKey() []byte {
+ if m != nil {
+ return m.PublicKey
+ }
+ return nil
+}
+
+func init() {
+ proto.RegisterEnum("hw.trezor.messages.common.Failure_FailureType", Failure_FailureType_name, Failure_FailureType_value)
+ proto.RegisterEnum("hw.trezor.messages.common.ButtonRequest_ButtonRequestType", ButtonRequest_ButtonRequestType_name, ButtonRequest_ButtonRequestType_value)
+ proto.RegisterEnum("hw.trezor.messages.common.PinMatrixRequest_PinMatrixRequestType", PinMatrixRequest_PinMatrixRequestType_name, PinMatrixRequest_PinMatrixRequestType_value)
+ proto.RegisterType((*Success)(nil), "hw.trezor.messages.common.Success")
+ proto.RegisterType((*Failure)(nil), "hw.trezor.messages.common.Failure")
+ proto.RegisterType((*ButtonRequest)(nil), "hw.trezor.messages.common.ButtonRequest")
+ proto.RegisterType((*ButtonAck)(nil), "hw.trezor.messages.common.ButtonAck")
+ proto.RegisterType((*PinMatrixRequest)(nil), "hw.trezor.messages.common.PinMatrixRequest")
+ proto.RegisterType((*PinMatrixAck)(nil), "hw.trezor.messages.common.PinMatrixAck")
+ proto.RegisterType((*PassphraseRequest)(nil), "hw.trezor.messages.common.PassphraseRequest")
+ proto.RegisterType((*PassphraseAck)(nil), "hw.trezor.messages.common.PassphraseAck")
+ proto.RegisterType((*PassphraseStateRequest)(nil), "hw.trezor.messages.common.PassphraseStateRequest")
+ proto.RegisterType((*PassphraseStateAck)(nil), "hw.trezor.messages.common.PassphraseStateAck")
+ proto.RegisterType((*HDNodeType)(nil), "hw.trezor.messages.common.HDNodeType")
+}
+
+func init() { proto.RegisterFile("messages-common.proto", fileDescriptor_aaf30d059fdbc38d) }
+
+var fileDescriptor_aaf30d059fdbc38d = []byte{
+ // 846 bytes of a gzipped FileDescriptorProto
+ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x54, 0xcd, 0x52, 0x23, 0x37,
+ 0x10, 0x2e, 0xff, 0x80, 0xed, 0xb6, 0xd9, 0x08, 0xc5, 0x80, 0x09, 0xb0, 0x38, 0xc3, 0x21, 0x5c,
+ 0xe2, 0x4a, 0xe5, 0x98, 0x53, 0x58, 0x83, 0x2b, 0xd4, 0x16, 0x86, 0x1a, 0xd8, 0xda, 0xa3, 0x4b,
+ 0xd1, 0xf4, 0x32, 0x2a, 0xcf, 0x48, 0x13, 0x8d, 0x06, 0xf0, 0x5e, 0xf2, 0x6a, 0x79, 0x89, 0xbc,
+ 0x42, 0xaa, 0x52, 0xb9, 0xe4, 0x11, 0xb6, 0x34, 0x3f, 0x78, 0xc6, 0x66, 0x39, 0xcd, 0xe8, 0xfb,
+ 0xbe, 0xee, 0x96, 0xba, 0x3f, 0x09, 0x76, 0x42, 0x8c, 0x63, 0x76, 0x8f, 0xf1, 0x8f, 0x5c, 0x85,
+ 0xa1, 0x92, 0xa3, 0x48, 0x2b, 0xa3, 0xe8, 0xbe, 0xff, 0x38, 0x32, 0x1a, 0x3f, 0x2b, 0x3d, 0x2a,
+ 0x04, 0xa3, 0x4c, 0xe0, 0x9c, 0x40, 0xeb, 0x36, 0xe1, 0x1c, 0xe3, 0x98, 0x0e, 0xa0, 0x95, 0xb3,
+ 0x83, 0xda, 0xb0, 0x76, 0xda, 0x71, 0x8b, 0xa5, 0xf3, 0x77, 0x03, 0x5a, 0x13, 0x26, 0x82, 0x44,
+ 0x23, 0x7d, 0x07, 0x4d, 0xae, 0xbc, 0x4c, 0xf2, 0xe6, 0xe7, 0xd1, 0xe8, 0xab, 0xa9, 0x47, 0x79,
+ 0x44, 0xf1, 0xbd, 0x5b, 0x44, 0xe8, 0xa6, 0xb1, 0xe5, 0x4a, 0xf5, 0x6a, 0xa5, 0xff, 0xea, 0xd0,
+ 0x2d, 0xe9, 0xe9, 0x11, 0xec, 0xe7, 0xcb, 0xd9, 0x07, 0x89, 0x4f, 0x11, 0x72, 0x83, 0xde, 0x55,
+ 0x26, 0x26, 0x35, 0xfa, 0x1d, 0xec, 0x16, 0xf4, 0xbb, 0xc4, 0x18, 0x25, 0x2f, 0x72, 0x09, 0xa9,
+ 0xd3, 0x1d, 0xd8, 0x2e, 0xb8, 0x73, 0x66, 0xd8, 0x85, 0xd6, 0x4a, 0x93, 0x06, 0x3d, 0x80, 0xbd,
+ 0x02, 0x3e, 0xe3, 0x46, 0x28, 0x39, 0x66, 0x92, 0x63, 0x10, 0xa0, 0x47, 0x9a, 0x74, 0x0f, 0xbe,
+ 0x2d, 0xc8, 0x1b, 0xb1, 0x4c, 0xb6, 0x41, 0x07, 0xd0, 0x2f, 0x11, 0xcb, 0x90, 0x4d, 0xba, 0x0b,
+ 0xb4, 0xc4, 0x5c, 0xca, 0x07, 0x16, 0x08, 0x8f, 0xb4, 0xe8, 0x21, 0x0c, 0x0a, 0x3c, 0x07, 0x6f,
+ 0xc5, 0xbd, 0x64, 0x26, 0xd1, 0x48, 0xda, 0x95, 0x7c, 0x5a, 0xd9, 0xf6, 0x67, 0xfb, 0xeb, 0x94,
+ 0x8f, 0x34, 0x55, 0xe6, 0x42, 0xaa, 0xe4, 0xde, 0x9f, 0x24, 0xd2, 0x8b, 0x09, 0xac, 0x70, 0x97,
+ 0x52, 0x18, 0xc1, 0x02, 0xf1, 0x19, 0x3d, 0xd2, 0x5d, 0xd9, 0xfa, 0x95, 0x88, 0x43, 0x66, 0xb8,
+ 0x4f, 0x7a, 0x74, 0x1f, 0x76, 0x0a, 0x62, 0x22, 0x74, 0xf8, 0xc8, 0x34, 0x66, 0xb5, 0xb8, 0xf3,
+ 0x4f, 0x13, 0xb6, 0xb2, 0xbe, 0xb9, 0xf8, 0x47, 0x82, 0xb1, 0xa1, 0xd3, 0xca, 0x74, 0x7f, 0x79,
+ 0x65, 0xba, 0x95, 0xb8, 0xea, 0xaa, 0x34, 0x69, 0x0a, 0x4d, 0x8f, 0x19, 0x96, 0x8f, 0x39, 0xfd,
+ 0x77, 0xfe, 0x6f, 0xc0, 0xf6, 0x9a, 0xde, 0xee, 0xbf, 0x02, 0xce, 0xae, 0x8d, 0x8f, 0x9a, 0xd4,
+ 0xa8, 0x03, 0x6f, 0xab, 0xc4, 0x04, 0xf1, 0xfa, 0x01, 0xf5, 0x9d, 0xaf, 0x31, 0xf6, 0x55, 0x60,
+ 0x67, 0x7d, 0x0c, 0x07, 0x55, 0xcd, 0x58, 0xc9, 0x4f, 0x42, 0x87, 0xd7, 0x89, 0x89, 0x12, 0x43,
+ 0x1a, 0xd6, 0x47, 0x55, 0x81, 0x8b, 0x31, 0x9a, 0x73, 0x7c, 0x10, 0x1c, 0x49, 0x73, 0x9d, 0xce,
+ 0xe3, 0x3f, 0x2a, 0x6d, 0xa7, 0x7f, 0x08, 0x83, 0x2a, 0xfd, 0x51, 0x44, 0x98, 0x07, 0x6f, 0xae,
+ 0x07, 0xdf, 0x68, 0x65, 0x90, 0x9b, 0x31, 0x0b, 0x02, 0xd2, 0xb2, 0xa3, 0xae, 0xd2, 0xd6, 0x07,
+ 0x77, 0x4f, 0xa4, 0xbd, 0xbe, 0xeb, 0x62, 0x3e, 0x63, 0x1f, 0xf9, 0x9c, 0x74, 0xec, 0xe8, 0xaa,
+ 0x82, 0x33, 0xcf, 0xd3, 0x18, 0x5b, 0x2b, 0x1c, 0xc0, 0xde, 0x4a, 0xd1, 0xe4, 0xf7, 0x40, 0xf0,
+ 0xf7, 0xb8, 0x20, 0x5d, 0x7a, 0x02, 0xc7, 0x55, 0xf2, 0x4a, 0x62, 0xa8, 0xa4, 0xe0, 0xf6, 0x3c,
+ 0x63, 0x95, 0x48, 0x43, 0x7a, 0xeb, 0xd5, 0x0b, 0xd1, 0xa5, 0xb4, 0x3d, 0xdb, 0xa2, 0x43, 0x38,
+ 0x5c, 0x29, 0xc1, 0xe2, 0x38, 0xf2, 0x35, 0x8b, 0xd3, 0xbb, 0x49, 0xde, 0xd0, 0x1f, 0xe0, 0xa4,
+ 0xaa, 0xf8, 0x20, 0xe7, 0x52, 0x3d, 0xca, 0x73, 0xd4, 0xe2, 0x81, 0xd9, 0xcb, 0x75, 0xc3, 0x8c,
+ 0x4f, 0xbe, 0x71, 0xba, 0xd0, 0xc9, 0x84, 0x67, 0x7c, 0xee, 0xfc, 0x5b, 0x03, 0x62, 0x2d, 0xca,
+ 0x8c, 0x16, 0x4f, 0x85, 0xf1, 0xee, 0xa0, 0x69, 0x16, 0x51, 0x61, 0xbc, 0x5f, 0x5f, 0x31, 0xde,
+ 0x6a, 0xe8, 0x1a, 0x90, 0xd9, 0xcf, 0x66, 0x73, 0xfe, 0x84, 0xfe, 0x4b, 0xac, 0x3d, 0xda, 0x4b,
+ 0xf8, 0x6c, 0x9c, 0x68, 0x8d, 0xd2, 0x90, 0x1a, 0xfd, 0x1e, 0x8e, 0x5e, 0x54, 0x4c, 0xf1, 0x71,
+ 0x22, 0x74, 0x6c, 0x48, 0xdd, 0x1a, 0xf3, 0x6b, 0x92, 0x5b, 0xe4, 0x4a, 0x7a, 0xa4, 0xe1, 0x0c,
+ 0xa1, 0xf7, 0xac, 0x39, 0xe3, 0x73, 0x4a, 0xa0, 0x11, 0x09, 0x39, 0xa8, 0x0d, 0xeb, 0xa7, 0x1d,
+ 0xd7, 0xfe, 0x3a, 0x3f, 0xc1, 0xf6, 0xb2, 0xaf, 0x45, 0x37, 0x0e, 0xa0, 0xa3, 0xe4, 0xcc, 0x4b,
+ 0x1d, 0x96, 0xb6, 0xa4, 0xed, 0xb6, 0x95, 0xcc, 0x1c, 0xe7, 0x5c, 0xc0, 0xd6, 0x32, 0xc2, 0x26,
+ 0x7d, 0x0b, 0x10, 0x3d, 0x03, 0xf9, 0xdb, 0x5d, 0x42, 0x68, 0x1f, 0x36, 0x62, 0xc3, 0x4c, 0xf6,
+ 0xd8, 0xf6, 0xdc, 0x6c, 0xe1, 0x8c, 0x60, 0x77, 0x99, 0xe6, 0xd6, 0x42, 0x45, 0xf5, 0x67, 0x7d,
+ 0xad, 0xac, 0xef, 0x03, 0x5d, 0xd1, 0xdb, 0x61, 0xfe, 0x55, 0x03, 0xf8, 0xed, 0x7c, 0xaa, 0xbc,
+ 0xec, 0xbd, 0xee, 0xc3, 0x86, 0x87, 0x91, 0xf1, 0xd3, 0x13, 0x6e, 0xb9, 0xd9, 0x82, 0x0e, 0xa1,
+ 0xfb, 0x49, 0xc8, 0x7b, 0xd4, 0x91, 0x16, 0xd2, 0x0c, 0xea, 0x29, 0x57, 0x86, 0xec, 0x81, 0xb9,
+ 0x2f, 0x02, 0x6f, 0x26, 0x93, 0x70, 0xd0, 0x48, 0xf9, 0x76, 0x0a, 0x4c, 0x93, 0x90, 0x1e, 0x01,
+ 0x70, 0x9f, 0x09, 0x39, 0x4b, 0x9f, 0xa6, 0xe6, 0xb0, 0x7e, 0xda, 0x73, 0x3b, 0x29, 0x32, 0xb6,
+ 0x6f, 0xcc, 0x31, 0x74, 0xa3, 0xd4, 0x6f, 0x38, 0x9b, 0xe3, 0x62, 0xb0, 0x91, 0x6e, 0x1a, 0x72,
+ 0xe8, 0x3d, 0x2e, 0x6c, 0x7c, 0x94, 0xde, 0x8e, 0x94, 0xdf, 0x4c, 0xf9, 0x4e, 0x54, 0xdc, 0x97,
+ 0x2f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb2, 0x7d, 0x20, 0xa6, 0x35, 0x07, 0x00, 0x00,
+}
diff --git a/accounts/usbwallet/trezor/messages-common.proto b/accounts/usbwallet/trezor/messages-common.proto
new file mode 100644
index 0000000..75a983b
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages-common.proto
@@ -0,0 +1,147 @@
+// This file originates from the SatoshiLabs Trezor `common` repository at:
+// https://github.com/trezor/trezor-common/blob/master/protob/messages-common.proto
+// dated 28.05.2019, commit 893fd219d4a01bcffa0cd9cfa631856371ec5aa9.
+
+syntax = "proto2";
+package hw.trezor.messages.common;
+
+/**
+ * Response: Success of the previous request
+ * @end
+ */
+message Success {
+ optional string message = 1; // human readable description of action or request-specific payload
+}
+
+/**
+ * Response: Failure of the previous request
+ * @end
+ */
+message Failure {
+ optional FailureType code = 1; // computer-readable definition of the error state
+ optional string message = 2; // human-readable message of the error state
+ enum FailureType {
+ Failure_UnexpectedMessage = 1;
+ Failure_ButtonExpected = 2;
+ Failure_DataError = 3;
+ Failure_ActionCancelled = 4;
+ Failure_PinExpected = 5;
+ Failure_PinCancelled = 6;
+ Failure_PinInvalid = 7;
+ Failure_InvalidSignature = 8;
+ Failure_ProcessError = 9;
+ Failure_NotEnoughFunds = 10;
+ Failure_NotInitialized = 11;
+ Failure_PinMismatch = 12;
+ Failure_FirmwareError = 99;
+ }
+}
+
+/**
+ * Response: Device is waiting for HW button press.
+ * @auxstart
+ * @next ButtonAck
+ */
+message ButtonRequest {
+ optional ButtonRequestType code = 1;
+ optional string data = 2;
+ /**
+ * Type of button request
+ */
+ enum ButtonRequestType {
+ ButtonRequest_Other = 1;
+ ButtonRequest_FeeOverThreshold = 2;
+ ButtonRequest_ConfirmOutput = 3;
+ ButtonRequest_ResetDevice = 4;
+ ButtonRequest_ConfirmWord = 5;
+ ButtonRequest_WipeDevice = 6;
+ ButtonRequest_ProtectCall = 7;
+ ButtonRequest_SignTx = 8;
+ ButtonRequest_FirmwareCheck = 9;
+ ButtonRequest_Address = 10;
+ ButtonRequest_PublicKey = 11;
+ ButtonRequest_MnemonicWordCount = 12;
+ ButtonRequest_MnemonicInput = 13;
+ ButtonRequest_PassphraseType = 14;
+ ButtonRequest_UnknownDerivationPath = 15;
+ }
+}
+
+/**
+ * Request: Computer agrees to wait for HW button press
+ * @auxend
+ */
+message ButtonAck {
+}
+
+/**
+ * Response: Device is asking computer to show PIN matrix and awaits PIN encoded using this matrix scheme
+ * @auxstart
+ * @next PinMatrixAck
+ */
+message PinMatrixRequest {
+ optional PinMatrixRequestType type = 1;
+ /**
+ * Type of PIN request
+ */
+ enum PinMatrixRequestType {
+ PinMatrixRequestType_Current = 1;
+ PinMatrixRequestType_NewFirst = 2;
+ PinMatrixRequestType_NewSecond = 3;
+ }
+}
+
+/**
+ * Request: Computer responds with encoded PIN
+ * @auxend
+ */
+message PinMatrixAck {
+ required string pin = 1; // matrix encoded PIN entered by user
+}
+
+/**
+ * Response: Device awaits encryption passphrase
+ * @auxstart
+ * @next PassphraseAck
+ */
+message PassphraseRequest {
+ optional bool on_device = 1; // passphrase is being entered on the device
+}
+
+/**
+ * Request: Send passphrase back
+ * @next PassphraseStateRequest
+ */
+message PassphraseAck {
+ optional string passphrase = 1;
+ optional bytes state = 2; // expected device state
+}
+
+/**
+ * Response: Device awaits passphrase state
+ * @next PassphraseStateAck
+ */
+message PassphraseStateRequest {
+ optional bytes state = 1; // actual device state
+}
+
+/**
+ * Request: Send passphrase state back
+ * @auxend
+ */
+message PassphraseStateAck {
+}
+
+/**
+ * Structure representing BIP32 (hierarchical deterministic) node
+ * Used for imports of private key into the device and exporting public key out of device
+ * @embed
+ */
+message HDNodeType {
+ required uint32 depth = 1;
+ required uint32 fingerprint = 2;
+ required uint32 child_num = 3;
+ required bytes chain_code = 4;
+ optional bytes private_key = 5;
+ optional bytes public_key = 6;
+}
diff --git a/accounts/usbwallet/trezor/messages-ethereum.pb.go b/accounts/usbwallet/trezor/messages-ethereum.pb.go
new file mode 100644
index 0000000..5d664f5
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages-ethereum.pb.go
@@ -0,0 +1,698 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: messages-ethereum.proto
+
+package trezor
+
+import (
+ fmt "fmt"
+ math "math"
+
+ proto "github.com/golang/protobuf/proto"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+//*
+// Request: Ask device for public key corresponding to address_n path
+// @start
+// @next EthereumPublicKey
+// @next Failure
+type EthereumGetPublicKey struct {
+ AddressN []uint32 `protobuf:"varint,1,rep,name=address_n,json=addressN" json:"address_n,omitempty"`
+ ShowDisplay *bool `protobuf:"varint,2,opt,name=show_display,json=showDisplay" json:"show_display,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumGetPublicKey) Reset() { *m = EthereumGetPublicKey{} }
+func (m *EthereumGetPublicKey) String() string { return proto.CompactTextString(m) }
+func (*EthereumGetPublicKey) ProtoMessage() {}
+func (*EthereumGetPublicKey) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{0}
+}
+
+func (m *EthereumGetPublicKey) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumGetPublicKey.Unmarshal(m, b)
+}
+func (m *EthereumGetPublicKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumGetPublicKey.Marshal(b, m, deterministic)
+}
+func (m *EthereumGetPublicKey) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumGetPublicKey.Merge(m, src)
+}
+func (m *EthereumGetPublicKey) XXX_Size() int {
+ return xxx_messageInfo_EthereumGetPublicKey.Size(m)
+}
+func (m *EthereumGetPublicKey) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumGetPublicKey.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumGetPublicKey proto.InternalMessageInfo
+
+func (m *EthereumGetPublicKey) GetAddressN() []uint32 {
+ if m != nil {
+ return m.AddressN
+ }
+ return nil
+}
+
+func (m *EthereumGetPublicKey) GetShowDisplay() bool {
+ if m != nil && m.ShowDisplay != nil {
+ return *m.ShowDisplay
+ }
+ return false
+}
+
+//*
+// Response: Contains public key derived from device private seed
+// @end
+type EthereumPublicKey struct {
+ Node *HDNodeType `protobuf:"bytes,1,opt,name=node" json:"node,omitempty"`
+ Xpub *string `protobuf:"bytes,2,opt,name=xpub" json:"xpub,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumPublicKey) Reset() { *m = EthereumPublicKey{} }
+func (m *EthereumPublicKey) String() string { return proto.CompactTextString(m) }
+func (*EthereumPublicKey) ProtoMessage() {}
+func (*EthereumPublicKey) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{1}
+}
+
+func (m *EthereumPublicKey) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumPublicKey.Unmarshal(m, b)
+}
+func (m *EthereumPublicKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumPublicKey.Marshal(b, m, deterministic)
+}
+func (m *EthereumPublicKey) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumPublicKey.Merge(m, src)
+}
+func (m *EthereumPublicKey) XXX_Size() int {
+ return xxx_messageInfo_EthereumPublicKey.Size(m)
+}
+func (m *EthereumPublicKey) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumPublicKey.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumPublicKey proto.InternalMessageInfo
+
+func (m *EthereumPublicKey) GetNode() *HDNodeType {
+ if m != nil {
+ return m.Node
+ }
+ return nil
+}
+
+func (m *EthereumPublicKey) GetXpub() string {
+ if m != nil && m.Xpub != nil {
+ return *m.Xpub
+ }
+ return ""
+}
+
+//*
+// Request: Ask device for Ethereum address corresponding to address_n path
+// @start
+// @next EthereumAddress
+// @next Failure
+type EthereumGetAddress struct {
+ AddressN []uint32 `protobuf:"varint,1,rep,name=address_n,json=addressN" json:"address_n,omitempty"`
+ ShowDisplay *bool `protobuf:"varint,2,opt,name=show_display,json=showDisplay" json:"show_display,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumGetAddress) Reset() { *m = EthereumGetAddress{} }
+func (m *EthereumGetAddress) String() string { return proto.CompactTextString(m) }
+func (*EthereumGetAddress) ProtoMessage() {}
+func (*EthereumGetAddress) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{2}
+}
+
+func (m *EthereumGetAddress) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumGetAddress.Unmarshal(m, b)
+}
+func (m *EthereumGetAddress) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumGetAddress.Marshal(b, m, deterministic)
+}
+func (m *EthereumGetAddress) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumGetAddress.Merge(m, src)
+}
+func (m *EthereumGetAddress) XXX_Size() int {
+ return xxx_messageInfo_EthereumGetAddress.Size(m)
+}
+func (m *EthereumGetAddress) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumGetAddress.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumGetAddress proto.InternalMessageInfo
+
+func (m *EthereumGetAddress) GetAddressN() []uint32 {
+ if m != nil {
+ return m.AddressN
+ }
+ return nil
+}
+
+func (m *EthereumGetAddress) GetShowDisplay() bool {
+ if m != nil && m.ShowDisplay != nil {
+ return *m.ShowDisplay
+ }
+ return false
+}
+
+//*
+// Response: Contains an Ethereum address derived from device private seed
+// @end
+type EthereumAddress struct {
+ AddressBin []byte `protobuf:"bytes,1,opt,name=addressBin" json:"addressBin,omitempty"`
+ AddressHex *string `protobuf:"bytes,2,opt,name=addressHex" json:"addressHex,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumAddress) Reset() { *m = EthereumAddress{} }
+func (m *EthereumAddress) String() string { return proto.CompactTextString(m) }
+func (*EthereumAddress) ProtoMessage() {}
+func (*EthereumAddress) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{3}
+}
+
+func (m *EthereumAddress) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumAddress.Unmarshal(m, b)
+}
+func (m *EthereumAddress) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumAddress.Marshal(b, m, deterministic)
+}
+func (m *EthereumAddress) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumAddress.Merge(m, src)
+}
+func (m *EthereumAddress) XXX_Size() int {
+ return xxx_messageInfo_EthereumAddress.Size(m)
+}
+func (m *EthereumAddress) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumAddress.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumAddress proto.InternalMessageInfo
+
+func (m *EthereumAddress) GetAddressBin() []byte {
+ if m != nil {
+ return m.AddressBin
+ }
+ return nil
+}
+
+func (m *EthereumAddress) GetAddressHex() string {
+ if m != nil && m.AddressHex != nil {
+ return *m.AddressHex
+ }
+ return ""
+}
+
+//*
+// Request: Ask device to sign transaction
+// All fields are optional from the protocol's point of view. Each field defaults to value `0` if missing.
+// Note: the first at most 1024 bytes of data MUST be transmitted as part of this message.
+// @start
+// @next EthereumTxRequest
+// @next Failure
+type EthereumSignTx struct {
+ AddressN []uint32 `protobuf:"varint,1,rep,name=address_n,json=addressN" json:"address_n,omitempty"`
+ Nonce []byte `protobuf:"bytes,2,opt,name=nonce" json:"nonce,omitempty"`
+ GasPrice []byte `protobuf:"bytes,3,opt,name=gas_price,json=gasPrice" json:"gas_price,omitempty"`
+ GasLimit []byte `protobuf:"bytes,4,opt,name=gas_limit,json=gasLimit" json:"gas_limit,omitempty"`
+ ToBin []byte `protobuf:"bytes,5,opt,name=toBin" json:"toBin,omitempty"`
+ ToHex *string `protobuf:"bytes,11,opt,name=toHex" json:"toHex,omitempty"`
+ Value []byte `protobuf:"bytes,6,opt,name=value" json:"value,omitempty"`
+ DataInitialChunk []byte `protobuf:"bytes,7,opt,name=data_initial_chunk,json=dataInitialChunk" json:"data_initial_chunk,omitempty"`
+ DataLength *uint32 `protobuf:"varint,8,opt,name=data_length,json=dataLength" json:"data_length,omitempty"`
+ ChainId *uint32 `protobuf:"varint,9,opt,name=chain_id,json=chainId" json:"chain_id,omitempty"`
+ TxType *uint32 `protobuf:"varint,10,opt,name=tx_type,json=txType" json:"tx_type,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumSignTx) Reset() { *m = EthereumSignTx{} }
+func (m *EthereumSignTx) String() string { return proto.CompactTextString(m) }
+func (*EthereumSignTx) ProtoMessage() {}
+func (*EthereumSignTx) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{4}
+}
+
+func (m *EthereumSignTx) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumSignTx.Unmarshal(m, b)
+}
+func (m *EthereumSignTx) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumSignTx.Marshal(b, m, deterministic)
+}
+func (m *EthereumSignTx) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumSignTx.Merge(m, src)
+}
+func (m *EthereumSignTx) XXX_Size() int {
+ return xxx_messageInfo_EthereumSignTx.Size(m)
+}
+func (m *EthereumSignTx) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumSignTx.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumSignTx proto.InternalMessageInfo
+
+func (m *EthereumSignTx) GetAddressN() []uint32 {
+ if m != nil {
+ return m.AddressN
+ }
+ return nil
+}
+
+func (m *EthereumSignTx) GetNonce() []byte {
+ if m != nil {
+ return m.Nonce
+ }
+ return nil
+}
+
+func (m *EthereumSignTx) GetGasPrice() []byte {
+ if m != nil {
+ return m.GasPrice
+ }
+ return nil
+}
+
+func (m *EthereumSignTx) GetGasLimit() []byte {
+ if m != nil {
+ return m.GasLimit
+ }
+ return nil
+}
+
+func (m *EthereumSignTx) GetToBin() []byte {
+ if m != nil {
+ return m.ToBin
+ }
+ return nil
+}
+
+func (m *EthereumSignTx) GetToHex() string {
+ if m != nil && m.ToHex != nil {
+ return *m.ToHex
+ }
+ return ""
+}
+
+func (m *EthereumSignTx) GetValue() []byte {
+ if m != nil {
+ return m.Value
+ }
+ return nil
+}
+
+func (m *EthereumSignTx) GetDataInitialChunk() []byte {
+ if m != nil {
+ return m.DataInitialChunk
+ }
+ return nil
+}
+
+func (m *EthereumSignTx) GetDataLength() uint32 {
+ if m != nil && m.DataLength != nil {
+ return *m.DataLength
+ }
+ return 0
+}
+
+func (m *EthereumSignTx) GetChainId() uint32 {
+ if m != nil && m.ChainId != nil {
+ return *m.ChainId
+ }
+ return 0
+}
+
+func (m *EthereumSignTx) GetTxType() uint32 {
+ if m != nil && m.TxType != nil {
+ return *m.TxType
+ }
+ return 0
+}
+
+//*
+// Response: Device asks for more data from transaction payload, or returns the signature.
+// If data_length is set, device awaits that many more bytes of payload.
+// Otherwise, the signature_* fields contain the computed transaction signature. All three fields will be present.
+// @end
+// @next EthereumTxAck
+type EthereumTxRequest struct {
+ DataLength *uint32 `protobuf:"varint,1,opt,name=data_length,json=dataLength" json:"data_length,omitempty"`
+ SignatureV *uint32 `protobuf:"varint,2,opt,name=signature_v,json=signatureV" json:"signature_v,omitempty"`
+ SignatureR []byte `protobuf:"bytes,3,opt,name=signature_r,json=signatureR" json:"signature_r,omitempty"`
+ SignatureS []byte `protobuf:"bytes,4,opt,name=signature_s,json=signatureS" json:"signature_s,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumTxRequest) Reset() { *m = EthereumTxRequest{} }
+func (m *EthereumTxRequest) String() string { return proto.CompactTextString(m) }
+func (*EthereumTxRequest) ProtoMessage() {}
+func (*EthereumTxRequest) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{5}
+}
+
+func (m *EthereumTxRequest) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumTxRequest.Unmarshal(m, b)
+}
+func (m *EthereumTxRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumTxRequest.Marshal(b, m, deterministic)
+}
+func (m *EthereumTxRequest) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumTxRequest.Merge(m, src)
+}
+func (m *EthereumTxRequest) XXX_Size() int {
+ return xxx_messageInfo_EthereumTxRequest.Size(m)
+}
+func (m *EthereumTxRequest) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumTxRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumTxRequest proto.InternalMessageInfo
+
+func (m *EthereumTxRequest) GetDataLength() uint32 {
+ if m != nil && m.DataLength != nil {
+ return *m.DataLength
+ }
+ return 0
+}
+
+func (m *EthereumTxRequest) GetSignatureV() uint32 {
+ if m != nil && m.SignatureV != nil {
+ return *m.SignatureV
+ }
+ return 0
+}
+
+func (m *EthereumTxRequest) GetSignatureR() []byte {
+ if m != nil {
+ return m.SignatureR
+ }
+ return nil
+}
+
+func (m *EthereumTxRequest) GetSignatureS() []byte {
+ if m != nil {
+ return m.SignatureS
+ }
+ return nil
+}
+
+//*
+// Request: Transaction payload data.
+// @next EthereumTxRequest
+type EthereumTxAck struct {
+ DataChunk []byte `protobuf:"bytes,1,opt,name=data_chunk,json=dataChunk" json:"data_chunk,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumTxAck) Reset() { *m = EthereumTxAck{} }
+func (m *EthereumTxAck) String() string { return proto.CompactTextString(m) }
+func (*EthereumTxAck) ProtoMessage() {}
+func (*EthereumTxAck) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{6}
+}
+
+func (m *EthereumTxAck) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumTxAck.Unmarshal(m, b)
+}
+func (m *EthereumTxAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumTxAck.Marshal(b, m, deterministic)
+}
+func (m *EthereumTxAck) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumTxAck.Merge(m, src)
+}
+func (m *EthereumTxAck) XXX_Size() int {
+ return xxx_messageInfo_EthereumTxAck.Size(m)
+}
+func (m *EthereumTxAck) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumTxAck.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumTxAck proto.InternalMessageInfo
+
+func (m *EthereumTxAck) GetDataChunk() []byte {
+ if m != nil {
+ return m.DataChunk
+ }
+ return nil
+}
+
+//*
+// Request: Ask device to sign message
+// @start
+// @next EthereumMessageSignature
+// @next Failure
+type EthereumSignMessage struct {
+ AddressN []uint32 `protobuf:"varint,1,rep,name=address_n,json=addressN" json:"address_n,omitempty"`
+ Message []byte `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumSignMessage) Reset() { *m = EthereumSignMessage{} }
+func (m *EthereumSignMessage) String() string { return proto.CompactTextString(m) }
+func (*EthereumSignMessage) ProtoMessage() {}
+func (*EthereumSignMessage) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{7}
+}
+
+func (m *EthereumSignMessage) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumSignMessage.Unmarshal(m, b)
+}
+func (m *EthereumSignMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumSignMessage.Marshal(b, m, deterministic)
+}
+func (m *EthereumSignMessage) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumSignMessage.Merge(m, src)
+}
+func (m *EthereumSignMessage) XXX_Size() int {
+ return xxx_messageInfo_EthereumSignMessage.Size(m)
+}
+func (m *EthereumSignMessage) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumSignMessage.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumSignMessage proto.InternalMessageInfo
+
+func (m *EthereumSignMessage) GetAddressN() []uint32 {
+ if m != nil {
+ return m.AddressN
+ }
+ return nil
+}
+
+func (m *EthereumSignMessage) GetMessage() []byte {
+ if m != nil {
+ return m.Message
+ }
+ return nil
+}
+
+//*
+// Response: Signed message
+// @end
+type EthereumMessageSignature struct {
+ AddressBin []byte `protobuf:"bytes,1,opt,name=addressBin" json:"addressBin,omitempty"`
+ Signature []byte `protobuf:"bytes,2,opt,name=signature" json:"signature,omitempty"`
+ AddressHex *string `protobuf:"bytes,3,opt,name=addressHex" json:"addressHex,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumMessageSignature) Reset() { *m = EthereumMessageSignature{} }
+func (m *EthereumMessageSignature) String() string { return proto.CompactTextString(m) }
+func (*EthereumMessageSignature) ProtoMessage() {}
+func (*EthereumMessageSignature) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{8}
+}
+
+func (m *EthereumMessageSignature) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumMessageSignature.Unmarshal(m, b)
+}
+func (m *EthereumMessageSignature) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumMessageSignature.Marshal(b, m, deterministic)
+}
+func (m *EthereumMessageSignature) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumMessageSignature.Merge(m, src)
+}
+func (m *EthereumMessageSignature) XXX_Size() int {
+ return xxx_messageInfo_EthereumMessageSignature.Size(m)
+}
+func (m *EthereumMessageSignature) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumMessageSignature.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumMessageSignature proto.InternalMessageInfo
+
+func (m *EthereumMessageSignature) GetAddressBin() []byte {
+ if m != nil {
+ return m.AddressBin
+ }
+ return nil
+}
+
+func (m *EthereumMessageSignature) GetSignature() []byte {
+ if m != nil {
+ return m.Signature
+ }
+ return nil
+}
+
+func (m *EthereumMessageSignature) GetAddressHex() string {
+ if m != nil && m.AddressHex != nil {
+ return *m.AddressHex
+ }
+ return ""
+}
+
+//*
+// Request: Ask device to verify message
+// @start
+// @next Success
+// @next Failure
+type EthereumVerifyMessage struct {
+ AddressBin []byte `protobuf:"bytes,1,opt,name=addressBin" json:"addressBin,omitempty"`
+ Signature []byte `protobuf:"bytes,2,opt,name=signature" json:"signature,omitempty"`
+ Message []byte `protobuf:"bytes,3,opt,name=message" json:"message,omitempty"`
+ AddressHex *string `protobuf:"bytes,4,opt,name=addressHex" json:"addressHex,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EthereumVerifyMessage) Reset() { *m = EthereumVerifyMessage{} }
+func (m *EthereumVerifyMessage) String() string { return proto.CompactTextString(m) }
+func (*EthereumVerifyMessage) ProtoMessage() {}
+func (*EthereumVerifyMessage) Descriptor() ([]byte, []int) {
+ return fileDescriptor_cb33f46ba915f15c, []int{9}
+}
+
+func (m *EthereumVerifyMessage) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EthereumVerifyMessage.Unmarshal(m, b)
+}
+func (m *EthereumVerifyMessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EthereumVerifyMessage.Marshal(b, m, deterministic)
+}
+func (m *EthereumVerifyMessage) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EthereumVerifyMessage.Merge(m, src)
+}
+func (m *EthereumVerifyMessage) XXX_Size() int {
+ return xxx_messageInfo_EthereumVerifyMessage.Size(m)
+}
+func (m *EthereumVerifyMessage) XXX_DiscardUnknown() {
+ xxx_messageInfo_EthereumVerifyMessage.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EthereumVerifyMessage proto.InternalMessageInfo
+
+func (m *EthereumVerifyMessage) GetAddressBin() []byte {
+ if m != nil {
+ return m.AddressBin
+ }
+ return nil
+}
+
+func (m *EthereumVerifyMessage) GetSignature() []byte {
+ if m != nil {
+ return m.Signature
+ }
+ return nil
+}
+
+func (m *EthereumVerifyMessage) GetMessage() []byte {
+ if m != nil {
+ return m.Message
+ }
+ return nil
+}
+
+func (m *EthereumVerifyMessage) GetAddressHex() string {
+ if m != nil && m.AddressHex != nil {
+ return *m.AddressHex
+ }
+ return ""
+}
+
+func init() {
+ proto.RegisterType((*EthereumGetPublicKey)(nil), "hw.trezor.messages.ethereum.EthereumGetPublicKey")
+ proto.RegisterType((*EthereumPublicKey)(nil), "hw.trezor.messages.ethereum.EthereumPublicKey")
+ proto.RegisterType((*EthereumGetAddress)(nil), "hw.trezor.messages.ethereum.EthereumGetAddress")
+ proto.RegisterType((*EthereumAddress)(nil), "hw.trezor.messages.ethereum.EthereumAddress")
+ proto.RegisterType((*EthereumSignTx)(nil), "hw.trezor.messages.ethereum.EthereumSignTx")
+ proto.RegisterType((*EthereumTxRequest)(nil), "hw.trezor.messages.ethereum.EthereumTxRequest")
+ proto.RegisterType((*EthereumTxAck)(nil), "hw.trezor.messages.ethereum.EthereumTxAck")
+ proto.RegisterType((*EthereumSignMessage)(nil), "hw.trezor.messages.ethereum.EthereumSignMessage")
+ proto.RegisterType((*EthereumMessageSignature)(nil), "hw.trezor.messages.ethereum.EthereumMessageSignature")
+ proto.RegisterType((*EthereumVerifyMessage)(nil), "hw.trezor.messages.ethereum.EthereumVerifyMessage")
+}
+
+func init() { proto.RegisterFile("messages-ethereum.proto", fileDescriptor_cb33f46ba915f15c) }
+
+var fileDescriptor_cb33f46ba915f15c = []byte{
+ // 593 bytes of a gzipped FileDescriptorProto
+ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0x4d, 0x6f, 0xd3, 0x40,
+ 0x10, 0x95, 0x9b, 0xb4, 0x49, 0x26, 0x0d, 0x1f, 0xa6, 0x55, 0x17, 0x0a, 0x34, 0x18, 0x21, 0xe5,
+ 0x00, 0x3e, 0x70, 0x43, 0xe2, 0xd2, 0x52, 0x44, 0x2b, 0x4a, 0x55, 0xdc, 0xa8, 0x57, 0x6b, 0x63,
+ 0x6f, 0xe3, 0x55, 0x9d, 0xdd, 0xe0, 0x5d, 0xb7, 0x0e, 0x7f, 0x82, 0x23, 0xff, 0x87, 0x5f, 0x86,
+ 0xf6, 0x2b, 0x71, 0x52, 0x54, 0x0e, 0xbd, 0x65, 0xde, 0xbc, 0x7d, 0xf3, 0x66, 0xf4, 0x62, 0xd8,
+ 0x99, 0x10, 0x21, 0xf0, 0x98, 0x88, 0x77, 0x44, 0x66, 0xa4, 0x20, 0xe5, 0x24, 0x9c, 0x16, 0x5c,
+ 0x72, 0x7f, 0x37, 0xbb, 0x09, 0x65, 0x41, 0x7e, 0xf2, 0x22, 0x74, 0x94, 0xd0, 0x51, 0x9e, 0x6d,
+ 0xcf, 0x5f, 0x25, 0x7c, 0x32, 0xe1, 0xcc, 0xbc, 0x09, 0x2e, 0x60, 0xeb, 0xb3, 0xa5, 0x7c, 0x21,
+ 0xf2, 0xac, 0x1c, 0xe5, 0x34, 0xf9, 0x4a, 0x66, 0xfe, 0x2e, 0x74, 0x70, 0x9a, 0x16, 0x44, 0x88,
+ 0x98, 0x21, 0xaf, 0xdf, 0x18, 0xf4, 0xa2, 0xb6, 0x05, 0x4e, 0xfd, 0x57, 0xb0, 0x29, 0x32, 0x7e,
+ 0x13, 0xa7, 0x54, 0x4c, 0x73, 0x3c, 0x43, 0x6b, 0x7d, 0x6f, 0xd0, 0x8e, 0xba, 0x0a, 0x3b, 0x34,
+ 0x50, 0x30, 0x82, 0xc7, 0x4e, 0x77, 0x21, 0xfa, 0x01, 0x9a, 0x8c, 0xa7, 0x04, 0x79, 0x7d, 0x6f,
+ 0xd0, 0x7d, 0xff, 0x26, 0xfc, 0x87, 0x5f, 0x6b, 0xee, 0xe8, 0xf0, 0x94, 0xa7, 0x64, 0x38, 0x9b,
+ 0x92, 0x48, 0x3f, 0xf1, 0x7d, 0x68, 0x56, 0xd3, 0x72, 0xa4, 0x47, 0x75, 0x22, 0xfd, 0x3b, 0x18,
+ 0x82, 0x5f, 0xf3, 0xbe, 0x6f, 0xdc, 0xdd, 0xdb, 0xf9, 0x77, 0x78, 0xe8, 0x54, 0x9d, 0xe4, 0x4b,
+ 0x00, 0xab, 0x70, 0x40, 0x99, 0x76, 0xbf, 0x19, 0xd5, 0x90, 0x5a, 0xff, 0x88, 0x54, 0xd6, 0x62,
+ 0x0d, 0x09, 0xfe, 0xac, 0xc1, 0x03, 0xa7, 0x79, 0x4e, 0xc7, 0x6c, 0x58, 0xdd, 0xed, 0x72, 0x0b,
+ 0xd6, 0x19, 0x67, 0x09, 0xd1, 0x52, 0x9b, 0x91, 0x29, 0xd4, 0x93, 0x31, 0x16, 0xf1, 0xb4, 0xa0,
+ 0x09, 0x41, 0x0d, 0xdd, 0x69, 0x8f, 0xb1, 0x38, 0x53, 0xb5, 0x6b, 0xe6, 0x74, 0x42, 0x25, 0x6a,
+ 0xce, 0x9b, 0x27, 0xaa, 0x56, 0x7a, 0x92, 0x2b, 0xeb, 0xeb, 0x46, 0x4f, 0x17, 0x06, 0x55, 0x86,
+ 0xbb, 0xda, 0xb0, 0x29, 0x14, 0x7a, 0x8d, 0xf3, 0x92, 0xa0, 0x0d, 0xc3, 0xd5, 0x85, 0xff, 0x16,
+ 0xfc, 0x14, 0x4b, 0x1c, 0x53, 0x46, 0x25, 0xc5, 0x79, 0x9c, 0x64, 0x25, 0xbb, 0x42, 0x2d, 0x4d,
+ 0x79, 0xa4, 0x3a, 0xc7, 0xa6, 0xf1, 0x49, 0xe1, 0xfe, 0x1e, 0x74, 0x35, 0x3b, 0x27, 0x6c, 0x2c,
+ 0x33, 0xd4, 0xee, 0x7b, 0x83, 0x5e, 0x04, 0x0a, 0x3a, 0xd1, 0x88, 0xff, 0x14, 0xda, 0x49, 0x86,
+ 0x29, 0x8b, 0x69, 0x8a, 0x3a, 0xba, 0xdb, 0xd2, 0xf5, 0x71, 0xea, 0xef, 0x40, 0x4b, 0x56, 0xb1,
+ 0x9c, 0x4d, 0x09, 0x02, 0xdd, 0xd9, 0x90, 0x95, 0xca, 0x41, 0xf0, 0xdb, 0x5b, 0x44, 0x6a, 0x58,
+ 0x45, 0xe4, 0x47, 0x49, 0x84, 0x5c, 0x1d, 0xe5, 0xdd, 0x1a, 0xb5, 0x07, 0x5d, 0x41, 0xc7, 0x0c,
+ 0xcb, 0xb2, 0x20, 0xf1, 0xb5, 0xbe, 0x68, 0x2f, 0x82, 0x39, 0x74, 0xb1, 0x4c, 0x28, 0xec, 0x61,
+ 0x17, 0x84, 0x68, 0x99, 0x20, 0xec, 0x71, 0x17, 0x84, 0xf3, 0x20, 0x84, 0xde, 0xc2, 0xd8, 0x7e,
+ 0x72, 0xe5, 0xbf, 0x00, 0xed, 0xc0, 0x5e, 0xc9, 0xe4, 0xa5, 0xa3, 0x10, 0x7d, 0x9e, 0xe0, 0x04,
+ 0x9e, 0xd4, 0xd3, 0xf0, 0xcd, 0x64, 0xff, 0xee, 0x48, 0x20, 0x68, 0xd9, 0xff, 0x88, 0x0d, 0x85,
+ 0x2b, 0x83, 0x0a, 0x90, 0x53, 0xb3, 0x4a, 0xe7, 0xce, 0xda, 0x7f, 0x83, 0xfb, 0x1c, 0x3a, 0xf3,
+ 0x3d, 0xac, 0xee, 0x02, 0x58, 0x89, 0x75, 0xe3, 0x56, 0xac, 0x7f, 0x79, 0xb0, 0xed, 0x46, 0x5f,
+ 0x90, 0x82, 0x5e, 0xce, 0xdc, 0x2a, 0xf7, 0x9b, 0x5b, 0xdb, 0xb5, 0xb1, 0xb4, 0xeb, 0x8a, 0xa3,
+ 0xe6, 0xaa, 0xa3, 0x83, 0x8f, 0xf0, 0x3a, 0xe1, 0x93, 0x50, 0x60, 0xc9, 0x45, 0x46, 0x73, 0x3c,
+ 0x12, 0xee, 0x03, 0x93, 0xd3, 0x91, 0xf9, 0xe2, 0x8d, 0xca, 0xcb, 0x83, 0xed, 0xa1, 0x06, 0xad,
+ 0x5b, 0xb7, 0xc2, 0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x8a, 0xce, 0x81, 0xc8, 0x59, 0x05, 0x00,
+ 0x00,
+}
diff --git a/accounts/usbwallet/trezor/messages-ethereum.proto b/accounts/usbwallet/trezor/messages-ethereum.proto
new file mode 100644
index 0000000..096bed2
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages-ethereum.proto
@@ -0,0 +1,131 @@
+// This file originates from the SatoshiLabs Trezor `common` repository at:
+// https://github.com/trezor/trezor-common/blob/master/protob/messages-ethereum.proto
+// dated 28.05.2019, commit 893fd219d4a01bcffa0cd9cfa631856371ec5aa9.
+
+syntax = "proto2";
+package hw.trezor.messages.ethereum;
+
+// Sugar for easier handling in Java
+option java_package = "com.satoshilabs.trezor.lib.protobuf";
+option java_outer_classname = "TrezorMessageEthereum";
+
+import "messages-common.proto";
+
+
+/**
+ * Request: Ask device for public key corresponding to address_n path
+ * @start
+ * @next EthereumPublicKey
+ * @next Failure
+ */
+message EthereumGetPublicKey {
+ repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
+ optional bool show_display = 2; // optionally show on display before sending the result
+}
+
+/**
+ * Response: Contains public key derived from device private seed
+ * @end
+ */
+message EthereumPublicKey {
+ optional hw.trezor.messages.common.HDNodeType node = 1; // BIP32 public node
+ optional string xpub = 2; // serialized form of public node
+}
+
+/**
+ * Request: Ask device for Ethereum address corresponding to address_n path
+ * @start
+ * @next EthereumAddress
+ * @next Failure
+ */
+message EthereumGetAddress {
+ repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
+ optional bool show_display = 2; // optionally show on display before sending the result
+}
+
+/**
+ * Response: Contains an Ethereum address derived from device private seed
+ * @end
+ */
+message EthereumAddress {
+ optional bytes addressBin = 1; // Ethereum address as 20 bytes (legacy firmwares)
+ optional string addressHex = 2; // Ethereum address as hex string (newer firmwares)
+}
+
+/**
+ * Request: Ask device to sign transaction
+ * All fields are optional from the protocol's point of view. Each field defaults to value `0` if missing.
+ * Note: the first at most 1024 bytes of data MUST be transmitted as part of this message.
+ * @start
+ * @next EthereumTxRequest
+ * @next Failure
+ */
+message EthereumSignTx {
+ repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
+ optional bytes nonce = 2; // <=256 bit unsigned big endian
+ optional bytes gas_price = 3; // <=256 bit unsigned big endian (in wei)
+ optional bytes gas_limit = 4; // <=256 bit unsigned big endian
+ optional bytes toBin = 5; // recipient address (20 bytes, legacy firmware)
+ optional string toHex = 11; // recipient address (hex string, newer firmware)
+ optional bytes value = 6; // <=256 bit unsigned big endian (in wei)
+ optional bytes data_initial_chunk = 7; // The initial data chunk (<= 1024 bytes)
+ optional uint32 data_length = 8; // Length of transaction payload
+ optional uint32 chain_id = 9; // Chain Id for EIP 155
+ optional uint32 tx_type = 10; // (only for Wanchain)
+}
+
+/**
+ * Response: Device asks for more data from transaction payload, or returns the signature.
+ * If data_length is set, device awaits that many more bytes of payload.
+ * Otherwise, the signature_* fields contain the computed transaction signature. All three fields will be present.
+ * @end
+ * @next EthereumTxAck
+ */
+message EthereumTxRequest {
+ optional uint32 data_length = 1; // Number of bytes being requested (<= 1024)
+ optional uint32 signature_v = 2; // Computed signature (recovery parameter, limited to 27 or 28)
+ optional bytes signature_r = 3; // Computed signature R component (256 bit)
+ optional bytes signature_s = 4; // Computed signature S component (256 bit)
+}
+
+/**
+ * Request: Transaction payload data.
+ * @next EthereumTxRequest
+ */
+message EthereumTxAck {
+ optional bytes data_chunk = 1; // Bytes from transaction payload (<= 1024 bytes)
+}
+
+/**
+ * Request: Ask device to sign message
+ * @start
+ * @next EthereumMessageSignature
+ * @next Failure
+ */
+message EthereumSignMessage {
+ repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
+ optional bytes message = 2; // message to be signed
+}
+
+/**
+ * Response: Signed message
+ * @end
+ */
+message EthereumMessageSignature {
+ optional bytes addressBin = 1; // address used to sign the message (20 bytes, legacy firmware)
+ optional bytes signature = 2; // signature of the message
+ optional string addressHex = 3; // address used to sign the message (hex string, newer firmware)
+}
+
+/**
+ * Request: Ask device to verify message
+ * @start
+ * @next Success
+ * @next Failure
+ */
+message EthereumVerifyMessage {
+ optional bytes addressBin = 1; // address to verify (20 bytes, legacy firmware)
+ optional bytes signature = 2; // signature to verify
+ optional bytes message = 3; // message to verify
+ optional string addressHex = 4; // address to verify (hex string, newer firmware)
+}
diff --git a/accounts/usbwallet/trezor/messages-management.pb.go b/accounts/usbwallet/trezor/messages-management.pb.go
new file mode 100644
index 0000000..f5c872f
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages-management.pb.go
@@ -0,0 +1,1621 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: messages-management.proto
+
+package trezor
+
+import (
+ fmt "fmt"
+ math "math"
+
+ proto "github.com/golang/protobuf/proto"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+//*
+// Structure representing passphrase source
+type ApplySettings_PassphraseSourceType int32
+
+const (
+ ApplySettings_ASK ApplySettings_PassphraseSourceType = 0
+ ApplySettings_DEVICE ApplySettings_PassphraseSourceType = 1
+ ApplySettings_HOST ApplySettings_PassphraseSourceType = 2
+)
+
+var ApplySettings_PassphraseSourceType_name = map[int32]string{
+ 0: "ASK",
+ 1: "DEVICE",
+ 2: "HOST",
+}
+
+var ApplySettings_PassphraseSourceType_value = map[string]int32{
+ "ASK": 0,
+ "DEVICE": 1,
+ "HOST": 2,
+}
+
+func (x ApplySettings_PassphraseSourceType) Enum() *ApplySettings_PassphraseSourceType {
+ p := new(ApplySettings_PassphraseSourceType)
+ *p = x
+ return p
+}
+
+func (x ApplySettings_PassphraseSourceType) String() string {
+ return proto.EnumName(ApplySettings_PassphraseSourceType_name, int32(x))
+}
+
+func (x *ApplySettings_PassphraseSourceType) UnmarshalJSON(data []byte) error {
+ value, err := proto.UnmarshalJSONEnum(ApplySettings_PassphraseSourceType_value, data, "ApplySettings_PassphraseSourceType")
+ if err != nil {
+ return err
+ }
+ *x = ApplySettings_PassphraseSourceType(value)
+ return nil
+}
+
+func (ApplySettings_PassphraseSourceType) EnumDescriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{4, 0}
+}
+
+//*
+// Type of recovery procedure. These should be used as bitmask, e.g.,
+// `RecoveryDeviceType_ScrambledWords | RecoveryDeviceType_Matrix`
+// listing every method supported by the host computer.
+//
+// Note that ScrambledWords must be supported by every implementation
+// for backward compatibility; there is no way to not support it.
+type RecoveryDevice_RecoveryDeviceType int32
+
+const (
+ // use powers of two when extending this field
+ RecoveryDevice_RecoveryDeviceType_ScrambledWords RecoveryDevice_RecoveryDeviceType = 0
+ RecoveryDevice_RecoveryDeviceType_Matrix RecoveryDevice_RecoveryDeviceType = 1
+)
+
+var RecoveryDevice_RecoveryDeviceType_name = map[int32]string{
+ 0: "RecoveryDeviceType_ScrambledWords",
+ 1: "RecoveryDeviceType_Matrix",
+}
+
+var RecoveryDevice_RecoveryDeviceType_value = map[string]int32{
+ "RecoveryDeviceType_ScrambledWords": 0,
+ "RecoveryDeviceType_Matrix": 1,
+}
+
+func (x RecoveryDevice_RecoveryDeviceType) Enum() *RecoveryDevice_RecoveryDeviceType {
+ p := new(RecoveryDevice_RecoveryDeviceType)
+ *p = x
+ return p
+}
+
+func (x RecoveryDevice_RecoveryDeviceType) String() string {
+ return proto.EnumName(RecoveryDevice_RecoveryDeviceType_name, int32(x))
+}
+
+func (x *RecoveryDevice_RecoveryDeviceType) UnmarshalJSON(data []byte) error {
+ value, err := proto.UnmarshalJSONEnum(RecoveryDevice_RecoveryDeviceType_value, data, "RecoveryDevice_RecoveryDeviceType")
+ if err != nil {
+ return err
+ }
+ *x = RecoveryDevice_RecoveryDeviceType(value)
+ return nil
+}
+
+func (RecoveryDevice_RecoveryDeviceType) EnumDescriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{17, 0}
+}
+
+//*
+// Type of Recovery Word request
+type WordRequest_WordRequestType int32
+
+const (
+ WordRequest_WordRequestType_Plain WordRequest_WordRequestType = 0
+ WordRequest_WordRequestType_Matrix9 WordRequest_WordRequestType = 1
+ WordRequest_WordRequestType_Matrix6 WordRequest_WordRequestType = 2
+)
+
+var WordRequest_WordRequestType_name = map[int32]string{
+ 0: "WordRequestType_Plain",
+ 1: "WordRequestType_Matrix9",
+ 2: "WordRequestType_Matrix6",
+}
+
+var WordRequest_WordRequestType_value = map[string]int32{
+ "WordRequestType_Plain": 0,
+ "WordRequestType_Matrix9": 1,
+ "WordRequestType_Matrix6": 2,
+}
+
+func (x WordRequest_WordRequestType) Enum() *WordRequest_WordRequestType {
+ p := new(WordRequest_WordRequestType)
+ *p = x
+ return p
+}
+
+func (x WordRequest_WordRequestType) String() string {
+ return proto.EnumName(WordRequest_WordRequestType_name, int32(x))
+}
+
+func (x *WordRequest_WordRequestType) UnmarshalJSON(data []byte) error {
+ value, err := proto.UnmarshalJSONEnum(WordRequest_WordRequestType_value, data, "WordRequest_WordRequestType")
+ if err != nil {
+ return err
+ }
+ *x = WordRequest_WordRequestType(value)
+ return nil
+}
+
+func (WordRequest_WordRequestType) EnumDescriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{18, 0}
+}
+
+//*
+// Request: Reset device to default state and ask for device details
+// @start
+// @next Features
+type Initialize struct {
+ State []byte `protobuf:"bytes,1,opt,name=state" json:"state,omitempty"`
+ SkipPassphrase *bool `protobuf:"varint,2,opt,name=skip_passphrase,json=skipPassphrase" json:"skip_passphrase,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *Initialize) Reset() { *m = Initialize{} }
+func (m *Initialize) String() string { return proto.CompactTextString(m) }
+func (*Initialize) ProtoMessage() {}
+func (*Initialize) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{0}
+}
+
+func (m *Initialize) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_Initialize.Unmarshal(m, b)
+}
+func (m *Initialize) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_Initialize.Marshal(b, m, deterministic)
+}
+func (m *Initialize) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_Initialize.Merge(m, src)
+}
+func (m *Initialize) XXX_Size() int {
+ return xxx_messageInfo_Initialize.Size(m)
+}
+func (m *Initialize) XXX_DiscardUnknown() {
+ xxx_messageInfo_Initialize.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Initialize proto.InternalMessageInfo
+
+func (m *Initialize) GetState() []byte {
+ if m != nil {
+ return m.State
+ }
+ return nil
+}
+
+func (m *Initialize) GetSkipPassphrase() bool {
+ if m != nil && m.SkipPassphrase != nil {
+ return *m.SkipPassphrase
+ }
+ return false
+}
+
+//*
+// Request: Ask for device details (no device reset)
+// @start
+// @next Features
+type GetFeatures struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *GetFeatures) Reset() { *m = GetFeatures{} }
+func (m *GetFeatures) String() string { return proto.CompactTextString(m) }
+func (*GetFeatures) ProtoMessage() {}
+func (*GetFeatures) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{1}
+}
+
+func (m *GetFeatures) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_GetFeatures.Unmarshal(m, b)
+}
+func (m *GetFeatures) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_GetFeatures.Marshal(b, m, deterministic)
+}
+func (m *GetFeatures) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_GetFeatures.Merge(m, src)
+}
+func (m *GetFeatures) XXX_Size() int {
+ return xxx_messageInfo_GetFeatures.Size(m)
+}
+func (m *GetFeatures) XXX_DiscardUnknown() {
+ xxx_messageInfo_GetFeatures.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_GetFeatures proto.InternalMessageInfo
+
+//*
+// Response: Reports various information about the device
+// @end
+type Features struct {
+ Vendor *string `protobuf:"bytes,1,opt,name=vendor" json:"vendor,omitempty"`
+ MajorVersion *uint32 `protobuf:"varint,2,opt,name=major_version,json=majorVersion" json:"major_version,omitempty"`
+ MinorVersion *uint32 `protobuf:"varint,3,opt,name=minor_version,json=minorVersion" json:"minor_version,omitempty"`
+ PatchVersion *uint32 `protobuf:"varint,4,opt,name=patch_version,json=patchVersion" json:"patch_version,omitempty"`
+ BootloaderMode *bool `protobuf:"varint,5,opt,name=bootloader_mode,json=bootloaderMode" json:"bootloader_mode,omitempty"`
+ DeviceId *string `protobuf:"bytes,6,opt,name=device_id,json=deviceId" json:"device_id,omitempty"`
+ PinProtection *bool `protobuf:"varint,7,opt,name=pin_protection,json=pinProtection" json:"pin_protection,omitempty"`
+ PassphraseProtection *bool `protobuf:"varint,8,opt,name=passphrase_protection,json=passphraseProtection" json:"passphrase_protection,omitempty"`
+ Language *string `protobuf:"bytes,9,opt,name=language" json:"language,omitempty"`
+ Label *string `protobuf:"bytes,10,opt,name=label" json:"label,omitempty"`
+ Initialized *bool `protobuf:"varint,12,opt,name=initialized" json:"initialized,omitempty"`
+ Revision []byte `protobuf:"bytes,13,opt,name=revision" json:"revision,omitempty"`
+ BootloaderHash []byte `protobuf:"bytes,14,opt,name=bootloader_hash,json=bootloaderHash" json:"bootloader_hash,omitempty"`
+ Imported *bool `protobuf:"varint,15,opt,name=imported" json:"imported,omitempty"`
+ PinCached *bool `protobuf:"varint,16,opt,name=pin_cached,json=pinCached" json:"pin_cached,omitempty"`
+ PassphraseCached *bool `protobuf:"varint,17,opt,name=passphrase_cached,json=passphraseCached" json:"passphrase_cached,omitempty"`
+ FirmwarePresent *bool `protobuf:"varint,18,opt,name=firmware_present,json=firmwarePresent" json:"firmware_present,omitempty"`
+ NeedsBackup *bool `protobuf:"varint,19,opt,name=needs_backup,json=needsBackup" json:"needs_backup,omitempty"`
+ Flags *uint32 `protobuf:"varint,20,opt,name=flags" json:"flags,omitempty"`
+ Model *string `protobuf:"bytes,21,opt,name=model" json:"model,omitempty"`
+ FwMajor *uint32 `protobuf:"varint,22,opt,name=fw_major,json=fwMajor" json:"fw_major,omitempty"`
+ FwMinor *uint32 `protobuf:"varint,23,opt,name=fw_minor,json=fwMinor" json:"fw_minor,omitempty"`
+ FwPatch *uint32 `protobuf:"varint,24,opt,name=fw_patch,json=fwPatch" json:"fw_patch,omitempty"`
+ FwVendor *string `protobuf:"bytes,25,opt,name=fw_vendor,json=fwVendor" json:"fw_vendor,omitempty"`
+ FwVendorKeys []byte `protobuf:"bytes,26,opt,name=fw_vendor_keys,json=fwVendorKeys" json:"fw_vendor_keys,omitempty"`
+ UnfinishedBackup *bool `protobuf:"varint,27,opt,name=unfinished_backup,json=unfinishedBackup" json:"unfinished_backup,omitempty"`
+ NoBackup *bool `protobuf:"varint,28,opt,name=no_backup,json=noBackup" json:"no_backup,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *Features) Reset() { *m = Features{} }
+func (m *Features) String() string { return proto.CompactTextString(m) }
+func (*Features) ProtoMessage() {}
+func (*Features) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{2}
+}
+
+func (m *Features) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_Features.Unmarshal(m, b)
+}
+func (m *Features) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_Features.Marshal(b, m, deterministic)
+}
+func (m *Features) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_Features.Merge(m, src)
+}
+func (m *Features) XXX_Size() int {
+ return xxx_messageInfo_Features.Size(m)
+}
+func (m *Features) XXX_DiscardUnknown() {
+ xxx_messageInfo_Features.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Features proto.InternalMessageInfo
+
+func (m *Features) GetVendor() string {
+ if m != nil && m.Vendor != nil {
+ return *m.Vendor
+ }
+ return ""
+}
+
+func (m *Features) GetMajorVersion() uint32 {
+ if m != nil && m.MajorVersion != nil {
+ return *m.MajorVersion
+ }
+ return 0
+}
+
+func (m *Features) GetMinorVersion() uint32 {
+ if m != nil && m.MinorVersion != nil {
+ return *m.MinorVersion
+ }
+ return 0
+}
+
+func (m *Features) GetPatchVersion() uint32 {
+ if m != nil && m.PatchVersion != nil {
+ return *m.PatchVersion
+ }
+ return 0
+}
+
+func (m *Features) GetBootloaderMode() bool {
+ if m != nil && m.BootloaderMode != nil {
+ return *m.BootloaderMode
+ }
+ return false
+}
+
+func (m *Features) GetDeviceId() string {
+ if m != nil && m.DeviceId != nil {
+ return *m.DeviceId
+ }
+ return ""
+}
+
+func (m *Features) GetPinProtection() bool {
+ if m != nil && m.PinProtection != nil {
+ return *m.PinProtection
+ }
+ return false
+}
+
+func (m *Features) GetPassphraseProtection() bool {
+ if m != nil && m.PassphraseProtection != nil {
+ return *m.PassphraseProtection
+ }
+ return false
+}
+
+func (m *Features) GetLanguage() string {
+ if m != nil && m.Language != nil {
+ return *m.Language
+ }
+ return ""
+}
+
+func (m *Features) GetLabel() string {
+ if m != nil && m.Label != nil {
+ return *m.Label
+ }
+ return ""
+}
+
+func (m *Features) GetInitialized() bool {
+ if m != nil && m.Initialized != nil {
+ return *m.Initialized
+ }
+ return false
+}
+
+func (m *Features) GetRevision() []byte {
+ if m != nil {
+ return m.Revision
+ }
+ return nil
+}
+
+func (m *Features) GetBootloaderHash() []byte {
+ if m != nil {
+ return m.BootloaderHash
+ }
+ return nil
+}
+
+func (m *Features) GetImported() bool {
+ if m != nil && m.Imported != nil {
+ return *m.Imported
+ }
+ return false
+}
+
+func (m *Features) GetPinCached() bool {
+ if m != nil && m.PinCached != nil {
+ return *m.PinCached
+ }
+ return false
+}
+
+func (m *Features) GetPassphraseCached() bool {
+ if m != nil && m.PassphraseCached != nil {
+ return *m.PassphraseCached
+ }
+ return false
+}
+
+func (m *Features) GetFirmwarePresent() bool {
+ if m != nil && m.FirmwarePresent != nil {
+ return *m.FirmwarePresent
+ }
+ return false
+}
+
+func (m *Features) GetNeedsBackup() bool {
+ if m != nil && m.NeedsBackup != nil {
+ return *m.NeedsBackup
+ }
+ return false
+}
+
+func (m *Features) GetFlags() uint32 {
+ if m != nil && m.Flags != nil {
+ return *m.Flags
+ }
+ return 0
+}
+
+func (m *Features) GetModel() string {
+ if m != nil && m.Model != nil {
+ return *m.Model
+ }
+ return ""
+}
+
+func (m *Features) GetFwMajor() uint32 {
+ if m != nil && m.FwMajor != nil {
+ return *m.FwMajor
+ }
+ return 0
+}
+
+func (m *Features) GetFwMinor() uint32 {
+ if m != nil && m.FwMinor != nil {
+ return *m.FwMinor
+ }
+ return 0
+}
+
+func (m *Features) GetFwPatch() uint32 {
+ if m != nil && m.FwPatch != nil {
+ return *m.FwPatch
+ }
+ return 0
+}
+
+func (m *Features) GetFwVendor() string {
+ if m != nil && m.FwVendor != nil {
+ return *m.FwVendor
+ }
+ return ""
+}
+
+func (m *Features) GetFwVendorKeys() []byte {
+ if m != nil {
+ return m.FwVendorKeys
+ }
+ return nil
+}
+
+func (m *Features) GetUnfinishedBackup() bool {
+ if m != nil && m.UnfinishedBackup != nil {
+ return *m.UnfinishedBackup
+ }
+ return false
+}
+
+func (m *Features) GetNoBackup() bool {
+ if m != nil && m.NoBackup != nil {
+ return *m.NoBackup
+ }
+ return false
+}
+
+//*
+// Request: clear session (removes cached PIN, passphrase, etc).
+// @start
+// @next Success
+type ClearSession struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *ClearSession) Reset() { *m = ClearSession{} }
+func (m *ClearSession) String() string { return proto.CompactTextString(m) }
+func (*ClearSession) ProtoMessage() {}
+func (*ClearSession) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{3}
+}
+
+func (m *ClearSession) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_ClearSession.Unmarshal(m, b)
+}
+func (m *ClearSession) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_ClearSession.Marshal(b, m, deterministic)
+}
+func (m *ClearSession) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_ClearSession.Merge(m, src)
+}
+func (m *ClearSession) XXX_Size() int {
+ return xxx_messageInfo_ClearSession.Size(m)
+}
+func (m *ClearSession) XXX_DiscardUnknown() {
+ xxx_messageInfo_ClearSession.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ClearSession proto.InternalMessageInfo
+
+//*
+// Request: change language and/or label of the device
+// @start
+// @next Success
+// @next Failure
+type ApplySettings struct {
+ Language *string `protobuf:"bytes,1,opt,name=language" json:"language,omitempty"`
+ Label *string `protobuf:"bytes,2,opt,name=label" json:"label,omitempty"`
+ UsePassphrase *bool `protobuf:"varint,3,opt,name=use_passphrase,json=usePassphrase" json:"use_passphrase,omitempty"`
+ Homescreen []byte `protobuf:"bytes,4,opt,name=homescreen" json:"homescreen,omitempty"`
+ PassphraseSource *ApplySettings_PassphraseSourceType `protobuf:"varint,5,opt,name=passphrase_source,json=passphraseSource,enum=hw.trezor.messages.management.ApplySettings_PassphraseSourceType" json:"passphrase_source,omitempty"`
+ AutoLockDelayMs *uint32 `protobuf:"varint,6,opt,name=auto_lock_delay_ms,json=autoLockDelayMs" json:"auto_lock_delay_ms,omitempty"`
+ DisplayRotation *uint32 `protobuf:"varint,7,opt,name=display_rotation,json=displayRotation" json:"display_rotation,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *ApplySettings) Reset() { *m = ApplySettings{} }
+func (m *ApplySettings) String() string { return proto.CompactTextString(m) }
+func (*ApplySettings) ProtoMessage() {}
+func (*ApplySettings) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{4}
+}
+
+func (m *ApplySettings) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_ApplySettings.Unmarshal(m, b)
+}
+func (m *ApplySettings) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_ApplySettings.Marshal(b, m, deterministic)
+}
+func (m *ApplySettings) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_ApplySettings.Merge(m, src)
+}
+func (m *ApplySettings) XXX_Size() int {
+ return xxx_messageInfo_ApplySettings.Size(m)
+}
+func (m *ApplySettings) XXX_DiscardUnknown() {
+ xxx_messageInfo_ApplySettings.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ApplySettings proto.InternalMessageInfo
+
+func (m *ApplySettings) GetLanguage() string {
+ if m != nil && m.Language != nil {
+ return *m.Language
+ }
+ return ""
+}
+
+func (m *ApplySettings) GetLabel() string {
+ if m != nil && m.Label != nil {
+ return *m.Label
+ }
+ return ""
+}
+
+func (m *ApplySettings) GetUsePassphrase() bool {
+ if m != nil && m.UsePassphrase != nil {
+ return *m.UsePassphrase
+ }
+ return false
+}
+
+func (m *ApplySettings) GetHomescreen() []byte {
+ if m != nil {
+ return m.Homescreen
+ }
+ return nil
+}
+
+func (m *ApplySettings) GetPassphraseSource() ApplySettings_PassphraseSourceType {
+ if m != nil && m.PassphraseSource != nil {
+ return *m.PassphraseSource
+ }
+ return ApplySettings_ASK
+}
+
+func (m *ApplySettings) GetAutoLockDelayMs() uint32 {
+ if m != nil && m.AutoLockDelayMs != nil {
+ return *m.AutoLockDelayMs
+ }
+ return 0
+}
+
+func (m *ApplySettings) GetDisplayRotation() uint32 {
+ if m != nil && m.DisplayRotation != nil {
+ return *m.DisplayRotation
+ }
+ return 0
+}
+
+//*
+// Request: set flags of the device
+// @start
+// @next Success
+// @next Failure
+type ApplyFlags struct {
+ Flags *uint32 `protobuf:"varint,1,opt,name=flags" json:"flags,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *ApplyFlags) Reset() { *m = ApplyFlags{} }
+func (m *ApplyFlags) String() string { return proto.CompactTextString(m) }
+func (*ApplyFlags) ProtoMessage() {}
+func (*ApplyFlags) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{5}
+}
+
+func (m *ApplyFlags) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_ApplyFlags.Unmarshal(m, b)
+}
+func (m *ApplyFlags) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_ApplyFlags.Marshal(b, m, deterministic)
+}
+func (m *ApplyFlags) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_ApplyFlags.Merge(m, src)
+}
+func (m *ApplyFlags) XXX_Size() int {
+ return xxx_messageInfo_ApplyFlags.Size(m)
+}
+func (m *ApplyFlags) XXX_DiscardUnknown() {
+ xxx_messageInfo_ApplyFlags.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ApplyFlags proto.InternalMessageInfo
+
+func (m *ApplyFlags) GetFlags() uint32 {
+ if m != nil && m.Flags != nil {
+ return *m.Flags
+ }
+ return 0
+}
+
+//*
+// Request: Starts workflow for setting/changing/removing the PIN
+// @start
+// @next Success
+// @next Failure
+type ChangePin struct {
+ Remove *bool `protobuf:"varint,1,opt,name=remove" json:"remove,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *ChangePin) Reset() { *m = ChangePin{} }
+func (m *ChangePin) String() string { return proto.CompactTextString(m) }
+func (*ChangePin) ProtoMessage() {}
+func (*ChangePin) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{6}
+}
+
+func (m *ChangePin) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_ChangePin.Unmarshal(m, b)
+}
+func (m *ChangePin) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_ChangePin.Marshal(b, m, deterministic)
+}
+func (m *ChangePin) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_ChangePin.Merge(m, src)
+}
+func (m *ChangePin) XXX_Size() int {
+ return xxx_messageInfo_ChangePin.Size(m)
+}
+func (m *ChangePin) XXX_DiscardUnknown() {
+ xxx_messageInfo_ChangePin.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ChangePin proto.InternalMessageInfo
+
+func (m *ChangePin) GetRemove() bool {
+ if m != nil && m.Remove != nil {
+ return *m.Remove
+ }
+ return false
+}
+
+//*
+// Request: Test if the device is alive, device sends back the message in Success response
+// @start
+// @next Success
+type Ping struct {
+ Message *string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`
+ ButtonProtection *bool `protobuf:"varint,2,opt,name=button_protection,json=buttonProtection" json:"button_protection,omitempty"`
+ PinProtection *bool `protobuf:"varint,3,opt,name=pin_protection,json=pinProtection" json:"pin_protection,omitempty"`
+ PassphraseProtection *bool `protobuf:"varint,4,opt,name=passphrase_protection,json=passphraseProtection" json:"passphrase_protection,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *Ping) Reset() { *m = Ping{} }
+func (m *Ping) String() string { return proto.CompactTextString(m) }
+func (*Ping) ProtoMessage() {}
+func (*Ping) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{7}
+}
+
+func (m *Ping) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_Ping.Unmarshal(m, b)
+}
+func (m *Ping) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_Ping.Marshal(b, m, deterministic)
+}
+func (m *Ping) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_Ping.Merge(m, src)
+}
+func (m *Ping) XXX_Size() int {
+ return xxx_messageInfo_Ping.Size(m)
+}
+func (m *Ping) XXX_DiscardUnknown() {
+ xxx_messageInfo_Ping.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Ping proto.InternalMessageInfo
+
+func (m *Ping) GetMessage() string {
+ if m != nil && m.Message != nil {
+ return *m.Message
+ }
+ return ""
+}
+
+func (m *Ping) GetButtonProtection() bool {
+ if m != nil && m.ButtonProtection != nil {
+ return *m.ButtonProtection
+ }
+ return false
+}
+
+func (m *Ping) GetPinProtection() bool {
+ if m != nil && m.PinProtection != nil {
+ return *m.PinProtection
+ }
+ return false
+}
+
+func (m *Ping) GetPassphraseProtection() bool {
+ if m != nil && m.PassphraseProtection != nil {
+ return *m.PassphraseProtection
+ }
+ return false
+}
+
+//*
+// Request: Abort last operation that required user interaction
+// @start
+// @next Failure
+type Cancel struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *Cancel) Reset() { *m = Cancel{} }
+func (m *Cancel) String() string { return proto.CompactTextString(m) }
+func (*Cancel) ProtoMessage() {}
+func (*Cancel) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{8}
+}
+
+func (m *Cancel) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_Cancel.Unmarshal(m, b)
+}
+func (m *Cancel) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_Cancel.Marshal(b, m, deterministic)
+}
+func (m *Cancel) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_Cancel.Merge(m, src)
+}
+func (m *Cancel) XXX_Size() int {
+ return xxx_messageInfo_Cancel.Size(m)
+}
+func (m *Cancel) XXX_DiscardUnknown() {
+ xxx_messageInfo_Cancel.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Cancel proto.InternalMessageInfo
+
+//*
+// Request: Request a sample of random data generated by hardware RNG. May be used for testing.
+// @start
+// @next Entropy
+// @next Failure
+type GetEntropy struct {
+ Size *uint32 `protobuf:"varint,1,req,name=size" json:"size,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *GetEntropy) Reset() { *m = GetEntropy{} }
+func (m *GetEntropy) String() string { return proto.CompactTextString(m) }
+func (*GetEntropy) ProtoMessage() {}
+func (*GetEntropy) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{9}
+}
+
+func (m *GetEntropy) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_GetEntropy.Unmarshal(m, b)
+}
+func (m *GetEntropy) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_GetEntropy.Marshal(b, m, deterministic)
+}
+func (m *GetEntropy) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_GetEntropy.Merge(m, src)
+}
+func (m *GetEntropy) XXX_Size() int {
+ return xxx_messageInfo_GetEntropy.Size(m)
+}
+func (m *GetEntropy) XXX_DiscardUnknown() {
+ xxx_messageInfo_GetEntropy.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_GetEntropy proto.InternalMessageInfo
+
+func (m *GetEntropy) GetSize() uint32 {
+ if m != nil && m.Size != nil {
+ return *m.Size
+ }
+ return 0
+}
+
+//*
+// Response: Reply with random data generated by internal RNG
+// @end
+type Entropy struct {
+ Entropy []byte `protobuf:"bytes,1,req,name=entropy" json:"entropy,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *Entropy) Reset() { *m = Entropy{} }
+func (m *Entropy) String() string { return proto.CompactTextString(m) }
+func (*Entropy) ProtoMessage() {}
+func (*Entropy) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{10}
+}
+
+func (m *Entropy) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_Entropy.Unmarshal(m, b)
+}
+func (m *Entropy) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_Entropy.Marshal(b, m, deterministic)
+}
+func (m *Entropy) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_Entropy.Merge(m, src)
+}
+func (m *Entropy) XXX_Size() int {
+ return xxx_messageInfo_Entropy.Size(m)
+}
+func (m *Entropy) XXX_DiscardUnknown() {
+ xxx_messageInfo_Entropy.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Entropy proto.InternalMessageInfo
+
+func (m *Entropy) GetEntropy() []byte {
+ if m != nil {
+ return m.Entropy
+ }
+ return nil
+}
+
+//*
+// Request: Request device to wipe all sensitive data and settings
+// @start
+// @next Success
+// @next Failure
+type WipeDevice struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *WipeDevice) Reset() { *m = WipeDevice{} }
+func (m *WipeDevice) String() string { return proto.CompactTextString(m) }
+func (*WipeDevice) ProtoMessage() {}
+func (*WipeDevice) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{11}
+}
+
+func (m *WipeDevice) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_WipeDevice.Unmarshal(m, b)
+}
+func (m *WipeDevice) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_WipeDevice.Marshal(b, m, deterministic)
+}
+func (m *WipeDevice) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_WipeDevice.Merge(m, src)
+}
+func (m *WipeDevice) XXX_Size() int {
+ return xxx_messageInfo_WipeDevice.Size(m)
+}
+func (m *WipeDevice) XXX_DiscardUnknown() {
+ xxx_messageInfo_WipeDevice.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_WipeDevice proto.InternalMessageInfo
+
+//*
+// Request: Load seed and related internal settings from the computer
+// @start
+// @next Success
+// @next Failure
+type LoadDevice struct {
+ Mnemonic *string `protobuf:"bytes,1,opt,name=mnemonic" json:"mnemonic,omitempty"`
+ Node *HDNodeType `protobuf:"bytes,2,opt,name=node" json:"node,omitempty"`
+ Pin *string `protobuf:"bytes,3,opt,name=pin" json:"pin,omitempty"`
+ PassphraseProtection *bool `protobuf:"varint,4,opt,name=passphrase_protection,json=passphraseProtection" json:"passphrase_protection,omitempty"`
+ Language *string `protobuf:"bytes,5,opt,name=language,def=english" json:"language,omitempty"`
+ Label *string `protobuf:"bytes,6,opt,name=label" json:"label,omitempty"`
+ SkipChecksum *bool `protobuf:"varint,7,opt,name=skip_checksum,json=skipChecksum" json:"skip_checksum,omitempty"`
+ U2FCounter *uint32 `protobuf:"varint,8,opt,name=u2f_counter,json=u2fCounter" json:"u2f_counter,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *LoadDevice) Reset() { *m = LoadDevice{} }
+func (m *LoadDevice) String() string { return proto.CompactTextString(m) }
+func (*LoadDevice) ProtoMessage() {}
+func (*LoadDevice) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{12}
+}
+
+func (m *LoadDevice) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_LoadDevice.Unmarshal(m, b)
+}
+func (m *LoadDevice) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_LoadDevice.Marshal(b, m, deterministic)
+}
+func (m *LoadDevice) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_LoadDevice.Merge(m, src)
+}
+func (m *LoadDevice) XXX_Size() int {
+ return xxx_messageInfo_LoadDevice.Size(m)
+}
+func (m *LoadDevice) XXX_DiscardUnknown() {
+ xxx_messageInfo_LoadDevice.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_LoadDevice proto.InternalMessageInfo
+
+const Default_LoadDevice_Language string = "english"
+
+func (m *LoadDevice) GetMnemonic() string {
+ if m != nil && m.Mnemonic != nil {
+ return *m.Mnemonic
+ }
+ return ""
+}
+
+func (m *LoadDevice) GetNode() *HDNodeType {
+ if m != nil {
+ return m.Node
+ }
+ return nil
+}
+
+func (m *LoadDevice) GetPin() string {
+ if m != nil && m.Pin != nil {
+ return *m.Pin
+ }
+ return ""
+}
+
+func (m *LoadDevice) GetPassphraseProtection() bool {
+ if m != nil && m.PassphraseProtection != nil {
+ return *m.PassphraseProtection
+ }
+ return false
+}
+
+func (m *LoadDevice) GetLanguage() string {
+ if m != nil && m.Language != nil {
+ return *m.Language
+ }
+ return Default_LoadDevice_Language
+}
+
+func (m *LoadDevice) GetLabel() string {
+ if m != nil && m.Label != nil {
+ return *m.Label
+ }
+ return ""
+}
+
+func (m *LoadDevice) GetSkipChecksum() bool {
+ if m != nil && m.SkipChecksum != nil {
+ return *m.SkipChecksum
+ }
+ return false
+}
+
+func (m *LoadDevice) GetU2FCounter() uint32 {
+ if m != nil && m.U2FCounter != nil {
+ return *m.U2FCounter
+ }
+ return 0
+}
+
+//*
+// Request: Ask device to do initialization involving user interaction
+// @start
+// @next EntropyRequest
+// @next Failure
+type ResetDevice struct {
+ DisplayRandom *bool `protobuf:"varint,1,opt,name=display_random,json=displayRandom" json:"display_random,omitempty"`
+ Strength *uint32 `protobuf:"varint,2,opt,name=strength,def=256" json:"strength,omitempty"`
+ PassphraseProtection *bool `protobuf:"varint,3,opt,name=passphrase_protection,json=passphraseProtection" json:"passphrase_protection,omitempty"`
+ PinProtection *bool `protobuf:"varint,4,opt,name=pin_protection,json=pinProtection" json:"pin_protection,omitempty"`
+ Language *string `protobuf:"bytes,5,opt,name=language,def=english" json:"language,omitempty"`
+ Label *string `protobuf:"bytes,6,opt,name=label" json:"label,omitempty"`
+ U2FCounter *uint32 `protobuf:"varint,7,opt,name=u2f_counter,json=u2fCounter" json:"u2f_counter,omitempty"`
+ SkipBackup *bool `protobuf:"varint,8,opt,name=skip_backup,json=skipBackup" json:"skip_backup,omitempty"`
+ NoBackup *bool `protobuf:"varint,9,opt,name=no_backup,json=noBackup" json:"no_backup,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *ResetDevice) Reset() { *m = ResetDevice{} }
+func (m *ResetDevice) String() string { return proto.CompactTextString(m) }
+func (*ResetDevice) ProtoMessage() {}
+func (*ResetDevice) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{13}
+}
+
+func (m *ResetDevice) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_ResetDevice.Unmarshal(m, b)
+}
+func (m *ResetDevice) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_ResetDevice.Marshal(b, m, deterministic)
+}
+func (m *ResetDevice) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_ResetDevice.Merge(m, src)
+}
+func (m *ResetDevice) XXX_Size() int {
+ return xxx_messageInfo_ResetDevice.Size(m)
+}
+func (m *ResetDevice) XXX_DiscardUnknown() {
+ xxx_messageInfo_ResetDevice.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_ResetDevice proto.InternalMessageInfo
+
+const Default_ResetDevice_Strength uint32 = 256
+const Default_ResetDevice_Language string = "english"
+
+func (m *ResetDevice) GetDisplayRandom() bool {
+ if m != nil && m.DisplayRandom != nil {
+ return *m.DisplayRandom
+ }
+ return false
+}
+
+func (m *ResetDevice) GetStrength() uint32 {
+ if m != nil && m.Strength != nil {
+ return *m.Strength
+ }
+ return Default_ResetDevice_Strength
+}
+
+func (m *ResetDevice) GetPassphraseProtection() bool {
+ if m != nil && m.PassphraseProtection != nil {
+ return *m.PassphraseProtection
+ }
+ return false
+}
+
+func (m *ResetDevice) GetPinProtection() bool {
+ if m != nil && m.PinProtection != nil {
+ return *m.PinProtection
+ }
+ return false
+}
+
+func (m *ResetDevice) GetLanguage() string {
+ if m != nil && m.Language != nil {
+ return *m.Language
+ }
+ return Default_ResetDevice_Language
+}
+
+func (m *ResetDevice) GetLabel() string {
+ if m != nil && m.Label != nil {
+ return *m.Label
+ }
+ return ""
+}
+
+func (m *ResetDevice) GetU2FCounter() uint32 {
+ if m != nil && m.U2FCounter != nil {
+ return *m.U2FCounter
+ }
+ return 0
+}
+
+func (m *ResetDevice) GetSkipBackup() bool {
+ if m != nil && m.SkipBackup != nil {
+ return *m.SkipBackup
+ }
+ return false
+}
+
+func (m *ResetDevice) GetNoBackup() bool {
+ if m != nil && m.NoBackup != nil {
+ return *m.NoBackup
+ }
+ return false
+}
+
+//*
+// Request: Perform backup of the device seed if not backed up using ResetDevice
+// @start
+// @next Success
+type BackupDevice struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *BackupDevice) Reset() { *m = BackupDevice{} }
+func (m *BackupDevice) String() string { return proto.CompactTextString(m) }
+func (*BackupDevice) ProtoMessage() {}
+func (*BackupDevice) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{14}
+}
+
+func (m *BackupDevice) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_BackupDevice.Unmarshal(m, b)
+}
+func (m *BackupDevice) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_BackupDevice.Marshal(b, m, deterministic)
+}
+func (m *BackupDevice) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_BackupDevice.Merge(m, src)
+}
+func (m *BackupDevice) XXX_Size() int {
+ return xxx_messageInfo_BackupDevice.Size(m)
+}
+func (m *BackupDevice) XXX_DiscardUnknown() {
+ xxx_messageInfo_BackupDevice.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_BackupDevice proto.InternalMessageInfo
+
+//*
+// Response: Ask for additional entropy from host computer
+// @next EntropyAck
+type EntropyRequest struct {
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EntropyRequest) Reset() { *m = EntropyRequest{} }
+func (m *EntropyRequest) String() string { return proto.CompactTextString(m) }
+func (*EntropyRequest) ProtoMessage() {}
+func (*EntropyRequest) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{15}
+}
+
+func (m *EntropyRequest) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EntropyRequest.Unmarshal(m, b)
+}
+func (m *EntropyRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EntropyRequest.Marshal(b, m, deterministic)
+}
+func (m *EntropyRequest) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EntropyRequest.Merge(m, src)
+}
+func (m *EntropyRequest) XXX_Size() int {
+ return xxx_messageInfo_EntropyRequest.Size(m)
+}
+func (m *EntropyRequest) XXX_DiscardUnknown() {
+ xxx_messageInfo_EntropyRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EntropyRequest proto.InternalMessageInfo
+
+//*
+// Request: Provide additional entropy for seed generation function
+// @next Success
+type EntropyAck struct {
+ Entropy []byte `protobuf:"bytes,1,opt,name=entropy" json:"entropy,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *EntropyAck) Reset() { *m = EntropyAck{} }
+func (m *EntropyAck) String() string { return proto.CompactTextString(m) }
+func (*EntropyAck) ProtoMessage() {}
+func (*EntropyAck) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{16}
+}
+
+func (m *EntropyAck) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_EntropyAck.Unmarshal(m, b)
+}
+func (m *EntropyAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_EntropyAck.Marshal(b, m, deterministic)
+}
+func (m *EntropyAck) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_EntropyAck.Merge(m, src)
+}
+func (m *EntropyAck) XXX_Size() int {
+ return xxx_messageInfo_EntropyAck.Size(m)
+}
+func (m *EntropyAck) XXX_DiscardUnknown() {
+ xxx_messageInfo_EntropyAck.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_EntropyAck proto.InternalMessageInfo
+
+func (m *EntropyAck) GetEntropy() []byte {
+ if m != nil {
+ return m.Entropy
+ }
+ return nil
+}
+
+//*
+// Request: Start recovery workflow asking user for specific words of mnemonic
+// Used to recovery device safely even on untrusted computer.
+// @start
+// @next WordRequest
+type RecoveryDevice struct {
+ WordCount *uint32 `protobuf:"varint,1,opt,name=word_count,json=wordCount" json:"word_count,omitempty"`
+ PassphraseProtection *bool `protobuf:"varint,2,opt,name=passphrase_protection,json=passphraseProtection" json:"passphrase_protection,omitempty"`
+ PinProtection *bool `protobuf:"varint,3,opt,name=pin_protection,json=pinProtection" json:"pin_protection,omitempty"`
+ Language *string `protobuf:"bytes,4,opt,name=language,def=english" json:"language,omitempty"`
+ Label *string `protobuf:"bytes,5,opt,name=label" json:"label,omitempty"`
+ EnforceWordlist *bool `protobuf:"varint,6,opt,name=enforce_wordlist,json=enforceWordlist" json:"enforce_wordlist,omitempty"`
+ // 7 reserved for unused recovery method
+ Type *RecoveryDevice_RecoveryDeviceType `protobuf:"varint,8,opt,name=type,enum=hw.trezor.messages.management.RecoveryDevice_RecoveryDeviceType" json:"type,omitempty"`
+ U2FCounter *uint32 `protobuf:"varint,9,opt,name=u2f_counter,json=u2fCounter" json:"u2f_counter,omitempty"`
+ DryRun *bool `protobuf:"varint,10,opt,name=dry_run,json=dryRun" json:"dry_run,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *RecoveryDevice) Reset() { *m = RecoveryDevice{} }
+func (m *RecoveryDevice) String() string { return proto.CompactTextString(m) }
+func (*RecoveryDevice) ProtoMessage() {}
+func (*RecoveryDevice) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{17}
+}
+
+func (m *RecoveryDevice) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_RecoveryDevice.Unmarshal(m, b)
+}
+func (m *RecoveryDevice) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_RecoveryDevice.Marshal(b, m, deterministic)
+}
+func (m *RecoveryDevice) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_RecoveryDevice.Merge(m, src)
+}
+func (m *RecoveryDevice) XXX_Size() int {
+ return xxx_messageInfo_RecoveryDevice.Size(m)
+}
+func (m *RecoveryDevice) XXX_DiscardUnknown() {
+ xxx_messageInfo_RecoveryDevice.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_RecoveryDevice proto.InternalMessageInfo
+
+const Default_RecoveryDevice_Language string = "english"
+
+func (m *RecoveryDevice) GetWordCount() uint32 {
+ if m != nil && m.WordCount != nil {
+ return *m.WordCount
+ }
+ return 0
+}
+
+func (m *RecoveryDevice) GetPassphraseProtection() bool {
+ if m != nil && m.PassphraseProtection != nil {
+ return *m.PassphraseProtection
+ }
+ return false
+}
+
+func (m *RecoveryDevice) GetPinProtection() bool {
+ if m != nil && m.PinProtection != nil {
+ return *m.PinProtection
+ }
+ return false
+}
+
+func (m *RecoveryDevice) GetLanguage() string {
+ if m != nil && m.Language != nil {
+ return *m.Language
+ }
+ return Default_RecoveryDevice_Language
+}
+
+func (m *RecoveryDevice) GetLabel() string {
+ if m != nil && m.Label != nil {
+ return *m.Label
+ }
+ return ""
+}
+
+func (m *RecoveryDevice) GetEnforceWordlist() bool {
+ if m != nil && m.EnforceWordlist != nil {
+ return *m.EnforceWordlist
+ }
+ return false
+}
+
+func (m *RecoveryDevice) GetType() RecoveryDevice_RecoveryDeviceType {
+ if m != nil && m.Type != nil {
+ return *m.Type
+ }
+ return RecoveryDevice_RecoveryDeviceType_ScrambledWords
+}
+
+func (m *RecoveryDevice) GetU2FCounter() uint32 {
+ if m != nil && m.U2FCounter != nil {
+ return *m.U2FCounter
+ }
+ return 0
+}
+
+func (m *RecoveryDevice) GetDryRun() bool {
+ if m != nil && m.DryRun != nil {
+ return *m.DryRun
+ }
+ return false
+}
+
+//*
+// Response: Device is waiting for user to enter word of the mnemonic
+// Its position is shown only on device's internal display.
+// @next WordAck
+type WordRequest struct {
+ Type *WordRequest_WordRequestType `protobuf:"varint,1,opt,name=type,enum=hw.trezor.messages.management.WordRequest_WordRequestType" json:"type,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *WordRequest) Reset() { *m = WordRequest{} }
+func (m *WordRequest) String() string { return proto.CompactTextString(m) }
+func (*WordRequest) ProtoMessage() {}
+func (*WordRequest) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{18}
+}
+
+func (m *WordRequest) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_WordRequest.Unmarshal(m, b)
+}
+func (m *WordRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_WordRequest.Marshal(b, m, deterministic)
+}
+func (m *WordRequest) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_WordRequest.Merge(m, src)
+}
+func (m *WordRequest) XXX_Size() int {
+ return xxx_messageInfo_WordRequest.Size(m)
+}
+func (m *WordRequest) XXX_DiscardUnknown() {
+ xxx_messageInfo_WordRequest.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_WordRequest proto.InternalMessageInfo
+
+func (m *WordRequest) GetType() WordRequest_WordRequestType {
+ if m != nil && m.Type != nil {
+ return *m.Type
+ }
+ return WordRequest_WordRequestType_Plain
+}
+
+//*
+// Request: Computer replies with word from the mnemonic
+// @next WordRequest
+// @next Success
+// @next Failure
+type WordAck struct {
+ Word *string `protobuf:"bytes,1,req,name=word" json:"word,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *WordAck) Reset() { *m = WordAck{} }
+func (m *WordAck) String() string { return proto.CompactTextString(m) }
+func (*WordAck) ProtoMessage() {}
+func (*WordAck) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{19}
+}
+
+func (m *WordAck) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_WordAck.Unmarshal(m, b)
+}
+func (m *WordAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_WordAck.Marshal(b, m, deterministic)
+}
+func (m *WordAck) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_WordAck.Merge(m, src)
+}
+func (m *WordAck) XXX_Size() int {
+ return xxx_messageInfo_WordAck.Size(m)
+}
+func (m *WordAck) XXX_DiscardUnknown() {
+ xxx_messageInfo_WordAck.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_WordAck proto.InternalMessageInfo
+
+func (m *WordAck) GetWord() string {
+ if m != nil && m.Word != nil {
+ return *m.Word
+ }
+ return ""
+}
+
+//*
+// Request: Set U2F counter
+// @start
+// @next Success
+type SetU2FCounter struct {
+ U2FCounter *uint32 `protobuf:"varint,1,opt,name=u2f_counter,json=u2fCounter" json:"u2f_counter,omitempty"`
+ XXX_NoUnkeyedLiteral struct{} `json:"-"`
+ XXX_unrecognized []byte `json:"-"`
+ XXX_sizecache int32 `json:"-"`
+}
+
+func (m *SetU2FCounter) Reset() { *m = SetU2FCounter{} }
+func (m *SetU2FCounter) String() string { return proto.CompactTextString(m) }
+func (*SetU2FCounter) ProtoMessage() {}
+func (*SetU2FCounter) Descriptor() ([]byte, []int) {
+ return fileDescriptor_0c720c20d27aa029, []int{20}
+}
+
+func (m *SetU2FCounter) XXX_Unmarshal(b []byte) error {
+ return xxx_messageInfo_SetU2FCounter.Unmarshal(m, b)
+}
+func (m *SetU2FCounter) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+ return xxx_messageInfo_SetU2FCounter.Marshal(b, m, deterministic)
+}
+func (m *SetU2FCounter) XXX_Merge(src proto.Message) {
+ xxx_messageInfo_SetU2FCounter.Merge(m, src)
+}
+func (m *SetU2FCounter) XXX_Size() int {
+ return xxx_messageInfo_SetU2FCounter.Size(m)
+}
+func (m *SetU2FCounter) XXX_DiscardUnknown() {
+ xxx_messageInfo_SetU2FCounter.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_SetU2FCounter proto.InternalMessageInfo
+
+func (m *SetU2FCounter) GetU2FCounter() uint32 {
+ if m != nil && m.U2FCounter != nil {
+ return *m.U2FCounter
+ }
+ return 0
+}
+
+func init() {
+ proto.RegisterEnum("hw.trezor.messages.management.ApplySettings_PassphraseSourceType", ApplySettings_PassphraseSourceType_name, ApplySettings_PassphraseSourceType_value)
+ proto.RegisterEnum("hw.trezor.messages.management.RecoveryDevice_RecoveryDeviceType", RecoveryDevice_RecoveryDeviceType_name, RecoveryDevice_RecoveryDeviceType_value)
+ proto.RegisterEnum("hw.trezor.messages.management.WordRequest_WordRequestType", WordRequest_WordRequestType_name, WordRequest_WordRequestType_value)
+ proto.RegisterType((*Initialize)(nil), "hw.trezor.messages.management.Initialize")
+ proto.RegisterType((*GetFeatures)(nil), "hw.trezor.messages.management.GetFeatures")
+ proto.RegisterType((*Features)(nil), "hw.trezor.messages.management.Features")
+ proto.RegisterType((*ClearSession)(nil), "hw.trezor.messages.management.ClearSession")
+ proto.RegisterType((*ApplySettings)(nil), "hw.trezor.messages.management.ApplySettings")
+ proto.RegisterType((*ApplyFlags)(nil), "hw.trezor.messages.management.ApplyFlags")
+ proto.RegisterType((*ChangePin)(nil), "hw.trezor.messages.management.ChangePin")
+ proto.RegisterType((*Ping)(nil), "hw.trezor.messages.management.Ping")
+ proto.RegisterType((*Cancel)(nil), "hw.trezor.messages.management.Cancel")
+ proto.RegisterType((*GetEntropy)(nil), "hw.trezor.messages.management.GetEntropy")
+ proto.RegisterType((*Entropy)(nil), "hw.trezor.messages.management.Entropy")
+ proto.RegisterType((*WipeDevice)(nil), "hw.trezor.messages.management.WipeDevice")
+ proto.RegisterType((*LoadDevice)(nil), "hw.trezor.messages.management.LoadDevice")
+ proto.RegisterType((*ResetDevice)(nil), "hw.trezor.messages.management.ResetDevice")
+ proto.RegisterType((*BackupDevice)(nil), "hw.trezor.messages.management.BackupDevice")
+ proto.RegisterType((*EntropyRequest)(nil), "hw.trezor.messages.management.EntropyRequest")
+ proto.RegisterType((*EntropyAck)(nil), "hw.trezor.messages.management.EntropyAck")
+ proto.RegisterType((*RecoveryDevice)(nil), "hw.trezor.messages.management.RecoveryDevice")
+ proto.RegisterType((*WordRequest)(nil), "hw.trezor.messages.management.WordRequest")
+ proto.RegisterType((*WordAck)(nil), "hw.trezor.messages.management.WordAck")
+ proto.RegisterType((*SetU2FCounter)(nil), "hw.trezor.messages.management.SetU2FCounter")
+}
+
+func init() { proto.RegisterFile("messages-management.proto", fileDescriptor_0c720c20d27aa029) }
+
+var fileDescriptor_0c720c20d27aa029 = []byte{
+ // 1393 bytes of a gzipped FileDescriptorProto
+ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x57, 0xdd, 0x6e, 0xdb, 0xc8,
+ 0x15, 0x8e, 0x7e, 0x62, 0x49, 0xc7, 0xfa, 0xcb, 0xd4, 0x8e, 0xe9, 0xb8, 0x6e, 0x1c, 0xba, 0x6e,
+ 0x12, 0x04, 0x15, 0x0a, 0x17, 0x09, 0x90, 0x5c, 0x14, 0x75, 0xec, 0xfc, 0x21, 0x71, 0x6a, 0xd0,
+ 0x6e, 0x02, 0xf4, 0x86, 0x18, 0x91, 0x47, 0xd2, 0xd4, 0xe4, 0x0c, 0xcb, 0x19, 0xda, 0x55, 0x5e,
+ 0x60, 0x6f, 0xf6, 0x45, 0x16, 0xfb, 0x1c, 0x7b, 0xb5, 0xcf, 0xb0, 0xef, 0xb2, 0x98, 0x19, 0x52,
+ 0xa2, 0x65, 0x3b, 0x46, 0x76, 0xef, 0xe6, 0x7c, 0xe7, 0xe3, 0x68, 0xce, 0x77, 0xbe, 0x39, 0x63,
+ 0xc3, 0x7a, 0x8c, 0x52, 0xd2, 0x31, 0xca, 0xbf, 0xc6, 0x94, 0xd3, 0x31, 0xc6, 0xc8, 0xd5, 0x20,
+ 0x49, 0x85, 0x12, 0x64, 0x73, 0x72, 0x3e, 0x50, 0x29, 0x7e, 0x11, 0xe9, 0xa0, 0x20, 0x0d, 0xe6,
+ 0xa4, 0x7b, 0xab, 0xb3, 0x2f, 0x03, 0x11, 0xc7, 0x82, 0xdb, 0xaf, 0xdc, 0xf7, 0x00, 0xef, 0x38,
+ 0x53, 0x8c, 0x46, 0xec, 0x0b, 0x92, 0x15, 0xb8, 0x2d, 0x15, 0x55, 0xe8, 0x54, 0xb6, 0x2a, 0x8f,
+ 0xda, 0x9e, 0x0d, 0xc8, 0x43, 0xe8, 0xc9, 0x53, 0x96, 0xf8, 0x09, 0x95, 0x32, 0x99, 0xa4, 0x54,
+ 0xa2, 0x53, 0xdd, 0xaa, 0x3c, 0x6a, 0x7a, 0x5d, 0x0d, 0x1f, 0xcd, 0x50, 0xb7, 0x03, 0xcb, 0x6f,
+ 0x50, 0xbd, 0x46, 0xaa, 0xb2, 0x14, 0xa5, 0xfb, 0x7d, 0x03, 0x9a, 0x45, 0x40, 0xee, 0xc2, 0xd2,
+ 0x19, 0xf2, 0x50, 0xa4, 0x66, 0xef, 0x96, 0x97, 0x47, 0x64, 0x1b, 0x3a, 0x31, 0xfd, 0xaf, 0x48,
+ 0xfd, 0x33, 0x4c, 0x25, 0x13, 0xdc, 0x6c, 0xdd, 0xf1, 0xda, 0x06, 0xfc, 0x64, 0x31, 0x43, 0x62,
+ 0xbc, 0x44, 0xaa, 0xe5, 0x24, 0x0d, 0x96, 0x48, 0x09, 0x55, 0xc1, 0x64, 0x46, 0xaa, 0x5b, 0x92,
+ 0x01, 0x0b, 0xd2, 0x43, 0xe8, 0x0d, 0x85, 0x50, 0x91, 0xa0, 0x21, 0xa6, 0x7e, 0x2c, 0x42, 0x74,
+ 0x6e, 0xdb, 0x5a, 0xe6, 0xf0, 0xa1, 0x08, 0x91, 0x6c, 0x40, 0x2b, 0xc4, 0x33, 0x16, 0xa0, 0xcf,
+ 0x42, 0x67, 0xc9, 0x1c, 0xb9, 0x69, 0x81, 0x77, 0x21, 0xd9, 0x81, 0x6e, 0xc2, 0xb8, 0xaf, 0x25,
+ 0xc4, 0x40, 0xe9, 0xdf, 0x6a, 0x98, 0x4d, 0x3a, 0x09, 0xe3, 0x47, 0x33, 0x90, 0xfc, 0x1d, 0x56,
+ 0xe7, 0x9a, 0x95, 0xd9, 0x4d, 0xc3, 0x5e, 0x99, 0x27, 0x4b, 0x1f, 0xdd, 0x83, 0x66, 0x44, 0xf9,
+ 0x38, 0xa3, 0x63, 0x74, 0x5a, 0xf6, 0x77, 0x8b, 0x58, 0xf7, 0x27, 0xa2, 0x43, 0x8c, 0x1c, 0x30,
+ 0x09, 0x1b, 0x90, 0x2d, 0x58, 0x66, 0xb3, 0x1e, 0x86, 0x4e, 0xdb, 0x6c, 0x5e, 0x86, 0xf4, 0x9e,
+ 0x29, 0x9e, 0x31, 0xa3, 0x4a, 0xc7, 0xb4, 0x76, 0x16, 0x2f, 0x28, 0x32, 0xa1, 0x72, 0xe2, 0x74,
+ 0x0d, 0xa5, 0xa4, 0xc8, 0x5b, 0x2a, 0x27, 0x7a, 0x13, 0x16, 0x27, 0x22, 0x55, 0x18, 0x3a, 0x3d,
+ 0xf3, 0x1b, 0xb3, 0x98, 0x6c, 0x02, 0x68, 0x41, 0x02, 0x1a, 0x4c, 0x30, 0x74, 0xfa, 0x26, 0xdb,
+ 0x4a, 0x18, 0xdf, 0x37, 0x00, 0x79, 0x02, 0x77, 0x4a, 0x42, 0xe4, 0xac, 0x3b, 0x86, 0xd5, 0x9f,
+ 0x27, 0x72, 0xf2, 0x63, 0xe8, 0x8f, 0x58, 0x1a, 0x9f, 0xd3, 0x54, 0x6b, 0x86, 0x12, 0xb9, 0x72,
+ 0x88, 0xe1, 0xf6, 0x0a, 0xfc, 0xc8, 0xc2, 0xe4, 0x01, 0xb4, 0x39, 0x62, 0x28, 0xfd, 0x21, 0x0d,
+ 0x4e, 0xb3, 0xc4, 0xf9, 0x83, 0x2d, 0xdd, 0x60, 0x2f, 0x0d, 0xa4, 0x25, 0x1b, 0x45, 0x74, 0x2c,
+ 0x9d, 0x15, 0xe3, 0x06, 0x1b, 0x68, 0x54, 0xf7, 0x3e, 0x72, 0x56, 0xad, 0x90, 0x26, 0x20, 0xeb,
+ 0xd0, 0x1c, 0x9d, 0xfb, 0xc6, 0x79, 0xce, 0x5d, 0x43, 0x6f, 0x8c, 0xce, 0x0f, 0x75, 0x58, 0xa4,
+ 0xb4, 0xdf, 0x9c, 0xb5, 0x59, 0x4a, 0x87, 0x79, 0xca, 0xb8, 0xcc, 0x71, 0x8a, 0xd4, 0x91, 0x0e,
+ 0xb5, 0x89, 0x46, 0xe7, 0x7e, 0xee, 0xfb, 0x75, 0xdb, 0xcc, 0xd1, 0xf9, 0x27, 0xeb, 0xfc, 0x3f,
+ 0x43, 0x77, 0x96, 0xf4, 0x4f, 0x71, 0x2a, 0x9d, 0x7b, 0x46, 0xf7, 0x76, 0xc1, 0x78, 0x8f, 0x53,
+ 0xa9, 0xa5, 0xcb, 0xf8, 0x88, 0x71, 0x26, 0x27, 0x18, 0x16, 0x75, 0x6e, 0x58, 0xe9, 0xe6, 0x89,
+ 0xbc, 0xd8, 0x0d, 0x68, 0x71, 0x51, 0x90, 0xfe, 0x68, 0x7b, 0xc4, 0x85, 0x4d, 0xba, 0x5d, 0x68,
+ 0xef, 0x47, 0x48, 0xd3, 0x63, 0x94, 0xba, 0xf1, 0xee, 0x77, 0x35, 0xe8, 0xec, 0x25, 0x49, 0x34,
+ 0x3d, 0x46, 0xa5, 0x18, 0x1f, 0xcb, 0x0b, 0xd6, 0xab, 0x5c, 0x67, 0xbd, 0x6a, 0xd9, 0x7a, 0x3b,
+ 0xd0, 0xcd, 0xb4, 0xb5, 0xe7, 0x93, 0xa1, 0x66, 0x2f, 0x42, 0x26, 0x71, 0x3e, 0x18, 0xc8, 0x9f,
+ 0x00, 0x26, 0x22, 0x46, 0x19, 0xa4, 0x88, 0xf6, 0x5e, 0xb6, 0xbd, 0x12, 0x42, 0xf8, 0x05, 0x7f,
+ 0x48, 0x91, 0xa5, 0x81, 0xbd, 0x97, 0xdd, 0xdd, 0xbd, 0xc1, 0x57, 0xe7, 0xda, 0xe0, 0x42, 0x05,
+ 0x83, 0xf9, 0x6f, 0x1e, 0x9b, 0x4d, 0x4e, 0xa6, 0x09, 0x96, 0x2d, 0x66, 0x51, 0xf2, 0x04, 0x08,
+ 0xcd, 0x94, 0xf0, 0x23, 0x11, 0x9c, 0xfa, 0x21, 0x46, 0x74, 0xea, 0xc7, 0xd2, 0xdc, 0xf2, 0x8e,
+ 0xd7, 0xd3, 0x99, 0x0f, 0x22, 0x38, 0x3d, 0xd0, 0xf8, 0xa1, 0xd4, 0x7e, 0x0c, 0x99, 0x4c, 0x34,
+ 0x29, 0x15, 0x8a, 0xce, 0xae, 0x7b, 0xc7, 0xeb, 0xe5, 0xb8, 0x97, 0xc3, 0xee, 0x53, 0x58, 0xb9,
+ 0xea, 0x04, 0xa4, 0x01, 0xb5, 0xbd, 0xe3, 0xf7, 0xfd, 0x5b, 0x04, 0x60, 0xe9, 0xe0, 0xd5, 0xa7,
+ 0x77, 0xfb, 0xaf, 0xfa, 0x15, 0xd2, 0x84, 0xfa, 0xdb, 0x7f, 0x1d, 0x9f, 0xf4, 0xab, 0xae, 0x0b,
+ 0x60, 0xca, 0x78, 0x5d, 0x78, 0xd3, 0x3a, 0xb6, 0x52, 0x72, 0xac, 0xbb, 0x0d, 0xad, 0xfd, 0x09,
+ 0xe5, 0x63, 0x3c, 0x62, 0x5c, 0x0f, 0xd3, 0x14, 0x63, 0x71, 0x66, 0xdb, 0xd4, 0xf4, 0xf2, 0xc8,
+ 0xfd, 0xa1, 0x02, 0xf5, 0x23, 0xc6, 0xc7, 0xc4, 0x81, 0x46, 0x2e, 0x56, 0xde, 0xc8, 0x22, 0xd4,
+ 0x7e, 0x1a, 0x66, 0x4a, 0x89, 0x0b, 0xd3, 0xcb, 0x8e, 0xf3, 0xbe, 0x4d, 0x94, 0x66, 0xd1, 0xe5,
+ 0x39, 0x57, 0xfb, 0xa6, 0x39, 0x57, 0xbf, 0x7e, 0xce, 0xb9, 0x4d, 0x58, 0xda, 0xa7, 0x3c, 0xc0,
+ 0xc8, 0xdd, 0x02, 0x78, 0x83, 0xea, 0x15, 0x57, 0xa9, 0x48, 0xa6, 0x84, 0x40, 0x5d, 0xb2, 0x2f,
+ 0xfa, 0xdc, 0xd5, 0x47, 0x1d, 0xcf, 0xac, 0xdd, 0x6d, 0x68, 0x14, 0x69, 0x07, 0x1a, 0x68, 0x97,
+ 0x86, 0xd1, 0xf6, 0x8a, 0xd0, 0x6d, 0x03, 0x7c, 0x66, 0x09, 0x1e, 0x98, 0x21, 0xed, 0xfe, 0x58,
+ 0x05, 0xf8, 0x20, 0x68, 0x68, 0x43, 0x6d, 0xed, 0x98, 0x63, 0x2c, 0x38, 0x0b, 0x0a, 0x6b, 0x17,
+ 0x31, 0x79, 0x0e, 0x75, 0xae, 0x1f, 0x02, 0xad, 0xc2, 0xf2, 0xee, 0xce, 0x55, 0x86, 0xcb, 0xdf,
+ 0xcc, 0xb7, 0x07, 0x1f, 0x45, 0x68, 0x4d, 0x65, 0x3e, 0x21, 0x7d, 0xa8, 0x25, 0xcc, 0xaa, 0xd2,
+ 0xf2, 0xf4, 0xf2, 0x37, 0x69, 0x41, 0xb6, 0x4b, 0x17, 0x4f, 0xdb, 0xbe, 0xf5, 0xa2, 0x81, 0x7c,
+ 0x1c, 0x31, 0x39, 0xb9, 0xea, 0x06, 0x2e, 0x95, 0x6f, 0xe0, 0x36, 0x74, 0xcc, 0xe3, 0x1c, 0x4c,
+ 0x30, 0x38, 0x95, 0x59, 0x9c, 0xbf, 0x44, 0x6d, 0x0d, 0xee, 0xe7, 0x18, 0xb9, 0x0f, 0xcb, 0xd9,
+ 0xee, 0xc8, 0x0f, 0x44, 0xc6, 0x15, 0xa6, 0xe6, 0xf9, 0xe9, 0x78, 0x90, 0xed, 0x8e, 0xf6, 0x2d,
+ 0xe2, 0xfe, 0x5c, 0x85, 0x65, 0x0f, 0x25, 0xaa, 0x5c, 0xae, 0x1d, 0xe8, 0xce, 0x3c, 0x4f, 0x79,
+ 0x28, 0xe2, 0xdc, 0x68, 0x9d, 0xc2, 0xf1, 0x06, 0x24, 0xf7, 0xa1, 0x29, 0x55, 0x8a, 0x7c, 0xac,
+ 0x26, 0xf6, 0xdd, 0x7e, 0x51, 0xdb, 0x7d, 0xfa, 0xcc, 0x9b, 0x81, 0xd7, 0xab, 0x51, 0xfb, 0x8a,
+ 0x1a, 0x97, 0x5d, 0x57, 0xbf, 0xca, 0x75, 0xbf, 0x43, 0xb4, 0x05, 0x3d, 0x1a, 0x8b, 0x7a, 0x68,
+ 0x82, 0x51, 0x35, 0x1f, 0xa5, 0xf6, 0xbd, 0x06, 0x0d, 0x5d, 0x35, 0x69, 0x5b, 0x97, 0x27, 0xad,
+ 0x5d, 0xe5, 0x5e, 0xec, 0x43, 0x37, 0xb7, 0xaf, 0x87, 0xff, 0xcb, 0x50, 0x2a, 0xf7, 0x2f, 0x00,
+ 0x39, 0xb2, 0x17, 0x9c, 0x5e, 0xf4, 0x74, 0xa5, 0xec, 0xe9, 0x5f, 0x6a, 0xd0, 0xf5, 0x30, 0x10,
+ 0x67, 0x98, 0x4e, 0xf3, 0xd6, 0x6c, 0x02, 0x9c, 0x8b, 0x34, 0xb4, 0x87, 0xcf, 0x67, 0x44, 0x4b,
+ 0x23, 0xe6, 0xec, 0xd7, 0x2b, 0x5e, 0xfd, 0x26, 0xc5, 0x6b, 0x37, 0x29, 0x5e, 0xbf, 0x51, 0xf1,
+ 0xdb, 0x65, 0xc5, 0x1f, 0x43, 0x1f, 0xf9, 0x48, 0xa4, 0x01, 0xfa, 0xfa, 0xac, 0x11, 0x93, 0xca,
+ 0xb4, 0xa4, 0xe9, 0xf5, 0x72, 0xfc, 0x73, 0x0e, 0x93, 0x13, 0xa8, 0xab, 0x69, 0x82, 0x46, 0xf4,
+ 0xee, 0xee, 0x3f, 0x6f, 0x98, 0xff, 0x17, 0xd5, 0x59, 0x08, 0xed, 0x4d, 0xd5, 0xbb, 0x2d, 0xb6,
+ 0xbc, 0x75, 0xa9, 0xe5, 0x6b, 0xd0, 0x08, 0xd3, 0xa9, 0x9f, 0x66, 0xdc, 0xfc, 0x75, 0xd5, 0xf4,
+ 0x96, 0xc2, 0x74, 0xea, 0x65, 0xdc, 0xfd, 0x0f, 0x90, 0xcb, 0xbb, 0x92, 0x1d, 0x78, 0x70, 0x19,
+ 0xf5, 0x8f, 0x83, 0x94, 0xc6, 0xc3, 0x08, 0x43, 0x5d, 0x8d, 0xec, 0xdf, 0x22, 0x9b, 0xb0, 0x7e,
+ 0x05, 0xed, 0x90, 0xaa, 0x94, 0xfd, 0xbf, 0x5f, 0x71, 0x7f, 0xaa, 0xc0, 0xb2, 0xa6, 0xe6, 0xbe,
+ 0x20, 0x1f, 0xf3, 0xda, 0x2b, 0xa6, 0xf6, 0x17, 0x37, 0xd4, 0x5e, 0xfa, 0xb2, 0xbc, 0x9e, 0x57,
+ 0xed, 0x8e, 0xa0, 0xb7, 0x90, 0x20, 0xeb, 0xb0, 0xba, 0x00, 0xf9, 0x47, 0x11, 0x65, 0xbc, 0x7f,
+ 0x8b, 0x6c, 0xc0, 0xda, 0x62, 0xca, 0x9e, 0xf4, 0x79, 0xbf, 0x72, 0x7d, 0xf2, 0x59, 0xbf, 0xea,
+ 0x6e, 0x42, 0x43, 0x27, 0xb5, 0x99, 0x09, 0xd4, 0x75, 0x87, 0xcd, 0x74, 0x6e, 0x79, 0x66, 0xed,
+ 0xfe, 0x0d, 0x3a, 0xc7, 0xa8, 0xfe, 0xbd, 0xfb, 0xba, 0x74, 0xbf, 0xca, 0xdd, 0xa8, 0x2c, 0x76,
+ 0xe3, 0xe5, 0x3f, 0x60, 0x3b, 0x10, 0xf1, 0x40, 0x52, 0x25, 0xe4, 0x84, 0x45, 0x74, 0x28, 0x0b,
+ 0x21, 0x22, 0x36, 0xb4, 0xff, 0xbb, 0x0c, 0xb3, 0xd1, 0xcb, 0xb5, 0x13, 0x03, 0x1e, 0x5a, 0x71,
+ 0x0e, 0x67, 0xd2, 0xfc, 0x1a, 0x00, 0x00, 0xff, 0xff, 0xd7, 0x6e, 0xfc, 0x59, 0x29, 0x0d, 0x00,
+ 0x00,
+}
diff --git a/accounts/usbwallet/trezor/messages-management.proto b/accounts/usbwallet/trezor/messages-management.proto
new file mode 100644
index 0000000..0ab825a
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages-management.proto
@@ -0,0 +1,289 @@
+// This file originates from the SatoshiLabs Trezor `common` repository at:
+// https://github.com/trezor/trezor-common/blob/master/protob/messages-management.proto
+// dated 28.05.2019, commit 893fd219d4a01bcffa0cd9cfa631856371ec5aa9.
+
+syntax = "proto2";
+package hw.trezor.messages.management;
+
+// Sugar for easier handling in Java
+option java_package = "com.satoshilabs.trezor.lib.protobuf";
+option java_outer_classname = "TrezorMessageManagement";
+
+import "messages-common.proto";
+
+/**
+ * Request: Reset device to default state and ask for device details
+ * @start
+ * @next Features
+ */
+message Initialize {
+ optional bytes state = 1; // assumed device state, clear session if set and different
+ optional bool skip_passphrase = 2; // this session should always assume empty passphrase
+}
+
+/**
+ * Request: Ask for device details (no device reset)
+ * @start
+ * @next Features
+ */
+message GetFeatures {
+}
+
+/**
+ * Response: Reports various information about the device
+ * @end
+ */
+message Features {
+ optional string vendor = 1; // name of the manufacturer, e.g. "trezor.io"
+ optional uint32 major_version = 2; // major version of the firmware/bootloader, e.g. 1
+ optional uint32 minor_version = 3; // minor version of the firmware/bootloader, e.g. 0
+ optional uint32 patch_version = 4; // patch version of the firmware/bootloader, e.g. 0
+ optional bool bootloader_mode = 5; // is device in bootloader mode?
+ optional string device_id = 6; // device's unique identifier
+ optional bool pin_protection = 7; // is device protected by PIN?
+ optional bool passphrase_protection = 8; // is node/mnemonic encrypted using passphrase?
+ optional string language = 9; // device language
+ optional string label = 10; // device description label
+ optional bool initialized = 12; // does device contain seed?
+ optional bytes revision = 13; // SCM revision of firmware
+ optional bytes bootloader_hash = 14; // hash of the bootloader
+ optional bool imported = 15; // was storage imported from an external source?
+ optional bool pin_cached = 16; // is PIN already cached in session?
+ optional bool passphrase_cached = 17; // is passphrase already cached in session?
+ optional bool firmware_present = 18; // is valid firmware loaded?
+ optional bool needs_backup = 19; // does storage need backup? (equals to Storage.needs_backup)
+ optional uint32 flags = 20; // device flags (equals to Storage.flags)
+ optional string model = 21; // device hardware model
+ optional uint32 fw_major = 22; // reported firmware version if in bootloader mode
+ optional uint32 fw_minor = 23; // reported firmware version if in bootloader mode
+ optional uint32 fw_patch = 24; // reported firmware version if in bootloader mode
+ optional string fw_vendor = 25; // reported firmware vendor if in bootloader mode
+ optional bytes fw_vendor_keys = 26; // reported firmware vendor keys (their hash)
+ optional bool unfinished_backup = 27; // report unfinished backup (equals to Storage.unfinished_backup)
+ optional bool no_backup = 28; // report no backup (equals to Storage.no_backup)
+}
+
+/**
+ * Request: clear session (removes cached PIN, passphrase, etc).
+ * @start
+ * @next Success
+ */
+message ClearSession {
+}
+
+/**
+ * Request: change language and/or label of the device
+ * @start
+ * @next Success
+ * @next Failure
+ */
+message ApplySettings {
+ optional string language = 1;
+ optional string label = 2;
+ optional bool use_passphrase = 3;
+ optional bytes homescreen = 4;
+ optional PassphraseSourceType passphrase_source = 5;
+ optional uint32 auto_lock_delay_ms = 6;
+ optional uint32 display_rotation = 7; // in degrees from North
+ /**
+ * Structure representing passphrase source
+ */
+ enum PassphraseSourceType {
+ ASK = 0;
+ DEVICE = 1;
+ HOST = 2;
+ }
+}
+
+/**
+ * Request: set flags of the device
+ * @start
+ * @next Success
+ * @next Failure
+ */
+message ApplyFlags {
+ optional uint32 flags = 1; // bitmask, can only set bits, not unset
+}
+
+/**
+ * Request: Starts workflow for setting/changing/removing the PIN
+ * @start
+ * @next Success
+ * @next Failure
+ */
+message ChangePin {
+ optional bool remove = 1; // is PIN removal requested?
+}
+
+/**
+ * Request: Test if the device is alive, device sends back the message in Success response
+ * @start
+ * @next Success
+ */
+message Ping {
+ optional string message = 1; // message to send back in Success message
+ optional bool button_protection = 2; // ask for button press
+ optional bool pin_protection = 3; // ask for PIN if set in device
+ optional bool passphrase_protection = 4; // ask for passphrase if set in device
+}
+
+/**
+ * Request: Abort last operation that required user interaction
+ * @start
+ * @next Failure
+ */
+message Cancel {
+}
+
+/**
+ * Request: Request a sample of random data generated by hardware RNG. May be used for testing.
+ * @start
+ * @next Entropy
+ * @next Failure
+ */
+message GetEntropy {
+ required uint32 size = 1; // size of requested entropy
+}
+
+/**
+ * Response: Reply with random data generated by internal RNG
+ * @end
+ */
+message Entropy {
+ required bytes entropy = 1; // chunk of random generated bytes
+}
+
+/**
+ * Request: Request device to wipe all sensitive data and settings
+ * @start
+ * @next Success
+ * @next Failure
+ */
+message WipeDevice {
+}
+
+/**
+ * Request: Load seed and related internal settings from the computer
+ * @start
+ * @next Success
+ * @next Failure
+ */
+message LoadDevice {
+ optional string mnemonic = 1; // seed encoded as BIP-39 mnemonic (12, 18 or 24 words)
+ optional hw.trezor.messages.common.HDNodeType node = 2; // BIP-32 node
+ optional string pin = 3; // set PIN protection
+ optional bool passphrase_protection = 4; // enable master node encryption using passphrase
+ optional string language = 5 [default='english']; // device language
+ optional string label = 6; // device label
+ optional bool skip_checksum = 7; // do not test mnemonic for valid BIP-39 checksum
+ optional uint32 u2f_counter = 8; // U2F counter
+}
+
+/**
+ * Request: Ask device to do initialization involving user interaction
+ * @start
+ * @next EntropyRequest
+ * @next Failure
+ */
+message ResetDevice {
+ optional bool display_random = 1; // display entropy generated by the device before asking for additional entropy
+ optional uint32 strength = 2 [default=256]; // strength of seed in bits
+ optional bool passphrase_protection = 3; // enable master node encryption using passphrase
+ optional bool pin_protection = 4; // enable PIN protection
+ optional string language = 5 [default='english']; // device language
+ optional string label = 6; // device label
+ optional uint32 u2f_counter = 7; // U2F counter
+ optional bool skip_backup = 8; // postpone seed backup to BackupDevice workflow
+ optional bool no_backup = 9; // indicate that no backup is going to be made
+}
+
+/**
+ * Request: Perform backup of the device seed if not backed up using ResetDevice
+ * @start
+ * @next Success
+ */
+message BackupDevice {
+}
+
+/**
+ * Response: Ask for additional entropy from host computer
+ * @next EntropyAck
+ */
+message EntropyRequest {
+}
+
+/**
+ * Request: Provide additional entropy for seed generation function
+ * @next Success
+ */
+message EntropyAck {
+ optional bytes entropy = 1; // 256 bits (32 bytes) of random data
+}
+
+/**
+ * Request: Start recovery workflow asking user for specific words of mnemonic
+ * Used to recovery device safely even on untrusted computer.
+ * @start
+ * @next WordRequest
+ */
+message RecoveryDevice {
+ optional uint32 word_count = 1; // number of words in BIP-39 mnemonic
+ optional bool passphrase_protection = 2; // enable master node encryption using passphrase
+ optional bool pin_protection = 3; // enable PIN protection
+ optional string language = 4 [default='english']; // device language
+ optional string label = 5; // device label
+ optional bool enforce_wordlist = 6; // enforce BIP-39 wordlist during the process
+ // 7 reserved for unused recovery method
+ optional RecoveryDeviceType type = 8; // supported recovery type
+ optional uint32 u2f_counter = 9; // U2F counter
+ optional bool dry_run = 10; // perform dry-run recovery workflow (for safe mnemonic validation)
+ /**
+ * Type of recovery procedure. These should be used as bitmask, e.g.,
+ * `RecoveryDeviceType_ScrambledWords | RecoveryDeviceType_Matrix`
+ * listing every method supported by the host computer.
+ *
+ * Note that ScrambledWords must be supported by every implementation
+ * for backward compatibility; there is no way to not support it.
+ */
+ enum RecoveryDeviceType {
+ // use powers of two when extending this field
+ RecoveryDeviceType_ScrambledWords = 0; // words in scrambled order
+ RecoveryDeviceType_Matrix = 1; // matrix recovery type
+ }
+}
+
+/**
+ * Response: Device is waiting for user to enter word of the mnemonic
+ * Its position is shown only on device's internal display.
+ * @next WordAck
+ */
+message WordRequest {
+ optional WordRequestType type = 1;
+ /**
+ * Type of Recovery Word request
+ */
+ enum WordRequestType {
+ WordRequestType_Plain = 0;
+ WordRequestType_Matrix9 = 1;
+ WordRequestType_Matrix6 = 2;
+ }
+}
+
+/**
+ * Request: Computer replies with word from the mnemonic
+ * @next WordRequest
+ * @next Success
+ * @next Failure
+ */
+message WordAck {
+ required string word = 1; // one word of mnemonic on asked position
+}
+
+/**
+ * Request: Set U2F counter
+ * @start
+ * @next Success
+ */
+message SetU2FCounter {
+ optional uint32 u2f_counter = 1; // counter
+}
diff --git a/accounts/usbwallet/trezor/messages.pb.go b/accounts/usbwallet/trezor/messages.pb.go
new file mode 100644
index 0000000..6278bd8
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages.pb.go
@@ -0,0 +1,889 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: messages.proto
+
+package trezor
+
+import (
+ fmt "fmt"
+ math "math"
+
+ proto "github.com/golang/protobuf/proto"
+ descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+//*
+// Mapping between TREZOR wire identifier (uint) and a protobuf message
+type MessageType int32
+
+const (
+ // Management
+ MessageType_MessageType_Initialize MessageType = 0
+ MessageType_MessageType_Ping MessageType = 1
+ MessageType_MessageType_Success MessageType = 2
+ MessageType_MessageType_Failure MessageType = 3
+ MessageType_MessageType_ChangePin MessageType = 4
+ MessageType_MessageType_WipeDevice MessageType = 5
+ MessageType_MessageType_GetEntropy MessageType = 9
+ MessageType_MessageType_Entropy MessageType = 10
+ MessageType_MessageType_LoadDevice MessageType = 13
+ MessageType_MessageType_ResetDevice MessageType = 14
+ MessageType_MessageType_Features MessageType = 17
+ MessageType_MessageType_PinMatrixRequest MessageType = 18
+ MessageType_MessageType_PinMatrixAck MessageType = 19
+ MessageType_MessageType_Cancel MessageType = 20
+ MessageType_MessageType_ClearSession MessageType = 24
+ MessageType_MessageType_ApplySettings MessageType = 25
+ MessageType_MessageType_ButtonRequest MessageType = 26
+ MessageType_MessageType_ButtonAck MessageType = 27
+ MessageType_MessageType_ApplyFlags MessageType = 28
+ MessageType_MessageType_BackupDevice MessageType = 34
+ MessageType_MessageType_EntropyRequest MessageType = 35
+ MessageType_MessageType_EntropyAck MessageType = 36
+ MessageType_MessageType_PassphraseRequest MessageType = 41
+ MessageType_MessageType_PassphraseAck MessageType = 42
+ MessageType_MessageType_PassphraseStateRequest MessageType = 77
+ MessageType_MessageType_PassphraseStateAck MessageType = 78
+ MessageType_MessageType_RecoveryDevice MessageType = 45
+ MessageType_MessageType_WordRequest MessageType = 46
+ MessageType_MessageType_WordAck MessageType = 47
+ MessageType_MessageType_GetFeatures MessageType = 55
+ MessageType_MessageType_SetU2FCounter MessageType = 63
+ // Bootloader
+ MessageType_MessageType_FirmwareErase MessageType = 6
+ MessageType_MessageType_FirmwareUpload MessageType = 7
+ MessageType_MessageType_FirmwareRequest MessageType = 8
+ MessageType_MessageType_SelfTest MessageType = 32
+ // Bitcoin
+ MessageType_MessageType_GetPublicKey MessageType = 11
+ MessageType_MessageType_PublicKey MessageType = 12
+ MessageType_MessageType_SignTx MessageType = 15
+ MessageType_MessageType_TxRequest MessageType = 21
+ MessageType_MessageType_TxAck MessageType = 22
+ MessageType_MessageType_GetAddress MessageType = 29
+ MessageType_MessageType_Address MessageType = 30
+ MessageType_MessageType_SignMessage MessageType = 38
+ MessageType_MessageType_VerifyMessage MessageType = 39
+ MessageType_MessageType_MessageSignature MessageType = 40
+ // Crypto
+ MessageType_MessageType_CipherKeyValue MessageType = 23
+ MessageType_MessageType_CipheredKeyValue MessageType = 48
+ MessageType_MessageType_SignIdentity MessageType = 53
+ MessageType_MessageType_SignedIdentity MessageType = 54
+ MessageType_MessageType_GetECDHSessionKey MessageType = 61
+ MessageType_MessageType_ECDHSessionKey MessageType = 62
+ MessageType_MessageType_CosiCommit MessageType = 71
+ MessageType_MessageType_CosiCommitment MessageType = 72
+ MessageType_MessageType_CosiSign MessageType = 73
+ MessageType_MessageType_CosiSignature MessageType = 74
+ // Debug
+ MessageType_MessageType_DebugLinkDecision MessageType = 100
+ MessageType_MessageType_DebugLinkGetState MessageType = 101
+ MessageType_MessageType_DebugLinkState MessageType = 102
+ MessageType_MessageType_DebugLinkStop MessageType = 103
+ MessageType_MessageType_DebugLinkLog MessageType = 104
+ MessageType_MessageType_DebugLinkMemoryRead MessageType = 110
+ MessageType_MessageType_DebugLinkMemory MessageType = 111
+ MessageType_MessageType_DebugLinkMemoryWrite MessageType = 112
+ MessageType_MessageType_DebugLinkFlashErase MessageType = 113
+ // Ethereum
+ MessageType_MessageType_EthereumGetPublicKey MessageType = 450
+ MessageType_MessageType_EthereumPublicKey MessageType = 451
+ MessageType_MessageType_EthereumGetAddress MessageType = 56
+ MessageType_MessageType_EthereumAddress MessageType = 57
+ MessageType_MessageType_EthereumSignTx MessageType = 58
+ MessageType_MessageType_EthereumTxRequest MessageType = 59
+ MessageType_MessageType_EthereumTxAck MessageType = 60
+ MessageType_MessageType_EthereumSignMessage MessageType = 64
+ MessageType_MessageType_EthereumVerifyMessage MessageType = 65
+ MessageType_MessageType_EthereumMessageSignature MessageType = 66
+ // NEM
+ MessageType_MessageType_NEMGetAddress MessageType = 67
+ MessageType_MessageType_NEMAddress MessageType = 68
+ MessageType_MessageType_NEMSignTx MessageType = 69
+ MessageType_MessageType_NEMSignedTx MessageType = 70
+ MessageType_MessageType_NEMDecryptMessage MessageType = 75
+ MessageType_MessageType_NEMDecryptedMessage MessageType = 76
+ // Lisk
+ MessageType_MessageType_LiskGetAddress MessageType = 114
+ MessageType_MessageType_LiskAddress MessageType = 115
+ MessageType_MessageType_LiskSignTx MessageType = 116
+ MessageType_MessageType_LiskSignedTx MessageType = 117
+ MessageType_MessageType_LiskSignMessage MessageType = 118
+ MessageType_MessageType_LiskMessageSignature MessageType = 119
+ MessageType_MessageType_LiskVerifyMessage MessageType = 120
+ MessageType_MessageType_LiskGetPublicKey MessageType = 121
+ MessageType_MessageType_LiskPublicKey MessageType = 122
+ // Tezos
+ MessageType_MessageType_TezosGetAddress MessageType = 150
+ MessageType_MessageType_TezosAddress MessageType = 151
+ MessageType_MessageType_TezosSignTx MessageType = 152
+ MessageType_MessageType_TezosSignedTx MessageType = 153
+ MessageType_MessageType_TezosGetPublicKey MessageType = 154
+ MessageType_MessageType_TezosPublicKey MessageType = 155
+ // Stellar
+ MessageType_MessageType_StellarSignTx MessageType = 202
+ MessageType_MessageType_StellarTxOpRequest MessageType = 203
+ MessageType_MessageType_StellarGetAddress MessageType = 207
+ MessageType_MessageType_StellarAddress MessageType = 208
+ MessageType_MessageType_StellarCreateAccountOp MessageType = 210
+ MessageType_MessageType_StellarPaymentOp MessageType = 211
+ MessageType_MessageType_StellarPathPaymentOp MessageType = 212
+ MessageType_MessageType_StellarManageOfferOp MessageType = 213
+ MessageType_MessageType_StellarCreatePassiveOfferOp MessageType = 214
+ MessageType_MessageType_StellarSetOptionsOp MessageType = 215
+ MessageType_MessageType_StellarChangeTrustOp MessageType = 216
+ MessageType_MessageType_StellarAllowTrustOp MessageType = 217
+ MessageType_MessageType_StellarAccountMergeOp MessageType = 218
+ // omitted: StellarInflationOp is not a supported operation, would be 219
+ MessageType_MessageType_StellarManageDataOp MessageType = 220
+ MessageType_MessageType_StellarBumpSequenceOp MessageType = 221
+ MessageType_MessageType_StellarSignedTx MessageType = 230
+ // TRON
+ MessageType_MessageType_TronGetAddress MessageType = 250
+ MessageType_MessageType_TronAddress MessageType = 251
+ MessageType_MessageType_TronSignTx MessageType = 252
+ MessageType_MessageType_TronSignedTx MessageType = 253
+ // Cardano
+ // dropped Sign/VerifyMessage ids 300-302
+ MessageType_MessageType_CardanoSignTx MessageType = 303
+ MessageType_MessageType_CardanoTxRequest MessageType = 304
+ MessageType_MessageType_CardanoGetPublicKey MessageType = 305
+ MessageType_MessageType_CardanoPublicKey MessageType = 306
+ MessageType_MessageType_CardanoGetAddress MessageType = 307
+ MessageType_MessageType_CardanoAddress MessageType = 308
+ MessageType_MessageType_CardanoTxAck MessageType = 309
+ MessageType_MessageType_CardanoSignedTx MessageType = 310
+ // Ontology
+ MessageType_MessageType_OntologyGetAddress MessageType = 350
+ MessageType_MessageType_OntologyAddress MessageType = 351
+ MessageType_MessageType_OntologyGetPublicKey MessageType = 352
+ MessageType_MessageType_OntologyPublicKey MessageType = 353
+ MessageType_MessageType_OntologySignTransfer MessageType = 354
+ MessageType_MessageType_OntologySignedTransfer MessageType = 355
+ MessageType_MessageType_OntologySignWithdrawOng MessageType = 356
+ MessageType_MessageType_OntologySignedWithdrawOng MessageType = 357
+ MessageType_MessageType_OntologySignOntIdRegister MessageType = 358
+ MessageType_MessageType_OntologySignedOntIdRegister MessageType = 359
+ MessageType_MessageType_OntologySignOntIdAddAttributes MessageType = 360
+ MessageType_MessageType_OntologySignedOntIdAddAttributes MessageType = 361
+ // Ripple
+ MessageType_MessageType_RippleGetAddress MessageType = 400
+ MessageType_MessageType_RippleAddress MessageType = 401
+ MessageType_MessageType_RippleSignTx MessageType = 402
+ MessageType_MessageType_RippleSignedTx MessageType = 403
+ // Monero
+ MessageType_MessageType_MoneroTransactionInitRequest MessageType = 501
+ MessageType_MessageType_MoneroTransactionInitAck MessageType = 502
+ MessageType_MessageType_MoneroTransactionSetInputRequest MessageType = 503
+ MessageType_MessageType_MoneroTransactionSetInputAck MessageType = 504
+ MessageType_MessageType_MoneroTransactionInputsPermutationRequest MessageType = 505
+ MessageType_MessageType_MoneroTransactionInputsPermutationAck MessageType = 506
+ MessageType_MessageType_MoneroTransactionInputViniRequest MessageType = 507
+ MessageType_MessageType_MoneroTransactionInputViniAck MessageType = 508
+ MessageType_MessageType_MoneroTransactionAllInputsSetRequest MessageType = 509
+ MessageType_MessageType_MoneroTransactionAllInputsSetAck MessageType = 510
+ MessageType_MessageType_MoneroTransactionSetOutputRequest MessageType = 511
+ MessageType_MessageType_MoneroTransactionSetOutputAck MessageType = 512
+ MessageType_MessageType_MoneroTransactionAllOutSetRequest MessageType = 513
+ MessageType_MessageType_MoneroTransactionAllOutSetAck MessageType = 514
+ MessageType_MessageType_MoneroTransactionSignInputRequest MessageType = 515
+ MessageType_MessageType_MoneroTransactionSignInputAck MessageType = 516
+ MessageType_MessageType_MoneroTransactionFinalRequest MessageType = 517
+ MessageType_MessageType_MoneroTransactionFinalAck MessageType = 518
+ MessageType_MessageType_MoneroKeyImageExportInitRequest MessageType = 530
+ MessageType_MessageType_MoneroKeyImageExportInitAck MessageType = 531
+ MessageType_MessageType_MoneroKeyImageSyncStepRequest MessageType = 532
+ MessageType_MessageType_MoneroKeyImageSyncStepAck MessageType = 533
+ MessageType_MessageType_MoneroKeyImageSyncFinalRequest MessageType = 534
+ MessageType_MessageType_MoneroKeyImageSyncFinalAck MessageType = 535
+ MessageType_MessageType_MoneroGetAddress MessageType = 540
+ MessageType_MessageType_MoneroAddress MessageType = 541
+ MessageType_MessageType_MoneroGetWatchKey MessageType = 542
+ MessageType_MessageType_MoneroWatchKey MessageType = 543
+ MessageType_MessageType_DebugMoneroDiagRequest MessageType = 546
+ MessageType_MessageType_DebugMoneroDiagAck MessageType = 547
+ MessageType_MessageType_MoneroGetTxKeyRequest MessageType = 550
+ MessageType_MessageType_MoneroGetTxKeyAck MessageType = 551
+ MessageType_MessageType_MoneroLiveRefreshStartRequest MessageType = 552
+ MessageType_MessageType_MoneroLiveRefreshStartAck MessageType = 553
+ MessageType_MessageType_MoneroLiveRefreshStepRequest MessageType = 554
+ MessageType_MessageType_MoneroLiveRefreshStepAck MessageType = 555
+ MessageType_MessageType_MoneroLiveRefreshFinalRequest MessageType = 556
+ MessageType_MessageType_MoneroLiveRefreshFinalAck MessageType = 557
+ // EOS
+ MessageType_MessageType_EosGetPublicKey MessageType = 600
+ MessageType_MessageType_EosPublicKey MessageType = 601
+ MessageType_MessageType_EosSignTx MessageType = 602
+ MessageType_MessageType_EosTxActionRequest MessageType = 603
+ MessageType_MessageType_EosTxActionAck MessageType = 604
+ MessageType_MessageType_EosSignedTx MessageType = 605
+ // Binance
+ MessageType_MessageType_BinanceGetAddress MessageType = 700
+ MessageType_MessageType_BinanceAddress MessageType = 701
+ MessageType_MessageType_BinanceGetPublicKey MessageType = 702
+ MessageType_MessageType_BinancePublicKey MessageType = 703
+ MessageType_MessageType_BinanceSignTx MessageType = 704
+ MessageType_MessageType_BinanceTxRequest MessageType = 705
+ MessageType_MessageType_BinanceTransferMsg MessageType = 706
+ MessageType_MessageType_BinanceOrderMsg MessageType = 707
+ MessageType_MessageType_BinanceCancelMsg MessageType = 708
+ MessageType_MessageType_BinanceSignedTx MessageType = 709
+)
+
+var MessageType_name = map[int32]string{
+ 0: "MessageType_Initialize",
+ 1: "MessageType_Ping",
+ 2: "MessageType_Success",
+ 3: "MessageType_Failure",
+ 4: "MessageType_ChangePin",
+ 5: "MessageType_WipeDevice",
+ 9: "MessageType_GetEntropy",
+ 10: "MessageType_Entropy",
+ 13: "MessageType_LoadDevice",
+ 14: "MessageType_ResetDevice",
+ 17: "MessageType_Features",
+ 18: "MessageType_PinMatrixRequest",
+ 19: "MessageType_PinMatrixAck",
+ 20: "MessageType_Cancel",
+ 24: "MessageType_ClearSession",
+ 25: "MessageType_ApplySettings",
+ 26: "MessageType_ButtonRequest",
+ 27: "MessageType_ButtonAck",
+ 28: "MessageType_ApplyFlags",
+ 34: "MessageType_BackupDevice",
+ 35: "MessageType_EntropyRequest",
+ 36: "MessageType_EntropyAck",
+ 41: "MessageType_PassphraseRequest",
+ 42: "MessageType_PassphraseAck",
+ 77: "MessageType_PassphraseStateRequest",
+ 78: "MessageType_PassphraseStateAck",
+ 45: "MessageType_RecoveryDevice",
+ 46: "MessageType_WordRequest",
+ 47: "MessageType_WordAck",
+ 55: "MessageType_GetFeatures",
+ 63: "MessageType_SetU2FCounter",
+ 6: "MessageType_FirmwareErase",
+ 7: "MessageType_FirmwareUpload",
+ 8: "MessageType_FirmwareRequest",
+ 32: "MessageType_SelfTest",
+ 11: "MessageType_GetPublicKey",
+ 12: "MessageType_PublicKey",
+ 15: "MessageType_SignTx",
+ 21: "MessageType_TxRequest",
+ 22: "MessageType_TxAck",
+ 29: "MessageType_GetAddress",
+ 30: "MessageType_Address",
+ 38: "MessageType_SignMessage",
+ 39: "MessageType_VerifyMessage",
+ 40: "MessageType_MessageSignature",
+ 23: "MessageType_CipherKeyValue",
+ 48: "MessageType_CipheredKeyValue",
+ 53: "MessageType_SignIdentity",
+ 54: "MessageType_SignedIdentity",
+ 61: "MessageType_GetECDHSessionKey",
+ 62: "MessageType_ECDHSessionKey",
+ 71: "MessageType_CosiCommit",
+ 72: "MessageType_CosiCommitment",
+ 73: "MessageType_CosiSign",
+ 74: "MessageType_CosiSignature",
+ 100: "MessageType_DebugLinkDecision",
+ 101: "MessageType_DebugLinkGetState",
+ 102: "MessageType_DebugLinkState",
+ 103: "MessageType_DebugLinkStop",
+ 104: "MessageType_DebugLinkLog",
+ 110: "MessageType_DebugLinkMemoryRead",
+ 111: "MessageType_DebugLinkMemory",
+ 112: "MessageType_DebugLinkMemoryWrite",
+ 113: "MessageType_DebugLinkFlashErase",
+ 450: "MessageType_EthereumGetPublicKey",
+ 451: "MessageType_EthereumPublicKey",
+ 56: "MessageType_EthereumGetAddress",
+ 57: "MessageType_EthereumAddress",
+ 58: "MessageType_EthereumSignTx",
+ 59: "MessageType_EthereumTxRequest",
+ 60: "MessageType_EthereumTxAck",
+ 64: "MessageType_EthereumSignMessage",
+ 65: "MessageType_EthereumVerifyMessage",
+ 66: "MessageType_EthereumMessageSignature",
+ 67: "MessageType_NEMGetAddress",
+ 68: "MessageType_NEMAddress",
+ 69: "MessageType_NEMSignTx",
+ 70: "MessageType_NEMSignedTx",
+ 75: "MessageType_NEMDecryptMessage",
+ 76: "MessageType_NEMDecryptedMessage",
+ 114: "MessageType_LiskGetAddress",
+ 115: "MessageType_LiskAddress",
+ 116: "MessageType_LiskSignTx",
+ 117: "MessageType_LiskSignedTx",
+ 118: "MessageType_LiskSignMessage",
+ 119: "MessageType_LiskMessageSignature",
+ 120: "MessageType_LiskVerifyMessage",
+ 121: "MessageType_LiskGetPublicKey",
+ 122: "MessageType_LiskPublicKey",
+ 150: "MessageType_TezosGetAddress",
+ 151: "MessageType_TezosAddress",
+ 152: "MessageType_TezosSignTx",
+ 153: "MessageType_TezosSignedTx",
+ 154: "MessageType_TezosGetPublicKey",
+ 155: "MessageType_TezosPublicKey",
+ 202: "MessageType_StellarSignTx",
+ 203: "MessageType_StellarTxOpRequest",
+ 207: "MessageType_StellarGetAddress",
+ 208: "MessageType_StellarAddress",
+ 210: "MessageType_StellarCreateAccountOp",
+ 211: "MessageType_StellarPaymentOp",
+ 212: "MessageType_StellarPathPaymentOp",
+ 213: "MessageType_StellarManageOfferOp",
+ 214: "MessageType_StellarCreatePassiveOfferOp",
+ 215: "MessageType_StellarSetOptionsOp",
+ 216: "MessageType_StellarChangeTrustOp",
+ 217: "MessageType_StellarAllowTrustOp",
+ 218: "MessageType_StellarAccountMergeOp",
+ 220: "MessageType_StellarManageDataOp",
+ 221: "MessageType_StellarBumpSequenceOp",
+ 230: "MessageType_StellarSignedTx",
+ 250: "MessageType_TronGetAddress",
+ 251: "MessageType_TronAddress",
+ 252: "MessageType_TronSignTx",
+ 253: "MessageType_TronSignedTx",
+ 303: "MessageType_CardanoSignTx",
+ 304: "MessageType_CardanoTxRequest",
+ 305: "MessageType_CardanoGetPublicKey",
+ 306: "MessageType_CardanoPublicKey",
+ 307: "MessageType_CardanoGetAddress",
+ 308: "MessageType_CardanoAddress",
+ 309: "MessageType_CardanoTxAck",
+ 310: "MessageType_CardanoSignedTx",
+ 350: "MessageType_OntologyGetAddress",
+ 351: "MessageType_OntologyAddress",
+ 352: "MessageType_OntologyGetPublicKey",
+ 353: "MessageType_OntologyPublicKey",
+ 354: "MessageType_OntologySignTransfer",
+ 355: "MessageType_OntologySignedTransfer",
+ 356: "MessageType_OntologySignWithdrawOng",
+ 357: "MessageType_OntologySignedWithdrawOng",
+ 358: "MessageType_OntologySignOntIdRegister",
+ 359: "MessageType_OntologySignedOntIdRegister",
+ 360: "MessageType_OntologySignOntIdAddAttributes",
+ 361: "MessageType_OntologySignedOntIdAddAttributes",
+ 400: "MessageType_RippleGetAddress",
+ 401: "MessageType_RippleAddress",
+ 402: "MessageType_RippleSignTx",
+ 403: "MessageType_RippleSignedTx",
+ 501: "MessageType_MoneroTransactionInitRequest",
+ 502: "MessageType_MoneroTransactionInitAck",
+ 503: "MessageType_MoneroTransactionSetInputRequest",
+ 504: "MessageType_MoneroTransactionSetInputAck",
+ 505: "MessageType_MoneroTransactionInputsPermutationRequest",
+ 506: "MessageType_MoneroTransactionInputsPermutationAck",
+ 507: "MessageType_MoneroTransactionInputViniRequest",
+ 508: "MessageType_MoneroTransactionInputViniAck",
+ 509: "MessageType_MoneroTransactionAllInputsSetRequest",
+ 510: "MessageType_MoneroTransactionAllInputsSetAck",
+ 511: "MessageType_MoneroTransactionSetOutputRequest",
+ 512: "MessageType_MoneroTransactionSetOutputAck",
+ 513: "MessageType_MoneroTransactionAllOutSetRequest",
+ 514: "MessageType_MoneroTransactionAllOutSetAck",
+ 515: "MessageType_MoneroTransactionSignInputRequest",
+ 516: "MessageType_MoneroTransactionSignInputAck",
+ 517: "MessageType_MoneroTransactionFinalRequest",
+ 518: "MessageType_MoneroTransactionFinalAck",
+ 530: "MessageType_MoneroKeyImageExportInitRequest",
+ 531: "MessageType_MoneroKeyImageExportInitAck",
+ 532: "MessageType_MoneroKeyImageSyncStepRequest",
+ 533: "MessageType_MoneroKeyImageSyncStepAck",
+ 534: "MessageType_MoneroKeyImageSyncFinalRequest",
+ 535: "MessageType_MoneroKeyImageSyncFinalAck",
+ 540: "MessageType_MoneroGetAddress",
+ 541: "MessageType_MoneroAddress",
+ 542: "MessageType_MoneroGetWatchKey",
+ 543: "MessageType_MoneroWatchKey",
+ 546: "MessageType_DebugMoneroDiagRequest",
+ 547: "MessageType_DebugMoneroDiagAck",
+ 550: "MessageType_MoneroGetTxKeyRequest",
+ 551: "MessageType_MoneroGetTxKeyAck",
+ 552: "MessageType_MoneroLiveRefreshStartRequest",
+ 553: "MessageType_MoneroLiveRefreshStartAck",
+ 554: "MessageType_MoneroLiveRefreshStepRequest",
+ 555: "MessageType_MoneroLiveRefreshStepAck",
+ 556: "MessageType_MoneroLiveRefreshFinalRequest",
+ 557: "MessageType_MoneroLiveRefreshFinalAck",
+ 600: "MessageType_EosGetPublicKey",
+ 601: "MessageType_EosPublicKey",
+ 602: "MessageType_EosSignTx",
+ 603: "MessageType_EosTxActionRequest",
+ 604: "MessageType_EosTxActionAck",
+ 605: "MessageType_EosSignedTx",
+ 700: "MessageType_BinanceGetAddress",
+ 701: "MessageType_BinanceAddress",
+ 702: "MessageType_BinanceGetPublicKey",
+ 703: "MessageType_BinancePublicKey",
+ 704: "MessageType_BinanceSignTx",
+ 705: "MessageType_BinanceTxRequest",
+ 706: "MessageType_BinanceTransferMsg",
+ 707: "MessageType_BinanceOrderMsg",
+ 708: "MessageType_BinanceCancelMsg",
+ 709: "MessageType_BinanceSignedTx",
+}
+
+var MessageType_value = map[string]int32{
+ "MessageType_Initialize": 0,
+ "MessageType_Ping": 1,
+ "MessageType_Success": 2,
+ "MessageType_Failure": 3,
+ "MessageType_ChangePin": 4,
+ "MessageType_WipeDevice": 5,
+ "MessageType_GetEntropy": 9,
+ "MessageType_Entropy": 10,
+ "MessageType_LoadDevice": 13,
+ "MessageType_ResetDevice": 14,
+ "MessageType_Features": 17,
+ "MessageType_PinMatrixRequest": 18,
+ "MessageType_PinMatrixAck": 19,
+ "MessageType_Cancel": 20,
+ "MessageType_ClearSession": 24,
+ "MessageType_ApplySettings": 25,
+ "MessageType_ButtonRequest": 26,
+ "MessageType_ButtonAck": 27,
+ "MessageType_ApplyFlags": 28,
+ "MessageType_BackupDevice": 34,
+ "MessageType_EntropyRequest": 35,
+ "MessageType_EntropyAck": 36,
+ "MessageType_PassphraseRequest": 41,
+ "MessageType_PassphraseAck": 42,
+ "MessageType_PassphraseStateRequest": 77,
+ "MessageType_PassphraseStateAck": 78,
+ "MessageType_RecoveryDevice": 45,
+ "MessageType_WordRequest": 46,
+ "MessageType_WordAck": 47,
+ "MessageType_GetFeatures": 55,
+ "MessageType_SetU2FCounter": 63,
+ "MessageType_FirmwareErase": 6,
+ "MessageType_FirmwareUpload": 7,
+ "MessageType_FirmwareRequest": 8,
+ "MessageType_SelfTest": 32,
+ "MessageType_GetPublicKey": 11,
+ "MessageType_PublicKey": 12,
+ "MessageType_SignTx": 15,
+ "MessageType_TxRequest": 21,
+ "MessageType_TxAck": 22,
+ "MessageType_GetAddress": 29,
+ "MessageType_Address": 30,
+ "MessageType_SignMessage": 38,
+ "MessageType_VerifyMessage": 39,
+ "MessageType_MessageSignature": 40,
+ "MessageType_CipherKeyValue": 23,
+ "MessageType_CipheredKeyValue": 48,
+ "MessageType_SignIdentity": 53,
+ "MessageType_SignedIdentity": 54,
+ "MessageType_GetECDHSessionKey": 61,
+ "MessageType_ECDHSessionKey": 62,
+ "MessageType_CosiCommit": 71,
+ "MessageType_CosiCommitment": 72,
+ "MessageType_CosiSign": 73,
+ "MessageType_CosiSignature": 74,
+ "MessageType_DebugLinkDecision": 100,
+ "MessageType_DebugLinkGetState": 101,
+ "MessageType_DebugLinkState": 102,
+ "MessageType_DebugLinkStop": 103,
+ "MessageType_DebugLinkLog": 104,
+ "MessageType_DebugLinkMemoryRead": 110,
+ "MessageType_DebugLinkMemory": 111,
+ "MessageType_DebugLinkMemoryWrite": 112,
+ "MessageType_DebugLinkFlashErase": 113,
+ "MessageType_EthereumGetPublicKey": 450,
+ "MessageType_EthereumPublicKey": 451,
+ "MessageType_EthereumGetAddress": 56,
+ "MessageType_EthereumAddress": 57,
+ "MessageType_EthereumSignTx": 58,
+ "MessageType_EthereumTxRequest": 59,
+ "MessageType_EthereumTxAck": 60,
+ "MessageType_EthereumSignMessage": 64,
+ "MessageType_EthereumVerifyMessage": 65,
+ "MessageType_EthereumMessageSignature": 66,
+ "MessageType_NEMGetAddress": 67,
+ "MessageType_NEMAddress": 68,
+ "MessageType_NEMSignTx": 69,
+ "MessageType_NEMSignedTx": 70,
+ "MessageType_NEMDecryptMessage": 75,
+ "MessageType_NEMDecryptedMessage": 76,
+ "MessageType_LiskGetAddress": 114,
+ "MessageType_LiskAddress": 115,
+ "MessageType_LiskSignTx": 116,
+ "MessageType_LiskSignedTx": 117,
+ "MessageType_LiskSignMessage": 118,
+ "MessageType_LiskMessageSignature": 119,
+ "MessageType_LiskVerifyMessage": 120,
+ "MessageType_LiskGetPublicKey": 121,
+ "MessageType_LiskPublicKey": 122,
+ "MessageType_TezosGetAddress": 150,
+ "MessageType_TezosAddress": 151,
+ "MessageType_TezosSignTx": 152,
+ "MessageType_TezosSignedTx": 153,
+ "MessageType_TezosGetPublicKey": 154,
+ "MessageType_TezosPublicKey": 155,
+ "MessageType_StellarSignTx": 202,
+ "MessageType_StellarTxOpRequest": 203,
+ "MessageType_StellarGetAddress": 207,
+ "MessageType_StellarAddress": 208,
+ "MessageType_StellarCreateAccountOp": 210,
+ "MessageType_StellarPaymentOp": 211,
+ "MessageType_StellarPathPaymentOp": 212,
+ "MessageType_StellarManageOfferOp": 213,
+ "MessageType_StellarCreatePassiveOfferOp": 214,
+ "MessageType_StellarSetOptionsOp": 215,
+ "MessageType_StellarChangeTrustOp": 216,
+ "MessageType_StellarAllowTrustOp": 217,
+ "MessageType_StellarAccountMergeOp": 218,
+ "MessageType_StellarManageDataOp": 220,
+ "MessageType_StellarBumpSequenceOp": 221,
+ "MessageType_StellarSignedTx": 230,
+ "MessageType_TronGetAddress": 250,
+ "MessageType_TronAddress": 251,
+ "MessageType_TronSignTx": 252,
+ "MessageType_TronSignedTx": 253,
+ "MessageType_CardanoSignTx": 303,
+ "MessageType_CardanoTxRequest": 304,
+ "MessageType_CardanoGetPublicKey": 305,
+ "MessageType_CardanoPublicKey": 306,
+ "MessageType_CardanoGetAddress": 307,
+ "MessageType_CardanoAddress": 308,
+ "MessageType_CardanoTxAck": 309,
+ "MessageType_CardanoSignedTx": 310,
+ "MessageType_OntologyGetAddress": 350,
+ "MessageType_OntologyAddress": 351,
+ "MessageType_OntologyGetPublicKey": 352,
+ "MessageType_OntologyPublicKey": 353,
+ "MessageType_OntologySignTransfer": 354,
+ "MessageType_OntologySignedTransfer": 355,
+ "MessageType_OntologySignWithdrawOng": 356,
+ "MessageType_OntologySignedWithdrawOng": 357,
+ "MessageType_OntologySignOntIdRegister": 358,
+ "MessageType_OntologySignedOntIdRegister": 359,
+ "MessageType_OntologySignOntIdAddAttributes": 360,
+ "MessageType_OntologySignedOntIdAddAttributes": 361,
+ "MessageType_RippleGetAddress": 400,
+ "MessageType_RippleAddress": 401,
+ "MessageType_RippleSignTx": 402,
+ "MessageType_RippleSignedTx": 403,
+ "MessageType_MoneroTransactionInitRequest": 501,
+ "MessageType_MoneroTransactionInitAck": 502,
+ "MessageType_MoneroTransactionSetInputRequest": 503,
+ "MessageType_MoneroTransactionSetInputAck": 504,
+ "MessageType_MoneroTransactionInputsPermutationRequest": 505,
+ "MessageType_MoneroTransactionInputsPermutationAck": 506,
+ "MessageType_MoneroTransactionInputViniRequest": 507,
+ "MessageType_MoneroTransactionInputViniAck": 508,
+ "MessageType_MoneroTransactionAllInputsSetRequest": 509,
+ "MessageType_MoneroTransactionAllInputsSetAck": 510,
+ "MessageType_MoneroTransactionSetOutputRequest": 511,
+ "MessageType_MoneroTransactionSetOutputAck": 512,
+ "MessageType_MoneroTransactionAllOutSetRequest": 513,
+ "MessageType_MoneroTransactionAllOutSetAck": 514,
+ "MessageType_MoneroTransactionSignInputRequest": 515,
+ "MessageType_MoneroTransactionSignInputAck": 516,
+ "MessageType_MoneroTransactionFinalRequest": 517,
+ "MessageType_MoneroTransactionFinalAck": 518,
+ "MessageType_MoneroKeyImageExportInitRequest": 530,
+ "MessageType_MoneroKeyImageExportInitAck": 531,
+ "MessageType_MoneroKeyImageSyncStepRequest": 532,
+ "MessageType_MoneroKeyImageSyncStepAck": 533,
+ "MessageType_MoneroKeyImageSyncFinalRequest": 534,
+ "MessageType_MoneroKeyImageSyncFinalAck": 535,
+ "MessageType_MoneroGetAddress": 540,
+ "MessageType_MoneroAddress": 541,
+ "MessageType_MoneroGetWatchKey": 542,
+ "MessageType_MoneroWatchKey": 543,
+ "MessageType_DebugMoneroDiagRequest": 546,
+ "MessageType_DebugMoneroDiagAck": 547,
+ "MessageType_MoneroGetTxKeyRequest": 550,
+ "MessageType_MoneroGetTxKeyAck": 551,
+ "MessageType_MoneroLiveRefreshStartRequest": 552,
+ "MessageType_MoneroLiveRefreshStartAck": 553,
+ "MessageType_MoneroLiveRefreshStepRequest": 554,
+ "MessageType_MoneroLiveRefreshStepAck": 555,
+ "MessageType_MoneroLiveRefreshFinalRequest": 556,
+ "MessageType_MoneroLiveRefreshFinalAck": 557,
+ "MessageType_EosGetPublicKey": 600,
+ "MessageType_EosPublicKey": 601,
+ "MessageType_EosSignTx": 602,
+ "MessageType_EosTxActionRequest": 603,
+ "MessageType_EosTxActionAck": 604,
+ "MessageType_EosSignedTx": 605,
+ "MessageType_BinanceGetAddress": 700,
+ "MessageType_BinanceAddress": 701,
+ "MessageType_BinanceGetPublicKey": 702,
+ "MessageType_BinancePublicKey": 703,
+ "MessageType_BinanceSignTx": 704,
+ "MessageType_BinanceTxRequest": 705,
+ "MessageType_BinanceTransferMsg": 706,
+ "MessageType_BinanceOrderMsg": 707,
+ "MessageType_BinanceCancelMsg": 708,
+ "MessageType_BinanceSignedTx": 709,
+}
+
+func (x MessageType) Enum() *MessageType {
+ p := new(MessageType)
+ *p = x
+ return p
+}
+
+func (x MessageType) String() string {
+ return proto.EnumName(MessageType_name, int32(x))
+}
+
+func (x *MessageType) UnmarshalJSON(data []byte) error {
+ value, err := proto.UnmarshalJSONEnum(MessageType_value, data, "MessageType")
+ if err != nil {
+ return err
+ }
+ *x = MessageType(value)
+ return nil
+}
+
+func (MessageType) EnumDescriptor() ([]byte, []int) {
+ return fileDescriptor_4dc296cbfe5ffcd5, []int{0}
+}
+
+var E_WireIn = &proto.ExtensionDesc{
+ ExtendedType: (*descriptor.EnumValueOptions)(nil),
+ ExtensionType: (*bool)(nil),
+ Field: 50002,
+ Name: "hw.trezor.messages.wire_in",
+ Tag: "varint,50002,opt,name=wire_in",
+ Filename: "messages.proto",
+}
+
+var E_WireOut = &proto.ExtensionDesc{
+ ExtendedType: (*descriptor.EnumValueOptions)(nil),
+ ExtensionType: (*bool)(nil),
+ Field: 50003,
+ Name: "hw.trezor.messages.wire_out",
+ Tag: "varint,50003,opt,name=wire_out",
+ Filename: "messages.proto",
+}
+
+var E_WireDebugIn = &proto.ExtensionDesc{
+ ExtendedType: (*descriptor.EnumValueOptions)(nil),
+ ExtensionType: (*bool)(nil),
+ Field: 50004,
+ Name: "hw.trezor.messages.wire_debug_in",
+ Tag: "varint,50004,opt,name=wire_debug_in",
+ Filename: "messages.proto",
+}
+
+var E_WireDebugOut = &proto.ExtensionDesc{
+ ExtendedType: (*descriptor.EnumValueOptions)(nil),
+ ExtensionType: (*bool)(nil),
+ Field: 50005,
+ Name: "hw.trezor.messages.wire_debug_out",
+ Tag: "varint,50005,opt,name=wire_debug_out",
+ Filename: "messages.proto",
+}
+
+var E_WireTiny = &proto.ExtensionDesc{
+ ExtendedType: (*descriptor.EnumValueOptions)(nil),
+ ExtensionType: (*bool)(nil),
+ Field: 50006,
+ Name: "hw.trezor.messages.wire_tiny",
+ Tag: "varint,50006,opt,name=wire_tiny",
+ Filename: "messages.proto",
+}
+
+var E_WireBootloader = &proto.ExtensionDesc{
+ ExtendedType: (*descriptor.EnumValueOptions)(nil),
+ ExtensionType: (*bool)(nil),
+ Field: 50007,
+ Name: "hw.trezor.messages.wire_bootloader",
+ Tag: "varint,50007,opt,name=wire_bootloader",
+ Filename: "messages.proto",
+}
+
+var E_WireNoFsm = &proto.ExtensionDesc{
+ ExtendedType: (*descriptor.EnumValueOptions)(nil),
+ ExtensionType: (*bool)(nil),
+ Field: 50008,
+ Name: "hw.trezor.messages.wire_no_fsm",
+ Tag: "varint,50008,opt,name=wire_no_fsm",
+ Filename: "messages.proto",
+}
+
+func init() {
+ proto.RegisterEnum("hw.trezor.messages.MessageType", MessageType_name, MessageType_value)
+ proto.RegisterExtension(E_WireIn)
+ proto.RegisterExtension(E_WireOut)
+ proto.RegisterExtension(E_WireDebugIn)
+ proto.RegisterExtension(E_WireDebugOut)
+ proto.RegisterExtension(E_WireTiny)
+ proto.RegisterExtension(E_WireBootloader)
+ proto.RegisterExtension(E_WireNoFsm)
+}
+
+func init() { proto.RegisterFile("messages.proto", fileDescriptor_4dc296cbfe5ffcd5) }
+
+var fileDescriptor_4dc296cbfe5ffcd5 = []byte{
+ // 2430 bytes of a gzipped FileDescriptorProto
+ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x9a, 0xd9, 0x73, 0x1c, 0xc5,
+ 0x1d, 0xc7, 0xb3, 0xab, 0x11, 0x88, 0xf6, 0x41, 0x23, 0xb0, 0x2d, 0xaf, 0x2f, 0xf9, 0xc0, 0x96,
+ 0x2f, 0xd9, 0x10, 0x0c, 0x44, 0x38, 0x60, 0x69, 0xb5, 0x12, 0x8a, 0xb5, 0x5a, 0x97, 0x76, 0xb1,
+ 0x1f, 0x5d, 0xa3, 0x9d, 0xd6, 0x6e, 0x97, 0x67, 0x67, 0x86, 0x9e, 0x1e, 0x49, 0xeb, 0xa7, 0x9c,
+ 0x3c, 0x13, 0x48, 0xc0, 0xb9, 0xa9, 0xa4, 0x2a, 0x21, 0x57, 0x85, 0x1c, 0x4e, 0x25, 0x55, 0x39,
+ 0x08, 0x24, 0x2f, 0xc9, 0x43, 0x52, 0x9c, 0x86, 0x40, 0xee, 0x90, 0xe4, 0x0f, 0xc8, 0xc5, 0x91,
+ 0xa4, 0x7a, 0xa6, 0xbb, 0xe7, 0xd8, 0xdf, 0xae, 0x36, 0x6f, 0x58, 0xf3, 0xf9, 0x7d, 0x7f, 0x47,
+ 0xff, 0xfa, 0x37, 0xdd, 0xb3, 0xa0, 0xcd, 0x2d, 0xe2, 0xfb, 0x66, 0x83, 0xf8, 0xe3, 0x1e, 0x73,
+ 0xb9, 0x3b, 0x3c, 0xdc, 0x5c, 0x1d, 0xe7, 0x8c, 0x5c, 0x76, 0xd9, 0xb8, 0x7a, 0x52, 0x18, 0x6d,
+ 0xb8, 0x6e, 0xc3, 0x26, 0x27, 0x42, 0x62, 0x29, 0x58, 0x3e, 0x61, 0x11, 0xbf, 0xce, 0xa8, 0xc7,
+ 0x5d, 0x16, 0x59, 0x1d, 0xf9, 0xfe, 0x7d, 0x68, 0x43, 0x39, 0xc2, 0x6b, 0x6d, 0x8f, 0x0c, 0x1f,
+ 0x40, 0x5b, 0x13, 0xff, 0xbc, 0x38, 0xe7, 0x50, 0x4e, 0x4d, 0x9b, 0x5e, 0x26, 0xf8, 0x5d, 0x85,
+ 0xa1, 0x87, 0xaf, 0x8e, 0xe4, 0x9e, 0xba, 0x3a, 0x92, 0x1b, 0x2e, 0x20, 0x9c, 0xa4, 0xce, 0x51,
+ 0xa7, 0x81, 0x73, 0x05, 0x43, 0x3c, 0x1f, 0xde, 0x85, 0x6e, 0x4e, 0x3e, 0xab, 0x06, 0xf5, 0x3a,
+ 0xf1, 0x7d, 0x9c, 0x2f, 0x18, 0x57, 0x80, 0xc7, 0x33, 0x26, 0xb5, 0x03, 0x46, 0xf0, 0x80, 0x7c,
+ 0xbc, 0x07, 0x6d, 0x49, 0x3e, 0x2e, 0x36, 0x4d, 0xa7, 0x41, 0xce, 0x51, 0x07, 0x1b, 0x52, 0x7e,
+ 0x34, 0x1d, 0xe0, 0x05, 0xea, 0x91, 0x69, 0xb2, 0x42, 0xeb, 0x04, 0x0f, 0xc2, 0xc4, 0x2c, 0xe1,
+ 0x25, 0x87, 0x33, 0xd7, 0x6b, 0xe3, 0x1b, 0xe0, 0x10, 0xd5, 0x63, 0x24, 0x63, 0xc8, 0x08, 0xcc,
+ 0xbb, 0xa6, 0x25, 0x5d, 0x6c, 0x92, 0x02, 0x7b, 0xd1, 0xb6, 0x24, 0xb1, 0x48, 0x7c, 0xc2, 0x25,
+ 0xb2, 0x59, 0x22, 0xbb, 0xd1, 0x2d, 0xa9, 0x3c, 0x89, 0xc9, 0x03, 0x46, 0x7c, 0x7c, 0x93, 0x74,
+ 0x72, 0x10, 0xed, 0xcc, 0x94, 0xb0, 0x6c, 0x72, 0x46, 0xd7, 0x16, 0xc9, 0x83, 0x01, 0xf1, 0x39,
+ 0x1e, 0x96, 0xdc, 0x11, 0x34, 0x02, 0x72, 0x93, 0xf5, 0x4b, 0xf8, 0xe6, 0xc2, 0x46, 0xb5, 0x24,
+ 0x4f, 0x47, 0x81, 0x0f, 0xa7, 0x8a, 0x67, 0x3a, 0x75, 0x62, 0xe3, 0x5b, 0x12, 0x0b, 0xb7, 0x2f,
+ 0xad, 0x56, 0xb4, 0x89, 0xc9, 0xaa, 0xc4, 0xf7, 0xa9, 0xeb, 0xe0, 0x11, 0x19, 0xf9, 0x7e, 0xb4,
+ 0x3d, 0xc9, 0x4c, 0x7a, 0x9e, 0xdd, 0xae, 0x12, 0xce, 0xa9, 0xd3, 0xf0, 0xf1, 0x76, 0x18, 0x9a,
+ 0x0a, 0x38, 0x77, 0x1d, 0x15, 0x7b, 0x41, 0xc6, 0x7e, 0x28, 0xbd, 0x98, 0x11, 0x24, 0x02, 0xdf,
+ 0xd1, 0x11, 0xf8, 0xd6, 0x0e, 0x97, 0x33, 0xb6, 0xd9, 0xf0, 0xf1, 0x4e, 0xe9, 0x2f, 0x13, 0xf8,
+ 0x94, 0x59, 0xbf, 0x14, 0x78, 0xb2, 0xe4, 0xfb, 0x24, 0x73, 0x00, 0x15, 0x80, 0x65, 0x55, 0x41,
+ 0xed, 0x87, 0x57, 0x57, 0x52, 0x22, 0xaa, 0x03, 0x52, 0xe7, 0x10, 0xda, 0x95, 0x2a, 0xb9, 0xe9,
+ 0xfb, 0x5e, 0x93, 0x99, 0x3e, 0x51, 0x52, 0x87, 0xa5, 0xd4, 0xd1, 0x74, 0x11, 0x62, 0x50, 0xa8,
+ 0x1d, 0xc9, 0xe4, 0x78, 0x0c, 0xed, 0x83, 0xe1, 0x2a, 0x37, 0xb9, 0x96, 0x2e, 0x4b, 0xe9, 0x93,
+ 0x68, 0x77, 0x0f, 0x5a, 0xe8, 0x2f, 0x64, 0xf4, 0x33, 0xd9, 0x2f, 0x92, 0xba, 0xbb, 0x42, 0x58,
+ 0x5b, 0xd6, 0xe8, 0x38, 0xdc, 0xb9, 0x17, 0x5c, 0x66, 0x29, 0xd7, 0xe3, 0xf0, 0x0e, 0x15, 0x88,
+ 0xf0, 0x77, 0x02, 0x56, 0x98, 0x25, 0x5c, 0xf7, 0xf6, 0x5d, 0x70, 0x73, 0x54, 0x09, 0x7f, 0xe0,
+ 0xf6, 0x99, 0xa2, 0x1b, 0x38, 0x9c, 0x30, 0x7c, 0x9f, 0xae, 0x72, 0x0a, 0x9a, 0xa1, 0xac, 0xb5,
+ 0x6a, 0x32, 0x52, 0x12, 0x49, 0xe2, 0xeb, 0xa2, 0x9e, 0xfd, 0x9e, 0x00, 0xc7, 0xd2, 0x89, 0x29,
+ 0xf0, 0x01, 0xcf, 0x76, 0x4d, 0x0b, 0x5f, 0x9f, 0x20, 0x0f, 0xa3, 0x1d, 0x10, 0xa9, 0x12, 0x1c,
+ 0x2a, 0x0c, 0x5d, 0x51, 0xe8, 0xbe, 0xf4, 0xf6, 0xac, 0x12, 0x7b, 0xb9, 0x26, 0x98, 0xd1, 0x84,
+ 0x5c, 0xa6, 0xe7, 0x66, 0x09, 0x3f, 0x17, 0x2c, 0xd9, 0xb4, 0x7e, 0x96, 0xb4, 0xf1, 0x06, 0x99,
+ 0x45, 0x66, 0x5e, 0xc5, 0xc0, 0x46, 0x59, 0xcd, 0x9d, 0xe9, 0x3d, 0x59, 0xa5, 0x0d, 0xa7, 0xb6,
+ 0x86, 0x6f, 0x84, 0xcd, 0x6b, 0x7a, 0xfb, 0x6f, 0x91, 0xe6, 0x3b, 0xd0, 0x4d, 0x69, 0x40, 0x2c,
+ 0xc5, 0xd6, 0xae, 0x93, 0x6e, 0xd2, 0xb2, 0x98, 0x98, 0xb6, 0xbb, 0xe0, 0x49, 0xa7, 0x1e, 0xef,
+ 0x96, 0xea, 0x99, 0xb5, 0x14, 0xc1, 0xc9, 0x7f, 0xe3, 0x83, 0xf0, 0x5a, 0x9e, 0x27, 0x8c, 0x2e,
+ 0xb7, 0x15, 0x74, 0x48, 0x42, 0x99, 0x61, 0x26, 0xff, 0x5b, 0xc8, 0x85, 0x9d, 0x81, 0xc7, 0xa4,
+ 0xbf, 0x4c, 0x8f, 0x16, 0xa9, 0xd7, 0x24, 0xec, 0x2c, 0x69, 0x9f, 0x37, 0xed, 0x80, 0xe0, 0x6d,
+ 0xb0, 0x5a, 0x44, 0x11, 0x4b, 0x73, 0x27, 0xa5, 0x5a, 0x66, 0x7d, 0x84, 0xbb, 0x39, 0x8b, 0x38,
+ 0x9c, 0xf2, 0x36, 0x3e, 0x05, 0xcf, 0x04, 0xc1, 0x10, 0x4b, 0x53, 0x77, 0xea, 0x41, 0xb5, 0x2b,
+ 0xfb, 0xca, 0x28, 0x4e, 0xdf, 0x2f, 0x07, 0xa3, 0x58, 0xcd, 0xf7, 0x76, 0x19, 0x31, 0x69, 0xea,
+ 0x5e, 0x78, 0xc4, 0x14, 0x5d, 0x9f, 0x16, 0xdd, 0x56, 0x8b, 0x72, 0x3c, 0x0b, 0xeb, 0xc4, 0x44,
+ 0x8b, 0x38, 0x1c, 0xdf, 0x2f, 0x75, 0x32, 0xef, 0x10, 0x41, 0x89, 0x04, 0xf0, 0x1c, 0xbc, 0x36,
+ 0xea, 0x79, 0x54, 0xf3, 0xf7, 0x49, 0x91, 0x13, 0xe9, 0xdc, 0xa6, 0xc9, 0x52, 0xd0, 0x98, 0xa7,
+ 0xce, 0xa5, 0x69, 0x52, 0xa7, 0xe1, 0xdc, 0xb7, 0x0a, 0x1b, 0x9f, 0x48, 0x0e, 0x92, 0xa3, 0x5d,
+ 0x0c, 0x66, 0x09, 0x0f, 0x87, 0x0f, 0x26, 0x85, 0x21, 0x65, 0x90, 0x4d, 0x44, 0xc3, 0x11, 0xb9,
+ 0x5c, 0x30, 0x9e, 0x04, 0x02, 0x4d, 0x50, 0xae, 0x87, 0x1b, 0x05, 0xe3, 0x09, 0x60, 0x39, 0x35,
+ 0x34, 0xef, 0x36, 0x70, 0x53, 0x0a, 0x1d, 0x46, 0x7b, 0x40, 0xa6, 0x4c, 0x5a, 0x2e, 0x6b, 0x2f,
+ 0x12, 0xd3, 0xc2, 0x8e, 0x94, 0xbb, 0x35, 0x3d, 0x0c, 0x32, 0x28, 0x76, 0xa5, 0xe2, 0x11, 0x34,
+ 0xda, 0x03, 0xbb, 0xc0, 0x28, 0x27, 0xd8, 0x93, 0x92, 0xdd, 0xbc, 0xcf, 0xd8, 0xa6, 0xdf, 0x8c,
+ 0x06, 0xd7, 0x83, 0x12, 0x3d, 0x9a, 0x96, 0x2d, 0x71, 0xd1, 0xc2, 0x41, 0x2b, 0x35, 0x43, 0x9e,
+ 0x19, 0x90, 0xeb, 0x38, 0x96, 0xae, 0xb8, 0x82, 0x63, 0xf2, 0x59, 0x75, 0x3c, 0x1a, 0x4b, 0xbf,
+ 0x16, 0x12, 0xb2, 0x6a, 0x6b, 0xdf, 0x2d, 0x35, 0x33, 0xe9, 0x2b, 0x52, 0x61, 0xef, 0x81, 0x77,
+ 0xa4, 0xc2, 0xe4, 0x98, 0x9a, 0x80, 0xdf, 0x88, 0x8a, 0x8a, 0xc7, 0xd5, 0x3d, 0x52, 0x2e, 0xb3,
+ 0xd0, 0x31, 0x28, 0xc6, 0xd6, 0x69, 0xa9, 0x96, 0x29, 0x63, 0xd2, 0xa7, 0x1a, 0x2c, 0x67, 0x24,
+ 0x7a, 0x14, 0xed, 0x85, 0xd0, 0xf4, 0x14, 0x9a, 0x94, 0xf0, 0x38, 0x3a, 0x00, 0xc1, 0x1d, 0xd3,
+ 0x68, 0x0a, 0x0e, 0x76, 0xa1, 0x54, 0x4e, 0xd4, 0xb1, 0x08, 0xcf, 0xd8, 0x85, 0x52, 0x59, 0x11,
+ 0xd3, 0xf0, 0x91, 0x75, 0xa1, 0x54, 0x96, 0xd5, 0x2b, 0xc1, 0x6f, 0x4c, 0x09, 0x10, 0xab, 0xb6,
+ 0x86, 0x67, 0xe0, 0x01, 0xb4, 0x50, 0x2a, 0x4f, 0x93, 0x3a, 0x6b, 0x7b, 0x5c, 0xe5, 0x78, 0x16,
+ 0xae, 0x5d, 0x0c, 0x12, 0x4b, 0xa1, 0xf3, 0xf0, 0xd2, 0xce, 0x53, 0xff, 0x52, 0x22, 0x3f, 0x06,
+ 0x07, 0x27, 0x28, 0x85, 0xf8, 0x5d, 0xce, 0xc3, 0xd4, 0xbf, 0x24, 0x33, 0xe4, 0xf0, 0xe9, 0x4c,
+ 0x11, 0x61, 0x8a, 0x81, 0x54, 0xc9, 0x34, 0xa4, 0x62, 0x54, 0xd4, 0x2b, 0x52, 0x2a, 0xb3, 0x1f,
+ 0x05, 0xd6, 0xb1, 0x80, 0xab, 0x70, 0xd5, 0x04, 0x9b, 0xee, 0x8c, 0x35, 0xf8, 0x8d, 0x22, 0x4b,
+ 0x11, 0xef, 0xaf, 0x36, 0x3c, 0x50, 0x05, 0x17, 0x43, 0x97, 0xf5, 0xc9, 0x3d, 0x95, 0x48, 0x8d,
+ 0x5c, 0x76, 0xfd, 0x44, 0x61, 0x1f, 0xcb, 0x69, 0xb1, 0x91, 0x0e, 0x4e, 0x41, 0x8f, 0xe7, 0xf4,
+ 0x3b, 0x6c, 0x5b, 0x07, 0x24, 0x8b, 0x7b, 0x25, 0xa7, 0x5f, 0x16, 0xdb, 0x41, 0x26, 0x2c, 0xef,
+ 0x27, 0x72, 0x7a, 0x34, 0xec, 0x82, 0xc2, 0x8a, 0xe3, 0xff, 0x64, 0x4e, 0x8f, 0x86, 0x42, 0x07,
+ 0x19, 0x63, 0x9f, 0xca, 0xe9, 0xfe, 0x49, 0x9f, 0xe2, 0x38, 0xb1, 0x6d, 0x93, 0xc9, 0xe0, 0x7e,
+ 0x9e, 0xd3, 0x0d, 0xb9, 0x1b, 0xa0, 0x6a, 0x6b, 0x15, 0x4f, 0xcd, 0x86, 0x5f, 0x74, 0x89, 0x50,
+ 0xa2, 0x89, 0xd2, 0xfd, 0xb2, 0x4b, 0x84, 0x92, 0x54, 0xd8, 0xaf, 0x94, 0xe0, 0xf1, 0xf4, 0x91,
+ 0x5a, 0x62, 0x45, 0x46, 0xc2, 0x23, 0x72, 0x5d, 0x1c, 0x38, 0x2b, 0x1e, 0x7e, 0x2e, 0xa7, 0xa7,
+ 0xd8, 0x4e, 0x00, 0x3f, 0x67, 0xb6, 0xc5, 0x4b, 0xb7, 0xe2, 0xe1, 0xe7, 0x73, 0x7a, 0xea, 0x8c,
+ 0x82, 0x20, 0x6f, 0xc6, 0xf0, 0x0b, 0xbd, 0xe1, 0xb2, 0xe9, 0x98, 0x0d, 0x52, 0x59, 0x5e, 0x26,
+ 0xac, 0xe2, 0xe1, 0x17, 0x15, 0x7c, 0x3b, 0x3a, 0xd4, 0x35, 0x62, 0x71, 0xc6, 0xa7, 0x2b, 0xda,
+ 0xe6, 0xa5, 0x9c, 0xde, 0x11, 0x7b, 0xa0, 0x75, 0x20, 0xbc, 0xe2, 0x71, 0xea, 0x3a, 0x7e, 0xc5,
+ 0xc3, 0x2f, 0xf7, 0x0e, 0x26, 0xba, 0x45, 0xd7, 0x58, 0xe0, 0x8b, 0xc8, 0xaf, 0xf5, 0x16, 0x9e,
+ 0xb4, 0x6d, 0x77, 0x55, 0xb1, 0xaf, 0x28, 0xf6, 0x58, 0x7a, 0x10, 0x2b, 0x36, 0x2a, 0x72, 0x99,
+ 0xb0, 0x06, 0xa9, 0x78, 0xf8, 0xd5, 0xde, 0xca, 0x51, 0x4d, 0xa6, 0x4d, 0x6e, 0x56, 0x3c, 0xfc,
+ 0x5a, 0x6f, 0xe5, 0xa9, 0xa0, 0xe5, 0x55, 0x45, 0x03, 0x39, 0x75, 0xa1, 0xfc, 0x7a, 0x4e, 0xef,
+ 0xe4, 0x1d, 0x5d, 0x9a, 0x32, 0xdc, 0x0d, 0x6f, 0xe4, 0xf4, 0xb4, 0x49, 0xf7, 0x38, 0x73, 0x9d,
+ 0x44, 0xa3, 0xbd, 0x99, 0xd3, 0x83, 0x6b, 0x5b, 0x16, 0x53, 0xcc, 0x5b, 0x39, 0x7d, 0x48, 0xde,
+ 0x9a, 0x65, 0xe4, 0x26, 0x78, 0xbb, 0xdb, 0x56, 0x97, 0x48, 0x18, 0xd2, 0x3b, 0x5d, 0xf6, 0x53,
+ 0xd1, 0x64, 0x96, 0xe9, 0xb8, 0x52, 0xea, 0x1b, 0x79, 0xb8, 0x49, 0x25, 0x15, 0xbf, 0x69, 0x9f,
+ 0xca, 0xeb, 0x0f, 0x03, 0x7b, 0x00, 0x30, 0xb5, 0xe3, 0xbf, 0xd9, 0x5b, 0x34, 0x06, 0xbf, 0x95,
+ 0x87, 0xb7, 0x68, 0x2c, 0xaa, 0xaa, 0xf2, 0xed, 0x3c, 0xbc, 0x45, 0x25, 0xa9, 0xb0, 0xef, 0xe4,
+ 0xf5, 0x3b, 0x76, 0x04, 0x4c, 0x47, 0x9c, 0x07, 0xae, 0xe6, 0xe1, 0x45, 0x4d, 0x54, 0x26, 0xac,
+ 0xe0, 0x77, 0x95, 0x58, 0x66, 0xd6, 0x54, 0x1c, 0xee, 0xda, 0x6e, 0xa3, 0x9d, 0x08, 0xef, 0x37,
+ 0x5d, 0x24, 0x15, 0xaa, 0xb8, 0xdf, 0xe6, 0xf5, 0x15, 0x7e, 0xb4, 0x8b, 0x64, 0x5c, 0x9d, 0xdf,
+ 0xe5, 0xe1, 0x73, 0x9a, 0x82, 0x63, 0xf2, 0xf7, 0xeb, 0xc8, 0x86, 0x8b, 0xcd, 0x4c, 0xc7, 0x5f,
+ 0x26, 0x0c, 0xff, 0x41, 0xc9, 0x66, 0xc6, 0x58, 0x12, 0x26, 0x96, 0xc6, 0xff, 0xa8, 0xb4, 0xc7,
+ 0xd1, 0xfe, 0x6e, 0xf8, 0x05, 0xca, 0x9b, 0x16, 0x33, 0x57, 0x2b, 0x4e, 0x03, 0xff, 0x49, 0xc9,
+ 0x9f, 0x44, 0xb7, 0x76, 0x97, 0x4f, 0x5a, 0xfc, 0x39, 0xaf, 0x3f, 0x3e, 0x74, 0xb5, 0xa8, 0x38,
+ 0x7c, 0xce, 0x5a, 0x24, 0x0d, 0xea, 0x8b, 0xbb, 0xfc, 0x1b, 0x79, 0x78, 0xae, 0xa5, 0x7d, 0xa4,
+ 0x6d, 0xfe, 0xa2, 0xbc, 0x9c, 0x42, 0x47, 0x7a, 0x7a, 0x99, 0xb4, 0xac, 0x49, 0xce, 0x19, 0x5d,
+ 0x0a, 0x38, 0xf1, 0xf1, 0x5f, 0x95, 0xab, 0xbb, 0xd0, 0xb1, 0x75, 0x5c, 0xa5, 0x0d, 0xff, 0x96,
+ 0xd7, 0xa7, 0x85, 0xd4, 0x26, 0x58, 0xa4, 0x9e, 0x67, 0x93, 0x44, 0xef, 0x3c, 0x3c, 0x00, 0xbf,
+ 0x6f, 0x23, 0x50, 0x51, 0x1f, 0x1d, 0x80, 0x3b, 0x3b, 0xa2, 0xe4, 0x6e, 0x7e, 0x64, 0x00, 0xde,
+ 0x25, 0x31, 0x14, 0x36, 0xf6, 0xa3, 0x0a, 0x7b, 0x37, 0x1a, 0x4b, 0xdd, 0x9f, 0x5d, 0x87, 0x30,
+ 0x37, 0x5c, 0x79, 0xb3, 0x2e, 0x66, 0xfc, 0x9c, 0x43, 0xb9, 0x1a, 0x00, 0x7f, 0x1f, 0xd0, 0x17,
+ 0xbb, 0x03, 0xeb, 0x1a, 0x89, 0x6d, 0xf6, 0x0f, 0x65, 0x90, 0xa9, 0x5c, 0x87, 0x41, 0x95, 0xf0,
+ 0x39, 0xc7, 0x0b, 0xb4, 0xa7, 0x7f, 0x2a, 0xc3, 0xf5, 0xc2, 0x53, 0x86, 0xc2, 0xdb, 0xbf, 0x94,
+ 0xd1, 0x19, 0x74, 0x6a, 0x9d, 0xf0, 0xbc, 0x80, 0xfb, 0xe7, 0x08, 0x6b, 0x05, 0xdc, 0x14, 0x7f,
+ 0x50, 0x6e, 0xff, 0xad, 0x14, 0x4e, 0xa3, 0xdb, 0xfe, 0x3f, 0x05, 0xe1, 0xff, 0x4d, 0x65, 0x7d,
+ 0x37, 0x3a, 0xbe, 0xbe, 0xf5, 0x79, 0xea, 0x50, 0xe5, 0xf7, 0x2d, 0x65, 0x79, 0x07, 0x3a, 0xdc,
+ 0x9f, 0xa5, 0xf0, 0xf7, 0xb6, 0xb2, 0xba, 0x07, 0x9d, 0xec, 0x69, 0x35, 0x69, 0xdb, 0x51, 0xc0,
+ 0x55, 0xa2, 0x2b, 0xfc, 0x4e, 0xbf, 0x4b, 0x93, 0x34, 0x16, 0x5e, 0xff, 0xd3, 0x6f, 0x96, 0xe2,
+ 0x98, 0x10, 0xf0, 0xc4, 0xa2, 0xfe, 0xb7, 0xdf, 0x2c, 0xb5, 0xa5, 0xf0, 0xf7, 0x7e, 0xa3, 0x4f,
+ 0x7f, 0x93, 0xb6, 0x5d, 0x09, 0x78, 0x22, 0xc5, 0x0f, 0x18, 0x7d, 0xfa, 0xd3, 0x96, 0xc2, 0xdf,
+ 0x07, 0xfb, 0xf5, 0x17, 0x7e, 0xf4, 0x49, 0x36, 0xed, 0x87, 0xfa, 0xf5, 0xa7, 0x2d, 0x85, 0xbf,
+ 0x0f, 0xf7, 0x6b, 0x35, 0x43, 0x1d, 0xd3, 0x56, 0xbe, 0x3e, 0x62, 0xc0, 0x03, 0x13, 0xb6, 0x12,
+ 0x7e, 0x1e, 0x52, 0x16, 0x77, 0xa2, 0xa3, 0x9d, 0x16, 0x67, 0x49, 0x7b, 0xae, 0x65, 0x36, 0x48,
+ 0x69, 0xcd, 0x73, 0x19, 0x4f, 0x6e, 0xfa, 0x47, 0x94, 0x5d, 0x66, 0xd0, 0x76, 0xb3, 0x13, 0xbe,
+ 0x1e, 0xed, 0x99, 0x93, 0xb2, 0xa9, 0xb6, 0x9d, 0x7a, 0x95, 0x13, 0x7d, 0x5a, 0xff, 0x58, 0xcf,
+ 0x9c, 0xb2, 0x56, 0xc2, 0xcf, 0xc7, 0x0d, 0x78, 0xa0, 0x77, 0x5a, 0xa4, 0x8a, 0xf7, 0x98, 0x32,
+ 0xbb, 0x0d, 0x1d, 0xec, 0xc3, 0x4c, 0x78, 0x7a, 0xdc, 0x80, 0x47, 0x79, 0x64, 0x92, 0x18, 0xe5,
+ 0x9f, 0x36, 0xe0, 0x51, 0x1e, 0x81, 0x8a, 0xfa, 0x8c, 0x01, 0x9f, 0x7a, 0xb4, 0xdc, 0x05, 0x93,
+ 0xd7, 0x9b, 0xe2, 0xbd, 0xfe, 0x59, 0x03, 0x9e, 0xe7, 0x11, 0xa9, 0xb1, 0xcf, 0x19, 0xf0, 0xc5,
+ 0x24, 0xfc, 0x50, 0x14, 0xb1, 0xd3, 0xd4, 0x6c, 0xa8, 0x0a, 0x7c, 0xde, 0x80, 0xef, 0x50, 0x19,
+ 0x5c, 0x64, 0xfe, 0x05, 0xa5, 0x9c, 0x39, 0x2d, 0xeb, 0x50, 0x6b, 0x6b, 0x67, 0x89, 0xfe, 0xa9,
+ 0xe3, 0x8b, 0x06, 0x7c, 0x60, 0x49, 0xd3, 0x42, 0xf7, 0x4b, 0x3d, 0x7b, 0x64, 0x9e, 0xae, 0x90,
+ 0x45, 0xb2, 0xcc, 0x88, 0xdf, 0xac, 0x72, 0x93, 0xe9, 0x6e, 0x7c, 0xd2, 0x80, 0x8f, 0x16, 0xb0,
+ 0x95, 0xf0, 0xf3, 0x65, 0xa3, 0xd7, 0xab, 0x24, 0x65, 0x11, 0xb7, 0xe2, 0x57, 0x94, 0x1b, 0xf0,
+ 0x4d, 0x97, 0x31, 0x12, 0x5e, 0xbe, 0xda, 0x6f, 0x36, 0xa9, 0x46, 0xfc, 0x5a, 0xbf, 0xd9, 0xe8,
+ 0x3e, 0xfc, 0xba, 0x01, 0x7f, 0x0a, 0x28, 0x65, 0x6e, 0xdc, 0xd7, 0x0c, 0xf8, 0x7e, 0x50, 0x4a,
+ 0xde, 0xb7, 0x5f, 0x31, 0xf4, 0x67, 0x96, 0x2d, 0x19, 0x48, 0x9e, 0x26, 0x5e, 0xed, 0xd2, 0x27,
+ 0x25, 0xd7, 0x17, 0x07, 0xe9, 0xe4, 0xbb, 0xf3, 0xd7, 0x06, 0x7c, 0xff, 0x49, 0xa0, 0x22, 0x81,
+ 0xd7, 0x0c, 0xf8, 0xfe, 0x53, 0x4a, 0x7c, 0x58, 0x78, 0xbd, 0xcb, 0xee, 0x98, 0xa2, 0x8e, 0xe9,
+ 0xd4, 0x93, 0x07, 0xa7, 0x1f, 0x0c, 0xc2, 0xbb, 0x43, 0x92, 0x0a, 0xfb, 0xe1, 0x20, 0x7c, 0x73,
+ 0x89, 0x05, 0xe3, 0xa2, 0xfc, 0x68, 0x10, 0xbe, 0xb9, 0x48, 0x36, 0x06, 0x7f, 0x3c, 0x08, 0xdf,
+ 0xae, 0x24, 0x28, 0x2b, 0xf8, 0x74, 0x6f, 0xb9, 0xf8, 0x76, 0xf5, 0x93, 0x41, 0xf8, 0xaa, 0xa1,
+ 0x40, 0x79, 0x18, 0x2f, 0xfb, 0x0d, 0xfc, 0xcc, 0x20, 0x7c, 0xd5, 0x90, 0x68, 0x85, 0x59, 0x11,
+ 0xf7, 0x6c, 0x6f, 0xdf, 0xd1, 0x8f, 0xb4, 0x02, 0xfc, 0x69, 0x6f, 0x41, 0xbd, 0x30, 0x3f, 0x93,
+ 0x31, 0x4e, 0x9c, 0x46, 0xd7, 0xaf, 0x52, 0x46, 0x2e, 0x52, 0x67, 0x78, 0xef, 0x78, 0xf4, 0x4b,
+ 0xff, 0xb8, 0xfa, 0xa5, 0x7f, 0xbc, 0xe4, 0x04, 0xad, 0xf0, 0xe7, 0x12, 0xf9, 0x95, 0x60, 0xe4,
+ 0xb9, 0x87, 0x06, 0x46, 0x73, 0x63, 0x43, 0x8b, 0xd7, 0x09, 0x9b, 0x39, 0x67, 0xe2, 0x5e, 0x34,
+ 0x14, 0x5a, 0xbb, 0x01, 0xef, 0xc7, 0xfc, 0x79, 0x69, 0x1e, 0xba, 0xac, 0x04, 0x7c, 0x62, 0x16,
+ 0x6d, 0x0a, 0xed, 0x2d, 0x31, 0xad, 0xfa, 0x8c, 0xe1, 0x05, 0x29, 0xb2, 0x41, 0x58, 0x86, 0x63,
+ 0x6e, 0xce, 0x99, 0x98, 0x43, 0x9b, 0x13, 0x42, 0x7d, 0x86, 0xf3, 0xa2, 0x54, 0xda, 0xa8, 0x95,
+ 0x44, 0x4c, 0x67, 0xd0, 0x0d, 0xa1, 0x14, 0xa7, 0x4e, 0xbb, 0x1f, 0x95, 0x97, 0xa4, 0x4a, 0x58,
+ 0x89, 0x1a, 0x75, 0xda, 0x13, 0xf3, 0xe8, 0xc6, 0x50, 0x61, 0xc9, 0x75, 0xb9, 0xed, 0x9a, 0x16,
+ 0x61, 0xfd, 0xe8, 0xbc, 0x2c, 0x75, 0xc2, 0x44, 0xa6, 0xb4, 0xe9, 0x44, 0x11, 0x85, 0x99, 0x5e,
+ 0x74, 0xdc, 0x8b, 0xcb, 0x7e, 0xab, 0x1f, 0xa5, 0x6b, 0x52, 0x29, 0xcc, 0x63, 0xc1, 0x9d, 0xf1,
+ 0x5b, 0x53, 0x77, 0xa0, 0xfd, 0x75, 0xb7, 0x35, 0xee, 0x9b, 0xdc, 0xf5, 0x9b, 0xd4, 0x36, 0x97,
+ 0x7c, 0xf5, 0xff, 0x79, 0xd8, 0x74, 0x49, 0x4b, 0x4d, 0x6d, 0xaa, 0x85, 0x7f, 0x94, 0x9d, 0xf3,
+ 0xbf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xa3, 0x69, 0x67, 0x5d, 0x1f, 0x22, 0x00, 0x00,
+}
diff --git a/accounts/usbwallet/trezor/messages.proto b/accounts/usbwallet/trezor/messages.proto
new file mode 100644
index 0000000..3e0482e
--- /dev/null
+++ b/accounts/usbwallet/trezor/messages.proto
@@ -0,0 +1,264 @@
+// This file originates from the SatoshiLabs Trezor `common` repository at:
+// https://github.com/trezor/trezor-common/blob/master/protob/messages.proto
+// dated 28.05.2019, commit 893fd219d4a01bcffa0cd9cfa631856371ec5aa9.
+
+syntax = "proto2";
+package hw.trezor.messages;
+
+/**
+ * Messages for TREZOR communication
+ */
+
+// Sugar for easier handling in Java
+option java_package = "com.satoshilabs.trezor.lib.protobuf";
+option java_outer_classname = "TrezorMessage";
+
+import "google/protobuf/descriptor.proto";
+
+/**
+ * Options for specifying message direction and type of wire (normal/debug)
+ */
+extend google.protobuf.EnumValueOptions {
+ optional bool wire_in = 50002; // message can be transmitted via wire from PC to TREZOR
+ optional bool wire_out = 50003; // message can be transmitted via wire from TREZOR to PC
+ optional bool wire_debug_in = 50004; // message can be transmitted via debug wire from PC to TREZOR
+ optional bool wire_debug_out = 50005; // message can be transmitted via debug wire from TREZOR to PC
+ optional bool wire_tiny = 50006; // message is handled by TREZOR when the USB stack is in tiny mode
+ optional bool wire_bootloader = 50007; // message is only handled by TREZOR Bootloader
+ optional bool wire_no_fsm = 50008; // message is not handled by TREZOR unless the USB stack is in tiny mode
+}
+
+/**
+ * Mapping between TREZOR wire identifier (uint) and a protobuf message
+ */
+enum MessageType {
+
+ // Management
+ MessageType_Initialize = 0 [(wire_in) = true, (wire_tiny) = true];
+ MessageType_Ping = 1 [(wire_in) = true];
+ MessageType_Success = 2 [(wire_out) = true];
+ MessageType_Failure = 3 [(wire_out) = true];
+ MessageType_ChangePin = 4 [(wire_in) = true];
+ MessageType_WipeDevice = 5 [(wire_in) = true];
+ MessageType_GetEntropy = 9 [(wire_in) = true];
+ MessageType_Entropy = 10 [(wire_out) = true];
+ MessageType_LoadDevice = 13 [(wire_in) = true];
+ MessageType_ResetDevice = 14 [(wire_in) = true];
+ MessageType_Features = 17 [(wire_out) = true];
+ MessageType_PinMatrixRequest = 18 [(wire_out) = true];
+ MessageType_PinMatrixAck = 19 [(wire_in) = true, (wire_tiny) = true, (wire_no_fsm) = true];
+ MessageType_Cancel = 20 [(wire_in) = true, (wire_tiny) = true];
+ MessageType_ClearSession = 24 [(wire_in) = true];
+ MessageType_ApplySettings = 25 [(wire_in) = true];
+ MessageType_ButtonRequest = 26 [(wire_out) = true];
+ MessageType_ButtonAck = 27 [(wire_in) = true, (wire_tiny) = true, (wire_no_fsm) = true];
+ MessageType_ApplyFlags = 28 [(wire_in) = true];
+ MessageType_BackupDevice = 34 [(wire_in) = true];
+ MessageType_EntropyRequest = 35 [(wire_out) = true];
+ MessageType_EntropyAck = 36 [(wire_in) = true];
+ MessageType_PassphraseRequest = 41 [(wire_out) = true];
+ MessageType_PassphraseAck = 42 [(wire_in) = true, (wire_tiny) = true, (wire_no_fsm) = true];
+ MessageType_PassphraseStateRequest = 77 [(wire_out) = true];
+ MessageType_PassphraseStateAck = 78 [(wire_in) = true, (wire_tiny) = true, (wire_no_fsm) = true];
+ MessageType_RecoveryDevice = 45 [(wire_in) = true];
+ MessageType_WordRequest = 46 [(wire_out) = true];
+ MessageType_WordAck = 47 [(wire_in) = true];
+ MessageType_GetFeatures = 55 [(wire_in) = true];
+ MessageType_SetU2FCounter = 63 [(wire_in) = true];
+
+ // Bootloader
+ MessageType_FirmwareErase = 6 [(wire_in) = true, (wire_bootloader) = true];
+ MessageType_FirmwareUpload = 7 [(wire_in) = true, (wire_bootloader) = true];
+ MessageType_FirmwareRequest = 8 [(wire_out) = true, (wire_bootloader) = true];
+ MessageType_SelfTest = 32 [(wire_in) = true, (wire_bootloader) = true];
+
+ // Bitcoin
+ MessageType_GetPublicKey = 11 [(wire_in) = true];
+ MessageType_PublicKey = 12 [(wire_out) = true];
+ MessageType_SignTx = 15 [(wire_in) = true];
+ MessageType_TxRequest = 21 [(wire_out) = true];
+ MessageType_TxAck = 22 [(wire_in) = true];
+ MessageType_GetAddress = 29 [(wire_in) = true];
+ MessageType_Address = 30 [(wire_out) = true];
+ MessageType_SignMessage = 38 [(wire_in) = true];
+ MessageType_VerifyMessage = 39 [(wire_in) = true];
+ MessageType_MessageSignature = 40 [(wire_out) = true];
+
+ // Crypto
+ MessageType_CipherKeyValue = 23 [(wire_in) = true];
+ MessageType_CipheredKeyValue = 48 [(wire_out) = true];
+ MessageType_SignIdentity = 53 [(wire_in) = true];
+ MessageType_SignedIdentity = 54 [(wire_out) = true];
+ MessageType_GetECDHSessionKey = 61 [(wire_in) = true];
+ MessageType_ECDHSessionKey = 62 [(wire_out) = true];
+ MessageType_CosiCommit = 71 [(wire_in) = true];
+ MessageType_CosiCommitment = 72 [(wire_out) = true];
+ MessageType_CosiSign = 73 [(wire_in) = true];
+ MessageType_CosiSignature = 74 [(wire_out) = true];
+
+ // Debug
+ MessageType_DebugLinkDecision = 100 [(wire_debug_in) = true, (wire_tiny) = true, (wire_no_fsm) = true];
+ MessageType_DebugLinkGetState = 101 [(wire_debug_in) = true, (wire_tiny) = true];
+ MessageType_DebugLinkState = 102 [(wire_debug_out) = true];
+ MessageType_DebugLinkStop = 103 [(wire_debug_in) = true];
+ MessageType_DebugLinkLog = 104 [(wire_debug_out) = true];
+ MessageType_DebugLinkMemoryRead = 110 [(wire_debug_in) = true];
+ MessageType_DebugLinkMemory = 111 [(wire_debug_out) = true];
+ MessageType_DebugLinkMemoryWrite = 112 [(wire_debug_in) = true];
+ MessageType_DebugLinkFlashErase = 113 [(wire_debug_in) = true];
+
+ // Ethereum
+ MessageType_EthereumGetPublicKey = 450 [(wire_in) = true];
+ MessageType_EthereumPublicKey = 451 [(wire_out) = true];
+ MessageType_EthereumGetAddress = 56 [(wire_in) = true];
+ MessageType_EthereumAddress = 57 [(wire_out) = true];
+ MessageType_EthereumSignTx = 58 [(wire_in) = true];
+ MessageType_EthereumTxRequest = 59 [(wire_out) = true];
+ MessageType_EthereumTxAck = 60 [(wire_in) = true];
+ MessageType_EthereumSignMessage = 64 [(wire_in) = true];
+ MessageType_EthereumVerifyMessage = 65 [(wire_in) = true];
+ MessageType_EthereumMessageSignature = 66 [(wire_out) = true];
+
+ // NEM
+ MessageType_NEMGetAddress = 67 [(wire_in) = true];
+ MessageType_NEMAddress = 68 [(wire_out) = true];
+ MessageType_NEMSignTx = 69 [(wire_in) = true];
+ MessageType_NEMSignedTx = 70 [(wire_out) = true];
+ MessageType_NEMDecryptMessage = 75 [(wire_in) = true];
+ MessageType_NEMDecryptedMessage = 76 [(wire_out) = true];
+
+ // Lisk
+ MessageType_LiskGetAddress = 114 [(wire_in) = true];
+ MessageType_LiskAddress = 115 [(wire_out) = true];
+ MessageType_LiskSignTx = 116 [(wire_in) = true];
+ MessageType_LiskSignedTx = 117 [(wire_out) = true];
+ MessageType_LiskSignMessage = 118 [(wire_in) = true];
+ MessageType_LiskMessageSignature = 119 [(wire_out) = true];
+ MessageType_LiskVerifyMessage = 120 [(wire_in) = true];
+ MessageType_LiskGetPublicKey = 121 [(wire_in) = true];
+ MessageType_LiskPublicKey = 122 [(wire_out) = true];
+
+ // Tezos
+ MessageType_TezosGetAddress = 150 [(wire_in) = true];
+ MessageType_TezosAddress = 151 [(wire_out) = true];
+ MessageType_TezosSignTx = 152 [(wire_in) = true];
+ MessageType_TezosSignedTx = 153 [(wire_out) = true];
+ MessageType_TezosGetPublicKey = 154 [(wire_in) = true];
+ MessageType_TezosPublicKey = 155 [(wire_out) = true];
+
+ // Stellar
+ MessageType_StellarSignTx = 202 [(wire_in) = true];
+ MessageType_StellarTxOpRequest = 203 [(wire_out) = true];
+ MessageType_StellarGetAddress = 207 [(wire_in) = true];
+ MessageType_StellarAddress = 208 [(wire_out) = true];
+ MessageType_StellarCreateAccountOp = 210 [(wire_in) = true];
+ MessageType_StellarPaymentOp = 211 [(wire_in) = true];
+ MessageType_StellarPathPaymentOp = 212 [(wire_in) = true];
+ MessageType_StellarManageOfferOp = 213 [(wire_in) = true];
+ MessageType_StellarCreatePassiveOfferOp = 214 [(wire_in) = true];
+ MessageType_StellarSetOptionsOp = 215 [(wire_in) = true];
+ MessageType_StellarChangeTrustOp = 216 [(wire_in) = true];
+ MessageType_StellarAllowTrustOp = 217 [(wire_in) = true];
+ MessageType_StellarAccountMergeOp = 218 [(wire_in) = true];
+ // omitted: StellarInflationOp is not a supported operation, would be 219
+ MessageType_StellarManageDataOp = 220 [(wire_in) = true];
+ MessageType_StellarBumpSequenceOp = 221 [(wire_in) = true];
+ MessageType_StellarSignedTx = 230 [(wire_out) = true];
+
+ // TRON
+ MessageType_TronGetAddress = 250 [(wire_in) = true];
+ MessageType_TronAddress = 251 [(wire_out) = true];
+ MessageType_TronSignTx = 252 [(wire_in) = true];
+ MessageType_TronSignedTx = 253 [(wire_out) = true];
+
+ // Cardano
+ // dropped Sign/VerifyMessage ids 300-302
+ MessageType_CardanoSignTx = 303 [(wire_in) = true];
+ MessageType_CardanoTxRequest = 304 [(wire_out) = true];
+ MessageType_CardanoGetPublicKey = 305 [(wire_in) = true];
+ MessageType_CardanoPublicKey = 306 [(wire_out) = true];
+ MessageType_CardanoGetAddress = 307 [(wire_in) = true];
+ MessageType_CardanoAddress = 308 [(wire_out) = true];
+ MessageType_CardanoTxAck = 309 [(wire_in) = true];
+ MessageType_CardanoSignedTx = 310 [(wire_out) = true];
+
+ // Ontology
+ MessageType_OntologyGetAddress = 350 [(wire_in) = true];
+ MessageType_OntologyAddress = 351 [(wire_out) = true];
+ MessageType_OntologyGetPublicKey = 352 [(wire_in) = true];
+ MessageType_OntologyPublicKey = 353 [(wire_out) = true];
+ MessageType_OntologySignTransfer = 354 [(wire_in) = true];
+ MessageType_OntologySignedTransfer = 355 [(wire_out) = true];
+ MessageType_OntologySignWithdrawOng = 356 [(wire_in) = true];
+ MessageType_OntologySignedWithdrawOng = 357 [(wire_out) = true];
+ MessageType_OntologySignOntIdRegister = 358 [(wire_in) = true];
+ MessageType_OntologySignedOntIdRegister = 359 [(wire_out) = true];
+ MessageType_OntologySignOntIdAddAttributes = 360 [(wire_in) = true];
+ MessageType_OntologySignedOntIdAddAttributes = 361 [(wire_out) = true];
+
+ // Ripple
+ MessageType_RippleGetAddress = 400 [(wire_in) = true];
+ MessageType_RippleAddress = 401 [(wire_out) = true];
+ MessageType_RippleSignTx = 402 [(wire_in) = true];
+ MessageType_RippleSignedTx = 403 [(wire_in) = true];
+
+ // Monero
+ MessageType_MoneroTransactionInitRequest = 501 [(wire_out) = true];
+ MessageType_MoneroTransactionInitAck = 502 [(wire_out) = true];
+ MessageType_MoneroTransactionSetInputRequest = 503 [(wire_out) = true];
+ MessageType_MoneroTransactionSetInputAck = 504 [(wire_out) = true];
+ MessageType_MoneroTransactionInputsPermutationRequest = 505 [(wire_out) = true];
+ MessageType_MoneroTransactionInputsPermutationAck = 506 [(wire_out) = true];
+ MessageType_MoneroTransactionInputViniRequest = 507 [(wire_out) = true];
+ MessageType_MoneroTransactionInputViniAck = 508 [(wire_out) = true];
+ MessageType_MoneroTransactionAllInputsSetRequest = 509 [(wire_out) = true];
+ MessageType_MoneroTransactionAllInputsSetAck = 510 [(wire_out) = true];
+ MessageType_MoneroTransactionSetOutputRequest = 511 [(wire_out) = true];
+ MessageType_MoneroTransactionSetOutputAck = 512 [(wire_out) = true];
+ MessageType_MoneroTransactionAllOutSetRequest = 513 [(wire_out) = true];
+ MessageType_MoneroTransactionAllOutSetAck = 514 [(wire_out) = true];
+ MessageType_MoneroTransactionSignInputRequest = 515 [(wire_out) = true];
+ MessageType_MoneroTransactionSignInputAck = 516 [(wire_out) = true];
+ MessageType_MoneroTransactionFinalRequest = 517 [(wire_out) = true];
+ MessageType_MoneroTransactionFinalAck = 518 [(wire_out) = true];
+ MessageType_MoneroKeyImageExportInitRequest = 530 [(wire_out) = true];
+ MessageType_MoneroKeyImageExportInitAck = 531 [(wire_out) = true];
+ MessageType_MoneroKeyImageSyncStepRequest = 532 [(wire_out) = true];
+ MessageType_MoneroKeyImageSyncStepAck = 533 [(wire_out) = true];
+ MessageType_MoneroKeyImageSyncFinalRequest = 534 [(wire_out) = true];
+ MessageType_MoneroKeyImageSyncFinalAck = 535 [(wire_out) = true];
+ MessageType_MoneroGetAddress = 540 [(wire_in) = true];
+ MessageType_MoneroAddress = 541 [(wire_out) = true];
+ MessageType_MoneroGetWatchKey = 542 [(wire_in) = true];
+ MessageType_MoneroWatchKey = 543 [(wire_out) = true];
+ MessageType_DebugMoneroDiagRequest = 546 [(wire_in) = true];
+ MessageType_DebugMoneroDiagAck = 547 [(wire_out) = true];
+ MessageType_MoneroGetTxKeyRequest = 550 [(wire_in) = true];
+ MessageType_MoneroGetTxKeyAck = 551 [(wire_out) = true];
+ MessageType_MoneroLiveRefreshStartRequest = 552 [(wire_in) = true];
+ MessageType_MoneroLiveRefreshStartAck = 553 [(wire_out) = true];
+ MessageType_MoneroLiveRefreshStepRequest = 554 [(wire_in) = true];
+ MessageType_MoneroLiveRefreshStepAck = 555 [(wire_out) = true];
+ MessageType_MoneroLiveRefreshFinalRequest = 556 [(wire_in) = true];
+ MessageType_MoneroLiveRefreshFinalAck = 557 [(wire_out) = true];
+
+ // EOS
+ MessageType_EosGetPublicKey = 600 [(wire_in) = true];
+ MessageType_EosPublicKey = 601 [(wire_out) = true];
+ MessageType_EosSignTx = 602 [(wire_in) = true];
+ MessageType_EosTxActionRequest = 603 [(wire_out) = true];
+ MessageType_EosTxActionAck = 604 [(wire_in) = true];
+ MessageType_EosSignedTx = 605 [(wire_out) = true];
+
+ // Binance
+ MessageType_BinanceGetAddress = 700 [(wire_in) = true];
+ MessageType_BinanceAddress = 701 [(wire_out) = true];
+ MessageType_BinanceGetPublicKey = 702 [(wire_in) = true];
+ MessageType_BinancePublicKey = 703 [(wire_out) = true];
+ MessageType_BinanceSignTx = 704 [(wire_in) = true];
+ MessageType_BinanceTxRequest = 705 [(wire_out) = true];
+ MessageType_BinanceTransferMsg = 706 [(wire_in) = true];
+ MessageType_BinanceOrderMsg = 707 [(wire_in) = true];
+ MessageType_BinanceCancelMsg = 708 [(wire_in) = true];
+ MessageType_BinanceSignedTx = 709 [(wire_out) = true];
+}
diff --git a/accounts/usbwallet/trezor/trezor.go b/accounts/usbwallet/trezor/trezor.go
new file mode 100644
index 0000000..7e756e6
--- /dev/null
+++ b/accounts/usbwallet/trezor/trezor.go
@@ -0,0 +1,70 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// This file contains the implementation for interacting with the Trezor hardware
+// wallets. The wire protocol spec can be found on the SatoshiLabs website:
+// https://wiki.trezor.io/Developers_guide-Message_Workflows
+
+// !!! STAHP !!!
+//
+// Before you touch the protocol files, you need to be aware of a breaking change
+// that occurred between firmware versions 1.7.3->1.8.0 (Model One) and 2.0.10->
+// 2.1.0 (Model T). The Ethereum address representation was changed from the 20
+// byte binary blob to a 42 byte hex string. The upstream protocol buffer files
+// only support the new format, so blindly pulling in a new spec will break old
+// devices!
+//
+// The Trezor devs had the foresight to add the string version as a new message
+// code instead of replacing the binary one. This means that the proto file can
+// actually define both the old and the new versions as optional. Please ensure
+// that you add back the old addresses everywhere (to avoid name clash. use the
+// addressBin and addressHex names).
+//
+// If in doubt, reach out to @karalabe.
+
+// To regenerate the protocol files in this package:
+// - Download the latest protoc https://github.com/protocolbuffers/protobuf/releases
+// - Build with the usual `./configure && make` and ensure it's on your $PATH
+// - Delete all the .proto and .pb.go files, pull in fresh ones from Trezor
+// - Grab the latest Go plugin `go get -u github.com/golang/protobuf/protoc-gen-go`
+// - Vendor in the latest Go plugin `govendor fetch github.com/golang/protobuf/...`
+
+//go:generate protoc -I/usr/local/include:. --go_out=import_path=trezor:. messages.proto messages-common.proto messages-management.proto messages-ethereum.proto
+
+// Package trezor contains the wire protocol.
+package trezor
+
+import (
+ "reflect"
+
+ "github.com/golang/protobuf/proto"
+)
+
+// Type returns the protocol buffer type number of a specific message. If the
+// message is nil, this method panics!
+func Type(msg proto.Message) uint16 {
+ return uint16(MessageType_value["MessageType_"+reflect.TypeOf(msg).Elem().Name()])
+}
+
+// Name returns the friendly message type name of a specific protocol buffer
+// type number.
+func Name(kind uint16) string {
+ name := MessageType_name[int32(kind)]
+ if len(name) < 12 {
+ return name
+ }
+ return name[12:]
+}
diff --git a/accounts/usbwallet/wallet.go b/accounts/usbwallet/wallet.go
new file mode 100644
index 0000000..3622c92
--- /dev/null
+++ b/accounts/usbwallet/wallet.go
@@ -0,0 +1,595 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package usbwallet implements support for USB hardware wallets.
+package usbwallet
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "math/big"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/types"
+ ethereum "github.com/ava-labs/go-ethereum"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/karalabe/usb"
+)
+
+// Maximum time between wallet health checks to detect USB unplugs.
+const heartbeatCycle = time.Second
+
+// Minimum time to wait between self derivation attempts, even it the user is
+// requesting accounts like crazy.
+const selfDeriveThrottling = time.Second
+
+// driver defines the vendor specific functionality hardware wallets instances
+// must implement to allow using them with the wallet lifecycle management.
+type driver interface {
+ // Status returns a textual status to aid the user in the current state of the
+ // wallet. It also returns an error indicating any failure the wallet might have
+ // encountered.
+ Status() (string, error)
+
+ // Open initializes access to a wallet instance. The passphrase parameter may
+ // or may not be used by the implementation of a particular wallet instance.
+ Open(device io.ReadWriter, passphrase string) error
+
+ // Close releases any resources held by an open wallet instance.
+ Close() error
+
+ // Heartbeat performs a sanity check against the hardware wallet to see if it
+ // is still online and healthy.
+ Heartbeat() error
+
+ // Derive sends a derivation request to the USB device and returns the Ethereum
+ // address located on that path.
+ Derive(path accounts.DerivationPath) (common.Address, error)
+
+ // SignTx sends the transaction to the USB device and waits for the user to confirm
+ // or deny the transaction.
+ SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error)
+}
+
+// wallet represents the common functionality shared by all USB hardware
+// wallets to prevent reimplementing the same complex maintenance mechanisms
+// for different vendors.
+type wallet struct {
+ hub *Hub // USB hub scanning
+ driver driver // Hardware implementation of the low level device operations
+ url *accounts.URL // Textual URL uniquely identifying this wallet
+
+ info usb.DeviceInfo // Known USB device infos about the wallet
+ device usb.Device // USB device advertising itself as a hardware wallet
+
+ accounts []accounts.Account // List of derive accounts pinned on the hardware wallet
+ paths map[common.Address]accounts.DerivationPath // Known derivation paths for signing operations
+
+ deriveNextPaths []accounts.DerivationPath // Next derivation paths for account auto-discovery (multiple bases supported)
+ deriveNextAddrs []common.Address // Next derived account addresses for auto-discovery (multiple bases supported)
+ deriveChain ethereum.ChainStateReader // Blockchain state reader to discover used account with
+ deriveReq chan chan struct{} // Channel to request a self-derivation on
+ deriveQuit chan chan error // Channel to terminate the self-deriver with
+
+ healthQuit chan chan error
+
+ // Locking a hardware wallet is a bit special. Since hardware devices are lower
+ // performing, any communication with them might take a non negligible amount of
+ // time. Worse still, waiting for user confirmation can take arbitrarily long,
+ // but exclusive communication must be upheld during. Locking the entire wallet
+ // in the mean time however would stall any parts of the system that don't want
+ // to communicate, just read some state (e.g. list the accounts).
+ //
+ // As such, a hardware wallet needs two locks to function correctly. A state
+ // lock can be used to protect the wallet's software-side internal state, which
+ // must not be held exclusively during hardware communication. A communication
+ // lock can be used to achieve exclusive access to the device itself, this one
+ // however should allow "skipping" waiting for operations that might want to
+ // use the device, but can live without too (e.g. account self-derivation).
+ //
+ // Since we have two locks, it's important to know how to properly use them:
+ // - Communication requires the `device` to not change, so obtaining the
+ // commsLock should be done after having a stateLock.
+ // - Communication must not disable read access to the wallet state, so it
+ // must only ever hold a *read* lock to stateLock.
+ commsLock chan struct{} // Mutex (buf=1) for the USB comms without keeping the state locked
+ stateLock sync.RWMutex // Protects read and write access to the wallet struct fields
+
+ log log.Logger // Contextual logger to tag the base with its id
+}
+
+// URL implements accounts.Wallet, returning the URL of the USB hardware device.
+func (w *wallet) URL() accounts.URL {
+ return *w.url // Immutable, no need for a lock
+}
+
+// Status implements accounts.Wallet, returning a custom status message from the
+// underlying vendor-specific hardware wallet implementation.
+func (w *wallet) Status() (string, error) {
+ w.stateLock.RLock() // No device communication, state lock is enough
+ defer w.stateLock.RUnlock()
+
+ status, failure := w.driver.Status()
+ if w.device == nil {
+ return "Closed", failure
+ }
+ return status, failure
+}
+
+// Open implements accounts.Wallet, attempting to open a USB connection to the
+// hardware wallet.
+func (w *wallet) Open(passphrase string) error {
+ w.stateLock.Lock() // State lock is enough since there's no connection yet at this point
+ defer w.stateLock.Unlock()
+
+ // If the device was already opened once, refuse to try again
+ if w.paths != nil {
+ return accounts.ErrWalletAlreadyOpen
+ }
+ // Make sure the actual device connection is done only once
+ if w.device == nil {
+ device, err := w.info.Open()
+ if err != nil {
+ return err
+ }
+ w.device = device
+ w.commsLock = make(chan struct{}, 1)
+ w.commsLock <- struct{}{} // Enable lock
+ }
+ // Delegate device initialization to the underlying driver
+ if err := w.driver.Open(w.device, passphrase); err != nil {
+ return err
+ }
+ // Connection successful, start life-cycle management
+ w.paths = make(map[common.Address]accounts.DerivationPath)
+
+ w.deriveReq = make(chan chan struct{})
+ w.deriveQuit = make(chan chan error)
+ w.healthQuit = make(chan chan error)
+
+ go w.heartbeat()
+ go w.selfDerive()
+
+ // Notify anyone listening for wallet events that a new device is accessible
+ go w.hub.updateFeed.Send(accounts.WalletEvent{Wallet: w, Kind: accounts.WalletOpened})
+
+ return nil
+}
+
+// heartbeat is a health check loop for the USB wallets to periodically verify
+// whether they are still present or if they malfunctioned.
+func (w *wallet) heartbeat() {
+ w.log.Debug("USB wallet health-check started")
+ defer w.log.Debug("USB wallet health-check stopped")
+
+ // Execute heartbeat checks until termination or error
+ var (
+ errc chan error
+ err error
+ )
+ for errc == nil && err == nil {
+ // Wait until termination is requested or the heartbeat cycle arrives
+ select {
+ case errc = <-w.healthQuit:
+ // Termination requested
+ continue
+ case <-time.After(heartbeatCycle):
+ // Heartbeat time
+ }
+ // Execute a tiny data exchange to see responsiveness
+ w.stateLock.RLock()
+ if w.device == nil {
+ // Terminated while waiting for the lock
+ w.stateLock.RUnlock()
+ continue
+ }
+ <-w.commsLock // Don't lock state while resolving version
+ err = w.driver.Heartbeat()
+ w.commsLock <- struct{}{}
+ w.stateLock.RUnlock()
+
+ if err != nil {
+ w.stateLock.Lock() // Lock state to tear the wallet down
+ w.close()
+ w.stateLock.Unlock()
+ }
+ // Ignore non hardware related errors
+ err = nil
+ }
+ // In case of error, wait for termination
+ if err != nil {
+ w.log.Debug("USB wallet health-check failed", "err", err)
+ errc = <-w.healthQuit
+ }
+ errc <- err
+}
+
+// Close implements accounts.Wallet, closing the USB connection to the device.
+func (w *wallet) Close() error {
+ // Ensure the wallet was opened
+ w.stateLock.RLock()
+ hQuit, dQuit := w.healthQuit, w.deriveQuit
+ w.stateLock.RUnlock()
+
+ // Terminate the health checks
+ var herr error
+ if hQuit != nil {
+ errc := make(chan error)
+ hQuit <- errc
+ herr = <-errc // Save for later, we *must* close the USB
+ }
+ // Terminate the self-derivations
+ var derr error
+ if dQuit != nil {
+ errc := make(chan error)
+ dQuit <- errc
+ derr = <-errc // Save for later, we *must* close the USB
+ }
+ // Terminate the device connection
+ w.stateLock.Lock()
+ defer w.stateLock.Unlock()
+
+ w.healthQuit = nil
+ w.deriveQuit = nil
+ w.deriveReq = nil
+
+ if err := w.close(); err != nil {
+ return err
+ }
+ if herr != nil {
+ return herr
+ }
+ return derr
+}
+
+// close is the internal wallet closer that terminates the USB connection and
+// resets all the fields to their defaults.
+//
+// Note, close assumes the state lock is held!
+func (w *wallet) close() error {
+ // Allow duplicate closes, especially for health-check failures
+ if w.device == nil {
+ return nil
+ }
+ // Close the device, clear everything, then return
+ w.device.Close()
+ w.device = nil
+
+ w.accounts, w.paths = nil, nil
+ return w.driver.Close()
+}
+
+// Accounts implements accounts.Wallet, returning the list of accounts pinned to
+// the USB hardware wallet. If self-derivation was enabled, the account list is
+// periodically expanded based on current chain state.
+func (w *wallet) Accounts() []accounts.Account {
+ // Attempt self-derivation if it's running
+ reqc := make(chan struct{}, 1)
+ select {
+ case w.deriveReq <- reqc:
+ // Self-derivation request accepted, wait for it
+ <-reqc
+ default:
+ // Self-derivation offline, throttled or busy, skip
+ }
+ // Return whatever account list we ended up with
+ w.stateLock.RLock()
+ defer w.stateLock.RUnlock()
+
+ cpy := make([]accounts.Account, len(w.accounts))
+ copy(cpy, w.accounts)
+ return cpy
+}
+
+// selfDerive is an account derivation loop that upon request attempts to find
+// new non-zero accounts.
+func (w *wallet) selfDerive() {
+ w.log.Debug("USB wallet self-derivation started")
+ defer w.log.Debug("USB wallet self-derivation stopped")
+
+ // Execute self-derivations until termination or error
+ var (
+ reqc chan struct{}
+ errc chan error
+ err error
+ )
+ for errc == nil && err == nil {
+ // Wait until either derivation or termination is requested
+ select {
+ case errc = <-w.deriveQuit:
+ // Termination requested
+ continue
+ case reqc = <-w.deriveReq:
+ // Account discovery requested
+ }
+ // Derivation needs a chain and device access, skip if either unavailable
+ w.stateLock.RLock()
+ if w.device == nil || w.deriveChain == nil {
+ w.stateLock.RUnlock()
+ reqc <- struct{}{}
+ continue
+ }
+ select {
+ case <-w.commsLock:
+ default:
+ w.stateLock.RUnlock()
+ reqc <- struct{}{}
+ continue
+ }
+ // Device lock obtained, derive the next batch of accounts
+ var (
+ accs []accounts.Account
+ paths []accounts.DerivationPath
+
+ nextPaths = append([]accounts.DerivationPath{}, w.deriveNextPaths...)
+ nextAddrs = append([]common.Address{}, w.deriveNextAddrs...)
+
+ context = context.Background()
+ )
+ for i := 0; i < len(nextAddrs); i++ {
+ for empty := false; !empty; {
+ // Retrieve the next derived Ethereum account
+ if nextAddrs[i] == (common.Address{}) {
+ if nextAddrs[i], err = w.driver.Derive(nextPaths[i]); err != nil {
+ w.log.Warn("USB wallet account derivation failed", "err", err)
+ break
+ }
+ }
+ // Check the account's status against the current chain state
+ var (
+ balance *big.Int
+ nonce uint64
+ )
+ balance, err = w.deriveChain.BalanceAt(context, nextAddrs[i], nil)
+ if err != nil {
+ w.log.Warn("USB wallet balance retrieval failed", "err", err)
+ break
+ }
+ nonce, err = w.deriveChain.NonceAt(context, nextAddrs[i], nil)
+ if err != nil {
+ w.log.Warn("USB wallet nonce retrieval failed", "err", err)
+ break
+ }
+ // If the next account is empty, stop self-derivation, but add for the last base path
+ if balance.Sign() == 0 && nonce == 0 {
+ empty = true
+ if i < len(nextAddrs)-1 {
+ break
+ }
+ }
+ // We've just self-derived a new account, start tracking it locally
+ path := make(accounts.DerivationPath, len(nextPaths[i]))
+ copy(path[:], nextPaths[i][:])
+ paths = append(paths, path)
+
+ account := accounts.Account{
+ Address: nextAddrs[i],
+ URL: accounts.URL{Scheme: w.url.Scheme, Path: fmt.Sprintf("%s/%s", w.url.Path, path)},
+ }
+ accs = append(accs, account)
+
+ // Display a log message to the user for new (or previously empty accounts)
+ if _, known := w.paths[nextAddrs[i]]; !known || (!empty && nextAddrs[i] == w.deriveNextAddrs[i]) {
+ w.log.Info("USB wallet discovered new account", "address", nextAddrs[i], "path", path, "balance", balance, "nonce", nonce)
+ }
+ // Fetch the next potential account
+ if !empty {
+ nextAddrs[i] = common.Address{}
+ nextPaths[i][len(nextPaths[i])-1]++
+ }
+ }
+ }
+ // Self derivation complete, release device lock
+ w.commsLock <- struct{}{}
+ w.stateLock.RUnlock()
+
+ // Insert any accounts successfully derived
+ w.stateLock.Lock()
+ for i := 0; i < len(accs); i++ {
+ if _, ok := w.paths[accs[i].Address]; !ok {
+ w.accounts = append(w.accounts, accs[i])
+ w.paths[accs[i].Address] = paths[i]
+ }
+ }
+ // Shift the self-derivation forward
+ // TODO(karalabe): don't overwrite changes from wallet.SelfDerive
+ w.deriveNextAddrs = nextAddrs
+ w.deriveNextPaths = nextPaths
+ w.stateLock.Unlock()
+
+ // Notify the user of termination and loop after a bit of time (to avoid trashing)
+ reqc <- struct{}{}
+ if err == nil {
+ select {
+ case errc = <-w.deriveQuit:
+ // Termination requested, abort
+ case <-time.After(selfDeriveThrottling):
+ // Waited enough, willing to self-derive again
+ }
+ }
+ }
+ // In case of error, wait for termination
+ if err != nil {
+ w.log.Debug("USB wallet self-derivation failed", "err", err)
+ errc = <-w.deriveQuit
+ }
+ errc <- err
+}
+
+// Contains implements accounts.Wallet, returning whether a particular account is
+// or is not pinned into this wallet instance. Although we could attempt to resolve
+// unpinned accounts, that would be an non-negligible hardware operation.
+func (w *wallet) Contains(account accounts.Account) bool {
+ w.stateLock.RLock()
+ defer w.stateLock.RUnlock()
+
+ _, exists := w.paths[account.Address]
+ return exists
+}
+
+// Derive implements accounts.Wallet, deriving a new account at the specific
+// derivation path. If pin is set to true, the account will be added to the list
+// of tracked accounts.
+func (w *wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
+ // Try to derive the actual account and update its URL if successful
+ w.stateLock.RLock() // Avoid device disappearing during derivation
+
+ if w.device == nil {
+ w.stateLock.RUnlock()
+ return accounts.Account{}, accounts.ErrWalletClosed
+ }
+ <-w.commsLock // Avoid concurrent hardware access
+ address, err := w.driver.Derive(path)
+ w.commsLock <- struct{}{}
+
+ w.stateLock.RUnlock()
+
+ // If an error occurred or no pinning was requested, return
+ if err != nil {
+ return accounts.Account{}, err
+ }
+ account := accounts.Account{
+ Address: address,
+ URL: accounts.URL{Scheme: w.url.Scheme, Path: fmt.Sprintf("%s/%s", w.url.Path, path)},
+ }
+ if !pin {
+ return account, nil
+ }
+ // Pinning needs to modify the state
+ w.stateLock.Lock()
+ defer w.stateLock.Unlock()
+
+ if _, ok := w.paths[address]; !ok {
+ w.accounts = append(w.accounts, account)
+ w.paths[address] = make(accounts.DerivationPath, len(path))
+ copy(w.paths[address], path)
+ }
+ return account, nil
+}
+
+// SelfDerive sets a base account derivation path from which the wallet attempts
+// to discover non zero accounts and automatically add them to list of tracked
+// accounts.
+//
+// Note, self derivaton will increment the last component of the specified path
+// opposed to decending into a child path to allow discovering accounts starting
+// from non zero components.
+//
+// Some hardware wallets switched derivation paths through their evolution, so
+// this method supports providing multiple bases to discover old user accounts
+// too. Only the last base will be used to derive the next empty account.
+//
+// You can disable automatic account discovery by calling SelfDerive with a nil
+// chain state reader.
+func (w *wallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
+ w.stateLock.Lock()
+ defer w.stateLock.Unlock()
+
+ w.deriveNextPaths = make([]accounts.DerivationPath, len(bases))
+ for i, base := range bases {
+ w.deriveNextPaths[i] = make(accounts.DerivationPath, len(base))
+ copy(w.deriveNextPaths[i][:], base[:])
+ }
+ w.deriveNextAddrs = make([]common.Address, len(bases))
+ w.deriveChain = chain
+}
+
+// signHash implements accounts.Wallet, however signing arbitrary data is not
+// supported for hardware wallets, so this method will always return an error.
+func (w *wallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
+ return nil, accounts.ErrNotSupported
+}
+
+// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
+func (w *wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
+ return w.signHash(account, crypto.Keccak256(data))
+}
+
+// SignDataWithPassphrase implements accounts.Wallet, attempting to sign the given
+// data with the given account using passphrase as extra authentication.
+// Since USB wallets don't rely on passphrases, these are silently ignored.
+func (w *wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
+ return w.SignData(account, mimeType, data)
+}
+
+func (w *wallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
+ return w.signHash(account, accounts.TextHash(text))
+}
+
+// SignTx implements accounts.Wallet. It sends the transaction over to the Ledger
+// wallet to request a confirmation from the user. It returns either the signed
+// transaction or a failure if the user denied the transaction.
+//
+// Note, if the version of the Ethereum application running on the Ledger wallet is
+// too old to sign EIP-155 transactions, but such is requested nonetheless, an error
+// will be returned opposed to silently signing in Homestead mode.
+func (w *wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ w.stateLock.RLock() // Comms have own mutex, this is for the state fields
+ defer w.stateLock.RUnlock()
+
+ // If the wallet is closed, abort
+ if w.device == nil {
+ return nil, accounts.ErrWalletClosed
+ }
+ // Make sure the requested account is contained within
+ path, ok := w.paths[account.Address]
+ if !ok {
+ return nil, accounts.ErrUnknownAccount
+ }
+ // All infos gathered and metadata checks out, request signing
+ <-w.commsLock
+ defer func() { w.commsLock <- struct{}{} }()
+
+ // Ensure the device isn't screwed with while user confirmation is pending
+ // TODO(karalabe): remove if hotplug lands on Windows
+ w.hub.commsLock.Lock()
+ w.hub.commsPend++
+ w.hub.commsLock.Unlock()
+
+ defer func() {
+ w.hub.commsLock.Lock()
+ w.hub.commsPend--
+ w.hub.commsLock.Unlock()
+ }()
+ // Sign the transaction and verify the sender to avoid hardware fault surprises
+ sender, signed, err := w.driver.SignTx(path, tx, chainID)
+ if err != nil {
+ return nil, err
+ }
+ if sender != account.Address {
+ return nil, fmt.Errorf("signer mismatch: expected %s, got %s", account.Address.Hex(), sender.Hex())
+ }
+ return signed, nil
+}
+
+// SignHashWithPassphrase implements accounts.Wallet, however signing arbitrary
+// data is not supported for Ledger wallets, so this method will always return
+// an error.
+func (w *wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
+ return w.SignText(account, accounts.TextHash(text))
+}
+
+// SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
+// transaction with the given account using passphrase as extra authentication.
+// Since USB wallets don't rely on passphrases, these are silently ignored.
+func (w *wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
+ return w.SignTx(account, tx, chainID)
+}
diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go
deleted file mode 100644
index 97b5870..0000000
--- a/cmd/geth/chaincmd.go
+++ /dev/null
@@ -1,559 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-package geth
-
-import (
- "encoding/json"
- "fmt"
- "os"
- "path/filepath"
- "runtime"
- "strconv"
- "sync/atomic"
- "time"
-
- "github.com/ava-labs/coreth/cmd/utils"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/console"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/eth/downloader"
- "github.com/ava-labs/go-ethereum/event"
- "github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/trie"
- "gopkg.in/urfave/cli.v1"
-)
-
-var (
- initCommand = cli.Command{
- Action: utils.MigrateFlags(initGenesis),
- Name: "init",
- Usage: "Bootstrap and initialize a new genesis block",
- ArgsUsage: "<genesisPath>",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
-The init command initializes a new genesis block and definition for the network.
-This is a destructive action and changes the network in which you will be
-participating.
-
-It expects the genesis file as argument.`,
- }
- importCommand = cli.Command{
- Action: utils.MigrateFlags(importChain),
- Name: "import",
- Usage: "Import a blockchain file",
- ArgsUsage: "<filename> (<filename 2> ... <filename N>) ",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- utils.CacheFlag,
- utils.SyncModeFlag,
- utils.GCModeFlag,
- utils.CacheDatabaseFlag,
- utils.CacheGCFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
-The import command imports blocks from an RLP-encoded form. The form can be one file
-with several RLP-encoded blocks, or several files can be used.
-
-If only one file is used, import error will result in failure. If several files are used,
-processing will proceed even if an individual RLP-file import failure occurs.`,
- }
- exportCommand = cli.Command{
- Action: utils.MigrateFlags(exportChain),
- Name: "export",
- Usage: "Export blockchain into file",
- ArgsUsage: "<filename> [<blockNumFirst> <blockNumLast>]",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- utils.CacheFlag,
- utils.SyncModeFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
-Requires a first argument of the file to write to.
-Optional second and third arguments control the first and
-last block to write. In this mode, the file will be appended
-if already existing. If the file ends with .gz, the output will
-be gzipped.`,
- }
- importPreimagesCommand = cli.Command{
- Action: utils.MigrateFlags(importPreimages),
- Name: "import-preimages",
- Usage: "Import the preimage database from an RLP stream",
- ArgsUsage: "<datafile>",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- utils.CacheFlag,
- utils.SyncModeFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
- The import-preimages command imports hash preimages from an RLP encoded stream.`,
- }
- exportPreimagesCommand = cli.Command{
- Action: utils.MigrateFlags(exportPreimages),
- Name: "export-preimages",
- Usage: "Export the preimage database into an RLP stream",
- ArgsUsage: "<dumpfile>",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- utils.CacheFlag,
- utils.SyncModeFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
-The export-preimages command export hash preimages to an RLP encoded stream`,
- }
- copydbCommand = cli.Command{
- Action: utils.MigrateFlags(copyDb),
- Name: "copydb",
- Usage: "Create a local chain from a target chaindata folder",
- ArgsUsage: "<sourceChaindataDir>",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- utils.CacheFlag,
- utils.SyncModeFlag,
- utils.FakePoWFlag,
- utils.TestnetFlag,
- utils.RinkebyFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
-The first argument must be the directory containing the blockchain to download from`,
- }
- removedbCommand = cli.Command{
- Action: utils.MigrateFlags(removeDB),
- Name: "removedb",
- Usage: "Remove blockchain and state databases",
- ArgsUsage: " ",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
-Remove blockchain and state databases`,
- }
- dumpCommand = cli.Command{
- Action: utils.MigrateFlags(dump),
- Name: "dump",
- Usage: "Dump a specific block from storage",
- ArgsUsage: "[<blockHash> | <blockNum>]...",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- utils.CacheFlag,
- utils.SyncModeFlag,
- utils.IterativeOutputFlag,
- utils.ExcludeCodeFlag,
- utils.ExcludeStorageFlag,
- utils.IncludeIncompletesFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- Description: `
-The arguments are interpreted as block numbers or hashes.
-Use "ethereum dump 0" to dump the genesis block.`,
- }
- inspectCommand = cli.Command{
- Action: utils.MigrateFlags(inspect),
- Name: "inspect",
- Usage: "Inspect the storage size for each type of data in the database",
- ArgsUsage: " ",
- Flags: []cli.Flag{
- utils.DataDirFlag,
- utils.AncientFlag,
- utils.CacheFlag,
- utils.TestnetFlag,
- utils.RinkebyFlag,
- utils.GoerliFlag,
- utils.SyncModeFlag,
- },
- Category: "BLOCKCHAIN COMMANDS",
- }
-)
-
-// initGenesis will initialise the given JSON format genesis file and writes it as
-// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
-func initGenesis(ctx *cli.Context) error {
- // Make sure we have a valid genesis JSON
- genesisPath := ctx.Args().First()
- if len(genesisPath) == 0 {
- utils.Fatalf("Must supply path to genesis JSON file")
- }
- file, err := os.Open(genesisPath)
- if err != nil {
- utils.Fatalf("Failed to read genesis file: %v", err)
- }
- defer file.Close()
-
- genesis := new(core.Genesis)
- if err := json.NewDecoder(file).Decode(genesis); err != nil {
- utils.Fatalf("invalid genesis file: %v", err)
- }
- // Open an initialise both full and light databases
- stack := makeFullNode(ctx)
- defer stack.Close()
-
- for _, name := range []string{"chaindata", "lightchaindata"} {
- chaindb, err := stack.OpenDatabase(name, 0, 0, "")
- if err != nil {
- utils.Fatalf("Failed to open database: %v", err)
- }
- _, hash, err := core.SetupGenesisBlock(chaindb, genesis)
- if err != nil {
- utils.Fatalf("Failed to write genesis block: %v", err)
- }
- chaindb.Close()
- log.Info("Successfully wrote genesis state", "database", name, "hash", hash)
- }
- return nil
-}
-
-func importChain(ctx *cli.Context) error {
- if len(ctx.Args()) < 1 {
- utils.Fatalf("This command requires an argument.")
- }
- stack := makeFullNode(ctx)
- defer stack.Close()
-
- chain, db := utils.MakeChain(ctx, stack)
- defer db.Close()
-
- // Start periodically gathering memory profiles
- var peakMemAlloc, peakMemSys uint64
- go func() {
- stats := new(runtime.MemStats)
- for {
- runtime.ReadMemStats(stats)
- if atomic.LoadUint64(&peakMemAlloc) < stats.Alloc {
- atomic.StoreUint64(&peakMemAlloc, stats.Alloc)
- }
- if atomic.LoadUint64(&peakMemSys) < stats.Sys {
- atomic.StoreUint64(&peakMemSys, stats.Sys)
- }
- time.Sleep(5 * time.Second)
- }
- }()
- // Import the chain
- start := time.Now()
-
- if len(ctx.Args()) == 1 {
- if err := utils.ImportChain(chain, ctx.Args().First()); err != nil {
- log.Error("Import error", "err", err)
- }
- } else {
- for _, arg := range ctx.Args() {
- if err := utils.ImportChain(chain, arg); err != nil {
- log.Error("Import error", "file", arg, "err", err)
- }
- }
- }
- chain.Stop()
- fmt.Printf("Import done in %v.\n\n", time.Since(start))
-
- // Output pre-compaction stats mostly to see the import trashing
- stats, err := db.Stat("leveldb.stats")
- if err != nil {
- utils.Fatalf("Failed to read database stats: %v", err)
- }
- fmt.Println(stats)
-
- ioStats, err := db.Stat("leveldb.iostats")
- if err != nil {
- utils.Fatalf("Failed to read database iostats: %v", err)
- }
- fmt.Println(ioStats)
-
- // Print the memory statistics used by the importing
- mem := new(runtime.MemStats)
- runtime.ReadMemStats(mem)
-
- fmt.Printf("Object memory: %.3f MB current, %.3f MB peak\n", float64(mem.Alloc)/1024/1024, float64(atomic.LoadUint64(&peakMemAlloc))/1024/1024)
- fmt.Printf("System memory: %.3f MB current, %.3f MB peak\n", float64(mem.Sys)/1024/1024, float64(atomic.LoadUint64(&peakMemSys))/1024/1024)
- fmt.Printf("Allocations: %.3f million\n", float64(mem.Mallocs)/1000000)
- fmt.Printf("GC pause: %v\n\n", time.Duration(mem.PauseTotalNs))
-
- if ctx.GlobalBool(utils.NoCompactionFlag.Name) {
- return nil
- }
-
- // Compact the entire database to more accurately measure disk io and print the stats
- start = time.Now()
- fmt.Println("Compacting entire database...")
- if err = db.Compact(nil, nil); err != nil {
- utils.Fatalf("Compaction failed: %v", err)
- }
- fmt.Printf("Compaction done in %v.\n\n", time.Since(start))
-
- stats, err = db.Stat("leveldb.stats")
- if err != nil {
- utils.Fatalf("Failed to read database stats: %v", err)
- }
- fmt.Println(stats)
-
- ioStats, err = db.Stat("leveldb.iostats")
- if err != nil {
- utils.Fatalf("Failed to read database iostats: %v", err)
- }
- fmt.Println(ioStats)
- return nil
-}
-
-func exportChain(ctx *cli.Context) error {
- if len(ctx.Args()) < 1 {
- utils.Fatalf("This command requires an argument.")
- }
- stack := makeFullNode(ctx)
- defer stack.Close()
-
- chain, _ := utils.MakeChain(ctx, stack)
- start := time.Now()
-
- var err error
- fp := ctx.Args().First()
- if len(ctx.Args()) < 3 {
- err = utils.ExportChain(chain, fp)
- } else {
- // This can be improved to allow for numbers larger than 9223372036854775807
- first, ferr := strconv.ParseInt(ctx.Args().Get(1), 10, 64)
- last, lerr := strconv.ParseInt(ctx.Args().Get(2), 10, 64)
- if ferr != nil || lerr != nil {
- utils.Fatalf("Export error in parsing parameters: block number not an integer\n")
- }
- if first < 0 || last < 0 {
- utils.Fatalf("Export error: block number must be greater than 0\n")
- }
- err = utils.ExportAppendChain(chain, fp, uint64(first), uint64(last))
- }
-
- if err != nil {
- utils.Fatalf("Export error: %v\n", err)
- }
- fmt.Printf("Export done in %v\n", time.Since(start))
- return nil
-}
-
-// importPreimages imports preimage data from the specified file.
-func importPreimages(ctx *cli.Context) error {
- if len(ctx.Args()) < 1 {
- utils.Fatalf("This command requires an argument.")
- }
- stack := makeFullNode(ctx)
- defer stack.Close()
-
- db := utils.MakeChainDatabase(ctx, stack)
- start := time.Now()
-
- if err := utils.ImportPreimages(db, ctx.Args().First()); err != nil {
- utils.Fatalf("Import error: %v\n", err)
- }
- fmt.Printf("Import done in %v\n", time.Since(start))
- return nil
-}
-
-// exportPreimages dumps the preimage data to specified json file in streaming way.
-func exportPreimages(ctx *cli.Context) error {
- if len(ctx.Args()) < 1 {
- utils.Fatalf("This command requires an argument.")
- }
- stack := makeFullNode(ctx)
- defer stack.Close()
-
- db := utils.MakeChainDatabase(ctx, stack)
- start := time.Now()
-
- if err := utils.ExportPreimages(db, ctx.Args().First()); err != nil {
- utils.Fatalf("Export error: %v\n", err)
- }
- fmt.Printf("Export done in %v\n", time.Since(start))
- return nil
-}
-
-func copyDb(ctx *cli.Context) error {
- // Ensure we have a source chain directory to copy
- if len(ctx.Args()) < 1 {
- utils.Fatalf("Source chaindata directory path argument missing")
- }
- if len(ctx.Args()) < 2 {
- utils.Fatalf("Source ancient chain directory path argument missing")
- }
- // Initialize a new chain for the running node to sync into
- stack := makeFullNode(ctx)
- defer stack.Close()
-
- chain, chainDb := utils.MakeChain(ctx, stack)
- syncMode := *utils.GlobalTextMarshaler(ctx, utils.SyncModeFlag.Name).(*downloader.SyncMode)
-
- var syncBloom *trie.SyncBloom
- if syncMode == downloader.FastSync {
- syncBloom = trie.NewSyncBloom(uint64(ctx.GlobalInt(utils.CacheFlag.Name)/2), chainDb)
- }
- dl := downloader.New(0, chainDb, syncBloom, new(event.TypeMux), chain, nil, nil)
-
- // Create a source peer to satisfy downloader requests from
- db, err := rawdb.NewLevelDBDatabaseWithFreezer(ctx.Args().First(), ctx.GlobalInt(utils.CacheFlag.Name)/2, 256, ctx.Args().Get(1), "")
- if err != nil {
- return err
- }
- hc, err := core.NewHeaderChain(db, chain.Config(), chain.Engine(), func() bool { return false })
- if err != nil {
- return err
- }
- peer := downloader.NewFakePeer("local", db, hc, dl)
- if err = dl.RegisterPeer("local", 63, peer); err != nil {
- return err
- }
- // Synchronise with the simulated peer
- start := time.Now()
-
- currentHeader := hc.CurrentHeader()
- if err = dl.Synchronise("local", currentHeader.Hash(), hc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64()), syncMode); err != nil {
- return err
- }
- for dl.Synchronising() {
- time.Sleep(10 * time.Millisecond)
- }
- fmt.Printf("Database copy done in %v\n", time.Since(start))
-
- // Compact the entire database to remove any sync overhead
- start = time.Now()
- fmt.Println("Compacting entire database...")
- if err = db.Compact(nil, nil); err != nil {
- utils.Fatalf("Compaction failed: %v", err)
- }
- fmt.Printf("Compaction done in %v.\n\n", time.Since(start))
- return nil
-}
-
-func removeDB(ctx *cli.Context) error {
- stack, config := makeConfigNode(ctx)
-
- // Remove the full node state database
- path := stack.ResolvePath("chaindata")
- if common.FileExist(path) {
- confirmAndRemoveDB(path, "full node state database")
- } else {
- log.Info("Full node state database missing", "path", path)
- }
- // Remove the full node ancient database
- path = config.Eth.DatabaseFreezer
- switch {
- case path == "":
- path = filepath.Join(stack.ResolvePath("chaindata"), "ancient")
- case !filepath.IsAbs(path):
- path = config.Node.ResolvePath(path)
- }
- if common.FileExist(path) {
- confirmAndRemoveDB(path, "full node ancient database")
- } else {
- log.Info("Full node ancient database missing", "path", path)
- }
- // Remove the light node database
- path = stack.ResolvePath("lightchaindata")
- if common.FileExist(path) {
- confirmAndRemoveDB(path, "light node database")
- } else {
- log.Info("Light node database missing", "path", path)
- }
- return nil
-}
-
-// confirmAndRemoveDB prompts the user for a last confirmation and removes the
-// folder if accepted.
-func confirmAndRemoveDB(database string, kind string) {
- confirm, err := console.Stdin.PromptConfirm(fmt.Sprintf("Remove %s (%s)?", kind, database))
- switch {
- case err != nil:
- utils.Fatalf("%v", err)
- case !confirm:
- log.Info("Database deletion skipped", "path", database)
- default:
- start := time.Now()
- filepath.Walk(database, func(path string, info os.FileInfo, err error) error {
- // If we're at the top level folder, recurse into
- if path == database {
- return nil
- }
- // Delete all the files, but not subfolders
- if !info.IsDir() {
- os.Remove(path)
- return nil
- }
- return filepath.SkipDir
- })
- log.Info("Database successfully deleted", "path", database, "elapsed", common.PrettyDuration(time.Since(start)))
- }
-}
-
-func dump(ctx *cli.Context) error {
- stack := makeFullNode(ctx)
- defer stack.Close()
-
- chain, chainDb := utils.MakeChain(ctx, stack)
- defer chainDb.Close()
- for _, arg := range ctx.Args() {
- var block *types.Block
- if hashish(arg) {
- block = chain.GetBlockByHash(common.HexToHash(arg))
- } else {
- num, _ := strconv.Atoi(arg)
- block = chain.GetBlockByNumber(uint64(num))
- }
- if block == nil {
- fmt.Println("{}")
- utils.Fatalf("block not found")
- } else {
- state, err := state.New(block.Root(), state.NewDatabase(chainDb))
- if err != nil {
- utils.Fatalf("could not create new state: %v", err)
- }
- excludeCode := ctx.Bool(utils.ExcludeCodeFlag.Name)
- excludeStorage := ctx.Bool(utils.ExcludeStorageFlag.Name)
- includeMissing := ctx.Bool(utils.IncludeIncompletesFlag.Name)
- if ctx.Bool(utils.IterativeOutputFlag.Name) {
- state.IterativeDump(excludeCode, excludeStorage, !includeMissing, json.NewEncoder(os.Stdout))
- } else {
- if includeMissing {
- fmt.Printf("If you want to include accounts with missing preimages, you need iterative output, since" +
- " otherwise the accounts will overwrite each other in the resulting mapping.")
- }
- fmt.Printf("%v %s\n", includeMissing, state.Dump(excludeCode, excludeStorage, false))
- }
- }
- }
- return nil
-}
-
-func inspect(ctx *cli.Context) error {
- node, _ := makeConfigNode(ctx)
- defer node.Close()
-
- _, chainDb := utils.MakeChain(ctx, node)
- defer chainDb.Close()
-
- return rawdb.InspectDatabase(chainDb)
-}
-
-// hashish returns true for strings that look like hashes.
-func hashish(x string) bool {
- _, err := strconv.Atoi(x)
- return err != nil
-}
diff --git a/cmd/geth/config.go b/cmd/geth/config.go
deleted file mode 100644
index b3dc833..0000000
--- a/cmd/geth/config.go
+++ /dev/null
@@ -1,211 +0,0 @@
-// Copyright 2017 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-package geth
-
-import (
- "bufio"
- "errors"
- "fmt"
- "os"
- "reflect"
- "unicode"
-
- cli "gopkg.in/urfave/cli.v1"
-
- "github.com/ava-labs/coreth/cmd/utils"
- "github.com/ava-labs/go-ethereum/dashboard"
- "github.com/ava-labs/coreth/eth"
- "github.com/ava-labs/coreth/node"
- "github.com/ava-labs/go-ethereum/params"
- whisper "github.com/ava-labs/go-ethereum/whisper/whisperv6"
- "github.com/naoina/toml"
-)
-
-var (
- dumpConfigCommand = cli.Command{
- Action: utils.MigrateFlags(dumpConfig),
- Name: "dumpconfig",
- Usage: "Show configuration values",
- ArgsUsage: "",
- Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...),
- Category: "MISCELLANEOUS COMMANDS",
- Description: `The dumpconfig command shows configuration values.`,
- }
-
- configFileFlag = cli.StringFlag{
- Name: "config",
- Usage: "TOML configuration file",
- }
-)
-
-// These settings ensure that TOML keys use the same names as Go struct fields.
-var tomlSettings = toml.Config{
- NormFieldName: func(rt reflect.Type, key string) string {
- return key
- },
- FieldToKey: func(rt reflect.Type, field string) string {
- return field
- },
- MissingField: func(rt reflect.Type, field string) error {
- link := ""
- if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
- link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
- }
- return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
- },
-}
-
-type ethstatsConfig struct {
- URL string `toml:",omitempty"`
-}
-
-type gethConfig struct {
- Eth eth.Config
- Shh whisper.Config
- Node node.Config
- Ethstats ethstatsConfig
- Dashboard dashboard.Config
-}
-
-func loadConfig(file string, cfg *gethConfig) error {
- f, err := os.Open(file)
- if err != nil {
- return err
- }
- defer f.Close()
-
- err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
- // Add file name to errors that have a line number.
- if _, ok := err.(*toml.LineError); ok {
- err = errors.New(file + ", " + err.Error())
- }
- return err
-}
-
-func defaultNodeConfig() node.Config {
- cfg := node.DefaultConfig
- cfg.Name = clientIdentifier
- cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
- cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
- cfg.WSModules = append(cfg.WSModules, "eth", "shh")
- cfg.IPCPath = "geth.ipc"
- return cfg
-}
-
-func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
- // Load defaults.
- cfg := gethConfig{
- Eth: eth.MyDefaultConfig(),
- Shh: whisper.DefaultConfig,
- Node: defaultNodeConfig(),
- Dashboard: dashboard.DefaultConfig,
- }
-
- // Load config file.
- if file := ctx.GlobalString(configFileFlag.Name); file != "" {
- if err := loadConfig(file, &cfg); err != nil {
- utils.Fatalf("%v", err)
- }
- }
-
- // Apply flags.
- utils.SetNodeConfig(ctx, &cfg.Node)
- stack, err := node.New(&cfg.Node)
- if err != nil {
- utils.Fatalf("Failed to create the protocol stack: %v", err)
- }
- utils.SetEthConfig(ctx, stack, &cfg.Eth)
- if ctx.GlobalIsSet(utils.EthStatsURLFlag.Name) {
- cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
- }
- utils.SetShhConfig(ctx, stack, &cfg.Shh)
- utils.SetDashboardConfig(ctx, &cfg.Dashboard)
-
- return stack, cfg
-}
-
-// enableWhisper returns true in case one of the whisper flags is set.
-func enableWhisper(ctx *cli.Context) bool {
- for _, flag := range whisperFlags {
- if ctx.GlobalIsSet(flag.GetName()) {
- return true
- }
- }
- return false
-}
-
-func makeFullNode(ctx *cli.Context) *node.Node {
- stack, cfg := makeConfigNode(ctx)
- utils.RegisterEthService(stack, &cfg.Eth)
-
- if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
- utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
- }
- // Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
- shhEnabled := enableWhisper(ctx)
- shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DeveloperFlag.Name)
- if shhEnabled || shhAutoEnabled {
- if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
- cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
- }
- if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
- cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
- }
- if ctx.GlobalIsSet(utils.WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
- cfg.Shh.RestrictConnectionBetweenLightClients = true
- }
- utils.RegisterShhService(stack, &cfg.Shh)
- }
- // Configure GraphQL if requested
- if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
- utils.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts)
- }
- // Add the Ethereum Stats daemon if requested.
- if cfg.Ethstats.URL != "" {
- utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
- }
- return stack
-}
-
-// dumpConfig is the dumpconfig command.
-func dumpConfig(ctx *cli.Context) error {
- _, cfg := makeConfigNode(ctx)
- comment := ""
-
- if cfg.Eth.Genesis != nil {
- cfg.Eth.Genesis = nil
- comment += "# Note: this config doesn't contain the genesis block.\n\n"
- }
-
- out, err := tomlSettings.Marshal(&cfg)
- if err != nil {
- return err
- }
-
- dump := os.Stdout
- if ctx.NArg() > 0 {
- dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
- if err != nil {
- return err
- }
- defer dump.Close()
- }
- dump.WriteString(comment)
- dump.Write(out)
-
- return nil
-}
diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go
deleted file mode 100644
index 57ea2c9..0000000
--- a/cmd/geth/consolecmd.go
+++ /dev/null
@@ -1,220 +0,0 @@
-// Copyright 2016 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-package geth
-
-import (
- "fmt"
- "os"
- "os/signal"
- "path/filepath"
- "strings"
- "syscall"
-
- "github.com/ava-labs/coreth/cmd/utils"
- "github.com/ava-labs/go-ethereum/console"
- "github.com/ava-labs/coreth/node"
- "github.com/ava-labs/go-ethereum/rpc"
- "gopkg.in/urfave/cli.v1"
-)
-
-var (
- consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
-
- consoleCommand = cli.Command{
- Action: utils.MigrateFlags(localConsole),
- Name: "console",
- Usage: "Start an interactive JavaScript environment",
- Flags: append(append(append(nodeFlags, rpcFlags...), consoleFlags...), whisperFlags...),
- Category: "CONSOLE COMMANDS",
- Description: `
-The Geth console is an interactive shell for the JavaScript runtime environment
-which exposes a node admin interface as well as the Ðapp JavaScript API.
-See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console.`,
- }
-
- attachCommand = cli.Command{
- Action: utils.MigrateFlags(remoteConsole),
- Name: "attach",
- Usage: "Start an interactive JavaScript environment (connect to node)",
- ArgsUsage: "[endpoint]",
- Flags: append(consoleFlags, utils.DataDirFlag),
- Category: "CONSOLE COMMANDS",
- Description: `
-The Geth console is an interactive shell for the JavaScript runtime environment
-which exposes a node admin interface as well as the Ðapp JavaScript API.
-See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console.
-This command allows to open a console on a running geth node.`,
- }
-
- javascriptCommand = cli.Command{
- Action: utils.MigrateFlags(ephemeralConsole),
- Name: "js",
- Usage: "Execute the specified JavaScript files",
- ArgsUsage: "<jsfile> [jsfile...]",
- Flags: append(nodeFlags, consoleFlags...),
- Category: "CONSOLE COMMANDS",
- Description: `
-The JavaScript VM exposes a node admin interface as well as the Ðapp
-JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console`,
- }
-)
-
-// localConsole starts a new geth node, attaching a JavaScript console to it at the
-// same time.
-func localConsole(ctx *cli.Context) error {
- // Create and start the node based on the CLI flags
- node := makeFullNode(ctx)
- startNode(ctx, node)
- defer node.Close()
-
- // Attach to the newly started node and start the JavaScript console
- client, err := node.Attach()
- if err != nil {
- utils.Fatalf("Failed to attach to the inproc geth: %v", err)
- }
- config := console.Config{
- DataDir: utils.MakeDataDir(ctx),
- DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
- Client: client,
- Preload: utils.MakeConsolePreloads(ctx),
- }
-
- console, err := console.New(config)
- if err != nil {
- utils.Fatalf("Failed to start the JavaScript console: %v", err)
- }
- defer console.Stop(false)
-
- // If only a short execution was requested, evaluate and return
- if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
- console.Evaluate(script)
- return nil
- }
- // Otherwise print the welcome screen and enter interactive mode
- console.Welcome()
- console.Interactive()
-
- return nil
-}
-
-// remoteConsole will connect to a remote geth instance, attaching a JavaScript
-// console to it.
-func remoteConsole(ctx *cli.Context) error {
- // Attach to a remotely running geth instance and start the JavaScript console
- endpoint := ctx.Args().First()
- if endpoint == "" {
- path := node.DefaultDataDir()
- if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
- path = ctx.GlobalString(utils.DataDirFlag.Name)
- }
- if path != "" {
- if ctx.GlobalBool(utils.TestnetFlag.Name) {
- path = filepath.Join(path, "testnet")
- } else if ctx.GlobalBool(utils.RinkebyFlag.Name) {
- path = filepath.Join(path, "rinkeby")
- }
- }
- endpoint = fmt.Sprintf("%s/geth.ipc", path)
- }
- client, err := dialRPC(endpoint)
- if err != nil {
- utils.Fatalf("Unable to attach to remote geth: %v", err)
- }
- config := console.Config{
- DataDir: utils.MakeDataDir(ctx),
- DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
- Client: client,
- Preload: utils.MakeConsolePreloads(ctx),
- }
-
- console, err := console.New(config)
- if err != nil {
- utils.Fatalf("Failed to start the JavaScript console: %v", err)
- }
- defer console.Stop(false)
-
- if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
- console.Evaluate(script)
- return nil
- }
-
- // Otherwise print the welcome screen and enter interactive mode
- console.Welcome()
- console.Interactive()
-
- return nil
-}
-
-// dialRPC returns a RPC client which connects to the given endpoint.
-// The check for empty endpoint implements the defaulting logic
-// for "geth attach" and "geth monitor" with no argument.
-func dialRPC(endpoint string) (*rpc.Client, error) {
- if endpoint == "" {
- endpoint = node.DefaultIPCEndpoint(clientIdentifier)
- } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
- // Backwards compatibility with geth < 1.5 which required
- // these prefixes.
- endpoint = endpoint[4:]
- }
- return rpc.Dial(endpoint)
-}
-
-// ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
-// console to it, executes each of the files specified as arguments and tears
-// everything down.
-func ephemeralConsole(ctx *cli.Context) error {
- // Create and start the node based on the CLI flags
- node := makeFullNode(ctx)
- startNode(ctx, node)
- defer node.Close()
-
- // Attach to the newly started node and start the JavaScript console
- client, err := node.Attach()
- if err != nil {
- utils.Fatalf("Failed to attach to the inproc geth: %v", err)
- }
- config := console.Config{
- DataDir: utils.MakeDataDir(ctx),
- DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
- Client: client,
- Preload: utils.MakeConsolePreloads(ctx),
- }
-
- console, err := console.New(config)
- if err != nil {
- utils.Fatalf("Failed to start the JavaScript console: %v", err)
- }
- defer console.Stop(false)
-
- // Evaluate each of the specified JavaScript files
- for _, file := range ctx.Args() {
- if err = console.Execute(file); err != nil {
- utils.Fatalf("Failed to execute %s: %v", file, err)
- }
- }
- // Wait for pending callbacks, but stop for Ctrl-C.
- abort := make(chan os.Signal, 1)
- signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM)
-
- go func() {
- <-abort
- os.Exit(0)
- }()
- console.Stop(true)
-
- return nil
-}
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
deleted file mode 100644
index 82ad47e..0000000
--- a/cmd/geth/main.go
+++ /dev/null
@@ -1,403 +0,0 @@
-// Copyright 2014 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-// geth is the official command-line client for Ethereum.
-package geth
-
-import (
- "fmt"
- "math"
- "os"
- "runtime"
- godebug "runtime/debug"
- "sort"
- "strconv"
- "time"
-
- "github.com/elastic/gosigar"
- "github.com/ava-labs/coreth/cmd/utils"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/console"
- "github.com/ava-labs/coreth/eth"
- "github.com/ava-labs/go-ethereum/eth/downloader"
- "github.com/ava-labs/go-ethereum/ethclient"
- "github.com/ava-labs/coreth/internal/debug"
- "github.com/ava-labs/go-ethereum/les"
- "github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/metrics"
- "github.com/ava-labs/coreth/node"
- cli "gopkg.in/urfave/cli.v1"
-)
-
-const (
- clientIdentifier = "geth" // Client identifier to advertise over the network
-)
-
-var (
- // Git SHA1 commit hash of the release (set via linker flags)
- gitCommit = ""
- gitDate = ""
- // The App that holds all commands and flags.
- App = utils.NewApp(gitCommit, gitDate, "the go-ethereum command line interface")
- // flags that configure the node
- nodeFlags = []cli.Flag{
- utils.IdentityFlag,
- utils.PasswordFileFlag,
- utils.BootnodesFlag,
- utils.BootnodesV4Flag,
- utils.BootnodesV5Flag,
- utils.DataDirFlag,
- utils.AncientFlag,
- utils.KeyStoreDirFlag,
- utils.ExternalSignerFlag,
- utils.NoUSBFlag,
- utils.SmartCardDaemonPathFlag,
- utils.DashboardEnabledFlag,
- utils.DashboardAddrFlag,
- utils.DashboardPortFlag,
- utils.DashboardRefreshFlag,
- utils.EthashCacheDirFlag,
- utils.EthashCachesInMemoryFlag,
- utils.EthashCachesOnDiskFlag,
- utils.EthashDatasetDirFlag,
- utils.EthashDatasetsInMemoryFlag,
- utils.EthashDatasetsOnDiskFlag,
- utils.TxPoolLocalsFlag,
- utils.TxPoolNoLocalsFlag,
- utils.TxPoolJournalFlag,
- utils.TxPoolRejournalFlag,
- utils.TxPoolPriceLimitFlag,
- utils.TxPoolPriceBumpFlag,
- utils.TxPoolAccountSlotsFlag,
- utils.TxPoolGlobalSlotsFlag,
- utils.TxPoolAccountQueueFlag,
- utils.TxPoolGlobalQueueFlag,
- utils.TxPoolLifetimeFlag,
- utils.SyncModeFlag,
- utils.ExitWhenSyncedFlag,
- utils.GCModeFlag,
- utils.LightServeFlag,
- utils.LightLegacyServFlag,
- utils.LightIngressFlag,
- utils.LightEgressFlag,
- utils.LightMaxPeersFlag,
- utils.LightLegacyPeersFlag,
- utils.LightKDFFlag,
- utils.UltraLightServersFlag,
- utils.UltraLightFractionFlag,
- utils.UltraLightOnlyAnnounceFlag,
- utils.WhitelistFlag,
- utils.CacheFlag,
- utils.CacheDatabaseFlag,
- utils.CacheTrieFlag,
- utils.CacheGCFlag,
- utils.CacheNoPrefetchFlag,
- utils.ListenPortFlag,
- utils.MaxPeersFlag,
- utils.MaxPendingPeersFlag,
- utils.MiningEnabledFlag,
- utils.MinerThreadsFlag,
- utils.MinerLegacyThreadsFlag,
- utils.MinerNotifyFlag,
- utils.MinerGasTargetFlag,
- utils.MinerLegacyGasTargetFlag,
- utils.MinerGasLimitFlag,
- utils.MinerGasPriceFlag,
- utils.MinerLegacyGasPriceFlag,
- utils.MinerEtherbaseFlag,
- utils.MinerLegacyEtherbaseFlag,
- utils.MinerExtraDataFlag,
- utils.MinerLegacyExtraDataFlag,
- utils.MinerRecommitIntervalFlag,
- utils.MinerNoVerfiyFlag,
- utils.NATFlag,
- utils.NoDiscoverFlag,
- utils.DiscoveryV5Flag,
- utils.NetrestrictFlag,
- utils.NodeKeyFileFlag,
- utils.NodeKeyHexFlag,
- utils.DeveloperFlag,
- utils.DeveloperPeriodFlag,
- utils.TestnetFlag,
- utils.RinkebyFlag,
- utils.GoerliFlag,
- utils.VMEnableDebugFlag,
- utils.NetworkIdFlag,
- utils.EthStatsURLFlag,
- utils.FakePoWFlag,
- utils.NoCompactionFlag,
- utils.GpoBlocksFlag,
- utils.GpoPercentileFlag,
- utils.EWASMInterpreterFlag,
- utils.EVMInterpreterFlag,
- configFileFlag,
- }
-
- rpcFlags = []cli.Flag{
- utils.RPCEnabledFlag,
- utils.RPCListenAddrFlag,
- utils.RPCPortFlag,
- utils.RPCCORSDomainFlag,
- utils.RPCVirtualHostsFlag,
- utils.GraphQLEnabledFlag,
- utils.GraphQLListenAddrFlag,
- utils.GraphQLPortFlag,
- utils.GraphQLCORSDomainFlag,
- utils.GraphQLVirtualHostsFlag,
- utils.RPCApiFlag,
- utils.WSEnabledFlag,
- utils.WSListenAddrFlag,
- utils.WSPortFlag,
- utils.WSApiFlag,
- utils.WSAllowedOriginsFlag,
- utils.IPCDisabledFlag,
- utils.IPCPathFlag,
- utils.InsecureUnlockAllowedFlag,
- utils.RPCGlobalGasCap,
- }
-
- whisperFlags = []cli.Flag{
- utils.WhisperEnabledFlag,
- utils.WhisperMaxMessageSizeFlag,
- utils.WhisperMinPOWFlag,
- utils.WhisperRestrictConnectionBetweenLightClientsFlag,
- }
-
- metricsFlags = []cli.Flag{
- utils.MetricsEnabledFlag,
- utils.MetricsEnabledExpensiveFlag,
- utils.MetricsEnableInfluxDBFlag,
- utils.MetricsInfluxDBEndpointFlag,
- utils.MetricsInfluxDBDatabaseFlag,
- utils.MetricsInfluxDBUsernameFlag,
- utils.MetricsInfluxDBPasswordFlag,
- utils.MetricsInfluxDBTagsFlag,
- }
-)
-
-func init() {
- // Initialize the CLI App and start Geth
- App.Action = geth
- App.HideVersion = true // we have a command to print the version
- App.Copyright = "Copyright 2013-2019 The go-ethereum Authors"
- App.Commands = []cli.Command{
- // See chaincmd.go:
- initCommand,
- importCommand,
- exportCommand,
- importPreimagesCommand,
- exportPreimagesCommand,
- copydbCommand,
- removedbCommand,
- dumpCommand,
- inspectCommand,
- // See accountcmd.go:
- // See consolecmd.go:
- consoleCommand,
- attachCommand,
- javascriptCommand,
- // See misccmd.go:
- makecacheCommand,
- makedagCommand,
- versionCommand,
- licenseCommand,
- // See config.go
- dumpConfigCommand,
- // See retesteth.go
- retestethCommand,
- }
- sort.Sort(cli.CommandsByName(App.Commands))
-
- App.Flags = append(App.Flags, nodeFlags...)
- App.Flags = append(App.Flags, rpcFlags...)
- App.Flags = append(App.Flags, consoleFlags...)
- App.Flags = append(App.Flags, debug.Flags...)
- App.Flags = append(App.Flags, whisperFlags...)
- App.Flags = append(App.Flags, metricsFlags...)
-
- App.Before = func(ctx *cli.Context) error {
- logdir := ""
- if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
- logdir = (&node.Config{DataDir: utils.MakeDataDir(ctx)}).ResolvePath("logs")
- }
- if err := debug.Setup(ctx, logdir); err != nil {
- return err
- }
- // If we're a full node on mainnet without --cache specified, bump default cache allowance
- if ctx.GlobalString(utils.SyncModeFlag.Name) != "light" && !ctx.GlobalIsSet(utils.CacheFlag.Name) && !ctx.GlobalIsSet(utils.NetworkIdFlag.Name) {
- // Make sure we're not on any supported preconfigured testnet either
- if !ctx.GlobalIsSet(utils.TestnetFlag.Name) && !ctx.GlobalIsSet(utils.RinkebyFlag.Name) && !ctx.GlobalIsSet(utils.GoerliFlag.Name) && !ctx.GlobalIsSet(utils.DeveloperFlag.Name) {
- // Nope, we're really on mainnet. Bump that cache up!
- log.Info("Bumping default cache on mainnet", "provided", ctx.GlobalInt(utils.CacheFlag.Name), "updated", 4096)
- ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(4096))
- }
- }
- // If we're running a light client on any network, drop the cache to some meaningfully low amount
- if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" && !ctx.GlobalIsSet(utils.CacheFlag.Name) {
- log.Info("Dropping default light client cache", "provided", ctx.GlobalInt(utils.CacheFlag.Name), "updated", 128)
- ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(128))
- }
- // Cap the cache allowance and tune the garbage collector
- var mem gosigar.Mem
- // Workaround until OpenBSD support lands into gosigar
- // Check https://github.com/elastic/gosigar#supported-platforms
- if runtime.GOOS != "openbsd" {
- if err := mem.Get(); err == nil {
- allowance := int(mem.Total / 1024 / 1024 / 3)
- if cache := ctx.GlobalInt(utils.CacheFlag.Name); cache > allowance {
- log.Warn("Sanitizing cache to Go's GC limits", "provided", cache, "updated", allowance)
- ctx.GlobalSet(utils.CacheFlag.Name, strconv.Itoa(allowance))
- }
- }
- }
- // Ensure Go's GC ignores the database cache for trigger percentage
- cache := ctx.GlobalInt(utils.CacheFlag.Name)
- gogc := math.Max(20, math.Min(100, 100/(float64(cache)/1024)))
-
- log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc))
- godebug.SetGCPercent(int(gogc))
-
- // Start metrics export if enabled
- utils.SetupMetrics(ctx)
-
- // Start system runtime metrics collection
- go metrics.CollectProcessMetrics(3 * time.Second)
-
- return nil
- }
-
- App.After = func(ctx *cli.Context) error {
- debug.Exit()
- console.Stdin.Close() // Resets terminal mode.
- return nil
- }
-}
-
-func main() {
- if err := App.Run(os.Args); err != nil {
- fmt.Fprintln(os.Stderr, err)
- os.Exit(1)
- }
-}
-
-// geth is the main entry point into the system if no special subcommand is ran.
-// It creates a default node based on the command line arguments and runs it in
-// blocking mode, waiting for it to be shut down.
-func geth(ctx *cli.Context) error {
- if args := ctx.Args(); len(args) > 0 {
- return fmt.Errorf("invalid command: %q", args[0])
- }
- node := makeFullNode(ctx)
- defer node.Close()
- startNode(ctx, node)
- node.Wait()
- return nil
-}
-
-// startNode boots up the system node and all registered protocols, after which
-// it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
-// miner.
-func startNode(ctx *cli.Context, stack *node.Node) {
- debug.Memsize.Add("node", stack)
-
- // Start up the node itself
- utils.StartNode(stack)
-
- // Unlock any account specifically requested
-
- // Register wallet event handlers to open and auto-derive wallets
-
- // Create a client to interact with local geth node.
- rpcClient, err := stack.Attach()
- if err != nil {
- utils.Fatalf("Failed to attach to self: %v", err)
- }
- ethClient := ethclient.NewClient(rpcClient)
-
- // Set contract backend for ethereum service if local node
- // is serving LES requests.
- if ctx.GlobalInt(utils.LightLegacyServFlag.Name) > 0 || ctx.GlobalInt(utils.LightServeFlag.Name) > 0 {
- var ethService *eth.Ethereum
- if err := stack.Service(&ethService); err != nil {
- utils.Fatalf("Failed to retrieve ethereum service: %v", err)
- }
- ethService.SetContractBackend(ethClient)
- }
- // Set contract backend for les service if local node is
- // running as a light client.
- if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" {
- var lesService *les.LightEthereum
- if err := stack.Service(&lesService); err != nil {
- utils.Fatalf("Failed to retrieve light ethereum service: %v", err)
- }
- lesService.SetContractBackend(ethClient)
- }
-
- // Spawn a standalone goroutine for status synchronization monitoring,
- // close the node when synchronization is complete if user required.
- if ctx.GlobalBool(utils.ExitWhenSyncedFlag.Name) {
- go func() {
- sub := stack.EventMux().Subscribe(downloader.DoneEvent{})
- defer sub.Unsubscribe()
- for {
- event := <-sub.Chan()
- if event == nil {
- continue
- }
- done, ok := event.Data.(downloader.DoneEvent)
- if !ok {
- continue
- }
- if timestamp := time.Unix(int64(done.Latest.Time), 0); time.Since(timestamp) < 10*time.Minute {
- log.Info("Synchronisation completed", "latestnum", done.Latest.Number, "latesthash", done.Latest.Hash(),
- "age", common.PrettyAge(timestamp))
- stack.Stop()
- }
- }
- }()
- }
-
- // Start auxiliary services if enabled
- if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
- // Mining only makes sense if a full Ethereum node is running
- if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" {
- utils.Fatalf("Light clients do not support mining")
- }
- var ethereum *eth.Ethereum
- if err := stack.Service(&ethereum); err != nil {
- utils.Fatalf("Ethereum service not running: %v", err)
- }
- etherBase := &common.Address {
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- }
- ethereum.SetEtherbase(*etherBase)
- // Set the gas price to the limits from the CLI and start mining
- gasprice := utils.GlobalBig(ctx, utils.MinerLegacyGasPriceFlag.Name)
- if ctx.IsSet(utils.MinerGasPriceFlag.Name) {
- gasprice = utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
- }
- ethereum.TxPool().SetGasPrice(gasprice)
-
- threads := ctx.GlobalInt(utils.MinerLegacyThreadsFlag.Name)
- if ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) {
- threads = ctx.GlobalInt(utils.MinerThreadsFlag.Name)
- }
- if err := ethereum.StartMining(threads); err != nil {
- utils.Fatalf("Failed to start mining: %v", err)
- }
- }
-}
diff --git a/cmd/geth/misccmd.go b/cmd/geth/misccmd.go
deleted file mode 100644
index a49f2ba..0000000
--- a/cmd/geth/misccmd.go
+++ /dev/null
@@ -1,142 +0,0 @@
-// Copyright 2016 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-package geth
-
-import (
- "fmt"
- "os"
- "runtime"
- "strconv"
- "strings"
-
- "github.com/ava-labs/coreth/cmd/utils"
- "github.com/ava-labs/go-ethereum/consensus/ethash"
- "github.com/ava-labs/go-ethereum/eth"
- "github.com/ava-labs/go-ethereum/params"
- "gopkg.in/urfave/cli.v1"
-)
-
-var (
- makecacheCommand = cli.Command{
- Action: utils.MigrateFlags(makecache),
- Name: "makecache",
- Usage: "Generate ethash verification cache (for testing)",
- ArgsUsage: "<blockNum> <outputDir>",
- Category: "MISCELLANEOUS COMMANDS",
- Description: `
-The makecache command generates an ethash cache in <outputDir>.
-
-This command exists to support the system testing project.
-Regular users do not need to execute it.
-`,
- }
- makedagCommand = cli.Command{
- Action: utils.MigrateFlags(makedag),
- Name: "makedag",
- Usage: "Generate ethash mining DAG (for testing)",
- ArgsUsage: "<blockNum> <outputDir>",
- Category: "MISCELLANEOUS COMMANDS",
- Description: `
-The makedag command generates an ethash DAG in <outputDir>.
-
-This command exists to support the system testing project.
-Regular users do not need to execute it.
-`,
- }
- versionCommand = cli.Command{
- Action: utils.MigrateFlags(version),
- Name: "version",
- Usage: "Print version numbers",
- ArgsUsage: " ",
- Category: "MISCELLANEOUS COMMANDS",
- Description: `
-The output of this command is supposed to be machine-readable.
-`,
- }
- licenseCommand = cli.Command{
- Action: utils.MigrateFlags(license),
- Name: "license",
- Usage: "Display license information",
- ArgsUsage: " ",
- Category: "MISCELLANEOUS COMMANDS",
- }
-)
-
-// makecache generates an ethash verification cache into the provided folder.
-func makecache(ctx *cli.Context) error {
- args := ctx.Args()
- if len(args) != 2 {
- utils.Fatalf(`Usage: geth makecache <block number> <outputdir>`)
- }
- block, err := strconv.ParseUint(args[0], 0, 64)
- if err != nil {
- utils.Fatalf("Invalid block number: %v", err)
- }
- ethash.MakeCache(block, args[1])
-
- return nil
-}
-
-// makedag generates an ethash mining DAG into the provided folder.
-func makedag(ctx *cli.Context) error {
- args := ctx.Args()
- if len(args) != 2 {
- utils.Fatalf(`Usage: geth makedag <block number> <outputdir>`)
- }
- block, err := strconv.ParseUint(args[0], 0, 64)
- if err != nil {
- utils.Fatalf("Invalid block number: %v", err)
- }
- ethash.MakeDataset(block, args[1])
-
- return nil
-}
-
-func version(ctx *cli.Context) error {
- fmt.Println(strings.Title(clientIdentifier))
- fmt.Println("Version:", params.VersionWithMeta)
- if gitCommit != "" {
- fmt.Println("Git Commit:", gitCommit)
- }
- if gitDate != "" {
- fmt.Println("Git Commit Date:", gitDate)
- }
- fmt.Println("Architecture:", runtime.GOARCH)
- fmt.Println("Protocol Versions:", eth.ProtocolVersions)
- fmt.Println("Network Id:", eth.DefaultConfig.NetworkId)
- fmt.Println("Go Version:", runtime.Version())
- fmt.Println("Operating System:", runtime.GOOS)
- fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
- fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
- return nil
-}
-
-func license(_ *cli.Context) error {
- fmt.Println(`Geth is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-Geth is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with geth. If not, see <http://www.gnu.org/licenses/>.`)
- return nil
-}
diff --git a/cmd/geth/retesteth.go b/cmd/geth/retesteth.go
deleted file mode 100644
index a5bebb0..0000000
--- a/cmd/geth/retesteth.go
+++ /dev/null
@@ -1,891 +0,0 @@
-// Copyright 2019 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-package geth
-
-import (
- "bytes"
- "context"
- "fmt"
- "math/big"
- "os"
- "os/signal"
- "strings"
- "time"
-
- "github.com/ava-labs/coreth/cmd/utils"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/common/math"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/consensus/ethash"
- "github.com/ava-labs/go-ethereum/consensus/misc"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
- "github.com/ava-labs/go-ethereum/crypto"
- "github.com/ava-labs/go-ethereum/ethdb"
- "github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/node"
- "github.com/ava-labs/go-ethereum/params"
- "github.com/ava-labs/go-ethereum/rlp"
- "github.com/ava-labs/go-ethereum/rpc"
- "github.com/ava-labs/go-ethereum/trie"
-
- cli "gopkg.in/urfave/cli.v1"
-)
-
-var (
- rpcPortFlag = cli.IntFlag{
- Name: "rpcport",
- Usage: "HTTP-RPC server listening port",
- Value: node.DefaultHTTPPort,
- }
- retestethCommand = cli.Command{
- Action: utils.MigrateFlags(retesteth),
- Name: "retesteth",
- Usage: "Launches geth in retesteth mode",
- ArgsUsage: "",
- Flags: []cli.Flag{rpcPortFlag},
- Category: "MISCELLANEOUS COMMANDS",
- Description: `Launches geth in retesteth mode (no database, no network, only retesteth RPC interface)`,
- }
-)
-
-type RetestethTestAPI interface {
- SetChainParams(ctx context.Context, chainParams ChainParams) (bool, error)
- MineBlocks(ctx context.Context, number uint64) (bool, error)
- ModifyTimestamp(ctx context.Context, interval uint64) (bool, error)
- ImportRawBlock(ctx context.Context, rawBlock hexutil.Bytes) (common.Hash, error)
- RewindToBlock(ctx context.Context, number uint64) (bool, error)
- GetLogHash(ctx context.Context, txHash common.Hash) (common.Hash, error)
-}
-
-type RetestethEthAPI interface {
- SendRawTransaction(ctx context.Context, rawTx hexutil.Bytes) (common.Hash, error)
- BlockNumber(ctx context.Context) (uint64, error)
- GetBlockByNumber(ctx context.Context, blockNr math.HexOrDecimal64, fullTx bool) (map[string]interface{}, error)
- GetBalance(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (*math.HexOrDecimal256, error)
- GetCode(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (hexutil.Bytes, error)
- GetTransactionCount(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (uint64, error)
-}
-
-type RetestethDebugAPI interface {
- AccountRange(ctx context.Context,
- blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
- addressHash *math.HexOrDecimal256, maxResults uint64,
- ) (AccountRangeResult, error)
- StorageRangeAt(ctx context.Context,
- blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
- address common.Address,
- begin *math.HexOrDecimal256, maxResults uint64,
- ) (StorageRangeResult, error)
-}
-
-type RetestWeb3API interface {
- ClientVersion(ctx context.Context) (string, error)
-}
-
-type RetestethAPI struct {
- ethDb ethdb.Database
- db state.Database
- chainConfig *params.ChainConfig
- author common.Address
- extraData []byte
- genesisHash common.Hash
- engine *NoRewardEngine
- blockchain *core.BlockChain
- blockNumber uint64
- txMap map[common.Address]map[uint64]*types.Transaction // Sender -> Nonce -> Transaction
- txSenders map[common.Address]struct{} // Set of transaction senders
- blockInterval uint64
-}
-
-type ChainParams struct {
- SealEngine string `json:"sealEngine"`
- Params CParamsParams `json:"params"`
- Genesis CParamsGenesis `json:"genesis"`
- Accounts map[common.Address]CParamsAccount `json:"accounts"`
-}
-
-type CParamsParams struct {
- AccountStartNonce math.HexOrDecimal64 `json:"accountStartNonce"`
- HomesteadForkBlock *math.HexOrDecimal64 `json:"homesteadForkBlock"`
- EIP150ForkBlock *math.HexOrDecimal64 `json:"EIP150ForkBlock"`
- EIP158ForkBlock *math.HexOrDecimal64 `json:"EIP158ForkBlock"`
- DaoHardforkBlock *math.HexOrDecimal64 `json:"daoHardforkBlock"`
- ByzantiumForkBlock *math.HexOrDecimal64 `json:"byzantiumForkBlock"`
- ConstantinopleForkBlock *math.HexOrDecimal64 `json:"constantinopleForkBlock"`
- ConstantinopleFixForkBlock *math.HexOrDecimal64 `json:"constantinopleFixForkBlock"`
- ChainID *math.HexOrDecimal256 `json:"chainID"`
- MaximumExtraDataSize math.HexOrDecimal64 `json:"maximumExtraDataSize"`
- TieBreakingGas bool `json:"tieBreakingGas"`
- MinGasLimit math.HexOrDecimal64 `json:"minGasLimit"`
- MaxGasLimit math.HexOrDecimal64 `json:"maxGasLimit"`
- GasLimitBoundDivisor math.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
- MinimumDifficulty math.HexOrDecimal256 `json:"minimumDifficulty"`
- DifficultyBoundDivisor math.HexOrDecimal256 `json:"difficultyBoundDivisor"`
- DurationLimit math.HexOrDecimal256 `json:"durationLimit"`
- BlockReward math.HexOrDecimal256 `json:"blockReward"`
- NetworkID math.HexOrDecimal256 `json:"networkID"`
-}
-
-type CParamsGenesis struct {
- Nonce math.HexOrDecimal64 `json:"nonce"`
- Difficulty *math.HexOrDecimal256 `json:"difficulty"`
- MixHash *math.HexOrDecimal256 `json:"mixHash"`
- Author common.Address `json:"author"`
- Timestamp math.HexOrDecimal64 `json:"timestamp"`
- ParentHash common.Hash `json:"parentHash"`
- ExtraData hexutil.Bytes `json:"extraData"`
- GasLimit math.HexOrDecimal64 `json:"gasLimit"`
-}
-
-type CParamsAccount struct {
- Balance *math.HexOrDecimal256 `json:"balance"`
- Precompiled *CPAccountPrecompiled `json:"precompiled"`
- Code hexutil.Bytes `json:"code"`
- Storage map[string]string `json:"storage"`
- Nonce *math.HexOrDecimal64 `json:"nonce"`
-}
-
-type CPAccountPrecompiled struct {
- Name string `json:"name"`
- StartingBlock math.HexOrDecimal64 `json:"startingBlock"`
- Linear *CPAPrecompiledLinear `json:"linear"`
-}
-
-type CPAPrecompiledLinear struct {
- Base uint64 `json:"base"`
- Word uint64 `json:"word"`
-}
-
-type AccountRangeResult struct {
- AddressMap map[common.Hash]common.Address `json:"addressMap"`
- NextKey common.Hash `json:"nextKey"`
-}
-
-type StorageRangeResult struct {
- Complete bool `json:"complete"`
- Storage map[common.Hash]SRItem `json:"storage"`
-}
-
-type SRItem struct {
- Key string `json:"key"`
- Value string `json:"value"`
-}
-
-type NoRewardEngine struct {
- inner consensus.Engine
- rewardsOn bool
-}
-
-func (e *NoRewardEngine) Author(header *types.Header) (common.Address, error) {
- return e.inner.Author(header)
-}
-
-func (e *NoRewardEngine) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
- return e.inner.VerifyHeader(chain, header, seal)
-}
-
-func (e *NoRewardEngine) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
- return e.inner.VerifyHeaders(chain, headers, seals)
-}
-
-func (e *NoRewardEngine) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
- return e.inner.VerifyUncles(chain, block)
-}
-
-func (e *NoRewardEngine) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
- return e.inner.VerifySeal(chain, header)
-}
-
-func (e *NoRewardEngine) Prepare(chain consensus.ChainReader, header *types.Header) error {
- return e.inner.Prepare(chain, header)
-}
-
-func (e *NoRewardEngine) accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
- // Simply touch miner and uncle coinbase accounts
- reward := big.NewInt(0)
- for _, uncle := range uncles {
- state.AddBalance(uncle.Coinbase, reward)
- }
- state.AddBalance(header.Coinbase, reward)
-}
-
-func (e *NoRewardEngine) Finalize(chain consensus.ChainReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
- uncles []*types.Header) {
- if e.rewardsOn {
- e.inner.Finalize(chain, header, statedb, txs, uncles)
- } else {
- e.accumulateRewards(chain.Config(), statedb, header, uncles)
- header.Root = statedb.IntermediateRoot(chain.Config().IsEIP158(header.Number))
- }
-}
-
-func (e *NoRewardEngine) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
- uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
- if e.rewardsOn {
- return e.inner.FinalizeAndAssemble(chain, header, statedb, txs, uncles, receipts)
- } else {
- e.accumulateRewards(chain.Config(), statedb, header, uncles)
- header.Root = statedb.IntermediateRoot(chain.Config().IsEIP158(header.Number))
-
- // Header seems complete, assemble into a block and return
- return types.NewBlock(header, txs, uncles, receipts), nil
- }
-}
-
-func (e *NoRewardEngine) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
- return e.inner.Seal(chain, block, results, stop)
-}
-
-func (e *NoRewardEngine) SealHash(header *types.Header) common.Hash {
- return e.inner.SealHash(header)
-}
-
-func (e *NoRewardEngine) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
- return e.inner.CalcDifficulty(chain, time, parent)
-}
-
-func (e *NoRewardEngine) APIs(chain consensus.ChainReader) []rpc.API {
- return e.inner.APIs(chain)
-}
-
-func (e *NoRewardEngine) Close() error {
- return e.inner.Close()
-}
-
-func (api *RetestethAPI) SetChainParams(ctx context.Context, chainParams ChainParams) (bool, error) {
- // Clean up
- if api.blockchain != nil {
- api.blockchain.Stop()
- }
- if api.engine != nil {
- api.engine.Close()
- }
- if api.ethDb != nil {
- api.ethDb.Close()
- }
- ethDb := rawdb.NewMemoryDatabase()
- accounts := make(core.GenesisAlloc)
- for address, account := range chainParams.Accounts {
- balance := big.NewInt(0)
- if account.Balance != nil {
- balance.Set((*big.Int)(account.Balance))
- }
- var nonce uint64
- if account.Nonce != nil {
- nonce = uint64(*account.Nonce)
- }
- if account.Precompiled == nil || account.Balance != nil {
- storage := make(map[common.Hash]common.Hash)
- for k, v := range account.Storage {
- storage[common.HexToHash(k)] = common.HexToHash(v)
- }
- accounts[address] = core.GenesisAccount{
- Balance: balance,
- Code: account.Code,
- Nonce: nonce,
- Storage: storage,
- }
- }
- }
- chainId := big.NewInt(1)
- if chainParams.Params.ChainID != nil {
- chainId.Set((*big.Int)(chainParams.Params.ChainID))
- }
- var (
- homesteadBlock *big.Int
- daoForkBlock *big.Int
- eip150Block *big.Int
- eip155Block *big.Int
- eip158Block *big.Int
- byzantiumBlock *big.Int
- constantinopleBlock *big.Int
- petersburgBlock *big.Int
- )
- if chainParams.Params.HomesteadForkBlock != nil {
- homesteadBlock = big.NewInt(int64(*chainParams.Params.HomesteadForkBlock))
- }
- if chainParams.Params.DaoHardforkBlock != nil {
- daoForkBlock = big.NewInt(int64(*chainParams.Params.DaoHardforkBlock))
- }
- if chainParams.Params.EIP150ForkBlock != nil {
- eip150Block = big.NewInt(int64(*chainParams.Params.EIP150ForkBlock))
- }
- if chainParams.Params.EIP158ForkBlock != nil {
- eip158Block = big.NewInt(int64(*chainParams.Params.EIP158ForkBlock))
- eip155Block = eip158Block
- }
- if chainParams.Params.ByzantiumForkBlock != nil {
- byzantiumBlock = big.NewInt(int64(*chainParams.Params.ByzantiumForkBlock))
- }
- if chainParams.Params.ConstantinopleForkBlock != nil {
- constantinopleBlock = big.NewInt(int64(*chainParams.Params.ConstantinopleForkBlock))
- }
- if chainParams.Params.ConstantinopleFixForkBlock != nil {
- petersburgBlock = big.NewInt(int64(*chainParams.Params.ConstantinopleFixForkBlock))
- }
- if constantinopleBlock != nil && petersburgBlock == nil {
- petersburgBlock = big.NewInt(100000000000)
- }
- genesis := &core.Genesis{
- Config: &params.ChainConfig{
- ChainID: chainId,
- HomesteadBlock: homesteadBlock,
- DAOForkBlock: daoForkBlock,
- DAOForkSupport: false,
- EIP150Block: eip150Block,
- EIP155Block: eip155Block,
- EIP158Block: eip158Block,
- ByzantiumBlock: byzantiumBlock,
- ConstantinopleBlock: constantinopleBlock,
- PetersburgBlock: petersburgBlock,
- },
- Nonce: uint64(chainParams.Genesis.Nonce),
- Timestamp: uint64(chainParams.Genesis.Timestamp),
- ExtraData: chainParams.Genesis.ExtraData,
- GasLimit: uint64(chainParams.Genesis.GasLimit),
- Difficulty: big.NewInt(0).Set((*big.Int)(chainParams.Genesis.Difficulty)),
- Mixhash: common.BigToHash((*big.Int)(chainParams.Genesis.MixHash)),
- Coinbase: chainParams.Genesis.Author,
- ParentHash: chainParams.Genesis.ParentHash,
- Alloc: accounts,
- }
- chainConfig, genesisHash, err := core.SetupGenesisBlock(ethDb, genesis)
- if err != nil {
- return false, err
- }
- fmt.Printf("Chain config: %v\n", chainConfig)
-
- var inner consensus.Engine
- switch chainParams.SealEngine {
- case "NoProof", "NoReward":
- inner = ethash.NewFaker()
- case "Ethash":
- inner = ethash.New(ethash.Config{
- CacheDir: "ethash",
- CachesInMem: 2,
- CachesOnDisk: 3,
- DatasetsInMem: 1,
- DatasetsOnDisk: 2,
- }, nil, false)
- default:
- return false, fmt.Errorf("unrecognised seal engine: %s", chainParams.SealEngine)
- }
- engine := &NoRewardEngine{inner: inner, rewardsOn: chainParams.SealEngine != "NoReward"}
-
- blockchain, err := core.NewBlockChain(ethDb, nil, chainConfig, engine, vm.Config{}, nil)
- if err != nil {
- return false, err
- }
-
- api.chainConfig = chainConfig
- api.genesisHash = genesisHash
- api.author = chainParams.Genesis.Author
- api.extraData = chainParams.Genesis.ExtraData
- api.ethDb = ethDb
- api.engine = engine
- api.blockchain = blockchain
- api.db = state.NewDatabase(api.ethDb)
- api.blockNumber = 0
- api.txMap = make(map[common.Address]map[uint64]*types.Transaction)
- api.txSenders = make(map[common.Address]struct{})
- api.blockInterval = 0
- return true, nil
-}
-
-func (api *RetestethAPI) SendRawTransaction(ctx context.Context, rawTx hexutil.Bytes) (common.Hash, error) {
- tx := new(types.Transaction)
- if err := rlp.DecodeBytes(rawTx, tx); err != nil {
- // Return nil is not by mistake - some tests include sending transaction where gasLimit overflows uint64
- return common.Hash{}, nil
- }
- signer := types.MakeSigner(api.chainConfig, big.NewInt(int64(api.blockNumber)))
- sender, err := types.Sender(signer, tx)
- if err != nil {
- return common.Hash{}, err
- }
- if nonceMap, ok := api.txMap[sender]; ok {
- nonceMap[tx.Nonce()] = tx
- } else {
- nonceMap = make(map[uint64]*types.Transaction)
- nonceMap[tx.Nonce()] = tx
- api.txMap[sender] = nonceMap
- }
- api.txSenders[sender] = struct{}{}
- return tx.Hash(), nil
-}
-
-func (api *RetestethAPI) MineBlocks(ctx context.Context, number uint64) (bool, error) {
- for i := 0; i < int(number); i++ {
- if err := api.mineBlock(); err != nil {
- return false, err
- }
- }
- fmt.Printf("Mined %d blocks\n", number)
- return true, nil
-}
-
-func (api *RetestethAPI) mineBlock() error {
- parentHash := rawdb.ReadCanonicalHash(api.ethDb, api.blockNumber)
- parent := rawdb.ReadBlock(api.ethDb, parentHash, api.blockNumber)
- var timestamp uint64
- if api.blockInterval == 0 {
- timestamp = uint64(time.Now().Unix())
- } else {
- timestamp = parent.Time() + api.blockInterval
- }
- gasLimit := core.CalcGasLimit(parent, 9223372036854775807, 9223372036854775807)
- header := &types.Header{
- ParentHash: parent.Hash(),
- Number: big.NewInt(int64(api.blockNumber + 1)),
- GasLimit: gasLimit,
- Extra: api.extraData,
- Time: timestamp,
- }
- header.Coinbase = api.author
- if api.engine != nil {
- api.engine.Prepare(api.blockchain, header)
- }
- // If we are care about TheDAO hard-fork check whether to override the extra-data or not
- if daoBlock := api.chainConfig.DAOForkBlock; daoBlock != nil {
- // Check whether the block is among the fork extra-override range
- limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
- if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
- // Depending whether we support or oppose the fork, override differently
- if api.chainConfig.DAOForkSupport {
- header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
- } else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
- header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data
- }
- }
- }
- statedb, err := api.blockchain.StateAt(parent.Root())
- if err != nil {
- return err
- }
- if api.chainConfig.DAOForkSupport && api.chainConfig.DAOForkBlock != nil && api.chainConfig.DAOForkBlock.Cmp(header.Number) == 0 {
- misc.ApplyDAOHardFork(statedb)
- }
- gasPool := new(core.GasPool).AddGas(header.GasLimit)
- txCount := 0
- var txs []*types.Transaction
- var receipts []*types.Receipt
- var coalescedLogs []*types.Log
- var blockFull = gasPool.Gas() < params.TxGas
- for address := range api.txSenders {
- if blockFull {
- break
- }
- m := api.txMap[address]
- for nonce := statedb.GetNonce(address); ; nonce++ {
- if tx, ok := m[nonce]; ok {
- // Try to apply transactions to the state
- statedb.Prepare(tx.Hash(), common.Hash{}, txCount)
- snap := statedb.Snapshot()
-
- receipt, _, err := core.ApplyTransaction(
- api.chainConfig,
- api.blockchain,
- &api.author,
- gasPool,
- statedb,
- header, tx, &header.GasUsed, *api.blockchain.GetVMConfig(),
- )
- if err != nil {
- statedb.RevertToSnapshot(snap)
- break
- }
- txs = append(txs, tx)
- receipts = append(receipts, receipt)
- coalescedLogs = append(coalescedLogs, receipt.Logs...)
- delete(m, nonce)
- if len(m) == 0 {
- // Last tx for the sender
- delete(api.txMap, address)
- delete(api.txSenders, address)
- }
- txCount++
- if gasPool.Gas() < params.TxGas {
- blockFull = true
- break
- }
- } else {
- break // Gap in the nonces
- }
- }
- }
- block, err := api.engine.FinalizeAndAssemble(api.blockchain, header, statedb, txs, []*types.Header{}, receipts)
- if err != nil {
- return err
- }
- return api.importBlock(block)
-}
-
-func (api *RetestethAPI) importBlock(block *types.Block) error {
- if _, err := api.blockchain.InsertChain([]*types.Block{block}); err != nil {
- return err
- }
- api.blockNumber = block.NumberU64()
- fmt.Printf("Imported block %d\n", block.NumberU64())
- return nil
-}
-
-func (api *RetestethAPI) ModifyTimestamp(ctx context.Context, interval uint64) (bool, error) {
- api.blockInterval = interval
- return true, nil
-}
-
-func (api *RetestethAPI) ImportRawBlock(ctx context.Context, rawBlock hexutil.Bytes) (common.Hash, error) {
- block := new(types.Block)
- if err := rlp.DecodeBytes(rawBlock, block); err != nil {
- return common.Hash{}, err
- }
- fmt.Printf("Importing block %d with parent hash: %x, genesisHash: %x\n", block.NumberU64(), block.ParentHash(), api.genesisHash)
- if err := api.importBlock(block); err != nil {
- return common.Hash{}, err
- }
- return block.Hash(), nil
-}
-
-func (api *RetestethAPI) RewindToBlock(ctx context.Context, newHead uint64) (bool, error) {
- if err := api.blockchain.SetHead(newHead); err != nil {
- return false, err
- }
- api.blockNumber = newHead
- return true, nil
-}
-
-var emptyListHash common.Hash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
-
-func (api *RetestethAPI) GetLogHash(ctx context.Context, txHash common.Hash) (common.Hash, error) {
- receipt, _, _, _ := rawdb.ReadReceipt(api.ethDb, txHash, api.chainConfig)
- if receipt == nil {
- return emptyListHash, nil
- } else {
- if logListRlp, err := rlp.EncodeToBytes(receipt.Logs); err != nil {
- return common.Hash{}, err
- } else {
- return common.BytesToHash(crypto.Keccak256(logListRlp)), nil
- }
- }
-}
-
-func (api *RetestethAPI) BlockNumber(ctx context.Context) (uint64, error) {
- //fmt.Printf("BlockNumber, response: %d\n", api.blockNumber)
- return api.blockNumber, nil
-}
-
-func (api *RetestethAPI) GetBlockByNumber(ctx context.Context, blockNr math.HexOrDecimal64, fullTx bool) (map[string]interface{}, error) {
- block := api.blockchain.GetBlockByNumber(uint64(blockNr))
- if block != nil {
- response, err := RPCMarshalBlock(block, true, fullTx)
- if err != nil {
- return nil, err
- }
- response["author"] = response["miner"]
- response["totalDifficulty"] = (*hexutil.Big)(api.blockchain.GetTd(block.Hash(), uint64(blockNr)))
- return response, err
- }
- return nil, fmt.Errorf("block %d not found", blockNr)
-}
-
-func (api *RetestethAPI) AccountRange(ctx context.Context,
- blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
- addressHash *math.HexOrDecimal256, maxResults uint64,
-) (AccountRangeResult, error) {
- var (
- header *types.Header
- block *types.Block
- )
- if (*big.Int)(blockHashOrNumber).Cmp(big.NewInt(math.MaxInt64)) > 0 {
- blockHash := common.BigToHash((*big.Int)(blockHashOrNumber))
- header = api.blockchain.GetHeaderByHash(blockHash)
- block = api.blockchain.GetBlockByHash(blockHash)
- //fmt.Printf("Account range: %x, txIndex %d, start: %x, maxResults: %d\n", blockHash, txIndex, common.BigToHash((*big.Int)(addressHash)), maxResults)
- } else {
- blockNumber := (*big.Int)(blockHashOrNumber).Uint64()
- header = api.blockchain.GetHeaderByNumber(blockNumber)
- block = api.blockchain.GetBlockByNumber(blockNumber)
- //fmt.Printf("Account range: %d, txIndex %d, start: %x, maxResults: %d\n", blockNumber, txIndex, common.BigToHash((*big.Int)(addressHash)), maxResults)
- }
- parentHeader := api.blockchain.GetHeaderByHash(header.ParentHash)
- var root common.Hash
- var statedb *state.StateDB
- var err error
- if parentHeader == nil || int(txIndex) >= len(block.Transactions()) {
- root = header.Root
- statedb, err = api.blockchain.StateAt(root)
- if err != nil {
- return AccountRangeResult{}, err
- }
- } else {
- root = parentHeader.Root
- statedb, err = api.blockchain.StateAt(root)
- if err != nil {
- return AccountRangeResult{}, err
- }
- // Recompute transactions up to the target index.
- signer := types.MakeSigner(api.blockchain.Config(), block.Number())
- for idx, tx := range block.Transactions() {
- // Assemble the transaction call message and return if the requested offset
- msg, _ := tx.AsMessage(signer)
- context := core.NewEVMContext(msg, block.Header(), api.blockchain, nil)
- // Not yet the searched for transaction, execute on top of the current state
- vmenv := vm.NewEVM(context, statedb, api.blockchain.Config(), vm.Config{})
- if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
- return AccountRangeResult{}, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
- }
- // Ensure any modifications are committed to the state
- // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
- root = statedb.IntermediateRoot(vmenv.ChainConfig().IsEIP158(block.Number()))
- if idx == int(txIndex) {
- // This is to make sure root can be opened by OpenTrie
- root, err = statedb.Commit(api.chainConfig.IsEIP158(block.Number()))
- if err != nil {
- return AccountRangeResult{}, err
- }
- break
- }
- }
- }
- accountTrie, err := statedb.Database().OpenTrie(root)
- if err != nil {
- return AccountRangeResult{}, err
- }
- it := trie.NewIterator(accountTrie.NodeIterator(common.BigToHash((*big.Int)(addressHash)).Bytes()))
- result := AccountRangeResult{AddressMap: make(map[common.Hash]common.Address)}
- for i := 0; /*i < int(maxResults) && */ it.Next(); i++ {
- if preimage := accountTrie.GetKey(it.Key); preimage != nil {
- result.AddressMap[common.BytesToHash(it.Key)] = common.BytesToAddress(preimage)
- //fmt.Printf("%x: %x\n", it.Key, preimage)
- } else {
- //fmt.Printf("could not find preimage for %x\n", it.Key)
- }
- }
- //fmt.Printf("Number of entries returned: %d\n", len(result.AddressMap))
- // Add the 'next key' so clients can continue downloading.
- if it.Next() {
- next := common.BytesToHash(it.Key)
- result.NextKey = next
- }
- return result, nil
-}
-
-func (api *RetestethAPI) GetBalance(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (*math.HexOrDecimal256, error) {
- //fmt.Printf("GetBalance %x, block %d\n", address, blockNr)
- header := api.blockchain.GetHeaderByNumber(uint64(blockNr))
- statedb, err := api.blockchain.StateAt(header.Root)
- if err != nil {
- return nil, err
- }
- return (*math.HexOrDecimal256)(statedb.GetBalance(address)), nil
-}
-
-func (api *RetestethAPI) GetCode(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (hexutil.Bytes, error) {
- header := api.blockchain.GetHeaderByNumber(uint64(blockNr))
- statedb, err := api.blockchain.StateAt(header.Root)
- if err != nil {
- return nil, err
- }
- return statedb.GetCode(address), nil
-}
-
-func (api *RetestethAPI) GetTransactionCount(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (uint64, error) {
- header := api.blockchain.GetHeaderByNumber(uint64(blockNr))
- statedb, err := api.blockchain.StateAt(header.Root)
- if err != nil {
- return 0, err
- }
- return statedb.GetNonce(address), nil
-}
-
-func (api *RetestethAPI) StorageRangeAt(ctx context.Context,
- blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
- address common.Address,
- begin *math.HexOrDecimal256, maxResults uint64,
-) (StorageRangeResult, error) {
- var (
- header *types.Header
- block *types.Block
- )
- if (*big.Int)(blockHashOrNumber).Cmp(big.NewInt(math.MaxInt64)) > 0 {
- blockHash := common.BigToHash((*big.Int)(blockHashOrNumber))
- header = api.blockchain.GetHeaderByHash(blockHash)
- block = api.blockchain.GetBlockByHash(blockHash)
- //fmt.Printf("Storage range: %x, txIndex %d, addr: %x, start: %x, maxResults: %d\n",
- // blockHash, txIndex, address, common.BigToHash((*big.Int)(begin)), maxResults)
- } else {
- blockNumber := (*big.Int)(blockHashOrNumber).Uint64()
- header = api.blockchain.GetHeaderByNumber(blockNumber)
- block = api.blockchain.GetBlockByNumber(blockNumber)
- //fmt.Printf("Storage range: %d, txIndex %d, addr: %x, start: %x, maxResults: %d\n",
- // blockNumber, txIndex, address, common.BigToHash((*big.Int)(begin)), maxResults)
- }
- parentHeader := api.blockchain.GetHeaderByHash(header.ParentHash)
- var root common.Hash
- var statedb *state.StateDB
- var err error
- if parentHeader == nil || int(txIndex) >= len(block.Transactions()) {
- root = header.Root
- statedb, err = api.blockchain.StateAt(root)
- if err != nil {
- return StorageRangeResult{}, err
- }
- } else {
- root = parentHeader.Root
- statedb, err = api.blockchain.StateAt(root)
- if err != nil {
- return StorageRangeResult{}, err
- }
- // Recompute transactions up to the target index.
- signer := types.MakeSigner(api.blockchain.Config(), block.Number())
- for idx, tx := range block.Transactions() {
- // Assemble the transaction call message and return if the requested offset
- msg, _ := tx.AsMessage(signer)
- context := core.NewEVMContext(msg, block.Header(), api.blockchain, nil)
- // Not yet the searched for transaction, execute on top of the current state
- vmenv := vm.NewEVM(context, statedb, api.blockchain.Config(), vm.Config{})
- if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
- return StorageRangeResult{}, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
- }
- // Ensure any modifications are committed to the state
- // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
- _ = statedb.IntermediateRoot(vmenv.ChainConfig().IsEIP158(block.Number()))
- if idx == int(txIndex) {
- // This is to make sure root can be opened by OpenTrie
- _, err = statedb.Commit(vmenv.ChainConfig().IsEIP158(block.Number()))
- if err != nil {
- return StorageRangeResult{}, err
- }
- }
- }
- }
- storageTrie := statedb.StorageTrie(address)
- it := trie.NewIterator(storageTrie.NodeIterator(common.BigToHash((*big.Int)(begin)).Bytes()))
- result := StorageRangeResult{Storage: make(map[common.Hash]SRItem)}
- for i := 0; /*i < int(maxResults) && */ it.Next(); i++ {
- if preimage := storageTrie.GetKey(it.Key); preimage != nil {
- key := (*math.HexOrDecimal256)(big.NewInt(0).SetBytes(preimage))
- v, _, err := rlp.SplitString(it.Value)
- if err != nil {
- return StorageRangeResult{}, err
- }
- value := (*math.HexOrDecimal256)(big.NewInt(0).SetBytes(v))
- ks, _ := key.MarshalText()
- vs, _ := value.MarshalText()
- if len(ks)%2 != 0 {
- ks = append(append(append([]byte{}, ks[:2]...), byte('0')), ks[2:]...)
- }
- if len(vs)%2 != 0 {
- vs = append(append(append([]byte{}, vs[:2]...), byte('0')), vs[2:]...)
- }
- result.Storage[common.BytesToHash(it.Key)] = SRItem{
- Key: string(ks),
- Value: string(vs),
- }
- //fmt.Printf("Key: %s, Value: %s\n", ks, vs)
- } else {
- //fmt.Printf("Did not find preimage for %x\n", it.Key)
- }
- }
- if it.Next() {
- result.Complete = false
- } else {
- result.Complete = true
- }
- return result, nil
-}
-
-func (api *RetestethAPI) ClientVersion(ctx context.Context) (string, error) {
- return "Geth-" + params.VersionWithCommit(gitCommit, gitDate), nil
-}
-
-// splitAndTrim splits input separated by a comma
-// and trims excessive white space from the substrings.
-func splitAndTrim(input string) []string {
- result := strings.Split(input, ",")
- for i, r := range result {
- result[i] = strings.TrimSpace(r)
- }
- return result
-}
-
-func retesteth(ctx *cli.Context) error {
- log.Info("Welcome to retesteth!")
- // register signer API with server
- var (
- extapiURL string
- )
- apiImpl := &RetestethAPI{}
- var testApi RetestethTestAPI = apiImpl
- var ethApi RetestethEthAPI = apiImpl
- var debugApi RetestethDebugAPI = apiImpl
- var web3Api RetestWeb3API = apiImpl
- rpcAPI := []rpc.API{
- {
- Namespace: "test",
- Public: true,
- Service: testApi,
- Version: "1.0",
- },
- {
- Namespace: "eth",
- Public: true,
- Service: ethApi,
- Version: "1.0",
- },
- {
- Namespace: "debug",
- Public: true,
- Service: debugApi,
- Version: "1.0",
- },
- {
- Namespace: "web3",
- Public: true,
- Service: web3Api,
- Version: "1.0",
- },
- }
- vhosts := splitAndTrim(ctx.GlobalString(utils.RPCVirtualHostsFlag.Name))
- cors := splitAndTrim(ctx.GlobalString(utils.RPCCORSDomainFlag.Name))
-
- // start http server
- httpEndpoint := fmt.Sprintf("%s:%d", ctx.GlobalString(utils.RPCListenAddrFlag.Name), ctx.Int(rpcPortFlag.Name))
- listener, _, err := rpc.StartHTTPEndpoint(httpEndpoint, rpcAPI, []string{"test", "eth", "debug", "web3"}, cors, vhosts, rpc.DefaultHTTPTimeouts)
- if err != nil {
- utils.Fatalf("Could not start RPC api: %v", err)
- }
- extapiURL = fmt.Sprintf("http://%s", httpEndpoint)
- log.Info("HTTP endpoint opened", "url", extapiURL)
-
- defer func() {
- listener.Close()
- log.Info("HTTP endpoint closed", "url", httpEndpoint)
- }()
-
- abortChan := make(chan os.Signal)
- signal.Notify(abortChan, os.Interrupt)
-
- sig := <-abortChan
- log.Info("Exiting...", "signal", sig)
- return nil
-}
diff --git a/cmd/geth/retesteth_copypaste.go b/cmd/geth/retesteth_copypaste.go
deleted file mode 100644
index b48b007..0000000
--- a/cmd/geth/retesteth_copypaste.go
+++ /dev/null
@@ -1,148 +0,0 @@
-// Copyright 2019 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-package geth
-
-import (
- "math/big"
-
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core/types"
-)
-
-// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
-type RPCTransaction struct {
- BlockHash common.Hash `json:"blockHash"`
- BlockNumber *hexutil.Big `json:"blockNumber"`
- From common.Address `json:"from"`
- Gas hexutil.Uint64 `json:"gas"`
- GasPrice *hexutil.Big `json:"gasPrice"`
- Hash common.Hash `json:"hash"`
- Input hexutil.Bytes `json:"input"`
- Nonce hexutil.Uint64 `json:"nonce"`
- To *common.Address `json:"to"`
- TransactionIndex hexutil.Uint `json:"transactionIndex"`
- Value *hexutil.Big `json:"value"`
- V *hexutil.Big `json:"v"`
- R *hexutil.Big `json:"r"`
- S *hexutil.Big `json:"s"`
-}
-
-// newRPCTransaction returns a transaction that will serialize to the RPC
-// representation, with the given location metadata set (if available).
-func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64) *RPCTransaction {
- var signer types.Signer = types.FrontierSigner{}
- if tx.Protected() {
- signer = types.NewEIP155Signer(tx.ChainId())
- }
- from, _ := types.Sender(signer, tx)
- v, r, s := tx.RawSignatureValues()
-
- result := &RPCTransaction{
- From: from,
- Gas: hexutil.Uint64(tx.Gas()),
- GasPrice: (*hexutil.Big)(tx.GasPrice()),
- Hash: tx.Hash(),
- Input: hexutil.Bytes(tx.Data()),
- Nonce: hexutil.Uint64(tx.Nonce()),
- To: tx.To(),
- Value: (*hexutil.Big)(tx.Value()),
- V: (*hexutil.Big)(v),
- R: (*hexutil.Big)(r),
- S: (*hexutil.Big)(s),
- }
- if blockHash != (common.Hash{}) {
- result.BlockHash = blockHash
- result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
- result.TransactionIndex = hexutil.Uint(index)
- }
- return result
-}
-
-// newRPCTransactionFromBlockIndex returns a transaction that will serialize to the RPC representation.
-func newRPCTransactionFromBlockIndex(b *types.Block, index uint64) *RPCTransaction {
- txs := b.Transactions()
- if index >= uint64(len(txs)) {
- return nil
- }
- return newRPCTransaction(txs[index], b.Hash(), b.NumberU64(), index)
-}
-
-// newRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
-func newRPCTransactionFromBlockHash(b *types.Block, hash common.Hash) *RPCTransaction {
- for idx, tx := range b.Transactions() {
- if tx.Hash() == hash {
- return newRPCTransactionFromBlockIndex(b, uint64(idx))
- }
- }
- return nil
-}
-
-// RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
-// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
-// transaction hashes.
-func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
- head := b.Header() // copies the header once
- fields := map[string]interface{}{
- "number": (*hexutil.Big)(head.Number),
- "hash": b.Hash(),
- "parentHash": head.ParentHash,
- "nonce": head.Nonce,
- "mixHash": head.MixDigest,
- "sha3Uncles": head.UncleHash,
- "logsBloom": head.Bloom,
- "stateRoot": head.Root,
- "miner": head.Coinbase,
- "difficulty": (*hexutil.Big)(head.Difficulty),
- "extraData": hexutil.Bytes(head.Extra),
- "size": hexutil.Uint64(b.Size()),
- "gasLimit": hexutil.Uint64(head.GasLimit),
- "gasUsed": hexutil.Uint64(head.GasUsed),
- "timestamp": hexutil.Uint64(head.Time),
- "transactionsRoot": head.TxHash,
- "receiptsRoot": head.ReceiptHash,
- }
-
- if inclTx {
- formatTx := func(tx *types.Transaction) (interface{}, error) {
- return tx.Hash(), nil
- }
- if fullTx {
- formatTx = func(tx *types.Transaction) (interface{}, error) {
- return newRPCTransactionFromBlockHash(b, tx.Hash()), nil
- }
- }
- txs := b.Transactions()
- transactions := make([]interface{}, len(txs))
- var err error
- for i, tx := range txs {
- if transactions[i], err = formatTx(tx); err != nil {
- return nil, err
- }
- }
- fields["transactions"] = transactions
- }
-
- uncles := b.Uncles()
- uncleHashes := make([]common.Hash, len(uncles))
- for i, uncle := range uncles {
- uncleHashes[i] = uncle.Hash()
- }
- fields["uncles"] = uncleHashes
-
- return fields, nil
-}
diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go
deleted file mode 100644
index 2eb822d..0000000
--- a/cmd/geth/usage.go
+++ /dev/null
@@ -1,370 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-// Contains the geth command usage template and generator.
-
-package geth
-
-import (
- "io"
- "sort"
-
- "strings"
-
- "github.com/ava-labs/coreth/cmd/utils"
- "github.com/ava-labs/coreth/internal/debug"
- cli "gopkg.in/urfave/cli.v1"
-)
-
-// AppHelpTemplate is the test template for the default, global app help topic.
-var AppHelpTemplate = `NAME:
- {{.App.Name}} - {{.App.Usage}}
-
- Copyright 2013-2019 The go-ethereum Authors
-
-USAGE:
- {{.App.HelpName}} [options]{{if .App.Commands}} command [command options]{{end}} {{if .App.ArgsUsage}}{{.App.ArgsUsage}}{{else}}[arguments...]{{end}}
- {{if .App.Version}}
-VERSION:
- {{.App.Version}}
- {{end}}{{if len .App.Authors}}
-AUTHOR(S):
- {{range .App.Authors}}{{ . }}{{end}}
- {{end}}{{if .App.Commands}}
-COMMANDS:
- {{range .App.Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
- {{end}}{{end}}{{if .FlagGroups}}
-{{range .FlagGroups}}{{.Name}} OPTIONS:
- {{range .Flags}}{{.}}
- {{end}}
-{{end}}{{end}}{{if .App.Copyright }}
-COPYRIGHT:
- {{.App.Copyright}}
- {{end}}
-`
-
-// flagGroup is a collection of flags belonging to a single topic.
-type flagGroup struct {
- Name string
- Flags []cli.Flag
-}
-
-// AppHelpFlagGroups is the application flags, grouped by functionality.
-var AppHelpFlagGroups = []flagGroup{
- {
- Name: "ETHEREUM",
- Flags: []cli.Flag{
- configFileFlag,
- utils.DataDirFlag,
- utils.AncientFlag,
- utils.KeyStoreDirFlag,
- utils.NoUSBFlag,
- utils.SmartCardDaemonPathFlag,
- utils.NetworkIdFlag,
- utils.TestnetFlag,
- utils.RinkebyFlag,
- utils.GoerliFlag,
- utils.SyncModeFlag,
- utils.ExitWhenSyncedFlag,
- utils.GCModeFlag,
- utils.EthStatsURLFlag,
- utils.IdentityFlag,
- utils.LightKDFFlag,
- utils.WhitelistFlag,
- },
- },
- {
- Name: "LIGHT CLIENT",
- Flags: []cli.Flag{
- utils.LightServeFlag,
- utils.LightIngressFlag,
- utils.LightEgressFlag,
- utils.LightMaxPeersFlag,
- utils.UltraLightServersFlag,
- utils.UltraLightFractionFlag,
- utils.UltraLightOnlyAnnounceFlag,
- },
- },
- {
- Name: "DEVELOPER CHAIN",
- Flags: []cli.Flag{
- utils.DeveloperFlag,
- utils.DeveloperPeriodFlag,
- },
- },
- {
- Name: "ETHASH",
- Flags: []cli.Flag{
- utils.EthashCacheDirFlag,
- utils.EthashCachesInMemoryFlag,
- utils.EthashCachesOnDiskFlag,
- utils.EthashDatasetDirFlag,
- utils.EthashDatasetsInMemoryFlag,
- utils.EthashDatasetsOnDiskFlag,
- },
- },
- //{
- // Name: "DASHBOARD",
- // Flags: []cli.Flag{
- // utils.DashboardEnabledFlag,
- // utils.DashboardAddrFlag,
- // utils.DashboardPortFlag,
- // utils.DashboardRefreshFlag,
- // utils.DashboardAssetsFlag,
- // },
- //},
- {
- Name: "TRANSACTION POOL",
- Flags: []cli.Flag{
- utils.TxPoolLocalsFlag,
- utils.TxPoolNoLocalsFlag,
- utils.TxPoolJournalFlag,
- utils.TxPoolRejournalFlag,
- utils.TxPoolPriceLimitFlag,
- utils.TxPoolPriceBumpFlag,
- utils.TxPoolAccountSlotsFlag,
- utils.TxPoolGlobalSlotsFlag,
- utils.TxPoolAccountQueueFlag,
- utils.TxPoolGlobalQueueFlag,
- utils.TxPoolLifetimeFlag,
- },
- },
- {
- Name: "PERFORMANCE TUNING",
- Flags: []cli.Flag{
- utils.CacheFlag,
- utils.CacheDatabaseFlag,
- utils.CacheTrieFlag,
- utils.CacheGCFlag,
- utils.CacheNoPrefetchFlag,
- },
- },
- {
- Name: "ACCOUNT",
- Flags: []cli.Flag{
- utils.UnlockedAccountFlag,
- utils.PasswordFileFlag,
- utils.ExternalSignerFlag,
- utils.InsecureUnlockAllowedFlag,
- },
- },
- {
- Name: "API AND CONSOLE",
- Flags: []cli.Flag{
- utils.IPCDisabledFlag,
- utils.IPCPathFlag,
- utils.RPCEnabledFlag,
- utils.RPCListenAddrFlag,
- utils.RPCPortFlag,
- utils.RPCApiFlag,
- utils.RPCGlobalGasCap,
- utils.RPCCORSDomainFlag,
- utils.RPCVirtualHostsFlag,
- utils.WSEnabledFlag,
- utils.WSListenAddrFlag,
- utils.WSPortFlag,
- utils.WSApiFlag,
- utils.WSAllowedOriginsFlag,
- utils.GraphQLEnabledFlag,
- utils.GraphQLListenAddrFlag,
- utils.GraphQLPortFlag,
- utils.GraphQLCORSDomainFlag,
- utils.GraphQLVirtualHostsFlag,
- utils.JSpathFlag,
- utils.ExecFlag,
- utils.PreloadJSFlag,
- },
- },
- {
- Name: "NETWORKING",
- Flags: []cli.Flag{
- utils.BootnodesFlag,
- utils.BootnodesV4Flag,
- utils.BootnodesV5Flag,
- utils.ListenPortFlag,
- utils.MaxPeersFlag,
- utils.MaxPendingPeersFlag,
- utils.NATFlag,
- utils.NoDiscoverFlag,
- utils.DiscoveryV5Flag,
- utils.NetrestrictFlag,
- utils.NodeKeyFileFlag,
- utils.NodeKeyHexFlag,
- },
- },
- {
- Name: "MINER",
- Flags: []cli.Flag{
- utils.MiningEnabledFlag,
- utils.MinerThreadsFlag,
- utils.MinerNotifyFlag,
- utils.MinerGasPriceFlag,
- utils.MinerGasTargetFlag,
- utils.MinerGasLimitFlag,
- utils.MinerEtherbaseFlag,
- utils.MinerExtraDataFlag,
- utils.MinerRecommitIntervalFlag,
- utils.MinerNoVerfiyFlag,
- },
- },
- {
- Name: "GAS PRICE ORACLE",
- Flags: []cli.Flag{
- utils.GpoBlocksFlag,
- utils.GpoPercentileFlag,
- },
- },
- {
- Name: "VIRTUAL MACHINE",
- Flags: []cli.Flag{
- utils.VMEnableDebugFlag,
- utils.EVMInterpreterFlag,
- utils.EWASMInterpreterFlag,
- },
- },
- {
- Name: "LOGGING AND DEBUGGING",
- Flags: append([]cli.Flag{
- utils.FakePoWFlag,
- utils.NoCompactionFlag,
- }, debug.Flags...),
- },
- {
- Name: "METRICS AND STATS",
- Flags: metricsFlags,
- },
- {
- Name: "WHISPER (EXPERIMENTAL)",
- Flags: whisperFlags,
- },
- {
- Name: "DEPRECATED",
- Flags: []cli.Flag{
- utils.LightLegacyServFlag,
- utils.LightLegacyPeersFlag,
- utils.MinerLegacyThreadsFlag,
- utils.MinerLegacyGasTargetFlag,
- utils.MinerLegacyGasPriceFlag,
- utils.MinerLegacyEtherbaseFlag,
- utils.MinerLegacyExtraDataFlag,
- },
- },
- {
- Name: "MISC",
- },
-}
-
-// byCategory sorts an array of flagGroup by Name in the order
-// defined in AppHelpFlagGroups.
-type byCategory []flagGroup
-
-func (a byCategory) Len() int { return len(a) }
-func (a byCategory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
-func (a byCategory) Less(i, j int) bool {
- iCat, jCat := a[i].Name, a[j].Name
- iIdx, jIdx := len(AppHelpFlagGroups), len(AppHelpFlagGroups) // ensure non categorized flags come last
-
- for i, group := range AppHelpFlagGroups {
- if iCat == group.Name {
- iIdx = i
- }
- if jCat == group.Name {
- jIdx = i
- }
- }
-
- return iIdx < jIdx
-}
-
-func flagCategory(flag cli.Flag) string {
- for _, category := range AppHelpFlagGroups {
- for _, flg := range category.Flags {
- if flg.GetName() == flag.GetName() {
- return category.Name
- }
- }
- }
- return "MISC"
-}
-
-func init() {
- // Override the default app help template
- cli.AppHelpTemplate = AppHelpTemplate
-
- // Define a one shot struct to pass to the usage template
- type helpData struct {
- App interface{}
- FlagGroups []flagGroup
- }
-
- // Override the default app help printer, but only for the global app help
- originalHelpPrinter := cli.HelpPrinter
- cli.HelpPrinter = func(w io.Writer, tmpl string, data interface{}) {
- if tmpl == AppHelpTemplate {
- // Iterate over all the flags and add any uncategorized ones
- categorized := make(map[string]struct{})
- for _, group := range AppHelpFlagGroups {
- for _, flag := range group.Flags {
- categorized[flag.String()] = struct{}{}
- }
- }
- var uncategorized []cli.Flag
- for _, flag := range data.(*cli.App).Flags {
- if _, ok := categorized[flag.String()]; !ok {
- if strings.HasPrefix(flag.GetName(), "dashboard") {
- continue
- }
- uncategorized = append(uncategorized, flag)
- }
- }
- if len(uncategorized) > 0 {
- // Append all ungategorized options to the misc group
- miscs := len(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags)
- AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = append(AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags, uncategorized...)
-
- // Make sure they are removed afterwards
- defer func() {
- AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags = AppHelpFlagGroups[len(AppHelpFlagGroups)-1].Flags[:miscs]
- }()
- }
- // Render out custom usage screen
- originalHelpPrinter(w, tmpl, helpData{data, AppHelpFlagGroups})
- } else if tmpl == utils.CommandHelpTemplate {
- // Iterate over all command specific flags and categorize them
- categorized := make(map[string][]cli.Flag)
- for _, flag := range data.(cli.Command).Flags {
- if _, ok := categorized[flag.String()]; !ok {
- categorized[flagCategory(flag)] = append(categorized[flagCategory(flag)], flag)
- }
- }
-
- // sort to get a stable ordering
- sorted := make([]flagGroup, 0, len(categorized))
- for cat, flgs := range categorized {
- sorted = append(sorted, flagGroup{cat, flgs})
- }
- sort.Sort(byCategory(sorted))
-
- // add sorted array to data and render with default printer
- originalHelpPrinter(w, tmpl, map[string]interface{}{
- "cmd": data,
- "categorizedFlags": sorted,
- })
- } else {
- originalHelpPrinter(w, tmpl, data)
- }
- }
-}
diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go
deleted file mode 100644
index cf88aec..0000000
--- a/cmd/utils/cmd.go
+++ /dev/null
@@ -1,314 +0,0 @@
-// Copyright 2014 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-// Package utils contains internal helper functions for go-ethereum commands.
-package utils
-
-import (
- "compress/gzip"
- "fmt"
- "io"
- "os"
- "os/signal"
- "runtime"
- "strings"
- "syscall"
-
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/crypto"
- "github.com/ava-labs/go-ethereum/ethdb"
- "github.com/ava-labs/coreth/internal/debug"
- "github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/coreth/node"
- "github.com/ava-labs/go-ethereum/rlp"
-)
-
-const (
- importBatchSize = 2500
-)
-
-// Fatalf formats a message to standard error and exits the program.
-// The message is also printed to standard output if standard error
-// is redirected to a different file.
-func Fatalf(format string, args ...interface{}) {
- w := io.MultiWriter(os.Stdout, os.Stderr)
- if runtime.GOOS == "windows" {
- // The SameFile check below doesn't work on Windows.
- // stdout is unlikely to get redirected though, so just print there.
- w = os.Stdout
- } else {
- outf, _ := os.Stdout.Stat()
- errf, _ := os.Stderr.Stat()
- if outf != nil && errf != nil && os.SameFile(outf, errf) {
- w = os.Stderr
- }
- }
- fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
- os.Exit(1)
-}
-
-func StartNode(stack *node.Node) {
- if err := stack.Start(); err != nil {
- Fatalf("Error starting protocol stack: %v", err)
- }
- go func() {
- sigc := make(chan os.Signal, 1)
- signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
- defer signal.Stop(sigc)
- <-sigc
- log.Info("Got interrupt, shutting down...")
- go stack.Stop()
- for i := 10; i > 0; i-- {
- <-sigc
- if i > 1 {
- log.Warn("Already shutting down, interrupt more to panic.", "times", i-1)
- }
- }
- debug.Exit() // ensure trace and CPU profile data is flushed.
- debug.LoudPanic("boom")
- }()
-}
-
-func ImportChain(chain *core.BlockChain, fn string) error {
- // Watch for Ctrl-C while the import is running.
- // If a signal is received, the import will stop at the next batch.
- interrupt := make(chan os.Signal, 1)
- stop := make(chan struct{})
- signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
- defer signal.Stop(interrupt)
- defer close(interrupt)
- go func() {
- if _, ok := <-interrupt; ok {
- log.Info("Interrupted during import, stopping at next batch")
- }
- close(stop)
- }()
- checkInterrupt := func() bool {
- select {
- case <-stop:
- return true
- default:
- return false
- }
- }
-
- log.Info("Importing blockchain", "file", fn)
-
- // Open the file handle and potentially unwrap the gzip stream
- fh, err := os.Open(fn)
- if err != nil {
- return err
- }
- defer fh.Close()
-
- var reader io.Reader = fh
- if strings.HasSuffix(fn, ".gz") {
- if reader, err = gzip.NewReader(reader); err != nil {
- return err
- }
- }
- stream := rlp.NewStream(reader, 0)
-
- // Run actual the import.
- blocks := make(types.Blocks, importBatchSize)
- n := 0
- for batch := 0; ; batch++ {
- // Load a batch of RLP blocks.
- if checkInterrupt() {
- return fmt.Errorf("interrupted")
- }
- i := 0
- for ; i < importBatchSize; i++ {
- var b types.Block
- if err := stream.Decode(&b); err == io.EOF {
- break
- } else if err != nil {
- return fmt.Errorf("at block %d: %v", n, err)
- }
- // don't import first block
- if b.NumberU64() == 0 {
- i--
- continue
- }
- blocks[i] = &b
- n++
- }
- if i == 0 {
- break
- }
- // Import the batch.
- if checkInterrupt() {
- return fmt.Errorf("interrupted")
- }
- missing := missingBlocks(chain, blocks[:i])
- if len(missing) == 0 {
- log.Info("Skipping batch as all blocks present", "batch", batch, "first", blocks[0].Hash(), "last", blocks[i-1].Hash())
- continue
- }
- if _, err := chain.InsertChain(missing); err != nil {
- return fmt.Errorf("invalid block %d: %v", n, err)
- }
- }
- return nil
-}
-
-func missingBlocks(chain *core.BlockChain, blocks []*types.Block) []*types.Block {
- head := chain.CurrentBlock()
- for i, block := range blocks {
- // If we're behind the chain head, only check block, state is available at head
- if head.NumberU64() > block.NumberU64() {
- if !chain.HasBlock(block.Hash(), block.NumberU64()) {
- return blocks[i:]
- }
- continue
- }
- // If we're above the chain head, state availability is a must
- if !chain.HasBlockAndState(block.Hash(), block.NumberU64()) {
- return blocks[i:]
- }
- }
- return nil
-}
-
-// ExportChain exports a blockchain into the specified file, truncating any data
-// already present in the file.
-func ExportChain(blockchain *core.BlockChain, fn string) error {
- log.Info("Exporting blockchain", "file", fn)
-
- // Open the file handle and potentially wrap with a gzip stream
- fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
- if err != nil {
- return err
- }
- defer fh.Close()
-
- var writer io.Writer = fh
- if strings.HasSuffix(fn, ".gz") {
- writer = gzip.NewWriter(writer)
- defer writer.(*gzip.Writer).Close()
- }
- // Iterate over the blocks and export them
- if err := blockchain.Export(writer); err != nil {
- return err
- }
- log.Info("Exported blockchain", "file", fn)
-
- return nil
-}
-
-// ExportAppendChain exports a blockchain into the specified file, appending to
-// the file if data already exists in it.
-func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, last uint64) error {
- log.Info("Exporting blockchain", "file", fn)
-
- // Open the file handle and potentially wrap with a gzip stream
- fh, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
- if err != nil {
- return err
- }
- defer fh.Close()
-
- var writer io.Writer = fh
- if strings.HasSuffix(fn, ".gz") {
- writer = gzip.NewWriter(writer)
- defer writer.(*gzip.Writer).Close()
- }
- // Iterate over the blocks and export them
- if err := blockchain.ExportN(writer, first, last); err != nil {
- return err
- }
- log.Info("Exported blockchain to", "file", fn)
- return nil
-}
-
-// ImportPreimages imports a batch of exported hash preimages into the database.
-func ImportPreimages(db ethdb.Database, fn string) error {
- log.Info("Importing preimages", "file", fn)
-
- // Open the file handle and potentially unwrap the gzip stream
- fh, err := os.Open(fn)
- if err != nil {
- return err
- }
- defer fh.Close()
-
- var reader io.Reader = fh
- if strings.HasSuffix(fn, ".gz") {
- if reader, err = gzip.NewReader(reader); err != nil {
- return err
- }
- }
- stream := rlp.NewStream(reader, 0)
-
- // Import the preimages in batches to prevent disk trashing
- preimages := make(map[common.Hash][]byte)
-
- for {
- // Read the next entry and ensure it's not junk
- var blob []byte
-
- if err := stream.Decode(&blob); err != nil {
- if err == io.EOF {
- break
- }
- return err
- }
- // Accumulate the preimages and flush when enough ws gathered
- preimages[crypto.Keccak256Hash(blob)] = common.CopyBytes(blob)
- if len(preimages) > 1024 {
- rawdb.WritePreimages(db, preimages)
- preimages = make(map[common.Hash][]byte)
- }
- }
- // Flush the last batch preimage data
- if len(preimages) > 0 {
- rawdb.WritePreimages(db, preimages)
- }
- return nil
-}
-
-// ExportPreimages exports all known hash preimages into the specified file,
-// truncating any data already present in the file.
-func ExportPreimages(db ethdb.Database, fn string) error {
- log.Info("Exporting preimages", "file", fn)
-
- // Open the file handle and potentially wrap with a gzip stream
- fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
- if err != nil {
- return err
- }
- defer fh.Close()
-
- var writer io.Writer = fh
- if strings.HasSuffix(fn, ".gz") {
- writer = gzip.NewWriter(writer)
- defer writer.(*gzip.Writer).Close()
- }
- // Iterate over the preimages and export them
- it := db.NewIteratorWithPrefix([]byte("secure-key-"))
- defer it.Release()
-
- for it.Next() {
- if err := rlp.Encode(writer, it.Value()); err != nil {
- return err
- }
- }
- log.Info("Exported preimages", "file", fn)
- return nil
-}
diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go
deleted file mode 100644
index e3941e0..0000000
--- a/cmd/utils/customflags.go
+++ /dev/null
@@ -1,240 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-package utils
-
-import (
- "encoding"
- "errors"
- "flag"
- "fmt"
- "math/big"
- "os"
- "os/user"
- "path"
- "strings"
-
- "github.com/ava-labs/go-ethereum/common/math"
- "gopkg.in/urfave/cli.v1"
-)
-
-// Custom type which is registered in the flags library which cli uses for
-// argument parsing. This allows us to expand Value to an absolute path when
-// the argument is parsed
-type DirectoryString struct {
- Value string
-}
-
-func (self *DirectoryString) String() string {
- return self.Value
-}
-
-func (self *DirectoryString) Set(value string) error {
- self.Value = expandPath(value)
- return nil
-}
-
-// Custom cli.Flag type which expand the received string to an absolute path.
-// e.g. ~/.ethereum -> /home/username/.ethereum
-type DirectoryFlag struct {
- Name string
- Value DirectoryString
- Usage string
-}
-
-func (self DirectoryFlag) String() string {
- fmtString := "%s %v\t%v"
- if len(self.Value.Value) > 0 {
- fmtString = "%s \"%v\"\t%v"
- }
- return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage)
-}
-
-func eachName(longName string, fn func(string)) {
- parts := strings.Split(longName, ",")
- for _, name := range parts {
- name = strings.Trim(name, " ")
- fn(name)
- }
-}
-
-// called by cli library, grabs variable from environment (if in env)
-// and adds variable to flag set for parsing.
-func (self DirectoryFlag) Apply(set *flag.FlagSet) {
- eachName(self.Name, func(name string) {
- set.Var(&self.Value, self.Name, self.Usage)
- })
-}
-
-type TextMarshaler interface {
- encoding.TextMarshaler
- encoding.TextUnmarshaler
-}
-
-// textMarshalerVal turns a TextMarshaler into a flag.Value
-type textMarshalerVal struct {
- v TextMarshaler
-}
-
-func (v textMarshalerVal) String() string {
- if v.v == nil {
- return ""
- }
- text, _ := v.v.MarshalText()
- return string(text)
-}
-
-func (v textMarshalerVal) Set(s string) error {
- return v.v.UnmarshalText([]byte(s))
-}
-
-// TextMarshalerFlag wraps a TextMarshaler value.
-type TextMarshalerFlag struct {
- Name string
- Value TextMarshaler
- Usage string
-}
-
-func (f TextMarshalerFlag) GetName() string {
- return f.Name
-}
-
-func (f TextMarshalerFlag) String() string {
- return fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)
-}
-
-func (f TextMarshalerFlag) Apply(set *flag.FlagSet) {
- eachName(f.Name, func(name string) {
- set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage)
- })
-}
-
-// GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set.
-func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler {
- val := ctx.GlobalGeneric(name)
- if val == nil {
- return nil
- }
- return val.(textMarshalerVal).v
-}
-
-// BigFlag is a command line flag that accepts 256 bit big integers in decimal or
-// hexadecimal syntax.
-type BigFlag struct {
- Name string
- Value *big.Int
- Usage string
-}
-
-// bigValue turns *big.Int into a flag.Value
-type bigValue big.Int
-
-func (b *bigValue) String() string {
- if b == nil {
- return ""
- }
- return (*big.Int)(b).String()
-}
-
-func (b *bigValue) Set(s string) error {
- int, ok := math.ParseBig256(s)
- if !ok {
- return errors.New("invalid integer syntax")
- }
- *b = (bigValue)(*int)
- return nil
-}
-
-func (f BigFlag) GetName() string {
- return f.Name
-}
-
-func (f BigFlag) String() string {
- fmtString := "%s %v\t%v"
- if f.Value != nil {
- fmtString = "%s \"%v\"\t%v"
- }
- return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)
-}
-
-func (f BigFlag) Apply(set *flag.FlagSet) {
- eachName(f.Name, func(name string) {
- set.Var((*bigValue)(f.Value), f.Name, f.Usage)
- })
-}
-
-// GlobalBig returns the value of a BigFlag from the global flag set.
-func GlobalBig(ctx *cli.Context, name string) *big.Int {
- val := ctx.GlobalGeneric(name)
- if val == nil {
- return nil
- }
- return (*big.Int)(val.(*bigValue))
-}
-
-func prefixFor(name string) (prefix string) {
- if len(name) == 1 {
- prefix = "-"
- } else {
- prefix = "--"
- }
-
- return
-}
-
-func prefixedNames(fullName string) (prefixed string) {
- parts := strings.Split(fullName, ",")
- for i, name := range parts {
- name = strings.Trim(name, " ")
- prefixed += prefixFor(name) + name
- if i < len(parts)-1 {
- prefixed += ", "
- }
- }
- return
-}
-
-func (self DirectoryFlag) GetName() string {
- return self.Name
-}
-
-func (self *DirectoryFlag) Set(value string) {
- self.Value.Value = value
-}
-
-// Expands a file path
-// 1. replace tilde with users home dir
-// 2. expands embedded environment variables
-// 3. cleans the path, e.g. /a/b/../c -> /a/c
-// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
-func expandPath(p string) string {
- if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
- if home := homeDir(); home != "" {
- p = home + p[1:]
- }
- }
- return path.Clean(os.ExpandEnv(p))
-}
-
-func homeDir() string {
- if home := os.Getenv("HOME"); home != "" {
- return home
- }
- if usr, err := user.Current(); err == nil {
- return usr.HomeDir
- }
- return ""
-}
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
deleted file mode 100644
index ea3ca1d..0000000
--- a/cmd/utils/flags.go
+++ /dev/null
@@ -1,1700 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of go-ethereum.
-//
-// go-ethereum is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// go-ethereum is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-
-// Package utils contains internal helper functions for go-ethereum commands.
-package utils
-
-import (
- "crypto/ecdsa"
- "errors"
- "fmt"
- "io/ioutil"
- "math/big"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
-
- "github.com/ava-labs/coreth/consensus/dummy"
- "github.com/ava-labs/coreth/eth"
- "github.com/ava-labs/coreth/ethstats"
- "github.com/ava-labs/coreth/miner"
- "github.com/ava-labs/coreth/node"
- "github.com/ava-labs/go-ethereum/accounts"
- "github.com/ava-labs/go-ethereum/accounts/keystore"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/common/fdlimit"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/consensus/clique"
- "github.com/ava-labs/go-ethereum/consensus/ethash"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/vm"
- "github.com/ava-labs/go-ethereum/crypto"
- "github.com/ava-labs/go-ethereum/dashboard"
- "github.com/ava-labs/go-ethereum/eth/downloader"
- "github.com/ava-labs/go-ethereum/eth/gasprice"
- "github.com/ava-labs/go-ethereum/ethdb"
- "github.com/ava-labs/go-ethereum/graphql"
- "github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/metrics"
- "github.com/ava-labs/go-ethereum/metrics/influxdb"
- "github.com/ava-labs/go-ethereum/p2p"
- "github.com/ava-labs/go-ethereum/p2p/discv5"
- "github.com/ava-labs/go-ethereum/p2p/enode"
- "github.com/ava-labs/go-ethereum/p2p/nat"
- "github.com/ava-labs/go-ethereum/p2p/netutil"
- "github.com/ava-labs/go-ethereum/params"
- "github.com/ava-labs/go-ethereum/rpc"
- whisper "github.com/ava-labs/go-ethereum/whisper/whisperv6"
- pcsclite "github.com/gballet/go-libpcsclite"
- cli "gopkg.in/urfave/cli.v1"
-)
-
-var (
- CommandHelpTemplate = `{{.cmd.Name}}{{if .cmd.Subcommands}} command{{end}}{{if .cmd.Flags}} [command options]{{end}} [arguments...]
-{{if .cmd.Description}}{{.cmd.Description}}
-{{end}}{{if .cmd.Subcommands}}
-SUBCOMMANDS:
- {{range .cmd.Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
- {{end}}{{end}}{{if .categorizedFlags}}
-{{range $idx, $categorized := .categorizedFlags}}{{$categorized.Name}} OPTIONS:
-{{range $categorized.Flags}}{{"\t"}}{{.}}
-{{end}}
-{{end}}{{end}}`
-)
-
-func init() {
- cli.AppHelpTemplate = `{{.Name}} {{if .Flags}}[global options] {{end}}command{{if .Flags}} [command options]{{end}} [arguments...]
-
-VERSION:
- {{.Version}}
-
-COMMANDS:
- {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
- {{end}}{{if .Flags}}
-GLOBAL OPTIONS:
- {{range .Flags}}{{.}}
- {{end}}{{end}}
-`
-
- cli.CommandHelpTemplate = CommandHelpTemplate
-}
-
-// NewApp creates an app with sane defaults.
-func NewApp(gitCommit, gitDate, usage string) *cli.App {
- app := cli.NewApp()
- app.Name = filepath.Base(os.Args[0])
- app.Author = ""
- app.Email = ""
- app.Version = params.VersionWithCommit(gitCommit, gitDate)
- app.Usage = usage
- return app
-}
-
-// These are all the command line flags we support.
-// If you add to this list, please remember to include the
-// flag in the appropriate command definition.
-//
-// The flags are defined here so their names and help texts
-// are the same for all commands.
-
-var (
- // General settings
- DataDirFlag = DirectoryFlag{
- Name: "datadir",
- Usage: "Data directory for the databases and keystore",
- Value: DirectoryString{node.DefaultDataDir()},
- }
- AncientFlag = DirectoryFlag{
- Name: "datadir.ancient",
- Usage: "Data directory for ancient chain segments (default = inside chaindata)",
- }
- KeyStoreDirFlag = DirectoryFlag{
- Name: "keystore",
- Usage: "Directory for the keystore (default = inside the datadir)",
- }
- NoUSBFlag = cli.BoolFlag{
- Name: "nousb",
- Usage: "Disables monitoring for and managing USB hardware wallets",
- }
- SmartCardDaemonPathFlag = cli.StringFlag{
- Name: "pcscdpath",
- Usage: "Path to the smartcard daemon (pcscd) socket file",
- Value: pcsclite.PCSCDSockName,
- }
- NetworkIdFlag = cli.Uint64Flag{
- Name: "networkid",
- Usage: "Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby)",
- Value: eth.DefaultConfig.NetworkId,
- }
- TestnetFlag = cli.BoolFlag{
- Name: "testnet",
- Usage: "Ropsten network: pre-configured proof-of-work test network",
- }
- RinkebyFlag = cli.BoolFlag{
- Name: "rinkeby",
- Usage: "Rinkeby network: pre-configured proof-of-authority test network",
- }
- GoerliFlag = cli.BoolFlag{
- Name: "goerli",
- Usage: "Görli network: pre-configured proof-of-authority test network",
- }
- DeveloperFlag = cli.BoolFlag{
- Name: "dev",
- Usage: "Ephemeral proof-of-authority network with a pre-funded developer account, mining enabled",
- }
- DeveloperPeriodFlag = cli.IntFlag{
- Name: "dev.period",
- Usage: "Block period to use in developer mode (0 = mine only if transaction pending)",
- }
- IdentityFlag = cli.StringFlag{
- Name: "identity",
- Usage: "Custom node name",
- }
- DocRootFlag = DirectoryFlag{
- Name: "docroot",
- Usage: "Document Root for HTTPClient file scheme",
- Value: DirectoryString{homeDir()},
- }
- ExitWhenSyncedFlag = cli.BoolFlag{
- Name: "exitwhensynced",
- Usage: "Exits after block synchronisation completes",
- }
- IterativeOutputFlag = cli.BoolFlag{
- Name: "iterative",
- Usage: "Print streaming JSON iteratively, delimited by newlines",
- }
- ExcludeStorageFlag = cli.BoolFlag{
- Name: "nostorage",
- Usage: "Exclude storage entries (save db lookups)",
- }
- IncludeIncompletesFlag = cli.BoolFlag{
- Name: "incompletes",
- Usage: "Include accounts for which we don't have the address (missing preimage)",
- }
- ExcludeCodeFlag = cli.BoolFlag{
- Name: "nocode",
- Usage: "Exclude contract code (save db lookups)",
- }
- defaultSyncMode = eth.DefaultConfig.SyncMode
- SyncModeFlag = TextMarshalerFlag{
- Name: "syncmode",
- Usage: `Blockchain sync mode ("fast", "full", or "light")`,
- Value: &defaultSyncMode,
- }
- GCModeFlag = cli.StringFlag{
- Name: "gcmode",
- Usage: `Blockchain garbage collection mode ("full", "archive")`,
- Value: "full",
- }
- LightKDFFlag = cli.BoolFlag{
- Name: "lightkdf",
- Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength",
- }
- WhitelistFlag = cli.StringFlag{
- Name: "whitelist",
- Usage: "Comma separated block number-to-hash mappings to enforce (<number>=<hash>)",
- }
- // Light server and client settings
- LightLegacyServFlag = cli.IntFlag{ // Deprecated in favor of light.serve, remove in 2021
- Name: "lightserv",
- Usage: "Maximum percentage of time allowed for serving LES requests (deprecated, use --light.serve)",
- Value: eth.DefaultConfig.LightServ,
- }
- LightServeFlag = cli.IntFlag{
- Name: "light.serve",
- Usage: "Maximum percentage of time allowed for serving LES requests (multi-threaded processing allows values over 100)",
- Value: eth.DefaultConfig.LightServ,
- }
- LightIngressFlag = cli.IntFlag{
- Name: "light.ingress",
- Usage: "Incoming bandwidth limit for serving light clients (kilobytes/sec, 0 = unlimited)",
- Value: eth.DefaultConfig.LightIngress,
- }
- LightEgressFlag = cli.IntFlag{
- Name: "light.egress",
- Usage: "Outgoing bandwidth limit for serving light clients (kilobytes/sec, 0 = unlimited)",
- Value: eth.DefaultConfig.LightEgress,
- }
- LightLegacyPeersFlag = cli.IntFlag{ // Deprecated in favor of light.maxpeers, remove in 2021
- Name: "lightpeers",
- Usage: "Maximum number of light clients to serve, or light servers to attach to (deprecated, use --light.maxpeers)",
- Value: eth.DefaultConfig.LightPeers,
- }
- LightMaxPeersFlag = cli.IntFlag{
- Name: "light.maxpeers",
- Usage: "Maximum number of light clients to serve, or light servers to attach to",
- Value: eth.DefaultConfig.LightPeers,
- }
- UltraLightServersFlag = cli.StringFlag{
- Name: "ulc.servers",
- Usage: "List of trusted ultra-light servers",
- Value: strings.Join(eth.DefaultConfig.UltraLightServers, ","),
- }
- UltraLightFractionFlag = cli.IntFlag{
- Name: "ulc.fraction",
- Usage: "Minimum % of trusted ultra-light servers required to announce a new head",
- Value: eth.DefaultConfig.UltraLightFraction,
- }
- UltraLightOnlyAnnounceFlag = cli.BoolFlag{
- Name: "ulc.onlyannounce",
- Usage: "Ultra light server sends announcements only",
- }
- // Dashboard settings
- DashboardEnabledFlag = cli.BoolFlag{
- Name: "dashboard",
- Usage: "Enable the dashboard",
- }
- DashboardAddrFlag = cli.StringFlag{
- Name: "dashboard.addr",
- Usage: "Dashboard listening interface",
- Value: dashboard.DefaultConfig.Host,
- }
- DashboardPortFlag = cli.IntFlag{
- Name: "dashboard.host",
- Usage: "Dashboard listening port",
- Value: dashboard.DefaultConfig.Port,
- }
- DashboardRefreshFlag = cli.DurationFlag{
- Name: "dashboard.refresh",
- Usage: "Dashboard metrics collection refresh rate",
- Value: dashboard.DefaultConfig.Refresh,
- }
- // Ethash settings
- EthashCacheDirFlag = DirectoryFlag{
- Name: "ethash.cachedir",
- Usage: "Directory to store the ethash verification caches (default = inside the datadir)",
- }
- EthashCachesInMemoryFlag = cli.IntFlag{
- Name: "ethash.cachesinmem",
- Usage: "Number of recent ethash caches to keep in memory (16MB each)",
- Value: eth.DefaultConfig.Ethash.CachesInMem,
- }
- EthashCachesOnDiskFlag = cli.IntFlag{
- Name: "ethash.cachesondisk",
- Usage: "Number of recent ethash caches to keep on disk (16MB each)",
- Value: eth.DefaultConfig.Ethash.CachesOnDisk,
- }
- EthashDatasetDirFlag = DirectoryFlag{
- Name: "ethash.dagdir",
- Usage: "Directory to store the ethash mining DAGs (default = inside home folder)",
- Value: DirectoryString{eth.DefaultConfig.Ethash.DatasetDir},
- }
- EthashDatasetsInMemoryFlag = cli.IntFlag{
- Name: "ethash.dagsinmem",
- Usage: "Number of recent ethash mining DAGs to keep in memory (1+GB each)",
- Value: eth.DefaultConfig.Ethash.DatasetsInMem,
- }
- EthashDatasetsOnDiskFlag = cli.IntFlag{
- Name: "ethash.dagsondisk",
- Usage: "Number of recent ethash mining DAGs to keep on disk (1+GB each)",
- Value: eth.DefaultConfig.Ethash.DatasetsOnDisk,
- }
- // Transaction pool settings
- TxPoolLocalsFlag = cli.StringFlag{
- Name: "txpool.locals",
- Usage: "Comma separated accounts to treat as locals (no flush, priority inclusion)",
- }
- TxPoolNoLocalsFlag = cli.BoolFlag{
- Name: "txpool.nolocals",
- Usage: "Disables price exemptions for locally submitted transactions",
- }
- TxPoolJournalFlag = cli.StringFlag{
- Name: "txpool.journal",
- Usage: "Disk journal for local transaction to survive node restarts",
- Value: core.DefaultTxPoolConfig.Journal,
- }
- TxPoolRejournalFlag = cli.DurationFlag{
- Name: "txpool.rejournal",
- Usage: "Time interval to regenerate the local transaction journal",
- Value: core.DefaultTxPoolConfig.Rejournal,
- }
- TxPoolPriceLimitFlag = cli.Uint64Flag{
- Name: "txpool.pricelimit",
- Usage: "Minimum gas price limit to enforce for acceptance into the pool",
- Value: eth.DefaultConfig.TxPool.PriceLimit,
- }
- TxPoolPriceBumpFlag = cli.Uint64Flag{
- Name: "txpool.pricebump",
- Usage: "Price bump percentage to replace an already existing transaction",
- Value: eth.DefaultConfig.TxPool.PriceBump,
- }
- TxPoolAccountSlotsFlag = cli.Uint64Flag{
- Name: "txpool.accountslots",
- Usage: "Minimum number of executable transaction slots guaranteed per account",
- Value: eth.DefaultConfig.TxPool.AccountSlots,
- }
- TxPoolGlobalSlotsFlag = cli.Uint64Flag{
- Name: "txpool.globalslots",
- Usage: "Maximum number of executable transaction slots for all accounts",
- Value: eth.DefaultConfig.TxPool.GlobalSlots,
- }
- TxPoolAccountQueueFlag = cli.Uint64Flag{
- Name: "txpool.accountqueue",
- Usage: "Maximum number of non-executable transaction slots permitted per account",
- Value: eth.DefaultConfig.TxPool.AccountQueue,
- }
- TxPoolGlobalQueueFlag = cli.Uint64Flag{
- Name: "txpool.globalqueue",
- Usage: "Maximum number of non-executable transaction slots for all accounts",
- Value: eth.DefaultConfig.TxPool.GlobalQueue,
- }
- TxPoolLifetimeFlag = cli.DurationFlag{
- Name: "txpool.lifetime",
- Usage: "Maximum amount of time non-executable transaction are queued",
- Value: eth.DefaultConfig.TxPool.Lifetime,
- }
- // Performance tuning settings
- CacheFlag = cli.IntFlag{
- Name: "cache",
- Usage: "Megabytes of memory allocated to internal caching (default = 4096 mainnet full node, 128 light mode)",
- Value: 1024,
- }
- CacheDatabaseFlag = cli.IntFlag{
- Name: "cache.database",
- Usage: "Percentage of cache memory allowance to use for database io",
- Value: 50,
- }
- CacheTrieFlag = cli.IntFlag{
- Name: "cache.trie",
- Usage: "Percentage of cache memory allowance to use for trie caching (default = 25% full mode, 50% archive mode)",
- Value: 25,
- }
- CacheGCFlag = cli.IntFlag{
- Name: "cache.gc",
- Usage: "Percentage of cache memory allowance to use for trie pruning (default = 25% full mode, 0% archive mode)",
- Value: 25,
- }
- CacheNoPrefetchFlag = cli.BoolFlag{
- Name: "cache.noprefetch",
- Usage: "Disable heuristic state prefetch during block import (less CPU and disk IO, more time waiting for data)",
- }
- // Miner settings
- MiningEnabledFlag = cli.BoolFlag{
- Name: "mine",
- Usage: "Enable mining",
- }
- MinerThreadsFlag = cli.IntFlag{
- Name: "miner.threads",
- Usage: "Number of CPU threads to use for mining",
- Value: 0,
- }
- MinerLegacyThreadsFlag = cli.IntFlag{
- Name: "minerthreads",
- Usage: "Number of CPU threads to use for mining (deprecated, use --miner.threads)",
- Value: 0,
- }
- MinerNotifyFlag = cli.StringFlag{
- Name: "miner.notify",
- Usage: "Comma separated HTTP URL list to notify of new work packages",
- }
- MinerGasTargetFlag = cli.Uint64Flag{
- Name: "miner.gastarget",
- Usage: "Target gas floor for mined blocks",
- Value: eth.DefaultConfig.Miner.GasFloor,
- }
- MinerLegacyGasTargetFlag = cli.Uint64Flag{
- Name: "targetgaslimit",
- Usage: "Target gas floor for mined blocks (deprecated, use --miner.gastarget)",
- Value: eth.DefaultConfig.Miner.GasFloor,
- }
- MinerGasLimitFlag = cli.Uint64Flag{
- Name: "miner.gaslimit",
- Usage: "Target gas ceiling for mined blocks",
- Value: eth.DefaultConfig.Miner.GasCeil,
- }
- MinerGasPriceFlag = BigFlag{
- Name: "miner.gasprice",
- Usage: "Minimum gas price for mining a transaction",
- Value: eth.DefaultConfig.Miner.GasPrice,
- }
- MinerLegacyGasPriceFlag = BigFlag{
- Name: "gasprice",
- Usage: "Minimum gas price for mining a transaction (deprecated, use --miner.gasprice)",
- Value: eth.DefaultConfig.Miner.GasPrice,
- }
- MinerEtherbaseFlag = cli.StringFlag{
- Name: "miner.etherbase",
- Usage: "Public address for block mining rewards (default = first account)",
- Value: "0",
- }
- MinerLegacyEtherbaseFlag = cli.StringFlag{
- Name: "etherbase",
- Usage: "Public address for block mining rewards (default = first account, deprecated, use --miner.etherbase)",
- Value: "0",
- }
- MinerExtraDataFlag = cli.StringFlag{
- Name: "miner.extradata",
- Usage: "Block extra data set by the miner (default = client version)",
- }
- MinerLegacyExtraDataFlag = cli.StringFlag{
- Name: "extradata",
- Usage: "Block extra data set by the miner (default = client version, deprecated, use --miner.extradata)",
- }
- MinerRecommitIntervalFlag = cli.DurationFlag{
- Name: "miner.recommit",
- Usage: "Time interval to recreate the block being mined",
- Value: eth.DefaultConfig.Miner.Recommit,
- }
- MinerNoVerfiyFlag = cli.BoolFlag{
- Name: "miner.noverify",
- Usage: "Disable remote sealing verification",
- }
- // Account settings
- UnlockedAccountFlag = cli.StringFlag{
- Name: "unlock",
- Usage: "Comma separated list of accounts to unlock",
- Value: "",
- }
- PasswordFileFlag = cli.StringFlag{
- Name: "password",
- Usage: "Password file to use for non-interactive password input",
- Value: "",
- }
- ExternalSignerFlag = cli.StringFlag{
- Name: "signer",
- Usage: "External signer (url or path to ipc file)",
- Value: "",
- }
- VMEnableDebugFlag = cli.BoolFlag{
- Name: "vmdebug",
- Usage: "Record information useful for VM and contract debugging",
- }
- InsecureUnlockAllowedFlag = cli.BoolFlag{
- Name: "allow-insecure-unlock",
- Usage: "Allow insecure account unlocking when account-related RPCs are exposed by http",
- }
- RPCGlobalGasCap = cli.Uint64Flag{
- Name: "rpc.gascap",
- Usage: "Sets a cap on gas that can be used in eth_call/estimateGas",
- }
- // Logging and debug settings
- EthStatsURLFlag = cli.StringFlag{
- Name: "ethstats",
- Usage: "Reporting URL of a ethstats service (nodename:secret@host:port)",
- }
- FakePoWFlag = cli.BoolFlag{
- Name: "fakepow",
- Usage: "Disables proof-of-work verification",
- }
- NoCompactionFlag = cli.BoolFlag{
- Name: "nocompaction",
- Usage: "Disables db compaction after import",
- }
- // RPC settings
- IPCDisabledFlag = cli.BoolFlag{
- Name: "ipcdisable",
- Usage: "Disable the IPC-RPC server",
- }
- IPCPathFlag = DirectoryFlag{
- Name: "ipcpath",
- Usage: "Filename for IPC socket/pipe within the datadir (explicit paths escape it)",
- }
- RPCEnabledFlag = cli.BoolFlag{
- Name: "rpc",
- Usage: "Enable the HTTP-RPC server",
- }
- RPCListenAddrFlag = cli.StringFlag{
- Name: "rpcaddr",
- Usage: "HTTP-RPC server listening interface",
- Value: node.DefaultHTTPHost,
- }
- RPCPortFlag = cli.IntFlag{
- Name: "rpcport",
- Usage: "HTTP-RPC server listening port",
- Value: node.DefaultHTTPPort,
- }
- RPCCORSDomainFlag = cli.StringFlag{
- Name: "rpccorsdomain",
- Usage: "Comma separated list of domains from which to accept cross origin requests (browser enforced)",
- Value: "",
- }
- RPCVirtualHostsFlag = cli.StringFlag{
- Name: "rpcvhosts",
- Usage: "Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard.",
- Value: strings.Join(node.DefaultConfig.HTTPVirtualHosts, ","),
- }
- RPCApiFlag = cli.StringFlag{
- Name: "rpcapi",
- Usage: "API's offered over the HTTP-RPC interface",
- Value: "",
- }
- WSEnabledFlag = cli.BoolFlag{
- Name: "ws",
- Usage: "Enable the WS-RPC server",
- }
- WSListenAddrFlag = cli.StringFlag{
- Name: "wsaddr",
- Usage: "WS-RPC server listening interface",
- Value: node.DefaultWSHost,
- }
- WSPortFlag = cli.IntFlag{
- Name: "wsport",
- Usage: "WS-RPC server listening port",
- Value: node.DefaultWSPort,
- }
- WSApiFlag = cli.StringFlag{
- Name: "wsapi",
- Usage: "API's offered over the WS-RPC interface",
- Value: "",
- }
- WSAllowedOriginsFlag = cli.StringFlag{
- Name: "wsorigins",
- Usage: "Origins from which to accept websockets requests",
- Value: "",
- }
- GraphQLEnabledFlag = cli.BoolFlag{
- Name: "graphql",
- Usage: "Enable the GraphQL server",
- }
- GraphQLListenAddrFlag = cli.StringFlag{
- Name: "graphql.addr",
- Usage: "GraphQL server listening interface",
- Value: node.DefaultGraphQLHost,
- }
- GraphQLPortFlag = cli.IntFlag{
- Name: "graphql.port",
- Usage: "GraphQL server listening port",
- Value: node.DefaultGraphQLPort,
- }
- GraphQLCORSDomainFlag = cli.StringFlag{
- Name: "graphql.corsdomain",
- Usage: "Comma separated list of domains from which to accept cross origin requests (browser enforced)",
- Value: "",
- }
- GraphQLVirtualHostsFlag = cli.StringFlag{
- Name: "graphql.vhosts",
- Usage: "Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard.",
- Value: strings.Join(node.DefaultConfig.GraphQLVirtualHosts, ","),
- }
- ExecFlag = cli.StringFlag{
- Name: "exec",
- Usage: "Execute JavaScript statement",
- }
- PreloadJSFlag = cli.StringFlag{
- Name: "preload",
- Usage: "Comma separated list of JavaScript files to preload into the console",
- }
-
- // Network Settings
- MaxPeersFlag = cli.IntFlag{
- Name: "maxpeers",
- Usage: "Maximum number of network peers (network disabled if set to 0)",
- Value: node.DefaultConfig.P2P.MaxPeers,
- }
- MaxPendingPeersFlag = cli.IntFlag{
- Name: "maxpendpeers",
- Usage: "Maximum number of pending connection attempts (defaults used if set to 0)",
- Value: node.DefaultConfig.P2P.MaxPendingPeers,
- }
- ListenPortFlag = cli.IntFlag{
- Name: "port",
- Usage: "Network listening port",
- Value: 30303,
- }
- BootnodesFlag = cli.StringFlag{
- Name: "bootnodes",
- Usage: "Comma separated enode URLs for P2P discovery bootstrap (set v4+v5 instead for light servers)",
- Value: "",
- }
- BootnodesV4Flag = cli.StringFlag{
- Name: "bootnodesv4",
- Usage: "Comma separated enode URLs for P2P v4 discovery bootstrap (light server, full nodes)",
- Value: "",
- }
- BootnodesV5Flag = cli.StringFlag{
- Name: "bootnodesv5",
- Usage: "Comma separated enode URLs for P2P v5 discovery bootstrap (light server, light nodes)",
- Value: "",
- }
- NodeKeyFileFlag = cli.StringFlag{
- Name: "nodekey",
- Usage: "P2P node key file",
- }
- NodeKeyHexFlag = cli.StringFlag{
- Name: "nodekeyhex",
- Usage: "P2P node key as hex (for testing)",
- }
- NATFlag = cli.StringFlag{
- Name: "nat",
- Usage: "NAT port mapping mechanism (any|none|upnp|pmp|extip:<IP>)",
- Value: "any",
- }
- NoDiscoverFlag = cli.BoolFlag{
- Name: "nodiscover",
- Usage: "Disables the peer discovery mechanism (manual peer addition)",
- }
- DiscoveryV5Flag = cli.BoolFlag{
- Name: "v5disc",
- Usage: "Enables the experimental RLPx V5 (Topic Discovery) mechanism",
- }
- NetrestrictFlag = cli.StringFlag{
- Name: "netrestrict",
- Usage: "Restricts network communication to the given IP networks (CIDR masks)",
- }
-
- // ATM the url is left to the user and deployment to
- JSpathFlag = cli.StringFlag{
- Name: "jspath",
- Usage: "JavaScript root path for `loadScript`",
- Value: ".",
- }
-
- // Gas price oracle settings
- GpoBlocksFlag = cli.IntFlag{
- Name: "gpoblocks",
- Usage: "Number of recent blocks to check for gas prices",
- Value: eth.DefaultConfig.GPO.Blocks,
- }
- GpoPercentileFlag = cli.IntFlag{
- Name: "gpopercentile",
- Usage: "Suggested gas price is the given percentile of a set of recent transaction gas prices",
- Value: eth.DefaultConfig.GPO.Percentile,
- }
- WhisperEnabledFlag = cli.BoolFlag{
- Name: "shh",
- Usage: "Enable Whisper",
- }
- WhisperMaxMessageSizeFlag = cli.IntFlag{
- Name: "shh.maxmessagesize",
- Usage: "Max message size accepted",
- Value: int(whisper.DefaultMaxMessageSize),
- }
- WhisperMinPOWFlag = cli.Float64Flag{
- Name: "shh.pow",
- Usage: "Minimum POW accepted",
- Value: whisper.DefaultMinimumPoW,
- }
- WhisperRestrictConnectionBetweenLightClientsFlag = cli.BoolFlag{
- Name: "shh.restrict-light",
- Usage: "Restrict connection between two whisper light clients",
- }
-
- // Metrics flags
- MetricsEnabledFlag = cli.BoolFlag{
- Name: "metrics",
- Usage: "Enable metrics collection and reporting",
- }
- MetricsEnabledExpensiveFlag = cli.BoolFlag{
- Name: "metrics.expensive",
- Usage: "Enable expensive metrics collection and reporting",
- }
- MetricsEnableInfluxDBFlag = cli.BoolFlag{
- Name: "metrics.influxdb",
- Usage: "Enable metrics export/push to an external InfluxDB database",
- }
- MetricsInfluxDBEndpointFlag = cli.StringFlag{
- Name: "metrics.influxdb.endpoint",
- Usage: "InfluxDB API endpoint to report metrics to",
- Value: "http://localhost:8086",
- }
- MetricsInfluxDBDatabaseFlag = cli.StringFlag{
- Name: "metrics.influxdb.database",
- Usage: "InfluxDB database name to push reported metrics to",
- Value: "geth",
- }
- MetricsInfluxDBUsernameFlag = cli.StringFlag{
- Name: "metrics.influxdb.username",
- Usage: "Username to authorize access to the database",
- Value: "test",
- }
- MetricsInfluxDBPasswordFlag = cli.StringFlag{
- Name: "metrics.influxdb.password",
- Usage: "Password to authorize access to the database",
- Value: "test",
- }
- // Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
- // For example `host` tag could be used so that we can group all nodes and average a measurement
- // across all of them, but also so that we can select a specific node and inspect its measurements.
- // https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
- MetricsInfluxDBTagsFlag = cli.StringFlag{
- Name: "metrics.influxdb.tags",
- Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
- Value: "host=localhost",
- }
-
- EWASMInterpreterFlag = cli.StringFlag{
- Name: "vm.ewasm",
- Usage: "External ewasm configuration (default = built-in interpreter)",
- Value: "",
- }
- EVMInterpreterFlag = cli.StringFlag{
- Name: "vm.evm",
- Usage: "External EVM configuration (default = built-in interpreter)",
- Value: "",
- }
-)
-
-// MakeDataDir retrieves the currently requested data directory, terminating
-// if none (or the empty string) is specified. If the node is starting a testnet,
-// the a subdirectory of the specified datadir will be used.
-func MakeDataDir(ctx *cli.Context) string {
- if path := ctx.GlobalString(DataDirFlag.Name); path != "" {
- if ctx.GlobalBool(TestnetFlag.Name) {
- return filepath.Join(path, "testnet")
- }
- if ctx.GlobalBool(RinkebyFlag.Name) {
- return filepath.Join(path, "rinkeby")
- }
- if ctx.GlobalBool(GoerliFlag.Name) {
- return filepath.Join(path, "goerli")
- }
- return path
- }
- Fatalf("Cannot determine default data directory, please set manually (--datadir)")
- return ""
-}
-
-// setNodeKey creates a node key from set command line flags, either loading it
-// from a file or as a specified hex value. If neither flags were provided, this
-// method returns nil and an emphemeral key is to be generated.
-func setNodeKey(ctx *cli.Context, cfg *p2p.Config) {
- var (
- hex = ctx.GlobalString(NodeKeyHexFlag.Name)
- file = ctx.GlobalString(NodeKeyFileFlag.Name)
- key *ecdsa.PrivateKey
- err error
- )
- switch {
- case file != "" && hex != "":
- Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name)
- case file != "":
- if key, err = crypto.LoadECDSA(file); err != nil {
- Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err)
- }
- cfg.PrivateKey = key
- case hex != "":
- if key, err = crypto.HexToECDSA(hex); err != nil {
- Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err)
- }
- cfg.PrivateKey = key
- }
-}
-
-// setNodeUserIdent creates the user identifier from CLI flags.
-func setNodeUserIdent(ctx *cli.Context, cfg *node.Config) {
- if identity := ctx.GlobalString(IdentityFlag.Name); len(identity) > 0 {
- cfg.UserIdent = identity
- }
-}
-
-// setBootstrapNodes creates a list of bootstrap nodes from the command line
-// flags, reverting to pre-configured ones if none have been specified.
-func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
- urls := params.MainnetBootnodes
- switch {
- case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(BootnodesV4Flag.Name):
- if ctx.GlobalIsSet(BootnodesV4Flag.Name) {
- urls = strings.Split(ctx.GlobalString(BootnodesV4Flag.Name), ",")
- } else {
- urls = strings.Split(ctx.GlobalString(BootnodesFlag.Name), ",")
- }
- case ctx.GlobalBool(TestnetFlag.Name):
- urls = params.TestnetBootnodes
- case ctx.GlobalBool(RinkebyFlag.Name):
- urls = params.RinkebyBootnodes
- case ctx.GlobalBool(GoerliFlag.Name):
- urls = params.GoerliBootnodes
- case cfg.BootstrapNodes != nil:
- return // already set, don't apply defaults.
- }
-
- cfg.BootstrapNodes = make([]*enode.Node, 0, len(urls))
- for _, url := range urls {
- if url != "" {
- node, err := enode.Parse(enode.ValidSchemes, url)
- if err != nil {
- log.Crit("Bootstrap URL invalid", "enode", url, "err", err)
- continue
- }
- cfg.BootstrapNodes = append(cfg.BootstrapNodes, node)
- }
- }
-}
-
-// setBootstrapNodesV5 creates a list of bootstrap nodes from the command line
-// flags, reverting to pre-configured ones if none have been specified.
-func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
- urls := params.DiscoveryV5Bootnodes
- switch {
- case ctx.GlobalIsSet(BootnodesFlag.Name) || ctx.GlobalIsSet(BootnodesV5Flag.Name):
- if ctx.GlobalIsSet(BootnodesV5Flag.Name) {
- urls = strings.Split(ctx.GlobalString(BootnodesV5Flag.Name), ",")
- } else {
- urls = strings.Split(ctx.GlobalString(BootnodesFlag.Name), ",")
- }
- case ctx.GlobalBool(RinkebyFlag.Name):
- urls = params.RinkebyBootnodes
- case ctx.GlobalBool(GoerliFlag.Name):
- urls = params.GoerliBootnodes
- case cfg.BootstrapNodesV5 != nil:
- return // already set, don't apply defaults.
- }
-
- cfg.BootstrapNodesV5 = make([]*discv5.Node, 0, len(urls))
- for _, url := range urls {
- if url != "" {
- node, err := discv5.ParseNode(url)
- if err != nil {
- log.Error("Bootstrap URL invalid", "enode", url, "err", err)
- continue
- }
- cfg.BootstrapNodesV5 = append(cfg.BootstrapNodesV5, node)
- }
- }
-}
-
-// setListenAddress creates a TCP listening address string from set command
-// line flags.
-func setListenAddress(ctx *cli.Context, cfg *p2p.Config) {
- if ctx.GlobalIsSet(ListenPortFlag.Name) {
- cfg.ListenAddr = fmt.Sprintf(":%d", ctx.GlobalInt(ListenPortFlag.Name))
- }
-}
-
-// setNAT creates a port mapper from command line flags.
-func setNAT(ctx *cli.Context, cfg *p2p.Config) {
- if ctx.GlobalIsSet(NATFlag.Name) {
- natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
- if err != nil {
- Fatalf("Option %s: %v", NATFlag.Name, err)
- }
- cfg.NAT = natif
- }
-}
-
-// splitAndTrim splits input separated by a comma
-// and trims excessive white space from the substrings.
-func splitAndTrim(input string) []string {
- result := strings.Split(input, ",")
- for i, r := range result {
- result[i] = strings.TrimSpace(r)
- }
- return result
-}
-
-// setHTTP creates the HTTP RPC listener interface string from the set
-// command line flags, returning empty if the HTTP endpoint is disabled.
-func setHTTP(ctx *cli.Context, cfg *node.Config) {
- if ctx.GlobalBool(RPCEnabledFlag.Name) && cfg.HTTPHost == "" {
- cfg.HTTPHost = "127.0.0.1"
- if ctx.GlobalIsSet(RPCListenAddrFlag.Name) {
- cfg.HTTPHost = ctx.GlobalString(RPCListenAddrFlag.Name)
- }
- }
- if ctx.GlobalIsSet(RPCPortFlag.Name) {
- cfg.HTTPPort = ctx.GlobalInt(RPCPortFlag.Name)
- }
- if ctx.GlobalIsSet(RPCCORSDomainFlag.Name) {
- cfg.HTTPCors = splitAndTrim(ctx.GlobalString(RPCCORSDomainFlag.Name))
- }
- if ctx.GlobalIsSet(RPCApiFlag.Name) {
- cfg.HTTPModules = splitAndTrim(ctx.GlobalString(RPCApiFlag.Name))
- }
- if ctx.GlobalIsSet(RPCVirtualHostsFlag.Name) {
- cfg.HTTPVirtualHosts = splitAndTrim(ctx.GlobalString(RPCVirtualHostsFlag.Name))
- }
-}
-
-// setGraphQL creates the GraphQL listener interface string from the set
-// command line flags, returning empty if the GraphQL endpoint is disabled.
-func setGraphQL(ctx *cli.Context, cfg *node.Config) {
- if ctx.GlobalBool(GraphQLEnabledFlag.Name) && cfg.GraphQLHost == "" {
- cfg.GraphQLHost = "127.0.0.1"
- if ctx.GlobalIsSet(GraphQLListenAddrFlag.Name) {
- cfg.GraphQLHost = ctx.GlobalString(GraphQLListenAddrFlag.Name)
- }
- }
- cfg.GraphQLPort = ctx.GlobalInt(GraphQLPortFlag.Name)
- if ctx.GlobalIsSet(GraphQLCORSDomainFlag.Name) {
- cfg.GraphQLCors = splitAndTrim(ctx.GlobalString(GraphQLCORSDomainFlag.Name))
- }
- if ctx.GlobalIsSet(GraphQLVirtualHostsFlag.Name) {
- cfg.GraphQLVirtualHosts = splitAndTrim(ctx.GlobalString(GraphQLVirtualHostsFlag.Name))
- }
-}
-
-// setWS creates the WebSocket RPC listener interface string from the set
-// command line flags, returning empty if the HTTP endpoint is disabled.
-func setWS(ctx *cli.Context, cfg *node.Config) {
- if ctx.GlobalBool(WSEnabledFlag.Name) && cfg.WSHost == "" {
- cfg.WSHost = "127.0.0.1"
- if ctx.GlobalIsSet(WSListenAddrFlag.Name) {
- cfg.WSHost = ctx.GlobalString(WSListenAddrFlag.Name)
- }
- }
- if ctx.GlobalIsSet(WSPortFlag.Name) {
- cfg.WSPort = ctx.GlobalInt(WSPortFlag.Name)
- }
- if ctx.GlobalIsSet(WSAllowedOriginsFlag.Name) {
- cfg.WSOrigins = splitAndTrim(ctx.GlobalString(WSAllowedOriginsFlag.Name))
- }
- if ctx.GlobalIsSet(WSApiFlag.Name) {
- cfg.WSModules = splitAndTrim(ctx.GlobalString(WSApiFlag.Name))
- }
-}
-
-// setIPC creates an IPC path configuration from the set command line flags,
-// returning an empty string if IPC was explicitly disabled, or the set path.
-func setIPC(ctx *cli.Context, cfg *node.Config) {
- CheckExclusive(ctx, IPCDisabledFlag, IPCPathFlag)
- switch {
- case ctx.GlobalBool(IPCDisabledFlag.Name):
- cfg.IPCPath = ""
- case ctx.GlobalIsSet(IPCPathFlag.Name):
- cfg.IPCPath = ctx.GlobalString(IPCPathFlag.Name)
- }
-}
-
-// makeDatabaseHandles raises out the number of allowed file handles per process
-// for Geth and returns half of the allowance to assign to the database.
-func makeDatabaseHandles() int {
- limit, err := fdlimit.Maximum()
- if err != nil {
- Fatalf("Failed to retrieve file descriptor allowance: %v", err)
- }
- raised, err := fdlimit.Raise(uint64(limit))
- if err != nil {
- Fatalf("Failed to raise file descriptor allowance: %v", err)
- }
- return int(raised / 2) // Leave half for networking and other stuff
-}
-
-// MakeAddress converts an account specified directly as a hex encoded string or
-// a key index in the key store to an internal account representation.
-func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error) {
- // If the specified account is a valid address, return it
- if common.IsHexAddress(account) {
- return accounts.Account{Address: common.HexToAddress(account)}, nil
- }
- // Otherwise try to interpret the account as a keystore index
- index, err := strconv.Atoi(account)
- if err != nil || index < 0 {
- return accounts.Account{}, fmt.Errorf("invalid account address or index %q", account)
- }
- log.Warn("-------------------------------------------------------------------")
- log.Warn("Referring to accounts by order in the keystore folder is dangerous!")
- log.Warn("This functionality is deprecated and will be removed in the future!")
- log.Warn("Please use explicit addresses! (can search via `geth account list`)")
- log.Warn("-------------------------------------------------------------------")
-
- accs := ks.Accounts()
- if len(accs) <= index {
- return accounts.Account{}, fmt.Errorf("index %d higher than number of accounts %d", index, len(accs))
- }
- return accs[index], nil
-}
-
-// setEtherbase retrieves the etherbase either from the directly specified
-// command line flags or from the keystore if CLI indexed.
-func setEtherbase(ctx *cli.Context, ks *keystore.KeyStore, cfg *eth.Config) {
- // Extract the current etherbase, new flag overriding legacy one
- var etherbase string
- if ctx.GlobalIsSet(MinerLegacyEtherbaseFlag.Name) {
- etherbase = ctx.GlobalString(MinerLegacyEtherbaseFlag.Name)
- }
- if ctx.GlobalIsSet(MinerEtherbaseFlag.Name) {
- etherbase = ctx.GlobalString(MinerEtherbaseFlag.Name)
- }
- // Convert the etherbase into an address and configure it
- if etherbase != "" {
- if ks != nil {
- account, err := MakeAddress(ks, etherbase)
- if err != nil {
- Fatalf("Invalid miner etherbase: %v", err)
- }
- cfg.Miner.Etherbase = account.Address
- } else {
- Fatalf("No etherbase configured")
- }
- }
-}
-
-// MakePasswordList reads password lines from the file specified by the global --password flag.
-func MakePasswordList(ctx *cli.Context) []string {
- path := ctx.GlobalString(PasswordFileFlag.Name)
- if path == "" {
- return nil
- }
- text, err := ioutil.ReadFile(path)
- if err != nil {
- Fatalf("Failed to read password file: %v", err)
- }
- lines := strings.Split(string(text), "\n")
- // Sanitise DOS line endings.
- for i := range lines {
- lines[i] = strings.TrimRight(lines[i], "\r")
- }
- return lines
-}
-
-func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
- setNodeKey(ctx, cfg)
- setNAT(ctx, cfg)
- setListenAddress(ctx, cfg)
- setBootstrapNodes(ctx, cfg)
- setBootstrapNodesV5(ctx, cfg)
-
- lightClient := ctx.GlobalString(SyncModeFlag.Name) == "light"
- lightServer := (ctx.GlobalInt(LightLegacyServFlag.Name) != 0 || ctx.GlobalInt(LightServeFlag.Name) != 0)
-
- lightPeers := ctx.GlobalInt(LightLegacyPeersFlag.Name)
- if ctx.GlobalIsSet(LightMaxPeersFlag.Name) {
- lightPeers = ctx.GlobalInt(LightMaxPeersFlag.Name)
- }
- if ctx.GlobalIsSet(MaxPeersFlag.Name) {
- cfg.MaxPeers = ctx.GlobalInt(MaxPeersFlag.Name)
- if lightServer && !ctx.GlobalIsSet(LightLegacyPeersFlag.Name) && !ctx.GlobalIsSet(LightMaxPeersFlag.Name) {
- cfg.MaxPeers += lightPeers
- }
- } else {
- if lightServer {
- cfg.MaxPeers += lightPeers
- }
- if lightClient && (ctx.GlobalIsSet(LightLegacyPeersFlag.Name) || ctx.GlobalIsSet(LightMaxPeersFlag.Name)) && cfg.MaxPeers < lightPeers {
- cfg.MaxPeers = lightPeers
- }
- }
- if !(lightClient || lightServer) {
- lightPeers = 0
- }
- ethPeers := cfg.MaxPeers - lightPeers
- if lightClient {
- ethPeers = 0
- }
- log.Info("Maximum peer count", "ETH", ethPeers, "LES", lightPeers, "total", cfg.MaxPeers)
-
- if ctx.GlobalIsSet(MaxPendingPeersFlag.Name) {
- cfg.MaxPendingPeers = ctx.GlobalInt(MaxPendingPeersFlag.Name)
- }
- if ctx.GlobalIsSet(NoDiscoverFlag.Name) || lightClient {
- cfg.NoDiscovery = true
- }
-
- // if we're running a light client or server, force enable the v5 peer discovery
- // unless it is explicitly disabled with --nodiscover note that explicitly specifying
- // --v5disc overrides --nodiscover, in which case the later only disables v4 discovery
- forceV5Discovery := (lightClient || lightServer) && !ctx.GlobalBool(NoDiscoverFlag.Name)
- if ctx.GlobalIsSet(DiscoveryV5Flag.Name) {
- cfg.DiscoveryV5 = ctx.GlobalBool(DiscoveryV5Flag.Name)
- } else if forceV5Discovery {
- cfg.DiscoveryV5 = true
- }
-
- if netrestrict := ctx.GlobalString(NetrestrictFlag.Name); netrestrict != "" {
- list, err := netutil.ParseNetlist(netrestrict)
- if err != nil {
- Fatalf("Option %q: %v", NetrestrictFlag.Name, err)
- }
- cfg.NetRestrict = list
- }
-
- if ctx.GlobalBool(DeveloperFlag.Name) {
- // --dev mode can't use p2p networking.
- cfg.MaxPeers = 0
- cfg.ListenAddr = ":0"
- cfg.NoDiscovery = true
- cfg.DiscoveryV5 = false
- }
-}
-
-// SetNodeConfig applies node-related command line flags to the config.
-func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
- SetP2PConfig(ctx, &cfg.P2P)
- setIPC(ctx, cfg)
- setHTTP(ctx, cfg)
- setGraphQL(ctx, cfg)
- setWS(ctx, cfg)
- setNodeUserIdent(ctx, cfg)
- setDataDir(ctx, cfg)
- setSmartCard(ctx, cfg)
-
- if ctx.GlobalIsSet(ExternalSignerFlag.Name) {
- cfg.ExternalSigner = ctx.GlobalString(ExternalSignerFlag.Name)
- }
-
- if ctx.GlobalIsSet(KeyStoreDirFlag.Name) {
- cfg.KeyStoreDir = ctx.GlobalString(KeyStoreDirFlag.Name)
- }
- if ctx.GlobalIsSet(LightKDFFlag.Name) {
- cfg.UseLightweightKDF = ctx.GlobalBool(LightKDFFlag.Name)
- }
- if ctx.GlobalIsSet(NoUSBFlag.Name) {
- cfg.NoUSB = ctx.GlobalBool(NoUSBFlag.Name)
- }
- if ctx.GlobalIsSet(InsecureUnlockAllowedFlag.Name) {
- cfg.InsecureUnlockAllowed = ctx.GlobalBool(InsecureUnlockAllowedFlag.Name)
- }
-}
-
-func setSmartCard(ctx *cli.Context, cfg *node.Config) {
- // Skip enabling smartcards if no path is set
- path := ctx.GlobalString(SmartCardDaemonPathFlag.Name)
- if path == "" {
- return
- }
- // Sanity check that the smartcard path is valid
- fi, err := os.Stat(path)
- if err != nil {
- log.Info("Smartcard socket not found, disabling", "err", err)
- return
- }
- if fi.Mode()&os.ModeType != os.ModeSocket {
- log.Error("Invalid smartcard daemon path", "path", path, "type", fi.Mode().String())
- return
- }
- // Smartcard daemon path exists and is a socket, enable it
- cfg.SmartCardDaemonPath = path
-}
-
-func setDataDir(ctx *cli.Context, cfg *node.Config) {
- switch {
- case ctx.GlobalIsSet(DataDirFlag.Name):
- cfg.DataDir = ctx.GlobalString(DataDirFlag.Name)
- case ctx.GlobalBool(DeveloperFlag.Name):
- cfg.DataDir = "" // unless explicitly requested, use memory databases
- case ctx.GlobalBool(TestnetFlag.Name) && cfg.DataDir == node.DefaultDataDir():
- cfg.DataDir = filepath.Join(node.DefaultDataDir(), "testnet")
- case ctx.GlobalBool(RinkebyFlag.Name) && cfg.DataDir == node.DefaultDataDir():
- cfg.DataDir = filepath.Join(node.DefaultDataDir(), "rinkeby")
- case ctx.GlobalBool(GoerliFlag.Name) && cfg.DataDir == node.DefaultDataDir():
- cfg.DataDir = filepath.Join(node.DefaultDataDir(), "goerli")
- }
-}
-
-func setGPO(ctx *cli.Context, cfg *gasprice.Config) {
- if ctx.GlobalIsSet(GpoBlocksFlag.Name) {
- cfg.Blocks = ctx.GlobalInt(GpoBlocksFlag.Name)
- }
- if ctx.GlobalIsSet(GpoPercentileFlag.Name) {
- cfg.Percentile = ctx.GlobalInt(GpoPercentileFlag.Name)
- }
-}
-
-func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) {
- if ctx.GlobalIsSet(TxPoolLocalsFlag.Name) {
- locals := strings.Split(ctx.GlobalString(TxPoolLocalsFlag.Name), ",")
- for _, account := range locals {
- if trimmed := strings.TrimSpace(account); !common.IsHexAddress(trimmed) {
- Fatalf("Invalid account in --txpool.locals: %s", trimmed)
- } else {
- cfg.Locals = append(cfg.Locals, common.HexToAddress(account))
- }
- }
- }
- if ctx.GlobalIsSet(TxPoolNoLocalsFlag.Name) {
- cfg.NoLocals = ctx.GlobalBool(TxPoolNoLocalsFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolJournalFlag.Name) {
- cfg.Journal = ctx.GlobalString(TxPoolJournalFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolRejournalFlag.Name) {
- cfg.Rejournal = ctx.GlobalDuration(TxPoolRejournalFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolPriceLimitFlag.Name) {
- cfg.PriceLimit = ctx.GlobalUint64(TxPoolPriceLimitFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolPriceBumpFlag.Name) {
- cfg.PriceBump = ctx.GlobalUint64(TxPoolPriceBumpFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolAccountSlotsFlag.Name) {
- cfg.AccountSlots = ctx.GlobalUint64(TxPoolAccountSlotsFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolGlobalSlotsFlag.Name) {
- cfg.GlobalSlots = ctx.GlobalUint64(TxPoolGlobalSlotsFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolAccountQueueFlag.Name) {
- cfg.AccountQueue = ctx.GlobalUint64(TxPoolAccountQueueFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolGlobalQueueFlag.Name) {
- cfg.GlobalQueue = ctx.GlobalUint64(TxPoolGlobalQueueFlag.Name)
- }
- if ctx.GlobalIsSet(TxPoolLifetimeFlag.Name) {
- cfg.Lifetime = ctx.GlobalDuration(TxPoolLifetimeFlag.Name)
- }
-}
-
-func setEthash(ctx *cli.Context, cfg *eth.Config) {
- if ctx.GlobalIsSet(EthashCacheDirFlag.Name) {
- cfg.Ethash.CacheDir = ctx.GlobalString(EthashCacheDirFlag.Name)
- }
- if ctx.GlobalIsSet(EthashDatasetDirFlag.Name) {
- cfg.Ethash.DatasetDir = ctx.GlobalString(EthashDatasetDirFlag.Name)
- }
- if ctx.GlobalIsSet(EthashCachesInMemoryFlag.Name) {
- cfg.Ethash.CachesInMem = ctx.GlobalInt(EthashCachesInMemoryFlag.Name)
- }
- if ctx.GlobalIsSet(EthashCachesOnDiskFlag.Name) {
- cfg.Ethash.CachesOnDisk = ctx.GlobalInt(EthashCachesOnDiskFlag.Name)
- }
- if ctx.GlobalIsSet(EthashDatasetsInMemoryFlag.Name) {
- cfg.Ethash.DatasetsInMem = ctx.GlobalInt(EthashDatasetsInMemoryFlag.Name)
- }
- if ctx.GlobalIsSet(EthashDatasetsOnDiskFlag.Name) {
- cfg.Ethash.DatasetsOnDisk = ctx.GlobalInt(EthashDatasetsOnDiskFlag.Name)
- }
-}
-
-func setMiner(ctx *cli.Context, cfg *miner.Config) {
- if ctx.GlobalIsSet(MinerNotifyFlag.Name) {
- cfg.Notify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",")
- }
- if ctx.GlobalIsSet(MinerLegacyExtraDataFlag.Name) {
- cfg.ExtraData = []byte(ctx.GlobalString(MinerLegacyExtraDataFlag.Name))
- }
- if ctx.GlobalIsSet(MinerExtraDataFlag.Name) {
- cfg.ExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name))
- }
- if ctx.GlobalIsSet(MinerLegacyGasTargetFlag.Name) {
- cfg.GasFloor = ctx.GlobalUint64(MinerLegacyGasTargetFlag.Name)
- }
- if ctx.GlobalIsSet(MinerGasTargetFlag.Name) {
- cfg.GasFloor = ctx.GlobalUint64(MinerGasTargetFlag.Name)
- }
- if ctx.GlobalIsSet(MinerGasLimitFlag.Name) {
- cfg.GasCeil = ctx.GlobalUint64(MinerGasLimitFlag.Name)
- }
- if ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) {
- cfg.GasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name)
- }
- if ctx.GlobalIsSet(MinerGasPriceFlag.Name) {
- cfg.GasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name)
- }
- if ctx.GlobalIsSet(MinerRecommitIntervalFlag.Name) {
- cfg.Recommit = ctx.Duration(MinerRecommitIntervalFlag.Name)
- }
- if ctx.GlobalIsSet(MinerNoVerfiyFlag.Name) {
- cfg.Noverify = ctx.Bool(MinerNoVerfiyFlag.Name)
- }
-}
-
-func setWhitelist(ctx *cli.Context, cfg *eth.Config) {
- whitelist := ctx.GlobalString(WhitelistFlag.Name)
- if whitelist == "" {
- return
- }
- cfg.Whitelist = make(map[uint64]common.Hash)
- for _, entry := range strings.Split(whitelist, ",") {
- parts := strings.Split(entry, "=")
- if len(parts) != 2 {
- Fatalf("Invalid whitelist entry: %s", entry)
- }
- number, err := strconv.ParseUint(parts[0], 0, 64)
- if err != nil {
- Fatalf("Invalid whitelist block number %s: %v", parts[0], err)
- }
- var hash common.Hash
- if err = hash.UnmarshalText([]byte(parts[1])); err != nil {
- Fatalf("Invalid whitelist hash %s: %v", parts[1], err)
- }
- cfg.Whitelist[number] = hash
- }
-}
-
-// CheckExclusive verifies that only a single instance of the provided flags was
-// set by the user. Each flag might optionally be followed by a string type to
-// specialize it further.
-func CheckExclusive(ctx *cli.Context, args ...interface{}) {
- set := make([]string, 0, 1)
- for i := 0; i < len(args); i++ {
- // Make sure the next argument is a flag and skip if not set
- flag, ok := args[i].(cli.Flag)
- if !ok {
- panic(fmt.Sprintf("invalid argument, not cli.Flag type: %T", args[i]))
- }
- // Check if next arg extends current and expand its name if so
- name := flag.GetName()
-
- if i+1 < len(args) {
- switch option := args[i+1].(type) {
- case string:
- // Extended flag check, make sure value set doesn't conflict with passed in option
- if ctx.GlobalString(flag.GetName()) == option {
- name += "=" + option
- set = append(set, "--"+name)
- }
- // shift arguments and continue
- i++
- continue
-
- case cli.Flag:
- default:
- panic(fmt.Sprintf("invalid argument, not cli.Flag or string extension: %T", args[i+1]))
- }
- }
- // Mark the flag if it's set
- if ctx.GlobalIsSet(flag.GetName()) {
- set = append(set, "--"+name)
- }
- }
- if len(set) > 1 {
- Fatalf("Flags %v can't be used at the same time", strings.Join(set, ", "))
- }
-}
-
-// SetShhConfig applies shh-related command line flags to the config.
-func SetShhConfig(ctx *cli.Context, stack *node.Node, cfg *whisper.Config) {
- if ctx.GlobalIsSet(WhisperMaxMessageSizeFlag.Name) {
- cfg.MaxMessageSize = uint32(ctx.GlobalUint(WhisperMaxMessageSizeFlag.Name))
- }
- if ctx.GlobalIsSet(WhisperMinPOWFlag.Name) {
- cfg.MinimumAcceptedPOW = ctx.GlobalFloat64(WhisperMinPOWFlag.Name)
- }
- if ctx.GlobalIsSet(WhisperRestrictConnectionBetweenLightClientsFlag.Name) {
- cfg.RestrictConnectionBetweenLightClients = true
- }
-}
-
-// SetEthConfig applies eth-related command line flags to the config.
-func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
- // Avoid conflicting network flags
- CheckExclusive(ctx, DeveloperFlag, TestnetFlag, RinkebyFlag, GoerliFlag)
- CheckExclusive(ctx, LightLegacyServFlag, LightServeFlag, SyncModeFlag, "light")
- CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
-
- var ks *keystore.KeyStore
- if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
- ks = keystores[0].(*keystore.KeyStore)
- }
- setEtherbase(ctx, ks, cfg)
- setGPO(ctx, &cfg.GPO)
- setTxPool(ctx, &cfg.TxPool)
- setEthash(ctx, cfg)
- setMiner(ctx, &cfg.Miner)
- setWhitelist(ctx, cfg)
-
- if ctx.GlobalIsSet(SyncModeFlag.Name) {
- cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
- }
- if ctx.GlobalIsSet(NetworkIdFlag.Name) {
- cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name)
- }
- if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheDatabaseFlag.Name) {
- cfg.DatabaseCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
- }
- cfg.DatabaseHandles = makeDatabaseHandles()
- if ctx.GlobalIsSet(AncientFlag.Name) {
- cfg.DatabaseFreezer = ctx.GlobalString(AncientFlag.Name)
- }
-
- if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
- Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
- }
- cfg.NoPruning = ctx.GlobalString(GCModeFlag.Name) == "archive"
- cfg.NoPrefetch = ctx.GlobalBool(CacheNoPrefetchFlag.Name)
-
- if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheTrieFlag.Name) {
- cfg.TrieCleanCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheTrieFlag.Name) / 100
- }
- if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
- cfg.TrieDirtyCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
- }
- if ctx.GlobalIsSet(DocRootFlag.Name) {
- cfg.DocRoot = ctx.GlobalString(DocRootFlag.Name)
- }
- if ctx.GlobalIsSet(VMEnableDebugFlag.Name) {
- // TODO(fjl): force-enable this in --dev mode
- cfg.EnablePreimageRecording = ctx.GlobalBool(VMEnableDebugFlag.Name)
- }
-
- if ctx.GlobalIsSet(EWASMInterpreterFlag.Name) {
- cfg.EWASMInterpreter = ctx.GlobalString(EWASMInterpreterFlag.Name)
- }
-
- if ctx.GlobalIsSet(EVMInterpreterFlag.Name) {
- cfg.EVMInterpreter = ctx.GlobalString(EVMInterpreterFlag.Name)
- }
- if ctx.GlobalIsSet(RPCGlobalGasCap.Name) {
- cfg.RPCGasCap = new(big.Int).SetUint64(ctx.GlobalUint64(RPCGlobalGasCap.Name))
- }
-
- // Override any default configs for hard coded networks.
- switch {
- case ctx.GlobalBool(TestnetFlag.Name):
- if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
- cfg.NetworkId = 3
- }
- cfg.Genesis = core.DefaultTestnetGenesisBlock()
- case ctx.GlobalBool(RinkebyFlag.Name):
- if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
- cfg.NetworkId = 4
- }
- cfg.Genesis = core.DefaultRinkebyGenesisBlock()
- case ctx.GlobalBool(GoerliFlag.Name):
- if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
- cfg.NetworkId = 5
- }
- cfg.Genesis = core.DefaultGoerliGenesisBlock()
- case ctx.GlobalBool(DeveloperFlag.Name):
- if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
- cfg.NetworkId = 1337
- }
- // Create new developer account or reuse existing one
- var (
- developer accounts.Account
- err error
- )
- if accs := ks.Accounts(); len(accs) > 0 {
- developer = ks.Accounts()[0]
- } else {
- developer, err = ks.NewAccount("")
- if err != nil {
- Fatalf("Failed to create developer account: %v", err)
- }
- }
- if err := ks.Unlock(developer, ""); err != nil {
- Fatalf("Failed to unlock developer account: %v", err)
- }
- log.Info("Using developer account", "address", developer.Address)
-
- cfg.Genesis = core.DeveloperGenesisBlock(uint64(ctx.GlobalInt(DeveloperPeriodFlag.Name)), developer.Address)
- if !ctx.GlobalIsSet(MinerGasPriceFlag.Name) && !ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) {
- cfg.Miner.GasPrice = big.NewInt(1)
- }
- }
-}
-
-// SetDashboardConfig applies dashboard related command line flags to the config.
-func SetDashboardConfig(ctx *cli.Context, cfg *dashboard.Config) {
- cfg.Host = ctx.GlobalString(DashboardAddrFlag.Name)
- cfg.Port = ctx.GlobalInt(DashboardPortFlag.Name)
- cfg.Refresh = ctx.GlobalDuration(DashboardRefreshFlag.Name)
-}
-
-// RegisterEthService adds an Ethereum client to the stack.
-func RegisterEthService(stack *node.Node, cfg *eth.Config) {
- var err error
- if cfg.SyncMode == downloader.LightSync {
- panic("not supported")
- } else {
- err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
- fullNode, err := eth.New(ctx, cfg, &dummy.ConsensusCallbacks{}, &miner.MinerCallbacks{}, nil)
- if fullNode != nil && cfg.LightServ > 0 {
- panic("not supported")
- }
- return fullNode, err
- })
- }
- if err != nil {
- Fatalf("Failed to register the Ethereum service: %v", err)
- }
-}
-
-// RegisterDashboardService adds a dashboard to the stack.
-func RegisterDashboardService(stack *node.Node, cfg *dashboard.Config, commit string) {
- stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
- return dashboard.New(cfg, commit, ctx.ResolvePath("logs")), nil
- })
-}
-
-// RegisterShhService configures Whisper and adds it to the given node.
-func RegisterShhService(stack *node.Node, cfg *whisper.Config) {
- if err := stack.Register(func(n *node.ServiceContext) (node.Service, error) {
- return whisper.New(cfg), nil
- }); err != nil {
- Fatalf("Failed to register the Whisper service: %v", err)
- }
-}
-
-// RegisterEthStatsService configures the Ethereum Stats daemon and adds it to
-// the given node.
-func RegisterEthStatsService(stack *node.Node, url string) {
- if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
- // Retrieve both eth service
- var ethServ *eth.Ethereum
- ctx.Service(&ethServ)
-
- // Let ethstats use whichever is not nil
- return ethstats.New(url, ethServ, nil)
- }); err != nil {
- Fatalf("Failed to register the Ethereum Stats service: %v", err)
- }
-}
-
-// RegisterGraphQLService is a utility function to construct a new service and register it against a node.
-func RegisterGraphQLService(stack *node.Node, endpoint string, cors, vhosts []string, timeouts rpc.HTTPTimeouts) {
- if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
- // Try to construct the GraphQL service backed by a full node
- var ethServ *eth.Ethereum
- if err := ctx.Service(&ethServ); err == nil {
- return graphql.New(ethServ.APIBackend, endpoint, cors, vhosts, timeouts)
- }
- // Well, this should not have happened, bail out
- return nil, errors.New("no Ethereum service")
- }); err != nil {
- Fatalf("Failed to register the GraphQL service: %v", err)
- }
-}
-
-func SetupMetrics(ctx *cli.Context) {
- if metrics.Enabled {
- log.Info("Enabling metrics collection")
- var (
- enableExport = ctx.GlobalBool(MetricsEnableInfluxDBFlag.Name)
- endpoint = ctx.GlobalString(MetricsInfluxDBEndpointFlag.Name)
- database = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name)
- username = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name)
- password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
- )
-
- if enableExport {
- tagsMap := SplitTagsFlag(ctx.GlobalString(MetricsInfluxDBTagsFlag.Name))
-
- log.Info("Enabling metrics export to InfluxDB")
-
- go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap)
- }
- }
-}
-
-func SplitTagsFlag(tagsFlag string) map[string]string {
- tags := strings.Split(tagsFlag, ",")
- tagsMap := map[string]string{}
-
- for _, t := range tags {
- if t != "" {
- kv := strings.Split(t, "=")
-
- if len(kv) == 2 {
- tagsMap[kv[0]] = kv[1]
- }
- }
- }
-
- return tagsMap
-}
-
-// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
-func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
- var (
- cache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
- handles = makeDatabaseHandles()
- )
- name := "chaindata"
- if ctx.GlobalString(SyncModeFlag.Name) == "light" {
- name = "lightchaindata"
- }
- chainDb, err := stack.OpenDatabaseWithFreezer(name, cache, handles, ctx.GlobalString(AncientFlag.Name), "")
- if err != nil {
- Fatalf("Could not open database: %v", err)
- }
- return chainDb
-}
-
-func MakeGenesis(ctx *cli.Context) *core.Genesis {
- var genesis *core.Genesis
- switch {
- case ctx.GlobalBool(TestnetFlag.Name):
- genesis = core.DefaultTestnetGenesisBlock()
- case ctx.GlobalBool(RinkebyFlag.Name):
- genesis = core.DefaultRinkebyGenesisBlock()
- case ctx.GlobalBool(GoerliFlag.Name):
- genesis = core.DefaultGoerliGenesisBlock()
- case ctx.GlobalBool(DeveloperFlag.Name):
- Fatalf("Developer chains are ephemeral")
- }
- return genesis
-}
-
-// MakeChain creates a chain manager from set command line flags.
-func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chainDb ethdb.Database) {
- var err error
- chainDb = MakeChainDatabase(ctx, stack)
- config, _, err := core.SetupGenesisBlock(chainDb, MakeGenesis(ctx))
- if err != nil {
- Fatalf("%v", err)
- }
- var engine consensus.Engine
- if config.Clique != nil {
- engine = clique.New(config.Clique, chainDb)
- } else {
- engine = ethash.NewFaker()
- if !ctx.GlobalBool(FakePoWFlag.Name) {
- engine = ethash.New(ethash.Config{
- CacheDir: stack.ResolvePath(eth.DefaultConfig.Ethash.CacheDir),
- CachesInMem: eth.DefaultConfig.Ethash.CachesInMem,
- CachesOnDisk: eth.DefaultConfig.Ethash.CachesOnDisk,
- DatasetDir: stack.ResolvePath(eth.DefaultConfig.Ethash.DatasetDir),
- DatasetsInMem: eth.DefaultConfig.Ethash.DatasetsInMem,
- DatasetsOnDisk: eth.DefaultConfig.Ethash.DatasetsOnDisk,
- }, nil, false)
- }
- }
- if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
- Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
- }
- cache := &core.CacheConfig{
- TrieCleanLimit: eth.DefaultConfig.TrieCleanCache,
- TrieCleanNoPrefetch: ctx.GlobalBool(CacheNoPrefetchFlag.Name),
- TrieDirtyLimit: eth.DefaultConfig.TrieDirtyCache,
- TrieDirtyDisabled: ctx.GlobalString(GCModeFlag.Name) == "archive",
- TrieTimeLimit: eth.DefaultConfig.TrieTimeout,
- }
- if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheTrieFlag.Name) {
- cache.TrieCleanLimit = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheTrieFlag.Name) / 100
- }
- if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
- cache.TrieDirtyLimit = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
- }
- vmcfg := vm.Config{EnablePreimageRecording: ctx.GlobalBool(VMEnableDebugFlag.Name)}
- chain, err = core.NewBlockChain(chainDb, cache, config, engine, vmcfg, nil)
- if err != nil {
- Fatalf("Can't create BlockChain: %v", err)
- }
- return chain, chainDb
-}
-
-// MakeConsolePreloads retrieves the absolute paths for the console JavaScript
-// scripts to preload before starting.
-func MakeConsolePreloads(ctx *cli.Context) []string {
- // Skip preloading if there's nothing to preload
- if ctx.GlobalString(PreloadJSFlag.Name) == "" {
- return nil
- }
- // Otherwise resolve absolute paths and return them
- var preloads []string
-
- assets := ctx.GlobalString(JSpathFlag.Name)
- for _, file := range strings.Split(ctx.GlobalString(PreloadJSFlag.Name), ",") {
- preloads = append(preloads, common.AbsolutePath(assets, strings.TrimSpace(file)))
- }
- return preloads
-}
-
-// MigrateFlags sets the global flag from a local flag when it's set.
-// This is a temporary function used for migrating old command/flags to the
-// new format.
-//
-// e.g. geth account new --keystore /tmp/mykeystore --lightkdf
-//
-// is equivalent after calling this method with:
-//
-// geth --keystore /tmp/mykeystore --lightkdf account new
-//
-// This allows the use of the existing configuration functionality.
-// When all flags are migrated this function can be removed and the existing
-// configuration functionality must be changed that is uses local flags
-func MigrateFlags(action func(ctx *cli.Context) error) func(*cli.Context) error {
- return func(ctx *cli.Context) error {
- for _, name := range ctx.FlagNames() {
- if ctx.IsSet(name) {
- ctx.GlobalSet(name, ctx.String(name))
- }
- }
- return action(ctx)
- }
-}
diff --git a/consensus/clique/api.go b/consensus/clique/api.go
new file mode 100644
index 0000000..04e74eb
--- /dev/null
+++ b/consensus/clique/api.go
@@ -0,0 +1,119 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package clique
+
+import (
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// 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.ChainReader
+ clique *Clique
+}
+
+// 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.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
+}
+
+// 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.clique.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.clique.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.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
+ if err != nil {
+ return nil, err
+ }
+ return snap.signers(), nil
+}
+
+// Proposals returns the current proposals the node tries to uphold and vote on.
+func (api *API) Proposals() map[common.Address]bool {
+ api.clique.lock.RLock()
+ defer api.clique.lock.RUnlock()
+
+ proposals := make(map[common.Address]bool)
+ for address, auth := range api.clique.proposals {
+ proposals[address] = auth
+ }
+ return proposals
+}
+
+// Propose injects a new authorization proposal that the signer will attempt to
+// push through.
+func (api *API) Propose(address common.Address, auth bool) {
+ api.clique.lock.Lock()
+ defer api.clique.lock.Unlock()
+
+ api.clique.proposals[address] = auth
+}
+
+// Discard drops a currently running proposal, stopping the signer from casting
+// further votes (either for or against).
+func (api *API) Discard(address common.Address) {
+ api.clique.lock.Lock()
+ defer api.clique.lock.Unlock()
+
+ delete(api.clique.proposals, address)
+}
diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go
new file mode 100644
index 0000000..c97e198
--- /dev/null
+++ b/consensus/clique/clique.go
@@ -0,0 +1,738 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package clique implements the proof-of-authority consensus engine.
+package clique
+
+import (
+ "bytes"
+ "errors"
+ "io"
+ "math/big"
+ "math/rand"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/consensus/misc"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/rlp"
+ lru "github.com/hashicorp/golang-lru"
+ "golang.org/x/crypto/sha3"
+)
+
+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
+
+ wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
+)
+
+// Clique proof-of-authority protocol constants.
+var (
+ epochLength = uint64(30000) // 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 = crypto.SignatureLength // Fixed number of extra-data suffix bytes reserved for signer seal
+
+ nonceAuthVote = hexutil.MustDecode("0xffffffffffffffff") // Magic nonce number to vote on adding a new signer
+ nonceDropVote = hexutil.MustDecode("0x0000000000000000") // Magic nonce number to vote on removing a signer.
+
+ 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
+)
+
+// 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")
+
+ // errExtraSigners is returned if non-checkpoint block contain signer data in
+ // their extra-data fields.
+ errExtraSigners = errors.New("non-checkpoint block contains extra signer list")
+
+ // errInvalidCheckpointSigners is returned if a checkpoint block contains an
+ // invalid list of signers (i.e. non divisible by 20 bytes).
+ errInvalidCheckpointSigners = errors.New("invalid signer list on checkpoint block")
+
+ // errMismatchingCheckpointSigners is returned if a checkpoint block contains a
+ // list of signers different than the one the local node calculated.
+ errMismatchingCheckpointSigners = errors.New("mismatching signer list on checkpoint 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")
+
+ // errWrongDifficulty is returned if the difficulty of a block doesn't match the
+ // turn of the signer.
+ errWrongDifficulty = errors.New("wrong 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")
+
+ // errInvalidVotingChain is returned if an authorization list is attempted to
+ // be modified via out-of-range or non-contiguous headers.
+ errInvalidVotingChain = errors.New("invalid voting chain")
+
+ // errUnauthorizedSigner is returned if a header is signed by a non-authorized entity.
+ errUnauthorizedSigner = errors.New("unauthorized signer")
+
+ // errRecentlySigned is returned if a header is signed by an authorized entity
+ // that already signed a header recently, thus is temporarily not allowed to.
+ errRecentlySigned = errors.New("recently signed")
+)
+
+// 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
+}
+
+// Clique is the proof-of-authority consensus engine proposed to support the
+// Ethereum testnet following the Ropsten attacks.
+type Clique struct {
+ config *params.CliqueConfig // Consensus engine configuration parameters
+ 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
+
+ proposals map[common.Address]bool // Current list of proposals we are pushing
+
+ signer common.Address // Ethereum address of the signing key
+ signFn SignerFn // Signer function to authorize hashes with
+ lock sync.RWMutex // Protects the signer fields
+
+ // The fields below are for testing only
+ fakeDiff bool // Skip difficulty verifications
+}
+
+// New creates a Clique proof-of-authority consensus engine with the initial
+// signers set to the ones provided by the user.
+func New(config *params.CliqueConfig, db ethdb.Database) *Clique {
+ // Set any missing consensus parameters to their defaults
+ conf := *config
+ if conf.Epoch == 0 {
+ conf.Epoch = epochLength
+ }
+ // Allocate the snapshot caches and create the engine
+ recents, _ := lru.NewARC(inmemorySnapshots)
+ signatures, _ := lru.NewARC(inmemorySignatures)
+
+ return &Clique{
+ config: &conf,
+ db: db,
+ recents: recents,
+ signatures: signatures,
+ proposals: make(map[common.Address]bool),
+ }
+}
+
+// Author implements consensus.Engine, returning the Ethereum address recovered
+// from the signature in the header's extra-data section.
+func (c *Clique) Author(header *types.Header) (common.Address, error) {
+ return ecrecover(header, c.signatures)
+}
+
+// VerifyHeader checks whether a header conforms to the consensus rules.
+func (c *Clique) VerifyHeader(chain consensus.ChainReader, 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 *Clique) VerifyHeaders(chain consensus.ChainReader, 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 *Clique) verifyHeader(chain consensus.ChainReader, 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
+ }
+ // Checkpoint blocks need to enforce zero beneficiary
+ checkpoint := (number % c.config.Epoch) == 0
+ if checkpoint && header.Coinbase != (common.Address{}) {
+ return errInvalidCheckpointBeneficiary
+ }
+ // Nonces must be 0x00..0 or 0xff..f, zeroes enforced on checkpoints
+ if !bytes.Equal(header.Nonce[:], nonceAuthVote) && !bytes.Equal(header.Nonce[:], nonceDropVote) {
+ return errInvalidVote
+ }
+ if checkpoint && !bytes.Equal(header.Nonce[:], nonceDropVote) {
+ return errInvalidCheckpointVote
+ }
+ // Check that the extra-data contains both the vanity and signature
+ if len(header.Extra) < extraVanity {
+ return errMissingVanity
+ }
+ if len(header.Extra) < extraVanity+extraSeal {
+ return errMissingSignature
+ }
+ // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise
+ signersBytes := len(header.Extra) - extraVanity - extraSeal
+ if !checkpoint && signersBytes != 0 {
+ return errExtraSigners
+ }
+ if checkpoint && signersBytes%common.AddressLength != 0 {
+ return errInvalidCheckpointSigners
+ }
+ // 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 || (header.Difficulty.Cmp(diffInTurn) != 0 && header.Difficulty.Cmp(diffNoTurn) != 0) {
+ 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)
+}
+
+// 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 *Clique) verifyCascadingFields(chain consensus.ChainReader, 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
+ }
+ // If the block is a checkpoint block, verify the signer list
+ if number%c.config.Epoch == 0 {
+ signers := make([]byte, len(snap.Signers)*common.AddressLength)
+ for i, signer := range snap.signers() {
+ copy(signers[i*common.AddressLength:], signer[:])
+ }
+ extraSuffix := len(header.Extra) - extraSeal
+ if !bytes.Equal(header.Extra[extraVanity:extraSuffix], signers) {
+ return errMismatchingCheckpointSigners
+ }
+ }
+ // 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 *Clique) snapshot(chain consensus.ChainReader, 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); err == nil {
+ log.Trace("Loaded voting 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.
+ if number == 0 || (number%c.config.Epoch == 0 && (len(headers) > params.ImmutabilityThreshold || chain.GetHeaderByNumber(number-1) == nil)) {
+ checkpoint := chain.GetHeaderByNumber(number)
+ if checkpoint != nil {
+ hash := checkpoint.Hash()
+
+ signers := make([]common.Address, (len(checkpoint.Extra)-extraVanity-extraSeal)/common.AddressLength)
+ for i := 0; i < len(signers); i++ {
+ copy(signers[i][:], checkpoint.Extra[extraVanity+i*common.AddressLength:])
+ }
+ snap = newSnapshot(c.config, c.signatures, number, hash, signers)
+ 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
+ }
+ // 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 voting 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 *Clique) 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 *Clique) VerifySeal(chain consensus.ChainReader, 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 *Clique) verifySeal(chain consensus.ChainReader, 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 _, ok := snap.Signers[signer]; !ok {
+ return errUnauthorizedSigner
+ }
+ for seen, recent := range snap.Recents {
+ if recent == signer {
+ // Signer is among recents, only fail if the current block doesn't shift it out
+ if limit := uint64(len(snap.Signers)/2 + 1); seen > number-limit {
+ return errRecentlySigned
+ }
+ }
+ }
+ // Ensure that the difficulty corresponds to the turn-ness of the signer
+ if !c.fakeDiff {
+ inturn := snap.inturn(header.Number.Uint64(), signer)
+ if inturn && header.Difficulty.Cmp(diffInTurn) != 0 {
+ return errWrongDifficulty
+ }
+ if !inturn && header.Difficulty.Cmp(diffNoTurn) != 0 {
+ return errWrongDifficulty
+ }
+ }
+ return nil
+}
+
+// Prepare implements consensus.Engine, preparing all the consensus fields of the
+// header for running the transactions on top.
+func (c *Clique) Prepare(chain consensus.ChainReader, 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 voting snapshot to check which votes make sense
+ snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
+ if err != nil {
+ return err
+ }
+ if number%c.config.Epoch != 0 {
+ c.lock.RLock()
+
+ // Gather all the proposals that make sense voting on
+ addresses := make([]common.Address, 0, len(c.proposals))
+ for address, authorize := range c.proposals {
+ if snap.validVote(address, authorize) {
+ addresses = append(addresses, address)
+ }
+ }
+ // If there's pending proposals, cast a vote on them
+ if len(addresses) > 0 {
+ header.Coinbase = addresses[rand.Intn(len(addresses))]
+ if c.proposals[header.Coinbase] {
+ copy(header.Nonce[:], nonceAuthVote)
+ } else {
+ copy(header.Nonce[:], nonceDropVote)
+ }
+ }
+ c.lock.RUnlock()
+ }
+ // Set the correct difficulty
+ header.Difficulty = CalcDifficulty(snap, 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]
+
+ if number%c.config.Epoch == 0 {
+ for _, signer := range snap.signers() {
+ header.Extra = append(header.Extra, signer[:]...)
+ }
+ }
+ 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
+ }
+ header.Time = parent.Time + c.config.Period
+ 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 *Clique) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
+ // 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 *Clique) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
+ // 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 and return the final block for sealing
+ return types.NewBlock(header, txs, nil, receipts), nil
+}
+
+// Authorize injects a private key into the consensus engine to mint new blocks
+// with.
+func (c *Clique) 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 *Clique) Seal(chain consensus.ChainReader, 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()
+
+ // Bail out if we're unauthorized to sign a block
+ snap, err := c.snapshot(chain, number-1, header.ParentHash, nil)
+ if err != nil {
+ return err
+ }
+ if _, authorized := snap.Signers[signer]; !authorized {
+ return errUnauthorizedSigner
+ }
+ // If we're amongst the recent signers, wait for the next block
+ for seen, recent := range snap.Recents {
+ if recent == signer {
+ // Signer is among recents, only wait if the current block doesn't shift it out
+ if limit := uint64(len(snap.Signers)/2 + 1); number < limit || seen > number-limit {
+ log.Info("Signed recently, must wait for others")
+ return nil
+ }
+ }
+ }
+ // 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
+ if header.Difficulty.Cmp(diffNoTurn) == 0 {
+ // It's not our turn explicitly to sign, delay it a bit
+ wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime
+ delay += time.Duration(rand.Int63n(int64(wiggle)))
+
+ log.Trace("Out-of-turn signing requested", "wiggle", common.PrettyDuration(wiggle))
+ }
+ // Sign all the things!
+ sighash, err := signFn(accounts.Account{Address: signer}, accounts.MimetypeClique, CliqueRLP(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:
+ return
+ case <-time.After(delay):
+ }
+
+ select {
+ case results <- block.WithSeal(header):
+ default:
+ log.Warn("Sealing result is not read by miner", "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 *Clique) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
+ snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil)
+ if err != nil {
+ return nil
+ }
+ return CalcDifficulty(snap, c.signer)
+}
+
+// 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 CalcDifficulty(snap *Snapshot, signer common.Address) *big.Int {
+ if snap.inturn(snap.Number+1, signer) {
+ return new(big.Int).Set(diffInTurn)
+ }
+ return new(big.Int).Set(diffNoTurn)
+}
+
+// SealHash returns the hash of a block prior to it being sealed.
+func (c *Clique) SealHash(header *types.Header) common.Hash {
+ return SealHash(header)
+}
+
+// Close implements consensus.Engine. It's a noop for clique as there are no background threads.
+func (c *Clique) Close() error {
+ return nil
+}
+
+// APIs implements consensus.Engine, returning the user facing RPC API to allow
+// controlling the signer voting.
+func (c *Clique) APIs(chain consensus.ChainReader) []rpc.API {
+ return []rpc.API{{
+ Namespace: "clique",
+ Version: "1.0",
+ Service: &API{chain: chain, clique: c},
+ Public: false,
+ }}
+}
+
+// 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
+}
+
+// CliqueRLP returns the rlp bytes which needs to be signed for the proof-of-authority
+// 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 CliqueRLP(header *types.Header) []byte {
+ b := new(bytes.Buffer)
+ encodeSigHeader(b, header)
+ return b.Bytes()
+}
+
+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)-crypto.SignatureLength], // Yes, this will panic if extra is too short
+ header.MixDigest,
+ header.Nonce,
+ })
+ if err != nil {
+ panic("can't encode: " + err.Error())
+ }
+}
diff --git a/consensus/clique/snapshot.go b/consensus/clique/snapshot.go
new file mode 100644
index 0000000..6660c0f
--- /dev/null
+++ b/consensus/clique/snapshot.go
@@ -0,0 +1,326 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package clique
+
+import (
+ "bytes"
+ "encoding/json"
+ "sort"
+ "time"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/log"
+ lru "github.com/hashicorp/golang-lru"
+)
+
+// Vote represents a single vote that an authorized signer made to modify the
+// list of authorizations.
+type Vote struct {
+ Signer common.Address `json:"signer"` // Authorized signer that cast this vote
+ Block uint64 `json:"block"` // Block number the vote was cast in (expire old votes)
+ Address common.Address `json:"address"` // Account being voted on to change its authorization
+ Authorize bool `json:"authorize"` // Whether to authorize or deauthorize the voted account
+}
+
+// Tally is a simple vote tally to keep the current score of votes. Votes that
+// go against the proposal aren't counted since it's equivalent to not voting.
+type Tally struct {
+ Authorize bool `json:"authorize"` // Whether the vote is about authorizing or kicking someone
+ Votes int `json:"votes"` // Number of votes until now wanting to pass the proposal
+}
+
+// Snapshot is the state of the authorization voting at a given point in time.
+type Snapshot struct {
+ config *params.CliqueConfig // Consensus engine parameters to fine tune behavior
+ 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
+ Signers map[common.Address]struct{} `json:"signers"` // Set of authorized signers at this moment
+ Recents map[uint64]common.Address `json:"recents"` // Set of recent signers for spam protections
+ Votes []*Vote `json:"votes"` // List of votes cast in chronological order
+ Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
+}
+
+// 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.CliqueConfig, sigcache *lru.ARCCache, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
+ snap := &Snapshot{
+ config: config,
+ sigcache: sigcache,
+ Number: number,
+ Hash: hash,
+ Signers: make(map[common.Address]struct{}),
+ Recents: make(map[uint64]common.Address),
+ Tally: make(map[common.Address]Tally),
+ }
+ for _, signer := range signers {
+ snap.Signers[signer] = struct{}{}
+ }
+ return snap
+}
+
+// loadSnapshot loads an existing snapshot from the database.
+func loadSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
+ blob, err := db.Get(append([]byte("clique-"), 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
+
+ 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("clique-"), 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,
+ sigcache: s.sigcache,
+ Number: s.Number,
+ Hash: s.Hash,
+ Signers: make(map[common.Address]struct{}),
+ Recents: make(map[uint64]common.Address),
+ Votes: make([]*Vote, len(s.Votes)),
+ Tally: make(map[common.Address]Tally),
+ }
+ for signer := range s.Signers {
+ cpy.Signers[signer] = struct{}{}
+ }
+ for block, signer := range s.Recents {
+ cpy.Recents[block] = signer
+ }
+ for address, tally := range s.Tally {
+ cpy.Tally[address] = tally
+ }
+ copy(cpy.Votes, s.Votes)
+
+ return cpy
+}
+
+// validVote returns whether it makes sense to cast the specified vote in the
+// given snapshot context (e.g. don't try to add an already authorized signer).
+func (s *Snapshot) validVote(address common.Address, authorize bool) bool {
+ _, signer := s.Signers[address]
+ return (signer && !authorize) || (!signer && authorize)
+}
+
+// cast adds a new vote into the tally.
+func (s *Snapshot) cast(address common.Address, authorize bool) bool {
+ // Ensure the vote is meaningful
+ if !s.validVote(address, authorize) {
+ return false
+ }
+ // Cast the vote into an existing or new tally
+ if old, ok := s.Tally[address]; ok {
+ old.Votes++
+ s.Tally[address] = old
+ } else {
+ s.Tally[address] = Tally{Authorize: authorize, Votes: 1}
+ }
+ return true
+}
+
+// uncast removes a previously cast vote from the tally.
+func (s *Snapshot) uncast(address common.Address, authorize bool) bool {
+ // If there's no tally, it's a dangling vote, just drop
+ tally, ok := s.Tally[address]
+ if !ok {
+ return false
+ }
+ // Ensure we only revert counted votes
+ if tally.Authorize != authorize {
+ return false
+ }
+ // Otherwise revert the vote
+ if tally.Votes > 1 {
+ tally.Votes--
+ s.Tally[address] = tally
+ } else {
+ delete(s.Tally, address)
+ }
+ return true
+}
+
+// apply creates a new authorization snapshot by applying the given headers to
+// the original one.
+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, errInvalidVotingChain
+ }
+ }
+ if headers[0].Number.Uint64() != s.Number+1 {
+ return nil, errInvalidVotingChain
+ }
+ // Iterate through the headers and create a new snapshot
+ snap := s.copy()
+
+ var (
+ start = time.Now()
+ logged = time.Now()
+ )
+ for i, header := range headers {
+ // Remove any votes on checkpoint blocks
+ number := header.Number.Uint64()
+ if number%s.config.Epoch == 0 {
+ snap.Votes = nil
+ snap.Tally = make(map[common.Address]Tally)
+ }
+ // Delete the oldest signer from the recent list to allow it signing again
+ if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
+ delete(snap.Recents, number-limit)
+ }
+ // Resolve the authorization key and check against signers
+ signer, err := ecrecover(header, s.sigcache)
+ if err != nil {
+ return nil, err
+ }
+ if _, ok := snap.Signers[signer]; !ok {
+ return nil, errUnauthorizedSigner
+ }
+ for _, recent := range snap.Recents {
+ if recent == signer {
+ return nil, errRecentlySigned
+ }
+ }
+ snap.Recents[number] = signer
+
+ // Header authorized, discard any previous votes from the signer
+ for i, vote := range snap.Votes {
+ if vote.Signer == signer && vote.Address == header.Coinbase {
+ // Uncast the vote from the cached tally
+ snap.uncast(vote.Address, vote.Authorize)
+
+ // Uncast the vote from the chronological list
+ snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
+ break // only one vote allowed
+ }
+ }
+ // Tally up the new vote from the signer
+ var authorize bool
+ switch {
+ case bytes.Equal(header.Nonce[:], nonceAuthVote):
+ authorize = true
+ case bytes.Equal(header.Nonce[:], nonceDropVote):
+ authorize = false
+ default:
+ return nil, errInvalidVote
+ }
+ if snap.cast(header.Coinbase, authorize) {
+ snap.Votes = append(snap.Votes, &Vote{
+ Signer: signer,
+ Block: number,
+ Address: header.Coinbase,
+ Authorize: authorize,
+ })
+ }
+ // If the vote passed, update the list of signers
+ if tally := snap.Tally[header.Coinbase]; tally.Votes > len(snap.Signers)/2 {
+ if tally.Authorize {
+ snap.Signers[header.Coinbase] = struct{}{}
+ } else {
+ delete(snap.Signers, header.Coinbase)
+
+ // Signer list shrunk, delete any leftover recent caches
+ if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
+ delete(snap.Recents, number-limit)
+ }
+ // Discard any previous votes the deauthorized signer cast
+ for i := 0; i < len(snap.Votes); i++ {
+ if snap.Votes[i].Signer == header.Coinbase {
+ // Uncast the vote from the cached tally
+ snap.uncast(snap.Votes[i].Address, snap.Votes[i].Authorize)
+
+ // Uncast the vote from the chronological list
+ snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
+
+ i--
+ }
+ }
+ }
+ // Discard any previous votes around the just changed account
+ for i := 0; i < len(snap.Votes); i++ {
+ if snap.Votes[i].Address == header.Coinbase {
+ snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
+ i--
+ }
+ }
+ delete(snap.Tally, header.Coinbase)
+ }
+ // If we're taking too much time (ecrecover), notify the user once a while
+ if time.Since(logged) > 8*time.Second {
+ log.Info("Reconstructing voting history", "processed", i, "total", len(headers), "elapsed", common.PrettyDuration(time.Since(start)))
+ logged = time.Now()
+ }
+ }
+ if time.Since(start) > 8*time.Second {
+ log.Info("Reconstructed voting history", "processed", len(headers), "elapsed", common.PrettyDuration(time.Since(start)))
+ }
+ snap.Number += uint64(len(headers))
+ snap.Hash = headers[len(headers)-1].Hash()
+
+ return snap, nil
+}
+
+// signers retrieves the list of authorized signers in ascending order.
+func (s *Snapshot) signers() []common.Address {
+ sigs := make([]common.Address, 0, len(s.Signers))
+ for sig := range s.Signers {
+ sigs = append(sigs, sig)
+ }
+ sort.Sort(signersAscending(sigs))
+ return sigs
+}
+
+// inturn returns if a signer at a given block height is in-turn or not.
+func (s *Snapshot) inturn(number uint64, signer common.Address) bool {
+ signers, offset := s.signers(), 0
+ for offset < len(signers) && signers[offset] != signer {
+ offset++
+ }
+ return (number % uint64(len(signers))) == uint64(offset)
+}
diff --git a/consensus/consensus.go b/consensus/consensus.go
new file mode 100644
index 0000000..93fc9dc
--- /dev/null
+++ b/consensus/consensus.go
@@ -0,0 +1,125 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package consensus implements different Ethereum consensus engines.
+package consensus
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// ChainReader defines a small collection of methods needed to access the local
+// blockchain during header and/or uncle verification.
+type ChainReader interface {
+ // Config retrieves the blockchain's chain configuration.
+ Config() *params.ChainConfig
+
+ // CurrentHeader retrieves the current header from the local chain.
+ CurrentHeader() *types.Header
+
+ // GetHeader retrieves a block header from the database by hash and number.
+ GetHeader(hash common.Hash, number uint64) *types.Header
+
+ // GetHeaderByNumber retrieves a block header from the database by number.
+ GetHeaderByNumber(number uint64) *types.Header
+
+ // GetHeaderByHash retrieves a block header from the database by its hash.
+ GetHeaderByHash(hash common.Hash) *types.Header
+
+ // GetBlock retrieves a block from the database by hash and number.
+ GetBlock(hash common.Hash, number uint64) *types.Block
+}
+
+// Engine is an algorithm agnostic consensus engine.
+type Engine interface {
+ // Author retrieves the Ethereum address of the account that minted the given
+ // block, which may be different from the header's coinbase if a consensus
+ // engine is based on signatures.
+ Author(header *types.Header) (common.Address, error)
+
+ // VerifyHeader checks whether a header conforms to the consensus rules of a
+ // given engine. Verifying the seal may be done optionally here, or explicitly
+ // via the VerifySeal method.
+ VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
+
+ // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
+ // concurrently. 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).
+ VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
+
+ // VerifyUncles verifies that the given block's uncles conform to the consensus
+ // rules of a given engine.
+ VerifyUncles(chain ChainReader, block *types.Block) error
+
+ // VerifySeal checks whether the crypto seal on a header is valid according to
+ // the consensus rules of the given engine.
+ VerifySeal(chain ChainReader, header *types.Header) error
+
+ // Prepare initializes the consensus fields of a block header according to the
+ // rules of a particular engine. The changes are executed inline.
+ Prepare(chain ChainReader, header *types.Header) error
+
+ // Finalize runs any post-transaction state modifications (e.g. block rewards)
+ // but does not assemble the block.
+ //
+ // Note: The block header and state database might be updated to reflect any
+ // consensus rules that happen at finalization (e.g. block rewards).
+ Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
+ uncles []*types.Header)
+
+ // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
+ // rewards) and assembles the final block.
+ //
+ // Note: The block header and state database might be updated to reflect any
+ // consensus rules that happen at finalization (e.g. block rewards).
+ FinalizeAndAssemble(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
+ uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
+
+ // Seal generates a new sealing request for the given input block and pushes
+ // the result into the given channel.
+ //
+ // Note, the method returns immediately and will send the result async. More
+ // than one result may also be returned depending on the consensus algorithm.
+ Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
+
+ // SealHash returns the hash of a block prior to it being sealed.
+ SealHash(header *types.Header) common.Hash
+
+ // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
+ // that a new block should have.
+ CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
+
+ // APIs returns the RPC APIs this consensus engine provides.
+ APIs(chain ChainReader) []rpc.API
+
+ // Close terminates any background threads maintained by the consensus engine.
+ Close() error
+}
+
+// PoW is a consensus engine based on proof-of-work.
+type PoW interface {
+ Engine
+
+ // Hashrate returns the current mining hashrate of a PoW consensus engine.
+ Hashrate() float64
+}
diff --git a/consensus/dummy/consensus.go b/consensus/dummy/consensus.go
index 92bd261..adc85fe 100644
--- a/consensus/dummy/consensus.go
+++ b/consensus/dummy/consensus.go
@@ -8,14 +8,13 @@ import (
"runtime"
"time"
- myparams "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/params"
"github.com/ava-labs/go-ethereum/rlp"
- "github.com/ava-labs/go-ethereum/rpc"
mapset "github.com/deckarep/golang-set"
)
@@ -56,8 +55,8 @@ var (
// modified from consensus.go
func (self *DummyEngine) verifyHeader(chain consensus.ChainReader, header, parent *types.Header, uncle bool, seal bool) error {
// Ensure that the header's extra-data section is of a reasonable size
- if uint64(len(header.Extra)) > myparams.MaximumExtraDataSize {
- return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), myparams.MaximumExtraDataSize)
+ if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
+ return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
}
// Verify the header's timestamp
if !uncle {
diff --git a/consensus/errors.go b/consensus/errors.go
new file mode 100644
index 0000000..a005c5f
--- /dev/null
+++ b/consensus/errors.go
@@ -0,0 +1,37 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package consensus
+
+import "errors"
+
+var (
+ // ErrUnknownAncestor is returned when validating a block requires an ancestor
+ // that is unknown.
+ ErrUnknownAncestor = errors.New("unknown ancestor")
+
+ // ErrPrunedAncestor is returned when validating a block requires an ancestor
+ // that is known, but the state of which is not available.
+ ErrPrunedAncestor = errors.New("pruned ancestor")
+
+ // ErrFutureBlock is returned when a block's timestamp is in the future according
+ // to the current node.
+ ErrFutureBlock = errors.New("block in the future")
+
+ // ErrInvalidNumber is returned if a block's number doesn't equal it's parent's
+ // plus one.
+ ErrInvalidNumber = errors.New("invalid block number")
+)
diff --git a/consensus/ethash/algorithm.go b/consensus/ethash/algorithm.go
new file mode 100644
index 0000000..2b66c33
--- /dev/null
+++ b/consensus/ethash/algorithm.go
@@ -0,0 +1,1148 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package ethash
+
+import (
+ "encoding/binary"
+ "hash"
+ "math/big"
+ "reflect"
+ "runtime"
+ "sync"
+ "sync/atomic"
+ "time"
+ "unsafe"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/bitutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/log"
+ "golang.org/x/crypto/sha3"
+)
+
+const (
+ datasetInitBytes = 1 << 30 // Bytes in dataset at genesis
+ datasetGrowthBytes = 1 << 23 // Dataset growth per epoch
+ cacheInitBytes = 1 << 24 // Bytes in cache at genesis
+ cacheGrowthBytes = 1 << 17 // Cache growth per epoch
+ epochLength = 30000 // Blocks per epoch
+ mixBytes = 128 // Width of mix
+ hashBytes = 64 // Hash length in bytes
+ hashWords = 16 // Number of 32 bit ints in a hash
+ datasetParents = 256 // Number of parents of each dataset element
+ cacheRounds = 3 // Number of rounds in cache production
+ loopAccesses = 64 // Number of accesses in hashimoto loop
+)
+
+// cacheSize returns the size of the ethash verification cache that belongs to a certain
+// block number.
+func cacheSize(block uint64) uint64 {
+ epoch := int(block / epochLength)
+ if epoch < maxEpoch {
+ return cacheSizes[epoch]
+ }
+ return calcCacheSize(epoch)
+}
+
+// calcCacheSize calculates the cache size for epoch. The cache size grows linearly,
+// however, we always take the highest prime below the linearly growing threshold in order
+// to reduce the risk of accidental regularities leading to cyclic behavior.
+func calcCacheSize(epoch int) uint64 {
+ size := cacheInitBytes + cacheGrowthBytes*uint64(epoch) - hashBytes
+ for !new(big.Int).SetUint64(size / hashBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
+ size -= 2 * hashBytes
+ }
+ return size
+}
+
+// datasetSize returns the size of the ethash mining dataset that belongs to a certain
+// block number.
+func datasetSize(block uint64) uint64 {
+ epoch := int(block / epochLength)
+ if epoch < maxEpoch {
+ return datasetSizes[epoch]
+ }
+ return calcDatasetSize(epoch)
+}
+
+// calcDatasetSize calculates the dataset size for epoch. The dataset size grows linearly,
+// however, we always take the highest prime below the linearly growing threshold in order
+// to reduce the risk of accidental regularities leading to cyclic behavior.
+func calcDatasetSize(epoch int) uint64 {
+ size := datasetInitBytes + datasetGrowthBytes*uint64(epoch) - mixBytes
+ for !new(big.Int).SetUint64(size / mixBytes).ProbablyPrime(1) { // Always accurate for n < 2^64
+ size -= 2 * mixBytes
+ }
+ return size
+}
+
+// hasher is a repetitive hasher allowing the same hash data structures to be
+// reused between hash runs instead of requiring new ones to be created.
+type hasher func(dest []byte, data []byte)
+
+// makeHasher creates a repetitive hasher, allowing the same hash data structures to
+// be reused between hash runs instead of requiring new ones to be created. The returned
+// function is not thread safe!
+func makeHasher(h hash.Hash) hasher {
+ // sha3.state supports Read to get the sum, use it to avoid the overhead of Sum.
+ // Read alters the state but we reset the hash before every operation.
+ type readerHash interface {
+ hash.Hash
+ Read([]byte) (int, error)
+ }
+ rh, ok := h.(readerHash)
+ if !ok {
+ panic("can't find Read method on hash")
+ }
+ outputLen := rh.Size()
+ return func(dest []byte, data []byte) {
+ rh.Reset()
+ rh.Write(data)
+ rh.Read(dest[:outputLen])
+ }
+}
+
+// seedHash is the seed to use for generating a verification cache and the mining
+// dataset.
+func seedHash(block uint64) []byte {
+ seed := make([]byte, 32)
+ if block < epochLength {
+ return seed
+ }
+ keccak256 := makeHasher(sha3.NewLegacyKeccak256())
+ for i := 0; i < int(block/epochLength); i++ {
+ keccak256(seed, seed)
+ }
+ return seed
+}
+
+// generateCache creates a verification cache of a given size for an input seed.
+// The cache production process involves first sequentially filling up 32 MB of
+// memory, then performing two passes of Sergio Demian Lerner's RandMemoHash
+// algorithm from Strict Memory Hard Hashing Functions (2014). The output is a
+// set of 524288 64-byte values.
+// This method places the result into dest in machine byte order.
+func generateCache(dest []uint32, epoch uint64, seed []byte) {
+ // Print some debug logs to allow analysis on low end devices
+ logger := log.New("epoch", epoch)
+
+ start := time.Now()
+ defer func() {
+ elapsed := time.Since(start)
+
+ logFn := logger.Debug
+ if elapsed > 3*time.Second {
+ logFn = logger.Info
+ }
+ logFn("Generated ethash verification cache", "elapsed", common.PrettyDuration(elapsed))
+ }()
+ // Convert our destination slice to a byte buffer
+ header := *(*reflect.SliceHeader)(unsafe.Pointer(&dest))
+ header.Len *= 4
+ header.Cap *= 4
+ cache := *(*[]byte)(unsafe.Pointer(&header))
+
+ // Calculate the number of theoretical rows (we'll store in one buffer nonetheless)
+ size := uint64(len(cache))
+ rows := int(size) / hashBytes
+
+ // Start a monitoring goroutine to report progress on low end devices
+ var progress uint32
+
+ done := make(chan struct{})
+ defer close(done)
+
+ go func() {
+ for {
+ select {
+ case <-done:
+ return
+ case <-time.After(3 * time.Second):
+ logger.Info("Generating ethash verification cache", "percentage", atomic.LoadUint32(&progress)*100/uint32(rows)/4, "elapsed", common.PrettyDuration(time.Since(start)))
+ }
+ }
+ }()
+ // Create a hasher to reuse between invocations
+ keccak512 := makeHasher(sha3.NewLegacyKeccak512())
+
+ // Sequentially produce the initial dataset
+ keccak512(cache, seed)
+ for offset := uint64(hashBytes); offset < size; offset += hashBytes {
+ keccak512(cache[offset:], cache[offset-hashBytes:offset])
+ atomic.AddUint32(&progress, 1)
+ }
+ // Use a low-round version of randmemohash
+ temp := make([]byte, hashBytes)
+
+ for i := 0; i < cacheRounds; i++ {
+ for j := 0; j < rows; j++ {
+ var (
+ srcOff = ((j - 1 + rows) % rows) * hashBytes
+ dstOff = j * hashBytes
+ xorOff = (binary.LittleEndian.Uint32(cache[dstOff:]) % uint32(rows)) * hashBytes
+ )
+ bitutil.XORBytes(temp, cache[srcOff:srcOff+hashBytes], cache[xorOff:xorOff+hashBytes])
+ keccak512(cache[dstOff:], temp)
+
+ atomic.AddUint32(&progress, 1)
+ }
+ }
+ // Swap the byte order on big endian systems and return
+ if !isLittleEndian() {
+ swap(cache)
+ }
+}
+
+// swap changes the byte order of the buffer assuming a uint32 representation.
+func swap(buffer []byte) {
+ for i := 0; i < len(buffer); i += 4 {
+ binary.BigEndian.PutUint32(buffer[i:], binary.LittleEndian.Uint32(buffer[i:]))
+ }
+}
+
+// fnv is an algorithm inspired by the FNV hash, which in some cases is used as
+// a non-associative substitute for XOR. Note that we multiply the prime with
+// the full 32-bit input, in contrast with the FNV-1 spec which multiplies the
+// prime with one byte (octet) in turn.
+func fnv(a, b uint32) uint32 {
+ return a*0x01000193 ^ b
+}
+
+// fnvHash mixes in data into mix using the ethash fnv method.
+func fnvHash(mix []uint32, data []uint32) {
+ for i := 0; i < len(mix); i++ {
+ mix[i] = mix[i]*0x01000193 ^ data[i]
+ }
+}
+
+// generateDatasetItem combines data from 256 pseudorandomly selected cache nodes,
+// and hashes that to compute a single dataset node.
+func generateDatasetItem(cache []uint32, index uint32, keccak512 hasher) []byte {
+ // Calculate the number of theoretical rows (we use one buffer nonetheless)
+ rows := uint32(len(cache) / hashWords)
+
+ // Initialize the mix
+ mix := make([]byte, hashBytes)
+
+ binary.LittleEndian.PutUint32(mix, cache[(index%rows)*hashWords]^index)
+ for i := 1; i < hashWords; i++ {
+ binary.LittleEndian.PutUint32(mix[i*4:], cache[(index%rows)*hashWords+uint32(i)])
+ }
+ keccak512(mix, mix)
+
+ // Convert the mix to uint32s to avoid constant bit shifting
+ intMix := make([]uint32, hashWords)
+ for i := 0; i < len(intMix); i++ {
+ intMix[i] = binary.LittleEndian.Uint32(mix[i*4:])
+ }
+ // fnv it with a lot of random cache nodes based on index
+ for i := uint32(0); i < datasetParents; i++ {
+ parent := fnv(index^i, intMix[i%16]) % rows
+ fnvHash(intMix, cache[parent*hashWords:])
+ }
+ // Flatten the uint32 mix into a binary one and return
+ for i, val := range intMix {
+ binary.LittleEndian.PutUint32(mix[i*4:], val)
+ }
+ keccak512(mix, mix)
+ return mix
+}
+
+// generateDataset generates the entire ethash dataset for mining.
+// This method places the result into dest in machine byte order.
+func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
+ // Print some debug logs to allow analysis on low end devices
+ logger := log.New("epoch", epoch)
+
+ start := time.Now()
+ defer func() {
+ elapsed := time.Since(start)
+
+ logFn := logger.Debug
+ if elapsed > 3*time.Second {
+ logFn = logger.Info
+ }
+ logFn("Generated ethash verification cache", "elapsed", common.PrettyDuration(elapsed))
+ }()
+
+ // Figure out whether the bytes need to be swapped for the machine
+ swapped := !isLittleEndian()
+
+ // Convert our destination slice to a byte buffer
+ header := *(*reflect.SliceHeader)(unsafe.Pointer(&dest))
+ header.Len *= 4
+ header.Cap *= 4
+ dataset := *(*[]byte)(unsafe.Pointer(&header))
+
+ // Generate the dataset on many goroutines since it takes a while
+ threads := runtime.NumCPU()
+ size := uint64(len(dataset))
+
+ var pend sync.WaitGroup
+ pend.Add(threads)
+
+ var progress uint32
+ for i := 0; i < threads; i++ {
+ go func(id int) {
+ defer pend.Done()
+
+ // Create a hasher to reuse between invocations
+ keccak512 := makeHasher(sha3.NewLegacyKeccak512())
+
+ // Calculate the data segment this thread should generate
+ batch := uint32((size + hashBytes*uint64(threads) - 1) / (hashBytes * uint64(threads)))
+ first := uint32(id) * batch
+ limit := first + batch
+ if limit > uint32(size/hashBytes) {
+ limit = uint32(size / hashBytes)
+ }
+ // Calculate the dataset segment
+ percent := uint32(size / hashBytes / 100)
+ for index := first; index < limit; index++ {
+ item := generateDatasetItem(cache, index, keccak512)
+ if swapped {
+ swap(item)
+ }
+ copy(dataset[index*hashBytes:], item)
+
+ if status := atomic.AddUint32(&progress, 1); status%percent == 0 {
+ logger.Info("Generating DAG in progress", "percentage", uint64(status*100)/(size/hashBytes), "elapsed", common.PrettyDuration(time.Since(start)))
+ }
+ }
+ }(i)
+ }
+ // Wait for all the generators to finish and return
+ pend.Wait()
+}
+
+// hashimoto aggregates data from the full dataset in order to produce our final
+// value for a particular header hash and nonce.
+func hashimoto(hash []byte, nonce uint64, size uint64, lookup func(index uint32) []uint32) ([]byte, []byte) {
+ // Calculate the number of theoretical rows (we use one buffer nonetheless)
+ rows := uint32(size / mixBytes)
+
+ // Combine header+nonce into a 64 byte seed
+ seed := make([]byte, 40)
+ copy(seed, hash)
+ binary.LittleEndian.PutUint64(seed[32:], nonce)
+
+ seed = crypto.Keccak512(seed)
+ seedHead := binary.LittleEndian.Uint32(seed)
+
+ // Start the mix with replicated seed
+ mix := make([]uint32, mixBytes/4)
+ for i := 0; i < len(mix); i++ {
+ mix[i] = binary.LittleEndian.Uint32(seed[i%16*4:])
+ }
+ // Mix in random dataset nodes
+ temp := make([]uint32, len(mix))
+
+ for i := 0; i < loopAccesses; i++ {
+ parent := fnv(uint32(i)^seedHead, mix[i%len(mix)]) % rows
+ for j := uint32(0); j < mixBytes/hashBytes; j++ {
+ copy(temp[j*hashWords:], lookup(2*parent+j))
+ }
+ fnvHash(mix, temp)
+ }
+ // Compress mix
+ for i := 0; i < len(mix); i += 4 {
+ mix[i/4] = fnv(fnv(fnv(mix[i], mix[i+1]), mix[i+2]), mix[i+3])
+ }
+ mix = mix[:len(mix)/4]
+
+ digest := make([]byte, common.HashLength)
+ for i, val := range mix {
+ binary.LittleEndian.PutUint32(digest[i*4:], val)
+ }
+ return digest, crypto.Keccak256(append(seed, digest...))
+}
+
+// hashimotoLight aggregates data from the full dataset (using only a small
+// in-memory cache) in order to produce our final value for a particular header
+// hash and nonce.
+func hashimotoLight(size uint64, cache []uint32, hash []byte, nonce uint64) ([]byte, []byte) {
+ keccak512 := makeHasher(sha3.NewLegacyKeccak512())
+
+ lookup := func(index uint32) []uint32 {
+ rawData := generateDatasetItem(cache, index, keccak512)
+
+ data := make([]uint32, len(rawData)/4)
+ for i := 0; i < len(data); i++ {
+ data[i] = binary.LittleEndian.Uint32(rawData[i*4:])
+ }
+ return data
+ }
+ return hashimoto(hash, nonce, size, lookup)
+}
+
+// hashimotoFull aggregates data from the full dataset (using the full in-memory
+// dataset) in order to produce our final value for a particular header hash and
+// nonce.
+func hashimotoFull(dataset []uint32, hash []byte, nonce uint64) ([]byte, []byte) {
+ lookup := func(index uint32) []uint32 {
+ offset := index * hashWords
+ return dataset[offset : offset+hashWords]
+ }
+ return hashimoto(hash, nonce, uint64(len(dataset))*4, lookup)
+}
+
+const maxEpoch = 2048
+
+// datasetSizes is a lookup table for the ethash dataset size for the first 2048
+// epochs (i.e. 61440000 blocks).
+var datasetSizes = [maxEpoch]uint64{
+ 1073739904, 1082130304, 1090514816, 1098906752, 1107293056,
+ 1115684224, 1124070016, 1132461952, 1140849536, 1149232768,
+ 1157627776, 1166013824, 1174404736, 1182786944, 1191180416,
+ 1199568512, 1207958912, 1216345216, 1224732032, 1233124736,
+ 1241513344, 1249902464, 1258290304, 1266673792, 1275067264,
+ 1283453312, 1291844992, 1300234112, 1308619904, 1317010048,
+ 1325397376, 1333787776, 1342176128, 1350561664, 1358954368,
+ 1367339392, 1375731584, 1384118144, 1392507008, 1400897408,
+ 1409284736, 1417673344, 1426062464, 1434451072, 1442839168,
+ 1451229056, 1459615616, 1468006016, 1476394112, 1484782976,
+ 1493171584, 1501559168, 1509948032, 1518337664, 1526726528,
+ 1535114624, 1543503488, 1551892096, 1560278656, 1568669056,
+ 1577056384, 1585446272, 1593831296, 1602219392, 1610610304,
+ 1619000192, 1627386752, 1635773824, 1644164224, 1652555648,
+ 1660943488, 1669332608, 1677721216, 1686109312, 1694497664,
+ 1702886272, 1711274624, 1719661184, 1728047744, 1736434816,
+ 1744829056, 1753218944, 1761606272, 1769995904, 1778382464,
+ 1786772864, 1795157888, 1803550592, 1811937664, 1820327552,
+ 1828711552, 1837102976, 1845488768, 1853879936, 1862269312,
+ 1870656896, 1879048064, 1887431552, 1895825024, 1904212096,
+ 1912601216, 1920988544, 1929379456, 1937765504, 1946156672,
+ 1954543232, 1962932096, 1971321728, 1979707264, 1988093056,
+ 1996487552, 2004874624, 2013262208, 2021653888, 2030039936,
+ 2038430848, 2046819968, 2055208576, 2063596672, 2071981952,
+ 2080373632, 2088762752, 2097149056, 2105539712, 2113928576,
+ 2122315136, 2130700672, 2139092608, 2147483264, 2155872128,
+ 2164257664, 2172642176, 2181035392, 2189426048, 2197814912,
+ 2206203008, 2214587264, 2222979712, 2231367808, 2239758208,
+ 2248145024, 2256527744, 2264922752, 2273312128, 2281701248,
+ 2290086272, 2298476672, 2306867072, 2315251072, 2323639168,
+ 2332032128, 2340420224, 2348808064, 2357196416, 2365580416,
+ 2373966976, 2382363008, 2390748544, 2399139968, 2407530368,
+ 2415918976, 2424307328, 2432695424, 2441084288, 2449472384,
+ 2457861248, 2466247808, 2474637184, 2483026816, 2491414144,
+ 2499803776, 2508191872, 2516582272, 2524970368, 2533359232,
+ 2541743488, 2550134144, 2558525056, 2566913408, 2575301504,
+ 2583686528, 2592073856, 2600467328, 2608856192, 2617240448,
+ 2625631616, 2634022016, 2642407552, 2650796416, 2659188352,
+ 2667574912, 2675965312, 2684352896, 2692738688, 2701130624,
+ 2709518464, 2717907328, 2726293376, 2734685056, 2743073152,
+ 2751462016, 2759851648, 2768232832, 2776625536, 2785017728,
+ 2793401984, 2801794432, 2810182016, 2818571648, 2826959488,
+ 2835349376, 2843734144, 2852121472, 2860514432, 2868900992,
+ 2877286784, 2885676928, 2894069632, 2902451584, 2910843008,
+ 2919234688, 2927622784, 2936011648, 2944400768, 2952789376,
+ 2961177728, 2969565568, 2977951616, 2986338944, 2994731392,
+ 3003120256, 3011508352, 3019895936, 3028287104, 3036675968,
+ 3045063808, 3053452928, 3061837696, 3070228352, 3078615424,
+ 3087003776, 3095394944, 3103782272, 3112173184, 3120562048,
+ 3128944768, 3137339264, 3145725056, 3154109312, 3162505088,
+ 3170893184, 3179280256, 3187669376, 3196056704, 3204445568,
+ 3212836736, 3221224064, 3229612928, 3238002304, 3246391168,
+ 3254778496, 3263165824, 3271556224, 3279944576, 3288332416,
+ 3296719232, 3305110912, 3313500032, 3321887104, 3330273152,
+ 3338658944, 3347053184, 3355440512, 3363827072, 3372220288,
+ 3380608384, 3388997504, 3397384576, 3405774208, 3414163072,
+ 3422551936, 3430937984, 3439328384, 3447714176, 3456104576,
+ 3464493952, 3472883584, 3481268864, 3489655168, 3498048896,
+ 3506434432, 3514826368, 3523213952, 3531603584, 3539987072,
+ 3548380288, 3556763264, 3565157248, 3573545344, 3581934464,
+ 3590324096, 3598712704, 3607098752, 3615488384, 3623877248,
+ 3632265856, 3640646528, 3649043584, 3657430144, 3665821568,
+ 3674207872, 3682597504, 3690984832, 3699367808, 3707764352,
+ 3716152448, 3724541056, 3732925568, 3741318016, 3749706368,
+ 3758091136, 3766481536, 3774872704, 3783260032, 3791650432,
+ 3800036224, 3808427648, 3816815488, 3825204608, 3833592704,
+ 3841981568, 3850370432, 3858755968, 3867147904, 3875536256,
+ 3883920512, 3892313728, 3900702592, 3909087872, 3917478784,
+ 3925868416, 3934256512, 3942645376, 3951032192, 3959422336,
+ 3967809152, 3976200064, 3984588416, 3992974976, 4001363584,
+ 4009751168, 4018141312, 4026530432, 4034911616, 4043308928,
+ 4051695488, 4060084352, 4068472448, 4076862848, 4085249408,
+ 4093640576, 4102028416, 4110413696, 4118805632, 4127194496,
+ 4135583104, 4143971968, 4152360832, 4160746112, 4169135744,
+ 4177525888, 4185912704, 4194303616, 4202691968, 4211076736,
+ 4219463552, 4227855488, 4236246656, 4244633728, 4253022848,
+ 4261412224, 4269799808, 4278184832, 4286578048, 4294962304,
+ 4303349632, 4311743104, 4320130432, 4328521088, 4336909184,
+ 4345295488, 4353687424, 4362073472, 4370458496, 4378852736,
+ 4387238528, 4395630208, 4404019072, 4412407424, 4420790656,
+ 4429182848, 4437571456, 4445962112, 4454344064, 4462738048,
+ 4471119232, 4479516544, 4487904128, 4496289664, 4504682368,
+ 4513068416, 4521459584, 4529846144, 4538232704, 4546619776,
+ 4555010176, 4563402112, 4571790208, 4580174464, 4588567936,
+ 4596957056, 4605344896, 4613734016, 4622119808, 4630511488,
+ 4638898816, 4647287936, 4655675264, 4664065664, 4672451968,
+ 4680842624, 4689231488, 4697620352, 4706007424, 4714397056,
+ 4722786176, 4731173248, 4739562368, 4747951744, 4756340608,
+ 4764727936, 4773114496, 4781504384, 4789894784, 4798283648,
+ 4806667648, 4815059584, 4823449472, 4831835776, 4840226176,
+ 4848612224, 4857003392, 4865391488, 4873780096, 4882169728,
+ 4890557312, 4898946944, 4907333248, 4915722368, 4924110976,
+ 4932499328, 4940889728, 4949276032, 4957666432, 4966054784,
+ 4974438016, 4982831488, 4991221376, 4999607168, 5007998848,
+ 5016386432, 5024763776, 5033164672, 5041544576, 5049941888,
+ 5058329728, 5066717056, 5075107456, 5083494272, 5091883904,
+ 5100273536, 5108662144, 5117048192, 5125436032, 5133827456,
+ 5142215296, 5150605184, 5158993024, 5167382144, 5175769472,
+ 5184157568, 5192543872, 5200936064, 5209324928, 5217711232,
+ 5226102656, 5234490496, 5242877312, 5251263872, 5259654016,
+ 5268040832, 5276434304, 5284819328, 5293209728, 5301598592,
+ 5309986688, 5318374784, 5326764416, 5335151488, 5343542144,
+ 5351929472, 5360319872, 5368706944, 5377096576, 5385484928,
+ 5393871232, 5402263424, 5410650496, 5419040384, 5427426944,
+ 5435816576, 5444205952, 5452594816, 5460981376, 5469367936,
+ 5477760896, 5486148736, 5494536832, 5502925952, 5511315328,
+ 5519703424, 5528089984, 5536481152, 5544869504, 5553256064,
+ 5561645696, 5570032768, 5578423936, 5586811264, 5595193216,
+ 5603585408, 5611972736, 5620366208, 5628750464, 5637143936,
+ 5645528192, 5653921408, 5662310272, 5670694784, 5679082624,
+ 5687474048, 5695864448, 5704251008, 5712641408, 5721030272,
+ 5729416832, 5737806208, 5746194304, 5754583936, 5762969984,
+ 5771358592, 5779748224, 5788137856, 5796527488, 5804911232,
+ 5813300608, 5821692544, 5830082176, 5838468992, 5846855552,
+ 5855247488, 5863636096, 5872024448, 5880411008, 5888799872,
+ 5897186432, 5905576832, 5913966976, 5922352768, 5930744704,
+ 5939132288, 5947522432, 5955911296, 5964299392, 5972688256,
+ 5981074304, 5989465472, 5997851008, 6006241408, 6014627968,
+ 6023015552, 6031408256, 6039796096, 6048185216, 6056574848,
+ 6064963456, 6073351808, 6081736064, 6090128768, 6098517632,
+ 6106906496, 6115289216, 6123680896, 6132070016, 6140459648,
+ 6148849024, 6157237376, 6165624704, 6174009728, 6182403712,
+ 6190792064, 6199176064, 6207569792, 6215952256, 6224345216,
+ 6232732544, 6241124224, 6249510272, 6257899136, 6266287744,
+ 6274676864, 6283065728, 6291454336, 6299843456, 6308232064,
+ 6316620928, 6325006208, 6333395584, 6341784704, 6350174848,
+ 6358562176, 6366951296, 6375337856, 6383729536, 6392119168,
+ 6400504192, 6408895616, 6417283456, 6425673344, 6434059136,
+ 6442444672, 6450837376, 6459223424, 6467613056, 6476004224,
+ 6484393088, 6492781952, 6501170048, 6509555072, 6517947008,
+ 6526336384, 6534725504, 6543112832, 6551500672, 6559888768,
+ 6568278656, 6576662912, 6585055616, 6593443456, 6601834112,
+ 6610219648, 6618610304, 6626999168, 6635385472, 6643777408,
+ 6652164224, 6660552832, 6668941952, 6677330048, 6685719424,
+ 6694107776, 6702493568, 6710882176, 6719274112, 6727662976,
+ 6736052096, 6744437632, 6752825984, 6761213824, 6769604224,
+ 6777993856, 6786383488, 6794770816, 6803158144, 6811549312,
+ 6819937664, 6828326528, 6836706176, 6845101696, 6853491328,
+ 6861880448, 6870269312, 6878655104, 6887046272, 6895433344,
+ 6903822208, 6912212864, 6920596864, 6928988288, 6937377152,
+ 6945764992, 6954149248, 6962544256, 6970928768, 6979317376,
+ 6987709312, 6996093824, 7004487296, 7012875392, 7021258624,
+ 7029652352, 7038038912, 7046427776, 7054818944, 7063207808,
+ 7071595136, 7079980928, 7088372608, 7096759424, 7105149824,
+ 7113536896, 7121928064, 7130315392, 7138699648, 7147092352,
+ 7155479168, 7163865728, 7172249984, 7180648064, 7189036672,
+ 7197424768, 7205810816, 7214196608, 7222589824, 7230975104,
+ 7239367552, 7247755904, 7256145536, 7264533376, 7272921472,
+ 7281308032, 7289694848, 7298088832, 7306471808, 7314864512,
+ 7323253888, 7331643008, 7340029568, 7348419712, 7356808832,
+ 7365196672, 7373585792, 7381973888, 7390362752, 7398750592,
+ 7407138944, 7415528576, 7423915648, 7432302208, 7440690304,
+ 7449080192, 7457472128, 7465860992, 7474249088, 7482635648,
+ 7491023744, 7499412608, 7507803008, 7516192384, 7524579968,
+ 7532967296, 7541358464, 7549745792, 7558134656, 7566524032,
+ 7574912896, 7583300992, 7591690112, 7600075136, 7608466816,
+ 7616854912, 7625244544, 7633629824, 7642020992, 7650410368,
+ 7658794112, 7667187328, 7675574912, 7683961984, 7692349568,
+ 7700739712, 7709130368, 7717519232, 7725905536, 7734295424,
+ 7742683264, 7751069056, 7759457408, 7767849088, 7776238208,
+ 7784626816, 7793014912, 7801405312, 7809792128, 7818179968,
+ 7826571136, 7834957184, 7843347328, 7851732352, 7860124544,
+ 7868512384, 7876902016, 7885287808, 7893679744, 7902067072,
+ 7910455936, 7918844288, 7927230848, 7935622784, 7944009344,
+ 7952400256, 7960786048, 7969176704, 7977565312, 7985953408,
+ 7994339968, 8002730368, 8011119488, 8019508096, 8027896192,
+ 8036285056, 8044674688, 8053062272, 8061448832, 8069838464,
+ 8078227328, 8086616704, 8095006592, 8103393664, 8111783552,
+ 8120171392, 8128560256, 8136949376, 8145336704, 8153726848,
+ 8162114944, 8170503296, 8178891904, 8187280768, 8195669632,
+ 8204058496, 8212444544, 8220834176, 8229222272, 8237612672,
+ 8246000768, 8254389376, 8262775168, 8271167104, 8279553664,
+ 8287944064, 8296333184, 8304715136, 8313108352, 8321497984,
+ 8329885568, 8338274432, 8346663296, 8355052928, 8363441536,
+ 8371828352, 8380217984, 8388606592, 8396996224, 8405384576,
+ 8413772672, 8422161536, 8430549376, 8438939008, 8447326592,
+ 8455715456, 8464104832, 8472492928, 8480882048, 8489270656,
+ 8497659776, 8506045312, 8514434944, 8522823808, 8531208832,
+ 8539602304, 8547990656, 8556378752, 8564768384, 8573154176,
+ 8581542784, 8589933952, 8598322816, 8606705024, 8615099264,
+ 8623487872, 8631876992, 8640264064, 8648653952, 8657040256,
+ 8665430656, 8673820544, 8682209152, 8690592128, 8698977152,
+ 8707374464, 8715763328, 8724151424, 8732540032, 8740928384,
+ 8749315712, 8757704576, 8766089344, 8774480768, 8782871936,
+ 8791260032, 8799645824, 8808034432, 8816426368, 8824812928,
+ 8833199488, 8841591424, 8849976448, 8858366336, 8866757248,
+ 8875147136, 8883532928, 8891923328, 8900306816, 8908700288,
+ 8917088384, 8925478784, 8933867392, 8942250368, 8950644608,
+ 8959032704, 8967420544, 8975809664, 8984197504, 8992584064,
+ 9000976256, 9009362048, 9017752448, 9026141312, 9034530688,
+ 9042917504, 9051307904, 9059694208, 9068084864, 9076471424,
+ 9084861824, 9093250688, 9101638528, 9110027648, 9118416512,
+ 9126803584, 9135188096, 9143581312, 9151969664, 9160356224,
+ 9168747136, 9177134464, 9185525632, 9193910144, 9202302848,
+ 9210690688, 9219079552, 9227465344, 9235854464, 9244244864,
+ 9252633472, 9261021824, 9269411456, 9277799296, 9286188928,
+ 9294574208, 9302965888, 9311351936, 9319740032, 9328131968,
+ 9336516736, 9344907392, 9353296768, 9361685888, 9370074752,
+ 9378463616, 9386849408, 9395239808, 9403629184, 9412016512,
+ 9420405376, 9428795008, 9437181568, 9445570688, 9453960832,
+ 9462346624, 9470738048, 9479121536, 9487515008, 9495903616,
+ 9504289664, 9512678528, 9521067904, 9529456256, 9537843584,
+ 9546233728, 9554621312, 9563011456, 9571398784, 9579788672,
+ 9588178304, 9596567168, 9604954496, 9613343104, 9621732992,
+ 9630121856, 9638508416, 9646898816, 9655283584, 9663675776,
+ 9672061312, 9680449664, 9688840064, 9697230464, 9705617536,
+ 9714003584, 9722393984, 9730772608, 9739172224, 9747561088,
+ 9755945344, 9764338816, 9772726144, 9781116544, 9789503872,
+ 9797892992, 9806282624, 9814670464, 9823056512, 9831439232,
+ 9839833984, 9848224384, 9856613504, 9865000576, 9873391232,
+ 9881772416, 9890162816, 9898556288, 9906940544, 9915333248,
+ 9923721088, 9932108672, 9940496512, 9948888448, 9957276544,
+ 9965666176, 9974048384, 9982441088, 9990830464, 9999219584,
+ 10007602816, 10015996544, 10024385152, 10032774016, 10041163648,
+ 10049548928, 10057940096, 10066329472, 10074717824, 10083105152,
+ 10091495296, 10099878784, 10108272256, 10116660608, 10125049216,
+ 10133437312, 10141825664, 10150213504, 10158601088, 10166991232,
+ 10175378816, 10183766144, 10192157312, 10200545408, 10208935552,
+ 10217322112, 10225712768, 10234099328, 10242489472, 10250876032,
+ 10259264896, 10267656064, 10276042624, 10284429184, 10292820352,
+ 10301209472, 10309598848, 10317987712, 10326375296, 10334763392,
+ 10343153536, 10351541632, 10359930752, 10368318592, 10376707456,
+ 10385096576, 10393484672, 10401867136, 10410262144, 10418647424,
+ 10427039104, 10435425664, 10443810176, 10452203648, 10460589952,
+ 10468982144, 10477369472, 10485759104, 10494147712, 10502533504,
+ 10510923392, 10519313536, 10527702656, 10536091264, 10544478592,
+ 10552867712, 10561255808, 10569642368, 10578032768, 10586423168,
+ 10594805632, 10603200128, 10611588992, 10619976064, 10628361344,
+ 10636754048, 10645143424, 10653531776, 10661920384, 10670307968,
+ 10678696832, 10687086464, 10695475072, 10703863168, 10712246144,
+ 10720639616, 10729026688, 10737414784, 10745806208, 10754190976,
+ 10762581376, 10770971264, 10779356288, 10787747456, 10796135552,
+ 10804525184, 10812915584, 10821301888, 10829692288, 10838078336,
+ 10846469248, 10854858368, 10863247232, 10871631488, 10880023424,
+ 10888412032, 10896799616, 10905188992, 10913574016, 10921964672,
+ 10930352768, 10938742912, 10947132544, 10955518592, 10963909504,
+ 10972298368, 10980687488, 10989074816, 10997462912, 11005851776,
+ 11014241152, 11022627712, 11031017344, 11039403904, 11047793024,
+ 11056184704, 11064570752, 11072960896, 11081343872, 11089737856,
+ 11098128256, 11106514816, 11114904448, 11123293568, 11131680128,
+ 11140065152, 11148458368, 11156845696, 11165236864, 11173624192,
+ 11182013824, 11190402688, 11198790784, 11207179136, 11215568768,
+ 11223957376, 11232345728, 11240734592, 11249122688, 11257511296,
+ 11265899648, 11274285952, 11282675584, 11291065472, 11299452544,
+ 11307842432, 11316231296, 11324616832, 11333009024, 11341395584,
+ 11349782656, 11358172288, 11366560384, 11374950016, 11383339648,
+ 11391721856, 11400117376, 11408504192, 11416893568, 11425283456,
+ 11433671552, 11442061184, 11450444672, 11458837888, 11467226752,
+ 11475611776, 11484003968, 11492392064, 11500780672, 11509169024,
+ 11517550976, 11525944448, 11534335616, 11542724224, 11551111808,
+ 11559500672, 11567890304, 11576277376, 11584667008, 11593056128,
+ 11601443456, 11609830016, 11618221952, 11626607488, 11634995072,
+ 11643387776, 11651775104, 11660161664, 11668552576, 11676940928,
+ 11685330304, 11693718656, 11702106496, 11710496128, 11718882688,
+ 11727273088, 11735660416, 11744050048, 11752437376, 11760824704,
+ 11769216128, 11777604736, 11785991296, 11794381952, 11802770048,
+ 11811157888, 11819548544, 11827932544, 11836324736, 11844713344,
+ 11853100928, 11861486464, 11869879936, 11878268032, 11886656896,
+ 11895044992, 11903433088, 11911822976, 11920210816, 11928600448,
+ 11936987264, 11945375872, 11953761152, 11962151296, 11970543488,
+ 11978928512, 11987320448, 11995708288, 12004095104, 12012486272,
+ 12020875136, 12029255552, 12037652096, 12046039168, 12054429568,
+ 12062813824, 12071206528, 12079594624, 12087983744, 12096371072,
+ 12104759936, 12113147264, 12121534592, 12129924992, 12138314624,
+ 12146703232, 12155091584, 12163481216, 12171864704, 12180255872,
+ 12188643968, 12197034112, 12205424512, 12213811328, 12222199424,
+ 12230590336, 12238977664, 12247365248, 12255755392, 12264143488,
+ 12272531584, 12280920448, 12289309568, 12297694592, 12306086528,
+ 12314475392, 12322865024, 12331253632, 12339640448, 12348029312,
+ 12356418944, 12364805248, 12373196672, 12381580928, 12389969024,
+ 12398357632, 12406750592, 12415138432, 12423527552, 12431916416,
+ 12440304512, 12448692352, 12457081216, 12465467776, 12473859968,
+ 12482245504, 12490636672, 12499025536, 12507411584, 12515801728,
+ 12524190592, 12532577152, 12540966272, 12549354368, 12557743232,
+ 12566129536, 12574523264, 12582911872, 12591299456, 12599688064,
+ 12608074624, 12616463488, 12624845696, 12633239936, 12641631616,
+ 12650019968, 12658407296, 12666795136, 12675183232, 12683574656,
+ 12691960192, 12700350592, 12708740224, 12717128576, 12725515904,
+ 12733906816, 12742295168, 12750680192, 12759071872, 12767460736,
+ 12775848832, 12784236928, 12792626816, 12801014656, 12809404288,
+ 12817789312, 12826181504, 12834568832, 12842954624, 12851345792,
+ 12859732352, 12868122496, 12876512128, 12884901248, 12893289088,
+ 12901672832, 12910067584, 12918455168, 12926842496, 12935232896,
+ 12943620736, 12952009856, 12960396928, 12968786816, 12977176192,
+ 12985563776, 12993951104, 13002341504, 13010730368, 13019115392,
+ 13027506304, 13035895168, 13044272512, 13052673152, 13061062528,
+ 13069446272, 13077838976, 13086227072, 13094613632, 13103000192,
+ 13111393664, 13119782528, 13128157568, 13136559232, 13144945024,
+ 13153329536, 13161724288, 13170111872, 13178502784, 13186884736,
+ 13195279744, 13203667072, 13212057472, 13220445824, 13228832128,
+ 13237221248, 13245610624, 13254000512, 13262388352, 13270777472,
+ 13279166336, 13287553408, 13295943296, 13304331904, 13312719488,
+ 13321108096, 13329494656, 13337885824, 13346274944, 13354663808,
+ 13363051136, 13371439232, 13379825024, 13388210816, 13396605056,
+ 13404995456, 13413380224, 13421771392, 13430159744, 13438546048,
+ 13446937216, 13455326848, 13463708288, 13472103808, 13480492672,
+ 13488875648, 13497269888, 13505657728, 13514045312, 13522435712,
+ 13530824576, 13539210112, 13547599232, 13555989376, 13564379008,
+ 13572766336, 13581154432, 13589544832, 13597932928, 13606320512,
+ 13614710656, 13623097472, 13631477632, 13639874944, 13648264064,
+ 13656652928, 13665041792, 13673430656, 13681818496, 13690207616,
+ 13698595712, 13706982272, 13715373184, 13723762048, 13732150144,
+ 13740536704, 13748926592, 13757316224, 13765700992, 13774090112,
+ 13782477952, 13790869376, 13799259008, 13807647872, 13816036736,
+ 13824425344, 13832814208, 13841202304, 13849591424, 13857978752,
+ 13866368896, 13874754688, 13883145344, 13891533184, 13899919232,
+ 13908311168, 13916692096, 13925085056, 13933473152, 13941866368,
+ 13950253696, 13958643584, 13967032192, 13975417216, 13983807616,
+ 13992197504, 14000582272, 14008973696, 14017363072, 14025752192,
+ 14034137984, 14042528384, 14050918016, 14059301504, 14067691648,
+ 14076083584, 14084470144, 14092852352, 14101249664, 14109635968,
+ 14118024832, 14126407552, 14134804352, 14143188608, 14151577984,
+ 14159968384, 14168357248, 14176741504, 14185127296, 14193521024,
+ 14201911424, 14210301824, 14218685056, 14227067264, 14235467392,
+ 14243855488, 14252243072, 14260630144, 14269021568, 14277409408,
+ 14285799296, 14294187904, 14302571392, 14310961792, 14319353728,
+ 14327738752, 14336130944, 14344518784, 14352906368, 14361296512,
+ 14369685376, 14378071424, 14386462592, 14394848128, 14403230848,
+ 14411627392, 14420013952, 14428402304, 14436793472, 14445181568,
+ 14453569664, 14461959808, 14470347904, 14478737024, 14487122816,
+ 14495511424, 14503901824, 14512291712, 14520677504, 14529064832,
+ 14537456768, 14545845632, 14554234496, 14562618496, 14571011456,
+ 14579398784, 14587789184, 14596172672, 14604564608, 14612953984,
+ 14621341312, 14629724288, 14638120832, 14646503296, 14654897536,
+ 14663284864, 14671675264, 14680061056, 14688447616, 14696835968,
+ 14705228416, 14713616768, 14722003328, 14730392192, 14738784128,
+ 14747172736, 14755561088, 14763947648, 14772336512, 14780725376,
+ 14789110144, 14797499776, 14805892736, 14814276992, 14822670208,
+ 14831056256, 14839444352, 14847836032, 14856222848, 14864612992,
+ 14872997504, 14881388672, 14889775744, 14898165376, 14906553472,
+ 14914944896, 14923329664, 14931721856, 14940109696, 14948497024,
+ 14956887424, 14965276544, 14973663616, 14982053248, 14990439808,
+ 14998830976, 15007216768, 15015605888, 15023995264, 15032385152,
+ 15040768384, 15049154944, 15057549184, 15065939072, 15074328448,
+ 15082715008, 15091104128, 15099493504, 15107879296, 15116269184,
+ 15124659584, 15133042304, 15141431936, 15149824384, 15158214272,
+ 15166602368, 15174991232, 15183378304, 15191760512, 15200154496,
+ 15208542592, 15216931712, 15225323392, 15233708416, 15242098048,
+ 15250489216, 15258875264, 15267265408, 15275654528, 15284043136,
+ 15292431488, 15300819584, 15309208192, 15317596544, 15325986176,
+ 15334374784, 15342763648, 15351151744, 15359540608, 15367929728,
+ 15376318336, 15384706432, 15393092992, 15401481856, 15409869952,
+ 15418258816, 15426649984, 15435037568, 15443425664, 15451815296,
+ 15460203392, 15468589184, 15476979328, 15485369216, 15493755776,
+ 15502146944, 15510534272, 15518924416, 15527311232, 15535699072,
+ 15544089472, 15552478336, 15560866688, 15569254528, 15577642624,
+ 15586031488, 15594419072, 15602809472, 15611199104, 15619586432,
+ 15627975296, 15636364928, 15644753792, 15653141888, 15661529216,
+ 15669918848, 15678305152, 15686696576, 15695083136, 15703474048,
+ 15711861632, 15720251264, 15728636288, 15737027456, 15745417088,
+ 15753804928, 15762194048, 15770582656, 15778971008, 15787358336,
+ 15795747712, 15804132224, 15812523392, 15820909696, 15829300096,
+ 15837691264, 15846071936, 15854466944, 15862855808, 15871244672,
+ 15879634816, 15888020608, 15896409728, 15904799104, 15913185152,
+ 15921577088, 15929966464, 15938354816, 15946743424, 15955129472,
+ 15963519872, 15971907968, 15980296064, 15988684928, 15997073024,
+ 16005460864, 16013851264, 16022241152, 16030629248, 16039012736,
+ 16047406976, 16055794816, 16064181376, 16072571264, 16080957824,
+ 16089346688, 16097737856, 16106125184, 16114514816, 16122904192,
+ 16131292544, 16139678848, 16148066944, 16156453504, 16164839552,
+ 16173236096, 16181623424, 16190012032, 16198401152, 16206790528,
+ 16215177344, 16223567744, 16231956352, 16240344704, 16248731008,
+ 16257117824, 16265504384, 16273898624, 16282281856, 16290668672,
+ 16299064192, 16307449216, 16315842176, 16324230016, 16332613504,
+ 16341006464, 16349394304, 16357783168, 16366172288, 16374561664,
+ 16382951296, 16391337856, 16399726208, 16408116352, 16416505472,
+ 16424892032, 16433282176, 16441668224, 16450058624, 16458448768,
+ 16466836864, 16475224448, 16483613056, 16492001408, 16500391808,
+ 16508779648, 16517166976, 16525555328, 16533944192, 16542330752,
+ 16550719616, 16559110528, 16567497088, 16575888512, 16584274816,
+ 16592665472, 16601051008, 16609442944, 16617832064, 16626218624,
+ 16634607488, 16642996096, 16651385728, 16659773824, 16668163712,
+ 16676552576, 16684938112, 16693328768, 16701718144, 16710095488,
+ 16718492288, 16726883968, 16735272832, 16743661184, 16752049792,
+ 16760436608, 16768827008, 16777214336, 16785599104, 16793992832,
+ 16802381696, 16810768768, 16819151744, 16827542656, 16835934848,
+ 16844323712, 16852711552, 16861101952, 16869489536, 16877876864,
+ 16886265728, 16894653056, 16903044736, 16911431296, 16919821696,
+ 16928207488, 16936592768, 16944987776, 16953375616, 16961763968,
+ 16970152832, 16978540928, 16986929536, 16995319168, 17003704448,
+ 17012096896, 17020481152, 17028870784, 17037262208, 17045649536,
+ 17054039936, 17062426496, 17070814336, 17079205504, 17087592064,
+ 17095978112, 17104369024, 17112759424, 17121147776, 17129536384,
+ 17137926016, 17146314368, 17154700928, 17163089792, 17171480192,
+ 17179864192, 17188256896, 17196644992, 17205033856, 17213423488,
+ 17221811072, 17230198912, 17238588032, 17246976896, 17255360384,
+ 17263754624, 17272143232, 17280530048, 17288918912, 17297309312,
+ 17305696384, 17314085504, 17322475136, 17330863744, 17339252096,
+ 17347640192, 17356026496, 17364413824, 17372796544, 17381190016,
+ 17389583488, 17397972608, 17406360704, 17414748544, 17423135872,
+ 17431527296, 17439915904, 17448303232, 17456691584, 17465081728,
+ 17473468288, 17481857408, 17490247552, 17498635904, 17507022464,
+ 17515409024, 17523801728, 17532189824, 17540577664, 17548966016,
+ 17557353344, 17565741184, 17574131584, 17582519168, 17590907008,
+ 17599296128, 17607687808, 17616076672, 17624455808, 17632852352,
+ 17641238656, 17649630848, 17658018944, 17666403968, 17674794112,
+ 17683178368, 17691573376, 17699962496, 17708350592, 17716739968,
+ 17725126528, 17733517184, 17741898112, 17750293888, 17758673024,
+ 17767070336, 17775458432, 17783848832, 17792236928, 17800625536,
+ 17809012352, 17817402752, 17825785984, 17834178944, 17842563968,
+ 17850955648, 17859344512, 17867732864, 17876119424, 17884511872,
+ 17892900224, 17901287296, 17909677696, 17918058112, 17926451072,
+ 17934843776, 17943230848, 17951609216, 17960008576, 17968397696,
+ 17976784256, 17985175424, 17993564032, 18001952128, 18010339712,
+ 18018728576, 18027116672, 18035503232, 18043894144, 18052283264,
+ 18060672128, 18069056384, 18077449856, 18085837184, 18094225792,
+ 18102613376, 18111004544, 18119388544, 18127781248, 18136170368,
+ 18144558976, 18152947328, 18161336192, 18169724288, 18178108544,
+ 18186498944, 18194886784, 18203275648, 18211666048, 18220048768,
+ 18228444544, 18236833408, 18245220736}
+
+// cacheSizes is a lookup table for the ethash verification cache size for the
+// first 2048 epochs (i.e. 61440000 blocks).
+var cacheSizes = [maxEpoch]uint64{
+ 16776896, 16907456, 17039296, 17170112, 17301056, 17432512, 17563072,
+ 17693888, 17824192, 17955904, 18087488, 18218176, 18349504, 18481088,
+ 18611392, 18742336, 18874304, 19004224, 19135936, 19267264, 19398208,
+ 19529408, 19660096, 19791424, 19922752, 20053952, 20184896, 20315968,
+ 20446912, 20576576, 20709184, 20840384, 20971072, 21102272, 21233216,
+ 21364544, 21494848, 21626816, 21757376, 21887552, 22019392, 22151104,
+ 22281536, 22412224, 22543936, 22675264, 22806464, 22935872, 23068096,
+ 23198272, 23330752, 23459008, 23592512, 23723968, 23854912, 23986112,
+ 24116672, 24247616, 24378688, 24509504, 24640832, 24772544, 24903488,
+ 25034432, 25165376, 25296704, 25427392, 25558592, 25690048, 25820096,
+ 25951936, 26081728, 26214208, 26345024, 26476096, 26606656, 26737472,
+ 26869184, 26998208, 27131584, 27262528, 27393728, 27523904, 27655744,
+ 27786688, 27917888, 28049344, 28179904, 28311488, 28441792, 28573504,
+ 28700864, 28835648, 28966208, 29096768, 29228608, 29359808, 29490752,
+ 29621824, 29752256, 29882816, 30014912, 30144448, 30273728, 30406976,
+ 30538432, 30670784, 30799936, 30932672, 31063744, 31195072, 31325248,
+ 31456192, 31588288, 31719232, 31850432, 31981504, 32110784, 32243392,
+ 32372672, 32505664, 32636608, 32767808, 32897344, 33029824, 33160768,
+ 33289664, 33423296, 33554368, 33683648, 33816512, 33947456, 34076992,
+ 34208704, 34340032, 34471744, 34600256, 34734016, 34864576, 34993984,
+ 35127104, 35258176, 35386688, 35518528, 35650624, 35782336, 35910976,
+ 36044608, 36175808, 36305728, 36436672, 36568384, 36699968, 36830656,
+ 36961984, 37093312, 37223488, 37355072, 37486528, 37617472, 37747904,
+ 37879232, 38009792, 38141888, 38272448, 38403392, 38535104, 38660672,
+ 38795584, 38925632, 39059264, 39190336, 39320768, 39452096, 39581632,
+ 39713984, 39844928, 39974848, 40107968, 40238144, 40367168, 40500032,
+ 40631744, 40762816, 40894144, 41023552, 41155904, 41286208, 41418304,
+ 41547712, 41680448, 41811904, 41942848, 42073792, 42204992, 42334912,
+ 42467008, 42597824, 42729152, 42860096, 42991552, 43122368, 43253696,
+ 43382848, 43515712, 43646912, 43777088, 43907648, 44039104, 44170432,
+ 44302144, 44433344, 44564288, 44694976, 44825152, 44956864, 45088448,
+ 45219008, 45350464, 45481024, 45612608, 45744064, 45874496, 46006208,
+ 46136768, 46267712, 46399424, 46529344, 46660672, 46791488, 46923328,
+ 47053504, 47185856, 47316928, 47447872, 47579072, 47710144, 47839936,
+ 47971648, 48103232, 48234176, 48365248, 48496192, 48627136, 48757312,
+ 48889664, 49020736, 49149248, 49283008, 49413824, 49545152, 49675712,
+ 49807168, 49938368, 50069056, 50200256, 50331584, 50462656, 50593472,
+ 50724032, 50853952, 50986048, 51117632, 51248576, 51379904, 51510848,
+ 51641792, 51773248, 51903296, 52035136, 52164032, 52297664, 52427968,
+ 52557376, 52690112, 52821952, 52952896, 53081536, 53213504, 53344576,
+ 53475776, 53608384, 53738816, 53870528, 54000832, 54131776, 54263744,
+ 54394688, 54525248, 54655936, 54787904, 54918592, 55049152, 55181248,
+ 55312064, 55442752, 55574336, 55705024, 55836224, 55967168, 56097856,
+ 56228672, 56358592, 56490176, 56621888, 56753728, 56884928, 57015488,
+ 57146816, 57278272, 57409216, 57540416, 57671104, 57802432, 57933632,
+ 58064576, 58195264, 58326976, 58457408, 58588864, 58720192, 58849984,
+ 58981696, 59113024, 59243456, 59375552, 59506624, 59637568, 59768512,
+ 59897792, 60030016, 60161984, 60293056, 60423872, 60554432, 60683968,
+ 60817216, 60948032, 61079488, 61209664, 61341376, 61471936, 61602752,
+ 61733696, 61865792, 61996736, 62127808, 62259136, 62389568, 62520512,
+ 62651584, 62781632, 62910784, 63045056, 63176128, 63307072, 63438656,
+ 63569216, 63700928, 63831616, 63960896, 64093888, 64225088, 64355392,
+ 64486976, 64617664, 64748608, 64879424, 65009216, 65142464, 65273792,
+ 65402816, 65535424, 65666752, 65797696, 65927744, 66060224, 66191296,
+ 66321344, 66453056, 66584384, 66715328, 66846656, 66977728, 67108672,
+ 67239104, 67370432, 67501888, 67631296, 67763776, 67895104, 68026304,
+ 68157248, 68287936, 68419264, 68548288, 68681408, 68811968, 68942912,
+ 69074624, 69205568, 69337024, 69467584, 69599168, 69729472, 69861184,
+ 69989824, 70122944, 70253888, 70385344, 70515904, 70647232, 70778816,
+ 70907968, 71040832, 71171648, 71303104, 71432512, 71564992, 71695168,
+ 71826368, 71958464, 72089536, 72219712, 72350144, 72482624, 72613568,
+ 72744512, 72875584, 73006144, 73138112, 73268672, 73400128, 73530944,
+ 73662272, 73793344, 73924544, 74055104, 74185792, 74316992, 74448832,
+ 74579392, 74710976, 74841664, 74972864, 75102784, 75233344, 75364544,
+ 75497024, 75627584, 75759296, 75890624, 76021696, 76152256, 76283072,
+ 76414144, 76545856, 76676672, 76806976, 76937792, 77070016, 77200832,
+ 77331392, 77462464, 77593664, 77725376, 77856448, 77987776, 78118336,
+ 78249664, 78380992, 78511424, 78642496, 78773056, 78905152, 79033664,
+ 79166656, 79297472, 79429568, 79560512, 79690816, 79822784, 79953472,
+ 80084672, 80214208, 80346944, 80477632, 80608576, 80740288, 80870848,
+ 81002048, 81133504, 81264448, 81395648, 81525952, 81657536, 81786304,
+ 81919808, 82050112, 82181312, 82311616, 82443968, 82573376, 82705984,
+ 82835776, 82967744, 83096768, 83230528, 83359552, 83491264, 83622464,
+ 83753536, 83886016, 84015296, 84147776, 84277184, 84409792, 84540608,
+ 84672064, 84803008, 84934336, 85065152, 85193792, 85326784, 85458496,
+ 85589312, 85721024, 85851968, 85982656, 86112448, 86244416, 86370112,
+ 86506688, 86637632, 86769344, 86900672, 87031744, 87162304, 87293632,
+ 87424576, 87555392, 87687104, 87816896, 87947968, 88079168, 88211264,
+ 88341824, 88473152, 88603712, 88735424, 88862912, 88996672, 89128384,
+ 89259712, 89390272, 89521984, 89652544, 89783872, 89914816, 90045376,
+ 90177088, 90307904, 90438848, 90569152, 90700096, 90832832, 90963776,
+ 91093696, 91223744, 91356992, 91486784, 91618496, 91749824, 91880384,
+ 92012224, 92143552, 92273344, 92405696, 92536768, 92666432, 92798912,
+ 92926016, 93060544, 93192128, 93322816, 93453632, 93583936, 93715136,
+ 93845056, 93977792, 94109504, 94240448, 94371776, 94501184, 94632896,
+ 94764224, 94895552, 95023424, 95158208, 95287744, 95420224, 95550016,
+ 95681216, 95811904, 95943872, 96075328, 96203584, 96337856, 96468544,
+ 96599744, 96731072, 96860992, 96992576, 97124288, 97254848, 97385536,
+ 97517248, 97647808, 97779392, 97910464, 98041408, 98172608, 98303168,
+ 98434496, 98565568, 98696768, 98827328, 98958784, 99089728, 99220928,
+ 99352384, 99482816, 99614272, 99745472, 99876416, 100007104,
+ 100138048, 100267072, 100401088, 100529984, 100662592, 100791872,
+ 100925248, 101056064, 101187392, 101317952, 101449408, 101580608,
+ 101711296, 101841728, 101973824, 102104896, 102235712, 102366016,
+ 102498112, 102628672, 102760384, 102890432, 103021888, 103153472,
+ 103284032, 103415744, 103545152, 103677248, 103808576, 103939648,
+ 104070976, 104201792, 104332736, 104462528, 104594752, 104725952,
+ 104854592, 104988608, 105118912, 105247808, 105381184, 105511232,
+ 105643072, 105774784, 105903296, 106037056, 106167872, 106298944,
+ 106429504, 106561472, 106691392, 106822592, 106954304, 107085376,
+ 107216576, 107346368, 107478464, 107609792, 107739712, 107872192,
+ 108003136, 108131392, 108265408, 108396224, 108527168, 108657344,
+ 108789568, 108920384, 109049792, 109182272, 109312576, 109444928,
+ 109572928, 109706944, 109837888, 109969088, 110099648, 110230976,
+ 110362432, 110492992, 110624704, 110755264, 110886208, 111017408,
+ 111148864, 111279296, 111410752, 111541952, 111673024, 111803456,
+ 111933632, 112066496, 112196416, 112328512, 112457792, 112590784,
+ 112715968, 112852672, 112983616, 113114944, 113244224, 113376448,
+ 113505472, 113639104, 113770304, 113901376, 114031552, 114163264,
+ 114294592, 114425536, 114556864, 114687424, 114818624, 114948544,
+ 115080512, 115212224, 115343296, 115473472, 115605184, 115736128,
+ 115867072, 115997248, 116128576, 116260288, 116391488, 116522944,
+ 116652992, 116784704, 116915648, 117046208, 117178304, 117308608,
+ 117440192, 117569728, 117701824, 117833024, 117964096, 118094656,
+ 118225984, 118357312, 118489024, 118617536, 118749632, 118882112,
+ 119012416, 119144384, 119275328, 119406016, 119537344, 119668672,
+ 119798464, 119928896, 120061376, 120192832, 120321728, 120454336,
+ 120584512, 120716608, 120848192, 120979136, 121109056, 121241408,
+ 121372352, 121502912, 121634752, 121764416, 121895744, 122027072,
+ 122157632, 122289088, 122421184, 122550592, 122682944, 122813888,
+ 122945344, 123075776, 123207488, 123338048, 123468736, 123600704,
+ 123731264, 123861952, 123993664, 124124608, 124256192, 124386368,
+ 124518208, 124649024, 124778048, 124911296, 125041088, 125173696,
+ 125303744, 125432896, 125566912, 125696576, 125829056, 125958592,
+ 126090304, 126221248, 126352832, 126483776, 126615232, 126746432,
+ 126876608, 127008704, 127139392, 127270336, 127401152, 127532224,
+ 127663552, 127794752, 127925696, 128055232, 128188096, 128319424,
+ 128449856, 128581312, 128712256, 128843584, 128973632, 129103808,
+ 129236288, 129365696, 129498944, 129629888, 129760832, 129892288,
+ 130023104, 130154048, 130283968, 130416448, 130547008, 130678336,
+ 130807616, 130939456, 131071552, 131202112, 131331776, 131464384,
+ 131594048, 131727296, 131858368, 131987392, 132120256, 132250816,
+ 132382528, 132513728, 132644672, 132774976, 132905792, 133038016,
+ 133168832, 133299392, 133429312, 133562048, 133692992, 133823296,
+ 133954624, 134086336, 134217152, 134348608, 134479808, 134607296,
+ 134741056, 134872384, 135002944, 135134144, 135265472, 135396544,
+ 135527872, 135659072, 135787712, 135921472, 136052416, 136182848,
+ 136313792, 136444864, 136576448, 136707904, 136837952, 136970048,
+ 137099584, 137232064, 137363392, 137494208, 137625536, 137755712,
+ 137887424, 138018368, 138149824, 138280256, 138411584, 138539584,
+ 138672832, 138804928, 138936128, 139066688, 139196864, 139328704,
+ 139460032, 139590208, 139721024, 139852864, 139984576, 140115776,
+ 140245696, 140376512, 140508352, 140640064, 140769856, 140902336,
+ 141032768, 141162688, 141294016, 141426496, 141556544, 141687488,
+ 141819584, 141949888, 142080448, 142212544, 142342336, 142474432,
+ 142606144, 142736192, 142868288, 142997824, 143129408, 143258944,
+ 143392448, 143523136, 143653696, 143785024, 143916992, 144045632,
+ 144177856, 144309184, 144440768, 144570688, 144701888, 144832448,
+ 144965056, 145096384, 145227584, 145358656, 145489856, 145620928,
+ 145751488, 145883072, 146011456, 146144704, 146275264, 146407232,
+ 146538176, 146668736, 146800448, 146931392, 147062336, 147193664,
+ 147324224, 147455936, 147586624, 147717056, 147848768, 147979456,
+ 148110784, 148242368, 148373312, 148503232, 148635584, 148766144,
+ 148897088, 149028416, 149159488, 149290688, 149420224, 149551552,
+ 149683136, 149814976, 149943616, 150076352, 150208064, 150338624,
+ 150470464, 150600256, 150732224, 150862784, 150993088, 151125952,
+ 151254976, 151388096, 151519168, 151649728, 151778752, 151911104,
+ 152042944, 152174144, 152304704, 152435648, 152567488, 152698816,
+ 152828992, 152960576, 153091648, 153222976, 153353792, 153484096,
+ 153616192, 153747008, 153878336, 154008256, 154139968, 154270912,
+ 154402624, 154533824, 154663616, 154795712, 154926272, 155057984,
+ 155188928, 155319872, 155450816, 155580608, 155712064, 155843392,
+ 155971136, 156106688, 156237376, 156367424, 156499264, 156630976,
+ 156761536, 156892352, 157024064, 157155008, 157284416, 157415872,
+ 157545536, 157677248, 157810496, 157938112, 158071744, 158203328,
+ 158334656, 158464832, 158596288, 158727616, 158858048, 158988992,
+ 159121216, 159252416, 159381568, 159513152, 159645632, 159776192,
+ 159906496, 160038464, 160169536, 160300352, 160430656, 160563008,
+ 160693952, 160822208, 160956352, 161086784, 161217344, 161349184,
+ 161480512, 161611456, 161742272, 161873216, 162002752, 162135872,
+ 162266432, 162397888, 162529216, 162660032, 162790976, 162922048,
+ 163052096, 163184576, 163314752, 163446592, 163577408, 163707968,
+ 163839296, 163969984, 164100928, 164233024, 164364224, 164494912,
+ 164625856, 164756672, 164887616, 165019072, 165150016, 165280064,
+ 165412672, 165543104, 165674944, 165805888, 165936832, 166067648,
+ 166198336, 166330048, 166461248, 166591552, 166722496, 166854208,
+ 166985408, 167116736, 167246656, 167378368, 167508416, 167641024,
+ 167771584, 167903168, 168034112, 168164032, 168295744, 168427456,
+ 168557632, 168688448, 168819136, 168951616, 169082176, 169213504,
+ 169344832, 169475648, 169605952, 169738048, 169866304, 169999552,
+ 170131264, 170262464, 170393536, 170524352, 170655424, 170782016,
+ 170917696, 171048896, 171179072, 171310784, 171439936, 171573184,
+ 171702976, 171835072, 171966272, 172097216, 172228288, 172359232,
+ 172489664, 172621376, 172747712, 172883264, 173014208, 173144512,
+ 173275072, 173407424, 173539136, 173669696, 173800768, 173931712,
+ 174063424, 174193472, 174325696, 174455744, 174586816, 174718912,
+ 174849728, 174977728, 175109696, 175242688, 175374272, 175504832,
+ 175636288, 175765696, 175898432, 176028992, 176159936, 176291264,
+ 176422592, 176552512, 176684864, 176815424, 176946496, 177076544,
+ 177209152, 177340096, 177470528, 177600704, 177731648, 177864256,
+ 177994816, 178126528, 178257472, 178387648, 178518464, 178650176,
+ 178781888, 178912064, 179044288, 179174848, 179305024, 179436736,
+ 179568448, 179698496, 179830208, 179960512, 180092608, 180223808,
+ 180354752, 180485696, 180617152, 180748096, 180877504, 181009984,
+ 181139264, 181272512, 181402688, 181532608, 181663168, 181795136,
+ 181926592, 182057536, 182190016, 182320192, 182451904, 182582336,
+ 182713792, 182843072, 182976064, 183107264, 183237056, 183368384,
+ 183494848, 183631424, 183762752, 183893824, 184024768, 184154816,
+ 184286656, 184417984, 184548928, 184680128, 184810816, 184941248,
+ 185072704, 185203904, 185335616, 185465408, 185596352, 185727296,
+ 185859904, 185989696, 186121664, 186252992, 186383552, 186514112,
+ 186645952, 186777152, 186907328, 187037504, 187170112, 187301824,
+ 187429184, 187562048, 187693504, 187825472, 187957184, 188087104,
+ 188218304, 188349376, 188481344, 188609728, 188743616, 188874304,
+ 189005248, 189136448, 189265088, 189396544, 189528128, 189660992,
+ 189791936, 189923264, 190054208, 190182848, 190315072, 190447424,
+ 190577984, 190709312, 190840768, 190971328, 191102656, 191233472,
+ 191364032, 191495872, 191626816, 191758016, 191888192, 192020288,
+ 192148928, 192282176, 192413504, 192542528, 192674752, 192805952,
+ 192937792, 193068608, 193198912, 193330496, 193462208, 193592384,
+ 193723456, 193854272, 193985984, 194116672, 194247232, 194379712,
+ 194508352, 194641856, 194772544, 194900672, 195035072, 195166016,
+ 195296704, 195428032, 195558592, 195690304, 195818176, 195952576,
+ 196083392, 196214336, 196345792, 196476736, 196607552, 196739008,
+ 196869952, 197000768, 197130688, 197262784, 197394368, 197523904,
+ 197656384, 197787584, 197916608, 198049472, 198180544, 198310208,
+ 198442432, 198573632, 198705088, 198834368, 198967232, 199097792,
+ 199228352, 199360192, 199491392, 199621696, 199751744, 199883968,
+ 200014016, 200146624, 200276672, 200408128, 200540096, 200671168,
+ 200801984, 200933312, 201062464, 201194944, 201326144, 201457472,
+ 201588544, 201719744, 201850816, 201981632, 202111552, 202244032,
+ 202374464, 202505152, 202636352, 202767808, 202898368, 203030336,
+ 203159872, 203292608, 203423296, 203553472, 203685824, 203816896,
+ 203947712, 204078272, 204208192, 204341056, 204472256, 204603328,
+ 204733888, 204864448, 204996544, 205125568, 205258304, 205388864,
+ 205517632, 205650112, 205782208, 205913536, 206044736, 206176192,
+ 206307008, 206434496, 206569024, 206700224, 206831168, 206961856,
+ 207093056, 207223616, 207355328, 207486784, 207616832, 207749056,
+ 207879104, 208010048, 208141888, 208273216, 208404032, 208534336,
+ 208666048, 208796864, 208927424, 209059264, 209189824, 209321792,
+ 209451584, 209582656, 209715136, 209845568, 209976896, 210106432,
+ 210239296, 210370112, 210501568, 210630976, 210763712, 210894272,
+ 211024832, 211156672, 211287616, 211418176, 211549376, 211679296,
+ 211812032, 211942592, 212074432, 212204864, 212334016, 212467648,
+ 212597824, 212727616, 212860352, 212991424, 213120832, 213253952,
+ 213385024, 213515584, 213645632, 213777728, 213909184, 214040128,
+ 214170688, 214302656, 214433728, 214564544, 214695232, 214826048,
+ 214956992, 215089088, 215219776, 215350592, 215482304, 215613248,
+ 215743552, 215874752, 216005312, 216137024, 216267328, 216399296,
+ 216530752, 216661696, 216790592, 216923968, 217054528, 217183168,
+ 217316672, 217448128, 217579072, 217709504, 217838912, 217972672,
+ 218102848, 218233024, 218364736, 218496832, 218627776, 218759104,
+ 218888896, 219021248, 219151936, 219281728, 219413056, 219545024,
+ 219675968, 219807296, 219938624, 220069312, 220200128, 220331456,
+ 220461632, 220592704, 220725184, 220855744, 220987072, 221117888,
+ 221249216, 221378368, 221510336, 221642048, 221772736, 221904832,
+ 222031808, 222166976, 222297536, 222428992, 222559936, 222690368,
+ 222820672, 222953152, 223083968, 223213376, 223345984, 223476928,
+ 223608512, 223738688, 223869376, 224001472, 224132672, 224262848,
+ 224394944, 224524864, 224657344, 224788288, 224919488, 225050432,
+ 225181504, 225312704, 225443776, 225574592, 225704768, 225834176,
+ 225966784, 226097216, 226229824, 226360384, 226491712, 226623424,
+ 226754368, 226885312, 227015104, 227147456, 227278528, 227409472,
+ 227539904, 227669696, 227802944, 227932352, 228065216, 228196288,
+ 228326464, 228457792, 228588736, 228720064, 228850112, 228981056,
+ 229113152, 229243328, 229375936, 229505344, 229636928, 229769152,
+ 229894976, 230030272, 230162368, 230292416, 230424512, 230553152,
+ 230684864, 230816704, 230948416, 231079616, 231210944, 231342016,
+ 231472448, 231603776, 231733952, 231866176, 231996736, 232127296,
+ 232259392, 232388672, 232521664, 232652608, 232782272, 232914496,
+ 233043904, 233175616, 233306816, 233438528, 233569984, 233699776,
+ 233830592, 233962688, 234092224, 234221888, 234353984, 234485312,
+ 234618304, 234749888, 234880832, 235011776, 235142464, 235274048,
+ 235403456, 235535936, 235667392, 235797568, 235928768, 236057152,
+ 236190272, 236322752, 236453312, 236583616, 236715712, 236846528,
+ 236976448, 237108544, 237239104, 237371072, 237501632, 237630784,
+ 237764416, 237895232, 238026688, 238157632, 238286912, 238419392,
+ 238548032, 238681024, 238812608, 238941632, 239075008, 239206336,
+ 239335232, 239466944, 239599168, 239730496, 239861312, 239992384,
+ 240122816, 240254656, 240385856, 240516928, 240647872, 240779072,
+ 240909632, 241040704, 241171904, 241302848, 241433408, 241565248,
+ 241696192, 241825984, 241958848, 242088256, 242220224, 242352064,
+ 242481856, 242611648, 242744896, 242876224, 243005632, 243138496,
+ 243268672, 243400384, 243531712, 243662656, 243793856, 243924544,
+ 244054592, 244187072, 244316608, 244448704, 244580032, 244710976,
+ 244841536, 244972864, 245104448, 245233984, 245365312, 245497792,
+ 245628736, 245759936, 245889856, 246021056, 246152512, 246284224,
+ 246415168, 246545344, 246675904, 246808384, 246939584, 247070144,
+ 247199552, 247331648, 247463872, 247593536, 247726016, 247857088,
+ 247987648, 248116928, 248249536, 248380736, 248512064, 248643008,
+ 248773312, 248901056, 249036608, 249167552, 249298624, 249429184,
+ 249560512, 249692096, 249822784, 249954112, 250085312, 250215488,
+ 250345792, 250478528, 250608704, 250739264, 250870976, 251002816,
+ 251133632, 251263552, 251395136, 251523904, 251657792, 251789248,
+ 251919424, 252051392, 252182464, 252313408, 252444224, 252575552,
+ 252706624, 252836032, 252968512, 253099712, 253227584, 253361728,
+ 253493056, 253623488, 253754432, 253885504, 254017216, 254148032,
+ 254279488, 254410432, 254541376, 254672576, 254803264, 254933824,
+ 255065792, 255196736, 255326528, 255458752, 255589952, 255721408,
+ 255851072, 255983296, 256114624, 256244416, 256374208, 256507712,
+ 256636096, 256768832, 256900544, 257031616, 257162176, 257294272,
+ 257424448, 257555776, 257686976, 257818432, 257949632, 258079552,
+ 258211136, 258342464, 258473408, 258603712, 258734656, 258867008,
+ 258996544, 259127744, 259260224, 259391296, 259522112, 259651904,
+ 259784384, 259915328, 260045888, 260175424, 260308544, 260438336,
+ 260570944, 260700992, 260832448, 260963776, 261092672, 261226304,
+ 261356864, 261487936, 261619648, 261750592, 261879872, 262011968,
+ 262143424, 262274752, 262404416, 262537024, 262667968, 262799296,
+ 262928704, 263061184, 263191744, 263322944, 263454656, 263585216,
+ 263716672, 263847872, 263978944, 264108608, 264241088, 264371648,
+ 264501184, 264632768, 264764096, 264895936, 265024576, 265158464,
+ 265287488, 265418432, 265550528, 265681216, 265813312, 265943488,
+ 266075968, 266206144, 266337728, 266468032, 266600384, 266731072,
+ 266862272, 266993344, 267124288, 267255616, 267386432, 267516992,
+ 267648704, 267777728, 267910592, 268040512, 268172096, 268302784,
+ 268435264, 268566208, 268696256, 268828096, 268959296, 269090368,
+ 269221312, 269352256, 269482688, 269614784, 269745856, 269876416,
+ 270007616, 270139328, 270270272, 270401216, 270531904, 270663616,
+ 270791744, 270924736, 271056832, 271186112, 271317184, 271449536,
+ 271580992, 271711936, 271843136, 271973056, 272105408, 272236352,
+ 272367296, 272498368, 272629568, 272759488, 272891456, 273022784,
+ 273153856, 273284672, 273415616, 273547072, 273677632, 273808448,
+ 273937088, 274071488, 274200896, 274332992, 274463296, 274595392,
+ 274726208, 274857536, 274988992, 275118656, 275250496, 275382208,
+ 275513024, 275643968, 275775296, 275906368, 276037184, 276167872,
+ 276297664, 276429376, 276560576, 276692672, 276822976, 276955072,
+ 277085632, 277216832, 277347008, 277478848, 277609664, 277740992,
+ 277868608, 278002624, 278134336, 278265536, 278395328, 278526784,
+ 278657728, 278789824, 278921152, 279052096, 279182912, 279313088,
+ 279443776, 279576256, 279706048, 279838528, 279969728, 280099648,
+ 280230976, 280361408, 280493632, 280622528, 280755392, 280887104,
+ 281018176, 281147968, 281278912, 281411392, 281542592, 281673152,
+ 281803712, 281935552, 282066496, 282197312, 282329024, 282458816,
+ 282590272, 282720832, 282853184, 282983744, 283115072, 283246144,
+ 283377344, 283508416, 283639744, 283770304, 283901504, 284032576,
+ 284163136, 284294848, 284426176, 284556992, 284687296, 284819264,
+ 284950208, 285081536}
diff --git a/consensus/ethash/api.go b/consensus/ethash/api.go
new file mode 100644
index 0000000..34ed386
--- /dev/null
+++ b/consensus/ethash/api.go
@@ -0,0 +1,118 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package ethash
+
+import (
+ "errors"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+)
+
+var errEthashStopped = errors.New("ethash stopped")
+
+// API exposes ethash related methods for the RPC interface.
+type API struct {
+ ethash *Ethash // Make sure the mode of ethash is normal.
+}
+
+// GetWork returns a work package for external miner.
+//
+// The work package consists of 3 strings:
+// result[0] - 32 bytes hex encoded current block header pow-hash
+// result[1] - 32 bytes hex encoded seed hash used for DAG
+// result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
+// result[3] - hex encoded block number
+func (api *API) GetWork() ([4]string, error) {
+ if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
+ return [4]string{}, errors.New("not supported")
+ }
+
+ var (
+ workCh = make(chan [4]string, 1)
+ errc = make(chan error, 1)
+ )
+
+ select {
+ case api.ethash.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
+ case <-api.ethash.exitCh:
+ return [4]string{}, errEthashStopped
+ }
+
+ select {
+ case work := <-workCh:
+ return work, nil
+ case err := <-errc:
+ return [4]string{}, err
+ }
+}
+
+// SubmitWork can be used by external miner to submit their POW solution.
+// It returns an indication if the work was accepted.
+// Note either an invalid solution, a stale work a non-existent work will return false.
+func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool {
+ if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
+ return false
+ }
+
+ var errc = make(chan error, 1)
+
+ select {
+ case api.ethash.submitWorkCh <- &mineResult{
+ nonce: nonce,
+ mixDigest: digest,
+ hash: hash,
+ errc: errc,
+ }:
+ case <-api.ethash.exitCh:
+ return false
+ }
+
+ err := <-errc
+ return err == nil
+}
+
+// SubmitHashrate can be used for remote miners to submit their hash rate.
+// This enables the node to report the combined hash rate of all miners
+// which submit work through this node.
+//
+// It accepts the miner hash rate and an identifier which must be unique
+// between nodes.
+func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool {
+ if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
+ return false
+ }
+
+ var done = make(chan struct{}, 1)
+
+ select {
+ case api.ethash.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
+ case <-api.ethash.exitCh:
+ return false
+ }
+
+ // Block until hash rate submitted successfully.
+ <-done
+
+ return true
+}
+
+// GetHashrate returns the current hashrate for local CPU miner and remote miner.
+func (api *API) GetHashrate() uint64 {
+ return uint64(api.ethash.Hashrate())
+}
diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go
new file mode 100644
index 0000000..d9130ec
--- /dev/null
+++ b/consensus/ethash/consensus.go
@@ -0,0 +1,637 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package ethash
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "math/big"
+ "runtime"
+ "time"
+
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/consensus/misc"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+ "github.com/ava-labs/go-ethereum/rlp"
+ mapset "github.com/deckarep/golang-set"
+ "golang.org/x/crypto/sha3"
+)
+
+// Ethash proof-of-work protocol constants.
+var (
+ FrontierBlockReward = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
+ ByzantiumBlockReward = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium
+ ConstantinopleBlockReward = big.NewInt(2e+18) // Block reward in wei for successfully mining a block upward from Constantinople
+ maxUncles = 2 // Maximum number of uncles allowed in a single block
+ allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
+
+ // calcDifficultyConstantinople is the difficulty adjustment algorithm for Constantinople.
+ // It returns the difficulty that a new block should have when created at time given the
+ // parent block's time and difficulty. The calculation uses the Byzantium rules, but with
+ // bomb offset 5M.
+ // Specification EIP-1234: https://eips.ethereum.org/EIPS/eip-1234
+ calcDifficultyConstantinople = makeDifficultyCalculator(big.NewInt(5000000))
+
+ // calcDifficultyByzantium is the difficulty adjustment algorithm. It returns
+ // the difficulty that a new block should have when created at time given the
+ // parent block's time and difficulty. The calculation uses the Byzantium rules.
+ // Specification EIP-649: https://eips.ethereum.org/EIPS/eip-649
+ calcDifficultyByzantium = makeDifficultyCalculator(big.NewInt(3000000))
+)
+
+// 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 (
+ errZeroBlockTime = errors.New("timestamp equals parent's")
+ errTooManyUncles = errors.New("too many uncles")
+ errDuplicateUncle = errors.New("duplicate uncle")
+ errUncleIsAncestor = errors.New("uncle is ancestor")
+ errDanglingUncle = errors.New("uncle's parent is not ancestor")
+ errInvalidDifficulty = errors.New("non-positive difficulty")
+ errInvalidMixDigest = errors.New("invalid mix digest")
+ errInvalidPoW = errors.New("invalid proof-of-work")
+)
+
+// Author implements consensus.Engine, returning the header's coinbase as the
+// proof-of-work verified author of the block.
+func (ethash *Ethash) Author(header *types.Header) (common.Address, error) {
+ return header.Coinbase, nil
+}
+
+// VerifyHeader checks whether a header conforms to the consensus rules of the
+// stock Ethereum ethash engine.
+func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
+ // If we're running a full engine faking, accept any input as valid
+ if ethash.config.PowMode == ModeFullFake {
+ return nil
+ }
+ // Short circuit if the header is known, or it's parent not
+ number := header.Number.Uint64()
+ if chain.GetHeader(header.Hash(), number) != nil {
+ return nil
+ }
+ parent := chain.GetHeader(header.ParentHash, number-1)
+ if parent == nil {
+ return consensus.ErrUnknownAncestor
+ }
+ // Sanity checks passed, do a proper verification
+ return ethash.verifyHeader(chain, header, parent, false, seal)
+}
+
+// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
+// concurrently. The method returns a quit channel to abort the operations and
+// a results channel to retrieve the async verifications.
+func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
+ // If we're running a full engine faking, accept any input as valid
+ if ethash.config.PowMode == ModeFullFake || len(headers) == 0 {
+ abort, results := make(chan struct{}), make(chan error, len(headers))
+ for i := 0; i < len(headers); i++ {
+ results <- nil
+ }
+ return abort, results
+ }
+
+ // Spawn as many workers as allowed threads
+ workers := runtime.GOMAXPROCS(0)
+ if len(headers) < workers {
+ workers = len(headers)
+ }
+
+ // Create a task channel and spawn the verifiers
+ var (
+ inputs = make(chan int)
+ done = make(chan int, workers)
+ errors = make([]error, len(headers))
+ abort = make(chan struct{})
+ )
+ for i := 0; i < workers; i++ {
+ go func() {
+ for index := range inputs {
+ errors[index] = ethash.verifyHeaderWorker(chain, headers, seals, index)
+ done <- index
+ }
+ }()
+ }
+
+ errorsOut := make(chan error, len(headers))
+ go func() {
+ defer close(inputs)
+ var (
+ in, out = 0, 0
+ checked = make([]bool, len(headers))
+ inputs = inputs
+ )
+ for {
+ select {
+ case inputs <- in:
+ if in++; in == len(headers) {
+ // Reached end of headers. Stop sending to workers.
+ inputs = nil
+ }
+ case index := <-done:
+ for checked[index] = true; checked[out]; out++ {
+ errorsOut <- errors[out]
+ if out == len(headers)-1 {
+ return
+ }
+ }
+ case <-abort:
+ return
+ }
+ }
+ }()
+ return abort, errorsOut
+}
+
+func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, headers []*types.Header, seals []bool, index int) error {
+ var parent *types.Header
+ if index == 0 {
+ parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
+ } else if headers[index-1].Hash() == headers[index].ParentHash {
+ parent = headers[index-1]
+ }
+ if parent == nil {
+ return consensus.ErrUnknownAncestor
+ }
+ if chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()) != nil {
+ return nil // known block
+ }
+ return ethash.verifyHeader(chain, headers[index], parent, false, seals[index])
+}
+
+// VerifyUncles verifies that the given block's uncles conform to the consensus
+// rules of the stock Ethereum ethash engine.
+func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
+ // If we're running a full engine faking, accept any input as valid
+ if ethash.config.PowMode == ModeFullFake {
+ return nil
+ }
+ // Verify that there are at most 2 uncles included in this block
+ if len(block.Uncles()) > maxUncles {
+ return errTooManyUncles
+ }
+ if len(block.Uncles()) == 0 {
+ return nil
+ }
+ // Gather the set of past uncles and ancestors
+ uncles, ancestors := mapset.NewSet(), make(map[common.Hash]*types.Header)
+
+ number, parent := block.NumberU64()-1, block.ParentHash()
+ for i := 0; i < 7; i++ {
+ ancestor := chain.GetBlock(parent, number)
+ if ancestor == nil {
+ break
+ }
+ ancestors[ancestor.Hash()] = ancestor.Header()
+ for _, uncle := range ancestor.Uncles() {
+ uncles.Add(uncle.Hash())
+ }
+ parent, number = ancestor.ParentHash(), number-1
+ }
+ ancestors[block.Hash()] = block.Header()
+ uncles.Add(block.Hash())
+
+ // Verify each of the uncles that it's recent, but not an ancestor
+ for _, uncle := range block.Uncles() {
+ // Make sure every uncle is rewarded only once
+ hash := uncle.Hash()
+ if uncles.Contains(hash) {
+ return errDuplicateUncle
+ }
+ uncles.Add(hash)
+
+ // Make sure the uncle has a valid ancestry
+ if ancestors[hash] != nil {
+ return errUncleIsAncestor
+ }
+ if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() {
+ return errDanglingUncle
+ }
+ if err := ethash.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// verifyHeader checks whether a header conforms to the consensus rules of the
+// stock Ethereum ethash engine.
+// See YP section 4.3.4. "Block Header Validity"
+func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *types.Header, uncle bool, seal bool) error {
+ // Ensure that the header's extra-data section is of a reasonable size
+ if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
+ return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
+ }
+ // Verify the header's timestamp
+ if !uncle {
+ if header.Time > uint64(time.Now().Add(allowedFutureBlockTime).Unix()) {
+ return consensus.ErrFutureBlock
+ }
+ }
+ if header.Time <= parent.Time {
+ return errZeroBlockTime
+ }
+ // Verify the block's difficulty based in it's timestamp and parent's difficulty
+ expected := ethash.CalcDifficulty(chain, header.Time, parent)
+
+ if expected.Cmp(header.Difficulty) != 0 {
+ return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, expected)
+ }
+ // Verify that the gas limit is <= 2^63-1
+ cap := uint64(0x7fffffffffffffff)
+ if header.GasLimit > cap {
+ return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, cap)
+ }
+ // Verify that the gasUsed is <= gasLimit
+ if header.GasUsed > header.GasLimit {
+ return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
+ }
+
+ // Verify that the gas limit remains within allowed bounds
+ diff := int64(parent.GasLimit) - int64(header.GasLimit)
+ if diff < 0 {
+ diff *= -1
+ }
+ limit := parent.GasLimit / params.GasLimitBoundDivisor
+
+ if uint64(diff) >= limit || header.GasLimit < params.MinGasLimit {
+ return fmt.Errorf("invalid gas limit: have %d, want %d += %d", header.GasLimit, parent.GasLimit, limit)
+ }
+ // Verify that the block number is parent's +1
+ if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
+ return consensus.ErrInvalidNumber
+ }
+ // Verify the engine specific seal securing the block
+ if seal {
+ if err := ethash.VerifySeal(chain, header); err != nil {
+ return err
+ }
+ }
+ // If all checks passed, validate any special fields for hard forks
+ if err := misc.VerifyDAOHeaderExtraData(chain.Config(), header); err != nil {
+ return err
+ }
+ if err := misc.VerifyForkHashes(chain.Config(), header, uncle); err != nil {
+ return err
+ }
+ return nil
+}
+
+// CalcDifficulty is the difficulty adjustment algorithm. It returns
+// the difficulty that a new block should have when created at time
+// given the parent block's time and difficulty.
+func (ethash *Ethash) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
+ return CalcDifficulty(chain.Config(), time, parent)
+}
+
+// CalcDifficulty is the difficulty adjustment algorithm. It returns
+// the difficulty that a new block should have when created at time
+// given the parent block's time and difficulty.
+func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {
+ next := new(big.Int).Add(parent.Number, big1)
+ switch {
+ case config.IsConstantinople(next):
+ return calcDifficultyConstantinople(time, parent)
+ case config.IsByzantium(next):
+ return calcDifficultyByzantium(time, parent)
+ case config.IsHomestead(next):
+ return calcDifficultyHomestead(time, parent)
+ default:
+ return calcDifficultyFrontier(time, parent)
+ }
+}
+
+// Some weird constants to avoid constant memory allocs for them.
+var (
+ expDiffPeriod = big.NewInt(100000)
+ big1 = big.NewInt(1)
+ big2 = big.NewInt(2)
+ big9 = big.NewInt(9)
+ big10 = big.NewInt(10)
+ bigMinus99 = big.NewInt(-99)
+)
+
+// makeDifficultyCalculator creates a difficultyCalculator with the given bomb-delay.
+// the difficulty is calculated with Byzantium rules, which differs from Homestead in
+// how uncles affect the calculation
+func makeDifficultyCalculator(bombDelay *big.Int) func(time uint64, parent *types.Header) *big.Int {
+ // Note, the calculations below looks at the parent number, which is 1 below
+ // the block number. Thus we remove one from the delay given
+ bombDelayFromParent := new(big.Int).Sub(bombDelay, big1)
+ return func(time uint64, parent *types.Header) *big.Int {
+ // https://github.com/ethereum/EIPs/issues/100.
+ // algorithm:
+ // diff = (parent_diff +
+ // (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
+ // ) + 2^(periodCount - 2)
+
+ bigTime := new(big.Int).SetUint64(time)
+ bigParentTime := new(big.Int).SetUint64(parent.Time)
+
+ // holds intermediate values to make the algo easier to read & audit
+ x := new(big.Int)
+ y := new(big.Int)
+
+ // (2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9
+ x.Sub(bigTime, bigParentTime)
+ x.Div(x, big9)
+ if parent.UncleHash == types.EmptyUncleHash {
+ x.Sub(big1, x)
+ } else {
+ x.Sub(big2, x)
+ }
+ // max((2 if len(parent_uncles) else 1) - (block_timestamp - parent_timestamp) // 9, -99)
+ if x.Cmp(bigMinus99) < 0 {
+ x.Set(bigMinus99)
+ }
+ // parent_diff + (parent_diff / 2048 * max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99))
+ y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
+ x.Mul(y, x)
+ x.Add(parent.Difficulty, x)
+
+ // minimum difficulty can ever be (before exponential factor)
+ if x.Cmp(params.MinimumDifficulty) < 0 {
+ x.Set(params.MinimumDifficulty)
+ }
+ // calculate a fake block number for the ice-age delay
+ // Specification: https://eips.ethereum.org/EIPS/eip-1234
+ fakeBlockNumber := new(big.Int)
+ if parent.Number.Cmp(bombDelayFromParent) >= 0 {
+ fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, bombDelayFromParent)
+ }
+ // for the exponential factor
+ periodCount := fakeBlockNumber
+ periodCount.Div(periodCount, expDiffPeriod)
+
+ // the exponential factor, commonly referred to as "the bomb"
+ // diff = diff + 2^(periodCount - 2)
+ if periodCount.Cmp(big1) > 0 {
+ y.Sub(periodCount, big2)
+ y.Exp(big2, y, nil)
+ x.Add(x, y)
+ }
+ return x
+ }
+}
+
+// calcDifficultyHomestead is the difficulty adjustment algorithm. It returns
+// the difficulty that a new block should have when created at time given the
+// parent block's time and difficulty. The calculation uses the Homestead rules.
+func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int {
+ // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
+ // algorithm:
+ // diff = (parent_diff +
+ // (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
+ // ) + 2^(periodCount - 2)
+
+ bigTime := new(big.Int).SetUint64(time)
+ bigParentTime := new(big.Int).SetUint64(parent.Time)
+
+ // holds intermediate values to make the algo easier to read & audit
+ x := new(big.Int)
+ y := new(big.Int)
+
+ // 1 - (block_timestamp - parent_timestamp) // 10
+ x.Sub(bigTime, bigParentTime)
+ x.Div(x, big10)
+ x.Sub(big1, x)
+
+ // max(1 - (block_timestamp - parent_timestamp) // 10, -99)
+ if x.Cmp(bigMinus99) < 0 {
+ x.Set(bigMinus99)
+ }
+ // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
+ y.Div(parent.Difficulty, params.DifficultyBoundDivisor)
+ x.Mul(y, x)
+ x.Add(parent.Difficulty, x)
+
+ // minimum difficulty can ever be (before exponential factor)
+ if x.Cmp(params.MinimumDifficulty) < 0 {
+ x.Set(params.MinimumDifficulty)
+ }
+ // for the exponential factor
+ periodCount := new(big.Int).Add(parent.Number, big1)
+ periodCount.Div(periodCount, expDiffPeriod)
+
+ // the exponential factor, commonly referred to as "the bomb"
+ // diff = diff + 2^(periodCount - 2)
+ if periodCount.Cmp(big1) > 0 {
+ y.Sub(periodCount, big2)
+ y.Exp(big2, y, nil)
+ x.Add(x, y)
+ }
+ return x
+}
+
+// calcDifficultyFrontier is the difficulty adjustment algorithm. It returns the
+// difficulty that a new block should have when created at time given the parent
+// block's time and difficulty. The calculation uses the Frontier rules.
+func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int {
+ diff := new(big.Int)
+ adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor)
+ bigTime := new(big.Int)
+ bigParentTime := new(big.Int)
+
+ bigTime.SetUint64(time)
+ bigParentTime.SetUint64(parent.Time)
+
+ if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
+ diff.Add(parent.Difficulty, adjust)
+ } else {
+ diff.Sub(parent.Difficulty, adjust)
+ }
+ if diff.Cmp(params.MinimumDifficulty) < 0 {
+ diff.Set(params.MinimumDifficulty)
+ }
+
+ periodCount := new(big.Int).Add(parent.Number, big1)
+ periodCount.Div(periodCount, expDiffPeriod)
+ if periodCount.Cmp(big1) > 0 {
+ // diff = diff + 2^(periodCount - 2)
+ expDiff := periodCount.Sub(periodCount, big2)
+ expDiff.Exp(big2, expDiff, nil)
+ diff.Add(diff, expDiff)
+ diff = math.BigMax(diff, params.MinimumDifficulty)
+ }
+ return diff
+}
+
+// VerifySeal implements consensus.Engine, checking whether the given block satisfies
+// the PoW difficulty requirements.
+func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
+ return ethash.verifySeal(chain, header, false)
+}
+
+// verifySeal checks whether a block satisfies the PoW difficulty requirements,
+// either using the usual ethash cache for it, or alternatively using a full DAG
+// to make remote mining fast.
+func (ethash *Ethash) verifySeal(chain consensus.ChainReader, header *types.Header, fulldag bool) error {
+ // If we're running a fake PoW, accept any seal as valid
+ if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
+ time.Sleep(ethash.fakeDelay)
+ if ethash.fakeFail == header.Number.Uint64() {
+ return errInvalidPoW
+ }
+ return nil
+ }
+ // If we're running a shared PoW, delegate verification to it
+ if ethash.shared != nil {
+ return ethash.shared.verifySeal(chain, header, fulldag)
+ }
+ // Ensure that we have a valid difficulty for the block
+ if header.Difficulty.Sign() <= 0 {
+ return errInvalidDifficulty
+ }
+ // Recompute the digest and PoW values
+ number := header.Number.Uint64()
+
+ var (
+ digest []byte
+ result []byte
+ )
+ // If fast-but-heavy PoW verification was requested, use an ethash dataset
+ if fulldag {
+ dataset := ethash.dataset(number, true)
+ if dataset.generated() {
+ digest, result = hashimotoFull(dataset.dataset, ethash.SealHash(header).Bytes(), header.Nonce.Uint64())
+
+ // Datasets are unmapped in a finalizer. Ensure that the dataset stays alive
+ // until after the call to hashimotoFull so it's not unmapped while being used.
+ runtime.KeepAlive(dataset)
+ } else {
+ // Dataset not yet generated, don't hang, use a cache instead
+ fulldag = false
+ }
+ }
+ // If slow-but-light PoW verification was requested (or DAG not yet ready), use an ethash cache
+ if !fulldag {
+ cache := ethash.cache(number)
+
+ size := datasetSize(number)
+ if ethash.config.PowMode == ModeTest {
+ size = 32 * 1024
+ }
+ digest, result = hashimotoLight(size, cache.cache, ethash.SealHash(header).Bytes(), header.Nonce.Uint64())
+
+ // Caches are unmapped in a finalizer. Ensure that the cache stays alive
+ // until after the call to hashimotoLight so it's not unmapped while being used.
+ runtime.KeepAlive(cache)
+ }
+ // Verify the calculated values against the ones provided in the header
+ if !bytes.Equal(header.MixDigest[:], digest) {
+ return errInvalidMixDigest
+ }
+ target := new(big.Int).Div(two256, header.Difficulty)
+ if new(big.Int).SetBytes(result).Cmp(target) > 0 {
+ return errInvalidPoW
+ }
+ return nil
+}
+
+// Prepare implements consensus.Engine, initializing the difficulty field of a
+// header to conform to the ethash protocol. The changes are done inline.
+func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error {
+ parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
+ if parent == nil {
+ return consensus.ErrUnknownAncestor
+ }
+ header.Difficulty = ethash.CalcDifficulty(chain, header.Time, parent)
+ return nil
+}
+
+// Finalize implements consensus.Engine, accumulating the block and uncle rewards,
+// setting the final state on the header
+func (ethash *Ethash) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
+ // Accumulate any block and uncle rewards and commit the final state root
+ accumulateRewards(chain.Config(), state, header, uncles)
+ header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
+}
+
+// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
+// uncle rewards, setting the final state and assembling the block.
+func (ethash *Ethash) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
+ // Accumulate any block and uncle rewards and commit the final state root
+ accumulateRewards(chain.Config(), state, header, uncles)
+ header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
+
+ // Header seems complete, assemble into a block and return
+ return types.NewBlock(header, txs, uncles, receipts), nil
+}
+
+// SealHash returns the hash of a block prior to it being sealed.
+func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
+ hasher := sha3.NewLegacyKeccak256()
+
+ rlp.Encode(hasher, []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,
+ })
+ hasher.Sum(hash[:0])
+ return hash
+}
+
+// Some weird constants to avoid constant memory allocs for them.
+var (
+ big8 = big.NewInt(8)
+ big32 = big.NewInt(32)
+)
+
+// AccumulateRewards credits the coinbase of the given block with the mining
+// reward. The total reward consists of the static block reward and rewards for
+// included uncles. The coinbase of each uncle block is also rewarded.
+func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
+ // Select the correct block reward based on chain progression
+ blockReward := FrontierBlockReward
+ if config.IsByzantium(header.Number) {
+ blockReward = ByzantiumBlockReward
+ }
+ if config.IsConstantinople(header.Number) {
+ blockReward = ConstantinopleBlockReward
+ }
+ // Accumulate the rewards for the miner and any included uncles
+ reward := new(big.Int).Set(blockReward)
+ r := new(big.Int)
+ for _, uncle := range uncles {
+ r.Add(uncle.Number, big8)
+ r.Sub(r, header.Number)
+ r.Mul(r, blockReward)
+ r.Div(r, big8)
+ state.AddBalance(uncle.Coinbase, r)
+
+ r.Div(blockReward, big32)
+ reward.Add(reward, r)
+ }
+ state.AddBalance(header.Coinbase, reward)
+}
diff --git a/consensus/ethash/ethash.go b/consensus/ethash/ethash.go
new file mode 100644
index 0000000..53420d0
--- /dev/null
+++ b/consensus/ethash/ethash.go
@@ -0,0 +1,717 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package ethash implements the ethash proof-of-work consensus engine.
+package ethash
+
+import (
+ "errors"
+ "fmt"
+ "math"
+ "math/big"
+ "math/rand"
+ "os"
+ "path/filepath"
+ "reflect"
+ "runtime"
+ "strconv"
+ "sync"
+ "sync/atomic"
+ "time"
+ "unsafe"
+
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/metrics"
+ mmap "github.com/edsrzf/mmap-go"
+ "github.com/hashicorp/golang-lru/simplelru"
+)
+
+var ErrInvalidDumpMagic = errors.New("invalid dump magic")
+
+var (
+ // two256 is a big integer representing 2^256
+ two256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
+
+ // sharedEthash is a full instance that can be shared between multiple users.
+ sharedEthash = New(Config{"", 3, 0, "", 1, 0, ModeNormal}, nil, false)
+
+ // algorithmRevision is the data structure version used for file naming.
+ algorithmRevision = 23
+
+ // dumpMagic is a dataset dump header to sanity check a data dump.
+ dumpMagic = []uint32{0xbaddcafe, 0xfee1dead}
+)
+
+// isLittleEndian returns whether the local system is running in little or big
+// endian byte order.
+func isLittleEndian() bool {
+ n := uint32(0x01020304)
+ return *(*byte)(unsafe.Pointer(&n)) == 0x04
+}
+
+// memoryMap tries to memory map a file of uint32s for read only access.
+func memoryMap(path string) (*os.File, mmap.MMap, []uint32, error) {
+ file, err := os.OpenFile(path, os.O_RDONLY, 0644)
+ if err != nil {
+ return nil, nil, nil, err
+ }
+ mem, buffer, err := memoryMapFile(file, false)
+ if err != nil {
+ file.Close()
+ return nil, nil, nil, err
+ }
+ for i, magic := range dumpMagic {
+ if buffer[i] != magic {
+ mem.Unmap()
+ file.Close()
+ return nil, nil, nil, ErrInvalidDumpMagic
+ }
+ }
+ return file, mem, buffer[len(dumpMagic):], err
+}
+
+// memoryMapFile tries to memory map an already opened file descriptor.
+func memoryMapFile(file *os.File, write bool) (mmap.MMap, []uint32, error) {
+ // Try to memory map the file
+ flag := mmap.RDONLY
+ if write {
+ flag = mmap.RDWR
+ }
+ mem, err := mmap.Map(file, flag, 0)
+ if err != nil {
+ return nil, nil, err
+ }
+ // Yay, we managed to memory map the file, here be dragons
+ header := *(*reflect.SliceHeader)(unsafe.Pointer(&mem))
+ header.Len /= 4
+ header.Cap /= 4
+
+ return mem, *(*[]uint32)(unsafe.Pointer(&header)), nil
+}
+
+// memoryMapAndGenerate tries to memory map a temporary file of uint32s for write
+// access, fill it with the data from a generator and then move it into the final
+// path requested.
+func memoryMapAndGenerate(path string, size uint64, generator func(buffer []uint32)) (*os.File, mmap.MMap, []uint32, error) {
+ // Ensure the data folder exists
+ if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
+ return nil, nil, nil, err
+ }
+ // Create a huge temporary empty file to fill with data
+ temp := path + "." + strconv.Itoa(rand.Int())
+
+ dump, err := os.Create(temp)
+ if err != nil {
+ return nil, nil, nil, err
+ }
+ if err = dump.Truncate(int64(len(dumpMagic))*4 + int64(size)); err != nil {
+ return nil, nil, nil, err
+ }
+ // Memory map the file for writing and fill it with the generator
+ mem, buffer, err := memoryMapFile(dump, true)
+ if err != nil {
+ dump.Close()
+ return nil, nil, nil, err
+ }
+ copy(buffer, dumpMagic)
+
+ data := buffer[len(dumpMagic):]
+ generator(data)
+
+ if err := mem.Unmap(); err != nil {
+ return nil, nil, nil, err
+ }
+ if err := dump.Close(); err != nil {
+ return nil, nil, nil, err
+ }
+ if err := os.Rename(temp, path); err != nil {
+ return nil, nil, nil, err
+ }
+ return memoryMap(path)
+}
+
+// lru tracks caches or datasets by their last use time, keeping at most N of them.
+type lru struct {
+ what string
+ new func(epoch uint64) interface{}
+ mu sync.Mutex
+ // Items are kept in a LRU cache, but there is a special case:
+ // We always keep an item for (highest seen epoch) + 1 as the 'future item'.
+ cache *simplelru.LRU
+ future uint64
+ futureItem interface{}
+}
+
+// newlru create a new least-recently-used cache for either the verification caches
+// or the mining datasets.
+func newlru(what string, maxItems int, new func(epoch uint64) interface{}) *lru {
+ if maxItems <= 0 {
+ maxItems = 1
+ }
+ cache, _ := simplelru.NewLRU(maxItems, func(key, value interface{}) {
+ log.Trace("Evicted ethash "+what, "epoch", key)
+ })
+ return &lru{what: what, new: new, cache: cache}
+}
+
+// get retrieves or creates an item for the given epoch. The first return value is always
+// non-nil. The second return value is non-nil if lru thinks that an item will be useful in
+// the near future.
+func (lru *lru) get(epoch uint64) (item, future interface{}) {
+ lru.mu.Lock()
+ defer lru.mu.Unlock()
+
+ // Get or create the item for the requested epoch.
+ item, ok := lru.cache.Get(epoch)
+ if !ok {
+ if lru.future > 0 && lru.future == epoch {
+ item = lru.futureItem
+ } else {
+ log.Trace("Requiring new ethash "+lru.what, "epoch", epoch)
+ item = lru.new(epoch)
+ }
+ lru.cache.Add(epoch, item)
+ }
+ // Update the 'future item' if epoch is larger than previously seen.
+ if epoch < maxEpoch-1 && lru.future < epoch+1 {
+ log.Trace("Requiring new future ethash "+lru.what, "epoch", epoch+1)
+ future = lru.new(epoch + 1)
+ lru.future = epoch + 1
+ lru.futureItem = future
+ }
+ return item, future
+}
+
+// cache wraps an ethash cache with some metadata to allow easier concurrent use.
+type cache struct {
+ epoch uint64 // Epoch for which this cache is relevant
+ dump *os.File // File descriptor of the memory mapped cache
+ mmap mmap.MMap // Memory map itself to unmap before releasing
+ cache []uint32 // The actual cache data content (may be memory mapped)
+ once sync.Once // Ensures the cache is generated only once
+}
+
+// newCache creates a new ethash verification cache and returns it as a plain Go
+// interface to be usable in an LRU cache.
+func newCache(epoch uint64) interface{} {
+ return &cache{epoch: epoch}
+}
+
+// generate ensures that the cache content is generated before use.
+func (c *cache) generate(dir string, limit int, test bool) {
+ c.once.Do(func() {
+ size := cacheSize(c.epoch*epochLength + 1)
+ seed := seedHash(c.epoch*epochLength + 1)
+ if test {
+ size = 1024
+ }
+ // If we don't store anything on disk, generate and return.
+ if dir == "" {
+ c.cache = make([]uint32, size/4)
+ generateCache(c.cache, c.epoch, seed)
+ return
+ }
+ // Disk storage is needed, this will get fancy
+ var endian string
+ if !isLittleEndian() {
+ endian = ".be"
+ }
+ path := filepath.Join(dir, fmt.Sprintf("cache-R%d-%x%s", algorithmRevision, seed[:8], endian))
+ logger := log.New("epoch", c.epoch)
+
+ // We're about to mmap the file, ensure that the mapping is cleaned up when the
+ // cache becomes unused.
+ runtime.SetFinalizer(c, (*cache).finalizer)
+
+ // Try to load the file from disk and memory map it
+ var err error
+ c.dump, c.mmap, c.cache, err = memoryMap(path)
+ if err == nil {
+ logger.Debug("Loaded old ethash cache from disk")
+ return
+ }
+ logger.Debug("Failed to load old ethash cache", "err", err)
+
+ // No previous cache available, create a new cache file to fill
+ c.dump, c.mmap, c.cache, err = memoryMapAndGenerate(path, size, func(buffer []uint32) { generateCache(buffer, c.epoch, seed) })
+ if err != nil {
+ logger.Error("Failed to generate mapped ethash cache", "err", err)
+
+ c.cache = make([]uint32, size/4)
+ generateCache(c.cache, c.epoch, seed)
+ }
+ // Iterate over all previous instances and delete old ones
+ for ep := int(c.epoch) - limit; ep >= 0; ep-- {
+ seed := seedHash(uint64(ep)*epochLength + 1)
+ path := filepath.Join(dir, fmt.Sprintf("cache-R%d-%x%s", algorithmRevision, seed[:8], endian))
+ os.Remove(path)
+ }
+ })
+}
+
+// finalizer unmaps the memory and closes the file.
+func (c *cache) finalizer() {
+ if c.mmap != nil {
+ c.mmap.Unmap()
+ c.dump.Close()
+ c.mmap, c.dump = nil, nil
+ }
+}
+
+// dataset wraps an ethash dataset with some metadata to allow easier concurrent use.
+type dataset struct {
+ epoch uint64 // Epoch for which this cache is relevant
+ dump *os.File // File descriptor of the memory mapped cache
+ mmap mmap.MMap // Memory map itself to unmap before releasing
+ dataset []uint32 // The actual cache data content
+ once sync.Once // Ensures the cache is generated only once
+ done uint32 // Atomic flag to determine generation status
+}
+
+// newDataset creates a new ethash mining dataset and returns it as a plain Go
+// interface to be usable in an LRU cache.
+func newDataset(epoch uint64) interface{} {
+ return &dataset{epoch: epoch}
+}
+
+// generate ensures that the dataset content is generated before use.
+func (d *dataset) generate(dir string, limit int, test bool) {
+ d.once.Do(func() {
+ // Mark the dataset generated after we're done. This is needed for remote
+ defer atomic.StoreUint32(&d.done, 1)
+
+ csize := cacheSize(d.epoch*epochLength + 1)
+ dsize := datasetSize(d.epoch*epochLength + 1)
+ seed := seedHash(d.epoch*epochLength + 1)
+ if test {
+ csize = 1024
+ dsize = 32 * 1024
+ }
+ // If we don't store anything on disk, generate and return
+ if dir == "" {
+ cache := make([]uint32, csize/4)
+ generateCache(cache, d.epoch, seed)
+
+ d.dataset = make([]uint32, dsize/4)
+ generateDataset(d.dataset, d.epoch, cache)
+
+ return
+ }
+ // Disk storage is needed, this will get fancy
+ var endian string
+ if !isLittleEndian() {
+ endian = ".be"
+ }
+ path := filepath.Join(dir, fmt.Sprintf("full-R%d-%x%s", algorithmRevision, seed[:8], endian))
+ logger := log.New("epoch", d.epoch)
+
+ // We're about to mmap the file, ensure that the mapping is cleaned up when the
+ // cache becomes unused.
+ runtime.SetFinalizer(d, (*dataset).finalizer)
+
+ // Try to load the file from disk and memory map it
+ var err error
+ d.dump, d.mmap, d.dataset, err = memoryMap(path)
+ if err == nil {
+ logger.Debug("Loaded old ethash dataset from disk")
+ return
+ }
+ logger.Debug("Failed to load old ethash dataset", "err", err)
+
+ // No previous dataset available, create a new dataset file to fill
+ cache := make([]uint32, csize/4)
+ generateCache(cache, d.epoch, seed)
+
+ d.dump, d.mmap, d.dataset, err = memoryMapAndGenerate(path, dsize, func(buffer []uint32) { generateDataset(buffer, d.epoch, cache) })
+ if err != nil {
+ logger.Error("Failed to generate mapped ethash dataset", "err", err)
+
+ d.dataset = make([]uint32, dsize/2)
+ generateDataset(d.dataset, d.epoch, cache)
+ }
+ // Iterate over all previous instances and delete old ones
+ for ep := int(d.epoch) - limit; ep >= 0; ep-- {
+ seed := seedHash(uint64(ep)*epochLength + 1)
+ path := filepath.Join(dir, fmt.Sprintf("full-R%d-%x%s", algorithmRevision, seed[:8], endian))
+ os.Remove(path)
+ }
+ })
+}
+
+// generated returns whether this particular dataset finished generating already
+// or not (it may not have been started at all). This is useful for remote miners
+// to default to verification caches instead of blocking on DAG generations.
+func (d *dataset) generated() bool {
+ return atomic.LoadUint32(&d.done) == 1
+}
+
+// finalizer closes any file handlers and memory maps open.
+func (d *dataset) finalizer() {
+ if d.mmap != nil {
+ d.mmap.Unmap()
+ d.dump.Close()
+ d.mmap, d.dump = nil, nil
+ }
+}
+
+// MakeCache generates a new ethash cache and optionally stores it to disk.
+func MakeCache(block uint64, dir string) {
+ c := cache{epoch: block / epochLength}
+ c.generate(dir, math.MaxInt32, false)
+}
+
+// MakeDataset generates a new ethash dataset and optionally stores it to disk.
+func MakeDataset(block uint64, dir string) {
+ d := dataset{epoch: block / epochLength}
+ d.generate(dir, math.MaxInt32, false)
+}
+
+// Mode defines the type and amount of PoW verification an ethash engine makes.
+type Mode uint
+
+const (
+ ModeNormal Mode = iota
+ ModeShared
+ ModeTest
+ ModeFake
+ ModeFullFake
+)
+
+// Config are the configuration parameters of the ethash.
+type Config struct {
+ CacheDir string
+ CachesInMem int
+ CachesOnDisk int
+ DatasetDir string
+ DatasetsInMem int
+ DatasetsOnDisk int
+ PowMode Mode
+}
+
+// sealTask wraps a seal block with relative result channel for remote sealer thread.
+type sealTask struct {
+ block *types.Block
+ results chan<- *types.Block
+}
+
+// mineResult wraps the pow solution parameters for the specified block.
+type mineResult struct {
+ nonce types.BlockNonce
+ mixDigest common.Hash
+ hash common.Hash
+
+ errc chan error
+}
+
+// hashrate wraps the hash rate submitted by the remote sealer.
+type hashrate struct {
+ id common.Hash
+ ping time.Time
+ rate uint64
+
+ done chan struct{}
+}
+
+// sealWork wraps a seal work package for remote sealer.
+type sealWork struct {
+ errc chan error
+ res chan [4]string
+}
+
+// Ethash is a consensus engine based on proof-of-work implementing the ethash
+// algorithm.
+type Ethash struct {
+ config Config
+
+ caches *lru // In memory caches to avoid regenerating too often
+ datasets *lru // In memory datasets to avoid regenerating too often
+
+ // Mining related fields
+ rand *rand.Rand // Properly seeded random source for nonces
+ threads int // Number of threads to mine on if mining
+ update chan struct{} // Notification channel to update mining parameters
+ hashrate metrics.Meter // Meter tracking the average hashrate
+
+ // Remote sealer related fields
+ workCh chan *sealTask // Notification channel to push new work and relative result channel to remote sealer
+ fetchWorkCh chan *sealWork // Channel used for remote sealer to fetch mining work
+ submitWorkCh chan *mineResult // Channel used for remote sealer to submit their mining result
+ fetchRateCh chan chan uint64 // Channel used to gather submitted hash rate for local or remote sealer.
+ submitRateCh chan *hashrate // Channel used for remote sealer to submit their mining hashrate
+
+ // The fields below are hooks for testing
+ shared *Ethash // Shared PoW verifier to avoid cache regeneration
+ fakeFail uint64 // Block number which fails PoW check even in fake mode
+ fakeDelay time.Duration // Time delay to sleep for before returning from verify
+
+ lock sync.Mutex // Ensures thread safety for the in-memory caches and mining fields
+ closeOnce sync.Once // Ensures exit channel will not be closed twice.
+ exitCh chan chan error // Notification channel to exiting backend threads
+}
+
+// New creates a full sized ethash PoW scheme and starts a background thread for
+// remote mining, also optionally notifying a batch of remote services of new work
+// packages.
+func New(config Config, notify []string, noverify bool) *Ethash {
+ if config.CachesInMem <= 0 {
+ log.Warn("One ethash cache must always be in memory", "requested", config.CachesInMem)
+ config.CachesInMem = 1
+ }
+ if config.CacheDir != "" && config.CachesOnDisk > 0 {
+ log.Info("Disk storage enabled for ethash caches", "dir", config.CacheDir, "count", config.CachesOnDisk)
+ }
+ if config.DatasetDir != "" && config.DatasetsOnDisk > 0 {
+ log.Info("Disk storage enabled for ethash DAGs", "dir", config.DatasetDir, "count", config.DatasetsOnDisk)
+ }
+ ethash := &Ethash{
+ config: config,
+ caches: newlru("cache", config.CachesInMem, newCache),
+ datasets: newlru("dataset", config.DatasetsInMem, newDataset),
+ update: make(chan struct{}),
+ hashrate: metrics.NewMeterForced(),
+ workCh: make(chan *sealTask),
+ fetchWorkCh: make(chan *sealWork),
+ submitWorkCh: make(chan *mineResult),
+ fetchRateCh: make(chan chan uint64),
+ submitRateCh: make(chan *hashrate),
+ exitCh: make(chan chan error),
+ }
+ go ethash.remote(notify, noverify)
+ return ethash
+}
+
+// NewTester creates a small sized ethash PoW scheme useful only for testing
+// purposes.
+func NewTester(notify []string, noverify bool) *Ethash {
+ ethash := &Ethash{
+ config: Config{PowMode: ModeTest},
+ caches: newlru("cache", 1, newCache),
+ datasets: newlru("dataset", 1, newDataset),
+ update: make(chan struct{}),
+ hashrate: metrics.NewMeterForced(),
+ workCh: make(chan *sealTask),
+ fetchWorkCh: make(chan *sealWork),
+ submitWorkCh: make(chan *mineResult),
+ fetchRateCh: make(chan chan uint64),
+ submitRateCh: make(chan *hashrate),
+ exitCh: make(chan chan error),
+ }
+ go ethash.remote(notify, noverify)
+ return ethash
+}
+
+// NewFaker creates a ethash consensus engine with a fake PoW scheme that accepts
+// all blocks' seal as valid, though they still have to conform to the Ethereum
+// consensus rules.
+func NewFaker() *Ethash {
+ return &Ethash{
+ config: Config{
+ PowMode: ModeFake,
+ },
+ }
+}
+
+// NewFakeFailer creates a ethash consensus engine with a fake PoW scheme that
+// accepts all blocks as valid apart from the single one specified, though they
+// still have to conform to the Ethereum consensus rules.
+func NewFakeFailer(fail uint64) *Ethash {
+ return &Ethash{
+ config: Config{
+ PowMode: ModeFake,
+ },
+ fakeFail: fail,
+ }
+}
+
+// NewFakeDelayer creates a ethash consensus engine with a fake PoW scheme that
+// accepts all blocks as valid, but delays verifications by some time, though
+// they still have to conform to the Ethereum consensus rules.
+func NewFakeDelayer(delay time.Duration) *Ethash {
+ return &Ethash{
+ config: Config{
+ PowMode: ModeFake,
+ },
+ fakeDelay: delay,
+ }
+}
+
+// NewFullFaker creates an ethash consensus engine with a full fake scheme that
+// accepts all blocks as valid, without checking any consensus rules whatsoever.
+func NewFullFaker() *Ethash {
+ return &Ethash{
+ config: Config{
+ PowMode: ModeFullFake,
+ },
+ }
+}
+
+// NewShared creates a full sized ethash PoW shared between all requesters running
+// in the same process.
+func NewShared() *Ethash {
+ return &Ethash{shared: sharedEthash}
+}
+
+// Close closes the exit channel to notify all backend threads exiting.
+func (ethash *Ethash) Close() error {
+ var err error
+ ethash.closeOnce.Do(func() {
+ // Short circuit if the exit channel is not allocated.
+ if ethash.exitCh == nil {
+ return
+ }
+ errc := make(chan error)
+ ethash.exitCh <- errc
+ err = <-errc
+ close(ethash.exitCh)
+ })
+ return err
+}
+
+// cache tries to retrieve a verification cache for the specified block number
+// by first checking against a list of in-memory caches, then against caches
+// stored on disk, and finally generating one if none can be found.
+func (ethash *Ethash) cache(block uint64) *cache {
+ epoch := block / epochLength
+ currentI, futureI := ethash.caches.get(epoch)
+ current := currentI.(*cache)
+
+ // Wait for generation finish.
+ current.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == ModeTest)
+
+ // If we need a new future cache, now's a good time to regenerate it.
+ if futureI != nil {
+ future := futureI.(*cache)
+ go future.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == ModeTest)
+ }
+ return current
+}
+
+// dataset tries to retrieve a mining dataset for the specified block number
+// by first checking against a list of in-memory datasets, then against DAGs
+// stored on disk, and finally generating one if none can be found.
+//
+// If async is specified, not only the future but the current DAG is also
+// generates on a background thread.
+func (ethash *Ethash) dataset(block uint64, async bool) *dataset {
+ // Retrieve the requested ethash dataset
+ epoch := block / epochLength
+ currentI, futureI := ethash.datasets.get(epoch)
+ current := currentI.(*dataset)
+
+ // If async is specified, generate everything in a background thread
+ if async && !current.generated() {
+ go func() {
+ current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
+
+ if futureI != nil {
+ future := futureI.(*dataset)
+ future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
+ }
+ }()
+ } else {
+ // Either blocking generation was requested, or already done
+ current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
+
+ if futureI != nil {
+ future := futureI.(*dataset)
+ go future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
+ }
+ }
+ return current
+}
+
+// Threads returns the number of mining threads currently enabled. This doesn't
+// necessarily mean that mining is running!
+func (ethash *Ethash) Threads() int {
+ ethash.lock.Lock()
+ defer ethash.lock.Unlock()
+
+ return ethash.threads
+}
+
+// SetThreads updates the number of mining threads currently enabled. Calling
+// this method does not start mining, only sets the thread count. If zero is
+// specified, the miner will use all cores of the machine. Setting a thread
+// count below zero is allowed and will cause the miner to idle, without any
+// work being done.
+func (ethash *Ethash) SetThreads(threads int) {
+ ethash.lock.Lock()
+ defer ethash.lock.Unlock()
+
+ // If we're running a shared PoW, set the thread count on that instead
+ if ethash.shared != nil {
+ ethash.shared.SetThreads(threads)
+ return
+ }
+ // Update the threads and ping any running seal to pull in any changes
+ ethash.threads = threads
+ select {
+ case ethash.update <- struct{}{}:
+ default:
+ }
+}
+
+// Hashrate implements PoW, returning the measured rate of the search invocations
+// per second over the last minute.
+// Note the returned hashrate includes local hashrate, but also includes the total
+// hashrate of all remote miner.
+func (ethash *Ethash) Hashrate() float64 {
+ // Short circuit if we are run the ethash in normal/test mode.
+ if ethash.config.PowMode != ModeNormal && ethash.config.PowMode != ModeTest {
+ return ethash.hashrate.Rate1()
+ }
+ var res = make(chan uint64, 1)
+
+ select {
+ case ethash.fetchRateCh <- res:
+ case <-ethash.exitCh:
+ // Return local hashrate only if ethash is stopped.
+ return ethash.hashrate.Rate1()
+ }
+
+ // Gather total submitted hash rate of remote sealers.
+ return ethash.hashrate.Rate1() + float64(<-res)
+}
+
+// APIs implements consensus.Engine, returning the user facing RPC APIs.
+func (ethash *Ethash) APIs(chain consensus.ChainReader) []rpc.API {
+ // In order to ensure backward compatibility, we exposes ethash RPC APIs
+ // to both eth and ethash namespaces.
+ return []rpc.API{
+ {
+ Namespace: "eth",
+ Version: "1.0",
+ Service: &API{ethash},
+ Public: true,
+ },
+ {
+ Namespace: "ethash",
+ Version: "1.0",
+ Service: &API{ethash},
+ Public: true,
+ },
+ }
+}
+
+// SeedHash is the seed to use for generating a verification cache and the mining
+// dataset.
+func SeedHash(block uint64) []byte {
+ return seedHash(block)
+}
diff --git a/consensus/ethash/sealer.go b/consensus/ethash/sealer.go
new file mode 100644
index 0000000..799be05
--- /dev/null
+++ b/consensus/ethash/sealer.go
@@ -0,0 +1,371 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package ethash
+
+import (
+ "bytes"
+ crand "crypto/rand"
+ "encoding/json"
+ "errors"
+ "math"
+ "math/big"
+ "math/rand"
+ "net/http"
+ "runtime"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+const (
+ // staleThreshold is the maximum depth of the acceptable stale but valid ethash solution.
+ staleThreshold = 7
+)
+
+var (
+ errNoMiningWork = errors.New("no mining work available yet")
+ errInvalidSealResult = errors.New("invalid or stale proof-of-work solution")
+)
+
+// Seal implements consensus.Engine, attempting to find a nonce that satisfies
+// the block's difficulty requirements.
+func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
+ // If we're running a fake PoW, simply return a 0 nonce immediately
+ if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
+ header := block.Header()
+ header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{}
+ select {
+ case results <- block.WithSeal(header):
+ default:
+ log.Warn("Sealing result is not read by miner", "mode", "fake", "sealhash", ethash.SealHash(block.Header()))
+ }
+ return nil
+ }
+ // If we're running a shared PoW, delegate sealing to it
+ if ethash.shared != nil {
+ return ethash.shared.Seal(chain, block, results, stop)
+ }
+ // Create a runner and the multiple search threads it directs
+ abort := make(chan struct{})
+
+ ethash.lock.Lock()
+ threads := ethash.threads
+ if ethash.rand == nil {
+ seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
+ if err != nil {
+ ethash.lock.Unlock()
+ return err
+ }
+ ethash.rand = rand.New(rand.NewSource(seed.Int64()))
+ }
+ ethash.lock.Unlock()
+ if threads == 0 {
+ threads = runtime.NumCPU()
+ }
+ if threads < 0 {
+ threads = 0 // Allows disabling local mining without extra logic around local/remote
+ }
+ // Push new work to remote sealer
+ if ethash.workCh != nil {
+ ethash.workCh <- &sealTask{block: block, results: results}
+ }
+ var (
+ pend sync.WaitGroup
+ locals = make(chan *types.Block)
+ )
+ for i := 0; i < threads; i++ {
+ pend.Add(1)
+ go func(id int, nonce uint64) {
+ defer pend.Done()
+ ethash.mine(block, id, nonce, abort, locals)
+ }(i, uint64(ethash.rand.Int63()))
+ }
+ // Wait until sealing is terminated or a nonce is found
+ go func() {
+ var result *types.Block
+ select {
+ case <-stop:
+ // Outside abort, stop all miner threads
+ close(abort)
+ case result = <-locals:
+ // One of the threads found a block, abort all others
+ select {
+ case results <- result:
+ default:
+ log.Warn("Sealing result is not read by miner", "mode", "local", "sealhash", ethash.SealHash(block.Header()))
+ }
+ close(abort)
+ case <-ethash.update:
+ // Thread count was changed on user request, restart
+ close(abort)
+ if err := ethash.Seal(chain, block, results, stop); err != nil {
+ log.Error("Failed to restart sealing after update", "err", err)
+ }
+ }
+ // Wait for all miners to terminate and return the block
+ pend.Wait()
+ }()
+ return nil
+}
+
+// mine is the actual proof-of-work miner that searches for a nonce starting from
+// seed that results in correct final block difficulty.
+func (ethash *Ethash) mine(block *types.Block, id int, seed uint64, abort chan struct{}, found chan *types.Block) {
+ // Extract some data from the header
+ var (
+ header = block.Header()
+ hash = ethash.SealHash(header).Bytes()
+ target = new(big.Int).Div(two256, header.Difficulty)
+ number = header.Number.Uint64()
+ dataset = ethash.dataset(number, false)
+ )
+ // Start generating random nonces until we abort or find a good one
+ var (
+ attempts = int64(0)
+ nonce = seed
+ )
+ logger := log.New("miner", id)
+ logger.Trace("Started ethash search for new nonces", "seed", seed)
+search:
+ for {
+ select {
+ case <-abort:
+ // Mining terminated, update stats and abort
+ logger.Trace("Ethash nonce search aborted", "attempts", nonce-seed)
+ ethash.hashrate.Mark(attempts)
+ break search
+
+ default:
+ // We don't have to update hash rate on every nonce, so update after after 2^X nonces
+ attempts++
+ if (attempts % (1 << 15)) == 0 {
+ ethash.hashrate.Mark(attempts)
+ attempts = 0
+ }
+ // Compute the PoW value of this nonce
+ digest, result := hashimotoFull(dataset.dataset, hash, nonce)
+ if new(big.Int).SetBytes(result).Cmp(target) <= 0 {
+ // Correct nonce found, create a new header with it
+ header = types.CopyHeader(header)
+ header.Nonce = types.EncodeNonce(nonce)
+ header.MixDigest = common.BytesToHash(digest)
+
+ // Seal and return a block (if still needed)
+ select {
+ case found <- block.WithSeal(header):
+ logger.Trace("Ethash nonce found and reported", "attempts", nonce-seed, "nonce", nonce)
+ case <-abort:
+ logger.Trace("Ethash nonce found but discarded", "attempts", nonce-seed, "nonce", nonce)
+ }
+ break search
+ }
+ nonce++
+ }
+ }
+ // Datasets are unmapped in a finalizer. Ensure that the dataset stays live
+ // during sealing so it's not unmapped while being read.
+ runtime.KeepAlive(dataset)
+}
+
+// remote is a standalone goroutine to handle remote mining related stuff.
+func (ethash *Ethash) remote(notify []string, noverify bool) {
+ var (
+ works = make(map[common.Hash]*types.Block)
+ rates = make(map[common.Hash]hashrate)
+
+ results chan<- *types.Block
+ currentBlock *types.Block
+ currentWork [4]string
+
+ notifyTransport = &http.Transport{}
+ notifyClient = &http.Client{
+ Transport: notifyTransport,
+ Timeout: time.Second,
+ }
+ notifyReqs = make([]*http.Request, len(notify))
+ )
+ // notifyWork notifies all the specified mining endpoints of the availability of
+ // new work to be processed.
+ notifyWork := func() {
+ work := currentWork
+ blob, _ := json.Marshal(work)
+
+ for i, url := range notify {
+ // Terminate any previously pending request and create the new work
+ if notifyReqs[i] != nil {
+ notifyTransport.CancelRequest(notifyReqs[i])
+ }
+ notifyReqs[i], _ = http.NewRequest("POST", url, bytes.NewReader(blob))
+ notifyReqs[i].Header.Set("Content-Type", "application/json")
+
+ // Push the new work concurrently to all the remote nodes
+ go func(req *http.Request, url string) {
+ res, err := notifyClient.Do(req)
+ if err != nil {
+ log.Warn("Failed to notify remote miner", "err", err)
+ } else {
+ log.Trace("Notified remote miner", "miner", url, "hash", log.Lazy{Fn: func() common.Hash { return common.HexToHash(work[0]) }}, "target", work[2])
+ res.Body.Close()
+ }
+ }(notifyReqs[i], url)
+ }
+ }
+ // makeWork creates a work package for external miner.
+ //
+ // The work package consists of 3 strings:
+ // result[0], 32 bytes hex encoded current block header pow-hash
+ // result[1], 32 bytes hex encoded seed hash used for DAG
+ // result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
+ // result[3], hex encoded block number
+ makeWork := func(block *types.Block) {
+ hash := ethash.SealHash(block.Header())
+
+ currentWork[0] = hash.Hex()
+ currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex()
+ currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex()
+ currentWork[3] = hexutil.EncodeBig(block.Number())
+
+ // Trace the seal work fetched by remote sealer.
+ currentBlock = block
+ works[hash] = block
+ }
+ // submitWork verifies the submitted pow solution, returning
+ // whether the solution was accepted or not (not can be both a bad pow as well as
+ // any other error, like no pending work or stale mining result).
+ submitWork := func(nonce types.BlockNonce, mixDigest common.Hash, sealhash common.Hash) bool {
+ if currentBlock == nil {
+ log.Error("Pending work without block", "sealhash", sealhash)
+ return false
+ }
+ // Make sure the work submitted is present
+ block := works[sealhash]
+ if block == nil {
+ log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", currentBlock.NumberU64())
+ return false
+ }
+ // Verify the correctness of submitted result.
+ header := block.Header()
+ header.Nonce = nonce
+ header.MixDigest = mixDigest
+
+ start := time.Now()
+ if !noverify {
+ if err := ethash.verifySeal(nil, header, true); err != nil {
+ log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)), "err", err)
+ return false
+ }
+ }
+ // Make sure the result channel is assigned.
+ if results == nil {
+ log.Warn("Ethash result channel is empty, submitted mining result is rejected")
+ return false
+ }
+ log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)))
+
+ // Solutions seems to be valid, return to the miner and notify acceptance.
+ solution := block.WithSeal(header)
+
+ // The submitted solution is within the scope of acceptance.
+ if solution.NumberU64()+staleThreshold > currentBlock.NumberU64() {
+ select {
+ case results <- solution:
+ log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
+ return true
+ default:
+ log.Warn("Sealing result is not read by miner", "mode", "remote", "sealhash", sealhash)
+ return false
+ }
+ }
+ // The submitted block is too old to accept, drop it.
+ log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash())
+ return false
+ }
+
+ ticker := time.NewTicker(5 * time.Second)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case work := <-ethash.workCh:
+ // Update current work with new received block.
+ // Note same work can be past twice, happens when changing CPU threads.
+ results = work.results
+
+ makeWork(work.block)
+
+ // Notify and requested URLs of the new work availability
+ notifyWork()
+
+ case work := <-ethash.fetchWorkCh:
+ // Return current mining work to remote miner.
+ if currentBlock == nil {
+ work.errc <- errNoMiningWork
+ } else {
+ work.res <- currentWork
+ }
+
+ case result := <-ethash.submitWorkCh:
+ // Verify submitted PoW solution based on maintained mining blocks.
+ if submitWork(result.nonce, result.mixDigest, result.hash) {
+ result.errc <- nil
+ } else {
+ result.errc <- errInvalidSealResult
+ }
+
+ case result := <-ethash.submitRateCh:
+ // Trace remote sealer's hash rate by submitted value.
+ rates[result.id] = hashrate{rate: result.rate, ping: time.Now()}
+ close(result.done)
+
+ case req := <-ethash.fetchRateCh:
+ // Gather all hash rate submitted by remote sealer.
+ var total uint64
+ for _, rate := range rates {
+ // this could overflow
+ total += rate.rate
+ }
+ req <- total
+
+ case <-ticker.C:
+ // Clear stale submitted hash rate.
+ for id, rate := range rates {
+ if time.Since(rate.ping) > 10*time.Second {
+ delete(rates, id)
+ }
+ }
+ // Clear stale pending blocks
+ if currentBlock != nil {
+ for hash, block := range works {
+ if block.NumberU64()+staleThreshold <= currentBlock.NumberU64() {
+ delete(works, hash)
+ }
+ }
+ }
+
+ case errc := <-ethash.exitCh:
+ // Exit remote loop if ethash is closed and return relevant error.
+ errc <- nil
+ log.Trace("Ethash remote sealer is exiting")
+ return
+ }
+ }
+}
diff --git a/consensus/misc/dao.go b/consensus/misc/dao.go
new file mode 100644
index 0000000..8ce79a2
--- /dev/null
+++ b/consensus/misc/dao.go
@@ -0,0 +1,85 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package misc
+
+import (
+ "bytes"
+ "errors"
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+)
+
+var (
+ // ErrBadProDAOExtra is returned if a header doens't support the DAO fork on a
+ // pro-fork client.
+ ErrBadProDAOExtra = errors.New("bad DAO pro-fork extra-data")
+
+ // ErrBadNoDAOExtra is returned if a header does support the DAO fork on a no-
+ // fork client.
+ ErrBadNoDAOExtra = errors.New("bad DAO no-fork extra-data")
+)
+
+// VerifyDAOHeaderExtraData validates the extra-data field of a block header to
+// ensure it conforms to DAO hard-fork rules.
+//
+// DAO hard-fork extension to the header validity:
+// a) if the node is no-fork, do not accept blocks in the [fork, fork+10) range
+// with the fork specific extra-data set
+// b) if the node is pro-fork, require blocks in the specific range to have the
+// unique extra-data set.
+func VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header) error {
+ // Short circuit validation if the node doesn't care about the DAO fork
+ if config.DAOForkBlock == nil {
+ return nil
+ }
+ // Make sure the block is within the fork's modified extra-data range
+ limit := new(big.Int).Add(config.DAOForkBlock, params.DAOForkExtraRange)
+ if header.Number.Cmp(config.DAOForkBlock) < 0 || header.Number.Cmp(limit) >= 0 {
+ return nil
+ }
+ // Depending on whether we support or oppose the fork, validate the extra-data contents
+ if config.DAOForkSupport {
+ if !bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
+ return ErrBadProDAOExtra
+ }
+ } else {
+ if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
+ return ErrBadNoDAOExtra
+ }
+ }
+ // All ok, header has the same extra-data we expect
+ return nil
+}
+
+// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
+// rules, transferring all balances of a set of DAO accounts to a single refund
+// contract.
+func ApplyDAOHardFork(statedb *state.StateDB) {
+ // Retrieve the contract to refund balances into
+ if !statedb.Exist(params.DAORefundContract) {
+ statedb.CreateAccount(params.DAORefundContract)
+ }
+
+ // Move every DAO account and extra-balance account funds into the refund contract
+ for _, addr := range params.DAODrainList() {
+ statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr))
+ statedb.SetBalance(addr, new(big.Int))
+ }
+}
diff --git a/consensus/misc/forks.go b/consensus/misc/forks.go
new file mode 100644
index 0000000..06792b0
--- /dev/null
+++ b/consensus/misc/forks.go
@@ -0,0 +1,43 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package misc
+
+import (
+ "fmt"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// VerifyForkHashes verifies that blocks conforming to network hard-forks do have
+// the correct hashes, to avoid clients going off on different chains. This is an
+// optional feature.
+func VerifyForkHashes(config *params.ChainConfig, header *types.Header, uncle bool) error {
+ // We don't care about uncles
+ if uncle {
+ return nil
+ }
+ // If the homestead reprice hash is set, validate it
+ if config.EIP150Block != nil && config.EIP150Block.Cmp(header.Number) == 0 {
+ if config.EIP150Hash != (common.Hash{}) && config.EIP150Hash != header.Hash() {
+ return fmt.Errorf("homestead gas reprice fork: have 0x%x, want 0x%x", header.Hash(), config.EIP150Hash)
+ }
+ }
+ // All ok, return
+ return nil
+}
diff --git a/core/block_validator.go b/core/block_validator.go
index ae6cd4d..f6cdafe 100644
--- a/core/block_validator.go
+++ b/core/block_validator.go
@@ -19,10 +19,10 @@ package core
import (
"fmt"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/params"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
)
// BlockValidator is responsible for validating block headers, uncles and
diff --git a/core/blockchain.go b/core/blockchain.go
index dddca62..64c2451 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -27,19 +27,19 @@ import (
"sync/atomic"
"time"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/mclock"
"github.com/ava-labs/go-ethereum/common/prque"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/metrics"
- "github.com/ava-labs/go-ethereum/params"
"github.com/ava-labs/go-ethereum/rlp"
"github.com/ava-labs/go-ethereum/trie"
"github.com/hashicorp/golang-lru"
diff --git a/core/blockchain_insert.go b/core/blockchain_insert.go
index afcbb2b..8991a97 100644
--- a/core/blockchain_insert.go
+++ b/core/blockchain_insert.go
@@ -19,9 +19,9 @@ package core
import (
"time"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/mclock"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/log"
)
diff --git a/core/bloombits/doc.go b/core/bloombits/doc.go
new file mode 100644
index 0000000..3d159e7
--- /dev/null
+++ b/core/bloombits/doc.go
@@ -0,0 +1,18 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package bloombits implements bloom filtering on batches of data.
+package bloombits
diff --git a/core/bloombits/generator.go b/core/bloombits/generator.go
new file mode 100644
index 0000000..3dd54b6
--- /dev/null
+++ b/core/bloombits/generator.go
@@ -0,0 +1,93 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bloombits
+
+import (
+ "errors"
+
+ "github.com/ava-labs/coreth/core/types"
+)
+
+var (
+ // errSectionOutOfBounds is returned if the user tried to add more bloom filters
+ // to the batch than available space, or if tries to retrieve above the capacity.
+ errSectionOutOfBounds = errors.New("section out of bounds")
+
+ // errBloomBitOutOfBounds is returned if the user tried to retrieve specified
+ // bit bloom above the capacity.
+ errBloomBitOutOfBounds = errors.New("bloom bit out of bounds")
+)
+
+// Generator takes a number of bloom filters and generates the rotated bloom bits
+// to be used for batched filtering.
+type Generator struct {
+ blooms [types.BloomBitLength][]byte // Rotated blooms for per-bit matching
+ sections uint // Number of sections to batch together
+ nextSec uint // Next section to set when adding a bloom
+}
+
+// NewGenerator creates a rotated bloom generator that can iteratively fill a
+// batched bloom filter's bits.
+func NewGenerator(sections uint) (*Generator, error) {
+ if sections%8 != 0 {
+ return nil, errors.New("section count not multiple of 8")
+ }
+ b := &Generator{sections: sections}
+ for i := 0; i < types.BloomBitLength; i++ {
+ b.blooms[i] = make([]byte, sections/8)
+ }
+ return b, nil
+}
+
+// AddBloom takes a single bloom filter and sets the corresponding bit column
+// in memory accordingly.
+func (b *Generator) AddBloom(index uint, bloom types.Bloom) error {
+ // Make sure we're not adding more bloom filters than our capacity
+ if b.nextSec >= b.sections {
+ return errSectionOutOfBounds
+ }
+ if b.nextSec != index {
+ return errors.New("bloom filter with unexpected index")
+ }
+ // Rotate the bloom and insert into our collection
+ byteIndex := b.nextSec / 8
+ bitMask := byte(1) << byte(7-b.nextSec%8)
+
+ for i := 0; i < types.BloomBitLength; i++ {
+ bloomByteIndex := types.BloomByteLength - 1 - i/8
+ bloomBitMask := byte(1) << byte(i%8)
+
+ if (bloom[bloomByteIndex] & bloomBitMask) != 0 {
+ b.blooms[i][byteIndex] |= bitMask
+ }
+ }
+ b.nextSec++
+
+ return nil
+}
+
+// Bitset returns the bit vector belonging to the given bit index after all
+// blooms have been added.
+func (b *Generator) Bitset(idx uint) ([]byte, error) {
+ if b.nextSec != b.sections {
+ return nil, errors.New("bloom not fully generated yet")
+ }
+ if idx >= types.BloomBitLength {
+ return nil, errBloomBitOutOfBounds
+ }
+ return b.blooms[idx], nil
+}
diff --git a/core/bloombits/matcher.go b/core/bloombits/matcher.go
new file mode 100644
index 0000000..fdf296a
--- /dev/null
+++ b/core/bloombits/matcher.go
@@ -0,0 +1,650 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bloombits
+
+import (
+ "bytes"
+ "context"
+ "errors"
+ "math"
+ "sort"
+ "sync"
+ "sync/atomic"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/common/bitutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// bloomIndexes represents the bit indexes inside the bloom filter that belong
+// to some key.
+type bloomIndexes [3]uint
+
+// calcBloomIndexes returns the bloom filter bit indexes belonging to the given key.
+func calcBloomIndexes(b []byte) bloomIndexes {
+ b = crypto.Keccak256(b)
+
+ var idxs bloomIndexes
+ for i := 0; i < len(idxs); i++ {
+ idxs[i] = (uint(b[2*i])<<8)&2047 + uint(b[2*i+1])
+ }
+ return idxs
+}
+
+// partialMatches with a non-nil vector represents a section in which some sub-
+// matchers have already found potential matches. Subsequent sub-matchers will
+// binary AND their matches with this vector. If vector is nil, it represents a
+// section to be processed by the first sub-matcher.
+type partialMatches struct {
+ section uint64
+ bitset []byte
+}
+
+// Retrieval represents a request for retrieval task assignments for a given
+// bit with the given number of fetch elements, or a response for such a request.
+// It can also have the actual results set to be used as a delivery data struct.
+//
+// The contest and error fields are used by the light client to terminate matching
+// early if an error is encountered on some path of the pipeline.
+type Retrieval struct {
+ Bit uint
+ Sections []uint64
+ Bitsets [][]byte
+
+ Context context.Context
+ Error error
+}
+
+// Matcher is a pipelined system of schedulers and logic matchers which perform
+// binary AND/OR operations on the bit-streams, creating a stream of potential
+// blocks to inspect for data content.
+type Matcher struct {
+ sectionSize uint64 // Size of the data batches to filter on
+
+ filters [][]bloomIndexes // Filter the system is matching for
+ schedulers map[uint]*scheduler // Retrieval schedulers for loading bloom bits
+
+ retrievers chan chan uint // Retriever processes waiting for bit allocations
+ counters chan chan uint // Retriever processes waiting for task count reports
+ retrievals chan chan *Retrieval // Retriever processes waiting for task allocations
+ deliveries chan *Retrieval // Retriever processes waiting for task response deliveries
+
+ running uint32 // Atomic flag whether a session is live or not
+}
+
+// NewMatcher creates a new pipeline for retrieving bloom bit streams and doing
+// address and topic filtering on them. Setting a filter component to `nil` is
+// allowed and will result in that filter rule being skipped (OR 0x11...1).
+func NewMatcher(sectionSize uint64, filters [][][]byte) *Matcher {
+ // Create the matcher instance
+ m := &Matcher{
+ sectionSize: sectionSize,
+ schedulers: make(map[uint]*scheduler),
+ retrievers: make(chan chan uint),
+ counters: make(chan chan uint),
+ retrievals: make(chan chan *Retrieval),
+ deliveries: make(chan *Retrieval),
+ }
+ // Calculate the bloom bit indexes for the groups we're interested in
+ m.filters = nil
+
+ for _, filter := range filters {
+ // Gather the bit indexes of the filter rule, special casing the nil filter
+ if len(filter) == 0 {
+ continue
+ }
+ bloomBits := make([]bloomIndexes, len(filter))
+ for i, clause := range filter {
+ if clause == nil {
+ bloomBits = nil
+ break
+ }
+ bloomBits[i] = calcBloomIndexes(clause)
+ }
+ // Accumulate the filter rules if no nil rule was within
+ if bloomBits != nil {
+ m.filters = append(m.filters, bloomBits)
+ }
+ }
+ // For every bit, create a scheduler to load/download the bit vectors
+ for _, bloomIndexLists := range m.filters {
+ for _, bloomIndexList := range bloomIndexLists {
+ for _, bloomIndex := range bloomIndexList {
+ m.addScheduler(bloomIndex)
+ }
+ }
+ }
+ return m
+}
+
+// addScheduler adds a bit stream retrieval scheduler for the given bit index if
+// it has not existed before. If the bit is already selected for filtering, the
+// existing scheduler can be used.
+func (m *Matcher) addScheduler(idx uint) {
+ if _, ok := m.schedulers[idx]; ok {
+ return
+ }
+ m.schedulers[idx] = newScheduler(idx)
+}
+
+// Start starts the matching process and returns a stream of bloom matches in
+// a given range of blocks. If there are no more matches in the range, the result
+// channel is closed.
+func (m *Matcher) Start(ctx context.Context, begin, end uint64, results chan uint64) (*MatcherSession, error) {
+ // Make sure we're not creating concurrent sessions
+ if atomic.SwapUint32(&m.running, 1) == 1 {
+ return nil, errors.New("matcher already running")
+ }
+ defer atomic.StoreUint32(&m.running, 0)
+
+ // Initiate a new matching round
+ session := &MatcherSession{
+ matcher: m,
+ quit: make(chan struct{}),
+ kill: make(chan struct{}),
+ ctx: ctx,
+ }
+ for _, scheduler := range m.schedulers {
+ scheduler.reset()
+ }
+ sink := m.run(begin, end, cap(results), session)
+
+ // Read the output from the result sink and deliver to the user
+ session.pend.Add(1)
+ go func() {
+ defer session.pend.Done()
+ defer close(results)
+
+ for {
+ select {
+ case <-session.quit:
+ return
+
+ case res, ok := <-sink:
+ // New match result found
+ if !ok {
+ return
+ }
+ // Calculate the first and last blocks of the section
+ sectionStart := res.section * m.sectionSize
+
+ first := sectionStart
+ if begin > first {
+ first = begin
+ }
+ last := sectionStart + m.sectionSize - 1
+ if end < last {
+ last = end
+ }
+ // Iterate over all the blocks in the section and return the matching ones
+ for i := first; i <= last; i++ {
+ // Skip the entire byte if no matches are found inside (and we're processing an entire byte!)
+ next := res.bitset[(i-sectionStart)/8]
+ if next == 0 {
+ if i%8 == 0 {
+ i += 7
+ }
+ continue
+ }
+ // Some bit it set, do the actual submatching
+ if bit := 7 - i%8; next&(1<<bit) != 0 {
+ select {
+ case <-session.quit:
+ return
+ case results <- i:
+ }
+ }
+ }
+ }
+ }
+ }()
+ return session, nil
+}
+
+// run creates a daisy-chain of sub-matchers, one for the address set and one
+// for each topic set, each sub-matcher receiving a section only if the previous
+// ones have all found a potential match in one of the blocks of the section,
+// then binary AND-ing its own matches and forwarding the result to the next one.
+//
+// The method starts feeding the section indexes into the first sub-matcher on a
+// new goroutine and returns a sink channel receiving the results.
+func (m *Matcher) run(begin, end uint64, buffer int, session *MatcherSession) chan *partialMatches {
+ // Create the source channel and feed section indexes into
+ source := make(chan *partialMatches, buffer)
+
+ session.pend.Add(1)
+ go func() {
+ defer session.pend.Done()
+ defer close(source)
+
+ for i := begin / m.sectionSize; i <= end/m.sectionSize; i++ {
+ select {
+ case <-session.quit:
+ return
+ case source <- &partialMatches{i, bytes.Repeat([]byte{0xff}, int(m.sectionSize/8))}:
+ }
+ }
+ }()
+ // Assemble the daisy-chained filtering pipeline
+ next := source
+ dist := make(chan *request, buffer)
+
+ for _, bloom := range m.filters {
+ next = m.subMatch(next, dist, bloom, session)
+ }
+ // Start the request distribution
+ session.pend.Add(1)
+ go m.distributor(dist, session)
+
+ return next
+}
+
+// subMatch creates a sub-matcher that filters for a set of addresses or topics, binary OR-s those matches, then
+// binary AND-s the result to the daisy-chain input (source) and forwards it to the daisy-chain output.
+// The matches of each address/topic are calculated by fetching the given sections of the three bloom bit indexes belonging to
+// that address/topic, and binary AND-ing those vectors together.
+func (m *Matcher) subMatch(source chan *partialMatches, dist chan *request, bloom []bloomIndexes, session *MatcherSession) chan *partialMatches {
+ // Start the concurrent schedulers for each bit required by the bloom filter
+ sectionSources := make([][3]chan uint64, len(bloom))
+ sectionSinks := make([][3]chan []byte, len(bloom))
+ for i, bits := range bloom {
+ for j, bit := range bits {
+ sectionSources[i][j] = make(chan uint64, cap(source))
+ sectionSinks[i][j] = make(chan []byte, cap(source))
+
+ m.schedulers[bit].run(sectionSources[i][j], dist, sectionSinks[i][j], session.quit, &session.pend)
+ }
+ }
+
+ process := make(chan *partialMatches, cap(source)) // entries from source are forwarded here after fetches have been initiated
+ results := make(chan *partialMatches, cap(source))
+
+ session.pend.Add(2)
+ go func() {
+ // Tear down the goroutine and terminate all source channels
+ defer session.pend.Done()
+ defer close(process)
+
+ defer func() {
+ for _, bloomSources := range sectionSources {
+ for _, bitSource := range bloomSources {
+ close(bitSource)
+ }
+ }
+ }()
+ // Read sections from the source channel and multiplex into all bit-schedulers
+ for {
+ select {
+ case <-session.quit:
+ return
+
+ case subres, ok := <-source:
+ // New subresult from previous link
+ if !ok {
+ return
+ }
+ // Multiplex the section index to all bit-schedulers
+ for _, bloomSources := range sectionSources {
+ for _, bitSource := range bloomSources {
+ select {
+ case <-session.quit:
+ return
+ case bitSource <- subres.section:
+ }
+ }
+ }
+ // Notify the processor that this section will become available
+ select {
+ case <-session.quit:
+ return
+ case process <- subres:
+ }
+ }
+ }
+ }()
+
+ go func() {
+ // Tear down the goroutine and terminate the final sink channel
+ defer session.pend.Done()
+ defer close(results)
+
+ // Read the source notifications and collect the delivered results
+ for {
+ select {
+ case <-session.quit:
+ return
+
+ case subres, ok := <-process:
+ // Notified of a section being retrieved
+ if !ok {
+ return
+ }
+ // Gather all the sub-results and merge them together
+ var orVector []byte
+ for _, bloomSinks := range sectionSinks {
+ var andVector []byte
+ for _, bitSink := range bloomSinks {
+ var data []byte
+ select {
+ case <-session.quit:
+ return
+ case data = <-bitSink:
+ }
+ if andVector == nil {
+ andVector = make([]byte, int(m.sectionSize/8))
+ copy(andVector, data)
+ } else {
+ bitutil.ANDBytes(andVector, andVector, data)
+ }
+ }
+ if orVector == nil {
+ orVector = andVector
+ } else {
+ bitutil.ORBytes(orVector, orVector, andVector)
+ }
+ }
+
+ if orVector == nil {
+ orVector = make([]byte, int(m.sectionSize/8))
+ }
+ if subres.bitset != nil {
+ bitutil.ANDBytes(orVector, orVector, subres.bitset)
+ }
+ if bitutil.TestBytes(orVector) {
+ select {
+ case <-session.quit:
+ return
+ case results <- &partialMatches{subres.section, orVector}:
+ }
+ }
+ }
+ }
+ }()
+ return results
+}
+
+// distributor receives requests from the schedulers and queues them into a set
+// of pending requests, which are assigned to retrievers wanting to fulfil them.
+func (m *Matcher) distributor(dist chan *request, session *MatcherSession) {
+ defer session.pend.Done()
+
+ var (
+ requests = make(map[uint][]uint64) // Per-bit list of section requests, ordered by section number
+ unallocs = make(map[uint]struct{}) // Bits with pending requests but not allocated to any retriever
+ retrievers chan chan uint // Waiting retrievers (toggled to nil if unallocs is empty)
+ )
+ var (
+ allocs int // Number of active allocations to handle graceful shutdown requests
+ shutdown = session.quit // Shutdown request channel, will gracefully wait for pending requests
+ )
+
+ // assign is a helper method fo try to assign a pending bit an actively
+ // listening servicer, or schedule it up for later when one arrives.
+ assign := func(bit uint) {
+ select {
+ case fetcher := <-m.retrievers:
+ allocs++
+ fetcher <- bit
+ default:
+ // No retrievers active, start listening for new ones
+ retrievers = m.retrievers
+ unallocs[bit] = struct{}{}
+ }
+ }
+
+ for {
+ select {
+ case <-shutdown:
+ // Graceful shutdown requested, wait until all pending requests are honoured
+ if allocs == 0 {
+ return
+ }
+ shutdown = nil
+
+ case <-session.kill:
+ // Pending requests not honoured in time, hard terminate
+ return
+
+ case req := <-dist:
+ // New retrieval request arrived to be distributed to some fetcher process
+ queue := requests[req.bit]
+ index := sort.Search(len(queue), func(i int) bool { return queue[i] >= req.section })
+ requests[req.bit] = append(queue[:index], append([]uint64{req.section}, queue[index:]...)...)
+
+ // If it's a new bit and we have waiting fetchers, allocate to them
+ if len(queue) == 0 {
+ assign(req.bit)
+ }
+
+ case fetcher := <-retrievers:
+ // New retriever arrived, find the lowest section-ed bit to assign
+ bit, best := uint(0), uint64(math.MaxUint64)
+ for idx := range unallocs {
+ if requests[idx][0] < best {
+ bit, best = idx, requests[idx][0]
+ }
+ }
+ // Stop tracking this bit (and alloc notifications if no more work is available)
+ delete(unallocs, bit)
+ if len(unallocs) == 0 {
+ retrievers = nil
+ }
+ allocs++
+ fetcher <- bit
+
+ case fetcher := <-m.counters:
+ // New task count request arrives, return number of items
+ fetcher <- uint(len(requests[<-fetcher]))
+
+ case fetcher := <-m.retrievals:
+ // New fetcher waiting for tasks to retrieve, assign
+ task := <-fetcher
+ if want := len(task.Sections); want >= len(requests[task.Bit]) {
+ task.Sections = requests[task.Bit]
+ delete(requests, task.Bit)
+ } else {
+ task.Sections = append(task.Sections[:0], requests[task.Bit][:want]...)
+ requests[task.Bit] = append(requests[task.Bit][:0], requests[task.Bit][want:]...)
+ }
+ fetcher <- task
+
+ // If anything was left unallocated, try to assign to someone else
+ if len(requests[task.Bit]) > 0 {
+ assign(task.Bit)
+ }
+
+ case result := <-m.deliveries:
+ // New retrieval task response from fetcher, split out missing sections and
+ // deliver complete ones
+ var (
+ sections = make([]uint64, 0, len(result.Sections))
+ bitsets = make([][]byte, 0, len(result.Bitsets))
+ missing = make([]uint64, 0, len(result.Sections))
+ )
+ for i, bitset := range result.Bitsets {
+ if len(bitset) == 0 {
+ missing = append(missing, result.Sections[i])
+ continue
+ }
+ sections = append(sections, result.Sections[i])
+ bitsets = append(bitsets, bitset)
+ }
+ m.schedulers[result.Bit].deliver(sections, bitsets)
+ allocs--
+
+ // Reschedule missing sections and allocate bit if newly available
+ if len(missing) > 0 {
+ queue := requests[result.Bit]
+ for _, section := range missing {
+ index := sort.Search(len(queue), func(i int) bool { return queue[i] >= section })
+ queue = append(queue[:index], append([]uint64{section}, queue[index:]...)...)
+ }
+ requests[result.Bit] = queue
+
+ if len(queue) == len(missing) {
+ assign(result.Bit)
+ }
+ }
+ // If we're in the process of shutting down, terminate
+ if allocs == 0 && shutdown == nil {
+ return
+ }
+ }
+ }
+}
+
+// MatcherSession is returned by a started matcher to be used as a terminator
+// for the actively running matching operation.
+type MatcherSession struct {
+ matcher *Matcher
+
+ closer sync.Once // Sync object to ensure we only ever close once
+ quit chan struct{} // Quit channel to request pipeline termination
+ kill chan struct{} // Term channel to signal non-graceful forced shutdown
+
+ ctx context.Context // Context used by the light client to abort filtering
+ err atomic.Value // Global error to track retrieval failures deep in the chain
+
+ pend sync.WaitGroup
+}
+
+// Close stops the matching process and waits for all subprocesses to terminate
+// before returning. The timeout may be used for graceful shutdown, allowing the
+// currently running retrievals to complete before this time.
+func (s *MatcherSession) Close() {
+ s.closer.Do(func() {
+ // Signal termination and wait for all goroutines to tear down
+ close(s.quit)
+ time.AfterFunc(time.Second, func() { close(s.kill) })
+ s.pend.Wait()
+ })
+}
+
+// Error returns any failure encountered during the matching session.
+func (s *MatcherSession) Error() error {
+ if err := s.err.Load(); err != nil {
+ return err.(error)
+ }
+ return nil
+}
+
+// AllocateRetrieval assigns a bloom bit index to a client process that can either
+// immediately request and fetch the section contents assigned to this bit or wait
+// a little while for more sections to be requested.
+func (s *MatcherSession) AllocateRetrieval() (uint, bool) {
+ fetcher := make(chan uint)
+
+ select {
+ case <-s.quit:
+ return 0, false
+ case s.matcher.retrievers <- fetcher:
+ bit, ok := <-fetcher
+ return bit, ok
+ }
+}
+
+// PendingSections returns the number of pending section retrievals belonging to
+// the given bloom bit index.
+func (s *MatcherSession) PendingSections(bit uint) int {
+ fetcher := make(chan uint)
+
+ select {
+ case <-s.quit:
+ return 0
+ case s.matcher.counters <- fetcher:
+ fetcher <- bit
+ return int(<-fetcher)
+ }
+}
+
+// AllocateSections assigns all or part of an already allocated bit-task queue
+// to the requesting process.
+func (s *MatcherSession) AllocateSections(bit uint, count int) []uint64 {
+ fetcher := make(chan *Retrieval)
+
+ select {
+ case <-s.quit:
+ return nil
+ case s.matcher.retrievals <- fetcher:
+ task := &Retrieval{
+ Bit: bit,
+ Sections: make([]uint64, count),
+ }
+ fetcher <- task
+ return (<-fetcher).Sections
+ }
+}
+
+// DeliverSections delivers a batch of section bit-vectors for a specific bloom
+// bit index to be injected into the processing pipeline.
+func (s *MatcherSession) DeliverSections(bit uint, sections []uint64, bitsets [][]byte) {
+ select {
+ case <-s.kill:
+ return
+ case s.matcher.deliveries <- &Retrieval{Bit: bit, Sections: sections, Bitsets: bitsets}:
+ }
+}
+
+// Multiplex polls the matcher session for retrieval tasks and multiplexes it into
+// the requested retrieval queue to be serviced together with other sessions.
+//
+// This method will block for the lifetime of the session. Even after termination
+// of the session, any request in-flight need to be responded to! Empty responses
+// are fine though in that case.
+func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan *Retrieval) {
+ for {
+ // Allocate a new bloom bit index to retrieve data for, stopping when done
+ bit, ok := s.AllocateRetrieval()
+ if !ok {
+ return
+ }
+ // Bit allocated, throttle a bit if we're below our batch limit
+ if s.PendingSections(bit) < batch {
+ select {
+ case <-s.quit:
+ // Session terminating, we can't meaningfully service, abort
+ s.AllocateSections(bit, 0)
+ s.DeliverSections(bit, []uint64{}, [][]byte{})
+ return
+
+ case <-time.After(wait):
+ // Throttling up, fetch whatever's available
+ }
+ }
+ // Allocate as much as we can handle and request servicing
+ sections := s.AllocateSections(bit, batch)
+ request := make(chan *Retrieval)
+
+ select {
+ case <-s.quit:
+ // Session terminating, we can't meaningfully service, abort
+ s.DeliverSections(bit, sections, make([][]byte, len(sections)))
+ return
+
+ case mux <- request:
+ // Retrieval accepted, something must arrive before we're aborting
+ request <- &Retrieval{Bit: bit, Sections: sections, Context: s.ctx}
+
+ result := <-request
+ if result.Error != nil {
+ s.err.Store(result.Error)
+ s.Close()
+ }
+ s.DeliverSections(result.Bit, result.Sections, result.Bitsets)
+ }
+ }
+}
diff --git a/core/bloombits/scheduler.go b/core/bloombits/scheduler.go
new file mode 100644
index 0000000..6449c74
--- /dev/null
+++ b/core/bloombits/scheduler.go
@@ -0,0 +1,181 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package bloombits
+
+import (
+ "sync"
+)
+
+// request represents a bloom retrieval task to prioritize and pull from the local
+// database or remotely from the network.
+type request struct {
+ section uint64 // Section index to retrieve the a bit-vector from
+ bit uint // Bit index within the section to retrieve the vector of
+}
+
+// response represents the state of a requested bit-vector through a scheduler.
+type response struct {
+ cached []byte // Cached bits to dedup multiple requests
+ done chan struct{} // Channel to allow waiting for completion
+}
+
+// scheduler handles the scheduling of bloom-filter retrieval operations for
+// entire section-batches belonging to a single bloom bit. Beside scheduling the
+// retrieval operations, this struct also deduplicates the requests and caches
+// the results to minimize network/database overhead even in complex filtering
+// scenarios.
+type scheduler struct {
+ bit uint // Index of the bit in the bloom filter this scheduler is responsible for
+ responses map[uint64]*response // Currently pending retrieval requests or already cached responses
+ lock sync.Mutex // Lock protecting the responses from concurrent access
+}
+
+// newScheduler creates a new bloom-filter retrieval scheduler for a specific
+// bit index.
+func newScheduler(idx uint) *scheduler {
+ return &scheduler{
+ bit: idx,
+ responses: make(map[uint64]*response),
+ }
+}
+
+// run creates a retrieval pipeline, receiving section indexes from sections and
+// returning the results in the same order through the done channel. Concurrent
+// runs of the same scheduler are allowed, leading to retrieval task deduplication.
+func (s *scheduler) run(sections chan uint64, dist chan *request, done chan []byte, quit chan struct{}, wg *sync.WaitGroup) {
+ // Create a forwarder channel between requests and responses of the same size as
+ // the distribution channel (since that will block the pipeline anyway).
+ pend := make(chan uint64, cap(dist))
+
+ // Start the pipeline schedulers to forward between user -> distributor -> user
+ wg.Add(2)
+ go s.scheduleRequests(sections, dist, pend, quit, wg)
+ go s.scheduleDeliveries(pend, done, quit, wg)
+}
+
+// reset cleans up any leftovers from previous runs. This is required before a
+// restart to ensure the no previously requested but never delivered state will
+// cause a lockup.
+func (s *scheduler) reset() {
+ s.lock.Lock()
+ defer s.lock.Unlock()
+
+ for section, res := range s.responses {
+ if res.cached == nil {
+ delete(s.responses, section)
+ }
+ }
+}
+
+// scheduleRequests reads section retrieval requests from the input channel,
+// deduplicates the stream and pushes unique retrieval tasks into the distribution
+// channel for a database or network layer to honour.
+func (s *scheduler) scheduleRequests(reqs chan uint64, dist chan *request, pend chan uint64, quit chan struct{}, wg *sync.WaitGroup) {
+ // Clean up the goroutine and pipeline when done
+ defer wg.Done()
+ defer close(pend)
+
+ // Keep reading and scheduling section requests
+ for {
+ select {
+ case <-quit:
+ return
+
+ case section, ok := <-reqs:
+ // New section retrieval requested
+ if !ok {
+ return
+ }
+ // Deduplicate retrieval requests
+ unique := false
+
+ s.lock.Lock()
+ if s.responses[section] == nil {
+ s.responses[section] = &response{
+ done: make(chan struct{}),
+ }
+ unique = true
+ }
+ s.lock.Unlock()
+
+ // Schedule the section for retrieval and notify the deliverer to expect this section
+ if unique {
+ select {
+ case <-quit:
+ return
+ case dist <- &request{bit: s.bit, section: section}:
+ }
+ }
+ select {
+ case <-quit:
+ return
+ case pend <- section:
+ }
+ }
+ }
+}
+
+// scheduleDeliveries reads section acceptance notifications and waits for them
+// to be delivered, pushing them into the output data buffer.
+func (s *scheduler) scheduleDeliveries(pend chan uint64, done chan []byte, quit chan struct{}, wg *sync.WaitGroup) {
+ // Clean up the goroutine and pipeline when done
+ defer wg.Done()
+ defer close(done)
+
+ // Keep reading notifications and scheduling deliveries
+ for {
+ select {
+ case <-quit:
+ return
+
+ case idx, ok := <-pend:
+ // New section retrieval pending
+ if !ok {
+ return
+ }
+ // Wait until the request is honoured
+ s.lock.Lock()
+ res := s.responses[idx]
+ s.lock.Unlock()
+
+ select {
+ case <-quit:
+ return
+ case <-res.done:
+ }
+ // Deliver the result
+ select {
+ case <-quit:
+ return
+ case done <- res.cached:
+ }
+ }
+ }
+}
+
+// deliver is called by the request distributor when a reply to a request arrives.
+func (s *scheduler) deliver(sections []uint64, data [][]byte) {
+ s.lock.Lock()
+ defer s.lock.Unlock()
+
+ for i, section := range sections {
+ if res := s.responses[section]; res != nil && res.cached == nil { // Avoid non-requests and double deliveries
+ res.cached = data[i]
+ close(res.done)
+ }
+ }
+}
diff --git a/core/chain_indexer.go b/core/chain_indexer.go
index 8389e13..a221355 100644
--- a/core/chain_indexer.go
+++ b/core/chain_indexer.go
@@ -24,9 +24,9 @@ import (
"sync/atomic"
"time"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
diff --git a/core/events.go b/core/events.go
index 135d07f..f05e69b 100644
--- a/core/events.go
+++ b/core/events.go
@@ -17,8 +17,8 @@
package core
import (
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
)
// NewTxsEvent is posted when a batch of transactions enter the transaction pool.
diff --git a/core/evm.go b/core/evm.go
index 5c9d178..ecab8e6 100644
--- a/core/evm.go
+++ b/core/evm.go
@@ -19,10 +19,11 @@ package core
import (
"math/big"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
+ "github.com/ava-labs/go-ethereum/log"
)
// ChainContext supports retrieving headers and consensus parameters from the
@@ -45,16 +46,18 @@ func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author
beneficiary = *author
}
return vm.Context{
- CanTransfer: CanTransfer,
- Transfer: Transfer,
- GetHash: GetHashFn(header, chain),
- Origin: msg.From(),
- Coinbase: beneficiary,
- BlockNumber: new(big.Int).Set(header.Number),
- Time: new(big.Int).SetUint64(header.Time),
- Difficulty: new(big.Int).Set(header.Difficulty),
- GasLimit: header.GasLimit,
- GasPrice: new(big.Int).Set(msg.GasPrice()),
+ CanTransfer: CanTransfer,
+ CanTransferMC: CanTransferMC,
+ Transfer: Transfer,
+ TransferMultiCoin: TransferMultiCoin,
+ GetHash: GetHashFn(header, chain),
+ Origin: msg.From(),
+ Coinbase: beneficiary,
+ BlockNumber: new(big.Int).Set(header.Number),
+ Time: new(big.Int).SetUint64(header.Time),
+ Difficulty: new(big.Int).Set(header.Difficulty),
+ GasLimit: header.GasLimit,
+ GasPrice: new(big.Int).Set(msg.GasPrice()),
}
}
@@ -90,8 +93,39 @@ func CanTransfer(db vm.StateDB, addr common.Address, amount *big.Int) bool {
return db.GetBalance(addr).Cmp(amount) >= 0
}
+func CanTransferMC(db vm.StateDB, addr common.Address, to common.Address, coinID *common.Hash, amount *big.Int) int {
+ if coinID == nil {
+ return 0
+ }
+ if !db.IsMultiCoin(addr) {
+ err := db.EnableMultiCoin(addr)
+ log.Debug("try to enable MC", "err", err)
+ }
+ if !(db.IsMultiCoin(addr) && db.IsMultiCoin(to)) {
+ // incompatible
+ return -1
+ }
+ if db.GetBalanceMultiCoin(addr, *coinID).Cmp(amount) >= 0 {
+ return 0
+ }
+ // insufficient balance
+ return 1
+}
+
// 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) {
db.SubBalance(sender, amount)
db.AddBalance(recipient, amount)
}
+
+// Transfer subtracts amount from sender and adds amount to recipient using the given Db
+func TransferMultiCoin(db vm.StateDB, sender, recipient common.Address, coinID *common.Hash, amount *big.Int) {
+ if coinID == nil {
+ return
+ }
+ db.SubBalanceMultiCoin(sender, *coinID, amount)
+ z := &big.Int{}
+ z.Add(amount, big.NewInt(1000000000000000000))
+ log.Info("hi")
+ db.AddBalanceMultiCoin(recipient, *coinID, z)
+}
diff --git a/core/genesis.go b/core/genesis.go
index 78249a7..7cab7bf 100644
--- a/core/genesis.go
+++ b/core/genesis.go
@@ -19,28 +19,90 @@ package core
import (
"bytes"
"encoding/hex"
+ "encoding/json"
"errors"
"fmt"
"math/big"
"strings"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/state"
+ "github.com/ava-labs/go-ethereum/common/math"
"github.com/ava-labs/go-ethereum/crypto"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/params"
"github.com/ava-labs/go-ethereum/rlp"
)
var errGenesisNoConfig = errors.New("genesis has no chain configuration")
-type Genesis = core.Genesis
-type GenesisAlloc = core.GenesisAlloc
-type GenesisAccount = core.GenesisAccount
+// Genesis specifies the header fields, state of a genesis block. It also defines hard
+// fork switch-over blocks through the chain configuration.
+type Genesis struct {
+ Config *params.ChainConfig `json:"config"`
+ Nonce uint64 `json:"nonce"`
+ Timestamp uint64 `json:"timestamp"`
+ ExtraData []byte `json:"extraData"`
+ GasLimit uint64 `json:"gasLimit" gencodec:"required"`
+ Difficulty *big.Int `json:"difficulty" gencodec:"required"`
+ Mixhash common.Hash `json:"mixHash"`
+ Coinbase common.Address `json:"coinbase"`
+ Alloc GenesisAlloc `json:"alloc" gencodec:"required"`
+
+ // These fields are used for consensus tests. Please don't use them
+ // in actual genesis blocks.
+ Number uint64 `json:"number"`
+ GasUsed uint64 `json:"gasUsed"`
+ ParentHash common.Hash `json:"parentHash"`
+}
+
+// GenesisAlloc specifies the initial state that is part of the genesis block.
+type GenesisAlloc map[common.Address]GenesisAccount
+
+func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error {
+ m := make(map[common.UnprefixedAddress]GenesisAccount)
+ if err := json.Unmarshal(data, &m); err != nil {
+ return err
+ }
+ *ga = make(GenesisAlloc)
+ for addr, a := range m {
+ (*ga)[common.Address(addr)] = a
+ }
+ return nil
+}
+
+// GenesisAccount is an account in the state of the genesis block.
+type GenesisAccount struct {
+ Code []byte `json:"code,omitempty"`
+ Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
+ Balance *big.Int `json:"balance" gencodec:"required"`
+ Nonce uint64 `json:"nonce,omitempty"`
+ PrivateKey []byte `json:"secretKey,omitempty"` // for tests
+}
+
+// field type overrides for gencodec
+type genesisSpecMarshaling struct {
+ Nonce math.HexOrDecimal64
+ Timestamp math.HexOrDecimal64
+ ExtraData hexutil.Bytes
+ GasLimit math.HexOrDecimal64
+ GasUsed math.HexOrDecimal64
+ Number math.HexOrDecimal64
+ Difficulty *math.HexOrDecimal256
+ Alloc map[common.UnprefixedAddress]GenesisAccount
+}
+
+type genesisAccountMarshaling struct {
+ Code hexutil.Bytes
+ Balance *math.HexOrDecimal256
+ Nonce math.HexOrDecimal64
+ Storage map[storageJSON]storageJSON
+ PrivateKey hexutil.Bytes
+}
// storageJSON represents a 256 bit byte array, but allows less than 256 bits when
// unmarshaling from hex.
@@ -182,6 +244,86 @@ func configOrDefault(g *Genesis, ghash common.Hash) *params.ChainConfig {
}
}
+// ToBlock creates the genesis block and writes state of a genesis specification
+// to the given database (or discards it if nil).
+func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
+ if db == nil {
+ db = rawdb.NewMemoryDatabase()
+ }
+ statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
+ for addr, account := range g.Alloc {
+ statedb.AddBalance(addr, account.Balance)
+ statedb.SetCode(addr, account.Code)
+ statedb.SetNonce(addr, account.Nonce)
+ for key, value := range account.Storage {
+ statedb.SetState(addr, key, value)
+ }
+ }
+ root := statedb.IntermediateRoot(false)
+ head := &types.Header{
+ Number: new(big.Int).SetUint64(g.Number),
+ Nonce: types.EncodeNonce(g.Nonce),
+ Time: g.Timestamp,
+ ParentHash: g.ParentHash,
+ Extra: g.ExtraData,
+ GasLimit: g.GasLimit,
+ GasUsed: g.GasUsed,
+ Difficulty: g.Difficulty,
+ MixDigest: g.Mixhash,
+ Coinbase: g.Coinbase,
+ Root: root,
+ }
+ if g.GasLimit == 0 {
+ head.GasLimit = params.GenesisGasLimit
+ }
+ if g.Difficulty == nil {
+ head.Difficulty = params.GenesisDifficulty
+ }
+ statedb.Commit(false)
+ statedb.Database().TrieDB().Commit(root, true)
+
+ return types.NewBlock(head, nil, nil, nil)
+}
+
+// Commit writes the block and state of a genesis specification to the database.
+// The block is committed as the canonical head block.
+func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
+ block := g.ToBlock(db)
+ if block.Number().Sign() != 0 {
+ return nil, fmt.Errorf("can't commit genesis block with number > 0")
+ }
+ rawdb.WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty)
+ rawdb.WriteBlock(db, block)
+ rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil)
+ rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
+ rawdb.WriteHeadBlockHash(db, block.Hash())
+ rawdb.WriteHeadFastBlockHash(db, block.Hash())
+ rawdb.WriteHeadHeaderHash(db, block.Hash())
+
+ config := g.Config
+ if config == nil {
+ config = params.AllEthashProtocolChanges
+ }
+ rawdb.WriteChainConfig(db, block.Hash(), config)
+ return block, nil
+}
+
+// MustCommit writes the genesis block and state to db, panicking on error.
+// The block is committed as the canonical head block.
+func (g *Genesis) MustCommit(db ethdb.Database) *types.Block {
+ block, err := g.Commit(db)
+ if err != nil {
+ panic(err)
+ }
+ return block
+}
+
+// GenesisBlockForTesting creates and writes a block in which addr has the given wei balance.
+func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
+ g := Genesis{Alloc: GenesisAlloc{addr: {Balance: balance}}}
+ return g.MustCommit(db)
+}
+
// DefaultGenesisBlock returns the Ethereum main net genesis block.
func DefaultGenesisBlock() *Genesis {
return &Genesis{
diff --git a/core/headerchain.go b/core/headerchain.go
index 2a64763..d956226 100644
--- a/core/headerchain.go
+++ b/core/headerchain.go
@@ -26,13 +26,13 @@ import (
"sync/atomic"
"time"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/params"
lru "github.com/hashicorp/golang-lru"
)
diff --git a/core/mkalloc.go b/core/mkalloc.go
index 0faf47b..35716a2 100644
--- a/core/mkalloc.go
+++ b/core/mkalloc.go
@@ -34,7 +34,7 @@ import (
"sort"
"strconv"
- "github.com/ava-labs/go-ethereum/core"
+ "github.com/ava-labs/coreth/core"
"github.com/ava-labs/go-ethereum/rlp"
)
diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go
new file mode 100644
index 0000000..7620eac
--- /dev/null
+++ b/core/rawdb/accessors_chain.go
@@ -0,0 +1,560 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "bytes"
+ "encoding/binary"
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
+func ReadCanonicalHash(db ethdb.Reader, number uint64) common.Hash {
+ data, _ := db.Ancient(freezerHashTable, number)
+ if len(data) == 0 {
+ data, _ = db.Get(headerHashKey(number))
+ // In the background freezer is moving data from leveldb to flatten files.
+ // So during the first check for ancient db, the data is not yet in there,
+ // but when we reach into leveldb, the data was already moved. That would
+ // result in a not found error.
+ if len(data) == 0 {
+ data, _ = db.Ancient(freezerHashTable, number)
+ }
+ }
+ if len(data) == 0 {
+ return common.Hash{}
+ }
+ return common.BytesToHash(data)
+}
+
+// WriteCanonicalHash stores the hash assigned to a canonical block number.
+func WriteCanonicalHash(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil {
+ log.Crit("Failed to store number to hash mapping", "err", err)
+ }
+}
+
+// DeleteCanonicalHash removes the number to hash canonical mapping.
+func DeleteCanonicalHash(db ethdb.KeyValueWriter, number uint64) {
+ if err := db.Delete(headerHashKey(number)); err != nil {
+ log.Crit("Failed to delete number to hash mapping", "err", err)
+ }
+}
+
+// ReadAllHashes retrieves all the hashes assigned to blocks at a certain heights,
+// both canonical and reorged forks included.
+func ReadAllHashes(db ethdb.Iteratee, number uint64) []common.Hash {
+ prefix := headerKeyPrefix(number)
+
+ hashes := make([]common.Hash, 0, 1)
+ it := db.NewIteratorWithPrefix(prefix)
+ defer it.Release()
+
+ for it.Next() {
+ if key := it.Key(); len(key) == len(prefix)+32 {
+ hashes = append(hashes, common.BytesToHash(key[len(key)-32:]))
+ }
+ }
+ return hashes
+}
+
+// ReadHeaderNumber returns the header number assigned to a hash.
+func ReadHeaderNumber(db ethdb.KeyValueReader, hash common.Hash) *uint64 {
+ data, _ := db.Get(headerNumberKey(hash))
+ if len(data) != 8 {
+ return nil
+ }
+ number := binary.BigEndian.Uint64(data)
+ return &number
+}
+
+// WriteHeaderNumber stores the hash->number mapping.
+func WriteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ key := headerNumberKey(hash)
+ enc := encodeBlockNumber(number)
+ if err := db.Put(key, enc); err != nil {
+ log.Crit("Failed to store hash to number mapping", "err", err)
+ }
+}
+
+// DeleteHeaderNumber removes hash->number mapping.
+func DeleteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash) {
+ if err := db.Delete(headerNumberKey(hash)); err != nil {
+ log.Crit("Failed to delete hash to number mapping", "err", err)
+ }
+}
+
+// ReadHeadHeaderHash retrieves the hash of the current canonical head header.
+func ReadHeadHeaderHash(db ethdb.KeyValueReader) common.Hash {
+ data, _ := db.Get(headHeaderKey)
+ if len(data) == 0 {
+ return common.Hash{}
+ }
+ return common.BytesToHash(data)
+}
+
+// WriteHeadHeaderHash stores the hash of the current canonical head header.
+func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) {
+ if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
+ log.Crit("Failed to store last header's hash", "err", err)
+ }
+}
+
+// ReadHeadBlockHash retrieves the hash of the current canonical head block.
+func ReadHeadBlockHash(db ethdb.KeyValueReader) common.Hash {
+ data, _ := db.Get(headBlockKey)
+ if len(data) == 0 {
+ return common.Hash{}
+ }
+ return common.BytesToHash(data)
+}
+
+// WriteHeadBlockHash stores the head block's hash.
+func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
+ if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
+ log.Crit("Failed to store last block's hash", "err", err)
+ }
+}
+
+// ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block.
+func ReadHeadFastBlockHash(db ethdb.KeyValueReader) common.Hash {
+ data, _ := db.Get(headFastBlockKey)
+ if len(data) == 0 {
+ return common.Hash{}
+ }
+ return common.BytesToHash(data)
+}
+
+// WriteHeadFastBlockHash stores the hash of the current fast-sync head block.
+func WriteHeadFastBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
+ if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil {
+ log.Crit("Failed to store last fast block's hash", "err", err)
+ }
+}
+
+// ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow
+// reporting correct numbers across restarts.
+func ReadFastTrieProgress(db ethdb.KeyValueReader) uint64 {
+ data, _ := db.Get(fastTrieProgressKey)
+ if len(data) == 0 {
+ return 0
+ }
+ return new(big.Int).SetBytes(data).Uint64()
+}
+
+// WriteFastTrieProgress stores the fast sync trie process counter to support
+// retrieving it across restarts.
+func WriteFastTrieProgress(db ethdb.KeyValueWriter, count uint64) {
+ if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil {
+ log.Crit("Failed to store fast sync trie progress", "err", err)
+ }
+}
+
+// ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
+func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
+ data, _ := db.Ancient(freezerHeaderTable, number)
+ if len(data) == 0 {
+ data, _ = db.Get(headerKey(number, hash))
+ // In the background freezer is moving data from leveldb to flatten files.
+ // So during the first check for ancient db, the data is not yet in there,
+ // but when we reach into leveldb, the data was already moved. That would
+ // result in a not found error.
+ if len(data) == 0 {
+ data, _ = db.Ancient(freezerHeaderTable, number)
+ }
+ }
+ return data
+}
+
+// HasHeader verifies the existence of a block header corresponding to the hash.
+func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
+ if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
+ return true
+ }
+ if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
+ return false
+ }
+ return true
+}
+
+// ReadHeader retrieves the block header corresponding to the hash.
+func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header {
+ data := ReadHeaderRLP(db, hash, number)
+ if len(data) == 0 {
+ return nil
+ }
+ header := new(types.Header)
+ if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
+ log.Error("Invalid block header RLP", "hash", hash, "err", err)
+ return nil
+ }
+ return header
+}
+
+// WriteHeader stores a block header into the database and also stores the hash-
+// to-number mapping.
+func WriteHeader(db ethdb.KeyValueWriter, header *types.Header) {
+ var (
+ hash = header.Hash()
+ number = header.Number.Uint64()
+ )
+ // Write the hash -> number mapping
+ WriteHeaderNumber(db, hash, number)
+
+ // Write the encoded header
+ data, err := rlp.EncodeToBytes(header)
+ if err != nil {
+ log.Crit("Failed to RLP encode header", "err", err)
+ }
+ key := headerKey(number, hash)
+ if err := db.Put(key, data); err != nil {
+ log.Crit("Failed to store header", "err", err)
+ }
+}
+
+// DeleteHeader removes all block header data associated with a hash.
+func DeleteHeader(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ deleteHeaderWithoutNumber(db, hash, number)
+ if err := db.Delete(headerNumberKey(hash)); err != nil {
+ log.Crit("Failed to delete hash to number mapping", "err", err)
+ }
+}
+
+// deleteHeaderWithoutNumber removes only the block header but does not remove
+// the hash to number mapping.
+func deleteHeaderWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ if err := db.Delete(headerKey(number, hash)); err != nil {
+ log.Crit("Failed to delete header", "err", err)
+ }
+}
+
+// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
+func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
+ data, _ := db.Ancient(freezerBodiesTable, number)
+ if len(data) == 0 {
+ data, _ = db.Get(blockBodyKey(number, hash))
+ // In the background freezer is moving data from leveldb to flatten files.
+ // So during the first check for ancient db, the data is not yet in there,
+ // but when we reach into leveldb, the data was already moved. That would
+ // result in a not found error.
+ if len(data) == 0 {
+ data, _ = db.Ancient(freezerBodiesTable, number)
+ }
+ }
+ return data
+}
+
+// WriteBodyRLP stores an RLP encoded block body into the database.
+func WriteBodyRLP(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
+ if err := db.Put(blockBodyKey(number, hash), rlp); err != nil {
+ log.Crit("Failed to store block body", "err", err)
+ }
+}
+
+// HasBody verifies the existence of a block body corresponding to the hash.
+func HasBody(db ethdb.Reader, hash common.Hash, number uint64) bool {
+ if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
+ return true
+ }
+ if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
+ return false
+ }
+ return true
+}
+
+// ReadBody retrieves the block body corresponding to the hash.
+func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body {
+ data := ReadBodyRLP(db, hash, number)
+ if len(data) == 0 {
+ return nil
+ }
+ body := new(types.Body)
+ if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
+ log.Error("Invalid block body RLP", "hash", hash, "err", err)
+ return nil
+ }
+ return body
+}
+
+// WriteBody stores a block body into the database.
+func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *types.Body) {
+ data, err := rlp.EncodeToBytes(body)
+ if err != nil {
+ log.Crit("Failed to RLP encode body", "err", err)
+ }
+ WriteBodyRLP(db, hash, number, data)
+}
+
+// DeleteBody removes all block body data associated with a hash.
+func DeleteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ if err := db.Delete(blockBodyKey(number, hash)); err != nil {
+ log.Crit("Failed to delete block body", "err", err)
+ }
+}
+
+// ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding.
+func ReadTdRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
+ data, _ := db.Ancient(freezerDifficultyTable, number)
+ if len(data) == 0 {
+ data, _ = db.Get(headerTDKey(number, hash))
+ // In the background freezer is moving data from leveldb to flatten files.
+ // So during the first check for ancient db, the data is not yet in there,
+ // but when we reach into leveldb, the data was already moved. That would
+ // result in a not found error.
+ if len(data) == 0 {
+ data, _ = db.Ancient(freezerDifficultyTable, number)
+ }
+ }
+ return data
+}
+
+// ReadTd retrieves a block's total difficulty corresponding to the hash.
+func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int {
+ data := ReadTdRLP(db, hash, number)
+ if len(data) == 0 {
+ return nil
+ }
+ td := new(big.Int)
+ if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
+ log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
+ return nil
+ }
+ return td
+}
+
+// WriteTd stores the total difficulty of a block into the database.
+func WriteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64, td *big.Int) {
+ data, err := rlp.EncodeToBytes(td)
+ if err != nil {
+ log.Crit("Failed to RLP encode block total difficulty", "err", err)
+ }
+ if err := db.Put(headerTDKey(number, hash), data); err != nil {
+ log.Crit("Failed to store block total difficulty", "err", err)
+ }
+}
+
+// DeleteTd removes all block total difficulty data associated with a hash.
+func DeleteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ if err := db.Delete(headerTDKey(number, hash)); err != nil {
+ log.Crit("Failed to delete block total difficulty", "err", err)
+ }
+}
+
+// HasReceipts verifies the existence of all the transaction receipts belonging
+// to a block.
+func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool {
+ if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
+ return true
+ }
+ if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil {
+ return false
+ }
+ return true
+}
+
+// ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
+func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
+ data, _ := db.Ancient(freezerReceiptTable, number)
+ if len(data) == 0 {
+ data, _ = db.Get(blockReceiptsKey(number, hash))
+ // In the background freezer is moving data from leveldb to flatten files.
+ // So during the first check for ancient db, the data is not yet in there,
+ // but when we reach into leveldb, the data was already moved. That would
+ // result in a not found error.
+ if len(data) == 0 {
+ data, _ = db.Ancient(freezerReceiptTable, number)
+ }
+ }
+ return data
+}
+
+// ReadRawReceipts retrieves all the transaction receipts belonging to a block.
+// The receipt metadata fields are not guaranteed to be populated, so they
+// should not be used. Use ReadReceipts instead if the metadata is needed.
+func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
+ // Retrieve the flattened receipt slice
+ data := ReadReceiptsRLP(db, hash, number)
+ if len(data) == 0 {
+ return nil
+ }
+ // Convert the receipts from their storage form to their internal representation
+ storageReceipts := []*types.ReceiptForStorage{}
+ if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
+ log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
+ return nil
+ }
+ receipts := make(types.Receipts, len(storageReceipts))
+ for i, storageReceipt := range storageReceipts {
+ receipts[i] = (*types.Receipt)(storageReceipt)
+ }
+ return receipts
+}
+
+// ReadReceipts retrieves all the transaction receipts belonging to a block, including
+// its correspoinding metadata fields. If it is unable to populate these metadata
+// fields then nil is returned.
+//
+// The current implementation populates these metadata fields by reading the receipts'
+// corresponding block body, so if the block body is not found it will return nil even
+// if the receipt itself is stored.
+func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts {
+ // We're deriving many fields from the block body, retrieve beside the receipt
+ receipts := ReadRawReceipts(db, hash, number)
+ if receipts == nil {
+ return nil
+ }
+ body := ReadBody(db, hash, number)
+ if body == nil {
+ log.Error("Missing body but have receipt", "hash", hash, "number", number)
+ return nil
+ }
+ if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil {
+ log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err)
+ return nil
+ }
+ return receipts
+}
+
+// WriteReceipts stores all the transaction receipts belonging to a block.
+func WriteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64, receipts types.Receipts) {
+ // Convert the receipts into their storage form and serialize them
+ storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
+ for i, receipt := range receipts {
+ storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
+ }
+ bytes, err := rlp.EncodeToBytes(storageReceipts)
+ if err != nil {
+ log.Crit("Failed to encode block receipts", "err", err)
+ }
+ // Store the flattened receipt slice
+ if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
+ log.Crit("Failed to store block receipts", "err", err)
+ }
+}
+
+// DeleteReceipts removes all receipt data associated with a block hash.
+func DeleteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
+ log.Crit("Failed to delete block receipts", "err", err)
+ }
+}
+
+// ReadBlock retrieves an entire block corresponding to the hash, assembling it
+// back from the stored header and body. If either the header or body could not
+// be retrieved nil is returned.
+//
+// Note, due to concurrent download of header and block body the header and thus
+// canonical hash can be stored in the database but the body data not (yet).
+func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
+ header := ReadHeader(db, hash, number)
+ if header == nil {
+ return nil
+ }
+ body := ReadBody(db, hash, number)
+ if body == nil {
+ return nil
+ }
+ return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
+}
+
+// WriteBlock serializes a block into the database, header and body separately.
+func WriteBlock(db ethdb.KeyValueWriter, block *types.Block) {
+ WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
+ WriteHeader(db, block.Header())
+}
+
+// WriteAncientBlock writes entire block data into ancient store and returns the total written size.
+func WriteAncientBlock(db ethdb.AncientWriter, block *types.Block, receipts types.Receipts, td *big.Int) int {
+ // Encode all block components to RLP format.
+ headerBlob, err := rlp.EncodeToBytes(block.Header())
+ if err != nil {
+ log.Crit("Failed to RLP encode block header", "err", err)
+ }
+ bodyBlob, err := rlp.EncodeToBytes(block.Body())
+ if err != nil {
+ log.Crit("Failed to RLP encode body", "err", err)
+ }
+ storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
+ for i, receipt := range receipts {
+ storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
+ }
+ receiptBlob, err := rlp.EncodeToBytes(storageReceipts)
+ if err != nil {
+ log.Crit("Failed to RLP encode block receipts", "err", err)
+ }
+ tdBlob, err := rlp.EncodeToBytes(td)
+ if err != nil {
+ log.Crit("Failed to RLP encode block total difficulty", "err", err)
+ }
+ // Write all blob to flatten files.
+ err = db.AppendAncient(block.NumberU64(), block.Hash().Bytes(), headerBlob, bodyBlob, receiptBlob, tdBlob)
+ if err != nil {
+ log.Crit("Failed to write block data to ancient store", "err", err)
+ }
+ return len(headerBlob) + len(bodyBlob) + len(receiptBlob) + len(tdBlob) + common.HashLength
+}
+
+// DeleteBlock removes all block data associated with a hash.
+func DeleteBlock(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ DeleteReceipts(db, hash, number)
+ DeleteHeader(db, hash, number)
+ DeleteBody(db, hash, number)
+ DeleteTd(db, hash, number)
+}
+
+// DeleteBlockWithoutNumber removes all block data associated with a hash, except
+// the hash to number mapping.
+func DeleteBlockWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
+ DeleteReceipts(db, hash, number)
+ deleteHeaderWithoutNumber(db, hash, number)
+ DeleteBody(db, hash, number)
+ DeleteTd(db, hash, number)
+}
+
+// FindCommonAncestor returns the last common ancestor of two block headers
+func FindCommonAncestor(db ethdb.Reader, a, b *types.Header) *types.Header {
+ for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
+ a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
+ if a == nil {
+ return nil
+ }
+ }
+ for an := a.Number.Uint64(); an < b.Number.Uint64(); {
+ b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
+ if b == nil {
+ return nil
+ }
+ }
+ for a.Hash() != b.Hash() {
+ a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
+ if a == nil {
+ return nil
+ }
+ b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
+ if b == nil {
+ return nil
+ }
+ }
+ return a
+}
diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go
new file mode 100644
index 0000000..1dd478a
--- /dev/null
+++ b/core/rawdb/accessors_indexes.go
@@ -0,0 +1,131 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
+// hash to allow retrieving the transaction or receipt by hash.
+func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
+ data, _ := db.Get(txLookupKey(hash))
+ if len(data) == 0 {
+ return nil
+ }
+ // Database v6 tx lookup just stores the block number
+ if len(data) < common.HashLength {
+ number := new(big.Int).SetBytes(data).Uint64()
+ return &number
+ }
+ // Database v4-v5 tx lookup format just stores the hash
+ if len(data) == common.HashLength {
+ return ReadHeaderNumber(db, common.BytesToHash(data))
+ }
+ // Finally try database v3 tx lookup format
+ var entry LegacyTxLookupEntry
+ if err := rlp.DecodeBytes(data, &entry); err != nil {
+ log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err)
+ return nil
+ }
+ return &entry.BlockIndex
+}
+
+// WriteTxLookupEntries stores a positional metadata for every transaction from
+// a block, enabling hash based transaction and receipt lookups.
+func WriteTxLookupEntries(db ethdb.KeyValueWriter, block *types.Block) {
+ number := block.Number().Bytes()
+ for _, tx := range block.Transactions() {
+ if err := db.Put(txLookupKey(tx.Hash()), number); err != nil {
+ log.Crit("Failed to store transaction lookup entry", "err", err)
+ }
+ }
+}
+
+// DeleteTxLookupEntry removes all transaction data associated with a hash.
+func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
+ db.Delete(txLookupKey(hash))
+}
+
+// ReadTransaction retrieves a specific transaction from the database, along with
+// its added positional metadata.
+func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
+ blockNumber := ReadTxLookupEntry(db, hash)
+ if blockNumber == nil {
+ return nil, common.Hash{}, 0, 0
+ }
+ blockHash := ReadCanonicalHash(db, *blockNumber)
+ if blockHash == (common.Hash{}) {
+ return nil, common.Hash{}, 0, 0
+ }
+ body := ReadBody(db, blockHash, *blockNumber)
+ if body == nil {
+ log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
+ return nil, common.Hash{}, 0, 0
+ }
+ for txIndex, tx := range body.Transactions {
+ if tx.Hash() == hash {
+ return tx, blockHash, *blockNumber, uint64(txIndex)
+ }
+ }
+ log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
+ return nil, common.Hash{}, 0, 0
+}
+
+// ReadReceipt retrieves a specific transaction receipt from the database, along with
+// its added positional metadata.
+func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
+ // Retrieve the context of the receipt based on the transaction hash
+ blockNumber := ReadTxLookupEntry(db, hash)
+ if blockNumber == nil {
+ return nil, common.Hash{}, 0, 0
+ }
+ blockHash := ReadCanonicalHash(db, *blockNumber)
+ if blockHash == (common.Hash{}) {
+ return nil, common.Hash{}, 0, 0
+ }
+ // Read all the receipts from the block and return the one with the matching hash
+ receipts := ReadReceipts(db, blockHash, *blockNumber, config)
+ for receiptIndex, receipt := range receipts {
+ if receipt.TxHash == hash {
+ return receipt, blockHash, *blockNumber, uint64(receiptIndex)
+ }
+ }
+ log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
+ return nil, common.Hash{}, 0, 0
+}
+
+// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
+// section and bit index from the.
+func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
+ return db.Get(bloomBitsKey(bit, section, head))
+}
+
+// WriteBloomBits stores the compressed bloom bits vector belonging to the given
+// section and bit index.
+func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
+ if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
+ log.Crit("Failed to store bloom bits", "err", err)
+ }
+}
diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go
new file mode 100644
index 0000000..7a17123
--- /dev/null
+++ b/core/rawdb/accessors_metadata.go
@@ -0,0 +1,98 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "encoding/json"
+
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+// ReadDatabaseVersion retrieves the version number of the database.
+func ReadDatabaseVersion(db ethdb.KeyValueReader) *uint64 {
+ var version uint64
+
+ enc, _ := db.Get(databaseVerisionKey)
+ if len(enc) == 0 {
+ return nil
+ }
+ if err := rlp.DecodeBytes(enc, &version); err != nil {
+ return nil
+ }
+
+ return &version
+}
+
+// WriteDatabaseVersion stores the version number of the database
+func WriteDatabaseVersion(db ethdb.KeyValueWriter, version uint64) {
+ enc, err := rlp.EncodeToBytes(version)
+ if err != nil {
+ log.Crit("Failed to encode database version", "err", err)
+ }
+ if err = db.Put(databaseVerisionKey, enc); err != nil {
+ log.Crit("Failed to store the database version", "err", err)
+ }
+}
+
+// ReadChainConfig retrieves the consensus settings based on the given genesis hash.
+func ReadChainConfig(db ethdb.KeyValueReader, hash common.Hash) *params.ChainConfig {
+ data, _ := db.Get(configKey(hash))
+ if len(data) == 0 {
+ return nil
+ }
+ var config params.ChainConfig
+ if err := json.Unmarshal(data, &config); err != nil {
+ log.Error("Invalid chain config JSON", "hash", hash, "err", err)
+ return nil
+ }
+ return &config
+}
+
+// WriteChainConfig writes the chain config settings to the database.
+func WriteChainConfig(db ethdb.KeyValueWriter, hash common.Hash, cfg *params.ChainConfig) {
+ if cfg == nil {
+ return
+ }
+ data, err := json.Marshal(cfg)
+ if err != nil {
+ log.Crit("Failed to JSON encode chain config", "err", err)
+ }
+ if err := db.Put(configKey(hash), data); err != nil {
+ log.Crit("Failed to store chain config", "err", err)
+ }
+}
+
+// ReadPreimage retrieves a single preimage of the provided hash.
+func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
+ data, _ := db.Get(preimageKey(hash))
+ return data
+}
+
+// WritePreimages writes the provided set of preimages to the database.
+func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) {
+ for hash, preimage := range preimages {
+ if err := db.Put(preimageKey(hash), preimage); err != nil {
+ log.Crit("Failed to store trie preimage", "err", err)
+ }
+ }
+ preimageCounter.Inc(int64(len(preimages)))
+ preimageHitCounter.Inc(int64(len(preimages)))
+}
diff --git a/core/rawdb/database.go b/core/rawdb/database.go
new file mode 100644
index 0000000..f04c34f
--- /dev/null
+++ b/core/rawdb/database.go
@@ -0,0 +1,355 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "os"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/ethdb/leveldb"
+ "github.com/ava-labs/go-ethereum/ethdb/memorydb"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/olekukonko/tablewriter"
+)
+
+// freezerdb is a database wrapper that enabled freezer data retrievals.
+type freezerdb struct {
+ ethdb.KeyValueStore
+ ethdb.AncientStore
+}
+
+// Close implements io.Closer, closing both the fast key-value store as well as
+// the slow ancient tables.
+func (frdb *freezerdb) Close() error {
+ var errs []error
+ if err := frdb.KeyValueStore.Close(); err != nil {
+ errs = append(errs, err)
+ }
+ if err := frdb.AncientStore.Close(); err != nil {
+ errs = append(errs, err)
+ }
+ if len(errs) != 0 {
+ return fmt.Errorf("%v", errs)
+ }
+ return nil
+}
+
+// nofreezedb is a database wrapper that disables freezer data retrievals.
+type nofreezedb struct {
+ ethdb.KeyValueStore
+}
+
+// HasAncient returns an error as we don't have a backing chain freezer.
+func (db *nofreezedb) HasAncient(kind string, number uint64) (bool, error) {
+ return false, errNotSupported
+}
+
+// Ancient returns an error as we don't have a backing chain freezer.
+func (db *nofreezedb) Ancient(kind string, number uint64) ([]byte, error) {
+ return nil, errNotSupported
+}
+
+// Ancients returns an error as we don't have a backing chain freezer.
+func (db *nofreezedb) Ancients() (uint64, error) {
+ return 0, errNotSupported
+}
+
+// AncientSize returns an error as we don't have a backing chain freezer.
+func (db *nofreezedb) AncientSize(kind string) (uint64, error) {
+ return 0, errNotSupported
+}
+
+// AppendAncient returns an error as we don't have a backing chain freezer.
+func (db *nofreezedb) AppendAncient(number uint64, hash, header, body, receipts, td []byte) error {
+ return errNotSupported
+}
+
+// TruncateAncients returns an error as we don't have a backing chain freezer.
+func (db *nofreezedb) TruncateAncients(items uint64) error {
+ return errNotSupported
+}
+
+// Sync returns an error as we don't have a backing chain freezer.
+func (db *nofreezedb) Sync() error {
+ return errNotSupported
+}
+
+// NewDatabase creates a high level database on top of a given key-value data
+// store without a freezer moving immutable chain segments into cold storage.
+func NewDatabase(db ethdb.KeyValueStore) ethdb.Database {
+ return &nofreezedb{
+ KeyValueStore: db,
+ }
+}
+
+// NewDatabaseWithFreezer creates a high level database on top of a given key-
+// value data store with a freezer moving immutable chain segments into cold
+// storage.
+func NewDatabaseWithFreezer(db ethdb.KeyValueStore, freezer string, namespace string) (ethdb.Database, error) {
+ // Create the idle freezer instance
+ frdb, err := newFreezer(freezer, namespace)
+ if err != nil {
+ return nil, err
+ }
+ // Since the freezer can be stored separately from the user's key-value database,
+ // there's a fairly high probability that the user requests invalid combinations
+ // of the freezer and database. Ensure that we don't shoot ourselves in the foot
+ // by serving up conflicting data, leading to both datastores getting corrupted.
+ //
+ // - If both the freezer and key-value store is empty (no genesis), we just
+ // initialized a new empty freezer, so everything's fine.
+ // - If the key-value store is empty, but the freezer is not, we need to make
+ // sure the user's genesis matches the freezer. That will be checked in the
+ // blockchain, since we don't have the genesis block here (nor should we at
+ // this point care, the key-value/freezer combo is valid).
+ // - If neither the key-value store nor the freezer is empty, cross validate
+ // the genesis hashes to make sure they are compatible. If they are, also
+ // ensure that there's no gap between the freezer and sunsequently leveldb.
+ // - If the key-value store is not empty, but the freezer is we might just be
+ // upgrading to the freezer release, or we might have had a small chain and
+ // not frozen anything yet. Ensure that no blocks are missing yet from the
+ // key-value store, since that would mean we already had an old freezer.
+
+ // If the genesis hash is empty, we have a new key-value store, so nothing to
+ // validate in this method. If, however, the genesis hash is not nil, compare
+ // it to the freezer content.
+ if kvgenesis, _ := db.Get(headerHashKey(0)); len(kvgenesis) > 0 {
+ if frozen, _ := frdb.Ancients(); frozen > 0 {
+ // If the freezer already contains something, ensure that the genesis blocks
+ // match, otherwise we might mix up freezers across chains and destroy both
+ // the freezer and the key-value store.
+ if frgenesis, _ := frdb.Ancient(freezerHashTable, 0); !bytes.Equal(kvgenesis, frgenesis) {
+ return nil, fmt.Errorf("genesis mismatch: %#x (leveldb) != %#x (ancients)", kvgenesis, frgenesis)
+ }
+ // Key-value store and freezer belong to the same network. Ensure that they
+ // are contiguous, otherwise we might end up with a non-functional freezer.
+ if kvhash, _ := db.Get(headerHashKey(frozen)); len(kvhash) == 0 {
+ // Subsequent header after the freezer limit is missing from the database.
+ // Reject startup is the database has a more recent head.
+ if *ReadHeaderNumber(db, ReadHeadHeaderHash(db)) > frozen-1 {
+ return nil, fmt.Errorf("gap (#%d) in the chain between ancients and leveldb", frozen)
+ }
+ // Database contains only older data than the freezer, this happens if the
+ // state was wiped and reinited from an existing freezer.
+ } else {
+ // Key-value store continues where the freezer left off, all is fine. We might
+ // have duplicate blocks (crash after freezer write but before kay-value store
+ // deletion, but that's fine).
+ }
+ } else {
+ // If the freezer is empty, ensure nothing was moved yet from the key-value
+ // store, otherwise we'll end up missing data. We check block #1 to decide
+ // if we froze anything previously or not, but do take care of databases with
+ // only the genesis block.
+ if ReadHeadHeaderHash(db) != common.BytesToHash(kvgenesis) {
+ // Key-value store contains more data than the genesis block, make sure we
+ // didn't freeze anything yet.
+ if kvblob, _ := db.Get(headerHashKey(1)); len(kvblob) == 0 {
+ return nil, errors.New("ancient chain segments already extracted, please set --datadir.ancient to the correct path")
+ }
+ // Block #1 is still in the database, we're allowed to init a new feezer
+ } else {
+ // The head header is still the genesis, we're allowed to init a new feezer
+ }
+ }
+ }
+ // Freezer is consistent with the key-value database, permit combining the two
+ go frdb.freeze(db)
+
+ return &freezerdb{
+ KeyValueStore: db,
+ AncientStore: frdb,
+ }, nil
+}
+
+// NewMemoryDatabase creates an ephemeral in-memory key-value database without a
+// freezer moving immutable chain segments into cold storage.
+func NewMemoryDatabase() ethdb.Database {
+ return NewDatabase(memorydb.New())
+}
+
+// NewMemoryDatabaseWithCap creates an ephemeral in-memory key-value database
+// with an initial starting capacity, but without a freezer moving immutable
+// chain segments into cold storage.
+func NewMemoryDatabaseWithCap(size int) ethdb.Database {
+ return NewDatabase(memorydb.NewWithCap(size))
+}
+
+// NewLevelDBDatabase creates a persistent key-value database without a freezer
+// moving immutable chain segments into cold storage.
+func NewLevelDBDatabase(file string, cache int, handles int, namespace string) (ethdb.Database, error) {
+ db, err := leveldb.New(file, cache, handles, namespace)
+ if err != nil {
+ return nil, err
+ }
+ return NewDatabase(db), nil
+}
+
+// NewLevelDBDatabaseWithFreezer creates a persistent key-value database with a
+// freezer moving immutable chain segments into cold storage.
+func NewLevelDBDatabaseWithFreezer(file string, cache int, handles int, freezer string, namespace string) (ethdb.Database, error) {
+ kvdb, err := leveldb.New(file, cache, handles, namespace)
+ if err != nil {
+ return nil, err
+ }
+ frdb, err := NewDatabaseWithFreezer(kvdb, freezer, namespace)
+ if err != nil {
+ kvdb.Close()
+ return nil, err
+ }
+ return frdb, nil
+}
+
+// InspectDatabase traverses the entire database and checks the size
+// of all different categories of data.
+func InspectDatabase(db ethdb.Database) error {
+ it := db.NewIterator()
+ defer it.Release()
+
+ var (
+ count int64
+ start = time.Now()
+ logged = time.Now()
+
+ // Key-value store statistics
+ total common.StorageSize
+ headerSize common.StorageSize
+ bodySize common.StorageSize
+ receiptSize common.StorageSize
+ tdSize common.StorageSize
+ numHashPairing common.StorageSize
+ hashNumPairing common.StorageSize
+ trieSize common.StorageSize
+ txlookupSize common.StorageSize
+ preimageSize common.StorageSize
+ bloomBitsSize common.StorageSize
+ cliqueSnapsSize common.StorageSize
+
+ // Ancient store statistics
+ ancientHeaders common.StorageSize
+ ancientBodies common.StorageSize
+ ancientReceipts common.StorageSize
+ ancientHashes common.StorageSize
+ ancientTds common.StorageSize
+
+ // Les statistic
+ chtTrieNodes common.StorageSize
+ bloomTrieNodes common.StorageSize
+
+ // Meta- and unaccounted data
+ metadata common.StorageSize
+ unaccounted common.StorageSize
+ )
+ // Inspect key-value database first.
+ for it.Next() {
+ var (
+ key = it.Key()
+ size = common.StorageSize(len(key) + len(it.Value()))
+ )
+ total += size
+ switch {
+ case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerTDSuffix):
+ tdSize += size
+ case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerHashSuffix):
+ numHashPairing += size
+ case bytes.HasPrefix(key, headerPrefix) && len(key) == (len(headerPrefix)+8+common.HashLength):
+ headerSize += size
+ case bytes.HasPrefix(key, headerNumberPrefix) && len(key) == (len(headerNumberPrefix)+common.HashLength):
+ hashNumPairing += size
+ case bytes.HasPrefix(key, blockBodyPrefix) && len(key) == (len(blockBodyPrefix)+8+common.HashLength):
+ bodySize += size
+ case bytes.HasPrefix(key, blockReceiptsPrefix) && len(key) == (len(blockReceiptsPrefix)+8+common.HashLength):
+ receiptSize += size
+ case bytes.HasPrefix(key, txLookupPrefix) && len(key) == (len(txLookupPrefix)+common.HashLength):
+ txlookupSize += size
+ case bytes.HasPrefix(key, preimagePrefix) && len(key) == (len(preimagePrefix)+common.HashLength):
+ preimageSize += size
+ case bytes.HasPrefix(key, bloomBitsPrefix) && len(key) == (len(bloomBitsPrefix)+10+common.HashLength):
+ bloomBitsSize += size
+ case bytes.HasPrefix(key, []byte("clique-")) && len(key) == 7+common.HashLength:
+ cliqueSnapsSize += size
+ case bytes.HasPrefix(key, []byte("cht-")) && len(key) == 4+common.HashLength:
+ chtTrieNodes += size
+ case bytes.HasPrefix(key, []byte("blt-")) && len(key) == 4+common.HashLength:
+ bloomTrieNodes += size
+ case len(key) == common.HashLength:
+ trieSize += size
+ default:
+ var accounted bool
+ for _, meta := range [][]byte{databaseVerisionKey, headHeaderKey, headBlockKey, headFastBlockKey, fastTrieProgressKey} {
+ if bytes.Equal(key, meta) {
+ metadata += size
+ accounted = true
+ break
+ }
+ }
+ if !accounted {
+ unaccounted += size
+ }
+ }
+ count += 1
+ if count%1000 == 0 && time.Since(logged) > 8*time.Second {
+ log.Info("Inspecting database", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
+ logged = time.Now()
+ }
+ }
+ // Inspect append-only file store then.
+ ancients := []*common.StorageSize{&ancientHeaders, &ancientBodies, &ancientReceipts, &ancientHashes, &ancientTds}
+ for i, category := range []string{freezerHeaderTable, freezerBodiesTable, freezerReceiptTable, freezerHashTable, freezerDifficultyTable} {
+ if size, err := db.AncientSize(category); err == nil {
+ *ancients[i] += common.StorageSize(size)
+ total += common.StorageSize(size)
+ }
+ }
+ // Display the database statistic.
+ stats := [][]string{
+ {"Key-Value store", "Headers", headerSize.String()},
+ {"Key-Value store", "Bodies", bodySize.String()},
+ {"Key-Value store", "Receipts", receiptSize.String()},
+ {"Key-Value store", "Difficulties", tdSize.String()},
+ {"Key-Value store", "Block number->hash", numHashPairing.String()},
+ {"Key-Value store", "Block hash->number", hashNumPairing.String()},
+ {"Key-Value store", "Transaction index", txlookupSize.String()},
+ {"Key-Value store", "Bloombit index", bloomBitsSize.String()},
+ {"Key-Value store", "Trie nodes", trieSize.String()},
+ {"Key-Value store", "Trie preimages", preimageSize.String()},
+ {"Key-Value store", "Clique snapshots", cliqueSnapsSize.String()},
+ {"Key-Value store", "Singleton metadata", metadata.String()},
+ {"Ancient store", "Headers", ancientHeaders.String()},
+ {"Ancient store", "Bodies", ancientBodies.String()},
+ {"Ancient store", "Receipts", ancientReceipts.String()},
+ {"Ancient store", "Difficulties", ancientTds.String()},
+ {"Ancient store", "Block number->hash", ancientHashes.String()},
+ {"Light client", "CHT trie nodes", chtTrieNodes.String()},
+ {"Light client", "Bloom trie nodes", bloomTrieNodes.String()},
+ }
+ table := tablewriter.NewWriter(os.Stdout)
+ table.SetHeader([]string{"Database", "Category", "Size"})
+ table.SetFooter([]string{"", "Total", total.String()})
+ table.AppendBulk(stats)
+ table.Render()
+
+ if unaccounted > 0 {
+ log.Error("Database contains unaccounted data", "size", unaccounted)
+ }
+ return nil
+}
diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go
new file mode 100644
index 0000000..ce2e879
--- /dev/null
+++ b/core/rawdb/freezer.go
@@ -0,0 +1,393 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "errors"
+ "fmt"
+ "math"
+ "os"
+ "path/filepath"
+ "sync/atomic"
+ "time"
+
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/metrics"
+ "github.com/prometheus/tsdb/fileutil"
+)
+
+var (
+ // errUnknownTable is returned if the user attempts to read from a table that is
+ // not tracked by the freezer.
+ errUnknownTable = errors.New("unknown table")
+
+ // errOutOrderInsertion is returned if the user attempts to inject out-of-order
+ // binary blobs into the freezer.
+ errOutOrderInsertion = errors.New("the append operation is out-order")
+
+ // errSymlinkDatadir is returned if the ancient directory specified by user
+ // is a symbolic link.
+ errSymlinkDatadir = errors.New("symbolic link datadir is not supported")
+)
+
+const (
+ // freezerRecheckInterval is the frequency to check the key-value database for
+ // chain progression that might permit new blocks to be frozen into immutable
+ // storage.
+ freezerRecheckInterval = time.Minute
+
+ // freezerBatchLimit is the maximum number of blocks to freeze in one batch
+ // before doing an fsync and deleting it from the key-value store.
+ freezerBatchLimit = 30000
+)
+
+// freezer is an memory mapped append-only database to store immutable chain data
+// into flat files:
+//
+// - The append only nature ensures that disk writes are minimized.
+// - The memory mapping ensures we can max out system memory for caching without
+// reserving it for go-ethereum. This would also reduce the memory requirements
+// of Geth, and thus also GC overhead.
+type freezer struct {
+ // WARNING: The `frozen` field is accessed atomically. On 32 bit platforms, only
+ // 64-bit aligned fields can be atomic. The struct is guaranteed to be so aligned,
+ // so take advantage of that (https://golang.org/pkg/sync/atomic/#pkg-note-BUG).
+ frozen uint64 // Number of blocks already frozen
+
+ tables map[string]*freezerTable // Data tables for storing everything
+ instanceLock fileutil.Releaser // File-system lock to prevent double opens
+}
+
+// newFreezer creates a chain freezer that moves ancient chain data into
+// append-only flat file containers.
+func newFreezer(datadir string, namespace string) (*freezer, error) {
+ // Create the initial freezer object
+ var (
+ readMeter = metrics.NewRegisteredMeter(namespace+"ancient/read", nil)
+ writeMeter = metrics.NewRegisteredMeter(namespace+"ancient/write", nil)
+ sizeCounter = metrics.NewRegisteredCounter(namespace+"ancient/size", nil)
+ )
+ // Ensure the datadir is not a symbolic link if it exists.
+ if info, err := os.Lstat(datadir); !os.IsNotExist(err) {
+ if info.Mode()&os.ModeSymlink != 0 {
+ log.Warn("Symbolic link ancient database is not supported", "path", datadir)
+ return nil, errSymlinkDatadir
+ }
+ }
+ // Leveldb uses LOCK as the filelock filename. To prevent the
+ // name collision, we use FLOCK as the lock name.
+ lock, _, err := fileutil.Flock(filepath.Join(datadir, "FLOCK"))
+ if err != nil {
+ return nil, err
+ }
+ // Open all the supported data tables
+ freezer := &freezer{
+ tables: make(map[string]*freezerTable),
+ instanceLock: lock,
+ }
+ for name, disableSnappy := range freezerNoSnappy {
+ table, err := newTable(datadir, name, readMeter, writeMeter, sizeCounter, disableSnappy)
+ if err != nil {
+ for _, table := range freezer.tables {
+ table.Close()
+ }
+ lock.Release()
+ return nil, err
+ }
+ freezer.tables[name] = table
+ }
+ if err := freezer.repair(); err != nil {
+ for _, table := range freezer.tables {
+ table.Close()
+ }
+ lock.Release()
+ return nil, err
+ }
+ log.Info("Opened ancient database", "database", datadir)
+ return freezer, nil
+}
+
+// Close terminates the chain freezer, unmapping all the data files.
+func (f *freezer) Close() error {
+ var errs []error
+ for _, table := range f.tables {
+ if err := table.Close(); err != nil {
+ errs = append(errs, err)
+ }
+ }
+ if err := f.instanceLock.Release(); err != nil {
+ errs = append(errs, err)
+ }
+ if errs != nil {
+ return fmt.Errorf("%v", errs)
+ }
+ return nil
+}
+
+// HasAncient returns an indicator whether the specified ancient data exists
+// in the freezer.
+func (f *freezer) HasAncient(kind string, number uint64) (bool, error) {
+ if table := f.tables[kind]; table != nil {
+ return table.has(number), nil
+ }
+ return false, nil
+}
+
+// Ancient retrieves an ancient binary blob from the append-only immutable files.
+func (f *freezer) Ancient(kind string, number uint64) ([]byte, error) {
+ if table := f.tables[kind]; table != nil {
+ return table.Retrieve(number)
+ }
+ return nil, errUnknownTable
+}
+
+// Ancients returns the length of the frozen items.
+func (f *freezer) Ancients() (uint64, error) {
+ return atomic.LoadUint64(&f.frozen), nil
+}
+
+// AncientSize returns the ancient size of the specified category.
+func (f *freezer) AncientSize(kind string) (uint64, error) {
+ if table := f.tables[kind]; table != nil {
+ return table.size()
+ }
+ return 0, errUnknownTable
+}
+
+// AppendAncient injects all binary blobs belong to block at the end of the
+// append-only immutable table files.
+//
+// Notably, this function is lock free but kind of thread-safe. All out-of-order
+// injection will be rejected. But if two injections with same number happen at
+// the same time, we can get into the trouble.
+func (f *freezer) AppendAncient(number uint64, hash, header, body, receipts, td []byte) (err error) {
+ // Ensure the binary blobs we are appending is continuous with freezer.
+ if atomic.LoadUint64(&f.frozen) != number {
+ return errOutOrderInsertion
+ }
+ // Rollback all inserted data if any insertion below failed to ensure
+ // the tables won't out of sync.
+ defer func() {
+ if err != nil {
+ rerr := f.repair()
+ if rerr != nil {
+ log.Crit("Failed to repair freezer", "err", rerr)
+ }
+ log.Info("Append ancient failed", "number", number, "err", err)
+ }
+ }()
+ // Inject all the components into the relevant data tables
+ if err := f.tables[freezerHashTable].Append(f.frozen, hash[:]); err != nil {
+ log.Error("Failed to append ancient hash", "number", f.frozen, "hash", hash, "err", err)
+ return err
+ }
+ if err := f.tables[freezerHeaderTable].Append(f.frozen, header); err != nil {
+ log.Error("Failed to append ancient header", "number", f.frozen, "hash", hash, "err", err)
+ return err
+ }
+ if err := f.tables[freezerBodiesTable].Append(f.frozen, body); err != nil {
+ log.Error("Failed to append ancient body", "number", f.frozen, "hash", hash, "err", err)
+ return err
+ }
+ if err := f.tables[freezerReceiptTable].Append(f.frozen, receipts); err != nil {
+ log.Error("Failed to append ancient receipts", "number", f.frozen, "hash", hash, "err", err)
+ return err
+ }
+ if err := f.tables[freezerDifficultyTable].Append(f.frozen, td); err != nil {
+ log.Error("Failed to append ancient difficulty", "number", f.frozen, "hash", hash, "err", err)
+ return err
+ }
+ atomic.AddUint64(&f.frozen, 1) // Only modify atomically
+ return nil
+}
+
+// Truncate discards any recent data above the provided threshold number.
+func (f *freezer) TruncateAncients(items uint64) error {
+ if atomic.LoadUint64(&f.frozen) <= items {
+ return nil
+ }
+ for _, table := range f.tables {
+ if err := table.truncate(items); err != nil {
+ return err
+ }
+ }
+ atomic.StoreUint64(&f.frozen, items)
+ return nil
+}
+
+// sync flushes all data tables to disk.
+func (f *freezer) Sync() error {
+ var errs []error
+ for _, table := range f.tables {
+ if err := table.Sync(); err != nil {
+ errs = append(errs, err)
+ }
+ }
+ if errs != nil {
+ return fmt.Errorf("%v", errs)
+ }
+ return nil
+}
+
+// freeze is a background thread that periodically checks the blockchain for any
+// import progress and moves ancient data from the fast database into the freezer.
+//
+// This functionality is deliberately broken off from block importing to avoid
+// incurring additional data shuffling delays on block propagation.
+func (f *freezer) freeze(db ethdb.KeyValueStore) {
+ nfdb := &nofreezedb{KeyValueStore: db}
+
+ for {
+ // Retrieve the freezing threshold.
+ hash := ReadHeadBlockHash(nfdb)
+ if hash == (common.Hash{}) {
+ log.Debug("Current full block hash unavailable") // new chain, empty database
+ time.Sleep(freezerRecheckInterval)
+ continue
+ }
+ number := ReadHeaderNumber(nfdb, hash)
+ switch {
+ case number == nil:
+ log.Error("Current full block number unavailable", "hash", hash)
+ time.Sleep(freezerRecheckInterval)
+ continue
+
+ case *number < params.ImmutabilityThreshold:
+ log.Debug("Current full block not old enough", "number", *number, "hash", hash, "delay", params.ImmutabilityThreshold)
+ time.Sleep(freezerRecheckInterval)
+ continue
+
+ case *number-params.ImmutabilityThreshold <= f.frozen:
+ log.Debug("Ancient blocks frozen already", "number", *number, "hash", hash, "frozen", f.frozen)
+ time.Sleep(freezerRecheckInterval)
+ continue
+ }
+ head := ReadHeader(nfdb, hash, *number)
+ if head == nil {
+ log.Error("Current full block unavailable", "number", *number, "hash", hash)
+ time.Sleep(freezerRecheckInterval)
+ continue
+ }
+ // Seems we have data ready to be frozen, process in usable batches
+ limit := *number - params.ImmutabilityThreshold
+ if limit-f.frozen > freezerBatchLimit {
+ limit = f.frozen + freezerBatchLimit
+ }
+ var (
+ start = time.Now()
+ first = f.frozen
+ ancients = make([]common.Hash, 0, limit)
+ )
+ for f.frozen < limit {
+ // Retrieves all the components of the canonical block
+ hash := ReadCanonicalHash(nfdb, f.frozen)
+ if hash == (common.Hash{}) {
+ log.Error("Canonical hash missing, can't freeze", "number", f.frozen)
+ break
+ }
+ header := ReadHeaderRLP(nfdb, hash, f.frozen)
+ if len(header) == 0 {
+ log.Error("Block header missing, can't freeze", "number", f.frozen, "hash", hash)
+ break
+ }
+ body := ReadBodyRLP(nfdb, hash, f.frozen)
+ if len(body) == 0 {
+ log.Error("Block body missing, can't freeze", "number", f.frozen, "hash", hash)
+ break
+ }
+ receipts := ReadReceiptsRLP(nfdb, hash, f.frozen)
+ if len(receipts) == 0 {
+ log.Error("Block receipts missing, can't freeze", "number", f.frozen, "hash", hash)
+ break
+ }
+ td := ReadTdRLP(nfdb, hash, f.frozen)
+ if len(td) == 0 {
+ log.Error("Total difficulty missing, can't freeze", "number", f.frozen, "hash", hash)
+ break
+ }
+ log.Trace("Deep froze ancient block", "number", f.frozen, "hash", hash)
+ // Inject all the components into the relevant data tables
+ if err := f.AppendAncient(f.frozen, hash[:], header, body, receipts, td); err != nil {
+ break
+ }
+ ancients = append(ancients, hash)
+ }
+ // Batch of blocks have been frozen, flush them before wiping from leveldb
+ if err := f.Sync(); err != nil {
+ log.Crit("Failed to flush frozen tables", "err", err)
+ }
+ // Wipe out all data from the active database
+ batch := db.NewBatch()
+ for i := 0; i < len(ancients); i++ {
+ // Always keep the genesis block in active database
+ if first+uint64(i) != 0 {
+ DeleteBlockWithoutNumber(batch, ancients[i], first+uint64(i))
+ DeleteCanonicalHash(batch, first+uint64(i))
+ }
+ }
+ if err := batch.Write(); err != nil {
+ log.Crit("Failed to delete frozen canonical blocks", "err", err)
+ }
+ batch.Reset()
+ // Wipe out side chain also.
+ for number := first; number < f.frozen; number++ {
+ // Always keep the genesis block in active database
+ if number != 0 {
+ for _, hash := range ReadAllHashes(db, number) {
+ DeleteBlock(batch, hash, number)
+ }
+ }
+ }
+ if err := batch.Write(); err != nil {
+ log.Crit("Failed to delete frozen side blocks", "err", err)
+ }
+ // Log something friendly for the user
+ context := []interface{}{
+ "blocks", f.frozen - first, "elapsed", common.PrettyDuration(time.Since(start)), "number", f.frozen - 1,
+ }
+ if n := len(ancients); n > 0 {
+ context = append(context, []interface{}{"hash", ancients[n-1]}...)
+ }
+ log.Info("Deep froze chain segment", context...)
+
+ // Avoid database thrashing with tiny writes
+ if f.frozen-first < freezerBatchLimit {
+ time.Sleep(freezerRecheckInterval)
+ }
+ }
+}
+
+// repair truncates all data tables to the same length.
+func (f *freezer) repair() error {
+ min := uint64(math.MaxUint64)
+ for _, table := range f.tables {
+ items := atomic.LoadUint64(&table.items)
+ if min > items {
+ min = items
+ }
+ }
+ for _, table := range f.tables {
+ if err := table.truncate(min); err != nil {
+ return err
+ }
+ }
+ atomic.StoreUint64(&f.frozen, min)
+ return nil
+}
diff --git a/core/rawdb/freezer_reinit.go b/core/rawdb/freezer_reinit.go
new file mode 100644
index 0000000..6b9fb79
--- /dev/null
+++ b/core/rawdb/freezer_reinit.go
@@ -0,0 +1,127 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "errors"
+ "runtime"
+ "sync/atomic"
+ "time"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/prque"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+// InitDatabaseFromFreezer reinitializes an empty database from a previous batch
+// of frozen ancient blocks. The method iterates over all the frozen blocks and
+// injects into the database the block hash->number mappings and the transaction
+// lookup entries.
+func InitDatabaseFromFreezer(db ethdb.Database) error {
+ // If we can't access the freezer or it's empty, abort
+ frozen, err := db.Ancients()
+ if err != nil || frozen == 0 {
+ return err
+ }
+ // Blocks previously frozen, iterate over- and hash them concurrently
+ var (
+ number = ^uint64(0) // -1
+ results = make(chan *types.Block, 4*runtime.NumCPU())
+ )
+ abort := make(chan struct{})
+ defer close(abort)
+
+ for i := 0; i < runtime.NumCPU(); i++ {
+ go func() {
+ for {
+ // Fetch the next task number, terminating if everything's done
+ n := atomic.AddUint64(&number, 1)
+ if n >= frozen {
+ return
+ }
+ // Retrieve the block from the freezer (no need for the hash, we pull by
+ // number from the freezer). If successful, pre-cache the block hash and
+ // the individual transaction hashes for storing into the database.
+ block := ReadBlock(db, common.Hash{}, n)
+ if block != nil {
+ block.Hash()
+ for _, tx := range block.Transactions() {
+ tx.Hash()
+ }
+ }
+ // Feed the block to the aggregator, or abort on interrupt
+ select {
+ case results <- block:
+ case <-abort:
+ return
+ }
+ }
+ }()
+ }
+ // Reassemble the blocks into a contiguous stream and push them out to disk
+ var (
+ queue = prque.New(nil)
+ next = int64(0)
+
+ batch = db.NewBatch()
+ start = time.Now()
+ logged time.Time
+ )
+ for i := uint64(0); i < frozen; i++ {
+ // Retrieve the next result and bail if it's nil
+ block := <-results
+ if block == nil {
+ return errors.New("broken ancient database")
+ }
+ // Push the block into the import queue and process contiguous ranges
+ queue.Push(block, -int64(block.NumberU64()))
+ for !queue.Empty() {
+ // If the next available item is gapped, return
+ if _, priority := queue.Peek(); -priority != next {
+ break
+ }
+ // Next block available, pop it off and index it
+ block = queue.PopItem().(*types.Block)
+ next++
+
+ // Inject hash<->number mapping and txlookup indexes
+ WriteHeaderNumber(batch, block.Hash(), block.NumberU64())
+ WriteTxLookupEntries(batch, block)
+
+ // If enough data was accumulated in memory or we're at the last block, dump to disk
+ if batch.ValueSize() > ethdb.IdealBatchSize || uint64(next) == frozen {
+ if err := batch.Write(); err != nil {
+ return err
+ }
+ batch.Reset()
+ }
+ // If we've spent too much time already, notify the user of what we're doing
+ if time.Since(logged) > 8*time.Second {
+ log.Info("Initializing chain from ancient data", "number", block.Number(), "hash", block.Hash(), "total", frozen-1, "elapsed", common.PrettyDuration(time.Since(start)))
+ logged = time.Now()
+ }
+ }
+ }
+ hash := ReadCanonicalHash(db, frozen-1)
+ WriteHeadHeaderHash(db, hash)
+ WriteHeadFastBlockHash(db, hash)
+
+ log.Info("Initialized chain from ancient data", "number", frozen-1, "hash", hash, "elapsed", common.PrettyDuration(time.Since(start)))
+ return nil
+}
diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go
new file mode 100644
index 0000000..fc72669
--- /dev/null
+++ b/core/rawdb/freezer_table.go
@@ -0,0 +1,637 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "encoding/binary"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "path/filepath"
+ "sync"
+ "sync/atomic"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/metrics"
+ "github.com/golang/snappy"
+)
+
+var (
+ // errClosed is returned if an operation attempts to read from or write to the
+ // freezer table after it has already been closed.
+ errClosed = errors.New("closed")
+
+ // errOutOfBounds is returned if the item requested is not contained within the
+ // freezer table.
+ errOutOfBounds = errors.New("out of bounds")
+
+ // errNotSupported is returned if the database doesn't support the required operation.
+ errNotSupported = errors.New("this operation is not supported")
+)
+
+// indexEntry contains the number/id of the file that the data resides in, aswell as the
+// offset within the file to the end of the data
+// In serialized form, the filenum is stored as uint16.
+type indexEntry struct {
+ filenum uint32 // stored as uint16 ( 2 bytes)
+ offset uint32 // stored as uint32 ( 4 bytes)
+}
+
+const indexEntrySize = 6
+
+// unmarshallBinary deserializes binary b into the rawIndex entry.
+func (i *indexEntry) unmarshalBinary(b []byte) error {
+ i.filenum = uint32(binary.BigEndian.Uint16(b[:2]))
+ i.offset = binary.BigEndian.Uint32(b[2:6])
+ return nil
+}
+
+// marshallBinary serializes the rawIndex entry into binary.
+func (i *indexEntry) marshallBinary() []byte {
+ b := make([]byte, indexEntrySize)
+ binary.BigEndian.PutUint16(b[:2], uint16(i.filenum))
+ binary.BigEndian.PutUint32(b[2:6], i.offset)
+ return b
+}
+
+// freezerTable represents a single chained data table within the freezer (e.g. blocks).
+// It consists of a data file (snappy encoded arbitrary data blobs) and an indexEntry
+// file (uncompressed 64 bit indices into the data file).
+type freezerTable struct {
+ // WARNING: The `items` field is accessed atomically. On 32 bit platforms, only
+ // 64-bit aligned fields can be atomic. The struct is guaranteed to be so aligned,
+ // so take advantage of that (https://golang.org/pkg/sync/atomic/#pkg-note-BUG).
+ items uint64 // Number of items stored in the table (including items removed from tail)
+
+ noCompression bool // if true, disables snappy compression. Note: does not work retroactively
+ maxFileSize uint32 // Max file size for data-files
+ name string
+ path string
+
+ head *os.File // File descriptor for the data head of the table
+ files map[uint32]*os.File // open files
+ headId uint32 // number of the currently active head file
+ tailId uint32 // number of the earliest file
+ index *os.File // File descriptor for the indexEntry file of the table
+
+ // In the case that old items are deleted (from the tail), we use itemOffset
+ // to count how many historic items have gone missing.
+ itemOffset uint32 // Offset (number of discarded items)
+
+ headBytes uint32 // Number of bytes written to the head file
+ readMeter metrics.Meter // Meter for measuring the effective amount of data read
+ writeMeter metrics.Meter // Meter for measuring the effective amount of data written
+ sizeCounter metrics.Counter // Counter for tracking the combined size of all freezer tables
+
+ logger log.Logger // Logger with database path and table name ambedded
+ lock sync.RWMutex // Mutex protecting the data file descriptors
+}
+
+// newTable opens a freezer table with default settings - 2G files
+func newTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeCounter metrics.Counter, disableSnappy bool) (*freezerTable, error) {
+ return newCustomTable(path, name, readMeter, writeMeter, sizeCounter, 2*1000*1000*1000, disableSnappy)
+}
+
+// openFreezerFileForAppend opens a freezer table file and seeks to the end
+func openFreezerFileForAppend(filename string) (*os.File, error) {
+ // Open the file without the O_APPEND flag
+ // because it has differing behaviour during Truncate operations
+ // on different OS's
+ file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
+ if err != nil {
+ return nil, err
+ }
+ // Seek to end for append
+ if _, err = file.Seek(0, io.SeekEnd); err != nil {
+ return nil, err
+ }
+ return file, nil
+}
+
+// openFreezerFileForReadOnly opens a freezer table file for read only access
+func openFreezerFileForReadOnly(filename string) (*os.File, error) {
+ return os.OpenFile(filename, os.O_RDONLY, 0644)
+}
+
+// openFreezerFileTruncated opens a freezer table making sure it is truncated
+func openFreezerFileTruncated(filename string) (*os.File, error) {
+ return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
+}
+
+// truncateFreezerFile resizes a freezer table file and seeks to the end
+func truncateFreezerFile(file *os.File, size int64) error {
+ if err := file.Truncate(size); err != nil {
+ return err
+ }
+ // Seek to end for append
+ if _, err := file.Seek(0, io.SeekEnd); err != nil {
+ return err
+ }
+ return nil
+}
+
+// newCustomTable opens a freezer table, creating the data and index files if they are
+// non existent. Both files are truncated to the shortest common length to ensure
+// they don't go out of sync.
+func newCustomTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeCounter metrics.Counter, maxFilesize uint32, noCompression bool) (*freezerTable, error) {
+ // Ensure the containing directory exists and open the indexEntry file
+ if err := os.MkdirAll(path, 0755); err != nil {
+ return nil, err
+ }
+ var idxName string
+ if noCompression {
+ // Raw idx
+ idxName = fmt.Sprintf("%s.ridx", name)
+ } else {
+ // Compressed idx
+ idxName = fmt.Sprintf("%s.cidx", name)
+ }
+ offsets, err := openFreezerFileForAppend(filepath.Join(path, idxName))
+ if err != nil {
+ return nil, err
+ }
+ // Create the table and repair any past inconsistency
+ tab := &freezerTable{
+ index: offsets,
+ files: make(map[uint32]*os.File),
+ readMeter: readMeter,
+ writeMeter: writeMeter,
+ sizeCounter: sizeCounter,
+ name: name,
+ path: path,
+ logger: log.New("database", path, "table", name),
+ noCompression: noCompression,
+ maxFileSize: maxFilesize,
+ }
+ if err := tab.repair(); err != nil {
+ tab.Close()
+ return nil, err
+ }
+ // Initialize the starting size counter
+ size, err := tab.sizeNolock()
+ if err != nil {
+ tab.Close()
+ return nil, err
+ }
+ tab.sizeCounter.Inc(int64(size))
+
+ return tab, nil
+}
+
+// repair cross checks the head and the index file and truncates them to
+// be in sync with each other after a potential crash / data loss.
+func (t *freezerTable) repair() error {
+ // Create a temporary offset buffer to init files with and read indexEntry into
+ buffer := make([]byte, indexEntrySize)
+
+ // If we've just created the files, initialize the index with the 0 indexEntry
+ stat, err := t.index.Stat()
+ if err != nil {
+ return err
+ }
+ if stat.Size() == 0 {
+ if _, err := t.index.Write(buffer); err != nil {
+ return err
+ }
+ }
+ // Ensure the index is a multiple of indexEntrySize bytes
+ if overflow := stat.Size() % indexEntrySize; overflow != 0 {
+ truncateFreezerFile(t.index, stat.Size()-overflow) // New file can't trigger this path
+ }
+ // Retrieve the file sizes and prepare for truncation
+ if stat, err = t.index.Stat(); err != nil {
+ return err
+ }
+ offsetsSize := stat.Size()
+
+ // Open the head file
+ var (
+ firstIndex indexEntry
+ lastIndex indexEntry
+ contentSize int64
+ contentExp int64
+ )
+ // Read index zero, determine what file is the earliest
+ // and what item offset to use
+ t.index.ReadAt(buffer, 0)
+ firstIndex.unmarshalBinary(buffer)
+
+ t.tailId = firstIndex.offset
+ t.itemOffset = firstIndex.filenum
+
+ t.index.ReadAt(buffer, offsetsSize-indexEntrySize)
+ lastIndex.unmarshalBinary(buffer)
+ t.head, err = t.openFile(lastIndex.filenum, openFreezerFileForAppend)
+ if err != nil {
+ return err
+ }
+ if stat, err = t.head.Stat(); err != nil {
+ return err
+ }
+ contentSize = stat.Size()
+
+ // Keep truncating both files until they come in sync
+ contentExp = int64(lastIndex.offset)
+
+ for contentExp != contentSize {
+ // Truncate the head file to the last offset pointer
+ if contentExp < contentSize {
+ t.logger.Warn("Truncating dangling head", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize))
+ if err := truncateFreezerFile(t.head, contentExp); err != nil {
+ return err
+ }
+ contentSize = contentExp
+ }
+ // Truncate the index to point within the head file
+ if contentExp > contentSize {
+ t.logger.Warn("Truncating dangling indexes", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize))
+ if err := truncateFreezerFile(t.index, offsetsSize-indexEntrySize); err != nil {
+ return err
+ }
+ offsetsSize -= indexEntrySize
+ t.index.ReadAt(buffer, offsetsSize-indexEntrySize)
+ var newLastIndex indexEntry
+ newLastIndex.unmarshalBinary(buffer)
+ // We might have slipped back into an earlier head-file here
+ if newLastIndex.filenum != lastIndex.filenum {
+ // Release earlier opened file
+ t.releaseFile(lastIndex.filenum)
+ if t.head, err = t.openFile(newLastIndex.filenum, openFreezerFileForAppend); err != nil {
+ return err
+ }
+ if stat, err = t.head.Stat(); err != nil {
+ // TODO, anything more we can do here?
+ // A data file has gone missing...
+ return err
+ }
+ contentSize = stat.Size()
+ }
+ lastIndex = newLastIndex
+ contentExp = int64(lastIndex.offset)
+ }
+ }
+ // Ensure all reparation changes have been written to disk
+ if err := t.index.Sync(); err != nil {
+ return err
+ }
+ if err := t.head.Sync(); err != nil {
+ return err
+ }
+ // Update the item and byte counters and return
+ t.items = uint64(t.itemOffset) + uint64(offsetsSize/indexEntrySize-1) // last indexEntry points to the end of the data file
+ t.headBytes = uint32(contentSize)
+ t.headId = lastIndex.filenum
+
+ // Close opened files and preopen all files
+ if err := t.preopen(); err != nil {
+ return err
+ }
+ t.logger.Debug("Chain freezer table opened", "items", t.items, "size", common.StorageSize(t.headBytes))
+ return nil
+}
+
+// preopen opens all files that the freezer will need. This method should be called from an init-context,
+// since it assumes that it doesn't have to bother with locking
+// The rationale for doing preopen is to not have to do it from within Retrieve, thus not needing to ever
+// obtain a write-lock within Retrieve.
+func (t *freezerTable) preopen() (err error) {
+ // The repair might have already opened (some) files
+ t.releaseFilesAfter(0, false)
+ // Open all except head in RDONLY
+ for i := t.tailId; i < t.headId; i++ {
+ if _, err = t.openFile(i, openFreezerFileForReadOnly); err != nil {
+ return err
+ }
+ }
+ // Open head in read/write
+ t.head, err = t.openFile(t.headId, openFreezerFileForAppend)
+ return err
+}
+
+// truncate discards any recent data above the provided threshold number.
+func (t *freezerTable) truncate(items uint64) error {
+ t.lock.Lock()
+ defer t.lock.Unlock()
+
+ // If our item count is correct, don't do anything
+ if atomic.LoadUint64(&t.items) <= items {
+ return nil
+ }
+ // We need to truncate, save the old size for metrics tracking
+ oldSize, err := t.sizeNolock()
+ if err != nil {
+ return err
+ }
+ // Something's out of sync, truncate the table's offset index
+ t.logger.Warn("Truncating freezer table", "items", t.items, "limit", items)
+ if err := truncateFreezerFile(t.index, int64(items+1)*indexEntrySize); err != nil {
+ return err
+ }
+ // Calculate the new expected size of the data file and truncate it
+ buffer := make([]byte, indexEntrySize)
+ if _, err := t.index.ReadAt(buffer, int64(items*indexEntrySize)); err != nil {
+ return err
+ }
+ var expected indexEntry
+ expected.unmarshalBinary(buffer)
+
+ // We might need to truncate back to older files
+ if expected.filenum != t.headId {
+ // If already open for reading, force-reopen for writing
+ t.releaseFile(expected.filenum)
+ newHead, err := t.openFile(expected.filenum, openFreezerFileForAppend)
+ if err != nil {
+ return err
+ }
+ // Release any files _after the current head -- both the previous head
+ // and any files which may have been opened for reading
+ t.releaseFilesAfter(expected.filenum, true)
+ // Set back the historic head
+ t.head = newHead
+ atomic.StoreUint32(&t.headId, expected.filenum)
+ }
+ if err := truncateFreezerFile(t.head, int64(expected.offset)); err != nil {
+ return err
+ }
+ // All data files truncated, set internal counters and return
+ atomic.StoreUint64(&t.items, items)
+ atomic.StoreUint32(&t.headBytes, expected.offset)
+
+ // Retrieve the new size and update the total size counter
+ newSize, err := t.sizeNolock()
+ if err != nil {
+ return err
+ }
+ t.sizeCounter.Dec(int64(oldSize - newSize))
+
+ return nil
+}
+
+// Close closes all opened files.
+func (t *freezerTable) Close() error {
+ t.lock.Lock()
+ defer t.lock.Unlock()
+
+ var errs []error
+ if err := t.index.Close(); err != nil {
+ errs = append(errs, err)
+ }
+ t.index = nil
+
+ for _, f := range t.files {
+ if err := f.Close(); err != nil {
+ errs = append(errs, err)
+ }
+ }
+ t.head = nil
+
+ if errs != nil {
+ return fmt.Errorf("%v", errs)
+ }
+ return nil
+}
+
+// openFile assumes that the write-lock is held by the caller
+func (t *freezerTable) openFile(num uint32, opener func(string) (*os.File, error)) (f *os.File, err error) {
+ var exist bool
+ if f, exist = t.files[num]; !exist {
+ var name string
+ if t.noCompression {
+ name = fmt.Sprintf("%s.%04d.rdat", t.name, num)
+ } else {
+ name = fmt.Sprintf("%s.%04d.cdat", t.name, num)
+ }
+ f, err = opener(filepath.Join(t.path, name))
+ if err != nil {
+ return nil, err
+ }
+ t.files[num] = f
+ }
+ return f, err
+}
+
+// releaseFile closes a file, and removes it from the open file cache.
+// Assumes that the caller holds the write lock
+func (t *freezerTable) releaseFile(num uint32) {
+ if f, exist := t.files[num]; exist {
+ delete(t.files, num)
+ f.Close()
+ }
+}
+
+// releaseFilesAfter closes all open files with a higher number, and optionally also deletes the files
+func (t *freezerTable) releaseFilesAfter(num uint32, remove bool) {
+ for fnum, f := range t.files {
+ if fnum > num {
+ delete(t.files, fnum)
+ f.Close()
+ if remove {
+ os.Remove(f.Name())
+ }
+ }
+ }
+}
+
+// Append injects a binary blob at the end of the freezer table. The item number
+// is a precautionary parameter to ensure data correctness, but the table will
+// reject already existing data.
+//
+// Note, this method will *not* flush any data to disk so be sure to explicitly
+// fsync before irreversibly deleting data from the database.
+func (t *freezerTable) Append(item uint64, blob []byte) error {
+ // Read lock prevents competition with truncate
+ t.lock.RLock()
+ // Ensure the table is still accessible
+ if t.index == nil || t.head == nil {
+ t.lock.RUnlock()
+ return errClosed
+ }
+ // Ensure only the next item can be written, nothing else
+ if atomic.LoadUint64(&t.items) != item {
+ t.lock.RUnlock()
+ return fmt.Errorf("appending unexpected item: want %d, have %d", t.items, item)
+ }
+ // Encode the blob and write it into the data file
+ if !t.noCompression {
+ blob = snappy.Encode(nil, blob)
+ }
+ bLen := uint32(len(blob))
+ if t.headBytes+bLen < bLen ||
+ t.headBytes+bLen > t.maxFileSize {
+ // we need a new file, writing would overflow
+ t.lock.RUnlock()
+ t.lock.Lock()
+ nextID := atomic.LoadUint32(&t.headId) + 1
+ // We open the next file in truncated mode -- if this file already
+ // exists, we need to start over from scratch on it
+ newHead, err := t.openFile(nextID, openFreezerFileTruncated)
+ if err != nil {
+ t.lock.Unlock()
+ return err
+ }
+ // Close old file, and reopen in RDONLY mode
+ t.releaseFile(t.headId)
+ t.openFile(t.headId, openFreezerFileForReadOnly)
+
+ // Swap out the current head
+ t.head = newHead
+ atomic.StoreUint32(&t.headBytes, 0)
+ atomic.StoreUint32(&t.headId, nextID)
+ t.lock.Unlock()
+ t.lock.RLock()
+ }
+
+ defer t.lock.RUnlock()
+ if _, err := t.head.Write(blob); err != nil {
+ return err
+ }
+ newOffset := atomic.AddUint32(&t.headBytes, bLen)
+ idx := indexEntry{
+ filenum: atomic.LoadUint32(&t.headId),
+ offset: newOffset,
+ }
+ // Write indexEntry
+ t.index.Write(idx.marshallBinary())
+
+ t.writeMeter.Mark(int64(bLen + indexEntrySize))
+ t.sizeCounter.Inc(int64(bLen + indexEntrySize))
+
+ atomic.AddUint64(&t.items, 1)
+ return nil
+}
+
+// getBounds returns the indexes for the item
+// returns start, end, filenumber and error
+func (t *freezerTable) getBounds(item uint64) (uint32, uint32, uint32, error) {
+ var startIdx, endIdx indexEntry
+ buffer := make([]byte, indexEntrySize)
+ if _, err := t.index.ReadAt(buffer, int64(item*indexEntrySize)); err != nil {
+ return 0, 0, 0, err
+ }
+ startIdx.unmarshalBinary(buffer)
+ if _, err := t.index.ReadAt(buffer, int64((item+1)*indexEntrySize)); err != nil {
+ return 0, 0, 0, err
+ }
+ endIdx.unmarshalBinary(buffer)
+ if startIdx.filenum != endIdx.filenum {
+ // If a piece of data 'crosses' a data-file,
+ // it's actually in one piece on the second data-file.
+ // We return a zero-indexEntry for the second file as start
+ return 0, endIdx.offset, endIdx.filenum, nil
+ }
+ return startIdx.offset, endIdx.offset, endIdx.filenum, nil
+}
+
+// Retrieve looks up the data offset of an item with the given number and retrieves
+// the raw binary blob from the data file.
+func (t *freezerTable) Retrieve(item uint64) ([]byte, error) {
+ // Ensure the table and the item is accessible
+ if t.index == nil || t.head == nil {
+ return nil, errClosed
+ }
+ if atomic.LoadUint64(&t.items) <= item {
+ return nil, errOutOfBounds
+ }
+ // Ensure the item was not deleted from the tail either
+ offset := atomic.LoadUint32(&t.itemOffset)
+ if uint64(offset) > item {
+ return nil, errOutOfBounds
+ }
+ t.lock.RLock()
+ startOffset, endOffset, filenum, err := t.getBounds(item - uint64(offset))
+ if err != nil {
+ t.lock.RUnlock()
+ return nil, err
+ }
+ dataFile, exist := t.files[filenum]
+ if !exist {
+ t.lock.RUnlock()
+ return nil, fmt.Errorf("missing data file %d", filenum)
+ }
+ // Retrieve the data itself, decompress and return
+ blob := make([]byte, endOffset-startOffset)
+ if _, err := dataFile.ReadAt(blob, int64(startOffset)); err != nil {
+ t.lock.RUnlock()
+ return nil, err
+ }
+ t.lock.RUnlock()
+ t.readMeter.Mark(int64(len(blob) + 2*indexEntrySize))
+
+ if t.noCompression {
+ return blob, nil
+ }
+ return snappy.Decode(nil, blob)
+}
+
+// has returns an indicator whether the specified number data
+// exists in the freezer table.
+func (t *freezerTable) has(number uint64) bool {
+ return atomic.LoadUint64(&t.items) > number
+}
+
+// size returns the total data size in the freezer table.
+func (t *freezerTable) size() (uint64, error) {
+ t.lock.RLock()
+ defer t.lock.RUnlock()
+
+ return t.sizeNolock()
+}
+
+// sizeNolock returns the total data size in the freezer table without obtaining
+// the mutex first.
+func (t *freezerTable) sizeNolock() (uint64, error) {
+ stat, err := t.index.Stat()
+ if err != nil {
+ return 0, err
+ }
+ total := uint64(t.maxFileSize)*uint64(t.headId-t.tailId) + uint64(t.headBytes) + uint64(stat.Size())
+ return total, nil
+}
+
+// Sync pushes any pending data from memory out to disk. This is an expensive
+// operation, so use it with care.
+func (t *freezerTable) Sync() error {
+ if err := t.index.Sync(); err != nil {
+ return err
+ }
+ return t.head.Sync()
+}
+
+// printIndex is a debug print utility function for testing
+func (t *freezerTable) printIndex() {
+ buf := make([]byte, indexEntrySize)
+
+ fmt.Printf("|-----------------|\n")
+ fmt.Printf("| fileno | offset |\n")
+ fmt.Printf("|--------+--------|\n")
+
+ for i := uint64(0); ; i++ {
+ if _, err := t.index.ReadAt(buf, int64(i*indexEntrySize)); err != nil {
+ break
+ }
+ var entry indexEntry
+ entry.unmarshalBinary(buf)
+ fmt.Printf("| %03d | %03d | \n", entry.filenum, entry.offset)
+ if i > 100 {
+ fmt.Printf(" ... \n")
+ break
+ }
+ }
+ fmt.Printf("|-----------------|\n")
+}
diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go
new file mode 100644
index 0000000..51d1f66
--- /dev/null
+++ b/core/rawdb/schema.go
@@ -0,0 +1,166 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package rawdb contains a collection of low level database accessors.
+package rawdb
+
+import (
+ "encoding/binary"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/metrics"
+)
+
+// The fields below define the low level database schema prefixing.
+var (
+ // databaseVerisionKey tracks the current database version.
+ databaseVerisionKey = []byte("DatabaseVersion")
+
+ // headHeaderKey tracks the latest known header's hash.
+ headHeaderKey = []byte("LastHeader")
+
+ // headBlockKey tracks the latest known full block's hash.
+ headBlockKey = []byte("LastBlock")
+
+ // headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
+ headFastBlockKey = []byte("LastFast")
+
+ // fastTrieProgressKey tracks the number of trie entries imported during fast sync.
+ fastTrieProgressKey = []byte("TrieSync")
+
+ // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
+ headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
+ headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
+ headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
+ headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
+
+ blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
+ blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
+
+ txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
+ bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
+
+ preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
+ configPrefix = []byte("ethereum-config-") // config prefix for the db
+
+ // Chain index prefixes (use `i` + single byte to avoid mixing data types).
+ BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
+
+ preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
+ preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
+)
+
+const (
+ // freezerHeaderTable indicates the name of the freezer header table.
+ freezerHeaderTable = "headers"
+
+ // freezerHashTable indicates the name of the freezer canonical hash table.
+ freezerHashTable = "hashes"
+
+ // freezerBodiesTable indicates the name of the freezer block body table.
+ freezerBodiesTable = "bodies"
+
+ // freezerReceiptTable indicates the name of the freezer receipts table.
+ freezerReceiptTable = "receipts"
+
+ // freezerDifficultyTable indicates the name of the freezer total difficulty table.
+ freezerDifficultyTable = "diffs"
+)
+
+// freezerNoSnappy configures whether compression is disabled for the ancient-tables.
+// Hashes and difficulties don't compress well.
+var freezerNoSnappy = map[string]bool{
+ freezerHeaderTable: false,
+ freezerHashTable: true,
+ freezerBodiesTable: false,
+ freezerReceiptTable: false,
+ freezerDifficultyTable: true,
+}
+
+// LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
+// fields.
+type LegacyTxLookupEntry struct {
+ BlockHash common.Hash
+ BlockIndex uint64
+ Index uint64
+}
+
+// encodeBlockNumber encodes a block number as big endian uint64
+func encodeBlockNumber(number uint64) []byte {
+ enc := make([]byte, 8)
+ binary.BigEndian.PutUint64(enc, number)
+ return enc
+}
+
+// headerKeyPrefix = headerPrefix + num (uint64 big endian)
+func headerKeyPrefix(number uint64) []byte {
+ return append(headerPrefix, encodeBlockNumber(number)...)
+}
+
+// headerKey = headerPrefix + num (uint64 big endian) + hash
+func headerKey(number uint64, hash common.Hash) []byte {
+ return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
+}
+
+// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
+func headerTDKey(number uint64, hash common.Hash) []byte {
+ return append(headerKey(number, hash), headerTDSuffix...)
+}
+
+// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
+func headerHashKey(number uint64) []byte {
+ return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
+}
+
+// headerNumberKey = headerNumberPrefix + hash
+func headerNumberKey(hash common.Hash) []byte {
+ return append(headerNumberPrefix, hash.Bytes()...)
+}
+
+// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
+func blockBodyKey(number uint64, hash common.Hash) []byte {
+ return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
+}
+
+// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
+func blockReceiptsKey(number uint64, hash common.Hash) []byte {
+ return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
+}
+
+// txLookupKey = txLookupPrefix + hash
+func txLookupKey(hash common.Hash) []byte {
+ return append(txLookupPrefix, hash.Bytes()...)
+}
+
+// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
+func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
+ key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
+
+ binary.BigEndian.PutUint16(key[1:], uint16(bit))
+ binary.BigEndian.PutUint64(key[3:], section)
+
+ return key
+}
+
+// preimageKey = preimagePrefix + hash
+func preimageKey(hash common.Hash) []byte {
+ return append(preimagePrefix, hash.Bytes()...)
+}
+
+// configKey = configPrefix + hash
+func configKey(hash common.Hash) []byte {
+ return append(configPrefix, hash.Bytes()...)
+}
diff --git a/core/rawdb/table.go b/core/rawdb/table.go
new file mode 100644
index 0000000..f9078e8
--- /dev/null
+++ b/core/rawdb/table.go
@@ -0,0 +1,204 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rawdb
+
+import (
+ "github.com/ava-labs/go-ethereum/ethdb"
+)
+
+// table is a wrapper around a database that prefixes each key access with a pre-
+// configured string.
+type table struct {
+ db ethdb.Database
+ prefix string
+}
+
+// NewTable returns a database object that prefixes all keys with a given string.
+func NewTable(db ethdb.Database, prefix string) ethdb.Database {
+ return &table{
+ db: db,
+ prefix: prefix,
+ }
+}
+
+// Close is a noop to implement the Database interface.
+func (t *table) Close() error {
+ return nil
+}
+
+// Has retrieves if a prefixed version of a key is present in the database.
+func (t *table) Has(key []byte) (bool, error) {
+ return t.db.Has(append([]byte(t.prefix), key...))
+}
+
+// Get retrieves the given prefixed key if it's present in the database.
+func (t *table) Get(key []byte) ([]byte, error) {
+ return t.db.Get(append([]byte(t.prefix), key...))
+}
+
+// HasAncient is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) HasAncient(kind string, number uint64) (bool, error) {
+ return t.db.HasAncient(kind, number)
+}
+
+// Ancient is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) Ancient(kind string, number uint64) ([]byte, error) {
+ return t.db.Ancient(kind, number)
+}
+
+// Ancients is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) Ancients() (uint64, error) {
+ return t.db.Ancients()
+}
+
+// AncientSize is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) AncientSize(kind string) (uint64, error) {
+ return t.db.AncientSize(kind)
+}
+
+// AppendAncient is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) AppendAncient(number uint64, hash, header, body, receipts, td []byte) error {
+ return t.db.AppendAncient(number, hash, header, body, receipts, td)
+}
+
+// TruncateAncients is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) TruncateAncients(items uint64) error {
+ return t.db.TruncateAncients(items)
+}
+
+// Sync is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) Sync() error {
+ return t.db.Sync()
+}
+
+// Put inserts the given value into the database at a prefixed version of the
+// provided key.
+func (t *table) Put(key []byte, value []byte) error {
+ return t.db.Put(append([]byte(t.prefix), key...), value)
+}
+
+// Delete removes the given prefixed key from the database.
+func (t *table) Delete(key []byte) error {
+ return t.db.Delete(append([]byte(t.prefix), key...))
+}
+
+// NewIterator creates a binary-alphabetical iterator over the entire keyspace
+// contained within the database.
+func (t *table) NewIterator() ethdb.Iterator {
+ return t.NewIteratorWithPrefix(nil)
+}
+
+// NewIteratorWithStart creates a binary-alphabetical iterator over a subset of
+// database content starting at a particular initial key (or after, if it does
+// not exist).
+func (t *table) NewIteratorWithStart(start []byte) ethdb.Iterator {
+ return t.db.NewIteratorWithStart(start)
+}
+
+// NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
+// of database content with a particular key prefix.
+func (t *table) NewIteratorWithPrefix(prefix []byte) ethdb.Iterator {
+ return t.db.NewIteratorWithPrefix(append([]byte(t.prefix), prefix...))
+}
+
+// Stat returns a particular internal stat of the database.
+func (t *table) Stat(property string) (string, error) {
+ return t.db.Stat(property)
+}
+
+// Compact flattens the underlying data store for the given key range. In essence,
+// deleted and overwritten versions are discarded, and the data is rearranged to
+// reduce the cost of operations needed to access them.
+//
+// A nil start is treated as a key before all keys in the data store; a nil limit
+// is treated as a key after all keys in the data store. If both is nil then it
+// will compact entire data store.
+func (t *table) Compact(start []byte, limit []byte) error {
+ // If no start was specified, use the table prefix as the first value
+ if start == nil {
+ start = []byte(t.prefix)
+ }
+ // If no limit was specified, use the first element not matching the prefix
+ // as the limit
+ if limit == nil {
+ limit = []byte(t.prefix)
+ for i := len(limit) - 1; i >= 0; i-- {
+ // Bump the current character, stopping if it doesn't overflow
+ limit[i]++
+ if limit[i] > 0 {
+ break
+ }
+ // Character overflown, proceed to the next or nil if the last
+ if i == 0 {
+ limit = nil
+ }
+ }
+ }
+ // Range correctly calculated based on table prefix, delegate down
+ return t.db.Compact(start, limit)
+}
+
+// NewBatch creates a write-only database that buffers changes to its host db
+// until a final write is called, each operation prefixing all keys with the
+// pre-configured string.
+func (t *table) NewBatch() ethdb.Batch {
+ return &tableBatch{t.db.NewBatch(), t.prefix}
+}
+
+// tableBatch is a wrapper around a database batch that prefixes each key access
+// with a pre-configured string.
+type tableBatch struct {
+ batch ethdb.Batch
+ prefix string
+}
+
+// Put inserts the given value into the batch for later committing.
+func (b *tableBatch) Put(key, value []byte) error {
+ return b.batch.Put(append([]byte(b.prefix), key...), value)
+}
+
+// Delete inserts the a key removal into the batch for later committing.
+func (b *tableBatch) Delete(key []byte) error {
+ return b.batch.Delete(append([]byte(b.prefix), key...))
+}
+
+// ValueSize retrieves the amount of data queued up for writing.
+func (b *tableBatch) ValueSize() int {
+ return b.batch.ValueSize()
+}
+
+// Write flushes any accumulated data to disk.
+func (b *tableBatch) Write() error {
+ return b.batch.Write()
+}
+
+// Reset resets the batch for reuse.
+func (b *tableBatch) Reset() {
+ b.batch.Reset()
+}
+
+// Replay replays the batch contents.
+func (b *tableBatch) Replay(w ethdb.KeyValueWriter) error {
+ return b.batch.Replay(w)
+}
diff --git a/core/state/database.go b/core/state/database.go
new file mode 100644
index 0000000..8c641c3
--- /dev/null
+++ b/core/state/database.go
@@ -0,0 +1,163 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package state
+
+import (
+ "fmt"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/trie"
+ lru "github.com/hashicorp/golang-lru"
+)
+
+const (
+ // Number of codehash->size associations to keep.
+ codeSizeCacheSize = 100000
+)
+
+// Database wraps access to tries and contract code.
+type Database interface {
+ // OpenTrie opens the main account trie.
+ OpenTrie(root common.Hash) (Trie, error)
+
+ // OpenStorageTrie opens the storage trie of an account.
+ OpenStorageTrie(addrHash, root common.Hash) (Trie, error)
+
+ // CopyTrie returns an independent copy of the given trie.
+ CopyTrie(Trie) Trie
+
+ // ContractCode retrieves a particular contract's code.
+ ContractCode(addrHash, codeHash common.Hash) ([]byte, error)
+
+ // ContractCodeSize retrieves a particular contracts code's size.
+ ContractCodeSize(addrHash, codeHash common.Hash) (int, error)
+
+ // TrieDB retrieves the low level trie database used for data storage.
+ TrieDB() *trie.Database
+}
+
+// Trie is a Ethereum Merkle Patricia trie.
+type Trie interface {
+ // GetKey returns the sha3 preimage of a hashed key that was previously used
+ // to store a value.
+ //
+ // TODO(fjl): remove this when SecureTrie is removed
+ GetKey([]byte) []byte
+
+ // TryGet returns the value for key stored in the trie. The value bytes must
+ // not be modified by the caller. If a node was not found in the database, a
+ // trie.MissingNodeError is returned.
+ TryGet(key []byte) ([]byte, error)
+
+ // TryUpdate associates key with value in the trie. If value has length zero, any
+ // existing value is deleted from the trie. The value bytes must not be modified
+ // by the caller while they are stored in the trie. If a node was not found in the
+ // database, a trie.MissingNodeError is returned.
+ TryUpdate(key, value []byte) error
+
+ // TryDelete removes any existing value for key from the trie. If a node was not
+ // found in the database, a trie.MissingNodeError is returned.
+ TryDelete(key []byte) error
+
+ // Hash returns the root hash of the trie. It does not write to the database and
+ // can be used even if the trie doesn't have one.
+ Hash() common.Hash
+
+ // Commit writes all nodes to the trie's memory database, tracking the internal
+ // and external (for account tries) references.
+ Commit(onleaf trie.LeafCallback) (common.Hash, error)
+
+ // NodeIterator returns an iterator that returns nodes of the trie. Iteration
+ // starts at the key after the given start key.
+ NodeIterator(startKey []byte) trie.NodeIterator
+
+ // Prove constructs a Merkle proof for key. The result contains all encoded nodes
+ // on the path to the value at key. The value itself is also included in the last
+ // node and can be retrieved by verifying the proof.
+ //
+ // If the trie does not contain a value for key, the returned proof contains all
+ // nodes of the longest existing prefix of the key (at least the root), ending
+ // with the node that proves the absence of the key.
+ Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error
+}
+
+// NewDatabase creates a backing store for state. The returned database is safe for
+// concurrent use, but does not retain any recent trie nodes in memory. To keep some
+// historical state in memory, use the NewDatabaseWithCache constructor.
+func NewDatabase(db ethdb.Database) Database {
+ return NewDatabaseWithCache(db, 0)
+}
+
+// NewDatabaseWithCache creates a backing store for state. The returned database
+// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
+// large memory cache.
+func NewDatabaseWithCache(db ethdb.Database, cache int) Database {
+ csc, _ := lru.New(codeSizeCacheSize)
+ return &cachingDB{
+ db: trie.NewDatabaseWithCache(db, cache),
+ codeSizeCache: csc,
+ }
+}
+
+type cachingDB struct {
+ db *trie.Database
+ codeSizeCache *lru.Cache
+}
+
+// OpenTrie opens the main account trie at a specific root hash.
+func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
+ return trie.NewSecure(root, db.db)
+}
+
+// OpenStorageTrie opens the storage trie of an account.
+func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
+ return trie.NewSecure(root, db.db)
+}
+
+// CopyTrie returns an independent copy of the given trie.
+func (db *cachingDB) CopyTrie(t Trie) Trie {
+ switch t := t.(type) {
+ case *trie.SecureTrie:
+ return t.Copy()
+ default:
+ panic(fmt.Errorf("unknown trie type %T", t))
+ }
+}
+
+// ContractCode retrieves a particular contract's code.
+func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
+ code, err := db.db.Node(codeHash)
+ if err == nil {
+ db.codeSizeCache.Add(codeHash, len(code))
+ }
+ return code, err
+}
+
+// ContractCodeSize retrieves a particular contracts code's size.
+func (db *cachingDB) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) {
+ if cached, ok := db.codeSizeCache.Get(codeHash); ok {
+ return cached.(int), nil
+ }
+ code, err := db.ContractCode(addrHash, codeHash)
+ return len(code), err
+}
+
+// TrieDB retrieves any intermediate trie-node caching layer.
+func (db *cachingDB) TrieDB() *trie.Database {
+ return db.db
+}
diff --git a/core/state/dump.go b/core/state/dump.go
new file mode 100644
index 0000000..91c7d08
--- /dev/null
+++ b/core/state/dump.go
@@ -0,0 +1,158 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package state
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/rlp"
+ "github.com/ava-labs/go-ethereum/trie"
+)
+
+// DumpAccount represents an account in the state
+type DumpAccount struct {
+ Balance string `json:"balance"`
+ Nonce uint64 `json:"nonce"`
+ Root string `json:"root"`
+ CodeHash string `json:"codeHash"`
+ Code string `json:"code,omitempty"`
+ Storage map[common.Hash]string `json:"storage,omitempty"`
+ Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
+ SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
+
+}
+
+// Dump represents the full dump in a collected format, as one large map
+type Dump struct {
+ Root string `json:"root"`
+ Accounts map[common.Address]DumpAccount `json:"accounts"`
+}
+
+// iterativeDump is a 'collector'-implementation which dump output line-by-line iteratively
+type iterativeDump json.Encoder
+
+// Collector interface which the state trie calls during iteration
+type collector interface {
+ onRoot(common.Hash)
+ onAccount(common.Address, DumpAccount)
+}
+
+func (self *Dump) onRoot(root common.Hash) {
+ self.Root = fmt.Sprintf("%x", root)
+}
+
+func (self *Dump) onAccount(addr common.Address, account DumpAccount) {
+ self.Accounts[addr] = account
+}
+
+func (self iterativeDump) onAccount(addr common.Address, account DumpAccount) {
+ dumpAccount := &DumpAccount{
+ Balance: account.Balance,
+ Nonce: account.Nonce,
+ Root: account.Root,
+ CodeHash: account.CodeHash,
+ Code: account.Code,
+ Storage: account.Storage,
+ SecureKey: account.SecureKey,
+ Address: nil,
+ }
+ if addr != (common.Address{}) {
+ dumpAccount.Address = &addr
+ }
+ (*json.Encoder)(&self).Encode(dumpAccount)
+}
+func (self iterativeDump) onRoot(root common.Hash) {
+ (*json.Encoder)(&self).Encode(struct {
+ Root common.Hash `json:"root"`
+ }{root})
+}
+
+func (self *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
+ emptyAddress := (common.Address{})
+ missingPreimages := 0
+ c.onRoot(self.trie.Hash())
+ it := trie.NewIterator(self.trie.NodeIterator(nil))
+ for it.Next() {
+ var data Account
+ if err := rlp.DecodeBytes(it.Value, &data); err != nil {
+ panic(err)
+ }
+ addr := common.BytesToAddress(self.trie.GetKey(it.Key))
+ obj := newObject(nil, addr, data)
+ account := DumpAccount{
+ Balance: data.Balance.String(),
+ Nonce: data.Nonce,
+ Root: common.Bytes2Hex(data.Root[:]),
+ CodeHash: common.Bytes2Hex(data.CodeHash),
+ }
+ if emptyAddress == addr {
+ // Preimage missing
+ missingPreimages++
+ if excludeMissingPreimages {
+ continue
+ }
+ account.SecureKey = it.Key
+ }
+ if !excludeCode {
+ account.Code = common.Bytes2Hex(obj.Code(self.db))
+ }
+ if !excludeStorage {
+ account.Storage = make(map[common.Hash]string)
+ storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
+ for storageIt.Next() {
+ _, content, _, err := rlp.Split(storageIt.Value)
+ if err != nil {
+ log.Error("Failed to decode the value returned by iterator", "error", err)
+ continue
+ }
+ account.Storage[common.BytesToHash(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
+ }
+ }
+ c.onAccount(addr, account)
+ }
+ if missingPreimages > 0 {
+ log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages)
+ }
+}
+
+// RawDump returns the entire state an a single large object
+func (self *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
+ dump := &Dump{
+ Accounts: make(map[common.Address]DumpAccount),
+ }
+ self.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
+ return *dump
+}
+
+// Dump returns a JSON string representing the entire state as a single json-object
+func (self *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
+ dump := self.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
+ json, err := json.MarshalIndent(dump, "", " ")
+ if err != nil {
+ fmt.Println("dump err", err)
+ }
+ return json
+}
+
+// IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
+func (self *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
+ self.dump(iterativeDump(*output), excludeCode, excludeStorage, excludeMissingPreimages)
+}
diff --git a/core/state/iterator.go b/core/state/iterator.go
new file mode 100644
index 0000000..c6d2a48
--- /dev/null
+++ b/core/state/iterator.go
@@ -0,0 +1,154 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package state
+
+import (
+ "bytes"
+ "fmt"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/rlp"
+ "github.com/ava-labs/go-ethereum/trie"
+)
+
+// NodeIterator is an iterator to traverse the entire state trie post-order,
+// including all of the contract code and contract state tries.
+type NodeIterator struct {
+ state *StateDB // State being iterated
+
+ stateIt trie.NodeIterator // Primary iterator for the global state trie
+ dataIt trie.NodeIterator // Secondary iterator for the data trie of a contract
+
+ accountHash common.Hash // Hash of the node containing the account
+ codeHash common.Hash // Hash of the contract source code
+ code []byte // Source code associated with a contract
+
+ Hash common.Hash // Hash of the current entry being iterated (nil if not standalone)
+ Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
+
+ Error error // Failure set in case of an internal error in the iterator
+}
+
+// NewNodeIterator creates an post-order state node iterator.
+func NewNodeIterator(state *StateDB) *NodeIterator {
+ return &NodeIterator{
+ state: state,
+ }
+}
+
+// Next moves the iterator to the next node, returning whether there are any
+// further nodes. In case of an internal error this method returns false and
+// sets the Error field to the encountered failure.
+func (it *NodeIterator) Next() bool {
+ // If the iterator failed previously, don't do anything
+ if it.Error != nil {
+ return false
+ }
+ // Otherwise step forward with the iterator and report any errors
+ if err := it.step(); err != nil {
+ it.Error = err
+ return false
+ }
+ return it.retrieve()
+}
+
+// step moves the iterator to the next entry of the state trie.
+func (it *NodeIterator) step() error {
+ // Abort if we reached the end of the iteration
+ if it.state == nil {
+ return nil
+ }
+ // Initialize the iterator if we've just started
+ if it.stateIt == nil {
+ it.stateIt = it.state.trie.NodeIterator(nil)
+ }
+ // If we had data nodes previously, we surely have at least state nodes
+ if it.dataIt != nil {
+ if cont := it.dataIt.Next(true); !cont {
+ if it.dataIt.Error() != nil {
+ return it.dataIt.Error()
+ }
+ it.dataIt = nil
+ }
+ return nil
+ }
+ // If we had source code previously, discard that
+ if it.code != nil {
+ it.code = nil
+ return nil
+ }
+ // Step to the next state trie node, terminating if we're out of nodes
+ if cont := it.stateIt.Next(true); !cont {
+ if it.stateIt.Error() != nil {
+ return it.stateIt.Error()
+ }
+ it.state, it.stateIt = nil, nil
+ return nil
+ }
+ // If the state trie node is an internal entry, leave as is
+ if !it.stateIt.Leaf() {
+ return nil
+ }
+ // Otherwise we've reached an account node, initiate data iteration
+ var account Account
+ if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
+ return err
+ }
+ dataTrie, err := it.state.db.OpenStorageTrie(common.BytesToHash(it.stateIt.LeafKey()), account.Root)
+ if err != nil {
+ return err
+ }
+ it.dataIt = dataTrie.NodeIterator(nil)
+ if !it.dataIt.Next(true) {
+ it.dataIt = nil
+ }
+ if !bytes.Equal(account.CodeHash, emptyCodeHash) {
+ it.codeHash = common.BytesToHash(account.CodeHash)
+ addrHash := common.BytesToHash(it.stateIt.LeafKey())
+ it.code, err = it.state.db.ContractCode(addrHash, common.BytesToHash(account.CodeHash))
+ if err != nil {
+ return fmt.Errorf("code %x: %v", account.CodeHash, err)
+ }
+ }
+ it.accountHash = it.stateIt.Parent()
+ return nil
+}
+
+// retrieve pulls and caches the current state entry the iterator is traversing.
+// The method returns whether there are any more data left for inspection.
+func (it *NodeIterator) retrieve() bool {
+ // Clear out any previously set values
+ it.Hash = common.Hash{}
+
+ // If the iteration's done, return no available data
+ if it.state == nil {
+ return false
+ }
+ // Otherwise retrieve the current entry
+ switch {
+ case it.dataIt != nil:
+ it.Hash, it.Parent = it.dataIt.Hash(), it.dataIt.Parent()
+ if it.Parent == (common.Hash{}) {
+ it.Parent = it.accountHash
+ }
+ case it.code != nil:
+ it.Hash, it.Parent = it.codeHash, it.accountHash
+ case it.stateIt != nil:
+ it.Hash, it.Parent = it.stateIt.Hash(), it.stateIt.Parent()
+ }
+ return true
+}
diff --git a/core/state/journal.go b/core/state/journal.go
new file mode 100644
index 0000000..6e85173
--- /dev/null
+++ b/core/state/journal.go
@@ -0,0 +1,245 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package state
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// journalEntry is a modification entry in the state change journal that can be
+// reverted on demand.
+type journalEntry interface {
+ // revert undoes the changes introduced by this journal entry.
+ revert(*StateDB)
+
+ // dirtied returns the Ethereum address modified by this journal entry.
+ dirtied() *common.Address
+}
+
+// journal contains the list of state modifications applied since the last state
+// commit. These are tracked to be able to be reverted in case of an execution
+// exception or revertal request.
+type journal struct {
+ entries []journalEntry // Current changes tracked by the journal
+ dirties map[common.Address]int // Dirty accounts and the number of changes
+}
+
+// newJournal create a new initialized journal.
+func newJournal() *journal {
+ return &journal{
+ dirties: make(map[common.Address]int),
+ }
+}
+
+// append inserts a new modification entry to the end of the change journal.
+func (j *journal) append(entry journalEntry) {
+ j.entries = append(j.entries, entry)
+ if addr := entry.dirtied(); addr != nil {
+ j.dirties[*addr]++
+ }
+}
+
+// revert undoes a batch of journalled modifications along with any reverted
+// dirty handling too.
+func (j *journal) revert(statedb *StateDB, snapshot int) {
+ for i := len(j.entries) - 1; i >= snapshot; i-- {
+ // Undo the changes made by the operation
+ j.entries[i].revert(statedb)
+
+ // Drop any dirty tracking induced by the change
+ if addr := j.entries[i].dirtied(); addr != nil {
+ if j.dirties[*addr]--; j.dirties[*addr] == 0 {
+ delete(j.dirties, *addr)
+ }
+ }
+ }
+ j.entries = j.entries[:snapshot]
+}
+
+// dirty explicitly sets an address to dirty, even if the change entries would
+// otherwise suggest it as clean. This method is an ugly hack to handle the RIPEMD
+// precompile consensus exception.
+func (j *journal) dirty(addr common.Address) {
+ j.dirties[addr]++
+}
+
+// length returns the current number of entries in the journal.
+func (j *journal) length() int {
+ return len(j.entries)
+}
+
+type (
+ // Changes to the account trie.
+ createObjectChange struct {
+ account *common.Address
+ }
+ resetObjectChange struct {
+ prev *stateObject
+ }
+ suicideChange struct {
+ account *common.Address
+ prev bool // whether account had already suicided
+ prevbalance *big.Int
+ }
+
+ // Changes to individual accounts.
+ balanceChange struct {
+ account *common.Address
+ prev *big.Int
+ }
+ multiCoinEnable struct {
+ account *common.Address
+ }
+ nonceChange struct {
+ account *common.Address
+ prev uint64
+ }
+ storageChange struct {
+ account *common.Address
+ key, prevalue common.Hash
+ }
+ codeChange struct {
+ account *common.Address
+ prevcode, prevhash []byte
+ }
+
+ // Changes to other state values.
+ refundChange struct {
+ prev uint64
+ }
+ addLogChange struct {
+ txhash common.Hash
+ }
+ addPreimageChange struct {
+ hash common.Hash
+ }
+ touchChange struct {
+ account *common.Address
+ prev bool
+ prevDirty bool
+ }
+)
+
+func (ch createObjectChange) revert(s *StateDB) {
+ delete(s.stateObjects, *ch.account)
+ delete(s.stateObjectsDirty, *ch.account)
+}
+
+func (ch createObjectChange) dirtied() *common.Address {
+ return ch.account
+}
+
+func (ch resetObjectChange) revert(s *StateDB) {
+ s.setStateObject(ch.prev)
+}
+
+func (ch resetObjectChange) dirtied() *common.Address {
+ return nil
+}
+
+func (ch suicideChange) revert(s *StateDB) {
+ obj := s.getStateObject(*ch.account)
+ if obj != nil {
+ obj.suicided = ch.prev
+ obj.setBalance(ch.prevbalance)
+ }
+}
+
+func (ch suicideChange) dirtied() *common.Address {
+ return ch.account
+}
+
+var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
+
+func (ch touchChange) revert(s *StateDB) {
+}
+
+func (ch touchChange) dirtied() *common.Address {
+ return ch.account
+}
+
+func (ch balanceChange) revert(s *StateDB) {
+ s.getStateObject(*ch.account).setBalance(ch.prev)
+}
+
+func (ch balanceChange) dirtied() *common.Address {
+ return ch.account
+}
+
+func (ch multiCoinEnable) revert(s *StateDB) {
+ s.getStateObject(*ch.account).data.IsMultiCoin = false
+}
+
+func (ch multiCoinEnable) dirtied() *common.Address {
+ return ch.account
+}
+
+func (ch nonceChange) revert(s *StateDB) {
+ s.getStateObject(*ch.account).setNonce(ch.prev)
+}
+
+func (ch nonceChange) dirtied() *common.Address {
+ return ch.account
+}
+
+func (ch codeChange) revert(s *StateDB) {
+ s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
+}
+
+func (ch codeChange) dirtied() *common.Address {
+ return ch.account
+}
+
+func (ch storageChange) revert(s *StateDB) {
+ s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
+}
+
+func (ch storageChange) dirtied() *common.Address {
+ return ch.account
+}
+
+func (ch refundChange) revert(s *StateDB) {
+ s.refund = ch.prev
+}
+
+func (ch refundChange) dirtied() *common.Address {
+ return nil
+}
+
+func (ch addLogChange) revert(s *StateDB) {
+ logs := s.logs[ch.txhash]
+ if len(logs) == 1 {
+ delete(s.logs, ch.txhash)
+ } else {
+ s.logs[ch.txhash] = logs[:len(logs)-1]
+ }
+ s.logSize--
+}
+
+func (ch addLogChange) dirtied() *common.Address {
+ return nil
+}
+
+func (ch addPreimageChange) revert(s *StateDB) {
+ delete(s.preimages, ch.hash)
+}
+
+func (ch addPreimageChange) dirtied() *common.Address {
+ return nil
+}
diff --git a/core/state/state_object.go b/core/state/state_object.go
new file mode 100644
index 0000000..9c47dc4
--- /dev/null
+++ b/core/state/state_object.go
@@ -0,0 +1,499 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package state
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "math/big"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/metrics"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+var emptyCodeHash = crypto.Keccak256(nil)
+
+type Code []byte
+
+func (c Code) String() string {
+ return string(c) //strings.Join(Disassemble(c), " ")
+}
+
+type Storage map[common.Hash]common.Hash
+
+func (s Storage) String() (str string) {
+ for key, value := range s {
+ str += fmt.Sprintf("%X : %X\n", key, value)
+ }
+
+ return
+}
+
+func (s Storage) Copy() Storage {
+ cpy := make(Storage)
+ for key, value := range s {
+ cpy[key] = value
+ }
+
+ return cpy
+}
+
+// stateObject represents an Ethereum account which is being modified.
+//
+// The usage pattern is as follows:
+// First you need to obtain a state object.
+// Account values can be accessed and modified through the object.
+// Finally, call CommitTrie to write the modified storage trie into a database.
+type stateObject struct {
+ address common.Address
+ addrHash common.Hash // hash of ethereum address of the account
+ data Account
+ db *StateDB
+
+ // DB error.
+ // State objects are used by the consensus core and VM which are
+ // unable to deal with database-level errors. Any error that occurs
+ // during a database read is memoized here and will eventually be returned
+ // by StateDB.Commit.
+ dbErr error
+
+ // Write caches.
+ trie Trie // storage trie, which becomes non-nil on first access
+ code Code // contract bytecode, which gets set when code is loaded
+
+ originStorage Storage // Storage cache of original entries to dedup rewrites
+ dirtyStorage Storage // Storage entries that need to be flushed to disk
+ fakeStorage Storage // Fake storage which constructed by caller for debugging purpose.
+
+ // Cache flags.
+ // When an object is marked suicided it will be delete from the trie
+ // during the "update" phase of the state transition.
+ dirtyCode bool // true if the code was updated
+ suicided bool
+ deleted bool
+}
+
+// empty returns whether the account is considered empty.
+func (s *stateObject) empty() bool {
+ return s.data.Nonce == 0 && s.data.Balance.Sign() == 0 && bytes.Equal(s.data.CodeHash, emptyCodeHash)
+}
+
+// Account is the Ethereum consensus representation of accounts.
+// These objects are stored in the main account trie.
+type Account struct {
+ Nonce uint64
+ Balance *big.Int
+ Root common.Hash // merkle root of the storage trie
+ CodeHash []byte
+ IsMultiCoin bool
+}
+
+// newObject creates a state object.
+func newObject(db *StateDB, address common.Address, data Account) *stateObject {
+ if data.Balance == nil {
+ data.Balance = new(big.Int)
+ }
+ if data.CodeHash == nil {
+ data.CodeHash = emptyCodeHash
+ }
+ return &stateObject{
+ db: db,
+ address: address,
+ addrHash: crypto.Keccak256Hash(address[:]),
+ data: data,
+ originStorage: make(Storage),
+ dirtyStorage: make(Storage),
+ }
+}
+
+// EncodeRLP implements rlp.Encoder.
+func (s *stateObject) EncodeRLP(w io.Writer) error {
+ return rlp.Encode(w, s.data)
+}
+
+// setError remembers the first non-nil error it is called with.
+func (s *stateObject) setError(err error) {
+ if s.dbErr == nil {
+ s.dbErr = err
+ }
+}
+
+func (s *stateObject) markSuicided() {
+ s.suicided = true
+}
+
+func (s *stateObject) touch() {
+ s.db.journal.append(touchChange{
+ account: &s.address,
+ })
+ if s.address == ripemd {
+ // Explicitly put it in the dirty-cache, which is otherwise generated from
+ // flattened journals.
+ s.db.journal.dirty(s.address)
+ }
+}
+
+func (s *stateObject) getTrie(db Database) Trie {
+ if s.trie == nil {
+ var err error
+ s.trie, err = db.OpenStorageTrie(s.addrHash, s.data.Root)
+ if err != nil {
+ s.trie, _ = db.OpenStorageTrie(s.addrHash, common.Hash{})
+ s.setError(fmt.Errorf("can't create storage trie: %v", err))
+ }
+ }
+ return s.trie
+}
+
+// GetState retrieves a value from the account storage trie.
+func (s *stateObject) GetState(db Database, key common.Hash) common.Hash {
+ // If the fake storage is set, only lookup the state here(in the debugging mode)
+ if s.fakeStorage != nil {
+ return s.fakeStorage[key]
+ }
+ // If we have a dirty value for this state entry, return it
+ value, dirty := s.dirtyStorage[key]
+ if dirty {
+ return value
+ }
+ // Otherwise return the entry's original value
+ return s.GetCommittedState(db, key)
+}
+
+// GetCommittedState retrieves a value from the committed account storage trie.
+func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash {
+ // If the fake storage is set, only lookup the state here(in the debugging mode)
+ if s.fakeStorage != nil {
+ return s.fakeStorage[key]
+ }
+ // If we have the original value cached, return that
+ value, cached := s.originStorage[key]
+ if cached {
+ return value
+ }
+ // Track the amount of time wasted on reading the storage trie
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.db.StorageReads += time.Since(start) }(time.Now())
+ }
+ // Otherwise load the value from the database
+ enc, err := s.getTrie(db).TryGet(key[:])
+ if err != nil {
+ s.setError(err)
+ return common.Hash{}
+ }
+ if len(enc) > 0 {
+ _, content, _, err := rlp.Split(enc)
+ if err != nil {
+ s.setError(err)
+ }
+ value.SetBytes(content)
+ }
+ s.originStorage[key] = value
+ return value
+}
+
+// SetState updates a value in account storage.
+func (s *stateObject) SetState(db Database, key, value common.Hash) {
+ // If the fake storage is set, put the temporary state update here.
+ if s.fakeStorage != nil {
+ s.fakeStorage[key] = value
+ return
+ }
+ // If the new value is the same as old, don't set
+ prev := s.GetState(db, key)
+ if prev == value {
+ return
+ }
+ // New value is different, update and journal the change
+ s.db.journal.append(storageChange{
+ account: &s.address,
+ key: key,
+ prevalue: prev,
+ })
+ s.setState(key, value)
+}
+
+// SetStorage replaces the entire state storage with the given one.
+//
+// After this function is called, all original state will be ignored and state
+// lookup only happens in the fake state storage.
+//
+// Note this function should only be used for debugging purpose.
+func (s *stateObject) SetStorage(storage map[common.Hash]common.Hash) {
+ // Allocate fake storage if it's nil.
+ if s.fakeStorage == nil {
+ s.fakeStorage = make(Storage)
+ }
+ for key, value := range storage {
+ s.fakeStorage[key] = value
+ }
+ // Don't bother journal since this function should only be used for
+ // debugging and the `fake` storage won't be committed to database.
+}
+
+func (s *stateObject) setState(key, value common.Hash) {
+ s.dirtyStorage[key] = value
+}
+
+// updateTrie writes cached storage modifications into the object's storage trie.
+func (s *stateObject) updateTrie(db Database) Trie {
+ // Track the amount of time wasted on updating the storge trie
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())
+ }
+ // Update all the dirty slots in the trie
+ tr := s.getTrie(db)
+ for key, value := range s.dirtyStorage {
+ delete(s.dirtyStorage, key)
+
+ // Skip noop changes, persist actual changes
+ if value == s.originStorage[key] {
+ continue
+ }
+ s.originStorage[key] = value
+
+ if (value == common.Hash{}) {
+ s.setError(tr.TryDelete(key[:]))
+ continue
+ }
+ // Encoding []byte cannot fail, ok to ignore the error.
+ v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
+ s.setError(tr.TryUpdate(key[:], v))
+ }
+ return tr
+}
+
+// UpdateRoot sets the trie root to the current root hash of
+func (s *stateObject) updateRoot(db Database) {
+ s.updateTrie(db)
+
+ // Track the amount of time wasted on hashing the storge trie
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.db.StorageHashes += time.Since(start) }(time.Now())
+ }
+ s.data.Root = s.trie.Hash()
+}
+
+// CommitTrie the storage trie of the object to db.
+// This updates the trie root.
+func (s *stateObject) CommitTrie(db Database) error {
+ s.updateTrie(db)
+ if s.dbErr != nil {
+ return s.dbErr
+ }
+ // Track the amount of time wasted on committing the storge trie
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.db.StorageCommits += time.Since(start) }(time.Now())
+ }
+ root, err := s.trie.Commit(nil)
+ if err == nil {
+ s.data.Root = root
+ }
+ return err
+}
+
+// AddBalance removes amount from c's balance.
+// It is used to add funds to the destination account of a transfer.
+func (s *stateObject) AddBalance(amount *big.Int) {
+ // EIP158: We must check emptiness for the objects such that the account
+ // clearing (0,0,0 objects) can take effect.
+ if amount.Sign() == 0 {
+ if s.empty() {
+ s.touch()
+ }
+
+ return
+ }
+ s.SetBalance(new(big.Int).Add(s.Balance(), amount))
+}
+
+// SubBalance removes amount from c's balance.
+// It is used to remove funds from the origin account of a transfer.
+func (s *stateObject) SubBalance(amount *big.Int) {
+ if amount.Sign() == 0 {
+ return
+ }
+ s.SetBalance(new(big.Int).Sub(s.Balance(), amount))
+}
+
+func (s *stateObject) SetBalance(amount *big.Int) {
+ s.db.journal.append(balanceChange{
+ account: &s.address,
+ prev: new(big.Int).Set(s.data.Balance),
+ })
+ s.setBalance(amount)
+}
+
+// AddBalance removes amount from c's balance.
+// It is used to add funds to the destination account of a transfer.
+func (s *stateObject) AddBalanceMultiCoin(coinID common.Hash, amount *big.Int, db Database) {
+ if amount.Sign() == 0 {
+ if s.empty() {
+ s.touch()
+ }
+
+ return
+ }
+ s.SetBalanceMultiCoin(coinID, new(big.Int).Add(s.BalanceMultiCoin(coinID, db), amount), db)
+}
+
+// SubBalance removes amount from c's balance.
+// It is used to remove funds from the origin account of a transfer.
+func (s *stateObject) SubBalanceMultiCoin(coinID common.Hash, amount *big.Int, db Database) {
+ if amount.Sign() == 0 {
+ return
+ }
+ s.SetBalanceMultiCoin(coinID, new(big.Int).Sub(s.BalanceMultiCoin(coinID, db), amount), db)
+}
+
+func (s *stateObject) SetBalanceMultiCoin(coinID common.Hash, amount *big.Int, db Database) {
+ NormalizeCoinID(&coinID)
+ s.SetState(db, coinID, common.BigToHash(amount))
+}
+
+func (s *stateObject) setBalance(amount *big.Int) {
+ s.data.Balance = amount
+}
+
+func (s *stateObject) enableMultiCoin() {
+ s.data.IsMultiCoin = true
+}
+
+// Return the gas back to the origin. Used by the Virtual machine or Closures
+func (s *stateObject) ReturnGas(gas *big.Int) {}
+
+func (s *stateObject) deepCopy(db *StateDB) *stateObject {
+ stateObject := newObject(db, s.address, s.data)
+ if s.trie != nil {
+ stateObject.trie = db.db.CopyTrie(s.trie)
+ }
+ stateObject.code = s.code
+ stateObject.dirtyStorage = s.dirtyStorage.Copy()
+ stateObject.originStorage = s.originStorage.Copy()
+ stateObject.suicided = s.suicided
+ stateObject.dirtyCode = s.dirtyCode
+ stateObject.deleted = s.deleted
+ return stateObject
+}
+
+//
+// Attribute accessors
+//
+
+// Returns the address of the contract/account
+func (s *stateObject) Address() common.Address {
+ return s.address
+}
+
+// Code returns the contract code associated with this object, if any.
+func (s *stateObject) Code(db Database) []byte {
+ if s.code != nil {
+ return s.code
+ }
+ if bytes.Equal(s.CodeHash(), emptyCodeHash) {
+ return nil
+ }
+ code, err := db.ContractCode(s.addrHash, common.BytesToHash(s.CodeHash()))
+ if err != nil {
+ s.setError(fmt.Errorf("can't load code hash %x: %v", s.CodeHash(), err))
+ }
+ s.code = code
+ return code
+}
+
+func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
+ prevcode := s.Code(s.db.db)
+ s.db.journal.append(codeChange{
+ account: &s.address,
+ prevhash: s.CodeHash(),
+ prevcode: prevcode,
+ })
+ s.setCode(codeHash, code)
+}
+
+func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
+ s.code = code
+ s.data.CodeHash = codeHash[:]
+ s.dirtyCode = true
+}
+
+func (s *stateObject) SetNonce(nonce uint64) {
+ s.db.journal.append(nonceChange{
+ account: &s.address,
+ prev: s.data.Nonce,
+ })
+ s.setNonce(nonce)
+}
+
+func (s *stateObject) setNonce(nonce uint64) {
+ s.data.Nonce = nonce
+}
+
+func (s *stateObject) CodeHash() []byte {
+ return s.data.CodeHash
+}
+
+func (s *stateObject) Balance() *big.Int {
+ return s.data.Balance
+}
+
+func IsMultiCoinKey(key common.Hash) bool {
+ return key[0]&0x01 == 0x01
+}
+
+func NormalizeCoinID(coinID *common.Hash) {
+ coinID[0] |= 0x01
+}
+
+func NormalizeStateKey(key *common.Hash) {
+ key[0] &= 0xfe
+}
+
+func (s *stateObject) BalanceMultiCoin(coinID common.Hash, db Database) *big.Int {
+ NormalizeCoinID(&coinID)
+ return s.GetState(db, coinID).Big()
+}
+
+func (s *stateObject) EnableMultiCoin() bool {
+ if s.data.IsMultiCoin {
+ return false
+ }
+ s.db.journal.append(multiCoinEnable{
+ account: &s.address,
+ })
+ s.enableMultiCoin()
+ return true
+}
+
+func (s *stateObject) IsMultiCoin() bool {
+ return s.data.IsMultiCoin
+}
+
+func (s *stateObject) Nonce() uint64 {
+ return s.data.Nonce
+}
+
+// Never called, but must be present to allow stateObject to be used
+// as a vm.Account interface that also satisfies the vm.ContractRef
+// interface. Interfaces are awesome.
+func (s *stateObject) Value() *big.Int {
+ panic("Value on stateObject should never be called")
+}
diff --git a/core/state/statedb.go b/core/state/statedb.go
new file mode 100644
index 0000000..12deebe
--- /dev/null
+++ b/core/state/statedb.go
@@ -0,0 +1,793 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package state provides a caching layer atop the Ethereum state trie.
+package state
+
+import (
+ "errors"
+ "fmt"
+ "math/big"
+ "sort"
+ "time"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/ava-labs/go-ethereum/metrics"
+ "github.com/ava-labs/go-ethereum/rlp"
+ "github.com/ava-labs/go-ethereum/trie"
+)
+
+type revision struct {
+ id int
+ journalIndex int
+}
+
+var (
+ // emptyRoot is the known root hash of an empty trie.
+ emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
+
+ // emptyCode is the known hash of the empty EVM bytecode.
+ emptyCode = crypto.Keccak256Hash(nil)
+)
+
+type proofList [][]byte
+
+func (n *proofList) Put(key []byte, value []byte) error {
+ *n = append(*n, value)
+ return nil
+}
+
+func (n *proofList) Delete(key []byte) error {
+ panic("not supported")
+}
+
+// StateDBs within the ethereum protocol are used to store anything
+// within the merkle trie. StateDBs take care of caching and storing
+// nested states. It's the general query interface to retrieve:
+// * Contracts
+// * Accounts
+type StateDB struct {
+ db Database
+ trie Trie
+
+ // This map holds 'live' objects, which will get modified while processing a state transition.
+ stateObjects map[common.Address]*stateObject
+ stateObjectsDirty map[common.Address]struct{}
+
+ // DB error.
+ // State objects are used by the consensus core and VM which are
+ // unable to deal with database-level errors. Any error that occurs
+ // during a database read is memoized here and will eventually be returned
+ // by StateDB.Commit.
+ dbErr error
+
+ // The refund counter, also used by state transitioning.
+ refund uint64
+
+ thash, bhash common.Hash
+ txIndex int
+ logs map[common.Hash][]*types.Log
+ logSize uint
+
+ preimages map[common.Hash][]byte
+
+ // Journal of state modifications. This is the backbone of
+ // Snapshot and RevertToSnapshot.
+ journal *journal
+ validRevisions []revision
+ nextRevisionId int
+
+ // Measurements gathered during execution for debugging purposes
+ AccountReads time.Duration
+ AccountHashes time.Duration
+ AccountUpdates time.Duration
+ AccountCommits time.Duration
+ StorageReads time.Duration
+ StorageHashes time.Duration
+ StorageUpdates time.Duration
+ StorageCommits time.Duration
+}
+
+// Create a new state from a given trie.
+func New(root common.Hash, db Database) (*StateDB, error) {
+ tr, err := db.OpenTrie(root)
+ if err != nil {
+ return nil, err
+ }
+ return &StateDB{
+ db: db,
+ trie: tr,
+ stateObjects: make(map[common.Address]*stateObject),
+ stateObjectsDirty: make(map[common.Address]struct{}),
+ logs: make(map[common.Hash][]*types.Log),
+ preimages: make(map[common.Hash][]byte),
+ journal: newJournal(),
+ }, nil
+}
+
+// setError remembers the first non-nil error it is called with.
+func (self *StateDB) setError(err error) {
+ if self.dbErr == nil {
+ self.dbErr = err
+ }
+}
+
+func (self *StateDB) Error() error {
+ return self.dbErr
+}
+
+// Reset clears out all ephemeral state objects from the state db, but keeps
+// the underlying state trie to avoid reloading data for the next operations.
+func (self *StateDB) Reset(root common.Hash) error {
+ tr, err := self.db.OpenTrie(root)
+ if err != nil {
+ return err
+ }
+ self.trie = tr
+ self.stateObjects = make(map[common.Address]*stateObject)
+ self.stateObjectsDirty = make(map[common.Address]struct{})
+ self.thash = common.Hash{}
+ self.bhash = common.Hash{}
+ self.txIndex = 0
+ self.logs = make(map[common.Hash][]*types.Log)
+ self.logSize = 0
+ self.preimages = make(map[common.Hash][]byte)
+ self.clearJournalAndRefund()
+ return nil
+}
+
+func (self *StateDB) AddLog(log *types.Log) {
+ self.journal.append(addLogChange{txhash: self.thash})
+
+ log.TxHash = self.thash
+ log.BlockHash = self.bhash
+ log.TxIndex = uint(self.txIndex)
+ log.Index = self.logSize
+ self.logs[self.thash] = append(self.logs[self.thash], log)
+ self.logSize++
+}
+
+func (self *StateDB) GetLogs(hash common.Hash) []*types.Log {
+ return self.logs[hash]
+}
+
+func (self *StateDB) Logs() []*types.Log {
+ var logs []*types.Log
+ for _, lgs := range self.logs {
+ logs = append(logs, lgs...)
+ }
+ return logs
+}
+
+// AddPreimage records a SHA3 preimage seen by the VM.
+func (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
+ if _, ok := self.preimages[hash]; !ok {
+ self.journal.append(addPreimageChange{hash: hash})
+ pi := make([]byte, len(preimage))
+ copy(pi, preimage)
+ self.preimages[hash] = pi
+ }
+}
+
+// Preimages returns a list of SHA3 preimages that have been submitted.
+func (self *StateDB) Preimages() map[common.Hash][]byte {
+ return self.preimages
+}
+
+// AddRefund adds gas to the refund counter
+func (self *StateDB) AddRefund(gas uint64) {
+ self.journal.append(refundChange{prev: self.refund})
+ self.refund += gas
+}
+
+// SubRefund removes gas from the refund counter.
+// This method will panic if the refund counter goes below zero
+func (self *StateDB) SubRefund(gas uint64) {
+ self.journal.append(refundChange{prev: self.refund})
+ if gas > self.refund {
+ panic("Refund counter below zero")
+ }
+ self.refund -= gas
+}
+
+// Exist reports whether the given account address exists in the state.
+// Notably this also returns true for suicided accounts.
+func (self *StateDB) Exist(addr common.Address) bool {
+ return self.getStateObject(addr) != nil
+}
+
+// Empty returns whether the state object is either non-existent
+// or empty according to the EIP161 specification (balance = nonce = code = 0)
+func (self *StateDB) Empty(addr common.Address) bool {
+ so := self.getStateObject(addr)
+ return so == nil || so.empty()
+}
+
+// Retrieve the balance from the given address or 0 if object not found
+func (self *StateDB) GetBalance(addr common.Address) *big.Int {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.Balance()
+ }
+ return common.Big0
+}
+
+// Retrieve the balance from the given address or 0 if object not found
+func (self *StateDB) GetBalanceMultiCoin(addr common.Address, coinID common.Hash) *big.Int {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.BalanceMultiCoin(coinID, self.db)
+ }
+ return common.Big0
+}
+
+func (self *StateDB) EnableMultiCoin(addr common.Address) error {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject.data.Root != emptyRoot {
+ return errors.New("not a fresh account")
+ }
+ if !stateObject.EnableMultiCoin() {
+ return errors.New("multi-coin mode already enabled")
+ }
+ return nil
+}
+
+func (self *StateDB) IsMultiCoin(addr common.Address) bool {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.IsMultiCoin()
+ }
+ return false
+}
+
+func (self *StateDB) GetNonce(addr common.Address) uint64 {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.Nonce()
+ }
+
+ return 0
+}
+
+// TxIndex returns the current transaction index set by Prepare.
+func (self *StateDB) TxIndex() int {
+ return self.txIndex
+}
+
+// BlockHash returns the current block hash set by Prepare.
+func (self *StateDB) BlockHash() common.Hash {
+ return self.bhash
+}
+
+func (self *StateDB) GetCode(addr common.Address) []byte {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.Code(self.db)
+ }
+ return nil
+}
+
+func (self *StateDB) GetCodeSize(addr common.Address) int {
+ stateObject := self.getStateObject(addr)
+ if stateObject == nil {
+ return 0
+ }
+ if stateObject.code != nil {
+ return len(stateObject.code)
+ }
+ size, err := self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
+ if err != nil {
+ self.setError(err)
+ }
+ return size
+}
+
+func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
+ stateObject := self.getStateObject(addr)
+ if stateObject == nil {
+ return common.Hash{}
+ }
+ return common.BytesToHash(stateObject.CodeHash())
+}
+
+// GetState retrieves a value from the given account's storage trie.
+func (self *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.GetState(self.db, hash)
+ }
+ return common.Hash{}
+}
+
+// GetProof returns the MerkleProof for a given Account
+func (self *StateDB) GetProof(a common.Address) ([][]byte, error) {
+ var proof proofList
+ err := self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof)
+ return [][]byte(proof), err
+}
+
+// GetProof returns the StorageProof for given key
+func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
+ var proof proofList
+ trie := self.StorageTrie(a)
+ if trie == nil {
+ return proof, errors.New("storage trie for requested address does not exist")
+ }
+ err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
+ return [][]byte(proof), err
+}
+
+// GetCommittedState retrieves a value from the given account's committed storage trie.
+func (self *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.GetCommittedState(self.db, hash)
+ }
+ return common.Hash{}
+}
+
+// Database retrieves the low level database supporting the lower level trie ops.
+func (self *StateDB) Database() Database {
+ return self.db
+}
+
+// StorageTrie returns the storage trie of an account.
+// The return value is a copy and is nil for non-existent accounts.
+func (self *StateDB) StorageTrie(addr common.Address) Trie {
+ stateObject := self.getStateObject(addr)
+ if stateObject == nil {
+ return nil
+ }
+ cpy := stateObject.deepCopy(self)
+ return cpy.updateTrie(self.db)
+}
+
+func (self *StateDB) HasSuicided(addr common.Address) bool {
+ stateObject := self.getStateObject(addr)
+ if stateObject != nil {
+ return stateObject.suicided
+ }
+ return false
+}
+
+/*
+ * SETTERS
+ */
+
+// AddBalance adds amount to the account associated with addr.
+func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.AddBalance(amount)
+ }
+}
+
+// SubBalance subtracts amount from the account associated with addr.
+func (self *StateDB) SubBalance(addr common.Address, amount *big.Int) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.SubBalance(amount)
+ }
+}
+
+func (self *StateDB) SetBalance(addr common.Address, amount *big.Int) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.SetBalance(amount)
+ }
+}
+
+// AddBalance adds amount to the account associated with addr.
+func (self *StateDB) AddBalanceMultiCoin(addr common.Address, coinID common.Hash, amount *big.Int) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.AddBalanceMultiCoin(coinID, amount, self.db)
+ }
+}
+
+// SubBalance subtracts amount from the account associated with addr.
+func (self *StateDB) SubBalanceMultiCoin(addr common.Address, coinID common.Hash, amount *big.Int) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.SubBalanceMultiCoin(coinID, amount, self.db)
+ }
+}
+
+func (self *StateDB) SetBalanceMultiCoin(addr common.Address, coinID common.Hash, amount *big.Int) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.SetBalanceMultiCoin(coinID, amount, self.db)
+ }
+}
+
+func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.SetNonce(nonce)
+ }
+}
+
+func (self *StateDB) SetCode(addr common.Address, code []byte) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.SetCode(crypto.Keccak256Hash(code), code)
+ }
+}
+
+func (self *StateDB) SetState(addr common.Address, key, value common.Hash) (res error) {
+ res = nil
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ if stateObject.data.IsMultiCoin {
+ NormalizeStateKey(&key)
+ }
+ stateObject.SetState(self.db, key, value)
+ }
+ return
+}
+
+// SetStorage replaces the entire storage for the specified account with given
+// storage. This function should only be used for debugging.
+func (self *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
+ stateObject := self.GetOrNewStateObject(addr)
+ if stateObject != nil {
+ stateObject.SetStorage(storage)
+ }
+}
+
+// Suicide marks the given account as suicided.
+// This clears the account balance.
+//
+// The account's state object is still available until the state is committed,
+// getStateObject will return a non-nil account after Suicide.
+func (self *StateDB) Suicide(addr common.Address) bool {
+ stateObject := self.getStateObject(addr)
+ if stateObject == nil {
+ return false
+ }
+ self.journal.append(suicideChange{
+ account: &addr,
+ prev: stateObject.suicided,
+ prevbalance: new(big.Int).Set(stateObject.Balance()),
+ })
+ stateObject.markSuicided()
+ stateObject.data.Balance = new(big.Int)
+
+ return true
+}
+
+//
+// Setting, updating & deleting state object methods.
+//
+
+// updateStateObject writes the given object to the trie.
+func (s *StateDB) updateStateObject(stateObject *stateObject) {
+ // Track the amount of time wasted on updating the account from the trie
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
+ }
+ // Encode the account and update the account trie
+ addr := stateObject.Address()
+
+ data, err := rlp.EncodeToBytes(stateObject)
+ if err != nil {
+ panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
+ }
+ s.setError(s.trie.TryUpdate(addr[:], data))
+}
+
+// deleteStateObject removes the given object from the state trie.
+func (s *StateDB) deleteStateObject(stateObject *stateObject) {
+ // Track the amount of time wasted on deleting the account from the trie
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
+ }
+ // Delete the account from the trie
+ stateObject.deleted = true
+
+ addr := stateObject.Address()
+ s.setError(s.trie.TryDelete(addr[:]))
+}
+
+// Retrieve a state object given by the address. Returns nil if not found.
+func (s *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) {
+ // Prefer live objects
+ if obj := s.stateObjects[addr]; obj != nil {
+ if obj.deleted {
+ return nil
+ }
+ return obj
+ }
+ // Track the amount of time wasted on loading the object from the database
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
+ }
+ // Load the object from the database
+ enc, err := s.trie.TryGet(addr[:])
+ if len(enc) == 0 {
+ s.setError(err)
+ return nil
+ }
+ var data Account
+ if err := rlp.DecodeBytes(enc, &data); err != nil {
+ log.Error("Failed to decode state object", "addr", addr, "err", err)
+ return nil
+ }
+ // Insert into the live set
+ obj := newObject(s, addr, data)
+ s.setStateObject(obj)
+ return obj
+}
+
+func (self *StateDB) setStateObject(object *stateObject) {
+ self.stateObjects[object.Address()] = object
+}
+
+// Retrieve a state object or create a new state object if nil.
+func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
+ stateObject := self.getStateObject(addr)
+ if stateObject == nil || stateObject.deleted {
+ stateObject, _ = self.createObject(addr)
+ }
+ return stateObject
+}
+
+// createObject creates a new state object. If there is an existing account with
+// the given address, it is overwritten and returned as the second return value.
+func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
+ prev = self.getStateObject(addr)
+ newobj = newObject(self, addr, Account{})
+ newobj.setNonce(0) // sets the object to dirty
+ if prev == nil {
+ self.journal.append(createObjectChange{account: &addr})
+ } else {
+ self.journal.append(resetObjectChange{prev: prev})
+ }
+ self.setStateObject(newobj)
+ return newobj, prev
+}
+
+// CreateAccount explicitly creates a state object. If a state object with the address
+// already exists the balance is carried over to the new account.
+//
+// CreateAccount is called during the EVM CREATE operation. The situation might arise that
+// a contract does the following:
+//
+// 1. sends funds to sha(account ++ (nonce + 1))
+// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
+//
+// Carrying over the balance ensures that Ether doesn't disappear.
+func (self *StateDB) CreateAccount(addr common.Address) {
+ newObj, prev := self.createObject(addr)
+ if prev != nil {
+ newObj.setBalance(prev.data.Balance)
+ }
+}
+
+func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
+ so := db.getStateObject(addr)
+ if so == nil {
+ return nil
+ }
+ it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
+
+ for it.Next() {
+ key := common.BytesToHash(db.trie.GetKey(it.Key))
+ if value, dirty := so.dirtyStorage[key]; dirty {
+ if !cb(key, value) {
+ return nil
+ }
+ continue
+ }
+
+ if len(it.Value) > 0 {
+ _, content, _, err := rlp.Split(it.Value)
+ if err != nil {
+ return err
+ }
+ if !cb(key, common.BytesToHash(content)) {
+ return nil
+ }
+ }
+ }
+ return nil
+}
+
+// Copy creates a deep, independent copy of the state.
+// Snapshots of the copied state cannot be applied to the copy.
+func (self *StateDB) Copy() *StateDB {
+ // Copy all the basic fields, initialize the memory ones
+ state := &StateDB{
+ db: self.db,
+ trie: self.db.CopyTrie(self.trie),
+ stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)),
+ stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)),
+ refund: self.refund,
+ logs: make(map[common.Hash][]*types.Log, len(self.logs)),
+ logSize: self.logSize,
+ preimages: make(map[common.Hash][]byte, len(self.preimages)),
+ journal: newJournal(),
+ }
+ // Copy the dirty states, logs, and preimages
+ for addr := range self.journal.dirties {
+ // As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
+ // and in the Finalise-method, there is a case where an object is in the journal but not
+ // in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
+ // nil
+ if object, exist := self.stateObjects[addr]; exist {
+ state.stateObjects[addr] = object.deepCopy(state)
+ state.stateObjectsDirty[addr] = struct{}{}
+ }
+ }
+ // Above, we don't copy the actual journal. This means that if the copy is copied, the
+ // loop above will be a no-op, since the copy's journal is empty.
+ // Thus, here we iterate over stateObjects, to enable copies of copies
+ for addr := range self.stateObjectsDirty {
+ if _, exist := state.stateObjects[addr]; !exist {
+ state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state)
+ state.stateObjectsDirty[addr] = struct{}{}
+ }
+ }
+ for hash, logs := range self.logs {
+ cpy := make([]*types.Log, len(logs))
+ for i, l := range logs {
+ cpy[i] = new(types.Log)
+ *cpy[i] = *l
+ }
+ state.logs[hash] = cpy
+ }
+ for hash, preimage := range self.preimages {
+ state.preimages[hash] = preimage
+ }
+ return state
+}
+
+// Snapshot returns an identifier for the current revision of the state.
+func (self *StateDB) Snapshot() int {
+ id := self.nextRevisionId
+ self.nextRevisionId++
+ self.validRevisions = append(self.validRevisions, revision{id, self.journal.length()})
+ return id
+}
+
+// RevertToSnapshot reverts all state changes made since the given revision.
+func (self *StateDB) RevertToSnapshot(revid int) {
+ // Find the snapshot in the stack of valid snapshots.
+ idx := sort.Search(len(self.validRevisions), func(i int) bool {
+ return self.validRevisions[i].id >= revid
+ })
+ if idx == len(self.validRevisions) || self.validRevisions[idx].id != revid {
+ panic(fmt.Errorf("revision id %v cannot be reverted", revid))
+ }
+ snapshot := self.validRevisions[idx].journalIndex
+
+ // Replay the journal to undo changes and remove invalidated snapshots
+ self.journal.revert(self, snapshot)
+ self.validRevisions = self.validRevisions[:idx]
+}
+
+// GetRefund returns the current value of the refund counter.
+func (self *StateDB) GetRefund() uint64 {
+ return self.refund
+}
+
+// Finalise finalises the state by removing the self destructed objects
+// and clears the journal as well as the refunds.
+func (s *StateDB) Finalise(deleteEmptyObjects bool) {
+ for addr := range s.journal.dirties {
+ stateObject, exist := s.stateObjects[addr]
+ if !exist {
+ // ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
+ // That tx goes out of gas, and although the notion of 'touched' does not exist there, the
+ // touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
+ // it will persist in the journal even though the journal is reverted. In this special circumstance,
+ // it may exist in `s.journal.dirties` but not in `s.stateObjects`.
+ // Thus, we can safely ignore it here
+ continue
+ }
+
+ if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) {
+ s.deleteStateObject(stateObject)
+ } else {
+ stateObject.updateRoot(s.db)
+ s.updateStateObject(stateObject)
+ }
+ s.stateObjectsDirty[addr] = struct{}{}
+ }
+ // Invalidate journal because reverting across transactions is not allowed.
+ s.clearJournalAndRefund()
+}
+
+// IntermediateRoot computes the current root hash of the state trie.
+// It is called in between transactions to get the root hash that
+// goes into transaction receipts.
+func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
+ s.Finalise(deleteEmptyObjects)
+
+ // Track the amount of time wasted on hashing the account trie
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
+ }
+ return s.trie.Hash()
+}
+
+// Prepare sets the current transaction hash and index and block hash which is
+// used when the EVM emits new state logs.
+func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) {
+ self.thash = thash
+ self.bhash = bhash
+ self.txIndex = ti
+}
+
+func (s *StateDB) clearJournalAndRefund() {
+ s.journal = newJournal()
+ s.validRevisions = s.validRevisions[:0]
+ s.refund = 0
+}
+
+// Commit writes the state to the underlying in-memory trie database.
+func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) {
+ defer s.clearJournalAndRefund()
+
+ for addr := range s.journal.dirties {
+ s.stateObjectsDirty[addr] = struct{}{}
+ }
+ // Commit objects to the trie, measuring the elapsed time
+ for addr, stateObject := range s.stateObjects {
+ _, isDirty := s.stateObjectsDirty[addr]
+ switch {
+ case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()):
+ // If the object has been removed, don't bother syncing it
+ // and just mark it for deletion in the trie.
+ s.deleteStateObject(stateObject)
+ case isDirty:
+ // Write any contract code associated with the state object
+ if stateObject.code != nil && stateObject.dirtyCode {
+ s.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code)
+ stateObject.dirtyCode = false
+ }
+ // Write any storage changes in the state object to its storage trie.
+ if err := stateObject.CommitTrie(s.db); err != nil {
+ return common.Hash{}, err
+ }
+ // Update the object in the main account trie.
+ s.updateStateObject(stateObject)
+ }
+ delete(s.stateObjectsDirty, addr)
+ }
+ // Write the account trie changes, measuing the amount of wasted time
+ if metrics.EnabledExpensive {
+ defer func(start time.Time) { s.AccountCommits += time.Since(start) }(time.Now())
+ }
+ root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
+ var account Account
+ if err := rlp.DecodeBytes(leaf, &account); err != nil {
+ return nil
+ }
+ if account.Root != emptyRoot {
+ s.db.TrieDB().Reference(account.Root, parent)
+ }
+ code := common.BytesToHash(account.CodeHash)
+ if code != emptyCode {
+ s.db.TrieDB().Reference(code, parent)
+ }
+ return nil
+ })
+ return root, err
+}
diff --git a/core/state/sync.go b/core/state/sync.go
new file mode 100644
index 0000000..873395c
--- /dev/null
+++ b/core/state/sync.go
@@ -0,0 +1,42 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package state
+
+import (
+ "bytes"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/ethdb"
+ "github.com/ava-labs/go-ethereum/rlp"
+ "github.com/ava-labs/go-ethereum/trie"
+)
+
+// NewStateSync create a new state trie download scheduler.
+func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.SyncBloom) *trie.Sync {
+ var syncer *trie.Sync
+ callback := func(leaf []byte, parent common.Hash) error {
+ var obj Account
+ if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
+ return err
+ }
+ syncer.AddSubTrie(obj.Root, 64, parent, nil)
+ syncer.AddRawEntry(common.BytesToHash(obj.CodeHash), 64, parent)
+ return nil
+ }
+ syncer = trie.NewSync(root, database, callback, bloom)
+ return syncer
+}
diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go
index f408098..4997333 100644
--- a/core/state_prefetcher.go
+++ b/core/state_prefetcher.go
@@ -19,12 +19,12 @@ package core
import (
"sync/atomic"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
- "github.com/ava-labs/go-ethereum/params"
)
// statePrefetcher is a basic Prefetcher, which blindly executes a block on top
diff --git a/core/state_processor.go b/core/state_processor.go
index 680d512..d80997e 100644
--- a/core/state_processor.go
+++ b/core/state_processor.go
@@ -17,14 +17,15 @@
package core
import (
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/consensus/misc"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/consensus/misc"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
"github.com/ava-labs/go-ethereum/crypto"
- "github.com/ava-labs/go-ethereum/params"
+ "github.com/ava-labs/go-ethereum/log"
)
// StateProcessor is a basic Processor, which takes care of transitioning
@@ -54,6 +55,7 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
// returns the amount of gas that was used in the process. If any of the
// transactions failed to execute due to insufficient gas it will return an error.
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
+ log.Debug("here4")
var (
receipts types.Receipts
usedGas = new(uint64)
diff --git a/core/state_transition.go b/core/state_transition.go
index f648bce..139289f 100644
--- a/core/state_transition.go
+++ b/core/state_transition.go
@@ -21,10 +21,10 @@ import (
"math"
"math/big"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/vm"
"github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/params"
)
var (
@@ -55,6 +55,8 @@ type StateTransition struct {
gasPrice *big.Int
initialGas uint64
value *big.Int
+ coinID *common.Hash
+ value2 *big.Int
data []byte
state vm.StateDB
evm *vm.EVM
@@ -69,6 +71,8 @@ type Message interface {
GasPrice() *big.Int
Gas() uint64
Value() *big.Int
+ CoinID() *common.Hash
+ Value2() *big.Int
Nonce() uint64
CheckNonce() bool
@@ -120,6 +124,8 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
msg: msg,
gasPrice: msg.GasPrice(),
value: msg.Value(),
+ coinID: msg.CoinID(),
+ value2: msg.Value2(),
data: msg.Data(),
state: evm.StateDB,
}
@@ -213,9 +219,10 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
if contractCreation {
ret, _, st.gas, vmerr = evm.Create(sender, st.data, st.gas, st.value)
} else {
+ log.Debug("here2")
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1)
- ret, st.gas, vmerr = evm.Call(sender, st.to(), st.data, st.gas, st.value)
+ ret, st.gas, vmerr = evm.CallExpert(sender, st.to(), st.data, st.gas, st.value, st.coinID, st.value2)
}
if vmerr != nil {
log.Debug("VM returned with error", "err", vmerr)
diff --git a/core/tx_cacher.go b/core/tx_cacher.go
index ffac7f1..554160f 100644
--- a/core/tx_cacher.go
+++ b/core/tx_cacher.go
@@ -19,7 +19,7 @@ package core
import (
"runtime"
- "github.com/ava-labs/go-ethereum/core/types"
+ "github.com/ava-labs/coreth/core/types"
)
// senderCacher is a concurrent transaction sender recoverer and cacher.
diff --git a/core/tx_journal.go b/core/tx_journal.go
index dfd8dab..a35e78f 100644
--- a/core/tx_journal.go
+++ b/core/tx_journal.go
@@ -21,8 +21,8 @@ import (
"io"
"os"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/rlp"
)
diff --git a/core/tx_list.go b/core/tx_list.go
index a54ca77..241b14a 100644
--- a/core/tx_list.go
+++ b/core/tx_list.go
@@ -22,8 +22,8 @@ import (
"math/big"
"sort"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/log"
)
diff --git a/core/tx_noncer.go b/core/tx_noncer.go
index d52e267..56cdf03 100644
--- a/core/tx_noncer.go
+++ b/core/tx_noncer.go
@@ -19,8 +19,8 @@ package core
import (
"sync"
+ "github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/state"
)
// txNoncer is a tiny virtual state database to manage the executable nonces of
diff --git a/core/tx_pool.go b/core/tx_pool.go
index c6178bb..5b2a3c0 100644
--- a/core/tx_pool.go
+++ b/core/tx_pool.go
@@ -25,14 +25,14 @@ import (
"sync"
"time"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/prque"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/metrics"
- "github.com/ava-labs/go-ethereum/params"
)
const (
diff --git a/core/types.go b/core/types.go
index fc67e71..44237cd 100644
--- a/core/types.go
+++ b/core/types.go
@@ -17,9 +17,9 @@
package core
import (
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
)
// Validator is an interface which defines the standard for block validation. It
diff --git a/core/types/block.go b/core/types/block.go
new file mode 100644
index 0000000..2fdc904
--- /dev/null
+++ b/core/types/block.go
@@ -0,0 +1,419 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package types contains data types related to Ethereum consensus.
+package types
+
+import (
+ "encoding/binary"
+ "fmt"
+ "io"
+ "math/big"
+ "reflect"
+ "sort"
+ "sync/atomic"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/rlp"
+ "golang.org/x/crypto/sha3"
+)
+
+var (
+ EmptyRootHash = DeriveSha(Transactions{})
+ EmptyUncleHash = rlpHash([]*Header(nil))
+)
+
+// A BlockNonce is a 64-bit hash which proves (combined with the
+// mix-hash) that a sufficient amount of computation has been carried
+// out on a block.
+type BlockNonce [8]byte
+
+// EncodeNonce converts the given integer to a block nonce.
+func EncodeNonce(i uint64) BlockNonce {
+ var n BlockNonce
+ binary.BigEndian.PutUint64(n[:], i)
+ return n
+}
+
+// Uint64 returns the integer value of a block nonce.
+func (n BlockNonce) Uint64() uint64 {
+ return binary.BigEndian.Uint64(n[:])
+}
+
+// MarshalText encodes n as a hex string with 0x prefix.
+func (n BlockNonce) MarshalText() ([]byte, error) {
+ return hexutil.Bytes(n[:]).MarshalText()
+}
+
+// UnmarshalText implements encoding.TextUnmarshaler.
+func (n *BlockNonce) UnmarshalText(input []byte) error {
+ return hexutil.UnmarshalFixedText("BlockNonce", input, n[:])
+}
+
+//go:generate gencodec -type Header -field-override headerMarshaling -out gen_header_json.go
+
+// Header represents a block header in the Ethereum blockchain.
+type Header struct {
+ ParentHash common.Hash `json:"parentHash" gencodec:"required"`
+ UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
+ Coinbase common.Address `json:"miner" gencodec:"required"`
+ Root common.Hash `json:"stateRoot" gencodec:"required"`
+ TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
+ ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
+ Bloom Bloom `json:"logsBloom" gencodec:"required"`
+ Difficulty *big.Int `json:"difficulty" gencodec:"required"`
+ Number *big.Int `json:"number" gencodec:"required"`
+ GasLimit uint64 `json:"gasLimit" gencodec:"required"`
+ GasUsed uint64 `json:"gasUsed" gencodec:"required"`
+ Time uint64 `json:"timestamp" gencodec:"required"`
+ Extra []byte `json:"extraData" gencodec:"required"`
+ MixDigest common.Hash `json:"mixHash"`
+ Nonce BlockNonce `json:"nonce"`
+}
+
+// field type overrides for gencodec
+type headerMarshaling struct {
+ Difficulty *hexutil.Big
+ Number *hexutil.Big
+ GasLimit hexutil.Uint64
+ GasUsed hexutil.Uint64
+ Time hexutil.Uint64
+ Extra hexutil.Bytes
+ Hash common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON
+}
+
+// Hash returns the block hash of the header, which is simply the keccak256 hash of its
+// RLP encoding.
+func (h *Header) Hash() common.Hash {
+ return rlpHash(h)
+}
+
+var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size())
+
+// Size returns the approximate memory used by all internal contents. It is used
+// to approximate and limit the memory consumption of various caches.
+func (h *Header) Size() common.StorageSize {
+ return headerSize + common.StorageSize(len(h.Extra)+(h.Difficulty.BitLen()+h.Number.BitLen())/8)
+}
+
+// SanityCheck checks a few basic things -- these checks are way beyond what
+// any 'sane' production values should hold, and can mainly be used to prevent
+// that the unbounded fields are stuffed with junk data to add processing
+// overhead
+func (h *Header) SanityCheck() error {
+ if h.Number != nil && !h.Number.IsUint64() {
+ return fmt.Errorf("too large block number: bitlen %d", h.Number.BitLen())
+ }
+ if h.Difficulty != nil {
+ if diffLen := h.Difficulty.BitLen(); diffLen > 80 {
+ return fmt.Errorf("too large block difficulty: bitlen %d", diffLen)
+ }
+ }
+ if eLen := len(h.Extra); eLen > 100*1024 {
+ return fmt.Errorf("too large block extradata: size %d", eLen)
+ }
+ return nil
+}
+
+func rlpHash(x interface{}) (h common.Hash) {
+ hw := sha3.NewLegacyKeccak256()
+ rlp.Encode(hw, x)
+ hw.Sum(h[:0])
+ return h
+}
+
+// Body is a simple (mutable, non-safe) data container for storing and moving
+// a block's data contents (transactions and uncles) together.
+type Body struct {
+ Transactions []*Transaction
+ Uncles []*Header
+}
+
+// Block represents an entire block in the Ethereum blockchain.
+type Block struct {
+ header *Header
+ uncles []*Header
+ transactions Transactions
+
+ // caches
+ hash atomic.Value
+ size atomic.Value
+
+ // Td is used by package core to store the total difficulty
+ // of the chain up to and including the block.
+ td *big.Int
+
+ // These fields are used by package eth to track
+ // inter-peer block relay.
+ ReceivedAt time.Time
+ ReceivedFrom interface{}
+}
+
+// DeprecatedTd is an old relic for extracting the TD of a block. It is in the
+// code solely to facilitate upgrading the database from the old format to the
+// new, after which it should be deleted. Do not use!
+func (b *Block) DeprecatedTd() *big.Int {
+ return b.td
+}
+
+// [deprecated by eth/63]
+// StorageBlock defines the RLP encoding of a Block stored in the
+// state database. The StorageBlock encoding contains fields that
+// would otherwise need to be recomputed.
+type StorageBlock Block
+
+// "external" block encoding. used for eth protocol, etc.
+type extblock struct {
+ Header *Header
+ Txs []*Transaction
+ Uncles []*Header
+}
+
+// [deprecated by eth/63]
+// "storage" block encoding. used for database.
+type storageblock struct {
+ Header *Header
+ Txs []*Transaction
+ Uncles []*Header
+ TD *big.Int
+}
+
+// NewBlock creates a new block. The input data is copied,
+// changes to header and to the field values will not affect the
+// block.
+//
+// The values of TxHash, UncleHash, ReceiptHash and Bloom in header
+// are ignored and set to values derived from the given txs, uncles
+// and receipts.
+func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt) *Block {
+ b := &Block{header: CopyHeader(header), td: new(big.Int)}
+
+ // TODO: panic if len(txs) != len(receipts)
+ if len(txs) == 0 {
+ b.header.TxHash = EmptyRootHash
+ } else {
+ b.header.TxHash = DeriveSha(Transactions(txs))
+ b.transactions = make(Transactions, len(txs))
+ copy(b.transactions, txs)
+ }
+
+ if len(receipts) == 0 {
+ b.header.ReceiptHash = EmptyRootHash
+ } else {
+ b.header.ReceiptHash = DeriveSha(Receipts(receipts))
+ b.header.Bloom = CreateBloom(receipts)
+ }
+
+ if len(uncles) == 0 {
+ b.header.UncleHash = EmptyUncleHash
+ } else {
+ b.header.UncleHash = CalcUncleHash(uncles)
+ b.uncles = make([]*Header, len(uncles))
+ for i := range uncles {
+ b.uncles[i] = CopyHeader(uncles[i])
+ }
+ }
+
+ return b
+}
+
+// NewBlockWithHeader creates a block with the given header data. The
+// header data is copied, changes to header and to the field values
+// will not affect the block.
+func NewBlockWithHeader(header *Header) *Block {
+ return &Block{header: CopyHeader(header)}
+}
+
+// CopyHeader creates a deep copy of a block header to prevent side effects from
+// modifying a header variable.
+func CopyHeader(h *Header) *Header {
+ cpy := *h
+ if cpy.Difficulty = new(big.Int); h.Difficulty != nil {
+ cpy.Difficulty.Set(h.Difficulty)
+ }
+ if cpy.Number = new(big.Int); h.Number != nil {
+ cpy.Number.Set(h.Number)
+ }
+ if len(h.Extra) > 0 {
+ cpy.Extra = make([]byte, len(h.Extra))
+ copy(cpy.Extra, h.Extra)
+ }
+ return &cpy
+}
+
+// DecodeRLP decodes the Ethereum
+func (b *Block) DecodeRLP(s *rlp.Stream) error {
+ var eb extblock
+ _, size, _ := s.Kind()
+ if err := s.Decode(&eb); err != nil {
+ return err
+ }
+ b.header, b.uncles, b.transactions = eb.Header, eb.Uncles, eb.Txs
+ b.size.Store(common.StorageSize(rlp.ListSize(size)))
+ return nil
+}
+
+// EncodeRLP serializes b into the Ethereum RLP block format.
+func (b *Block) EncodeRLP(w io.Writer) error {
+ return rlp.Encode(w, extblock{
+ Header: b.header,
+ Txs: b.transactions,
+ Uncles: b.uncles,
+ })
+}
+
+// [deprecated by eth/63]
+func (b *StorageBlock) DecodeRLP(s *rlp.Stream) error {
+ var sb storageblock
+ if err := s.Decode(&sb); err != nil {
+ return err
+ }
+ b.header, b.uncles, b.transactions, b.td = sb.Header, sb.Uncles, sb.Txs, sb.TD
+ return nil
+}
+
+// TODO: copies
+
+func (b *Block) Uncles() []*Header { return b.uncles }
+func (b *Block) Transactions() Transactions { return b.transactions }
+
+func (b *Block) Transaction(hash common.Hash) *Transaction {
+ for _, transaction := range b.transactions {
+ if transaction.Hash() == hash {
+ return transaction
+ }
+ }
+ return nil
+}
+
+func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number) }
+func (b *Block) GasLimit() uint64 { return b.header.GasLimit }
+func (b *Block) GasUsed() uint64 { return b.header.GasUsed }
+func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
+func (b *Block) Time() uint64 { return b.header.Time }
+
+func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
+func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }
+func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.header.Nonce[:]) }
+func (b *Block) Bloom() Bloom { return b.header.Bloom }
+func (b *Block) Coinbase() common.Address { return b.header.Coinbase }
+func (b *Block) Root() common.Hash { return b.header.Root }
+func (b *Block) ParentHash() common.Hash { return b.header.ParentHash }
+func (b *Block) TxHash() common.Hash { return b.header.TxHash }
+func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash }
+func (b *Block) UncleHash() common.Hash { return b.header.UncleHash }
+func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) }
+
+func (b *Block) Header() *Header { return CopyHeader(b.header) }
+
+// Body returns the non-header content of the block.
+func (b *Block) Body() *Body { return &Body{b.transactions, b.uncles} }
+
+// Size returns the true RLP encoded storage size of the block, either by encoding
+// and returning it, or returning a previsouly cached value.
+func (b *Block) Size() common.StorageSize {
+ if size := b.size.Load(); size != nil {
+ return size.(common.StorageSize)
+ }
+ c := writeCounter(0)
+ rlp.Encode(&c, b)
+ b.size.Store(common.StorageSize(c))
+ return common.StorageSize(c)
+}
+
+// SanityCheck can be used to prevent that unbounded fields are
+// stuffed with junk data to add processing overhead
+func (b *Block) SanityCheck() error {
+ return b.header.SanityCheck()
+}
+
+type writeCounter common.StorageSize
+
+func (c *writeCounter) Write(b []byte) (int, error) {
+ *c += writeCounter(len(b))
+ return len(b), nil
+}
+
+func CalcUncleHash(uncles []*Header) common.Hash {
+ if len(uncles) == 0 {
+ return EmptyUncleHash
+ }
+ return rlpHash(uncles)
+}
+
+// WithSeal returns a new block with the data from b but the header replaced with
+// the sealed one.
+func (b *Block) WithSeal(header *Header) *Block {
+ cpy := *header
+
+ return &Block{
+ header: &cpy,
+ transactions: b.transactions,
+ uncles: b.uncles,
+ }
+}
+
+// WithBody returns a new block with the given transaction and uncle contents.
+func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
+ block := &Block{
+ header: CopyHeader(b.header),
+ transactions: make([]*Transaction, len(transactions)),
+ uncles: make([]*Header, len(uncles)),
+ }
+ copy(block.transactions, transactions)
+ for i := range uncles {
+ block.uncles[i] = CopyHeader(uncles[i])
+ }
+ return block
+}
+
+// Hash returns the keccak256 hash of b's header.
+// The hash is computed on the first call and cached thereafter.
+func (b *Block) Hash() common.Hash {
+ if hash := b.hash.Load(); hash != nil {
+ return hash.(common.Hash)
+ }
+ v := b.header.Hash()
+ b.hash.Store(v)
+ return v
+}
+
+type Blocks []*Block
+
+type BlockBy func(b1, b2 *Block) bool
+
+func (self BlockBy) Sort(blocks Blocks) {
+ bs := blockSorter{
+ blocks: blocks,
+ by: self,
+ }
+ sort.Sort(bs)
+}
+
+type blockSorter struct {
+ blocks Blocks
+ by func(b1, b2 *Block) bool
+}
+
+func (self blockSorter) Len() int { return len(self.blocks) }
+func (self blockSorter) Swap(i, j int) {
+ self.blocks[i], self.blocks[j] = self.blocks[j], self.blocks[i]
+}
+func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) }
+
+func Number(b1, b2 *Block) bool { return b1.header.Number.Cmp(b2.header.Number) < 0 }
diff --git a/core/types/bloom9.go b/core/types/bloom9.go
new file mode 100644
index 0000000..76923c4
--- /dev/null
+++ b/core/types/bloom9.go
@@ -0,0 +1,136 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+import (
+ "fmt"
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+type bytesBacked interface {
+ Bytes() []byte
+}
+
+const (
+ // BloomByteLength represents the number of bytes used in a header log bloom.
+ BloomByteLength = 256
+
+ // BloomBitLength represents the number of bits used in a header log bloom.
+ BloomBitLength = 8 * BloomByteLength
+)
+
+// Bloom represents a 2048 bit bloom filter.
+type Bloom [BloomByteLength]byte
+
+// BytesToBloom converts a byte slice to a bloom filter.
+// It panics if b is not of suitable size.
+func BytesToBloom(b []byte) Bloom {
+ var bloom Bloom
+ bloom.SetBytes(b)
+ return bloom
+}
+
+// SetBytes sets the content of b to the given bytes.
+// It panics if d is not of suitable size.
+func (b *Bloom) SetBytes(d []byte) {
+ if len(b) < len(d) {
+ panic(fmt.Sprintf("bloom bytes too big %d %d", len(b), len(d)))
+ }
+ copy(b[BloomByteLength-len(d):], d)
+}
+
+// Add adds d to the filter. Future calls of Test(d) will return true.
+func (b *Bloom) Add(d *big.Int) {
+ bin := new(big.Int).SetBytes(b[:])
+ bin.Or(bin, bloom9(d.Bytes()))
+ b.SetBytes(bin.Bytes())
+}
+
+// Big converts b to a big integer.
+func (b Bloom) Big() *big.Int {
+ return new(big.Int).SetBytes(b[:])
+}
+
+func (b Bloom) Bytes() []byte {
+ return b[:]
+}
+
+func (b Bloom) Test(test *big.Int) bool {
+ return BloomLookup(b, test)
+}
+
+func (b Bloom) TestBytes(test []byte) bool {
+ return b.Test(new(big.Int).SetBytes(test))
+
+}
+
+// MarshalText encodes b as a hex string with 0x prefix.
+func (b Bloom) MarshalText() ([]byte, error) {
+ return hexutil.Bytes(b[:]).MarshalText()
+}
+
+// UnmarshalText b as a hex string with 0x prefix.
+func (b *Bloom) UnmarshalText(input []byte) error {
+ return hexutil.UnmarshalFixedText("Bloom", input, b[:])
+}
+
+func CreateBloom(receipts Receipts) Bloom {
+ bin := new(big.Int)
+ for _, receipt := range receipts {
+ bin.Or(bin, LogsBloom(receipt.Logs))
+ }
+
+ return BytesToBloom(bin.Bytes())
+}
+
+func LogsBloom(logs []*Log) *big.Int {
+ bin := new(big.Int)
+ for _, log := range logs {
+ bin.Or(bin, bloom9(log.Address.Bytes()))
+ for _, b := range log.Topics {
+ bin.Or(bin, bloom9(b[:]))
+ }
+ }
+
+ return bin
+}
+
+func bloom9(b []byte) *big.Int {
+ b = crypto.Keccak256(b)
+
+ r := new(big.Int)
+
+ for i := 0; i < 6; i += 2 {
+ t := big.NewInt(1)
+ b := (uint(b[i+1]) + (uint(b[i]) << 8)) & 2047
+ r.Or(r, t.Lsh(t, b))
+ }
+
+ return r
+}
+
+var Bloom9 = bloom9
+
+func BloomLookup(bin Bloom, topic bytesBacked) bool {
+ bloom := bin.Big()
+ cmp := bloom9(topic.Bytes())
+
+ return bloom.And(bloom, cmp).Cmp(cmp) == 0
+}
diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go
new file mode 100644
index 0000000..8234191
--- /dev/null
+++ b/core/types/derive_sha.go
@@ -0,0 +1,41 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+import (
+ "bytes"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/rlp"
+ "github.com/ava-labs/go-ethereum/trie"
+)
+
+type DerivableList interface {
+ Len() int
+ GetRlp(i int) []byte
+}
+
+func DeriveSha(list DerivableList) common.Hash {
+ keybuf := new(bytes.Buffer)
+ trie := new(trie.Trie)
+ for i := 0; i < list.Len(); i++ {
+ keybuf.Reset()
+ rlp.Encode(keybuf, uint(i))
+ trie.Update(keybuf.Bytes(), list.GetRlp(i))
+ }
+ return trie.Hash()
+}
diff --git a/core/types/gen_header_json.go b/core/types/gen_header_json.go
new file mode 100644
index 0000000..48b35db
--- /dev/null
+++ b/core/types/gen_header_json.go
@@ -0,0 +1,138 @@
+// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
+
+package types
+
+import (
+ "encoding/json"
+ "errors"
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+)
+
+var _ = (*headerMarshaling)(nil)
+
+// MarshalJSON marshals as JSON.
+func (h Header) MarshalJSON() ([]byte, error) {
+ type Header struct {
+ ParentHash common.Hash `json:"parentHash" gencodec:"required"`
+ UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
+ Coinbase common.Address `json:"miner" gencodec:"required"`
+ Root common.Hash `json:"stateRoot" gencodec:"required"`
+ TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
+ ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
+ Bloom Bloom `json:"logsBloom" gencodec:"required"`
+ Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
+ Number *hexutil.Big `json:"number" gencodec:"required"`
+ GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
+ GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
+ Time hexutil.Uint64 `json:"timestamp" gencodec:"required"`
+ Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
+ MixDigest common.Hash `json:"mixHash"`
+ Nonce BlockNonce `json:"nonce"`
+ Hash common.Hash `json:"hash"`
+ }
+ var enc Header
+ enc.ParentHash = h.ParentHash
+ enc.UncleHash = h.UncleHash
+ enc.Coinbase = h.Coinbase
+ enc.Root = h.Root
+ enc.TxHash = h.TxHash
+ enc.ReceiptHash = h.ReceiptHash
+ enc.Bloom = h.Bloom
+ enc.Difficulty = (*hexutil.Big)(h.Difficulty)
+ enc.Number = (*hexutil.Big)(h.Number)
+ enc.GasLimit = hexutil.Uint64(h.GasLimit)
+ enc.GasUsed = hexutil.Uint64(h.GasUsed)
+ enc.Time = hexutil.Uint64(h.Time)
+ enc.Extra = h.Extra
+ enc.MixDigest = h.MixDigest
+ enc.Nonce = h.Nonce
+ enc.Hash = h.Hash()
+ return json.Marshal(&enc)
+}
+
+// UnmarshalJSON unmarshals from JSON.
+func (h *Header) UnmarshalJSON(input []byte) error {
+ type Header struct {
+ ParentHash *common.Hash `json:"parentHash" gencodec:"required"`
+ UncleHash *common.Hash `json:"sha3Uncles" gencodec:"required"`
+ Coinbase *common.Address `json:"miner" gencodec:"required"`
+ Root *common.Hash `json:"stateRoot" gencodec:"required"`
+ TxHash *common.Hash `json:"transactionsRoot" gencodec:"required"`
+ ReceiptHash *common.Hash `json:"receiptsRoot" gencodec:"required"`
+ Bloom *Bloom `json:"logsBloom" gencodec:"required"`
+ Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
+ Number *hexutil.Big `json:"number" gencodec:"required"`
+ GasLimit *hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
+ GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
+ Time *hexutil.Uint64 `json:"timestamp" gencodec:"required"`
+ Extra *hexutil.Bytes `json:"extraData" gencodec:"required"`
+ MixDigest *common.Hash `json:"mixHash"`
+ Nonce *BlockNonce `json:"nonce"`
+ }
+ var dec Header
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ if dec.ParentHash == nil {
+ return errors.New("missing required field 'parentHash' for Header")
+ }
+ h.ParentHash = *dec.ParentHash
+ if dec.UncleHash == nil {
+ return errors.New("missing required field 'sha3Uncles' for Header")
+ }
+ h.UncleHash = *dec.UncleHash
+ if dec.Coinbase == nil {
+ return errors.New("missing required field 'miner' for Header")
+ }
+ h.Coinbase = *dec.Coinbase
+ if dec.Root == nil {
+ return errors.New("missing required field 'stateRoot' for Header")
+ }
+ h.Root = *dec.Root
+ if dec.TxHash == nil {
+ return errors.New("missing required field 'transactionsRoot' for Header")
+ }
+ h.TxHash = *dec.TxHash
+ if dec.ReceiptHash == nil {
+ return errors.New("missing required field 'receiptsRoot' for Header")
+ }
+ h.ReceiptHash = *dec.ReceiptHash
+ if dec.Bloom == nil {
+ return errors.New("missing required field 'logsBloom' for Header")
+ }
+ h.Bloom = *dec.Bloom
+ if dec.Difficulty == nil {
+ return errors.New("missing required field 'difficulty' for Header")
+ }
+ h.Difficulty = (*big.Int)(dec.Difficulty)
+ if dec.Number == nil {
+ return errors.New("missing required field 'number' for Header")
+ }
+ h.Number = (*big.Int)(dec.Number)
+ if dec.GasLimit == nil {
+ return errors.New("missing required field 'gasLimit' for Header")
+ }
+ h.GasLimit = uint64(*dec.GasLimit)
+ if dec.GasUsed == nil {
+ return errors.New("missing required field 'gasUsed' for Header")
+ }
+ h.GasUsed = uint64(*dec.GasUsed)
+ if dec.Time == nil {
+ return errors.New("missing required field 'timestamp' for Header")
+ }
+ h.Time = uint64(*dec.Time)
+ if dec.Extra == nil {
+ return errors.New("missing required field 'extraData' for Header")
+ }
+ h.Extra = *dec.Extra
+ if dec.MixDigest != nil {
+ h.MixDigest = *dec.MixDigest
+ }
+ if dec.Nonce != nil {
+ h.Nonce = *dec.Nonce
+ }
+ return nil
+}
diff --git a/core/types/gen_log_json.go b/core/types/gen_log_json.go
new file mode 100644
index 0000000..0e48863
--- /dev/null
+++ b/core/types/gen_log_json.go
@@ -0,0 +1,92 @@
+// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
+
+package types
+
+import (
+ "encoding/json"
+ "errors"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+)
+
+var _ = (*logMarshaling)(nil)
+
+// MarshalJSON marshals as JSON.
+func (l Log) MarshalJSON() ([]byte, error) {
+ type Log struct {
+ Address common.Address `json:"address" gencodec:"required"`
+ Topics []common.Hash `json:"topics" gencodec:"required"`
+ Data hexutil.Bytes `json:"data" gencodec:"required"`
+ BlockNumber hexutil.Uint64 `json:"blockNumber"`
+ TxHash common.Hash `json:"transactionHash" gencodec:"required"`
+ TxIndex hexutil.Uint `json:"transactionIndex" gencodec:"required"`
+ BlockHash common.Hash `json:"blockHash"`
+ Index hexutil.Uint `json:"logIndex" gencodec:"required"`
+ Removed bool `json:"removed"`
+ }
+ var enc Log
+ enc.Address = l.Address
+ enc.Topics = l.Topics
+ enc.Data = l.Data
+ enc.BlockNumber = hexutil.Uint64(l.BlockNumber)
+ enc.TxHash = l.TxHash
+ enc.TxIndex = hexutil.Uint(l.TxIndex)
+ enc.BlockHash = l.BlockHash
+ enc.Index = hexutil.Uint(l.Index)
+ enc.Removed = l.Removed
+ return json.Marshal(&enc)
+}
+
+// UnmarshalJSON unmarshals from JSON.
+func (l *Log) UnmarshalJSON(input []byte) error {
+ type Log struct {
+ Address *common.Address `json:"address" gencodec:"required"`
+ Topics []common.Hash `json:"topics" gencodec:"required"`
+ Data *hexutil.Bytes `json:"data" gencodec:"required"`
+ BlockNumber *hexutil.Uint64 `json:"blockNumber"`
+ TxHash *common.Hash `json:"transactionHash" gencodec:"required"`
+ TxIndex *hexutil.Uint `json:"transactionIndex" gencodec:"required"`
+ BlockHash *common.Hash `json:"blockHash"`
+ Index *hexutil.Uint `json:"logIndex" gencodec:"required"`
+ Removed *bool `json:"removed"`
+ }
+ var dec Log
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ if dec.Address == nil {
+ return errors.New("missing required field 'address' for Log")
+ }
+ l.Address = *dec.Address
+ if dec.Topics == nil {
+ return errors.New("missing required field 'topics' for Log")
+ }
+ l.Topics = dec.Topics
+ if dec.Data == nil {
+ return errors.New("missing required field 'data' for Log")
+ }
+ l.Data = *dec.Data
+ if dec.BlockNumber != nil {
+ l.BlockNumber = uint64(*dec.BlockNumber)
+ }
+ if dec.TxHash == nil {
+ return errors.New("missing required field 'transactionHash' for Log")
+ }
+ l.TxHash = *dec.TxHash
+ if dec.TxIndex == nil {
+ return errors.New("missing required field 'transactionIndex' for Log")
+ }
+ l.TxIndex = uint(*dec.TxIndex)
+ if dec.BlockHash != nil {
+ l.BlockHash = *dec.BlockHash
+ }
+ if dec.Index == nil {
+ return errors.New("missing required field 'logIndex' for Log")
+ }
+ l.Index = uint(*dec.Index)
+ if dec.Removed != nil {
+ l.Removed = *dec.Removed
+ }
+ return nil
+}
diff --git a/core/types/gen_receipt_json.go b/core/types/gen_receipt_json.go
new file mode 100644
index 0000000..f7cde61
--- /dev/null
+++ b/core/types/gen_receipt_json.go
@@ -0,0 +1,104 @@
+// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
+
+package types
+
+import (
+ "encoding/json"
+ "errors"
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+)
+
+var _ = (*receiptMarshaling)(nil)
+
+// MarshalJSON marshals as JSON.
+func (r Receipt) MarshalJSON() ([]byte, error) {
+ type Receipt struct {
+ PostState hexutil.Bytes `json:"root"`
+ Status hexutil.Uint64 `json:"status"`
+ CumulativeGasUsed hexutil.Uint64 `json:"cumulativeGasUsed" gencodec:"required"`
+ Bloom Bloom `json:"logsBloom" gencodec:"required"`
+ Logs []*Log `json:"logs" gencodec:"required"`
+ TxHash common.Hash `json:"transactionHash" gencodec:"required"`
+ ContractAddress common.Address `json:"contractAddress"`
+ GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
+ BlockHash common.Hash `json:"blockHash,omitempty"`
+ BlockNumber *hexutil.Big `json:"blockNumber,omitempty"`
+ TransactionIndex hexutil.Uint `json:"transactionIndex"`
+ }
+ var enc Receipt
+ enc.PostState = r.PostState
+ enc.Status = hexutil.Uint64(r.Status)
+ enc.CumulativeGasUsed = hexutil.Uint64(r.CumulativeGasUsed)
+ enc.Bloom = r.Bloom
+ enc.Logs = r.Logs
+ enc.TxHash = r.TxHash
+ enc.ContractAddress = r.ContractAddress
+ enc.GasUsed = hexutil.Uint64(r.GasUsed)
+ enc.BlockHash = r.BlockHash
+ enc.BlockNumber = (*hexutil.Big)(r.BlockNumber)
+ enc.TransactionIndex = hexutil.Uint(r.TransactionIndex)
+ return json.Marshal(&enc)
+}
+
+// UnmarshalJSON unmarshals from JSON.
+func (r *Receipt) UnmarshalJSON(input []byte) error {
+ type Receipt struct {
+ PostState *hexutil.Bytes `json:"root"`
+ Status *hexutil.Uint64 `json:"status"`
+ CumulativeGasUsed *hexutil.Uint64 `json:"cumulativeGasUsed" gencodec:"required"`
+ Bloom *Bloom `json:"logsBloom" gencodec:"required"`
+ Logs []*Log `json:"logs" gencodec:"required"`
+ TxHash *common.Hash `json:"transactionHash" gencodec:"required"`
+ ContractAddress *common.Address `json:"contractAddress"`
+ GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
+ BlockHash *common.Hash `json:"blockHash,omitempty"`
+ BlockNumber *hexutil.Big `json:"blockNumber,omitempty"`
+ TransactionIndex *hexutil.Uint `json:"transactionIndex"`
+ }
+ var dec Receipt
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ if dec.PostState != nil {
+ r.PostState = *dec.PostState
+ }
+ if dec.Status != nil {
+ r.Status = uint64(*dec.Status)
+ }
+ if dec.CumulativeGasUsed == nil {
+ return errors.New("missing required field 'cumulativeGasUsed' for Receipt")
+ }
+ r.CumulativeGasUsed = uint64(*dec.CumulativeGasUsed)
+ if dec.Bloom == nil {
+ return errors.New("missing required field 'logsBloom' for Receipt")
+ }
+ r.Bloom = *dec.Bloom
+ if dec.Logs == nil {
+ return errors.New("missing required field 'logs' for Receipt")
+ }
+ r.Logs = dec.Logs
+ if dec.TxHash == nil {
+ return errors.New("missing required field 'transactionHash' for Receipt")
+ }
+ r.TxHash = *dec.TxHash
+ if dec.ContractAddress != nil {
+ r.ContractAddress = *dec.ContractAddress
+ }
+ if dec.GasUsed == nil {
+ return errors.New("missing required field 'gasUsed' for Receipt")
+ }
+ r.GasUsed = uint64(*dec.GasUsed)
+ if dec.BlockHash != nil {
+ r.BlockHash = *dec.BlockHash
+ }
+ if dec.BlockNumber != nil {
+ r.BlockNumber = (*big.Int)(dec.BlockNumber)
+ }
+ if dec.TransactionIndex != nil {
+ r.TransactionIndex = uint(*dec.TransactionIndex)
+ }
+ return nil
+}
diff --git a/core/types/gen_tx_json.go b/core/types/gen_tx_json.go
new file mode 100644
index 0000000..0410632
--- /dev/null
+++ b/core/types/gen_tx_json.go
@@ -0,0 +1,101 @@
+// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
+
+package types
+
+import (
+ "encoding/json"
+ "errors"
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+)
+
+var _ = (*txdataMarshaling)(nil)
+
+// MarshalJSON marshals as JSON.
+func (t txdata) MarshalJSON() ([]byte, error) {
+ type txdata struct {
+ AccountNonce hexutil.Uint64 `json:"nonce" gencodec:"required"`
+ Price *hexutil.Big `json:"gasPrice" gencodec:"required"`
+ GasLimit hexutil.Uint64 `json:"gas" gencodec:"required"`
+ Recipient *common.Address `json:"to" rlp:"nil"`
+ Amount *hexutil.Big `json:"value" gencodec:"required"`
+ Payload hexutil.Bytes `json:"input" gencodec:"required"`
+ V *hexutil.Big `json:"v" gencodec:"required"`
+ R *hexutil.Big `json:"r" gencodec:"required"`
+ S *hexutil.Big `json:"s" gencodec:"required"`
+ Hash *common.Hash `json:"hash" rlp:"-"`
+ }
+ var enc txdata
+ enc.AccountNonce = hexutil.Uint64(t.AccountNonce)
+ enc.Price = (*hexutil.Big)(t.Price)
+ enc.GasLimit = hexutil.Uint64(t.GasLimit)
+ enc.Recipient = t.Recipient
+ enc.Amount = (*hexutil.Big)(t.Amount)
+ enc.Payload = t.Payload
+ enc.V = (*hexutil.Big)(t.V)
+ enc.R = (*hexutil.Big)(t.R)
+ enc.S = (*hexutil.Big)(t.S)
+ enc.Hash = t.Hash
+ return json.Marshal(&enc)
+}
+
+// UnmarshalJSON unmarshals from JSON.
+func (t *txdata) UnmarshalJSON(input []byte) error {
+ type txdata struct {
+ AccountNonce *hexutil.Uint64 `json:"nonce" gencodec:"required"`
+ Price *hexutil.Big `json:"gasPrice" gencodec:"required"`
+ GasLimit *hexutil.Uint64 `json:"gas" gencodec:"required"`
+ Recipient *common.Address `json:"to" rlp:"nil"`
+ Amount *hexutil.Big `json:"value" gencodec:"required"`
+ Payload *hexutil.Bytes `json:"input" gencodec:"required"`
+ V *hexutil.Big `json:"v" gencodec:"required"`
+ R *hexutil.Big `json:"r" gencodec:"required"`
+ S *hexutil.Big `json:"s" gencodec:"required"`
+ Hash *common.Hash `json:"hash" rlp:"-"`
+ }
+ var dec txdata
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ if dec.AccountNonce == nil {
+ return errors.New("missing required field 'nonce' for txdata")
+ }
+ t.AccountNonce = uint64(*dec.AccountNonce)
+ if dec.Price == nil {
+ return errors.New("missing required field 'gasPrice' for txdata")
+ }
+ t.Price = (*big.Int)(dec.Price)
+ if dec.GasLimit == nil {
+ return errors.New("missing required field 'gas' for txdata")
+ }
+ t.GasLimit = uint64(*dec.GasLimit)
+ if dec.Recipient != nil {
+ t.Recipient = dec.Recipient
+ }
+ if dec.Amount == nil {
+ return errors.New("missing required field 'value' for txdata")
+ }
+ t.Amount = (*big.Int)(dec.Amount)
+ if dec.Payload == nil {
+ return errors.New("missing required field 'input' for txdata")
+ }
+ t.Payload = *dec.Payload
+ if dec.V == nil {
+ return errors.New("missing required field 'v' for txdata")
+ }
+ t.V = (*big.Int)(dec.V)
+ if dec.R == nil {
+ return errors.New("missing required field 'r' for txdata")
+ }
+ t.R = (*big.Int)(dec.R)
+ if dec.S == nil {
+ return errors.New("missing required field 's' for txdata")
+ }
+ t.S = (*big.Int)(dec.S)
+ if dec.Hash != nil {
+ t.Hash = dec.Hash
+ }
+ return nil
+}
diff --git a/core/types/log.go b/core/types/log.go
new file mode 100644
index 0000000..0b6184f
--- /dev/null
+++ b/core/types/log.go
@@ -0,0 +1,143 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+import (
+ "io"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+//go:generate gencodec -type Log -field-override logMarshaling -out gen_log_json.go
+
+// Log represents a contract log event. These events are generated by the LOG opcode and
+// stored/indexed by the node.
+type Log struct {
+ // Consensus fields:
+ // address of the contract that generated the event
+ Address common.Address `json:"address" gencodec:"required"`
+ // list of topics provided by the contract.
+ Topics []common.Hash `json:"topics" gencodec:"required"`
+ // supplied by the contract, usually ABI-encoded
+ Data []byte `json:"data" gencodec:"required"`
+
+ // Derived fields. These fields are filled in by the node
+ // but not secured by consensus.
+ // block in which the transaction was included
+ BlockNumber uint64 `json:"blockNumber"`
+ // hash of the transaction
+ TxHash common.Hash `json:"transactionHash" gencodec:"required"`
+ // index of the transaction in the block
+ TxIndex uint `json:"transactionIndex" gencodec:"required"`
+ // hash of the block in which the transaction was included
+ BlockHash common.Hash `json:"blockHash"`
+ // index of the log in the block
+ Index uint `json:"logIndex" gencodec:"required"`
+
+ // The Removed field is true if this log was reverted due to a chain reorganisation.
+ // You must pay attention to this field if you receive logs through a filter query.
+ Removed bool `json:"removed"`
+}
+
+type logMarshaling struct {
+ Data hexutil.Bytes
+ BlockNumber hexutil.Uint64
+ TxIndex hexutil.Uint
+ Index hexutil.Uint
+}
+
+type rlpLog struct {
+ Address common.Address
+ Topics []common.Hash
+ Data []byte
+}
+
+// rlpStorageLog is the storage encoding of a log.
+type rlpStorageLog rlpLog
+
+// legacyRlpStorageLog is the previous storage encoding of a log including some redundant fields.
+type legacyRlpStorageLog struct {
+ Address common.Address
+ Topics []common.Hash
+ Data []byte
+ BlockNumber uint64
+ TxHash common.Hash
+ TxIndex uint
+ BlockHash common.Hash
+ Index uint
+}
+
+// EncodeRLP implements rlp.Encoder.
+func (l *Log) EncodeRLP(w io.Writer) error {
+ return rlp.Encode(w, rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data})
+}
+
+// DecodeRLP implements rlp.Decoder.
+func (l *Log) DecodeRLP(s *rlp.Stream) error {
+ var dec rlpLog
+ err := s.Decode(&dec)
+ if err == nil {
+ l.Address, l.Topics, l.Data = dec.Address, dec.Topics, dec.Data
+ }
+ return err
+}
+
+// LogForStorage is a wrapper around a Log that flattens and parses the entire content of
+// a log including non-consensus fields.
+type LogForStorage Log
+
+// EncodeRLP implements rlp.Encoder.
+func (l *LogForStorage) EncodeRLP(w io.Writer) error {
+ return rlp.Encode(w, rlpStorageLog{
+ Address: l.Address,
+ Topics: l.Topics,
+ Data: l.Data,
+ })
+}
+
+// DecodeRLP implements rlp.Decoder.
+//
+// Note some redundant fields(e.g. block number, tx hash etc) will be assembled later.
+func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
+ blob, err := s.Raw()
+ if err != nil {
+ return err
+ }
+ var dec rlpStorageLog
+ err = rlp.DecodeBytes(blob, &dec)
+ if err == nil {
+ *l = LogForStorage{
+ Address: dec.Address,
+ Topics: dec.Topics,
+ Data: dec.Data,
+ }
+ } else {
+ // Try to decode log with previous definition.
+ var dec legacyRlpStorageLog
+ err = rlp.DecodeBytes(blob, &dec)
+ if err == nil {
+ *l = LogForStorage{
+ Address: dec.Address,
+ Topics: dec.Topics,
+ Data: dec.Data,
+ }
+ }
+ }
+ return err
+}
diff --git a/core/types/receipt.go b/core/types/receipt.go
new file mode 100644
index 0000000..46538e8
--- /dev/null
+++ b/core/types/receipt.go
@@ -0,0 +1,336 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "io"
+ "math/big"
+ "unsafe"
+
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+//go:generate gencodec -type Receipt -field-override receiptMarshaling -out gen_receipt_json.go
+
+var (
+ receiptStatusFailedRLP = []byte{}
+ receiptStatusSuccessfulRLP = []byte{0x01}
+)
+
+const (
+ // ReceiptStatusFailed is the status code of a transaction if execution failed.
+ ReceiptStatusFailed = uint64(0)
+
+ // ReceiptStatusSuccessful is the status code of a transaction if execution succeeded.
+ ReceiptStatusSuccessful = uint64(1)
+)
+
+// Receipt represents the results of a transaction.
+type Receipt struct {
+ // Consensus fields: These fields are defined by the Yellow Paper
+ PostState []byte `json:"root"`
+ Status uint64 `json:"status"`
+ CumulativeGasUsed uint64 `json:"cumulativeGasUsed" gencodec:"required"`
+ Bloom Bloom `json:"logsBloom" gencodec:"required"`
+ Logs []*Log `json:"logs" gencodec:"required"`
+
+ // Implementation fields: These fields are added by geth when processing a transaction.
+ // They are stored in the chain database.
+ TxHash common.Hash `json:"transactionHash" gencodec:"required"`
+ ContractAddress common.Address `json:"contractAddress"`
+ GasUsed uint64 `json:"gasUsed" gencodec:"required"`
+
+ // Inclusion information: These fields provide information about the inclusion of the
+ // transaction corresponding to this receipt.
+ BlockHash common.Hash `json:"blockHash,omitempty"`
+ BlockNumber *big.Int `json:"blockNumber,omitempty"`
+ TransactionIndex uint `json:"transactionIndex"`
+}
+
+type receiptMarshaling struct {
+ PostState hexutil.Bytes
+ Status hexutil.Uint64
+ CumulativeGasUsed hexutil.Uint64
+ GasUsed hexutil.Uint64
+ BlockNumber *hexutil.Big
+ TransactionIndex hexutil.Uint
+}
+
+// receiptRLP is the consensus encoding of a receipt.
+type receiptRLP struct {
+ PostStateOrStatus []byte
+ CumulativeGasUsed uint64
+ Bloom Bloom
+ Logs []*Log
+}
+
+// storedReceiptRLP is the storage encoding of a receipt.
+type storedReceiptRLP struct {
+ PostStateOrStatus []byte
+ CumulativeGasUsed uint64
+ Logs []*LogForStorage
+}
+
+// v4StoredReceiptRLP is the storage encoding of a receipt used in database version 4.
+type v4StoredReceiptRLP struct {
+ PostStateOrStatus []byte
+ CumulativeGasUsed uint64
+ TxHash common.Hash
+ ContractAddress common.Address
+ Logs []*LogForStorage
+ GasUsed uint64
+}
+
+// v3StoredReceiptRLP is the original storage encoding of a receipt including some unnecessary fields.
+type v3StoredReceiptRLP struct {
+ PostStateOrStatus []byte
+ CumulativeGasUsed uint64
+ Bloom Bloom
+ TxHash common.Hash
+ ContractAddress common.Address
+ Logs []*LogForStorage
+ GasUsed uint64
+}
+
+// NewReceipt creates a barebone transaction receipt, copying the init fields.
+func NewReceipt(root []byte, failed bool, cumulativeGasUsed uint64) *Receipt {
+ r := &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: cumulativeGasUsed}
+ if failed {
+ r.Status = ReceiptStatusFailed
+ } else {
+ r.Status = ReceiptStatusSuccessful
+ }
+ return r
+}
+
+// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
+// into an RLP stream. If no post state is present, byzantium fork is assumed.
+func (r *Receipt) EncodeRLP(w io.Writer) error {
+ return rlp.Encode(w, &receiptRLP{r.statusEncoding(), r.CumulativeGasUsed, r.Bloom, r.Logs})
+}
+
+// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
+// from an RLP stream.
+func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
+ var dec receiptRLP
+ if err := s.Decode(&dec); err != nil {
+ return err
+ }
+ if err := r.setStatus(dec.PostStateOrStatus); err != nil {
+ return err
+ }
+ r.CumulativeGasUsed, r.Bloom, r.Logs = dec.CumulativeGasUsed, dec.Bloom, dec.Logs
+ return nil
+}
+
+func (r *Receipt) setStatus(postStateOrStatus []byte) error {
+ switch {
+ case bytes.Equal(postStateOrStatus, receiptStatusSuccessfulRLP):
+ r.Status = ReceiptStatusSuccessful
+ case bytes.Equal(postStateOrStatus, receiptStatusFailedRLP):
+ r.Status = ReceiptStatusFailed
+ case len(postStateOrStatus) == len(common.Hash{}):
+ r.PostState = postStateOrStatus
+ default:
+ return fmt.Errorf("invalid receipt status %x", postStateOrStatus)
+ }
+ return nil
+}
+
+func (r *Receipt) statusEncoding() []byte {
+ if len(r.PostState) == 0 {
+ if r.Status == ReceiptStatusFailed {
+ return receiptStatusFailedRLP
+ }
+ return receiptStatusSuccessfulRLP
+ }
+ return r.PostState
+}
+
+// Size returns the approximate memory used by all internal contents. It is used
+// to approximate and limit the memory consumption of various caches.
+func (r *Receipt) Size() common.StorageSize {
+ size := common.StorageSize(unsafe.Sizeof(*r)) + common.StorageSize(len(r.PostState))
+
+ size += common.StorageSize(len(r.Logs)) * common.StorageSize(unsafe.Sizeof(Log{}))
+ for _, log := range r.Logs {
+ size += common.StorageSize(len(log.Topics)*common.HashLength + len(log.Data))
+ }
+ return size
+}
+
+// ReceiptForStorage is a wrapper around a Receipt that flattens and parses the
+// entire content of a receipt, as opposed to only the consensus fields originally.
+type ReceiptForStorage Receipt
+
+// EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
+// into an RLP stream.
+func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
+ enc := &storedReceiptRLP{
+ PostStateOrStatus: (*Receipt)(r).statusEncoding(),
+ CumulativeGasUsed: r.CumulativeGasUsed,
+ Logs: make([]*LogForStorage, len(r.Logs)),
+ }
+ for i, log := range r.Logs {
+ enc.Logs[i] = (*LogForStorage)(log)
+ }
+ return rlp.Encode(w, enc)
+}
+
+// DecodeRLP implements rlp.Decoder, and loads both consensus and implementation
+// fields of a receipt from an RLP stream.
+func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
+ // Retrieve the entire receipt blob as we need to try multiple decoders
+ blob, err := s.Raw()
+ if err != nil {
+ return err
+ }
+ // Try decoding from the newest format for future proofness, then the older one
+ // for old nodes that just upgraded. V4 was an intermediate unreleased format so
+ // we do need to decode it, but it's not common (try last).
+ if err := decodeStoredReceiptRLP(r, blob); err == nil {
+ return nil
+ }
+ if err := decodeV3StoredReceiptRLP(r, blob); err == nil {
+ return nil
+ }
+ return decodeV4StoredReceiptRLP(r, blob)
+}
+
+func decodeStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
+ var stored storedReceiptRLP
+ if err := rlp.DecodeBytes(blob, &stored); err != nil {
+ return err
+ }
+ if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
+ return err
+ }
+ r.CumulativeGasUsed = stored.CumulativeGasUsed
+ r.Logs = make([]*Log, len(stored.Logs))
+ for i, log := range stored.Logs {
+ r.Logs[i] = (*Log)(log)
+ }
+ r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})
+
+ return nil
+}
+
+func decodeV4StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
+ var stored v4StoredReceiptRLP
+ if err := rlp.DecodeBytes(blob, &stored); err != nil {
+ return err
+ }
+ if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
+ return err
+ }
+ r.CumulativeGasUsed = stored.CumulativeGasUsed
+ r.TxHash = stored.TxHash
+ r.ContractAddress = stored.ContractAddress
+ r.GasUsed = stored.GasUsed
+ r.Logs = make([]*Log, len(stored.Logs))
+ for i, log := range stored.Logs {
+ r.Logs[i] = (*Log)(log)
+ }
+ r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})
+
+ return nil
+}
+
+func decodeV3StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
+ var stored v3StoredReceiptRLP
+ if err := rlp.DecodeBytes(blob, &stored); err != nil {
+ return err
+ }
+ if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
+ return err
+ }
+ r.CumulativeGasUsed = stored.CumulativeGasUsed
+ r.Bloom = stored.Bloom
+ r.TxHash = stored.TxHash
+ r.ContractAddress = stored.ContractAddress
+ r.GasUsed = stored.GasUsed
+ r.Logs = make([]*Log, len(stored.Logs))
+ for i, log := range stored.Logs {
+ r.Logs[i] = (*Log)(log)
+ }
+ return nil
+}
+
+// Receipts is a wrapper around a Receipt array to implement DerivableList.
+type Receipts []*Receipt
+
+// Len returns the number of receipts in this list.
+func (r Receipts) Len() int { return len(r) }
+
+// GetRlp returns the RLP encoding of one receipt from the list.
+func (r Receipts) GetRlp(i int) []byte {
+ bytes, err := rlp.EncodeToBytes(r[i])
+ if err != nil {
+ panic(err)
+ }
+ return bytes
+}
+
+// DeriveFields fills the receipts with their computed fields based on consensus
+// data and contextual infos like containing block and transactions.
+func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error {
+ signer := MakeSigner(config, new(big.Int).SetUint64(number))
+
+ logIndex := uint(0)
+ if len(txs) != len(r) {
+ return errors.New("transaction and receipt count mismatch")
+ }
+ for i := 0; i < len(r); i++ {
+ // The transaction hash can be retrieved from the transaction itself
+ r[i].TxHash = txs[i].Hash()
+
+ // block location fields
+ r[i].BlockHash = hash
+ r[i].BlockNumber = new(big.Int).SetUint64(number)
+ r[i].TransactionIndex = uint(i)
+
+ // The contract address can be derived from the transaction itself
+ if txs[i].To() == nil {
+ // Deriving the signer is expensive, only do if it's actually needed
+ from, _ := Sender(signer, txs[i])
+ r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce())
+ }
+ // The used gas can be calculated based on previous r
+ if i == 0 {
+ r[i].GasUsed = r[i].CumulativeGasUsed
+ } else {
+ r[i].GasUsed = r[i].CumulativeGasUsed - r[i-1].CumulativeGasUsed
+ }
+ // The derived log fields can simply be set from the block and transaction
+ for j := 0; j < len(r[i].Logs); j++ {
+ r[i].Logs[j].BlockNumber = number
+ r[i].Logs[j].BlockHash = hash
+ r[i].Logs[j].TxHash = r[i].TxHash
+ r[i].Logs[j].TxIndex = uint(i)
+ r[i].Logs[j].Index = logIndex
+ logIndex++
+ }
+ }
+ return nil
+}
diff --git a/core/types/transaction.go b/core/types/transaction.go
new file mode 100644
index 0000000..cf9e61a
--- /dev/null
+++ b/core/types/transaction.go
@@ -0,0 +1,440 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+import (
+ "container/heap"
+ "errors"
+ "io"
+ "math/big"
+ "sync/atomic"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/rlp"
+)
+
+//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
+
+var (
+ ErrInvalidSig = errors.New("invalid transaction v, r, s values")
+)
+
+type Transaction struct {
+ data txdata
+ // caches
+ hash atomic.Value
+ size atomic.Value
+ from atomic.Value
+}
+
+type txdata struct {
+ AccountNonce uint64 `json:"nonce" gencodec:"required"`
+ Price *big.Int `json:"gasPrice" gencodec:"required"`
+ GasLimit uint64 `json:"gas" gencodec:"required"`
+ Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
+ Amount *big.Int `json:"value" gencodec:"required"`
+ CoinID *common.Hash `json:"coinid" rlp:"-"`
+ Amount2 *big.Int `json:"value2"`
+ Payload []byte `json:"input" gencodec:"required"`
+
+ // Signature values
+ V *big.Int `json:"v" gencodec:"required"`
+ R *big.Int `json:"r" gencodec:"required"`
+ S *big.Int `json:"s" gencodec:"required"`
+
+ // This is only used when marshaling to JSON.
+ Hash *common.Hash `json:"hash" rlp:"-"`
+}
+
+type txdataMarshaling struct {
+ AccountNonce hexutil.Uint64
+ Price *hexutil.Big
+ GasLimit hexutil.Uint64
+ Amount *hexutil.Big
+ CoinID *hexutil.Bytes
+ Amount2 *hexutil.Big
+ Payload hexutil.Bytes
+ V *hexutil.Big
+ R *hexutil.Big
+ S *hexutil.Big
+}
+
+func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
+ return newTransaction(nonce, &to, amount, gasLimit, gasPrice, data)
+}
+
+func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
+ return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data)
+}
+
+func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
+ if len(data) > 0 {
+ data = common.CopyBytes(data)
+ }
+ d := txdata{
+ AccountNonce: nonce,
+ Recipient: to,
+ Payload: data,
+ Amount: new(big.Int),
+ CoinID: nil,
+ Amount2: new(big.Int),
+ GasLimit: gasLimit,
+ Price: new(big.Int),
+ V: new(big.Int),
+ R: new(big.Int),
+ S: new(big.Int),
+ }
+ if amount != nil {
+ d.Amount.Set(amount)
+ }
+ if gasPrice != nil {
+ d.Price.Set(gasPrice)
+ }
+
+ return &Transaction{data: d}
+}
+
+func (tx *Transaction) SetMultiCoinValue(coinID *common.Hash, amount *big.Int) {
+ tx.data.CoinID = coinID
+ tx.data.Amount2.Set(amount)
+}
+
+// ChainId returns which chain id this transaction was signed for (if at all)
+func (tx *Transaction) ChainId() *big.Int {
+ return deriveChainId(tx.data.V)
+}
+
+// Protected returns whether the transaction is protected from replay protection.
+func (tx *Transaction) Protected() bool {
+ return isProtectedV(tx.data.V)
+}
+
+func isProtectedV(V *big.Int) bool {
+ if V.BitLen() <= 8 {
+ v := V.Uint64()
+ return v != 27 && v != 28
+ }
+ // anything not 27 or 28 is considered protected
+ return true
+}
+
+// EncodeRLP implements rlp.Encoder
+func (tx *Transaction) EncodeRLP(w io.Writer) error {
+ return rlp.Encode(w, &tx.data)
+}
+
+// DecodeRLP implements rlp.Decoder
+func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
+ _, size, _ := s.Kind()
+ err := s.Decode(&tx.data)
+ if err == nil {
+ tx.size.Store(common.StorageSize(rlp.ListSize(size)))
+ }
+
+ return err
+}
+
+// MarshalJSON encodes the web3 RPC transaction format.
+func (tx *Transaction) MarshalJSON() ([]byte, error) {
+ hash := tx.Hash()
+ data := tx.data
+ data.Hash = &hash
+ return data.MarshalJSON()
+}
+
+// UnmarshalJSON decodes the web3 RPC transaction format.
+func (tx *Transaction) UnmarshalJSON(input []byte) error {
+ var dec txdata
+ if err := dec.UnmarshalJSON(input); err != nil {
+ return err
+ }
+
+ withSignature := dec.V.Sign() != 0 || dec.R.Sign() != 0 || dec.S.Sign() != 0
+ if withSignature {
+ var V byte
+ if isProtectedV(dec.V) {
+ chainID := deriveChainId(dec.V).Uint64()
+ V = byte(dec.V.Uint64() - 35 - 2*chainID)
+ } else {
+ V = byte(dec.V.Uint64() - 27)
+ }
+ if !crypto.ValidateSignatureValues(V, dec.R, dec.S, false) {
+ return ErrInvalidSig
+ }
+ }
+
+ *tx = Transaction{data: dec}
+ return nil
+}
+
+func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) }
+func (tx *Transaction) Gas() uint64 { return tx.data.GasLimit }
+func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Price) }
+func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) }
+func (tx *Transaction) CoinID() *big.Int { return big.NewInt(0) }
+func (tx *Transaction) Value2() *big.Int { return big.NewInt(0) }
+func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce }
+func (tx *Transaction) CheckNonce() bool { return true }
+
+// To returns the recipient address of the transaction.
+// It returns nil if the transaction is a contract creation.
+func (tx *Transaction) To() *common.Address {
+ if tx.data.Recipient == nil {
+ return nil
+ }
+ to := *tx.data.Recipient
+ return &to
+}
+
+// Hash hashes the RLP encoding of tx.
+// It uniquely identifies the transaction.
+func (tx *Transaction) Hash() common.Hash {
+ if hash := tx.hash.Load(); hash != nil {
+ return hash.(common.Hash)
+ }
+ v := rlpHash(tx)
+ tx.hash.Store(v)
+ return v
+}
+
+// Size returns the true RLP encoded storage size of the transaction, either by
+// encoding and returning it, or returning a previsouly cached value.
+func (tx *Transaction) Size() common.StorageSize {
+ if size := tx.size.Load(); size != nil {
+ return size.(common.StorageSize)
+ }
+ c := writeCounter(0)
+ rlp.Encode(&c, &tx.data)
+ tx.size.Store(common.StorageSize(c))
+ return common.StorageSize(c)
+}
+
+// AsMessage returns the transaction as a core.Message.
+//
+// AsMessage requires a signer to derive the sender.
+//
+// XXX Rename message to something less arbitrary?
+func (tx *Transaction) AsMessage(s Signer) (Message, error) {
+ msg := Message{
+ nonce: tx.data.AccountNonce,
+ gasLimit: tx.data.GasLimit,
+ gasPrice: new(big.Int).Set(tx.data.Price),
+ to: tx.data.Recipient,
+ amount: tx.data.Amount,
+ coinID: tx.data.CoinID,
+ amount2: tx.data.Amount2,
+ data: tx.data.Payload,
+ checkNonce: true,
+ }
+
+ var err error
+ msg.from, err = Sender(s, tx)
+ return msg, err
+}
+
+// WithSignature returns a new transaction with the given signature.
+// This signature needs to be in the [R || S || V] format where V is 0 or 1.
+func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, error) {
+ r, s, v, err := signer.SignatureValues(tx, sig)
+ if err != nil {
+ return nil, err
+ }
+ cpy := &Transaction{data: tx.data}
+ cpy.data.R, cpy.data.S, cpy.data.V = r, s, v
+ return cpy, nil
+}
+
+// Cost returns amount + gasprice * gaslimit.
+func (tx *Transaction) Cost() *big.Int {
+ total := new(big.Int).Mul(tx.data.Price, new(big.Int).SetUint64(tx.data.GasLimit))
+ total.Add(total, tx.data.Amount)
+ return total
+}
+
+// RawSignatureValues returns the V, R, S signature values of the transaction.
+// The return values should not be modified by the caller.
+func (tx *Transaction) RawSignatureValues() (v, r, s *big.Int) {
+ return tx.data.V, tx.data.R, tx.data.S
+}
+
+// Transactions is a Transaction slice type for basic sorting.
+type Transactions []*Transaction
+
+// Len returns the length of s.
+func (s Transactions) Len() int { return len(s) }
+
+// Swap swaps the i'th and the j'th element in s.
+func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+// GetRlp implements Rlpable and returns the i'th element of s in rlp.
+func (s Transactions) GetRlp(i int) []byte {
+ enc, _ := rlp.EncodeToBytes(s[i])
+ return enc
+}
+
+// TxDifference returns a new set which is the difference between a and b.
+func TxDifference(a, b Transactions) Transactions {
+ keep := make(Transactions, 0, len(a))
+
+ remove := make(map[common.Hash]struct{})
+ for _, tx := range b {
+ remove[tx.Hash()] = struct{}{}
+ }
+
+ for _, tx := range a {
+ if _, ok := remove[tx.Hash()]; !ok {
+ keep = append(keep, tx)
+ }
+ }
+
+ return keep
+}
+
+// TxByNonce implements the sort interface to allow sorting a list of transactions
+// by their nonces. This is usually only useful for sorting transactions from a
+// single account, otherwise a nonce comparison doesn't make much sense.
+type TxByNonce Transactions
+
+func (s TxByNonce) Len() int { return len(s) }
+func (s TxByNonce) Less(i, j int) bool { return s[i].data.AccountNonce < s[j].data.AccountNonce }
+func (s TxByNonce) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+// TxByPrice implements both the sort and the heap interface, making it useful
+// for all at once sorting as well as individually adding and removing elements.
+type TxByPrice Transactions
+
+func (s TxByPrice) Len() int { return len(s) }
+func (s TxByPrice) Less(i, j int) bool { return s[i].data.Price.Cmp(s[j].data.Price) > 0 }
+func (s TxByPrice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+func (s *TxByPrice) Push(x interface{}) {
+ *s = append(*s, x.(*Transaction))
+}
+
+func (s *TxByPrice) Pop() interface{} {
+ old := *s
+ n := len(old)
+ x := old[n-1]
+ *s = old[0 : n-1]
+ return x
+}
+
+// TransactionsByPriceAndNonce represents a set of transactions that can return
+// transactions in a profit-maximizing sorted order, while supporting removing
+// entire batches of transactions for non-executable accounts.
+type TransactionsByPriceAndNonce struct {
+ txs map[common.Address]Transactions // Per account nonce-sorted list of transactions
+ heads TxByPrice // Next transaction for each unique account (price heap)
+ signer Signer // Signer for the set of transactions
+}
+
+// NewTransactionsByPriceAndNonce creates a transaction set that can retrieve
+// price sorted transactions in a nonce-honouring way.
+//
+// Note, the input map is reowned so the caller should not interact any more with
+// if after providing it to the constructor.
+func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
+ // Initialize a price based heap with the head transactions
+ heads := make(TxByPrice, 0, len(txs))
+ for from, accTxs := range txs {
+ heads = append(heads, accTxs[0])
+ // Ensure the sender address is from the signer
+ acc, _ := Sender(signer, accTxs[0])
+ txs[acc] = accTxs[1:]
+ if from != acc {
+ delete(txs, from)
+ }
+ }
+ heap.Init(&heads)
+
+ // Assemble and return the transaction set
+ return &TransactionsByPriceAndNonce{
+ txs: txs,
+ heads: heads,
+ signer: signer,
+ }
+}
+
+// Peek returns the next transaction by price.
+func (t *TransactionsByPriceAndNonce) Peek() *Transaction {
+ if len(t.heads) == 0 {
+ return nil
+ }
+ return t.heads[0]
+}
+
+// Shift replaces the current best head with the next one from the same account.
+func (t *TransactionsByPriceAndNonce) Shift() {
+ acc, _ := Sender(t.signer, t.heads[0])
+ if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
+ t.heads[0], t.txs[acc] = txs[0], txs[1:]
+ heap.Fix(&t.heads, 0)
+ } else {
+ heap.Pop(&t.heads)
+ }
+}
+
+// Pop removes the best transaction, *not* replacing it with the next one from
+// the same account. This should be used when a transaction cannot be executed
+// and hence all subsequent ones should be discarded from the same account.
+func (t *TransactionsByPriceAndNonce) Pop() {
+ heap.Pop(&t.heads)
+}
+
+// Message is a fully derived transaction and implements core.Message
+//
+// NOTE: In a future PR this will be removed.
+type Message struct {
+ to *common.Address
+ from common.Address
+ nonce uint64
+ amount *big.Int
+ coinID *common.Hash
+ amount2 *big.Int
+ gasLimit uint64
+ gasPrice *big.Int
+ data []byte
+ checkNonce bool
+}
+
+func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, checkNonce bool) Message {
+ return Message{
+ from: from,
+ to: to,
+ nonce: nonce,
+ amount: amount,
+ coinID: nil,
+ amount2: big.NewInt(0),
+ gasLimit: gasLimit,
+ gasPrice: gasPrice,
+ data: data,
+ checkNonce: checkNonce,
+ }
+}
+
+func (m Message) From() common.Address { return m.from }
+func (m Message) To() *common.Address { return m.to }
+func (m Message) GasPrice() *big.Int { return m.gasPrice }
+func (m Message) Value() *big.Int { return m.amount }
+func (m Message) CoinID() *common.Hash { return m.coinID }
+func (m Message) Value2() *big.Int { return m.amount2 }
+func (m Message) Gas() uint64 { return m.gasLimit }
+func (m Message) Nonce() uint64 { return m.nonce }
+func (m Message) Data() []byte { return m.data }
+func (m Message) CheckNonce() bool { return m.checkNonce }
diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go
new file mode 100644
index 0000000..cb556eb
--- /dev/null
+++ b/core/types/transaction_signing.go
@@ -0,0 +1,260 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package types
+
+import (
+ "crypto/ecdsa"
+ "errors"
+ "fmt"
+ "math/big"
+
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+var (
+ ErrInvalidChainId = errors.New("invalid chain id for signer")
+)
+
+// sigCache is used to cache the derived sender and contains
+// the signer used to derive it.
+type sigCache struct {
+ signer Signer
+ from common.Address
+}
+
+// MakeSigner returns a Signer based on the given chain config and block number.
+func MakeSigner(config *params.ChainConfig, blockNumber *big.Int) Signer {
+ var signer Signer
+ switch {
+ case config.IsEIP155(blockNumber):
+ signer = NewEIP155Signer(config.ChainID)
+ case config.IsHomestead(blockNumber):
+ signer = HomesteadSigner{}
+ default:
+ signer = FrontierSigner{}
+ }
+ return signer
+}
+
+// SignTx signs the transaction using the given signer and private key
+func SignTx(tx *Transaction, s Signer, prv *ecdsa.PrivateKey) (*Transaction, error) {
+ h := s.Hash(tx)
+ sig, err := crypto.Sign(h[:], prv)
+ if err != nil {
+ return nil, err
+ }
+ return tx.WithSignature(s, sig)
+}
+
+// Sender returns the address derived from the signature (V, R, S) using secp256k1
+// elliptic curve and an error if it failed deriving or upon an incorrect
+// signature.
+//
+// Sender may cache the address, allowing it to be used regardless of
+// signing method. The cache is invalidated if the cached signer does
+// not match the signer used in the current call.
+func Sender(signer Signer, tx *Transaction) (common.Address, error) {
+ if sc := tx.from.Load(); sc != nil {
+ sigCache := sc.(sigCache)
+ // If the signer used to derive from in a previous
+ // call is not the same as used current, invalidate
+ // the cache.
+ if sigCache.signer.Equal(signer) {
+ return sigCache.from, nil
+ }
+ }
+
+ addr, err := signer.Sender(tx)
+ if err != nil {
+ return common.Address{}, err
+ }
+ tx.from.Store(sigCache{signer: signer, from: addr})
+ return addr, nil
+}
+
+// Signer encapsulates transaction signature handling. Note that this interface is not a
+// stable API and may change at any time to accommodate new protocol rules.
+type Signer interface {
+ // Sender returns the sender address of the transaction.
+ Sender(tx *Transaction) (common.Address, error)
+ // SignatureValues returns the raw R, S, V values corresponding to the
+ // given signature.
+ SignatureValues(tx *Transaction, sig []byte) (r, s, v *big.Int, err error)
+ // Hash returns the hash to be signed.
+ Hash(tx *Transaction) common.Hash
+ // Equal returns true if the given signer is the same as the receiver.
+ Equal(Signer) bool
+}
+
+// EIP155Transaction implements Signer using the EIP155 rules.
+type EIP155Signer struct {
+ chainId, chainIdMul *big.Int
+}
+
+func NewEIP155Signer(chainId *big.Int) EIP155Signer {
+ if chainId == nil {
+ chainId = new(big.Int)
+ }
+ return EIP155Signer{
+ chainId: chainId,
+ chainIdMul: new(big.Int).Mul(chainId, big.NewInt(2)),
+ }
+}
+
+func (s EIP155Signer) Equal(s2 Signer) bool {
+ eip155, ok := s2.(EIP155Signer)
+ return ok && eip155.chainId.Cmp(s.chainId) == 0
+}
+
+var big8 = big.NewInt(8)
+
+func (s EIP155Signer) Sender(tx *Transaction) (common.Address, error) {
+ if !tx.Protected() {
+ return HomesteadSigner{}.Sender(tx)
+ }
+ if tx.ChainId().Cmp(s.chainId) != 0 {
+ return common.Address{}, ErrInvalidChainId
+ }
+ V := new(big.Int).Sub(tx.data.V, s.chainIdMul)
+ V.Sub(V, big8)
+ return recoverPlain(s.Hash(tx), tx.data.R, tx.data.S, V, true)
+}
+
+// SignatureValues returns signature values. This signature
+// needs to be in the [R || S || V] format where V is 0 or 1.
+func (s EIP155Signer) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big.Int, err error) {
+ R, S, V, err = HomesteadSigner{}.SignatureValues(tx, sig)
+ if err != nil {
+ return nil, nil, nil, err
+ }
+ if s.chainId.Sign() != 0 {
+ V = big.NewInt(int64(sig[64] + 35))
+ V.Add(V, s.chainIdMul)
+ }
+ return R, S, V, nil
+}
+
+// Hash returns the hash to be signed by the sender.
+// It does not uniquely identify the transaction.
+func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
+ return rlpHash([]interface{}{
+ tx.data.AccountNonce,
+ tx.data.Price,
+ tx.data.GasLimit,
+ tx.data.Recipient,
+ tx.data.Amount,
+ tx.data.Payload,
+ s.chainId, uint(0), uint(0),
+ })
+}
+
+// HomesteadTransaction implements TransactionInterface using the
+// homestead rules.
+type HomesteadSigner struct{ FrontierSigner }
+
+func (s HomesteadSigner) Equal(s2 Signer) bool {
+ _, ok := s2.(HomesteadSigner)
+ return ok
+}
+
+// SignatureValues returns signature values. This signature
+// needs to be in the [R || S || V] format where V is 0 or 1.
+func (hs HomesteadSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v *big.Int, err error) {
+ return hs.FrontierSigner.SignatureValues(tx, sig)
+}
+
+func (hs HomesteadSigner) Sender(tx *Transaction) (common.Address, error) {
+ return recoverPlain(hs.Hash(tx), tx.data.R, tx.data.S, tx.data.V, true)
+}
+
+type FrontierSigner struct{}
+
+func (s FrontierSigner) Equal(s2 Signer) bool {
+ _, ok := s2.(FrontierSigner)
+ return ok
+}
+
+// SignatureValues returns signature values. This signature
+// needs to be in the [R || S || V] format where V is 0 or 1.
+func (fs FrontierSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v *big.Int, err error) {
+ if len(sig) != crypto.SignatureLength {
+ panic(fmt.Sprintf("wrong size for signature: got %d, want %d", len(sig), crypto.SignatureLength))
+ }
+ r = new(big.Int).SetBytes(sig[:32])
+ s = new(big.Int).SetBytes(sig[32:64])
+ v = new(big.Int).SetBytes([]byte{sig[64] + 27})
+ return r, s, v, nil
+}
+
+// Hash returns the hash to be signed by the sender.
+// It does not uniquely identify the transaction.
+func (fs FrontierSigner) Hash(tx *Transaction) common.Hash {
+ return rlpHash([]interface{}{
+ tx.data.AccountNonce,
+ tx.data.Price,
+ tx.data.GasLimit,
+ tx.data.Recipient,
+ tx.data.Amount,
+ tx.data.Payload,
+ })
+}
+
+func (fs FrontierSigner) Sender(tx *Transaction) (common.Address, error) {
+ return recoverPlain(fs.Hash(tx), tx.data.R, tx.data.S, tx.data.V, false)
+}
+
+func recoverPlain(sighash common.Hash, R, S, Vb *big.Int, homestead bool) (common.Address, error) {
+ if Vb.BitLen() > 8 {
+ return common.Address{}, ErrInvalidSig
+ }
+ V := byte(Vb.Uint64() - 27)
+ if !crypto.ValidateSignatureValues(V, R, S, homestead) {
+ return common.Address{}, ErrInvalidSig
+ }
+ // encode the signature in uncompressed format
+ r, s := R.Bytes(), S.Bytes()
+ sig := make([]byte, crypto.SignatureLength)
+ copy(sig[32-len(r):32], r)
+ copy(sig[64-len(s):64], s)
+ sig[64] = V
+ // recover the public key from the signature
+ pub, err := crypto.Ecrecover(sighash[:], sig)
+ if err != nil {
+ return common.Address{}, err
+ }
+ if len(pub) == 0 || pub[0] != 4 {
+ return common.Address{}, errors.New("invalid public key")
+ }
+ var addr common.Address
+ copy(addr[:], crypto.Keccak256(pub[1:])[12:])
+ return addr, nil
+}
+
+// deriveChainId derives the chain id from the given v parameter
+func deriveChainId(v *big.Int) *big.Int {
+ if v.BitLen() <= 64 {
+ v := v.Uint64()
+ if v == 27 || v == 28 {
+ return new(big.Int)
+ }
+ return new(big.Int).SetUint64((v - 35) / 2)
+ }
+ v = new(big.Int).Sub(v, big.NewInt(35))
+ return v.Div(v, big.NewInt(2))
+}
diff --git a/core/vm/analysis.go b/core/vm/analysis.go
new file mode 100644
index 0000000..0ccf47b
--- /dev/null
+++ b/core/vm/analysis.go
@@ -0,0 +1,62 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+// bitvec is a bit vector which maps bytes in a program.
+// An unset bit means the byte is an opcode, a set bit means
+// it's data (i.e. argument of PUSHxx).
+type bitvec []byte
+
+func (bits *bitvec) set(pos uint64) {
+ (*bits)[pos/8] |= 0x80 >> (pos % 8)
+}
+func (bits *bitvec) set8(pos uint64) {
+ (*bits)[pos/8] |= 0xFF >> (pos % 8)
+ (*bits)[pos/8+1] |= ^(0xFF >> (pos % 8))
+}
+
+// codeSegment checks if the position is in a code segment.
+func (bits *bitvec) codeSegment(pos uint64) bool {
+ return ((*bits)[pos/8] & (0x80 >> (pos % 8))) == 0
+}
+
+// codeBitmap collects data locations in code.
+func codeBitmap(code []byte) bitvec {
+ // The bitmap is 4 bytes longer than necessary, in case the code
+ // ends with a PUSH32, the algorithm will push zeroes onto the
+ // bitvector outside the bounds of the actual code.
+ bits := make(bitvec, len(code)/8+1+4)
+ for pc := uint64(0); pc < uint64(len(code)); {
+ op := OpCode(code[pc])
+
+ if op >= PUSH1 && op <= PUSH32 {
+ numbits := op - PUSH1 + 1
+ pc++
+ for ; numbits >= 8; numbits -= 8 {
+ bits.set8(pc) // 8
+ pc += 8
+ }
+ for ; numbits > 0; numbits-- {
+ bits.set(pc)
+ pc++
+ }
+ } else {
+ pc++
+ }
+ }
+ return bits
+}
diff --git a/core/vm/common.go b/core/vm/common.go
new file mode 100644
index 0000000..ead30fc
--- /dev/null
+++ b/core/vm/common.go
@@ -0,0 +1,99 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+// calcMemSize64 calculates the required memory size, and returns
+// the size and whether the result overflowed uint64
+func calcMemSize64(off, l *big.Int) (uint64, bool) {
+ if !l.IsUint64() {
+ return 0, true
+ }
+ return calcMemSize64WithUint(off, l.Uint64())
+}
+
+// calcMemSize64WithUint calculates the required memory size, and returns
+// the size and whether the result overflowed uint64
+// Identical to calcMemSize64, but length is a uint64
+func calcMemSize64WithUint(off *big.Int, length64 uint64) (uint64, bool) {
+ // if length is zero, memsize is always zero, regardless of offset
+ if length64 == 0 {
+ return 0, false
+ }
+ // Check that offset doesn't overflow
+ if !off.IsUint64() {
+ return 0, true
+ }
+ offset64 := off.Uint64()
+ val := offset64 + length64
+ // if value < either of it's parts, then it overflowed
+ return val, val < offset64
+}
+
+// getData returns a slice from the data based on the start and size and pads
+// up to size with zero's. This function is overflow safe.
+func getData(data []byte, start uint64, size uint64) []byte {
+ length := uint64(len(data))
+ if start > length {
+ start = length
+ }
+ end := start + size
+ if end > length {
+ end = length
+ }
+ return common.RightPadBytes(data[start:end], int(size))
+}
+
+// getDataBig returns a slice from the data based on the start and size and pads
+// up to size with zero's. This function is overflow safe.
+func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
+ dlen := big.NewInt(int64(len(data)))
+
+ s := math.BigMin(start, dlen)
+ e := math.BigMin(new(big.Int).Add(s, size), dlen)
+ return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
+}
+
+// bigUint64 returns the integer casted to a uint64 and returns whether it
+// overflowed in the process.
+func bigUint64(v *big.Int) (uint64, bool) {
+ return v.Uint64(), !v.IsUint64()
+}
+
+// toWordSize returns the ceiled word size required for memory expansion.
+func toWordSize(size uint64) uint64 {
+ if size > math.MaxUint64-31 {
+ return math.MaxUint64/32 + 1
+ }
+
+ return (size + 31) / 32
+}
+
+func allZero(b []byte) bool {
+ for _, byte := range b {
+ if byte != 0 {
+ return false
+ }
+ }
+ return true
+}
diff --git a/core/vm/contract.go b/core/vm/contract.go
new file mode 100644
index 0000000..ed17402
--- /dev/null
+++ b/core/vm/contract.go
@@ -0,0 +1,184 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// ContractRef is a reference to the contract's backing object
+type ContractRef interface {
+ Address() common.Address
+}
+
+// AccountRef implements ContractRef.
+//
+// Account references are used during EVM initialisation and
+// it's primary use is to fetch addresses. Removing this object
+// proves difficult because of the cached jump destinations which
+// are fetched from the parent contract (i.e. the caller), which
+// is a ContractRef.
+type AccountRef common.Address
+
+// Address casts AccountRef to a Address
+func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
+
+// Contract represents an ethereum contract in the state database. It contains
+// the contract code, calling arguments. Contract implements ContractRef
+type Contract struct {
+ // CallerAddress is the result of the caller which initialised this
+ // contract. However when the "call method" is delegated this value
+ // needs to be initialised to that of the caller's caller.
+ CallerAddress common.Address
+ caller ContractRef
+ self ContractRef
+
+ jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
+ analysis bitvec // Locally cached result of JUMPDEST analysis
+
+ Code []byte
+ CodeHash common.Hash
+ CodeAddr *common.Address
+ Input []byte
+
+ Gas uint64
+ value *big.Int
+}
+
+// NewContract returns a new contract environment for the execution of EVM.
+func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
+ c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
+
+ if parent, ok := caller.(*Contract); ok {
+ // Reuse JUMPDEST analysis from parent context if available.
+ c.jumpdests = parent.jumpdests
+ } else {
+ c.jumpdests = make(map[common.Hash]bitvec)
+ }
+
+ // Gas should be a pointer so it can safely be reduced through the run
+ // This pointer will be off the state transition
+ c.Gas = gas
+ // ensures a value is set
+ c.value = value
+
+ return c
+}
+
+func (c *Contract) validJumpdest(dest *big.Int) bool {
+ udest := dest.Uint64()
+ // PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
+ // Don't bother checking for JUMPDEST in that case.
+ if dest.BitLen() >= 63 || udest >= uint64(len(c.Code)) {
+ return false
+ }
+ // Only JUMPDESTs allowed for destinations
+ if OpCode(c.Code[udest]) != JUMPDEST {
+ return false
+ }
+ // Do we have a contract hash already?
+ if c.CodeHash != (common.Hash{}) {
+ // Does parent context have the analysis?
+ analysis, exist := c.jumpdests[c.CodeHash]
+ if !exist {
+ // Do the analysis and save in parent context
+ // We do not need to store it in c.analysis
+ analysis = codeBitmap(c.Code)
+ c.jumpdests[c.CodeHash] = analysis
+ }
+ return analysis.codeSegment(udest)
+ }
+ // We don't have the code hash, most likely a piece of initcode not already
+ // in state trie. In that case, we do an analysis, and save it locally, so
+ // we don't have to recalculate it for every JUMP instruction in the execution
+ // However, we don't save it within the parent context
+ if c.analysis == nil {
+ c.analysis = codeBitmap(c.Code)
+ }
+ return c.analysis.codeSegment(udest)
+}
+
+// AsDelegate sets the contract to be a delegate call and returns the current
+// contract (for chaining calls)
+func (c *Contract) AsDelegate() *Contract {
+ // NOTE: caller must, at all times be a contract. It should never happen
+ // that caller is something other than a Contract.
+ parent := c.caller.(*Contract)
+ c.CallerAddress = parent.CallerAddress
+ c.value = parent.value
+
+ return c
+}
+
+// GetOp returns the n'th element in the contract's byte array
+func (c *Contract) GetOp(n uint64) OpCode {
+ return OpCode(c.GetByte(n))
+}
+
+// GetByte returns the n'th byte in the contract's byte array
+func (c *Contract) GetByte(n uint64) byte {
+ if n < uint64(len(c.Code)) {
+ return c.Code[n]
+ }
+
+ return 0
+}
+
+// Caller returns the caller of the contract.
+//
+// Caller will recursively call caller when the contract is a delegate
+// call, including that of caller's caller.
+func (c *Contract) Caller() common.Address {
+ return c.CallerAddress
+}
+
+// UseGas attempts the use gas and subtracts it and returns true on success
+func (c *Contract) UseGas(gas uint64) (ok bool) {
+ if c.Gas < gas {
+ return false
+ }
+ c.Gas -= gas
+ return true
+}
+
+// Address returns the contracts address
+func (c *Contract) Address() common.Address {
+ return c.self.Address()
+}
+
+// Value returns the contract's value (sent to it from it's caller)
+func (c *Contract) Value() *big.Int {
+ return c.value
+}
+
+// SetCallCode sets the code of the contract and address of the backing data
+// object
+func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
+ c.Code = code
+ c.CodeHash = hash
+ c.CodeAddr = addr
+}
+
+// SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
+// In case hash is not provided, the jumpdest analysis will not be saved to the parent context
+func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
+ c.Code = codeAndHash.code
+ c.CodeHash = codeAndHash.hash
+ c.CodeAddr = addr
+}
diff --git a/core/vm/contracts.go b/core/vm/contracts.go
new file mode 100644
index 0000000..54eab4e
--- /dev/null
+++ b/core/vm/contracts.go
@@ -0,0 +1,497 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "crypto/sha256"
+ "encoding/binary"
+ "errors"
+ "math/big"
+
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/crypto/blake2b"
+ "github.com/ava-labs/go-ethereum/crypto/bn256"
+ "golang.org/x/crypto/ripemd160"
+)
+
+// PrecompiledContract is the basic interface for native Go contracts. The implementation
+// requires a deterministic gas count based on the input size of the Run method of the
+// contract.
+type PrecompiledContract interface {
+ RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use
+ Run(input []byte) ([]byte, error) // Run runs the precompiled contract
+}
+
+// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
+// contracts used in the Frontier and Homestead releases.
+var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
+ common.BytesToAddress([]byte{1}): &ecrecover{},
+ common.BytesToAddress([]byte{2}): &sha256hash{},
+ common.BytesToAddress([]byte{3}): &ripemd160hash{},
+ common.BytesToAddress([]byte{4}): &dataCopy{},
+}
+
+// PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum
+// contracts used in the Byzantium release.
+var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
+ common.BytesToAddress([]byte{1}): &ecrecover{},
+ common.BytesToAddress([]byte{2}): &sha256hash{},
+ common.BytesToAddress([]byte{3}): &ripemd160hash{},
+ common.BytesToAddress([]byte{4}): &dataCopy{},
+ common.BytesToAddress([]byte{5}): &bigModExp{},
+ common.BytesToAddress([]byte{6}): &bn256AddByzantium{},
+ common.BytesToAddress([]byte{7}): &bn256ScalarMulByzantium{},
+ common.BytesToAddress([]byte{8}): &bn256PairingByzantium{},
+}
+
+// PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum
+// contracts used in the Istanbul release.
+var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
+ common.BytesToAddress([]byte{1}): &ecrecover{},
+ common.BytesToAddress([]byte{2}): &sha256hash{},
+ common.BytesToAddress([]byte{3}): &ripemd160hash{},
+ common.BytesToAddress([]byte{4}): &dataCopy{},
+ common.BytesToAddress([]byte{5}): &bigModExp{},
+ common.BytesToAddress([]byte{6}): &bn256AddIstanbul{},
+ common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{},
+ common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{},
+ common.BytesToAddress([]byte{9}): &blake2F{},
+}
+
+// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
+func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contract) (ret []byte, err error) {
+ gas := p.RequiredGas(input)
+ if contract.UseGas(gas) {
+ return p.Run(input)
+ }
+ return nil, ErrOutOfGas
+}
+
+// ECRECOVER implemented as a native contract.
+type ecrecover struct{}
+
+func (c *ecrecover) RequiredGas(input []byte) uint64 {
+ return params.EcrecoverGas
+}
+
+func (c *ecrecover) Run(input []byte) ([]byte, error) {
+ const ecRecoverInputLength = 128
+
+ input = common.RightPadBytes(input, ecRecoverInputLength)
+ // "input" is (hash, v, r, s), each 32 bytes
+ // but for ecrecover we want (r, s, v)
+
+ r := new(big.Int).SetBytes(input[64:96])
+ s := new(big.Int).SetBytes(input[96:128])
+ v := input[63] - 27
+
+ // tighter sig s values input homestead only apply to tx sigs
+ if !allZero(input[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) {
+ return nil, nil
+ }
+ // v needs to be at the end for libsecp256k1
+ pubKey, err := crypto.Ecrecover(input[:32], append(input[64:128], v))
+ // make sure the public key is a valid one
+ if err != nil {
+ return nil, nil
+ }
+
+ // the first byte of pubkey is bitcoin heritage
+ return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil
+}
+
+// SHA256 implemented as a native contract.
+type sha256hash struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+//
+// This method does not require any overflow checking as the input size gas costs
+// required for anything significant is so high it's impossible to pay for.
+func (c *sha256hash) RequiredGas(input []byte) uint64 {
+ return uint64(len(input)+31)/32*params.Sha256PerWordGas + params.Sha256BaseGas
+}
+func (c *sha256hash) Run(input []byte) ([]byte, error) {
+ h := sha256.Sum256(input)
+ return h[:], nil
+}
+
+// RIPEMD160 implemented as a native contract.
+type ripemd160hash struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+//
+// This method does not require any overflow checking as the input size gas costs
+// required for anything significant is so high it's impossible to pay for.
+func (c *ripemd160hash) RequiredGas(input []byte) uint64 {
+ return uint64(len(input)+31)/32*params.Ripemd160PerWordGas + params.Ripemd160BaseGas
+}
+func (c *ripemd160hash) Run(input []byte) ([]byte, error) {
+ ripemd := ripemd160.New()
+ ripemd.Write(input)
+ return common.LeftPadBytes(ripemd.Sum(nil), 32), nil
+}
+
+// data copy implemented as a native contract.
+type dataCopy struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+//
+// This method does not require any overflow checking as the input size gas costs
+// required for anything significant is so high it's impossible to pay for.
+func (c *dataCopy) RequiredGas(input []byte) uint64 {
+ return uint64(len(input)+31)/32*params.IdentityPerWordGas + params.IdentityBaseGas
+}
+func (c *dataCopy) Run(in []byte) ([]byte, error) {
+ return in, nil
+}
+
+// bigModExp implements a native big integer exponential modular operation.
+type bigModExp struct{}
+
+var (
+ big1 = big.NewInt(1)
+ big4 = big.NewInt(4)
+ big8 = big.NewInt(8)
+ big16 = big.NewInt(16)
+ big32 = big.NewInt(32)
+ big64 = big.NewInt(64)
+ big96 = big.NewInt(96)
+ big480 = big.NewInt(480)
+ big1024 = big.NewInt(1024)
+ big3072 = big.NewInt(3072)
+ big199680 = big.NewInt(199680)
+)
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+func (c *bigModExp) RequiredGas(input []byte) uint64 {
+ var (
+ baseLen = new(big.Int).SetBytes(getData(input, 0, 32))
+ expLen = new(big.Int).SetBytes(getData(input, 32, 32))
+ modLen = new(big.Int).SetBytes(getData(input, 64, 32))
+ )
+ if len(input) > 96 {
+ input = input[96:]
+ } else {
+ input = input[:0]
+ }
+ // Retrieve the head 32 bytes of exp for the adjusted exponent length
+ var expHead *big.Int
+ if big.NewInt(int64(len(input))).Cmp(baseLen) <= 0 {
+ expHead = new(big.Int)
+ } else {
+ if expLen.Cmp(big32) > 0 {
+ expHead = new(big.Int).SetBytes(getData(input, baseLen.Uint64(), 32))
+ } else {
+ expHead = new(big.Int).SetBytes(getData(input, baseLen.Uint64(), expLen.Uint64()))
+ }
+ }
+ // Calculate the adjusted exponent length
+ var msb int
+ if bitlen := expHead.BitLen(); bitlen > 0 {
+ msb = bitlen - 1
+ }
+ adjExpLen := new(big.Int)
+ if expLen.Cmp(big32) > 0 {
+ adjExpLen.Sub(expLen, big32)
+ adjExpLen.Mul(big8, adjExpLen)
+ }
+ adjExpLen.Add(adjExpLen, big.NewInt(int64(msb)))
+
+ // Calculate the gas cost of the operation
+ gas := new(big.Int).Set(math.BigMax(modLen, baseLen))
+ switch {
+ case gas.Cmp(big64) <= 0:
+ gas.Mul(gas, gas)
+ case gas.Cmp(big1024) <= 0:
+ gas = new(big.Int).Add(
+ new(big.Int).Div(new(big.Int).Mul(gas, gas), big4),
+ new(big.Int).Sub(new(big.Int).Mul(big96, gas), big3072),
+ )
+ default:
+ gas = new(big.Int).Add(
+ new(big.Int).Div(new(big.Int).Mul(gas, gas), big16),
+ new(big.Int).Sub(new(big.Int).Mul(big480, gas), big199680),
+ )
+ }
+ gas.Mul(gas, math.BigMax(adjExpLen, big1))
+ gas.Div(gas, new(big.Int).SetUint64(params.ModExpQuadCoeffDiv))
+
+ if gas.BitLen() > 64 {
+ return math.MaxUint64
+ }
+ return gas.Uint64()
+}
+
+func (c *bigModExp) Run(input []byte) ([]byte, error) {
+ var (
+ baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64()
+ expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64()
+ modLen = new(big.Int).SetBytes(getData(input, 64, 32)).Uint64()
+ )
+ if len(input) > 96 {
+ input = input[96:]
+ } else {
+ input = input[:0]
+ }
+ // Handle a special case when both the base and mod length is zero
+ if baseLen == 0 && modLen == 0 {
+ return []byte{}, nil
+ }
+ // Retrieve the operands and execute the exponentiation
+ var (
+ base = new(big.Int).SetBytes(getData(input, 0, baseLen))
+ exp = new(big.Int).SetBytes(getData(input, baseLen, expLen))
+ mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen))
+ )
+ if mod.BitLen() == 0 {
+ // Modulo 0 is undefined, return zero
+ return common.LeftPadBytes([]byte{}, int(modLen)), nil
+ }
+ return common.LeftPadBytes(base.Exp(base, exp, mod).Bytes(), int(modLen)), nil
+}
+
+// newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point,
+// returning it, or an error if the point is invalid.
+func newCurvePoint(blob []byte) (*bn256.G1, error) {
+ p := new(bn256.G1)
+ if _, err := p.Unmarshal(blob); err != nil {
+ return nil, err
+ }
+ return p, nil
+}
+
+// newTwistPoint unmarshals a binary blob into a bn256 elliptic curve point,
+// returning it, or an error if the point is invalid.
+func newTwistPoint(blob []byte) (*bn256.G2, error) {
+ p := new(bn256.G2)
+ if _, err := p.Unmarshal(blob); err != nil {
+ return nil, err
+ }
+ return p, nil
+}
+
+// runBn256Add implements the Bn256Add precompile, referenced by both
+// Byzantium and Istanbul operations.
+func runBn256Add(input []byte) ([]byte, error) {
+ x, err := newCurvePoint(getData(input, 0, 64))
+ if err != nil {
+ return nil, err
+ }
+ y, err := newCurvePoint(getData(input, 64, 64))
+ if err != nil {
+ return nil, err
+ }
+ res := new(bn256.G1)
+ res.Add(x, y)
+ return res.Marshal(), nil
+}
+
+// bn256Add implements a native elliptic curve point addition conforming to
+// Istanbul consensus rules.
+type bn256AddIstanbul struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+func (c *bn256AddIstanbul) RequiredGas(input []byte) uint64 {
+ return params.Bn256AddGasIstanbul
+}
+
+func (c *bn256AddIstanbul) Run(input []byte) ([]byte, error) {
+ return runBn256Add(input)
+}
+
+// bn256AddByzantium implements a native elliptic curve point addition
+// conforming to Byzantium consensus rules.
+type bn256AddByzantium struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+func (c *bn256AddByzantium) RequiredGas(input []byte) uint64 {
+ return params.Bn256AddGasByzantium
+}
+
+func (c *bn256AddByzantium) Run(input []byte) ([]byte, error) {
+ return runBn256Add(input)
+}
+
+// runBn256ScalarMul implements the Bn256ScalarMul precompile, referenced by
+// both Byzantium and Istanbul operations.
+func runBn256ScalarMul(input []byte) ([]byte, error) {
+ p, err := newCurvePoint(getData(input, 0, 64))
+ if err != nil {
+ return nil, err
+ }
+ res := new(bn256.G1)
+ res.ScalarMult(p, new(big.Int).SetBytes(getData(input, 64, 32)))
+ return res.Marshal(), nil
+}
+
+// bn256ScalarMulIstanbul implements a native elliptic curve scalar
+// multiplication conforming to Istanbul consensus rules.
+type bn256ScalarMulIstanbul struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+func (c *bn256ScalarMulIstanbul) RequiredGas(input []byte) uint64 {
+ return params.Bn256ScalarMulGasIstanbul
+}
+
+func (c *bn256ScalarMulIstanbul) Run(input []byte) ([]byte, error) {
+ return runBn256ScalarMul(input)
+}
+
+// bn256ScalarMulByzantium implements a native elliptic curve scalar
+// multiplication conforming to Byzantium consensus rules.
+type bn256ScalarMulByzantium struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+func (c *bn256ScalarMulByzantium) RequiredGas(input []byte) uint64 {
+ return params.Bn256ScalarMulGasByzantium
+}
+
+func (c *bn256ScalarMulByzantium) Run(input []byte) ([]byte, error) {
+ return runBn256ScalarMul(input)
+}
+
+var (
+ // true32Byte is returned if the bn256 pairing check succeeds.
+ true32Byte = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
+
+ // false32Byte is returned if the bn256 pairing check fails.
+ false32Byte = make([]byte, 32)
+
+ // errBadPairingInput is returned if the bn256 pairing input is invalid.
+ errBadPairingInput = errors.New("bad elliptic curve pairing size")
+)
+
+// runBn256Pairing implements the Bn256Pairing precompile, referenced by both
+// Byzantium and Istanbul operations.
+func runBn256Pairing(input []byte) ([]byte, error) {
+ // Handle some corner cases cheaply
+ if len(input)%192 > 0 {
+ return nil, errBadPairingInput
+ }
+ // Convert the input into a set of coordinates
+ var (
+ cs []*bn256.G1
+ ts []*bn256.G2
+ )
+ for i := 0; i < len(input); i += 192 {
+ c, err := newCurvePoint(input[i : i+64])
+ if err != nil {
+ return nil, err
+ }
+ t, err := newTwistPoint(input[i+64 : i+192])
+ if err != nil {
+ return nil, err
+ }
+ cs = append(cs, c)
+ ts = append(ts, t)
+ }
+ // Execute the pairing checks and return the results
+ if bn256.PairingCheck(cs, ts) {
+ return true32Byte, nil
+ }
+ return false32Byte, nil
+}
+
+// bn256PairingIstanbul implements a pairing pre-compile for the bn256 curve
+// conforming to Istanbul consensus rules.
+type bn256PairingIstanbul struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+func (c *bn256PairingIstanbul) RequiredGas(input []byte) uint64 {
+ return params.Bn256PairingBaseGasIstanbul + uint64(len(input)/192)*params.Bn256PairingPerPointGasIstanbul
+}
+
+func (c *bn256PairingIstanbul) Run(input []byte) ([]byte, error) {
+ return runBn256Pairing(input)
+}
+
+// bn256PairingByzantium implements a pairing pre-compile for the bn256 curve
+// conforming to Byzantium consensus rules.
+type bn256PairingByzantium struct{}
+
+// RequiredGas returns the gas required to execute the pre-compiled contract.
+func (c *bn256PairingByzantium) RequiredGas(input []byte) uint64 {
+ return params.Bn256PairingBaseGasByzantium + uint64(len(input)/192)*params.Bn256PairingPerPointGasByzantium
+}
+
+func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) {
+ return runBn256Pairing(input)
+}
+
+type blake2F struct{}
+
+func (c *blake2F) RequiredGas(input []byte) uint64 {
+ // If the input is malformed, we can't calculate the gas, return 0 and let the
+ // actual call choke and fault.
+ if len(input) != blake2FInputLength {
+ return 0
+ }
+ return uint64(binary.BigEndian.Uint32(input[0:4]))
+}
+
+const (
+ blake2FInputLength = 213
+ blake2FFinalBlockBytes = byte(1)
+ blake2FNonFinalBlockBytes = byte(0)
+)
+
+var (
+ errBlake2FInvalidInputLength = errors.New("invalid input length")
+ errBlake2FInvalidFinalFlag = errors.New("invalid final flag")
+)
+
+func (c *blake2F) Run(input []byte) ([]byte, error) {
+ // Make sure the input is valid (correct lenth and final flag)
+ if len(input) != blake2FInputLength {
+ return nil, errBlake2FInvalidInputLength
+ }
+ if input[212] != blake2FNonFinalBlockBytes && input[212] != blake2FFinalBlockBytes {
+ return nil, errBlake2FInvalidFinalFlag
+ }
+ // Parse the input into the Blake2b call parameters
+ var (
+ rounds = binary.BigEndian.Uint32(input[0:4])
+ final = (input[212] == blake2FFinalBlockBytes)
+
+ h [8]uint64
+ m [16]uint64
+ t [2]uint64
+ )
+ for i := 0; i < 8; i++ {
+ offset := 4 + i*8
+ h[i] = binary.LittleEndian.Uint64(input[offset : offset+8])
+ }
+ for i := 0; i < 16; i++ {
+ offset := 68 + i*8
+ m[i] = binary.LittleEndian.Uint64(input[offset : offset+8])
+ }
+ t[0] = binary.LittleEndian.Uint64(input[196:204])
+ t[1] = binary.LittleEndian.Uint64(input[204:212])
+
+ // Execute the compression function, extract and return the result
+ blake2b.F(&h, m, t, final, rounds)
+
+ output := make([]byte, 64)
+ for i := 0; i < 8; i++ {
+ offset := i * 8
+ binary.LittleEndian.PutUint64(output[offset:offset+8], h[i])
+ }
+ return output, nil
+}
diff --git a/core/vm/doc.go b/core/vm/doc.go
new file mode 100644
index 0000000..5864d0c
--- /dev/null
+++ b/core/vm/doc.go
@@ -0,0 +1,24 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+/*
+Package vm implements the Ethereum Virtual Machine.
+
+The vm package implements one EVM, a byte code VM. The BC (Byte Code) VM loops
+over a set of bytes and executes them according to the set of rules defined
+in the Ethereum yellow paper.
+*/
+package vm
diff --git a/core/vm/eips.go b/core/vm/eips.go
new file mode 100644
index 0000000..75e5fb1
--- /dev/null
+++ b/core/vm/eips.go
@@ -0,0 +1,92 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "fmt"
+
+ "github.com/ava-labs/coreth/params"
+)
+
+// EnableEIP enables the given EIP on the config.
+// This operation writes in-place, and callers need to ensure that the globally
+// defined jump tables are not polluted.
+func EnableEIP(eipNum int, jt *JumpTable) error {
+ switch eipNum {
+ case 2200:
+ enable2200(jt)
+ case 1884:
+ enable1884(jt)
+ case 1344:
+ enable1344(jt)
+ default:
+ return fmt.Errorf("undefined eip %d", eipNum)
+ }
+ return nil
+}
+
+// enable1884 applies EIP-1884 to the given jump table:
+// - Increase cost of BALANCE to 700
+// - Increase cost of EXTCODEHASH to 700
+// - Increase cost of SLOAD to 800
+// - Define SELFBALANCE, with cost GasFastStep (5)
+func enable1884(jt *JumpTable) {
+ // Gas cost changes
+ jt[BALANCE].constantGas = params.BalanceGasEIP1884
+ jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884
+ jt[SLOAD].constantGas = params.SloadGasEIP1884
+
+ // New opcode
+ jt[SELFBALANCE] = operation{
+ execute: opSelfBalance,
+ constantGas: GasFastStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ }
+}
+
+func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ balance := interpreter.intPool.get().Set(interpreter.evm.StateDB.GetBalance(contract.Address()))
+ stack.push(balance)
+ return nil, nil
+}
+
+// enable1344 applies EIP-1344 (ChainID Opcode)
+// - Adds an opcode that returns the current chain’s EIP-155 unique identifier
+func enable1344(jt *JumpTable) {
+ // New opcode
+ jt[CHAINID] = operation{
+ execute: opChainID,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ }
+}
+
+// opChainID implements CHAINID opcode
+func opChainID(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ chainId := interpreter.intPool.get().Set(interpreter.evm.chainConfig.ChainID)
+ stack.push(chainId)
+ return nil, nil
+}
+
+// enable2200 applies EIP-2200 (Rebalance net-metered SSTORE)
+func enable2200(jt *JumpTable) {
+ jt[SSTORE].dynamicGas = gasSStoreEIP2200
+}
diff --git a/core/vm/errors.go b/core/vm/errors.go
new file mode 100644
index 0000000..94f0ed8
--- /dev/null
+++ b/core/vm/errors.go
@@ -0,0 +1,31 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import "errors"
+
+// List execution errors
+var (
+ ErrOutOfGas = errors.New("out of gas")
+ ErrCodeStoreOutOfGas = errors.New("contract creation code storage out of gas")
+ ErrDepth = errors.New("max call depth exceeded")
+ ErrTraceLimitReached = errors.New("the number of logs reached the specified limit")
+ ErrInsufficientBalance = errors.New("insufficient balance for transfer")
+ ErrIncompatibleAccount = errors.New("incompatible account")
+ ErrContractAddressCollision = errors.New("contract address collision")
+ ErrNoCompatibleInterpreter = errors.New("no compatible interpreter")
+)
diff --git a/core/vm/evm.go b/core/vm/evm.go
new file mode 100644
index 0000000..ff3587c
--- /dev/null
+++ b/core/vm/evm.go
@@ -0,0 +1,557 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "fmt"
+ "math/big"
+ "sync/atomic"
+ "time"
+
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// emptyCodeHash is used by create to ensure deployment is disallowed to already
+// deployed contract addresses (relevant after the account abstraction).
+var emptyCodeHash = crypto.Keccak256Hash(nil)
+
+type (
+ // CanTransferFunc is the signature of a transfer guard function
+ CanTransferFunc func(StateDB, common.Address, *big.Int) bool
+ CanTransferMCFunc func(StateDB, common.Address, common.Address, *common.Hash, *big.Int) int
+ // TransferFunc is the signature of a transfer function
+ TransferFunc func(StateDB, common.Address, common.Address, *big.Int)
+ TransferMCFunc func(StateDB, common.Address, common.Address, *common.Hash, *big.Int)
+ // GetHashFunc returns the n'th block hash in the blockchain
+ // and is used by the BLOCKHASH EVM op code.
+ GetHashFunc func(uint64) common.Hash
+)
+
+// run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter.
+func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, error) {
+ if contract.CodeAddr != nil {
+ precompiles := PrecompiledContractsHomestead
+ if evm.chainRules.IsByzantium {
+ precompiles = PrecompiledContractsByzantium
+ }
+ if evm.chainRules.IsIstanbul {
+ precompiles = PrecompiledContractsIstanbul
+ }
+ if p := precompiles[*contract.CodeAddr]; p != nil {
+ return RunPrecompiledContract(p, input, contract)
+ }
+ }
+ for _, interpreter := range evm.interpreters {
+ if interpreter.CanRun(contract.Code) {
+ if evm.interpreter != interpreter {
+ // Ensure that the interpreter pointer is set back
+ // to its current value upon return.
+ defer func(i Interpreter) {
+ evm.interpreter = i
+ }(evm.interpreter)
+ evm.interpreter = interpreter
+ }
+ return interpreter.Run(contract, input, readOnly)
+ }
+ }
+ return nil, ErrNoCompatibleInterpreter
+}
+
+// Context provides the EVM with auxiliary information. Once provided
+// it shouldn't be modified.
+type Context struct {
+ // CanTransfer returns whether the account contains
+ // sufficient ether to transfer the value
+ CanTransfer CanTransferFunc
+ CanTransferMC CanTransferMCFunc
+ // Transfer transfers ether from one account to the other
+ Transfer TransferFunc
+ TransferMultiCoin TransferMCFunc
+ // GetHash returns the hash corresponding to n
+ GetHash GetHashFunc
+
+ // Message information
+ Origin common.Address // Provides information for ORIGIN
+ GasPrice *big.Int // Provides information for GASPRICE
+
+ // Block information
+ Coinbase common.Address // Provides information for COINBASE
+ GasLimit uint64 // Provides information for GASLIMIT
+ BlockNumber *big.Int // Provides information for NUMBER
+ Time *big.Int // Provides information for TIME
+ Difficulty *big.Int // Provides information for DIFFICULTY
+}
+
+// EVM is the Ethereum Virtual Machine base object and provides
+// the necessary tools to run a contract on the given state with
+// the provided context. It should be noted that any error
+// generated through any of the calls should be considered a
+// revert-state-and-consume-all-gas operation, no checks on
+// specific errors should ever be performed. The interpreter makes
+// sure that any errors generated are to be considered faulty code.
+//
+// The EVM should never be reused and is not thread safe.
+type EVM struct {
+ // Context provides auxiliary blockchain related information
+ Context
+ // StateDB gives access to the underlying state
+ StateDB StateDB
+ // Depth is the current call stack
+ depth int
+
+ // chainConfig contains information about the current chain
+ chainConfig *params.ChainConfig
+ // chain rules contains the chain rules for the current epoch
+ chainRules params.Rules
+ // virtual machine configuration options used to initialise the
+ // evm.
+ vmConfig Config
+ // global (to this context) ethereum virtual machine
+ // used throughout the execution of the tx.
+ interpreters []Interpreter
+ interpreter Interpreter
+ // abort is used to abort the EVM calling operations
+ // NOTE: must be set atomically
+ abort int32
+ // callGasTemp holds the gas available for the current call. This is needed because the
+ // available gas is calculated in gasCall* according to the 63/64 rule and later
+ // applied in opCall*.
+ callGasTemp uint64
+}
+
+// NewEVM returns a new EVM. The returned EVM is not thread safe and should
+// only ever be used *once*.
+func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *EVM {
+ evm := &EVM{
+ Context: ctx,
+ StateDB: statedb,
+ vmConfig: vmConfig,
+ chainConfig: chainConfig,
+ chainRules: chainConfig.Rules(ctx.BlockNumber),
+ interpreters: make([]Interpreter, 0, 1),
+ }
+
+ if chainConfig.IsEWASM(ctx.BlockNumber) {
+ // to be implemented by EVM-C and Wagon PRs.
+ // if vmConfig.EWASMInterpreter != "" {
+ // extIntOpts := strings.Split(vmConfig.EWASMInterpreter, ":")
+ // path := extIntOpts[0]
+ // options := []string{}
+ // if len(extIntOpts) > 1 {
+ // options = extIntOpts[1..]
+ // }
+ // evm.interpreters = append(evm.interpreters, NewEVMVCInterpreter(evm, vmConfig, options))
+ // } else {
+ // evm.interpreters = append(evm.interpreters, NewEWASMInterpreter(evm, vmConfig))
+ // }
+ panic("No supported ewasm interpreter yet.")
+ }
+
+ // vmConfig.EVMInterpreter will be used by EVM-C, it won't be checked here
+ // as we always want to have the built-in EVM as the failover option.
+ evm.interpreters = append(evm.interpreters, NewEVMInterpreter(evm, vmConfig))
+ evm.interpreter = evm.interpreters[0]
+
+ return evm
+}
+
+// Cancel cancels any running EVM operation. This may be called concurrently and
+// it's safe to be called multiple times.
+func (evm *EVM) Cancel() {
+ atomic.StoreInt32(&evm.abort, 1)
+}
+
+// Cancelled returns true if Cancel has been called
+func (evm *EVM) Cancelled() bool {
+ return atomic.LoadInt32(&evm.abort) == 1
+}
+
+// Interpreter returns the current interpreter
+func (evm *EVM) Interpreter() Interpreter {
+ return evm.interpreter
+}
+
+// Call executes the contract associated with the addr with the given input as
+// parameters. It also handles any necessary value transfer required and takes
+// the necessary steps to create accounts and reverses the state in case of an
+// execution error or failed value transfer.
+func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
+ if evm.vmConfig.NoRecursion && evm.depth > 0 {
+ return nil, gas, nil
+ }
+
+ // Fail if we're trying to execute above the call depth limit
+ if evm.depth > int(params.CallCreateDepth) {
+ return nil, gas, ErrDepth
+ }
+ // Fail if we're trying to transfer more than the available balance
+ if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
+ return nil, gas, ErrInsufficientBalance
+ }
+
+ var (
+ to = AccountRef(addr)
+ snapshot = evm.StateDB.Snapshot()
+ )
+ if !evm.StateDB.Exist(addr) {
+ precompiles := PrecompiledContractsHomestead
+ if evm.chainRules.IsByzantium {
+ precompiles = PrecompiledContractsByzantium
+ }
+ if evm.chainRules.IsIstanbul {
+ precompiles = PrecompiledContractsIstanbul
+ }
+ if precompiles[addr] == nil && evm.chainRules.IsEIP158 && value.Sign() == 0 {
+ // Calling a non existing account, don't do anything, but ping the tracer
+ if evm.vmConfig.Debug && evm.depth == 0 {
+ evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
+ evm.vmConfig.Tracer.CaptureEnd(ret, 0, 0, nil)
+ }
+ return nil, gas, nil
+ }
+ evm.StateDB.CreateAccount(addr)
+ }
+ evm.Transfer(evm.StateDB, caller.Address(), to.Address(), value)
+ // Initialise a new contract and set the code that is to be used by the EVM.
+ // The contract is a scoped environment for this execution context only.
+ contract := NewContract(caller, to, value, gas)
+ contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
+
+ // Even if the account has no code, we need to continue because it might be a precompile
+ start := time.Now()
+
+ // Capture the tracer start/end events in debug mode
+ if evm.vmConfig.Debug && evm.depth == 0 {
+ evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
+
+ defer func() { // Lazy evaluation of the parameters
+ evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
+ }()
+ }
+ ret, err = run(evm, contract, input, false)
+
+ // When an error was returned by the EVM or when setting the creation code
+ // above we revert to the snapshot and consume any gas remaining. Additionally
+ // when we're in homestead this also counts for code storage gas errors.
+ if err != nil {
+ evm.StateDB.RevertToSnapshot(snapshot)
+ if err != errExecutionReverted {
+ contract.UseGas(contract.Gas)
+ }
+ }
+ return ret, contract.Gas, err
+}
+
+// This allows the user transfer balance of a specified coinId in addition to a normal Call().
+func (evm *EVM) CallExpert(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int, coinID *common.Hash, value2 *big.Int) (ret []byte, leftOverGas uint64, err error) {
+ fmt.Println("CallExpert")
+ if evm.vmConfig.NoRecursion && evm.depth > 0 {
+ return nil, gas, nil
+ }
+
+ // Fail if we're trying to execute above the call depth limit
+ if evm.depth > int(params.CallCreateDepth) {
+ return nil, gas, ErrDepth
+ }
+ // Fail if we're trying to transfer more than the available balance
+ if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
+ return nil, gas, ErrInsufficientBalance
+ }
+
+ var to = AccountRef(addr)
+ mcerr := evm.Context.CanTransferMC(evm.StateDB, caller.Address(), to.Address(), coinID, value2)
+ if mcerr == 1 {
+ return nil, gas, ErrInsufficientBalance
+ } else if mcerr != 0 {
+ return nil, gas, ErrIncompatibleAccount
+ }
+
+ var snapshot = evm.StateDB.Snapshot()
+
+ if !evm.StateDB.Exist(addr) {
+ precompiles := PrecompiledContractsHomestead
+ if evm.chainRules.IsByzantium {
+ precompiles = PrecompiledContractsByzantium
+ }
+ if evm.chainRules.IsIstanbul {
+ precompiles = PrecompiledContractsIstanbul
+ }
+ if precompiles[addr] == nil && evm.chainRules.IsEIP158 && value.Sign() == 0 {
+ // Calling a non existing account, don't do anything, but ping the tracer
+ if evm.vmConfig.Debug && evm.depth == 0 {
+ evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
+ evm.vmConfig.Tracer.CaptureEnd(ret, 0, 0, nil)
+ }
+ return nil, gas, nil
+ }
+ evm.StateDB.CreateAccount(addr)
+ }
+ evm.Transfer(evm.StateDB, caller.Address(), to.Address(), value)
+ evm.TransferMultiCoin(evm.StateDB, caller.Address(), to.Address(), coinID, value2)
+ // Initialise a new contract and set the code that is to be used by the EVM.
+ // The contract is a scoped environment for this execution context only.
+ contract := NewContract(caller, to, value, gas)
+ contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
+
+ // Even if the account has no code, we need to continue because it might be a precompile
+ start := time.Now()
+
+ // Capture the tracer start/end events in debug mode
+ if evm.vmConfig.Debug && evm.depth == 0 {
+ evm.vmConfig.Tracer.CaptureStart(caller.Address(), addr, false, input, gas, value)
+
+ defer func() { // Lazy evaluation of the parameters
+ evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
+ }()
+ }
+ ret, err = run(evm, contract, input, false)
+
+ // When an error was returned by the EVM or when setting the creation code
+ // above we revert to the snapshot and consume any gas remaining. Additionally
+ // when we're in homestead this also counts for code storage gas errors.
+ if err != nil {
+ evm.StateDB.RevertToSnapshot(snapshot)
+ if err != errExecutionReverted {
+ contract.UseGas(contract.Gas)
+ }
+ }
+ return ret, contract.Gas, err
+}
+
+// CallCode executes the contract associated with the addr with the given input
+// as parameters. It also handles any necessary value transfer required and takes
+// the necessary steps to create accounts and reverses the state in case of an
+// execution error or failed value transfer.
+//
+// CallCode differs from Call in the sense that it executes the given address'
+// code with the caller as context.
+func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
+ if evm.vmConfig.NoRecursion && evm.depth > 0 {
+ return nil, gas, nil
+ }
+
+ // Fail if we're trying to execute above the call depth limit
+ if evm.depth > int(params.CallCreateDepth) {
+ return nil, gas, ErrDepth
+ }
+ // Fail if we're trying to transfer more than the available balance
+ if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
+ return nil, gas, ErrInsufficientBalance
+ }
+
+ var (
+ snapshot = evm.StateDB.Snapshot()
+ to = AccountRef(caller.Address())
+ )
+ // Initialise a new contract and set the code that is to be used by the EVM.
+ // The contract is a scoped environment for this execution context only.
+ contract := NewContract(caller, to, value, gas)
+ contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
+
+ ret, err = run(evm, contract, input, false)
+ if err != nil {
+ evm.StateDB.RevertToSnapshot(snapshot)
+ if err != errExecutionReverted {
+ contract.UseGas(contract.Gas)
+ }
+ }
+ return ret, contract.Gas, err
+}
+
+// DelegateCall executes the contract associated with the addr with the given input
+// as parameters. It reverses the state in case of an execution error.
+//
+// DelegateCall differs from CallCode in the sense that it executes the given address'
+// code with the caller as context and the caller is set to the caller of the caller.
+func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
+ if evm.vmConfig.NoRecursion && evm.depth > 0 {
+ return nil, gas, nil
+ }
+ // Fail if we're trying to execute above the call depth limit
+ if evm.depth > int(params.CallCreateDepth) {
+ return nil, gas, ErrDepth
+ }
+
+ var (
+ snapshot = evm.StateDB.Snapshot()
+ to = AccountRef(caller.Address())
+ )
+
+ // Initialise a new contract and make initialise the delegate values
+ contract := NewContract(caller, to, nil, gas).AsDelegate()
+ contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
+
+ ret, err = run(evm, contract, input, false)
+ if err != nil {
+ evm.StateDB.RevertToSnapshot(snapshot)
+ if err != errExecutionReverted {
+ contract.UseGas(contract.Gas)
+ }
+ }
+ return ret, contract.Gas, err
+}
+
+// StaticCall executes the contract associated with the addr with the given input
+// as parameters while disallowing any modifications to the state during the call.
+// Opcodes that attempt to perform such modifications will result in exceptions
+// instead of performing the modifications.
+func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error) {
+ if evm.vmConfig.NoRecursion && evm.depth > 0 {
+ return nil, gas, nil
+ }
+ // Fail if we're trying to execute above the call depth limit
+ if evm.depth > int(params.CallCreateDepth) {
+ return nil, gas, ErrDepth
+ }
+
+ var (
+ to = AccountRef(addr)
+ snapshot = evm.StateDB.Snapshot()
+ )
+ // Initialise a new contract and set the code that is to be used by the EVM.
+ // The contract is a scoped environment for this execution context only.
+ contract := NewContract(caller, to, new(big.Int), gas)
+ contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr))
+
+ // We do an AddBalance of zero here, just in order to trigger a touch.
+ // This doesn't matter on Mainnet, where all empties are gone at the time of Byzantium,
+ // but is the correct thing to do and matters on other networks, in tests, and potential
+ // future scenarios
+ evm.StateDB.AddBalance(addr, bigZero)
+
+ // When an error was returned by the EVM or when setting the creation code
+ // above we revert to the snapshot and consume any gas remaining. Additionally
+ // when we're in Homestead this also counts for code storage gas errors.
+ ret, err = run(evm, contract, input, true)
+ if err != nil {
+ evm.StateDB.RevertToSnapshot(snapshot)
+ if err != errExecutionReverted {
+ contract.UseGas(contract.Gas)
+ }
+ }
+ return ret, contract.Gas, err
+}
+
+type codeAndHash struct {
+ code []byte
+ hash common.Hash
+}
+
+func (c *codeAndHash) Hash() common.Hash {
+ if c.hash == (common.Hash{}) {
+ c.hash = crypto.Keccak256Hash(c.code)
+ }
+ return c.hash
+}
+
+// create creates a new contract using code as deployment code.
+func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, value *big.Int, address common.Address) ([]byte, common.Address, uint64, error) {
+ // Depth check execution. Fail if we're trying to execute above the
+ // limit.
+ if evm.depth > int(params.CallCreateDepth) {
+ return nil, common.Address{}, gas, ErrDepth
+ }
+ if !evm.CanTransfer(evm.StateDB, caller.Address(), value) {
+ return nil, common.Address{}, gas, ErrInsufficientBalance
+ }
+ nonce := evm.StateDB.GetNonce(caller.Address())
+ evm.StateDB.SetNonce(caller.Address(), nonce+1)
+
+ // Ensure there's no existing contract already at the designated address
+ contractHash := evm.StateDB.GetCodeHash(address)
+ if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != emptyCodeHash) {
+ return nil, common.Address{}, 0, ErrContractAddressCollision
+ }
+ // Create a new account on the state
+ snapshot := evm.StateDB.Snapshot()
+ evm.StateDB.CreateAccount(address)
+ if evm.chainRules.IsEIP158 {
+ evm.StateDB.SetNonce(address, 1)
+ }
+ evm.Transfer(evm.StateDB, caller.Address(), address, value)
+
+ // Initialise a new contract and set the code that is to be used by the EVM.
+ // The contract is a scoped environment for this execution context only.
+ contract := NewContract(caller, AccountRef(address), value, gas)
+ contract.SetCodeOptionalHash(&address, codeAndHash)
+
+ if evm.vmConfig.NoRecursion && evm.depth > 0 {
+ return nil, address, gas, nil
+ }
+
+ if evm.vmConfig.Debug && evm.depth == 0 {
+ evm.vmConfig.Tracer.CaptureStart(caller.Address(), address, true, codeAndHash.code, gas, value)
+ }
+ start := time.Now()
+
+ ret, err := run(evm, contract, nil, false)
+
+ // check whether the max code size has been exceeded
+ maxCodeSizeExceeded := evm.chainRules.IsEIP158 && len(ret) > params.MaxCodeSize
+ // if the contract creation ran successfully and no errors were returned
+ // calculate the gas required to store the code. If the code could not
+ // be stored due to not enough gas set an error and let it be handled
+ // by the error checking condition below.
+ if err == nil && !maxCodeSizeExceeded {
+ createDataGas := uint64(len(ret)) * params.CreateDataGas
+ if contract.UseGas(createDataGas) {
+ evm.StateDB.SetCode(address, ret)
+ } else {
+ err = ErrCodeStoreOutOfGas
+ }
+ }
+
+ // When an error was returned by the EVM or when setting the creation code
+ // above we revert to the snapshot and consume any gas remaining. Additionally
+ // when we're in homestead this also counts for code storage gas errors.
+ if maxCodeSizeExceeded || (err != nil && (evm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas)) {
+ evm.StateDB.RevertToSnapshot(snapshot)
+ if err != errExecutionReverted {
+ contract.UseGas(contract.Gas)
+ }
+ }
+ // Assign err if contract code size exceeds the max while the err is still empty.
+ if maxCodeSizeExceeded && err == nil {
+ err = errMaxCodeSizeExceeded
+ }
+ if evm.vmConfig.Debug && evm.depth == 0 {
+ evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)
+ }
+ return ret, address, contract.Gas, err
+
+}
+
+// Create creates a new contract using code as deployment code.
+func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
+ contractAddr = crypto.CreateAddress(caller.Address(), evm.StateDB.GetNonce(caller.Address()))
+ return evm.create(caller, &codeAndHash{code: code}, gas, value, contractAddr)
+}
+
+// Create2 creates a new contract using code as deployment code.
+//
+// The different between Create2 with Create is Create2 uses sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code))[12:]
+// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
+func (evm *EVM) Create2(caller ContractRef, code []byte, gas uint64, endowment *big.Int, salt *big.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
+ codeAndHash := &codeAndHash{code: code}
+ contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), codeAndHash.Hash().Bytes())
+ return evm.create(caller, codeAndHash, gas, endowment, contractAddr)
+}
+
+// ChainConfig returns the environment's chain configuration
+func (evm *EVM) ChainConfig() *params.ChainConfig { return evm.chainConfig }
diff --git a/core/vm/gas.go b/core/vm/gas.go
new file mode 100644
index 0000000..bd8b4f1
--- /dev/null
+++ b/core/vm/gas.go
@@ -0,0 +1,53 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "math/big"
+)
+
+// Gas costs
+const (
+ GasQuickStep uint64 = 2
+ GasFastestStep uint64 = 3
+ GasFastStep uint64 = 5
+ GasMidStep uint64 = 8
+ GasSlowStep uint64 = 10
+ GasExtStep uint64 = 20
+)
+
+// calcGas returns the actual gas cost of the call.
+//
+// The cost of gas was changed during the homestead price change HF.
+// As part of EIP 150 (TangerineWhistle), the returned gas is gas - base * 63 / 64.
+func callGas(isEip150 bool, availableGas, base uint64, callCost *big.Int) (uint64, error) {
+ if isEip150 {
+ availableGas = availableGas - base
+ gas := availableGas - availableGas/64
+ // If the bit length exceeds 64 bit we know that the newly calculated "gas" for EIP150
+ // is smaller than the requested amount. Therefor we return the new gas instead
+ // of returning an error.
+ if !callCost.IsUint64() || gas < callCost.Uint64() {
+ return gas, nil
+ }
+ }
+ if !callCost.IsUint64() {
+ return 0, errGasUintOverflow
+ }
+
+ return callCost.Uint64(), nil
+}
diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go
new file mode 100644
index 0000000..2adaf85
--- /dev/null
+++ b/core/vm/gas_table.go
@@ -0,0 +1,441 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "errors"
+
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+// memoryGasCost calculates the quadratic gas for memory expansion. It does so
+// only for the memory region that is expanded, not the total memory.
+func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) {
+ if newMemSize == 0 {
+ return 0, nil
+ }
+ // The maximum that will fit in a uint64 is max_word_count - 1. Anything above
+ // that will result in an overflow. Additionally, a newMemSize which results in
+ // a newMemSizeWords larger than 0xFFFFFFFF will cause the square operation to
+ // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used
+ // without overflowing the gas calculation.
+ if newMemSize > 0x1FFFFFFFE0 {
+ return 0, errGasUintOverflow
+ }
+ newMemSizeWords := toWordSize(newMemSize)
+ newMemSize = newMemSizeWords * 32
+
+ if newMemSize > uint64(mem.Len()) {
+ square := newMemSizeWords * newMemSizeWords
+ linCoef := newMemSizeWords * params.MemoryGas
+ quadCoef := square / params.QuadCoeffDiv
+ newTotalFee := linCoef + quadCoef
+
+ fee := newTotalFee - mem.lastGasCost
+ mem.lastGasCost = newTotalFee
+
+ return fee, nil
+ }
+ return 0, nil
+}
+
+// memoryCopierGas creates the gas functions for the following opcodes, and takes
+// the stack position of the operand which determines the size of the data to copy
+// as argument:
+// CALLDATACOPY (stack position 2)
+// CODECOPY (stack position 2)
+// EXTCODECOPY (stack poition 3)
+// RETURNDATACOPY (stack position 2)
+func memoryCopierGas(stackpos int) gasFunc {
+ return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ // Gas for expanding the memory
+ gas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ // And gas for copying data, charged per word at param.CopyGas
+ words, overflow := bigUint64(stack.Back(stackpos))
+ if overflow {
+ return 0, errGasUintOverflow
+ }
+
+ if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
+ return 0, errGasUintOverflow
+ }
+
+ if gas, overflow = math.SafeAdd(gas, words); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+ }
+}
+
+var (
+ gasCallDataCopy = memoryCopierGas(2)
+ gasCodeCopy = memoryCopierGas(2)
+ gasExtCodeCopy = memoryCopierGas(3)
+ gasReturnDataCopy = memoryCopierGas(2)
+)
+
+func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ var (
+ y, x = stack.Back(1), stack.Back(0)
+ current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
+ )
+ // The legacy gas metering only takes into consideration the current state
+ // Legacy rules should be applied if we are in Petersburg (removal of EIP-1283)
+ // OR Constantinople is not active
+ if evm.chainRules.IsPetersburg || !evm.chainRules.IsConstantinople {
+ // This checks for 3 scenario's and calculates gas accordingly:
+ //
+ // 1. From a zero-value address to a non-zero value (NEW VALUE)
+ // 2. From a non-zero value address to a zero-value address (DELETE)
+ // 3. From a non-zero to a non-zero (CHANGE)
+ switch {
+ case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0
+ return params.SstoreSetGas, nil
+ case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0
+ evm.StateDB.AddRefund(params.SstoreRefundGas)
+ return params.SstoreClearGas, nil
+ default: // non 0 => non 0 (or 0 => 0)
+ return params.SstoreResetGas, nil
+ }
+ }
+ // The new gas metering is based on net gas costs (EIP-1283):
+ //
+ // 1. If current value equals new value (this is a no-op), 200 gas is deducted.
+ // 2. If current value does not equal new value
+ // 2.1. If original value equals current value (this storage slot has not been changed by the current execution context)
+ // 2.1.1. If original value is 0, 20000 gas is deducted.
+ // 2.1.2. Otherwise, 5000 gas is deducted. If new value is 0, add 15000 gas to refund counter.
+ // 2.2. If original value does not equal current value (this storage slot is dirty), 200 gas is deducted. Apply both of the following clauses.
+ // 2.2.1. If original value is not 0
+ // 2.2.1.1. If current value is 0 (also means that new value is not 0), remove 15000 gas from refund counter. We can prove that refund counter will never go below 0.
+ // 2.2.1.2. If new value is 0 (also means that current value is not 0), add 15000 gas to refund counter.
+ // 2.2.2. If original value equals new value (this storage slot is reset)
+ // 2.2.2.1. If original value is 0, add 19800 gas to refund counter.
+ // 2.2.2.2. Otherwise, add 4800 gas to refund counter.
+ value := common.BigToHash(y)
+ if current == value { // noop (1)
+ return params.NetSstoreNoopGas, nil
+ }
+ original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x))
+ if original == current {
+ if original == (common.Hash{}) { // create slot (2.1.1)
+ return params.NetSstoreInitGas, nil
+ }
+ if value == (common.Hash{}) { // delete slot (2.1.2b)
+ evm.StateDB.AddRefund(params.NetSstoreClearRefund)
+ }
+ return params.NetSstoreCleanGas, nil // write existing slot (2.1.2)
+ }
+ if original != (common.Hash{}) {
+ if current == (common.Hash{}) { // recreate slot (2.2.1.1)
+ evm.StateDB.SubRefund(params.NetSstoreClearRefund)
+ } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
+ evm.StateDB.AddRefund(params.NetSstoreClearRefund)
+ }
+ }
+ if original == value {
+ if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
+ evm.StateDB.AddRefund(params.NetSstoreResetClearRefund)
+ } else { // reset to original existing slot (2.2.2.2)
+ evm.StateDB.AddRefund(params.NetSstoreResetRefund)
+ }
+ }
+ return params.NetSstoreDirtyGas, nil
+}
+
+// 0. If *gasleft* is less than or equal to 2300, fail the current call.
+// 1. If current value equals new value (this is a no-op), SSTORE_NOOP_GAS gas is deducted.
+// 2. If current value does not equal new value:
+// 2.1. If original value equals current value (this storage slot has not been changed by the current execution context):
+// 2.1.1. If original value is 0, SSTORE_INIT_GAS gas is deducted.
+// 2.1.2. Otherwise, SSTORE_CLEAN_GAS gas is deducted. If new value is 0, add SSTORE_CLEAR_REFUND to refund counter.
+// 2.2. If original value does not equal current value (this storage slot is dirty), SSTORE_DIRTY_GAS gas is deducted. Apply both of the following clauses:
+// 2.2.1. If original value is not 0:
+// 2.2.1.1. If current value is 0 (also means that new value is not 0), subtract SSTORE_CLEAR_REFUND gas from refund counter. We can prove that refund counter will never go below 0.
+// 2.2.1.2. If new value is 0 (also means that current value is not 0), add SSTORE_CLEAR_REFUND gas to refund counter.
+// 2.2.2. If original value equals new value (this storage slot is reset):
+// 2.2.2.1. If original value is 0, add SSTORE_INIT_REFUND to refund counter.
+// 2.2.2.2. Otherwise, add SSTORE_CLEAN_REFUND gas to refund counter.
+func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ // If we fail the minimum gas availability invariant, fail (0)
+ if contract.Gas <= params.SstoreSentryGasEIP2200 {
+ return 0, errors.New("not enough gas for reentrancy sentry")
+ }
+ // Gas sentry honoured, do the actual gas calculation based on the stored value
+ var (
+ y, x = stack.Back(1), stack.Back(0)
+ current = evm.StateDB.GetState(contract.Address(), common.BigToHash(x))
+ )
+ value := common.BigToHash(y)
+
+ if current == value { // noop (1)
+ return params.SstoreNoopGasEIP2200, nil
+ }
+ original := evm.StateDB.GetCommittedState(contract.Address(), common.BigToHash(x))
+ if original == current {
+ if original == (common.Hash{}) { // create slot (2.1.1)
+ return params.SstoreInitGasEIP2200, nil
+ }
+ if value == (common.Hash{}) { // delete slot (2.1.2b)
+ evm.StateDB.AddRefund(params.SstoreClearRefundEIP2200)
+ }
+ return params.SstoreCleanGasEIP2200, nil // write existing slot (2.1.2)
+ }
+ if original != (common.Hash{}) {
+ if current == (common.Hash{}) { // recreate slot (2.2.1.1)
+ evm.StateDB.SubRefund(params.SstoreClearRefundEIP2200)
+ } else if value == (common.Hash{}) { // delete slot (2.2.1.2)
+ evm.StateDB.AddRefund(params.SstoreClearRefundEIP2200)
+ }
+ }
+ if original == value {
+ if original == (common.Hash{}) { // reset to original inexistent slot (2.2.2.1)
+ evm.StateDB.AddRefund(params.SstoreInitRefundEIP2200)
+ } else { // reset to original existing slot (2.2.2.2)
+ evm.StateDB.AddRefund(params.SstoreCleanRefundEIP2200)
+ }
+ }
+ return params.SstoreDirtyGasEIP2200, nil // dirty update (2.2)
+}
+
+func makeGasLog(n uint64) gasFunc {
+ return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ requestedSize, overflow := bigUint64(stack.Back(1))
+ if overflow {
+ return 0, errGasUintOverflow
+ }
+
+ gas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+
+ if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {
+ return 0, errGasUintOverflow
+ }
+
+ var memorySizeGas uint64
+ if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+ }
+}
+
+func gasSha3(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ gas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ wordGas, overflow := bigUint64(stack.Back(1))
+ if overflow {
+ return 0, errGasUintOverflow
+ }
+ if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+// pureMemoryGascost is used by several operations, which aside from their
+// static cost have a dynamic cost which is solely based on the memory
+// expansion
+func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ return memoryGasCost(mem, memorySize)
+}
+
+var (
+ gasReturn = pureMemoryGascost
+ gasRevert = pureMemoryGascost
+ gasMLoad = pureMemoryGascost
+ gasMStore8 = pureMemoryGascost
+ gasMStore = pureMemoryGascost
+ gasCreate = pureMemoryGascost
+)
+
+func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ gas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ wordGas, overflow := bigUint64(stack.Back(2))
+ if overflow {
+ return 0, errGasUintOverflow
+ }
+ if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
+
+ var (
+ gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas
+ overflow bool
+ )
+ if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8)
+
+ var (
+ gas = expByteLen * params.ExpByteEIP158 // no overflow check required. Max is 256 * ExpByte gas
+ overflow bool
+ )
+ if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ var (
+ gas uint64
+ transfersValue = stack.Back(2).Sign() != 0
+ address = common.BigToAddress(stack.Back(1))
+ )
+ if evm.chainRules.IsEIP158 {
+ if transfersValue && evm.StateDB.Empty(address) {
+ gas += params.CallNewAccountGas
+ }
+ } else if !evm.StateDB.Exist(address) {
+ gas += params.CallNewAccountGas
+ }
+ if transfersValue {
+ gas += params.CallValueTransferGas
+ }
+ memoryGas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ var overflow bool
+ if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
+ return 0, errGasUintOverflow
+ }
+
+ evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
+ if err != nil {
+ return 0, err
+ }
+ if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ memoryGas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ var (
+ gas uint64
+ overflow bool
+ )
+ if stack.Back(2).Sign() != 0 {
+ gas += params.CallValueTransferGas
+ }
+ if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
+ return 0, errGasUintOverflow
+ }
+ evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
+ if err != nil {
+ return 0, err
+ }
+ if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+func gasDelegateCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ gas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
+ if err != nil {
+ return 0, err
+ }
+ var overflow bool
+ if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+func gasStaticCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ gas, err := memoryGasCost(mem, memorySize)
+ if err != nil {
+ return 0, err
+ }
+ evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, gas, stack.Back(0))
+ if err != nil {
+ return 0, err
+ }
+ var overflow bool
+ if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
+ return 0, errGasUintOverflow
+ }
+ return gas, nil
+}
+
+func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
+ var gas uint64
+ // EIP150 homestead gas reprice fork:
+ if evm.chainRules.IsEIP150 {
+ gas = params.SelfdestructGasEIP150
+ var address = common.BigToAddress(stack.Back(0))
+
+ if evm.chainRules.IsEIP158 {
+ // if empty and transfers value
+ if evm.StateDB.Empty(address) && evm.StateDB.GetBalance(contract.Address()).Sign() != 0 {
+ gas += params.CreateBySelfdestructGas
+ }
+ } else if !evm.StateDB.Exist(address) {
+ gas += params.CreateBySelfdestructGas
+ }
+ }
+
+ if !evm.StateDB.HasSuicided(contract.Address()) {
+ evm.StateDB.AddRefund(params.SelfdestructRefundGas)
+ }
+ return gas, nil
+}
diff --git a/core/vm/gen_structlog.go b/core/vm/gen_structlog.go
new file mode 100644
index 0000000..038841c
--- /dev/null
+++ b/core/vm/gen_structlog.go
@@ -0,0 +1,111 @@
+// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
+
+package vm
+
+import (
+ "encoding/json"
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+var _ = (*structLogMarshaling)(nil)
+
+// MarshalJSON marshals as JSON.
+func (s StructLog) MarshalJSON() ([]byte, error) {
+ type StructLog struct {
+ Pc uint64 `json:"pc"`
+ Op OpCode `json:"op"`
+ Gas math.HexOrDecimal64 `json:"gas"`
+ GasCost math.HexOrDecimal64 `json:"gasCost"`
+ Memory hexutil.Bytes `json:"memory"`
+ MemorySize int `json:"memSize"`
+ Stack []*math.HexOrDecimal256 `json:"stack"`
+ Storage map[common.Hash]common.Hash `json:"-"`
+ Depth int `json:"depth"`
+ RefundCounter uint64 `json:"refund"`
+ Err error `json:"-"`
+ OpName string `json:"opName"`
+ ErrorString string `json:"error"`
+ }
+ var enc StructLog
+ enc.Pc = s.Pc
+ enc.Op = s.Op
+ enc.Gas = math.HexOrDecimal64(s.Gas)
+ enc.GasCost = math.HexOrDecimal64(s.GasCost)
+ enc.Memory = s.Memory
+ enc.MemorySize = s.MemorySize
+ if s.Stack != nil {
+ enc.Stack = make([]*math.HexOrDecimal256, len(s.Stack))
+ for k, v := range s.Stack {
+ enc.Stack[k] = (*math.HexOrDecimal256)(v)
+ }
+ }
+ enc.Storage = s.Storage
+ enc.Depth = s.Depth
+ enc.RefundCounter = s.RefundCounter
+ enc.Err = s.Err
+ enc.OpName = s.OpName()
+ enc.ErrorString = s.ErrorString()
+ return json.Marshal(&enc)
+}
+
+// UnmarshalJSON unmarshals from JSON.
+func (s *StructLog) UnmarshalJSON(input []byte) error {
+ type StructLog struct {
+ Pc *uint64 `json:"pc"`
+ Op *OpCode `json:"op"`
+ Gas *math.HexOrDecimal64 `json:"gas"`
+ GasCost *math.HexOrDecimal64 `json:"gasCost"`
+ Memory *hexutil.Bytes `json:"memory"`
+ MemorySize *int `json:"memSize"`
+ Stack []*math.HexOrDecimal256 `json:"stack"`
+ Storage map[common.Hash]common.Hash `json:"-"`
+ Depth *int `json:"depth"`
+ RefundCounter *uint64 `json:"refund"`
+ Err error `json:"-"`
+ }
+ var dec StructLog
+ if err := json.Unmarshal(input, &dec); err != nil {
+ return err
+ }
+ if dec.Pc != nil {
+ s.Pc = *dec.Pc
+ }
+ if dec.Op != nil {
+ s.Op = *dec.Op
+ }
+ if dec.Gas != nil {
+ s.Gas = uint64(*dec.Gas)
+ }
+ if dec.GasCost != nil {
+ s.GasCost = uint64(*dec.GasCost)
+ }
+ if dec.Memory != nil {
+ s.Memory = *dec.Memory
+ }
+ if dec.MemorySize != nil {
+ s.MemorySize = *dec.MemorySize
+ }
+ if dec.Stack != nil {
+ s.Stack = make([]*big.Int, len(dec.Stack))
+ for k, v := range dec.Stack {
+ s.Stack[k] = (*big.Int)(v)
+ }
+ }
+ if dec.Storage != nil {
+ s.Storage = dec.Storage
+ }
+ if dec.Depth != nil {
+ s.Depth = *dec.Depth
+ }
+ if dec.RefundCounter != nil {
+ s.RefundCounter = *dec.RefundCounter
+ }
+ if dec.Err != nil {
+ s.Err = dec.Err
+ }
+ return nil
+}
diff --git a/core/vm/instructions.go b/core/vm/instructions.go
new file mode 100644
index 0000000..91233b3
--- /dev/null
+++ b/core/vm/instructions.go
@@ -0,0 +1,1011 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "errors"
+ "fmt"
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+ "golang.org/x/crypto/sha3"
+)
+
+var (
+ bigZero = new(big.Int)
+ tt255 = math.BigPow(2, 255)
+ errWriteProtection = errors.New("evm: write protection")
+ errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
+ errExecutionReverted = errors.New("evm: execution reverted")
+ errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded")
+ errInvalidJump = errors.New("evm: invalid jump destination")
+)
+
+func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ math.U256(y.Add(x, y))
+
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opSub(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ math.U256(y.Sub(x, y))
+
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opMul(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(math.U256(x.Mul(x, y)))
+
+ interpreter.intPool.put(y)
+
+ return nil, nil
+}
+
+func opDiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ if y.Sign() != 0 {
+ math.U256(y.Div(x, y))
+ } else {
+ y.SetUint64(0)
+ }
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opSdiv(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := math.S256(stack.pop()), math.S256(stack.pop())
+ res := interpreter.intPool.getZero()
+
+ if y.Sign() == 0 || x.Sign() == 0 {
+ stack.push(res)
+ } else {
+ if x.Sign() != y.Sign() {
+ res.Div(x.Abs(x), y.Abs(y))
+ res.Neg(res)
+ } else {
+ res.Div(x.Abs(x), y.Abs(y))
+ }
+ stack.push(math.U256(res))
+ }
+ interpreter.intPool.put(x, y)
+ return nil, nil
+}
+
+func opMod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.pop()
+ if y.Sign() == 0 {
+ stack.push(x.SetUint64(0))
+ } else {
+ stack.push(math.U256(x.Mod(x, y)))
+ }
+ interpreter.intPool.put(y)
+ return nil, nil
+}
+
+func opSmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := math.S256(stack.pop()), math.S256(stack.pop())
+ res := interpreter.intPool.getZero()
+
+ if y.Sign() == 0 {
+ stack.push(res)
+ } else {
+ if x.Sign() < 0 {
+ res.Mod(x.Abs(x), y.Abs(y))
+ res.Neg(res)
+ } else {
+ res.Mod(x.Abs(x), y.Abs(y))
+ }
+ stack.push(math.U256(res))
+ }
+ interpreter.intPool.put(x, y)
+ return nil, nil
+}
+
+func opExp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ base, exponent := stack.pop(), stack.pop()
+ // some shortcuts
+ cmpToOne := exponent.Cmp(big1)
+ if cmpToOne < 0 { // Exponent is zero
+ // x ^ 0 == 1
+ stack.push(base.SetUint64(1))
+ } else if base.Sign() == 0 {
+ // 0 ^ y, if y != 0, == 0
+ stack.push(base.SetUint64(0))
+ } else if cmpToOne == 0 { // Exponent is one
+ // x ^ 1 == x
+ stack.push(base)
+ } else {
+ stack.push(math.Exp(base, exponent))
+ interpreter.intPool.put(base)
+ }
+ interpreter.intPool.put(exponent)
+ return nil, nil
+}
+
+func opSignExtend(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ back := stack.pop()
+ if back.Cmp(big.NewInt(31)) < 0 {
+ bit := uint(back.Uint64()*8 + 7)
+ num := stack.pop()
+ mask := back.Lsh(common.Big1, bit)
+ mask.Sub(mask, common.Big1)
+ if num.Bit(int(bit)) > 0 {
+ num.Or(num, mask.Not(mask))
+ } else {
+ num.And(num, mask)
+ }
+
+ stack.push(math.U256(num))
+ }
+
+ interpreter.intPool.put(back)
+ return nil, nil
+}
+
+func opNot(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x := stack.peek()
+ math.U256(x.Not(x))
+ return nil, nil
+}
+
+func opLt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ if x.Cmp(y) < 0 {
+ y.SetUint64(1)
+ } else {
+ y.SetUint64(0)
+ }
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opGt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ if x.Cmp(y) > 0 {
+ y.SetUint64(1)
+ } else {
+ y.SetUint64(0)
+ }
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opSlt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+
+ xSign := x.Cmp(tt255)
+ ySign := y.Cmp(tt255)
+
+ switch {
+ case xSign >= 0 && ySign < 0:
+ y.SetUint64(1)
+
+ case xSign < 0 && ySign >= 0:
+ y.SetUint64(0)
+
+ default:
+ if x.Cmp(y) < 0 {
+ y.SetUint64(1)
+ } else {
+ y.SetUint64(0)
+ }
+ }
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opSgt(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+
+ xSign := x.Cmp(tt255)
+ ySign := y.Cmp(tt255)
+
+ switch {
+ case xSign >= 0 && ySign < 0:
+ y.SetUint64(0)
+
+ case xSign < 0 && ySign >= 0:
+ y.SetUint64(1)
+
+ default:
+ if x.Cmp(y) > 0 {
+ y.SetUint64(1)
+ } else {
+ y.SetUint64(0)
+ }
+ }
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opEq(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ if x.Cmp(y) == 0 {
+ y.SetUint64(1)
+ } else {
+ y.SetUint64(0)
+ }
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opIszero(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x := stack.peek()
+ if x.Sign() > 0 {
+ x.SetUint64(0)
+ } else {
+ x.SetUint64(1)
+ }
+ return nil, nil
+}
+
+func opAnd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(x.And(x, y))
+
+ interpreter.intPool.put(y)
+ return nil, nil
+}
+
+func opOr(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ y.Or(x, y)
+
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opXor(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y := stack.pop(), stack.peek()
+ y.Xor(x, y)
+
+ interpreter.intPool.put(x)
+ return nil, nil
+}
+
+func opByte(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ th, val := stack.pop(), stack.peek()
+ if th.Cmp(common.Big32) < 0 {
+ b := math.Byte(val, 32, int(th.Int64()))
+ val.SetUint64(uint64(b))
+ } else {
+ val.SetUint64(0)
+ }
+ interpreter.intPool.put(th)
+ return nil, nil
+}
+
+func opAddmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y, z := stack.pop(), stack.pop(), stack.pop()
+ if z.Cmp(bigZero) > 0 {
+ x.Add(x, y)
+ x.Mod(x, z)
+ stack.push(math.U256(x))
+ } else {
+ stack.push(x.SetUint64(0))
+ }
+ interpreter.intPool.put(y, z)
+ return nil, nil
+}
+
+func opMulmod(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ x, y, z := stack.pop(), stack.pop(), stack.pop()
+ if z.Cmp(bigZero) > 0 {
+ x.Mul(x, y)
+ x.Mod(x, z)
+ stack.push(math.U256(x))
+ } else {
+ stack.push(x.SetUint64(0))
+ }
+ interpreter.intPool.put(y, z)
+ return nil, nil
+}
+
+// opSHL implements Shift Left
+// The SHL instruction (shift left) pops 2 values from the stack, first arg1 and then arg2,
+// and pushes on the stack arg2 shifted to the left by arg1 number of bits.
+func opSHL(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
+ shift, value := math.U256(stack.pop()), math.U256(stack.peek())
+ defer interpreter.intPool.put(shift) // First operand back into the pool
+
+ if shift.Cmp(common.Big256) >= 0 {
+ value.SetUint64(0)
+ return nil, nil
+ }
+ n := uint(shift.Uint64())
+ math.U256(value.Lsh(value, n))
+
+ return nil, nil
+}
+
+// opSHR implements Logical Shift Right
+// The SHR instruction (logical shift right) pops 2 values from the stack, first arg1 and then arg2,
+// and pushes on the stack arg2 shifted to the right by arg1 number of bits with zero fill.
+func opSHR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Note, second operand is left in the stack; accumulate result into it, and no need to push it afterwards
+ shift, value := math.U256(stack.pop()), math.U256(stack.peek())
+ defer interpreter.intPool.put(shift) // First operand back into the pool
+
+ if shift.Cmp(common.Big256) >= 0 {
+ value.SetUint64(0)
+ return nil, nil
+ }
+ n := uint(shift.Uint64())
+ math.U256(value.Rsh(value, n))
+
+ return nil, nil
+}
+
+// opSAR implements Arithmetic Shift Right
+// The SAR instruction (arithmetic shift right) pops 2 values from the stack, first arg1 and then arg2,
+// and pushes on the stack arg2 shifted to the right by arg1 number of bits with sign extension.
+func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Note, S256 returns (potentially) a new bigint, so we're popping, not peeking this one
+ shift, value := math.U256(stack.pop()), math.S256(stack.pop())
+ defer interpreter.intPool.put(shift) // First operand back into the pool
+
+ if shift.Cmp(common.Big256) >= 0 {
+ if value.Sign() >= 0 {
+ value.SetUint64(0)
+ } else {
+ value.SetInt64(-1)
+ }
+ stack.push(math.U256(value))
+ return nil, nil
+ }
+ n := uint(shift.Uint64())
+ value.Rsh(value, n)
+ stack.push(math.U256(value))
+
+ return nil, nil
+}
+
+func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ offset, size := stack.pop(), stack.pop()
+ data := memory.Get(offset.Int64(), size.Int64())
+
+ if interpreter.hasher == nil {
+ interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
+ } else {
+ interpreter.hasher.Reset()
+ }
+ interpreter.hasher.Write(data)
+ interpreter.hasher.Read(interpreter.hasherBuf[:])
+
+ evm := interpreter.evm
+ if evm.vmConfig.EnablePreimageRecording {
+ evm.StateDB.AddPreimage(interpreter.hasherBuf, data)
+ }
+ stack.push(interpreter.intPool.get().SetBytes(interpreter.hasherBuf[:]))
+
+ interpreter.intPool.put(offset, size)
+ return nil, nil
+}
+
+func opAddress(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetBytes(contract.Address().Bytes()))
+ return nil, nil
+}
+
+func opBalance(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ slot := stack.peek()
+ slot.Set(interpreter.evm.StateDB.GetBalance(common.BigToAddress(slot)))
+ return nil, nil
+}
+
+func opBalanceMultiCoin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ addr, cid := stack.pop(), stack.pop()
+ fmt.Println(addr, cid)
+ stack.push(interpreter.evm.StateDB.GetBalanceMultiCoin(common.BigToAddress(addr), common.BigToHash(cid)))
+ return nil, nil
+}
+
+func opOrigin(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetBytes(interpreter.evm.Origin.Bytes()))
+ return nil, nil
+}
+
+func opCaller(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetBytes(contract.Caller().Bytes()))
+ return nil, nil
+}
+
+func opCallValue(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().Set(contract.value))
+ return nil, nil
+}
+
+func opCallDataLoad(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetBytes(getDataBig(contract.Input, stack.pop(), big32)))
+ return nil, nil
+}
+
+func opCallDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetInt64(int64(len(contract.Input))))
+ return nil, nil
+}
+
+func opCallDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ memOffset = stack.pop()
+ dataOffset = stack.pop()
+ length = stack.pop()
+ )
+ memory.Set(memOffset.Uint64(), length.Uint64(), getDataBig(contract.Input, dataOffset, length))
+
+ interpreter.intPool.put(memOffset, dataOffset, length)
+ return nil, nil
+}
+
+func opReturnDataSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetUint64(uint64(len(interpreter.returnData))))
+ return nil, nil
+}
+
+func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ memOffset = stack.pop()
+ dataOffset = stack.pop()
+ length = stack.pop()
+
+ end = interpreter.intPool.get().Add(dataOffset, length)
+ )
+ defer interpreter.intPool.put(memOffset, dataOffset, length, end)
+
+ if !end.IsUint64() || uint64(len(interpreter.returnData)) < end.Uint64() {
+ return nil, errReturnDataOutOfBounds
+ }
+ memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
+
+ return nil, nil
+}
+
+func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ slot := stack.peek()
+ slot.SetUint64(uint64(interpreter.evm.StateDB.GetCodeSize(common.BigToAddress(slot))))
+
+ return nil, nil
+}
+
+func opCodeSize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ l := interpreter.intPool.get().SetInt64(int64(len(contract.Code)))
+ stack.push(l)
+
+ return nil, nil
+}
+
+func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ memOffset = stack.pop()
+ codeOffset = stack.pop()
+ length = stack.pop()
+ )
+ codeCopy := getDataBig(contract.Code, codeOffset, length)
+ memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
+
+ interpreter.intPool.put(memOffset, codeOffset, length)
+ return nil, nil
+}
+
+func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ addr = common.BigToAddress(stack.pop())
+ memOffset = stack.pop()
+ codeOffset = stack.pop()
+ length = stack.pop()
+ )
+ codeCopy := getDataBig(interpreter.evm.StateDB.GetCode(addr), codeOffset, length)
+ memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
+
+ interpreter.intPool.put(memOffset, codeOffset, length)
+ return nil, nil
+}
+
+// opExtCodeHash returns the code hash of a specified account.
+// There are several cases when the function is called, while we can relay everything
+// to `state.GetCodeHash` function to ensure the correctness.
+// (1) Caller tries to get the code hash of a normal contract account, state
+// should return the relative code hash and set it as the result.
+//
+// (2) Caller tries to get the code hash of a non-existent account, state should
+// return common.Hash{} and zero will be set as the result.
+//
+// (3) Caller tries to get the code hash for an account without contract code,
+// state should return emptyCodeHash(0xc5d246...) as the result.
+//
+// (4) Caller tries to get the code hash of a precompiled account, the result
+// should be zero or emptyCodeHash.
+//
+// It is worth noting that in order to avoid unnecessary create and clean,
+// all precompile accounts on mainnet have been transferred 1 wei, so the return
+// here should be emptyCodeHash.
+// If the precompile account is not transferred any amount on a private or
+// customized chain, the return value will be zero.
+//
+// (5) Caller tries to get the code hash for an account which is marked as suicided
+// in the current transaction, the code hash of this account should be returned.
+//
+// (6) Caller tries to get the code hash for an account which is marked as deleted,
+// this account should be regarded as a non-existent account and zero should be returned.
+func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ slot := stack.peek()
+ address := common.BigToAddress(slot)
+ if interpreter.evm.StateDB.Empty(address) {
+ slot.SetUint64(0)
+ } else {
+ slot.SetBytes(interpreter.evm.StateDB.GetCodeHash(address).Bytes())
+ }
+ return nil, nil
+}
+
+func opGasprice(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().Set(interpreter.evm.GasPrice))
+ return nil, nil
+}
+
+func opBlockhash(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ num := stack.pop()
+
+ n := interpreter.intPool.get().Sub(interpreter.evm.BlockNumber, common.Big257)
+ if num.Cmp(n) > 0 && num.Cmp(interpreter.evm.BlockNumber) < 0 {
+ stack.push(interpreter.evm.GetHash(num.Uint64()).Big())
+ } else {
+ stack.push(interpreter.intPool.getZero())
+ }
+ interpreter.intPool.put(num, n)
+ return nil, nil
+}
+
+func opCoinbase(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetBytes(interpreter.evm.Coinbase.Bytes()))
+ return nil, nil
+}
+
+func opTimestamp(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Time)))
+ return nil, nil
+}
+
+func opNumber(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.BlockNumber)))
+ return nil, nil
+}
+
+func opDifficulty(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(math.U256(interpreter.intPool.get().Set(interpreter.evm.Difficulty)))
+ return nil, nil
+}
+
+func opGasLimit(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(math.U256(interpreter.intPool.get().SetUint64(interpreter.evm.GasLimit)))
+ return nil, nil
+}
+
+func opPop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ interpreter.intPool.put(stack.pop())
+ return nil, nil
+}
+
+func opMload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ offset := stack.pop()
+ val := interpreter.intPool.get().SetBytes(memory.Get(offset.Int64(), 32))
+ stack.push(val)
+
+ interpreter.intPool.put(offset)
+ return nil, nil
+}
+
+func opMstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // pop value of the stack
+ mStart, val := stack.pop(), stack.pop()
+ memory.Set32(mStart.Uint64(), val)
+
+ interpreter.intPool.put(mStart, val)
+ return nil, nil
+}
+
+func opMstore8(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ off, val := stack.pop().Int64(), stack.pop().Int64()
+ memory.store[off] = byte(val & 0xff)
+
+ return nil, nil
+}
+
+func opSload(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ loc := stack.peek()
+ val := interpreter.evm.StateDB.GetState(contract.Address(), common.BigToHash(loc))
+ loc.SetBytes(val.Bytes())
+ return nil, nil
+}
+
+func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ loc := common.BigToHash(stack.pop())
+ val := stack.pop()
+ if err := interpreter.evm.StateDB.SetState(contract.Address(), loc, common.BigToHash(val)); err != nil {
+ return nil, err
+ }
+ interpreter.intPool.put(val)
+ return nil, nil
+}
+
+func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ pos := stack.pop()
+ if !contract.validJumpdest(pos) {
+ return nil, errInvalidJump
+ }
+ *pc = pos.Uint64()
+
+ interpreter.intPool.put(pos)
+ return nil, nil
+}
+
+func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ pos, cond := stack.pop(), stack.pop()
+ if cond.Sign() != 0 {
+ if !contract.validJumpdest(pos) {
+ return nil, errInvalidJump
+ }
+ *pc = pos.Uint64()
+ } else {
+ *pc++
+ }
+
+ interpreter.intPool.put(pos, cond)
+ return nil, nil
+}
+
+func opJumpdest(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ return nil, nil
+}
+
+func opPc(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetUint64(*pc))
+ return nil, nil
+}
+
+func opMsize(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetInt64(int64(memory.Len())))
+ return nil, nil
+}
+
+func opGas(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.push(interpreter.intPool.get().SetUint64(contract.Gas))
+ return nil, nil
+}
+
+func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ value = stack.pop()
+ offset, size = stack.pop(), stack.pop()
+ input = memory.Get(offset.Int64(), size.Int64())
+ gas = contract.Gas
+ )
+ if interpreter.evm.chainRules.IsEIP150 {
+ gas -= gas / 64
+ }
+
+ contract.UseGas(gas)
+ res, addr, returnGas, suberr := interpreter.evm.Create(contract, input, gas, value)
+ // Push item on the stack based on the returned error. If the ruleset is
+ // homestead we must check for CodeStoreOutOfGasError (homestead only
+ // rule) and treat as an error, if the ruleset is frontier we must
+ // ignore this error and pretend the operation was successful.
+ if interpreter.evm.chainRules.IsHomestead && suberr == ErrCodeStoreOutOfGas {
+ stack.push(interpreter.intPool.getZero())
+ } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
+ stack.push(interpreter.intPool.getZero())
+ } else {
+ stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
+ }
+ contract.Gas += returnGas
+ interpreter.intPool.put(value, offset, size)
+
+ if suberr == errExecutionReverted {
+ return res, nil
+ }
+ return nil, nil
+}
+
+func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ endowment = stack.pop()
+ offset, size = stack.pop(), stack.pop()
+ salt = stack.pop()
+ input = memory.Get(offset.Int64(), size.Int64())
+ gas = contract.Gas
+ )
+
+ // Apply EIP150
+ gas -= gas / 64
+ contract.UseGas(gas)
+ res, addr, returnGas, suberr := interpreter.evm.Create2(contract, input, gas, endowment, salt)
+ // Push item on the stack based on the returned error.
+ if suberr != nil {
+ stack.push(interpreter.intPool.getZero())
+ } else {
+ stack.push(interpreter.intPool.get().SetBytes(addr.Bytes()))
+ }
+ contract.Gas += returnGas
+ interpreter.intPool.put(endowment, offset, size, salt)
+
+ if suberr == errExecutionReverted {
+ return res, nil
+ }
+ return nil, nil
+}
+
+func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Pop gas. The actual gas in interpreter.evm.callGasTemp.
+ interpreter.intPool.put(stack.pop())
+ gas := interpreter.evm.callGasTemp
+ // Pop other call parameters.
+ addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
+ toAddr := common.BigToAddress(addr)
+ value = math.U256(value)
+ // Get the arguments from the memory.
+ args := memory.Get(inOffset.Int64(), inSize.Int64())
+
+ if value.Sign() != 0 {
+ gas += params.CallStipend
+ }
+ ret, returnGas, err := interpreter.evm.Call(contract, toAddr, args, gas, value)
+ if err != nil {
+ stack.push(interpreter.intPool.getZero())
+ } else {
+ stack.push(interpreter.intPool.get().SetUint64(1))
+ }
+ if err == nil || err == errExecutionReverted {
+ memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
+ }
+ contract.Gas += returnGas
+
+ interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
+ return ret, nil
+}
+
+func opCallExpert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Pop gas. The actual gas in interpreter.evm.callGasTemp.
+ interpreter.intPool.put(stack.pop())
+ gas := interpreter.evm.callGasTemp
+ // Pop other call parameters.
+ addr, value, cid, value2, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
+ toAddr := common.BigToAddress(addr)
+ coinID := common.BigToHash(cid)
+ value = math.U256(value)
+ value2 = math.U256(value2)
+ // Get the arguments from the memory.
+ args := memory.Get(inOffset.Int64(), inSize.Int64())
+
+ if value.Sign() != 0 {
+ gas += params.CallStipend
+ }
+ ret, returnGas, err := interpreter.evm.CallExpert(contract, toAddr, args, gas, value, &coinID, value2)
+ if err != nil {
+ stack.push(interpreter.intPool.getZero())
+ } else {
+ stack.push(interpreter.intPool.get().SetUint64(1))
+ }
+ if err == nil || err == errExecutionReverted {
+ memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
+ }
+ contract.Gas += returnGas
+
+ interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
+ return ret, nil
+}
+
+func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
+ interpreter.intPool.put(stack.pop())
+ gas := interpreter.evm.callGasTemp
+ // Pop other call parameters.
+ addr, value, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
+ toAddr := common.BigToAddress(addr)
+ value = math.U256(value)
+ // Get arguments from the memory.
+ args := memory.Get(inOffset.Int64(), inSize.Int64())
+
+ if value.Sign() != 0 {
+ gas += params.CallStipend
+ }
+ ret, returnGas, err := interpreter.evm.CallCode(contract, toAddr, args, gas, value)
+ if err != nil {
+ stack.push(interpreter.intPool.getZero())
+ } else {
+ stack.push(interpreter.intPool.get().SetUint64(1))
+ }
+ if err == nil || err == errExecutionReverted {
+ memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
+ }
+ contract.Gas += returnGas
+
+ interpreter.intPool.put(addr, value, inOffset, inSize, retOffset, retSize)
+ return ret, nil
+}
+
+func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
+ interpreter.intPool.put(stack.pop())
+ gas := interpreter.evm.callGasTemp
+ // Pop other call parameters.
+ addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
+ toAddr := common.BigToAddress(addr)
+ // Get arguments from the memory.
+ args := memory.Get(inOffset.Int64(), inSize.Int64())
+
+ ret, returnGas, err := interpreter.evm.DelegateCall(contract, toAddr, args, gas)
+ if err != nil {
+ stack.push(interpreter.intPool.getZero())
+ } else {
+ stack.push(interpreter.intPool.get().SetUint64(1))
+ }
+ if err == nil || err == errExecutionReverted {
+ memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
+ }
+ contract.Gas += returnGas
+
+ interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
+ return ret, nil
+}
+
+func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ // Pop gas. The actual gas is in interpreter.evm.callGasTemp.
+ interpreter.intPool.put(stack.pop())
+ gas := interpreter.evm.callGasTemp
+ // Pop other call parameters.
+ addr, inOffset, inSize, retOffset, retSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
+ toAddr := common.BigToAddress(addr)
+ // Get arguments from the memory.
+ args := memory.Get(inOffset.Int64(), inSize.Int64())
+
+ ret, returnGas, err := interpreter.evm.StaticCall(contract, toAddr, args, gas)
+ if err != nil {
+ stack.push(interpreter.intPool.getZero())
+ } else {
+ stack.push(interpreter.intPool.get().SetUint64(1))
+ }
+ if err == nil || err == errExecutionReverted {
+ memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
+ }
+ contract.Gas += returnGas
+
+ interpreter.intPool.put(addr, inOffset, inSize, retOffset, retSize)
+ return ret, nil
+}
+
+func opReturn(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ offset, size := stack.pop(), stack.pop()
+ ret := memory.GetPtr(offset.Int64(), size.Int64())
+
+ interpreter.intPool.put(offset, size)
+ return ret, nil
+}
+
+func opRevert(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ offset, size := stack.pop(), stack.pop()
+ ret := memory.GetPtr(offset.Int64(), size.Int64())
+
+ interpreter.intPool.put(offset, size)
+ return ret, nil
+}
+
+func opStop(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ return nil, nil
+}
+
+func opSuicide(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ balance := interpreter.evm.StateDB.GetBalance(contract.Address())
+ interpreter.evm.StateDB.AddBalance(common.BigToAddress(stack.pop()), balance)
+
+ interpreter.evm.StateDB.Suicide(contract.Address())
+ return nil, nil
+}
+
+func opEMC(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ return nil, interpreter.evm.StateDB.EnableMultiCoin(contract.Address())
+}
+
+// following functions are used by the instruction jump table
+
+// make log instruction function
+func makeLog(size int) executionFunc {
+ return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ topics := make([]common.Hash, size)
+ mStart, mSize := stack.pop(), stack.pop()
+ for i := 0; i < size; i++ {
+ topics[i] = common.BigToHash(stack.pop())
+ }
+
+ d := memory.Get(mStart.Int64(), mSize.Int64())
+ interpreter.evm.StateDB.AddLog(&types.Log{
+ Address: contract.Address(),
+ Topics: topics,
+ Data: d,
+ // This is a non-consensus field, but assigned here because
+ // core/state doesn't know the current block number.
+ BlockNumber: interpreter.evm.BlockNumber.Uint64(),
+ })
+
+ interpreter.intPool.put(mStart, mSize)
+ return nil, nil
+ }
+}
+
+// opPush1 is a specialized version of pushN
+func opPush1(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ var (
+ codeLen = uint64(len(contract.Code))
+ integer = interpreter.intPool.get()
+ )
+ *pc += 1
+ if *pc < codeLen {
+ stack.push(integer.SetUint64(uint64(contract.Code[*pc])))
+ } else {
+ stack.push(integer.SetUint64(0))
+ }
+ return nil, nil
+}
+
+// make push instruction function
+func makePush(size uint64, pushByteSize int) executionFunc {
+ return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ codeLen := len(contract.Code)
+
+ startMin := codeLen
+ if int(*pc+1) < startMin {
+ startMin = int(*pc + 1)
+ }
+
+ endMin := codeLen
+ if startMin+pushByteSize < endMin {
+ endMin = startMin + pushByteSize
+ }
+
+ integer := interpreter.intPool.get()
+ stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
+
+ *pc += size
+ return nil, nil
+ }
+}
+
+// make dup instruction function
+func makeDup(size int64) executionFunc {
+ return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.dup(interpreter.intPool, int(size))
+ return nil, nil
+ }
+}
+
+// make swap instruction function
+func makeSwap(size int64) executionFunc {
+ // switch n + 1 otherwise n would be swapped with n
+ size++
+ return func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
+ stack.swap(int(size))
+ return nil, nil
+ }
+}
diff --git a/core/vm/int_pool_verifier.go b/core/vm/int_pool_verifier.go
new file mode 100644
index 0000000..82fbfed
--- /dev/null
+++ b/core/vm/int_pool_verifier.go
@@ -0,0 +1,31 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// +build VERIFY_EVM_INTEGER_POOL
+
+package vm
+
+import "fmt"
+
+const verifyPool = true
+
+func verifyIntegerPool(ip *intPool) {
+ for i, item := range ip.pool.data {
+ if item.Cmp(checkVal) != 0 {
+ panic(fmt.Sprintf("%d'th item failed aggressive pool check. Value was modified", i))
+ }
+ }
+}
diff --git a/core/vm/int_pool_verifier_empty.go b/core/vm/int_pool_verifier_empty.go
new file mode 100644
index 0000000..a5f1dc0
--- /dev/null
+++ b/core/vm/int_pool_verifier_empty.go
@@ -0,0 +1,23 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// +build !VERIFY_EVM_INTEGER_POOL
+
+package vm
+
+const verifyPool = false
+
+func verifyIntegerPool(ip *intPool) {}
diff --git a/core/vm/interface.go b/core/vm/interface.go
new file mode 100644
index 0000000..7e2d324
--- /dev/null
+++ b/core/vm/interface.go
@@ -0,0 +1,87 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// StateDB is an EVM database for full state querying.
+type StateDB interface {
+ CreateAccount(common.Address)
+
+ SubBalance(common.Address, *big.Int)
+ AddBalance(common.Address, *big.Int)
+ GetBalance(common.Address) *big.Int
+
+ SubBalanceMultiCoin(common.Address, common.Hash, *big.Int)
+ AddBalanceMultiCoin(common.Address, common.Hash, *big.Int)
+ GetBalanceMultiCoin(common.Address, common.Hash) *big.Int
+ EnableMultiCoin(common.Address) error
+ IsMultiCoin(common.Address) bool
+
+ GetNonce(common.Address) uint64
+ SetNonce(common.Address, uint64)
+
+ GetCodeHash(common.Address) common.Hash
+ GetCode(common.Address) []byte
+ SetCode(common.Address, []byte)
+ GetCodeSize(common.Address) int
+
+ AddRefund(uint64)
+ SubRefund(uint64)
+ GetRefund() uint64
+
+ GetCommittedState(common.Address, common.Hash) common.Hash
+ GetState(common.Address, common.Hash) common.Hash
+ SetState(common.Address, common.Hash, common.Hash) error
+
+ Suicide(common.Address) bool
+ HasSuicided(common.Address) bool
+
+ // Exist reports whether the given account exists in state.
+ // Notably this should also return true for suicided accounts.
+ Exist(common.Address) bool
+ // Empty returns whether the given account is empty. Empty
+ // is defined according to EIP161 (balance = nonce = code = 0).
+ Empty(common.Address) bool
+
+ RevertToSnapshot(int)
+ Snapshot() int
+
+ AddLog(*types.Log)
+ AddPreimage(common.Hash, []byte)
+
+ ForEachStorage(common.Address, func(common.Hash, common.Hash) bool) error
+}
+
+// CallContext provides a basic interface for the EVM calling conventions. The EVM
+// depends on this context being implemented for doing subcalls and initialising new EVM contracts.
+type CallContext interface {
+ // Call another contract
+ Call(env *EVM, me ContractRef, addr common.Address, data []byte, gas, value *big.Int) ([]byte, error)
+ CallExpert(env *EVM, me ContractRef, addr common.Address, data []byte, gas, value *big.Int, coinID *common.Hash, value2 *big.Int) ([]byte, error)
+ // Take another's contract code and execute within our own context
+ CallCode(env *EVM, me ContractRef, addr common.Address, data []byte, gas, value *big.Int) ([]byte, error)
+ // Same as CallCode except sender and value is propagated from parent to child scope
+ DelegateCall(env *EVM, me ContractRef, addr common.Address, data []byte, gas *big.Int) ([]byte, error)
+ // Create a new contract
+ Create(env *EVM, me ContractRef, data []byte, gas, value *big.Int) ([]byte, common.Address, error)
+}
diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go
new file mode 100644
index 0000000..007e5e3
--- /dev/null
+++ b/core/vm/interpreter.go
@@ -0,0 +1,300 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "fmt"
+ "hash"
+ "sync/atomic"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+// Config are the configuration options for the Interpreter
+type Config struct {
+ Debug bool // Enables debugging
+ Tracer Tracer // Opcode logger
+ NoRecursion bool // Disables call, callcode, delegate call and create
+ EnablePreimageRecording bool // Enables recording of SHA3/keccak preimages
+
+ JumpTable [256]operation // EVM instruction table, automatically populated if unset
+
+ EWASMInterpreter string // External EWASM interpreter options
+ EVMInterpreter string // External EVM interpreter options
+
+ ExtraEips []int // Additional EIPS that are to be enabled
+}
+
+// Interpreter is used to run Ethereum based contracts and will utilise the
+// passed environment to query external sources for state information.
+// The Interpreter will run the byte code VM based on the passed
+// configuration.
+type Interpreter interface {
+ // Run loops and evaluates the contract's code with the given input data and returns
+ // the return byte-slice and an error if one occurred.
+ Run(contract *Contract, input []byte, static bool) ([]byte, error)
+ // CanRun tells if the contract, passed as an argument, can be
+ // run by the current interpreter. This is meant so that the
+ // caller can do something like:
+ //
+ // ```golang
+ // for _, interpreter := range interpreters {
+ // if interpreter.CanRun(contract.code) {
+ // interpreter.Run(contract.code, input)
+ // }
+ // }
+ // ```
+ CanRun([]byte) bool
+}
+
+// keccakState wraps sha3.state. In addition to the usual hash methods, it also supports
+// Read to get a variable amount of data from the hash state. Read is faster than Sum
+// because it doesn't copy the internal state, but also modifies the internal state.
+type keccakState interface {
+ hash.Hash
+ Read([]byte) (int, error)
+}
+
+// EVMInterpreter represents an EVM interpreter
+type EVMInterpreter struct {
+ evm *EVM
+ cfg Config
+
+ intPool *intPool
+
+ hasher keccakState // Keccak256 hasher instance shared across opcodes
+ hasherBuf common.Hash // Keccak256 hasher result array shared aross opcodes
+
+ readOnly bool // Whether to throw on stateful modifications
+ returnData []byte // Last CALL's return data for subsequent reuse
+}
+
+// NewEVMInterpreter returns a new instance of the Interpreter.
+func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter {
+ // We use the STOP instruction whether to see
+ // the jump table was initialised. If it was not
+ // we'll set the default jump table.
+ if !cfg.JumpTable[STOP].valid {
+ var jt JumpTable
+ switch {
+ case evm.chainRules.IsIstanbul:
+ jt = istanbulInstructionSet
+ case evm.chainRules.IsConstantinople:
+ jt = constantinopleInstructionSet
+ case evm.chainRules.IsByzantium:
+ jt = byzantiumInstructionSet
+ case evm.chainRules.IsEIP158:
+ jt = spuriousDragonInstructionSet
+ case evm.chainRules.IsEIP150:
+ jt = tangerineWhistleInstructionSet
+ case evm.chainRules.IsHomestead:
+ jt = homesteadInstructionSet
+ default:
+ jt = frontierInstructionSet
+ }
+ for i, eip := range cfg.ExtraEips {
+ if err := EnableEIP(eip, &jt); err != nil {
+ // Disable it, so caller can check if it's activated or not
+ cfg.ExtraEips = append(cfg.ExtraEips[:i], cfg.ExtraEips[i+1:]...)
+ log.Error("EIP activation failed", "eip", eip, "error", err)
+ }
+ }
+ cfg.JumpTable = jt
+ }
+
+ return &EVMInterpreter{
+ evm: evm,
+ cfg: cfg,
+ }
+}
+
+// Run loops and evaluates the contract's code with the given input data and returns
+// the return byte-slice and an error if one occurred.
+//
+// It's important to note that any errors returned by the interpreter should be
+// considered a revert-and-consume-all-gas operation except for
+// errExecutionReverted which means revert-and-keep-gas-left.
+func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (ret []byte, err error) {
+ if in.intPool == nil {
+ in.intPool = poolOfIntPools.get()
+ defer func() {
+ poolOfIntPools.put(in.intPool)
+ in.intPool = nil
+ }()
+ }
+
+ // Increment the call depth which is restricted to 1024
+ in.evm.depth++
+ defer func() { in.evm.depth-- }()
+
+ // Make sure the readOnly is only set if we aren't in readOnly yet.
+ // This makes also sure that the readOnly flag isn't removed for child calls.
+ if readOnly && !in.readOnly {
+ in.readOnly = true
+ defer func() { in.readOnly = false }()
+ }
+
+ // Reset the previous call's return data. It's unimportant to preserve the old buffer
+ // as every returning call will return new data anyway.
+ in.returnData = nil
+
+ // Don't bother with the execution if there's no code.
+ if len(contract.Code) == 0 {
+ return nil, nil
+ }
+
+ var (
+ op OpCode // current opcode
+ mem = NewMemory() // bound memory
+ stack = newstack() // local stack
+ // For optimisation reason we're using uint64 as the program counter.
+ // It's theoretically possible to go above 2^64. The YP defines the PC
+ // to be uint256. Practically much less so feasible.
+ pc = uint64(0) // program counter
+ cost uint64
+ // copies used by tracer
+ pcCopy uint64 // needed for the deferred Tracer
+ gasCopy uint64 // for Tracer to log gas remaining before execution
+ logged bool // deferred Tracer should ignore already logged steps
+ res []byte // result of the opcode execution function
+ )
+ contract.Input = input
+
+ // Reclaim the stack as an int pool when the execution stops
+ defer func() { in.intPool.put(stack.data...) }()
+
+ if in.cfg.Debug {
+ defer func() {
+ if err != nil {
+ if !logged {
+ in.cfg.Tracer.CaptureState(in.evm, pcCopy, op, gasCopy, cost, mem, stack, contract, in.evm.depth, err)
+ } else {
+ in.cfg.Tracer.CaptureFault(in.evm, pcCopy, op, gasCopy, cost, mem, stack, contract, in.evm.depth, err)
+ }
+ }
+ }()
+ }
+ // The Interpreter main run loop (contextual). This loop runs until either an
+ // explicit STOP, RETURN or SELFDESTRUCT is executed, an error occurred during
+ // the execution of one of the operations or until the done flag is set by the
+ // parent context.
+ for atomic.LoadInt32(&in.evm.abort) == 0 {
+ if in.cfg.Debug {
+ // Capture pre-execution values for tracing.
+ logged, pcCopy, gasCopy = false, pc, contract.Gas
+ }
+
+ // Get the operation from the jump table and validate the stack to ensure there are
+ // enough stack items available to perform the operation.
+ op = contract.GetOp(pc)
+ operation := in.cfg.JumpTable[op]
+ if !operation.valid {
+ return nil, fmt.Errorf("invalid opcode 0x%x", int(op))
+ }
+ // Validate stack
+ if sLen := stack.len(); sLen < operation.minStack {
+ return nil, fmt.Errorf("stack underflow (%d <=> %d)", sLen, operation.minStack)
+ } else if sLen > operation.maxStack {
+ return nil, fmt.Errorf("stack limit reached %d (%d)", sLen, operation.maxStack)
+ }
+ // If the operation is valid, enforce and write restrictions
+ if in.readOnly && in.evm.chainRules.IsByzantium {
+ // If the interpreter is operating in readonly mode, make sure no
+ // state-modifying operation is performed. The 3rd stack item
+ // for a call operation is the value. Transferring value from one
+ // account to the others means the state is modified and should also
+ // return with an error.
+ if operation.writes || (op == CALL && stack.Back(2).Sign() != 0) {
+ return nil, errWriteProtection
+ }
+ }
+ // Static portion of gas
+ cost = operation.constantGas // For tracing
+ if !contract.UseGas(operation.constantGas) {
+ return nil, ErrOutOfGas
+ }
+
+ var memorySize uint64
+ // calculate the new memory size and expand the memory to fit
+ // the operation
+ // Memory check needs to be done prior to evaluating the dynamic gas portion,
+ // to detect calculation overflows
+ if operation.memorySize != nil {
+ memSize, overflow := operation.memorySize(stack)
+ if overflow {
+ return nil, errGasUintOverflow
+ }
+ // memory is expanded in words of 32 bytes. Gas
+ // is also calculated in words.
+ if memorySize, overflow = math.SafeMul(toWordSize(memSize), 32); overflow {
+ return nil, errGasUintOverflow
+ }
+ }
+ // Dynamic portion of gas
+ // consume the gas and return an error if not enough gas is available.
+ // cost is explicitly set so that the capture state defer method can get the proper cost
+ if operation.dynamicGas != nil {
+ var dynamicCost uint64
+ dynamicCost, err = operation.dynamicGas(in.evm, contract, stack, mem, memorySize)
+ cost += dynamicCost // total cost, for debug tracing
+ if err != nil || !contract.UseGas(dynamicCost) {
+ return nil, ErrOutOfGas
+ }
+ }
+ if memorySize > 0 {
+ mem.Resize(memorySize)
+ }
+
+ if in.cfg.Debug {
+ in.cfg.Tracer.CaptureState(in.evm, pc, op, gasCopy, cost, mem, stack, contract, in.evm.depth, err)
+ logged = true
+ }
+
+ // execute the operation
+ res, err = operation.execute(&pc, in, contract, mem, stack)
+ // verifyPool is a build flag. Pool verification makes sure the integrity
+ // of the integer pool by comparing values to a default value.
+ if verifyPool {
+ verifyIntegerPool(in.intPool)
+ }
+ // if the operation clears the return data (e.g. it has returning data)
+ // set the last return to the result of the operation.
+ if operation.returns {
+ in.returnData = res
+ }
+
+ switch {
+ case err != nil:
+ return nil, err
+ case operation.reverts:
+ return res, errExecutionReverted
+ case operation.halts:
+ return res, nil
+ case !operation.jumps:
+ pc++
+ }
+ }
+ return nil, nil
+}
+
+// CanRun tells if the contract, passed as an argument, can be
+// run by the current interpreter.
+func (in *EVMInterpreter) CanRun(code []byte) bool {
+ return true
+}
diff --git a/core/vm/intpool.go b/core/vm/intpool.go
new file mode 100644
index 0000000..917a78d
--- /dev/null
+++ b/core/vm/intpool.go
@@ -0,0 +1,106 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "math/big"
+ "sync"
+)
+
+var checkVal = big.NewInt(-42)
+
+const poolLimit = 256
+
+// intPool is a pool of big integers that
+// can be reused for all big.Int operations.
+type intPool struct {
+ pool *Stack
+}
+
+func newIntPool() *intPool {
+ return &intPool{pool: newstack()}
+}
+
+// get retrieves a big int from the pool, allocating one if the pool is empty.
+// Note, the returned int's value is arbitrary and will not be zeroed!
+func (p *intPool) get() *big.Int {
+ if p.pool.len() > 0 {
+ return p.pool.pop()
+ }
+ return new(big.Int)
+}
+
+// getZero retrieves a big int from the pool, setting it to zero or allocating
+// a new one if the pool is empty.
+func (p *intPool) getZero() *big.Int {
+ if p.pool.len() > 0 {
+ return p.pool.pop().SetUint64(0)
+ }
+ return new(big.Int)
+}
+
+// put returns an allocated big int to the pool to be later reused by get calls.
+// Note, the values as saved as is; neither put nor get zeroes the ints out!
+func (p *intPool) put(is ...*big.Int) {
+ if len(p.pool.data) > poolLimit {
+ return
+ }
+ for _, i := range is {
+ // verifyPool is a build flag. Pool verification makes sure the integrity
+ // of the integer pool by comparing values to a default value.
+ if verifyPool {
+ i.Set(checkVal)
+ }
+ p.pool.push(i)
+ }
+}
+
+// The intPool pool's default capacity
+const poolDefaultCap = 25
+
+// intPoolPool manages a pool of intPools.
+type intPoolPool struct {
+ pools []*intPool
+ lock sync.Mutex
+}
+
+var poolOfIntPools = &intPoolPool{
+ pools: make([]*intPool, 0, poolDefaultCap),
+}
+
+// get is looking for an available pool to return.
+func (ipp *intPoolPool) get() *intPool {
+ ipp.lock.Lock()
+ defer ipp.lock.Unlock()
+
+ if len(poolOfIntPools.pools) > 0 {
+ ip := ipp.pools[len(ipp.pools)-1]
+ ipp.pools = ipp.pools[:len(ipp.pools)-1]
+ return ip
+ }
+ return newIntPool()
+}
+
+// put a pool that has been allocated with get.
+func (ipp *intPoolPool) put(ip *intPool) {
+ ipp.lock.Lock()
+ defer ipp.lock.Unlock()
+
+ if len(ipp.pools) < cap(ipp.pools) {
+ ipp.pools = append(ipp.pools, ip)
+ }
+}
diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go
new file mode 100644
index 0000000..359b753
--- /dev/null
+++ b/core/vm/jump_table.go
@@ -0,0 +1,1183 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "errors"
+
+ "github.com/ava-labs/coreth/params"
+)
+
+type (
+ executionFunc func(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error)
+ gasFunc func(*EVM, *Contract, *Stack, *Memory, uint64) (uint64, error) // last parameter is the requested memory size as a uint64
+ // memorySizeFunc returns the required size, and whether the operation overflowed a uint64
+ memorySizeFunc func(*Stack) (size uint64, overflow bool)
+)
+
+var errGasUintOverflow = errors.New("gas uint64 overflow")
+
+type operation struct {
+ // execute is the operation function
+ execute executionFunc
+ constantGas uint64
+ dynamicGas gasFunc
+ // minStack tells how many stack items are required
+ minStack int
+ // maxStack specifies the max length the stack can have for this operation
+ // to not overflow the stack.
+ maxStack int
+
+ // memorySize returns the memory size required for the operation
+ memorySize memorySizeFunc
+
+ halts bool // indicates whether the operation should halt further execution
+ jumps bool // indicates whether the program counter should not increment
+ writes bool // determines whether this a state modifying operation
+ valid bool // indication whether the retrieved operation is valid and known
+ reverts bool // determines whether the operation reverts state (implicitly halts)
+ returns bool // determines whether the operations sets the return data content
+}
+
+var (
+ frontierInstructionSet = newFrontierInstructionSet()
+ homesteadInstructionSet = newHomesteadInstructionSet()
+ tangerineWhistleInstructionSet = newTangerineWhistleInstructionSet()
+ spuriousDragonInstructionSet = newSpuriousDragonInstructionSet()
+ byzantiumInstructionSet = newByzantiumInstructionSet()
+ constantinopleInstructionSet = newConstantinopleInstructionSet()
+ istanbulInstructionSet = newIstanbulInstructionSet()
+)
+
+// JumpTable contains the EVM opcodes supported at a given fork.
+type JumpTable [256]operation
+
+// newIstanbulInstructionSet returns the frontier, homestead
+// byzantium, contantinople and petersburg instructions.
+func newIstanbulInstructionSet() JumpTable {
+ instructionSet := newConstantinopleInstructionSet()
+
+ enable1344(&instructionSet) // ChainID opcode - https://eips.ethereum.org/EIPS/eip-1344
+ enable1884(&instructionSet) // Reprice reader opcodes - https://eips.ethereum.org/EIPS/eip-1884
+ enable2200(&instructionSet) // Net metered SSTORE - https://eips.ethereum.org/EIPS/eip-2200
+
+ return instructionSet
+}
+
+// newConstantinopleInstructionSet returns the frontier, homestead
+// byzantium and contantinople instructions.
+func newConstantinopleInstructionSet() JumpTable {
+ instructionSet := newByzantiumInstructionSet()
+ instructionSet[SHL] = operation{
+ execute: opSHL,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ }
+ instructionSet[SHR] = operation{
+ execute: opSHR,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ }
+ instructionSet[SAR] = operation{
+ execute: opSAR,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ }
+ instructionSet[EXTCODEHASH] = operation{
+ execute: opExtCodeHash,
+ constantGas: params.ExtcodeHashGasConstantinople,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ }
+ instructionSet[CREATE2] = operation{
+ execute: opCreate2,
+ constantGas: params.Create2Gas,
+ dynamicGas: gasCreate2,
+ minStack: minStack(4, 1),
+ maxStack: maxStack(4, 1),
+ memorySize: memoryCreate2,
+ valid: true,
+ writes: true,
+ returns: true,
+ }
+ return instructionSet
+}
+
+// newByzantiumInstructionSet returns the frontier, homestead and
+// byzantium instructions.
+func newByzantiumInstructionSet() JumpTable {
+ instructionSet := newSpuriousDragonInstructionSet()
+ instructionSet[STATICCALL] = operation{
+ execute: opStaticCall,
+ constantGas: params.CallGasEIP150,
+ dynamicGas: gasStaticCall,
+ minStack: minStack(6, 1),
+ maxStack: maxStack(6, 1),
+ memorySize: memoryStaticCall,
+ valid: true,
+ returns: true,
+ }
+ instructionSet[RETURNDATASIZE] = operation{
+ execute: opReturnDataSize,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ }
+ instructionSet[RETURNDATACOPY] = operation{
+ execute: opReturnDataCopy,
+ constantGas: GasFastestStep,
+ dynamicGas: gasReturnDataCopy,
+ minStack: minStack(3, 0),
+ maxStack: maxStack(3, 0),
+ memorySize: memoryReturnDataCopy,
+ valid: true,
+ }
+ instructionSet[REVERT] = operation{
+ execute: opRevert,
+ dynamicGas: gasRevert,
+ minStack: minStack(2, 0),
+ maxStack: maxStack(2, 0),
+ memorySize: memoryRevert,
+ valid: true,
+ reverts: true,
+ returns: true,
+ }
+ return instructionSet
+}
+
+// EIP 158 a.k.a Spurious Dragon
+func newSpuriousDragonInstructionSet() JumpTable {
+ instructionSet := newTangerineWhistleInstructionSet()
+ instructionSet[EXP].dynamicGas = gasExpEIP158
+ return instructionSet
+
+}
+
+// EIP 150 a.k.a Tangerine Whistle
+func newTangerineWhistleInstructionSet() JumpTable {
+ instructionSet := newHomesteadInstructionSet()
+ instructionSet[BALANCE].constantGas = params.BalanceGasEIP150
+ instructionSet[EXTCODESIZE].constantGas = params.ExtcodeSizeGasEIP150
+ instructionSet[SLOAD].constantGas = params.SloadGasEIP150
+ instructionSet[EXTCODECOPY].constantGas = params.ExtcodeCopyBaseEIP150
+ instructionSet[CALL].constantGas = params.CallGasEIP150
+ instructionSet[CALLCODE].constantGas = params.CallGasEIP150
+ instructionSet[DELEGATECALL].constantGas = params.CallGasEIP150
+ return instructionSet
+}
+
+// newHomesteadInstructionSet returns the frontier and homestead
+// instructions that can be executed during the homestead phase.
+func newHomesteadInstructionSet() JumpTable {
+ instructionSet := newFrontierInstructionSet()
+ instructionSet[DELEGATECALL] = operation{
+ execute: opDelegateCall,
+ dynamicGas: gasDelegateCall,
+ constantGas: params.CallGasFrontier,
+ minStack: minStack(6, 1),
+ maxStack: maxStack(6, 1),
+ memorySize: memoryDelegateCall,
+ valid: true,
+ returns: true,
+ }
+ return instructionSet
+}
+
+// newFrontierInstructionSet returns the frontier instructions
+// that can be executed during the frontier phase.
+func newFrontierInstructionSet() JumpTable {
+ return JumpTable{
+ STOP: {
+ execute: opStop,
+ constantGas: 0,
+ minStack: minStack(0, 0),
+ maxStack: maxStack(0, 0),
+ halts: true,
+ valid: true,
+ },
+ ADD: {
+ execute: opAdd,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ MUL: {
+ execute: opMul,
+ constantGas: GasFastStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ SUB: {
+ execute: opSub,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ DIV: {
+ execute: opDiv,
+ constantGas: GasFastStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ SDIV: {
+ execute: opSdiv,
+ constantGas: GasFastStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ MOD: {
+ execute: opMod,
+ constantGas: GasFastStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ SMOD: {
+ execute: opSmod,
+ constantGas: GasFastStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ ADDMOD: {
+ execute: opAddmod,
+ constantGas: GasMidStep,
+ minStack: minStack(3, 1),
+ maxStack: maxStack(3, 1),
+ valid: true,
+ },
+ MULMOD: {
+ execute: opMulmod,
+ constantGas: GasMidStep,
+ minStack: minStack(3, 1),
+ maxStack: maxStack(3, 1),
+ valid: true,
+ },
+ EXP: {
+ execute: opExp,
+ dynamicGas: gasExpFrontier,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ SIGNEXTEND: {
+ execute: opSignExtend,
+ constantGas: GasFastStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ LT: {
+ execute: opLt,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ GT: {
+ execute: opGt,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ SLT: {
+ execute: opSlt,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ SGT: {
+ execute: opSgt,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ EQ: {
+ execute: opEq,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ ISZERO: {
+ execute: opIszero,
+ constantGas: GasFastestStep,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ },
+ AND: {
+ execute: opAnd,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ XOR: {
+ execute: opXor,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ OR: {
+ execute: opOr,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ NOT: {
+ execute: opNot,
+ constantGas: GasFastestStep,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ },
+ BYTE: {
+ execute: opByte,
+ constantGas: GasFastestStep,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ SHA3: {
+ execute: opSha3,
+ constantGas: params.Sha3Gas,
+ dynamicGas: gasSha3,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ memorySize: memorySha3,
+ valid: true,
+ },
+ ADDRESS: {
+ execute: opAddress,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ BALANCE: {
+ execute: opBalance,
+ constantGas: params.BalanceGasFrontier,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ },
+ BALANCEMC: {
+ execute: opBalanceMultiCoin,
+ constantGas: params.BalanceGasFrontier,
+ minStack: minStack(2, 1),
+ maxStack: maxStack(2, 1),
+ valid: true,
+ },
+ ORIGIN: {
+ execute: opOrigin,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ CALLER: {
+ execute: opCaller,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ CALLVALUE: {
+ execute: opCallValue,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ CALLDATALOAD: {
+ execute: opCallDataLoad,
+ constantGas: GasFastestStep,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ },
+ CALLDATASIZE: {
+ execute: opCallDataSize,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ CALLDATACOPY: {
+ execute: opCallDataCopy,
+ constantGas: GasFastestStep,
+ dynamicGas: gasCallDataCopy,
+ minStack: minStack(3, 0),
+ maxStack: maxStack(3, 0),
+ memorySize: memoryCallDataCopy,
+ valid: true,
+ },
+ CODESIZE: {
+ execute: opCodeSize,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ CODECOPY: {
+ execute: opCodeCopy,
+ constantGas: GasFastestStep,
+ dynamicGas: gasCodeCopy,
+ minStack: minStack(3, 0),
+ maxStack: maxStack(3, 0),
+ memorySize: memoryCodeCopy,
+ valid: true,
+ },
+ GASPRICE: {
+ execute: opGasprice,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ EXTCODESIZE: {
+ execute: opExtCodeSize,
+ constantGas: params.ExtcodeSizeGasFrontier,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ },
+ EXTCODECOPY: {
+ execute: opExtCodeCopy,
+ constantGas: params.ExtcodeCopyBaseFrontier,
+ dynamicGas: gasExtCodeCopy,
+ minStack: minStack(4, 0),
+ maxStack: maxStack(4, 0),
+ memorySize: memoryExtCodeCopy,
+ valid: true,
+ },
+ BLOCKHASH: {
+ execute: opBlockhash,
+ constantGas: GasExtStep,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ },
+ COINBASE: {
+ execute: opCoinbase,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ TIMESTAMP: {
+ execute: opTimestamp,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ NUMBER: {
+ execute: opNumber,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ DIFFICULTY: {
+ execute: opDifficulty,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ GASLIMIT: {
+ execute: opGasLimit,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ POP: {
+ execute: opPop,
+ constantGas: GasQuickStep,
+ minStack: minStack(1, 0),
+ maxStack: maxStack(1, 0),
+ valid: true,
+ },
+ MLOAD: {
+ execute: opMload,
+ constantGas: GasFastestStep,
+ dynamicGas: gasMLoad,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ memorySize: memoryMLoad,
+ valid: true,
+ },
+ MSTORE: {
+ execute: opMstore,
+ constantGas: GasFastestStep,
+ dynamicGas: gasMStore,
+ minStack: minStack(2, 0),
+ maxStack: maxStack(2, 0),
+ memorySize: memoryMStore,
+ valid: true,
+ },
+ MSTORE8: {
+ execute: opMstore8,
+ constantGas: GasFastestStep,
+ dynamicGas: gasMStore8,
+ memorySize: memoryMStore8,
+ minStack: minStack(2, 0),
+ maxStack: maxStack(2, 0),
+
+ valid: true,
+ },
+ SLOAD: {
+ execute: opSload,
+ constantGas: params.SloadGasFrontier,
+ minStack: minStack(1, 1),
+ maxStack: maxStack(1, 1),
+ valid: true,
+ },
+ SSTORE: {
+ execute: opSstore,
+ dynamicGas: gasSStore,
+ minStack: minStack(2, 0),
+ maxStack: maxStack(2, 0),
+ valid: true,
+ writes: true,
+ },
+ JUMP: {
+ execute: opJump,
+ constantGas: GasMidStep,
+ minStack: minStack(1, 0),
+ maxStack: maxStack(1, 0),
+ jumps: true,
+ valid: true,
+ },
+ JUMPI: {
+ execute: opJumpi,
+ constantGas: GasSlowStep,
+ minStack: minStack(2, 0),
+ maxStack: maxStack(2, 0),
+ jumps: true,
+ valid: true,
+ },
+ PC: {
+ execute: opPc,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ MSIZE: {
+ execute: opMsize,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ GAS: {
+ execute: opGas,
+ constantGas: GasQuickStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ JUMPDEST: {
+ execute: opJumpdest,
+ constantGas: params.JumpdestGas,
+ minStack: minStack(0, 0),
+ maxStack: maxStack(0, 0),
+ valid: true,
+ },
+ EMC: {
+ execute: opEMC,
+ constantGas: params.EMCGas,
+ minStack: minStack(0, 0),
+ maxStack: maxStack(0, 0),
+ valid: true,
+ },
+ PUSH1: {
+ execute: opPush1,
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH2: {
+ execute: makePush(2, 2),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH3: {
+ execute: makePush(3, 3),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH4: {
+ execute: makePush(4, 4),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH5: {
+ execute: makePush(5, 5),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH6: {
+ execute: makePush(6, 6),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH7: {
+ execute: makePush(7, 7),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH8: {
+ execute: makePush(8, 8),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH9: {
+ execute: makePush(9, 9),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH10: {
+ execute: makePush(10, 10),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH11: {
+ execute: makePush(11, 11),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH12: {
+ execute: makePush(12, 12),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH13: {
+ execute: makePush(13, 13),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH14: {
+ execute: makePush(14, 14),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH15: {
+ execute: makePush(15, 15),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH16: {
+ execute: makePush(16, 16),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH17: {
+ execute: makePush(17, 17),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH18: {
+ execute: makePush(18, 18),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH19: {
+ execute: makePush(19, 19),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH20: {
+ execute: makePush(20, 20),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH21: {
+ execute: makePush(21, 21),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH22: {
+ execute: makePush(22, 22),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH23: {
+ execute: makePush(23, 23),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH24: {
+ execute: makePush(24, 24),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH25: {
+ execute: makePush(25, 25),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH26: {
+ execute: makePush(26, 26),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH27: {
+ execute: makePush(27, 27),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH28: {
+ execute: makePush(28, 28),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH29: {
+ execute: makePush(29, 29),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH30: {
+ execute: makePush(30, 30),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH31: {
+ execute: makePush(31, 31),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ PUSH32: {
+ execute: makePush(32, 32),
+ constantGas: GasFastestStep,
+ minStack: minStack(0, 1),
+ maxStack: maxStack(0, 1),
+ valid: true,
+ },
+ DUP1: {
+ execute: makeDup(1),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(1),
+ maxStack: maxDupStack(1),
+ valid: true,
+ },
+ DUP2: {
+ execute: makeDup(2),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(2),
+ maxStack: maxDupStack(2),
+ valid: true,
+ },
+ DUP3: {
+ execute: makeDup(3),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(3),
+ maxStack: maxDupStack(3),
+ valid: true,
+ },
+ DUP4: {
+ execute: makeDup(4),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(4),
+ maxStack: maxDupStack(4),
+ valid: true,
+ },
+ DUP5: {
+ execute: makeDup(5),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(5),
+ maxStack: maxDupStack(5),
+ valid: true,
+ },
+ DUP6: {
+ execute: makeDup(6),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(6),
+ maxStack: maxDupStack(6),
+ valid: true,
+ },
+ DUP7: {
+ execute: makeDup(7),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(7),
+ maxStack: maxDupStack(7),
+ valid: true,
+ },
+ DUP8: {
+ execute: makeDup(8),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(8),
+ maxStack: maxDupStack(8),
+ valid: true,
+ },
+ DUP9: {
+ execute: makeDup(9),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(9),
+ maxStack: maxDupStack(9),
+ valid: true,
+ },
+ DUP10: {
+ execute: makeDup(10),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(10),
+ maxStack: maxDupStack(10),
+ valid: true,
+ },
+ DUP11: {
+ execute: makeDup(11),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(11),
+ maxStack: maxDupStack(11),
+ valid: true,
+ },
+ DUP12: {
+ execute: makeDup(12),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(12),
+ maxStack: maxDupStack(12),
+ valid: true,
+ },
+ DUP13: {
+ execute: makeDup(13),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(13),
+ maxStack: maxDupStack(13),
+ valid: true,
+ },
+ DUP14: {
+ execute: makeDup(14),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(14),
+ maxStack: maxDupStack(14),
+ valid: true,
+ },
+ DUP15: {
+ execute: makeDup(15),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(15),
+ maxStack: maxDupStack(15),
+ valid: true,
+ },
+ DUP16: {
+ execute: makeDup(16),
+ constantGas: GasFastestStep,
+ minStack: minDupStack(16),
+ maxStack: maxDupStack(16),
+ valid: true,
+ },
+ SWAP1: {
+ execute: makeSwap(1),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(2),
+ maxStack: maxSwapStack(2),
+ valid: true,
+ },
+ SWAP2: {
+ execute: makeSwap(2),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(3),
+ maxStack: maxSwapStack(3),
+ valid: true,
+ },
+ SWAP3: {
+ execute: makeSwap(3),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(4),
+ maxStack: maxSwapStack(4),
+ valid: true,
+ },
+ SWAP4: {
+ execute: makeSwap(4),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(5),
+ maxStack: maxSwapStack(5),
+ valid: true,
+ },
+ SWAP5: {
+ execute: makeSwap(5),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(6),
+ maxStack: maxSwapStack(6),
+ valid: true,
+ },
+ SWAP6: {
+ execute: makeSwap(6),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(7),
+ maxStack: maxSwapStack(7),
+ valid: true,
+ },
+ SWAP7: {
+ execute: makeSwap(7),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(8),
+ maxStack: maxSwapStack(8),
+ valid: true,
+ },
+ SWAP8: {
+ execute: makeSwap(8),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(9),
+ maxStack: maxSwapStack(9),
+ valid: true,
+ },
+ SWAP9: {
+ execute: makeSwap(9),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(10),
+ maxStack: maxSwapStack(10),
+ valid: true,
+ },
+ SWAP10: {
+ execute: makeSwap(10),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(11),
+ maxStack: maxSwapStack(11),
+ valid: true,
+ },
+ SWAP11: {
+ execute: makeSwap(11),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(12),
+ maxStack: maxSwapStack(12),
+ valid: true,
+ },
+ SWAP12: {
+ execute: makeSwap(12),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(13),
+ maxStack: maxSwapStack(13),
+ valid: true,
+ },
+ SWAP13: {
+ execute: makeSwap(13),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(14),
+ maxStack: maxSwapStack(14),
+ valid: true,
+ },
+ SWAP14: {
+ execute: makeSwap(14),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(15),
+ maxStack: maxSwapStack(15),
+ valid: true,
+ },
+ SWAP15: {
+ execute: makeSwap(15),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(16),
+ maxStack: maxSwapStack(16),
+ valid: true,
+ },
+ SWAP16: {
+ execute: makeSwap(16),
+ constantGas: GasFastestStep,
+ minStack: minSwapStack(17),
+ maxStack: maxSwapStack(17),
+ valid: true,
+ },
+ LOG0: {
+ execute: makeLog(0),
+ dynamicGas: makeGasLog(0),
+ minStack: minStack(2, 0),
+ maxStack: maxStack(2, 0),
+ memorySize: memoryLog,
+ valid: true,
+ writes: true,
+ },
+ LOG1: {
+ execute: makeLog(1),
+ dynamicGas: makeGasLog(1),
+ minStack: minStack(3, 0),
+ maxStack: maxStack(3, 0),
+ memorySize: memoryLog,
+ valid: true,
+ writes: true,
+ },
+ LOG2: {
+ execute: makeLog(2),
+ dynamicGas: makeGasLog(2),
+ minStack: minStack(4, 0),
+ maxStack: maxStack(4, 0),
+ memorySize: memoryLog,
+ valid: true,
+ writes: true,
+ },
+ LOG3: {
+ execute: makeLog(3),
+ dynamicGas: makeGasLog(3),
+ minStack: minStack(5, 0),
+ maxStack: maxStack(5, 0),
+ memorySize: memoryLog,
+ valid: true,
+ writes: true,
+ },
+ LOG4: {
+ execute: makeLog(4),
+ dynamicGas: makeGasLog(4),
+ minStack: minStack(6, 0),
+ maxStack: maxStack(6, 0),
+ memorySize: memoryLog,
+ valid: true,
+ writes: true,
+ },
+ CREATE: {
+ execute: opCreate,
+ constantGas: params.CreateGas,
+ dynamicGas: gasCreate,
+ minStack: minStack(3, 1),
+ maxStack: maxStack(3, 1),
+ memorySize: memoryCreate,
+ valid: true,
+ writes: true,
+ returns: true,
+ },
+ CALL: {
+ execute: opCall,
+ constantGas: params.CallGasFrontier,
+ dynamicGas: gasCall,
+ minStack: minStack(7, 1),
+ maxStack: maxStack(7, 1),
+ memorySize: memoryCall,
+ valid: true,
+ returns: true,
+ },
+ CALLEX: {
+ execute: opCallExpert,
+ constantGas: params.CallGasFrontier,
+ dynamicGas: gasCall,
+ minStack: minStack(9, 1),
+ maxStack: maxStack(9, 1),
+ memorySize: memoryCall,
+ valid: true,
+ returns: true,
+ },
+ CALLCODE: {
+ execute: opCallCode,
+ constantGas: params.CallGasFrontier,
+ dynamicGas: gasCallCode,
+ minStack: minStack(7, 1),
+ maxStack: maxStack(7, 1),
+ memorySize: memoryCall,
+ valid: true,
+ returns: true,
+ },
+ RETURN: {
+ execute: opReturn,
+ dynamicGas: gasReturn,
+ minStack: minStack(2, 0),
+ maxStack: maxStack(2, 0),
+ memorySize: memoryReturn,
+ halts: true,
+ valid: true,
+ },
+ SELFDESTRUCT: {
+ execute: opSuicide,
+ dynamicGas: gasSelfdestruct,
+ minStack: minStack(1, 0),
+ maxStack: maxStack(1, 0),
+ halts: true,
+ valid: true,
+ writes: true,
+ },
+ }
+}
diff --git a/core/vm/logger.go b/core/vm/logger.go
new file mode 100644
index 0000000..95143f1
--- /dev/null
+++ b/core/vm/logger.go
@@ -0,0 +1,256 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "encoding/hex"
+ "fmt"
+ "io"
+ "math/big"
+ "time"
+
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+// Storage represents a contract's storage.
+type Storage map[common.Hash]common.Hash
+
+// Copy duplicates the current storage.
+func (s Storage) Copy() Storage {
+ cpy := make(Storage)
+ for key, value := range s {
+ cpy[key] = value
+ }
+
+ return cpy
+}
+
+// LogConfig are the configuration options for structured logger the EVM
+type LogConfig struct {
+ DisableMemory bool // disable memory capture
+ DisableStack bool // disable stack capture
+ DisableStorage bool // disable storage capture
+ Debug bool // print output during capture end
+ Limit int // maximum length of output, but zero means unlimited
+}
+
+//go:generate gencodec -type StructLog -field-override structLogMarshaling -out gen_structlog.go
+
+// StructLog is emitted to the EVM each cycle and lists information about the current internal state
+// prior to the execution of the statement.
+type StructLog struct {
+ Pc uint64 `json:"pc"`
+ Op OpCode `json:"op"`
+ Gas uint64 `json:"gas"`
+ GasCost uint64 `json:"gasCost"`
+ Memory []byte `json:"memory"`
+ MemorySize int `json:"memSize"`
+ Stack []*big.Int `json:"stack"`
+ Storage map[common.Hash]common.Hash `json:"-"`
+ Depth int `json:"depth"`
+ RefundCounter uint64 `json:"refund"`
+ Err error `json:"-"`
+}
+
+// overrides for gencodec
+type structLogMarshaling struct {
+ Stack []*math.HexOrDecimal256
+ Gas math.HexOrDecimal64
+ GasCost math.HexOrDecimal64
+ Memory hexutil.Bytes
+ OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
+ ErrorString string `json:"error"` // adds call to ErrorString() in MarshalJSON
+}
+
+// OpName formats the operand name in a human-readable format.
+func (s *StructLog) OpName() string {
+ return s.Op.String()
+}
+
+// ErrorString formats the log's error as a string.
+func (s *StructLog) ErrorString() string {
+ if s.Err != nil {
+ return s.Err.Error()
+ }
+ return ""
+}
+
+// Tracer is used to collect execution traces from an EVM transaction
+// execution. CaptureState is called for each step of the VM with the
+// current VM state.
+// Note that reference types are actual VM data structures; make copies
+// if you need to retain them beyond the current call.
+type Tracer interface {
+ CaptureStart(from common.Address, to common.Address, call bool, input []byte, gas uint64, value *big.Int) error
+ CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error
+ CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error
+ CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error
+}
+
+// StructLogger is an EVM state logger and implements Tracer.
+//
+// StructLogger can capture state based on the given Log configuration and also keeps
+// a track record of modified storage which is used in reporting snapshots of the
+// contract their storage.
+type StructLogger struct {
+ cfg LogConfig
+
+ logs []StructLog
+ changedValues map[common.Address]Storage
+ output []byte
+ err error
+}
+
+// NewStructLogger returns a new logger
+func NewStructLogger(cfg *LogConfig) *StructLogger {
+ logger := &StructLogger{
+ changedValues: make(map[common.Address]Storage),
+ }
+ if cfg != nil {
+ logger.cfg = *cfg
+ }
+ return logger
+}
+
+// CaptureStart implements the Tracer interface to initialize the tracing operation.
+func (l *StructLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
+ return nil
+}
+
+// CaptureState logs a new structured log message and pushes it out to the environment
+//
+// CaptureState also tracks SSTORE ops to track dirty values.
+func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
+ // check if already accumulated the specified number of logs
+ if l.cfg.Limit != 0 && l.cfg.Limit <= len(l.logs) {
+ return ErrTraceLimitReached
+ }
+
+ // initialise new changed values storage container for this contract
+ // if not present.
+ if l.changedValues[contract.Address()] == nil {
+ l.changedValues[contract.Address()] = make(Storage)
+ }
+
+ // capture SSTORE opcodes and determine the changed value and store
+ // it in the local storage container.
+ if op == SSTORE && stack.len() >= 2 {
+ var (
+ value = common.BigToHash(stack.data[stack.len()-2])
+ address = common.BigToHash(stack.data[stack.len()-1])
+ )
+ l.changedValues[contract.Address()][address] = value
+ }
+ // Copy a snapshot of the current memory state to a new buffer
+ var mem []byte
+ if !l.cfg.DisableMemory {
+ mem = make([]byte, len(memory.Data()))
+ copy(mem, memory.Data())
+ }
+ // Copy a snapshot of the current stack state to a new buffer
+ var stck []*big.Int
+ if !l.cfg.DisableStack {
+ stck = make([]*big.Int, len(stack.Data()))
+ for i, item := range stack.Data() {
+ stck[i] = new(big.Int).Set(item)
+ }
+ }
+ // Copy a snapshot of the current storage to a new container
+ var storage Storage
+ if !l.cfg.DisableStorage {
+ storage = l.changedValues[contract.Address()].Copy()
+ }
+ // create a new snapshot of the EVM.
+ log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, storage, depth, env.StateDB.GetRefund(), err}
+
+ l.logs = append(l.logs, log)
+ return nil
+}
+
+// CaptureFault implements the Tracer interface to trace an execution fault
+// while running an opcode.
+func (l *StructLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
+ return nil
+}
+
+// CaptureEnd is called after the call finishes to finalize the tracing.
+func (l *StructLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
+ l.output = output
+ l.err = err
+ if l.cfg.Debug {
+ fmt.Printf("0x%x\n", output)
+ if err != nil {
+ fmt.Printf(" error: %v\n", err)
+ }
+ }
+ return nil
+}
+
+// StructLogs returns the captured log entries.
+func (l *StructLogger) StructLogs() []StructLog { return l.logs }
+
+// Error returns the VM error captured by the trace.
+func (l *StructLogger) Error() error { return l.err }
+
+// Output returns the VM return value captured by the trace.
+func (l *StructLogger) Output() []byte { return l.output }
+
+// WriteTrace writes a formatted trace to the given writer
+func WriteTrace(writer io.Writer, logs []StructLog) {
+ for _, log := range logs {
+ fmt.Fprintf(writer, "%-16spc=%08d gas=%v cost=%v", log.Op, log.Pc, log.Gas, log.GasCost)
+ if log.Err != nil {
+ fmt.Fprintf(writer, " ERROR: %v", log.Err)
+ }
+ fmt.Fprintln(writer)
+
+ if len(log.Stack) > 0 {
+ fmt.Fprintln(writer, "Stack:")
+ for i := len(log.Stack) - 1; i >= 0; i-- {
+ fmt.Fprintf(writer, "%08d %x\n", len(log.Stack)-i-1, math.PaddedBigBytes(log.Stack[i], 32))
+ }
+ }
+ if len(log.Memory) > 0 {
+ fmt.Fprintln(writer, "Memory:")
+ fmt.Fprint(writer, hex.Dump(log.Memory))
+ }
+ if len(log.Storage) > 0 {
+ fmt.Fprintln(writer, "Storage:")
+ for h, item := range log.Storage {
+ fmt.Fprintf(writer, "%x: %x\n", h, item)
+ }
+ }
+ fmt.Fprintln(writer)
+ }
+}
+
+// WriteLogs writes vm logs in a readable format to the given writer
+func WriteLogs(writer io.Writer, logs []*types.Log) {
+ for _, log := range logs {
+ fmt.Fprintf(writer, "LOG%d: %x bn=%d txi=%x\n", len(log.Topics), log.Address, log.BlockNumber, log.TxIndex)
+
+ for i, topic := range log.Topics {
+ fmt.Fprintf(writer, "%08d %x\n", i, topic)
+ }
+
+ fmt.Fprint(writer, hex.Dump(log.Data))
+ fmt.Fprintln(writer)
+ }
+}
diff --git a/core/vm/logger_json.go b/core/vm/logger_json.go
new file mode 100644
index 0000000..5068343
--- /dev/null
+++ b/core/vm/logger_json.go
@@ -0,0 +1,87 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "encoding/json"
+ "io"
+ "math/big"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+type JSONLogger struct {
+ encoder *json.Encoder
+ cfg *LogConfig
+}
+
+// NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
+// into the provided stream.
+func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
+ l := &JSONLogger{json.NewEncoder(writer), cfg}
+ if l.cfg == nil {
+ l.cfg = &LogConfig{}
+ }
+ return l
+}
+
+func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
+ return nil
+}
+
+// CaptureState outputs state information on the logger.
+func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
+ log := StructLog{
+ Pc: pc,
+ Op: op,
+ Gas: gas,
+ GasCost: cost,
+ MemorySize: memory.Len(),
+ Storage: nil,
+ Depth: depth,
+ RefundCounter: env.StateDB.GetRefund(),
+ Err: err,
+ }
+ if !l.cfg.DisableMemory {
+ log.Memory = memory.Data()
+ }
+ if !l.cfg.DisableStack {
+ log.Stack = stack.Data()
+ }
+ return l.encoder.Encode(log)
+}
+
+// CaptureFault outputs state information on the logger.
+func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
+ return nil
+}
+
+// CaptureEnd is triggered at end of execution.
+func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
+ type endLog struct {
+ Output string `json:"output"`
+ GasUsed math.HexOrDecimal64 `json:"gasUsed"`
+ Time time.Duration `json:"time"`
+ Err string `json:"error,omitempty"`
+ }
+ if err != nil {
+ return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
+ }
+ return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
+}
diff --git a/core/vm/memory.go b/core/vm/memory.go
new file mode 100644
index 0000000..5408707
--- /dev/null
+++ b/core/vm/memory.go
@@ -0,0 +1,124 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "fmt"
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+// Memory implements a simple memory model for the ethereum virtual machine.
+type Memory struct {
+ store []byte
+ lastGasCost uint64
+}
+
+// NewMemory returns a new memory model.
+func NewMemory() *Memory {
+ return &Memory{}
+}
+
+// Set sets offset + size to value
+func (m *Memory) Set(offset, size uint64, value []byte) {
+ // It's possible the offset is greater than 0 and size equals 0. This is because
+ // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP)
+ if size > 0 {
+ // length of store may never be less than offset + size.
+ // The store should be resized PRIOR to setting the memory
+ if offset+size > uint64(len(m.store)) {
+ panic("invalid memory: store empty")
+ }
+ copy(m.store[offset:offset+size], value)
+ }
+}
+
+// Set32 sets the 32 bytes starting at offset to the value of val, left-padded with zeroes to
+// 32 bytes.
+func (m *Memory) Set32(offset uint64, val *big.Int) {
+ // length of store may never be less than offset + size.
+ // The store should be resized PRIOR to setting the memory
+ if offset+32 > uint64(len(m.store)) {
+ panic("invalid memory: store empty")
+ }
+ // Zero the memory area
+ copy(m.store[offset:offset+32], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
+ // Fill in relevant bits
+ math.ReadBits(val, m.store[offset:offset+32])
+}
+
+// Resize resizes the memory to size
+func (m *Memory) Resize(size uint64) {
+ if uint64(m.Len()) < size {
+ m.store = append(m.store, make([]byte, size-uint64(m.Len()))...)
+ }
+}
+
+// Get returns offset + size as a new slice
+func (m *Memory) Get(offset, size int64) (cpy []byte) {
+ if size == 0 {
+ return nil
+ }
+
+ if len(m.store) > int(offset) {
+ cpy = make([]byte, size)
+ copy(cpy, m.store[offset:offset+size])
+
+ return
+ }
+
+ return
+}
+
+// GetPtr returns the offset + size
+func (m *Memory) GetPtr(offset, size int64) []byte {
+ if size == 0 {
+ return nil
+ }
+
+ if len(m.store) > int(offset) {
+ return m.store[offset : offset+size]
+ }
+
+ return nil
+}
+
+// Len returns the length of the backing slice
+func (m *Memory) Len() int {
+ return len(m.store)
+}
+
+// Data returns the backing slice
+func (m *Memory) Data() []byte {
+ return m.store
+}
+
+// Print dumps the content of the memory.
+func (m *Memory) Print() {
+ fmt.Printf("### mem %d bytes ###\n", len(m.store))
+ if len(m.store) > 0 {
+ addr := 0
+ for i := 0; i+32 <= len(m.store); i += 32 {
+ fmt.Printf("%03d: % x\n", addr, m.store[i:i+32])
+ addr++
+ }
+ } else {
+ fmt.Println("-- empty --")
+ }
+ fmt.Println("####################")
+}
diff --git a/core/vm/memory_table.go b/core/vm/memory_table.go
new file mode 100644
index 0000000..4fcb414
--- /dev/null
+++ b/core/vm/memory_table.go
@@ -0,0 +1,113 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+func memorySha3(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(0), stack.Back(1))
+}
+
+func memoryCallDataCopy(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(0), stack.Back(2))
+}
+
+func memoryReturnDataCopy(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(0), stack.Back(2))
+}
+
+func memoryCodeCopy(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(0), stack.Back(2))
+}
+
+func memoryExtCodeCopy(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(1), stack.Back(3))
+}
+
+func memoryMLoad(stack *Stack) (uint64, bool) {
+ return calcMemSize64WithUint(stack.Back(0), 32)
+}
+
+func memoryMStore8(stack *Stack) (uint64, bool) {
+ return calcMemSize64WithUint(stack.Back(0), 1)
+}
+
+func memoryMStore(stack *Stack) (uint64, bool) {
+ return calcMemSize64WithUint(stack.Back(0), 32)
+}
+
+func memoryCreate(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(1), stack.Back(2))
+}
+
+func memoryCreate2(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(1), stack.Back(2))
+}
+
+func memoryCall(stack *Stack) (uint64, bool) {
+ x, overflow := calcMemSize64(stack.Back(5), stack.Back(6))
+ if overflow {
+ return 0, true
+ }
+ y, overflow := calcMemSize64(stack.Back(3), stack.Back(4))
+ if overflow {
+ return 0, true
+ }
+ if x > y {
+ return x, false
+ }
+ return y, false
+}
+func memoryDelegateCall(stack *Stack) (uint64, bool) {
+ x, overflow := calcMemSize64(stack.Back(4), stack.Back(5))
+ if overflow {
+ return 0, true
+ }
+ y, overflow := calcMemSize64(stack.Back(2), stack.Back(3))
+ if overflow {
+ return 0, true
+ }
+ if x > y {
+ return x, false
+ }
+ return y, false
+}
+
+func memoryStaticCall(stack *Stack) (uint64, bool) {
+ x, overflow := calcMemSize64(stack.Back(4), stack.Back(5))
+ if overflow {
+ return 0, true
+ }
+ y, overflow := calcMemSize64(stack.Back(2), stack.Back(3))
+ if overflow {
+ return 0, true
+ }
+ if x > y {
+ return x, false
+ }
+ return y, false
+}
+
+func memoryReturn(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(0), stack.Back(1))
+}
+
+func memoryRevert(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(0), stack.Back(1))
+}
+
+func memoryLog(stack *Stack) (uint64, bool) {
+ return calcMemSize64(stack.Back(0), stack.Back(1))
+}
diff --git a/core/vm/opcodes.go b/core/vm/opcodes.go
new file mode 100644
index 0000000..1a66ef8
--- /dev/null
+++ b/core/vm/opcodes.go
@@ -0,0 +1,555 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "fmt"
+)
+
+// OpCode is an EVM opcode
+type OpCode byte
+
+// IsPush specifies if an opcode is a PUSH opcode.
+func (op OpCode) IsPush() bool {
+ switch op {
+ case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
+ return true
+ }
+ return false
+}
+
+// IsStaticJump specifies if an opcode is JUMP.
+func (op OpCode) IsStaticJump() bool {
+ return op == JUMP
+}
+
+// 0x0 range - arithmetic ops.
+const (
+ STOP OpCode = iota
+ ADD
+ MUL
+ SUB
+ DIV
+ SDIV
+ MOD
+ SMOD
+ ADDMOD
+ MULMOD
+ EXP
+ SIGNEXTEND
+)
+
+// 0x10 range - comparison ops.
+const (
+ LT OpCode = iota + 0x10
+ GT
+ SLT
+ SGT
+ EQ
+ ISZERO
+ AND
+ OR
+ XOR
+ NOT
+ BYTE
+ SHL
+ SHR
+ SAR
+
+ SHA3 = 0x20
+)
+
+// 0x30 range - closure state.
+const (
+ ADDRESS OpCode = 0x30 + iota
+ BALANCE
+ ORIGIN
+ CALLER
+ CALLVALUE
+ CALLDATALOAD
+ CALLDATASIZE
+ CALLDATACOPY
+ CODESIZE
+ CODECOPY
+ GASPRICE
+ EXTCODESIZE
+ EXTCODECOPY
+ RETURNDATASIZE
+ RETURNDATACOPY
+ EXTCODEHASH
+)
+
+// 0x40 range - block operations.
+const (
+ BLOCKHASH OpCode = 0x40 + iota
+ COINBASE
+ TIMESTAMP
+ NUMBER
+ DIFFICULTY
+ GASLIMIT
+ CHAINID = 0x46
+ SELFBALANCE = 0x47
+)
+
+// 0x50 range - 'storage' and execution.
+const (
+ POP OpCode = 0x50 + iota
+ MLOAD
+ MSTORE
+ MSTORE8
+ SLOAD
+ SSTORE
+ JUMP
+ JUMPI
+ PC
+ MSIZE
+ GAS
+ JUMPDEST
+ EMC = 0x5c
+ BALANCEMC = 0x5d
+)
+
+// 0x60 range.
+const (
+ PUSH1 OpCode = 0x60 + iota
+ PUSH2
+ PUSH3
+ PUSH4
+ PUSH5
+ PUSH6
+ PUSH7
+ PUSH8
+ PUSH9
+ PUSH10
+ PUSH11
+ PUSH12
+ PUSH13
+ PUSH14
+ PUSH15
+ PUSH16
+ PUSH17
+ PUSH18
+ PUSH19
+ PUSH20
+ PUSH21
+ PUSH22
+ PUSH23
+ PUSH24
+ PUSH25
+ PUSH26
+ PUSH27
+ PUSH28
+ PUSH29
+ PUSH30
+ PUSH31
+ PUSH32
+ DUP1
+ DUP2
+ DUP3
+ DUP4
+ DUP5
+ DUP6
+ DUP7
+ DUP8
+ DUP9
+ DUP10
+ DUP11
+ DUP12
+ DUP13
+ DUP14
+ DUP15
+ DUP16
+ SWAP1
+ SWAP2
+ SWAP3
+ SWAP4
+ SWAP5
+ SWAP6
+ SWAP7
+ SWAP8
+ SWAP9
+ SWAP10
+ SWAP11
+ SWAP12
+ SWAP13
+ SWAP14
+ SWAP15
+ SWAP16
+)
+
+// 0xa0 range - logging ops.
+const (
+ LOG0 OpCode = 0xa0 + iota
+ LOG1
+ LOG2
+ LOG3
+ LOG4
+)
+
+// unofficial opcodes used for parsing.
+const (
+ PUSH OpCode = 0xb0 + iota
+ DUP
+ SWAP
+)
+
+// 0xf0 range - closures.
+const (
+ CREATE OpCode = 0xf0 + iota
+ CALL
+ CALLCODE
+ RETURN
+ DELEGATECALL
+ CREATE2
+ CALLEX = 0xf6
+ STATICCALL = 0xfa
+
+ REVERT = 0xfd
+ SELFDESTRUCT = 0xff
+)
+
+// Since the opcodes aren't all in order we can't use a regular slice.
+var opCodeToString = map[OpCode]string{
+ // 0x0 range - arithmetic ops.
+ STOP: "STOP",
+ ADD: "ADD",
+ MUL: "MUL",
+ SUB: "SUB",
+ DIV: "DIV",
+ SDIV: "SDIV",
+ MOD: "MOD",
+ SMOD: "SMOD",
+ EXP: "EXP",
+ NOT: "NOT",
+ LT: "LT",
+ GT: "GT",
+ SLT: "SLT",
+ SGT: "SGT",
+ EQ: "EQ",
+ ISZERO: "ISZERO",
+ SIGNEXTEND: "SIGNEXTEND",
+
+ // 0x10 range - bit ops.
+ AND: "AND",
+ OR: "OR",
+ XOR: "XOR",
+ BYTE: "BYTE",
+ SHL: "SHL",
+ SHR: "SHR",
+ SAR: "SAR",
+ ADDMOD: "ADDMOD",
+ MULMOD: "MULMOD",
+
+ // 0x20 range - crypto.
+ SHA3: "SHA3",
+
+ // 0x30 range - closure state.
+ ADDRESS: "ADDRESS",
+ BALANCE: "BALANCE",
+ BALANCEMC: "BALANCEMC",
+ ORIGIN: "ORIGIN",
+ CALLER: "CALLER",
+ CALLVALUE: "CALLVALUE",
+ CALLDATALOAD: "CALLDATALOAD",
+ CALLDATASIZE: "CALLDATASIZE",
+ CALLDATACOPY: "CALLDATACOPY",
+ CODESIZE: "CODESIZE",
+ CODECOPY: "CODECOPY",
+ GASPRICE: "GASPRICE",
+ EXTCODESIZE: "EXTCODESIZE",
+ EXTCODECOPY: "EXTCODECOPY",
+ RETURNDATASIZE: "RETURNDATASIZE",
+ RETURNDATACOPY: "RETURNDATACOPY",
+ EXTCODEHASH: "EXTCODEHASH",
+
+ // 0x40 range - block operations.
+ BLOCKHASH: "BLOCKHASH",
+ COINBASE: "COINBASE",
+ TIMESTAMP: "TIMESTAMP",
+ NUMBER: "NUMBER",
+ DIFFICULTY: "DIFFICULTY",
+ GASLIMIT: "GASLIMIT",
+ CHAINID: "CHAINID",
+ SELFBALANCE: "SELFBALANCE",
+
+ // 0x50 range - 'storage' and execution.
+ POP: "POP",
+ //DUP: "DUP",
+ //SWAP: "SWAP",
+ MLOAD: "MLOAD",
+ MSTORE: "MSTORE",
+ MSTORE8: "MSTORE8",
+ SLOAD: "SLOAD",
+ SSTORE: "SSTORE",
+ JUMP: "JUMP",
+ JUMPI: "JUMPI",
+ PC: "PC",
+ MSIZE: "MSIZE",
+ GAS: "GAS",
+ JUMPDEST: "JUMPDEST",
+ EMC: "EMC",
+
+ // 0x60 range - push.
+ PUSH1: "PUSH1",
+ PUSH2: "PUSH2",
+ PUSH3: "PUSH3",
+ PUSH4: "PUSH4",
+ PUSH5: "PUSH5",
+ PUSH6: "PUSH6",
+ PUSH7: "PUSH7",
+ PUSH8: "PUSH8",
+ PUSH9: "PUSH9",
+ PUSH10: "PUSH10",
+ PUSH11: "PUSH11",
+ PUSH12: "PUSH12",
+ PUSH13: "PUSH13",
+ PUSH14: "PUSH14",
+ PUSH15: "PUSH15",
+ PUSH16: "PUSH16",
+ PUSH17: "PUSH17",
+ PUSH18: "PUSH18",
+ PUSH19: "PUSH19",
+ PUSH20: "PUSH20",
+ PUSH21: "PUSH21",
+ PUSH22: "PUSH22",
+ PUSH23: "PUSH23",
+ PUSH24: "PUSH24",
+ PUSH25: "PUSH25",
+ PUSH26: "PUSH26",
+ PUSH27: "PUSH27",
+ PUSH28: "PUSH28",
+ PUSH29: "PUSH29",
+ PUSH30: "PUSH30",
+ PUSH31: "PUSH31",
+ PUSH32: "PUSH32",
+
+ DUP1: "DUP1",
+ DUP2: "DUP2",
+ DUP3: "DUP3",
+ DUP4: "DUP4",
+ DUP5: "DUP5",
+ DUP6: "DUP6",
+ DUP7: "DUP7",
+ DUP8: "DUP8",
+ DUP9: "DUP9",
+ DUP10: "DUP10",
+ DUP11: "DUP11",
+ DUP12: "DUP12",
+ DUP13: "DUP13",
+ DUP14: "DUP14",
+ DUP15: "DUP15",
+ DUP16: "DUP16",
+
+ SWAP1: "SWAP1",
+ SWAP2: "SWAP2",
+ SWAP3: "SWAP3",
+ SWAP4: "SWAP4",
+ SWAP5: "SWAP5",
+ SWAP6: "SWAP6",
+ SWAP7: "SWAP7",
+ SWAP8: "SWAP8",
+ SWAP9: "SWAP9",
+ SWAP10: "SWAP10",
+ SWAP11: "SWAP11",
+ SWAP12: "SWAP12",
+ SWAP13: "SWAP13",
+ SWAP14: "SWAP14",
+ SWAP15: "SWAP15",
+ SWAP16: "SWAP16",
+ LOG0: "LOG0",
+ LOG1: "LOG1",
+ LOG2: "LOG2",
+ LOG3: "LOG3",
+ LOG4: "LOG4",
+
+ // 0xf0 range.
+ CREATE: "CREATE",
+ CALL: "CALL",
+ CALLEX: "CALLEX",
+ RETURN: "RETURN",
+ CALLCODE: "CALLCODE",
+ DELEGATECALL: "DELEGATECALL",
+ CREATE2: "CREATE2",
+ STATICCALL: "STATICCALL",
+ REVERT: "REVERT",
+ SELFDESTRUCT: "SELFDESTRUCT",
+
+ PUSH: "PUSH",
+ DUP: "DUP",
+ SWAP: "SWAP",
+}
+
+func (op OpCode) String() string {
+ str := opCodeToString[op]
+ if len(str) == 0 {
+ return fmt.Sprintf("Missing opcode 0x%x", int(op))
+ }
+
+ return str
+}
+
+var stringToOp = map[string]OpCode{
+ "STOP": STOP,
+ "ADD": ADD,
+ "MUL": MUL,
+ "SUB": SUB,
+ "DIV": DIV,
+ "SDIV": SDIV,
+ "MOD": MOD,
+ "SMOD": SMOD,
+ "EXP": EXP,
+ "NOT": NOT,
+ "LT": LT,
+ "GT": GT,
+ "SLT": SLT,
+ "SGT": SGT,
+ "EQ": EQ,
+ "ISZERO": ISZERO,
+ "SIGNEXTEND": SIGNEXTEND,
+ "AND": AND,
+ "OR": OR,
+ "XOR": XOR,
+ "BYTE": BYTE,
+ "SHL": SHL,
+ "SHR": SHR,
+ "SAR": SAR,
+ "ADDMOD": ADDMOD,
+ "MULMOD": MULMOD,
+ "SHA3": SHA3,
+ "ADDRESS": ADDRESS,
+ "BALANCE": BALANCE,
+ "BALANCEMC": BALANCEMC,
+ "ORIGIN": ORIGIN,
+ "CALLER": CALLER,
+ "CALLVALUE": CALLVALUE,
+ "CALLDATALOAD": CALLDATALOAD,
+ "CALLDATASIZE": CALLDATASIZE,
+ "CALLDATACOPY": CALLDATACOPY,
+ "CHAINID": CHAINID,
+ "DELEGATECALL": DELEGATECALL,
+ "STATICCALL": STATICCALL,
+ "CODESIZE": CODESIZE,
+ "CODECOPY": CODECOPY,
+ "GASPRICE": GASPRICE,
+ "EXTCODESIZE": EXTCODESIZE,
+ "EXTCODECOPY": EXTCODECOPY,
+ "RETURNDATASIZE": RETURNDATASIZE,
+ "RETURNDATACOPY": RETURNDATACOPY,
+ "EXTCODEHASH": EXTCODEHASH,
+ "BLOCKHASH": BLOCKHASH,
+ "COINBASE": COINBASE,
+ "TIMESTAMP": TIMESTAMP,
+ "NUMBER": NUMBER,
+ "DIFFICULTY": DIFFICULTY,
+ "GASLIMIT": GASLIMIT,
+ "SELFBALANCE": SELFBALANCE,
+ "POP": POP,
+ "MLOAD": MLOAD,
+ "MSTORE": MSTORE,
+ "MSTORE8": MSTORE8,
+ "SLOAD": SLOAD,
+ "SSTORE": SSTORE,
+ "JUMP": JUMP,
+ "JUMPI": JUMPI,
+ "PC": PC,
+ "MSIZE": MSIZE,
+ "GAS": GAS,
+ "JUMPDEST": JUMPDEST,
+ "EMC": EMC,
+ "PUSH1": PUSH1,
+ "PUSH2": PUSH2,
+ "PUSH3": PUSH3,
+ "PUSH4": PUSH4,
+ "PUSH5": PUSH5,
+ "PUSH6": PUSH6,
+ "PUSH7": PUSH7,
+ "PUSH8": PUSH8,
+ "PUSH9": PUSH9,
+ "PUSH10": PUSH10,
+ "PUSH11": PUSH11,
+ "PUSH12": PUSH12,
+ "PUSH13": PUSH13,
+ "PUSH14": PUSH14,
+ "PUSH15": PUSH15,
+ "PUSH16": PUSH16,
+ "PUSH17": PUSH17,
+ "PUSH18": PUSH18,
+ "PUSH19": PUSH19,
+ "PUSH20": PUSH20,
+ "PUSH21": PUSH21,
+ "PUSH22": PUSH22,
+ "PUSH23": PUSH23,
+ "PUSH24": PUSH24,
+ "PUSH25": PUSH25,
+ "PUSH26": PUSH26,
+ "PUSH27": PUSH27,
+ "PUSH28": PUSH28,
+ "PUSH29": PUSH29,
+ "PUSH30": PUSH30,
+ "PUSH31": PUSH31,
+ "PUSH32": PUSH32,
+ "DUP1": DUP1,
+ "DUP2": DUP2,
+ "DUP3": DUP3,
+ "DUP4": DUP4,
+ "DUP5": DUP5,
+ "DUP6": DUP6,
+ "DUP7": DUP7,
+ "DUP8": DUP8,
+ "DUP9": DUP9,
+ "DUP10": DUP10,
+ "DUP11": DUP11,
+ "DUP12": DUP12,
+ "DUP13": DUP13,
+ "DUP14": DUP14,
+ "DUP15": DUP15,
+ "DUP16": DUP16,
+ "SWAP1": SWAP1,
+ "SWAP2": SWAP2,
+ "SWAP3": SWAP3,
+ "SWAP4": SWAP4,
+ "SWAP5": SWAP5,
+ "SWAP6": SWAP6,
+ "SWAP7": SWAP7,
+ "SWAP8": SWAP8,
+ "SWAP9": SWAP9,
+ "SWAP10": SWAP10,
+ "SWAP11": SWAP11,
+ "SWAP12": SWAP12,
+ "SWAP13": SWAP13,
+ "SWAP14": SWAP14,
+ "SWAP15": SWAP15,
+ "SWAP16": SWAP16,
+ "LOG0": LOG0,
+ "LOG1": LOG1,
+ "LOG2": LOG2,
+ "LOG3": LOG3,
+ "LOG4": LOG4,
+ "CREATE": CREATE,
+ "CREATE2": CREATE2,
+ "CALL": CALL,
+ "CALLEX": CALLEX,
+ "RETURN": RETURN,
+ "CALLCODE": CALLCODE,
+ "REVERT": REVERT,
+ "SELFDESTRUCT": SELFDESTRUCT,
+}
+
+// StringToOp finds the opcode whose name is stored in `str`.
+func StringToOp(str string) OpCode {
+ return stringToOp[str]
+}
diff --git a/core/vm/stack.go b/core/vm/stack.go
new file mode 100644
index 0000000..4c1b9e8
--- /dev/null
+++ b/core/vm/stack.go
@@ -0,0 +1,95 @@
+// Copyright 2014 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "fmt"
+ "math/big"
+)
+
+// Stack is an object for basic stack operations. Items popped to the stack are
+// expected to be changed and modified. stack does not take care of adding newly
+// initialised objects.
+type Stack struct {
+ data []*big.Int
+}
+
+func newstack() *Stack {
+ return &Stack{data: make([]*big.Int, 0, 1024)}
+}
+
+// Data returns the underlying big.Int array.
+func (st *Stack) Data() []*big.Int {
+ return st.data
+}
+
+func (st *Stack) push(d *big.Int) {
+ // NOTE push limit (1024) is checked in baseCheck
+ //stackItem := new(big.Int).Set(d)
+ //st.data = append(st.data, stackItem)
+ st.data = append(st.data, d)
+}
+func (st *Stack) pushN(ds ...*big.Int) {
+ st.data = append(st.data, ds...)
+}
+
+func (st *Stack) pop() (ret *big.Int) {
+ ret = st.data[len(st.data)-1]
+ st.data = st.data[:len(st.data)-1]
+ return
+}
+
+func (st *Stack) len() int {
+ return len(st.data)
+}
+
+func (st *Stack) swap(n int) {
+ st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
+}
+
+func (st *Stack) dup(pool *intPool, n int) {
+ st.push(pool.get().Set(st.data[st.len()-n]))
+}
+
+func (st *Stack) peek() *big.Int {
+ return st.data[st.len()-1]
+}
+
+// Back returns the n'th item in stack
+func (st *Stack) Back(n int) *big.Int {
+ return st.data[st.len()-n-1]
+}
+
+func (st *Stack) require(n int) error {
+ if st.len() < n {
+ return fmt.Errorf("stack underflow (%d <=> %d)", len(st.data), n)
+ }
+ return nil
+}
+
+// Print dumps the content of the stack
+func (st *Stack) Print() {
+ fmt.Println("### stack ###")
+ if len(st.data) > 0 {
+ for i, val := range st.data {
+ fmt.Printf("%-3d %v\n", i, val)
+ }
+ } else {
+ fmt.Println("-- empty --")
+ }
+ fmt.Println("#############")
+}
diff --git a/core/vm/stack_table.go b/core/vm/stack_table.go
new file mode 100644
index 0000000..192d024
--- /dev/null
+++ b/core/vm/stack_table.go
@@ -0,0 +1,42 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package vm
+
+import (
+ "github.com/ava-labs/coreth/params"
+)
+
+func minSwapStack(n int) int {
+ return minStack(n, n)
+}
+func maxSwapStack(n int) int {
+ return maxStack(n, n)
+}
+
+func minDupStack(n int) int {
+ return minStack(n, n+1)
+}
+func maxDupStack(n int) int {
+ return maxStack(n, n+1)
+}
+
+func maxStack(pop, push int) int {
+ return int(params.StackLimit) + pop - push
+}
+func minStack(pops, push int) int {
+ return pops
+}
diff --git a/coreth.go b/coreth.go
index b7e45af..78783ec 100644
--- a/coreth.go
+++ b/coreth.go
@@ -8,17 +8,17 @@ import (
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/coreth/miner"
"github.com/ava-labs/coreth/node"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/crypto"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/rpc"
"github.com/mattn/go-isatty"
)
diff --git a/eth/api.go b/eth/api.go
index 97a0758..b64090e 100644
--- a/eth/api.go
+++ b/eth/api.go
@@ -29,13 +29,13 @@ import (
"time"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/rlp"
"github.com/ava-labs/go-ethereum/trie"
)
diff --git a/eth/api_backend.go b/eth/api_backend.go
index c4ec8f0..1ca7b94 100644
--- a/eth/api_backend.go
+++ b/eth/api_backend.go
@@ -21,22 +21,21 @@ import (
"errors"
"math/big"
+ "github.com/ava-labs/coreth/accounts"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/bloombits"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/eth/gasprice"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/rpc"
- "github.com/ava-labs/go-ethereum/accounts"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/math"
- ethcore "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/bloombits"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
"github.com/ava-labs/go-ethereum/eth/downloader"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
- "github.com/ava-labs/go-ethereum/params"
)
// EthAPIBackend implements ethapi.Backend for full nodes
@@ -60,7 +59,7 @@ func (b *EthAPIBackend) AcceptedBlock() *types.Block {
}
func (b *EthAPIBackend) SetHead(number uint64) {
- b.eth.protocolManager.downloader.Cancel()
+ //b.eth.protocolManager.downloader.Cancel()
b.eth.blockchain.SetHead(number)
}
@@ -142,7 +141,7 @@ func (b *EthAPIBackend) GetTd(blockHash common.Hash) *big.Int {
return b.eth.blockchain.GetTdByHash(blockHash)
}
-func (b *EthAPIBackend) GetEVM(ctx context.Context, msg ethcore.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
+func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) {
state.SetBalance(msg.From(), math.MaxBig256)
vmError := func() error { return nil }
diff --git a/eth/api_tracer.go b/eth/api_tracer.go
index b87a9fe..c8a5307 100644
--- a/eth/api_tracer.go
+++ b/eth/api_tracer.go
@@ -28,19 +28,18 @@ import (
"sync"
"time"
+ "github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/eth/tracers"
"github.com/ava-labs/coreth/internal/ethapi"
- myrpc "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
- "github.com/ava-labs/go-ethereum/eth/tracers"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/rlp"
- "github.com/ava-labs/go-ethereum/rpc"
"github.com/ava-labs/go-ethereum/trie"
)
@@ -102,26 +101,26 @@ type txTraceTask struct {
// TraceChain returns the structured logs created during the execution of EVM
// between two blocks (excluding start) and returns them as a JSON object.
-func (api *PrivateDebugAPI) TraceChain(ctx context.Context, start, end myrpc.BlockNumber, config *TraceConfig) (*rpc.Subscription, error) {
+func (api *PrivateDebugAPI) TraceChain(ctx context.Context, start, end rpc.BlockNumber, config *TraceConfig) (*rpc.Subscription, error) {
// Fetch the block interval that we want to trace
var from, to *types.Block
switch start {
- case myrpc.PendingBlockNumber:
+ case rpc.PendingBlockNumber:
from = api.eth.miner.PendingBlock()
- case myrpc.LatestBlockNumber:
+ case rpc.LatestBlockNumber:
from = api.eth.blockchain.CurrentBlock()
- case myrpc.AcceptedBlockNumber:
+ case rpc.AcceptedBlockNumber:
from = api.eth.AcceptedBlock()
default:
from = api.eth.blockchain.GetBlockByNumber(uint64(start))
}
switch end {
- case myrpc.PendingBlockNumber:
+ case rpc.PendingBlockNumber:
to = api.eth.miner.PendingBlock()
- case myrpc.LatestBlockNumber:
+ case rpc.LatestBlockNumber:
to = api.eth.blockchain.CurrentBlock()
- case myrpc.AcceptedBlockNumber:
+ case rpc.AcceptedBlockNumber:
from = api.eth.AcceptedBlock()
default:
to = api.eth.blockchain.GetBlockByNumber(uint64(end))
@@ -357,16 +356,16 @@ func (api *PrivateDebugAPI) traceChain(ctx context.Context, start, end *types.Bl
// TraceBlockByNumber returns the structured logs created during the execution of
// EVM and returns them as a JSON object.
-func (api *PrivateDebugAPI) TraceBlockByNumber(ctx context.Context, number myrpc.BlockNumber, config *TraceConfig) ([]*txTraceResult, error) {
+func (api *PrivateDebugAPI) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *TraceConfig) ([]*txTraceResult, error) {
// Fetch the block that we want to trace
var block *types.Block
switch number {
- case myrpc.PendingBlockNumber:
+ case rpc.PendingBlockNumber:
block = api.eth.miner.PendingBlock()
- case myrpc.LatestBlockNumber:
+ case rpc.LatestBlockNumber:
block = api.eth.blockchain.CurrentBlock()
- case myrpc.AcceptedBlockNumber:
+ case rpc.AcceptedBlockNumber:
block = api.eth.AcceptedBlock()
default:
block = api.eth.blockchain.GetBlockByNumber(uint64(number))
diff --git a/eth/backend.go b/eth/backend.go
index 6d052e2..983909c 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -23,36 +23,34 @@ import (
"math/big"
"runtime"
"sync"
- "sync/atomic"
+ //"sync/atomic"
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/accounts/abi/bind"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/consensus/clique"
"github.com/ava-labs/coreth/consensus/dummy"
+ "github.com/ava-labs/coreth/consensus/ethash"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/bloombits"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/eth/filters"
"github.com/ava-labs/coreth/eth/gasprice"
"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/miner"
"github.com/ava-labs/coreth/node"
- myparams "github.com/ava-labs/coreth/params"
- "github.com/ava-labs/go-ethereum/accounts"
- "github.com/ava-labs/go-ethereum/accounts/abi/bind"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/consensus/clique"
- "github.com/ava-labs/go-ethereum/consensus/ethash"
- "github.com/ava-labs/go-ethereum/core/bloombits"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
"github.com/ava-labs/go-ethereum/eth/downloader"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/p2p"
- //"github.com/ava-labs/go-ethereum/p2p/enr"
- "github.com/ava-labs/go-ethereum/params"
"github.com/ava-labs/go-ethereum/rlp"
- "github.com/ava-labs/go-ethereum/rpc"
)
type LesServer interface {
@@ -78,10 +76,10 @@ type Ethereum struct {
server *p2p.Server
// Handlers
- txPool *core.TxPool
- blockchain *core.BlockChain
- protocolManager *ProtocolManager
- lesServer LesServer
+ txPool *core.TxPool
+ blockchain *core.BlockChain
+ //protocolManager *ProtocolManager
+ lesServer LesServer
// DB interfaces
chainDb ethdb.Database // Block chain database
@@ -253,8 +251,8 @@ func makeExtraData(extra []byte) []byte {
runtime.GOOS,
})
}
- if uint64(len(extra)) > myparams.MaximumExtraDataSize {
- log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", myparams.MaximumExtraDataSize)
+ if uint64(len(extra)) > params.MaximumExtraDataSize {
+ log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize)
extra = nil
}
return extra
@@ -486,8 +484,8 @@ func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb }
func (s *Ethereum) IsListening() bool { return true } // Always listening
func (s *Ethereum) EthVersion() int { return int(ProtocolVersions[0]) }
func (s *Ethereum) NetVersion() uint64 { return s.networkID }
-func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
-func (s *Ethereum) Synced() bool { return atomic.LoadUint32(&s.protocolManager.acceptTxs) == 1 }
+func (s *Ethereum) Downloader() *downloader.Downloader { return nil } // s.protocolManager.downloader }
+func (s *Ethereum) Synced() bool { return true } // atomic.LoadUint32(&s.protocolManager.acceptTxs) == 1 }
func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruning }
// Protocols implements node.Service, returning all the currently configured
diff --git a/eth/bloombits.go b/eth/bloombits.go
index e4792bb..7136c29 100644
--- a/eth/bloombits.go
+++ b/eth/bloombits.go
@@ -21,11 +21,11 @@ import (
"time"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/bloombits"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/bitutil"
- "github.com/ava-labs/go-ethereum/core/bloombits"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/ethdb"
)
diff --git a/eth/config.go b/eth/config.go
index ee0fdb7..18dbd8e 100644
--- a/eth/config.go
+++ b/eth/config.go
@@ -25,14 +25,14 @@ import (
"runtime"
"time"
+ "github.com/ava-labs/coreth/consensus/ethash"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/eth/gasprice"
"github.com/ava-labs/coreth/miner"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/consensus/ethash"
"github.com/ava-labs/go-ethereum/eth/downloader"
- "github.com/ava-labs/go-ethereum/params"
)
// DefaultConfig contains default settings for use on the Ethereum main net.
diff --git a/eth/filters/api.go b/eth/filters/api.go
index 9d9e757..a1d0b21 100644
--- a/eth/filters/api.go
+++ b/eth/filters/api.go
@@ -25,13 +25,13 @@ import (
"sync"
"time"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/rpc"
ethereum "github.com/ava-labs/go-ethereum"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
- "github.com/ava-labs/go-ethereum/rpc"
)
var (
diff --git a/eth/filters/filter.go b/eth/filters/filter.go
index b25792c..3e133d4 100644
--- a/eth/filters/filter.go
+++ b/eth/filters/filter.go
@@ -22,10 +22,10 @@ import (
"math/big"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/bloombits"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/bloombits"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
)
diff --git a/eth/filters/filter_system.go b/eth/filters/filter_system.go
index f8ee038..41427c7 100644
--- a/eth/filters/filter_system.go
+++ b/eth/filters/filter_system.go
@@ -26,14 +26,13 @@ import (
"time"
"github.com/ava-labs/coreth/core"
- myrpc "github.com/ava-labs/coreth/rpc"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/rpc"
ethereum "github.com/ava-labs/go-ethereum"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/rpc"
)
// Type determines the kind of filter and is used to put the filter in to
@@ -197,24 +196,24 @@ func (es *EventSystem) subscribe(sub *subscription) *Subscription {
// given criteria to the given logs channel. Default value for the from and to
// block is "latest". If the fromBlock > toBlock an error is returned.
func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*types.Log) (*Subscription, error) {
- var from, to myrpc.BlockNumber
+ var from, to rpc.BlockNumber
if crit.FromBlock == nil {
- from = myrpc.LatestBlockNumber
+ from = rpc.LatestBlockNumber
} else {
- from = myrpc.BlockNumber(crit.FromBlock.Int64())
+ from = rpc.BlockNumber(crit.FromBlock.Int64())
}
if crit.ToBlock == nil {
- to = myrpc.LatestBlockNumber
+ to = rpc.LatestBlockNumber
} else {
- to = myrpc.BlockNumber(crit.ToBlock.Int64())
+ to = rpc.BlockNumber(crit.ToBlock.Int64())
}
// only interested in pending logs
- if from == myrpc.PendingBlockNumber && to == myrpc.PendingBlockNumber {
+ if from == rpc.PendingBlockNumber && to == rpc.PendingBlockNumber {
return es.subscribePendingLogs(crit, logs), nil
}
// only interested in new mined logs
- if from == myrpc.LatestBlockNumber && to == myrpc.LatestBlockNumber {
+ if from == rpc.LatestBlockNumber && to == rpc.LatestBlockNumber {
return es.subscribeLogs(crit, logs), nil
}
// only interested in mined logs within a specific block range
@@ -222,11 +221,11 @@ func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
return es.subscribeLogs(crit, logs), nil
}
// interested in mined logs from a specific block number, new logs and pending logs
- if from >= myrpc.LatestBlockNumber && to == myrpc.PendingBlockNumber {
+ if from >= rpc.LatestBlockNumber && to == rpc.PendingBlockNumber {
return es.subscribeMinedPendingLogs(crit, logs), nil
}
// interested in logs from a specific block number to new mined blocks
- if from >= 0 && to == myrpc.LatestBlockNumber {
+ if from >= 0 && to == rpc.LatestBlockNumber {
return es.subscribeLogs(crit, logs), nil
}
return nil, fmt.Errorf("invalid from and to block combination: from > to")
diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go
index 9b632c2..23e49a6 100644
--- a/eth/gasprice/gasprice.go
+++ b/eth/gasprice/gasprice.go
@@ -22,11 +22,11 @@ import (
"sort"
"sync"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/internal/ethapi"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
)
var maxPrice = big.NewInt(500 * params.GWei)
diff --git a/eth/gen_config.go b/eth/gen_config.go
index 6b8ddf1..617a885 100644
--- a/eth/gen_config.go
+++ b/eth/gen_config.go
@@ -6,13 +6,13 @@ import (
"math/big"
"time"
+ "github.com/ava-labs/coreth/consensus/ethash"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/eth/gasprice"
"github.com/ava-labs/coreth/miner"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus/ethash"
"github.com/ava-labs/go-ethereum/eth/downloader"
- "github.com/ava-labs/go-ethereum/params"
)
// MarshalTOML marshals as TOML.
diff --git a/eth/handler.go b/eth/handler.go
deleted file mode 100644
index f502973..0000000
--- a/eth/handler.go
+++ /dev/null
@@ -1,844 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of the go-ethereum library.
-//
-// The go-ethereum library is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// The go-ethereum library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
-
-package eth
-
-import (
- "encoding/json"
- "errors"
- "fmt"
- "math"
- "math/big"
- "sync"
- "sync/atomic"
- "time"
-
- "github.com/ava-labs/coreth/core"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/eth/downloader"
- "github.com/ava-labs/go-ethereum/eth/fetcher"
- "github.com/ava-labs/go-ethereum/ethdb"
- "github.com/ava-labs/go-ethereum/event"
- "github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/p2p"
- "github.com/ava-labs/go-ethereum/p2p/enode"
- "github.com/ava-labs/go-ethereum/params"
- "github.com/ava-labs/go-ethereum/rlp"
- "github.com/ava-labs/go-ethereum/trie"
-)
-
-const (
- softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data.
- estHeaderRlpSize = 500 // Approximate size of an RLP encoded block header
-
- // txChanSize is the size of channel listening to NewTxsEvent.
- // The number is referenced from the size of tx pool.
- txChanSize = 4096
-
- // minimim number of peers to broadcast new blocks to
- minBroadcastPeers = 4
-)
-
-var (
- syncChallengeTimeout = 15 * time.Second // Time allowance for a node to reply to the sync progress challenge
-)
-
-func errResp(code errCode, format string, v ...interface{}) error {
- return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
-}
-
-type ProtocolManager struct {
- networkID uint64
-
- fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
- acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
-
- checkpointNumber uint64 // Block number for the sync progress validator to cross reference
- checkpointHash common.Hash // Block hash for the sync progress validator to cross reference
-
- txpool txPool
- blockchain *core.BlockChain
- maxPeers int
-
- downloader *downloader.Downloader
- fetcher *fetcher.Fetcher
- peers *peerSet
-
- eventMux *event.TypeMux
- txsCh chan core.NewTxsEvent
- txsSub event.Subscription
- minedBlockSub *event.TypeMuxSubscription
-
- whitelist map[uint64]common.Hash
-
- // channels for fetcher, syncer, txsyncLoop
- newPeerCh chan *peer
- txsyncCh chan *txsync
- quitSync chan struct{}
- noMorePeers chan struct{}
-
- // wait group is used for graceful shutdowns during downloading
- // and processing
- wg sync.WaitGroup
-}
-
-// NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
-// with the Ethereum network.
-func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCheckpoint, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database, cacheLimit int, whitelist map[uint64]common.Hash) (*ProtocolManager, error) {
- // Create the protocol manager with the base fields
- manager := &ProtocolManager{
- networkID: networkID,
- eventMux: mux,
- txpool: txpool,
- blockchain: blockchain,
- peers: newPeerSet(),
- whitelist: whitelist,
- newPeerCh: make(chan *peer),
- noMorePeers: make(chan struct{}),
- txsyncCh: make(chan *txsync),
- quitSync: make(chan struct{}),
- }
- if mode == downloader.FullSync {
- // The database seems empty as the current block is the genesis. Yet the fast
- // block is ahead, so fast sync was enabled for this node at a certain point.
- // The scenarios where this can happen is
- // * if the user manually (or via a bad block) rolled back a fast sync node
- // below the sync point.
- // * the last fast sync is not finished while user specifies a full sync this
- // time. But we don't have any recent state for full sync.
- // In these cases however it's safe to reenable fast sync.
- fullBlock, fastBlock := blockchain.CurrentBlock(), blockchain.CurrentFastBlock()
- if fullBlock.NumberU64() == 0 && fastBlock.NumberU64() > 0 {
- manager.fastSync = uint32(1)
- log.Warn("Switch sync mode from full sync to fast sync")
- }
- } else {
- if blockchain.CurrentBlock().NumberU64() > 0 {
- // Print warning log if database is not empty to run fast sync.
- log.Warn("Switch sync mode from fast sync to full sync")
- } else {
- // If fast sync was requested and our database is empty, grant it
- manager.fastSync = uint32(1)
- }
- }
- // If we have trusted checkpoints, enforce them on the chain
- if checkpoint != nil {
- manager.checkpointNumber = (checkpoint.SectionIndex+1)*params.CHTFrequency - 1
- manager.checkpointHash = checkpoint.SectionHead
- }
-
- // Construct the downloader (long sync) and its backing state bloom if fast
- // sync is requested. The downloader is responsible for deallocating the state
- // bloom when it's done.
- var stateBloom *trie.SyncBloom
- if atomic.LoadUint32(&manager.fastSync) == 1 {
- stateBloom = trie.NewSyncBloom(uint64(cacheLimit), chaindb)
- }
- manager.downloader = downloader.New(manager.checkpointNumber, chaindb, stateBloom, manager.eventMux, blockchain, nil, manager.removePeer)
-
- // Construct the fetcher (short sync)
- validator := func(header *types.Header) error {
- return engine.VerifyHeader(blockchain, header, true)
- }
- heighter := func() uint64 {
- return blockchain.CurrentBlock().NumberU64()
- }
- inserter := func(blocks types.Blocks) (int, error) {
- // If sync hasn't reached the checkpoint yet, deny importing weird blocks.
- //
- // Ideally we would also compare the head block's timestamp and similarly reject
- // the propagated block if the head is too old. Unfortunately there is a corner
- // case when starting new networks, where the genesis might be ancient (0 unix)
- // which would prevent full nodes from accepting it.
- if manager.blockchain.CurrentBlock().NumberU64() < manager.checkpointNumber {
- log.Warn("Unsynced yet, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
- return 0, nil
- }
- // If fast sync is running, deny importing weird blocks. This is a problematic
- // clause when starting up a new network, because fast-syncing miners might not
- // accept each others' blocks until a restart. Unfortunately we haven't figured
- // out a way yet where nodes can decide unilaterally whether the network is new
- // or not. This should be fixed if we figure out a solution.
- if atomic.LoadUint32(&manager.fastSync) == 1 {
- log.Warn("Fast syncing, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
- return 0, nil
- }
- n, err := manager.blockchain.InsertChain(blocks)
- if err == nil {
- atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
- }
- return n, err
- }
- manager.fetcher = fetcher.New(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
-
- return manager, nil
-}
-
-func (pm *ProtocolManager) makeProtocol(version uint) p2p.Protocol {
- length, ok := protocolLengths[version]
- if !ok {
- panic("makeProtocol for unknown version")
- }
-
- return p2p.Protocol{
- Name: protocolName,
- Version: version,
- Length: length,
- Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
- peer := pm.newPeer(int(version), p, rw)
- select {
- case pm.newPeerCh <- peer:
- pm.wg.Add(1)
- defer pm.wg.Done()
- return pm.handle(peer)
- case <-pm.quitSync:
- return p2p.DiscQuitting
- }
- },
- NodeInfo: func() interface{} {
- return pm.NodeInfo()
- },
- PeerInfo: func(id enode.ID) interface{} {
- if p := pm.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
- return p.Info()
- }
- return nil
- },
- }
-}
-
-func (pm *ProtocolManager) removePeer(id string) {
- // Short circuit if the peer was already removed
- peer := pm.peers.Peer(id)
- if peer == nil {
- return
- }
- log.Debug("Removing Ethereum peer", "peer", id)
-
- // Unregister the peer from the downloader and Ethereum peer set
- pm.downloader.UnregisterPeer(id)
- if err := pm.peers.Unregister(id); err != nil {
- log.Error("Peer removal failed", "peer", id, "err", err)
- }
- // Hard disconnect at the networking layer
- if peer != nil {
- peer.Peer.Disconnect(p2p.DiscUselessPeer)
- }
-}
-
-func (pm *ProtocolManager) Start(maxPeers int) {
- pm.maxPeers = maxPeers
-
- // broadcast transactions
- pm.txsCh = make(chan core.NewTxsEvent, txChanSize)
- pm.txsSub = pm.txpool.SubscribeNewTxsEvent(pm.txsCh)
- go pm.txBroadcastLoop()
-
- // broadcast mined blocks
- pm.minedBlockSub = pm.eventMux.Subscribe(core.NewMinedBlockEvent{})
- go pm.minedBroadcastLoop()
-
- // start sync handlers
- go pm.syncer()
- go pm.txsyncLoop()
-}
-
-func (pm *ProtocolManager) Stop() {
- log.Info("Stopping Ethereum protocol")
-
- pm.txsSub.Unsubscribe() // quits txBroadcastLoop
- pm.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
-
- // Quit the sync loop.
- // After this send has completed, no new peers will be accepted.
- pm.noMorePeers <- struct{}{}
-
- // Quit fetcher, txsyncLoop.
- close(pm.quitSync)
-
- // Disconnect existing sessions.
- // This also closes the gate for any new registrations on the peer set.
- // sessions which are already established but not added to pm.peers yet
- // will exit when they try to register.
- pm.peers.Close()
-
- // Wait for all peer handler goroutines and the loops to come down.
- pm.wg.Wait()
-
- log.Info("Ethereum protocol stopped")
-}
-
-func (pm *ProtocolManager) newPeer(pv int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
- return newPeer(pv, p, newMeteredMsgWriter(rw))
-}
-
-// handle is the callback invoked to manage the life cycle of an eth peer. When
-// this function terminates, the peer is disconnected.
-func (pm *ProtocolManager) handle(p *peer) error {
- // Ignore maxPeers if this is a trusted peer
- if pm.peers.Len() >= pm.maxPeers && !p.Peer.Info().Network.Trusted {
- return p2p.DiscTooManyPeers
- }
- p.Log().Debug("Ethereum peer connected", "name", p.Name())
-
- // Execute the Ethereum handshake
- var (
- genesis = pm.blockchain.Genesis()
- head = pm.blockchain.CurrentHeader()
- hash = head.Hash()
- number = head.Number.Uint64()
- td = pm.blockchain.GetTd(hash, number)
- )
- if err := p.Handshake(pm.networkID, td, hash, genesis.Hash()); err != nil {
- p.Log().Debug("Ethereum handshake failed", "err", err)
- return err
- }
- if rw, ok := p.rw.(*meteredMsgReadWriter); ok {
- rw.Init(p.version)
- }
- // Register the peer locally
- if err := pm.peers.Register(p); err != nil {
- p.Log().Error("Ethereum peer registration failed", "err", err)
- return err
- }
- defer pm.removePeer(p.id)
-
- // Register the peer in the downloader. If the downloader considers it banned, we disconnect
- if err := pm.downloader.RegisterPeer(p.id, p.version, p); err != nil {
- return err
- }
- // Propagate existing transactions. new transactions appearing
- // after this will be sent via broadcasts.
- pm.syncTransactions(p)
-
- // If we have a trusted CHT, reject all peers below that (avoid fast sync eclipse)
- if pm.checkpointHash != (common.Hash{}) {
- // Request the peer's checkpoint header for chain height/weight validation
- if err := p.RequestHeadersByNumber(pm.checkpointNumber, 1, 0, false); err != nil {
- return err
- }
- // Start a timer to disconnect if the peer doesn't reply in time
- p.syncDrop = time.AfterFunc(syncChallengeTimeout, func() {
- p.Log().Warn("Checkpoint challenge timed out, dropping", "addr", p.RemoteAddr(), "type", p.Name())
- pm.removePeer(p.id)
- })
- // Make sure it's cleaned up if the peer dies off
- defer func() {
- if p.syncDrop != nil {
- p.syncDrop.Stop()
- p.syncDrop = nil
- }
- }()
- }
- // If we have any explicit whitelist block hashes, request them
- for number := range pm.whitelist {
- if err := p.RequestHeadersByNumber(number, 1, 0, false); err != nil {
- return err
- }
- }
- // Handle incoming messages until the connection is torn down
- for {
- if err := pm.handleMsg(p); err != nil {
- p.Log().Debug("Ethereum message handling failed", "err", err)
- return err
- }
- }
-}
-
-// handleMsg is invoked whenever an inbound message is received from a remote
-// peer. The remote connection is torn down upon returning any error.
-func (pm *ProtocolManager) handleMsg(p *peer) error {
- // Read the next message from the remote peer, and ensure it's fully consumed
- msg, err := p.rw.ReadMsg()
- if err != nil {
- return err
- }
- if msg.Size > protocolMaxMsgSize {
- return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, protocolMaxMsgSize)
- }
- defer msg.Discard()
-
- // Handle the message depending on its contents
- switch {
- case msg.Code == StatusMsg:
- // Status messages should never arrive after the handshake
- return errResp(ErrExtraStatusMsg, "uncontrolled status message")
-
- // Block header query, collect the requested headers and reply
- case msg.Code == GetBlockHeadersMsg:
- // Decode the complex header query
- var query getBlockHeadersData
- if err := msg.Decode(&query); err != nil {
- return errResp(ErrDecode, "%v: %v", msg, err)
- }
- hashMode := query.Origin.Hash != (common.Hash{})
- first := true
- maxNonCanonical := uint64(100)
-
- // Gather headers until the fetch or network limits is reached
- var (
- bytes common.StorageSize
- headers []*types.Header
- unknown bool
- )
- for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit && len(headers) < downloader.MaxHeaderFetch {
- // Retrieve the next header satisfying the query
- var origin *types.Header
- if hashMode {
- if first {
- first = false
- origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
- if origin != nil {
- query.Origin.Number = origin.Number.Uint64()
- }
- } else {
- origin = pm.blockchain.GetHeader(query.Origin.Hash, query.Origin.Number)
- }
- } else {
- origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number)
- }
- if origin == nil {
- break
- }
- headers = append(headers, origin)
- bytes += estHeaderRlpSize
-
- // Advance to the next header of the query
- switch {
- case hashMode && query.Reverse:
- // Hash based traversal towards the genesis block
- ancestor := query.Skip + 1
- if ancestor == 0 {
- unknown = true
- } else {
- query.Origin.Hash, query.Origin.Number = pm.blockchain.GetAncestor(query.Origin.Hash, query.Origin.Number, ancestor, &maxNonCanonical)
- unknown = (query.Origin.Hash == common.Hash{})
- }
- case hashMode && !query.Reverse:
- // Hash based traversal towards the leaf block
- var (
- current = origin.Number.Uint64()
- next = current + query.Skip + 1
- )
- if next <= current {
- infos, _ := json.MarshalIndent(p.Peer.Info(), "", " ")
- p.Log().Warn("GetBlockHeaders skip overflow attack", "current", current, "skip", query.Skip, "next", next, "attacker", infos)
- unknown = true
- } else {
- if header := pm.blockchain.GetHeaderByNumber(next); header != nil {
- nextHash := header.Hash()
- expOldHash, _ := pm.blockchain.GetAncestor(nextHash, next, query.Skip+1, &maxNonCanonical)
- if expOldHash == query.Origin.Hash {
- query.Origin.Hash, query.Origin.Number = nextHash, next
- } else {
- unknown = true
- }
- } else {
- unknown = true
- }
- }
- case query.Reverse:
- // Number based traversal towards the genesis block
- if query.Origin.Number >= query.Skip+1 {
- query.Origin.Number -= query.Skip + 1
- } else {
- unknown = true
- }
-
- case !query.Reverse:
- // Number based traversal towards the leaf block
- query.Origin.Number += query.Skip + 1
- }
- }
- return p.SendBlockHeaders(headers)
-
- case msg.Code == BlockHeadersMsg:
- // A batch of headers arrived to one of our previous requests
- var headers []*types.Header
- if err := msg.Decode(&headers); err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- // If no headers were received, but we're expencting a checkpoint header, consider it that
- if len(headers) == 0 && p.syncDrop != nil {
- // Stop the timer either way, decide later to drop or not
- p.syncDrop.Stop()
- p.syncDrop = nil
-
- // If we're doing a fast sync, we must enforce the checkpoint block to avoid
- // eclipse attacks. Unsynced nodes are welcome to connect after we're done
- // joining the network
- if atomic.LoadUint32(&pm.fastSync) == 1 {
- p.Log().Warn("Dropping unsynced node during fast sync", "addr", p.RemoteAddr(), "type", p.Name())
- return errors.New("unsynced node cannot serve fast sync")
- }
- }
- // Filter out any explicitly requested headers, deliver the rest to the downloader
- filter := len(headers) == 1
- if filter {
- // If it's a potential sync progress check, validate the content and advertised chain weight
- if p.syncDrop != nil && headers[0].Number.Uint64() == pm.checkpointNumber {
- // Disable the sync drop timer
- p.syncDrop.Stop()
- p.syncDrop = nil
-
- // Validate the header and either drop the peer or continue
- if headers[0].Hash() != pm.checkpointHash {
- return errors.New("checkpoint hash mismatch")
- }
- return nil
- }
- // Otherwise if it's a whitelisted block, validate against the set
- if want, ok := pm.whitelist[headers[0].Number.Uint64()]; ok {
- if hash := headers[0].Hash(); want != hash {
- p.Log().Info("Whitelist mismatch, dropping peer", "number", headers[0].Number.Uint64(), "hash", hash, "want", want)
- return errors.New("whitelist block mismatch")
- }
- p.Log().Debug("Whitelist block verified", "number", headers[0].Number.Uint64(), "hash", want)
- }
- // Irrelevant of the fork checks, send the header to the fetcher just in case
- headers = pm.fetcher.FilterHeaders(p.id, headers, time.Now())
- }
- if len(headers) > 0 || !filter {
- err := pm.downloader.DeliverHeaders(p.id, headers)
- if err != nil {
- log.Debug("Failed to deliver headers", "err", err)
- }
- }
-
- case msg.Code == GetBlockBodiesMsg:
- // Decode the retrieval message
- msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
- if _, err := msgStream.List(); err != nil {
- return err
- }
- // Gather blocks until the fetch or network limits is reached
- var (
- hash common.Hash
- bytes int
- bodies []rlp.RawValue
- )
- for bytes < softResponseLimit && len(bodies) < downloader.MaxBlockFetch {
- // Retrieve the hash of the next block
- if err := msgStream.Decode(&hash); err == rlp.EOL {
- break
- } else if err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- // Retrieve the requested block body, stopping if enough was found
- if data := pm.blockchain.GetBodyRLP(hash); len(data) != 0 {
- bodies = append(bodies, data)
- bytes += len(data)
- }
- }
- return p.SendBlockBodiesRLP(bodies)
-
- case msg.Code == BlockBodiesMsg:
- // A batch of block bodies arrived to one of our previous requests
- var request blockBodiesData
- if err := msg.Decode(&request); err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- // Deliver them all to the downloader for queuing
- transactions := make([][]*types.Transaction, len(request))
- uncles := make([][]*types.Header, len(request))
-
- for i, body := range request {
- transactions[i] = body.Transactions
- uncles[i] = body.Uncles
- }
- // Filter out any explicitly requested bodies, deliver the rest to the downloader
- filter := len(transactions) > 0 || len(uncles) > 0
- if filter {
- transactions, uncles = pm.fetcher.FilterBodies(p.id, transactions, uncles, time.Now())
- }
- if len(transactions) > 0 || len(uncles) > 0 || !filter {
- err := pm.downloader.DeliverBodies(p.id, transactions, uncles)
- if err != nil {
- log.Debug("Failed to deliver bodies", "err", err)
- }
- }
-
- case p.version >= eth63 && msg.Code == GetNodeDataMsg:
- // Decode the retrieval message
- msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
- if _, err := msgStream.List(); err != nil {
- return err
- }
- // Gather state data until the fetch or network limits is reached
- var (
- hash common.Hash
- bytes int
- data [][]byte
- )
- for bytes < softResponseLimit && len(data) < downloader.MaxStateFetch {
- // Retrieve the hash of the next state entry
- if err := msgStream.Decode(&hash); err == rlp.EOL {
- break
- } else if err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- // Retrieve the requested state entry, stopping if enough was found
- if entry, err := pm.blockchain.TrieNode(hash); err == nil {
- data = append(data, entry)
- bytes += len(entry)
- }
- }
- return p.SendNodeData(data)
-
- case p.version >= eth63 && msg.Code == NodeDataMsg:
- // A batch of node state data arrived to one of our previous requests
- var data [][]byte
- if err := msg.Decode(&data); err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- // Deliver all to the downloader
- if err := pm.downloader.DeliverNodeData(p.id, data); err != nil {
- log.Debug("Failed to deliver node state data", "err", err)
- }
-
- case p.version >= eth63 && msg.Code == GetReceiptsMsg:
- // Decode the retrieval message
- msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
- if _, err := msgStream.List(); err != nil {
- return err
- }
- // Gather state data until the fetch or network limits is reached
- var (
- hash common.Hash
- bytes int
- receipts []rlp.RawValue
- )
- for bytes < softResponseLimit && len(receipts) < downloader.MaxReceiptFetch {
- // Retrieve the hash of the next block
- if err := msgStream.Decode(&hash); err == rlp.EOL {
- break
- } else if err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- // Retrieve the requested block's receipts, skipping if unknown to us
- results := pm.blockchain.GetReceiptsByHash(hash)
- if results == nil {
- if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
- continue
- }
- }
- // If known, encode and queue for response packet
- if encoded, err := rlp.EncodeToBytes(results); err != nil {
- log.Error("Failed to encode receipt", "err", err)
- } else {
- receipts = append(receipts, encoded)
- bytes += len(encoded)
- }
- }
- return p.SendReceiptsRLP(receipts)
-
- case p.version >= eth63 && msg.Code == ReceiptsMsg:
- // A batch of receipts arrived to one of our previous requests
- var receipts [][]*types.Receipt
- if err := msg.Decode(&receipts); err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- // Deliver all to the downloader
- if err := pm.downloader.DeliverReceipts(p.id, receipts); err != nil {
- log.Debug("Failed to deliver receipts", "err", err)
- }
-
- case msg.Code == NewBlockHashesMsg:
- var announces newBlockHashesData
- if err := msg.Decode(&announces); err != nil {
- return errResp(ErrDecode, "%v: %v", msg, err)
- }
- // Mark the hashes as present at the remote node
- for _, block := range announces {
- p.MarkBlock(block.Hash)
- }
- // Schedule all the unknown hashes for retrieval
- unknown := make(newBlockHashesData, 0, len(announces))
- for _, block := range announces {
- if !pm.blockchain.HasBlock(block.Hash, block.Number) {
- unknown = append(unknown, block)
- }
- }
- for _, block := range unknown {
- pm.fetcher.Notify(p.id, block.Hash, block.Number, time.Now(), p.RequestOneHeader, p.RequestBodies)
- }
-
- case msg.Code == NewBlockMsg:
- // Retrieve and decode the propagated block
- var request newBlockData
- if err := msg.Decode(&request); err != nil {
- return errResp(ErrDecode, "%v: %v", msg, err)
- }
- if err := request.sanityCheck(); err != nil {
- return err
- }
- request.Block.ReceivedAt = msg.ReceivedAt
- request.Block.ReceivedFrom = p
-
- // Mark the peer as owning the block and schedule it for import
- p.MarkBlock(request.Block.Hash())
- pm.fetcher.Enqueue(p.id, request.Block)
-
- // Assuming the block is importable by the peer, but possibly not yet done so,
- // calculate the head hash and TD that the peer truly must have.
- var (
- trueHead = request.Block.ParentHash()
- trueTD = new(big.Int).Sub(request.TD, request.Block.Difficulty())
- )
- // Update the peer's total difficulty if better than the previous
- if _, td := p.Head(); trueTD.Cmp(td) > 0 {
- p.SetHead(trueHead, trueTD)
-
- // Schedule a sync if above ours. Note, this will not fire a sync for a gap of
- // a single block (as the true TD is below the propagated block), however this
- // scenario should easily be covered by the fetcher.
- currentBlock := pm.blockchain.CurrentBlock()
- if trueTD.Cmp(pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())) > 0 {
- go pm.synchronise(p)
- }
- }
-
- case msg.Code == TxMsg:
- // Transactions arrived, make sure we have a valid and fresh chain to handle them
- if atomic.LoadUint32(&pm.acceptTxs) == 0 {
- break
- }
- // Transactions can be processed, parse all of them and deliver to the pool
- var txs []*types.Transaction
- if err := msg.Decode(&txs); err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- for i, tx := range txs {
- // Validate and mark the remote transaction
- if tx == nil {
- return errResp(ErrDecode, "transaction %d is nil", i)
- }
- p.MarkTransaction(tx.Hash())
- }
- pm.txpool.AddRemotes(txs)
-
- default:
- return errResp(ErrInvalidMsgCode, "%v", msg.Code)
- }
- return nil
-}
-
-// BroadcastBlock will either propagate a block to a subset of it's peers, or
-// will only announce it's availability (depending what's requested).
-func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
- hash := block.Hash()
- peers := pm.peers.PeersWithoutBlock(hash)
-
- // If propagation is requested, send to a subset of the peer
- if propagate {
- // Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
- var td *big.Int
- if parent := pm.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil {
- td = new(big.Int).Add(block.Difficulty(), pm.blockchain.GetTd(block.ParentHash(), block.NumberU64()-1))
- } else {
- log.Error("Propagating dangling block", "number", block.Number(), "hash", hash)
- return
- }
- // Send the block to a subset of our peers
- transferLen := int(math.Sqrt(float64(len(peers))))
- if transferLen < minBroadcastPeers {
- transferLen = minBroadcastPeers
- }
- if transferLen > len(peers) {
- transferLen = len(peers)
- }
- transfer := peers[:transferLen]
- for _, peer := range transfer {
- peer.AsyncSendNewBlock(block, td)
- }
- log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
- return
- }
- // Otherwise if the block is indeed in out own chain, announce it
- if pm.blockchain.HasBlock(hash, block.NumberU64()) {
- for _, peer := range peers {
- peer.AsyncSendNewBlockHash(block)
- }
- log.Trace("Announced block", "hash", hash, "recipients", len(peers), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
- }
-}
-
-// BroadcastTxs will propagate a batch of transactions to all peers which are not known to
-// already have the given transaction.
-func (pm *ProtocolManager) BroadcastTxs(txs types.Transactions) {
- var txset = make(map[*peer]types.Transactions)
-
- // Broadcast transactions to a batch of peers not knowing about it
- for _, tx := range txs {
- peers := pm.peers.PeersWithoutTx(tx.Hash())
- for _, peer := range peers {
- txset[peer] = append(txset[peer], tx)
- }
- log.Trace("Broadcast transaction", "hash", tx.Hash(), "recipients", len(peers))
- }
- // FIXME include this again: peers = peers[:int(math.Sqrt(float64(len(peers))))]
- for peer, txs := range txset {
- peer.AsyncSendTransactions(txs)
- }
-}
-
-// Mined broadcast loop
-func (pm *ProtocolManager) minedBroadcastLoop() {
- // automatically stops if unsubscribe
- for obj := range pm.minedBlockSub.Chan() {
- if ev, ok := obj.Data.(core.NewMinedBlockEvent); ok {
- pm.BroadcastBlock(ev.Block, true) // First propagate block to peers
- pm.BroadcastBlock(ev.Block, false) // Only then announce to the rest
- }
- }
-}
-
-func (pm *ProtocolManager) txBroadcastLoop() {
- for {
- select {
- case event := <-pm.txsCh:
- pm.BroadcastTxs(event.Txs)
-
- // Err() channel will be closed when unsubscribing.
- case <-pm.txsSub.Err():
- return
- }
- }
-}
-
-// NodeInfo represents a short summary of the Ethereum sub-protocol metadata
-// known about the host peer.
-type NodeInfo struct {
- Network uint64 `json:"network"` // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
- Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
- Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
- Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules
- Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block
-}
-
-// NodeInfo retrieves some protocol metadata about the running host node.
-func (pm *ProtocolManager) NodeInfo() *NodeInfo {
- currentBlock := pm.blockchain.CurrentBlock()
- return &NodeInfo{
- Network: pm.networkID,
- Difficulty: pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()),
- Genesis: pm.blockchain.Genesis().Hash(),
- Config: pm.blockchain.Config(),
- Head: currentBlock.Hash(),
- }
-}
diff --git a/eth/peer.go b/eth/peer.go
deleted file mode 100644
index 9ce569c..0000000
--- a/eth/peer.go
+++ /dev/null
@@ -1,546 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of the go-ethereum library.
-//
-// The go-ethereum library is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// The go-ethereum library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
-
-package eth
-
-import (
- "errors"
- "fmt"
- "math/big"
- "sync"
- "time"
-
- mapset "github.com/deckarep/golang-set"
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/p2p"
- "github.com/ava-labs/go-ethereum/rlp"
-)
-
-var (
- errClosed = errors.New("peer set is closed")
- errAlreadyRegistered = errors.New("peer is already registered")
- errNotRegistered = errors.New("peer is not registered")
-)
-
-const (
- maxKnownTxs = 32768 // Maximum transactions hashes to keep in the known list (prevent DOS)
- maxKnownBlocks = 1024 // Maximum block hashes to keep in the known list (prevent DOS)
-
- // maxQueuedTxs is the maximum number of transaction lists to queue up before
- // dropping broadcasts. This is a sensitive number as a transaction list might
- // contain a single transaction, or thousands.
- maxQueuedTxs = 128
-
- // maxQueuedProps is the maximum number of block propagations to queue up before
- // dropping broadcasts. There's not much point in queueing stale blocks, so a few
- // that might cover uncles should be enough.
- maxQueuedProps = 4
-
- // maxQueuedAnns is the maximum number of block announcements to queue up before
- // dropping broadcasts. Similarly to block propagations, there's no point to queue
- // above some healthy uncle limit, so use that.
- maxQueuedAnns = 4
-
- handshakeTimeout = 5 * time.Second
-)
-
-// PeerInfo represents a short summary of the Ethereum sub-protocol metadata known
-// about a connected peer.
-type PeerInfo struct {
- Version int `json:"version"` // Ethereum protocol version negotiated
- Difficulty *big.Int `json:"difficulty"` // Total difficulty of the peer's blockchain
- Head string `json:"head"` // SHA3 hash of the peer's best owned block
-}
-
-// propEvent is a block propagation, waiting for its turn in the broadcast queue.
-type propEvent struct {
- block *types.Block
- td *big.Int
-}
-
-type peer struct {
- id string
-
- *p2p.Peer
- rw p2p.MsgReadWriter
-
- version int // Protocol version negotiated
- syncDrop *time.Timer // Timed connection dropper if sync progress isn't validated in time
-
- head common.Hash
- td *big.Int
- lock sync.RWMutex
-
- knownTxs mapset.Set // Set of transaction hashes known to be known by this peer
- knownBlocks mapset.Set // Set of block hashes known to be known by this peer
- queuedTxs chan []*types.Transaction // Queue of transactions to broadcast to the peer
- queuedProps chan *propEvent // Queue of blocks to broadcast to the peer
- queuedAnns chan *types.Block // Queue of blocks to announce to the peer
- term chan struct{} // Termination channel to stop the broadcaster
-}
-
-func newPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
- return &peer{
- Peer: p,
- rw: rw,
- version: version,
- id: fmt.Sprintf("%x", p.ID().Bytes()[:8]),
- knownTxs: mapset.NewSet(),
- knownBlocks: mapset.NewSet(),
- queuedTxs: make(chan []*types.Transaction, maxQueuedTxs),
- queuedProps: make(chan *propEvent, maxQueuedProps),
- queuedAnns: make(chan *types.Block, maxQueuedAnns),
- term: make(chan struct{}),
- }
-}
-
-// broadcast is a write loop that multiplexes block propagations, announcements
-// and transaction broadcasts into the remote peer. The goal is to have an async
-// writer that does not lock up node internals.
-func (p *peer) broadcast() {
- for {
- select {
- case txs := <-p.queuedTxs:
- if err := p.SendTransactions(txs); err != nil {
- return
- }
- p.Log().Trace("Broadcast transactions", "count", len(txs))
-
- case prop := <-p.queuedProps:
- if err := p.SendNewBlock(prop.block, prop.td); err != nil {
- return
- }
- p.Log().Trace("Propagated block", "number", prop.block.Number(), "hash", prop.block.Hash(), "td", prop.td)
-
- case block := <-p.queuedAnns:
- if err := p.SendNewBlockHashes([]common.Hash{block.Hash()}, []uint64{block.NumberU64()}); err != nil {
- return
- }
- p.Log().Trace("Announced block", "number", block.Number(), "hash", block.Hash())
-
- case <-p.term:
- return
- }
- }
-}
-
-// close signals the broadcast goroutine to terminate.
-func (p *peer) close() {
- close(p.term)
-}
-
-// Info gathers and returns a collection of metadata known about a peer.
-func (p *peer) Info() *PeerInfo {
- hash, td := p.Head()
-
- return &PeerInfo{
- Version: p.version,
- Difficulty: td,
- Head: hash.Hex(),
- }
-}
-
-// Head retrieves a copy of the current head hash and total difficulty of the
-// peer.
-func (p *peer) Head() (hash common.Hash, td *big.Int) {
- p.lock.RLock()
- defer p.lock.RUnlock()
-
- copy(hash[:], p.head[:])
- return hash, new(big.Int).Set(p.td)
-}
-
-// SetHead updates the head hash and total difficulty of the peer.
-func (p *peer) SetHead(hash common.Hash, td *big.Int) {
- p.lock.Lock()
- defer p.lock.Unlock()
-
- copy(p.head[:], hash[:])
- p.td.Set(td)
-}
-
-// MarkBlock marks a block as known for the peer, ensuring that the block will
-// never be propagated to this particular peer.
-func (p *peer) MarkBlock(hash common.Hash) {
- // If we reached the memory allowance, drop a previously known block hash
- for p.knownBlocks.Cardinality() >= maxKnownBlocks {
- p.knownBlocks.Pop()
- }
- p.knownBlocks.Add(hash)
-}
-
-// MarkTransaction marks a transaction as known for the peer, ensuring that it
-// will never be propagated to this particular peer.
-func (p *peer) MarkTransaction(hash common.Hash) {
- // If we reached the memory allowance, drop a previously known transaction hash
- for p.knownTxs.Cardinality() >= maxKnownTxs {
- p.knownTxs.Pop()
- }
- p.knownTxs.Add(hash)
-}
-
-// SendTransactions sends transactions to the peer and includes the hashes
-// in its transaction hash set for future reference.
-func (p *peer) SendTransactions(txs types.Transactions) error {
- // Mark all the transactions as known, but ensure we don't overflow our limits
- for _, tx := range txs {
- p.knownTxs.Add(tx.Hash())
- }
- for p.knownTxs.Cardinality() >= maxKnownTxs {
- p.knownTxs.Pop()
- }
- return p2p.Send(p.rw, TxMsg, txs)
-}
-
-// AsyncSendTransactions queues list of transactions propagation to a remote
-// peer. If the peer's broadcast queue is full, the event is silently dropped.
-func (p *peer) AsyncSendTransactions(txs []*types.Transaction) {
- select {
- case p.queuedTxs <- txs:
- // Mark all the transactions as known, but ensure we don't overflow our limits
- for _, tx := range txs {
- p.knownTxs.Add(tx.Hash())
- }
- for p.knownTxs.Cardinality() >= maxKnownTxs {
- p.knownTxs.Pop()
- }
- default:
- p.Log().Debug("Dropping transaction propagation", "count", len(txs))
- }
-}
-
-// SendNewBlockHashes announces the availability of a number of blocks through
-// a hash notification.
-func (p *peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error {
- // Mark all the block hashes as known, but ensure we don't overflow our limits
- for _, hash := range hashes {
- p.knownBlocks.Add(hash)
- }
- for p.knownBlocks.Cardinality() >= maxKnownBlocks {
- p.knownBlocks.Pop()
- }
- request := make(newBlockHashesData, len(hashes))
- for i := 0; i < len(hashes); i++ {
- request[i].Hash = hashes[i]
- request[i].Number = numbers[i]
- }
- return p2p.Send(p.rw, NewBlockHashesMsg, request)
-}
-
-// AsyncSendNewBlockHash queues the availability of a block for propagation to a
-// remote peer. If the peer's broadcast queue is full, the event is silently
-// dropped.
-func (p *peer) AsyncSendNewBlockHash(block *types.Block) {
- select {
- case p.queuedAnns <- block:
- // Mark all the block hash as known, but ensure we don't overflow our limits
- p.knownBlocks.Add(block.Hash())
- for p.knownBlocks.Cardinality() >= maxKnownBlocks {
- p.knownBlocks.Pop()
- }
- default:
- p.Log().Debug("Dropping block announcement", "number", block.NumberU64(), "hash", block.Hash())
- }
-}
-
-// SendNewBlock propagates an entire block to a remote peer.
-func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error {
- // Mark all the block hash as known, but ensure we don't overflow our limits
- p.knownBlocks.Add(block.Hash())
- for p.knownBlocks.Cardinality() >= maxKnownBlocks {
- p.knownBlocks.Pop()
- }
- return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, td})
-}
-
-// AsyncSendNewBlock queues an entire block for propagation to a remote peer. If
-// the peer's broadcast queue is full, the event is silently dropped.
-func (p *peer) AsyncSendNewBlock(block *types.Block, td *big.Int) {
- select {
- case p.queuedProps <- &propEvent{block: block, td: td}:
- // Mark all the block hash as known, but ensure we don't overflow our limits
- p.knownBlocks.Add(block.Hash())
- for p.knownBlocks.Cardinality() >= maxKnownBlocks {
- p.knownBlocks.Pop()
- }
- default:
- p.Log().Debug("Dropping block propagation", "number", block.NumberU64(), "hash", block.Hash())
- }
-}
-
-// SendBlockHeaders sends a batch of block headers to the remote peer.
-func (p *peer) SendBlockHeaders(headers []*types.Header) error {
- return p2p.Send(p.rw, BlockHeadersMsg, headers)
-}
-
-// SendBlockBodies sends a batch of block contents to the remote peer.
-func (p *peer) SendBlockBodies(bodies []*blockBody) error {
- return p2p.Send(p.rw, BlockBodiesMsg, blockBodiesData(bodies))
-}
-
-// SendBlockBodiesRLP sends a batch of block contents to the remote peer from
-// an already RLP encoded format.
-func (p *peer) SendBlockBodiesRLP(bodies []rlp.RawValue) error {
- return p2p.Send(p.rw, BlockBodiesMsg, bodies)
-}
-
-// SendNodeDataRLP sends a batch of arbitrary internal data, corresponding to the
-// hashes requested.
-func (p *peer) SendNodeData(data [][]byte) error {
- return p2p.Send(p.rw, NodeDataMsg, data)
-}
-
-// SendReceiptsRLP sends a batch of transaction receipts, corresponding to the
-// ones requested from an already RLP encoded format.
-func (p *peer) SendReceiptsRLP(receipts []rlp.RawValue) error {
- return p2p.Send(p.rw, ReceiptsMsg, receipts)
-}
-
-// RequestOneHeader is a wrapper around the header query functions to fetch a
-// single header. It is used solely by the fetcher.
-func (p *peer) RequestOneHeader(hash common.Hash) error {
- p.Log().Debug("Fetching single header", "hash", hash)
- return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Hash: hash}, Amount: uint64(1), Skip: uint64(0), Reverse: false})
-}
-
-// RequestHeadersByHash fetches a batch of blocks' headers corresponding to the
-// specified header query, based on the hash of an origin block.
-func (p *peer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error {
- p.Log().Debug("Fetching batch of headers", "count", amount, "fromhash", origin, "skip", skip, "reverse", reverse)
- return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Hash: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse})
-}
-
-// RequestHeadersByNumber fetches a batch of blocks' headers corresponding to the
-// specified header query, based on the number of an origin block.
-func (p *peer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error {
- p.Log().Debug("Fetching batch of headers", "count", amount, "fromnum", origin, "skip", skip, "reverse", reverse)
- return p2p.Send(p.rw, GetBlockHeadersMsg, &getBlockHeadersData{Origin: hashOrNumber{Number: origin}, Amount: uint64(amount), Skip: uint64(skip), Reverse: reverse})
-}
-
-// RequestBodies fetches a batch of blocks' bodies corresponding to the hashes
-// specified.
-func (p *peer) RequestBodies(hashes []common.Hash) error {
- p.Log().Debug("Fetching batch of block bodies", "count", len(hashes))
- return p2p.Send(p.rw, GetBlockBodiesMsg, hashes)
-}
-
-// RequestNodeData fetches a batch of arbitrary data from a node's known state
-// data, corresponding to the specified hashes.
-func (p *peer) RequestNodeData(hashes []common.Hash) error {
- p.Log().Debug("Fetching batch of state data", "count", len(hashes))
- return p2p.Send(p.rw, GetNodeDataMsg, hashes)
-}
-
-// RequestReceipts fetches a batch of transaction receipts from a remote node.
-func (p *peer) RequestReceipts(hashes []common.Hash) error {
- p.Log().Debug("Fetching batch of receipts", "count", len(hashes))
- return p2p.Send(p.rw, GetReceiptsMsg, hashes)
-}
-
-// Handshake executes the eth protocol handshake, negotiating version number,
-// network IDs, difficulties, head and genesis blocks.
-func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash) error {
- // Send out own handshake in a new thread
- errc := make(chan error, 2)
- var status statusData // safe to read after two values have been received from errc
-
- go func() {
- errc <- p2p.Send(p.rw, StatusMsg, &statusData{
- ProtocolVersion: uint32(p.version),
- NetworkId: network,
- TD: td,
- CurrentBlock: head,
- GenesisBlock: genesis,
- })
- }()
- go func() {
- errc <- p.readStatus(network, &status, genesis)
- }()
- timeout := time.NewTimer(handshakeTimeout)
- defer timeout.Stop()
- for i := 0; i < 2; i++ {
- select {
- case err := <-errc:
- if err != nil {
- return err
- }
- case <-timeout.C:
- return p2p.DiscReadTimeout
- }
- }
- p.td, p.head = status.TD, status.CurrentBlock
- return nil
-}
-
-func (p *peer) readStatus(network uint64, status *statusData, genesis common.Hash) (err error) {
- msg, err := p.rw.ReadMsg()
- if err != nil {
- return err
- }
- if msg.Code != StatusMsg {
- return errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg)
- }
- if msg.Size > protocolMaxMsgSize {
- return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, protocolMaxMsgSize)
- }
- // Decode the handshake and make sure everything matches
- if err := msg.Decode(&status); err != nil {
- return errResp(ErrDecode, "msg %v: %v", msg, err)
- }
- if status.GenesisBlock != genesis {
- return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock[:8], genesis[:8])
- }
- if status.NetworkId != network {
- return errResp(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, network)
- }
- if int(status.ProtocolVersion) != p.version {
- return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, p.version)
- }
- return nil
-}
-
-// String implements fmt.Stringer.
-func (p *peer) String() string {
- return fmt.Sprintf("Peer %s [%s]", p.id,
- fmt.Sprintf("eth/%2d", p.version),
- )
-}
-
-// peerSet represents the collection of active peers currently participating in
-// the Ethereum sub-protocol.
-type peerSet struct {
- peers map[string]*peer
- lock sync.RWMutex
- closed bool
-}
-
-// newPeerSet creates a new peer set to track the active participants.
-func newPeerSet() *peerSet {
- return &peerSet{
- peers: make(map[string]*peer),
- }
-}
-
-// Register injects a new peer into the working set, or returns an error if the
-// peer is already known. If a new peer it registered, its broadcast loop is also
-// started.
-func (ps *peerSet) Register(p *peer) error {
- ps.lock.Lock()
- defer ps.lock.Unlock()
-
- if ps.closed {
- return errClosed
- }
- if _, ok := ps.peers[p.id]; ok {
- return errAlreadyRegistered
- }
- ps.peers[p.id] = p
- go p.broadcast()
-
- return nil
-}
-
-// Unregister removes a remote peer from the active set, disabling any further
-// actions to/from that particular entity.
-func (ps *peerSet) Unregister(id string) error {
- ps.lock.Lock()
- defer ps.lock.Unlock()
-
- p, ok := ps.peers[id]
- if !ok {
- return errNotRegistered
- }
- delete(ps.peers, id)
- p.close()
-
- return nil
-}
-
-// Peer retrieves the registered peer with the given id.
-func (ps *peerSet) Peer(id string) *peer {
- ps.lock.RLock()
- defer ps.lock.RUnlock()
-
- return ps.peers[id]
-}
-
-// Len returns if the current number of peers in the set.
-func (ps *peerSet) Len() int {
- ps.lock.RLock()
- defer ps.lock.RUnlock()
-
- return len(ps.peers)
-}
-
-// PeersWithoutBlock retrieves a list of peers that do not have a given block in
-// their set of known hashes.
-func (ps *peerSet) PeersWithoutBlock(hash common.Hash) []*peer {
- ps.lock.RLock()
- defer ps.lock.RUnlock()
-
- list := make([]*peer, 0, len(ps.peers))
- for _, p := range ps.peers {
- if !p.knownBlocks.Contains(hash) {
- list = append(list, p)
- }
- }
- return list
-}
-
-// PeersWithoutTx retrieves a list of peers that do not have a given transaction
-// in their set of known hashes.
-func (ps *peerSet) PeersWithoutTx(hash common.Hash) []*peer {
- ps.lock.RLock()
- defer ps.lock.RUnlock()
-
- list := make([]*peer, 0, len(ps.peers))
- for _, p := range ps.peers {
- if !p.knownTxs.Contains(hash) {
- list = append(list, p)
- }
- }
- return list
-}
-
-// BestPeer retrieves the known peer with the currently highest total difficulty.
-func (ps *peerSet) BestPeer() *peer {
- ps.lock.RLock()
- defer ps.lock.RUnlock()
-
- var (
- bestPeer *peer
- bestTd *big.Int
- )
- for _, p := range ps.peers {
- if _, td := p.Head(); bestPeer == nil || td.Cmp(bestTd) > 0 {
- bestPeer, bestTd = p, td
- }
- }
- return bestPeer
-}
-
-// Close disconnects all peers.
-// No new peers can be registered after Close has returned.
-func (ps *peerSet) Close() {
- ps.lock.Lock()
- defer ps.lock.Unlock()
-
- for _, p := range ps.peers {
- p.Disconnect(p2p.DiscQuitting)
- }
- ps.closed = true
-}
diff --git a/eth/protocol.go b/eth/protocol.go
index 07d2def..e205aad 100644
--- a/eth/protocol.go
+++ b/eth/protocol.go
@@ -22,8 +22,8 @@ import (
"math/big"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/rlp"
)
diff --git a/eth/sync.go b/eth/sync.go
deleted file mode 100644
index 6f067a3..0000000
--- a/eth/sync.go
+++ /dev/null
@@ -1,216 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of the go-ethereum library.
-//
-// The go-ethereum library is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// The go-ethereum library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
-
-package eth
-
-import (
- "math/rand"
- "sync/atomic"
- "time"
-
- "github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/eth/downloader"
- "github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/p2p/enode"
-)
-
-const (
- forceSyncCycle = 10 * time.Second // Time interval to force syncs, even if few peers are available
- minDesiredPeerCount = 5 // Amount of peers desired to start syncing
-
- // This is the target size for the packs of transactions sent by txsyncLoop.
- // A pack can get larger than this if a single transactions exceeds this size.
- txsyncPackSize = 100 * 1024
-)
-
-type txsync struct {
- p *peer
- txs []*types.Transaction
-}
-
-// syncTransactions starts sending all currently pending transactions to the given peer.
-func (pm *ProtocolManager) syncTransactions(p *peer) {
- var txs types.Transactions
- pending, _ := pm.txpool.Pending()
- for _, batch := range pending {
- txs = append(txs, batch...)
- }
- if len(txs) == 0 {
- return
- }
- select {
- case pm.txsyncCh <- &txsync{p, txs}:
- case <-pm.quitSync:
- }
-}
-
-// txsyncLoop takes care of the initial transaction sync for each new
-// connection. When a new peer appears, we relay all currently pending
-// transactions. In order to minimise egress bandwidth usage, we send
-// the transactions in small packs to one peer at a time.
-func (pm *ProtocolManager) txsyncLoop() {
- var (
- pending = make(map[enode.ID]*txsync)
- sending = false // whether a send is active
- pack = new(txsync) // the pack that is being sent
- done = make(chan error, 1) // result of the send
- )
-
- // send starts a sending a pack of transactions from the sync.
- send := func(s *txsync) {
- // Fill pack with transactions up to the target size.
- size := common.StorageSize(0)
- pack.p = s.p
- pack.txs = pack.txs[:0]
- for i := 0; i < len(s.txs) && size < txsyncPackSize; i++ {
- pack.txs = append(pack.txs, s.txs[i])
- size += s.txs[i].Size()
- }
- // Remove the transactions that will be sent.
- s.txs = s.txs[:copy(s.txs, s.txs[len(pack.txs):])]
- if len(s.txs) == 0 {
- delete(pending, s.p.ID())
- }
- // Send the pack in the background.
- s.p.Log().Trace("Sending batch of transactions", "count", len(pack.txs), "bytes", size)
- sending = true
- go func() { done <- pack.p.SendTransactions(pack.txs) }()
- }
-
- // pick chooses the next pending sync.
- pick := func() *txsync {
- if len(pending) == 0 {
- return nil
- }
- n := rand.Intn(len(pending)) + 1
- for _, s := range pending {
- if n--; n == 0 {
- return s
- }
- }
- return nil
- }
-
- for {
- select {
- case s := <-pm.txsyncCh:
- pending[s.p.ID()] = s
- if !sending {
- send(s)
- }
- case err := <-done:
- sending = false
- // Stop tracking peers that cause send failures.
- if err != nil {
- pack.p.Log().Debug("Transaction send failed", "err", err)
- delete(pending, pack.p.ID())
- }
- // Schedule the next send.
- if s := pick(); s != nil {
- send(s)
- }
- case <-pm.quitSync:
- return
- }
- }
-}
-
-// syncer is responsible for periodically synchronising with the network, both
-// downloading hashes and blocks as well as handling the announcement handler.
-func (pm *ProtocolManager) syncer() {
- // Start and ensure cleanup of sync mechanisms
- pm.fetcher.Start()
- defer pm.fetcher.Stop()
- defer pm.downloader.Terminate()
-
- // Wait for different events to fire synchronisation operations
- forceSync := time.NewTicker(forceSyncCycle)
- defer forceSync.Stop()
-
- for {
- select {
- case <-pm.newPeerCh:
- // Make sure we have peers to select from, then sync
- if pm.peers.Len() < minDesiredPeerCount {
- break
- }
- go pm.synchronise(pm.peers.BestPeer())
-
- case <-forceSync.C:
- // Force a sync even if not enough peers are present
- go pm.synchronise(pm.peers.BestPeer())
-
- case <-pm.noMorePeers:
- return
- }
- }
-}
-
-// synchronise tries to sync up our local block chain with a remote peer.
-func (pm *ProtocolManager) synchronise(peer *peer) {
- // Short circuit if no peers are available
- if peer == nil {
- return
- }
- // Make sure the peer's TD is higher than our own
- currentBlock := pm.blockchain.CurrentBlock()
- td := pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())
-
- pHead, pTd := peer.Head()
- if pTd.Cmp(td) <= 0 {
- return
- }
- // Otherwise try to sync with the downloader
- mode := downloader.FullSync
- if atomic.LoadUint32(&pm.fastSync) == 1 {
- // Fast sync was explicitly requested, and explicitly granted
- mode = downloader.FastSync
- }
- if mode == downloader.FastSync {
- // Make sure the peer's total difficulty we are synchronizing is higher.
- if pm.blockchain.GetTdByHash(pm.blockchain.CurrentFastBlock().Hash()).Cmp(pTd) >= 0 {
- return
- }
- }
- // Run the sync cycle, and disable fast sync if we've went past the pivot block
- if err := pm.downloader.Synchronise(peer.id, pHead, pTd, mode); err != nil {
- return
- }
- if atomic.LoadUint32(&pm.fastSync) == 1 {
- log.Info("Fast sync complete, auto disabling")
- atomic.StoreUint32(&pm.fastSync, 0)
- }
- // If we've successfully finished a sync cycle and passed any required checkpoint,
- // enable accepting transactions from the network.
- head := pm.blockchain.CurrentBlock()
- if head.NumberU64() >= pm.checkpointNumber {
- // Checkpoint passed, sanity check the timestamp to have a fallback mechanism
- // for non-checkpointed (number = 0) private networks.
- if head.Time() >= uint64(time.Now().AddDate(0, -1, 0).Unix()) {
- atomic.StoreUint32(&pm.acceptTxs, 1)
- }
- }
- if head.NumberU64() > 0 {
- // We've completed a sync cycle, notify all peers of new state. This path is
- // essential in star-topology networks where a gateway node needs to notify
- // all its out-of-date peers of the availability of a new block. This failure
- // scenario will most often crop up in private and hackathon networks with
- // degenerate connectivity, but it should be healthy for the mainnet too to
- // more reliably update peers or the local TD state.
- go pm.BroadcastBlock(head, false)
- }
-}
diff --git a/eth/tracers/internal/tracers/4byte_tracer.js b/eth/tracers/internal/tracers/4byte_tracer.js
new file mode 100644
index 0000000..462b4ad
--- /dev/null
+++ b/eth/tracers/internal/tracers/4byte_tracer.js
@@ -0,0 +1,86 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// 4byteTracer searches for 4byte-identifiers, and collects them for post-processing.
+// It collects the methods identifiers along with the size of the supplied data, so
+// a reversed signature can be matched against the size of the data.
+//
+// Example:
+// > debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byteTracer"})
+// {
+// 0x27dc297e-128: 1,
+// 0x38cc4831-0: 2,
+// 0x524f3889-96: 1,
+// 0xadf59f99-288: 1,
+// 0xc281d19e-0: 1
+// }
+{
+ // ids aggregates the 4byte ids found.
+ ids : {},
+
+ // callType returns 'false' for non-calls, or the peek-index for the first param
+ // after 'value', i.e. meminstart.
+ callType: function(opstr){
+ switch(opstr){
+ case "CALL": case "CALLCODE":
+ // gas, addr, val, memin, meminsz, memout, memoutsz
+ return 3; // stack ptr to memin
+
+ case "DELEGATECALL": case "STATICCALL":
+ // gas, addr, memin, meminsz, memout, memoutsz
+ return 2; // stack ptr to memin
+ }
+ return false;
+ },
+
+ // store save the given indentifier and datasize.
+ store: function(id, size){
+ var key = "" + toHex(id) + "-" + size;
+ this.ids[key] = this.ids[key] + 1 || 1;
+ },
+
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) {
+ // Skip any opcodes that are not internal calls
+ var ct = this.callType(log.op.toString());
+ if (!ct) {
+ return;
+ }
+ // Skip any pre-compile invocations, those are just fancy opcodes
+ if (isPrecompiled(toAddress(log.stack.peek(1).toString(16)))) {
+ return;
+ }
+ // Gather internal call details
+ var inSz = log.stack.peek(ct + 1).valueOf();
+ if (inSz >= 4) {
+ var inOff = log.stack.peek(ct).valueOf();
+ this.store(log.memory.slice(inOff, inOff + 4), inSz-4);
+ }
+ },
+
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) { },
+
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx) {
+ // Save the outer calldata also
+ if (ctx.input.length >= 4) {
+ this.store(slice(ctx.input, 0, 4), ctx.input.length-4)
+ }
+ return this.ids;
+ },
+}
diff --git a/eth/tracers/internal/tracers/assets.go b/eth/tracers/internal/tracers/assets.go
new file mode 100644
index 0000000..d0a0bf7
--- /dev/null
+++ b/eth/tracers/internal/tracers/assets.go
@@ -0,0 +1,463 @@
+// Code generated by go-bindata. DO NOT EDIT.
+// sources:
+// 4byte_tracer.js (2.933kB)
+// bigram_tracer.js (1.712kB)
+// call_tracer.js (8.643kB)
+// evmdis_tracer.js (4.194kB)
+// noop_tracer.js (1.271kB)
+// opcount_tracer.js (1.372kB)
+// prestate_tracer.js (4.234kB)
+// trigram_tracer.js (1.788kB)
+// unigram_tracer.js (1.51kB)
+
+package tracers
+
+import (
+ "bytes"
+ "compress/gzip"
+ "crypto/sha256"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+)
+
+func bindataRead(data []byte, name string) ([]byte, error) {
+ gz, err := gzip.NewReader(bytes.NewBuffer(data))
+ if err != nil {
+ return nil, fmt.Errorf("read %q: %v", name, err)
+ }
+
+ var buf bytes.Buffer
+ _, err = io.Copy(&buf, gz)
+ clErr := gz.Close()
+
+ if err != nil {
+ return nil, fmt.Errorf("read %q: %v", name, err)
+ }
+ if clErr != nil {
+ return nil, err
+ }
+
+ return buf.Bytes(), nil
+}
+
+type asset struct {
+ bytes []byte
+ info os.FileInfo
+ digest [sha256.Size]byte
+}
+
+type bindataFileInfo struct {
+ name string
+ size int64
+ mode os.FileMode
+ modTime time.Time
+}
+
+func (fi bindataFileInfo) Name() string {
+ return fi.name
+}
+func (fi bindataFileInfo) Size() int64 {
+ return fi.size
+}
+func (fi bindataFileInfo) Mode() os.FileMode {
+ return fi.mode
+}
+func (fi bindataFileInfo) ModTime() time.Time {
+ return fi.modTime
+}
+func (fi bindataFileInfo) IsDir() bool {
+ return false
+}
+func (fi bindataFileInfo) Sys() interface{} {
+ return nil
+}
+
+var __4byte_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x56\x5b\x6f\xdb\x4a\x0e\x7e\xb6\x7f\x05\xd7\x2f\xb5\x51\x59\x8e\x2f\x89\x2f\xd9\x16\xf0\xe6\xa4\x6d\x80\x9c\x24\x88\xdd\x3d\x28\x16\xfb\x30\x9e\xa1\xac\xd9\xc8\x33\xc2\x0c\xe5\x4b\x73\xf2\xdf\x17\x1c\x49\x89\x93\xd3\x62\xbb\x4f\x96\x47\xc3\x8f\x1f\xc9\x8f\xa4\x7a\x3d\xb8\xb0\xf9\xc1\xe9\x75\x4a\x30\x38\xe9\x8f\x61\x99\x22\xac\x6d\x17\x29\x45\x87\xc5\x06\xe6\x05\xa5\xd6\xf9\x66\xaf\x07\xcb\x54\x7b\x48\x74\x86\xa0\x3d\xe4\xc2\x11\xd8\x04\xe8\xcd\xfd\x4c\xaf\x9c\x70\x87\xb8\xd9\xeb\x95\x36\x3f\x7c\xcd\x08\x89\x43\x04\x6f\x13\xda\x09\x87\x33\x38\xd8\x02\xa4\x30\xe0\x50\x69\x4f\x4e\xaf\x0a\x42\xd0\x04\xc2\xa8\x9e\x75\xb0\xb1\x4a\x27\x07\x86\xd4\x04\x85\x51\xe8\x82\x6b\x42\xb7\xf1\x35\x8f\xcf\x37\x5f\xe1\x1a\xbd\x47\x07\x9f\xd1\xa0\x13\x19\xdc\x15\xab\x4c\x4b\xb8\xd6\x12\x8d\x47\x10\x1e\x72\x3e\xf1\x29\x2a\x58\x05\x38\x36\xfc\xc4\x54\x16\x15\x15\xf8\x64\x0b\xa3\x04\x69\x6b\x22\x40\xcd\xcc\x61\x8b\xce\x6b\x6b\x60\x58\xbb\xaa\x00\x23\xb0\x8e\x41\xda\x82\x38\x00\x07\x36\x67\xbb\x0e\x08\x73\x80\x4c\xd0\x8b\xe9\x2f\x24\xe4\x25\x6e\x05\xda\x04\x37\xa9\xcd\x11\x28\x15\xc4\x51\xef\x74\x96\xc1\x0a\xa1\xf0\x98\x14\x59\xc4\x68\xab\x82\xe0\x8f\xab\xe5\x97\xdb\xaf\x4b\x98\xdf\x7c\x83\x3f\xe6\xf7\xf7\xf3\x9b\xe5\xb7\x73\xd8\x69\x4a\x6d\x41\x80\x5b\x2c\xa1\xf4\x26\xcf\x34\x2a\xd8\x09\xe7\x84\xa1\x03\xd8\x84\x11\x7e\xbf\xbc\xbf\xf8\x32\xbf\x59\xce\xff\x71\x75\x7d\xb5\xfc\x06\xd6\xc1\xa7\xab\xe5\xcd\xe5\x62\x01\x9f\x6e\xef\x61\x0e\x77\xf3\xfb\xe5\xd5\xc5\xd7\xeb\xf9\x3d\xdc\x7d\xbd\xbf\xbb\x5d\x5c\xc6\xb0\x40\x66\x85\x6c\xff\xbf\x73\x9e\x84\xea\x39\x04\x85\x24\x74\xe6\xeb\x4c\x7c\xb3\x05\xf8\xd4\x16\x99\x82\x54\x6c\x11\x1c\x4a\xd4\x5b\x54\x20\x40\xda\xfc\xf0\xcb\x45\x65\x2c\x91\x59\xb3\x0e\x31\xff\x54\x90\x70\x95\x80\xb1\x14\x81\x47\x84\xbf\xa7\x44\xf9\xac\xd7\xdb\xed\x76\xf1\xda\x14\xb1\x75\xeb\x5e\x56\xc2\xf9\xde\xc7\xb8\xc9\x98\xa3\xd5\x81\x70\xe9\x84\x44\x07\x1e\x85\x93\x29\xfa\x10\x4c\x78\xd1\xd5\x0a\x0d\xe9\x44\xa3\xf3\x11\x8b\x14\xa4\xcd\x32\x94\xe4\x99\xc1\x26\x5c\xcc\xad\xa7\x6e\xee\xac\x44\xef\xb5\x59\x73\xe0\x70\x45\xaf\x2e\xc2\x06\x29\xb5\xca\xc3\x11\xdc\xdb\x68\xbc\xfe\x8e\x75\x36\x7c\x91\x97\x65\x54\x82\x44\x04\xde\x86\xe8\xc1\x21\xcb\x0c\x15\x78\xbd\x36\x82\x0a\x87\xa1\x97\x56\x08\x1b\x41\x92\xc5\x2e\xd6\x42\x1b\x4f\x7f\x01\x64\x9c\xba\x22\x97\x7b\xb1\xc9\x33\x9c\xf1\x33\xc0\x47\x50\xb8\x2a\xd6\x31\x71\x0a\x96\x4e\x18\x2f\x24\x8b\xbb\x0d\xad\x93\xfd\xa0\x3f\xc2\xd3\xe9\x18\x87\xa7\x4a\x9c\x4c\x86\x67\xd3\x41\x72\x3a\x9c\x9c\xf5\x47\x7d\x3c\x9b\x26\xa3\x31\x4e\xc7\xc3\xd5\x40\x9e\x9e\xe1\x58\x4c\x4e\xc6\xc3\x55\x1f\xc5\xc9\x24\x51\xe3\xd3\x71\x1f\xa7\x0a\x5b\x11\x3c\x06\x60\x37\x83\xd6\x51\xa6\x5b\x4f\x9d\xd2\xfb\x63\xf9\x03\x70\xb2\x1f\x8c\x95\x1c\x4c\xc7\xd8\xed\x0f\x26\x33\xe8\x47\x2f\x6f\x86\x13\x29\x47\x93\x61\xbf\x7b\x32\x83\xc1\xd1\xf9\xe9\x60\x94\x0c\x27\x93\x69\x77\x7a\xf6\xda\x40\xa8\xe4\x74\x9a\x4c\xa7\xdd\xc1\xe4\x0d\x94\x1c\x4c\xfa\xaa\x3f\x45\x86\xea\x97\xc7\x4f\xcd\xc7\x66\x83\x07\x8e\xf2\x20\xd6\x6b\x87\x6b\x41\x58\x56\x2d\x30\x0e\x2f\x12\x1e\x16\x71\xb3\xc1\xcf\x33\x78\x7c\x8a\x9a\xc1\x46\x8a\x2c\x5b\x1e\x72\x56\x35\x15\xce\x78\x78\x97\x88\xcc\xe3\xbb\xa0\x0b\x63\x4d\x97\x2f\x78\x1e\x1f\x01\x2f\x47\x7c\xe8\x6a\xa3\x70\x1f\x2e\xf0\x51\xa2\x9d\x27\x1e\xb3\x62\x13\x10\x45\xc2\xd3\xe4\xdd\x56\x64\x05\xbe\x8b\x40\xc7\x18\xc3\x06\x37\x5c\x54\xe1\x28\x6e\x36\x6a\x97\x33\x48\x0a\x53\x56\xca\xe6\x9e\x5c\xe7\xb1\xd9\x68\xf8\x9d\x26\x99\x1e\x1d\x48\xe1\x11\x5a\x17\xf3\xeb\xeb\xd6\x0c\x5e\xfe\x5c\xdc\xfe\x76\xd9\x9a\x35\x1b\x0d\x76\xb9\x16\x2c\x6d\xa5\x5c\x04\x5b\x91\x45\xa5\xbb\xea\xc7\x7f\x0f\x0f\xb6\xa0\xfa\xd7\x7f\x67\xb3\x32\x5e\x18\x9e\x43\xaf\x07\x9e\x84\x7c\x80\x9c\x1c\x90\x2d\xcd\x9a\xcf\xae\x7f\xbb\xbc\xbe\xfc\x3c\x5f\x5e\xbe\xa2\xb0\x58\xce\x97\x57\x17\xe5\xd1\x5f\x49\xfc\x1f\xfe\x07\x3f\xf3\xdf\x68\x3c\x35\x9f\x6f\x85\x9a\x9c\x37\x1b\x75\xd5\x3c\xf1\x9c\xf2\x3c\x8d\xc2\x18\xd1\x3c\x3c\xb9\x2c\x55\x6b\x86\x3e\xe7\x8e\xe1\x0e\x8a\x9b\x8d\x70\xff\x28\xdf\x5a\x45\xa1\xb9\x42\x86\xb7\xc2\xc1\x03\x1e\xe0\x03\xb4\x5a\xf0\x1e\xc8\x7e\xc1\x7d\x5b\xab\x0e\xbc\x87\x56\x97\x4f\xf8\xe6\x79\xb3\xd1\xa0\x54\xfb\x58\x2b\xff\xaf\x07\x3c\xfc\x1b\x3e\xc0\xeb\xff\xef\xa1\x0f\x7f\xfe\x09\xfd\x57\x34\x31\xe7\x85\xa1\xcd\xd6\x3e\xa0\x0a\x92\xe1\x01\x70\x00\x9b\x4b\xab\xaa\x8d\xc1\x11\xfc\xf3\x77\xc0\x3d\xca\x82\xd0\x07\xba\x98\x1f\xb1\xcd\xec\x3a\x02\xb5\xea\x00\xb3\xed\xf5\x60\xf1\xa0\xf3\xb0\xb8\x4a\x14\x5f\xc2\xf0\x46\x34\x96\x40\x1b\x42\x67\x44\x16\xa4\xed\xab\xf8\x24\xd5\x7c\x6b\xf5\x31\x6a\x6c\xf3\x98\xec\x82\x9c\x36\xeb\x76\xa7\xc3\x31\xea\x04\xda\x7f\x93\x54\xfa\xaa\xd2\x7f\x5e\x15\xe3\xd8\x75\xee\xb0\x2b\xed\x26\x0f\x5f\x19\x66\x6b\x65\xd8\xc3\x3e\x02\x4a\x2d\xef\x6f\x87\xf0\x9f\xc2\x13\x24\xc2\xc8\x67\xa2\x15\xbe\xf6\x77\x0e\x2b\x63\xd5\x26\x3b\x57\xca\xa1\xf7\x81\x51\x50\x42\xcc\x6d\xd6\xee\x77\x5e\xc8\xf5\xcf\x3a\x9d\xce\xcf\x48\x7d\x16\x61\xf7\xbf\x0a\xbc\x5e\x62\x55\xfc\xda\x2c\xbe\xc3\x07\x78\xe3\x41\x12\x57\xad\x13\x87\x5e\xbd\x4d\xda\xcf\x19\x08\xd7\x3f\x7e\x80\x51\xe5\xb2\x84\xb8\x4d\x92\x1f\x61\xbc\xb1\x2f\x65\x12\x14\x17\x22\x62\xd1\xbb\x43\xec\x79\x6d\xb5\x03\x48\x54\x61\xbd\x87\x51\x27\x0a\xd4\xba\xa3\x4e\x15\x4f\x2d\x9d\x44\x14\x19\x1d\x6b\x67\x97\x56\xdf\x07\x42\x52\x21\xb2\x4a\x2e\xfc\xad\x63\x13\x10\xa6\x56\x54\x52\x6e\xee\x46\xb0\xff\xa1\x86\xa0\x76\xe1\xd0\xff\xc8\x07\x27\x8f\xfd\xd4\xe2\x0a\x3b\x7f\x85\xdc\x60\x84\x4e\xf0\x47\x8f\xdd\x56\x2d\x56\x0d\xcd\x00\x57\xce\x42\xce\x7f\x05\x5c\x2d\x2e\xde\x1e\x61\xa9\x36\xca\xf3\x23\x52\x92\xf6\x2f\xa2\xae\x9b\xd9\x16\x3c\x3f\xb9\x86\xdc\xc0\x20\x32\x6f\xab\xaa\x48\xda\xc7\xda\xe4\x05\xc5\x19\x9a\x35\xa5\xc7\x15\x3a\x4a\x7a\x99\xe9\xe7\xcb\x11\x9c\x44\x21\xd1\x6f\xcd\xbb\xa3\xce\xeb\x29\x53\xf7\x73\xd9\xc1\x4f\xcd\xff\x06\x00\x00\xff\xff\x8e\xc8\x27\x72\x75\x0b\x00\x00")
+
+func _4byte_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ __4byte_tracerJs,
+ "4byte_tracer.js",
+ )
+}
+
+func _4byte_tracerJs() (*asset, error) {
+ bytes, err := _4byte_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "4byte_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xb4, 0xc5, 0x48, 0x2d, 0xd9, 0x43, 0x95, 0x93, 0x3b, 0x93, 0x2c, 0x47, 0x8c, 0x84, 0x32, 0x3c, 0x8b, 0x2e, 0xf3, 0x72, 0xc4, 0x57, 0xe6, 0x3a, 0xb3, 0xdf, 0x1d, 0xbf, 0x45, 0x3, 0xfc, 0xa}}
+ return a, nil
+}
+
+var _bigram_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x54\x5b\x6f\xdb\x36\x14\x7e\xf7\xaf\xf8\xde\x92\x20\xae\xd4\x6e\x2f\x83\x33\x0f\xd0\xb2\xa4\x35\x90\xda\x81\xad\xac\x30\x86\x3d\x50\xd2\x91\x44\x84\x26\x05\xf2\xd0\xae\x50\xe4\xbf\x17\x94\x2c\x5f\x8a\x14\x8d\x9e\x64\xf3\xbb\x9d\x0b\x15\xc7\xb8\x35\x4d\x6b\x65\x55\x33\x7e\x7b\xff\xe1\x0f\xa4\x35\xa1\x32\xef\x88\x6b\xb2\xe4\x37\x48\x3c\xd7\xc6\xba\x51\x1c\x23\xad\xa5\x43\x29\x15\x41\x3a\x34\xc2\x32\x4c\x09\xfe\x01\xaf\x64\x66\x85\x6d\xa3\x51\x1c\xf7\x9c\x57\x8f\x83\x42\x69\x89\xe0\x4c\xc9\x3b\x61\x69\x82\xd6\x78\xe4\x42\xc3\x52\x21\x1d\x5b\x99\x79\x26\x48\x86\xd0\x45\x6c\x2c\x36\xa6\x90\x65\x1b\x24\x25\xc3\xeb\x82\x6c\x67\xcd\x64\x37\x6e\xc8\xf1\x71\xfe\x84\x07\x72\x8e\x2c\x3e\x92\x26\x2b\x14\x1e\x7d\xa6\x64\x8e\x07\x99\x93\x76\x04\xe1\xd0\x84\x7f\x5c\x4d\x05\xb2\x4e\x2e\x10\xef\x43\x94\xd5\x3e\x0a\xee\x8d\xd7\x85\x60\x69\xf4\x18\x24\x43\x72\x6c\xc9\x3a\x69\x34\x7e\x1f\xac\xf6\x82\x63\x18\x1b\x44\x2e\x05\x87\x02\x2c\x4c\x13\x78\x57\x10\xba\x85\x12\x7c\xa4\xbe\xa1\x21\xc7\xba\x0b\x48\xdd\xd9\xd4\xa6\x21\x70\x2d\x38\x54\xbd\x93\x4a\x21\x23\x78\x47\xa5\x57\xe3\xa0\x96\x79\xc6\x97\x59\xfa\x69\xf1\x94\x22\x99\xaf\xf1\x25\x59\x2e\x93\x79\xba\xbe\xc1\x4e\x72\x6d\x3c\x83\xb6\xd4\x4b\xc9\x4d\xa3\x24\x15\xd8\x09\x6b\x85\xe6\x16\xa6\x0c\x0a\x9f\xef\x96\xb7\x9f\x92\x79\x9a\xfc\x3d\x7b\x98\xa5\x6b\x18\x8b\xfb\x59\x3a\xbf\x5b\xad\x70\xbf\x58\x22\xc1\x63\xb2\x4c\x67\xb7\x4f\x0f\xc9\x12\x8f\x4f\xcb\xc7\xc5\xea\x2e\xc2\x8a\x42\x2a\x0a\xfc\x5f\xf7\xbc\xec\xa6\x67\x09\x05\xb1\x90\xca\x0d\x9d\x58\x1b\x0f\x57\x1b\xaf\x0a\xd4\x62\x4b\xb0\x94\x93\xdc\x52\x01\x81\xdc\x34\xed\x9b\x87\x1a\xb4\x84\x32\xba\xea\x6a\xfe\xe9\x42\x62\x56\x42\x1b\x1e\xc3\x11\xe1\xcf\x9a\xb9\x99\xc4\xf1\x6e\xb7\x8b\x2a\xed\x23\x63\xab\x58\xf5\x72\x2e\xfe\x2b\x1a\x8d\xbe\x8d\x00\x20\x8e\x51\x4b\xc7\x61\x38\x41\x36\x37\x5e\x33\xd9\x6e\xdf\x4c\x93\x9b\x82\x90\xc9\xca\x8a\x8d\xeb\xd0\x01\x3a\xc1\xb7\x97\xf1\xc0\x55\xc2\xf1\xa2\x09\xec\xf0\x06\xd3\x90\xed\xd6\xaa\x3b\xef\x0f\x27\xb8\xb8\x38\xe0\xe9\x2b\xe5\x3e\x00\x50\x50\xc3\x75\xb0\xd9\x13\x0f\x8c\x7f\xc2\xc1\x04\xef\x0f\x1c\xc7\xd4\x39\x48\xbd\x35\xcf\x54\x74\xdd\xa6\x2d\xd9\x76\x48\xd8\x6d\x4f\x48\xff\xef\xe7\xbd\x01\xb9\xa8\x63\x07\xea\x04\xa5\xd7\x79\xf0\xbc\x54\xa6\x1a\xa3\xc8\xae\xd0\xd7\x1e\x9e\xad\x08\x1b\x8d\x29\x94\xa9\x22\xd3\x44\x6c\x56\x6c\xa5\xae\x2e\xaf\x6e\xce\x30\x7d\xdc\x1e\x56\x51\x1f\xf2\x14\x23\x4b\x5c\xee\x31\x53\x70\x2d\x5d\x74\xa8\xe5\xea\xe8\x36\xa8\x3d\x53\x8b\x13\xd8\xa2\xb9\xbe\x78\x77\x71\x6d\x9a\x9b\x33\x64\xd0\xec\x30\xa1\xed\xff\x3d\x53\xfb\xff\x0f\x52\xe1\x39\x07\x5c\x5f\x9f\x4b\xbc\x9c\xfd\x22\xe5\x08\xbf\x92\xc0\x14\x1f\x7e\x26\x72\x7c\x3b\xc9\x8e\x29\x4e\x93\x9f\x17\x8f\x69\xdf\xba\xfe\xfc\xb8\x38\xa5\xf0\x8a\x4f\xa7\xba\xab\xf7\xb7\x58\xe4\xec\x85\x3a\xd9\x14\x53\x42\xe8\x61\xd6\x65\x7f\xbf\x82\x4a\x27\xf1\xea\x74\x8f\x36\x96\xdc\x6b\x3e\x42\xa9\xce\xab\x17\x75\xfd\xed\xcc\x88\x34\x24\x87\x0d\xa6\x02\x66\x4b\x36\x7c\x99\x61\x89\xbd\xd5\x6e\x50\x0c\xb4\x52\x6a\xa1\x06\xed\xfd\x25\x66\x2b\x72\xa9\xab\x3e\x5a\x7f\x74\x92\x2d\xe7\xaf\xa7\x5b\xd7\x6b\x1e\x1b\x7f\xe8\xce\xcb\xe8\x7b\x00\x00\x00\xff\xff\x83\xb5\xcb\x27\xb0\x06\x00\x00")
+
+func bigram_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _bigram_tracerJs,
+ "bigram_tracer.js",
+ )
+}
+
+func bigram_tracerJs() (*asset, error) {
+ bytes, err := bigram_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "bigram_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x77, 0x6c, 0xd, 0x24, 0xf2, 0x49, 0xbd, 0x58, 0x8b, 0xb5, 0xd1, 0xc9, 0xcd, 0xcf, 0x5b, 0x3e, 0x5c, 0xfb, 0x14, 0x50, 0xe7, 0xe3, 0xb9, 0xd1, 0x54, 0x69, 0xe6, 0x5e, 0x45, 0xa6, 0x2c, 0x6c}}
+ return a, nil
+}
+
+var _call_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xd4\x59\x5f\x6f\x1b\xb7\xb2\x7f\x96\x3e\xc5\x24\x0f\xb5\x84\x28\x92\x93\xf4\xf6\x02\x76\xd5\x0b\x5d\x47\x49\x0d\xb8\x71\x60\x2b\x0d\x82\x20\x0f\xd4\xee\xac\xc4\x9a\x4b\x6e\x49\xae\xe4\x3d\xa9\xbf\xfb\xc1\x0c\xb9\xab\xd5\x1f\x3b\x6e\x0f\xce\x41\xcf\x8b\xa0\x5d\xce\x0c\x87\x33\xbf\xf9\xc7\x1d\x8d\xe0\xcc\x14\x95\x95\x8b\xa5\x87\x97\xc7\x2f\xfe\x17\x66\x4b\x84\x85\x79\x8e\x7e\x89\x16\xcb\x1c\x26\xa5\x5f\x1a\xeb\xba\xa3\x11\xcc\x96\xd2\x41\x26\x15\x82\x74\x50\x08\xeb\xc1\x64\xe0\x77\xe8\x95\x9c\x5b\x61\xab\x61\x77\x34\x0a\x3c\x07\x97\x49\x42\x66\x11\xc1\x99\xcc\xaf\x85\xc5\x13\xa8\x4c\x09\x89\xd0\x60\x31\x95\xce\x5b\x39\x2f\x3d\x82\xf4\x20\x74\x3a\x32\x16\x72\x93\xca\xac\x22\x91\xd2\x43\xa9\x53\xb4\xbc\xb5\x47\x9b\xbb\x5a\x8f\xb7\xef\x3e\xc0\x05\x3a\x87\x16\xde\xa2\x46\x2b\x14\xbc\x2f\xe7\x4a\x26\x70\x21\x13\xd4\x0e\x41\x38\x28\xe8\x8d\x5b\x62\x0a\x73\x16\x47\x8c\x6f\x48\x95\xeb\xa8\x0a\xbc\x31\xa5\x4e\x85\x97\x46\x0f\x00\x25\x69\x0e\x2b\xb4\x4e\x1a\x0d\xaf\xea\xad\xa2\xc0\x01\x18\x4b\x42\x7a\xc2\xd3\x01\x2c\x98\x82\xf8\xfa\x20\x74\x05\x4a\xf8\x0d\xeb\x23\x0c\xb2\x39\x77\x0a\x52\xf3\x36\x4b\x53\x20\xf8\xa5\xf0\x74\xea\xb5\x54\x0a\xe6\x08\xa5\xc3\xac\x54\x03\x92\x36\x2f\x3d\x7c\x3c\x9f\xfd\x7c\xf9\x61\x06\x93\x77\x9f\xe0\xe3\xe4\xea\x6a\xf2\x6e\xf6\xe9\x14\xd6\xd2\x2f\x4d\xe9\x01\x57\x18\x44\xc9\xbc\x50\x12\x53\x58\x0b\x6b\x85\xf6\x15\x98\x8c\x24\xfc\x32\xbd\x3a\xfb\x79\xf2\x6e\x36\xf9\xff\xf3\x8b\xf3\xd9\x27\x30\x16\xde\x9c\xcf\xde\x4d\xaf\xaf\xe1\xcd\xe5\x15\x4c\xe0\xfd\xe4\x6a\x76\x7e\xf6\xe1\x62\x72\x05\xef\x3f\x5c\xbd\xbf\xbc\x9e\x0e\xe1\x1a\x49\x2b\x24\xfe\x6f\xdb\x3c\x63\xef\x59\x84\x14\xbd\x90\xca\xd5\x96\xf8\x64\x4a\x70\x4b\x53\xaa\x14\x96\x62\x85\x60\x31\x41\xb9\xc2\x14\x04\x24\xa6\xa8\x1e\xed\x54\x92\x25\x94\xd1\x0b\x3e\xf3\xbd\x80\x84\xf3\x0c\xb4\xf1\x03\x70\x88\xf0\xe3\xd2\xfb\xe2\x64\x34\x5a\xaf\xd7\xc3\x85\x2e\x87\xc6\x2e\x46\x2a\x88\x73\xa3\x9f\x86\x5d\x92\x99\x08\xa5\x66\x56\x24\x68\xc9\x39\x02\xb2\x92\xcc\xaf\xcc\x5a\x83\xb7\x42\x3b\x91\x90\xab\xe9\x7f\xc2\x60\x14\x1e\xf0\x96\x9e\xbc\x23\xd0\x82\xc5\xc2\x58\xfa\xaf\x54\x8d\x33\xa9\x3d\x5a\x2d\x14\xcb\x76\x90\x8b\x14\x61\x5e\x81\x68\x0b\x1c\xb4\x0f\x43\x30\x0a\xee\x06\xa9\x33\x63\x73\x86\xe5\xb0\xfb\xb5\xdb\x89\x1a\x3a\x2f\x92\x1b\x52\x90\xe4\x27\xa5\xb5\xa8\x3d\x99\xb2\xb4\x4e\xae\x90\x49\x20\xd0\x44\x7b\x4e\x7f\xfd\x05\xf0\x16\x93\x32\x48\xea\x34\x42\x4e\xe0\xf3\xd7\xbb\x2f\x83\x2e\x8b\x4e\xd1\x25\xa8\x53\x4c\xf9\x7c\x37\x0e\xd6\x4b\xb6\x28\xac\xf1\x68\x85\xf0\x5b\xe9\x7c\x8b\x26\xb3\x26\x07\xa1\xc1\x94\x84\xf8\xb6\x75\xa4\xf6\x86\x05\x0a\xfa\xaf\xd1\xb2\x46\xc3\x6e\xa7\x61\x3e\x81\x4c\x28\x87\x71\x5f\xe7\xb1\xa0\xd3\x48\xbd\x32\x37\x24\xd9\x58\x82\xb0\xad\xc0\x14\x89\x49\x63\x30\xd0\x39\x9a\x63\xa0\x1b\x76\x3b\xc4\x77\x02\x59\xa9\x79\xdb\x9e\x32\x8b\x01\xa4\xf3\x3e\x7c\xed\x76\x48\xec\x99\x28\x7c\x69\x91\xed\x89\xd6\x1a\xeb\x40\xe6\x39\xa6\x52\x78\x54\x55\xb7\xd3\x59\x09\x1b\x16\x60\x0c\xca\x2c\x86\x0b\xf4\x53\x7a\xec\xf5\x4f\xbb\x9d\x8e\xcc\xa0\x17\x56\x9f\x8c\xc7\x9c\x7d\x32\xa9\x31\x0d\xe2\x3b\x7e\x29\xdd\x30\x13\xa5\xf2\xcd\xbe\xc4\xd4\xb1\xe8\x4b\xab\xe9\xef\x5d\xd0\xe2\x23\x82\xd1\xaa\x82\x84\xb2\x8c\x98\x53\x78\xba\xca\x79\xcc\xe3\xe1\xdc\x00\x32\xe1\xc8\x84\x32\x83\x35\x42\x61\xf1\x79\xb2\x44\xf2\x9d\x4e\x30\x6a\xe9\x2a\xc7\x4e\x1d\x03\xed\x36\x34\xc5\xd0\x9b\x77\x65\x3e\x47\xdb\xeb\xc3\x77\x70\x7c\x9b\x1d\xf7\x61\x3c\xe6\x3f\xb5\xee\x91\x27\xea\x4b\x52\x4c\x11\x0f\xca\xfc\xd7\xde\x4a\xbd\x08\x67\x8d\xba\x9e\x67\x20\x40\xe3\x1a\x12\xa3\x19\xd4\xe4\x95\x39\x4a\xbd\x80\xc4\xa2\xf0\x98\x0e\x40\xa4\x29\x78\x13\x90\xd7\xe0\x6c\x7b\x4b\xf8\xee\x3b\xe8\xd1\x66\x63\x38\x3a\xbb\x9a\x4e\x66\xd3\x23\xf8\xe3\x0f\x08\x6f\x9e\x86\x37\x2f\x9f\xf6\x5b\x9a\x49\x7d\x99\x65\x51\x39\x16\x38\x2c\x10\x6f\x7a\x2f\xfa\xc3\x95\x50\x25\x5e\x66\x41\xcd\x48\x3b\xd5\x29\x8c\x23\xcf\xb3\x5d\x9e\x97\x5b\x3c\xc4\x34\x1a\xc1\xc4\x39\xcc\xe7\x0a\xf7\x03\x32\x46\x2c\x07\xaf\xf3\x94\xb1\x08\x7d\x89\xc9\x0b\x85\x84\xaa\x7a\xd7\x68\x7e\xd6\xb8\xe3\xab\x02\x4f\x00\x00\x4c\x31\xe0\x17\x14\x0b\xfc\xc2\x9b\x9f\xf1\x96\x7d\x54\x9b\x90\x50\x35\x49\x53\x8b\xce\xf5\xfa\xfd\x40\x2e\x75\x51\xfa\x93\x2d\xf2\x1c\x73\x63\xab\xa1\xa3\x84\xd4\xe3\xa3\x0d\xc2\x49\x6b\x9e\x85\x70\xe7\x9a\x78\x22\x52\xdf\x0a\xd7\xdb\x2c\x9d\x19\xe7\x4f\xea\x25\x7a\xa8\xd7\xd8\x16\xc4\x76\x74\x7c\x7b\xb4\x6f\xad\xe3\xfe\x06\x09\x2f\x7e\xe8\x13\xcb\xdd\x69\x83\xef\x26\x4d\x0c\x8b\xd2\x2d\x7b\x0c\xa7\xcd\xea\x26\x15\x8c\xc1\xdb\x12\x0f\xc2\x9f\x21\xb5\x0f\x27\x87\x2a\xa3\x5c\xe2\x6d\x99\x30\xac\x16\x82\x33\x0d\x47\xba\xa0\xcc\xeb\xca\x39\xdb\xdc\x1b\xb3\x8f\xae\x08\xae\xeb\xe9\xc5\x9b\xd7\xd3\xeb\xd9\xd5\x87\xb3\xd9\x51\x0b\x4e\x0a\x33\x4f\x4a\x6d\x9f\x41\xa1\x5e\xf8\x25\xeb\x4f\xe2\xb6\x57\x3f\x13\xcf\xf3\x17\x5f\xc2\x1b\x18\x1f\x08\xf9\xce\xc3\x1c\xf0\xf9\x0b\xcb\xbe\xdb\x37\xdf\x36\x69\x30\xe6\xd7\x00\x22\x53\xdc\xb5\x13\xc7\x81\x58\xcc\xd1\x2f\x4d\xca\xc9\x31\x11\x21\xbf\xd6\x56\x4c\x8d\xc6\x3f\x1f\x91\x93\x8b\x8b\x56\x3c\xf2\xf3\xd9\xe5\xeb\x76\x8c\x1e\xbd\x9e\x5e\x4c\xdf\x4e\x66\xd3\x5d\xda\xeb\xd9\x64\x76\x7e\xc6\x6f\xeb\xf0\x1d\x8d\xe0\xfa\x46\x16\x9c\x65\x39\x77\x99\xbc\xe0\x76\xb1\xd1\xd7\x0d\xc0\x2f\x0d\x35\x62\x36\x16\x91\x4c\xe8\xa4\x4e\xee\xae\x76\x9a\x37\xe4\x32\x53\xc7\xca\x7e\x2a\x68\x03\xb5\xdf\xb8\x51\xba\xf7\x16\xe3\xa6\x69\xcf\x9b\x5a\xaf\x8d\x41\x83\x47\x38\x01\x72\x92\xe9\x3d\xfe\x90\xf0\x7f\x70\x0c\x27\xf0\x22\x66\x92\x07\x52\xd5\x4b\x78\x46\xe2\xff\x42\xc2\x7a\x75\x80\xf3\xef\x99\xb6\xbc\x61\xe2\x9a\xdc\x9b\xff\x7c\x3a\x33\xa5\xbf\xcc\xb2\x13\xd8\x35\xe2\xf7\x7b\x46\x6c\xe8\x2f\x50\xef\xd3\xff\xcf\x1e\xfd\x26\xf5\x11\xaa\x4c\x01\x4f\xf6\x20\x12\x12\xcf\x93\x9d\x38\x88\xc6\xe5\x16\x87\xa5\xc1\xf8\x9e\x64\xfb\x72\x1b\xc3\xf7\x65\x8b\x7f\x29\xd9\x1e\x6c\xd5\xa8\x21\xdb\x6e\xc6\x06\x60\xd1\x5b\x89\x2b\x1a\xb7\x8e\x1c\x8b\xa4\xa6\xd5\xac\x85\x4e\x70\x08\x1f\x31\x48\xd4\x88\x9c\x5c\x62\x93\x4b\x3d\x0a\xf7\x7d\xd4\xa8\xc6\x71\x85\x21\x26\xb8\x17\xb5\x08\xb9\xa8\x68\x5c\xc9\x4a\x7d\x53\xc1\x42\x38\x48\x2b\x2d\x72\x99\xb8\x20\x8f\x1b\x5c\x8b\x0b\x61\x59\xac\xc5\xdf\x4b\x74\x34\xfb\x10\x90\x45\xe2\x4b\xa1\x54\x05\x0b\x49\x03\x0c\x71\xf7\x5e\xbe\x3a\x3e\x06\xe7\x65\x81\x3a\x1d\xc0\x0f\xaf\x46\x3f\x7c\x0f\xb6\x54\xd8\x1f\x76\x5b\x69\xbc\x39\x6a\xf4\x06\x2d\x44\xf4\xbc\xc6\xc2\x2f\x7b\x7d\xf8\xe9\x9e\x7a\x70\x4f\x72\x3f\x48\x0b\xcf\xe1\xc5\x97\x21\xe9\x35\xde\xc2\x6d\xf0\x24\xa0\x72\x18\xa5\xd1\xd0\x77\xf9\xfa\xb2\x77\x23\xac\x50\x62\x8e\xfd\x13\x1e\x02\xd9\x56\x6b\x11\xa7\x00\x72\x0a\x14\x4a\x48\x0d\x22\x49\x4c\xa9\x3d\x19\xbe\x6e\xe8\x55\x45\xf9\xfd\xc8\xd7\xf2\x78\x5e\x12\x49\x82\xce\xd5\xe9\x9e\xbd\x46\xea\x88\x9c\xb8\x41\x6a\x27\x53\x6c\x79\x85\xb2\x83\xe1\xd4\x1c\x29\x68\x9c\xac\x05\xe6\xc6\xd1\x26\x73\x84\xb5\xa5\xe1\xc3\x49\x9d\xf0\xf4\x9d\x22\x59\xdb\x81\xd1\x20\x40\x19\x1e\xf9\x39\xc6\x41\xd8\x85\x1b\x86\x7c\x4f\xdb\x52\xce\xd1\x66\x3d\xdc\x06\x72\x1b\xaa\xdc\xe6\xef\xb4\x03\x1a\xf0\x56\x3a\xcf\x5d\x25\x69\x29\x1d\x04\x24\x4b\xbd\x18\x40\x61\x0a\xce\xd3\xdf\x2a\x67\x31\x59\x5f\x4d\x7f\x9d\x5e\x35\xc5\xff\xf1\x4e\xac\xfb\xfe\xa7\xcd\x58\x04\x96\x66\x0e\x8f\xe9\xd3\x03\x8d\xfc\x01\x40\x8d\xef\x01\x14\xc9\xdf\xd4\xc6\xf7\xad\xe3\x28\xe1\xfc\xc6\x31\x0b\x0c\x33\x4d\x5b\x01\x57\x2a\xef\x76\x72\xf7\x6e\x72\x30\x45\x5d\x21\x48\x29\x4e\x3b\x94\xd8\x77\xbb\xed\xad\x85\x4d\xd3\xbd\xc1\xe7\x79\xcb\xc6\x6b\x6e\xb9\x02\x51\x2b\x35\xf0\x7a\xdd\xbb\x89\x50\x0d\x58\x77\x53\x7a\x82\x03\xd5\xef\x4d\xf2\x5b\x08\xf7\xc1\xb1\xd7\x63\xfa\x9b\xcb\xc5\xb9\xf6\xbd\x7a\xf1\x5c\xc3\x73\xa8\x1f\x28\xa9\xc3\xf3\xad\x28\x3a\x90\x1d\x3b\x29\x2a\xf4\x08\x1b\x11\xa7\xb0\xf3\x8a\x04\x05\x73\xb0\xd1\x2c\xfa\xfd\xe2\x7c\x1c\xa5\x91\xc1\x9e\x58\xf4\x43\xfc\xbd\x14\xca\xf5\x8e\x9b\x66\x21\x9c\xc0\x1b\x2e\x6f\xe3\xa6\xc0\xd5\x15\x90\x78\xb6\xda\x8f\x28\x30\xb0\x45\x6b\xd4\x6c\xe9\x3c\x54\xad\x14\x1f\x94\x10\x45\xc4\xb4\xd1\xf8\x32\x02\xf3\x50\xff\xd9\x69\x13\xc0\xd3\xa6\x21\xc8\x84\x54\xa5\xc5\xa7\xa7\x70\x20\xed\xb8\xd2\x66\x22\x61\x5f\x3a\x04\x9e\x58\x1d\x38\x93\xe3\xd2\xac\x83\x02\x87\x92\xd7\x3e\x38\x1a\x1c\xec\x94\x0f\xbe\x7a\x11\x0e\x4a\x27\x16\xd8\x02\x47\x63\xf0\xda\x51\x07\xc7\xe8\xbf\x0c\x9d\x67\xcd\xe3\x37\x50\x14\x76\xf9\x26\x34\x1e\xc2\xc6\x41\x2f\xef\x75\x39\x35\x11\xf7\x3a\xad\x87\x5a\xd5\xd0\x8a\x34\xc8\xf9\x33\x7e\xff\xf7\x38\x3e\x78\x3e\xfe\x3e\x36\xd0\x76\x69\xc3\x19\xb7\x89\xc3\x49\x37\xed\xcd\xb7\x51\xd0\xac\xde\x07\x80\xfb\x3a\x27\x82\xaa\xfe\x0d\x13\xbf\x81\x2b\x37\x3b\xf4\x54\x58\x5c\x49\x53\x52\x1d\xc3\xff\xa6\xc9\xb0\xe9\xfc\xee\xba\x9d\xbb\x78\x45\xc6\xee\x6b\xdf\x91\xad\x97\xf1\x8a\x37\x34\x4d\xad\x2a\x62\xb8\xc4\xc6\x9b\xb3\x2c\x5c\xbe\x76\x98\xff\x81\xbb\xb2\x18\xef\xde\x14\xd4\x15\xc4\x22\xa5\x2c\x8a\xb4\x6a\xea\xe2\x20\xf4\x23\xb0\x14\x3a\x8d\x33\x89\x48\x53\x49\xf2\x18\x8b\xa4\xa1\x58\x08\xa9\xbb\x07\xcd\xf8\xcd\x62\x7c\x08\x19\x7b\x2d\x6e\xbb\x9e\xc6\x59\x92\x06\x3f\xd6\xb8\xfb\x88\xba\xb9\x13\x4b\xbb\xd7\x7e\xf1\xe6\xd0\x68\x57\xe6\xdc\x10\x83\x58\x09\xa9\x04\x0d\x61\xdc\x68\xe9\x14\x12\x85\x42\x87\xcb\x7e\xcc\xbc\x59\xa1\x75\xdd\x47\x80\xfc\xaf\x60\x7c\x27\x39\xd6\x8f\xd1\x1c\x8f\x8f\xd9\xc7\x46\x6c\x38\xfe\x1b\x25\xbc\x8f\xf0\x6a\x99\x37\x44\x96\xf4\xfc\x1d\x08\xb5\xef\x3e\x2e\xa4\xb8\x75\x22\x9a\x9f\xe0\xb8\xd5\x9e\xff\x5d\x82\x6c\x1f\x62\x17\x4d\x9b\x16\x0f\xef\x8d\x19\x80\x42\xc1\xc3\x52\xfd\x95\xa6\x6e\x4b\x1f\x9a\xdd\xea\xe8\x0d\x8d\xdd\x5e\xf8\xf2\xf5\xd6\x12\xeb\x8b\x90\xd0\xe1\xcf\x11\x35\x48\x8f\x56\xd0\x58\x44\xe8\x8a\x1f\x16\x48\x4b\xc7\xe2\xd8\x2f\x92\x82\x2e\x0a\x8e\xb7\xfc\x54\x9f\xa5\x5e\x0c\xbb\x9d\xf0\xbe\x15\xef\x89\xbf\xdd\xc4\x7b\x28\x86\xcc\x19\xaf\x06\x9a\x9b\x81\xc4\xdf\x72\xd3\xc8\xd3\xf3\xce\xf5\x00\xad\xd1\xab\x30\x5a\xef\x5c\x06\x30\x63\xbc\x10\xd8\xbd\x73\xa4\x35\x7e\xb7\x05\x70\x26\x5d\x08\x17\xc4\xec\x84\x84\xbf\xdd\x8f\x88\x9a\x81\x82\xe1\xe4\x30\x03\x2d\x1d\x60\xda\xb9\xa0\x20\x62\x7e\x15\x56\x43\x61\x3f\x69\xaf\x86\x57\xf1\xa0\x32\x6f\xd9\x46\xe6\x6c\x9b\xbb\xd3\xc3\x49\xee\xb8\xc6\xe3\xe1\x64\x46\x36\x6f\x00\x7b\x0f\x6b\x7b\xe4\xd8\x27\x79\x28\x55\xb2\xf4\x3a\xb3\xdd\xc3\xca\xd2\x5b\xad\x87\xbf\x7d\xbc\xc8\x86\xb8\xad\xe2\x16\xcd\x21\x21\x31\xcf\x44\xba\x60\xd9\x5a\x40\x40\x75\xd0\x95\x11\x2d\xff\x81\x51\x62\x3b\x7e\xea\x25\xb0\x18\xbe\x43\x70\x43\x4a\xe1\x63\xe6\x5c\xfc\x4b\x47\xd3\xe4\x26\x2e\x52\x74\xd2\x62\x0a\x99\x44\x95\x82\x49\xd1\xf2\xac\xfa\x9b\x33\x3a\x7c\x71\x42\x2b\x49\x62\xf8\xb2\x16\x3e\x72\xf3\xf7\x3e\x2d\x13\xf4\x15\x64\x28\xf8\xd3\x91\x37\x50\x08\xe7\x20\x47\x41\xd3\x69\x56\x2a\x55\x81\xb1\x29\x92\xf0\x66\x5c\xa3\x90\x34\x50\x3a\xb4\x0e\xd6\x4b\x13\xcb\x24\x77\x69\x05\x35\x9d\xd2\x0f\xe2\x8d\x8c\x74\x85\x12\x15\x48\x4f\x25\x39\x1e\xaa\x1d\xa5\xcd\xf7\x1a\xfe\xe8\x63\xa8\xea\xee\x87\x68\x3d\xd8\x6d\xc7\x28\xbf\xa6\xa7\xed\xe8\x8c\x73\xcd\x76\x5c\x6e\xee\xaa\xb6\x83\xb0\x2e\x1b\xdb\x91\xd6\x2e\x42\xdb\xe1\xc4\x2b\xfc\xb4\x1d\x48\xad\x7e\x99\x17\x18\x1c\x0d\x03\x3f\xed\x84\x16\x6b\x19\x63\x2b\x7c\x9d\x6c\xc8\xf9\x69\x10\x01\x43\x5e\xec\x91\x71\x6e\xb0\xa2\x4c\x1c\x6c\xd4\x2a\x2b\xe1\xc5\xe7\x1b\xac\xbe\x1c\xae\x22\x11\x8e\x2d\xba\xa6\x6c\xd4\x90\x0e\x6b\x0f\x04\x72\xa3\x85\x1c\x1f\x9f\x82\xfc\xb1\xcd\x50\x57\x3e\x90\xcf\x9e\xd5\x7b\xb6\xd7\x3f\xcb\x2f\x75\x74\x36\x88\xdf\x59\xef\x6f\x69\x14\x63\x24\xd0\x50\x50\x74\xef\xba\xff\x0c\x00\x00\xff\xff\x00\x24\x55\x1f\xc3\x21\x00\x00")
+
+func call_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _call_tracerJs,
+ "call_tracer.js",
+ )
+}
+
+func call_tracerJs() (*asset, error) {
+ bytes, err := call_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "call_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe9, 0xef, 0x68, 0xda, 0xd8, 0x9, 0xf5, 0xd5, 0x71, 0xa8, 0x8a, 0xfb, 0x30, 0xe8, 0xf0, 0x72, 0x14, 0x36, 0x6b, 0x62, 0x5a, 0x4e, 0xff, 0x16, 0xdc, 0xd3, 0x2c, 0x68, 0x7b, 0x79, 0x9f, 0xd3}}
+ return a, nil
+}
+
+var _evmdis_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x57\xdf\x6f\xda\xca\x12\x7e\x86\xbf\x62\x94\x27\x50\x29\x60\x63\x08\x38\x27\x47\xe2\xa6\xf4\x1c\xae\xd2\x24\x02\x72\x8f\x2a\x94\x87\x05\xc6\xb0\xaa\xf1\x5a\xbb\x6b\x72\xb8\x55\xfe\xf7\xab\xd9\x59\x03\xf9\x75\xdb\x4a\xa7\x0f\x3b\xb5\x77\xbe\x6f\xbe\x9d\x19\xcf\x92\x56\x0b\xae\x54\xbe\xd7\x72\xbd\xb1\x10\xb6\x83\x73\x98\x6d\x10\xd6\xea\x23\xda\x0d\x6a\x2c\xb6\x30\x2c\xec\x46\x69\x53\x6d\xb5\x60\xb6\x91\x06\x12\x99\x22\x48\x03\xb9\xd0\x16\x54\x02\xf6\x85\x7f\x2a\x17\x5a\xe8\x7d\xb3\xda\x6a\x31\xe6\xcd\x6d\x62\x48\x34\x22\x18\x95\xd8\x47\xa1\x31\x86\xbd\x2a\x60\x29\x32\xd0\xb8\x92\xc6\x6a\xb9\x28\x2c\x82\xb4\x20\xb2\x55\x4b\x69\xd8\xaa\x95\x4c\xf6\x44\x29\x2d\x14\xd9\x0a\xb5\x0b\x6d\x51\x6f\x4d\xa9\xe3\x8f\x9b\x7b\xb8\x46\x63\x50\xc3\x1f\x98\xa1\x16\x29\xdc\x15\x8b\x54\x2e\xe1\x5a\x2e\x31\x33\x08\xc2\x40\x4e\x6f\xcc\x06\x57\xb0\x70\x74\x04\xfc\x4c\x52\xa6\x5e\x0a\x7c\x56\x45\xb6\x12\x56\xaa\xac\x01\x28\x49\x39\xec\x50\x1b\xa9\x32\xe8\x94\xa1\x3c\x61\x03\x94\x26\x92\x9a\xb0\x74\x00\x0d\x2a\x27\x5c\x1d\x44\xb6\x87\x54\xd8\x23\xf4\x27\x12\x72\x3c\xf7\x0a\x64\xe6\xc2\x6c\x54\x8e\x60\x37\xc2\xd2\xa9\x1f\x65\x9a\xc2\x02\xa1\x30\x98\x14\x69\x83\xd8\x16\x85\x85\xbf\xc6\xb3\x3f\x6f\xef\x67\x30\xbc\xf9\x0a\x7f\x0d\x27\x93\xe1\xcd\xec\xeb\x05\x3c\x4a\xbb\x51\x85\x05\xdc\x21\x53\xc9\x6d\x9e\x4a\x5c\xc1\xa3\xd0\x5a\x64\x76\x0f\x2a\x21\x86\x2f\xa3\xc9\xd5\x9f\xc3\x9b\xd9\xf0\x5f\xe3\xeb\xf1\xec\x2b\x28\x0d\x9f\xc7\xb3\x9b\xd1\x74\x0a\x9f\x6f\x27\x30\x84\xbb\xe1\x64\x36\xbe\xba\xbf\x1e\x4e\xe0\xee\x7e\x72\x77\x3b\x1d\x35\x61\x8a\xa4\x0a\x09\xff\xe3\x9c\x27\xae\x7a\x1a\x61\x85\x56\xc8\xd4\x94\x99\xf8\xaa\x0a\x30\x1b\x55\xa4\x2b\xd8\x88\x1d\x82\xc6\x25\xca\x1d\xae\x40\xc0\x52\xe5\xfb\x9f\x2e\x2a\x71\x89\x54\x65\x6b\x77\xe6\x77\x1b\x12\xc6\x09\x64\xca\x36\xc0\x20\xc2\x6f\x1b\x6b\xf3\xb8\xd5\x7a\x7c\x7c\x6c\xae\xb3\xa2\xa9\xf4\xba\x95\x32\x9d\x69\xfd\xde\xac\x12\x27\xee\xb6\x2b\x69\x66\x5a\x2c\x51\x83\x46\x5b\xe8\xcc\x80\x29\x92\x84\xfc\x2c\xc8\x2c\x51\x7a\xeb\xda\x04\x12\xad\xb6\x20\xc0\x92\x2f\x58\x05\x39\x6a\xda\xf4\x14\x1f\x8d\xdd\xa7\x4e\xe6\x4a\x1a\x61\x0c\x6e\x17\xe9\xbe\x59\xfd\x5e\xad\x18\x2b\x96\xdf\x62\x98\x7f\x57\xb9\x89\x61\xfe\xf0\xf4\xd0\xa8\x56\x2b\x59\x5e\x98\x0d\x9a\x18\xbe\xb7\x63\x68\x37\x20\x88\x21\x68\x40\xe8\xd6\x8e\x5b\x23\xb7\x76\xdd\xda\x73\xeb\xb9\x5b\xfb\x6e\x1d\xb8\x35\x68\xb3\x61\x74\xc0\x6e\x01\xfb\x05\xec\x18\xb0\x67\xc8\x9e\xa1\x8f\xc3\x81\x42\x8e\x14\x72\xa8\x90\x63\x85\xcc\xd2\x61\x97\x88\x59\x22\x66\xe9\x32\x4b\x97\x59\xba\xec\xd2\x65\x96\xae\x17\xdc\x75\xe7\xe9\x32\x4b\xf7\x9c\x9f\x98\xa5\xcb\x2c\x3d\x3e\x72\x8f\x01\x3d\x7f\x44\x06\xf4\x58\x7c\x8f\x01\x3d\x06\xf4\x19\xd0\xe7\xb0\xfd\x90\x9f\x3a\x6c\x98\xa5\xcf\x61\xfb\x3d\x36\x1c\xb6\xcf\x2c\x7d\x66\x19\xb0\xf8\x41\xe0\xf6\x06\x1c\x6f\xc0\xf1\x06\x3e\xab\x65\x5a\x7d\x5e\xdb\x3e\xb1\xed\xd0\xdb\x8e\xb7\x91\xb7\x5d\x6f\x7d\xe6\xdb\x3e\xf5\x6d\x9f\xfb\xb6\xe7\x3b\xd4\xc9\xf3\x05\x9e\x2f\xf0\x7c\x81\xe7\x0b\x3c\x5f\x59\xc9\xb2\x94\x65\x2d\x7d\x31\x03\x5f\xcd\xc0\x97\x33\xf0\xf5\x0c\x7c\x41\x03\x5f\xd1\xc0\x97\x34\xf0\x35\x0d\x42\xcf\x17\xf6\x63\x08\xc9\x0e\x62\xe8\x34\x20\xe8\xb4\x63\x88\xc8\x06\x31\x74\xc9\x86\x31\xf4\xc8\x76\x62\x38\x27\x1b\xc5\xd0\x27\xdb\x8d\x61\x40\x96\xf8\xa8\x6b\x3b\x44\x48\x8c\x1d\x52\x48\x94\x1d\x92\x48\x9c\x11\x69\x24\xd2\x88\x44\x12\x6b\x44\x2a\x89\x36\x22\x99\xc4\x1b\x45\xac\x23\xea\xb2\x8e\xa8\xc7\x3a\xa2\x73\xd6\x41\xdd\xe7\x00\x03\xd6\x41\xfd\x47\x3a\xa8\x01\x49\x87\xeb\x40\xd2\xe1\x7a\x90\x74\xb8\x2e\x24\x4a\xea\x43\xa7\xc3\x75\x22\x91\x52\x2f\x3a\x1d\xae\x1b\x89\xd6\xf5\x23\xf1\xfa\x8e\x0c\x7a\x81\xb7\xa1\xb7\x1d\x6f\x23\x67\xc3\xc8\x7f\x45\x91\xff\x8c\x22\xff\x1d\x45\x1d\xbf\xef\xfd\xdc\x47\xf0\x44\xdf\x79\xab\x05\x1a\x4d\x91\x5a\x1a\xfe\x32\xdb\xa9\x6f\x34\x9e\x37\x98\x81\x48\x53\x37\xc7\x54\xbe\x54\x2b\x34\x3c\x1f\x17\x88\x19\x48\x8b\x5a\xd0\x05\xa1\x76\xa8\xe9\x6e\x2c\x27\x93\xa3\x23\x4c\x22\x33\x91\x96\xc4\x7e\x86\xd2\x60\x92\xd9\xba\x59\xad\xf0\xfb\x18\x92\x22\x5b\xd2\xe8\xaa\xd5\xe1\xbb\xa7\x00\xbb\x91\xa6\xe9\x46\xd2\xbc\xfd\xd0\x54\xb9\xb9\x80\x52\x67\x22\xde\x92\x49\xd4\x62\x69\x0b\x91\x02\xfe\x8d\xcb\xc2\xcd\x42\x95\x80\xc8\xbc\x72\x48\x78\xe0\x57\x1c\xfe\x24\x6a\xaa\xd6\x0d\x58\x2d\x28\x78\x19\xc2\x58\xcc\x4f\x23\xd0\xb5\x81\x3b\xd4\xfb\x92\xcb\x5d\x83\x14\xf2\x3f\x5f\x7c\x38\x24\x6a\xc2\xbd\xc9\x5c\xad\x54\x76\x42\x43\xa2\xc5\x16\xe1\xf2\xf4\x74\xc7\xff\x36\x53\xcc\xd6\x76\x03\x1f\x21\x78\xb8\xa8\x7a\x04\x6a\xad\x34\x5c\x42\xaa\xd6\xcd\x35\xda\x11\x3d\xd6\xea\x17\xd5\x4a\x45\x26\x50\x73\xbb\x4c\x5f\x71\xdc\xf3\x33\xf7\xea\xec\x01\x2e\x19\x4a\x9e\x4f\x80\xa9\x41\x20\x80\xa7\xf9\x84\xb9\xdd\xd4\xea\x70\x79\x2a\xc5\xc7\xf7\x74\x2a\xa7\x4b\x05\x2e\xf9\xa9\xa2\xf2\x18\xe8\x1f\x11\xa8\xbc\x69\xd5\x4d\xb1\x5d\xa0\xae\xd5\x1b\x6e\x7b\x45\x84\x10\xc3\x73\x7e\xde\x2b\xcb\x3c\x7f\x70\xcf\x4f\x24\xc9\xa9\x77\x8a\xa9\xb6\xe5\xc9\x7f\x87\xb6\x8f\xee\xce\x9e\x6b\xdc\xa9\x1c\x2e\xe1\xe0\x38\x7f\x05\xe1\x64\x11\x22\x51\xba\x46\x28\x09\x97\xd0\xbe\x00\x09\xbf\xf1\xd9\xfc\x0d\x36\x67\xb6\xa6\xca\x1f\x2e\x40\x7e\xf8\x50\x77\xa0\x8a\x7f\xcb\x1a\x9b\xe4\xea\x72\xc4\x09\xc9\x11\xbf\xd5\x64\xbd\x69\xd5\xd4\x6a\x99\xad\x6b\x41\xaf\xee\x72\x5f\x79\xa2\xc5\x3c\x4a\xbb\x64\x7f\x97\x12\xef\x54\xf7\x67\x58\x0a\x83\x70\x76\x35\xbc\xbe\x3e\x8b\xe1\xf8\x70\x75\xfb\x69\x74\x16\x1f\x0e\x29\x33\x63\xe9\xe7\x2b\x97\xf8\x24\x6e\xa7\xde\xdc\x89\xb4\xc0\xdb\x84\xeb\x7d\x70\x97\xff\xc5\xd7\xde\xd1\x2b\x6f\x2e\xe0\xfc\x6c\x2d\x8c\x6b\x87\x17\x80\xf6\xbb\x00\xab\xde\xf2\x0f\x9e\xa7\xe1\x39\xc4\x31\xbd\x85\x0a\x4f\x50\x2f\x30\x32\xcb\x0b\x7b\xc0\x6c\x71\xab\xf4\xbe\x69\xe8\x87\x4f\xcd\xe7\xa4\x71\x48\xce\x07\x7f\xee\x17\x14\xc7\x5e\xcf\x8a\x34\x7d\xbe\xc7\x73\xe4\x9d\x4d\x95\x73\x4e\xe6\xbe\x77\x4e\x3e\x02\xd7\x02\xec\xe7\xa3\x2d\x34\x8a\x6f\x17\xc7\x8a\x7e\x1a\x5d\x8f\xfe\x18\xce\x46\xcf\x2a\x3b\x9d\x0d\x67\xe3\x2b\x7e\xf5\xe3\xda\x86\xbf\x54\xdb\xd7\x9d\x70\x3c\x87\x3b\x06\xbc\x6a\xc1\xb7\x5b\xe0\x97\x7b\xe0\x97\x9a\xe0\x58\xd0\x7f\xa2\xa2\xff\xbf\xa4\xff\x74\x4d\x27\xa3\xd9\xfd\xe4\xe6\xa4\x74\xf4\xe7\xca\x4f\x7c\x33\xde\xf5\xed\xba\x05\xaf\xdc\x79\x7c\xf9\x2b\xee\x8d\xc6\x57\x85\x6d\xb8\xd0\x1f\x4a\xd6\x77\xf4\x4e\x67\xb7\x77\xc7\xde\xbb\x1f\x5f\x8d\x0f\x43\xe5\x47\x31\xda\x0d\x68\xbf\xc3\xfa\xef\xfb\x2f\x77\x9f\x46\xd3\x99\x67\x2a\x33\x9b\x2f\x0f\x9f\xe9\x1a\xed\xdd\x55\xed\x64\x06\xca\xa4\x9c\x7f\xd2\xdc\x51\x9a\xcb\xe9\x77\x40\xa7\x98\x1d\xe0\xcf\x6e\x0e\xf8\x08\xed\xbf\xbb\x78\xe4\x3a\x0e\xf7\x97\x05\xf3\x37\x98\x23\x3e\xd6\xf5\xd9\x45\x7a\x3c\xdd\xf3\x3b\x88\xf1\xd5\xca\x53\xf5\xa9\xfa\xbf\x00\x00\x00\xff\xff\x51\x4b\xdc\x7e\x62\x10\x00\x00")
+
+func evmdis_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _evmdis_tracerJs,
+ "evmdis_tracer.js",
+ )
+}
+
+func evmdis_tracerJs() (*asset, error) {
+ bytes, err := evmdis_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "evmdis_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xd5, 0xe8, 0x96, 0xa1, 0x8b, 0xc, 0x68, 0x3c, 0xe8, 0x5d, 0x7e, 0xf0, 0xab, 0xfe, 0xec, 0xd1, 0xb, 0x3d, 0xfc, 0xc7, 0xac, 0xb5, 0xa, 0x41, 0x55, 0x0, 0x3a, 0x60, 0xa7, 0x8e, 0x46, 0x93}}
+ return a, nil
+}
+
+var _noop_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x93\x4f\x6f\xdb\x46\x10\xc5\xcf\xe6\xa7\x78\xc7\x04\x50\xc5\xfe\x39\x14\x70\x8a\x02\xac\x61\x27\x2a\x1c\xdb\x90\xe8\x06\x3e\x0e\xc9\xa1\xb8\xe9\x6a\x87\x9d\x9d\x95\x22\x18\xfe\xee\xc5\x92\x12\x12\x14\x69\x9b\x9b\xb0\xd2\xfb\xbd\x37\xf3\x46\x65\x89\x2b\x19\x8f\xea\xb6\x83\xe1\xc7\xef\x7f\xf8\x19\xf5\xc0\xd8\xca\x77\x6c\x03\x2b\xa7\x1d\xaa\x64\x83\x68\x2c\xca\x12\xf5\xe0\x22\x7a\xe7\x19\x2e\x62\x24\x35\x48\x0f\xfb\xc7\xef\xbd\x6b\x94\xf4\xb8\x2c\xca\x72\xd6\x7c\xf5\xeb\x4c\xe8\x95\x19\x51\x7a\x3b\x90\xf2\x25\x8e\x92\xd0\x52\x80\x72\xe7\xa2\xa9\x6b\x92\x31\x9c\x81\x42\x57\x8a\x62\x27\x9d\xeb\x8f\x19\xe9\x0c\x29\x74\xac\x93\xb5\xb1\xee\xe2\x39\xc7\xdb\xbb\x47\xdc\x72\x8c\xac\x78\xcb\x81\x95\x3c\x1e\x52\xe3\x5d\x8b\x5b\xd7\x72\x88\x0c\x8a\x18\xf3\x4b\x1c\xb8\x43\x33\xe1\xb2\xf0\x26\x47\xd9\x9c\xa2\xe0\x46\x52\xe8\xc8\x9c\x84\x05\xd8\xe5\xe4\xd8\xb3\x46\x27\x01\x3f\x9d\xad\x4e\xc0\x05\x44\x33\xe4\x15\x59\x1e\x40\x21\x63\xd6\xbd\x06\x85\x23\x3c\xd9\x67\xe9\x37\x2c\xe4\xf3\xdc\x1d\x5c\x98\x6c\x06\x19\x19\x36\x90\xe5\xa9\x0f\xce\x7b\x34\x8c\x14\xb9\x4f\x7e\x91\x69\x4d\x32\x7c\x58\xd5\xef\xee\x1f\x6b\x54\x77\x4f\xf8\x50\xad\xd7\xd5\x5d\xfd\xf4\x06\x07\x67\x83\x24\x03\xef\x79\x46\xb9\xdd\xe8\x1d\x77\x38\x90\x2a\x05\x3b\x42\xfa\x4c\x78\x7f\xbd\xbe\x7a\x57\xdd\xd5\xd5\x6f\xab\xdb\x55\xfd\x04\x51\xdc\xac\xea\xbb\xeb\xcd\x06\x37\xf7\x6b\x54\x78\xa8\xd6\xf5\xea\xea\xf1\xb6\x5a\xe3\xe1\x71\xfd\x70\xbf\xb9\x5e\x62\xc3\x39\x15\x67\xfd\xff\xef\xbc\x9f\xda\x53\x46\xc7\x46\xce\xc7\xf3\x26\x9e\x24\x21\x0e\x92\x7c\x87\x81\xf6\x0c\xe5\x96\xdd\x9e\x3b\x10\x5a\x19\x8f\xdf\x5c\x6a\x66\x91\x97\xb0\x9d\x66\xfe\xd7\x83\xc4\xaa\x47\x10\x5b\x20\x32\xe3\x97\xc1\x6c\xbc\x2c\xcb\xc3\xe1\xb0\xdc\x86\xb4\x14\xdd\x96\x7e\xc6\xc5\xf2\xd7\x65\x91\x99\x41\x64\xac\x95\x5a\xd6\x5c\xce\xc7\x14\x6d\x62\x37\xa4\xdc\x48\x60\x34\xe2\x3c\xeb\x98\x5b\x46\x2b\x5d\x1e\xe0\xaf\xe4\x94\x3b\xf4\x2a\x3b\x10\x7e\xa7\x3d\x6d\x5a\x75\xa3\x65\x9c\x34\x1f\xb9\x35\x98\xcc\x15\x52\xe3\xa7\x73\x24\x98\x52\x88\xd4\xe6\xbb\xc9\x9f\x5b\xd6\x65\xf1\x5c\x5c\x94\x25\xa2\xf1\x98\xbd\x5d\xd8\xcb\x9f\x99\x2b\x9a\xfb\xd4\x23\x64\x9c\x1c\xa7\xcb\xc8\xa1\xfe\x78\x0f\xfe\xc4\x6d\x32\x8e\xcb\xe2\x22\xeb\x2e\xd1\xa7\x30\x41\x5f\x79\xd9\x2e\xd0\x35\xaf\xf1\x8c\x97\x45\x31\x91\x7b\x4a\xde\xbe\x44\x1f\x86\xd3\x99\x50\x6b\x89\xfc\x89\x96\x23\x49\x0f\x0a\x67\xc3\x7e\x2e\xf0\x62\xd2\xff\xb7\x85\x72\xfc\x9a\x07\x79\x3f\xf9\xcc\xc0\x38\x57\xdf\x30\x07\x38\x63\xa5\x7c\xfb\xb2\x67\xcd\x7f\x7b\x28\x5b\xd2\x10\x27\x5c\xd6\xf4\x2e\x90\x3f\x83\x4f\xe7\x91\x37\xe6\xc2\x76\x59\x5c\xcc\xef\x5f\x84\x6a\xed\xd3\x39\xd4\x4c\xc2\xf3\xcb\x1b\xbc\x14\x2f\xc5\xdf\x01\x00\x00\xff\xff\x77\x56\xe7\x1a\xf7\x04\x00\x00")
+
+func noop_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _noop_tracerJs,
+ "noop_tracer.js",
+ )
+}
+
+func noop_tracerJs() (*asset, error) {
+ bytes, err := noop_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "noop_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe3, 0xf, 0x1c, 0x6f, 0x65, 0xaf, 0x90, 0x31, 0xab, 0xf, 0xe0, 0xca, 0x54, 0x7, 0xfd, 0xd3, 0xa1, 0x4a, 0x14, 0x1, 0x2a, 0x9d, 0xdc, 0xb9, 0x64, 0x69, 0x83, 0x30, 0xb1, 0x2a, 0xbd, 0xfb}}
+ return a, nil
+}
+
+var _opcount_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x94\xcf\x6e\xdb\x46\x10\x87\xcf\xe2\x53\xfc\x8e\x09\xa2\x92\x69\x7b\x28\xe0\x16\x05\x58\xc3\x4e\x04\xd8\xb2\x21\xd1\x09\x7c\x5c\x92\x43\x71\x9b\xd5\x2e\x31\x3b\x2b\x86\x08\xfc\xee\xc5\x2e\xc5\xc6\x08\x5c\xd4\xd7\xd5\xcc\xf7\xcd\x3f\xb1\x28\x70\xe9\x86\x89\xf5\xa1\x17\xfc\xf2\xfe\xe7\xdf\x50\xf5\x84\x83\xfb\x89\xa4\x27\xa6\x70\x44\x19\xa4\x77\xec\xb3\xa2\x40\xd5\x6b\x8f\x4e\x1b\x82\xf6\x18\x14\x0b\x5c\x07\xf9\x21\xde\xe8\x9a\x15\x4f\x79\x56\x14\x73\xce\x8b\x3f\x47\x42\xc7\x44\xf0\xae\x93\x51\x31\x5d\x60\x72\x01\x8d\xb2\x60\x6a\xb5\x17\xd6\x75\x10\x82\x16\x28\xdb\x16\x8e\x71\x74\xad\xee\xa6\x88\xd4\x82\x60\x5b\xe2\xa4\x16\xe2\xa3\x5f\xea\xf8\xb0\x7d\xc0\x0d\x79\x4f\x8c\x0f\x64\x89\x95\xc1\x7d\xa8\x8d\x6e\x70\xa3\x1b\xb2\x9e\xa0\x3c\x86\xf8\xe2\x7b\x6a\x51\x27\x5c\x4c\xbc\x8e\xa5\xec\xcf\xa5\xe0\xda\x05\xdb\x2a\xd1\xce\xae\x41\x3a\x56\x8e\x13\xb1\xd7\xce\xe2\xd7\x45\x75\x06\xae\xe1\x38\x42\xde\x28\x89\x0d\x30\xdc\x10\xf3\xde\x42\xd9\x09\x46\xc9\xf7\xd4\x57\x0c\xe4\x7b\xdf\x2d\xb4\x4d\x9a\xde\x0d\x04\xe9\x95\xc4\xae\x47\x6d\x0c\x6a\x42\xf0\xd4\x05\xb3\x8e\xb4\x3a\x08\x3e\x6f\xaa\x8f\x77\x0f\x15\xca\xed\x23\x3e\x97\xbb\x5d\xb9\xad\x1e\x7f\xc7\xa8\xa5\x77\x41\x40\x27\x9a\x51\xfa\x38\x18\x4d\x2d\x46\xc5\xac\xac\x4c\x70\x5d\x24\xdc\x5e\xed\x2e\x3f\x96\xdb\xaa\xfc\x6b\x73\xb3\xa9\x1e\xe1\x18\xd7\x9b\x6a\x7b\xb5\xdf\xe3\xfa\x6e\x87\x12\xf7\xe5\xae\xda\x5c\x3e\xdc\x94\x3b\xdc\x3f\xec\xee\xef\xf6\x57\x39\xf6\x14\xab\xa2\x98\xff\xff\x33\xef\xd2\xf6\x98\xd0\x92\x28\x6d\xfc\x32\x89\x47\x17\xe0\x7b\x17\x4c\x8b\x5e\x9d\x08\x4c\x0d\xe9\x13\xb5\x50\x68\xdc\x30\xbd\x7a\xa9\x91\xa5\x8c\xb3\x87\xd4\xf3\x7f\x1e\x24\x36\x1d\xac\x93\x35\x3c\x11\xfe\xe8\x45\x86\x8b\xa2\x18\xc7\x31\x3f\xd8\x90\x3b\x3e\x14\x66\xc6\xf9\xe2\xcf\x3c\x8b\x4c\x37\x34\x2e\x58\xa9\x58\x35\xc4\x71\x3f\x0a\x5e\x1d\x07\x43\x90\xf9\x29\xed\xe5\xef\xe0\x05\x29\xd0\x27\xb5\x0d\xc7\x9a\x38\x16\xaf\xad\x17\x0e\x4d\xbc\x87\xf4\xf7\xa1\xaf\xd4\xa4\xdd\xd6\x53\x8a\xbc\xfa\x74\x8b\x9a\xba\x38\x99\x74\xc9\xac\xac\x57\x29\x3c\x5d\xb5\xb6\x4a\xa8\xcd\xb3\x6f\xd9\xaa\x28\x66\x43\x12\x7f\xf9\xd1\x13\x39\xcf\x5d\xff\x8a\xf2\x6c\x95\xd2\x2e\xf0\x7e\x9d\x25\x8a\x17\x1a\x62\x27\xda\x9e\xdc\x17\x6a\xd3\x6a\xe8\x44\x3c\xa5\x66\xdb\xf3\xa9\x45\xfc\xa7\xdb\x05\xe3\xf3\x6c\x15\xf3\x2e\xd0\x05\x9b\x0c\x6f\x8c\x3b\xac\xd1\xd6\x6f\xf1\x0d\xd2\x6b\x9f\x27\xcb\xbb\x77\x78\x3a\x6b\x3a\x15\x8c\x3c\xf7\x8c\xfd\xf9\x08\x55\x23\x41\x99\x33\x3a\x76\xea\x3a\x28\xbb\xd8\xbb\xf9\x3c\x56\x29\xff\x65\xdf\xa2\x60\xf2\x2f\x39\x94\x31\xc9\x33\x03\xfd\x7c\x58\x35\x91\x85\x16\xe2\x38\x50\xb8\x13\x71\xfc\xa8\x80\x49\x02\x5b\x9f\x70\x31\xa7\xd3\x56\x99\x05\x7c\x3e\xbe\x38\x70\x6d\x0f\x79\xb6\x9a\xdf\x9f\x15\xd5\xc8\xd7\xa5\xa8\x99\xf4\x6c\x16\x78\xca\x9e\xb2\x7f\x02\x00\x00\xff\xff\xdd\xd8\xa1\x0a\x5c\x05\x00\x00")
+
+func opcount_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _opcount_tracerJs,
+ "opcount_tracer.js",
+ )
+}
+
+func opcount_tracerJs() (*asset, error) {
+ bytes, err := opcount_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "opcount_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x27, 0xe, 0x97, 0x88, 0x9b, 0x53, 0xbb, 0x20, 0x44, 0xd8, 0xf5, 0xeb, 0x41, 0xd2, 0x7e, 0xd6, 0xda, 0x6b, 0xf5, 0xaf, 0x0, 0x75, 0x9f, 0xd9, 0x22, 0xc, 0x6e, 0x74, 0xac, 0x2a, 0xa9, 0xa7}}
+ return a, nil
+}
+
+var _prestate_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x9c\x57\xdd\x6f\xdb\x38\x12\x7f\x96\xfe\x8a\x41\x5f\x6c\xa3\xae\xdc\x64\x81\x3d\xc0\xb9\x1c\xa0\xba\x6e\x1b\x20\x9b\x04\xb6\x7b\xb9\xdc\x62\x1f\x28\x72\x24\x73\x4d\x93\x02\x49\xd9\xf1\x15\xf9\xdf\x0f\x43\x7d\xf8\xa3\x49\xd3\xdd\x37\x9b\x1c\xfe\xe6\xfb\x37\xa3\xd1\x08\x26\xa6\xdc\x59\x59\x2c\x3d\x9c\xbf\x3f\xfb\x07\x2c\x96\x08\x85\x79\x87\x7e\x89\x16\xab\x35\xa4\x95\x5f\x1a\xeb\xe2\xd1\x08\x16\x4b\xe9\x20\x97\x0a\x41\x3a\x28\x99\xf5\x60\x72\xf0\x27\xf2\x4a\x66\x96\xd9\x5d\x12\x8f\x46\xf5\x9b\x67\xaf\x09\x21\xb7\x88\xe0\x4c\xee\xb7\xcc\xe2\x18\x76\xa6\x02\xce\x34\x58\x14\xd2\x79\x2b\xb3\xca\x23\x48\x0f\x4c\x8b\x91\xb1\xb0\x36\x42\xe6\x3b\x82\x94\x1e\x2a\x2d\xd0\x06\xd5\x1e\xed\xda\xb5\x76\x7c\xbe\xf9\x0a\xd7\xe8\x1c\x5a\xf8\x8c\x1a\x2d\x53\x70\x57\x65\x4a\x72\xb8\x96\x1c\xb5\x43\x60\x0e\x4a\x3a\x71\x4b\x14\x90\x05\x38\x7a\xf8\x89\x4c\x99\x37\xa6\xc0\x27\x53\x69\xc1\xbc\x34\x7a\x08\x28\xc9\x72\xd8\xa0\x75\xd2\x68\xf8\xa5\x55\xd5\x00\x0e\xc1\x58\x02\xe9\x33\x4f\x0e\x58\x30\x25\xbd\x1b\x00\xd3\x3b\x50\xcc\xef\x9f\xfe\x44\x40\xf6\x7e\x0b\x90\x3a\xa8\x59\x9a\x12\xc1\x2f\x99\x27\xaf\xb7\x52\x29\xc8\x10\x2a\x87\x79\xa5\x86\x84\x96\x55\x1e\xee\xaf\x16\x5f\x6e\xbf\x2e\x20\xbd\x79\x80\xfb\x74\x36\x4b\x6f\x16\x0f\x17\xb0\x95\x7e\x69\x2a\x0f\xb8\xc1\x1a\x4a\xae\x4b\x25\x51\xc0\x96\x59\xcb\xb4\xdf\x81\xc9\x09\xe1\xb7\xe9\x6c\xf2\x25\xbd\x59\xa4\x1f\xae\xae\xaf\x16\x0f\x60\x2c\x7c\xba\x5a\xdc\x4c\xe7\x73\xf8\x74\x3b\x83\x14\xee\xd2\xd9\xe2\x6a\xf2\xf5\x3a\x9d\xc1\xdd\xd7\xd9\xdd\xed\x7c\x9a\xc0\x1c\xc9\x2a\xa4\xf7\xaf\xc7\x3c\x0f\xd9\xb3\x08\x02\x3d\x93\xca\xb5\x91\x78\x30\x15\xb8\xa5\xa9\x94\x80\x25\xdb\x20\x58\xe4\x28\x37\x28\x80\x01\x37\xe5\xee\xa7\x93\x4a\x58\x4c\x19\x5d\x04\x9f\x5f\x2c\x48\xb8\xca\x41\x1b\x3f\x04\x87\x08\xff\x5c\x7a\x5f\x8e\x47\xa3\xed\x76\x9b\x14\xba\x4a\x8c\x2d\x46\xaa\x86\x73\xa3\x7f\x25\x31\x61\x96\x16\x9d\x67\x1e\x17\x96\x71\xb4\x60\x2a\x5f\x56\xde\x81\xab\xf2\x5c\x72\x89\xda\x83\xd4\xb9\xb1\xeb\x50\x29\xe0\x0d\x70\x8b\xcc\x23\x30\x50\x86\x33\x05\xf8\x88\xbc\x0a\x77\x75\xa4\x43\xb9\x5a\xa6\x1d\xe3\xe1\x34\xb7\x66\x4d\xbe\x56\xce\xd3\x0f\xe7\x70\x9d\x29\x14\x50\xa0\x46\x27\x1d\x64\xca\xf0\x55\x12\x7f\x8b\xa3\x03\x63\xa8\x4e\x82\x87\x8d\x50\xa8\x8d\x2d\xf6\x2c\x42\x56\x49\x25\xa4\x2e\x92\x38\x6a\xa5\xc7\xa0\x2b\xa5\x86\x71\x80\x50\xc6\xac\xaa\x32\xe5\xdc\x54\xc1\xf6\x3f\x91\xfb\x1a\xcc\x95\xc8\x65\x4e\xc5\xc1\xba\x5b\x6f\xc2\x55\xa7\xd7\x64\x24\x9f\xc4\xd1\x11\xcc\x18\xf2\x4a\x07\x77\xfa\x4c\x08\x3b\x04\x91\x0d\xbe\xc5\x51\xb4\x61\x96\xb0\xe0\x12\xbc\xf9\x82\x8f\xe1\x72\x70\x11\x47\x91\xcc\xa1\xef\x97\xd2\x25\x2d\xf0\xef\x8c\xf3\x3f\xe0\xf2\xf2\x32\x34\x75\x2e\x35\x8a\x01\x10\x44\xf4\x9c\x58\x7d\x13\x65\x4c\x31\xcd\x71\x0c\xbd\xf7\x8f\x3d\x78\x0b\x22\x4b\x0a\xf4\x1f\xea\xd3\x5a\x59\xe2\xcd\xdc\x5b\xa9\x8b\xfe\xd9\xaf\x83\x61\x78\xa5\x4d\x78\x03\x8d\xf8\x8d\xe9\x84\xeb\x7b\x6e\x44\xb8\x6e\x6c\xae\xa5\x26\x46\x34\x42\x8d\x94\xf3\xc6\xb2\x02\xc7\xf0\xed\x89\xfe\x3f\x91\x57\x4f\x71\xf4\x74\x14\xe5\x79\x2d\xf4\x42\x94\x1b\x08\x40\xed\x6d\x57\xe7\x85\xa4\x4e\x3d\x4c\x40\xc0\xfb\x51\x12\xe6\xad\x29\x27\x49\x58\xe1\xee\xf5\x4c\xd0\x85\x14\x8f\xdd\xc5\x0a\x77\x83\x8b\xf8\xc5\x14\x25\x8d\xd1\xbf\x4b\xf1\xf8\xb3\xf9\x3a\x79\x73\x14\xd7\x39\x49\xed\xed\x1d\x0c\x4e\xe2\x68\xd1\x55\xca\x53\xb9\x4b\xbd\x31\x2b\x22\xae\x25\xc5\x47\xa9\x10\x12\x53\x52\xb6\x5c\xcd\x1c\x19\xa2\x06\xe9\xd1\x32\xa2\x4e\xb3\x41\x4b\x53\x03\x2c\xfa\xca\x6a\xd7\x85\x31\x97\x9a\xa9\x16\xb8\x89\xba\xb7\x8c\xd7\x3d\x53\x9f\x1f\xc4\x92\xfb\xc7\x10\xc5\xe0\xdd\x68\x04\xa9\x07\x72\x11\x4a\x23\xb5\x1f\xc2\x16\x41\x23\x0a\x6a\x7c\x81\xa2\xe2\x3e\xe0\xf5\x36\x4c\x55\xd8\xab\x9b\x9b\x28\x32\x3c\x35\x15\x4d\x82\x83\xe6\x1f\x06\x03\xd7\x66\x13\x46\x5c\xc6\xf8\x0a\x9a\x86\x33\x56\x16\x52\xc7\x4d\x38\x8f\x9a\x8d\x2c\x4a\x08\x38\x98\x15\x72\x45\x49\xa4\x93\x0f\x4c\xc1\x25\x64\xb2\xb8\xd2\xfe\x24\x79\x75\xd0\xdb\xa7\x83\x3f\x92\xa6\x79\x12\x47\x84\xd7\x3f\x1f\x0c\xe1\xec\xd7\xae\x22\xbc\x21\x28\x78\x1d\xcc\x9b\x97\xa1\xe2\xd3\x62\x78\xfe\x59\x50\x43\x1d\xfc\x36\x68\x4d\x5c\x95\x51\x3a\x6a\x3f\x43\x1c\x8f\xbb\xf8\xe2\x07\xb8\xc7\xbe\xb5\xb8\x4d\x68\x12\x26\xc4\xcb\xa0\x75\x8a\x3e\x22\xb7\xb8\x26\x56\xa7\x2c\x70\xa6\x14\xda\x9e\x83\xc0\x19\xc3\xa6\x9c\x42\xbe\x70\x5d\xfa\x5d\xcb\xf5\x9e\xd9\x02\xbd\x7b\xdd\xb0\x80\xf3\xee\x5d\x4b\x81\x21\x14\xbb\x12\xe1\xf2\x12\x7a\x93\xd9\x34\x5d\x4c\x7b\x4d\x1b\x8d\x46\x70\x8f\x61\x13\xca\x94\xcc\x84\xda\x81\x40\x85\x1e\x6b\xbb\x8c\x0e\x21\xea\x28\x61\x48\x2b\x0d\x2d\x1b\xf8\x28\x9d\x97\xba\x80\x9a\x29\xb6\x34\x57\x1b\xb8\xd0\x23\x9c\x55\x8e\xaa\xf5\x64\x08\x79\x43\x1b\x85\x45\xe2\x15\xe2\xff\xd0\x6e\x4c\xc9\x6e\x03\xc9\xa5\x75\x1e\x4a\xc5\x38\x26\x84\xd7\x19\xf3\x72\x7e\x9b\x4e\x26\xd5\xb3\xd0\x82\x01\x68\x3f\xe0\x98\xa2\x01\x49\xea\x1d\xf4\x5b\x8c\x41\x1c\x45\xb6\x95\x3e\xc0\xbe\xd8\x53\x82\xf3\x58\x1e\x12\x02\x2d\x16\xb8\x41\xa2\xd0\xc0\x06\xf5\x30\x24\x5d\xff\xfe\xad\x99\xbe\xe8\x92\x38\xa2\x77\x07\x7d\xad\x4c\x71\xdc\xd7\xa2\x0e\x0b\xaf\xac\xa5\xfc\x77\x14\x9c\x53\x8f\xff\x59\x39\x4f\x31\xb5\x14\x9e\x86\x2d\x9e\x23\xc9\x40\x89\x34\x6d\x07\xdf\x93\x21\xcd\xad\x30\x27\x48\x5d\x33\xa5\xea\x6d\xae\x34\x1e\xb5\x97\x4c\xa9\x1d\xe5\x61\x6b\x69\x8d\xa1\xc5\x65\x08\x4e\x92\x54\x60\x9c\x20\x2a\x35\x57\x95\xa8\xcb\x20\xd4\x71\x83\xe7\x82\xcd\xc7\xfb\xcf\x1a\x9d\x63\x05\x26\x54\x49\xb9\x7c\x6c\x36\x48\x0d\xbd\x9a\xe4\xfa\x83\x5e\xd2\x19\x79\x4c\x31\xca\x14\x49\x5b\x64\x44\xd3\xa9\x10\x16\x9d\xeb\x0f\x1a\xce\xe9\x32\x7b\xbf\x44\x4d\xc1\x07\x8d\x5b\xe8\x56\x13\xc6\x39\xad\x6a\x62\x08\x4c\x08\xa2\xb6\x93\x35\x22\x8e\x22\xb7\x95\x9e\x2f\x21\x68\x32\xe5\xbe\x17\x07\x4d\xfd\x73\xe6\x10\xde\x4c\xff\xb3\x98\xdc\x7e\x9c\x4e\x6e\xef\x1e\xde\x8c\xe1\xe8\x6c\x7e\xf5\xdf\x69\x77\xf6\x21\xbd\x4e\x6f\x26\xd3\x37\xe3\x30\x9b\x9f\x71\xc8\x9b\xd6\x05\x52\xe8\x3c\xe3\xab\xa4\x44\x5c\xf5\xdf\x1f\xf3\xc0\xde\xc1\x28\xca\x2c\xb2\xd5\xc5\xde\x98\xba\x41\x1b\x1d\x2d\xe5\xc2\x25\xbc\x18\xac\x8b\x97\xad\x99\x34\xf2\xfd\x96\xc8\xf7\xab\x48\xa0\x8a\xd7\xed\x38\xff\xcb\x86\x84\xde\x61\x7c\x35\x06\xc7\x14\x6d\xc0\xf2\x7f\xf4\xe5\x92\xe7\x0e\xfd\x10\x50\x0b\xb3\x25\xe6\xeb\x50\xeb\x9b\x06\xf7\x20\x64\x67\x83\x9a\x41\x6f\xf3\xfe\xa0\x13\x26\xb0\xef\x45\xcf\x9f\x13\x45\x2d\xe0\xb2\x45\x7f\x1b\x5e\xbe\x1e\xa8\xf3\x26\x52\x27\x0a\x7e\x39\xd9\xf0\xc2\xfd\x1a\xd7\xc6\xee\x9a\x71\x74\xe0\xdf\x8f\xa3\x9a\x5e\x5f\x77\xf5\x44\x7f\xa8\xc8\xba\x83\x8f\xd3\xeb\xe9\xe7\x74\x31\x3d\x92\x9a\x2f\xd2\xc5\xd5\xa4\x3e\xfa\xcb\x85\x77\xf6\xd3\x85\xd7\x9b\xcf\x17\xb7\xb3\x69\x6f\xdc\xfc\xbb\xbe\x4d\x3f\xf6\xbe\x53\xd8\x6c\x81\x3f\x6a\x5d\x6f\xee\x8d\x15\x7f\xa7\x03\x0e\x36\xb2\x9c\x3d\xb7\x90\x05\x6a\xe7\xbe\x3a\xf9\xe0\x01\xa6\x5b\x56\xce\xeb\x8f\xbe\x28\xbc\x7f\x96\x87\x9f\xe2\xa7\xf8\xff\x01\x00\x00\xff\xff\xb1\x28\x85\x2a\x8a\x10\x00\x00")
+
+func prestate_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _prestate_tracerJs,
+ "prestate_tracer.js",
+ )
+}
+
+func prestate_tracerJs() (*asset, error) {
+ bytes, err := prestate_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "prestate_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe9, 0x79, 0x70, 0x4f, 0xc5, 0x78, 0x57, 0x63, 0x6f, 0x5, 0x31, 0xce, 0x3e, 0x5d, 0xbd, 0x71, 0x4, 0x46, 0x78, 0xcd, 0x1d, 0xcd, 0xb9, 0xd8, 0x10, 0xff, 0xe6, 0xc5, 0x59, 0xb9, 0x25, 0x6e}}
+ return a, nil
+}
+
+var _trigram_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x94\x4f\x6f\xe3\x36\x10\xc5\xef\xfe\x14\xaf\x27\x27\x88\xd7\x4a\xda\x4b\xe1\xd4\x05\xdc\x6c\xb2\x6b\x20\x6b\x07\xb6\xd2\x45\x10\xe4\x40\x4b\x23\x89\x08\x4d\x0a\xe4\xd0\x5e\x21\xc8\x77\x2f\xa8\x3f\xfe\x13\xb8\xed\xfa\x64\x70\xe6\xfd\xe6\xcd\x70\xc4\x28\xc2\x8d\x29\x2b\x2b\xf3\x82\xf1\xeb\xe5\xd5\xef\x88\x0b\x42\x6e\x3e\x11\x17\x64\xc9\xaf\x31\xf1\x5c\x18\xeb\x7a\x51\x84\xb8\x90\x0e\x99\x54\x04\xe9\x50\x0a\xcb\x30\x19\xf8\x43\xbe\x92\x2b\x2b\x6c\x35\xec\x45\x51\xa3\x39\x19\x0e\x84\xcc\x12\xc1\x99\x8c\xb7\xc2\xd2\x08\x95\xf1\x48\x84\x86\xa5\x54\x3a\xb6\x72\xe5\x99\x20\x19\x42\xa7\x91\xb1\x58\x9b\x54\x66\x55\x40\x4a\x86\xd7\x29\xd9\xba\x34\x93\x5d\xbb\xce\xc7\x97\xd9\x23\xee\xc9\x39\xb2\xf8\x42\x9a\xac\x50\x78\xf0\x2b\x25\x13\xdc\xcb\x84\xb4\x23\x08\x87\x32\x9c\xb8\x82\x52\xac\x6a\x5c\x10\xde\x05\x2b\xcb\xd6\x0a\xee\x8c\xd7\xa9\x60\x69\xf4\x00\x24\x83\x73\x6c\xc8\x3a\x69\x34\x7e\xeb\x4a\xb5\xc0\x01\x8c\x0d\x90\x33\xc1\xa1\x01\x0b\x53\x06\xdd\x39\x84\xae\xa0\x04\xef\xa5\x3f\x31\x90\x7d\xdf\x29\xa4\xae\xcb\x14\xa6\x24\x70\x21\x38\x74\xbd\x95\x4a\x61\x45\xf0\x8e\x32\xaf\x06\x81\xb6\xf2\x8c\xef\xd3\xf8\xeb\xfc\x31\xc6\x64\xf6\x84\xef\x93\xc5\x62\x32\x8b\x9f\xae\xb1\x95\x5c\x18\xcf\xa0\x0d\x35\x28\xb9\x2e\x95\xa4\x14\x5b\x61\xad\xd0\x5c\xc1\x64\x81\xf0\xed\x76\x71\xf3\x75\x32\x8b\x27\x7f\x4d\xef\xa7\xf1\x13\x8c\xc5\xdd\x34\x9e\xdd\x2e\x97\xb8\x9b\x2f\x30\xc1\xc3\x64\x11\x4f\x6f\x1e\xef\x27\x0b\x3c\x3c\x2e\x1e\xe6\xcb\xdb\x21\x96\x14\x5c\x51\xd0\xff\xff\xcc\xb3\xfa\xf6\x2c\x21\x25\x16\x52\xb9\x6e\x12\x4f\xc6\xc3\x15\xc6\xab\x14\x85\xd8\x10\x2c\x25\x24\x37\x94\x42\x20\x31\x65\xf5\xd3\x97\x1a\x58\x42\x19\x9d\xd7\x3d\xff\xeb\x42\x62\x9a\x41\x1b\x1e\xc0\x11\xe1\x8f\x82\xb9\x1c\x45\xd1\x76\xbb\x1d\xe6\xda\x0f\x8d\xcd\x23\xd5\xe0\x5c\xf4\xe7\xb0\xd7\x7b\xeb\x01\x40\x14\xa1\x90\x8e\xc3\xe5\x04\xec\x5a\x94\xb5\x2b\x2b\x73\x2b\xd6\x48\x8c\xd7\x4c\xd6\xd5\xa9\x21\x6f\x84\xb7\xf7\x41\x27\x54\xc2\xf1\xbc\x0c\xd2\xf0\x0f\xa6\x24\x5b\xef\x54\x1d\x6f\x82\x6e\x84\xe7\x7e\x7f\xd0\xef\xbf\x0c\x76\xa7\x9f\xa9\xe4\x62\x84\xcb\xe6\xa4\x65\x39\xa6\x9a\x24\xf5\xc6\xbc\x52\x5a\x8f\x94\x36\x64\x2b\x98\x32\x31\x69\xbb\x22\xc1\xe2\xdf\xdf\x40\x3f\x28\xf1\x4c\x6e\x58\x13\x82\x74\x84\xcc\xeb\x24\x14\x3f\x53\x26\x1f\x20\x5d\x9d\xe3\x6d\xc7\xdf\x08\x8b\x34\x54\xc5\x18\xca\xe4\xc3\x9c\x1a\x13\x67\xe7\xd7\xbb\x1c\x99\xe1\xac\xc9\xf9\x65\x0c\x2e\xa4\x1b\xee\xbc\x9e\xef\x49\xe1\xb7\x0b\xce\x4b\x87\x71\xd7\xdf\xf5\xe9\x9c\xcf\x6d\xd9\x1a\x7d\x9c\x63\x89\xbd\xd5\xfb\xb3\xf7\x23\xbf\xa6\x6c\xcd\x9a\x72\xc8\x66\xc9\x56\xea\xfc\xd0\x6f\xc8\x79\xa5\x0a\xe3\x23\x3f\xcf\x97\x2f\x17\xfd\x4f\xfd\x8b\xa3\xb3\xab\xe6\xcc\x94\xc7\xdd\xd6\x39\xe1\x52\x9f\x5f\xa9\x7a\x39\xd5\xe4\x2e\x78\x71\x71\xca\x26\x29\x47\xf8\x2f\x19\xc6\xb8\x3a\x25\xfc\xe0\xf8\x63\x0f\x57\x07\xc3\xfc\x10\xc0\x18\x5d\x1b\xfb\x3d\xcc\x84\x57\x7c\xb8\x3c\xdb\xa2\x7d\x11\x44\xc2\x5e\xa8\x76\x5f\xc2\xeb\x66\x32\x08\xdd\xad\x54\xd6\x7c\xab\x81\x52\x23\x4e\x2e\xd1\xbe\x8c\x25\x77\xaa\x8e\x50\xaa\xae\xd5\x40\x5d\xf3\xa5\xaf\x88\x34\x24\x87\x0f\x82\x52\x98\x0d\xd9\xf0\xca\xb7\x57\xee\x3a\x62\x90\x65\x52\x0b\xd5\xb1\xdb\x07\x81\xad\x48\xa4\xce\x1b\x6b\x4d\xe8\xc0\x5b\xc2\x3f\x0e\x97\xbb\x61\xee\x27\xbf\x9b\xce\x7b\xef\x9f\x00\x00\x00\xff\xff\xb3\x93\x16\xd5\xfc\x06\x00\x00")
+
+func trigram_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _trigram_tracerJs,
+ "trigram_tracer.js",
+ )
+}
+
+func trigram_tracerJs() (*asset, error) {
+ bytes, err := trigram_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "trigram_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x40, 0x63, 0xe1, 0x42, 0x60, 0x7, 0x1b, 0x79, 0x47, 0x1, 0xa1, 0xbf, 0xc4, 0x66, 0x19, 0x9b, 0x2b, 0x5a, 0x1f, 0x82, 0x3d, 0xcf, 0xee, 0xe7, 0x60, 0x25, 0x2c, 0x4f, 0x13, 0x97, 0xc7, 0x18}}
+ return a, nil
+}
+
+var _unigram_tracerJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x54\x4d\x6f\xdb\x46\x10\xbd\xeb\x57\xbc\xa3\x8c\xa8\xa4\xd3\x5e\x0a\xa5\x09\xc0\x1a\x76\x22\xc0\x91\x0d\x89\x6e\x60\x14\x3d\x2c\xc9\x21\xb9\xe8\x6a\x87\xd8\x9d\x95\x42\x04\xfa\xef\xc5\x92\xa2\xe5\x1a\x6e\x13\x9e\x04\xcd\xbc\x8f\x79\x33\x64\x9a\xe2\x8a\xbb\xde\xe9\xa6\x15\xfc\x7c\xf9\xf6\x57\xe4\x2d\xa1\xe1\x9f\x48\x5a\x72\x14\x76\xc8\x82\xb4\xec\xfc\x2c\x4d\x91\xb7\xda\xa3\xd6\x86\xa0\x3d\x3a\xe5\x04\x5c\x43\x5e\xf4\x1b\x5d\x38\xe5\xfa\x64\x96\xa6\x23\xe6\xd5\x72\x64\xa8\x1d\x11\x3c\xd7\x72\x50\x8e\x96\xe8\x39\xa0\x54\x16\x8e\x2a\xed\xc5\xe9\x22\x08\x41\x0b\x94\xad\x52\x76\xd8\x71\xa5\xeb\x3e\x52\x6a\x41\xb0\x15\xb9\x41\x5a\xc8\xed\xfc\xe4\xe3\xe3\xfa\x01\xb7\xe4\x3d\x39\x7c\x24\x4b\x4e\x19\xdc\x87\xc2\xe8\x12\xb7\xba\x24\xeb\x09\xca\xa3\x8b\xff\xf8\x96\x2a\x14\x03\x5d\x04\xde\x44\x2b\xdb\x93\x15\xdc\x70\xb0\x95\x12\xcd\x76\x01\xd2\xd1\x39\xf6\xe4\xbc\x66\x8b\x5f\x26\xa9\x13\xe1\x02\xec\x22\xc9\x5c\x49\x1c\xc0\x81\xbb\x88\xbb\x80\xb2\x3d\x8c\x92\x33\xf4\x07\x02\x39\xcf\x5d\x41\xdb\x41\xa6\xe5\x8e\x20\xad\x92\x38\xf5\x41\x1b\x83\x82\x10\x3c\xd5\xc1\x2c\x22\x5b\x11\x04\x5f\x56\xf9\xa7\xbb\x87\x1c\xd9\xfa\x11\x5f\xb2\xcd\x26\x5b\xe7\x8f\xef\x70\xd0\xd2\x72\x10\xd0\x9e\x46\x2a\xbd\xeb\x8c\xa6\x0a\x07\xe5\x9c\xb2\xd2\x83\xeb\xc8\xf0\xf9\x7a\x73\xf5\x29\x5b\xe7\xd9\xef\xab\xdb\x55\xfe\x08\x76\xb8\x59\xe5\xeb\xeb\xed\x16\x37\x77\x1b\x64\xb8\xcf\x36\xf9\xea\xea\xe1\x36\xdb\xe0\xfe\x61\x73\x7f\xb7\xbd\x4e\xb0\xa5\xe8\x8a\x22\xfe\xfb\x99\xd7\xc3\xf6\x1c\xa1\x22\x51\xda\xf8\x29\x89\x47\x0e\xf0\x2d\x07\x53\xa1\x55\x7b\x82\xa3\x92\xf4\x9e\x2a\x28\x94\xdc\xf5\x3f\xbc\xd4\xc8\xa5\x0c\xdb\x66\x98\xf9\x3f\x0f\x12\xab\x1a\x96\x65\x01\x4f\x84\xdf\x5a\x91\x6e\x99\xa6\x87\xc3\x21\x69\x6c\x48\xd8\x35\xa9\x19\xe9\x7c\xfa\x21\x99\xcd\xbe\xcd\x00\x20\x4d\xd1\x6a\x2f\x71\x39\x91\x76\xa7\xba\xe8\x8a\xbb\x92\x2b\xf2\x10\x46\xc9\xc1\x0a\x39\x3f\x74\xc7\xd6\x25\xbe\x1d\x17\x13\xd6\x72\xe7\xc7\x16\x0f\x1b\x76\x05\xb9\x11\x3e\xb6\xc7\xea\x12\x97\x4f\xdd\x5e\xa8\x8b\x4a\xda\xee\xf9\x6f\xaa\x86\xdc\x68\x4f\xae\x3f\x09\x8e\x77\x10\x7d\xfc\xf1\x19\xf4\x95\xca\x20\xe4\x93\x01\x1d\xa1\x4b\xd4\xc1\x96\xf1\xfa\xe6\x86\x9b\x05\xaa\xe2\x02\xe3\x14\xf1\xd9\xab\x78\x9b\x78\x0f\xc3\x4d\xc2\x5d\x22\xbc\x15\xa7\x6d\x33\xbf\x78\xf7\xd4\xa3\x6b\xcc\xa5\xd5\x3e\x89\x83\xfc\xc9\xdd\x5f\x17\x67\x7c\x7c\xfe\x55\x7b\xf3\xe6\x0c\x3c\x3e\xfd\x22\xe3\x09\xff\x83\xc2\x7b\xbc\x7d\x0d\x37\x34\xc5\x40\x26\xda\x73\x88\xb5\x0a\x46\x9e\xe7\x72\x68\x4f\x17\xad\x4a\x09\xca\x9c\xa2\x88\x6f\x27\xd7\x50\x76\x4a\xab\x1e\x6f\x2d\xb2\x0c\x14\xaf\xe6\x73\x5c\xcc\x26\x1d\x47\xfe\x35\x21\x65\xcc\x20\x36\x2d\x7d\x38\xd5\x82\xc8\x42\x0b\x39\x15\xdf\x55\xde\x93\x8b\x9f\x29\x38\x92\xe0\xac\x9f\x18\x23\xac\xd6\x56\x99\x89\xfb\x74\xd1\xe2\x54\xa9\x6d\x33\x7a\x1b\x4b\xcf\xcc\x95\xf2\xf5\xf9\xe2\x74\x3d\x7f\x0a\x07\x1f\x70\xf9\x62\x27\xa3\xe4\x39\xe4\x97\xe1\x1e\x17\xb3\xe3\xec\x9f\x00\x00\x00\xff\xff\x8d\xba\x8d\xa8\xe6\x05\x00\x00")
+
+func unigram_tracerJsBytes() ([]byte, error) {
+ return bindataRead(
+ _unigram_tracerJs,
+ "unigram_tracer.js",
+ )
+}
+
+func unigram_tracerJs() (*asset, error) {
+ bytes, err := unigram_tracerJsBytes()
+ if err != nil {
+ return nil, err
+ }
+
+ info := bindataFileInfo{name: "unigram_tracer.js", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
+ a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x2f, 0x36, 0x14, 0xc2, 0xf6, 0xc3, 0x80, 0x2b, 0x4a, 0x11, 0x7d, 0xd5, 0x3e, 0xef, 0x23, 0xb5, 0xd6, 0xe6, 0xe6, 0x5, 0x41, 0xf6, 0x14, 0x7a, 0x39, 0xf7, 0xf8, 0xac, 0x89, 0x8e, 0x43, 0xe6}}
+ return a, nil
+}
+
+// Asset loads and returns the asset for the given name.
+// It returns an error if the asset could not be found or
+// could not be loaded.
+func Asset(name string) ([]byte, error) {
+ canonicalName := strings.Replace(name, "\\", "/", -1)
+ if f, ok := _bindata[canonicalName]; ok {
+ a, err := f()
+ if err != nil {
+ return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
+ }
+ return a.bytes, nil
+ }
+ return nil, fmt.Errorf("Asset %s not found", name)
+}
+
+// AssetString returns the asset contents as a string (instead of a []byte).
+func AssetString(name string) (string, error) {
+ data, err := Asset(name)
+ return string(data), err
+}
+
+// MustAsset is like Asset but panics when Asset would return an error.
+// It simplifies safe initialization of global variables.
+func MustAsset(name string) []byte {
+ a, err := Asset(name)
+ if err != nil {
+ panic("asset: Asset(" + name + "): " + err.Error())
+ }
+
+ return a
+}
+
+// MustAssetString is like AssetString but panics when Asset would return an
+// error. It simplifies safe initialization of global variables.
+func MustAssetString(name string) string {
+ return string(MustAsset(name))
+}
+
+// AssetInfo loads and returns the asset info for the given name.
+// It returns an error if the asset could not be found or
+// could not be loaded.
+func AssetInfo(name string) (os.FileInfo, error) {
+ canonicalName := strings.Replace(name, "\\", "/", -1)
+ if f, ok := _bindata[canonicalName]; ok {
+ a, err := f()
+ if err != nil {
+ return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
+ }
+ return a.info, nil
+ }
+ return nil, fmt.Errorf("AssetInfo %s not found", name)
+}
+
+// AssetDigest returns the digest of the file with the given name. It returns an
+// error if the asset could not be found or the digest could not be loaded.
+func AssetDigest(name string) ([sha256.Size]byte, error) {
+ canonicalName := strings.Replace(name, "\\", "/", -1)
+ if f, ok := _bindata[canonicalName]; ok {
+ a, err := f()
+ if err != nil {
+ return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s can't read by error: %v", name, err)
+ }
+ return a.digest, nil
+ }
+ return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s not found", name)
+}
+
+// Digests returns a map of all known files and their checksums.
+func Digests() (map[string][sha256.Size]byte, error) {
+ mp := make(map[string][sha256.Size]byte, len(_bindata))
+ for name := range _bindata {
+ a, err := _bindata[name]()
+ if err != nil {
+ return nil, err
+ }
+ mp[name] = a.digest
+ }
+ return mp, nil
+}
+
+// AssetNames returns the names of the assets.
+func AssetNames() []string {
+ names := make([]string, 0, len(_bindata))
+ for name := range _bindata {
+ names = append(names, name)
+ }
+ return names
+}
+
+// _bindata is a table, holding each asset generator, mapped to its name.
+var _bindata = map[string]func() (*asset, error){
+ "4byte_tracer.js": _4byte_tracerJs,
+
+ "bigram_tracer.js": bigram_tracerJs,
+
+ "call_tracer.js": call_tracerJs,
+
+ "evmdis_tracer.js": evmdis_tracerJs,
+
+ "noop_tracer.js": noop_tracerJs,
+
+ "opcount_tracer.js": opcount_tracerJs,
+
+ "prestate_tracer.js": prestate_tracerJs,
+
+ "trigram_tracer.js": trigram_tracerJs,
+
+ "unigram_tracer.js": unigram_tracerJs,
+}
+
+// AssetDir returns the file names below a certain
+// directory embedded in the file by go-bindata.
+// For example if you run go-bindata on data/... and data contains the
+// following hierarchy:
+// data/
+// foo.txt
+// img/
+// a.png
+// b.png
+// then AssetDir("data") would return []string{"foo.txt", "img"},
+// AssetDir("data/img") would return []string{"a.png", "b.png"},
+// AssetDir("foo.txt") and AssetDir("notexist") would return an error, and
+// AssetDir("") will return []string{"data"}.
+func AssetDir(name string) ([]string, error) {
+ node := _bintree
+ if len(name) != 0 {
+ canonicalName := strings.Replace(name, "\\", "/", -1)
+ pathList := strings.Split(canonicalName, "/")
+ for _, p := range pathList {
+ node = node.Children[p]
+ if node == nil {
+ return nil, fmt.Errorf("Asset %s not found", name)
+ }
+ }
+ }
+ if node.Func != nil {
+ return nil, fmt.Errorf("Asset %s not found", name)
+ }
+ rv := make([]string, 0, len(node.Children))
+ for childName := range node.Children {
+ rv = append(rv, childName)
+ }
+ return rv, nil
+}
+
+type bintree struct {
+ Func func() (*asset, error)
+ Children map[string]*bintree
+}
+
+var _bintree = &bintree{nil, map[string]*bintree{
+ "4byte_tracer.js": {_4byte_tracerJs, map[string]*bintree{}},
+ "bigram_tracer.js": {bigram_tracerJs, map[string]*bintree{}},
+ "call_tracer.js": {call_tracerJs, map[string]*bintree{}},
+ "evmdis_tracer.js": {evmdis_tracerJs, map[string]*bintree{}},
+ "noop_tracer.js": {noop_tracerJs, map[string]*bintree{}},
+ "opcount_tracer.js": {opcount_tracerJs, map[string]*bintree{}},
+ "prestate_tracer.js": {prestate_tracerJs, map[string]*bintree{}},
+ "trigram_tracer.js": {trigram_tracerJs, map[string]*bintree{}},
+ "unigram_tracer.js": {unigram_tracerJs, map[string]*bintree{}},
+}}
+
+// RestoreAsset restores an asset under the given directory.
+func RestoreAsset(dir, name string) error {
+ data, err := Asset(name)
+ if err != nil {
+ return err
+ }
+ info, err := AssetInfo(name)
+ if err != nil {
+ return err
+ }
+ err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
+ if err != nil {
+ return err
+ }
+ err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
+ if err != nil {
+ return err
+ }
+ return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
+}
+
+// RestoreAssets restores an asset under the given directory recursively.
+func RestoreAssets(dir, name string) error {
+ children, err := AssetDir(name)
+ // File
+ if err != nil {
+ return RestoreAsset(dir, name)
+ }
+ // Dir
+ for _, child := range children {
+ err = RestoreAssets(dir, filepath.Join(name, child))
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func _filePath(dir, name string) string {
+ canonicalName := strings.Replace(name, "\\", "/", -1)
+ return filepath.Join(append([]string{dir}, strings.Split(canonicalName, "/")...)...)
+}
diff --git a/eth/tracers/internal/tracers/bigram_tracer.js b/eth/tracers/internal/tracers/bigram_tracer.js
new file mode 100644
index 0000000..421c360
--- /dev/null
+++ b/eth/tracers/internal/tracers/bigram_tracer.js
@@ -0,0 +1,47 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+{
+ // hist is the counters of opcode bigrams
+ hist: {},
+ // lastOp is last operation
+ lastOp: '',
+ // execution depth of last op
+ lastDepth: 0,
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) {
+ var op = log.op.toString();
+ var depth = log.getDepth();
+ if (depth == this.lastDepth){
+ var key = this.lastOp+'-'+op;
+ if (this.hist[key]){
+ this.hist[key]++;
+ }
+ else {
+ this.hist[key] = 1;
+ }
+ }
+ this.lastOp = op;
+ this.lastDepth = depth;
+ },
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) {},
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx) {
+ return this.hist;
+ },
+}
diff --git a/eth/tracers/internal/tracers/call_tracer.js b/eth/tracers/internal/tracers/call_tracer.js
new file mode 100644
index 0000000..f8b383c
--- /dev/null
+++ b/eth/tracers/internal/tracers/call_tracer.js
@@ -0,0 +1,246 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// callTracer is a full blown transaction tracer that extracts and reports all
+// the internal calls made by a transaction, along with any useful information.
+{
+ // callstack is the current recursive call stack of the EVM execution.
+ callstack: [{}],
+
+ // descended tracks whether we've just descended from an outer transaction into
+ // an inner call.
+ descended: false,
+
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) {
+ // Capture any errors immediately
+ var error = log.getError();
+ if (error !== undefined) {
+ this.fault(log, db);
+ return;
+ }
+ // We only care about system opcodes, faster if we pre-check once
+ var syscall = (log.op.toNumber() & 0xf0) == 0xf0;
+ if (syscall) {
+ var op = log.op.toString();
+ }
+ // If a new contract is being created, add to the call stack
+ if (syscall && (op == 'CREATE' || op == "CREATE2")) {
+ var inOff = log.stack.peek(1).valueOf();
+ var inEnd = inOff + log.stack.peek(2).valueOf();
+
+ // Assemble the internal call report and store for completion
+ var call = {
+ type: op,
+ from: toHex(log.contract.getAddress()),
+ input: toHex(log.memory.slice(inOff, inEnd)),
+ gasIn: log.getGas(),
+ gasCost: log.getCost(),
+ value: '0x' + log.stack.peek(0).toString(16)
+ };
+ this.callstack.push(call);
+ this.descended = true
+ return;
+ }
+ // If a contract is being self destructed, gather that as a subcall too
+ if (syscall && op == 'SELFDESTRUCT') {
+ var left = this.callstack.length;
+ if (this.callstack[left-1].calls === undefined) {
+ this.callstack[left-1].calls = [];
+ }
+ this.callstack[left-1].calls.push({type: op});
+ return
+ }
+ // If a new method invocation is being done, add to the call stack
+ if (syscall && (op == 'CALL' || op == 'CALLCODE' || op == 'DELEGATECALL' || op == 'STATICCALL')) {
+ // Skip any pre-compile invocations, those are just fancy opcodes
+ var to = toAddress(log.stack.peek(1).toString(16));
+ if (isPrecompiled(to)) {
+ return
+ }
+ var off = (op == 'DELEGATECALL' || op == 'STATICCALL' ? 0 : 1);
+
+ var inOff = log.stack.peek(2 + off).valueOf();
+ var inEnd = inOff + log.stack.peek(3 + off).valueOf();
+
+ // Assemble the internal call report and store for completion
+ var call = {
+ type: op,
+ from: toHex(log.contract.getAddress()),
+ to: toHex(to),
+ input: toHex(log.memory.slice(inOff, inEnd)),
+ gasIn: log.getGas(),
+ gasCost: log.getCost(),
+ outOff: log.stack.peek(4 + off).valueOf(),
+ outLen: log.stack.peek(5 + off).valueOf()
+ };
+ if (op != 'DELEGATECALL' && op != 'STATICCALL') {
+ call.value = '0x' + log.stack.peek(2).toString(16);
+ }
+ this.callstack.push(call);
+ this.descended = true
+ return;
+ }
+ // If we've just descended into an inner call, retrieve it's true allowance. We
+ // need to extract if from within the call as there may be funky gas dynamics
+ // with regard to requested and actually given gas (2300 stipend, 63/64 rule).
+ if (this.descended) {
+ if (log.getDepth() >= this.callstack.length) {
+ this.callstack[this.callstack.length - 1].gas = log.getGas();
+ } else {
+ // TODO(karalabe): The call was made to a plain account. We currently don't
+ // have access to the true gas amount inside the call and so any amount will
+ // mostly be wrong since it depends on a lot of input args. Skip gas for now.
+ }
+ this.descended = false;
+ }
+ // If an existing call is returning, pop off the call stack
+ if (syscall && op == 'REVERT') {
+ this.callstack[this.callstack.length - 1].error = "execution reverted";
+ return;
+ }
+ if (log.getDepth() == this.callstack.length - 1) {
+ // Pop off the last call and get the execution results
+ var call = this.callstack.pop();
+
+ if (call.type == 'CREATE' || call.type == "CREATE2") {
+ // If the call was a CREATE, retrieve the contract address and output code
+ call.gasUsed = '0x' + bigInt(call.gasIn - call.gasCost - log.getGas()).toString(16);
+ delete call.gasIn; delete call.gasCost;
+
+ var ret = log.stack.peek(0);
+ if (!ret.equals(0)) {
+ call.to = toHex(toAddress(ret.toString(16)));
+ call.output = toHex(db.getCode(toAddress(ret.toString(16))));
+ } else if (call.error === undefined) {
+ call.error = "internal failure"; // TODO(karalabe): surface these faults somehow
+ }
+ } else {
+ // If the call was a contract call, retrieve the gas usage and output
+ if (call.gas !== undefined) {
+ call.gasUsed = '0x' + bigInt(call.gasIn - call.gasCost + call.gas - log.getGas()).toString(16);
+
+ var ret = log.stack.peek(0);
+ if (!ret.equals(0)) {
+ call.output = toHex(log.memory.slice(call.outOff, call.outOff + call.outLen));
+ } else if (call.error === undefined) {
+ call.error = "internal failure"; // TODO(karalabe): surface these faults somehow
+ }
+ }
+ delete call.gasIn; delete call.gasCost;
+ delete call.outOff; delete call.outLen;
+ }
+ if (call.gas !== undefined) {
+ call.gas = '0x' + bigInt(call.gas).toString(16);
+ }
+ // Inject the call into the previous one
+ var left = this.callstack.length;
+ if (this.callstack[left-1].calls === undefined) {
+ this.callstack[left-1].calls = [];
+ }
+ this.callstack[left-1].calls.push(call);
+ }
+ },
+
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) {
+ // If the topmost call already reverted, don't handle the additional fault again
+ if (this.callstack[this.callstack.length - 1].error !== undefined) {
+ return;
+ }
+ // Pop off the just failed call
+ var call = this.callstack.pop();
+ call.error = log.getError();
+
+ // Consume all available gas and clean any leftovers
+ if (call.gas !== undefined) {
+ call.gas = '0x' + bigInt(call.gas).toString(16);
+ call.gasUsed = call.gas
+ }
+ delete call.gasIn; delete call.gasCost;
+ delete call.outOff; delete call.outLen;
+
+ // Flatten the failed call into its parent
+ var left = this.callstack.length;
+ if (left > 0) {
+ if (this.callstack[left-1].calls === undefined) {
+ this.callstack[left-1].calls = [];
+ }
+ this.callstack[left-1].calls.push(call);
+ return;
+ }
+ // Last call failed too, leave it in the stack
+ this.callstack.push(call);
+ },
+
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx, db) {
+ var result = {
+ type: ctx.type,
+ from: toHex(ctx.from),
+ to: toHex(ctx.to),
+ value: '0x' + ctx.value.toString(16),
+ gas: '0x' + bigInt(ctx.gas).toString(16),
+ gasUsed: '0x' + bigInt(ctx.gasUsed).toString(16),
+ input: toHex(ctx.input),
+ output: toHex(ctx.output),
+ time: ctx.time,
+ };
+ if (this.callstack[0].calls !== undefined) {
+ result.calls = this.callstack[0].calls;
+ }
+ if (this.callstack[0].error !== undefined) {
+ result.error = this.callstack[0].error;
+ } else if (ctx.error !== undefined) {
+ result.error = ctx.error;
+ }
+ if (result.error !== undefined) {
+ delete result.output;
+ }
+ return this.finalize(result);
+ },
+
+ // finalize recreates a call object using the final desired field oder for json
+ // serialization. This is a nicety feature to pass meaningfully ordered results
+ // to users who don't interpret it, just display it.
+ finalize: function(call) {
+ var sorted = {
+ type: call.type,
+ from: call.from,
+ to: call.to,
+ value: call.value,
+ gas: call.gas,
+ gasUsed: call.gasUsed,
+ input: call.input,
+ output: call.output,
+ error: call.error,
+ time: call.time,
+ calls: call.calls,
+ }
+ for (var key in sorted) {
+ if (sorted[key] === undefined) {
+ delete sorted[key];
+ }
+ }
+ if (sorted.calls !== undefined) {
+ for (var i=0; i<sorted.calls.length; i++) {
+ sorted.calls[i] = this.finalize(sorted.calls[i]);
+ }
+ }
+ return sorted;
+ }
+}
diff --git a/eth/tracers/internal/tracers/evmdis_tracer.js b/eth/tracers/internal/tracers/evmdis_tracer.js
new file mode 100644
index 0000000..bb19777
--- /dev/null
+++ b/eth/tracers/internal/tracers/evmdis_tracer.js
@@ -0,0 +1,93 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// evmdisTracer returns sufficient information from a trace to perform evmdis-style
+// disassembly.
+{
+ stack: [{ops: []}],
+
+ npushes: {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 32: 1, 48: 1, 49: 1, 50: 1, 51: 1, 52: 1, 53: 1, 54: 1, 55: 0, 56: 1, 57: 0, 58: 1, 59: 1, 60: 0, 64: 1, 65: 1, 66: 1, 67: 1, 68: 1, 69: 1, 80: 0, 81: 1, 82: 0, 83: 0, 84: 1, 85: 0, 86: 0, 87: 0, 88: 1, 89: 1, 90: 1, 91: 0, 96: 1, 97: 1, 98: 1, 99: 1, 100: 1, 101: 1, 102: 1, 103: 1, 104: 1, 105: 1, 106: 1, 107: 1, 108: 1, 109: 1, 110: 1, 111: 1, 112: 1, 113: 1, 114: 1, 115: 1, 116: 1, 117: 1, 118: 1, 119: 1, 120: 1, 121: 1, 122: 1, 123: 1, 124: 1, 125: 1, 126: 1, 127: 1, 128: 2, 129: 3, 130: 4, 131: 5, 132: 6, 133: 7, 134: 8, 135: 9, 136: 10, 137: 11, 138: 12, 139: 13, 140: 14, 141: 15, 142: 16, 143: 17, 144: 2, 145: 3, 146: 4, 147: 5, 148: 6, 149: 7, 150: 8, 151: 9, 152: 10, 153: 11, 154: 12, 155: 13, 156: 14, 157: 15, 158: 16, 159: 17, 160: 0, 161: 0, 162: 0, 163: 0, 164: 0, 240: 1, 241: 1, 242: 1, 243: 0, 244: 0, 255: 0},
+
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function() { return this.stack[0].ops; },
+
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) { },
+
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) {
+ var frame = this.stack[this.stack.length - 1];
+
+ var error = log.getError();
+ if (error) {
+ frame["error"] = error;
+ } else if (log.getDepth() == this.stack.length) {
+ opinfo = {
+ op: log.op.toNumber(),
+ depth : log.getDepth(),
+ result: [],
+ };
+ if (frame.ops.length > 0) {
+ var prevop = frame.ops[frame.ops.length - 1];
+ for(var i = 0; i < this.npushes[prevop.op]; i++)
+ prevop.result.push(log.stack.peek(i).toString(16));
+ }
+ switch(log.op.toString()) {
+ case "CALL": case "CALLCODE":
+ var instart = log.stack.peek(3).valueOf();
+ var insize = log.stack.peek(4).valueOf();
+ opinfo["gas"] = log.stack.peek(0).valueOf();
+ opinfo["to"] = log.stack.peek(1).toString(16);
+ opinfo["value"] = log.stack.peek(2).toString();
+ opinfo["input"] = log.memory.slice(instart, instart + insize);
+ opinfo["error"] = null;
+ opinfo["return"] = null;
+ opinfo["ops"] = [];
+ this.stack.push(opinfo);
+ break;
+ case "DELEGATECALL": case "STATICCALL":
+ var instart = log.stack.peek(2).valueOf();
+ var insize = log.stack.peek(3).valueOf();
+ opinfo["op"] = log.op.toString();
+ opinfo["gas"] = log.stack.peek(0).valueOf();
+ opinfo["to"] = log.stack.peek(1).toString(16);
+ opinfo["input"] = log.memory.slice(instart, instart + insize);
+ opinfo["error"] = null;
+ opinfo["return"] = null;
+ opinfo["ops"] = [];
+ this.stack.push(opinfo);
+ break;
+ case "RETURN":
+ var out = log.stack.peek(0).valueOf();
+ var outsize = log.stack.peek(1).valueOf();
+ frame.return = log.memory.slice(out, out + outsize);
+ break;
+ case "STOP": case "SUICIDE":
+ frame.return = log.memory.slice(0, 0);
+ break;
+ case "JUMPDEST":
+ opinfo["pc"] = log.getPC();
+ }
+ if(log.op.isPush()) {
+ opinfo["len"] = log.op.toNumber() - 0x5e;
+ }
+ frame.ops.push(opinfo);
+ } else {
+ this.stack = this.stack.slice(0, log.getDepth());
+ }
+ }
+}
diff --git a/eth/tracers/internal/tracers/noop_tracer.js b/eth/tracers/internal/tracers/noop_tracer.js
new file mode 100644
index 0000000..fe7ddc8
--- /dev/null
+++ b/eth/tracers/internal/tracers/noop_tracer.js
@@ -0,0 +1,29 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// noopTracer is just the barebone boilerplate code required from a JavaScript
+// object to be usable as a transaction tracer.
+{
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) { },
+
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) { },
+
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx, db) { return {}; }
+}
diff --git a/eth/tracers/internal/tracers/opcount_tracer.js b/eth/tracers/internal/tracers/opcount_tracer.js
new file mode 100644
index 0000000..f7984c7
--- /dev/null
+++ b/eth/tracers/internal/tracers/opcount_tracer.js
@@ -0,0 +1,32 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// opcountTracer is a sample tracer that just counts the number of instructions
+// executed by the EVM before the transaction terminated.
+{
+ // count tracks the number of EVM instructions executed.
+ count: 0,
+
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) { this.count++ },
+
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) { },
+
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx, db) { return this.count }
+}
diff --git a/eth/tracers/internal/tracers/prestate_tracer.js b/eth/tracers/internal/tracers/prestate_tracer.js
new file mode 100644
index 0000000..e0a22bf
--- /dev/null
+++ b/eth/tracers/internal/tracers/prestate_tracer.js
@@ -0,0 +1,108 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// prestateTracer outputs sufficient information to create a local execution of
+// the transaction from a custom assembled genesis block.
+{
+ // prestate is the genesis that we're building.
+ prestate: null,
+
+ // lookupAccount injects the specified account into the prestate object.
+ lookupAccount: function(addr, db){
+ var acc = toHex(addr);
+ if (this.prestate[acc] === undefined) {
+ this.prestate[acc] = {
+ balance: '0x' + db.getBalance(addr).toString(16),
+ nonce: db.getNonce(addr),
+ code: toHex(db.getCode(addr)),
+ storage: {}
+ };
+ }
+ },
+
+ // lookupStorage injects the specified storage entry of the given account into
+ // the prestate object.
+ lookupStorage: function(addr, key, db){
+ var acc = toHex(addr);
+ var idx = toHex(key);
+
+ if (this.prestate[acc].storage[idx] === undefined) {
+ this.prestate[acc].storage[idx] = toHex(db.getState(addr, key));
+ }
+ },
+
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx, db) {
+ // At this point, we need to deduct the 'value' from the
+ // outer transaction, and move it back to the origin
+ this.lookupAccount(ctx.from, db);
+
+ var fromBal = bigInt(this.prestate[toHex(ctx.from)].balance.slice(2), 16);
+ var toBal = bigInt(this.prestate[toHex(ctx.to)].balance.slice(2), 16);
+
+ this.prestate[toHex(ctx.to)].balance = '0x'+toBal.subtract(ctx.value).toString(16);
+ this.prestate[toHex(ctx.from)].balance = '0x'+fromBal.add(ctx.value).toString(16);
+
+ // Decrement the caller's nonce, and remove empty create targets
+ this.prestate[toHex(ctx.from)].nonce--;
+ if (ctx.type == 'CREATE') {
+ // We can blibdly delete the contract prestate, as any existing state would
+ // have caused the transaction to be rejected as invalid in the first place.
+ delete this.prestate[toHex(ctx.to)];
+ }
+ // Return the assembled allocations (prestate)
+ return this.prestate;
+ },
+
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) {
+ // Add the current account if we just started tracing
+ if (this.prestate === null){
+ this.prestate = {};
+ // Balance will potentially be wrong here, since this will include the value
+ // sent along with the message. We fix that in 'result()'.
+ this.lookupAccount(log.contract.getAddress(), db);
+ }
+ // Whenever new state is accessed, add it to the prestate
+ switch (log.op.toString()) {
+ case "EXTCODECOPY": case "EXTCODESIZE": case "BALANCE":
+ this.lookupAccount(toAddress(log.stack.peek(0).toString(16)), db);
+ break;
+ case "CREATE":
+ var from = log.contract.getAddress();
+ this.lookupAccount(toContract(from, db.getNonce(from)), db);
+ break;
+ case "CREATE2":
+ var from = log.contract.getAddress();
+ // stack: salt, size, offset, endowment
+ var offset = log.stack.peek(1).valueOf()
+ var size = log.stack.peek(2).valueOf()
+ var end = offset + size
+ this.lookupAccount(toContract2(from, log.stack.peek(3).toString(16), log.memory.slice(offset, end)), db);
+ break;
+ case "CALL": case "CALLCODE": case "DELEGATECALL": case "STATICCALL":
+ this.lookupAccount(toAddress(log.stack.peek(1).toString(16)), db);
+ break;
+ case 'SSTORE':case 'SLOAD':
+ this.lookupStorage(log.contract.getAddress(), toWord(log.stack.peek(0).toString(16)), db);
+ break;
+ }
+ },
+
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) {}
+}
diff --git a/eth/tracers/internal/tracers/tracers.go b/eth/tracers/internal/tracers/tracers.go
new file mode 100644
index 0000000..2e40975
--- /dev/null
+++ b/eth/tracers/internal/tracers/tracers.go
@@ -0,0 +1,21 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+//go:generate go-bindata -nometadata -o assets.go -pkg tracers -ignore tracers.go -ignore assets.go ./...
+//go:generate gofmt -s -w assets.go
+
+// Package tracers contains the actual JavaScript tracer assets.
+package tracers
diff --git a/eth/tracers/internal/tracers/trigram_tracer.js b/eth/tracers/internal/tracers/trigram_tracer.js
new file mode 100644
index 0000000..8756490
--- /dev/null
+++ b/eth/tracers/internal/tracers/trigram_tracer.js
@@ -0,0 +1,49 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+{
+ // hist is the map of trigram counters
+ hist: {},
+ // lastOp is last operation
+ lastOps: ['',''],
+ lastDepth: 0,
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) {
+ var depth = log.getDepth();
+ if (depth != this.lastDepth){
+ this.lastOps = ['',''];
+ this.lastDepth = depth;
+ return;
+ }
+ var op = log.op.toString();
+ var key = this.lastOps[0]+'-'+this.lastOps[1]+'-'+op;
+ if (this.hist[key]){
+ this.hist[key]++;
+ }
+ else {
+ this.hist[key] = 1;
+ }
+ this.lastOps[0] = this.lastOps[1];
+ this.lastOps[1] = op;
+ },
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) {},
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx) {
+ return this.hist;
+ },
+}
diff --git a/eth/tracers/internal/tracers/unigram_tracer.js b/eth/tracers/internal/tracers/unigram_tracer.js
new file mode 100644
index 0000000..000fb13
--- /dev/null
+++ b/eth/tracers/internal/tracers/unigram_tracer.js
@@ -0,0 +1,43 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+{
+ // hist is the map of opcodes to counters
+ hist: {},
+ // nops counts number of ops
+ nops: 0,
+ // step is invoked for every opcode that the VM executes.
+ step: function(log, db) {
+ var op = log.op.toString();
+ if (this.hist[op]){
+ this.hist[op]++;
+ }
+ else {
+ this.hist[op] = 1;
+ }
+ this.nops++;
+ },
+ // fault is invoked when the actual execution of an opcode fails.
+ fault: function(log, db) {},
+
+ // result is invoked when all the opcodes have been iterated over and returns
+ // the final result of the tracing.
+ result: function(ctx) {
+ if(this.nops > 0){
+ return this.hist;
+ }
+ },
+}
diff --git a/eth/tracers/testdata/call_tracer_create.json b/eth/tracers/testdata/call_tracer_create.json
new file mode 100644
index 0000000..8699bf3
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_create.json
@@ -0,0 +1,58 @@
+{
+ "context": {
+ "difficulty": "3755480783",
+ "gasLimit": "5401723",
+ "miner": "0xd049bfd667cb46aa3ef5df0da3e57db3be39e511",
+ "number": "2294702",
+ "timestamp": "1513676146"
+ },
+ "genesis": {
+ "alloc": {
+ "0x13e4acefe6a6700604929946e70e6443e4e73447": {
+ "balance": "0xcf3e0938579f000",
+ "code": "0x",
+ "nonce": "9",
+ "storage": {}
+ },
+ "0x7dc9c9730689ff0b0fd506c67db815f12d90a448": {
+ "balance": "0x0",
+ "code": "0x",
+ "nonce": "0",
+ "storage": {}
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "3757315409",
+ "extraData": "0x566961425443",
+ "gasLimit": "5406414",
+ "hash": "0xae107f592eebdd9ff8d6ba00363676096e6afb0e1007a7d3d0af88173077378d",
+ "miner": "0xd049bfd667cb46aa3ef5df0da3e57db3be39e511",
+ "mixHash": "0xc927aa05a38bc3de864e95c33b3ae559d3f39c4ccd51cef6f113f9c50ba0caf1",
+ "nonce": "0x93363bbd2c95f410",
+ "number": "2294701",
+ "stateRoot": "0x6b6737d5bde8058990483e915866bd1578014baeff57bd5e4ed228a2bfad635c",
+ "timestamp": "1513676127",
+ "totalDifficulty": "7160808139332585"
+ },
+ "input": "0xf907ef098504e3b29200830897be8080b9079c606060405260405160208061077c83398101604052808051906020019091905050600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415151561007d57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600460006101000a81548160ff02191690831515021790555050610653806101296000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029000000000000000000000000c65e620a3a55451316168d57e268f5702ef56a1129a01060f46676a5dff6f407f0f51eb6f37f5c8c54e238c70221e18e65fc29d3ea65a0557b01c50ff4ffaac8ed6e5d31237a4ecbac843ab1bfe8bb0165a0060df7c54f",
+ "result": {
+ "from": "0x13e4acefe6a6700604929946e70e6443e4e73447",
+ "gas": "0x5e106",
+ "gasUsed": "0x5e106",
+ "input": "0x606060405260405160208061077c83398101604052808051906020019091905050600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415151561007d57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600460006101000a81548160ff02191690831515021790555050610653806101296000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029000000000000000000000000c65e620a3a55451316168d57e268f5702ef56a11",
+ "output": "0x606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029",
+ "to": "0x7dc9c9730689ff0b0fd506c67db815f12d90a448",
+ "type": "CREATE",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_deep_calls.json b/eth/tracers/testdata/call_tracer_deep_calls.json
new file mode 100644
index 0000000..0353d4c
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_deep_calls.json
@@ -0,0 +1,415 @@
+{
+ "context": {
+ "difficulty": "117066904",
+ "gasLimit": "4712384",
+ "miner": "0x1977c248e1014cc103929dd7f154199c916e39ec",
+ "number": "25001",
+ "timestamp": "1479891545"
+ },
+ "genesis": {
+ "alloc": {
+ "0x2a98c5f40bfa3dee83431103c535f6fae9a8ad38": {
+ "balance": "0x0",
+ "code": "0x606060405236156100825760e060020a600035046302d05d3f811461008a5780630accce061461009c5780631ab9075a146100c757806331ed274614610102578063645a3b7214610133578063772fdae314610155578063a7f4377914610180578063ae5f80801461019e578063c9bded21146101ea578063f905c15a14610231575b61023a610002565b61023c600054600160a060020a031681565b61023a600435602435604435606435608435600254600160a060020a03166000141561024657610002565b61023a600435600254600160a060020a03166000148015906100f8575060025433600160a060020a03908116911614155b156102f457610002565b61023a60043560243560443560643560843560a43560c435600254600160a060020a03166000141561031657610002565b61023a600435602435600254600160a060020a0316600014156103d057610002565b61023a600435602435604435606435608435600254600160a060020a03166000141561046157610002565b61023a60025433600160a060020a0390811691161461051657610002565b61023a6004356024356044356060828152600160a060020a0382169060ff8516907fa6c2f0913db6f79ff0a4365762c61718973b3413d6e40382e704782a9a5099f690602090a3505050565b61023a600435602435600160a060020a038116606090815260ff8316907fee6348a7ec70f74e3d6cba55a53e9f9110d180d7698e9117fc466ae29a43e34790602090a25050565b61023c60035481565b005b6060908152602090f35b60025460e060020a6313bc6d4b02606090815233600160a060020a0390811660645291909116906313bc6d4b906084906020906024816000876161da5a03f115610002575050604051511515905061029d57610002565b60408051858152602081018390528151600160a060020a03858116939087169260ff8a16927f5a690ecd0cb15c1c1fd6b6f8a32df0d4f56cb41a54fea7e94020f013595de796929181900390910190a45050505050565b6002805473ffffffffffffffffffffffffffffffffffffffff19168217905550565b60025460e060020a6313bc6d4b02606090815233600160a060020a0390811660645291909116906313bc6d4b906084906020906024816000876161da5a03f115610002575050604051511515905061036d57610002565b6040805186815260208101869052808201859052606081018490529051600160a060020a03831691889160ff8b16917fd65d9ddafbad8824e2bbd6f56cc9f4ac27ba60737035c10a321ea2f681c94d47919081900360800190a450505050505050565b60025460e060020a6313bc6d4b02606090815233600160a060020a0390811660645291909116906313bc6d4b906084906020906024816000876161da5a03f115610002575050604051511515905061042757610002565b60408051828152905183917fa9c6cbc4bd352a6940479f6d802a1001550581858b310d7f68f7bea51218cda6919081900360200190a25050565b60025460e060020a6313bc6d4b02606090815233600160a060020a0390811660645291909116906313bc6d4b906084906020906024816000876161da5a03f11561000257505060405151151590506104b857610002565b80600160a060020a031684600160a060020a03168660ff167f69bdaf789251e1d3a0151259c0c715315496a7404bce9fd0b714674685c2cab78686604051808381526020018281526020019250505060405180910390a45050505050565b600254600160a060020a0316ff",
+ "nonce": "1",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000002cccf5e0538493c235d1c5ef6580f77d99e91396"
+ }
+ },
+ "0x2cccf5e0538493c235d1c5ef6580f77d99e91396": {
+ "balance": "0x0",
+ "code": "0x606060405236156100775760e060020a600035046302d05d3f811461007f57806313bc6d4b146100915780633688a877146100b95780635188f9961461012f5780637eadc976146101545780638ad79680146101d3578063a43e04d814610238578063a7f437791461025e578063e16c7d981461027c575b61029f610002565b6102a1600054600160a060020a031681565b6102be600435600160a060020a03811660009081526002602052604090205460ff165b919050565b6102d26004356040805160208181018352600080835284815260038252835190849020805460026001821615610100026000190190911604601f8101849004840283018401909552848252929390929183018282801561037d5780601f106103525761010080835404028352916020019161037d565b61029f6004356024356000805433600160a060020a039081169116146104a957610002565b61034060043560008181526001602090815260408083205481517ff905c15a0000000000000000000000000000000000000000000000000000000081529151600160a060020a03909116928392839263f905c15a92600483810193919291829003018189876161da5a03f1156100025750506040515195945050505050565b60408051602060248035600481810135601f810185900485028601850190965285855261029f9581359591946044949293909201918190840183828082843750949650505050505050600054600160a060020a0390811633909116146104f657610002565b61029f6004355b600080548190600160a060020a0390811633909116146105a457610002565b61029f60005433600160a060020a0390811691161461072957610002565b6102a1600435600081815260016020526040902054600160a060020a03166100b4565b005b60408051600160a060020a03929092168252519081900360200190f35b604080519115158252519081900360200190f35b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156103325780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60408051918252519081900360200190f35b820191906000526020600020905b81548152906001019060200180831161036057829003601f168201915b505050505090506100b4565b506000828152600160208181526040808420805473ffffffffffffffffffffffffffffffffffffffff191686179055600160a060020a038581168086526002909352818520805460ff191690941790935580517f1ab9075a0000000000000000000000000000000000000000000000000000000081523090931660048401525184939192631ab9075a926024828101939192829003018183876161da5a03f11561000257505060408051602081018690528082019290925243606083015260808083526003908301527f414444000000000000000000000000000000000000000000000000000000000060a0830152517f8ac68d4e97d65912f220b4c5f87978b8186320a5e378c1369850b5b5f90323d39181900360c00190a15b505050565b600083815260016020526040902054600160a060020a03838116911614156104d0576104a4565b600083815260016020526040812054600160a060020a031614610389576103898361023f565b600082815260036020908152604082208054845182855293839020919360026001831615610100026000190190921691909104601f90810184900483019391929186019083901061056a57805160ff19168380011785555b5061059a9291505b808211156105a05760008155600101610556565b8280016001018555821561054e579182015b8281111561054e57825182600050559160200191906001019061057c565b50505050565b5090565b600083815260016020526040812054600160a060020a031614156105c757610002565b50506000818152600160205260408082205481517fa7f437790000000000000000000000000000000000000000000000000000000081529151600160a060020a0391909116928392839263a7f4377992600483810193919291829003018183876161da5a03f11561000257505050600160005060008460001916815260200190815260200160002060006101000a815490600160a060020a0302191690556002600050600083600160a060020a0316815260200190815260200160002060006101000a81549060ff02191690557f8ac68d4e97d65912f220b4c5f87978b8186320a5e378c1369850b5b5f90323d383834360405180806020018560001916815260200184600160a060020a03168152602001838152602001828103825260038152602001807f44454c000000000000000000000000000000000000000000000000000000000081526020015060200194505050505060405180910390a1505050565b600054600160a060020a0316ff",
+ "nonce": "1",
+ "storage": {
+ "0x0684ac65a9fa32414dda56996f4183597d695987fdb82b145d722743891a6fe8": "0x0000000000000000000000003e9286eafa2db8101246c2131c09b49080d00690",
+ "0x1cd76f78169a420d99346e3501dd3e541622c38a226f9b63e01cfebc69879dc7": "0x000000000000000000000000b4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "0x8e54a4494fe5da016bfc01363f4f6cdc91013bb5434bd2a4a3359f13a23afa2f": "0x000000000000000000000000cf00ffd997ad14939736f026006498e3f099baaf",
+ "0x94edf7f600ba56655fd65fca1f1424334ce369326c1dc3e53151dcd1ad06bc13": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "0xbbee47108b275f55f98482c6800f6372165e88b0330d3f5dae6419df4734366c": "0x0000000000000000000000002a98c5f40bfa3dee83431103c535f6fae9a8ad38",
+ "0xd38c0c4e84de118cfdcc775130155d83b8bbaaf23dc7f3c83a626b10473213bd": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "0xfb3aa5c655c2ec9d40609401f88d505d1da61afaa550e36ef5da0509ada257ba": "0x0000000000000000000000007986bad81f4cbd9317f5a46861437dae58d69113"
+ }
+ },
+ "0x3e9286eafa2db8101246c2131c09b49080d00690": {
+ "balance": "0x0",
+ "code": "0x606060405236156100cf5760e060020a600035046302d05d3f81146100d7578063056d4470146100e957806316c66cc61461010c5780631ab9075a146101935780633ae1005c146101ce57806358541662146101fe5780635ed61af014610231578063644e3b791461025457806384dbac3b146102db578063949ae479146102fd5780639859387b14610321578063a7f4377914610340578063ab03fc261461035e578063e8161b7814610385578063e964d4e114610395578063f905c15a146103a5578063f92eb774146103ae575b6103be610002565b6103c0600054600160a060020a031681565b6103be6004356002546000908190600160a060020a031681141561040357610002565b6103dd60043560006108365b6040805160025460e360020a631c2d8fb30282527f636f6e747261637464620000000000000000000000000000000000000000000060048301529151600092600160a060020a03169163e16c7d98916024828101926020929190829003018187876161da5a03f1156100025750506040515191506104e29050565b6103be600435600254600160a060020a03166000148015906101c4575060025433600160a060020a03908116911614155b1561088d57610002565b6103be600435602435604435606435600254600090819081908190600160a060020a03168114156108af57610002565b6103c0600435602435604435606435608435600254600090819081908190600160a060020a03168114156110e857610002565b6103be6004356002546000908190600160a060020a03168114156115ec57610002565b6103c06004356000611b635b6040805160025460e360020a631c2d8fb30282527f6d61726b6574646200000000000000000000000000000000000000000000000060048301529151600092600160a060020a03169163e16c7d98916024828101926020929190829003018187876161da5a03f1156100025750506040515191506104e29050565b6103be600435602435600254600160a060020a031660001415611bb557610002565b6103be600435602435600254600090600160a060020a0316811415611d2e57610002565b6103be600435600254600160a060020a031660001415611fc657610002565b6103be60025433600160a060020a0390811691161461207e57610002565b6103be600435602435604435600254600090600160a060020a031681141561208c57610002565b6103dd60043560006124b8610260565b6103c0600435600061250a610118565b6103f160035481565b6103f16004356000612561610260565b005b60408051600160a060020a03929092168252519081900360200190f35b604080519115158252519081900360200190f35b60408051918252519081900360200190f35b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061046557610002565b8291506104e55b6040805160025460e360020a631c2d8fb30282527f63706f6f6c00000000000000000000000000000000000000000000000000000060048301529151600092600160a060020a03169163e16c7d98916024828101926020929190829003018187876161da5a03f115610002575050604051519150505b90565b600160a060020a031663b2206e6d83600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f1156100025750506040805180517fb2206e6d0000000000000000000000000000000000000000000000000000000082526004820152600160a060020a038816602482015290516044808301935060209282900301816000876161da5a03f11561000257505060405151915061059b90506106ba565b600160a060020a031663d5b205ce83600160a060020a03166336da44686040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e160020a636ad902e7028252600160a060020a0390811660048301526024820187905288166044820152905160648281019350600092829003018183876161da5a03f115610002575050506107355b6040805160025460e360020a631c2d8fb30282527f6c6f676d6772000000000000000000000000000000000000000000000000000060048301529151600092600160a060020a03169163e16c7d98916024828101926020929190829003018187876161da5a03f1156100025750506040515191506104e29050565b50826120ee5b6040805160025460e360020a631c2d8fb30282527f6163636f756e7463746c0000000000000000000000000000000000000000000060048301529151600092600160a060020a03169163e16c7d98916024828101926020929190829003018187876161da5a03f1156100025750506040515191506104e29050565b600160a060020a0316630accce06600684600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e360020a6306db488d02825291519192899290916336da446891600482810192602092919082900301816000876161da5a03f1156100025750505060405180519060200150866040518660e060020a028152600401808681526020018560001916815260200184600160a060020a0316815260200183600160a060020a03168152602001828152602001955050505050506000604051808303816000876161da5a03f11561000257505050505050565b600160a060020a03166316c66cc6836040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f115610002575050604051519150505b919050565b6002805473ffffffffffffffffffffffffffffffffffffffff19168217905550565b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061091157610002565b87935061091c610260565b600160a060020a031663bdbdb08685600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f1156100025750506040805180517fbdbdb0860000000000000000000000000000000000000000000000000000000082526004820152602481018a905290516044808301935060209282900301816000876161da5a03f1156100025750506040515193506109ca90506106ba565b600160a060020a03166381982a7a8885876040518460e060020a0281526004018084600160a060020a0316815260200183815260200182600160a060020a0316815260200193505050506000604051808303816000876161da5a03f11561000257505050610a3661046c565b600160a060020a03166308636bdb85600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f1156100025750506040805180517f08636bdb000000000000000000000000000000000000000000000000000000008252600482015260248101889052604481019290925251606482810192602092919082900301816000876161da5a03f11561000257505060408051805160e160020a630a5d50db028252600482018190529151919450600160a060020a03871692506314baa1b6916024828101926000929190829003018183876161da5a03f11561000257505050610b3561046c565b600160a060020a0316630a3b6ede85600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e160020a63051db76f0282526004820152600160a060020a038d16602482015290516044808301935060209282900301816000876161da5a03f115610002575050604051519150610bd590506106ba565b600160a060020a031663d5b205ce87838b6040518460e060020a0281526004018084600160a060020a0316815260200183815260200182600160a060020a0316815260200193505050506000604051808303816000876161da5a03f11561000257505050610c41610118565b600160a060020a031663988db79c888a6040518360e060020a0281526004018083600160a060020a0316815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f11561000257505050610ca5610260565b600160a060020a031663f4f2821b896040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f11561000257505050610d6f5b6040805160025460e360020a631c2d8fb30282527f747261646564620000000000000000000000000000000000000000000000000060048301529151600092600160a060020a03169163e16c7d98916024828101926020929190829003018187876161da5a03f1156100025750506040515191506104e29050565b600160a060020a0316635f539d69896040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f11561000257505050610dc2610639565b600160a060020a0316630accce06600386600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e360020a6315b1ea01028252915191928e928e9263ad8f500891600482810192602092919082900301816000876161da5a03f11561000257505050604051805190602001506040518660e060020a028152600401808681526020018560001916815260200184600160a060020a0316815260200183600160a060020a03168152602001828152602001955050505050506000604051808303816000876161da5a03f11561000257505050610ec5610639565b600160a060020a0316630accce06600386600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e360020a6315b1ea01028252915191928e928d9263ad8f500891600482810192602092919082900301816000876161da5a03f11561000257505050604051805190602001506040518660e060020a028152600401808681526020018560001916815260200184600160a060020a0316815260200183600160a060020a03168152602001828152602001955050505050506000604051808303816000876161da5a03f11561000257505050610fc8610639565b600160a060020a031663645a3b7285600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060405151905061101e610260565b600160a060020a031663f92eb77488600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e260020a633e4baddd028252600482015290516024828101935060209282900301816000876161da5a03f11561000257505060408051805160e060020a86028252600482019490945260248101939093525160448381019360009350829003018183876161da5a03f115610002575050505050505050505050565b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061114a57610002565b604051600254600160a060020a0316908a908a908a908a908a90611579806125b38339018087600160a060020a0316815260200186600160a060020a03168152602001856000191681526020018481526020018381526020018281526020019650505050505050604051809103906000f092506111c5610118565b600160a060020a031663b9858a288a856040518360e060020a0281526004018083600160a060020a0316815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f11561000257505050611229610260565b600160a060020a0316635188f99689856040518360e060020a028152600401808360001916815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f11561000257505050611288610260565b600160a060020a031663bdbdb08689896040518360e060020a0281526004018083600019168152602001828152602001925050506020604051808303816000876161da5a03f1156100025750506040515192506112e590506106ba565b600160a060020a03166346d88e7d8a858a6040518460e060020a0281526004018084600160a060020a0316815260200183600160a060020a0316815260200182815260200193505050506000604051808303816000876161da5a03f115610002575050506113516106ba565b600160a060020a03166381982a7a8a84866040518460e060020a0281526004018084600160a060020a0316815260200183815260200182600160a060020a0316815260200193505050506000604051808303816000876161da5a03f115610002575050506113bd61046c565b600160a060020a0316632b58469689856040518360e060020a028152600401808360001916815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f1156100025750505061141c61046c565b600160a060020a03166308636bdb8984866040518460e060020a028152600401808460001916815260200183815260200182600160a060020a0316815260200193505050506020604051808303816000876161da5a03f11561000257505060408051805160e160020a630a5d50db028252600482018190529151919350600160a060020a03861692506314baa1b6916024828101926000929190829003018183876161da5a03f115610002575050506114d3610639565b6040805160e160020a630566670302815260016004820152602481018b9052600160a060020a0386811660448301528c811660648301526000608483018190529251931692630accce069260a480840193919291829003018183876161da5a03f11561000257505050611544610639565b600160a060020a031663645a3b728961155b610260565b600160a060020a031663f92eb7748c6040518260e060020a02815260040180826000191681526020019150506020604051808303816000876161da5a03f11561000257505060408051805160e060020a86028252600482019490945260248101939093525160448084019360009350829003018183876161da5a03f1156100025750939a9950505050505050505050565b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061164e57610002565b82915061165961046c565b600160a060020a0316630a3b6ede83600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e160020a63051db76f0282526004820152600160a060020a038816602482015290516044808301935060209282900301816000876161da5a03f1156100025750506040515191506116f990506106ba565b600160a060020a031663d5b205ce83600160a060020a03166336da44686040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e160020a636ad902e7028252600160a060020a0390811660048301526024820187905288166044820152905160648281019350600092829003018183876161da5a03f1156100025750505061179b6106ba565b600160a060020a031663d653078983600160a060020a03166336da44686040518160e060020a0281526004018090506020604051808303816000876161da5a03f1156100025750506040805180517ff1ff78a0000000000000000000000000000000000000000000000000000000008252915191929163f1ff78a09160048181019260209290919082900301816000876161da5a03f1156100025750505060405180519060200150866040518460e060020a0281526004018084600160a060020a0316815260200183815260200182600160a060020a0316815260200193505050506000604051808303816000876161da5a03f1156100025750505061189f610260565b600160a060020a031663f4f2821b846040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f115610002575050506118f2610118565b600160a060020a031663f4f2821b846040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f11561000257505050611945610639565b600160a060020a0316630accce06600484600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e360020a6306db488d02825291519192899290916336da44689181870191602091908190038801816000876161da5a03f115610002575050506040518051906020015060006040518660e060020a028152600401808681526020018560001916815260200184600160a060020a0316815260200183600160a060020a03168152602001828152602001955050505050506000604051808303816000876161da5a03f11561000257505050611a48610639565b600160a060020a031663645a3b7283600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f115610002575050604051519050611a9e610260565b600160a060020a031663f92eb77486600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e260020a633e4baddd028252600482015290516024828101935060209282900301816000876161da5a03f11561000257505060408051805160e060020a86028252600482019490945260248101939093525160448381019360009350829003018183876161da5a03f11561000257505050505050565b600160a060020a03166381738c59836040518260e060020a02815260040180826000191681526020019150506020604051808303816000876161da5a03f1156100025750506040515191506108889050565b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f1156100025750506040515115159050611c1757610002565b611c1f610260565b600160a060020a03166338a699a4836040518260e060020a02815260040180826000191681526020019150506020604051808303816000876161da5a03f11561000257505060405151159050611c7457610002565b611c7c610260565b600160a060020a0316632243118a836040518260e060020a02815260040180826000191681526020019150506000604051808303816000876161da5a03f11561000257505050611cca610639565b600160a060020a031663ae5f8080600184846040518460e060020a028152600401808481526020018360001916815260200182600160a060020a0316815260200193505050506000604051808303816000876161da5a03f115610002575050505050565b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f1156100025750506040515115159050611d9057610002565b5081611d9a610260565b600160a060020a031663581d5d6084846040518360e060020a0281526004018083600160a060020a03168152602001828152602001925050506000604051808303816000876161da5a03f11561000257505050611df5610639565b600160a060020a0316630accce06600283600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e160020a630566670302825260048201949094526024810193909352600160a060020a038816604484015260006064840181905260848401819052905160a4808501949293509091829003018183876161da5a03f11561000257505050611eab610639565b600160a060020a031663645a3b7282600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f115610002575050604051519050611f01610260565b600160a060020a031663f92eb77485600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e260020a633e4baddd028252600482015290516024828101935060209282900301816000876161da5a03f11561000257505060408051805160e060020a86028252600482019490945260248101939093525160448381019360009350829003018183876161da5a03f11561000257505050505050565b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061202857610002565b612030610118565b600160a060020a0316639859387b826040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f1156100025750505050565b600254600160a060020a0316ff5b6040805160025460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f11561000257505060405151151590506106b457610002565b600160a060020a031663d65307898383600160a060020a031663f1ff78a06040518160e060020a0281526004018090506020604051808303816000876161da5a03f1156100025750506040805180517fd6530789000000000000000000000000000000000000000000000000000000008252600160a060020a039485166004830152602482015292891660448401525160648381019360009350829003018183876161da5a03f115610002575050506121a5610118565b600160a060020a031663f4f2821b856040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f115610002575050506121f8610cf4565b600160a060020a031663f4f2821b856040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f1156100025750505061224b610639565b600160a060020a0316630accce06600583600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e360020a6306db488d028252915191928a9290916336da446891600482810192602092919082900301816000876161da5a03f1156100025750505060405180519060200150886040518660e060020a028152600401808681526020018560001916815260200184600160a060020a0316815260200183600160a060020a03168152602001828152602001955050505050506000604051808303816000876161da5a03f1156100025750505080600160a060020a031663ea71b02d6040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060405151600160a060020a031660001490506124b25761239f610639565b600160a060020a0316630accce06600583600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f1156100025750506040805180517fea71b02d000000000000000000000000000000000000000000000000000000008252915191928a92909163ea71b02d91600482810192602092919082900301816000876161da5a03f1156100025750505060405180519060200150886040518660e060020a028152600401808681526020018560001916815260200184600160a060020a0316815260200183600160a060020a03168152602001828152602001955050505050506000604051808303816000876161da5a03f115610002575050505b50505050565b600160a060020a03166338a699a4836040518260e060020a02815260040180826000191681526020019150506020604051808303816000876161da5a03f1156100025750506040515191506108889050565b600160a060020a031663213fe2b7836040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515191506108889050565b600160a060020a031663f92eb774836040518260e060020a02815260040180826000191681526020019150506020604051808303816000876161da5a03f115610002575050604051519150610888905056606060405260405160c08061157983396101206040819052825160805160a051935160e0516101005160008054600160a060020a03199081163317909155600180546005805484168817905560048a90556006869055600b8590556008849055909116861760a060020a60ff02191690554360038190556002558686526101408390526101608190529396929594919390929091600160a060020a033016917f76885d242fb71c6f74a7e717416e42eff4d96faf54f6de75c6a0a6bbd8890c6b91a230600160a060020a03167fa609f6bd4ad0b4f419ddad4ac9f0d02c2b9295c5e6891469055cf73c2b568fff600b600050546040518082815260200191505060405180910390a250505050505061145e8061011b6000396000f3606060405236156101745760e060020a600035046302d05d3f811461017c57806304a7fdbc1461018e5780630e90f957146101fb5780630fb5a6b41461021257806314baa1b61461021b57806317fc45e21461023a5780632b096926146102435780632e94420f1461025b578063325a19f11461026457806336da44681461026d5780633f81a2c01461027f5780633fc306821461029757806345ecd3d7146102d45780634665096d146102dd5780634e71d92d146102e657806351a34eb8146103085780636111bb951461032d5780636f265b93146103445780637e9014e11461034d57806390ba009114610360578063927df5e014610393578063a7f437791461046c578063ad8f50081461046e578063bc6d909414610477578063bdec3ad114610557578063c19d93fb1461059a578063c9503fe2146105ad578063e0a73a93146105b6578063ea71b02d146105bf578063ea8a1af0146105d1578063ee4a96f9146105f3578063f1ff78a01461065c575b61046c610002565b610665600054600160a060020a031681565b6040805160c081810190925261046c9160049160c4918390600690839083908082843760408051808301909152929750909561018495509193509091908390839080828437509095505050505050600554600090600160a060020a0390811633909116146106a857610002565b61068260015460a060020a900460ff166000145b90565b61069660085481565b61046c600435600154600160a060020a03166000141561072157610002565b610696600d5481565b610696600435600f8160068110156100025750015481565b61069660045481565b61069660035481565b610665600554600160a060020a031681565b61069660043560158160068110156100025750015481565b6106966004355b600b54600f5460009160028202808203928083039290810191018386101561078357601054840186900394505b50505050919050565b61069660025481565b61069660095481565b61046c600554600090600160a060020a03908116339091161461085857610002565b61046c600435600554600090600160a060020a03908116339091161461092e57610002565b6106826001805460a060020a900460ff161461020f565b610696600b5481565b61068260075460a060020a900460ff1681565b6106966004355b600b54601554600091600282028082039280830392908101910183861015610a6c5760165494506102cb565b61046c6004356024356044356040805160015460e360020a631c2d8fb302825260b260020a691858d8dbdd5b9d18dd1b02600483015291516000928392600160a060020a03919091169163e16c7d9891602481810192602092909190829003018187876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663c4b0c96a336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610b4657610002565b005b610696600a5481565b61046c60006000600060006000600160009054906101000a9004600160a060020a0316600160a060020a031663e16c7d986040518160e060020a028152600401808060b260020a691858d8dbdd5b9d18dd1b0281526020015060200190506020604051808303816000876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663c4b0c96a336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610f1757610002565b61046c5b60015b60058160ff16101561071e57600f6001820160ff166006811015610002578101549060ff83166006811015610002570154101561129057610002565b61069660015460a060020a900460ff1681565b61069660065481565b610696600c5481565b610665600754600160a060020a031681565b61046c600554600090600160a060020a0390811633909116146112c857610002565b6040805160c081810190925261046c9160049160c4918390600690839083908082843760408051808301909152929750909561018495509193509091908390839080828437509095505050505050600154600090600160a060020a03168114156113fb57610002565b610696600e5481565b60408051600160a060020a03929092168252519081900360200190f35b604080519115158252519081900360200190f35b60408051918252519081900360200190f35b5060005b60068160ff16101561070857828160ff166006811015610002576020020151600f60ff831660068110156100025701558160ff82166006811015610002576020020151601560ff831660068110156100025701556001016106ac565b61071061055b565b505050565b600e8054820190555b50565b6040805160015460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061071557610002565b83861015801561079257508286105b156107b457600f546010546011548689039082030291909104900394506102cb565b8286101580156107c55750600b5486105b156107e757600f546011546012548589039082030291909104900394506102cb565b600b5486108015906107f857508186105b1561081d57600b54600f546012546013549289039281039290920204900394506102cb565b81861015801561082c57508086105b1561084e57600f546013546014548489039082030291909104900394506102cb565b60145494506102cb565b60015460a060020a900460ff1660001461087157610002565b600254600a01431161088257610002565b6040805160015460e360020a631c2d8fb302825260a860020a6a636f6e74726163746170690260048301529151600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663771d50e16040518160e060020a0281526004018090506000604051808303816000876161da5a03f1156100025750505050565b60015460a060020a900460ff1660001461094757610002565b600254600a01431161095857610002565b6040805160015460e360020a631c2d8fb302825260a860020a6a636f6e74726163746170690260048301529151600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180517f51a34eb8000000000000000000000000000000000000000000000000000000008252600482018690529151919350600160a060020a03841692506351a34eb8916024808301926000929190829003018183876161da5a03f11561000257505050600b8290554360025560408051838152905130600160a060020a0316917fa609f6bd4ad0b4f419ddad4ac9f0d02c2b9295c5e6891469055cf73c2b568fff919081900360200190a25050565b838610158015610a7b57508286105b15610a9d576015546016546017548689039082900302919091040194506102cb565b828610158015610aae5750600b5486105b15610ad0576015546017546018548589039082900302919091040194506102cb565b600b548610801590610ae157508186105b15610b0657600b546015546018546019549289039281900392909202040194506102cb565b818610158015610b1557508086105b15610b3757601554601954601a548489039082900302919091040194506102cb565b601a54860181900394506102cb565b60015460a060020a900460ff16600014610b5f57610002565b6001805460a060020a60ff02191660a060020a17908190556040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180516004805460e260020a633e4baddd028452908301529151919450600160a060020a038516925063f92eb77491602482810192602092919082900301816000876161da5a03f115610002575050604080518051600a556005547ffebf661200000000000000000000000000000000000000000000000000000000825233600160a060020a03908116600484015216602482015260448101879052905163febf661291606480820192600092909190829003018183876161da5a03f115610002575050508215610cc7576007805473ffffffffffffffffffffffffffffffffffffffff191633179055610dbb565b6040805160055460065460e060020a63599efa6b028352600160a060020a039182166004840152602483015291519184169163599efa6b91604481810192600092909190829003018183876161da5a03f115610002575050604080516006547f56ccb6f000000000000000000000000000000000000000000000000000000000825233600160a060020a03166004830152602482015290516356ccb6f091604480820192600092909190829003018183876161da5a03f115610002575050600580546007805473ffffffffffffffffffffffffffffffffffffffff19908116600160a060020a038416179091551633179055505b6007805460a060020a60ff02191660a060020a87810291909117918290556008544301600955900460ff1615610df757600a54610e039061029e565b600a54610e0b90610367565b600c55610e0f565b600c555b600c54670de0b6b3a7640000850204600d55600754600554604080517f759297bb000000000000000000000000000000000000000000000000000000008152600160a060020a039384166004820152918316602483015260448201879052519184169163759297bb91606481810192600092909190829003018183876161da5a03f11561000257505060408051600754600a54600d54600554600c5460a060020a850460ff161515865260208601929092528486019290925260608401529251600160a060020a0391821694509281169230909116917f3b3d1986083d191be01d28623dc19604728e29ae28bdb9ba52757fdee1a18de2919081900360800190a45050505050565b600954431015610f2657610002565b6001805460a060020a900460ff1614610f3e57610002565b6001805460a060020a60ff0219167402000000000000000000000000000000000000000017908190556040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180516004805460e260020a633e4baddd028452908301529151919750600160a060020a038816925063f92eb77491602482810192602092919082900301816000876161da5a03f115610002575050604051516007549095506000945060a060020a900460ff1615905061105c57600a5484111561105757600a54600d54670de0b6b3a7640000918603020492505b61107e565b600a5484101561107e57600a54600d54670de0b6b3a764000091869003020492505b60065483111561108e5760065492505b6006548390039150600083111561111857604080516005546007547f5928d37f000000000000000000000000000000000000000000000000000000008352600160a060020a0391821660048401528116602483015260448201869052915191871691635928d37f91606481810192600092909190829003018183876161da5a03f115610002575050505b600082111561117a576040805160055460e060020a63599efa6b028252600160a060020a0390811660048301526024820185905291519187169163599efa6b91604481810192600092909190829003018183876161da5a03f115610002575050505b6040805185815260208101849052808201859052905130600160a060020a0316917f89e690b1d5aaae14f3e85f108dc92d9ab3763a58d45aed8b59daedbbae8fe794919081900360600190a260008311156112285784600160a060020a0316634cc927d785336040518360e060020a0281526004018083815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f11561000257505050611282565b84600160a060020a0316634cc927d7600a60005054336040518360e060020a0281526004018083815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f115610002575050505b600054600160a060020a0316ff5b60156001820160ff166006811015610002578101549060ff8316600681101561000257015411156112c057610002565b60010161055e565b60015460a060020a900460ff166000146112e157610002565b600254600a0143116112f257610002565b6001546040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f11561000257505060408051805160055460065460e060020a63599efa6b028452600160a060020a03918216600485015260248401529251909450918416925063599efa6b916044808301926000929190829003018183876161da5a03f1156100025750505080600160a060020a0316632b68bb2d6040518160e060020a0281526004018090506000604051808303816000876161da5a03f115610002575050600054600160a060020a03169050ff5b6001546040805160e060020a6313bc6d4b02815233600160a060020a039081166004830152915191909216916313bc6d4b91602480830192602092919082900301816000876161da5a03f11561000257505060405151151590506106a85761000256",
+ "nonce": "16",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000002cccf5e0538493c235d1c5ef6580f77d99e91396"
+ }
+ },
+ "0x70c9217d814985faef62b124420f8dfbddd96433": {
+ "balance": "0x4ef436dcbda6cd4a",
+ "code": "0x",
+ "nonce": "1634",
+ "storage": {}
+ },
+ "0x7986bad81f4cbd9317f5a46861437dae58d69113": {
+ "balance": "0x0",
+ "code": "0x6060604052361561008d5760e060020a600035046302d05d3f811461009557806316c66cc6146100a75780631ab9075a146100d7578063213fe2b7146101125780639859387b1461013f578063988db79c1461015e578063a7f4377914610180578063b9858a281461019e578063c8e40fbf146101c0578063f4f2821b146101e8578063f905c15a14610209575b610212610002565b610214600054600160a060020a031681565b600160a060020a0360043581811660009081526005602052604081205461023193168114610257575060016101e3565b610212600435600254600160a060020a0316600014801590610108575060025433600160a060020a03908116911614155b1561025f57610002565b610214600435600160a060020a03811660009081526004602052604081205460ff16151561027557610002565b610212600435600254600160a060020a03166000141561029b57610002565b610212600435602435600254600160a060020a03166000141561050457610002565b61021260025433600160a060020a0390811691161461056757610002565b610212600435602435600254600160a060020a03166000141561057557610002565b610231600435600160a060020a03811660009081526004602052604090205460ff165b919050565b610212600435600254600090600160a060020a031681141561072057610002565b61024560035481565b005b60408051600160a060020a03929092168252519081900360200190f35b604080519115158252519081900360200190f35b60408051918252519081900360200190f35b5060006101e3565b60028054600160a060020a031916821790555b50565b50600160a060020a038181166000908152600460205260409020546101009004166101e3565b6002546040805160e060020a6313bc6d4b02815233600160a060020a039081166004830152915191909216916313bc6d4b91602482810192602092919082900301816000876161da5a03f11561000257505060405151151590506102fe57610002565b600160a060020a03811660009081526004602052604090205460ff161515610272576040516104028061092e833901809050604051809103906000f06004600050600083600160a060020a0316815260200190815260200160002060005060000160016101000a815481600160a060020a030219169083021790555060016004600050600083600160a060020a0316815260200190815260200160002060005060000160006101000a81548160ff0219169083021790555050565b600160a060020a03821660009081526004602052604090205460ff1615156104725760405161040280610d30833901809050604051809103906000f06004600050600084600160a060020a0316815260200190815260200160002060005060000160016101000a815481600160a060020a030219169083021790555060016004600050600084600160a060020a0316815260200190815260200160002060005060000160006101000a81548160ff021916908302179055505b600160a060020a03828116600090815260046020819052604080518184205460e060020a630a3b0a4f02825286861693820193909352905161010090920490931692630a3b0a4f926024828101939192829003018183876161da5a03f11561000257505050600160a060020a03811660009081526006602052604090208054600160a060020a031916831790555b5050565b6002546040805160e060020a6313bc6d4b02815233600160a060020a039081166004830152915191909216916313bc6d4b91602482810192602092919082900301816000876161da5a03f11561000257505060405151151590506103b957610002565b600254600160a060020a0316ff5b6002546040805160e060020a6313bc6d4b02815233600160a060020a039081166004830152915191909216916313bc6d4b91602482810192602092919082900301816000876161da5a03f11561000257505060405151151590506105d857610002565b600160a060020a03821660009081526004602052604090205460ff1615156106915760405161040280611132833901809050604051809103906000f06004600050600084600160a060020a0316815260200190815260200160002060005060000160016101000a815481600160a060020a030219169083021790555060016004600050600084600160a060020a0316815260200190815260200160002060005060000160006101000a81548160ff021916908302179055505b600160a060020a03828116600090815260046020819052604080518184205460e060020a630a3b0a4f02825286861693820193909352905161010090920490931692630a3b0a4f926024828101939192829003018183876161da5a03f11561000257505050600160a060020a031660009081526005602052604090208054600160a060020a0319169091179055565b6002546040805160e060020a6313bc6d4b02815233600160a060020a039081166004830152915191909216916313bc6d4b91602482810192602092919082900301816000876161da5a03f115610002575050604051511515905061078357610002565b50600160a060020a0381811660009081526005602090815260408083205490931680835260049091529190205460ff161561080f576040600081812054825160e260020a632e72bafd028152600160a060020a03868116600483015293516101009092049093169263b9caebf4926024828101939192829003018183876161da5a03f115610002575050505b600160a060020a03828116600090815260056020526040812054909116146108545760406000908120600160a060020a0384169091528054600160a060020a03191690555b50600160a060020a0381811660009081526006602090815260408083205490931680835260049091529190205460ff16156108e657600160a060020a038181166000908152604080518183205460e260020a632e72bafd028252868516600483015291516101009092049093169263b9caebf4926024828101939192829003018183876161da5a03f115610002575050505b600160a060020a03828116600090815260066020526040812054909116146105005760406000908120600160a060020a0384169091528054600160a060020a0319169055505056606060405260008054600160a060020a031916331790556103de806100246000396000f3606060405236156100615760e060020a600035046302d05d3f81146100695780630a3b0a4f1461007b5780630d327fa7146100f6578063524d81d314610109578063a7f4377914610114578063b9caebf414610132578063bbec3bae14610296575b6102ce610002565b6102d0600054600160a060020a031681565b6102ce600435600254600090600160a060020a03168114156102ed5760028054600160a060020a03199081168417808355600160a060020a03808616855260036020526040852060018101805493831694909316939093179091559154815461010060a860020a031916921661010002919091179055610372565b6102d0600254600160a060020a03165b90565b6102e3600154610106565b6102ce60005433600160a060020a039081169116146103c657610002565b6102ce600435600160a060020a038116600090815260036020526040812054819060ff16801561016457506001548190115b1561029157506040808220600180820154915461010090819004600160a060020a039081168087528587209093018054600160a060020a031916948216948517905583865293909420805461010060a860020a03191694820294909417909355600254909190811690841614156101e85760028054600160a060020a031916821790555b600254600160a060020a0390811690841614156102105760028054600160a060020a03191690555b6003600050600084600160a060020a0316815260200190815260200160002060006000820160006101000a81549060ff02191690556000820160016101000a815490600160a060020a0302191690556001820160006101000a815490600160a060020a03021916905550506001600081815054809291906001900391905055505b505050565b600160a060020a036004358181166000908152600360205260408120600101546002546102d09491821691168114156103d4576103d8565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60028054600160a060020a03908116835260036020526040808420805461010060a860020a0319808216610100808a029190911790935590829004841680875283872060019081018054600160a060020a03199081168b179091559654868a168952949097209687018054949095169390951692909217909255835416908202179091555b60016003600050600084600160a060020a0316815260200190815260200160002060005060000160006101000a81548160ff0219169083021790555060016000818150548092919060010191905055505050565b600054600160a060020a0316ff5b8091505b5091905056606060405260008054600160a060020a031916331790556103de806100246000396000f3606060405236156100615760e060020a600035046302d05d3f81146100695780630a3b0a4f1461007b5780630d327fa7146100f6578063524d81d314610109578063a7f4377914610114578063b9caebf414610132578063bbec3bae14610296575b6102ce610002565b6102d0600054600160a060020a031681565b6102ce600435600254600090600160a060020a03168114156102ed5760028054600160a060020a03199081168417808355600160a060020a03808616855260036020526040852060018101805493831694909316939093179091559154815461010060a860020a031916921661010002919091179055610372565b6102d0600254600160a060020a03165b90565b6102e3600154610106565b6102ce60005433600160a060020a039081169116146103c657610002565b6102ce600435600160a060020a038116600090815260036020526040812054819060ff16801561016457506001548190115b1561029157506040808220600180820154915461010090819004600160a060020a039081168087528587209093018054600160a060020a031916948216948517905583865293909420805461010060a860020a03191694820294909417909355600254909190811690841614156101e85760028054600160a060020a031916821790555b600254600160a060020a0390811690841614156102105760028054600160a060020a03191690555b6003600050600084600160a060020a0316815260200190815260200160002060006000820160006101000a81549060ff02191690556000820160016101000a815490600160a060020a0302191690556001820160006101000a815490600160a060020a03021916905550506001600081815054809291906001900391905055505b505050565b600160a060020a036004358181166000908152600360205260408120600101546002546102d09491821691168114156103d4576103d8565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60028054600160a060020a03908116835260036020526040808420805461010060a860020a0319808216610100808a029190911790935590829004841680875283872060019081018054600160a060020a03199081168b179091559654868a168952949097209687018054949095169390951692909217909255835416908202179091555b60016003600050600084600160a060020a0316815260200190815260200160002060005060000160006101000a81548160ff0219169083021790555060016000818150548092919060010191905055505050565b600054600160a060020a0316ff5b8091505b5091905056606060405260008054600160a060020a031916331790556103de806100246000396000f3606060405236156100615760e060020a600035046302d05d3f81146100695780630a3b0a4f1461007b5780630d327fa7146100f6578063524d81d314610109578063a7f4377914610114578063b9caebf414610132578063bbec3bae14610296575b6102ce610002565b6102d0600054600160a060020a031681565b6102ce600435600254600090600160a060020a03168114156102ed5760028054600160a060020a03199081168417808355600160a060020a03808616855260036020526040852060018101805493831694909316939093179091559154815461010060a860020a031916921661010002919091179055610372565b6102d0600254600160a060020a03165b90565b6102e3600154610106565b6102ce60005433600160a060020a039081169116146103c657610002565b6102ce600435600160a060020a038116600090815260036020526040812054819060ff16801561016457506001548190115b1561029157506040808220600180820154915461010090819004600160a060020a039081168087528587209093018054600160a060020a031916948216948517905583865293909420805461010060a860020a03191694820294909417909355600254909190811690841614156101e85760028054600160a060020a031916821790555b600254600160a060020a0390811690841614156102105760028054600160a060020a03191690555b6003600050600084600160a060020a0316815260200190815260200160002060006000820160006101000a81549060ff02191690556000820160016101000a815490600160a060020a0302191690556001820160006101000a815490600160a060020a03021916905550506001600081815054809291906001900391905055505b505050565b600160a060020a036004358181166000908152600360205260408120600101546002546102d09491821691168114156103d4576103d8565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60028054600160a060020a03908116835260036020526040808420805461010060a860020a0319808216610100808a029190911790935590829004841680875283872060019081018054600160a060020a03199081168b179091559654868a168952949097209687018054949095169390951692909217909255835416908202179091555b60016003600050600084600160a060020a0316815260200190815260200160002060005060000160006101000a81548160ff0219169083021790555060016000818150548092919060010191905055505050565b600054600160a060020a0316ff5b8091505b5091905056",
+ "nonce": "7",
+ "storage": {
+ "0xffc4df2d4f3d2cffad590bed6296406ab7926ca9e74784f74a95191fa069a174": "0x00000000000000000000000070c9217d814985faef62b124420f8dfbddd96433"
+ }
+ },
+ "0xb4fe7aa695b326c9d219158d2ca50db77b39f99f": {
+ "balance": "0x0",
+ "code": "0x606060405236156100ae5760e060020a600035046302d05d3f81146100b65780631ab9075a146100c85780632b68bb2d146101035780634cc927d7146101c557806351a34eb81461028e57806356ccb6f0146103545780635928d37f1461041d578063599efa6b146104e9578063759297bb146105b2578063771d50e11461067e578063a7f4377914610740578063f905c15a1461075e578063f92eb77414610767578063febf661214610836575b610902610002565b610904600054600160a060020a031681565b610902600435600254600160a060020a03166000148015906100f9575060025433600160a060020a03908116911614155b1561092057610002565b60025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b02606452610902916000918291600160a060020a03169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f115610002575050604051511515905061094257610002565b61090260043560243560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610a0d57610002565b61090260043560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610ae957610002565b61090260043560243560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610bbc57610002565b61090260043560243560443560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610c9657610002565b61090260043560243560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610de057610002565b61090260043560243560443560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610ebb57610002565b60025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b02606452610902916000918291600160a060020a03169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610f9e57610002565b61090260025433600160a060020a0390811691161461106957610002565b61090e60035481565b61090e60043560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750506040805180517ff92eb774000000000000000000000000000000000000000000000000000000008252600482018790529151919350600160a060020a038416925063f92eb774916024828101926020929190829003018188876161da5a03f11561000257505060405151949350505050565b61090260043560243560443560025460e360020a631c2d8fb302606090815260aa60020a6a18dbdb9d1c9858dd18dd1b026064526000918291600160a060020a039091169063e16c7d989060849060209060248187876161da5a03f1156100025750505060405180519060200150905080600160a060020a03166316c66cc6336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f115610002575050604051511515905061107757610002565b005b6060908152602090f35b60408051918252519081900360200190f35b6002805473ffffffffffffffffffffffffffffffffffffffff19168217905550565b6040805160025460e360020a631c2d8fb302825260aa60020a6a18dbdb9d1c9858dd18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517f5ed61af000000000000000000000000000000000000000000000000000000000825233600160a060020a039081166004840152925190959286169350635ed61af092602483810193919291829003018183876161da5a03f115610002575050505050565b6040805160025460e360020a631c2d8fb302825260aa60020a6a18dbdb9d1c9858dd18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517fab03fc2600000000000000000000000000000000000000000000000000000000825233600160a060020a03908116600484015260248301899052808816604484015292519095928616935063ab03fc2692606483810193919291829003018183876161da5a03f1156100025750505050505050565b6040805160025460e360020a631c2d8fb302825260aa60020a6a18dbdb9d1c9858dd18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517f949ae47900000000000000000000000000000000000000000000000000000000825233600160a060020a0390811660048401526024830188905292519095928616935063949ae47992604483810193919291829003018183876161da5a03f11561000257505050505050565b6040805160025460e360020a631c2d8fb302825260b260020a691858d8dbdd5b9d18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517f46d88e7d000000000000000000000000000000000000000000000000000000008252600160a060020a0380891660048401523381166024840152604483018890529251909592861693506346d88e7d92606483810193919291829003018183876161da5a03f1156100025750505050505050565b6040805160025460e360020a631c2d8fb302825260b260020a691858d8dbdd5b9d18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517f5315cdde00000000000000000000000000000000000000000000000000000000825233600160a060020a039081166004840152808a16602484015260448301889052925190959286169350635315cdde92606483810193919291829003018183876161da5a03f115610002575050604080517f5928d37f00000000000000000000000000000000000000000000000000000000815233600160a060020a03908116600483015287166024820152604481018690529051635928d37f91606481810192600092909190829003018183876161da5a03f115610002575050505050505050565b6040805160025460e360020a631c2d8fb302825260b260020a691858d8dbdd5b9d18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517fe68e401c00000000000000000000000000000000000000000000000000000000825233600160a060020a03908116600484015280891660248401526044830188905292519095928616935063e68e401c92606483810193919291829003018183876161da5a03f1156100025750505050505050565b6040805160025460e360020a631c2d8fb302825260b260020a691858d8dbdd5b9d18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517f5152f381000000000000000000000000000000000000000000000000000000008252600160a060020a03808a1660048401528089166024840152604483018890523381166064840152925190959286169350635152f38192608483810193919291829003018183876161da5a03f115610002575050505050505050565b6040805160025460e360020a631c2d8fb302825260aa60020a6a18dbdb9d1c9858dd18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517f056d447000000000000000000000000000000000000000000000000000000000825233600160a060020a03908116600484015292519095928616935063056d447092602483810193919291829003018183876161da5a03f115610002575050505050565b600254600160a060020a0316ff5b6040805160025460e360020a631c2d8fb302825260aa60020a6a18dbdb9d1c9858dd18dd1b0260048301529151600160a060020a03929092169163e16c7d9891602481810192602092909190829003018188876161da5a03f1156100025750506040805180517f3ae1005c00000000000000000000000000000000000000000000000000000000825233600160a060020a039081166004840152808a166024840152808916604484015260648301889052925190959286169350633ae1005c92608483810193919291829003018183876161da5a03f11561000257505050505050505056",
+ "nonce": "1",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000002cccf5e0538493c235d1c5ef6580f77d99e91396"
+ }
+ },
+ "0xc212e03b9e060e36facad5fd8f4435412ca22e6b": {
+ "balance": "0x0",
+ "code": "0x606060405236156101745760e060020a600035046302d05d3f811461017c57806304a7fdbc1461018e5780630e90f957146101fb5780630fb5a6b41461021257806314baa1b61461021b57806317fc45e21461023a5780632b096926146102435780632e94420f1461025b578063325a19f11461026457806336da44681461026d5780633f81a2c01461027f5780633fc306821461029757806345ecd3d7146102d45780634665096d146102dd5780634e71d92d146102e657806351a34eb8146103085780636111bb951461032d5780636f265b93146103445780637e9014e11461034d57806390ba009114610360578063927df5e014610393578063a7f437791461046c578063ad8f50081461046e578063bc6d909414610477578063bdec3ad114610557578063c19d93fb1461059a578063c9503fe2146105ad578063e0a73a93146105b6578063ea71b02d146105bf578063ea8a1af0146105d1578063ee4a96f9146105f3578063f1ff78a01461065c575b61046c610002565b610665600054600160a060020a031681565b6040805160c081810190925261046c9160049160c4918390600690839083908082843760408051808301909152929750909561018495509193509091908390839080828437509095505050505050600554600090600160a060020a0390811633909116146106a857610002565b61068260015460a060020a900460ff166000145b90565b61069660085481565b61046c600435600154600160a060020a03166000141561072157610002565b610696600d5481565b610696600435600f8160068110156100025750015481565b61069660045481565b61069660035481565b610665600554600160a060020a031681565b61069660043560158160068110156100025750015481565b6106966004355b600b54600f5460009160028202808203928083039290810191018386101561078357601054840186900394505b50505050919050565b61069660025481565b61069660095481565b61046c600554600090600160a060020a03908116339091161461085857610002565b61046c600435600554600090600160a060020a03908116339091161461092e57610002565b6106826001805460a060020a900460ff161461020f565b610696600b5481565b61068260075460a060020a900460ff1681565b6106966004355b600b54601554600091600282028082039280830392908101910183861015610a6c5760165494506102cb565b61046c6004356024356044356040805160015460e360020a631c2d8fb302825260b260020a691858d8dbdd5b9d18dd1b02600483015291516000928392600160a060020a03919091169163e16c7d9891602481810192602092909190829003018187876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663c4b0c96a336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610b4657610002565b005b610696600a5481565b61046c60006000600060006000600160009054906101000a9004600160a060020a0316600160a060020a031663e16c7d986040518160e060020a028152600401808060b260020a691858d8dbdd5b9d18dd1b0281526020015060200190506020604051808303816000876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663c4b0c96a336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610f1757610002565b61046c5b60015b60058160ff16101561071e57600f6001820160ff166006811015610002578101549060ff83166006811015610002570154101561129057610002565b61069660015460a060020a900460ff1681565b61069660065481565b610696600c5481565b610665600754600160a060020a031681565b61046c600554600090600160a060020a0390811633909116146112c857610002565b6040805160c081810190925261046c9160049160c4918390600690839083908082843760408051808301909152929750909561018495509193509091908390839080828437509095505050505050600154600090600160a060020a03168114156113fb57610002565b610696600e5481565b60408051600160a060020a03929092168252519081900360200190f35b604080519115158252519081900360200190f35b60408051918252519081900360200190f35b5060005b60068160ff16101561070857828160ff166006811015610002576020020151600f60ff831660068110156100025701558160ff82166006811015610002576020020151601560ff831660068110156100025701556001016106ac565b61071061055b565b505050565b600e8054820190555b50565b6040805160015460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061071557610002565b83861015801561079257508286105b156107b457600f546010546011548689039082030291909104900394506102cb565b8286101580156107c55750600b5486105b156107e757600f546011546012548589039082030291909104900394506102cb565b600b5486108015906107f857508186105b1561081d57600b54600f546012546013549289039281039290920204900394506102cb565b81861015801561082c57508086105b1561084e57600f546013546014548489039082030291909104900394506102cb565b60145494506102cb565b60015460a060020a900460ff1660001461087157610002565b600254600a01431161088257610002565b6040805160015460e360020a631c2d8fb302825260a860020a6a636f6e74726163746170690260048301529151600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663771d50e16040518160e060020a0281526004018090506000604051808303816000876161da5a03f1156100025750505050565b60015460a060020a900460ff1660001461094757610002565b600254600a01431161095857610002565b6040805160015460e360020a631c2d8fb302825260a860020a6a636f6e74726163746170690260048301529151600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180517f51a34eb8000000000000000000000000000000000000000000000000000000008252600482018690529151919350600160a060020a03841692506351a34eb8916024808301926000929190829003018183876161da5a03f11561000257505050600b8290554360025560408051838152905130600160a060020a0316917fa609f6bd4ad0b4f419ddad4ac9f0d02c2b9295c5e6891469055cf73c2b568fff919081900360200190a25050565b838610158015610a7b57508286105b15610a9d576015546016546017548689039082900302919091040194506102cb565b828610158015610aae5750600b5486105b15610ad0576015546017546018548589039082900302919091040194506102cb565b600b548610801590610ae157508186105b15610b0657600b546015546018546019549289039281900392909202040194506102cb565b818610158015610b1557508086105b15610b3757601554601954601a548489039082900302919091040194506102cb565b601a54860181900394506102cb565b60015460a060020a900460ff16600014610b5f57610002565b6001805460a060020a60ff02191660a060020a17908190556040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180516004805460e260020a633e4baddd028452908301529151919450600160a060020a038516925063f92eb77491602482810192602092919082900301816000876161da5a03f115610002575050604080518051600a556005547ffebf661200000000000000000000000000000000000000000000000000000000825233600160a060020a03908116600484015216602482015260448101879052905163febf661291606480820192600092909190829003018183876161da5a03f115610002575050508215610cc7576007805473ffffffffffffffffffffffffffffffffffffffff191633179055610dbb565b6040805160055460065460e060020a63599efa6b028352600160a060020a039182166004840152602483015291519184169163599efa6b91604481810192600092909190829003018183876161da5a03f115610002575050604080516006547f56ccb6f000000000000000000000000000000000000000000000000000000000825233600160a060020a03166004830152602482015290516356ccb6f091604480820192600092909190829003018183876161da5a03f115610002575050600580546007805473ffffffffffffffffffffffffffffffffffffffff19908116600160a060020a038416179091551633179055505b6007805460a060020a60ff02191660a060020a87810291909117918290556008544301600955900460ff1615610df757600a54610e039061029e565b600a54610e0b90610367565b600c55610e0f565b600c555b600c54670de0b6b3a7640000850204600d55600754600554604080517f759297bb000000000000000000000000000000000000000000000000000000008152600160a060020a039384166004820152918316602483015260448201879052519184169163759297bb91606481810192600092909190829003018183876161da5a03f11561000257505060408051600754600a54600d54600554600c5460a060020a850460ff161515865260208601929092528486019290925260608401529251600160a060020a0391821694509281169230909116917f3b3d1986083d191be01d28623dc19604728e29ae28bdb9ba52757fdee1a18de2919081900360800190a45050505050565b600954431015610f2657610002565b6001805460a060020a900460ff1614610f3e57610002565b6001805460a060020a60ff0219167402000000000000000000000000000000000000000017908190556040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180516004805460e260020a633e4baddd028452908301529151919750600160a060020a038816925063f92eb77491602482810192602092919082900301816000876161da5a03f115610002575050604051516007549095506000945060a060020a900460ff1615905061105c57600a5484111561105757600a54600d54670de0b6b3a7640000918603020492505b61107e565b600a5484101561107e57600a54600d54670de0b6b3a764000091869003020492505b60065483111561108e5760065492505b6006548390039150600083111561111857604080516005546007547f5928d37f000000000000000000000000000000000000000000000000000000008352600160a060020a0391821660048401528116602483015260448201869052915191871691635928d37f91606481810192600092909190829003018183876161da5a03f115610002575050505b600082111561117a576040805160055460e060020a63599efa6b028252600160a060020a0390811660048301526024820185905291519187169163599efa6b91604481810192600092909190829003018183876161da5a03f115610002575050505b6040805185815260208101849052808201859052905130600160a060020a0316917f89e690b1d5aaae14f3e85f108dc92d9ab3763a58d45aed8b59daedbbae8fe794919081900360600190a260008311156112285784600160a060020a0316634cc927d785336040518360e060020a0281526004018083815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f11561000257505050611282565b84600160a060020a0316634cc927d7600a60005054336040518360e060020a0281526004018083815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f115610002575050505b600054600160a060020a0316ff5b60156001820160ff166006811015610002578101549060ff8316600681101561000257015411156112c057610002565b60010161055e565b60015460a060020a900460ff166000146112e157610002565b600254600a0143116112f257610002565b6001546040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f11561000257505060408051805160055460065460e060020a63599efa6b028452600160a060020a03918216600485015260248401529251909450918416925063599efa6b916044808301926000929190829003018183876161da5a03f1156100025750505080600160a060020a0316632b68bb2d6040518160e060020a0281526004018090506000604051808303816000876161da5a03f115610002575050600054600160a060020a03169050ff5b6001546040805160e060020a6313bc6d4b02815233600160a060020a039081166004830152915191909216916313bc6d4b91602480830192602092919082900301816000876161da5a03f11561000257505060405151151590506106a85761000256",
+ "nonce": "1",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000001": "0x0000000000000000000000002cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000000000000000000000000000000000000000006195",
+ "0x0000000000000000000000000000000000000000000000000000000000000004": "0x5842545553440000000000000000000000000000000000000000000000000000",
+ "0x0000000000000000000000000000000000000000000000000000000000000005": "0x00000000000000000000000070c9217d814985faef62b124420f8dfbddd96433",
+ "0x0000000000000000000000000000000000000000000000000000000000000006": "0x0000000000000000000000000000000000000000000000008ac7230489e80000",
+ "0x000000000000000000000000000000000000000000000000000000000000000b": "0x0000000000000000000000000000000000000000000000283c7b9181eca20000"
+ }
+ },
+ "0xcf00ffd997ad14939736f026006498e3f099baaf": {
+ "balance": "0x0",
+ "code": "0x606060405236156100cf5760e060020a600035046302d05d3f81146100d7578063031e7f5d146100e95780631ab9075a1461010b5780632243118a1461014657806327aad68a1461016557806338a699a4146101da5780635188f996146101f8578063581d5d601461021e57806381738c5914610246578063977da54014610269578063a07421ce14610288578063a7f43779146102be578063bdbdb086146102dc578063e1c7111914610303578063f4f2821b14610325578063f905c15a1461034a578063f92eb77414610353575b610387610002565b610389600054600160a060020a031681565b610387600435602435600254600160a060020a0316600014156103a857610002565b610387600435600254600160a060020a031660001480159061013c575060025433600160a060020a03908116911614155b1561042957610002565b610387600435600254600160a060020a03166000141561044b57610002565b6102ac60043560008181526004602081815260408320547f524d81d3000000000000000000000000000000000000000000000000000000006060908152610100909104600160a060020a031692839263524d81d3926064928188876161da5a03f1156100025750506040515192506103819050565b61039c60043560008181526004602052604090205460ff165b919050565b6103876004356024356002546000908190600160a060020a031681141561079457610002565b61038760043560243560025460009081908190600160a060020a031681141561080457610002565b61038960043560008181526004602052604081205460ff1615156109e357610002565b610387600435600254600160a060020a0316600014156109fb57610002565b600435600090815260096020526040902054670de0b6b3a764000090810360243502045b60408051918252519081900360200190f35b61038760025433600160a060020a03908116911614610a9257610002565b600435600090815260086020526040902054670de0b6b3a7640000602435909102046102ac565b610387600435602435600254600160a060020a031660001415610aa057610002565b61038760043560025460009081908190600160a060020a0316811415610b3657610002565b6102ac60035481565b6102ac600435600081815260076020908152604080832054600690925290912054670de0b6b3a76400000204805b50919050565b005b600160a060020a03166060908152602090f35b15156060908152602090f35b60025460e060020a6313bc6d4b02606090815233600160a060020a03908116606452909116906313bc6d4b906084906020906024816000876161da5a03f11561000257505060405151151590506103fe57610002565b60008281526004602052604090205460ff16151561041b57610002565b600860205260406000205550565b6002805473ffffffffffffffffffffffffffffffffffffffff19168217905550565b60025460e060020a6313bc6d4b02606090815233600160a060020a03908116606452909116906313bc6d4b906084906020906024816000876161da5a03f11561000257505060405151151590506104a157610002565b604080516000838152600460205291909120805460ff1916600117905561040280610de2833901809050604051809103906000f0600460005060008360001916815260200190815260200160002060005060000160016101000a815481600160a060020a030219169083021790555066470de4df8200006008600050600083600019168152602001908152602001600020600050819055506703782dace9d9000060096000506000836000191681526020019081526020016000206000508190555050565b600460005060008560001916815260200190815260200160002060005060000160019054906101000a9004600160a060020a0316915081600160a060020a031663524d81d36040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060405151821415905061060057838152600660209081526040808320839055600790915281208190555b81600160a060020a0316630a3b0a4f846040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f11561000257505050600160a060020a038316808252600560209081526040808420879055805160e160020a6364a81ff102815290518694670de0b6b3a7640000949363c9503fe29360048181019492939183900301908290876161da5a03f11561000257505060408051805160e060020a636f265b930282529151919291636f265b939160048181019260209290919082900301816000876161da5a03f11561000257505050604051805190602001500204600660005060008660001916815260200190815260200160002060008282825054019250508190555080600160a060020a031663c9503fe26040518160e060020a0281526004018090506020604051808303816000876161da5a03f115610002575050506040518051906020015060076000506000866000191681526020019081526020016000206000828282505401925050819055505b50505050565b60025460e060020a6313bc6d4b02606090815233600160a060020a03908116606452909116906313bc6d4b9060849060209060248187876161da5a03f11561000257505060405151151590506107e957610002565b8381526004602052604081205460ff16151561056657610002565b60025460e060020a6313bc6d4b02606090815233600160a060020a03908116606452909116906313bc6d4b9060849060209060248187876161da5a03f115610002575050604051511515905061085957610002565b849250670de0b6b3a764000083600160a060020a031663c9503fe26040518160e060020a0281526004018090506020604051808303816000876161da5a03f115610002575060408051805160e160020a6364a81ff102825291519189028590049650600481810192602092909190829003018188876161da5a03f11561000257505060408051805160e060020a636f265b930282529151919291636f265b9391600481810192602092909190829003018189876161da5a03f115610002575050506040518051906020015002049050806006600050600085600160a060020a0316632e94420f6040518160e060020a0281526004018090506020604051808303816000876161da5a03f1156100025750604080518051855260208681528286208054989098039097557f2e94420f00000000000000000000000000000000000000000000000000000000815290518896600483810193919291829003018187876161da5a03f115610002575050604080515183526020939093525020805490910190555050505050565b60409020546101009004600160a060020a03166101f3565b60025460e060020a6313bc6d4b02606090815233600160a060020a03908116606452909116906313bc6d4b906084906020906024816000876161da5a03f1156100025750506040515115159050610a5157610002565b60008181526004602052604090205460ff161515610a6e57610002565b6040600020805474ffffffffffffffffffffffffffffffffffffffffff1916905550565b600254600160a060020a0316ff5b60025460e060020a6313bc6d4b02606090815233600160a060020a03908116606452909116906313bc6d4b906084906020906024816000876161da5a03f1156100025750506040515115159050610af657610002565b60008281526004602052604090205460ff161515610b1357610002565b670de0b6b3a7640000811115610b2857610002565b600960205260406000205550565b60025460e060020a6313bc6d4b02606090815233600160a060020a03908116606452909116906313bc6d4b9060849060209060248187876161da5a03f1156100025750506040515115159050610b8b57610002565b600160a060020a038416815260056020908152604080832054808452600490925282205490935060ff161515610bc057610002565b600460005060008460001916815260200190815260200160002060005060000160019054906101000a9004600160a060020a0316915081600160a060020a031663b9caebf4856040518260e060020a0281526004018082600160a060020a031681526020019150506000604051808303816000876161da5a03f115610002575050506005600050600085600160a060020a0316815260200190815260200160002060005060009055839050600082600160a060020a031663524d81d36040518160e060020a0281526004018090506020604051808303816000876161da5a03f115610002575050604051519190911115905061078e57670de0b6b3a764000081600160a060020a031663c9503fe26040518160e060020a0281526004018090506020604051808303816000876161da5a03f11561000257505060408051805160e060020a636f265b930282529151919291636f265b939160048181019260209290919082900301816000876161da5a03f11561000257505050604051805190602001500204600660005060008560001916815260200190815260200160002060008282825054039250508190555080600160a060020a031663c9503fe26040518160e060020a0281526004018090506020604051808303816000876161da5a03f115610002575050506040518051906020015060076000506000856000191681526020019081526020016000206000828282505403925050819055505050505056606060405260008054600160a060020a031916331790556103de806100246000396000f3606060405236156100615760e060020a600035046302d05d3f81146100695780630a3b0a4f1461007b5780630d327fa7146100f6578063524d81d314610109578063a7f4377914610114578063b9caebf414610132578063bbec3bae14610296575b6102ce610002565b6102d0600054600160a060020a031681565b6102ce600435600254600090600160a060020a03168114156102ed5760028054600160a060020a03199081168417808355600160a060020a03808616855260036020526040852060018101805493831694909316939093179091559154815461010060a860020a031916921661010002919091179055610372565b6102d0600254600160a060020a03165b90565b6102e3600154610106565b6102ce60005433600160a060020a039081169116146103c657610002565b6102ce600435600160a060020a038116600090815260036020526040812054819060ff16801561016457506001548190115b1561029157506040808220600180820154915461010090819004600160a060020a039081168087528587209093018054600160a060020a031916948216948517905583865293909420805461010060a860020a03191694820294909417909355600254909190811690841614156101e85760028054600160a060020a031916821790555b600254600160a060020a0390811690841614156102105760028054600160a060020a03191690555b6003600050600084600160a060020a0316815260200190815260200160002060006000820160006101000a81549060ff02191690556000820160016101000a815490600160a060020a0302191690556001820160006101000a815490600160a060020a03021916905550506001600081815054809291906001900391905055505b505050565b600160a060020a036004358181166000908152600360205260408120600101546002546102d09491821691168114156103d4576103d8565b005b600160a060020a03166060908152602090f35b6060908152602090f35b60028054600160a060020a03908116835260036020526040808420805461010060a860020a0319808216610100808a029190911790935590829004841680875283872060019081018054600160a060020a03199081168b179091559654868a168952949097209687018054949095169390951692909217909255835416908202179091555b60016003600050600084600160a060020a0316815260200190815260200160002060005060000160006101000a81548160ff0219169083021790555060016000818150548092919060010191905055505050565b600054600160a060020a0316ff5b8091505b5091905056",
+ "nonce": "3",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000002cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "0x3571d73f14f31a1463bd0a2f92f7fde1653d4e1ead7aedf4b0a5df02f16092ab": "0x0000000000000000000000000000000000000000000007d634e4c55188be0000",
+ "0x4e64fe2d1b72d95a0a31945cc6e4f4e524ac5ad56d6bd44a85ec7bc9cc0462c0": "0x000000000000000000000000000000000000000000000002b5e3af16b1880000"
+ }
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "117124093",
+ "extraData": "0xd5830105008650617269747986312e31322e31826d61",
+ "gasLimit": "4707788",
+ "hash": "0xad325e4c49145fb7a4058a68ac741cc8607a71114e23fc88083c7e881dd653e7",
+ "miner": "0x00714b9ac97fd6bd9325a059a70c9b9fa94ce050",
+ "mixHash": "0x0af918f65cb4af04b608fc1f14a849707696986a0e7049e97ef3981808bcc65f",
+ "nonce": "0x38dee147326a8d40",
+ "number": "25000",
+ "stateRoot": "0xc5d6bbcd46236fcdcc80b332ffaaa5476b980b01608f9708408cfef01b58bd5b",
+ "timestamp": "1479891517",
+ "totalDifficulty": "1895410389427"
+ },
+ "input": "0xf88b8206628504a817c8008303d09094c212e03b9e060e36facad5fd8f4435412ca22e6b80a451a34eb80000000000000000000000000000000000000000000000280faf689c35ac00002aa0a7ee5b7877811bf671d121b40569462e722657044808dc1d6c4f1e4233ec145ba0417e7543d52b65738d9df419cbe40a708424f4d54b0fc145c0a64545a2bb1065",
+ "result": {
+ "calls": [
+ {
+ "from": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "gas": "0x31217",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d98636f6e7472616374617069000000000000000000000000000000000000000000",
+ "output": "0x000000000000000000000000b4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "calls": [
+ {
+ "from": "0xb4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "gas": "0x2a68d",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d98636f6e747261637463746c000000000000000000000000000000000000000000",
+ "output": "0x0000000000000000000000003e9286eafa2db8101246c2131c09b49080d00690",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "calls": [
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x23ac9",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d98636f6e7472616374646200000000000000000000000000000000000000000000",
+ "output": "0x0000000000000000000000007986bad81f4cbd9317f5a46861437dae58d69113",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x23366",
+ "gasUsed": "0x273",
+ "input": "0x16c66cc6000000000000000000000000c212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x7986bad81f4cbd9317f5a46861437dae58d69113",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0xb4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "gas": "0x29f35",
+ "gasUsed": "0xf8d",
+ "input": "0x16c66cc6000000000000000000000000c212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0xb4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "gas": "0x28a9e",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d98636f6e747261637463746c000000000000000000000000000000000000000000",
+ "output": "0x0000000000000000000000003e9286eafa2db8101246c2131c09b49080d00690",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "calls": [
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x21d79",
+ "gasUsed": "0x24d",
+ "input": "0x13bc6d4b000000000000000000000000b4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x2165b",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d986d61726b65746462000000000000000000000000000000000000000000000000",
+ "output": "0x000000000000000000000000cf00ffd997ad14939736f026006498e3f099baaf",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "calls": [
+ {
+ "from": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "gas": "0x1a8e8",
+ "gasUsed": "0x24d",
+ "input": "0x13bc6d4b0000000000000000000000003e9286eafa2db8101246c2131c09b49080d00690",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "gas": "0x1a2c6",
+ "gasUsed": "0x3cb",
+ "input": "0xc9503fe2",
+ "output": "0x0000000000000000000000000000000000000000000000008ac7230489e80000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "gas": "0x19b72",
+ "gasUsed": "0x3cb",
+ "input": "0xc9503fe2",
+ "output": "0x0000000000000000000000000000000000000000000000008ac7230489e80000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "gas": "0x19428",
+ "gasUsed": "0x305",
+ "input": "0x6f265b93",
+ "output": "0x0000000000000000000000000000000000000000000000283c7b9181eca20000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "gas": "0x18d45",
+ "gasUsed": "0x229",
+ "input": "0x2e94420f",
+ "output": "0x5842545553440000000000000000000000000000000000000000000000000000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "gas": "0x1734e",
+ "gasUsed": "0x229",
+ "input": "0x2e94420f",
+ "output": "0x5842545553440000000000000000000000000000000000000000000000000000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x20ee1",
+ "gasUsed": "0x5374",
+ "input": "0x581d5d60000000000000000000000000c212e03b9e060e36facad5fd8f4435412ca22e6b0000000000000000000000000000000000000000000000280faf689c35ac0000",
+ "output": "0x",
+ "to": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x1b6c1",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d986c6f676d67720000000000000000000000000000000000000000000000000000",
+ "output": "0x0000000000000000000000002a98c5f40bfa3dee83431103c535f6fae9a8ad38",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x1af69",
+ "gasUsed": "0x229",
+ "input": "0x2e94420f",
+ "output": "0x5842545553440000000000000000000000000000000000000000000000000000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "calls": [
+ {
+ "from": "0x2a98c5f40bfa3dee83431103c535f6fae9a8ad38",
+ "gas": "0x143a5",
+ "gasUsed": "0x24d",
+ "input": "0x13bc6d4b0000000000000000000000003e9286eafa2db8101246c2131c09b49080d00690",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x1a91d",
+ "gasUsed": "0x12fa",
+ "input": "0x0accce0600000000000000000000000000000000000000000000000000000000000000025842545553440000000000000000000000000000000000000000000000000000000000000000000000000000c212e03b9e060e36facad5fd8f4435412ca22e6b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
+ "output": "0x",
+ "to": "0x2a98c5f40bfa3dee83431103c535f6fae9a8ad38",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x19177",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d986c6f676d67720000000000000000000000000000000000000000000000000000",
+ "output": "0x0000000000000000000000002a98c5f40bfa3dee83431103c535f6fae9a8ad38",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x18a22",
+ "gasUsed": "0x229",
+ "input": "0x2e94420f",
+ "output": "0x5842545553440000000000000000000000000000000000000000000000000000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x18341",
+ "gasUsed": "0x334",
+ "input": "0xe16c7d986d61726b65746462000000000000000000000000000000000000000000000000",
+ "output": "0x000000000000000000000000cf00ffd997ad14939736f026006498e3f099baaf",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x17bec",
+ "gasUsed": "0x229",
+ "input": "0x2e94420f",
+ "output": "0x5842545553440000000000000000000000000000000000000000000000000000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x1764e",
+ "gasUsed": "0x45c",
+ "input": "0xf92eb7745842545553440000000000000000000000000000000000000000000000000000",
+ "output": "0x00000000000000000000000000000000000000000000002816d180e30c390000",
+ "to": "0xcf00ffd997ad14939736f026006498e3f099baaf",
+ "type": "CALL",
+ "value": "0x0"
+ },
+ {
+ "calls": [
+ {
+ "from": "0x2a98c5f40bfa3dee83431103c535f6fae9a8ad38",
+ "gas": "0x108ba",
+ "gasUsed": "0x24d",
+ "input": "0x13bc6d4b0000000000000000000000003e9286eafa2db8101246c2131c09b49080d00690",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x2cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "gas": "0x16e62",
+ "gasUsed": "0xebb",
+ "input": "0x645a3b72584254555344000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002816d180e30c390000",
+ "output": "0x",
+ "to": "0x2a98c5f40bfa3dee83431103c535f6fae9a8ad38",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0xb4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "gas": "0x283b9",
+ "gasUsed": "0xc51c",
+ "input": "0x949ae479000000000000000000000000c212e03b9e060e36facad5fd8f4435412ca22e6b0000000000000000000000000000000000000000000000280faf689c35ac0000",
+ "output": "0x",
+ "to": "0x3e9286eafa2db8101246c2131c09b49080d00690",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "gas": "0x30b4a",
+ "gasUsed": "0xedb7",
+ "input": "0x51a34eb80000000000000000000000000000000000000000000000280faf689c35ac0000",
+ "output": "0x",
+ "to": "0xb4fe7aa695b326c9d219158d2ca50db77b39f99f",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0x70c9217d814985faef62b124420f8dfbddd96433",
+ "gas": "0x37b38",
+ "gasUsed": "0x12bb3",
+ "input": "0x51a34eb80000000000000000000000000000000000000000000000280faf689c35ac0000",
+ "output": "0x",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_delegatecall.json b/eth/tracers/testdata/call_tracer_delegatecall.json
new file mode 100644
index 0000000..f7ad6df
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_delegatecall.json
@@ -0,0 +1,97 @@
+{
+ "context": {
+ "difficulty": "31927752",
+ "gasLimit": "4707788",
+ "miner": "0x5659922ce141eedbc2733678f9806c77b4eebee8",
+ "number": "11495",
+ "timestamp": "1479735917"
+ },
+ "genesis": {
+ "alloc": {
+ "0x13204f5d64c28326fd7bd05fd4ea855302d7f2ff": {
+ "balance": "0x0",
+ "code": "0x606060405236156100825760e060020a60003504630a0313a981146100875780630a3b0a4f146101095780630cd40fea1461021257806329092d0e1461021f5780634cd06a5f146103295780635dbe47e8146103395780637a9e5410146103d9578063825db5f7146103e6578063a820b44d146103f3578063efa52fb31461047a575b610002565b34610002576104fc600435600060006000507342b02b5deeb78f34cd5ac896473b63e6c99a71a26333556e849091846000604051602001526040518360e060020a028152600401808381526020018281526020019250505060206040518083038186803b156100025760325a03f415610002575050604051519150505b919050565b346100025761051060043560006000507342b02b5deeb78f34cd5ac896473b63e6c99a71a2637d65837a9091336000604051602001526040518360e060020a0281526004018083815260200182600160a060020a031681526020019250505060206040518083038186803b156100025760325a03f4156100025750506040515115905061008257604080517f21ce24d4000000000000000000000000000000000000000000000000000000008152600060048201819052600160a060020a038416602483015291517342b02b5deeb78f34cd5ac896473b63e6c99a71a2926321ce24d49260448082019391829003018186803b156100025760325a03f415610002575050505b50565b3461000257610512600181565b346100025761051060043560006000507342b02b5deeb78f34cd5ac896473b63e6c99a71a2637d65837a9091336000604051602001526040518360e060020a0281526004018083815260200182600160a060020a031681526020019250505060206040518083038186803b156100025760325a03f4156100025750506040515115905061008257604080517f89489a87000000000000000000000000000000000000000000000000000000008152600060048201819052600160a060020a038416602483015291517342b02b5deeb78f34cd5ac896473b63e6c99a71a2926389489a879260448082019391829003018186803b156100025760325a03f4156100025750505061020f565b3461000257610528600435610403565b34610002576104fc600435604080516000602091820181905282517f7d65837a00000000000000000000000000000000000000000000000000000000815260048101829052600160a060020a0385166024820152925190927342b02b5deeb78f34cd5ac896473b63e6c99a71a292637d65837a92604480840193829003018186803b156100025760325a03f4156100025750506040515191506101049050565b3461000257610512600c81565b3461000257610512600081565b3461000257610528600061055660005b600060006000507342b02b5deeb78f34cd5ac896473b63e6c99a71a263685a1f3c9091846000604051602001526040518360e060020a028152600401808381526020018281526020019250505060206040518083038186803b156100025760325a03f4156100025750506040515191506101049050565b346100025761053a600435600060006000507342b02b5deeb78f34cd5ac896473b63e6c99a71a263f775b6b59091846000604051602001526040518360e060020a028152600401808381526020018281526020019250505060206040518083038186803b156100025760325a03f4156100025750506040515191506101049050565b604080519115158252519081900360200190f35b005b6040805160ff9092168252519081900360200190f35b60408051918252519081900360200190f35b60408051600160a060020a039092168252519081900360200190f35b90509056",
+ "nonce": "1",
+ "storage": {
+ "0x4d140b25abf3c71052885c66f73ce07cff141c1afabffdaf5cba04d625b7ebcc": "0x0000000000000000000000000000000000000000000000000000000000000001"
+ }
+ },
+ "0x269296dddce321a6bcbaa2f0181127593d732cba": {
+ "balance": "0x0",
+ "code": "0x606060405236156101275760e060020a60003504630cd40fea811461012c578063173825d9146101395780631849cb5a146101c7578063285791371461030f5780632a58b3301461033f5780632cb0d48a146103565780632f54bf6e1461036a578063332b9f061461039d5780633ca8b002146103c55780633df4ddf4146103d557806341c0e1b5146103f457806347799da81461040557806362a51eee1461042457806366907d13146104575780637065cb48146104825780637a9e541014610496578063825db5f7146104a3578063949d225d146104b0578063a51687df146104c7578063b4da4e37146104e6578063b4e6850b146104ff578063bd7474ca14610541578063e75623d814610541578063e9938e1114610555578063f5d241d314610643575b610002565b3461000257610682600181565b34610002576106986004356106ff335b60006001600a9054906101000a9004600160a060020a0316600160a060020a0316635dbe47e8836000604051602001526040518260e060020a0281526004018082600160a060020a03168152602001915050602060405180830381600087803b156100025760325a03f1156100025750506040515191506103989050565b3461000257604080516101008082018352600080835260208084018290528385018290526060808501839052608080860184905260a080870185905260c080880186905260e09788018690526001605060020a0360043581168752600586529589902089519788018a528054808816808a52605060020a91829004600160a060020a0316978a01889052600183015463ffffffff8082169d8c018e905264010000000082048116988c01899052604060020a90910416958a018690526002830154948a01859052600390920154808916938a01849052049096169690970186905293969495949293604080516001605060020a03998a16815297891660208901529590971686860152600160a060020a03909316606086015263ffffffff9182166080860152811660a08501521660c083015260e08201929092529051908190036101000190f35b346100025761069a60043560018054600091829160ff60f060020a909104161515141561063d5761072833610376565b34610002576106ae6004546001605060020a031681565b34610002576106986004356108b333610149565b346100025761069a6004355b600160a060020a03811660009081526002602052604090205460ff1615156001145b919050565b34610002576106986001805460ff60f060020a9091041615151415610913576108ed33610376565b346100025761069a600435610149565b34610002576106ae6003546001605060020a03605060020a9091041681565b346100025761069861091533610149565b34610002576106ae6003546001605060020a0360a060020a9091041681565b346100025761069a60043560243560018054600091829160ff60f060020a909104161515141561095e5761092633610376565b34610002576106986004356001805460ff60f060020a909104161515141561072557610a8b33610376565b3461000257610698600435610aa533610149565b3461000257610682600c81565b3461000257610682600081565b34610002576106ae6003546001605060020a031681565b34610002576106ca600154600160a060020a03605060020a9091041681565b346100025761069a60015460ff60f060020a9091041681565b346100025761069a60043560243560443560643560843560a43560c43560018054600091829160ff60f060020a9091041615151415610b5857610ad233610376565b3461000257610698600435610bd633610149565b34610002576106e6600435604080516101008181018352600080835260208084018290528385018290526060808501839052608080860184905260a080870185905260c080880186905260e09788018690526001605060020a03808b168752600586529589902089519788018a5280548088168952600160a060020a03605060020a918290041696890196909652600181015463ffffffff8082169b8a019b909b5264010000000081048b1695890195909552604060020a90940490981691860182905260028301549086015260039091015480841696850196909652940416918101919091525b50919050565b346100025761069a60043560243560443560643560843560a43560018054600091829160ff60f060020a9091041615151415610c8e57610bfb33610376565b6040805160ff9092168252519081900360200190f35b005b604080519115158252519081900360200190f35b604080516001605060020a039092168252519081900360200190f35b60408051600160a060020a039092168252519081900360200190f35b6040805163ffffffff9092168252519081900360200190f35b1561012757600160a060020a0381166000908152600260205260409020805460ff191690555b50565b1561063d57506001605060020a0380831660009081526005602052604090208054909116151561075b576000915061063d565b604080516101008101825282546001605060020a038082168352600160a060020a03605060020a92839004166020840152600185015463ffffffff80821695850195909552640100000000810485166060850152604060020a90049093166080830152600284015460a0830152600384015480841660c08401520490911660e0820152610817905b8051600354600090819060016001605060020a0390911611610c995760038054605060020a60f060020a0319169055610ddf565b600380546001605060020a031981166000196001605060020a03928316011782558416600090815260056020526040812080547fffff000000000000000000000000000000000000000000000000000000000000168155600181810180546bffffffffffffffffffffffff191690556002820192909255909101805473ffffffffffffffffffffffffffffffffffffffff19169055915061063d565b1561012757600180547fff00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1660f060020a8302179055610725565b1561091357600480546001605060020a031981166001605060020a039091166001011790555b565b156101275733600160a060020a0316ff5b1561095e57506001605060020a03808416600090815260056020526040902080549091161515610965576000915061095e565b600191505b5092915050565b60038101546001605060020a0384811691161415610986576001915061095e565b604080516101008101825282546001605060020a038082168352600160a060020a03605060020a92839004166020840152600185015463ffffffff80821695850195909552640100000000810485166060850152604060020a90049093166080830152600284015460a0830152600384015480841660c08401520490911660e0820152610a12906107e3565b61095983825b80546003546001605060020a0391821691600091161515610de55760038054605060020a60a060020a031916605060020a84021760a060020a69ffffffffffffffffffff02191660a060020a84021781558301805473ffffffffffffffffffffffffffffffffffffffff19169055610ddf565b1561072557600480546001605060020a0319168217905550565b1561012757600160a060020a0381166000908152600260205260409020805460ff19166001179055610725565b15610b5857506001605060020a038088166000908152600560205260409020805490911615610b645760009150610b58565b6004546001605060020a0390811690891610610b3057600480546001605060020a03191660018a011790555b6003805460016001605060020a03821681016001605060020a03199092169190911790915591505b50979650505050505050565b80546001605060020a0319168817605060020a60f060020a031916605060020a880217815560018101805463ffffffff1916871767ffffffff0000000019166401000000008702176bffffffff00000000000000001916604060020a860217905560028101839055610b048982610a18565b156101275760018054605060020a60f060020a031916605060020a8302179055610725565b15610c8e57506001605060020a03808816600090815260056020526040902080549091161515610c2e5760009150610c8e565b8054605060020a60f060020a031916605060020a88021781556001808201805463ffffffff1916881767ffffffff0000000019166401000000008802176bffffffff00000000000000001916604060020a87021790556002820184905591505b509695505050505050565b6003546001605060020a03848116605060020a909204161415610d095760e084015160038054605060020a928302605060020a60a060020a031990911617808255919091046001605060020a031660009081526005602052604090200180546001605060020a0319169055610ddf565b6003546001605060020a0384811660a060020a909204161415610d825760c08401516003805460a060020a92830260a060020a69ffffffffffffffffffff021990911617808255919091046001605060020a03166000908152600560205260409020018054605060020a60a060020a0319169055610ddf565b505060c082015160e08301516001605060020a0380831660009081526005602052604080822060039081018054605060020a60a060020a031916605060020a8702179055928416825290200180546001605060020a031916831790555b50505050565b6001605060020a0384161515610e6457600380546001605060020a03605060020a9182900481166000908152600560205260409020830180546001605060020a0319908116871790915583548785018054918590049093168402605060020a60a060020a03199182161790911690915582549185029116179055610ddf565b506001605060020a038381166000908152600560205260409020600390810180549185018054605060020a60a060020a0319908116605060020a94859004909516808502959095176001605060020a0319168817909155815416918402919091179055801515610ef4576003805460a060020a69ffffffffffffffffffff02191660a060020a8402179055610ddf565b6003808401546001605060020a03605060020a9091041660009081526005602052604090200180546001605060020a031916831790555050505056",
+ "nonce": "1",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000001": "0x000113204f5d64c28326fd7bd05fd4ea855302d7f2ff00000000000000000000"
+ }
+ },
+ "0x42b02b5deeb78f34cd5ac896473b63e6c99a71a2": {
+ "balance": "0x0",
+ "code": "0x6504032353da7150606060405236156100695760e060020a60003504631bf7509d811461006e57806321ce24d41461008157806333556e84146100ec578063685a1f3c146101035780637d65837a1461011757806389489a8714610140578063f775b6b5146101fc575b610007565b61023460043560006100fd82600061010d565b610246600435602435600160a060020a03811660009081526020839052604081205415156102cb57826001016000508054806001018281815481835581811511610278576000838152602090206102789181019083015b808211156102d057600081556001016100d8565b610248600435602435600182015481105b92915050565b6102346004356024355b60018101906100fd565b610248600435602435600160a060020a03811660009081526020839052604090205415156100fd565b61024660043560243580600160a060020a031632600160a060020a03161415156101f857600160a060020a038116600090815260208390526040902054156101f857600160a060020a038116600090815260208390526040902054600183018054909160001901908110156100075760009182526020808320909101805473ffffffffffffffffffffffffffffffffffffffff19169055600160a060020a038316825283905260408120556002820180546000190190555b5050565b61025c60043560243560008260010160005082815481101561000757600091825260209091200154600160a060020a03169392505050565b60408051918252519081900360200190f35b005b604080519115158252519081900360200190f35b60408051600160a060020a039092168252519081900360200190f35b50505060009283526020808420909201805473ffffffffffffffffffffffffffffffffffffffff191686179055600160a060020a0385168352908590526040909120819055600284018054600101905590505b505050565b509056",
+ "nonce": "1",
+ "storage": {}
+ },
+ "0xa529806c67cc6486d4d62024471772f47f6fd672": {
+ "balance": "0x67820e39ac8fe9800",
+ "code": "0x",
+ "nonce": "68",
+ "storage": {}
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "31912170",
+ "extraData": "0xd783010502846765746887676f312e372e33856c696e7578",
+ "gasLimit": "4712388",
+ "hash": "0x0855914bdc581bccdc62591fd438498386ffb59ea4d5361ed5c3702e26e2c72f",
+ "miner": "0x334391aa808257952a462d1475562ee2106a6c90",
+ "mixHash": "0x64bb70b8ca883cadb8fbbda2c70a861612407864089ed87b98e5de20acceada6",
+ "nonce": "0x684129f283aaef18",
+ "number": "11494",
+ "stateRoot": "0x7057f31fe3dab1d620771adad35224aae43eb70e94861208bc84c557ff5b9d10",
+ "timestamp": "1479735912",
+ "totalDifficulty": "90744064339"
+ },
+ "input": "0xf889448504a817c800832dc6c094269296dddce321a6bcbaa2f0181127593d732cba80a47065cb480000000000000000000000001523e55a1ca4efbae03355775ae89f8d7699ad9e29a080ed81e4c5e9971a730efab4885566e2c868cd80bd4166d0ed8c287fdf181650a069d7c49215e3d4416ad239cd09dbb71b9f04c16b33b385d14f40b618a7a65115",
+ "result": {
+ "calls": [
+ {
+ "calls": [
+ {
+ "from": "0x13204f5d64c28326fd7bd05fd4ea855302d7f2ff",
+ "gas": "0x2bf459",
+ "gasUsed": "0x2aa",
+ "input": "0x7d65837a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a529806c67cc6486d4d62024471772f47f6fd672",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x42b02b5deeb78f34cd5ac896473b63e6c99a71a2",
+ "type": "DELEGATECALL"
+ }
+ ],
+ "from": "0x269296dddce321a6bcbaa2f0181127593d732cba",
+ "gas": "0x2cae73",
+ "gasUsed": "0xa9d",
+ "input": "0x5dbe47e8000000000000000000000000a529806c67cc6486d4d62024471772f47f6fd672",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x13204f5d64c28326fd7bd05fd4ea855302d7f2ff",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "from": "0xa529806c67cc6486d4d62024471772f47f6fd672",
+ "gas": "0x2d6e28",
+ "gasUsed": "0x64bd",
+ "input": "0x7065cb480000000000000000000000001523e55a1ca4efbae03355775ae89f8d7699ad9e",
+ "output": "0x",
+ "to": "0x269296dddce321a6bcbaa2f0181127593d732cba",
+ "type": "CALL",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_inner_create_oog_outer_throw.json b/eth/tracers/testdata/call_tracer_inner_create_oog_outer_throw.json
new file mode 100644
index 0000000..d1c8ac9
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_inner_create_oog_outer_throw.json
@@ -0,0 +1,77 @@
+{
+ "context": {
+ "difficulty": "3451177886",
+ "gasLimit": "4709286",
+ "miner": "0x1585936b53834b021f68cc13eeefdec2efc8e724",
+ "number": "2290744",
+ "timestamp": "1513616439"
+ },
+ "genesis": {
+ "alloc": {
+ "0x1d3ddf7caf024f253487e18bc4a15b1a360c170a": {
+ "balance": "0x0",
+ "code": "0x606060405263ffffffff60e060020a6000350416633b91f50681146100505780635bb47808146100715780635f51fca01461008c578063bc7647a9146100ad578063f1bd0d7a146100c8575b610000565b346100005761006f600160a060020a03600435811690602435166100e9565b005b346100005761006f600160a060020a0360043516610152565b005b346100005761006f600160a060020a036004358116906024351661019c565b005b346100005761006f600160a060020a03600435166101fa565b005b346100005761006f600160a060020a0360043581169060243516610db8565b005b600160a060020a038083166000908152602081905260408120549091908116903316811461011657610000565b839150600160a060020a038316151561012d573392505b6101378284610e2e565b6101418284610db8565b61014a826101fa565b5b5b50505050565b600154600160a060020a03908116903316811461016e57610000565b6002805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0384161790555b5b5050565b600254600160a060020a0390811690331681146101b857610000565b600160a060020a038381166000908152602081905260409020805473ffffffffffffffffffffffffffffffffffffffff19169184169190911790555b5b505050565b6040805160e260020a631a481fc102815260016024820181905260026044830152606482015262093a8060848201819052600060a4830181905260c06004840152601e60c48401527f736574456e7469747953746174757328616464726573732c75696e743829000060e484015292519091600160a060020a038516916369207f049161010480820192879290919082900301818387803b156100005760325a03f1156100005750506040805160e260020a63379938570281526000602482018190526001604483015260606004830152602360648301527f626567696e506f6c6c28616464726573732c75696e7436342c626f6f6c2c626f60848301527f6f6c29000000000000000000000000000000000000000000000000000000000060a48301529151600160a060020a038716935063de64e15c9260c48084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a631a481fc102815260016024820181905260026044830152606482015267ffffffffffffffff8416608482015260ff851660a482015260c06004820152601960c48201527f61646453746f636b28616464726573732c75696e74323536290000000000000060e48201529051600160a060020a03861692506369207f04916101048082019260009290919082900301818387803b156100005760325a03f1156100005750506040805160e260020a631a481fc102815260016024820181905260026044830152606482015267ffffffffffffffff8416608482015260ff851660a482015260c06004820152601960c48201527f697373756553746f636b2875696e74382c75696e74323536290000000000000060e48201529051600160a060020a03861692506369207f04916101048082019260009290919082900301818387803b156100005760325a03f1156100005750506040805160e260020a63379938570281526002602482015260006044820181905260606004830152602160648301527f6772616e7453746f636b2875696e74382c75696e743235362c61646472657373608483015260f860020a60290260a48301529151600160a060020a038716935063de64e15c9260c48084019391929182900301818387803b156100005760325a03f115610000575050604080517f010555b8000000000000000000000000000000000000000000000000000000008152600160a060020a03338116602483015260006044830181905260606004840152603c60648401527f6772616e7456657374656453746f636b2875696e74382c75696e743235362c6160848401527f6464726573732c75696e7436342c75696e7436342c75696e743634290000000060a48401529251908716935063010555b89260c48084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a631a481fc102815260016024820181905260026044830152606482015267ffffffffffffffff8416608482015260ff851660a482015260c06004820152601260c48201527f626567696e53616c65286164647265737329000000000000000000000000000060e48201529051600160a060020a03861692506369207f04916101048082019260009290919082900301818387803b156100005760325a03f1156100005750506040805160e260020a63379938570281526002602482015260006044820181905260606004830152601a60648301527f7472616e7366657253616c6546756e64732875696e743235362900000000000060848301529151600160a060020a038716935063de64e15c9260a48084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a631a481fc102815260016024820181905260026044830152606482015267ffffffffffffffff8416608482015260ff851660a482015260c06004820152602d60c48201527f7365744163636f756e74696e6753657474696e67732875696e743235362c756960e48201527f6e7436342c75696e7432353629000000000000000000000000000000000000006101048201529051600160a060020a03861692506369207f04916101248082019260009290919082900301818387803b156100005760325a03f1156100005750506040805160e260020a63379938570281526002602482015260006044820181905260606004830152603460648301527f637265617465526563757272696e6752657761726428616464726573732c756960848301527f6e743235362c75696e7436342c737472696e672900000000000000000000000060a48301529151600160a060020a038716935063de64e15c9260c48084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a63379938570281526002602482015260006044820181905260606004830152601b60648301527f72656d6f7665526563757272696e675265776172642875696e7429000000000060848301529151600160a060020a038716935063de64e15c9260a48084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a63379938570281526002602482015260006044820181905260606004830152602360648301527f697373756552657761726428616464726573732c75696e743235362c7374726960848301527f6e6729000000000000000000000000000000000000000000000000000000000060a48301529151600160a060020a038716935063de64e15c9260c48084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a6337993857028152600160248201819052604482015260606004820152602260648201527f61737369676e53746f636b2875696e74382c616464726573732c75696e743235608482015260f060020a6136290260a48201529051600160a060020a038616925063de64e15c9160c48082019260009290919082900301818387803b156100005760325a03f1156100005750506040805160e260020a6337993857028152600160248201819052604482015260606004820152602260648201527f72656d6f766553746f636b2875696e74382c616464726573732c75696e743235608482015260f060020a6136290260a48201529051600160a060020a038616925063de64e15c9160c48082019260009290919082900301818387803b156100005760325a03f1156100005750506040805160e260020a631a481fc102815260026024808301919091526003604483015260006064830181905267ffffffffffffffff8616608484015260ff871660a484015260c0600484015260c48301919091527f7365744164647265737342796c617728737472696e672c616464726573732c6260e48301527f6f6f6c29000000000000000000000000000000000000000000000000000000006101048301529151600160a060020a03871693506369207f04926101248084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a631a481fc1028152600260248201526003604482015260006064820181905267ffffffffffffffff8516608483015260ff861660a483015260c06004830152602160c48301527f73657453746174757342796c617728737472696e672c75696e74382c626f6f6c60e483015260f860020a6029026101048301529151600160a060020a03871693506369207f04926101248084019391929182900301818387803b156100005760325a03f1156100005750506040805160e260020a631a481fc1028152600260248201526003604482015260006064820181905267ffffffffffffffff8516608483015260ff861660a483015260c06004830152603860c48301527f736574566f74696e6742796c617728737472696e672c75696e743235362c756960e48301527f6e743235362c626f6f6c2c75696e7436342c75696e74382900000000000000006101048301529151600160a060020a03871693506369207f04926101248084019391929182900301818387803b156100005760325a03f115610000575050505b505050565b604080517f225553a4000000000000000000000000000000000000000000000000000000008152600160a060020a0383811660048301526002602483015291519184169163225553a49160448082019260009290919082900301818387803b156100005760325a03f115610000575050505b5050565b600082604051611fd280610f488339600160a060020a03909216910190815260405190819003602001906000f0801561000057905082600160a060020a03166308b027418260016040518363ffffffff1660e060020a0281526004018083600160a060020a0316600160a060020a0316815260200182815260200192505050600060405180830381600087803b156100005760325a03f115610000575050604080517fa14e3ee300000000000000000000000000000000000000000000000000000000815260006004820181905260016024830152600160a060020a0386811660448401529251928716935063a14e3ee39260648084019382900301818387803b156100005760325a03f115610000575050505b5050505600606060405234620000005760405160208062001fd283398101604052515b805b600a8054600160a060020a031916600160a060020a0383161790555b506001600d819055600e81905560408051808201909152600c8082527f566f74696e672053746f636b00000000000000000000000000000000000000006020928301908152600b805460008290528251601860ff1990911617825590947f0175b7a638427703f0dbe7bb9bbf987a2551717b34e79f33b5b1008d1fa01db9600291831615610100026000190190921604601f0193909304830192906200010c565b828001600101855582156200010c579182015b828111156200010c578251825591602001919060010190620000ef565b5b50620001309291505b808211156200012c576000815560010162000116565b5090565b50506040805180820190915260038082527f43565300000000000000000000000000000000000000000000000000000000006020928301908152600c805460008290528251600660ff1990911617825590937fdf6966c971051c3d54ec59162606531493a51404a002842f56009d7e5cf4a8c760026001841615610100026000190190931692909204601f010481019291620001f7565b82800160010185558215620001f7579182015b82811115620001f7578251825591602001919060010190620001da565b5b506200021b9291505b808211156200012c576000815560010162000116565b5090565b50505b505b611da280620002306000396000f3006060604052361561019a5763ffffffff60e060020a600035041662e1986d811461019f57806302a72a4c146101d657806306eb4e421461020157806306fdde0314610220578063095ea7b3146102ad578063158ccb99146102dd57806318160ddd146102f85780631cf65a781461031757806323b872dd146103365780632c71e60a1461036c57806333148fd6146103ca578063435ebc2c146103f55780635eeb6e451461041e578063600e85b71461043c5780636103d70b146104a157806362c1e46a146104b05780636c182e99146104ba578063706dc87c146104f057806370a082311461052557806377174f851461055057806395d89b411461056f578063a7771ee3146105fc578063a9059cbb14610629578063ab377daa14610659578063b25dbb5e14610685578063b89a73cb14610699578063ca5eb5e1146106c6578063cbcf2e5a146106e1578063d21f05ba1461070e578063d347c2051461072d578063d96831e114610765578063dd62ed3e14610777578063df3c211b146107a8578063e2982c21146107d6578063eb944e4c14610801575b610000565b34610000576101d4600160a060020a036004351660243567ffffffffffffffff6044358116906064358116906084351661081f565b005b34610000576101ef600160a060020a0360043516610a30565b60408051918252519081900360200190f35b34610000576101ef610a4f565b60408051918252519081900360200190f35b346100005761022d610a55565b604080516020808252835181830152835191928392908301918501908083838215610273575b80518252602083111561027357601f199092019160209182019101610253565b505050905090810190601f16801561029f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34610000576102c9600160a060020a0360043516602435610ae3565b604080519115158252519081900360200190f35b34610000576101d4600160a060020a0360043516610b4e565b005b34610000576101ef610b89565b60408051918252519081900360200190f35b34610000576101ef610b8f565b60408051918252519081900360200190f35b34610000576102c9600160a060020a0360043581169060243516604435610b95565b604080519115158252519081900360200190f35b3461000057610388600160a060020a0360043516602435610bb7565b60408051600160a060020a039096168652602086019490945267ffffffffffffffff928316858501529082166060850152166080830152519081900360a00190f35b34610000576101ef600160a060020a0360043516610c21565b60408051918252519081900360200190f35b3461000057610402610c40565b60408051600160a060020a039092168252519081900360200190f35b34610000576101d4600160a060020a0360043516602435610c4f565b005b3461000057610458600160a060020a0360043516602435610cc9565b60408051600160a060020a03909716875260208701959095528585019390935267ffffffffffffffff9182166060860152811660808501521660a0830152519081900360c00190f35b34610000576101d4610d9e565b005b6101d4610e1e565b005b34610000576104d3600160a060020a0360043516610e21565b6040805167ffffffffffffffff9092168252519081900360200190f35b3461000057610402600160a060020a0360043516610ead565b60408051600160a060020a039092168252519081900360200190f35b34610000576101ef600160a060020a0360043516610ef9565b60408051918252519081900360200190f35b34610000576101ef610f18565b60408051918252519081900360200190f35b346100005761022d610f1e565b604080516020808252835181830152835191928392908301918501908083838215610273575b80518252602083111561027357601f199092019160209182019101610253565b505050905090810190601f16801561029f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34610000576102c9600160a060020a0360043516610fac565b604080519115158252519081900360200190f35b34610000576102c9600160a060020a0360043516602435610fc2565b604080519115158252519081900360200190f35b3461000057610402600435610fe2565b60408051600160a060020a039092168252519081900360200190f35b34610000576101d46004351515610ffd565b005b34610000576102c9600160a060020a036004351661104c565b604080519115158252519081900360200190f35b34610000576101d4600160a060020a0360043516611062565b005b34610000576102c9600160a060020a0360043516611070565b604080519115158252519081900360200190f35b34610000576101ef6110f4565b60408051918252519081900360200190f35b34610000576101ef600160a060020a036004351667ffffffffffffffff602435166110fa565b60408051918252519081900360200190f35b34610000576101d4600435611121565b005b34610000576101ef600160a060020a03600435811690602435166111c6565b60408051918252519081900360200190f35b34610000576101ef6004356024356044356064356084356111f3565b60408051918252519081900360200190f35b34610000576101ef600160a060020a036004351661128c565b60408051918252519081900360200190f35b34610000576101d4600160a060020a036004351660243561129e565b005b6040805160a08101825260008082526020820181905291810182905260608101829052608081019190915267ffffffffffffffff848116908416101561086457610000565b8367ffffffffffffffff168267ffffffffffffffff16101561088557610000565b8267ffffffffffffffff168267ffffffffffffffff1610156108a657610000565b506040805160a081018252600160a060020a033381168252602080830188905267ffffffffffffffff80871684860152858116606085015287166080840152908816600090815260039091529190912080546001810180835582818380158290116109615760030281600302836000526020600020918201910161096191905b8082111561095d578054600160a060020a031916815560006001820155600281018054600160c060020a0319169055600301610926565b5090565b5b505050916000526020600020906003020160005b5082518154600160a060020a031916600160a060020a03909116178155602083015160018201556040830151600290910180546060850151608086015167ffffffffffffffff1990921667ffffffffffffffff948516176fffffffffffffffff00000000000000001916604060020a918516919091021777ffffffffffffffff000000000000000000000000000000001916608060020a939091169290920291909117905550610a268686610fc2565b505b505050505050565b600160a060020a0381166000908152600360205260409020545b919050565b60055481565b600b805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610adb5780601f10610ab057610100808354040283529160200191610adb565b820191906000526020600020905b815481529060010190602001808311610abe57829003601f168201915b505050505081565b600160a060020a03338116600081815260026020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a35060015b92915050565b600a5433600160a060020a03908116911614610b6957610000565b600a8054600160a060020a031916600160a060020a0383161790555b5b50565b60005481565b60005b90565b6000610ba2848484611600565b610bad8484846116e2565b90505b9392505050565b600360205281600052604060002081815481101561000057906000526020600020906003020160005b5080546001820154600290920154600160a060020a03909116935090915067ffffffffffffffff80821691604060020a8104821691608060020a9091041685565b600160a060020a0381166000908152600860205260409020545b919050565b600a54600160a060020a031681565b600a5433600160a060020a03908116911614610c6a57610000565b610c7660005482611714565b6000908155600160a060020a038316815260016020526040902054610c9b9082611714565b600160a060020a038316600090815260016020526040812091909155610cc390839083611600565b5b5b5050565b6000600060006000600060006000600360008a600160a060020a0316600160a060020a0316815260200190815260200160002088815481101561000057906000526020600020906003020160005b508054600182015460028301546040805160a081018252600160a060020a039094168085526020850184905267ffffffffffffffff808416928601839052604060020a8404811660608701819052608060020a9094041660808601819052909c50929a509197509095509350909150610d90904261172d565b94505b509295509295509295565b33600160a060020a038116600090815260066020526040902054801515610dc457610000565b8030600160a060020a0316311015610ddb57610000565b600160a060020a0382166000818152600660205260408082208290555183156108fc0291849190818181858888f193505050501515610cc357610000565b5b5050565b5b565b600160a060020a03811660009081526003602052604081205442915b81811015610ea557600160a060020a03841660009081526003602052604090208054610e9a9190839081101561000057906000526020600020906003020160005b5060020154604060020a900467ffffffffffffffff168461177d565b92505b600101610e3d565b5b5050919050565b600160a060020a0380821660009081526007602052604081205490911615610eef57600160a060020a0380831660009081526007602052604090205416610ef1565b815b90505b919050565b600160a060020a0381166000908152600160205260409020545b919050565b600d5481565b600c805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610adb5780601f10610ab057610100808354040283529160200191610adb565b820191906000526020600020905b815481529060010190602001808311610abe57829003601f168201915b505050505081565b60006000610fb983610c21565b1190505b919050565b6000610fcf338484611600565b610fd983836117ac565b90505b92915050565b600460205260009081526040902054600160a060020a031681565b8015801561101a575061100f33610ef9565b61101833610c21565b115b1561102457610000565b33600160a060020a03166000908152600960205260409020805460ff19168215151790555b50565b60006000610fb983610ef9565b1190505b919050565b610b8533826117dc565b5b50565b600a54604080516000602091820181905282517fcbcf2e5a000000000000000000000000000000000000000000000000000000008152600160a060020a03868116600483015293519194939093169263cbcf2e5a92602480830193919282900301818787803b156100005760325a03f115610000575050604051519150505b919050565b600e5481565b6000610fd961110984846118b2565b61111385856119b6565b611a05565b90505b92915050565b600a5433600160a060020a0390811691161461113c57610000565b61114860005482611a1f565b600055600554600190101561116c57600a5461116c90600160a060020a0316611a47565b5b600a54600160a060020a03166000908152600160205260409020546111929082611a1f565b600a8054600160a060020a039081166000908152600160205260408120939093559054610b8592911683611600565b5b5b50565b600160a060020a038083166000908152600260209081526040808320938516835292905220545b92915050565b6000600060008487101561120a5760009250611281565b8387111561121a57879250611281565b61123f6112308961122b888a611714565b611a90565b61123a8689611714565b611abc565b915081925061124e8883611714565b905061127e8361127961126a8461122b8c8b611714565b611a90565b61123a888b611714565b611abc565b611a1f565b92505b505095945050505050565b60066020526000908152604090205481565b600160a060020a03821660009081526003602052604081208054829190849081101561000057906000526020600020906003020160005b50805490925033600160a060020a039081169116146112f357610000565b6040805160a0810182528354600160a060020a0316815260018401546020820152600284015467ffffffffffffffff80821693830193909352604060020a810483166060830152608060020a900490911660808201526113539042611af9565b600160a060020a0385166000908152600360205260409020805491925090849081101561000057906000526020600020906003020160005b508054600160a060020a031916815560006001820181905560029091018054600160c060020a0319169055600160a060020a0385168152600360205260409020805460001981019081101561000057906000526020600020906003020160005b50600160a060020a03851660009081526003602052604090208054859081101561000057906000526020600020906003020160005b5081548154600160a060020a031916600160a060020a03918216178255600180840154908301556002928301805493909201805467ffffffffffffffff191667ffffffffffffffff948516178082558354604060020a908190048616026fffffffffffffffff000000000000000019909116178082559254608060020a9081900490941690930277ffffffffffffffff00000000000000000000000000000000199092169190911790915584166000908152600360205260409020805460001981018083559190829080158290116115485760030281600302836000526020600020918201910161154891905b8082111561095d578054600160a060020a031916815560006001820155600281018054600160c060020a0319169055600301610926565b5090565b5b505050600160a060020a033316600090815260016020526040902054611570915082611a1f565b600160a060020a03338116600090815260016020526040808220939093559086168152205461159f9082611714565b600160a060020a038086166000818152600160209081526040918290209490945580518581529051339093169391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a35b50505050565b600160a060020a0383161561166e576116466008600061161f86610ead565b600160a060020a0316600160a060020a031681526020019081526020016000205482611714565b6008600061165386610ead565b600160a060020a031681526020810191909152604001600020555b600160a060020a038216156116dc576116b46008600061168d85610ead565b600160a060020a0316600160a060020a031681526020019081526020016000205482611a1f565b600860006116c185610ead565b600160a060020a031681526020810191909152604001600020555b5b505050565b600083826116f082426110fa565b8111156116fc57610000565b611707868686611b1b565b92505b5b50509392505050565b600061172283831115611b4d565b508082035b92915050565b6000610fd983602001518367ffffffffffffffff16856080015167ffffffffffffffff16866040015167ffffffffffffffff16876060015167ffffffffffffffff166111f3565b90505b92915050565b60008167ffffffffffffffff168367ffffffffffffffff1610156117a15781610fd9565b825b90505b92915050565b600033826117ba82426110fa565b8111156117c657610000565b6117d08585611b5d565b92505b5b505092915050565b6117e582610ef9565b6117ee83610c21565b11156117f957610000565b600160a060020a03811660009081526009602052604090205460ff16158015611834575081600160a060020a031681600160a060020a031614155b1561183e57610000565b61184782611070565b1561185157610000565b611864828261185f85610ef9565b611600565b600160a060020a0382811660009081526007602052604090208054600160a060020a031916918316918217905561189a82610ead565b600160a060020a031614610cc357610000565b5b5050565b600160a060020a038216600090815260036020526040812054815b818110156119885761197d836112796003600089600160a060020a0316600160a060020a0316815260200190815260200160002084815481101561000057906000526020600020906003020160005b506040805160a0810182528254600160a060020a031681526001830154602082015260029092015467ffffffffffffffff80821692840192909252604060020a810482166060840152608060020a900416608082015287611af9565b611a1f565b92505b6001016118cd565b600160a060020a0385166000908152600160205260409020546117d09084611714565b92505b505092915050565b600060006119c384611070565b80156119d157506000600d54115b90506119fb816119e9576119e485610ef9565b6119ec565b60005b6111138686611b7b565b611a05565b91505b5092915050565b60008183106117a15781610fd9565b825b90505b92915050565b6000828201611a3c848210801590611a375750838210155b611b4d565b8091505b5092915050565b611a508161104c565b15611a5a57610b85565b6005805460009081526004602052604090208054600160a060020a031916600160a060020a038416179055805460010190555b50565b6000828202611a3c841580611a37575083858381156100005704145b611b4d565b8091505b5092915050565b60006000611acc60008411611b4d565b8284811561000057049050611a3c838581156100005706828502018514611b4d565b8091505b5092915050565b6000610fd98360200151611b0d858561172d565b611714565b90505b92915050565b60008382611b2982426110fa565b811115611b3557610000565b611707868686611b8f565b92505b5b50509392505050565b801515610b8557610000565b5b50565b6000611b6883611a47565b610fd98383611c92565b90505b92915050565b6000610fd983610ef9565b90505b92915050565b600160a060020a038084166000908152600260209081526040808320338516845282528083205493861683526001909152812054909190611bd09084611a1f565b600160a060020a038086166000908152600160205260408082209390935590871681522054611bff9084611714565b600160a060020a038616600090815260016020526040902055611c228184611714565b600160a060020a038087166000818152600260209081526040808320338616845282529182902094909455805187815290519288169391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a3600191505b509392505050565b60003382611ca082426110fa565b811115611cac57610000565b6117d08585611cc2565b92505b5b505092915050565b600160a060020a033316600090815260016020526040812054611ce59083611714565b600160a060020a033381166000908152600160205260408082209390935590851681522054611d149083611a1f565b600160a060020a038085166000818152600160209081526040918290209490945580518681529051919333909316927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a35060015b929150505600a165627a7a72305820bfa5ddd3fecf3f43aed25385ec7ec3ef79638c2e58d99f85d9a3cc494183bf160029a165627a7a723058200e78a5f7e0f91739035d0fbf5eca02f79377210b722f63431f29a22e2880b3bd0029",
+ "nonce": "789",
+ "storage": {
+ "0xfe9ec0542a1c009be8b1f3acf43af97100ffff42eb736850fb038fa1151ad4d9": "0x000000000000000000000000e4a13bc304682a903e9472f469c33801dd18d9e8"
+ }
+ },
+ "0x5cb4a6b902fcb21588c86c3517e797b07cdaadb9": {
+ "balance": "0x0",
+ "code": "0x",
+ "nonce": "0",
+ "storage": {}
+ },
+ "0xe4a13bc304682a903e9472f469c33801dd18d9e8": {
+ "balance": "0x33c763c929f62c4f",
+ "code": "0x",
+ "nonce": "14",
+ "storage": {}
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "3451177886",
+ "extraData": "0x4554482e45544846414e532e4f52472d4641313738394444",
+ "gasLimit": "4713874",
+ "hash": "0x5d52a672417cd1269bf4f7095e25dcbf837747bba908cd5ef809dc1bd06144b5",
+ "miner": "0xbbf5029fd710d227630c8b7d338051b8e76d50b3",
+ "mixHash": "0x01a12845ed546b94a038a7a03e8df8d7952024ed41ccb3db7a7ade4abc290ce1",
+ "nonce": "0x28c446f1cb9748c1",
+ "number": "2290743",
+ "stateRoot": "0x4898aceede76739daef76448a367d10015a2c022c9e7909b99a10fbf6fb16708",
+ "timestamp": "1513616414",
+ "totalDifficulty": "7146523769022564"
+ },
+ "input": "0xf8aa0e8509502f9000830493e0941d3ddf7caf024f253487e18bc4a15b1a360c170a80b8443b91f506000000000000000000000000a14bdd7e5666d784dcce98ad24d383a6b1cd4182000000000000000000000000e4a13bc304682a903e9472f469c33801dd18d9e829a0524564944fa419f5c189b5074044f89210c6d6b2d77ee8f7f12a927d59b636dfa0015b28986807a424b18b186ee6642d76739df36cad802d20e8c00e79a61d7281",
+ "result": {
+ "calls": [
+ {
+ "error": "internal failure",
+ "from": "0x1d3ddf7caf024f253487e18bc4a15b1a360c170a",
+ "gas": "0x39ff0",
+ "gasUsed": "0x39ff0",
+ "input": "0x606060405234620000005760405160208062001fd283398101604052515b805b600a8054600160a060020a031916600160a060020a0383161790555b506001600d819055600e81905560408051808201909152600c8082527f566f74696e672053746f636b00000000000000000000000000000000000000006020928301908152600b805460008290528251601860ff1990911617825590947f0175b7a638427703f0dbe7bb9bbf987a2551717b34e79f33b5b1008d1fa01db9600291831615610100026000190190921604601f0193909304830192906200010c565b828001600101855582156200010c579182015b828111156200010c578251825591602001919060010190620000ef565b5b50620001309291505b808211156200012c576000815560010162000116565b5090565b50506040805180820190915260038082527f43565300000000000000000000000000000000000000000000000000000000006020928301908152600c805460008290528251600660ff1990911617825590937fdf6966c971051c3d54ec59162606531493a51404a002842f56009d7e5cf4a8c760026001841615610100026000190190931692909204601f010481019291620001f7565b82800160010185558215620001f7579182015b82811115620001f7578251825591602001919060010190620001da565b5b506200021b9291505b808211156200012c576000815560010162000116565b5090565b50505b505b611da280620002306000396000f3006060604052361561019a5763ffffffff60e060020a600035041662e1986d811461019f57806302a72a4c146101d657806306eb4e421461020157806306fdde0314610220578063095ea7b3146102ad578063158ccb99146102dd57806318160ddd146102f85780631cf65a781461031757806323b872dd146103365780632c71e60a1461036c57806333148fd6146103ca578063435ebc2c146103f55780635eeb6e451461041e578063600e85b71461043c5780636103d70b146104a157806362c1e46a146104b05780636c182e99146104ba578063706dc87c146104f057806370a082311461052557806377174f851461055057806395d89b411461056f578063a7771ee3146105fc578063a9059cbb14610629578063ab377daa14610659578063b25dbb5e14610685578063b89a73cb14610699578063ca5eb5e1146106c6578063cbcf2e5a146106e1578063d21f05ba1461070e578063d347c2051461072d578063d96831e114610765578063dd62ed3e14610777578063df3c211b146107a8578063e2982c21146107d6578063eb944e4c14610801575b610000565b34610000576101d4600160a060020a036004351660243567ffffffffffffffff6044358116906064358116906084351661081f565b005b34610000576101ef600160a060020a0360043516610a30565b60408051918252519081900360200190f35b34610000576101ef610a4f565b60408051918252519081900360200190f35b346100005761022d610a55565b604080516020808252835181830152835191928392908301918501908083838215610273575b80518252602083111561027357601f199092019160209182019101610253565b505050905090810190601f16801561029f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34610000576102c9600160a060020a0360043516602435610ae3565b604080519115158252519081900360200190f35b34610000576101d4600160a060020a0360043516610b4e565b005b34610000576101ef610b89565b60408051918252519081900360200190f35b34610000576101ef610b8f565b60408051918252519081900360200190f35b34610000576102c9600160a060020a0360043581169060243516604435610b95565b604080519115158252519081900360200190f35b3461000057610388600160a060020a0360043516602435610bb7565b60408051600160a060020a039096168652602086019490945267ffffffffffffffff928316858501529082166060850152166080830152519081900360a00190f35b34610000576101ef600160a060020a0360043516610c21565b60408051918252519081900360200190f35b3461000057610402610c40565b60408051600160a060020a039092168252519081900360200190f35b34610000576101d4600160a060020a0360043516602435610c4f565b005b3461000057610458600160a060020a0360043516602435610cc9565b60408051600160a060020a03909716875260208701959095528585019390935267ffffffffffffffff9182166060860152811660808501521660a0830152519081900360c00190f35b34610000576101d4610d9e565b005b6101d4610e1e565b005b34610000576104d3600160a060020a0360043516610e21565b6040805167ffffffffffffffff9092168252519081900360200190f35b3461000057610402600160a060020a0360043516610ead565b60408051600160a060020a039092168252519081900360200190f35b34610000576101ef600160a060020a0360043516610ef9565b60408051918252519081900360200190f35b34610000576101ef610f18565b60408051918252519081900360200190f35b346100005761022d610f1e565b604080516020808252835181830152835191928392908301918501908083838215610273575b80518252602083111561027357601f199092019160209182019101610253565b505050905090810190601f16801561029f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34610000576102c9600160a060020a0360043516610fac565b604080519115158252519081900360200190f35b34610000576102c9600160a060020a0360043516602435610fc2565b604080519115158252519081900360200190f35b3461000057610402600435610fe2565b60408051600160a060020a039092168252519081900360200190f35b34610000576101d46004351515610ffd565b005b34610000576102c9600160a060020a036004351661104c565b604080519115158252519081900360200190f35b34610000576101d4600160a060020a0360043516611062565b005b34610000576102c9600160a060020a0360043516611070565b604080519115158252519081900360200190f35b34610000576101ef6110f4565b60408051918252519081900360200190f35b34610000576101ef600160a060020a036004351667ffffffffffffffff602435166110fa565b60408051918252519081900360200190f35b34610000576101d4600435611121565b005b34610000576101ef600160a060020a03600435811690602435166111c6565b60408051918252519081900360200190f35b34610000576101ef6004356024356044356064356084356111f3565b60408051918252519081900360200190f35b34610000576101ef600160a060020a036004351661128c565b60408051918252519081900360200190f35b34610000576101d4600160a060020a036004351660243561129e565b005b6040805160a08101825260008082526020820181905291810182905260608101829052608081019190915267ffffffffffffffff848116908416101561086457610000565b8367ffffffffffffffff168267ffffffffffffffff16101561088557610000565b8267ffffffffffffffff168267ffffffffffffffff1610156108a657610000565b506040805160a081018252600160a060020a033381168252602080830188905267ffffffffffffffff80871684860152858116606085015287166080840152908816600090815260039091529190912080546001810180835582818380158290116109615760030281600302836000526020600020918201910161096191905b8082111561095d578054600160a060020a031916815560006001820155600281018054600160c060020a0319169055600301610926565b5090565b5b505050916000526020600020906003020160005b5082518154600160a060020a031916600160a060020a03909116178155602083015160018201556040830151600290910180546060850151608086015167ffffffffffffffff1990921667ffffffffffffffff948516176fffffffffffffffff00000000000000001916604060020a918516919091021777ffffffffffffffff000000000000000000000000000000001916608060020a939091169290920291909117905550610a268686610fc2565b505b505050505050565b600160a060020a0381166000908152600360205260409020545b919050565b60055481565b600b805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610adb5780601f10610ab057610100808354040283529160200191610adb565b820191906000526020600020905b815481529060010190602001808311610abe57829003601f168201915b505050505081565b600160a060020a03338116600081815260026020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a35060015b92915050565b600a5433600160a060020a03908116911614610b6957610000565b600a8054600160a060020a031916600160a060020a0383161790555b5b50565b60005481565b60005b90565b6000610ba2848484611600565b610bad8484846116e2565b90505b9392505050565b600360205281600052604060002081815481101561000057906000526020600020906003020160005b5080546001820154600290920154600160a060020a03909116935090915067ffffffffffffffff80821691604060020a8104821691608060020a9091041685565b600160a060020a0381166000908152600860205260409020545b919050565b600a54600160a060020a031681565b600a5433600160a060020a03908116911614610c6a57610000565b610c7660005482611714565b6000908155600160a060020a038316815260016020526040902054610c9b9082611714565b600160a060020a038316600090815260016020526040812091909155610cc390839083611600565b5b5b5050565b6000600060006000600060006000600360008a600160a060020a0316600160a060020a0316815260200190815260200160002088815481101561000057906000526020600020906003020160005b508054600182015460028301546040805160a081018252600160a060020a039094168085526020850184905267ffffffffffffffff808416928601839052604060020a8404811660608701819052608060020a9094041660808601819052909c50929a509197509095509350909150610d90904261172d565b94505b509295509295509295565b33600160a060020a038116600090815260066020526040902054801515610dc457610000565b8030600160a060020a0316311015610ddb57610000565b600160a060020a0382166000818152600660205260408082208290555183156108fc0291849190818181858888f193505050501515610cc357610000565b5b5050565b5b565b600160a060020a03811660009081526003602052604081205442915b81811015610ea557600160a060020a03841660009081526003602052604090208054610e9a9190839081101561000057906000526020600020906003020160005b5060020154604060020a900467ffffffffffffffff168461177d565b92505b600101610e3d565b5b5050919050565b600160a060020a0380821660009081526007602052604081205490911615610eef57600160a060020a0380831660009081526007602052604090205416610ef1565b815b90505b919050565b600160a060020a0381166000908152600160205260409020545b919050565b600d5481565b600c805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610adb5780601f10610ab057610100808354040283529160200191610adb565b820191906000526020600020905b815481529060010190602001808311610abe57829003601f168201915b505050505081565b60006000610fb983610c21565b1190505b919050565b6000610fcf338484611600565b610fd983836117ac565b90505b92915050565b600460205260009081526040902054600160a060020a031681565b8015801561101a575061100f33610ef9565b61101833610c21565b115b1561102457610000565b33600160a060020a03166000908152600960205260409020805460ff19168215151790555b50565b60006000610fb983610ef9565b1190505b919050565b610b8533826117dc565b5b50565b600a54604080516000602091820181905282517fcbcf2e5a000000000000000000000000000000000000000000000000000000008152600160a060020a03868116600483015293519194939093169263cbcf2e5a92602480830193919282900301818787803b156100005760325a03f115610000575050604051519150505b919050565b600e5481565b6000610fd961110984846118b2565b61111385856119b6565b611a05565b90505b92915050565b600a5433600160a060020a0390811691161461113c57610000565b61114860005482611a1f565b600055600554600190101561116c57600a5461116c90600160a060020a0316611a47565b5b600a54600160a060020a03166000908152600160205260409020546111929082611a1f565b600a8054600160a060020a039081166000908152600160205260408120939093559054610b8592911683611600565b5b5b50565b600160a060020a038083166000908152600260209081526040808320938516835292905220545b92915050565b6000600060008487101561120a5760009250611281565b8387111561121a57879250611281565b61123f6112308961122b888a611714565b611a90565b61123a8689611714565b611abc565b915081925061124e8883611714565b905061127e8361127961126a8461122b8c8b611714565b611a90565b61123a888b611714565b611abc565b611a1f565b92505b505095945050505050565b60066020526000908152604090205481565b600160a060020a03821660009081526003602052604081208054829190849081101561000057906000526020600020906003020160005b50805490925033600160a060020a039081169116146112f357610000565b6040805160a0810182528354600160a060020a0316815260018401546020820152600284015467ffffffffffffffff80821693830193909352604060020a810483166060830152608060020a900490911660808201526113539042611af9565b600160a060020a0385166000908152600360205260409020805491925090849081101561000057906000526020600020906003020160005b508054600160a060020a031916815560006001820181905560029091018054600160c060020a0319169055600160a060020a0385168152600360205260409020805460001981019081101561000057906000526020600020906003020160005b50600160a060020a03851660009081526003602052604090208054859081101561000057906000526020600020906003020160005b5081548154600160a060020a031916600160a060020a03918216178255600180840154908301556002928301805493909201805467ffffffffffffffff191667ffffffffffffffff948516178082558354604060020a908190048616026fffffffffffffffff000000000000000019909116178082559254608060020a9081900490941690930277ffffffffffffffff00000000000000000000000000000000199092169190911790915584166000908152600360205260409020805460001981018083559190829080158290116115485760030281600302836000526020600020918201910161154891905b8082111561095d578054600160a060020a031916815560006001820155600281018054600160c060020a0319169055600301610926565b5090565b5b505050600160a060020a033316600090815260016020526040902054611570915082611a1f565b600160a060020a03338116600090815260016020526040808220939093559086168152205461159f9082611714565b600160a060020a038086166000818152600160209081526040918290209490945580518581529051339093169391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a35b50505050565b600160a060020a0383161561166e576116466008600061161f86610ead565b600160a060020a0316600160a060020a031681526020019081526020016000205482611714565b6008600061165386610ead565b600160a060020a031681526020810191909152604001600020555b600160a060020a038216156116dc576116b46008600061168d85610ead565b600160a060020a0316600160a060020a031681526020019081526020016000205482611a1f565b600860006116c185610ead565b600160a060020a031681526020810191909152604001600020555b5b505050565b600083826116f082426110fa565b8111156116fc57610000565b611707868686611b1b565b92505b5b50509392505050565b600061172283831115611b4d565b508082035b92915050565b6000610fd983602001518367ffffffffffffffff16856080015167ffffffffffffffff16866040015167ffffffffffffffff16876060015167ffffffffffffffff166111f3565b90505b92915050565b60008167ffffffffffffffff168367ffffffffffffffff1610156117a15781610fd9565b825b90505b92915050565b600033826117ba82426110fa565b8111156117c657610000565b6117d08585611b5d565b92505b5b505092915050565b6117e582610ef9565b6117ee83610c21565b11156117f957610000565b600160a060020a03811660009081526009602052604090205460ff16158015611834575081600160a060020a031681600160a060020a031614155b1561183e57610000565b61184782611070565b1561185157610000565b611864828261185f85610ef9565b611600565b600160a060020a0382811660009081526007602052604090208054600160a060020a031916918316918217905561189a82610ead565b600160a060020a031614610cc357610000565b5b5050565b600160a060020a038216600090815260036020526040812054815b818110156119885761197d836112796003600089600160a060020a0316600160a060020a0316815260200190815260200160002084815481101561000057906000526020600020906003020160005b506040805160a0810182528254600160a060020a031681526001830154602082015260029092015467ffffffffffffffff80821692840192909252604060020a810482166060840152608060020a900416608082015287611af9565b611a1f565b92505b6001016118cd565b600160a060020a0385166000908152600160205260409020546117d09084611714565b92505b505092915050565b600060006119c384611070565b80156119d157506000600d54115b90506119fb816119e9576119e485610ef9565b6119ec565b60005b6111138686611b7b565b611a05565b91505b5092915050565b60008183106117a15781610fd9565b825b90505b92915050565b6000828201611a3c848210801590611a375750838210155b611b4d565b8091505b5092915050565b611a508161104c565b15611a5a57610b85565b6005805460009081526004602052604090208054600160a060020a031916600160a060020a038416179055805460010190555b50565b6000828202611a3c841580611a37575083858381156100005704145b611b4d565b8091505b5092915050565b60006000611acc60008411611b4d565b8284811561000057049050611a3c838581156100005706828502018514611b4d565b8091505b5092915050565b6000610fd98360200151611b0d858561172d565b611714565b90505b92915050565b60008382611b2982426110fa565b811115611b3557610000565b611707868686611b8f565b92505b5b50509392505050565b801515610b8557610000565b5b50565b6000611b6883611a47565b610fd98383611c92565b90505b92915050565b6000610fd983610ef9565b90505b92915050565b600160a060020a038084166000908152600260209081526040808320338516845282528083205493861683526001909152812054909190611bd09084611a1f565b600160a060020a038086166000908152600160205260408082209390935590871681522054611bff9084611714565b600160a060020a038616600090815260016020526040902055611c228184611714565b600160a060020a038087166000818152600260209081526040808320338616845282529182902094909455805187815290519288169391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a3600191505b509392505050565b60003382611ca082426110fa565b811115611cac57610000565b6117d08585611cc2565b92505b5b505092915050565b600160a060020a033316600090815260016020526040812054611ce59083611714565b600160a060020a033381166000908152600160205260408082209390935590851681522054611d149083611a1f565b600160a060020a038085166000818152600160209081526040918290209490945580518681529051919333909316927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a35060015b929150505600a165627a7a72305820bfa5ddd3fecf3f43aed25385ec7ec3ef79638c2e58d99f85d9a3cc494183bf160029000000000000000000000000a14bdd7e5666d784dcce98ad24d383a6b1cd4182",
+ "type": "CREATE",
+ "value": "0x0"
+ }
+ ],
+ "error": "evm: invalid jump destination",
+ "from": "0xe4a13bc304682a903e9472f469c33801dd18d9e8",
+ "gas": "0x435c8",
+ "gasUsed": "0x435c8",
+ "input": "0x3b91f506000000000000000000000000a14bdd7e5666d784dcce98ad24d383a6b1cd4182000000000000000000000000e4a13bc304682a903e9472f469c33801dd18d9e8",
+ "to": "0x1d3ddf7caf024f253487e18bc4a15b1a360c170a",
+ "type": "CALL",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_inner_throw_outer_revert.json b/eth/tracers/testdata/call_tracer_inner_throw_outer_revert.json
new file mode 100644
index 0000000..edd80e5
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_inner_throw_outer_revert.json
@@ -0,0 +1,81 @@
+{
+ "context": {
+ "difficulty": "3956606365",
+ "gasLimit": "5413248",
+ "miner": "0x00d8ae40d9a06d0e7a2877b62e32eb959afbe16d",
+ "number": "2295104",
+ "timestamp": "1513681256"
+ },
+ "genesis": {
+ "alloc": {
+ "0x33056b5dcac09a9b4becad0e1dcf92c19bd0af76": {
+ "balance": "0x0",
+ "code": "0x60606040526004361061015e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680625b4487146101a257806311df9995146101cb578063278ecde11461022057806330adce0e146102435780633197cbb61461026c5780634bb278f3146102955780636103d70b146102aa57806363a599a4146102bf5780636a2d1cb8146102d457806375f12b21146102fd57806378e979251461032a578063801db9cc1461035357806386d1a69f1461037c5780638da5cb5b146103915780638ef26a71146103e65780639890220b1461040f5780639b39caef14610424578063b85dfb801461044d578063be9a6555146104a1578063ccb07cef146104b6578063d06c91e4146104e3578063d669e1d414610538578063df40503c14610561578063e2982c2114610576578063f02e030d146105c3578063f2fde38b146105d8578063f3283fba14610611575b600060149054906101000a900460ff1615151561017a57600080fd5b60075442108061018b575060085442115b15151561019757600080fd5b6101a03361064a565b005b34156101ad57600080fd5b6101b5610925565b6040518082815260200191505060405180910390f35b34156101d657600080fd5b6101de61092b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561022b57600080fd5b6102416004808035906020019091905050610951565b005b341561024e57600080fd5b610256610c48565b6040518082815260200191505060405180910390f35b341561027757600080fd5b61027f610c4e565b6040518082815260200191505060405180910390f35b34156102a057600080fd5b6102a8610c54565b005b34156102b557600080fd5b6102bd610f3e565b005b34156102ca57600080fd5b6102d261105d565b005b34156102df57600080fd5b6102e76110d5565b6040518082815260200191505060405180910390f35b341561030857600080fd5b6103106110e1565b604051808215151515815260200191505060405180910390f35b341561033557600080fd5b61033d6110f4565b6040518082815260200191505060405180910390f35b341561035e57600080fd5b6103666110fa565b6040518082815260200191505060405180910390f35b341561038757600080fd5b61038f611104565b005b341561039c57600080fd5b6103a4611196565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156103f157600080fd5b6103f96111bb565b6040518082815260200191505060405180910390f35b341561041a57600080fd5b6104226111c1565b005b341561042f57600080fd5b610437611296565b6040518082815260200191505060405180910390f35b341561045857600080fd5b610484600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061129c565b604051808381526020018281526020019250505060405180910390f35b34156104ac57600080fd5b6104b46112c0565b005b34156104c157600080fd5b6104c9611341565b604051808215151515815260200191505060405180910390f35b34156104ee57600080fd5b6104f6611354565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561054357600080fd5b61054b61137a565b6040518082815260200191505060405180910390f35b341561056c57600080fd5b610574611385565b005b341561058157600080fd5b6105ad600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506116c3565b6040518082815260200191505060405180910390f35b34156105ce57600080fd5b6105d66116db565b005b34156105e357600080fd5b61060f600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050611829565b005b341561061c57600080fd5b610648600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506118fe565b005b600080670de0b6b3a7640000341015151561066457600080fd5b61069b610696670de0b6b3a7640000610688610258346119d990919063ffffffff16565b611a0c90919063ffffffff16565b611a27565b9150660221b262dd80006106ba60065484611a7e90919063ffffffff16565b111515156106c757600080fd5b600a60008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000209050600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a9059cbb84846000604051602001526040518363ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200192505050602060405180830381600087803b15156107d557600080fd5b6102c65a03f115156107e657600080fd5b5050506040518051905050610808828260010154611a7e90919063ffffffff16565b8160010181905550610827348260000154611a7e90919063ffffffff16565b816000018190555061084434600554611a7e90919063ffffffff16565b60058190555061085f82600654611a7e90919063ffffffff16565b6006819055503373ffffffffffffffffffffffffffffffffffffffff167ff3c1c7c0eb1328ddc834c4c9e579c06d35f443bf1102b034653624a239c7a40c836040518082815260200191505060405180910390a27fd1dc370699ae69fb860ed754789a4327413ec1cd379b93f2cbedf449a26b0e8583600554604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a1505050565b60025481565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600060085442108061096b5750651b48eb57e00060065410155b15151561097757600080fd5b600a60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010154821415156109c757600080fd5b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166323b872dd3330856000604051602001526040518463ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019350505050602060405180830381600087803b1515610ac857600080fd5b6102c65a03f11515610ad957600080fd5b5050506040518051905050600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166342966c68836000604051602001526040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050602060405180830381600087803b1515610b7d57600080fd5b6102c65a03f11515610b8e57600080fd5b505050604051805190501515610ba357600080fd5b600a60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000015490506000600a60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001819055506000811115610c4457610c433382611a9c565b5b5050565b60055481565b60085481565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141515610cb157600080fd5b600854421015610cd357660221b262dd8000600654141515610cd257600080fd5b5b651b48eb57e000600654108015610cf057506213c6806008540142105b151515610cfc57600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc3073ffffffffffffffffffffffffffffffffffffffff16319081150290604051600060405180830381858888f193505050501515610d7557600080fd5b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166370a08231306000604051602001526040518263ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001915050602060405180830381600087803b1515610e3a57600080fd5b6102c65a03f11515610e4b57600080fd5b5050506040518051905090506000811115610f2057600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166342966c68826000604051602001526040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050602060405180830381600087803b1515610ef957600080fd5b6102c65a03f11515610f0a57600080fd5b505050604051805190501515610f1f57600080fd5b5b6001600960006101000a81548160ff02191690831515021790555050565b600080339150600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905060008114151515610f9657600080fd5b803073ffffffffffffffffffffffffffffffffffffffff163110151515610fbc57600080fd5b610fd181600254611b5090919063ffffffff16565b6002819055506000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561105957fe5b5050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156110b857600080fd5b6001600060146101000a81548160ff021916908315150217905550565b670de0b6b3a764000081565b600060149054906101000a900460ff1681565b60075481565b651b48eb57e00081565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561115f57600080fd5b600060149054906101000a900460ff16151561117a57600080fd5b60008060146101000a81548160ff021916908315150217905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60065481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561121c57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc3073ffffffffffffffffffffffffffffffffffffffff16319081150290604051600060405180830381858888f19350505050151561129457600080fd5b565b61025881565b600a6020528060005260406000206000915090508060000154908060010154905082565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561131b57600080fd5b600060075414151561132c57600080fd5b4260078190555062278d004201600881905550565b600960009054906101000a900460ff1681565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b660221b262dd800081565b60008060008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156113e557600080fd5b600654660221b262dd800003925061142b670de0b6b3a764000061141c610258670de0b6b3a76400006119d990919063ffffffff16565b81151561142557fe5b04611a27565b915081831115151561143c57600080fd5b600a60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000209050600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a9059cbb6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff16856000604051602001526040518363ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200182815260200192505050602060405180830381600087803b151561158c57600080fd5b6102c65a03f1151561159d57600080fd5b50505060405180519050506115bf838260010154611a7e90919063ffffffff16565b81600101819055506115dc83600654611a7e90919063ffffffff16565b6006819055503073ffffffffffffffffffffffffffffffffffffffff167ff3c1c7c0eb1328ddc834c4c9e579c06d35f443bf1102b034653624a239c7a40c846040518082815260200191505060405180910390a27fd1dc370699ae69fb860ed754789a4327413ec1cd379b93f2cbedf449a26b0e856000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600554604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a1505050565b60016020528060005260406000206000915090505481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561173657600080fd5b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663f2fde38b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff166040518263ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001915050600060405180830381600087803b151561181357600080fd5b6102c65a03f1151561182457600080fd5b505050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561188457600080fd5b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415156118fb57806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b50565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561195957600080fd5b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415151561199557600080fd5b80600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600080828402905060008414806119fa57508284828115156119f757fe5b04145b1515611a0257fe5b8091505092915050565b6000808284811515611a1a57fe5b0490508091505092915050565b6000611a416202a300600754611a7e90919063ffffffff16565b421015611a7557611a6e611a5f600584611a0c90919063ffffffff16565b83611a7e90919063ffffffff16565b9050611a79565b8190505b919050565b6000808284019050838110151515611a9257fe5b8091505092915050565b611aee81600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054611a7e90919063ffffffff16565b600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550611b4681600254611a7e90919063ffffffff16565b6002819055505050565b6000828211151515611b5e57fe5b8183039050929150505600a165627a7a72305820ec0d82a406896ccf20989b3d6e650abe4dc104e400837f1f58e67ef499493ae90029",
+ "nonce": "1",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000008d69d00910d0b2afb2a99ed6c16c8129fa8e1751",
+ "0x0000000000000000000000000000000000000000000000000000000000000003": "0x000000000000000000000000e819f024b41358d2c08e3a868a5c5dd0566078d4",
+ "0x0000000000000000000000000000000000000000000000000000000000000007": "0x000000000000000000000000000000000000000000000000000000005a388981",
+ "0x0000000000000000000000000000000000000000000000000000000000000008": "0x000000000000000000000000000000000000000000000000000000005a3b38e6"
+ }
+ },
+ "0xd4fcab9f0a6dc0493af47c864f6f17a8a5e2e826": {
+ "balance": "0x2a2dd979a35cf000",
+ "code": "0x",
+ "nonce": "0",
+ "storage": {}
+ },
+ "0xe819f024b41358d2c08e3a868a5c5dd0566078d4": {
+ "balance": "0x0",
+ "code": "0x6060604052600436106100ba576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100bf578063095ea7b31461014d57806318160ddd146101a757806323b872dd146101d0578063313ce5671461024957806342966c681461027257806370a08231146102ad5780638da5cb5b146102fa57806395d89b411461034f578063a9059cbb146103dd578063dd62ed3e14610437578063f2fde38b146104a3575b600080fd5b34156100ca57600080fd5b6100d26104dc565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101125780820151818401526020810190506100f7565b50505050905090810190601f16801561013f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561015857600080fd5b61018d600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610515565b604051808215151515815260200191505060405180910390f35b34156101b257600080fd5b6101ba61069c565b6040518082815260200191505060405180910390f35b34156101db57600080fd5b61022f600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506106a2565b604051808215151515815260200191505060405180910390f35b341561025457600080fd5b61025c610952565b6040518082815260200191505060405180910390f35b341561027d57600080fd5b6102936004808035906020019091905050610957565b604051808215151515815260200191505060405180910390f35b34156102b857600080fd5b6102e4600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610abe565b6040518082815260200191505060405180910390f35b341561030557600080fd5b61030d610b07565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561035a57600080fd5b610362610b2d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103a2578082015181840152602081019050610387565b50505050905090810190601f1680156103cf5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103e857600080fd5b61041d600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610b66565b604051808215151515815260200191505060405180910390f35b341561044257600080fd5b61048d600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610d01565b6040518082815260200191505060405180910390f35b34156104ae57600080fd5b6104da600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610d88565b005b6040805190810160405280600b81526020017f416c6c436f6465436f696e00000000000000000000000000000000000000000081525081565b6000808214806105a157506000600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054145b15156105ac57600080fd5b81600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60005481565b600080600260008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905061077683600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610e5f90919063ffffffff16565b600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555061080b83600160008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610e7d90919063ffffffff16565b600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506108618382610e7d90919063ffffffff16565b600260008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508373ffffffffffffffffffffffffffffffffffffffff168573ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef856040518082815260200191505060405180910390a360019150509392505050565b600681565b6000600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156109b557600080fd5b610a0782600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610e7d90919063ffffffff16565b600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610a5f82600054610e7d90919063ffffffff16565b60008190555060003373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a360019050919050565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6040805190810160405280600481526020017f414c4c430000000000000000000000000000000000000000000000000000000081525081565b6000610bba82600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610e7d90919063ffffffff16565b600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610c4f82600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610e5f90919063ffffffff16565b600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b6000600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141515610de457600080fd5b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16141515610e5c5780600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b50565b6000808284019050838110151515610e7357fe5b8091505092915050565b6000828211151515610e8b57fe5b8183039050929150505600a165627a7a7230582059f3ea3df0b054e9ab711f37969684ba83fe38f255ffe2c8d850d951121c51100029",
+ "nonce": "1",
+ "storage": {}
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "3956606365",
+ "extraData": "0x566961425443",
+ "gasLimit": "5418523",
+ "hash": "0x6f37eb930a25da673ea1bb80fd9e32ddac19cdf7cd4bb2eac62cc13598624077",
+ "miner": "0xd049bfd667cb46aa3ef5df0da3e57db3be39e511",
+ "mixHash": "0x10971cde68c587c750c23b8589ae868ce82c2c646636b97e7d9856470c5297c7",
+ "nonce": "0x810f923ff4b450a1",
+ "number": "2295103",
+ "stateRoot": "0xff403612573d76dfdaf4fea2429b77dbe9764021ae0e38dc8ac79a3cf551179e",
+ "timestamp": "1513681246",
+ "totalDifficulty": "7162347056825919"
+ },
+ "input": "0xf86d808504e3b292008307dfa69433056b5dcac09a9b4becad0e1dcf92c19bd0af76880e92596fd62900008029a0e5f27bb66431f7081bb7f1f242003056d7f3f35414c352cd3d1848b52716dac2a07d0be78980edb0bd2a0678fc53aa90ea9558ce346b0d947967216918ac74ccea",
+ "result": {
+ "calls": [
+ {
+ "error": "invalid opcode 0xfe",
+ "from": "0x33056b5dcac09a9b4becad0e1dcf92c19bd0af76",
+ "gas": "0x75fe3",
+ "gasUsed": "0x75fe3",
+ "input": "0xa9059cbb000000000000000000000000d4fcab9f0a6dc0493af47c864f6f17a8a5e2e82600000000000000000000000000000000000000000000000000000000000002f4",
+ "to": "0xe819f024b41358d2c08e3a868a5c5dd0566078d4",
+ "type": "CALL",
+ "value": "0x0"
+ }
+ ],
+ "error": "execution reverted",
+ "from": "0xd4fcab9f0a6dc0493af47c864f6f17a8a5e2e826",
+ "gas": "0x78d9e",
+ "gasUsed": "0x76fc0",
+ "input": "0x",
+ "to": "0x33056b5dcac09a9b4becad0e1dcf92c19bd0af76",
+ "type": "CALL",
+ "value": "0xe92596fd6290000"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_oog.json b/eth/tracers/testdata/call_tracer_oog.json
new file mode 100644
index 0000000..de4fed6
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_oog.json
@@ -0,0 +1,60 @@
+{
+ "context": {
+ "difficulty": "3699098917",
+ "gasLimit": "5258985",
+ "miner": "0xd049bfd667cb46aa3ef5df0da3e57db3be39e511",
+ "number": "2294631",
+ "timestamp": "1513675366"
+ },
+ "genesis": {
+ "alloc": {
+ "0x43064693d3d38ad6a7cb579e0d6d9718c8aa6b62": {
+ "balance": "0x0",
+ "code": "0x6060604052600436106100ba576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100bf578063095ea7b31461014d57806318160ddd146101a757806323b872dd146101d0578063313ce5671461024957806342966c68146102785780635a3b7e42146102b357806370a082311461034157806379cc67901461038e57806395d89b41146103e8578063a9059cbb14610476578063dd62ed3e146104b8575b600080fd5b34156100ca57600080fd5b6100d2610524565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101125780820151818401526020810190506100f7565b50505050905090810190601f16801561013f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561015857600080fd5b61018d600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061055d565b604051808215151515815260200191505060405180910390f35b34156101b257600080fd5b6101ba6105ea565b6040518082815260200191505060405180910390f35b34156101db57600080fd5b61022f600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506105f0565b604051808215151515815260200191505060405180910390f35b341561025457600080fd5b61025c610910565b604051808260ff1660ff16815260200191505060405180910390f35b341561028357600080fd5b6102996004808035906020019091905050610915565b604051808215151515815260200191505060405180910390f35b34156102be57600080fd5b6102c6610a18565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103065780820151818401526020810190506102eb565b50505050905090810190601f1680156103335780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561034c57600080fd5b610378600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610a51565b6040518082815260200191505060405180910390f35b341561039957600080fd5b6103ce600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a69565b604051808215151515815260200191505060405180910390f35b34156103f357600080fd5b6103fb610bf8565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561043b578082015181840152602081019050610420565b50505050905090810190601f1680156104685780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561048157600080fd5b6104b6600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610c31565b005b34156104c357600080fd5b61050e600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610e34565b6040518082815260200191505060405180910390f35b6040805190810160405280600881526020017f446f70616d696e6500000000000000000000000000000000000000000000000081525081565b600081600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001905092915050565b60005481565b6000808373ffffffffffffffffffffffffffffffffffffffff161415151561061757600080fd5b81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015151561066557600080fd5b600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101515156106f157fe5b600260008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054821115151561077c57600080fd5b81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600260008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b601281565b600081600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015151561096557600080fd5b81600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508160008082825403925050819055503373ffffffffffffffffffffffffffffffffffffffff167fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5836040518082815260200191505060405180910390a260019050919050565b6040805190810160405280600981526020017f446f706d6e20302e32000000000000000000000000000000000000000000000081525081565b60016020528060005260406000206000915090505481565b600081600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610ab957600080fd5b600260008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548211151515610b4457600080fd5b81600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508160008082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff167fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5836040518082815260200191505060405180910390a26001905092915050565b6040805190810160405280600581526020017f444f504d4e00000000000000000000000000000000000000000000000000000081525081565b60008273ffffffffffffffffffffffffffffffffffffffff1614151515610c5757600080fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610ca557600080fd5b600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110151515610d3157fe5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60026020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058206d93424f4e7b11929b8276a269038402c10c0ddf21800e999916ddd9dff4a7630029",
+ "nonce": "1",
+ "storage": {
+ "0x296b66049cc4f9c8bf3d4f14752add261d1a980b39bdd194a7897baf39ac7579": "0x0000000000000000000000000000000000000000033b2e3c9fc9653f9e72b1e0"
+ }
+ },
+ "0x94194bc2aaf494501d7880b61274a169f6502a54": {
+ "balance": "0xea8c39a876d19888d",
+ "code": "0x",
+ "nonce": "265",
+ "storage": {}
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "3699098917",
+ "extraData": "0x4554482e45544846414e532e4f52472d4641313738394444",
+ "gasLimit": "5263953",
+ "hash": "0x03a0f62a8106793dafcfae7b75fd2654322062d585a19cea568314d7205790dc",
+ "miner": "0xbbf5029fd710d227630c8b7d338051b8e76d50b3",
+ "mixHash": "0x15482cc64b7c00a947f5bf015dfc010db1a6a668c74df61974d6a7848c174408",
+ "nonce": "0xd1bdb150f6fd170e",
+ "number": "2294630",
+ "stateRoot": "0x1ab1a534e84cc787cda1db21e0d5920ab06017948075b759166cfea7274657a1",
+ "timestamp": "1513675347",
+ "totalDifficulty": "7160543502214733"
+ },
+ "input": "0xf8ab820109855d21dba00082ca1d9443064693d3d38ad6a7cb579e0d6d9718c8aa6b6280b844a9059cbb000000000000000000000000e77b1ac803616503510bed0086e3a7be2627a69900000000000000000000000000000000000000000000000000000009502f90001ba0ce3ad83f5530136467b7c2bb225f406bd170f4ad59c254e5103c34eeabb5bd69a0455154527224a42ab405cacf0fe92918a75641ce4152f8db292019a5527aa956",
+ "result": {
+ "error": "out of gas",
+ "from": "0x94194bc2aaf494501d7880b61274a169f6502a54",
+ "gas": "0x7045",
+ "gasUsed": "0x7045",
+ "input": "0xa9059cbb000000000000000000000000e77b1ac803616503510bed0086e3a7be2627a69900000000000000000000000000000000000000000000000000000009502f9000",
+ "to": "0x43064693d3d38ad6a7cb579e0d6d9718c8aa6b62",
+ "type": "CALL",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_revert.json b/eth/tracers/testdata/call_tracer_revert.json
new file mode 100644
index 0000000..059040a
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_revert.json
@@ -0,0 +1,58 @@
+{
+ "context": {
+ "difficulty": "3665057456",
+ "gasLimit": "5232723",
+ "miner": "0xf4d8e706cfb25c0decbbdd4d2e2cc10c66376a3f",
+ "number": "2294501",
+ "timestamp": "1513673601"
+ },
+ "genesis": {
+ "alloc": {
+ "0x0f6cef2b7fbb504782e35aa82a2207e816a2b7a9": {
+ "balance": "0x2a3fc32bcc019283",
+ "code": "0x",
+ "nonce": "10",
+ "storage": {}
+ },
+ "0xabbcd5b340c80b5f1c0545c04c987b87310296ae": {
+ "balance": "0x0",
+ "code": "0x606060405236156100755763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416632d0335ab811461007a578063548db174146100ab5780637f649783146100fc578063b092145e1461014d578063c3f44c0a14610186578063c47cf5de14610203575b600080fd5b341561008557600080fd5b610099600160a060020a0360043516610270565b60405190815260200160405180910390f35b34156100b657600080fd5b6100fa600460248135818101908301358060208181020160405190810160405280939291908181526020018383602002808284375094965061028f95505050505050565b005b341561010757600080fd5b6100fa600460248135818101908301358060208181020160405190810160405280939291908181526020018383602002808284375094965061029e95505050505050565b005b341561015857600080fd5b610172600160a060020a03600435811690602435166102ad565b604051901515815260200160405180910390f35b341561019157600080fd5b6100fa6004803560ff1690602480359160443591606435600160a060020a0316919060a49060843590810190830135806020601f8201819004810201604051908101604052818152929190602084018383808284375094965050509235600160a060020a031692506102cd915050565b005b341561020e57600080fd5b61025460046024813581810190830135806020601f8201819004810201604051908101604052818152929190602084018383808284375094965061056a95505050505050565b604051600160a060020a03909116815260200160405180910390f35b600160a060020a0381166000908152602081905260409020545b919050565b61029a816000610594565b5b50565b61029a816001610594565b5b50565b600160209081526000928352604080842090915290825290205460ff1681565b60008080600160a060020a038416158061030d5750600160a060020a038085166000908152600160209081526040808320339094168352929052205460ff165b151561031857600080fd5b6103218561056a565b600160a060020a038116600090815260208190526040808220549295507f19000000000000000000000000000000000000000000000000000000000000009230918891908b908b90517fff000000000000000000000000000000000000000000000000000000000000008089168252871660018201526c01000000000000000000000000600160a060020a038088168202600284015286811682026016840152602a8301869052841602604a820152605e810182805190602001908083835b6020831061040057805182525b601f1990920191602091820191016103e0565b6001836020036101000a0380198251168184511617909252505050919091019850604097505050505050505051809103902091506001828a8a8a6040516000815260200160405260006040516020015260405193845260ff90921660208085019190915260408085019290925260608401929092526080909201915160208103908084039060008661646e5a03f1151561049957600080fd5b5050602060405103519050600160a060020a03838116908216146104bc57600080fd5b600160a060020a0380841660009081526020819052604090819020805460010190559087169086905180828051906020019080838360005b8381101561050d5780820151818401525b6020016104f4565b50505050905090810190601f16801561053a5780820380516001836020036101000a031916815260200191505b5091505060006040518083038160008661646e5a03f1915050151561055e57600080fd5b5b505050505050505050565b600060248251101561057e5750600061028a565b600160a060020a0360248301511690505b919050565b60005b825181101561060157600160a060020a033316600090815260016020526040812083918584815181106105c657fe5b90602001906020020151600160a060020a031681526020810191909152604001600020805460ff19169115159190911790555b600101610597565b5b5050505600a165627a7a723058200027e8b695e9d2dea9f3629519022a69f3a1d23055ce86406e686ea54f31ee9c0029",
+ "nonce": "1",
+ "storage": {}
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "3672229776",
+ "extraData": "0x4554482e45544846414e532e4f52472d4641313738394444",
+ "gasLimit": "5227619",
+ "hash": "0xa07b3d6c6bf63f5f981016db9f2d1d93033833f2c17e8bf7209e85f1faf08076",
+ "miner": "0xbbf5029fd710d227630c8b7d338051b8e76d50b3",
+ "mixHash": "0x806e151ce2817be922e93e8d5921fa0f0d0fd213d6b2b9a3fa17458e74a163d0",
+ "nonce": "0xbc5d43adc2c30c7d",
+ "number": "2294500",
+ "stateRoot": "0xca645b335888352ef9d8b1ef083e9019648180b259026572e3139717270de97d",
+ "timestamp": "1513673552",
+ "totalDifficulty": "7160066586979149"
+ },
+ "input": "0xf9018b0a8505d21dba00832dc6c094abbcd5b340c80b5f1c0545c04c987b87310296ae80b9012473b40a5c000000000000000000000000400de2e016bda6577407dfc379faba9899bc73ef0000000000000000000000002cc31912b2b0f3075a87b3640923d45a26cef3ee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d79d8e6c7265636f76657279416464726573730000000000000000000000000000000000000000000000000000000000383e3ec32dc0f66d8fe60dbdc2f6815bdf73a988383e3ec32dc0f66d8fe60dbdc2f6815bdf73a988000000000000000000000000000000000000000000000000000000000000000000000000000000001ba0fd659d76a4edbd2a823e324c93f78ad6803b30ff4a9c8bce71ba82798975c70ca06571eecc0b765688ec6c78942c5ee8b585e00988c0141b518287e9be919bc48a",
+ "result": {
+ "error": "execution reverted",
+ "from": "0x0f6cef2b7fbb504782e35aa82a2207e816a2b7a9",
+ "gas": "0x2d55e8",
+ "gasUsed": "0xc3",
+ "input": "0x73b40a5c000000000000000000000000400de2e016bda6577407dfc379faba9899bc73ef0000000000000000000000002cc31912b2b0f3075a87b3640923d45a26cef3ee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d79d8e6c7265636f76657279416464726573730000000000000000000000000000000000000000000000000000000000383e3ec32dc0f66d8fe60dbdc2f6815bdf73a988383e3ec32dc0f66d8fe60dbdc2f6815bdf73a98800000000000000000000000000000000000000000000000000000000000000000000000000000000",
+ "to": "0xabbcd5b340c80b5f1c0545c04c987b87310296ae",
+ "type": "CALL",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_simple.json b/eth/tracers/testdata/call_tracer_simple.json
new file mode 100644
index 0000000..b464321
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_simple.json
@@ -0,0 +1,78 @@
+{
+ "context": {
+ "difficulty": "3502894804",
+ "gasLimit": "4722976",
+ "miner": "0x1585936b53834b021f68cc13eeefdec2efc8e724",
+ "number": "2289806",
+ "timestamp": "1513601314"
+ },
+ "genesis": {
+ "alloc": {
+ "0x0024f658a46fbb89d8ac105e98d7ac7cbbaf27c5": {
+ "balance": "0x0",
+ "code": "0x",
+ "nonce": "22",
+ "storage": {}
+ },
+ "0x3b873a919aa0512d5a0f09e6dcceaa4a6727fafe": {
+ "balance": "0x4d87094125a369d9bd5",
+ "code": "0x606060405236156100935763ffffffff60e060020a60003504166311ee8382811461009c57806313af4035146100be5780631f5e8f4c146100ee57806324daddc5146101125780634921a91a1461013b57806363e4bff414610157578063764978f91461017f578063893d20e8146101a1578063ba40aaa1146101cd578063cebc9a82146101f4578063e177246e14610216575b61009a5b5b565b005b34156100a457fe5b6100ac61023d565b60408051918252519081900360200190f35b34156100c657fe5b6100da600160a060020a0360043516610244565b604080519115158252519081900360200190f35b34156100f657fe5b6100da610307565b604080519115158252519081900360200190f35b341561011a57fe5b6100da6004351515610318565b604080519115158252519081900360200190f35b6100da6103d6565b604080519115158252519081900360200190f35b6100da600160a060020a0360043516610420565b604080519115158252519081900360200190f35b341561018757fe5b6100ac61046c565b60408051918252519081900360200190f35b34156101a957fe5b6101b1610473565b60408051600160a060020a039092168252519081900360200190f35b34156101d557fe5b6100da600435610483565b604080519115158252519081900360200190f35b34156101fc57fe5b6100ac61050d565b60408051918252519081900360200190f35b341561021e57fe5b6100da600435610514565b604080519115158252519081900360200190f35b6003545b90565b60006000610250610473565b600160a060020a031633600160a060020a03161415156102705760006000fd5b600160a060020a03831615156102865760006000fd5b50600054600160a060020a0390811690831681146102fb57604051600160a060020a0380851691908316907ffcf23a92150d56e85e3a3d33b357493246e55783095eb6a733eb8439ffc752c890600090a360008054600160a060020a031916600160a060020a03851617905560019150610300565b600091505b5b50919050565b60005460a060020a900460ff165b90565b60006000610324610473565b600160a060020a031633600160a060020a03161415156103445760006000fd5b5060005460a060020a900460ff16801515831515146102fb576000546040805160a060020a90920460ff1615158252841515602083015280517fe6cd46a119083b86efc6884b970bfa30c1708f53ba57b86716f15b2f4551a9539281900390910190a16000805460a060020a60ff02191660a060020a8515150217905560019150610300565b600091505b5b50919050565b60006103e0610307565b801561040557506103ef610473565b600160a060020a031633600160a060020a031614155b156104105760006000fd5b610419336105a0565b90505b5b90565b600061042a610307565b801561044f5750610439610473565b600160a060020a031633600160a060020a031614155b1561045a5760006000fd5b610463826105a0565b90505b5b919050565b6001545b90565b600054600160a060020a03165b90565b6000600061048f610473565b600160a060020a031633600160a060020a03161415156104af5760006000fd5b506001548281146102fb57604080518281526020810185905281517f79a3746dde45672c9e8ab3644b8bb9c399a103da2dc94b56ba09777330a83509929181900390910190a160018381559150610300565b600091505b5b50919050565b6002545b90565b60006000610520610473565b600160a060020a031633600160a060020a03161415156105405760006000fd5b506002548281146102fb57604080518281526020810185905281517ff6991a728965fedd6e927fdf16bdad42d8995970b4b31b8a2bf88767516e2494929181900390910190a1600283905560019150610300565b600091505b5b50919050565b60006000426105ad61023d565b116102fb576105c46105bd61050d565b4201610652565b6105cc61046c565b604051909150600160a060020a038416908290600081818185876187965a03f1925050501561063d57604080518281529051600160a060020a038516917f9bca65ce52fdef8a470977b51f247a2295123a4807dfa9e502edf0d30722da3b919081900360200190a260019150610300565b6102fb42610652565b5b600091505b50919050565b60038190555b505600a165627a7a72305820f3c973c8b7ed1f62000b6701bd5b708469e19d0f1d73fde378a56c07fd0b19090029",
+ "nonce": "1",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000000": "0x000000000000000000000001b436ba50d378d4bbc8660d312a13df6af6e89dfb",
+ "0x0000000000000000000000000000000000000000000000000000000000000001": "0x00000000000000000000000000000000000000000000000006f05b59d3b20000",
+ "0x0000000000000000000000000000000000000000000000000000000000000002": "0x000000000000000000000000000000000000000000000000000000000000003c",
+ "0x0000000000000000000000000000000000000000000000000000000000000003": "0x000000000000000000000000000000000000000000000000000000005a37b834"
+ }
+ },
+ "0xb436ba50d378d4bbc8660d312a13df6af6e89dfb": {
+ "balance": "0x1780d77678137ac1b775",
+ "code": "0x",
+ "nonce": "29072",
+ "storage": {}
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "3509749784",
+ "extraData": "0x4554482e45544846414e532e4f52472d4641313738394444",
+ "gasLimit": "4727564",
+ "hash": "0x609948ac3bd3c00b7736b933248891d6c901ee28f066241bddb28f4e00a9f440",
+ "miner": "0xbbf5029fd710d227630c8b7d338051b8e76d50b3",
+ "mixHash": "0xb131e4507c93c7377de00e7c271bf409ec7492767142ff0f45c882f8068c2ada",
+ "nonce": "0x4eb12e19c16d43da",
+ "number": "2289805",
+ "stateRoot": "0xc7f10f352bff82fac3c2999d3085093d12652e19c7fd32591de49dc5d91b4f1f",
+ "timestamp": "1513601261",
+ "totalDifficulty": "7143276353481064"
+ },
+ "input": "0xf88b8271908506fc23ac0083015f90943b873a919aa0512d5a0f09e6dcceaa4a6727fafe80a463e4bff40000000000000000000000000024f658a46fbb89d8ac105e98d7ac7cbbaf27c52aa0bdce0b59e8761854e857fe64015f06dd08a4fbb7624f6094893a79a72e6ad6bea01d9dde033cff7bb235a3163f348a6d7ab8d6b52bc0963a95b91612e40ca766a4",
+ "result": {
+ "calls": [
+ {
+ "from": "0x3b873a919aa0512d5a0f09e6dcceaa4a6727fafe",
+ "input": "0x",
+ "to": "0x0024f658a46fbb89d8ac105e98d7ac7cbbaf27c5",
+ "type": "CALL",
+ "value": "0x6f05b59d3b20000"
+ }
+ ],
+ "from": "0xb436ba50d378d4bbc8660d312a13df6af6e89dfb",
+ "gas": "0x10738",
+ "gasUsed": "0x3ef9",
+ "input": "0x63e4bff40000000000000000000000000024f658a46fbb89d8ac105e98d7ac7cbbaf27c5",
+ "output": "0x0000000000000000000000000000000000000000000000000000000000000001",
+ "to": "0x3b873a919aa0512d5a0f09e6dcceaa4a6727fafe",
+ "type": "CALL",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/testdata/call_tracer_throw.json b/eth/tracers/testdata/call_tracer_throw.json
new file mode 100644
index 0000000..d66f1ff
--- /dev/null
+++ b/eth/tracers/testdata/call_tracer_throw.json
@@ -0,0 +1,62 @@
+{
+ "context": {
+ "difficulty": "117009631",
+ "gasLimit": "4712388",
+ "miner": "0x294e5d6c39a36ce38af1dca70c1060f78dee8070",
+ "number": "25009",
+ "timestamp": "1479891666"
+ },
+ "genesis": {
+ "alloc": {
+ "0x70c9217d814985faef62b124420f8dfbddd96433": {
+ "balance": "0x4ecd70668f5d854a",
+ "code": "0x",
+ "nonce": "1638",
+ "storage": {}
+ },
+ "0xc212e03b9e060e36facad5fd8f4435412ca22e6b": {
+ "balance": "0x0",
+ "code": "0x606060405236156101745760e060020a600035046302d05d3f811461017c57806304a7fdbc1461018e5780630e90f957146101fb5780630fb5a6b41461021257806314baa1b61461021b57806317fc45e21461023a5780632b096926146102435780632e94420f1461025b578063325a19f11461026457806336da44681461026d5780633f81a2c01461027f5780633fc306821461029757806345ecd3d7146102d45780634665096d146102dd5780634e71d92d146102e657806351a34eb8146103085780636111bb951461032d5780636f265b93146103445780637e9014e11461034d57806390ba009114610360578063927df5e014610393578063a7f437791461046c578063ad8f50081461046e578063bc6d909414610477578063bdec3ad114610557578063c19d93fb1461059a578063c9503fe2146105ad578063e0a73a93146105b6578063ea71b02d146105bf578063ea8a1af0146105d1578063ee4a96f9146105f3578063f1ff78a01461065c575b61046c610002565b610665600054600160a060020a031681565b6040805160c081810190925261046c9160049160c4918390600690839083908082843760408051808301909152929750909561018495509193509091908390839080828437509095505050505050600554600090600160a060020a0390811633909116146106a857610002565b61068260015460a060020a900460ff166000145b90565b61069660085481565b61046c600435600154600160a060020a03166000141561072157610002565b610696600d5481565b610696600435600f8160068110156100025750015481565b61069660045481565b61069660035481565b610665600554600160a060020a031681565b61069660043560158160068110156100025750015481565b6106966004355b600b54600f5460009160028202808203928083039290810191018386101561078357601054840186900394505b50505050919050565b61069660025481565b61069660095481565b61046c600554600090600160a060020a03908116339091161461085857610002565b61046c600435600554600090600160a060020a03908116339091161461092e57610002565b6106826001805460a060020a900460ff161461020f565b610696600b5481565b61068260075460a060020a900460ff1681565b6106966004355b600b54601554600091600282028082039280830392908101910183861015610a6c5760165494506102cb565b61046c6004356024356044356040805160015460e360020a631c2d8fb302825260b260020a691858d8dbdd5b9d18dd1b02600483015291516000928392600160a060020a03919091169163e16c7d9891602481810192602092909190829003018187876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663c4b0c96a336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610b4657610002565b005b610696600a5481565b61046c60006000600060006000600160009054906101000a9004600160a060020a0316600160a060020a031663e16c7d986040518160e060020a028152600401808060b260020a691858d8dbdd5b9d18dd1b0281526020015060200190506020604051808303816000876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663c4b0c96a336040518260e060020a0281526004018082600160a060020a031681526020019150506020604051808303816000876161da5a03f1156100025750506040515115159050610f1757610002565b61046c5b60015b60058160ff16101561071e57600f6001820160ff166006811015610002578101549060ff83166006811015610002570154101561129057610002565b61069660015460a060020a900460ff1681565b61069660065481565b610696600c5481565b610665600754600160a060020a031681565b61046c600554600090600160a060020a0390811633909116146112c857610002565b6040805160c081810190925261046c9160049160c4918390600690839083908082843760408051808301909152929750909561018495509193509091908390839080828437509095505050505050600154600090600160a060020a03168114156113fb57610002565b610696600e5481565b60408051600160a060020a03929092168252519081900360200190f35b604080519115158252519081900360200190f35b60408051918252519081900360200190f35b5060005b60068160ff16101561070857828160ff166006811015610002576020020151600f60ff831660068110156100025701558160ff82166006811015610002576020020151601560ff831660068110156100025701556001016106ac565b61071061055b565b505050565b600e8054820190555b50565b6040805160015460e060020a6313bc6d4b02825233600160a060020a03908116600484015292519216916313bc6d4b9160248181019260209290919082900301816000876161da5a03f115610002575050604051511515905061071557610002565b83861015801561079257508286105b156107b457600f546010546011548689039082030291909104900394506102cb565b8286101580156107c55750600b5486105b156107e757600f546011546012548589039082030291909104900394506102cb565b600b5486108015906107f857508186105b1561081d57600b54600f546012546013549289039281039290920204900394506102cb565b81861015801561082c57508086105b1561084e57600f546013546014548489039082030291909104900394506102cb565b60145494506102cb565b60015460a060020a900460ff1660001461087157610002565b600254600a01431161088257610002565b6040805160015460e360020a631c2d8fb302825260a860020a6a636f6e74726163746170690260048301529151600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750505060405180519060200150905080600160a060020a031663771d50e16040518160e060020a0281526004018090506000604051808303816000876161da5a03f1156100025750505050565b60015460a060020a900460ff1660001461094757610002565b600254600a01431161095857610002565b6040805160015460e360020a631c2d8fb302825260a860020a6a636f6e74726163746170690260048301529151600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180517f51a34eb8000000000000000000000000000000000000000000000000000000008252600482018690529151919350600160a060020a03841692506351a34eb8916024808301926000929190829003018183876161da5a03f11561000257505050600b8290554360025560408051838152905130600160a060020a0316917fa609f6bd4ad0b4f419ddad4ac9f0d02c2b9295c5e6891469055cf73c2b568fff919081900360200190a25050565b838610158015610a7b57508286105b15610a9d576015546016546017548689039082900302919091040194506102cb565b828610158015610aae5750600b5486105b15610ad0576015546017546018548589039082900302919091040194506102cb565b600b548610801590610ae157508186105b15610b0657600b546015546018546019549289039281900392909202040194506102cb565b818610158015610b1557508086105b15610b3757601554601954601a548489039082900302919091040194506102cb565b601a54860181900394506102cb565b60015460a060020a900460ff16600014610b5f57610002565b6001805460a060020a60ff02191660a060020a17908190556040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180516004805460e260020a633e4baddd028452908301529151919450600160a060020a038516925063f92eb77491602482810192602092919082900301816000876161da5a03f115610002575050604080518051600a556005547ffebf661200000000000000000000000000000000000000000000000000000000825233600160a060020a03908116600484015216602482015260448101879052905163febf661291606480820192600092909190829003018183876161da5a03f115610002575050508215610cc7576007805473ffffffffffffffffffffffffffffffffffffffff191633179055610dbb565b6040805160055460065460e060020a63599efa6b028352600160a060020a039182166004840152602483015291519184169163599efa6b91604481810192600092909190829003018183876161da5a03f115610002575050604080516006547f56ccb6f000000000000000000000000000000000000000000000000000000000825233600160a060020a03166004830152602482015290516356ccb6f091604480820192600092909190829003018183876161da5a03f115610002575050600580546007805473ffffffffffffffffffffffffffffffffffffffff19908116600160a060020a038416179091551633179055505b6007805460a060020a60ff02191660a060020a87810291909117918290556008544301600955900460ff1615610df757600a54610e039061029e565b600a54610e0b90610367565b600c55610e0f565b600c555b600c54670de0b6b3a7640000850204600d55600754600554604080517f759297bb000000000000000000000000000000000000000000000000000000008152600160a060020a039384166004820152918316602483015260448201879052519184169163759297bb91606481810192600092909190829003018183876161da5a03f11561000257505060408051600754600a54600d54600554600c5460a060020a850460ff161515865260208601929092528486019290925260608401529251600160a060020a0391821694509281169230909116917f3b3d1986083d191be01d28623dc19604728e29ae28bdb9ba52757fdee1a18de2919081900360800190a45050505050565b600954431015610f2657610002565b6001805460a060020a900460ff1614610f3e57610002565b6001805460a060020a60ff0219167402000000000000000000000000000000000000000017908190556040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f1156100025750506040805180516004805460e260020a633e4baddd028452908301529151919750600160a060020a038816925063f92eb77491602482810192602092919082900301816000876161da5a03f115610002575050604051516007549095506000945060a060020a900460ff1615905061105c57600a5484111561105757600a54600d54670de0b6b3a7640000918603020492505b61107e565b600a5484101561107e57600a54600d54670de0b6b3a764000091869003020492505b60065483111561108e5760065492505b6006548390039150600083111561111857604080516005546007547f5928d37f000000000000000000000000000000000000000000000000000000008352600160a060020a0391821660048401528116602483015260448201869052915191871691635928d37f91606481810192600092909190829003018183876161da5a03f115610002575050505b600082111561117a576040805160055460e060020a63599efa6b028252600160a060020a0390811660048301526024820185905291519187169163599efa6b91604481810192600092909190829003018183876161da5a03f115610002575050505b6040805185815260208101849052808201859052905130600160a060020a0316917f89e690b1d5aaae14f3e85f108dc92d9ab3763a58d45aed8b59daedbbae8fe794919081900360600190a260008311156112285784600160a060020a0316634cc927d785336040518360e060020a0281526004018083815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f11561000257505050611282565b84600160a060020a0316634cc927d7600a60005054336040518360e060020a0281526004018083815260200182600160a060020a03168152602001925050506000604051808303816000876161da5a03f115610002575050505b600054600160a060020a0316ff5b60156001820160ff166006811015610002578101549060ff8316600681101561000257015411156112c057610002565b60010161055e565b60015460a060020a900460ff166000146112e157610002565b600254600a0143116112f257610002565b6001546040805160e360020a631c2d8fb302815260a860020a6a636f6e74726163746170690260048201529051600160a060020a03929092169163e16c7d989160248181019260209290919082900301816000876161da5a03f11561000257505060408051805160055460065460e060020a63599efa6b028452600160a060020a03918216600485015260248401529251909450918416925063599efa6b916044808301926000929190829003018183876161da5a03f1156100025750505080600160a060020a0316632b68bb2d6040518160e060020a0281526004018090506000604051808303816000876161da5a03f115610002575050600054600160a060020a03169050ff5b6001546040805160e060020a6313bc6d4b02815233600160a060020a039081166004830152915191909216916313bc6d4b91602480830192602092919082900301816000876161da5a03f11561000257505060405151151590506106a85761000256",
+ "nonce": "1",
+ "storage": {
+ "0x0000000000000000000000000000000000000000000000000000000000000001": "0x0000000000000000000000002cccf5e0538493c235d1c5ef6580f77d99e91396",
+ "0x0000000000000000000000000000000000000000000000000000000000000002": "0x00000000000000000000000000000000000000000000000000000000000061a9",
+ "0x0000000000000000000000000000000000000000000000000000000000000005": "0x00000000000000000000000070c9217d814985faef62b124420f8dfbddd96433"
+ }
+ }
+ },
+ "config": {
+ "byzantiumBlock": 1700000,
+ "chainId": 3,
+ "daoForkSupport": true,
+ "eip150Block": 0,
+ "eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
+ "eip155Block": 10,
+ "eip158Block": 10,
+ "ethash": {},
+ "homesteadBlock": 0
+ },
+ "difficulty": "117066792",
+ "extraData": "0xd783010502846765746887676f312e372e33856c696e7578",
+ "gasLimit": "4712388",
+ "hash": "0xe23e8d4562a1045b70cbc99fefb20c101a8f0fc8559a80d65fea8896e2f1d46e",
+ "miner": "0x71842f946b98800fe6feb49f0ae4e253259031c9",
+ "mixHash": "0x0aada9d6e93dd4db0d09c0488dc0a048fca2ccdc1f3fc7b83ba2a8d393a3a4ff",
+ "nonce": "0x70849d5838dee2e9",
+ "number": "25008",
+ "stateRoot": "0x1e01d2161794768c5b917069e73d86e8dca80cd7f3168c0597de420ab93a3b7b",
+ "timestamp": "1479891641",
+ "totalDifficulty": "1896347038589"
+ },
+ "input": "0xf88b8206668504a817c8008303d09094c212e03b9e060e36facad5fd8f4435412ca22e6b80a451a34eb8000000000000000000000000000000000000000000000027fad02094277c000029a0692a3b4e7b2842f8dd7832e712c21e09f451f416c8976d5b8d02e8c0c2b4bea9a07645e90fc421b63dd755767fd93d3c03b4ec0c4d8fafa059558d08cf11d59750",
+ "result": {
+ "error": "evm: invalid jump destination",
+ "from": "0x70c9217d814985faef62b124420f8dfbddd96433",
+ "gas": "0x37b38",
+ "gasUsed": "0x37b38",
+ "input": "0x51a34eb8000000000000000000000000000000000000000000000027fad02094277c0000",
+ "to": "0xc212e03b9e060e36facad5fd8f4435412ca22e6b",
+ "type": "CALL",
+ "value": "0x0"
+ }
+}
diff --git a/eth/tracers/tracer.go b/eth/tracers/tracer.go
new file mode 100644
index 0000000..f2ef25d
--- /dev/null
+++ b/eth/tracers/tracer.go
@@ -0,0 +1,641 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package tracers
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "math/big"
+ "sync/atomic"
+ "time"
+ "unsafe"
+
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+ "github.com/ava-labs/go-ethereum/crypto"
+ "github.com/ava-labs/go-ethereum/log"
+ duktape "gopkg.in/olebedev/go-duktape.v3"
+)
+
+// bigIntegerJS is the minified version of https://github.com/peterolson/BigInteger.js.
+const bigIntegerJS = `var bigInt=function(undefined){"use strict";var BASE=1e7,LOG_BASE=7,MAX_INT=9007199254740992,MAX_INT_ARR=smallToArray(MAX_INT),LOG_MAX_INT=Math.log(MAX_INT);function Integer(v,radix){if(typeof v==="undefined")return Integer[0];if(typeof radix!=="undefined")return+radix===10?parseValue(v):parseBase(v,radix);return parseValue(v)}function BigInteger(value,sign){this.value=value;this.sign=sign;this.isSmall=false}BigInteger.prototype=Object.create(Integer.prototype);function SmallInteger(value){this.value=value;this.sign=value<0;this.isSmall=true}SmallInteger.prototype=Object.create(Integer.prototype);function isPrecise(n){return-MAX_INT<n&&n<MAX_INT}function smallToArray(n){if(n<1e7)return[n];if(n<1e14)return[n%1e7,Math.floor(n/1e7)];return[n%1e7,Math.floor(n/1e7)%1e7,Math.floor(n/1e14)]}function arrayToSmall(arr){trim(arr);var length=arr.length;if(length<4&&compareAbs(arr,MAX_INT_ARR)<0){switch(length){case 0:return 0;case 1:return arr[0];case 2:return arr[0]+arr[1]*BASE;default:return arr[0]+(arr[1]+arr[2]*BASE)*BASE}}return arr}function trim(v){var i=v.length;while(v[--i]===0);v.length=i+1}function createArray(length){var x=new Array(length);var i=-1;while(++i<length){x[i]=0}return x}function truncate(n){if(n>0)return Math.floor(n);return Math.ceil(n)}function add(a,b){var l_a=a.length,l_b=b.length,r=new Array(l_a),carry=0,base=BASE,sum,i;for(i=0;i<l_b;i++){sum=a[i]+b[i]+carry;carry=sum>=base?1:0;r[i]=sum-carry*base}while(i<l_a){sum=a[i]+carry;carry=sum===base?1:0;r[i++]=sum-carry*base}if(carry>0)r.push(carry);return r}function addAny(a,b){if(a.length>=b.length)return add(a,b);return add(b,a)}function addSmall(a,carry){var l=a.length,r=new Array(l),base=BASE,sum,i;for(i=0;i<l;i++){sum=a[i]-base+carry;carry=Math.floor(sum/base);r[i]=sum-carry*base;carry+=1}while(carry>0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}BigInteger.prototype.add=function(v){var n=parseValue(v);if(this.sign!==n.sign){return this.subtract(n.negate())}var a=this.value,b=n.value;if(n.isSmall){return new BigInteger(addSmall(a,Math.abs(b)),this.sign)}return new BigInteger(addAny(a,b),this.sign)};BigInteger.prototype.plus=BigInteger.prototype.add;SmallInteger.prototype.add=function(v){var n=parseValue(v);var a=this.value;if(a<0!==n.sign){return this.subtract(n.negate())}var b=n.value;if(n.isSmall){if(isPrecise(a+b))return new SmallInteger(a+b);b=smallToArray(Math.abs(b))}return new BigInteger(addSmall(b,Math.abs(a)),a<0)};SmallInteger.prototype.plus=SmallInteger.prototype.add;function subtract(a,b){var a_l=a.length,b_l=b.length,r=new Array(a_l),borrow=0,base=BASE,i,difference;for(i=0;i<b_l;i++){difference=a[i]-borrow-b[i];if(difference<0){difference+=base;borrow=1}else borrow=0;r[i]=difference}for(i=b_l;i<a_l;i++){difference=a[i]-borrow;if(difference<0)difference+=base;else{r[i++]=difference;break}r[i]=difference}for(;i<a_l;i++){r[i]=a[i]}trim(r);return r}function subtractAny(a,b,sign){var value;if(compareAbs(a,b)>=0){value=subtract(a,b)}else{value=subtract(b,a);sign=!sign}value=arrayToSmall(value);if(typeof value==="number"){if(sign)value=-value;return new SmallInteger(value)}return new BigInteger(value,sign)}function subtractSmall(a,b,sign){var l=a.length,r=new Array(l),carry=-b,base=BASE,i,difference;for(i=0;i<l;i++){difference=a[i]+carry;carry=Math.floor(difference/base);difference%=base;r[i]=difference<0?difference+base:difference}r=arrayToSmall(r);if(typeof r==="number"){if(sign)r=-r;return new SmallInteger(r)}return new BigInteger(r,sign)}BigInteger.prototype.subtract=function(v){var n=parseValue(v);if(this.sign!==n.sign){return this.add(n.negate())}var a=this.value,b=n.value;if(n.isSmall)return subtractSmall(a,Math.abs(b),this.sign);return subtractAny(a,b,this.sign)};BigInteger.prototype.minus=BigInteger.prototype.subtract;SmallInteger.prototype.subtract=function(v){var n=parseValue(v);var a=this.value;if(a<0!==n.sign){return this.add(n.negate())}var b=n.value;if(n.isSmall){return new SmallInteger(a-b)}return subtractSmall(b,Math.abs(a),a>=0)};SmallInteger.prototype.minus=SmallInteger.prototype.subtract;BigInteger.prototype.negate=function(){return new BigInteger(this.value,!this.sign)};SmallInteger.prototype.negate=function(){var sign=this.sign;var small=new SmallInteger(-this.value);small.sign=!sign;return small};BigInteger.prototype.abs=function(){return new BigInteger(this.value,false)};SmallInteger.prototype.abs=function(){return new SmallInteger(Math.abs(this.value))};function multiplyLong(a,b){var a_l=a.length,b_l=b.length,l=a_l+b_l,r=createArray(l),base=BASE,product,carry,i,a_i,b_j;for(i=0;i<a_l;++i){a_i=a[i];for(var j=0;j<b_l;++j){b_j=b[j];product=a_i*b_j+r[i+j];carry=Math.floor(product/base);r[i+j]=product-carry*base;r[i+j+1]+=carry}}trim(r);return r}function multiplySmall(a,b){var l=a.length,r=new Array(l),base=BASE,carry=0,product,i;for(i=0;i<l;i++){product=a[i]*b+carry;carry=Math.floor(product/base);r[i]=product-carry*base}while(carry>0){r[i++]=carry%base;carry=Math.floor(carry/base)}return r}function shiftLeft(x,n){var r=[];while(n-- >0)r.push(0);return r.concat(x)}function multiplyKaratsuba(x,y){var n=Math.max(x.length,y.length);if(n<=30)return multiplyLong(x,y);n=Math.ceil(n/2);var b=x.slice(n),a=x.slice(0,n),d=y.slice(n),c=y.slice(0,n);var ac=multiplyKaratsuba(a,c),bd=multiplyKaratsuba(b,d),abcd=multiplyKaratsuba(addAny(a,b),addAny(c,d));var product=addAny(addAny(ac,shiftLeft(subtract(subtract(abcd,ac),bd),n)),shiftLeft(bd,2*n));trim(product);return product}function useKaratsuba(l1,l2){return-.012*l1-.012*l2+15e-6*l1*l2>0}BigInteger.prototype.multiply=function(v){var n=parseValue(v),a=this.value,b=n.value,sign=this.sign!==n.sign,abs;if(n.isSmall){if(b===0)return Integer[0];if(b===1)return this;if(b===-1)return this.negate();abs=Math.abs(b);if(abs<BASE){return new BigInteger(multiplySmall(a,abs),sign)}b=smallToArray(abs)}if(useKaratsuba(a.length,b.length))return new BigInteger(multiplyKaratsuba(a,b),sign);return new BigInteger(multiplyLong(a,b),sign)};BigInteger.prototype.times=BigInteger.prototype.multiply;function multiplySmallAndArray(a,b,sign){if(a<BASE){return new BigInteger(multiplySmall(b,a),sign)}return new BigInteger(multiplyLong(b,smallToArray(a)),sign)}SmallInteger.prototype._multiplyBySmall=function(a){if(isPrecise(a.value*this.value)){return new SmallInteger(a.value*this.value)}return multiplySmallAndArray(Math.abs(a.value),smallToArray(Math.abs(this.value)),this.sign!==a.sign)};BigInteger.prototype._multiplyBySmall=function(a){if(a.value===0)return Integer[0];if(a.value===1)return this;if(a.value===-1)return this.negate();return multiplySmallAndArray(Math.abs(a.value),this.value,this.sign!==a.sign)};SmallInteger.prototype.multiply=function(v){return parseValue(v)._multiplyBySmall(this)};SmallInteger.prototype.times=SmallInteger.prototype.multiply;function square(a){var l=a.length,r=createArray(l+l),base=BASE,product,carry,i,a_i,a_j;for(i=0;i<l;i++){a_i=a[i];for(var j=0;j<l;j++){a_j=a[j];product=a_i*a_j+r[i+j];carry=Math.floor(product/base);r[i+j]=product-carry*base;r[i+j+1]+=carry}}trim(r);return r}BigInteger.prototype.square=function(){return new BigInteger(square(this.value),false)};SmallInteger.prototype.square=function(){var value=this.value*this.value;if(isPrecise(value))return new SmallInteger(value);return new BigInteger(square(smallToArray(Math.abs(this.value))),false)};function divMod1(a,b){var a_l=a.length,b_l=b.length,base=BASE,result=createArray(b.length),divisorMostSignificantDigit=b[b_l-1],lambda=Math.ceil(base/(2*divisorMostSignificantDigit)),remainder=multiplySmall(a,lambda),divisor=multiplySmall(b,lambda),quotientDigit,shift,carry,borrow,i,l,q;if(remainder.length<=a_l)remainder.push(0);divisor.push(0);divisorMostSignificantDigit=divisor[b_l-1];for(shift=a_l-b_l;shift>=0;shift--){quotientDigit=base-1;if(remainder[shift+b_l]!==divisorMostSignificantDigit){quotientDigit=Math.floor((remainder[shift+b_l]*base+remainder[shift+b_l-1])/divisorMostSignificantDigit)}carry=0;borrow=0;l=divisor.length;for(i=0;i<l;i++){carry+=quotientDigit*divisor[i];q=Math.floor(carry/base);borrow+=remainder[shift+i]-(carry-q*base);carry=q;if(borrow<0){remainder[shift+i]=borrow+base;borrow=-1}else{remainder[shift+i]=borrow;borrow=0}}while(borrow!==0){quotientDigit-=1;carry=0;for(i=0;i<l;i++){carry+=remainder[shift+i]-base+divisor[i];if(carry<0){remainder[shift+i]=carry+base;carry=0}else{remainder[shift+i]=carry;carry=1}}borrow+=carry}result[shift]=quotientDigit}remainder=divModSmall(remainder,lambda)[0];return[arrayToSmall(result),arrayToSmall(remainder)]}function divMod2(a,b){var a_l=a.length,b_l=b.length,result=[],part=[],base=BASE,guess,xlen,highx,highy,check;while(a_l){part.unshift(a[--a_l]);trim(part);if(compareAbs(part,b)<0){result.push(0);continue}xlen=part.length;highx=part[xlen-1]*base+part[xlen-2];highy=b[b_l-1]*base+b[b_l-2];if(xlen>b_l){highx=(highx+1)*base}guess=Math.ceil(highx/highy);do{check=multiplySmall(b,guess);if(compareAbs(check,part)<=0)break;guess--}while(guess);result.push(guess);part=subtract(part,check)}result.reverse();return[arrayToSmall(result),arrayToSmall(part)]}function divModSmall(value,lambda){var length=value.length,quotient=createArray(length),base=BASE,i,q,remainder,divisor;remainder=0;for(i=length-1;i>=0;--i){divisor=remainder*base+value[i];q=truncate(divisor/lambda);remainder=divisor-q*lambda;quotient[i]=q|0}return[quotient,remainder|0]}function divModAny(self,v){var value,n=parseValue(v);var a=self.value,b=n.value;var quotient;if(b===0)throw new Error("Cannot divide by zero");if(self.isSmall){if(n.isSmall){return[new SmallInteger(truncate(a/b)),new SmallInteger(a%b)]}return[Integer[0],self]}if(n.isSmall){if(b===1)return[self,Integer[0]];if(b==-1)return[self.negate(),Integer[0]];var abs=Math.abs(b);if(abs<BASE){value=divModSmall(a,abs);quotient=arrayToSmall(value[0]);var remainder=value[1];if(self.sign)remainder=-remainder;if(typeof quotient==="number"){if(self.sign!==n.sign)quotient=-quotient;return[new SmallInteger(quotient),new SmallInteger(remainder)]}return[new BigInteger(quotient,self.sign!==n.sign),new SmallInteger(remainder)]}b=smallToArray(abs)}var comparison=compareAbs(a,b);if(comparison===-1)return[Integer[0],self];if(comparison===0)return[Integer[self.sign===n.sign?1:-1],Integer[0]];if(a.length+b.length<=200)value=divMod1(a,b);else value=divMod2(a,b);quotient=value[0];var qSign=self.sign!==n.sign,mod=value[1],mSign=self.sign;if(typeof quotient==="number"){if(qSign)quotient=-quotient;quotient=new SmallInteger(quotient)}else quotient=new BigInteger(quotient,qSign);if(typeof mod==="number"){if(mSign)mod=-mod;mod=new SmallInteger(mod)}else mod=new BigInteger(mod,mSign);return[quotient,mod]}BigInteger.prototype.divmod=function(v){var result=divModAny(this,v);return{quotient:result[0],remainder:result[1]}};SmallInteger.prototype.divmod=BigInteger.prototype.divmod;BigInteger.prototype.divide=function(v){return divModAny(this,v)[0]};SmallInteger.prototype.over=SmallInteger.prototype.divide=BigInteger.prototype.over=BigInteger.prototype.divide;BigInteger.prototype.mod=function(v){return divModAny(this,v)[1]};SmallInteger.prototype.remainder=SmallInteger.prototype.mod=BigInteger.prototype.remainder=BigInteger.prototype.mod;BigInteger.prototype.pow=function(v){var n=parseValue(v),a=this.value,b=n.value,value,x,y;if(b===0)return Integer[1];if(a===0)return Integer[0];if(a===1)return Integer[1];if(a===-1)return n.isEven()?Integer[1]:Integer[-1];if(n.sign){return Integer[0]}if(!n.isSmall)throw new Error("The exponent "+n.toString()+" is too large.");if(this.isSmall){if(isPrecise(value=Math.pow(a,b)))return new SmallInteger(truncate(value))}x=this;y=Integer[1];while(true){if(b&1===1){y=y.times(x);--b}if(b===0)break;b/=2;x=x.square()}return y};SmallInteger.prototype.pow=BigInteger.prototype.pow;BigInteger.prototype.modPow=function(exp,mod){exp=parseValue(exp);mod=parseValue(mod);if(mod.isZero())throw new Error("Cannot take modPow with modulus 0");var r=Integer[1],base=this.mod(mod);while(exp.isPositive()){if(base.isZero())return Integer[0];if(exp.isOdd())r=r.multiply(base).mod(mod);exp=exp.divide(2);base=base.square().mod(mod)}return r};SmallInteger.prototype.modPow=BigInteger.prototype.modPow;function compareAbs(a,b){if(a.length!==b.length){return a.length>b.length?1:-1}for(var i=a.length-1;i>=0;i--){if(a[i]!==b[i])return a[i]>b[i]?1:-1}return 0}BigInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall)return 1;return compareAbs(a,b)};SmallInteger.prototype.compareAbs=function(v){var n=parseValue(v),a=Math.abs(this.value),b=n.value;if(n.isSmall){b=Math.abs(b);return a===b?0:a>b?1:-1}return-1};BigInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(this.sign!==n.sign){return n.sign?1:-1}if(n.isSmall){return this.sign?-1:1}return compareAbs(a,b)*(this.sign?-1:1)};BigInteger.prototype.compareTo=BigInteger.prototype.compare;SmallInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return 1}var n=parseValue(v),a=this.value,b=n.value;if(n.isSmall){return a==b?0:a>b?1:-1}if(a<0!==n.sign){return a<0?-1:1}return a<0?1:-1};SmallInteger.prototype.compareTo=SmallInteger.prototype.compare;BigInteger.prototype.equals=function(v){return this.compare(v)===0};SmallInteger.prototype.eq=SmallInteger.prototype.equals=BigInteger.prototype.eq=BigInteger.prototype.equals;BigInteger.prototype.notEquals=function(v){return this.compare(v)!==0};SmallInteger.prototype.neq=SmallInteger.prototype.notEquals=BigInteger.prototype.neq=BigInteger.prototype.notEquals;BigInteger.prototype.greater=function(v){return this.compare(v)>0};SmallInteger.prototype.gt=SmallInteger.prototype.greater=BigInteger.prototype.gt=BigInteger.prototype.greater;BigInteger.prototype.lesser=function(v){return this.compare(v)<0};SmallInteger.prototype.lt=SmallInteger.prototype.lesser=BigInteger.prototype.lt=BigInteger.prototype.lesser;BigInteger.prototype.greaterOrEquals=function(v){return this.compare(v)>=0};SmallInteger.prototype.geq=SmallInteger.prototype.greaterOrEquals=BigInteger.prototype.geq=BigInteger.prototype.greaterOrEquals;BigInteger.prototype.lesserOrEquals=function(v){return this.compare(v)<=0};SmallInteger.prototype.leq=SmallInteger.prototype.lesserOrEquals=BigInteger.prototype.leq=BigInteger.prototype.lesserOrEquals;BigInteger.prototype.isEven=function(){return(this.value[0]&1)===0};SmallInteger.prototype.isEven=function(){return(this.value&1)===0};BigInteger.prototype.isOdd=function(){return(this.value[0]&1)===1};SmallInteger.prototype.isOdd=function(){return(this.value&1)===1};BigInteger.prototype.isPositive=function(){return!this.sign};SmallInteger.prototype.isPositive=function(){return this.value>0};BigInteger.prototype.isNegative=function(){return this.sign};SmallInteger.prototype.isNegative=function(){return this.value<0};BigInteger.prototype.isUnit=function(){return false};SmallInteger.prototype.isUnit=function(){return Math.abs(this.value)===1};BigInteger.prototype.isZero=function(){return false};SmallInteger.prototype.isZero=function(){return this.value===0};BigInteger.prototype.isDivisibleBy=function(v){var n=parseValue(v);var value=n.value;if(value===0)return false;if(value===1)return true;if(value===2)return this.isEven();return this.mod(n).equals(Integer[0])};SmallInteger.prototype.isDivisibleBy=BigInteger.prototype.isDivisibleBy;function isBasicPrime(v){var n=v.abs();if(n.isUnit())return false;if(n.equals(2)||n.equals(3)||n.equals(5))return true;if(n.isEven()||n.isDivisibleBy(3)||n.isDivisibleBy(5))return false;if(n.lesser(25))return true}BigInteger.prototype.isPrime=function(){var isPrime=isBasicPrime(this);if(isPrime!==undefined)return isPrime;var n=this.abs(),nPrev=n.prev();var a=[2,3,5,7,11,13,17,19],b=nPrev,d,t,i,x;while(b.isEven())b=b.divide(2);for(i=0;i<a.length;i++){x=bigInt(a[i]).modPow(b,n);if(x.equals(Integer[1])||x.equals(nPrev))continue;for(t=true,d=b;t&&d.lesser(nPrev);d=d.multiply(2)){x=x.square().mod(n);if(x.equals(nPrev))t=false}if(t)return false}return true};SmallInteger.prototype.isPrime=BigInteger.prototype.isPrime;BigInteger.prototype.isProbablePrime=function(iterations){var isPrime=isBasicPrime(this);if(isPrime!==undefined)return isPrime;var n=this.abs();var t=iterations===undefined?5:iterations;for(var i=0;i<t;i++){var a=bigInt.randBetween(2,n.minus(2));if(!a.modPow(n.prev(),n).isUnit())return false}return true};SmallInteger.prototype.isProbablePrime=BigInteger.prototype.isProbablePrime;BigInteger.prototype.modInv=function(n){var t=bigInt.zero,newT=bigInt.one,r=parseValue(n),newR=this.abs(),q,lastT,lastR;while(!newR.equals(bigInt.zero)){q=r.divide(newR);lastT=t;lastR=r;t=newT;r=newR;newT=lastT.subtract(q.multiply(newT));newR=lastR.subtract(q.multiply(newR))}if(!r.equals(1))throw new Error(this.toString()+" and "+n.toString()+" are not co-prime");if(t.compare(0)===-1){t=t.add(n)}if(this.isNegative()){return t.negate()}return t};SmallInteger.prototype.modInv=BigInteger.prototype.modInv;BigInteger.prototype.next=function(){var value=this.value;if(this.sign){return subtractSmall(value,1,this.sign)}return new BigInteger(addSmall(value,1),this.sign)};SmallInteger.prototype.next=function(){var value=this.value;if(value+1<MAX_INT)return new SmallInteger(value+1);return new BigInteger(MAX_INT_ARR,false)};BigInteger.prototype.prev=function(){var value=this.value;if(this.sign){return new BigInteger(addSmall(value,1),true)}return subtractSmall(value,1,this.sign)};SmallInteger.prototype.prev=function(){var value=this.value;if(value-1>-MAX_INT)return new SmallInteger(value-1);return new BigInteger(MAX_INT_ARR,true)};var powersOfTwo=[1];while(2*powersOfTwo[powersOfTwo.length-1]<=BASE)powersOfTwo.push(2*powersOfTwo[powersOfTwo.length-1]);var powers2Length=powersOfTwo.length,highestPower2=powersOfTwo[powers2Length-1];function shift_isSmall(n){return(typeof n==="number"||typeof n==="string")&&+Math.abs(n)<=BASE||n instanceof BigInteger&&n.value.length<=1}BigInteger.prototype.shiftLeft=function(n){if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}n=+n;if(n<0)return this.shiftRight(-n);var result=this;while(n>=powers2Length){result=result.multiply(highestPower2);n-=powers2Length-1}return result.multiply(powersOfTwo[n])};SmallInteger.prototype.shiftLeft=BigInteger.prototype.shiftLeft;BigInteger.prototype.shiftRight=function(n){var remQuo;if(!shift_isSmall(n)){throw new Error(String(n)+" is too large for shifting.")}n=+n;if(n<0)return this.shiftLeft(-n);var result=this;while(n>=powers2Length){if(result.isZero())return result;remQuo=divModAny(result,highestPower2);result=remQuo[1].isNegative()?remQuo[0].prev():remQuo[0];n-=powers2Length-1}remQuo=divModAny(result,powersOfTwo[n]);return remQuo[1].isNegative()?remQuo[0].prev():remQuo[0]};SmallInteger.prototype.shiftRight=BigInteger.prototype.shiftRight;function bitwise(x,y,fn){y=parseValue(y);var xSign=x.isNegative(),ySign=y.isNegative();var xRem=xSign?x.not():x,yRem=ySign?y.not():y;var xDigit=0,yDigit=0;var xDivMod=null,yDivMod=null;var result=[];while(!xRem.isZero()||!yRem.isZero()){xDivMod=divModAny(xRem,highestPower2);xDigit=xDivMod[1].toJSNumber();if(xSign){xDigit=highestPower2-1-xDigit}yDivMod=divModAny(yRem,highestPower2);yDigit=yDivMod[1].toJSNumber();if(ySign){yDigit=highestPower2-1-yDigit}xRem=xDivMod[0];yRem=yDivMod[0];result.push(fn(xDigit,yDigit))}var sum=fn(xSign?1:0,ySign?1:0)!==0?bigInt(-1):bigInt(0);for(var i=result.length-1;i>=0;i-=1){sum=sum.multiply(highestPower2).add(bigInt(result[i]))}return sum}BigInteger.prototype.not=function(){return this.negate().prev()};SmallInteger.prototype.not=BigInteger.prototype.not;BigInteger.prototype.and=function(n){return bitwise(this,n,function(a,b){return a&b})};SmallInteger.prototype.and=BigInteger.prototype.and;BigInteger.prototype.or=function(n){return bitwise(this,n,function(a,b){return a|b})};SmallInteger.prototype.or=BigInteger.prototype.or;BigInteger.prototype.xor=function(n){return bitwise(this,n,function(a,b){return a^b})};SmallInteger.prototype.xor=BigInteger.prototype.xor;var LOBMASK_I=1<<30,LOBMASK_BI=(BASE&-BASE)*(BASE&-BASE)|LOBMASK_I;function roughLOB(n){var v=n.value,x=typeof v==="number"?v|LOBMASK_I:v[0]+v[1]*BASE|LOBMASK_BI;return x&-x}function max(a,b){a=parseValue(a);b=parseValue(b);return a.greater(b)?a:b}function min(a,b){a=parseValue(a);b=parseValue(b);return a.lesser(b)?a:b}function gcd(a,b){a=parseValue(a).abs();b=parseValue(b).abs();if(a.equals(b))return a;if(a.isZero())return b;if(b.isZero())return a;var c=Integer[1],d,t;while(a.isEven()&&b.isEven()){d=Math.min(roughLOB(a),roughLOB(b));a=a.divide(d);b=b.divide(d);c=c.multiply(d)}while(a.isEven()){a=a.divide(roughLOB(a))}do{while(b.isEven()){b=b.divide(roughLOB(b))}if(a.greater(b)){t=b;b=a;a=t}b=b.subtract(a)}while(!b.isZero());return c.isUnit()?a:a.multiply(c)}function lcm(a,b){a=parseValue(a).abs();b=parseValue(b).abs();return a.divide(gcd(a,b)).multiply(b)}function randBetween(a,b){a=parseValue(a);b=parseValue(b);var low=min(a,b),high=max(a,b);var range=high.subtract(low).add(1);if(range.isSmall)return low.add(Math.floor(Math.random()*range));var length=range.value.length-1;var result=[],restricted=true;for(var i=length;i>=0;i--){var top=restricted?range.value[i]:BASE;var digit=truncate(Math.random()*top);result.unshift(digit);if(digit<top)restricted=false}result=arrayToSmall(result);return low.add(typeof result==="number"?new SmallInteger(result):new BigInteger(result,false))}var parseBase=function(text,base){var length=text.length;var i;var absBase=Math.abs(base);for(var i=0;i<length;i++){var c=text[i].toLowerCase();if(c==="-")continue;if(/[a-z0-9]/.test(c)){if(/[0-9]/.test(c)&&+c>=absBase){if(c==="1"&&absBase===1)continue;throw new Error(c+" is not a valid digit in base "+base+".")}else if(c.charCodeAt(0)-87>=absBase){throw new Error(c+" is not a valid digit in base "+base+".")}}}if(2<=base&&base<=36){if(length<=LOG_MAX_INT/Math.log(base)){var result=parseInt(text,base);if(isNaN(result)){throw new Error(c+" is not a valid digit in base "+base+".")}return new SmallInteger(parseInt(text,base))}}base=parseValue(base);var digits=[];var isNegative=text[0]==="-";for(i=isNegative?1:0;i<text.length;i++){var c=text[i].toLowerCase(),charCode=c.charCodeAt(0);if(48<=charCode&&charCode<=57)digits.push(parseValue(c));else if(97<=charCode&&charCode<=122)digits.push(parseValue(c.charCodeAt(0)-87));else if(c==="<"){var start=i;do{i++}while(text[i]!==">");digits.push(parseValue(text.slice(start+1,i)))}else throw new Error(c+" is not a valid character")}return parseBaseFromArray(digits,base,isNegative)};function parseBaseFromArray(digits,base,isNegative){var val=Integer[0],pow=Integer[1],i;for(i=digits.length-1;i>=0;i--){val=val.add(digits[i].times(pow));pow=pow.times(base)}return isNegative?val.negate():val}function stringify(digit){var v=digit.value;if(typeof v==="number")v=[v];if(v.length===1&&v[0]<=35){return"0123456789abcdefghijklmnopqrstuvwxyz".charAt(v[0])}return"<"+v+">"}function toBase(n,base){base=bigInt(base);if(base.isZero()){if(n.isZero())return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(base.equals(-1)){if(n.isZero())return"0";if(n.isNegative())return new Array(1-n).join("10");return"1"+new Array(+n).join("01")}var minusSign="";if(n.isNegative()&&base.isPositive()){minusSign="-";n=n.abs()}if(base.equals(1)){if(n.isZero())return"0";return minusSign+new Array(+n+1).join(1)}var out=[];var left=n,divmod;while(left.isNegative()||left.compareAbs(base)>=0){divmod=left.divmod(base);left=divmod.quotient;var digit=divmod.remainder;if(digit.isNegative()){digit=base.minus(digit).abs();left=left.next()}out.push(stringify(digit))}out.push(stringify(left));return minusSign+out.reverse().join("")}BigInteger.prototype.toString=function(radix){if(radix===undefined)radix=10;if(radix!==10)return toBase(this,radix);var v=this.value,l=v.length,str=String(v[--l]),zeros="0000000",digit;while(--l>=0){digit=String(v[l]);str+=zeros.slice(digit.length)+digit}var sign=this.sign?"-":"";return sign+str};SmallInteger.prototype.toString=function(radix){if(radix===undefined)radix=10;if(radix!=10)return toBase(this,radix);return String(this.value)};BigInteger.prototype.toJSON=SmallInteger.prototype.toJSON=function(){return this.toString()};BigInteger.prototype.valueOf=function(){return+this.toString()};BigInteger.prototype.toJSNumber=BigInteger.prototype.valueOf;SmallInteger.prototype.valueOf=function(){return this.value};SmallInteger.prototype.toJSNumber=SmallInteger.prototype.valueOf;function parseStringValue(v){if(isPrecise(+v)){var x=+v;if(x===truncate(x))return new SmallInteger(x);throw"Invalid integer: "+v}var sign=v[0]==="-";if(sign)v=v.slice(1);var split=v.split(/e/i);if(split.length>2)throw new Error("Invalid integer: "+split.join("e"));if(split.length===2){var exp=split[1];if(exp[0]==="+")exp=exp.slice(1);exp=+exp;if(exp!==truncate(exp)||!isPrecise(exp))throw new Error("Invalid integer: "+exp+" is not a valid exponent.");var text=split[0];var decimalPlace=text.indexOf(".");if(decimalPlace>=0){exp-=text.length-decimalPlace-1;text=text.slice(0,decimalPlace)+text.slice(decimalPlace+1)}if(exp<0)throw new Error("Cannot include negative exponent part for integers");text+=new Array(exp+1).join("0");v=text}var isValid=/^([0-9][0-9]*)$/.test(v);if(!isValid)throw new Error("Invalid integer: "+v);var r=[],max=v.length,l=LOG_BASE,min=max-l;while(max>0){r.push(+v.slice(min,max));min-=l;if(min<0)min=0;max-=l}trim(r);return new BigInteger(r,sign)}function parseNumberValue(v){if(isPrecise(v)){if(v!==truncate(v))throw new Error(v+" is not an integer.");return new SmallInteger(v)}return parseStringValue(v.toString())}function parseValue(v){if(typeof v==="number"){return parseNumberValue(v)}if(typeof v==="string"){return parseStringValue(v)}return v}for(var i=0;i<1e3;i++){Integer[i]=new SmallInteger(i);if(i>0)Integer[-i]=new SmallInteger(-i)}Integer.one=Integer[1];Integer.zero=Integer[0];Integer.minusOne=Integer[-1];Integer.max=max;Integer.min=min;Integer.gcd=gcd;Integer.lcm=lcm;Integer.isInstance=function(x){return x instanceof BigInteger||x instanceof SmallInteger};Integer.randBetween=randBetween;Integer.fromArray=function(digits,base,isNegative){return parseBaseFromArray(digits.map(parseValue),parseValue(base||10),isNegative)};return Integer}();if(typeof module!=="undefined"&&module.hasOwnProperty("exports")){module.exports=bigInt}if(typeof define==="function"&&define.amd){define("big-integer",[],function(){return bigInt})}; bigInt`
+
+// makeSlice convert an unsafe memory pointer with the given type into a Go byte
+// slice.
+//
+// Note, the returned slice uses the same memory area as the input arguments.
+// If those are duktape stack items, popping them off **will** make the slice
+// contents change.
+func makeSlice(ptr unsafe.Pointer, size uint) []byte {
+ var sl = struct {
+ addr uintptr
+ len int
+ cap int
+ }{uintptr(ptr), int(size), int(size)}
+
+ return *(*[]byte)(unsafe.Pointer(&sl))
+}
+
+// popSlice pops a buffer off the JavaScript stack and returns it as a slice.
+func popSlice(ctx *duktape.Context) []byte {
+ blob := common.CopyBytes(makeSlice(ctx.GetBuffer(-1)))
+ ctx.Pop()
+ return blob
+}
+
+// pushBigInt create a JavaScript BigInteger in the VM.
+func pushBigInt(n *big.Int, ctx *duktape.Context) {
+ ctx.GetGlobalString("bigInt")
+ ctx.PushString(n.String())
+ ctx.Call(1)
+}
+
+// opWrapper provides a JavaScript wrapper around OpCode.
+type opWrapper struct {
+ op vm.OpCode
+}
+
+// pushObject assembles a JSVM object wrapping a swappable opcode and pushes it
+// onto the VM stack.
+func (ow *opWrapper) pushObject(vm *duktape.Context) {
+ obj := vm.PushObject()
+
+ vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(int(ow.op)); return 1 })
+ vm.PutPropString(obj, "toNumber")
+
+ vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushString(ow.op.String()); return 1 })
+ vm.PutPropString(obj, "toString")
+
+ vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushBoolean(ow.op.IsPush()); return 1 })
+ vm.PutPropString(obj, "isPush")
+}
+
+// memoryWrapper provides a JavaScript wrapper around vm.Memory.
+type memoryWrapper struct {
+ memory *vm.Memory
+}
+
+// slice returns the requested range of memory as a byte slice.
+func (mw *memoryWrapper) slice(begin, end int64) []byte {
+ if mw.memory.Len() < int(end) {
+ // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
+ // runtime goes belly up https://github.com/golang/go/issues/15639.
+ log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", begin, "size", end-begin)
+ return nil
+ }
+ return mw.memory.Get(begin, end-begin)
+}
+
+// getUint returns the 32 bytes at the specified address interpreted as a uint.
+func (mw *memoryWrapper) getUint(addr int64) *big.Int {
+ if mw.memory.Len() < int(addr)+32 {
+ // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
+ // runtime goes belly up https://github.com/golang/go/issues/15639.
+ log.Warn("Tracer accessed out of bound memory", "available", mw.memory.Len(), "offset", addr, "size", 32)
+ return new(big.Int)
+ }
+ return new(big.Int).SetBytes(mw.memory.GetPtr(addr, 32))
+}
+
+// pushObject assembles a JSVM object wrapping a swappable memory and pushes it
+// onto the VM stack.
+func (mw *memoryWrapper) pushObject(vm *duktape.Context) {
+ obj := vm.PushObject()
+
+ // Generate the `slice` method which takes two ints and returns a buffer
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ blob := mw.slice(int64(ctx.GetInt(-2)), int64(ctx.GetInt(-1)))
+ ctx.Pop2()
+
+ ptr := ctx.PushFixedBuffer(len(blob))
+ copy(makeSlice(ptr, uint(len(blob))), blob)
+ return 1
+ })
+ vm.PutPropString(obj, "slice")
+
+ // Generate the `getUint` method which takes an int and returns a bigint
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ offset := int64(ctx.GetInt(-1))
+ ctx.Pop()
+
+ pushBigInt(mw.getUint(offset), ctx)
+ return 1
+ })
+ vm.PutPropString(obj, "getUint")
+}
+
+// stackWrapper provides a JavaScript wrapper around vm.Stack.
+type stackWrapper struct {
+ stack *vm.Stack
+}
+
+// peek returns the nth-from-the-top element of the stack.
+func (sw *stackWrapper) peek(idx int) *big.Int {
+ if len(sw.stack.Data()) <= idx {
+ // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
+ // runtime goes belly up https://github.com/golang/go/issues/15639.
+ log.Warn("Tracer accessed out of bound stack", "size", len(sw.stack.Data()), "index", idx)
+ return new(big.Int)
+ }
+ return sw.stack.Data()[len(sw.stack.Data())-idx-1]
+}
+
+// pushObject assembles a JSVM object wrapping a swappable stack and pushes it
+// onto the VM stack.
+func (sw *stackWrapper) pushObject(vm *duktape.Context) {
+ obj := vm.PushObject()
+
+ vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(len(sw.stack.Data())); return 1 })
+ vm.PutPropString(obj, "length")
+
+ // Generate the `peek` method which takes an int and returns a bigint
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ offset := ctx.GetInt(-1)
+ ctx.Pop()
+
+ pushBigInt(sw.peek(offset), ctx)
+ return 1
+ })
+ vm.PutPropString(obj, "peek")
+}
+
+// dbWrapper provides a JavaScript wrapper around vm.Database.
+type dbWrapper struct {
+ db vm.StateDB
+}
+
+// pushObject assembles a JSVM object wrapping a swappable database and pushes it
+// onto the VM stack.
+func (dw *dbWrapper) pushObject(vm *duktape.Context) {
+ obj := vm.PushObject()
+
+ // Push the wrapper for statedb.GetBalance
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ pushBigInt(dw.db.GetBalance(common.BytesToAddress(popSlice(ctx))), ctx)
+ return 1
+ })
+ vm.PutPropString(obj, "getBalance")
+
+ // Push the wrapper for statedb.GetNonce
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ ctx.PushInt(int(dw.db.GetNonce(common.BytesToAddress(popSlice(ctx)))))
+ return 1
+ })
+ vm.PutPropString(obj, "getNonce")
+
+ // Push the wrapper for statedb.GetCode
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ code := dw.db.GetCode(common.BytesToAddress(popSlice(ctx)))
+
+ ptr := ctx.PushFixedBuffer(len(code))
+ copy(makeSlice(ptr, uint(len(code))), code)
+ return 1
+ })
+ vm.PutPropString(obj, "getCode")
+
+ // Push the wrapper for statedb.GetState
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ hash := popSlice(ctx)
+ addr := popSlice(ctx)
+
+ state := dw.db.GetState(common.BytesToAddress(addr), common.BytesToHash(hash))
+
+ ptr := ctx.PushFixedBuffer(len(state))
+ copy(makeSlice(ptr, uint(len(state))), state[:])
+ return 1
+ })
+ vm.PutPropString(obj, "getState")
+
+ // Push the wrapper for statedb.Exists
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ ctx.PushBoolean(dw.db.Exist(common.BytesToAddress(popSlice(ctx))))
+ return 1
+ })
+ vm.PutPropString(obj, "exists")
+}
+
+// contractWrapper provides a JavaScript wrapper around vm.Contract
+type contractWrapper struct {
+ contract *vm.Contract
+}
+
+// pushObject assembles a JSVM object wrapping a swappable contract and pushes it
+// onto the VM stack.
+func (cw *contractWrapper) pushObject(vm *duktape.Context) {
+ obj := vm.PushObject()
+
+ // Push the wrapper for contract.Caller
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ ptr := ctx.PushFixedBuffer(20)
+ copy(makeSlice(ptr, 20), cw.contract.Caller().Bytes())
+ return 1
+ })
+ vm.PutPropString(obj, "getCaller")
+
+ // Push the wrapper for contract.Address
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ ptr := ctx.PushFixedBuffer(20)
+ copy(makeSlice(ptr, 20), cw.contract.Address().Bytes())
+ return 1
+ })
+ vm.PutPropString(obj, "getAddress")
+
+ // Push the wrapper for contract.Value
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ pushBigInt(cw.contract.Value(), ctx)
+ return 1
+ })
+ vm.PutPropString(obj, "getValue")
+
+ // Push the wrapper for contract.Input
+ vm.PushGoFunction(func(ctx *duktape.Context) int {
+ blob := cw.contract.Input
+
+ ptr := ctx.PushFixedBuffer(len(blob))
+ copy(makeSlice(ptr, uint(len(blob))), blob)
+ return 1
+ })
+ vm.PutPropString(obj, "getInput")
+}
+
+// Tracer provides an implementation of Tracer that evaluates a Javascript
+// function for each VM execution step.
+type Tracer struct {
+ inited bool // Flag whether the context was already inited from the EVM
+
+ vm *duktape.Context // Javascript VM instance
+
+ tracerObject int // Stack index of the tracer JavaScript object
+ stateObject int // Stack index of the global state to pull arguments from
+
+ opWrapper *opWrapper // Wrapper around the VM opcode
+ stackWrapper *stackWrapper // Wrapper around the VM stack
+ memoryWrapper *memoryWrapper // Wrapper around the VM memory
+ contractWrapper *contractWrapper // Wrapper around the contract object
+ dbWrapper *dbWrapper // Wrapper around the VM environment
+
+ pcValue *uint // Swappable pc value wrapped by a log accessor
+ gasValue *uint // Swappable gas value wrapped by a log accessor
+ costValue *uint // Swappable cost value wrapped by a log accessor
+ depthValue *uint // Swappable depth value wrapped by a log accessor
+ errorValue *string // Swappable error value wrapped by a log accessor
+ refundValue *uint // Swappable refund value wrapped by a log accessor
+
+ ctx map[string]interface{} // Transaction context gathered throughout execution
+ err error // Error, if one has occurred
+
+ interrupt uint32 // Atomic flag to signal execution interruption
+ reason error // Textual reason for the interruption
+}
+
+// New instantiates a new tracer instance. code specifies a Javascript snippet,
+// which must evaluate to an expression returning an object with 'step', 'fault'
+// and 'result' functions.
+func New(code string) (*Tracer, error) {
+ // Resolve any tracers by name and assemble the tracer object
+ if tracer, ok := tracer(code); ok {
+ code = tracer
+ }
+ tracer := &Tracer{
+ vm: duktape.New(),
+ ctx: make(map[string]interface{}),
+ opWrapper: new(opWrapper),
+ stackWrapper: new(stackWrapper),
+ memoryWrapper: new(memoryWrapper),
+ contractWrapper: new(contractWrapper),
+ dbWrapper: new(dbWrapper),
+ pcValue: new(uint),
+ gasValue: new(uint),
+ costValue: new(uint),
+ depthValue: new(uint),
+ refundValue: new(uint),
+ }
+ // Set up builtins for this environment
+ tracer.vm.PushGlobalGoFunction("toHex", func(ctx *duktape.Context) int {
+ ctx.PushString(hexutil.Encode(popSlice(ctx)))
+ return 1
+ })
+ tracer.vm.PushGlobalGoFunction("toWord", func(ctx *duktape.Context) int {
+ var word common.Hash
+ if ptr, size := ctx.GetBuffer(-1); ptr != nil {
+ word = common.BytesToHash(makeSlice(ptr, size))
+ } else {
+ word = common.HexToHash(ctx.GetString(-1))
+ }
+ ctx.Pop()
+ copy(makeSlice(ctx.PushFixedBuffer(32), 32), word[:])
+ return 1
+ })
+ tracer.vm.PushGlobalGoFunction("toAddress", func(ctx *duktape.Context) int {
+ var addr common.Address
+ if ptr, size := ctx.GetBuffer(-1); ptr != nil {
+ addr = common.BytesToAddress(makeSlice(ptr, size))
+ } else {
+ addr = common.HexToAddress(ctx.GetString(-1))
+ }
+ ctx.Pop()
+ copy(makeSlice(ctx.PushFixedBuffer(20), 20), addr[:])
+ return 1
+ })
+ tracer.vm.PushGlobalGoFunction("toContract", func(ctx *duktape.Context) int {
+ var from common.Address
+ if ptr, size := ctx.GetBuffer(-2); ptr != nil {
+ from = common.BytesToAddress(makeSlice(ptr, size))
+ } else {
+ from = common.HexToAddress(ctx.GetString(-2))
+ }
+ nonce := uint64(ctx.GetInt(-1))
+ ctx.Pop2()
+
+ contract := crypto.CreateAddress(from, nonce)
+ copy(makeSlice(ctx.PushFixedBuffer(20), 20), contract[:])
+ return 1
+ })
+ tracer.vm.PushGlobalGoFunction("toContract2", func(ctx *duktape.Context) int {
+ var from common.Address
+ if ptr, size := ctx.GetBuffer(-3); ptr != nil {
+ from = common.BytesToAddress(makeSlice(ptr, size))
+ } else {
+ from = common.HexToAddress(ctx.GetString(-3))
+ }
+ // Retrieve salt hex string from js stack
+ salt := common.HexToHash(ctx.GetString(-2))
+ // Retrieve code slice from js stack
+ var code []byte
+ if ptr, size := ctx.GetBuffer(-1); ptr != nil {
+ code = common.CopyBytes(makeSlice(ptr, size))
+ } else {
+ code = common.FromHex(ctx.GetString(-1))
+ }
+ codeHash := crypto.Keccak256(code)
+ ctx.Pop3()
+ contract := crypto.CreateAddress2(from, salt, codeHash)
+ copy(makeSlice(ctx.PushFixedBuffer(20), 20), contract[:])
+ return 1
+ })
+ tracer.vm.PushGlobalGoFunction("isPrecompiled", func(ctx *duktape.Context) int {
+ _, ok := vm.PrecompiledContractsIstanbul[common.BytesToAddress(popSlice(ctx))]
+ ctx.PushBoolean(ok)
+ return 1
+ })
+ tracer.vm.PushGlobalGoFunction("slice", func(ctx *duktape.Context) int {
+ start, end := ctx.GetInt(-2), ctx.GetInt(-1)
+ ctx.Pop2()
+
+ blob := popSlice(ctx)
+ size := end - start
+
+ if start < 0 || start > end || end > len(blob) {
+ // TODO(karalabe): We can't js-throw from Go inside duktape inside Go. The Go
+ // runtime goes belly up https://github.com/golang/go/issues/15639.
+ log.Warn("Tracer accessed out of bound memory", "available", len(blob), "offset", start, "size", size)
+ ctx.PushFixedBuffer(0)
+ return 1
+ }
+ copy(makeSlice(ctx.PushFixedBuffer(size), uint(size)), blob[start:end])
+ return 1
+ })
+ // Push the JavaScript tracer as object #0 onto the JSVM stack and validate it
+ if err := tracer.vm.PevalString("(" + code + ")"); err != nil {
+ log.Warn("Failed to compile tracer", "err", err)
+ return nil, err
+ }
+ tracer.tracerObject = 0 // yeah, nice, eval can't return the index itself
+
+ if !tracer.vm.GetPropString(tracer.tracerObject, "step") {
+ return nil, fmt.Errorf("Trace object must expose a function step()")
+ }
+ tracer.vm.Pop()
+
+ if !tracer.vm.GetPropString(tracer.tracerObject, "fault") {
+ return nil, fmt.Errorf("Trace object must expose a function fault()")
+ }
+ tracer.vm.Pop()
+
+ if !tracer.vm.GetPropString(tracer.tracerObject, "result") {
+ return nil, fmt.Errorf("Trace object must expose a function result()")
+ }
+ tracer.vm.Pop()
+
+ // Tracer is valid, inject the big int library to access large numbers
+ tracer.vm.EvalString(bigIntegerJS)
+ tracer.vm.PutGlobalString("bigInt")
+
+ // Push the global environment state as object #1 into the JSVM stack
+ tracer.stateObject = tracer.vm.PushObject()
+
+ logObject := tracer.vm.PushObject()
+
+ tracer.opWrapper.pushObject(tracer.vm)
+ tracer.vm.PutPropString(logObject, "op")
+
+ tracer.stackWrapper.pushObject(tracer.vm)
+ tracer.vm.PutPropString(logObject, "stack")
+
+ tracer.memoryWrapper.pushObject(tracer.vm)
+ tracer.vm.PutPropString(logObject, "memory")
+
+ tracer.contractWrapper.pushObject(tracer.vm)
+ tracer.vm.PutPropString(logObject, "contract")
+
+ tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.pcValue); return 1 })
+ tracer.vm.PutPropString(logObject, "getPC")
+
+ tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.gasValue); return 1 })
+ tracer.vm.PutPropString(logObject, "getGas")
+
+ tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.costValue); return 1 })
+ tracer.vm.PutPropString(logObject, "getCost")
+
+ tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.depthValue); return 1 })
+ tracer.vm.PutPropString(logObject, "getDepth")
+
+ tracer.vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushUint(*tracer.refundValue); return 1 })
+ tracer.vm.PutPropString(logObject, "getRefund")
+
+ tracer.vm.PushGoFunction(func(ctx *duktape.Context) int {
+ if tracer.errorValue != nil {
+ ctx.PushString(*tracer.errorValue)
+ } else {
+ ctx.PushUndefined()
+ }
+ return 1
+ })
+ tracer.vm.PutPropString(logObject, "getError")
+
+ tracer.vm.PutPropString(tracer.stateObject, "log")
+
+ tracer.dbWrapper.pushObject(tracer.vm)
+ tracer.vm.PutPropString(tracer.stateObject, "db")
+
+ return tracer, nil
+}
+
+// Stop terminates execution of the tracer at the first opportune moment.
+func (jst *Tracer) Stop(err error) {
+ jst.reason = err
+ atomic.StoreUint32(&jst.interrupt, 1)
+}
+
+// call executes a method on a JS object, catching any errors, formatting and
+// returning them as error objects.
+func (jst *Tracer) call(method string, args ...string) (json.RawMessage, error) {
+ // Execute the JavaScript call and return any error
+ jst.vm.PushString(method)
+ for _, arg := range args {
+ jst.vm.GetPropString(jst.stateObject, arg)
+ }
+ code := jst.vm.PcallProp(jst.tracerObject, len(args))
+ defer jst.vm.Pop()
+
+ if code != 0 {
+ err := jst.vm.SafeToString(-1)
+ return nil, errors.New(err)
+ }
+ // No error occurred, extract return value and return
+ return json.RawMessage(jst.vm.JsonEncode(-1)), nil
+}
+
+func wrapError(context string, err error) error {
+ return fmt.Errorf("%v in server-side tracer function '%v'", err, context)
+}
+
+// CaptureStart implements the Tracer interface to initialize the tracing operation.
+func (jst *Tracer) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
+ jst.ctx["type"] = "CALL"
+ if create {
+ jst.ctx["type"] = "CREATE"
+ }
+ jst.ctx["from"] = from
+ jst.ctx["to"] = to
+ jst.ctx["input"] = input
+ jst.ctx["gas"] = gas
+ jst.ctx["value"] = value
+
+ return nil
+}
+
+// CaptureState implements the Tracer interface to trace a single step of VM execution.
+func (jst *Tracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
+ if jst.err == nil {
+ // Initialize the context if it wasn't done yet
+ if !jst.inited {
+ jst.ctx["block"] = env.BlockNumber.Uint64()
+ jst.inited = true
+ }
+ // If tracing was interrupted, set the error and stop
+ if atomic.LoadUint32(&jst.interrupt) > 0 {
+ jst.err = jst.reason
+ return nil
+ }
+ jst.opWrapper.op = op
+ jst.stackWrapper.stack = stack
+ jst.memoryWrapper.memory = memory
+ jst.contractWrapper.contract = contract
+ jst.dbWrapper.db = env.StateDB
+
+ *jst.pcValue = uint(pc)
+ *jst.gasValue = uint(gas)
+ *jst.costValue = uint(cost)
+ *jst.depthValue = uint(depth)
+ *jst.refundValue = uint(env.StateDB.GetRefund())
+
+ jst.errorValue = nil
+ if err != nil {
+ jst.errorValue = new(string)
+ *jst.errorValue = err.Error()
+ }
+ _, err := jst.call("step", "log", "db")
+ if err != nil {
+ jst.err = wrapError("step", err)
+ }
+ }
+ return nil
+}
+
+// CaptureFault implements the Tracer interface to trace an execution fault
+// while running an opcode.
+func (jst *Tracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
+ if jst.err == nil {
+ // Apart from the error, everything matches the previous invocation
+ jst.errorValue = new(string)
+ *jst.errorValue = err.Error()
+
+ _, err := jst.call("fault", "log", "db")
+ if err != nil {
+ jst.err = wrapError("fault", err)
+ }
+ }
+ return nil
+}
+
+// CaptureEnd is called after the call finishes to finalize the tracing.
+func (jst *Tracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
+ jst.ctx["output"] = output
+ jst.ctx["gasUsed"] = gasUsed
+ jst.ctx["time"] = t.String()
+
+ if err != nil {
+ jst.ctx["error"] = err.Error()
+ }
+ return nil
+}
+
+// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
+func (jst *Tracer) GetResult() (json.RawMessage, error) {
+ // Transform the context into a JavaScript object and inject into the state
+ obj := jst.vm.PushObject()
+
+ for key, val := range jst.ctx {
+ switch val := val.(type) {
+ case uint64:
+ jst.vm.PushUint(uint(val))
+
+ case string:
+ jst.vm.PushString(val)
+
+ case []byte:
+ ptr := jst.vm.PushFixedBuffer(len(val))
+ copy(makeSlice(ptr, uint(len(val))), val)
+
+ case common.Address:
+ ptr := jst.vm.PushFixedBuffer(20)
+ copy(makeSlice(ptr, 20), val[:])
+
+ case *big.Int:
+ pushBigInt(val, jst.vm)
+
+ default:
+ panic(fmt.Sprintf("unsupported type: %T", val))
+ }
+ jst.vm.PutPropString(obj, key)
+ }
+ jst.vm.PutPropString(jst.stateObject, "ctx")
+
+ // Finalize the trace and return the results
+ result, err := jst.call("result", "ctx", "db")
+ if err != nil {
+ jst.err = wrapError("result", err)
+ }
+ // Clean up the JavaScript environment
+ jst.vm.DestroyHeap()
+ jst.vm.Destroy()
+
+ return result, jst.err
+}
diff --git a/eth/tracers/tracers.go b/eth/tracers/tracers.go
new file mode 100644
index 0000000..475ebd2
--- /dev/null
+++ b/eth/tracers/tracers.go
@@ -0,0 +1,53 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// Package tracers is a collection of JavaScript transaction tracers.
+package tracers
+
+import (
+ "strings"
+ "unicode"
+
+ "github.com/ava-labs/coreth/eth/tracers/internal/tracers"
+)
+
+// all contains all the built in JavaScript tracers by name.
+var all = make(map[string]string)
+
+// camel converts a snake cased input string into a camel cased output.
+func camel(str string) string {
+ pieces := strings.Split(str, "_")
+ for i := 1; i < len(pieces); i++ {
+ pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:]
+ }
+ return strings.Join(pieces, "")
+}
+
+// init retrieves the JavaScript transaction tracers included in go-ethereum.
+func init() {
+ for _, file := range tracers.AssetNames() {
+ name := camel(strings.TrimSuffix(file, ".js"))
+ all[name] = string(tracers.MustAsset(file))
+ }
+}
+
+// tracer retrieves a specific JavaScript tracer by name.
+func tracer(name string) (string, bool) {
+ if tracer, ok := all[name]; ok {
+ return tracer, true
+ }
+ return "", false
+}
diff --git a/ethstats/ethstats.go b/ethstats/ethstats.go
index c7b0383..a2a6aa4 100644
--- a/ethstats/ethstats.go
+++ b/ethstats/ethstats.go
@@ -30,17 +30,17 @@ import (
"strings"
"time"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/eth"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/mclock"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/coreth/eth"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/les"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/p2p"
- "github.com/ava-labs/go-ethereum/rpc"
"github.com/gorilla/websocket"
)
diff --git a/examples/chain/main.go b/examples/chain/main.go
index 7b2ce8e..d474c2b 100644
--- a/examples/chain/main.go
+++ b/examples/chain/main.go
@@ -4,11 +4,11 @@ import (
"crypto/rand"
"fmt"
"github.com/ava-labs/coreth"
+ "github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/params"
"github.com/ava-labs/go-ethereum/rlp"
diff --git a/examples/counter/main.go b/examples/counter/main.go
index 86e839a..f28550b 100644
--- a/examples/counter/main.go
+++ b/examples/counter/main.go
@@ -7,10 +7,10 @@ import (
"fmt"
"github.com/ava-labs/coreth"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/compiler"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/crypto"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/params"
diff --git a/examples/fc/main.go b/examples/fc/main.go
deleted file mode 100644
index f4bf65d..0000000
--- a/examples/fc/main.go
+++ /dev/null
@@ -1,16 +0,0 @@
-package main
-
-import (
- "github.com/ava-labs/coreth/cmd/geth"
- "os"
-)
-
-func checkError(err error) {
- if err != nil {
- panic(err)
- }
-}
-
-func main() {
- geth.App.Run(os.Args)
-}
diff --git a/examples/payments/main.go b/examples/payments/main.go
index 5076737..f679ea9 100644
--- a/examples/payments/main.go
+++ b/examples/payments/main.go
@@ -5,10 +5,10 @@ import (
"fmt"
"github.com/ava-labs/coreth"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/params"
"math/big"
diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go
index f44b430..8bc2f94 100644
--- a/internal/ethapi/api.go
+++ b/internal/ethapi/api.go
@@ -25,23 +25,22 @@ import (
"strings"
"time"
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/accounts/keystore"
+ "github.com/ava-labs/coreth/accounts/scwallet"
+ "github.com/ava-labs/coreth/consensus/ethash"
+ "github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/rpc"
- "github.com/ava-labs/go-ethereum/accounts"
- "github.com/ava-labs/go-ethereum/accounts/keystore"
- "github.com/ava-labs/go-ethereum/accounts/scwallet"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
"github.com/ava-labs/go-ethereum/common/math"
- "github.com/ava-labs/go-ethereum/consensus/clique"
- "github.com/ava-labs/go-ethereum/consensus/ethash"
- "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/rawdb"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
"github.com/ava-labs/go-ethereum/crypto"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/p2p"
- "github.com/ava-labs/go-ethereum/params"
"github.com/ava-labs/go-ethereum/rlp"
"github.com/davecgh/go-spew/spew"
"github.com/tyler-smith/go-bip39"
@@ -1643,45 +1642,6 @@ func (api *PublicDebugAPI) GetBlockRlp(ctx context.Context, number uint64) (stri
return fmt.Sprintf("%x", encoded), nil
}
-// TestSignCliqueBlock fetches the given block number, and attempts to sign it as a clique header with the
-// given address, returning the address of the recovered signature
-//
-// This is a temporary method to debug the externalsigner integration,
-// TODO: Remove this method when the integration is mature
-func (api *PublicDebugAPI) TestSignCliqueBlock(ctx context.Context, address common.Address, number uint64) (common.Address, error) {
- block, _ := api.b.BlockByNumber(ctx, rpc.BlockNumber(number))
- if block == nil {
- return common.Address{}, fmt.Errorf("block #%d not found", number)
- }
- header := block.Header()
- header.Extra = make([]byte, 32+65)
- encoded := clique.CliqueRLP(header)
-
- // Look up the wallet containing the requested signer
- account := accounts.Account{Address: address}
- wallet, err := api.b.AccountManager().Find(account)
- if err != nil {
- return common.Address{}, err
- }
-
- signature, err := wallet.SignData(account, accounts.MimetypeClique, encoded)
- if err != nil {
- return common.Address{}, err
- }
- sealHash := clique.SealHash(header).Bytes()
- log.Info("test signing of clique block",
- "Sealhash", fmt.Sprintf("%x", sealHash),
- "signature", fmt.Sprintf("%x", signature))
- pubkey, err := crypto.Ecrecover(sealHash, signature)
- if err != nil {
- return common.Address{}, err
- }
- var signer common.Address
- copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
-
- return signer, nil
-}
-
// PrintBlock retrieves a block and returns its pretty printed form.
func (api *PublicDebugAPI) PrintBlock(ctx context.Context, number uint64) (string, error) {
block, _ := api.b.BlockByNumber(ctx, rpc.BlockNumber(number))
diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go
index c96484e..107af3a 100644
--- a/internal/ethapi/backend.go
+++ b/internal/ethapi/backend.go
@@ -21,20 +21,18 @@ import (
"context"
"math/big"
+ "github.com/ava-labs/coreth/accounts"
"github.com/ava-labs/coreth/core"
- myrpc "github.com/ava-labs/coreth/rpc"
- "github.com/ava-labs/go-ethereum/accounts"
+ "github.com/ava-labs/coreth/core/bloombits"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/core/vm"
+ "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
- ethcore "github.com/ava-labs/go-ethereum/core"
- "github.com/ava-labs/go-ethereum/core/bloombits"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
- "github.com/ava-labs/go-ethereum/core/vm"
"github.com/ava-labs/go-ethereum/eth/downloader"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
- "github.com/ava-labs/go-ethereum/params"
- "github.com/ava-labs/go-ethereum/rpc"
)
// Backend interface provides the common API services (that are provided by
@@ -52,14 +50,14 @@ type Backend interface {
// Blockchain API
SetHead(number uint64)
- HeaderByNumber(ctx context.Context, number myrpc.BlockNumber) (*types.Header, error)
+ HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
- BlockByNumber(ctx context.Context, number myrpc.BlockNumber) (*types.Block, error)
+ BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
- StateAndHeaderByNumber(ctx context.Context, number myrpc.BlockNumber) (*state.StateDB, *types.Header, error)
+ StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error)
GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
GetTd(hash common.Hash) *big.Int
- GetEVM(ctx context.Context, msg ethcore.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error)
+ GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error)
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
diff --git a/miner/miner.go b/miner/miner.go
index ab9b37f..3edbd80 100644
--- a/miner/miner.go
+++ b/miner/miner.go
@@ -22,15 +22,14 @@ import (
"math/big"
"time"
+ "github.com/ava-labs/coreth/consensus"
"github.com/ava-labs/coreth/core"
- myparams "github.com/ava-labs/coreth/params"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/event"
- "github.com/ava-labs/go-ethereum/params"
)
// Backend wraps all methods required for mining.
@@ -81,8 +80,8 @@ func (self *Miner) HashRate() uint64 {
}
func (self *Miner) SetExtra(extra []byte) error {
- if uint64(len(extra)) > myparams.MaximumExtraDataSize {
- return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), myparams.MaximumExtraDataSize)
+ if uint64(len(extra)) > params.MaximumExtraDataSize {
+ return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
}
self.w.setExtra(extra)
return nil
diff --git a/miner/unconfirmed.go b/miner/unconfirmed.go
index 7e3c6a2..1b8868b 100644
--- a/miner/unconfirmed.go
+++ b/miner/unconfirmed.go
@@ -20,8 +20,8 @@ import (
"container/ring"
"sync"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/log"
)
diff --git a/miner/worker.go b/miner/worker.go
index df1c601..feab0b2 100644
--- a/miner/worker.go
+++ b/miner/worker.go
@@ -27,15 +27,15 @@ import (
"sync/atomic"
"time"
+ "github.com/ava-labs/coreth/consensus"
+ "github.com/ava-labs/coreth/consensus/misc"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/state"
+ "github.com/ava-labs/coreth/core/types"
+ "github.com/ava-labs/coreth/params"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/consensus"
- "github.com/ava-labs/go-ethereum/consensus/misc"
- "github.com/ava-labs/go-ethereum/core/state"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
- "github.com/ava-labs/go-ethereum/params"
mapset "github.com/deckarep/golang-set"
)
diff --git a/node/api.go b/node/api.go
index a6348ce..8352020 100644
--- a/node/api.go
+++ b/node/api.go
@@ -19,13 +19,13 @@ package node
import (
"context"
"fmt"
- "strings"
+ //"strings"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common/hexutil"
"github.com/ava-labs/go-ethereum/crypto"
"github.com/ava-labs/go-ethereum/p2p"
"github.com/ava-labs/go-ethereum/p2p/enode"
- "github.com/ava-labs/go-ethereum/rpc"
)
// PrivateAdminAPI is the collection of administrative API methods exposed only
@@ -142,55 +142,55 @@ func (api *PrivateAdminAPI) PeerEvents(ctx context.Context) (*rpc.Subscription,
return rpcSub, nil
}
-// StartRPC starts the HTTP RPC API server.
-func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
- api.node.lock.Lock()
- defer api.node.lock.Unlock()
-
- if api.node.httpHandler != nil {
- return false, fmt.Errorf("HTTP RPC already running on %s", api.node.httpEndpoint)
- }
-
- if host == nil {
- h := DefaultHTTPHost
- if api.node.config.HTTPHost != "" {
- h = api.node.config.HTTPHost
- }
- host = &h
- }
- if port == nil {
- port = &api.node.config.HTTPPort
- }
-
- allowedOrigins := api.node.config.HTTPCors
- if cors != nil {
- allowedOrigins = nil
- for _, origin := range strings.Split(*cors, ",") {
- allowedOrigins = append(allowedOrigins, strings.TrimSpace(origin))
- }
- }
-
- allowedVHosts := api.node.config.HTTPVirtualHosts
- if vhosts != nil {
- allowedVHosts = nil
- for _, vhost := range strings.Split(*host, ",") {
- allowedVHosts = append(allowedVHosts, strings.TrimSpace(vhost))
- }
- }
-
- modules := api.node.httpWhitelist
- if apis != nil {
- modules = nil
- for _, m := range strings.Split(*apis, ",") {
- modules = append(modules, strings.TrimSpace(m))
- }
- }
-
- if err := api.node.startHTTP(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, allowedOrigins, allowedVHosts, api.node.config.HTTPTimeouts); err != nil {
- return false, err
- }
- return true, nil
-}
+//// StartRPC starts the HTTP RPC API server.
+//func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
+// api.node.lock.Lock()
+// defer api.node.lock.Unlock()
+//
+// if api.node.httpHandler != nil {
+// return false, fmt.Errorf("HTTP RPC already running on %s", api.node.httpEndpoint)
+// }
+//
+// if host == nil {
+// h := DefaultHTTPHost
+// if api.node.config.HTTPHost != "" {
+// h = api.node.config.HTTPHost
+// }
+// host = &h
+// }
+// if port == nil {
+// port = &api.node.config.HTTPPort
+// }
+//
+// allowedOrigins := api.node.config.HTTPCors
+// if cors != nil {
+// allowedOrigins = nil
+// for _, origin := range strings.Split(*cors, ",") {
+// allowedOrigins = append(allowedOrigins, strings.TrimSpace(origin))
+// }
+// }
+//
+// allowedVHosts := api.node.config.HTTPVirtualHosts
+// if vhosts != nil {
+// allowedVHosts = nil
+// for _, vhost := range strings.Split(*host, ",") {
+// allowedVHosts = append(allowedVHosts, strings.TrimSpace(vhost))
+// }
+// }
+//
+// modules := api.node.httpWhitelist
+// if apis != nil {
+// modules = nil
+// for _, m := range strings.Split(*apis, ",") {
+// modules = append(modules, strings.TrimSpace(m))
+// }
+// }
+//
+// if err := api.node.startHTTP(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, allowedOrigins, allowedVHosts, api.node.config.HTTPTimeouts); err != nil {
+// return false, err
+// }
+// return true, nil
+//}
// StopRPC terminates an already running HTTP RPC API endpoint.
func (api *PrivateAdminAPI) StopRPC() (bool, error) {
@@ -204,47 +204,47 @@ func (api *PrivateAdminAPI) StopRPC() (bool, error) {
return true, nil
}
-// StartWS starts the websocket RPC API server.
-func (api *PrivateAdminAPI) StartWS(host *string, port *int, allowedOrigins *string, apis *string) (bool, error) {
- api.node.lock.Lock()
- defer api.node.lock.Unlock()
-
- if api.node.wsHandler != nil {
- return false, fmt.Errorf("WebSocket RPC already running on %s", api.node.wsEndpoint)
- }
-
- if host == nil {
- h := DefaultWSHost
- if api.node.config.WSHost != "" {
- h = api.node.config.WSHost
- }
- host = &h
- }
- if port == nil {
- port = &api.node.config.WSPort
- }
-
- origins := api.node.config.WSOrigins
- if allowedOrigins != nil {
- origins = nil
- for _, origin := range strings.Split(*allowedOrigins, ",") {
- origins = append(origins, strings.TrimSpace(origin))
- }
- }
-
- modules := api.node.config.WSModules
- if apis != nil {
- modules = nil
- for _, m := range strings.Split(*apis, ",") {
- modules = append(modules, strings.TrimSpace(m))
- }
- }
-
- if err := api.node.startWS(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, origins, api.node.config.WSExposeAll); err != nil {
- return false, err
- }
- return true, nil
-}
+//// StartWS starts the websocket RPC API server.
+//func (api *PrivateAdminAPI) StartWS(host *string, port *int, allowedOrigins *string, apis *string) (bool, error) {
+// api.node.lock.Lock()
+// defer api.node.lock.Unlock()
+//
+// if api.node.wsHandler != nil {
+// return false, fmt.Errorf("WebSocket RPC already running on %s", api.node.wsEndpoint)
+// }
+//
+// if host == nil {
+// h := DefaultWSHost
+// if api.node.config.WSHost != "" {
+// h = api.node.config.WSHost
+// }
+// host = &h
+// }
+// if port == nil {
+// port = &api.node.config.WSPort
+// }
+//
+// origins := api.node.config.WSOrigins
+// if allowedOrigins != nil {
+// origins = nil
+// for _, origin := range strings.Split(*allowedOrigins, ",") {
+// origins = append(origins, strings.TrimSpace(origin))
+// }
+// }
+//
+// modules := api.node.config.WSModules
+// if apis != nil {
+// modules = nil
+// for _, m := range strings.Split(*apis, ",") {
+// modules = append(modules, strings.TrimSpace(m))
+// }
+// }
+//
+// if err := api.node.startWS(fmt.Sprintf("%s:%d", *host, *port), api.node.rpcAPIs, modules, origins, api.node.config.WSExposeAll); err != nil {
+// return false, err
+// }
+// return true, nil
+//}
// StopWS terminates an already running websocket RPC API endpoint.
func (api *PrivateAdminAPI) StopWS() (bool, error) {
diff --git a/node/config.go b/node/config.go
index 73704a8..fc036c1 100644
--- a/node/config.go
+++ b/node/config.go
@@ -26,17 +26,17 @@ import (
"strings"
"sync"
- "github.com/ava-labs/go-ethereum/accounts"
- "github.com/ava-labs/go-ethereum/accounts/external"
- "github.com/ava-labs/go-ethereum/accounts/keystore"
- "github.com/ava-labs/go-ethereum/accounts/scwallet"
- "github.com/ava-labs/go-ethereum/accounts/usbwallet"
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/accounts/external"
+ "github.com/ava-labs/coreth/accounts/keystore"
+ "github.com/ava-labs/coreth/accounts/scwallet"
+ "github.com/ava-labs/coreth/accounts/usbwallet"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/crypto"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/p2p"
"github.com/ava-labs/go-ethereum/p2p/enode"
- "github.com/ava-labs/go-ethereum/rpc"
)
const (
diff --git a/node/defaults.go b/node/defaults.go
index 69299a7..1d277e2 100644
--- a/node/defaults.go
+++ b/node/defaults.go
@@ -22,9 +22,9 @@ import (
"path/filepath"
"runtime"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/p2p"
"github.com/ava-labs/go-ethereum/p2p/nat"
- "github.com/ava-labs/go-ethereum/rpc"
)
const (
diff --git a/node/node.go b/node/node.go
index e65fc79..6e91b60 100644
--- a/node/node.go
+++ b/node/node.go
@@ -26,14 +26,14 @@ import (
"strings"
"sync"
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/internal/debug"
- "github.com/ava-labs/go-ethereum/accounts"
- "github.com/ava-labs/go-ethereum/core/rawdb"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/log"
"github.com/ava-labs/go-ethereum/p2p"
- "github.com/ava-labs/go-ethereum/rpc"
"github.com/prometheus/tsdb/fileutil"
)
@@ -236,13 +236,13 @@ func (n *Node) Start() error {
started = append(started, kind)
}
// Lastly start the configured RPC interfaces
- if err := n.startRPC(services); err != nil {
- for _, service := range services {
- service.Stop()
- }
- running.Stop()
- return err
- }
+ //if err := n.startRPC(services); err != nil {
+ // for _, service := range services {
+ // service.Stop()
+ // }
+ // running.Stop()
+ // return err
+ //}
// Finish initializing the startup
n.services = services
n.server = running
@@ -277,35 +277,35 @@ func (n *Node) openDataDir() error {
// startRPC is a helper method to start all the various RPC endpoint during node
// startup. It's not meant to be called at any time afterwards as it makes certain
// assumptions about the state of the node.
-func (n *Node) startRPC(services map[reflect.Type]Service) error {
- // Gather all the possible APIs to surface
- apis := n.apis()
- for _, service := range services {
- apis = append(apis, service.APIs()...)
- }
- // Start the various API endpoints, terminating all in case of errors
- if err := n.startInProc(apis); err != nil {
- return err
- }
- if err := n.startIPC(apis); err != nil {
- n.stopInProc()
- return err
- }
- if err := n.startHTTP(n.httpEndpoint, apis, n.config.HTTPModules, n.config.HTTPCors, n.config.HTTPVirtualHosts, n.config.HTTPTimeouts); err != nil {
- n.stopIPC()
- n.stopInProc()
- return err
- }
- if err := n.startWS(n.wsEndpoint, apis, n.config.WSModules, n.config.WSOrigins, n.config.WSExposeAll); err != nil {
- n.stopHTTP()
- n.stopIPC()
- n.stopInProc()
- return err
- }
- // All API endpoints started successfully
- n.rpcAPIs = apis
- return nil
-}
+//func (n *Node) startRPC(services map[reflect.Type]Service) error {
+// // Gather all the possible APIs to surface
+// apis := n.apis()
+// for _, service := range services {
+// apis = append(apis, service.APIs()...)
+// }
+// // Start the various API endpoints, terminating all in case of errors
+// if err := n.startInProc(apis); err != nil {
+// return err
+// }
+// if err := n.startIPC(apis); err != nil {
+// n.stopInProc()
+// return err
+// }
+// if err := n.startHTTP(n.httpEndpoint, apis, n.config.HTTPModules, n.config.HTTPCors, n.config.HTTPVirtualHosts, n.config.HTTPTimeouts); err != nil {
+// n.stopIPC()
+// n.stopInProc()
+// return err
+// }
+// if err := n.startWS(n.wsEndpoint, apis, n.config.WSModules, n.config.WSOrigins, n.config.WSExposeAll); err != nil {
+// n.stopHTTP()
+// n.stopIPC()
+// n.stopInProc()
+// return err
+// }
+// // All API endpoints started successfully
+// n.rpcAPIs = apis
+// return nil
+//}
// startInProc initializes an in-process RPC endpoint.
func (n *Node) startInProc(apis []rpc.API) error {
@@ -329,20 +329,20 @@ func (n *Node) stopInProc() {
}
}
-// startIPC initializes and starts the IPC RPC endpoint.
-func (n *Node) startIPC(apis []rpc.API) error {
- if n.ipcEndpoint == "" {
- return nil // IPC disabled.
- }
- listener, handler, err := rpc.StartIPCEndpoint(n.ipcEndpoint, apis)
- if err != nil {
- return err
- }
- n.ipcListener = listener
- n.ipcHandler = handler
- n.log.Info("IPC endpoint opened", "url", n.ipcEndpoint)
- return nil
-}
+//// startIPC initializes and starts the IPC RPC endpoint.
+//func (n *Node) startIPC(apis []rpc.API) error {
+// if n.ipcEndpoint == "" {
+// return nil // IPC disabled.
+// }
+// listener, handler, err := rpc.StartIPCEndpoint(n.ipcEndpoint, apis)
+// if err != nil {
+// return err
+// }
+// n.ipcListener = listener
+// n.ipcHandler = handler
+// n.log.Info("IPC endpoint opened", "url", n.ipcEndpoint)
+// return nil
+//}
// stopIPC terminates the IPC RPC endpoint.
func (n *Node) stopIPC() {
@@ -359,23 +359,23 @@ func (n *Node) stopIPC() {
}
// startHTTP initializes and starts the HTTP RPC endpoint.
-func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors []string, vhosts []string, timeouts rpc.HTTPTimeouts) error {
- // Short circuit if the HTTP endpoint isn't being exposed
- if endpoint == "" {
- return nil
- }
- listener, handler, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, cors, vhosts, timeouts)
- if err != nil {
- return err
- }
- n.log.Info("HTTP endpoint opened", "url", fmt.Sprintf("http://%s", endpoint), "cors", strings.Join(cors, ","), "vhosts", strings.Join(vhosts, ","))
- // All listeners booted successfully
- n.httpEndpoint = endpoint
- n.httpListener = listener
- n.httpHandler = handler
-
- return nil
-}
+//func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors []string, vhosts []string, timeouts rpc.HTTPTimeouts) error {
+// // Short circuit if the HTTP endpoint isn't being exposed
+// if endpoint == "" {
+// return nil
+// }
+// listener, handler, err := rpc.StartHTTPEndpoint(endpoint, apis, modules, cors, vhosts, timeouts)
+// if err != nil {
+// return err
+// }
+// n.log.Info("HTTP endpoint opened", "url", fmt.Sprintf("http://%s", endpoint), "cors", strings.Join(cors, ","), "vhosts", strings.Join(vhosts, ","))
+// // All listeners booted successfully
+// n.httpEndpoint = endpoint
+// n.httpListener = listener
+// n.httpHandler = handler
+//
+// return nil
+//}
// stopHTTP terminates the HTTP RPC endpoint.
func (n *Node) stopHTTP() {
@@ -391,24 +391,24 @@ func (n *Node) stopHTTP() {
}
}
-// startWS initializes and starts the websocket RPC endpoint.
-func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrigins []string, exposeAll bool) error {
- // Short circuit if the WS endpoint isn't being exposed
- if endpoint == "" {
- return nil
- }
- listener, handler, err := rpc.StartWSEndpoint(endpoint, apis, modules, wsOrigins, exposeAll)
- if err != nil {
- return err
- }
- n.log.Info("WebSocket endpoint opened", "url", fmt.Sprintf("ws://%s", listener.Addr()))
- // All listeners booted successfully
- n.wsEndpoint = endpoint
- n.wsListener = listener
- n.wsHandler = handler
-
- return nil
-}
+//// startWS initializes and starts the websocket RPC endpoint.
+//func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrigins []string, exposeAll bool) error {
+// // Short circuit if the WS endpoint isn't being exposed
+// if endpoint == "" {
+// return nil
+// }
+// listener, handler, err := rpc.StartWSEndpoint(endpoint, apis, modules, wsOrigins, exposeAll)
+// if err != nil {
+// return err
+// }
+// n.log.Info("WebSocket endpoint opened", "url", fmt.Sprintf("ws://%s", listener.Addr()))
+// // All listeners booted successfully
+// n.wsEndpoint = endpoint
+// n.wsListener = listener
+// n.wsHandler = handler
+//
+// return nil
+//}
// stopWS terminates the websocket RPC endpoint.
func (n *Node) stopWS() {
@@ -504,16 +504,16 @@ func (n *Node) Restart() error {
return nil
}
-// Attach creates an RPC client attached to an in-process API handler.
-func (n *Node) Attach() (*rpc.Client, error) {
- n.lock.RLock()
- defer n.lock.RUnlock()
-
- if n.server == nil {
- return nil, ErrNodeStopped
- }
- return rpc.DialInProc(n.inprocHandler), nil
-}
+//// Attach creates an RPC client attached to an in-process API handler.
+//func (n *Node) Attach() (*rpc.Client, error) {
+// n.lock.RLock()
+// defer n.lock.RUnlock()
+//
+// if n.server == nil {
+// return nil, ErrNodeStopped
+// }
+// return rpc.DialInProc(n.inprocHandler), nil
+//}
// RPCHandler returns the in-process RPC request handler.
func (n *Node) RPCHandler() (*rpc.Server, error) {
diff --git a/node/service.go b/node/service.go
index fca9d90..7b3a4ff 100644
--- a/node/service.go
+++ b/node/service.go
@@ -20,12 +20,12 @@ import (
"path/filepath"
"reflect"
- "github.com/ava-labs/go-ethereum/accounts"
- "github.com/ava-labs/go-ethereum/core/rawdb"
+ "github.com/ava-labs/coreth/accounts"
+ "github.com/ava-labs/coreth/core/rawdb"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/ethdb"
"github.com/ava-labs/go-ethereum/event"
"github.com/ava-labs/go-ethereum/p2p"
- "github.com/ava-labs/go-ethereum/rpc"
)
// ServiceContext is a collection of service independent options inherited from
diff --git a/params/config.go b/params/config.go
new file mode 100644
index 0000000..23286cc
--- /dev/null
+++ b/params/config.go
@@ -0,0 +1,541 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package params
+
+import (
+ "encoding/binary"
+ "fmt"
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+ "github.com/ava-labs/go-ethereum/crypto"
+)
+
+// Genesis hashes to enforce below configs on.
+var (
+ MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
+ TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
+ RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177")
+ GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
+)
+
+// TrustedCheckpoints associates each known checkpoint with the genesis hash of
+// the chain it belongs to.
+var TrustedCheckpoints = map[common.Hash]*TrustedCheckpoint{
+ MainnetGenesisHash: MainnetTrustedCheckpoint,
+ TestnetGenesisHash: TestnetTrustedCheckpoint,
+ RinkebyGenesisHash: RinkebyTrustedCheckpoint,
+ GoerliGenesisHash: GoerliTrustedCheckpoint,
+}
+
+// CheckpointOracles associates each known checkpoint oracles with the genesis hash of
+// the chain it belongs to.
+var CheckpointOracles = map[common.Hash]*CheckpointOracleConfig{
+ MainnetGenesisHash: MainnetCheckpointOracle,
+ TestnetGenesisHash: TestnetCheckpointOracle,
+ RinkebyGenesisHash: RinkebyCheckpointOracle,
+ GoerliGenesisHash: GoerliCheckpointOracle,
+}
+
+var (
+ // MainnetChainConfig is the chain parameters to run a node on the main network.
+ MainnetChainConfig = &ChainConfig{
+ ChainID: big.NewInt(1),
+ HomesteadBlock: big.NewInt(1150000),
+ DAOForkBlock: big.NewInt(1920000),
+ DAOForkSupport: true,
+ EIP150Block: big.NewInt(2463000),
+ EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
+ EIP155Block: big.NewInt(2675000),
+ EIP158Block: big.NewInt(2675000),
+ ByzantiumBlock: big.NewInt(4370000),
+ ConstantinopleBlock: big.NewInt(7280000),
+ PetersburgBlock: big.NewInt(7280000),
+ IstanbulBlock: nil,
+ Ethash: new(EthashConfig),
+ }
+
+ // MainnetTrustedCheckpoint contains the light client trusted checkpoint for the main network.
+ MainnetTrustedCheckpoint = &TrustedCheckpoint{
+ SectionIndex: 253,
+ SectionHead: common.HexToHash("0xf35fabd036e2030196183bb70ae194f6ce1ea7b58559e3825c168f1df9c0a258"),
+ CHTRoot: common.HexToHash("0x8992849e2be3390696eaf66312626e484045501cd3ec207922c27a6a80a7bb07"),
+ BloomRoot: common.HexToHash("0xcc510b51ca4d73fb3fdf43208d73286f8f23817cdc31b8ea9f4de8d645f07df4"),
+ }
+
+ // MainnetCheckpointOracle contains a set of configs for the main network oracle.
+ MainnetCheckpointOracle = &CheckpointOracleConfig{
+ Address: common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"),
+ Signers: []common.Address{
+ common.HexToAddress("0x1b2C260efc720BE89101890E4Db589b44E950527"), // Peter
+ common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
+ common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
+ common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
+ common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
+ },
+ Threshold: 2,
+ }
+
+ // TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
+ TestnetChainConfig = &ChainConfig{
+ ChainID: big.NewInt(3),
+ HomesteadBlock: big.NewInt(0),
+ DAOForkBlock: nil,
+ DAOForkSupport: true,
+ EIP150Block: big.NewInt(0),
+ EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
+ EIP155Block: big.NewInt(10),
+ EIP158Block: big.NewInt(10),
+ ByzantiumBlock: big.NewInt(1700000),
+ ConstantinopleBlock: big.NewInt(4230000),
+ PetersburgBlock: big.NewInt(4939394),
+ IstanbulBlock: nil,
+ Ethash: new(EthashConfig),
+ }
+
+ // TestnetTrustedCheckpoint contains the light client trusted checkpoint for the Ropsten test network.
+ TestnetTrustedCheckpoint = &TrustedCheckpoint{
+ SectionIndex: 187,
+ SectionHead: common.HexToHash("0x7d6db64d8ec43303e4392fb726d2346f7231b246decca3d8140dd7e2c0d0b07d"),
+ CHTRoot: common.HexToHash("0xa5095e1a004a8642fb93ca682eb91e8f20ef5bce151e47404fbb68772d17705b"),
+ BloomRoot: common.HexToHash("0x90b28050f948ec6fb35b23a91d9aed38ce0c92d3cdd6e1d383c1bddf8b4071cf"),
+ }
+
+ // TestnetCheckpointOracle contains a set of configs for the Ropsten test network oracle.
+ TestnetCheckpointOracle = &CheckpointOracleConfig{
+ Address: common.HexToAddress("0xEF79475013f154E6A65b54cB2742867791bf0B84"),
+ Signers: []common.Address{
+ common.HexToAddress("0x32162F3581E88a5f62e8A61892B42C46E2c18f7b"), // Peter
+ common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
+ common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
+ common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
+ common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
+ },
+ Threshold: 2,
+ }
+
+ // RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
+ RinkebyChainConfig = &ChainConfig{
+ ChainID: big.NewInt(4),
+ HomesteadBlock: big.NewInt(1),
+ DAOForkBlock: nil,
+ DAOForkSupport: true,
+ EIP150Block: big.NewInt(2),
+ EIP150Hash: common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
+ EIP155Block: big.NewInt(3),
+ EIP158Block: big.NewInt(3),
+ ByzantiumBlock: big.NewInt(1035301),
+ ConstantinopleBlock: big.NewInt(3660663),
+ PetersburgBlock: big.NewInt(4321234),
+ IstanbulBlock: nil,
+ Clique: &CliqueConfig{
+ Period: 15,
+ Epoch: 30000,
+ },
+ }
+
+ // RinkebyTrustedCheckpoint contains the light client trusted checkpoint for the Rinkeby test network.
+ RinkebyTrustedCheckpoint = &TrustedCheckpoint{
+ SectionIndex: 148,
+ SectionHead: common.HexToHash("0x45918f4686732c2a3e80827e1bc39cdb6a27fa362ddfe1fdfb61c69a7f1df1a9"),
+ CHTRoot: common.HexToHash("0x8ac7046391fec14834a2a0183513937c0b5f696666545991477d24b067008961"),
+ BloomRoot: common.HexToHash("0xfe4b852517612d7da54bf7e9fc18861a83171a93c72583bb6a61893b74422168"),
+ }
+
+ // RinkebyCheckpointOracle contains a set of configs for the Rinkeby test network oracle.
+ RinkebyCheckpointOracle = &CheckpointOracleConfig{
+ Address: common.HexToAddress("0xebe8eFA441B9302A0d7eaECc277c09d20D684540"),
+ Signers: []common.Address{
+ common.HexToAddress("0xd9c9cd5f6779558b6e0ed4e6acf6b1947e7fa1f3"), // Peter
+ common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
+ common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
+ common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
+ },
+ Threshold: 2,
+ }
+
+ // GoerliChainConfig contains the chain parameters to run a node on the Görli test network.
+ GoerliChainConfig = &ChainConfig{
+ ChainID: big.NewInt(5),
+ HomesteadBlock: big.NewInt(0),
+ DAOForkBlock: nil,
+ DAOForkSupport: true,
+ EIP150Block: big.NewInt(0),
+ EIP155Block: big.NewInt(0),
+ EIP158Block: big.NewInt(0),
+ ByzantiumBlock: big.NewInt(0),
+ ConstantinopleBlock: big.NewInt(0),
+ PetersburgBlock: big.NewInt(0),
+ IstanbulBlock: nil,
+ Clique: &CliqueConfig{
+ Period: 15,
+ Epoch: 30000,
+ },
+ }
+
+ // GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network.
+ GoerliTrustedCheckpoint = &TrustedCheckpoint{
+ SectionIndex: 32,
+ SectionHead: common.HexToHash("0x50eaedd8361fa9edd0ac2dec410310b9bdf67b963b60f3b1dce47f84b30670f9"),
+ CHTRoot: common.HexToHash("0x6504db73139f75ffa9102ae980e41b361cf3d5b66cea06c79cde9f457368820c"),
+ BloomRoot: common.HexToHash("0x7551ae027bb776252a20ded51ee2ff0cbfbd1d8d57261b9161cc1f2f80237001"),
+ }
+
+ // GoerliCheckpointOracle contains a set of configs for the Goerli test network oracle.
+ GoerliCheckpointOracle = &CheckpointOracleConfig{
+ Address: common.HexToAddress("0x18CA0E045F0D772a851BC7e48357Bcaab0a0795D"),
+ Signers: []common.Address{
+ common.HexToAddress("0x4769bcaD07e3b938B7f43EB7D278Bc7Cb9efFb38"), // Peter
+ common.HexToAddress("0x78d1aD571A1A09D60D9BBf25894b44e4C8859595"), // Martin
+ common.HexToAddress("0x286834935f4A8Cfb4FF4C77D5770C2775aE2b0E7"), // Zsolt
+ common.HexToAddress("0xb86e2B0Ab5A4B1373e40c51A7C712c70Ba2f9f8E"), // Gary
+ common.HexToAddress("0x0DF8fa387C602AE62559cC4aFa4972A7045d6707"), // Guillaume
+ },
+ Threshold: 2,
+ }
+
+ // AllEthashProtocolChanges contains every protocol change (EIPs) introduced
+ // and accepted by the Ethereum core developers into the Ethash consensus.
+ //
+ // 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), nil, nil, new(EthashConfig), 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), nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
+
+ 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), nil, nil, new(EthashConfig), nil}
+ TestRules = TestChainConfig.Rules(new(big.Int))
+)
+
+// TrustedCheckpoint represents a set of post-processed trie roots (CHT and
+// BloomTrie) associated with the appropriate section index and head hash. It is
+// used to start light syncing from this checkpoint and avoid downloading the
+// entire header chain while still being able to securely access old headers/logs.
+type TrustedCheckpoint struct {
+ SectionIndex uint64 `json:"sectionIndex"`
+ SectionHead common.Hash `json:"sectionHead"`
+ CHTRoot common.Hash `json:"chtRoot"`
+ BloomRoot common.Hash `json:"bloomRoot"`
+}
+
+// HashEqual returns an indicator comparing the itself hash with given one.
+func (c *TrustedCheckpoint) HashEqual(hash common.Hash) bool {
+ if c.Empty() {
+ return hash == common.Hash{}
+ }
+ return c.Hash() == hash
+}
+
+// Hash returns the hash of checkpoint's four key fields(index, sectionHead, chtRoot and bloomTrieRoot).
+func (c *TrustedCheckpoint) Hash() common.Hash {
+ buf := make([]byte, 8+3*common.HashLength)
+ binary.BigEndian.PutUint64(buf, c.SectionIndex)
+ copy(buf[8:], c.SectionHead.Bytes())
+ copy(buf[8+common.HashLength:], c.CHTRoot.Bytes())
+ copy(buf[8+2*common.HashLength:], c.BloomRoot.Bytes())
+ return crypto.Keccak256Hash(buf)
+}
+
+// Empty returns an indicator whether the checkpoint is regarded as empty.
+func (c *TrustedCheckpoint) Empty() bool {
+ return c.SectionHead == (common.Hash{}) || c.CHTRoot == (common.Hash{}) || c.BloomRoot == (common.Hash{})
+}
+
+// CheckpointOracleConfig represents a set of checkpoint contract(which acts as an oracle)
+// config which used for light client checkpoint syncing.
+type CheckpointOracleConfig struct {
+ Address common.Address `json:"address"`
+ Signers []common.Address `json:"signers"`
+ Threshold uint64 `json:"threshold"`
+}
+
+// ChainConfig is the core config which determines the blockchain settings.
+//
+// ChainConfig is stored in the database on a per block basis. This means
+// that any network, identified by its genesis block, can have its own
+// set of configuration options.
+type ChainConfig struct {
+ ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
+
+ HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
+
+ DAOForkBlock *big.Int `json:"daoForkBlock,omitempty"` // TheDAO hard-fork switch block (nil = no fork)
+ DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
+
+ // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
+ EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
+ EIP150Hash common.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed)
+
+ EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
+ EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
+
+ ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
+ ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
+ PetersburgBlock *big.Int `json:"petersburgBlock,omitempty"` // Petersburg switch block (nil = same as Constantinople)
+ IstanbulBlock *big.Int `json:"istanbulBlock,omitempty"` // Istanbul switch block (nil = no fork, 0 = already on istanbul)
+ EWASMBlock *big.Int `json:"ewasmBlock,omitempty"` // EWASM switch block (nil = no fork, 0 = already activated)
+
+ // Various consensus engines
+ Ethash *EthashConfig `json:"ethash,omitempty"`
+ Clique *CliqueConfig `json:"clique,omitempty"`
+}
+
+// EthashConfig is the consensus engine configs for proof-of-work based sealing.
+type EthashConfig struct{}
+
+// String implements the stringer interface, returning the consensus engine details.
+func (c *EthashConfig) String() string {
+ return "ethash"
+}
+
+// CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
+type CliqueConfig struct {
+ Period uint64 `json:"period"` // Number of seconds between blocks to enforce
+ Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
+}
+
+// String implements the stringer interface, returning the consensus engine details.
+func (c *CliqueConfig) String() string {
+ return "clique"
+}
+
+// String implements the fmt.Stringer interface.
+func (c *ChainConfig) String() string {
+ var engine interface{}
+ switch {
+ case c.Ethash != nil:
+ engine = c.Ethash
+ case c.Clique != nil:
+ engine = c.Clique
+ default:
+ engine = "unknown"
+ }
+ return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Petersburg: %v Istanbul: %v Engine: %v}",
+ c.ChainID,
+ c.HomesteadBlock,
+ c.DAOForkBlock,
+ c.DAOForkSupport,
+ c.EIP150Block,
+ c.EIP155Block,
+ c.EIP158Block,
+ c.ByzantiumBlock,
+ c.ConstantinopleBlock,
+ c.PetersburgBlock,
+ c.IstanbulBlock,
+ engine,
+ )
+}
+
+// IsHomestead returns whether num is either equal to the homestead block or greater.
+func (c *ChainConfig) IsHomestead(num *big.Int) bool {
+ return isForked(c.HomesteadBlock, num)
+}
+
+// IsDAOFork returns whether num is either equal to the DAO fork block or greater.
+func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
+ return isForked(c.DAOForkBlock, num)
+}
+
+// IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
+func (c *ChainConfig) IsEIP150(num *big.Int) bool {
+ return isForked(c.EIP150Block, num)
+}
+
+// IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
+func (c *ChainConfig) IsEIP155(num *big.Int) bool {
+ return isForked(c.EIP155Block, num)
+}
+
+// IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
+func (c *ChainConfig) IsEIP158(num *big.Int) bool {
+ return isForked(c.EIP158Block, num)
+}
+
+// IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
+func (c *ChainConfig) IsByzantium(num *big.Int) bool {
+ return isForked(c.ByzantiumBlock, num)
+}
+
+// IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
+func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
+ return isForked(c.ConstantinopleBlock, num)
+}
+
+// IsPetersburg returns whether num is either
+// - equal to or greater than the PetersburgBlock fork block,
+// - OR is nil, and Constantinople is active
+func (c *ChainConfig) IsPetersburg(num *big.Int) bool {
+ return isForked(c.PetersburgBlock, num) || c.PetersburgBlock == nil && isForked(c.ConstantinopleBlock, num)
+}
+
+// IsIstanbul returns whether num is either equal to the Istanbul fork block or greater.
+func (c *ChainConfig) IsIstanbul(num *big.Int) bool {
+ return isForked(c.IstanbulBlock, num)
+}
+
+// IsEWASM returns whether num represents a block number after the EWASM fork
+func (c *ChainConfig) IsEWASM(num *big.Int) bool {
+ return isForked(c.EWASMBlock, num)
+}
+
+// CheckCompatible checks whether scheduled fork transitions have been imported
+// with a mismatching chain configuration.
+func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
+ bhead := new(big.Int).SetUint64(height)
+
+ // Iterate checkCompatible to find the lowest conflict.
+ var lasterr *ConfigCompatError
+ for {
+ err := c.checkCompatible(newcfg, bhead)
+ if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
+ break
+ }
+ lasterr = err
+ bhead.SetUint64(err.RewindTo)
+ }
+ return lasterr
+}
+
+func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
+ if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
+ return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
+ }
+ if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
+ return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
+ }
+ if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
+ return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
+ }
+ if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
+ return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
+ }
+ if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
+ return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
+ }
+ if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
+ return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
+ }
+ if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
+ return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
+ }
+ if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
+ return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
+ }
+ if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
+ return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
+ }
+ if isForkIncompatible(c.PetersburgBlock, newcfg.PetersburgBlock, head) {
+ return newCompatError("Petersburg fork block", c.PetersburgBlock, newcfg.PetersburgBlock)
+ }
+ if isForkIncompatible(c.IstanbulBlock, newcfg.IstanbulBlock, head) {
+ return newCompatError("Istanbul fork block", c.IstanbulBlock, newcfg.IstanbulBlock)
+ }
+ if isForkIncompatible(c.EWASMBlock, newcfg.EWASMBlock, head) {
+ return newCompatError("ewasm fork block", c.EWASMBlock, newcfg.EWASMBlock)
+ }
+ return nil
+}
+
+// isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
+// block s2 because head is already past the fork.
+func isForkIncompatible(s1, s2, head *big.Int) bool {
+ return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
+}
+
+// isForked returns whether a fork scheduled at block s is active at the given head block.
+func isForked(s, head *big.Int) bool {
+ if s == nil || head == nil {
+ return false
+ }
+ return s.Cmp(head) <= 0
+}
+
+func configNumEqual(x, y *big.Int) bool {
+ if x == nil {
+ return y == nil
+ }
+ if y == nil {
+ return x == nil
+ }
+ return x.Cmp(y) == 0
+}
+
+// ConfigCompatError is raised if the locally-stored blockchain is initialised with a
+// ChainConfig that would alter the past.
+type ConfigCompatError struct {
+ What string
+ // block numbers of the stored and new configurations
+ StoredConfig, NewConfig *big.Int
+ // the block number to which the local chain must be rewound to correct the error
+ RewindTo uint64
+}
+
+func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
+ var rew *big.Int
+ switch {
+ case storedblock == nil:
+ rew = newblock
+ case newblock == nil || storedblock.Cmp(newblock) < 0:
+ rew = storedblock
+ default:
+ rew = newblock
+ }
+ err := &ConfigCompatError{what, storedblock, newblock, 0}
+ if rew != nil && rew.Sign() > 0 {
+ err.RewindTo = rew.Uint64() - 1
+ }
+ return err
+}
+
+func (err *ConfigCompatError) Error() string {
+ return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
+}
+
+// Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
+// that do not have or require information about the block.
+//
+// Rules is a one time interface meaning that it shouldn't be used in between transition
+// phases.
+type Rules struct {
+ ChainID *big.Int
+ IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
+ IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
+}
+
+// Rules ensures c's ChainID is not nil.
+func (c *ChainConfig) Rules(num *big.Int) Rules {
+ chainID := c.ChainID
+ if chainID == nil {
+ chainID = new(big.Int)
+ }
+ return Rules{
+ ChainID: new(big.Int).Set(chainID),
+ IsHomestead: c.IsHomestead(num),
+ IsEIP150: c.IsEIP150(num),
+ IsEIP155: c.IsEIP155(num),
+ IsEIP158: c.IsEIP158(num),
+ IsByzantium: c.IsByzantium(num),
+ IsConstantinople: c.IsConstantinople(num),
+ IsPetersburg: c.IsPetersburg(num),
+ IsIstanbul: c.IsIstanbul(num),
+ }
+}
diff --git a/params/dao.go b/params/dao.go
new file mode 100644
index 0000000..2d75845
--- /dev/null
+++ b/params/dao.go
@@ -0,0 +1,158 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package params
+
+import (
+ "math/big"
+
+ "github.com/ava-labs/go-ethereum/common"
+)
+
+// DAOForkBlockExtra is the block header extra-data field to set for the DAO fork
+// point and a number of consecutive blocks to allow fast/light syncers to correctly
+// pick the side they want ("dao-hard-fork").
+var DAOForkBlockExtra = common.FromHex("0x64616f2d686172642d666f726b")
+
+// DAOForkExtraRange is the number of consecutive blocks from the DAO fork point
+// to override the extra-data in to prevent no-fork attacks.
+var DAOForkExtraRange = big.NewInt(10)
+
+// DAORefundContract is the address of the refund contract to send DAO balances to.
+var DAORefundContract = common.HexToAddress("0xbf4ed7b27f1d666546e30d74d50d173d20bca754")
+
+// DAODrainList is the list of accounts whose full balances will be moved into a
+// refund contract at the beginning of the dao-fork block.
+func DAODrainList() []common.Address {
+ return []common.Address{
+ common.HexToAddress("0xd4fe7bc31cedb7bfb8a345f31e668033056b2728"),
+ common.HexToAddress("0xb3fb0e5aba0e20e5c49d252dfd30e102b171a425"),
+ common.HexToAddress("0x2c19c7f9ae8b751e37aeb2d93a699722395ae18f"),
+ common.HexToAddress("0xecd135fa4f61a655311e86238c92adcd779555d2"),
+ common.HexToAddress("0x1975bd06d486162d5dc297798dfc41edd5d160a7"),
+ common.HexToAddress("0xa3acf3a1e16b1d7c315e23510fdd7847b48234f6"),
+ common.HexToAddress("0x319f70bab6845585f412ec7724b744fec6095c85"),
+ common.HexToAddress("0x06706dd3f2c9abf0a21ddcc6941d9b86f0596936"),
+ common.HexToAddress("0x5c8536898fbb74fc7445814902fd08422eac56d0"),
+ common.HexToAddress("0x6966ab0d485353095148a2155858910e0965b6f9"),
+ common.HexToAddress("0x779543a0491a837ca36ce8c635d6154e3c4911a6"),
+ common.HexToAddress("0x2a5ed960395e2a49b1c758cef4aa15213cfd874c"),
+ common.HexToAddress("0x5c6e67ccd5849c0d29219c4f95f1a7a93b3f5dc5"),
+ common.HexToAddress("0x9c50426be05db97f5d64fc54bf89eff947f0a321"),
+ common.HexToAddress("0x200450f06520bdd6c527622a273333384d870efb"),
+ common.HexToAddress("0xbe8539bfe837b67d1282b2b1d61c3f723966f049"),
+ common.HexToAddress("0x6b0c4d41ba9ab8d8cfb5d379c69a612f2ced8ecb"),
+ common.HexToAddress("0xf1385fb24aad0cd7432824085e42aff90886fef5"),
+ common.HexToAddress("0xd1ac8b1ef1b69ff51d1d401a476e7e612414f091"),
+ common.HexToAddress("0x8163e7fb499e90f8544ea62bbf80d21cd26d9efd"),
+ common.HexToAddress("0x51e0ddd9998364a2eb38588679f0d2c42653e4a6"),
+ common.HexToAddress("0x627a0a960c079c21c34f7612d5d230e01b4ad4c7"),
+ common.HexToAddress("0xf0b1aa0eb660754448a7937c022e30aa692fe0c5"),
+ common.HexToAddress("0x24c4d950dfd4dd1902bbed3508144a54542bba94"),
+ common.HexToAddress("0x9f27daea7aca0aa0446220b98d028715e3bc803d"),
+ common.HexToAddress("0xa5dc5acd6a7968a4554d89d65e59b7fd3bff0f90"),
+ common.HexToAddress("0xd9aef3a1e38a39c16b31d1ace71bca8ef58d315b"),
+ common.HexToAddress("0x63ed5a272de2f6d968408b4acb9024f4cc208ebf"),
+ common.HexToAddress("0x6f6704e5a10332af6672e50b3d9754dc460dfa4d"),
+ common.HexToAddress("0x77ca7b50b6cd7e2f3fa008e24ab793fd56cb15f6"),
+ common.HexToAddress("0x492ea3bb0f3315521c31f273e565b868fc090f17"),
+ common.HexToAddress("0x0ff30d6de14a8224aa97b78aea5388d1c51c1f00"),
+ common.HexToAddress("0x9ea779f907f0b315b364b0cfc39a0fde5b02a416"),
+ common.HexToAddress("0xceaeb481747ca6c540a000c1f3641f8cef161fa7"),
+ common.HexToAddress("0xcc34673c6c40e791051898567a1222daf90be287"),
+ common.HexToAddress("0x579a80d909f346fbfb1189493f521d7f48d52238"),
+ common.HexToAddress("0xe308bd1ac5fda103967359b2712dd89deffb7973"),
+ common.HexToAddress("0x4cb31628079fb14e4bc3cd5e30c2f7489b00960c"),
+ common.HexToAddress("0xac1ecab32727358dba8962a0f3b261731aad9723"),
+ common.HexToAddress("0x4fd6ace747f06ece9c49699c7cabc62d02211f75"),
+ common.HexToAddress("0x440c59b325d2997a134c2c7c60a8c61611212bad"),
+ common.HexToAddress("0x4486a3d68fac6967006d7a517b889fd3f98c102b"),
+ common.HexToAddress("0x9c15b54878ba618f494b38f0ae7443db6af648ba"),
+ common.HexToAddress("0x27b137a85656544b1ccb5a0f2e561a5703c6a68f"),
+ common.HexToAddress("0x21c7fdb9ed8d291d79ffd82eb2c4356ec0d81241"),
+ common.HexToAddress("0x23b75c2f6791eef49c69684db4c6c1f93bf49a50"),
+ common.HexToAddress("0x1ca6abd14d30affe533b24d7a21bff4c2d5e1f3b"),
+ common.HexToAddress("0xb9637156d330c0d605a791f1c31ba5890582fe1c"),
+ common.HexToAddress("0x6131c42fa982e56929107413a9d526fd99405560"),
+ common.HexToAddress("0x1591fc0f688c81fbeb17f5426a162a7024d430c2"),
+ common.HexToAddress("0x542a9515200d14b68e934e9830d91645a980dd7a"),
+ common.HexToAddress("0xc4bbd073882dd2add2424cf47d35213405b01324"),
+ common.HexToAddress("0x782495b7b3355efb2833d56ecb34dc22ad7dfcc4"),
+ common.HexToAddress("0x58b95c9a9d5d26825e70a82b6adb139d3fd829eb"),
+ common.HexToAddress("0x3ba4d81db016dc2890c81f3acec2454bff5aada5"),
+ common.HexToAddress("0xb52042c8ca3f8aa246fa79c3feaa3d959347c0ab"),
+ common.HexToAddress("0xe4ae1efdfc53b73893af49113d8694a057b9c0d1"),
+ common.HexToAddress("0x3c02a7bc0391e86d91b7d144e61c2c01a25a79c5"),
+ common.HexToAddress("0x0737a6b837f97f46ebade41b9bc3e1c509c85c53"),
+ common.HexToAddress("0x97f43a37f595ab5dd318fb46e7a155eae057317a"),
+ common.HexToAddress("0x52c5317c848ba20c7504cb2c8052abd1fde29d03"),
+ common.HexToAddress("0x4863226780fe7c0356454236d3b1c8792785748d"),
+ common.HexToAddress("0x5d2b2e6fcbe3b11d26b525e085ff818dae332479"),
+ common.HexToAddress("0x5f9f3392e9f62f63b8eac0beb55541fc8627f42c"),
+ common.HexToAddress("0x057b56736d32b86616a10f619859c6cd6f59092a"),
+ common.HexToAddress("0x9aa008f65de0b923a2a4f02012ad034a5e2e2192"),
+ common.HexToAddress("0x304a554a310c7e546dfe434669c62820b7d83490"),
+ common.HexToAddress("0x914d1b8b43e92723e64fd0a06f5bdb8dd9b10c79"),
+ common.HexToAddress("0x4deb0033bb26bc534b197e61d19e0733e5679784"),
+ common.HexToAddress("0x07f5c1e1bc2c93e0402f23341973a0e043f7bf8a"),
+ common.HexToAddress("0x35a051a0010aba705c9008d7a7eff6fb88f6ea7b"),
+ common.HexToAddress("0x4fa802324e929786dbda3b8820dc7834e9134a2a"),
+ common.HexToAddress("0x9da397b9e80755301a3b32173283a91c0ef6c87e"),
+ common.HexToAddress("0x8d9edb3054ce5c5774a420ac37ebae0ac02343c6"),
+ common.HexToAddress("0x0101f3be8ebb4bbd39a2e3b9a3639d4259832fd9"),
+ common.HexToAddress("0x5dc28b15dffed94048d73806ce4b7a4612a1d48f"),
+ common.HexToAddress("0xbcf899e6c7d9d5a215ab1e3444c86806fa854c76"),
+ common.HexToAddress("0x12e626b0eebfe86a56d633b9864e389b45dcb260"),
+ common.HexToAddress("0xa2f1ccba9395d7fcb155bba8bc92db9bafaeade7"),
+ common.HexToAddress("0xec8e57756626fdc07c63ad2eafbd28d08e7b0ca5"),
+ common.HexToAddress("0xd164b088bd9108b60d0ca3751da4bceb207b0782"),
+ common.HexToAddress("0x6231b6d0d5e77fe001c2a460bd9584fee60d409b"),
+ common.HexToAddress("0x1cba23d343a983e9b5cfd19496b9a9701ada385f"),
+ common.HexToAddress("0xa82f360a8d3455c5c41366975bde739c37bfeb8a"),
+ common.HexToAddress("0x9fcd2deaff372a39cc679d5c5e4de7bafb0b1339"),
+ common.HexToAddress("0x005f5cee7a43331d5a3d3eec71305925a62f34b6"),
+ common.HexToAddress("0x0e0da70933f4c7849fc0d203f5d1d43b9ae4532d"),
+ common.HexToAddress("0xd131637d5275fd1a68a3200f4ad25c71a2a9522e"),
+ common.HexToAddress("0xbc07118b9ac290e4622f5e77a0853539789effbe"),
+ common.HexToAddress("0x47e7aa56d6bdf3f36be34619660de61275420af8"),
+ common.HexToAddress("0xacd87e28b0c9d1254e868b81cba4cc20d9a32225"),
+ common.HexToAddress("0xadf80daec7ba8dcf15392f1ac611fff65d94f880"),
+ common.HexToAddress("0x5524c55fb03cf21f549444ccbecb664d0acad706"),
+ common.HexToAddress("0x40b803a9abce16f50f36a77ba41180eb90023925"),
+ common.HexToAddress("0xfe24cdd8648121a43a7c86d289be4dd2951ed49f"),
+ common.HexToAddress("0x17802f43a0137c506ba92291391a8a8f207f487d"),
+ common.HexToAddress("0x253488078a4edf4d6f42f113d1e62836a942cf1a"),
+ common.HexToAddress("0x86af3e9626fce1957c82e88cbf04ddf3a2ed7915"),
+ common.HexToAddress("0xb136707642a4ea12fb4bae820f03d2562ebff487"),
+ common.HexToAddress("0xdbe9b615a3ae8709af8b93336ce9b477e4ac0940"),
+ common.HexToAddress("0xf14c14075d6c4ed84b86798af0956deef67365b5"),
+ common.HexToAddress("0xca544e5c4687d109611d0f8f928b53a25af72448"),
+ common.HexToAddress("0xaeeb8ff27288bdabc0fa5ebb731b6f409507516c"),
+ common.HexToAddress("0xcbb9d3703e651b0d496cdefb8b92c25aeb2171f7"),
+ common.HexToAddress("0x6d87578288b6cb5549d5076a207456a1f6a63dc0"),
+ common.HexToAddress("0xb2c6f0dfbb716ac562e2d85d6cb2f8d5ee87603e"),
+ common.HexToAddress("0xaccc230e8a6e5be9160b8cdf2864dd2a001c28b6"),
+ common.HexToAddress("0x2b3455ec7fedf16e646268bf88846bd7a2319bb2"),
+ common.HexToAddress("0x4613f3bca5c44ea06337a9e439fbc6d42e501d0a"),
+ common.HexToAddress("0xd343b217de44030afaa275f54d31a9317c7f441e"),
+ common.HexToAddress("0x84ef4b2357079cd7a7c69fd7a37cd0609a679106"),
+ common.HexToAddress("0xda2fef9e4a3230988ff17df2165440f37e8b1708"),
+ common.HexToAddress("0xf4c64518ea10f995918a454158c6b61407ea345c"),
+ common.HexToAddress("0x7602b46df5390e432ef1c307d4f2c9ff6d65cc97"),
+ common.HexToAddress("0xbb9bc244d798123fde783fcc1c72d3bb8c189413"),
+ common.HexToAddress("0x807640a13483f8ac783c557fcdf27be11ea4ac7a"),
+ }
+}
diff --git a/params/network_params.go b/params/network_params.go
new file mode 100644
index 0000000..bba2472
--- /dev/null
+++ b/params/network_params.go
@@ -0,0 +1,61 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package params
+
+// These are network parameters that need to be constant between clients, but
+// aren't necessarily consensus related.
+
+const (
+ // BloomBitsBlocks is the number of blocks a single bloom bit section vector
+ // contains on the server side.
+ BloomBitsBlocks uint64 = 4096
+
+ // BloomBitsBlocksClient is the number of blocks a single bloom bit section vector
+ // contains on the light client side
+ BloomBitsBlocksClient uint64 = 32768
+
+ // BloomConfirms is the number of confirmation blocks before a bloom section is
+ // considered probably final and its rotated bits are calculated.
+ BloomConfirms = 256
+
+ // CHTFrequency is the block frequency for creating CHTs
+ CHTFrequency = 32768
+
+ // BloomTrieFrequency is the block frequency for creating BloomTrie on both
+ // server/client sides.
+ BloomTrieFrequency = 32768
+
+ // HelperTrieConfirmations is the number of confirmations before a client is expected
+ // to have the given HelperTrie available.
+ HelperTrieConfirmations = 2048
+
+ // HelperTrieProcessConfirmations is the number of confirmations before a HelperTrie
+ // is generated
+ HelperTrieProcessConfirmations = 256
+
+ // CheckpointFrequency is the block frequency for creating checkpoint
+ CheckpointFrequency = 32768
+
+ // CheckpointProcessConfirmations is the number before a checkpoint is generated
+ CheckpointProcessConfirmations = 256
+
+ // ImmutabilityThreshold is the number of blocks after which a chain segment is
+ // considered immutable (i.e. soft finality). It is used by the downloader as a
+ // hard limit against deep ancestors, by the blockchain against deep reorgs, by
+ // the freezer as the cutoff treshold and by clique as the snapshot trust limit.
+ ImmutabilityThreshold = 90000
+)
diff --git a/params/protocol_params.go b/params/protocol_params.go
index c738450..b36dd91 100644
--- a/params/protocol_params.go
+++ b/params/protocol_params.go
@@ -16,8 +16,125 @@
package params
-//import "math/big"
+import "math/big"
const (
- MaximumExtraDataSize uint64 = 64 // Maximum size extra data may be after Genesis.
+ GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations.
+ MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be.
+ GenesisGasLimit uint64 = 4712388 // Gas limit of the Genesis block.
+
+ MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
+ ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.
+ SloadGas uint64 = 50 // Multiplied by the number of 32-byte words that are copied (round up) for any *COPY operation and added.
+ CallValueTransferGas uint64 = 9000 // Paid for CALL when the value transfer is non-zero.
+ CallNewAccountGas uint64 = 25000 // Paid for CALL when the destination address didn't exist prior.
+ TxGas uint64 = 21000 // Per transaction not creating a contract. NOTE: Not payable on data of calls between transactions.
+ TxGasContractCreation uint64 = 53000 // Per transaction that creates a contract. NOTE: Not payable on data of calls between transactions.
+ TxDataZeroGas uint64 = 4 // Per byte of data attached to a transaction that equals zero. NOTE: Not payable on data of calls between transactions.
+ QuadCoeffDiv uint64 = 512 // Divisor for the quadratic particle of the memory cost equation.
+ LogDataGas uint64 = 8 // Per byte in a LOG* operation's data.
+ CallStipend uint64 = 2300 // Free gas given at beginning of call.
+
+ Sha3Gas uint64 = 30 // Once per SHA3 operation.
+ Sha3WordGas uint64 = 6 // Once per word of the SHA3 operation's data.
+
+ SstoreSetGas uint64 = 20000 // Once per SLOAD operation.
+ SstoreResetGas uint64 = 5000 // Once per SSTORE operation if the zeroness changes from zero.
+ SstoreClearGas uint64 = 5000 // Once per SSTORE operation if the zeroness doesn't change.
+ SstoreRefundGas uint64 = 15000 // Once per SSTORE operation if the zeroness changes to zero.
+
+ NetSstoreNoopGas uint64 = 200 // Once per SSTORE operation if the value doesn't change.
+ NetSstoreInitGas uint64 = 20000 // Once per SSTORE operation from clean zero.
+ NetSstoreCleanGas uint64 = 5000 // Once per SSTORE operation from clean non-zero.
+ NetSstoreDirtyGas uint64 = 200 // Once per SSTORE operation from dirty.
+
+ NetSstoreClearRefund uint64 = 15000 // Once per SSTORE operation for clearing an originally existing storage slot
+ NetSstoreResetRefund uint64 = 4800 // Once per SSTORE operation for resetting to the original non-zero value
+ NetSstoreResetClearRefund uint64 = 19800 // Once per SSTORE operation for resetting to the original zero value
+
+ SstoreSentryGasEIP2200 uint64 = 2300 // Minimum gas required to be present for an SSTORE call, not consumed
+ SstoreNoopGasEIP2200 uint64 = 800 // Once per SSTORE operation if the value doesn't change.
+ SstoreDirtyGasEIP2200 uint64 = 800 // Once per SSTORE operation if a dirty value is changed.
+ SstoreInitGasEIP2200 uint64 = 20000 // Once per SSTORE operation from clean zero to non-zero
+ SstoreInitRefundEIP2200 uint64 = 19200 // Once per SSTORE operation for resetting to the original zero value
+ SstoreCleanGasEIP2200 uint64 = 5000 // Once per SSTORE operation from clean non-zero to something else
+ SstoreCleanRefundEIP2200 uint64 = 4200 // Once per SSTORE operation for resetting to the original non-zero value
+ SstoreClearRefundEIP2200 uint64 = 15000 // Once per SSTORE operation for clearing an originally existing storage slot
+
+ JumpdestGas uint64 = 1 // Once per JUMPDEST operation.
+ EMCGas uint64 = 1 // Once per EMC operation.
+ EpochDuration uint64 = 30000 // Duration between proof-of-work epochs.
+
+ CreateDataGas uint64 = 200 //
+ CallCreateDepth uint64 = 1024 // Maximum depth of call/create stack.
+ ExpGas uint64 = 10 // Once per EXP instruction
+ LogGas uint64 = 375 // Per LOG* operation.
+ CopyGas uint64 = 3 //
+ StackLimit uint64 = 1024 // Maximum size of VM stack allowed.
+ TierStepGas uint64 = 0 // Once per operation, for a selection of them.
+ LogTopicGas uint64 = 375 // Multiplied by the * of the LOG*, per LOG transaction. e.g. LOG0 incurs 0 * c_txLogTopicGas, LOG4 incurs 4 * c_txLogTopicGas.
+ CreateGas uint64 = 32000 // Once per CREATE operation & contract-creation transaction.
+ Create2Gas uint64 = 32000 // Once per CREATE2 operation
+ SelfdestructRefundGas uint64 = 24000 // Refunded following a selfdestruct operation.
+ MemoryGas uint64 = 3 // Times the address of the (highest referenced byte in memory + 1). NOTE: referencing happens on read, write and in instructions such as RETURN and CALL.
+ TxDataNonZeroGasFrontier uint64 = 68 // Per byte of data attached to a transaction that is not equal to zero. NOTE: Not payable on data of calls between transactions.
+ TxDataNonZeroGasEIP2028 uint64 = 16 // Per byte of non zero data attached to a transaction after EIP 2028 (part in Istanbul)
+
+ // These have been changed during the course of the chain
+ CallGasFrontier uint64 = 40 // Once per CALL operation & message call transaction.
+ CallGasEIP150 uint64 = 700 // Static portion of gas for CALL-derivates after EIP 150 (Tangerine)
+ BalanceGasFrontier uint64 = 20 // The cost of a BALANCE operation
+ BalanceGasEIP150 uint64 = 400 // The cost of a BALANCE operation after Tangerine
+ BalanceGasEIP1884 uint64 = 700 // The cost of a BALANCE operation after EIP 1884 (part of Istanbul)
+ ExtcodeSizeGasFrontier uint64 = 20 // Cost of EXTCODESIZE before EIP 150 (Tangerine)
+ ExtcodeSizeGasEIP150 uint64 = 700 // Cost of EXTCODESIZE after EIP 150 (Tangerine)
+ SloadGasFrontier uint64 = 50
+ SloadGasEIP150 uint64 = 200
+ SloadGasEIP1884 uint64 = 800 // Cost of SLOAD after EIP 1884 (part of Istanbul)
+ ExtcodeHashGasConstantinople uint64 = 400 // Cost of EXTCODEHASH (introduced in Constantinople)
+ ExtcodeHashGasEIP1884 uint64 = 700 // Cost of EXTCODEHASH after EIP 1884 (part in Istanbul)
+ SelfdestructGasEIP150 uint64 = 5000 // Cost of SELFDESTRUCT post EIP 150 (Tangerine)
+
+ // EXP has a dynamic portion depending on the size of the exponent
+ ExpByteFrontier uint64 = 10 // was set to 10 in Frontier
+ ExpByteEIP158 uint64 = 50 // was raised to 50 during Eip158 (Spurious Dragon)
+
+ // Extcodecopy has a dynamic AND a static cost. This represents only the
+ // static portion of the gas. It was changed during EIP 150 (Tangerine)
+ ExtcodeCopyBaseFrontier uint64 = 20
+ ExtcodeCopyBaseEIP150 uint64 = 700
+
+ // CreateBySelfdestructGas is used when the refunded account is one that does
+ // not exist. This logic is similar to call.
+ // Introduced in Tangerine Whistle (Eip 150)
+ CreateBySelfdestructGas uint64 = 25000
+
+ MaxCodeSize = 24576 // Maximum bytecode to permit for a contract
+
+ // Precompiled contract gas prices
+
+ EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price
+ Sha256BaseGas uint64 = 60 // Base price for a SHA256 operation
+ Sha256PerWordGas uint64 = 12 // Per-word price for a SHA256 operation
+ Ripemd160BaseGas uint64 = 600 // Base price for a RIPEMD160 operation
+ Ripemd160PerWordGas uint64 = 120 // Per-word price for a RIPEMD160 operation
+ IdentityBaseGas uint64 = 15 // Base price for a data copy operation
+ IdentityPerWordGas uint64 = 3 // Per-work price for a data copy operation
+ ModExpQuadCoeffDiv uint64 = 20 // Divisor for the quadratic particle of the big int modular exponentiation
+
+ Bn256AddGasByzantium uint64 = 500 // Byzantium gas needed for an elliptic curve addition
+ Bn256AddGasIstanbul uint64 = 150 // Gas needed for an elliptic curve addition
+ Bn256ScalarMulGasByzantium uint64 = 40000 // Byzantium gas needed for an elliptic curve scalar multiplication
+ Bn256ScalarMulGasIstanbul uint64 = 6000 // Gas needed for an elliptic curve scalar multiplication
+ Bn256PairingBaseGasByzantium uint64 = 100000 // Byzantium base price for an elliptic curve pairing check
+ Bn256PairingBaseGasIstanbul uint64 = 45000 // Base price for an elliptic curve pairing check
+ Bn256PairingPerPointGasByzantium uint64 = 80000 // Byzantium per-point price for an elliptic curve pairing check
+ Bn256PairingPerPointGasIstanbul uint64 = 34000 // Per-point price for an elliptic curve pairing check
+)
+
+var (
+ DifficultyBoundDivisor = big.NewInt(2048) // The bound divisor of the difficulty, used in the update calculations.
+ GenesisDifficulty = big.NewInt(131072) // Difficulty of the Genesis block.
+ MinimumDifficulty = big.NewInt(131072) // The minimum that the difficulty may ever be.
+ DurationLimit = big.NewInt(13) // The decision boundary on the blocktime duration used to determine whether difficulty should go up or not.
)
diff --git a/params/version.go b/params/version.go
new file mode 100644
index 0000000..94386f5
--- /dev/null
+++ b/params/version.go
@@ -0,0 +1,67 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package params
+
+import (
+ "fmt"
+)
+
+const (
+ VersionMajor = 1 // Major version component of the current release
+ VersionMinor = 9 // Minor version component of the current release
+ VersionPatch = 3 // Patch version component of the current release
+ VersionMeta = "unstable" // Version metadata to append to the version string
+)
+
+// Version holds the textual version string.
+var Version = func() string {
+ return fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
+}()
+
+// VersionWithMeta holds the textual version string including the metadata.
+var VersionWithMeta = func() string {
+ v := Version
+ if VersionMeta != "" {
+ v += "-" + VersionMeta
+ }
+ return v
+}()
+
+// ArchiveVersion holds the textual version string used for Geth archives.
+// e.g. "1.8.11-dea1ce05" for stable releases, or
+// "1.8.13-unstable-21c059b6" for unstable releases
+func ArchiveVersion(gitCommit string) string {
+ vsn := Version
+ if VersionMeta != "stable" {
+ vsn += "-" + VersionMeta
+ }
+ if len(gitCommit) >= 8 {
+ vsn += "-" + gitCommit[:8]
+ }
+ return vsn
+}
+
+func VersionWithCommit(gitCommit, gitDate string) string {
+ vsn := VersionWithMeta
+ if len(gitCommit) >= 8 {
+ vsn += "-" + gitCommit[:8]
+ }
+ if (VersionMeta != "stable") && (gitDate != "") {
+ vsn += "-" + gitDate
+ }
+ return vsn
+}
diff --git a/plugin/evm/block.go b/plugin/evm/block.go
index eaa78a5..449e261 100644
--- a/plugin/evm/block.go
+++ b/plugin/evm/block.go
@@ -6,7 +6,7 @@ package evm
import (
"fmt"
- "github.com/ava-labs/go-ethereum/core/types"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/rlp"
"github.com/ava-labs/gecko/ids"
diff --git a/plugin/evm/service.go b/plugin/evm/service.go
index 70135cc..62b124f 100644
--- a/plugin/evm/service.go
+++ b/plugin/evm/service.go
@@ -11,9 +11,9 @@ import (
"github.com/ava-labs/coreth"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/go-ethereum/common"
"github.com/ava-labs/go-ethereum/common/hexutil"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/crypto"
)
diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go
index 4b16f71..5f5e802 100644
--- a/plugin/evm/vm.go
+++ b/plugin/evm/vm.go
@@ -18,13 +18,13 @@ import (
"github.com/ava-labs/coreth"
"github.com/ava-labs/coreth/core"
+ "github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/eth"
"github.com/ava-labs/coreth/node"
+ "github.com/ava-labs/coreth/rpc"
"github.com/ava-labs/go-ethereum/common"
- "github.com/ava-labs/go-ethereum/core/types"
"github.com/ava-labs/go-ethereum/rlp"
- "github.com/ava-labs/go-ethereum/rpc"
"github.com/ava-labs/gecko/api/admin"
"github.com/ava-labs/gecko/cache"
diff --git a/rpc/client.go b/rpc/client.go
new file mode 100644
index 0000000..1c7058b
--- /dev/null
+++ b/rpc/client.go
@@ -0,0 +1,625 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net/url"
+ "reflect"
+ "strconv"
+ "sync/atomic"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+var (
+ ErrClientQuit = errors.New("client is closed")
+ ErrNoResult = errors.New("no result in JSON-RPC response")
+ ErrSubscriptionQueueOverflow = errors.New("subscription queue overflow")
+ errClientReconnected = errors.New("client reconnected")
+ errDead = errors.New("connection lost")
+)
+
+const (
+ // Timeouts
+ defaultDialTimeout = 10 * time.Second // used if context has no deadline
+ subscribeTimeout = 5 * time.Second // overall timeout eth_subscribe, rpc_modules calls
+)
+
+const (
+ // Subscriptions are removed when the subscriber cannot keep up.
+ //
+ // This can be worked around by supplying a channel with sufficiently sized buffer,
+ // but this can be inconvenient and hard to explain in the docs. Another issue with
+ // buffered channels is that the buffer is static even though it might not be needed
+ // most of the time.
+ //
+ // The approach taken here is to maintain a per-subscription linked list buffer
+ // shrinks on demand. If the buffer reaches the size below, the subscription is
+ // dropped.
+ maxClientSubscriptionBuffer = 20000
+)
+
+// BatchElem is an element in a batch request.
+type BatchElem struct {
+ Method string
+ Args []interface{}
+ // The result is unmarshaled into this field. Result must be set to a
+ // non-nil pointer value of the desired type, otherwise the response will be
+ // discarded.
+ Result interface{}
+ // Error is set if the server returns an error for this request, or if
+ // unmarshaling into Result fails. It is not set for I/O errors.
+ Error error
+}
+
+// Client represents a connection to an RPC server.
+type Client struct {
+ idgen func() ID // for subscriptions
+ isHTTP bool
+ services *serviceRegistry
+
+ idCounter uint32
+
+ // This function, if non-nil, is called when the connection is lost.
+ reconnectFunc reconnectFunc
+
+ // writeConn is used for writing to the connection on the caller's goroutine. It should
+ // only be accessed outside of dispatch, with the write lock held. The write lock is
+ // taken by sending on requestOp and released by sending on sendDone.
+ writeConn jsonWriter
+
+ // for dispatch
+ close chan struct{}
+ closing chan struct{} // closed when client is quitting
+ didClose chan struct{} // closed when client quits
+ reconnected chan ServerCodec // where write/reconnect sends the new connection
+ readOp chan readOp // read messages
+ readErr chan error // errors from read
+ reqInit chan *requestOp // register response IDs, takes write lock
+ reqSent chan error // signals write completion, releases write lock
+ reqTimeout chan *requestOp // removes response IDs when call timeout expires
+}
+
+type reconnectFunc func(ctx context.Context) (ServerCodec, error)
+
+type clientContextKey struct{}
+
+type clientConn struct {
+ codec ServerCodec
+ handler *handler
+}
+
+func (c *Client) newClientConn(conn ServerCodec) *clientConn {
+ ctx := context.WithValue(context.Background(), clientContextKey{}, c)
+ handler := newHandler(ctx, conn, c.idgen, c.services)
+ return &clientConn{conn, handler}
+}
+
+func (cc *clientConn) close(err error, inflightReq *requestOp) {
+ cc.handler.close(err, inflightReq)
+ cc.codec.Close()
+}
+
+type readOp struct {
+ msgs []*jsonrpcMessage
+ batch bool
+}
+
+type requestOp struct {
+ ids []json.RawMessage
+ err error
+ resp chan *jsonrpcMessage // receives up to len(ids) responses
+ sub *ClientSubscription // only set for EthSubscribe requests
+}
+
+func (op *requestOp) wait(ctx context.Context, c *Client) (*jsonrpcMessage, error) {
+ select {
+ case <-ctx.Done():
+ // Send the timeout to dispatch so it can remove the request IDs.
+ if !c.isHTTP {
+ select {
+ case c.reqTimeout <- op:
+ case <-c.closing:
+ }
+ }
+ return nil, ctx.Err()
+ case resp := <-op.resp:
+ return resp, op.err
+ }
+}
+
+// Dial creates a new client for the given URL.
+//
+// The currently supported URL schemes are "http", "https", "ws" and "wss". If rawurl is a
+// file name with no URL scheme, a local socket connection is established using UNIX
+// domain sockets on supported platforms and named pipes on Windows. If you want to
+// configure transport options, use DialHTTP, DialWebsocket or DialIPC instead.
+//
+// For websocket connections, the origin is set to the local host name.
+//
+// The client reconnects automatically if the connection is lost.
+func Dial(rawurl string) (*Client, error) {
+ return DialContext(context.Background(), rawurl)
+}
+
+// DialContext creates a new RPC client, just like Dial.
+//
+// The context is used to cancel or time out the initial connection establishment. It does
+// not affect subsequent interactions with the client.
+func DialContext(ctx context.Context, rawurl string) (*Client, error) {
+ u, err := url.Parse(rawurl)
+ if err != nil {
+ return nil, err
+ }
+ switch u.Scheme {
+ case "http", "https":
+ return DialHTTP(rawurl)
+ case "ws", "wss":
+ return DialWebsocket(ctx, rawurl, "")
+ //case "stdio":
+ // return DialStdIO(ctx)
+ //case "":
+ // return DialIPC(ctx, rawurl)
+ default:
+ return nil, fmt.Errorf("no known transport for URL scheme %q", u.Scheme)
+ }
+}
+
+// Client retrieves the client from the context, if any. This can be used to perform
+// 'reverse calls' in a handler method.
+func ClientFromContext(ctx context.Context) (*Client, bool) {
+ client, ok := ctx.Value(clientContextKey{}).(*Client)
+ return client, ok
+}
+
+func newClient(initctx context.Context, connect reconnectFunc) (*Client, error) {
+ conn, err := connect(initctx)
+ if err != nil {
+ return nil, err
+ }
+ c := initClient(conn, randomIDGenerator(), new(serviceRegistry))
+ c.reconnectFunc = connect
+ return c, nil
+}
+
+func initClient(conn ServerCodec, idgen func() ID, services *serviceRegistry) *Client {
+ _, isHTTP := conn.(*httpConn)
+ c := &Client{
+ idgen: idgen,
+ isHTTP: isHTTP,
+ services: services,
+ writeConn: conn,
+ close: make(chan struct{}),
+ closing: make(chan struct{}),
+ didClose: make(chan struct{}),
+ reconnected: make(chan ServerCodec),
+ readOp: make(chan readOp),
+ readErr: make(chan error),
+ reqInit: make(chan *requestOp),
+ reqSent: make(chan error, 1),
+ reqTimeout: make(chan *requestOp),
+ }
+ if !isHTTP {
+ go c.dispatch(conn)
+ }
+ return c
+}
+
+// RegisterName creates a service for the given receiver type under the given name. When no
+// methods on the given receiver match the criteria to be either a RPC method or a
+// subscription an error is returned. Otherwise a new service is created and added to the
+// service collection this client provides to the server.
+func (c *Client) RegisterName(name string, receiver interface{}) error {
+ return c.services.registerName(name, receiver)
+}
+
+func (c *Client) nextID() json.RawMessage {
+ id := atomic.AddUint32(&c.idCounter, 1)
+ return strconv.AppendUint(nil, uint64(id), 10)
+}
+
+// SupportedModules calls the rpc_modules method, retrieving the list of
+// APIs that are available on the server.
+func (c *Client) SupportedModules() (map[string]string, error) {
+ var result map[string]string
+ ctx, cancel := context.WithTimeout(context.Background(), subscribeTimeout)
+ defer cancel()
+ err := c.CallContext(ctx, &result, "rpc_modules")
+ return result, err
+}
+
+// Close closes the client, aborting any in-flight requests.
+func (c *Client) Close() {
+ if c.isHTTP {
+ return
+ }
+ select {
+ case c.close <- struct{}{}:
+ <-c.didClose
+ case <-c.didClose:
+ }
+}
+
+// Call performs a JSON-RPC call with the given arguments and unmarshals into
+// result if no error occurred.
+//
+// The result must be a pointer so that package json can unmarshal into it. You
+// can also pass nil, in which case the result is ignored.
+func (c *Client) Call(result interface{}, method string, args ...interface{}) error {
+ ctx := context.Background()
+ return c.CallContext(ctx, result, method, args...)
+}
+
+// CallContext performs a JSON-RPC call with the given arguments. If the context is
+// canceled before the call has successfully returned, CallContext returns immediately.
+//
+// The result must be a pointer so that package json can unmarshal into it. You
+// can also pass nil, in which case the result is ignored.
+func (c *Client) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
+ msg, err := c.newMessage(method, args...)
+ if err != nil {
+ return err
+ }
+ op := &requestOp{ids: []json.RawMessage{msg.ID}, resp: make(chan *jsonrpcMessage, 1)}
+
+ if c.isHTTP {
+ err = c.sendHTTP(ctx, op, msg)
+ } else {
+ err = c.send(ctx, op, msg)
+ }
+ if err != nil {
+ return err
+ }
+
+ // dispatch has accepted the request and will close the channel when it quits.
+ switch resp, err := op.wait(ctx, c); {
+ case err != nil:
+ return err
+ case resp.Error != nil:
+ return resp.Error
+ case len(resp.Result) == 0:
+ return ErrNoResult
+ default:
+ return json.Unmarshal(resp.Result, &result)
+ }
+}
+
+// BatchCall sends all given requests as a single batch and waits for the server
+// to return a response for all of them.
+//
+// In contrast to Call, BatchCall only returns I/O errors. Any error specific to
+// a request is reported through the Error field of the corresponding BatchElem.
+//
+// Note that batch calls may not be executed atomically on the server side.
+func (c *Client) BatchCall(b []BatchElem) error {
+ ctx := context.Background()
+ return c.BatchCallContext(ctx, b)
+}
+
+// BatchCall sends all given requests as a single batch and waits for the server
+// to return a response for all of them. The wait duration is bounded by the
+// context's deadline.
+//
+// In contrast to CallContext, BatchCallContext only returns errors that have occurred
+// while sending the request. Any error specific to a request is reported through the
+// Error field of the corresponding BatchElem.
+//
+// Note that batch calls may not be executed atomically on the server side.
+func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
+ msgs := make([]*jsonrpcMessage, len(b))
+ op := &requestOp{
+ ids: make([]json.RawMessage, len(b)),
+ resp: make(chan *jsonrpcMessage, len(b)),
+ }
+ for i, elem := range b {
+ msg, err := c.newMessage(elem.Method, elem.Args...)
+ if err != nil {
+ return err
+ }
+ msgs[i] = msg
+ op.ids[i] = msg.ID
+ }
+
+ var err error
+ if c.isHTTP {
+ err = c.sendBatchHTTP(ctx, op, msgs)
+ } else {
+ err = c.send(ctx, op, msgs)
+ }
+
+ // Wait for all responses to come back.
+ for n := 0; n < len(b) && err == nil; n++ {
+ var resp *jsonrpcMessage
+ resp, err = op.wait(ctx, c)
+ if err != nil {
+ break
+ }
+ // Find the element corresponding to this response.
+ // The element is guaranteed to be present because dispatch
+ // only sends valid IDs to our channel.
+ var elem *BatchElem
+ for i := range msgs {
+ if bytes.Equal(msgs[i].ID, resp.ID) {
+ elem = &b[i]
+ break
+ }
+ }
+ if resp.Error != nil {
+ elem.Error = resp.Error
+ continue
+ }
+ if len(resp.Result) == 0 {
+ elem.Error = ErrNoResult
+ continue
+ }
+ elem.Error = json.Unmarshal(resp.Result, elem.Result)
+ }
+ return err
+}
+
+// Notify sends a notification, i.e. a method call that doesn't expect a response.
+func (c *Client) Notify(ctx context.Context, method string, args ...interface{}) error {
+ op := new(requestOp)
+ msg, err := c.newMessage(method, args...)
+ if err != nil {
+ return err
+ }
+ msg.ID = nil
+
+ if c.isHTTP {
+ return c.sendHTTP(ctx, op, msg)
+ } else {
+ return c.send(ctx, op, msg)
+ }
+}
+
+// EthSubscribe registers a subscripion under the "eth" namespace.
+func (c *Client) EthSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
+ return c.Subscribe(ctx, "eth", channel, args...)
+}
+
+// ShhSubscribe registers a subscripion under the "shh" namespace.
+func (c *Client) ShhSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
+ return c.Subscribe(ctx, "shh", channel, args...)
+}
+
+// Subscribe calls the "<namespace>_subscribe" method with the given arguments,
+// registering a subscription. Server notifications for the subscription are
+// sent to the given channel. The element type of the channel must match the
+// expected type of content returned by the subscription.
+//
+// The context argument cancels the RPC request that sets up the subscription but has no
+// effect on the subscription after Subscribe has returned.
+//
+// Slow subscribers will be dropped eventually. Client buffers up to 20000 notifications
+// before considering the subscriber dead. The subscription Err channel will receive
+// ErrSubscriptionQueueOverflow. Use a sufficiently large buffer on the channel or ensure
+// that the channel usually has at least one reader to prevent this issue.
+func (c *Client) Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
+ // Check type of channel first.
+ chanVal := reflect.ValueOf(channel)
+ if chanVal.Kind() != reflect.Chan || chanVal.Type().ChanDir()&reflect.SendDir == 0 {
+ panic("first argument to Subscribe must be a writable channel")
+ }
+ if chanVal.IsNil() {
+ panic("channel given to Subscribe must not be nil")
+ }
+ if c.isHTTP {
+ return nil, ErrNotificationsUnsupported
+ }
+
+ msg, err := c.newMessage(namespace+subscribeMethodSuffix, args...)
+ if err != nil {
+ return nil, err
+ }
+ op := &requestOp{
+ ids: []json.RawMessage{msg.ID},
+ resp: make(chan *jsonrpcMessage),
+ sub: newClientSubscription(c, namespace, chanVal),
+ }
+
+ // Send the subscription request.
+ // The arrival and validity of the response is signaled on sub.quit.
+ if err := c.send(ctx, op, msg); err != nil {
+ return nil, err
+ }
+ if _, err := op.wait(ctx, c); err != nil {
+ return nil, err
+ }
+ return op.sub, nil
+}
+
+func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMessage, error) {
+ msg := &jsonrpcMessage{Version: vsn, ID: c.nextID(), Method: method}
+ if paramsIn != nil { // prevent sending "params":null
+ var err error
+ if msg.Params, err = json.Marshal(paramsIn); err != nil {
+ return nil, err
+ }
+ }
+ return msg, nil
+}
+
+// send registers op with the dispatch loop, then sends msg on the connection.
+// if sending fails, op is deregistered.
+func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error {
+ select {
+ case c.reqInit <- op:
+ err := c.write(ctx, msg)
+ c.reqSent <- err
+ return err
+ case <-ctx.Done():
+ // This can happen if the client is overloaded or unable to keep up with
+ // subscription notifications.
+ return ctx.Err()
+ case <-c.closing:
+ return ErrClientQuit
+ }
+}
+
+func (c *Client) write(ctx context.Context, msg interface{}) error {
+ // The previous write failed. Try to establish a new connection.
+ if c.writeConn == nil {
+ if err := c.reconnect(ctx); err != nil {
+ return err
+ }
+ }
+ err := c.writeConn.Write(ctx, msg)
+ if err != nil {
+ c.writeConn = nil
+ }
+ return err
+}
+
+func (c *Client) reconnect(ctx context.Context) error {
+ if c.reconnectFunc == nil {
+ return errDead
+ }
+
+ if _, ok := ctx.Deadline(); !ok {
+ var cancel func()
+ ctx, cancel = context.WithTimeout(ctx, defaultDialTimeout)
+ defer cancel()
+ }
+ newconn, err := c.reconnectFunc(ctx)
+ if err != nil {
+ log.Trace("RPC client reconnect failed", "err", err)
+ return err
+ }
+ select {
+ case c.reconnected <- newconn:
+ c.writeConn = newconn
+ return nil
+ case <-c.didClose:
+ newconn.Close()
+ return ErrClientQuit
+ }
+}
+
+// dispatch is the main loop of the client.
+// It sends read messages to waiting calls to Call and BatchCall
+// and subscription notifications to registered subscriptions.
+func (c *Client) dispatch(codec ServerCodec) {
+ var (
+ lastOp *requestOp // tracks last send operation
+ reqInitLock = c.reqInit // nil while the send lock is held
+ conn = c.newClientConn(codec)
+ reading = true
+ )
+ defer func() {
+ close(c.closing)
+ if reading {
+ conn.close(ErrClientQuit, nil)
+ c.drainRead()
+ }
+ close(c.didClose)
+ }()
+
+ // Spawn the initial read loop.
+ go c.read(codec)
+
+ for {
+ select {
+ case <-c.close:
+ return
+
+ // Read path:
+ case op := <-c.readOp:
+ if op.batch {
+ conn.handler.handleBatch(op.msgs)
+ } else {
+ conn.handler.handleMsg(op.msgs[0])
+ }
+
+ case err := <-c.readErr:
+ conn.handler.log.Debug("RPC connection read error", "err", err)
+ conn.close(err, lastOp)
+ reading = false
+
+ // Reconnect:
+ case newcodec := <-c.reconnected:
+ log.Debug("RPC client reconnected", "reading", reading, "conn", newcodec.RemoteAddr())
+ if reading {
+ // Wait for the previous read loop to exit. This is a rare case which
+ // happens if this loop isn't notified in time after the connection breaks.
+ // In those cases the caller will notice first and reconnect. Closing the
+ // handler terminates all waiting requests (closing op.resp) except for
+ // lastOp, which will be transferred to the new handler.
+ conn.close(errClientReconnected, lastOp)
+ c.drainRead()
+ }
+ go c.read(newcodec)
+ reading = true
+ conn = c.newClientConn(newcodec)
+ // Re-register the in-flight request on the new handler
+ // because that's where it will be sent.
+ conn.handler.addRequestOp(lastOp)
+
+ // Send path:
+ case op := <-reqInitLock:
+ // Stop listening for further requests until the current one has been sent.
+ reqInitLock = nil
+ lastOp = op
+ conn.handler.addRequestOp(op)
+
+ case err := <-c.reqSent:
+ if err != nil {
+ // Remove response handlers for the last send. When the read loop
+ // goes down, it will signal all other current operations.
+ conn.handler.removeRequestOp(lastOp)
+ }
+ // Let the next request in.
+ reqInitLock = c.reqInit
+ lastOp = nil
+
+ case op := <-c.reqTimeout:
+ conn.handler.removeRequestOp(op)
+ }
+ }
+}
+
+// drainRead drops read messages until an error occurs.
+func (c *Client) drainRead() {
+ for {
+ select {
+ case <-c.readOp:
+ case <-c.readErr:
+ return
+ }
+ }
+}
+
+// read decodes RPC messages from a codec, feeding them into dispatch.
+func (c *Client) read(codec ServerCodec) {
+ for {
+ msgs, batch, err := codec.Read()
+ if _, ok := err.(*json.SyntaxError); ok {
+ codec.Write(context.Background(), errorMessage(&parseError{err.Error()}))
+ }
+ if err != nil {
+ c.readErr <- err
+ return
+ }
+ c.readOp <- readOp{msgs, batch}
+ }
+}
diff --git a/rpc/client_example_test.go b/rpc/client_example_test.go
new file mode 100644
index 0000000..149de2c
--- /dev/null
+++ b/rpc/client_example_test.go
@@ -0,0 +1,88 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc_test
+
+import (
+ "context"
+ "fmt"
+ "math/big"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/rpc"
+)
+
+// In this example, our client wishes to track the latest 'block number'
+// known to the server. The server supports two methods:
+//
+// eth_getBlockByNumber("latest", {})
+// returns the latest block object.
+//
+// eth_subscribe("newBlocks")
+// creates a subscription which fires block objects when new blocks arrive.
+
+type Block struct {
+ Number *big.Int
+}
+
+func ExampleClientSubscription() {
+ // Connect the client.
+ client, _ := rpc.Dial("ws://127.0.0.1:8485")
+ subch := make(chan Block)
+
+ // Ensure that subch receives the latest block.
+ go func() {
+ for i := 0; ; i++ {
+ if i > 0 {
+ time.Sleep(2 * time.Second)
+ }
+ subscribeBlocks(client, subch)
+ }
+ }()
+
+ // Print events from the subscription as they arrive.
+ for block := range subch {
+ fmt.Println("latest block:", block.Number)
+ }
+}
+
+// subscribeBlocks runs in its own goroutine and maintains
+// a subscription for new blocks.
+func subscribeBlocks(client *rpc.Client, subch chan Block) {
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ // Subscribe to new blocks.
+ sub, err := client.EthSubscribe(ctx, subch, "newHeads")
+ if err != nil {
+ fmt.Println("subscribe error:", err)
+ return
+ }
+
+ // The connection is established now.
+ // Update the channel with the current block.
+ var lastBlock Block
+ if err := client.CallContext(ctx, &lastBlock, "eth_getBlockByNumber", "latest"); err != nil {
+ fmt.Println("can't get latest block:", err)
+ return
+ }
+ subch <- lastBlock
+
+ // The subscription will deliver events to the channel. Wait for the
+ // subscription to end for any reason, then loop around to re-establish
+ // the connection.
+ fmt.Println("connection lost: ", <-sub.Err())
+}
diff --git a/rpc/client_test.go b/rpc/client_test.go
new file mode 100644
index 0000000..79ea32e
--- /dev/null
+++ b/rpc/client_test.go
@@ -0,0 +1,569 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "fmt"
+ "math/rand"
+ "net"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "reflect"
+ "runtime"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/davecgh/go-spew/spew"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+func TestClientRequest(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+ client := DialInProc(server)
+ defer client.Close()
+
+ var resp Result
+ if err := client.Call(&resp, "test_echo", "hello", 10, &Args{"world"}); err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(resp, Result{"hello", 10, &Args{"world"}}) {
+ t.Errorf("incorrect result %#v", resp)
+ }
+}
+
+func TestClientBatchRequest(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+ client := DialInProc(server)
+ defer client.Close()
+
+ batch := []BatchElem{
+ {
+ Method: "test_echo",
+ Args: []interface{}{"hello", 10, &Args{"world"}},
+ Result: new(Result),
+ },
+ {
+ Method: "test_echo",
+ Args: []interface{}{"hello2", 11, &Args{"world"}},
+ Result: new(Result),
+ },
+ {
+ Method: "no_such_method",
+ Args: []interface{}{1, 2, 3},
+ Result: new(int),
+ },
+ }
+ if err := client.BatchCall(batch); err != nil {
+ t.Fatal(err)
+ }
+ wantResult := []BatchElem{
+ {
+ Method: "test_echo",
+ Args: []interface{}{"hello", 10, &Args{"world"}},
+ Result: &Result{"hello", 10, &Args{"world"}},
+ },
+ {
+ Method: "test_echo",
+ Args: []interface{}{"hello2", 11, &Args{"world"}},
+ Result: &Result{"hello2", 11, &Args{"world"}},
+ },
+ {
+ Method: "no_such_method",
+ Args: []interface{}{1, 2, 3},
+ Result: new(int),
+ Error: &jsonError{Code: -32601, Message: "the method no_such_method does not exist/is not available"},
+ },
+ }
+ if !reflect.DeepEqual(batch, wantResult) {
+ t.Errorf("batch results mismatch:\ngot %swant %s", spew.Sdump(batch), spew.Sdump(wantResult))
+ }
+}
+
+func TestClientNotify(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+ client := DialInProc(server)
+ defer client.Close()
+
+ if err := client.Notify(context.Background(), "test_echo", "hello", 10, &Args{"world"}); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// func TestClientCancelInproc(t *testing.T) { testClientCancel("inproc", t) }
+func TestClientCancelWebsocket(t *testing.T) { testClientCancel("ws", t) }
+func TestClientCancelHTTP(t *testing.T) { testClientCancel("http", t) }
+func TestClientCancelIPC(t *testing.T) { testClientCancel("ipc", t) }
+
+// This test checks that requests made through CallContext can be canceled by canceling
+// the context.
+func testClientCancel(transport string, t *testing.T) {
+ // These tests take a lot of time, run them all at once.
+ // You probably want to run with -parallel 1 or comment out
+ // the call to t.Parallel if you enable the logging.
+ t.Parallel()
+
+ server := newTestServer()
+ defer server.Stop()
+
+ // What we want to achieve is that the context gets canceled
+ // at various stages of request processing. The interesting cases
+ // are:
+ // - cancel during dial
+ // - cancel while performing a HTTP request
+ // - cancel while waiting for a response
+ //
+ // To trigger those, the times are chosen such that connections
+ // are killed within the deadline for every other call (maxKillTimeout
+ // is 2x maxCancelTimeout).
+ //
+ // Once a connection is dead, there is a fair chance it won't connect
+ // successfully because the accept is delayed by 1s.
+ maxContextCancelTimeout := 300 * time.Millisecond
+ fl := &flakeyListener{
+ maxAcceptDelay: 1 * time.Second,
+ maxKillTimeout: 600 * time.Millisecond,
+ }
+
+ var client *Client
+ switch transport {
+ case "ws", "http":
+ c, hs := httpTestClient(server, transport, fl)
+ defer hs.Close()
+ client = c
+ case "ipc":
+ c, l := ipcTestClient(server, fl)
+ defer l.Close()
+ client = c
+ default:
+ panic("unknown transport: " + transport)
+ }
+
+ // The actual test starts here.
+ var (
+ wg sync.WaitGroup
+ nreqs = 10
+ ncallers = 6
+ )
+ caller := func(index int) {
+ defer wg.Done()
+ for i := 0; i < nreqs; i++ {
+ var (
+ ctx context.Context
+ cancel func()
+ timeout = time.Duration(rand.Int63n(int64(maxContextCancelTimeout)))
+ )
+ if index < ncallers/2 {
+ // For half of the callers, create a context without deadline
+ // and cancel it later.
+ ctx, cancel = context.WithCancel(context.Background())
+ time.AfterFunc(timeout, cancel)
+ } else {
+ // For the other half, create a context with a deadline instead. This is
+ // different because the context deadline is used to set the socket write
+ // deadline.
+ ctx, cancel = context.WithTimeout(context.Background(), timeout)
+ }
+ // Now perform a call with the context.
+ // The key thing here is that no call will ever complete successfully.
+ sleepTime := maxContextCancelTimeout + 20*time.Millisecond
+ err := client.CallContext(ctx, nil, "test_sleep", sleepTime)
+ if err != nil {
+ log.Debug(fmt.Sprint("got expected error:", err))
+ } else {
+ t.Errorf("no error for call with %v wait time", timeout)
+ }
+ cancel()
+ }
+ }
+ wg.Add(ncallers)
+ for i := 0; i < ncallers; i++ {
+ go caller(i)
+ }
+ wg.Wait()
+}
+
+func TestClientSubscribeInvalidArg(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+ client := DialInProc(server)
+ defer client.Close()
+
+ check := func(shouldPanic bool, arg interface{}) {
+ defer func() {
+ err := recover()
+ if shouldPanic && err == nil {
+ t.Errorf("EthSubscribe should've panicked for %#v", arg)
+ }
+ if !shouldPanic && err != nil {
+ t.Errorf("EthSubscribe shouldn't have panicked for %#v", arg)
+ buf := make([]byte, 1024*1024)
+ buf = buf[:runtime.Stack(buf, false)]
+ t.Error(err)
+ t.Error(string(buf))
+ }
+ }()
+ client.EthSubscribe(context.Background(), arg, "foo_bar")
+ }
+ check(true, nil)
+ check(true, 1)
+ check(true, (chan int)(nil))
+ check(true, make(<-chan int))
+ check(false, make(chan int))
+ check(false, make(chan<- int))
+}
+
+func TestClientSubscribe(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+ client := DialInProc(server)
+ defer client.Close()
+
+ nc := make(chan int)
+ count := 10
+ sub, err := client.Subscribe(context.Background(), "nftest", nc, "someSubscription", count, 0)
+ if err != nil {
+ t.Fatal("can't subscribe:", err)
+ }
+ for i := 0; i < count; i++ {
+ if val := <-nc; val != i {
+ t.Fatalf("value mismatch: got %d, want %d", val, i)
+ }
+ }
+
+ sub.Unsubscribe()
+ select {
+ case v := <-nc:
+ t.Fatal("received value after unsubscribe:", v)
+ case err := <-sub.Err():
+ if err != nil {
+ t.Fatalf("Err returned a non-nil error after explicit unsubscribe: %q", err)
+ }
+ case <-time.After(1 * time.Second):
+ t.Fatalf("subscription not closed within 1s after unsubscribe")
+ }
+}
+
+// In this test, the connection drops while Subscribe is waiting for a response.
+func TestClientSubscribeClose(t *testing.T) {
+ server := newTestServer()
+ service := &notificationTestService{
+ gotHangSubscriptionReq: make(chan struct{}),
+ unblockHangSubscription: make(chan struct{}),
+ }
+ if err := server.RegisterName("nftest2", service); err != nil {
+ t.Fatal(err)
+ }
+
+ defer server.Stop()
+ client := DialInProc(server)
+ defer client.Close()
+
+ var (
+ nc = make(chan int)
+ errc = make(chan error)
+ sub *ClientSubscription
+ err error
+ )
+ go func() {
+ sub, err = client.Subscribe(context.Background(), "nftest2", nc, "hangSubscription", 999)
+ errc <- err
+ }()
+
+ <-service.gotHangSubscriptionReq
+ client.Close()
+ service.unblockHangSubscription <- struct{}{}
+
+ select {
+ case err := <-errc:
+ if err == nil {
+ t.Errorf("Subscribe returned nil error after Close")
+ }
+ if sub != nil {
+ t.Error("Subscribe returned non-nil subscription after Close")
+ }
+ case <-time.After(1 * time.Second):
+ t.Fatalf("Subscribe did not return within 1s after Close")
+ }
+}
+
+// This test reproduces https://github.com/ethereum/go-ethereum/issues/17837 where the
+// client hangs during shutdown when Unsubscribe races with Client.Close.
+func TestClientCloseUnsubscribeRace(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+
+ for i := 0; i < 20; i++ {
+ client := DialInProc(server)
+ nc := make(chan int)
+ sub, err := client.Subscribe(context.Background(), "nftest", nc, "someSubscription", 3, 1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ go client.Close()
+ go sub.Unsubscribe()
+ select {
+ case <-sub.Err():
+ case <-time.After(5 * time.Second):
+ t.Fatal("subscription not closed within timeout")
+ }
+ }
+}
+
+// This test checks that Client doesn't lock up when a single subscriber
+// doesn't read subscription events.
+func TestClientNotificationStorm(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+
+ doTest := func(count int, wantError bool) {
+ client := DialInProc(server)
+ defer client.Close()
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ // Subscribe on the server. It will start sending many notifications
+ // very quickly.
+ nc := make(chan int)
+ sub, err := client.Subscribe(ctx, "nftest", nc, "someSubscription", count, 0)
+ if err != nil {
+ t.Fatal("can't subscribe:", err)
+ }
+ defer sub.Unsubscribe()
+
+ // Process each notification, try to run a call in between each of them.
+ for i := 0; i < count; i++ {
+ select {
+ case val := <-nc:
+ if val != i {
+ t.Fatalf("(%d/%d) unexpected value %d", i, count, val)
+ }
+ case err := <-sub.Err():
+ if wantError && err != ErrSubscriptionQueueOverflow {
+ t.Fatalf("(%d/%d) got error %q, want %q", i, count, err, ErrSubscriptionQueueOverflow)
+ } else if !wantError {
+ t.Fatalf("(%d/%d) got unexpected error %q", i, count, err)
+ }
+ return
+ }
+ var r int
+ err := client.CallContext(ctx, &r, "nftest_echo", i)
+ if err != nil {
+ if !wantError {
+ t.Fatalf("(%d/%d) call error: %v", i, count, err)
+ }
+ return
+ }
+ }
+ if wantError {
+ t.Fatalf("didn't get expected error")
+ }
+ }
+
+ doTest(8000, false)
+ doTest(21000, true)
+}
+
+func TestClientHTTP(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+
+ client, hs := httpTestClient(server, "http", nil)
+ defer hs.Close()
+ defer client.Close()
+
+ // Launch concurrent requests.
+ var (
+ results = make([]Result, 100)
+ errc = make(chan error)
+ wantResult = Result{"a", 1, new(Args)}
+ )
+ defer client.Close()
+ for i := range results {
+ i := i
+ go func() {
+ errc <- client.Call(&results[i], "test_echo",
+ wantResult.String, wantResult.Int, wantResult.Args)
+ }()
+ }
+
+ // Wait for all of them to complete.
+ timeout := time.NewTimer(5 * time.Second)
+ defer timeout.Stop()
+ for i := range results {
+ select {
+ case err := <-errc:
+ if err != nil {
+ t.Fatal(err)
+ }
+ case <-timeout.C:
+ t.Fatalf("timeout (got %d/%d) results)", i+1, len(results))
+ }
+ }
+
+ // Check results.
+ for i := range results {
+ if !reflect.DeepEqual(results[i], wantResult) {
+ t.Errorf("result %d mismatch: got %#v, want %#v", i, results[i], wantResult)
+ }
+ }
+}
+
+func TestClientReconnect(t *testing.T) {
+ startServer := func(addr string) (*Server, net.Listener) {
+ srv := newTestServer()
+ l, err := net.Listen("tcp", addr)
+ if err != nil {
+ t.Fatal("can't listen:", err)
+ }
+ go http.Serve(l, srv.WebsocketHandler([]string{"*"}))
+ return srv, l
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
+ defer cancel()
+
+ // Start a server and corresponding client.
+ s1, l1 := startServer("127.0.0.1:0")
+ client, err := DialContext(ctx, "ws://"+l1.Addr().String())
+ if err != nil {
+ t.Fatal("can't dial", err)
+ }
+
+ // Perform a call. This should work because the server is up.
+ var resp Result
+ if err := client.CallContext(ctx, &resp, "test_echo", "", 1, nil); err != nil {
+ t.Fatal(err)
+ }
+
+ // Shut down the server and allow for some cool down time so we can listen on the same
+ // address again.
+ l1.Close()
+ s1.Stop()
+ time.Sleep(2 * time.Second)
+
+ // Try calling again. It shouldn't work.
+ if err := client.CallContext(ctx, &resp, "test_echo", "", 2, nil); err == nil {
+ t.Error("successful call while the server is down")
+ t.Logf("resp: %#v", resp)
+ }
+
+ // Start it up again and call again. The connection should be reestablished.
+ // We spawn multiple calls here to check whether this hangs somehow.
+ s2, l2 := startServer(l1.Addr().String())
+ defer l2.Close()
+ defer s2.Stop()
+
+ start := make(chan struct{})
+ errors := make(chan error, 20)
+ for i := 0; i < cap(errors); i++ {
+ go func() {
+ <-start
+ var resp Result
+ errors <- client.CallContext(ctx, &resp, "test_echo", "", 3, nil)
+ }()
+ }
+ close(start)
+ errcount := 0
+ for i := 0; i < cap(errors); i++ {
+ if err = <-errors; err != nil {
+ errcount++
+ }
+ }
+ t.Logf("%d errors, last error: %v", errcount, err)
+ if errcount > 1 {
+ t.Errorf("expected one error after disconnect, got %d", errcount)
+ }
+}
+
+func httpTestClient(srv *Server, transport string, fl *flakeyListener) (*Client, *httptest.Server) {
+ // Create the HTTP server.
+ var hs *httptest.Server
+ switch transport {
+ case "ws":
+ hs = httptest.NewUnstartedServer(srv.WebsocketHandler([]string{"*"}))
+ case "http":
+ hs = httptest.NewUnstartedServer(srv)
+ default:
+ panic("unknown HTTP transport: " + transport)
+ }
+ // Wrap the listener if required.
+ if fl != nil {
+ fl.Listener = hs.Listener
+ hs.Listener = fl
+ }
+ // Connect the client.
+ hs.Start()
+ client, err := Dial(transport + "://" + hs.Listener.Addr().String())
+ if err != nil {
+ panic(err)
+ }
+ return client, hs
+}
+
+func ipcTestClient(srv *Server, fl *flakeyListener) (*Client, net.Listener) {
+ // Listen on a random endpoint.
+ endpoint := fmt.Sprintf("go-ethereum-test-ipc-%d-%d", os.Getpid(), rand.Int63())
+ if runtime.GOOS == "windows" {
+ endpoint = `\\.\pipe\` + endpoint
+ } else {
+ endpoint = os.TempDir() + "/" + endpoint
+ }
+ l, err := ipcListen(endpoint)
+ if err != nil {
+ panic(err)
+ }
+ // Connect the listener to the server.
+ if fl != nil {
+ fl.Listener = l
+ l = fl
+ }
+ go srv.ServeListener(l)
+ // Connect the client.
+ client, err := Dial(endpoint)
+ if err != nil {
+ panic(err)
+ }
+ return client, l
+}
+
+// flakeyListener kills accepted connections after a random timeout.
+type flakeyListener struct {
+ net.Listener
+ maxKillTimeout time.Duration
+ maxAcceptDelay time.Duration
+}
+
+func (l *flakeyListener) Accept() (net.Conn, error) {
+ delay := time.Duration(rand.Int63n(int64(l.maxAcceptDelay)))
+ time.Sleep(delay)
+
+ c, err := l.Listener.Accept()
+ if err == nil {
+ timeout := time.Duration(rand.Int63n(int64(l.maxKillTimeout)))
+ time.AfterFunc(timeout, func() {
+ log.Debug(fmt.Sprintf("killing conn %v after %v", c.LocalAddr(), timeout))
+ c.Close()
+ })
+ }
+ return c, err
+}
diff --git a/rpc/constants_unix_nocgo.go b/rpc/constants_unix_nocgo.go
new file mode 100644
index 0000000..ecb231f
--- /dev/null
+++ b/rpc/constants_unix_nocgo.go
@@ -0,0 +1,25 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// +build !cgo,!windows
+
+package rpc
+
+var (
+ // On Linux, sun_path is 108 bytes in size
+ // see http://man7.org/linux/man-pages/man7/unix.7.html
+ max_path_size = 108
+)
diff --git a/rpc/doc.go b/rpc/doc.go
new file mode 100644
index 0000000..e5840c3
--- /dev/null
+++ b/rpc/doc.go
@@ -0,0 +1,118 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+/*
+
+Package rpc implements bi-directional JSON-RPC 2.0 on multiple transports.
+
+It provides access to the exported methods of an object across a network or other I/O
+connection. After creating a server or client instance, objects can be registered to make
+them visible as 'services'. Exported methods that follow specific conventions can be
+called remotely. It also has support for the publish/subscribe pattern.
+
+RPC Methods
+
+Methods that satisfy the following criteria are made available for remote access:
+
+ - method must be exported
+ - method returns 0, 1 (response or error) or 2 (response and error) values
+ - method argument(s) must be exported or builtin types
+ - method returned value(s) must be exported or builtin types
+
+An example method:
+
+ func (s *CalcService) Add(a, b int) (int, error)
+
+When the returned error isn't nil the returned integer is ignored and the error is sent
+back to the client. Otherwise the returned integer is sent back to the client.
+
+Optional arguments are supported by accepting pointer values as arguments. E.g. if we want
+to do the addition in an optional finite field we can accept a mod argument as pointer
+value.
+
+ func (s *CalcService) Add(a, b int, mod *int) (int, error)
+
+This RPC method can be called with 2 integers and a null value as third argument. In that
+case the mod argument will be nil. Or it can be called with 3 integers, in that case mod
+will be pointing to the given third argument. Since the optional argument is the last
+argument the RPC package will also accept 2 integers as arguments. It will pass the mod
+argument as nil to the RPC method.
+
+The server offers the ServeCodec method which accepts a ServerCodec instance. It will read
+requests from the codec, process the request and sends the response back to the client
+using the codec. The server can execute requests concurrently. Responses can be sent back
+to the client out of order.
+
+An example server which uses the JSON codec:
+
+ type CalculatorService struct {}
+
+ func (s *CalculatorService) Add(a, b int) int {
+ return a + b
+ }
+
+ func (s *CalculatorService) Div(a, b int) (int, error) {
+ if b == 0 {
+ return 0, errors.New("divide by zero")
+ }
+ return a/b, nil
+ }
+
+ calculator := new(CalculatorService)
+ server := NewServer()
+ server.RegisterName("calculator", calculator")
+
+ l, _ := net.ListenUnix("unix", &net.UnixAddr{Net: "unix", Name: "/tmp/calculator.sock"})
+ for {
+ c, _ := l.AcceptUnix()
+ codec := v2.NewJSONCodec(c)
+ go server.ServeCodec(codec, 0)
+ }
+
+Subscriptions
+
+The package also supports the publish subscribe pattern through the use of subscriptions.
+A method that is considered eligible for notifications must satisfy the following
+criteria:
+
+ - method must be exported
+ - first method argument type must be context.Context
+ - method argument(s) must be exported or builtin types
+ - method must have return types (rpc.Subscription, error)
+
+An example method:
+
+ func (s *BlockChainService) NewBlocks(ctx context.Context) (rpc.Subscription, error) {
+ ...
+ }
+
+When the service containing the subscription method is registered to the server, for
+example under the "blockchain" namespace, a subscription is created by calling the
+"blockchain_subscribe" method.
+
+Subscriptions are deleted when the user sends an unsubscribe request or when the
+connection which was used to create the subscription is closed. This can be initiated by
+the client and server. The server will close the connection for any write error.
+
+For more information about subscriptions, see https://github.com/ethereum/go-ethereum/wiki/RPC-PUB-SUB.
+
+Reverse Calls
+
+In any method handler, an instance of rpc.Client can be accessed through the
+ClientFromContext method. Using this client instance, server-to-client method calls can be
+performed on the RPC connection.
+*/
+package rpc
diff --git a/rpc/errors.go b/rpc/errors.go
new file mode 100644
index 0000000..c3aa826
--- /dev/null
+++ b/rpc/errors.go
@@ -0,0 +1,65 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import "fmt"
+
+const defaultErrorCode = -32000
+
+type methodNotFoundError struct{ method string }
+
+func (e *methodNotFoundError) ErrorCode() int { return -32601 }
+
+func (e *methodNotFoundError) Error() string {
+ return fmt.Sprintf("the method %s does not exist/is not available", e.method)
+}
+
+type subscriptionNotFoundError struct{ namespace, subscription string }
+
+func (e *subscriptionNotFoundError) ErrorCode() int { return -32601 }
+
+func (e *subscriptionNotFoundError) Error() string {
+ return fmt.Sprintf("no %q subscription in %s namespace", e.subscription, e.namespace)
+}
+
+// Invalid JSON was received by the server.
+type parseError struct{ message string }
+
+func (e *parseError) ErrorCode() int { return -32700 }
+
+func (e *parseError) Error() string { return e.message }
+
+// received message isn't a valid request
+type invalidRequestError struct{ message string }
+
+func (e *invalidRequestError) ErrorCode() int { return -32600 }
+
+func (e *invalidRequestError) Error() string { return e.message }
+
+// received message is invalid
+type invalidMessageError struct{ message string }
+
+func (e *invalidMessageError) ErrorCode() int { return -32700 }
+
+func (e *invalidMessageError) Error() string { return e.message }
+
+// unable to decode supplied params, or an invalid number of parameters
+type invalidParamsError struct{ message string }
+
+func (e *invalidParamsError) ErrorCode() int { return -32602 }
+
+func (e *invalidParamsError) Error() string { return e.message }
diff --git a/rpc/gzip.go b/rpc/gzip.go
new file mode 100644
index 0000000..a14fd09
--- /dev/null
+++ b/rpc/gzip.go
@@ -0,0 +1,66 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "compress/gzip"
+ "io"
+ "io/ioutil"
+ "net/http"
+ "strings"
+ "sync"
+)
+
+var gzPool = sync.Pool{
+ New: func() interface{} {
+ w := gzip.NewWriter(ioutil.Discard)
+ return w
+ },
+}
+
+type gzipResponseWriter struct {
+ io.Writer
+ http.ResponseWriter
+}
+
+func (w *gzipResponseWriter) WriteHeader(status int) {
+ w.Header().Del("Content-Length")
+ w.ResponseWriter.WriteHeader(status)
+}
+
+func (w *gzipResponseWriter) Write(b []byte) (int, error) {
+ return w.Writer.Write(b)
+}
+
+func newGzipHandler(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
+ next.ServeHTTP(w, r)
+ return
+ }
+
+ w.Header().Set("Content-Encoding", "gzip")
+
+ gz := gzPool.Get().(*gzip.Writer)
+ defer gzPool.Put(gz)
+
+ gz.Reset(w)
+ defer gz.Close()
+
+ next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
+ })
+}
diff --git a/rpc/handler.go b/rpc/handler.go
new file mode 100644
index 0000000..187d0f8
--- /dev/null
+++ b/rpc/handler.go
@@ -0,0 +1,397 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "encoding/json"
+ "reflect"
+ "strconv"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+// handler handles JSON-RPC messages. There is one handler per connection. Note that
+// handler is not safe for concurrent use. Message handling never blocks indefinitely
+// because RPCs are processed on background goroutines launched by handler.
+//
+// The entry points for incoming messages are:
+//
+// h.handleMsg(message)
+// h.handleBatch(message)
+//
+// Outgoing calls use the requestOp struct. Register the request before sending it
+// on the connection:
+//
+// op := &requestOp{ids: ...}
+// h.addRequestOp(op)
+//
+// Now send the request, then wait for the reply to be delivered through handleMsg:
+//
+// if err := op.wait(...); err != nil {
+// h.removeRequestOp(op) // timeout, etc.
+// }
+//
+type handler struct {
+ reg *serviceRegistry
+ unsubscribeCb *callback
+ idgen func() ID // subscription ID generator
+ respWait map[string]*requestOp // active client requests
+ clientSubs map[string]*ClientSubscription // active client subscriptions
+ callWG sync.WaitGroup // pending call goroutines
+ rootCtx context.Context // canceled by close()
+ cancelRoot func() // cancel function for rootCtx
+ conn jsonWriter // where responses will be sent
+ log log.Logger
+ allowSubscribe bool
+
+ subLock sync.Mutex
+ serverSubs map[ID]*Subscription
+}
+
+type callProc struct {
+ ctx context.Context
+ notifiers []*Notifier
+}
+
+func newHandler(connCtx context.Context, conn jsonWriter, idgen func() ID, reg *serviceRegistry) *handler {
+ rootCtx, cancelRoot := context.WithCancel(connCtx)
+ h := &handler{
+ reg: reg,
+ idgen: idgen,
+ conn: conn,
+ respWait: make(map[string]*requestOp),
+ clientSubs: make(map[string]*ClientSubscription),
+ rootCtx: rootCtx,
+ cancelRoot: cancelRoot,
+ allowSubscribe: true,
+ serverSubs: make(map[ID]*Subscription),
+ log: log.Root(),
+ }
+ if conn.RemoteAddr() != "" {
+ h.log = h.log.New("conn", conn.RemoteAddr())
+ }
+ h.unsubscribeCb = newCallback(reflect.Value{}, reflect.ValueOf(h.unsubscribe))
+ return h
+}
+
+// handleBatch executes all messages in a batch and returns the responses.
+func (h *handler) handleBatch(msgs []*jsonrpcMessage) {
+ // Emit error response for empty batches:
+ if len(msgs) == 0 {
+ h.startCallProc(func(cp *callProc) {
+ h.conn.Write(cp.ctx, errorMessage(&invalidRequestError{"empty batch"}))
+ })
+ return
+ }
+
+ // Handle non-call messages first:
+ calls := make([]*jsonrpcMessage, 0, len(msgs))
+ for _, msg := range msgs {
+ if handled := h.handleImmediate(msg); !handled {
+ calls = append(calls, msg)
+ }
+ }
+ if len(calls) == 0 {
+ return
+ }
+ // Process calls on a goroutine because they may block indefinitely:
+ h.startCallProc(func(cp *callProc) {
+ answers := make([]*jsonrpcMessage, 0, len(msgs))
+ for _, msg := range calls {
+ if answer := h.handleCallMsg(cp, msg); answer != nil {
+ answers = append(answers, answer)
+ }
+ }
+ h.addSubscriptions(cp.notifiers)
+ if len(answers) > 0 {
+ h.conn.Write(cp.ctx, answers)
+ }
+ for _, n := range cp.notifiers {
+ n.activate()
+ }
+ })
+}
+
+// handleMsg handles a single message.
+func (h *handler) handleMsg(msg *jsonrpcMessage) {
+ if ok := h.handleImmediate(msg); ok {
+ return
+ }
+ h.startCallProc(func(cp *callProc) {
+ answer := h.handleCallMsg(cp, msg)
+ h.addSubscriptions(cp.notifiers)
+ if answer != nil {
+ h.conn.Write(cp.ctx, answer)
+ }
+ for _, n := range cp.notifiers {
+ n.activate()
+ }
+ })
+}
+
+// close cancels all requests except for inflightReq and waits for
+// call goroutines to shut down.
+func (h *handler) close(err error, inflightReq *requestOp) {
+ h.cancelAllRequests(err, inflightReq)
+ h.callWG.Wait()
+ h.cancelRoot()
+ h.cancelServerSubscriptions(err)
+}
+
+// addRequestOp registers a request operation.
+func (h *handler) addRequestOp(op *requestOp) {
+ for _, id := range op.ids {
+ h.respWait[string(id)] = op
+ }
+}
+
+// removeRequestOps stops waiting for the given request IDs.
+func (h *handler) removeRequestOp(op *requestOp) {
+ for _, id := range op.ids {
+ delete(h.respWait, string(id))
+ }
+}
+
+// cancelAllRequests unblocks and removes pending requests and active subscriptions.
+func (h *handler) cancelAllRequests(err error, inflightReq *requestOp) {
+ didClose := make(map[*requestOp]bool)
+ if inflightReq != nil {
+ didClose[inflightReq] = true
+ }
+
+ for id, op := range h.respWait {
+ // Remove the op so that later calls will not close op.resp again.
+ delete(h.respWait, id)
+
+ if !didClose[op] {
+ op.err = err
+ close(op.resp)
+ didClose[op] = true
+ }
+ }
+ for id, sub := range h.clientSubs {
+ delete(h.clientSubs, id)
+ sub.quitWithError(err, false)
+ }
+}
+
+func (h *handler) addSubscriptions(nn []*Notifier) {
+ h.subLock.Lock()
+ defer h.subLock.Unlock()
+
+ for _, n := range nn {
+ if sub := n.takeSubscription(); sub != nil {
+ h.serverSubs[sub.ID] = sub
+ }
+ }
+}
+
+// cancelServerSubscriptions removes all subscriptions and closes their error channels.
+func (h *handler) cancelServerSubscriptions(err error) {
+ h.subLock.Lock()
+ defer h.subLock.Unlock()
+
+ for id, s := range h.serverSubs {
+ s.err <- err
+ close(s.err)
+ delete(h.serverSubs, id)
+ }
+}
+
+// startCallProc runs fn in a new goroutine and starts tracking it in the h.calls wait group.
+func (h *handler) startCallProc(fn func(*callProc)) {
+ h.callWG.Add(1)
+ go func() {
+ ctx, cancel := context.WithCancel(h.rootCtx)
+ defer h.callWG.Done()
+ defer cancel()
+ fn(&callProc{ctx: ctx})
+ }()
+}
+
+// handleImmediate executes non-call messages. It returns false if the message is a
+// call or requires a reply.
+func (h *handler) handleImmediate(msg *jsonrpcMessage) bool {
+ start := time.Now()
+ switch {
+ case msg.isNotification():
+ if strings.HasSuffix(msg.Method, notificationMethodSuffix) {
+ h.handleSubscriptionResult(msg)
+ return true
+ }
+ return false
+ case msg.isResponse():
+ h.handleResponse(msg)
+ h.log.Trace("Handled RPC response", "reqid", idForLog{msg.ID}, "t", time.Since(start))
+ return true
+ default:
+ return false
+ }
+}
+
+// handleSubscriptionResult processes subscription notifications.
+func (h *handler) handleSubscriptionResult(msg *jsonrpcMessage) {
+ var result subscriptionResult
+ if err := json.Unmarshal(msg.Params, &result); err != nil {
+ h.log.Debug("Dropping invalid subscription message")
+ return
+ }
+ if h.clientSubs[result.ID] != nil {
+ h.clientSubs[result.ID].deliver(result.Result)
+ }
+}
+
+// handleResponse processes method call responses.
+func (h *handler) handleResponse(msg *jsonrpcMessage) {
+ op := h.respWait[string(msg.ID)]
+ if op == nil {
+ h.log.Debug("Unsolicited RPC response", "reqid", idForLog{msg.ID})
+ return
+ }
+ delete(h.respWait, string(msg.ID))
+ // For normal responses, just forward the reply to Call/BatchCall.
+ if op.sub == nil {
+ op.resp <- msg
+ return
+ }
+ // For subscription responses, start the subscription if the server
+ // indicates success. EthSubscribe gets unblocked in either case through
+ // the op.resp channel.
+ defer close(op.resp)
+ if msg.Error != nil {
+ op.err = msg.Error
+ return
+ }
+ if op.err = json.Unmarshal(msg.Result, &op.sub.subid); op.err == nil {
+ go op.sub.start()
+ h.clientSubs[op.sub.subid] = op.sub
+ }
+}
+
+// handleCallMsg executes a call message and returns the answer.
+func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
+ start := time.Now()
+ switch {
+ case msg.isNotification():
+ h.handleCall(ctx, msg)
+ h.log.Debug("Served "+msg.Method, "t", time.Since(start))
+ return nil
+ case msg.isCall():
+ resp := h.handleCall(ctx, msg)
+ if resp.Error != nil {
+ h.log.Warn("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start), "err", resp.Error.Message)
+ } else {
+ h.log.Debug("Served "+msg.Method, "reqid", idForLog{msg.ID}, "t", time.Since(start))
+ }
+ return resp
+ case msg.hasValidID():
+ return msg.errorResponse(&invalidRequestError{"invalid request"})
+ default:
+ return errorMessage(&invalidRequestError{"invalid request"})
+ }
+}
+
+// handleCall processes method calls.
+func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
+ if msg.isSubscribe() {
+ return h.handleSubscribe(cp, msg)
+ }
+ var callb *callback
+ if msg.isUnsubscribe() {
+ callb = h.unsubscribeCb
+ } else {
+ callb = h.reg.callback(msg.Method)
+ }
+ if callb == nil {
+ return msg.errorResponse(&methodNotFoundError{method: msg.Method})
+ }
+ args, err := parsePositionalArguments(msg.Params, callb.argTypes)
+ if err != nil {
+ return msg.errorResponse(&invalidParamsError{err.Error()})
+ }
+
+ return h.runMethod(cp.ctx, msg, callb, args)
+}
+
+// handleSubscribe processes *_subscribe method calls.
+func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage {
+ if !h.allowSubscribe {
+ return msg.errorResponse(ErrNotificationsUnsupported)
+ }
+
+ // Subscription method name is first argument.
+ name, err := parseSubscriptionName(msg.Params)
+ if err != nil {
+ return msg.errorResponse(&invalidParamsError{err.Error()})
+ }
+ namespace := msg.namespace()
+ callb := h.reg.subscription(namespace, name)
+ if callb == nil {
+ return msg.errorResponse(&subscriptionNotFoundError{namespace, name})
+ }
+
+ // Parse subscription name arg too, but remove it before calling the callback.
+ argTypes := append([]reflect.Type{stringType}, callb.argTypes...)
+ args, err := parsePositionalArguments(msg.Params, argTypes)
+ if err != nil {
+ return msg.errorResponse(&invalidParamsError{err.Error()})
+ }
+ args = args[1:]
+
+ // Install notifier in context so the subscription handler can find it.
+ n := &Notifier{h: h, namespace: namespace}
+ cp.notifiers = append(cp.notifiers, n)
+ ctx := context.WithValue(cp.ctx, notifierKey{}, n)
+
+ return h.runMethod(ctx, msg, callb, args)
+}
+
+// runMethod runs the Go callback for an RPC method.
+func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *callback, args []reflect.Value) *jsonrpcMessage {
+ result, err := callb.call(ctx, msg.Method, args)
+ if err != nil {
+ return msg.errorResponse(err)
+ }
+ return msg.response(result)
+}
+
+// unsubscribe is the callback function for all *_unsubscribe calls.
+func (h *handler) unsubscribe(ctx context.Context, id ID) (bool, error) {
+ h.subLock.Lock()
+ defer h.subLock.Unlock()
+
+ s := h.serverSubs[id]
+ if s == nil {
+ return false, ErrSubscriptionNotFound
+ }
+ close(s.err)
+ delete(h.serverSubs, id)
+ return true, nil
+}
+
+type idForLog struct{ json.RawMessage }
+
+func (id idForLog) String() string {
+ if s, err := strconv.Unquote(string(id.RawMessage)); err == nil {
+ return s
+ }
+ return string(id.RawMessage)
+}
diff --git a/rpc/http.go b/rpc/http.go
new file mode 100644
index 0000000..2dffc5d
--- /dev/null
+++ b/rpc/http.go
@@ -0,0 +1,359 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "mime"
+ "net"
+ "net/http"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/rs/cors"
+)
+
+const (
+ maxRequestContentLength = 1024 * 1024 * 5
+ contentType = "application/json"
+)
+
+// https://www.jsonrpc.org/historical/json-rpc-over-http.html#id13
+var acceptedContentTypes = []string{contentType, "application/json-rpc", "application/jsonrequest"}
+
+type httpConn struct {
+ client *http.Client
+ req *http.Request
+ closeOnce sync.Once
+ closed chan interface{}
+}
+
+// httpConn is treated specially by Client.
+func (hc *httpConn) Write(context.Context, interface{}) error {
+ panic("Write called on httpConn")
+}
+
+func (hc *httpConn) RemoteAddr() string {
+ return hc.req.URL.String()
+}
+
+func (hc *httpConn) Read() ([]*jsonrpcMessage, bool, error) {
+ <-hc.closed
+ return nil, false, io.EOF
+}
+
+func (hc *httpConn) Close() {
+ hc.closeOnce.Do(func() { close(hc.closed) })
+}
+
+func (hc *httpConn) Closed() <-chan interface{} {
+ return hc.closed
+}
+
+// HTTPTimeouts represents the configuration params for the HTTP RPC server.
+type HTTPTimeouts struct {
+ // ReadTimeout is the maximum duration for reading the entire
+ // request, including the body.
+ //
+ // Because ReadTimeout does not let Handlers make per-request
+ // decisions on each request body's acceptable deadline or
+ // upload rate, most users will prefer to use
+ // ReadHeaderTimeout. It is valid to use them both.
+ ReadTimeout time.Duration
+
+ // WriteTimeout is the maximum duration before timing out
+ // writes of the response. It is reset whenever a new
+ // request's header is read. Like ReadTimeout, it does not
+ // let Handlers make decisions on a per-request basis.
+ WriteTimeout time.Duration
+
+ // IdleTimeout is the maximum amount of time to wait for the
+ // next request when keep-alives are enabled. If IdleTimeout
+ // is zero, the value of ReadTimeout is used. If both are
+ // zero, ReadHeaderTimeout is used.
+ IdleTimeout time.Duration
+}
+
+// DefaultHTTPTimeouts represents the default timeout values used if further
+// configuration is not provided.
+var DefaultHTTPTimeouts = HTTPTimeouts{
+ ReadTimeout: 30 * time.Second,
+ WriteTimeout: 30 * time.Second,
+ IdleTimeout: 120 * time.Second,
+}
+
+// DialHTTPWithClient creates a new RPC client that connects to an RPC server over HTTP
+// using the provided HTTP Client.
+func DialHTTPWithClient(endpoint string, client *http.Client) (*Client, error) {
+ req, err := http.NewRequest(http.MethodPost, endpoint, nil)
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Content-Type", contentType)
+ req.Header.Set("Accept", contentType)
+
+ initctx := context.Background()
+ return newClient(initctx, func(context.Context) (ServerCodec, error) {
+ return &httpConn{client: client, req: req, closed: make(chan interface{})}, nil
+ })
+}
+
+// DialHTTP creates a new RPC client that connects to an RPC server over HTTP.
+func DialHTTP(endpoint string) (*Client, error) {
+ return DialHTTPWithClient(endpoint, new(http.Client))
+}
+
+func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error {
+ hc := c.writeConn.(*httpConn)
+ respBody, err := hc.doRequest(ctx, msg)
+ if respBody != nil {
+ defer respBody.Close()
+ }
+
+ if err != nil {
+ if respBody != nil {
+ buf := new(bytes.Buffer)
+ if _, err2 := buf.ReadFrom(respBody); err2 == nil {
+ return fmt.Errorf("%v %v", err, buf.String())
+ }
+ }
+ return err
+ }
+ var respmsg jsonrpcMessage
+ if err := json.NewDecoder(respBody).Decode(&respmsg); err != nil {
+ return err
+ }
+ op.resp <- &respmsg
+ return nil
+}
+
+func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonrpcMessage) error {
+ hc := c.writeConn.(*httpConn)
+ respBody, err := hc.doRequest(ctx, msgs)
+ if err != nil {
+ return err
+ }
+ defer respBody.Close()
+ var respmsgs []jsonrpcMessage
+ if err := json.NewDecoder(respBody).Decode(&respmsgs); err != nil {
+ return err
+ }
+ for i := 0; i < len(respmsgs); i++ {
+ op.resp <- &respmsgs[i]
+ }
+ return nil
+}
+
+func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadCloser, error) {
+ body, err := json.Marshal(msg)
+ if err != nil {
+ return nil, err
+ }
+ req := hc.req.WithContext(ctx)
+ req.Body = ioutil.NopCloser(bytes.NewReader(body))
+ req.ContentLength = int64(len(body))
+
+ resp, err := hc.client.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
+ return resp.Body, errors.New(resp.Status)
+ }
+ return resp.Body, nil
+}
+
+// httpServerConn turns a HTTP connection into a Conn.
+type httpServerConn struct {
+ io.Reader
+ io.Writer
+ r *http.Request
+}
+
+func newHTTPServerConn(r *http.Request, w http.ResponseWriter) ServerCodec {
+ body := io.LimitReader(r.Body, maxRequestContentLength)
+ conn := &httpServerConn{Reader: body, Writer: w, r: r}
+ return NewJSONCodec(conn)
+}
+
+// Close does nothing and always returns nil.
+func (t *httpServerConn) Close() error { return nil }
+
+// RemoteAddr returns the peer address of the underlying connection.
+func (t *httpServerConn) RemoteAddr() string {
+ return t.r.RemoteAddr
+}
+
+// SetWriteDeadline does nothing and always returns nil.
+func (t *httpServerConn) SetWriteDeadline(time.Time) error { return nil }
+
+// NewHTTPServer creates a new HTTP RPC server around an API provider.
+//
+// Deprecated: Server implements http.Handler
+func NewHTTPServer(cors []string, vhosts []string, timeouts HTTPTimeouts, srv http.Handler) *http.Server {
+ // Wrap the CORS-handler within a host-handler
+ handler := newCorsHandler(srv, cors)
+ handler = newVHostHandler(vhosts, handler)
+ handler = newGzipHandler(handler)
+
+ // Make sure timeout values are meaningful
+ if timeouts.ReadTimeout < time.Second {
+ log.Warn("Sanitizing invalid HTTP read timeout", "provided", timeouts.ReadTimeout, "updated", DefaultHTTPTimeouts.ReadTimeout)
+ timeouts.ReadTimeout = DefaultHTTPTimeouts.ReadTimeout
+ }
+ if timeouts.WriteTimeout < time.Second {
+ log.Warn("Sanitizing invalid HTTP write timeout", "provided", timeouts.WriteTimeout, "updated", DefaultHTTPTimeouts.WriteTimeout)
+ timeouts.WriteTimeout = DefaultHTTPTimeouts.WriteTimeout
+ }
+ if timeouts.IdleTimeout < time.Second {
+ log.Warn("Sanitizing invalid HTTP idle timeout", "provided", timeouts.IdleTimeout, "updated", DefaultHTTPTimeouts.IdleTimeout)
+ timeouts.IdleTimeout = DefaultHTTPTimeouts.IdleTimeout
+ }
+ // Bundle and start the HTTP server
+ return &http.Server{
+ Handler: handler,
+ ReadTimeout: timeouts.ReadTimeout,
+ WriteTimeout: timeouts.WriteTimeout,
+ IdleTimeout: timeouts.IdleTimeout,
+ }
+}
+
+// ServeHTTP serves JSON-RPC requests over HTTP.
+func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ // Permit dumb empty requests for remote health-checks (AWS)
+ if r.Method == http.MethodGet && r.ContentLength == 0 && r.URL.RawQuery == "" {
+ return
+ }
+ if code, err := validateRequest(r); err != nil {
+ http.Error(w, err.Error(), code)
+ return
+ }
+ // All checks passed, create a codec that reads direct from the request body
+ // untilEOF and writes the response to w and order the server to process a
+ // single request.
+ ctx := r.Context()
+ ctx = context.WithValue(ctx, "remote", r.RemoteAddr)
+ ctx = context.WithValue(ctx, "scheme", r.Proto)
+ ctx = context.WithValue(ctx, "local", r.Host)
+ if ua := r.Header.Get("User-Agent"); ua != "" {
+ ctx = context.WithValue(ctx, "User-Agent", ua)
+ }
+ if origin := r.Header.Get("Origin"); origin != "" {
+ ctx = context.WithValue(ctx, "Origin", origin)
+ }
+
+ w.Header().Set("content-type", contentType)
+ codec := newHTTPServerConn(r, w)
+ defer codec.Close()
+ s.serveSingleRequest(ctx, codec)
+}
+
+// validateRequest returns a non-zero response code and error message if the
+// request is invalid.
+func validateRequest(r *http.Request) (int, error) {
+ if r.Method == http.MethodPut || r.Method == http.MethodDelete {
+ return http.StatusMethodNotAllowed, errors.New("method not allowed")
+ }
+ if r.ContentLength > maxRequestContentLength {
+ err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength)
+ return http.StatusRequestEntityTooLarge, err
+ }
+ // Allow OPTIONS (regardless of content-type)
+ if r.Method == http.MethodOptions {
+ return 0, nil
+ }
+ // Check content-type
+ if mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")); err == nil {
+ for _, accepted := range acceptedContentTypes {
+ if accepted == mt {
+ return 0, nil
+ }
+ }
+ }
+ // Invalid content-type
+ err := fmt.Errorf("invalid content type, only %s is supported", contentType)
+ return http.StatusUnsupportedMediaType, err
+}
+
+func newCorsHandler(srv http.Handler, allowedOrigins []string) http.Handler {
+ // disable CORS support if user has not specified a custom CORS configuration
+ if len(allowedOrigins) == 0 {
+ return srv
+ }
+ c := cors.New(cors.Options{
+ AllowedOrigins: allowedOrigins,
+ AllowedMethods: []string{http.MethodPost, http.MethodGet},
+ MaxAge: 600,
+ AllowedHeaders: []string{"*"},
+ })
+ return c.Handler(srv)
+}
+
+// virtualHostHandler is a handler which validates the Host-header of incoming requests.
+// The virtualHostHandler can prevent DNS rebinding attacks, which do not utilize CORS-headers,
+// since they do in-domain requests against the RPC api. Instead, we can see on the Host-header
+// which domain was used, and validate that against a whitelist.
+type virtualHostHandler struct {
+ vhosts map[string]struct{}
+ next http.Handler
+}
+
+// ServeHTTP serves JSON-RPC requests over HTTP, implements http.Handler
+func (h *virtualHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ // if r.Host is not set, we can continue serving since a browser would set the Host header
+ if r.Host == "" {
+ h.next.ServeHTTP(w, r)
+ return
+ }
+ host, _, err := net.SplitHostPort(r.Host)
+ if err != nil {
+ // Either invalid (too many colons) or no port specified
+ host = r.Host
+ }
+ if ipAddr := net.ParseIP(host); ipAddr != nil {
+ // It's an IP address, we can serve that
+ h.next.ServeHTTP(w, r)
+ return
+
+ }
+ // Not an ip address, but a hostname. Need to validate
+ if _, exist := h.vhosts["*"]; exist {
+ h.next.ServeHTTP(w, r)
+ return
+ }
+ if _, exist := h.vhosts[host]; exist {
+ h.next.ServeHTTP(w, r)
+ return
+ }
+ http.Error(w, "invalid host specified", http.StatusForbidden)
+}
+
+func newVHostHandler(vhosts []string, next http.Handler) http.Handler {
+ vhostMap := make(map[string]struct{})
+ for _, allowedHost := range vhosts {
+ vhostMap[strings.ToLower(allowedHost)] = struct{}{}
+ }
+ return &virtualHostHandler{vhostMap, next}
+}
diff --git a/rpc/http_test.go b/rpc/http_test.go
new file mode 100644
index 0000000..b3f694d
--- /dev/null
+++ b/rpc/http_test.go
@@ -0,0 +1,54 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+func TestHTTPErrorResponseWithDelete(t *testing.T) {
+ testHTTPErrorResponse(t, http.MethodDelete, contentType, "", http.StatusMethodNotAllowed)
+}
+
+func TestHTTPErrorResponseWithPut(t *testing.T) {
+ testHTTPErrorResponse(t, http.MethodPut, contentType, "", http.StatusMethodNotAllowed)
+}
+
+func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
+ body := make([]rune, maxRequestContentLength+1)
+ testHTTPErrorResponse(t,
+ http.MethodPost, contentType, string(body), http.StatusRequestEntityTooLarge)
+}
+
+func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
+ testHTTPErrorResponse(t, http.MethodPost, "", "", http.StatusUnsupportedMediaType)
+}
+
+func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
+ testHTTPErrorResponse(t, http.MethodPost, contentType, "", 0)
+}
+
+func testHTTPErrorResponse(t *testing.T, method, contentType, body string, expected int) {
+ request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
+ request.Header.Set("content-type", contentType)
+ if code, _ := validateRequest(request); code != expected {
+ t.Fatalf("response code should be %d not %d", expected, code)
+ }
+}
diff --git a/rpc/ipc_js.go b/rpc/ipc_js.go
new file mode 100644
index 0000000..7e7554a
--- /dev/null
+++ b/rpc/ipc_js.go
@@ -0,0 +1,37 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// +build js
+
+package rpc
+
+import (
+ "context"
+ "errors"
+ "net"
+)
+
+var errNotSupported = errors.New("rpc: not supported")
+
+// ipcListen will create a named pipe on the given endpoint.
+func ipcListen(endpoint string) (net.Listener, error) {
+ return nil, errNotSupported
+}
+
+// newIPCConnection will connect to a named pipe with the given endpoint as name.
+func newIPCConnection(ctx context.Context, endpoint string) (net.Conn, error) {
+ return nil, errNotSupported
+}
diff --git a/rpc/json.go b/rpc/json.go
new file mode 100644
index 0000000..75c2210
--- /dev/null
+++ b/rpc/json.go
@@ -0,0 +1,335 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "reflect"
+ "strings"
+ "sync"
+ "time"
+)
+
+const (
+ vsn = "2.0"
+ serviceMethodSeparator = "_"
+ subscribeMethodSuffix = "_subscribe"
+ unsubscribeMethodSuffix = "_unsubscribe"
+ notificationMethodSuffix = "_subscription"
+
+ defaultWriteTimeout = 10 * time.Second // used if context has no deadline
+)
+
+var null = json.RawMessage("null")
+
+type subscriptionResult struct {
+ ID string `json:"subscription"`
+ Result json.RawMessage `json:"result,omitempty"`
+}
+
+// A value of this type can a JSON-RPC request, notification, successful response or
+// error response. Which one it is depends on the fields.
+type jsonrpcMessage struct {
+ Version string `json:"jsonrpc,omitempty"`
+ ID json.RawMessage `json:"id,omitempty"`
+ Method string `json:"method,omitempty"`
+ Params json.RawMessage `json:"params,omitempty"`
+ Error *jsonError `json:"error,omitempty"`
+ Result json.RawMessage `json:"result,omitempty"`
+}
+
+func (msg *jsonrpcMessage) isNotification() bool {
+ return msg.ID == nil && msg.Method != ""
+}
+
+func (msg *jsonrpcMessage) isCall() bool {
+ return msg.hasValidID() && msg.Method != ""
+}
+
+func (msg *jsonrpcMessage) isResponse() bool {
+ return msg.hasValidID() && msg.Method == "" && msg.Params == nil && (msg.Result != nil || msg.Error != nil)
+}
+
+func (msg *jsonrpcMessage) hasValidID() bool {
+ return len(msg.ID) > 0 && msg.ID[0] != '{' && msg.ID[0] != '['
+}
+
+func (msg *jsonrpcMessage) isSubscribe() bool {
+ return strings.HasSuffix(msg.Method, subscribeMethodSuffix)
+}
+
+func (msg *jsonrpcMessage) isUnsubscribe() bool {
+ return strings.HasSuffix(msg.Method, unsubscribeMethodSuffix)
+}
+
+func (msg *jsonrpcMessage) namespace() string {
+ elem := strings.SplitN(msg.Method, serviceMethodSeparator, 2)
+ return elem[0]
+}
+
+func (msg *jsonrpcMessage) String() string {
+ b, _ := json.Marshal(msg)
+ return string(b)
+}
+
+func (msg *jsonrpcMessage) errorResponse(err error) *jsonrpcMessage {
+ resp := errorMessage(err)
+ resp.ID = msg.ID
+ return resp
+}
+
+func (msg *jsonrpcMessage) response(result interface{}) *jsonrpcMessage {
+ enc, err := json.Marshal(result)
+ if err != nil {
+ // TODO: wrap with 'internal server error'
+ return msg.errorResponse(err)
+ }
+ return &jsonrpcMessage{Version: vsn, ID: msg.ID, Result: enc}
+}
+
+func errorMessage(err error) *jsonrpcMessage {
+ msg := &jsonrpcMessage{Version: vsn, ID: null, Error: &jsonError{
+ Code: defaultErrorCode,
+ Message: err.Error(),
+ }}
+ ec, ok := err.(Error)
+ if ok {
+ msg.Error.Code = ec.ErrorCode()
+ }
+ return msg
+}
+
+type jsonError struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Data interface{} `json:"data,omitempty"`
+}
+
+func (err *jsonError) Error() string {
+ if err.Message == "" {
+ return fmt.Sprintf("json-rpc error %d", err.Code)
+ }
+ return err.Message
+}
+
+func (err *jsonError) ErrorCode() int {
+ return err.Code
+}
+
+// Conn is a subset of the methods of net.Conn which are sufficient for ServerCodec.
+type Conn interface {
+ io.ReadWriteCloser
+ SetWriteDeadline(time.Time) error
+}
+
+type deadlineCloser interface {
+ io.Closer
+ SetWriteDeadline(time.Time) error
+}
+
+// ConnRemoteAddr wraps the RemoteAddr operation, which returns a description
+// of the peer address of a connection. If a Conn also implements ConnRemoteAddr, this
+// description is used in log messages.
+type ConnRemoteAddr interface {
+ RemoteAddr() string
+}
+
+// connWithRemoteAddr overrides the remote address of a connection.
+type connWithRemoteAddr struct {
+ Conn
+ addr string
+}
+
+func (c connWithRemoteAddr) RemoteAddr() string { return c.addr }
+
+// jsonCodec reads and writes JSON-RPC messages to the underlying connection. It also has
+// support for parsing arguments and serializing (result) objects.
+type jsonCodec struct {
+ remoteAddr string
+ closer sync.Once // close closed channel once
+ closed chan interface{} // closed on Close
+ decode func(v interface{}) error // decoder to allow multiple transports
+ encMu sync.Mutex // guards the encoder
+ encode func(v interface{}) error // encoder to allow multiple transports
+ conn deadlineCloser
+}
+
+func newCodec(conn deadlineCloser, encode, decode func(v interface{}) error) ServerCodec {
+ codec := &jsonCodec{
+ closed: make(chan interface{}),
+ encode: encode,
+ decode: decode,
+ conn: conn,
+ }
+ if ra, ok := conn.(ConnRemoteAddr); ok {
+ codec.remoteAddr = ra.RemoteAddr()
+ }
+ return codec
+}
+
+// NewJSONCodec creates a codec that reads from the given connection. If conn implements
+// ConnRemoteAddr, log messages will use it to include the remote address of the
+// connection.
+func NewJSONCodec(conn Conn) ServerCodec {
+ enc := json.NewEncoder(conn)
+ dec := json.NewDecoder(conn)
+ dec.UseNumber()
+ return newCodec(conn, enc.Encode, dec.Decode)
+}
+
+func (c *jsonCodec) RemoteAddr() string {
+ return c.remoteAddr
+}
+
+func (c *jsonCodec) Read() (msg []*jsonrpcMessage, batch bool, err error) {
+ // Decode the next JSON object in the input stream.
+ // This verifies basic syntax, etc.
+ var rawmsg json.RawMessage
+ if err := c.decode(&rawmsg); err != nil {
+ return nil, false, err
+ }
+ msg, batch = parseMessage(rawmsg)
+ return msg, batch, nil
+}
+
+// Write sends a message to client.
+func (c *jsonCodec) Write(ctx context.Context, v interface{}) error {
+ c.encMu.Lock()
+ defer c.encMu.Unlock()
+
+ deadline, ok := ctx.Deadline()
+ if !ok {
+ deadline = time.Now().Add(defaultWriteTimeout)
+ }
+ c.conn.SetWriteDeadline(deadline)
+ return c.encode(v)
+}
+
+// Close the underlying connection
+func (c *jsonCodec) Close() {
+ c.closer.Do(func() {
+ close(c.closed)
+ c.conn.Close()
+ })
+}
+
+// Closed returns a channel which will be closed when Close is called
+func (c *jsonCodec) Closed() <-chan interface{} {
+ return c.closed
+}
+
+// parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error
+// checks in this function because the raw message has already been syntax-checked when it
+// is called. Any non-JSON-RPC messages in the input return the zero value of
+// jsonrpcMessage.
+func parseMessage(raw json.RawMessage) ([]*jsonrpcMessage, bool) {
+ if !isBatch(raw) {
+ msgs := []*jsonrpcMessage{{}}
+ json.Unmarshal(raw, &msgs[0])
+ return msgs, false
+ }
+ dec := json.NewDecoder(bytes.NewReader(raw))
+ dec.Token() // skip '['
+ var msgs []*jsonrpcMessage
+ for dec.More() {
+ msgs = append(msgs, new(jsonrpcMessage))
+ dec.Decode(&msgs[len(msgs)-1])
+ }
+ return msgs, true
+}
+
+// isBatch returns true when the first non-whitespace characters is '['
+func isBatch(raw json.RawMessage) bool {
+ for _, c := range raw {
+ // skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt)
+ if c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d {
+ continue
+ }
+ return c == '['
+ }
+ return false
+}
+
+// parsePositionalArguments tries to parse the given args to an array of values with the
+// given types. It returns the parsed values or an error when the args could not be
+// parsed. Missing optional arguments are returned as reflect.Zero values.
+func parsePositionalArguments(rawArgs json.RawMessage, types []reflect.Type) ([]reflect.Value, error) {
+ dec := json.NewDecoder(bytes.NewReader(rawArgs))
+ var args []reflect.Value
+ tok, err := dec.Token()
+ switch {
+ case err == io.EOF || tok == nil && err == nil:
+ // "params" is optional and may be empty. Also allow "params":null even though it's
+ // not in the spec because our own client used to send it.
+ case err != nil:
+ return nil, err
+ case tok == json.Delim('['):
+ // Read argument array.
+ if args, err = parseArgumentArray(dec, types); err != nil {
+ return nil, err
+ }
+ default:
+ return nil, errors.New("non-array args")
+ }
+ // Set any missing args to nil.
+ for i := len(args); i < len(types); i++ {
+ if types[i].Kind() != reflect.Ptr {
+ return nil, fmt.Errorf("missing value for required argument %d", i)
+ }
+ args = append(args, reflect.Zero(types[i]))
+ }
+ return args, nil
+}
+
+func parseArgumentArray(dec *json.Decoder, types []reflect.Type) ([]reflect.Value, error) {
+ args := make([]reflect.Value, 0, len(types))
+ for i := 0; dec.More(); i++ {
+ if i >= len(types) {
+ return args, fmt.Errorf("too many arguments, want at most %d", len(types))
+ }
+ argval := reflect.New(types[i])
+ if err := dec.Decode(argval.Interface()); err != nil {
+ return args, fmt.Errorf("invalid argument %d: %v", i, err)
+ }
+ if argval.IsNil() && types[i].Kind() != reflect.Ptr {
+ return args, fmt.Errorf("missing value for required argument %d", i)
+ }
+ args = append(args, argval.Elem())
+ }
+ // Read end of args array.
+ _, err := dec.Token()
+ return args, err
+}
+
+// parseSubscriptionName extracts the subscription name from an encoded argument array.
+func parseSubscriptionName(rawArgs json.RawMessage) (string, error) {
+ dec := json.NewDecoder(bytes.NewReader(rawArgs))
+ if tok, _ := dec.Token(); tok != json.Delim('[') {
+ return "", errors.New("non-array args")
+ }
+ v, _ := dec.Token()
+ method, ok := v.(string)
+ if !ok {
+ return "", errors.New("expected subscription name as first argument")
+ }
+ return method, nil
+}
diff --git a/rpc/server.go b/rpc/server.go
new file mode 100644
index 0000000..bf5d93e
--- /dev/null
+++ b/rpc/server.go
@@ -0,0 +1,147 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "io"
+ "sync/atomic"
+
+ mapset "github.com/deckarep/golang-set"
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+const MetadataApi = "rpc"
+
+// CodecOption specifies which type of messages a codec supports.
+//
+// Deprecated: this option is no longer honored by Server.
+type CodecOption int
+
+const (
+ // OptionMethodInvocation is an indication that the codec supports RPC method calls
+ OptionMethodInvocation CodecOption = 1 << iota
+
+ // OptionSubscriptions is an indication that the codec suports RPC notifications
+ OptionSubscriptions = 1 << iota // support pub sub
+)
+
+// Server is an RPC server.
+type Server struct {
+ services serviceRegistry
+ idgen func() ID
+ run int32
+ codecs mapset.Set
+}
+
+// NewServer creates a new server instance with no registered handlers.
+func NewServer() *Server {
+ server := &Server{idgen: randomIDGenerator(), codecs: mapset.NewSet(), run: 1}
+ // Register the default service providing meta information about the RPC service such
+ // as the services and methods it offers.
+ rpcService := &RPCService{server}
+ server.RegisterName(MetadataApi, rpcService)
+ return server
+}
+
+// RegisterName creates a service for the given receiver type under the given name. When no
+// methods on the given receiver match the criteria to be either a RPC method or a
+// subscription an error is returned. Otherwise a new service is created and added to the
+// service collection this server provides to clients.
+func (s *Server) RegisterName(name string, receiver interface{}) error {
+ return s.services.registerName(name, receiver)
+}
+
+// ServeCodec reads incoming requests from codec, calls the appropriate callback and writes
+// the response back using the given codec. It will block until the codec is closed or the
+// server is stopped. In either case the codec is closed.
+//
+// Note that codec options are no longer supported.
+func (s *Server) ServeCodec(codec ServerCodec, options CodecOption) {
+ defer codec.Close()
+
+ // Don't serve if server is stopped.
+ if atomic.LoadInt32(&s.run) == 0 {
+ return
+ }
+
+ // Add the codec to the set so it can be closed by Stop.
+ s.codecs.Add(codec)
+ defer s.codecs.Remove(codec)
+
+ c := initClient(codec, s.idgen, &s.services)
+ <-codec.Closed()
+ c.Close()
+}
+
+// serveSingleRequest reads and processes a single RPC request from the given codec. This
+// is used to serve HTTP connections. Subscriptions and reverse calls are not allowed in
+// this mode.
+func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
+ // Don't serve if server is stopped.
+ if atomic.LoadInt32(&s.run) == 0 {
+ return
+ }
+
+ h := newHandler(ctx, codec, s.idgen, &s.services)
+ h.allowSubscribe = false
+ defer h.close(io.EOF, nil)
+
+ reqs, batch, err := codec.Read()
+ if err != nil {
+ if err != io.EOF {
+ codec.Write(ctx, errorMessage(&invalidMessageError{"parse error"}))
+ }
+ return
+ }
+ if batch {
+ h.handleBatch(reqs)
+ } else {
+ h.handleMsg(reqs[0])
+ }
+}
+
+// Stop stops reading new requests, waits for stopPendingRequestTimeout to allow pending
+// requests to finish, then closes all codecs which will cancel pending requests and
+// subscriptions.
+func (s *Server) Stop() {
+ if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
+ log.Debug("RPC server shutting down")
+ s.codecs.Each(func(c interface{}) bool {
+ c.(ServerCodec).Close()
+ return true
+ })
+ }
+}
+
+// RPCService gives meta information about the server.
+// e.g. gives information about the loaded modules.
+type RPCService struct {
+ server *Server
+}
+
+// Modules returns the list of RPC services with their version number
+func (s *RPCService) Modules() map[string]string {
+ s.server.services.mu.Lock()
+ defer s.server.services.mu.Unlock()
+
+ modules := make(map[string]string)
+ for name := range s.server.services.services {
+ modules[name] = "1.0"
+ }
+ return modules
+}
diff --git a/rpc/server_test.go b/rpc/server_test.go
new file mode 100644
index 0000000..3909954
--- /dev/null
+++ b/rpc/server_test.go
@@ -0,0 +1,152 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "bufio"
+ "bytes"
+ "io"
+ "io/ioutil"
+ "net"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestServerRegisterName(t *testing.T) {
+ server := NewServer()
+ service := new(testService)
+
+ if err := server.RegisterName("test", service); err != nil {
+ t.Fatalf("%v", err)
+ }
+
+ if len(server.services.services) != 2 {
+ t.Fatalf("Expected 2 service entries, got %d", len(server.services.services))
+ }
+
+ svc, ok := server.services.services["test"]
+ if !ok {
+ t.Fatalf("Expected service calc to be registered")
+ }
+
+ wantCallbacks := 7
+ if len(svc.callbacks) != wantCallbacks {
+ t.Errorf("Expected %d callbacks for service 'service', got %d", wantCallbacks, len(svc.callbacks))
+ }
+}
+
+func TestServer(t *testing.T) {
+ files, err := ioutil.ReadDir("testdata")
+ if err != nil {
+ t.Fatal("where'd my testdata go?")
+ }
+ for _, f := range files {
+ if f.IsDir() || strings.HasPrefix(f.Name(), ".") {
+ continue
+ }
+ path := filepath.Join("testdata", f.Name())
+ name := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
+ t.Run(name, func(t *testing.T) {
+ runTestScript(t, path)
+ })
+ }
+}
+
+func runTestScript(t *testing.T, file string) {
+ server := newTestServer()
+ content, err := ioutil.ReadFile(file)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ clientConn, serverConn := net.Pipe()
+ defer clientConn.Close()
+ go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation|OptionSubscriptions)
+ readbuf := bufio.NewReader(clientConn)
+ for _, line := range strings.Split(string(content), "\n") {
+ line = strings.TrimSpace(line)
+ switch {
+ case len(line) == 0 || strings.HasPrefix(line, "//"):
+ // skip comments, blank lines
+ continue
+ case strings.HasPrefix(line, "--> "):
+ t.Log(line)
+ // write to connection
+ clientConn.SetWriteDeadline(time.Now().Add(5 * time.Second))
+ if _, err := io.WriteString(clientConn, line[4:]+"\n"); err != nil {
+ t.Fatalf("write error: %v", err)
+ }
+ case strings.HasPrefix(line, "<-- "):
+ t.Log(line)
+ want := line[4:]
+ // read line from connection and compare text
+ clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))
+ sent, err := readbuf.ReadString('\n')
+ if err != nil {
+ t.Fatalf("read error: %v", err)
+ }
+ sent = strings.TrimRight(sent, "\r\n")
+ if sent != want {
+ t.Errorf("wrong line from server\ngot: %s\nwant: %s", sent, want)
+ }
+ default:
+ panic("invalid line in test script: " + line)
+ }
+ }
+}
+
+// This test checks that responses are delivered for very short-lived connections that
+// only carry a single request.
+func TestServerShortLivedConn(t *testing.T) {
+ server := newTestServer()
+ defer server.Stop()
+
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal("can't listen:", err)
+ }
+ defer listener.Close()
+ go server.ServeListener(listener)
+
+ var (
+ request = `{"jsonrpc":"2.0","id":1,"method":"rpc_modules"}` + "\n"
+ wantResp = `{"jsonrpc":"2.0","id":1,"result":{"nftest":"1.0","rpc":"1.0","test":"1.0"}}` + "\n"
+ deadline = time.Now().Add(10 * time.Second)
+ )
+ for i := 0; i < 20; i++ {
+ conn, err := net.Dial("tcp", listener.Addr().String())
+ if err != nil {
+ t.Fatal("can't dial:", err)
+ }
+ defer conn.Close()
+ conn.SetDeadline(deadline)
+ // Write the request, then half-close the connection so the server stops reading.
+ conn.Write([]byte(request))
+ conn.(*net.TCPConn).CloseWrite()
+ // Now try to get the response.
+ buf := make([]byte, 2000)
+ n, err := conn.Read(buf)
+ if err != nil {
+ t.Fatal("read error:", err)
+ }
+ if !bytes.Equal(buf[:n], []byte(wantResp)) {
+ t.Fatalf("wrong response: %s", buf[:n])
+ }
+ }
+}
diff --git a/rpc/service.go b/rpc/service.go
new file mode 100644
index 0000000..ead6fb6
--- /dev/null
+++ b/rpc/service.go
@@ -0,0 +1,285 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "reflect"
+ "runtime"
+ "strings"
+ "sync"
+ "unicode"
+ "unicode/utf8"
+
+ "github.com/ava-labs/go-ethereum/log"
+)
+
+var (
+ contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
+ errorType = reflect.TypeOf((*error)(nil)).Elem()
+ subscriptionType = reflect.TypeOf(Subscription{})
+ stringType = reflect.TypeOf("")
+)
+
+type serviceRegistry struct {
+ mu sync.Mutex
+ services map[string]service
+}
+
+// service represents a registered object.
+type service struct {
+ name string // name for service
+ callbacks map[string]*callback // registered handlers
+ subscriptions map[string]*callback // available subscriptions/notifications
+}
+
+// callback is a method callback which was registered in the server
+type callback struct {
+ fn reflect.Value // the function
+ rcvr reflect.Value // receiver object of method, set if fn is method
+ argTypes []reflect.Type // input argument types
+ hasCtx bool // method's first argument is a context (not included in argTypes)
+ errPos int // err return idx, of -1 when method cannot return error
+ isSubscribe bool // true if this is a subscription callback
+}
+
+func (r *serviceRegistry) registerName(name string, rcvr interface{}) error {
+ rcvrVal := reflect.ValueOf(rcvr)
+ if name == "" {
+ return fmt.Errorf("no service name for type %s", rcvrVal.Type().String())
+ }
+ callbacks := suitableCallbacks(rcvrVal)
+ if len(callbacks) == 0 {
+ return fmt.Errorf("service %T doesn't have any suitable methods/subscriptions to expose", rcvr)
+ }
+
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ if r.services == nil {
+ r.services = make(map[string]service)
+ }
+ svc, ok := r.services[name]
+ if !ok {
+ svc = service{
+ name: name,
+ callbacks: make(map[string]*callback),
+ subscriptions: make(map[string]*callback),
+ }
+ r.services[name] = svc
+ }
+ for name, cb := range callbacks {
+ if cb.isSubscribe {
+ svc.subscriptions[name] = cb
+ } else {
+ svc.callbacks[name] = cb
+ }
+ }
+ return nil
+}
+
+// callback returns the callback corresponding to the given RPC method name.
+func (r *serviceRegistry) callback(method string) *callback {
+ elem := strings.SplitN(method, serviceMethodSeparator, 2)
+ if len(elem) != 2 {
+ return nil
+ }
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ return r.services[elem[0]].callbacks[elem[1]]
+}
+
+// subscription returns a subscription callback in the given service.
+func (r *serviceRegistry) subscription(service, name string) *callback {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ return r.services[service].subscriptions[name]
+}
+
+// suitableCallbacks iterates over the methods of the given type. It determines if a method
+// satisfies the criteria for a RPC callback or a subscription callback and adds it to the
+// collection of callbacks. See server documentation for a summary of these criteria.
+func suitableCallbacks(receiver reflect.Value) map[string]*callback {
+ typ := receiver.Type()
+ callbacks := make(map[string]*callback)
+ for m := 0; m < typ.NumMethod(); m++ {
+ method := typ.Method(m)
+ if method.PkgPath != "" {
+ continue // method not exported
+ }
+ cb := newCallback(receiver, method.Func)
+ if cb == nil {
+ continue // function invalid
+ }
+ name := formatName(method.Name)
+ callbacks[name] = cb
+ }
+ return callbacks
+}
+
+// newCallback turns fn (a function) into a callback object. It returns nil if the function
+// is unsuitable as an RPC callback.
+func newCallback(receiver, fn reflect.Value) *callback {
+ fntype := fn.Type()
+ c := &callback{fn: fn, rcvr: receiver, errPos: -1, isSubscribe: isPubSub(fntype)}
+ // Determine parameter types. They must all be exported or builtin types.
+ c.makeArgTypes()
+ if !allExportedOrBuiltin(c.argTypes) {
+ return nil
+ }
+ // Verify return types. The function must return at most one error
+ // and/or one other non-error value.
+ outs := make([]reflect.Type, fntype.NumOut())
+ for i := 0; i < fntype.NumOut(); i++ {
+ outs[i] = fntype.Out(i)
+ }
+ if len(outs) > 2 || !allExportedOrBuiltin(outs) {
+ return nil
+ }
+ // If an error is returned, it must be the last returned value.
+ switch {
+ case len(outs) == 1 && isErrorType(outs[0]):
+ c.errPos = 0
+ case len(outs) == 2:
+ if isErrorType(outs[0]) || !isErrorType(outs[1]) {
+ return nil
+ }
+ c.errPos = 1
+ }
+ return c
+}
+
+// makeArgTypes composes the argTypes list.
+func (c *callback) makeArgTypes() {
+ fntype := c.fn.Type()
+ // Skip receiver and context.Context parameter (if present).
+ firstArg := 0
+ if c.rcvr.IsValid() {
+ firstArg++
+ }
+ if fntype.NumIn() > firstArg && fntype.In(firstArg) == contextType {
+ c.hasCtx = true
+ firstArg++
+ }
+ // Add all remaining parameters.
+ c.argTypes = make([]reflect.Type, fntype.NumIn()-firstArg)
+ for i := firstArg; i < fntype.NumIn(); i++ {
+ c.argTypes[i-firstArg] = fntype.In(i)
+ }
+}
+
+// call invokes the callback.
+func (c *callback) call(ctx context.Context, method string, args []reflect.Value) (res interface{}, errRes error) {
+ // Create the argument slice.
+ fullargs := make([]reflect.Value, 0, 2+len(args))
+ if c.rcvr.IsValid() {
+ fullargs = append(fullargs, c.rcvr)
+ }
+ if c.hasCtx {
+ fullargs = append(fullargs, reflect.ValueOf(ctx))
+ }
+ fullargs = append(fullargs, args...)
+
+ // Catch panic while running the callback.
+ defer func() {
+ if err := recover(); err != nil {
+ const size = 64 << 10
+ buf := make([]byte, size)
+ buf = buf[:runtime.Stack(buf, false)]
+ log.Error("RPC method " + method + " crashed: " + fmt.Sprintf("%v\n%s", err, buf))
+ errRes = errors.New("method handler crashed")
+ }
+ }()
+ // Run the callback.
+ results := c.fn.Call(fullargs)
+ if len(results) == 0 {
+ return nil, nil
+ }
+ if c.errPos >= 0 && !results[c.errPos].IsNil() {
+ // Method has returned non-nil error value.
+ err := results[c.errPos].Interface().(error)
+ return reflect.Value{}, err
+ }
+ return results[0].Interface(), nil
+}
+
+// Is this an exported - upper case - name?
+func isExported(name string) bool {
+ rune, _ := utf8.DecodeRuneInString(name)
+ return unicode.IsUpper(rune)
+}
+
+// Are all those types exported or built-in?
+func allExportedOrBuiltin(types []reflect.Type) bool {
+ for _, typ := range types {
+ for typ.Kind() == reflect.Ptr {
+ typ = typ.Elem()
+ }
+ // PkgPath will be non-empty even for an exported type,
+ // so we need to check the type name as well.
+ if !isExported(typ.Name()) && typ.PkgPath() != "" {
+ return false
+ }
+ }
+ return true
+}
+
+// Is t context.Context or *context.Context?
+func isContextType(t reflect.Type) bool {
+ for t.Kind() == reflect.Ptr {
+ t = t.Elem()
+ }
+ return t == contextType
+}
+
+// Does t satisfy the error interface?
+func isErrorType(t reflect.Type) bool {
+ for t.Kind() == reflect.Ptr {
+ t = t.Elem()
+ }
+ return t.Implements(errorType)
+}
+
+// Is t Subscription or *Subscription?
+func isSubscriptionType(t reflect.Type) bool {
+ for t.Kind() == reflect.Ptr {
+ t = t.Elem()
+ }
+ return t == subscriptionType
+}
+
+// isPubSub tests whether the given method has as as first argument a context.Context and
+// returns the pair (Subscription, error).
+func isPubSub(methodType reflect.Type) bool {
+ // numIn(0) is the receiver type
+ if methodType.NumIn() < 2 || methodType.NumOut() != 2 {
+ return false
+ }
+ return isContextType(methodType.In(1)) &&
+ isSubscriptionType(methodType.Out(0)) &&
+ isErrorType(methodType.Out(1))
+}
+
+// formatName converts to first character of name to lowercase.
+func formatName(name string) string {
+ ret := []rune(name)
+ if len(ret) > 0 {
+ ret[0] = unicode.ToLower(ret[0])
+ }
+ return string(ret)
+}
diff --git a/rpc/subscription.go b/rpc/subscription.go
new file mode 100644
index 0000000..c1e869b
--- /dev/null
+++ b/rpc/subscription.go
@@ -0,0 +1,327 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "bufio"
+ "container/list"
+ "context"
+ crand "crypto/rand"
+ "encoding/binary"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "math/rand"
+ "reflect"
+ "strings"
+ "sync"
+ "time"
+)
+
+var (
+ // ErrNotificationsUnsupported is returned when the connection doesn't support notifications
+ ErrNotificationsUnsupported = errors.New("notifications not supported")
+ // ErrNotificationNotFound is returned when the notification for the given id is not found
+ ErrSubscriptionNotFound = errors.New("subscription not found")
+)
+
+var globalGen = randomIDGenerator()
+
+// ID defines a pseudo random number that is used to identify RPC subscriptions.
+type ID string
+
+// NewID returns a new, random ID.
+func NewID() ID {
+ return globalGen()
+}
+
+// randomIDGenerator returns a function generates a random IDs.
+func randomIDGenerator() func() ID {
+ seed, err := binary.ReadVarint(bufio.NewReader(crand.Reader))
+ if err != nil {
+ seed = int64(time.Now().Nanosecond())
+ }
+ var (
+ mu sync.Mutex
+ rng = rand.New(rand.NewSource(seed))
+ )
+ return func() ID {
+ mu.Lock()
+ defer mu.Unlock()
+ id := make([]byte, 16)
+ rng.Read(id)
+ return encodeID(id)
+ }
+}
+
+func encodeID(b []byte) ID {
+ id := hex.EncodeToString(b)
+ id = strings.TrimLeft(id, "0")
+ if id == "" {
+ id = "0" // ID's are RPC quantities, no leading zero's and 0 is 0x0.
+ }
+ return ID("0x" + id)
+}
+
+type notifierKey struct{}
+
+// NotifierFromContext returns the Notifier value stored in ctx, if any.
+func NotifierFromContext(ctx context.Context) (*Notifier, bool) {
+ n, ok := ctx.Value(notifierKey{}).(*Notifier)
+ return n, ok
+}
+
+// Notifier is tied to a RPC connection that supports subscriptions.
+// Server callbacks use the notifier to send notifications.
+type Notifier struct {
+ h *handler
+ namespace string
+
+ mu sync.Mutex
+ sub *Subscription
+ buffer []json.RawMessage
+ callReturned bool
+ activated bool
+}
+
+// CreateSubscription returns a new subscription that is coupled to the
+// RPC connection. By default subscriptions are inactive and notifications
+// are dropped until the subscription is marked as active. This is done
+// by the RPC server after the subscription ID is send to the client.
+func (n *Notifier) CreateSubscription() *Subscription {
+ n.mu.Lock()
+ defer n.mu.Unlock()
+
+ if n.sub != nil {
+ panic("can't create multiple subscriptions with Notifier")
+ } else if n.callReturned {
+ panic("can't create subscription after subscribe call has returned")
+ }
+ n.sub = &Subscription{ID: n.h.idgen(), namespace: n.namespace, err: make(chan error, 1)}
+ return n.sub
+}
+
+// Notify sends a notification to the client with the given data as payload.
+// If an error occurs the RPC connection is closed and the error is returned.
+func (n *Notifier) Notify(id ID, data interface{}) error {
+ enc, err := json.Marshal(data)
+ if err != nil {
+ return err
+ }
+
+ n.mu.Lock()
+ defer n.mu.Unlock()
+
+ if n.sub == nil {
+ panic("can't Notify before subscription is created")
+ } else if n.sub.ID != id {
+ panic("Notify with wrong ID")
+ }
+ if n.activated {
+ return n.send(n.sub, enc)
+ }
+ n.buffer = append(n.buffer, enc)
+ return nil
+}
+
+// Closed returns a channel that is closed when the RPC connection is closed.
+// Deprecated: use subscription error channel
+func (n *Notifier) Closed() <-chan interface{} {
+ return n.h.conn.Closed()
+}
+
+// takeSubscription returns the subscription (if one has been created). No subscription can
+// be created after this call.
+func (n *Notifier) takeSubscription() *Subscription {
+ n.mu.Lock()
+ defer n.mu.Unlock()
+ n.callReturned = true
+ return n.sub
+}
+
+// acticate is called after the subscription ID was sent to client. Notifications are
+// buffered before activation. This prevents notifications being sent to the client before
+// the subscription ID is sent to the client.
+func (n *Notifier) activate() error {
+ n.mu.Lock()
+ defer n.mu.Unlock()
+
+ for _, data := range n.buffer {
+ if err := n.send(n.sub, data); err != nil {
+ return err
+ }
+ }
+ n.activated = true
+ return nil
+}
+
+func (n *Notifier) send(sub *Subscription, data json.RawMessage) error {
+ params, _ := json.Marshal(&subscriptionResult{ID: string(sub.ID), Result: data})
+ ctx := context.Background()
+ return n.h.conn.Write(ctx, &jsonrpcMessage{
+ Version: vsn,
+ Method: n.namespace + notificationMethodSuffix,
+ Params: params,
+ })
+}
+
+// A Subscription is created by a notifier and tight to that notifier. The client can use
+// this subscription to wait for an unsubscribe request for the client, see Err().
+type Subscription struct {
+ ID ID
+ namespace string
+ err chan error // closed on unsubscribe
+}
+
+// Err returns a channel that is closed when the client send an unsubscribe request.
+func (s *Subscription) Err() <-chan error {
+ return s.err
+}
+
+// MarshalJSON marshals a subscription as its ID.
+func (s *Subscription) MarshalJSON() ([]byte, error) {
+ return json.Marshal(s.ID)
+}
+
+// ClientSubscription is a subscription established through the Client's Subscribe or
+// EthSubscribe methods.
+type ClientSubscription struct {
+ client *Client
+ etype reflect.Type
+ channel reflect.Value
+ namespace string
+ subid string
+ in chan json.RawMessage
+
+ quitOnce sync.Once // ensures quit is closed once
+ quit chan struct{} // quit is closed when the subscription exits
+ errOnce sync.Once // ensures err is closed once
+ err chan error
+}
+
+func newClientSubscription(c *Client, namespace string, channel reflect.Value) *ClientSubscription {
+ sub := &ClientSubscription{
+ client: c,
+ namespace: namespace,
+ etype: channel.Type().Elem(),
+ channel: channel,
+ quit: make(chan struct{}),
+ err: make(chan error, 1),
+ in: make(chan json.RawMessage),
+ }
+ return sub
+}
+
+// Err returns the subscription error channel. The intended use of Err is to schedule
+// resubscription when the client connection is closed unexpectedly.
+//
+// The error channel receives a value when the subscription has ended due
+// to an error. The received error is nil if Close has been called
+// on the underlying client and no other error has occurred.
+//
+// The error channel is closed when Unsubscribe is called on the subscription.
+func (sub *ClientSubscription) Err() <-chan error {
+ return sub.err
+}
+
+// Unsubscribe unsubscribes the notification and closes the error channel.
+// It can safely be called more than once.
+func (sub *ClientSubscription) Unsubscribe() {
+ sub.quitWithError(nil, true)
+ sub.errOnce.Do(func() { close(sub.err) })
+}
+
+func (sub *ClientSubscription) quitWithError(err error, unsubscribeServer bool) {
+ sub.quitOnce.Do(func() {
+ // The dispatch loop won't be able to execute the unsubscribe call
+ // if it is blocked on deliver. Close sub.quit first because it
+ // unblocks deliver.
+ close(sub.quit)
+ if unsubscribeServer {
+ sub.requestUnsubscribe()
+ }
+ if err != nil {
+ if err == ErrClientQuit {
+ err = nil // Adhere to subscription semantics.
+ }
+ sub.err <- err
+ }
+ })
+}
+
+func (sub *ClientSubscription) deliver(result json.RawMessage) (ok bool) {
+ select {
+ case sub.in <- result:
+ return true
+ case <-sub.quit:
+ return false
+ }
+}
+
+func (sub *ClientSubscription) start() {
+ sub.quitWithError(sub.forward())
+}
+
+func (sub *ClientSubscription) forward() (err error, unsubscribeServer bool) {
+ cases := []reflect.SelectCase{
+ {Dir: reflect.SelectRecv, Chan: reflect.ValueOf(sub.quit)},
+ {Dir: reflect.SelectRecv, Chan: reflect.ValueOf(sub.in)},
+ {Dir: reflect.SelectSend, Chan: sub.channel},
+ }
+ buffer := list.New()
+ defer buffer.Init()
+ for {
+ var chosen int
+ var recv reflect.Value
+ if buffer.Len() == 0 {
+ // Idle, omit send case.
+ chosen, recv, _ = reflect.Select(cases[:2])
+ } else {
+ // Non-empty buffer, send the first queued item.
+ cases[2].Send = reflect.ValueOf(buffer.Front().Value)
+ chosen, recv, _ = reflect.Select(cases)
+ }
+
+ switch chosen {
+ case 0: // <-sub.quit
+ return nil, false
+ case 1: // <-sub.in
+ val, err := sub.unmarshal(recv.Interface().(json.RawMessage))
+ if err != nil {
+ return err, true
+ }
+ if buffer.Len() == maxClientSubscriptionBuffer {
+ return ErrSubscriptionQueueOverflow, true
+ }
+ buffer.PushBack(val)
+ case 2: // sub.channel<-
+ cases[2].Send = reflect.Value{} // Don't hold onto the value.
+ buffer.Remove(buffer.Front())
+ }
+ }
+}
+
+func (sub *ClientSubscription) unmarshal(result json.RawMessage) (interface{}, error) {
+ val := reflect.New(sub.etype)
+ err := json.Unmarshal(result, val.Interface())
+ return val.Elem().Interface(), err
+}
+
+func (sub *ClientSubscription) requestUnsubscribe() error {
+ var result interface{}
+ return sub.client.Call(&result, sub.namespace+unsubscribeMethodSuffix, sub.subid)
+}
diff --git a/rpc/subscription_test.go b/rpc/subscription_test.go
new file mode 100644
index 0000000..eba1924
--- /dev/null
+++ b/rpc/subscription_test.go
@@ -0,0 +1,206 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "encoding/json"
+ "fmt"
+ "net"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestNewID(t *testing.T) {
+ hexchars := "0123456789ABCDEFabcdef"
+ for i := 0; i < 100; i++ {
+ id := string(NewID())
+ if !strings.HasPrefix(id, "0x") {
+ t.Fatalf("invalid ID prefix, want '0x...', got %s", id)
+ }
+
+ id = id[2:]
+ if len(id) == 0 || len(id) > 32 {
+ t.Fatalf("invalid ID length, want len(id) > 0 && len(id) <= 32), got %d", len(id))
+ }
+
+ for i := 0; i < len(id); i++ {
+ if strings.IndexByte(hexchars, id[i]) == -1 {
+ t.Fatalf("unexpected byte, want any valid hex char, got %c", id[i])
+ }
+ }
+ }
+}
+
+func TestSubscriptions(t *testing.T) {
+ var (
+ namespaces = []string{"eth", "shh", "bzz"}
+ service = &notificationTestService{}
+ subCount = len(namespaces)
+ notificationCount = 3
+
+ server = NewServer()
+ clientConn, serverConn = net.Pipe()
+ out = json.NewEncoder(clientConn)
+ in = json.NewDecoder(clientConn)
+ successes = make(chan subConfirmation)
+ notifications = make(chan subscriptionResult)
+ errors = make(chan error, subCount*notificationCount+1)
+ )
+
+ // setup and start server
+ for _, namespace := range namespaces {
+ if err := server.RegisterName(namespace, service); err != nil {
+ t.Fatalf("unable to register test service %v", err)
+ }
+ }
+ go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation|OptionSubscriptions)
+ defer server.Stop()
+
+ // wait for message and write them to the given channels
+ go waitForMessages(in, successes, notifications, errors)
+
+ // create subscriptions one by one
+ for i, namespace := range namespaces {
+ request := map[string]interface{}{
+ "id": i,
+ "method": fmt.Sprintf("%s_subscribe", namespace),
+ "version": "2.0",
+ "params": []interface{}{"someSubscription", notificationCount, i},
+ }
+ if err := out.Encode(&request); err != nil {
+ t.Fatalf("Could not create subscription: %v", err)
+ }
+ }
+
+ timeout := time.After(30 * time.Second)
+ subids := make(map[string]string, subCount)
+ count := make(map[string]int, subCount)
+ allReceived := func() bool {
+ done := len(count) == subCount
+ for _, c := range count {
+ if c < notificationCount {
+ done = false
+ }
+ }
+ return done
+ }
+ for !allReceived() {
+ select {
+ case confirmation := <-successes: // subscription created
+ subids[namespaces[confirmation.reqid]] = string(confirmation.subid)
+ case notification := <-notifications:
+ count[notification.ID]++
+ case err := <-errors:
+ t.Fatal(err)
+ case <-timeout:
+ for _, namespace := range namespaces {
+ subid, found := subids[namespace]
+ if !found {
+ t.Errorf("subscription for %q not created", namespace)
+ continue
+ }
+ if count, found := count[subid]; !found || count < notificationCount {
+ t.Errorf("didn't receive all notifications (%d<%d) in time for namespace %q", count, notificationCount, namespace)
+ }
+ }
+ t.Fatal("timed out")
+ }
+ }
+}
+
+// This test checks that unsubscribing works.
+func TestServerUnsubscribe(t *testing.T) {
+ // Start the server.
+ server := newTestServer()
+ service := &notificationTestService{unsubscribed: make(chan string)}
+ server.RegisterName("nftest2", service)
+ p1, p2 := net.Pipe()
+ go server.ServeCodec(NewJSONCodec(p1), OptionMethodInvocation|OptionSubscriptions)
+
+ p2.SetDeadline(time.Now().Add(10 * time.Second))
+
+ // Subscribe.
+ p2.Write([]byte(`{"jsonrpc":"2.0","id":1,"method":"nftest2_subscribe","params":["someSubscription",0,10]}`))
+
+ // Handle received messages.
+ resps := make(chan subConfirmation)
+ notifications := make(chan subscriptionResult)
+ errors := make(chan error)
+ go waitForMessages(json.NewDecoder(p2), resps, notifications, errors)
+
+ // Receive the subscription ID.
+ var sub subConfirmation
+ select {
+ case sub = <-resps:
+ case err := <-errors:
+ t.Fatal(err)
+ }
+
+ // Unsubscribe and check that it is handled on the server side.
+ p2.Write([]byte(`{"jsonrpc":"2.0","method":"nftest2_unsubscribe","params":["` + sub.subid + `"]}`))
+ for {
+ select {
+ case id := <-service.unsubscribed:
+ if id != string(sub.subid) {
+ t.Errorf("wrong subscription ID unsubscribed")
+ }
+ return
+ case err := <-errors:
+ t.Fatal(err)
+ case <-notifications:
+ // drop notifications
+ }
+ }
+}
+
+type subConfirmation struct {
+ reqid int
+ subid ID
+}
+
+func waitForMessages(in *json.Decoder, successes chan subConfirmation, notifications chan subscriptionResult, errors chan error) {
+ for {
+ var msg jsonrpcMessage
+ if err := in.Decode(&msg); err != nil {
+ errors <- fmt.Errorf("decode error: %v", err)
+ return
+ }
+ switch {
+ case msg.isNotification():
+ var res subscriptionResult
+ if err := json.Unmarshal(msg.Params, &res); err != nil {
+ errors <- fmt.Errorf("invalid subscription result: %v", err)
+ } else {
+ notifications <- res
+ }
+ case msg.isResponse():
+ var c subConfirmation
+ if msg.Error != nil {
+ errors <- msg.Error
+ } else if err := json.Unmarshal(msg.Result, &c.subid); err != nil {
+ errors <- fmt.Errorf("invalid response: %v", err)
+ } else {
+ json.Unmarshal(msg.ID, &c.reqid)
+ successes <- c
+ }
+ default:
+ errors <- fmt.Errorf("unrecognized message: %v", msg)
+ return
+ }
+ }
+}
diff --git a/rpc/testservice_test.go b/rpc/testservice_test.go
new file mode 100644
index 0000000..98871b5
--- /dev/null
+++ b/rpc/testservice_test.go
@@ -0,0 +1,180 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "encoding/binary"
+ "errors"
+ "sync"
+ "time"
+)
+
+func newTestServer() *Server {
+ server := NewServer()
+ server.idgen = sequentialIDGenerator()
+ if err := server.RegisterName("test", new(testService)); err != nil {
+ panic(err)
+ }
+ if err := server.RegisterName("nftest", new(notificationTestService)); err != nil {
+ panic(err)
+ }
+ return server
+}
+
+func sequentialIDGenerator() func() ID {
+ var (
+ mu sync.Mutex
+ counter uint64
+ )
+ return func() ID {
+ mu.Lock()
+ defer mu.Unlock()
+ counter++
+ id := make([]byte, 8)
+ binary.BigEndian.PutUint64(id, counter)
+ return encodeID(id)
+ }
+}
+
+type testService struct{}
+
+type Args struct {
+ S string
+}
+
+type Result struct {
+ String string
+ Int int
+ Args *Args
+}
+
+func (s *testService) NoArgsRets() {}
+
+func (s *testService) Echo(str string, i int, args *Args) Result {
+ return Result{str, i, args}
+}
+
+func (s *testService) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result {
+ return Result{str, i, args}
+}
+
+func (s *testService) Sleep(ctx context.Context, duration time.Duration) {
+ time.Sleep(duration)
+}
+
+func (s *testService) Rets() (string, error) {
+ return "", nil
+}
+
+func (s *testService) InvalidRets1() (error, string) {
+ return nil, ""
+}
+
+func (s *testService) InvalidRets2() (string, string) {
+ return "", ""
+}
+
+func (s *testService) InvalidRets3() (string, string, error) {
+ return "", "", nil
+}
+
+func (s *testService) CallMeBack(ctx context.Context, method string, args []interface{}) (interface{}, error) {
+ c, ok := ClientFromContext(ctx)
+ if !ok {
+ return nil, errors.New("no client")
+ }
+ var result interface{}
+ err := c.Call(&result, method, args...)
+ return result, err
+}
+
+func (s *testService) CallMeBackLater(ctx context.Context, method string, args []interface{}) error {
+ c, ok := ClientFromContext(ctx)
+ if !ok {
+ return errors.New("no client")
+ }
+ go func() {
+ <-ctx.Done()
+ var result interface{}
+ c.Call(&result, method, args...)
+ }()
+ return nil
+}
+
+func (s *testService) Subscription(ctx context.Context) (*Subscription, error) {
+ return nil, nil
+}
+
+type notificationTestService struct {
+ unsubscribed chan string
+ gotHangSubscriptionReq chan struct{}
+ unblockHangSubscription chan struct{}
+}
+
+func (s *notificationTestService) Echo(i int) int {
+ return i
+}
+
+func (s *notificationTestService) Unsubscribe(subid string) {
+ if s.unsubscribed != nil {
+ s.unsubscribed <- subid
+ }
+}
+
+func (s *notificationTestService) SomeSubscription(ctx context.Context, n, val int) (*Subscription, error) {
+ notifier, supported := NotifierFromContext(ctx)
+ if !supported {
+ return nil, ErrNotificationsUnsupported
+ }
+
+ // By explicitly creating an subscription we make sure that the subscription id is send
+ // back to the client before the first subscription.Notify is called. Otherwise the
+ // events might be send before the response for the *_subscribe method.
+ subscription := notifier.CreateSubscription()
+ go func() {
+ for i := 0; i < n; i++ {
+ if err := notifier.Notify(subscription.ID, val+i); err != nil {
+ return
+ }
+ }
+ select {
+ case <-notifier.Closed():
+ case <-subscription.Err():
+ }
+ if s.unsubscribed != nil {
+ s.unsubscribed <- string(subscription.ID)
+ }
+ }()
+ return subscription, nil
+}
+
+// HangSubscription blocks on s.unblockHangSubscription before sending anything.
+func (s *notificationTestService) HangSubscription(ctx context.Context, val int) (*Subscription, error) {
+ notifier, supported := NotifierFromContext(ctx)
+ if !supported {
+ return nil, ErrNotificationsUnsupported
+ }
+ s.gotHangSubscriptionReq <- struct{}{}
+ <-s.unblockHangSubscription
+ subscription := notifier.CreateSubscription()
+
+ go func() {
+ notifier.Notify(subscription.ID, val)
+ }()
+ return subscription, nil
+}
diff --git a/rpc/types.go b/rpc/types.go
index 7312397..202dd79 100644
--- a/rpc/types.go
+++ b/rpc/types.go
@@ -17,6 +17,7 @@
package rpc
import (
+ "context"
"fmt"
"math"
"strings"
@@ -24,6 +25,39 @@ import (
"github.com/ava-labs/go-ethereum/common/hexutil"
)
+// API describes the set of methods offered over the RPC interface
+type API struct {
+ Namespace string // namespace under which the rpc methods of Service are exposed
+ Version string // api version for DApp's
+ Service interface{} // receiver instance which holds the methods
+ Public bool // indication if the methods must be considered safe for public use
+}
+
+// Error wraps RPC errors, which contain an error code in addition to the message.
+type Error interface {
+ Error() string // returns the message
+ ErrorCode() int // returns the code
+}
+
+// ServerCodec implements reading, parsing and writing RPC messages for the server side of
+// a RPC session. Implementations must be go-routine safe since the codec can be called in
+// multiple go-routines concurrently.
+type ServerCodec interface {
+ Read() (msgs []*jsonrpcMessage, isBatch bool, err error)
+ Close()
+ jsonWriter
+}
+
+// jsonWriter can write JSON messages to its underlying connection.
+// Implementations must be safe for concurrent use.
+type jsonWriter interface {
+ Write(context.Context, interface{}) error
+ // Closed returns a channel which is closed when the connection is closed.
+ Closed() <-chan interface{}
+ // RemoteAddr returns the peer address of the connection.
+ RemoteAddr() string
+}
+
type BlockNumber int64
const (
diff --git a/rpc/types_test.go b/rpc/types_test.go
new file mode 100644
index 0000000..0465849
--- /dev/null
+++ b/rpc/types_test.go
@@ -0,0 +1,66 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/ava-labs/go-ethereum/common/math"
+)
+
+func TestBlockNumberJSONUnmarshal(t *testing.T) {
+ tests := []struct {
+ input string
+ mustFail bool
+ expected BlockNumber
+ }{
+ 0: {`"0x"`, true, BlockNumber(0)},
+ 1: {`"0x0"`, false, BlockNumber(0)},
+ 2: {`"0X1"`, false, BlockNumber(1)},
+ 3: {`"0x00"`, true, BlockNumber(0)},
+ 4: {`"0x01"`, true, BlockNumber(0)},
+ 5: {`"0x1"`, false, BlockNumber(1)},
+ 6: {`"0x12"`, false, BlockNumber(18)},
+ 7: {`"0x7fffffffffffffff"`, false, BlockNumber(math.MaxInt64)},
+ 8: {`"0x8000000000000000"`, true, BlockNumber(0)},
+ 9: {"0", true, BlockNumber(0)},
+ 10: {`"ff"`, true, BlockNumber(0)},
+ 11: {`"pending"`, false, PendingBlockNumber},
+ 12: {`"latest"`, false, LatestBlockNumber},
+ 13: {`"earliest"`, false, EarliestBlockNumber},
+ 14: {`someString`, true, BlockNumber(0)},
+ 15: {`""`, true, BlockNumber(0)},
+ 16: {``, true, BlockNumber(0)},
+ }
+
+ for i, test := range tests {
+ var num BlockNumber
+ err := json.Unmarshal([]byte(test.input), &num)
+ if test.mustFail && err == nil {
+ t.Errorf("Test %d should fail", i)
+ continue
+ }
+ if !test.mustFail && err != nil {
+ t.Errorf("Test %d should pass but got err: %v", i, err)
+ continue
+ }
+ if num != test.expected {
+ t.Errorf("Test %d got unexpected value, want %d, got %d", i, test.expected, num)
+ }
+ }
+}
diff --git a/rpc/websocket.go b/rpc/websocket.go
new file mode 100644
index 0000000..d87e8a5
--- /dev/null
+++ b/rpc/websocket.go
@@ -0,0 +1,175 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "encoding/base64"
+ "fmt"
+ "net/http"
+ "net/url"
+ "os"
+ "strings"
+ "sync"
+
+ mapset "github.com/deckarep/golang-set"
+ "github.com/ava-labs/go-ethereum/log"
+ "github.com/gorilla/websocket"
+)
+
+const (
+ wsReadBuffer = 1024
+ wsWriteBuffer = 1024
+)
+
+var wsBufferPool = new(sync.Pool)
+
+// NewWSServer creates a new websocket RPC server around an API provider.
+//
+// Deprecated: use Server.WebsocketHandler
+func NewWSServer(allowedOrigins []string, srv *Server) *http.Server {
+ return &http.Server{Handler: srv.WebsocketHandler(allowedOrigins)}
+}
+
+// WebsocketHandler returns a handler that serves JSON-RPC to WebSocket connections.
+//
+// allowedOrigins should be a comma-separated list of allowed origin URLs.
+// To allow connections with any origin, pass "*".
+func (s *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
+ var upgrader = websocket.Upgrader{
+ ReadBufferSize: wsReadBuffer,
+ WriteBufferSize: wsWriteBuffer,
+ WriteBufferPool: wsBufferPool,
+ CheckOrigin: wsHandshakeValidator(allowedOrigins),
+ }
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ conn, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ log.Debug("WebSocket upgrade failed", "err", err)
+ return
+ }
+ codec := newWebsocketCodec(conn)
+ s.ServeCodec(codec, OptionMethodInvocation|OptionSubscriptions)
+ })
+}
+
+// wsHandshakeValidator returns a handler that verifies the origin during the
+// websocket upgrade process. When a '*' is specified as an allowed origins all
+// connections are accepted.
+func wsHandshakeValidator(allowedOrigins []string) func(*http.Request) bool {
+ origins := mapset.NewSet()
+ allowAllOrigins := false
+
+ for _, origin := range allowedOrigins {
+ if origin == "*" {
+ allowAllOrigins = true
+ }
+ if origin != "" {
+ origins.Add(strings.ToLower(origin))
+ }
+ }
+ // allow localhost if no allowedOrigins are specified.
+ if len(origins.ToSlice()) == 0 {
+ origins.Add("http://localhost")
+ if hostname, err := os.Hostname(); err == nil {
+ origins.Add("http://" + strings.ToLower(hostname))
+ }
+ }
+ log.Debug(fmt.Sprintf("Allowed origin(s) for WS RPC interface %v", origins.ToSlice()))
+
+ f := func(req *http.Request) bool {
+ // Skip origin verification if no Origin header is present. The origin check
+ // is supposed to protect against browser based attacks. Browsers always set
+ // Origin. Non-browser software can put anything in origin and checking it doesn't
+ // provide additional security.
+ if _, ok := req.Header["Origin"]; !ok {
+ return true
+ }
+ // Verify origin against whitelist.
+ origin := strings.ToLower(req.Header.Get("Origin"))
+ if allowAllOrigins || origins.Contains(origin) {
+ return true
+ }
+ log.Warn("Rejected WebSocket connection", "origin", origin)
+ return false
+ }
+
+ return f
+}
+
+type wsHandshakeError struct {
+ err error
+ status string
+}
+
+func (e wsHandshakeError) Error() string {
+ s := e.err.Error()
+ if e.status != "" {
+ s += " (HTTP status " + e.status + ")"
+ }
+ return s
+}
+
+// DialWebsocket creates a new RPC client that communicates with a JSON-RPC server
+// that is listening on the given endpoint.
+//
+// The context is used for the initial connection establishment. It does not
+// affect subsequent interactions with the client.
+func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error) {
+ endpoint, header, err := wsClientHeaders(endpoint, origin)
+ if err != nil {
+ return nil, err
+ }
+ dialer := websocket.Dialer{
+ ReadBufferSize: wsReadBuffer,
+ WriteBufferSize: wsWriteBuffer,
+ WriteBufferPool: wsBufferPool,
+ }
+ return newClient(ctx, func(ctx context.Context) (ServerCodec, error) {
+ conn, resp, err := dialer.DialContext(ctx, endpoint, header)
+ if err != nil {
+ hErr := wsHandshakeError{err: err}
+ if resp != nil {
+ hErr.status = resp.Status
+ }
+ return nil, hErr
+ }
+ return newWebsocketCodec(conn), nil
+ })
+}
+
+func wsClientHeaders(endpoint, origin string) (string, http.Header, error) {
+ endpointURL, err := url.Parse(endpoint)
+ if err != nil {
+ return endpoint, nil, err
+ }
+ header := make(http.Header)
+ if origin != "" {
+ header.Add("origin", origin)
+ }
+ if endpointURL.User != nil {
+ b64auth := base64.StdEncoding.EncodeToString([]byte(endpointURL.User.String()))
+ header.Add("authorization", "Basic "+b64auth)
+ endpointURL.User = nil
+ }
+ return endpointURL.String(), header, nil
+}
+
+func newWebsocketCodec(conn *websocket.Conn) ServerCodec {
+ conn.SetReadLimit(maxRequestContentLength)
+ return newCodec(conn, conn.WriteJSON, conn.ReadJSON)
+}
diff --git a/rpc/websocket_test.go b/rpc/websocket_test.go
new file mode 100644
index 0000000..9dc1084
--- /dev/null
+++ b/rpc/websocket_test.go
@@ -0,0 +1,259 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "context"
+ "net"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/gorilla/websocket"
+)
+
+func TestWebsocketClientHeaders(t *testing.T) {
+ t.Parallel()
+
+ endpoint, header, err := wsClientHeaders("wss://testuser:[email protected]:1234", "https://example.com")
+ if err != nil {
+ t.Fatalf("wsGetConfig failed: %s", err)
+ }
+ if endpoint != "wss://example.com:1234" {
+ t.Fatal("User should have been stripped from the URL")
+ }
+ if header.Get("authorization") != "Basic dGVzdHVzZXI6dGVzdC1QQVNTXzAx" {
+ t.Fatal("Basic auth header is incorrect")
+ }
+ if header.Get("origin") != "https://example.com" {
+ t.Fatal("Origin not set")
+ }
+}
+
+// This test checks that the server rejects connections from disallowed origins.
+func TestWebsocketOriginCheck(t *testing.T) {
+ t.Parallel()
+
+ var (
+ srv = newTestServer()
+ httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"http://example.com"}))
+ wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
+ )
+ defer srv.Stop()
+ defer httpsrv.Close()
+
+ client, err := DialWebsocket(context.Background(), wsURL, "http://ekzample.com")
+ if err == nil {
+ client.Close()
+ t.Fatal("no error for wrong origin")
+ }
+ wantErr := wsHandshakeError{websocket.ErrBadHandshake, "403 Forbidden"}
+ if !reflect.DeepEqual(err, wantErr) {
+ t.Fatalf("wrong error for wrong origin: %q", err)
+ }
+
+ // Connections without origin header should work.
+ client, err = DialWebsocket(context.Background(), wsURL, "")
+ if err != nil {
+ t.Fatal("error for empty origin")
+ }
+ client.Close()
+}
+
+// This test checks whether calls exceeding the request size limit are rejected.
+func TestWebsocketLargeCall(t *testing.T) {
+ t.Parallel()
+
+ var (
+ srv = newTestServer()
+ httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"*"}))
+ wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
+ )
+ defer srv.Stop()
+ defer httpsrv.Close()
+
+ client, err := DialWebsocket(context.Background(), wsURL, "")
+ if err != nil {
+ t.Fatalf("can't dial: %v", err)
+ }
+ defer client.Close()
+
+ // This call sends slightly less than the limit and should work.
+ var result Result
+ arg := strings.Repeat("x", maxRequestContentLength-200)
+ if err := client.Call(&result, "test_echo", arg, 1); err != nil {
+ t.Fatalf("valid call didn't work: %v", err)
+ }
+ if result.String != arg {
+ t.Fatal("wrong string echoed")
+ }
+
+ // This call sends twice the allowed size and shouldn't work.
+ arg = strings.Repeat("x", maxRequestContentLength*2)
+ err = client.Call(&result, "test_echo", arg)
+ if err == nil {
+ t.Fatal("no error for too large call")
+ }
+}
+
+// This test checks that client handles WebSocket ping frames correctly.
+func TestClientWebsocketPing(t *testing.T) {
+ t.Parallel()
+
+ var (
+ sendPing = make(chan struct{})
+ server = wsPingTestServer(t, sendPing)
+ ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
+ )
+ defer cancel()
+ defer server.Shutdown(ctx)
+
+ client, err := DialContext(ctx, "ws://"+server.Addr)
+ if err != nil {
+ t.Fatalf("client dial error: %v", err)
+ }
+ resultChan := make(chan int)
+ sub, err := client.EthSubscribe(ctx, resultChan, "foo")
+ if err != nil {
+ t.Fatalf("client subscribe error: %v", err)
+ }
+
+ // Wait for the context's deadline to be reached before proceeding.
+ // This is important for reproducing https://github.com/ethereum/go-ethereum/issues/19798
+ <-ctx.Done()
+ close(sendPing)
+
+ // Wait for the subscription result.
+ timeout := time.NewTimer(5 * time.Second)
+ for {
+ select {
+ case err := <-sub.Err():
+ t.Error("client subscription error:", err)
+ case result := <-resultChan:
+ t.Log("client got result:", result)
+ return
+ case <-timeout.C:
+ t.Error("didn't get any result within the test timeout")
+ return
+ }
+ }
+}
+
+// wsPingTestServer runs a WebSocket server which accepts a single subscription request.
+// When a value arrives on sendPing, the server sends a ping frame, waits for a matching
+// pong and finally delivers a single subscription result.
+func wsPingTestServer(t *testing.T, sendPing <-chan struct{}) *http.Server {
+ var srv http.Server
+ shutdown := make(chan struct{})
+ srv.RegisterOnShutdown(func() {
+ close(shutdown)
+ })
+ srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // Upgrade to WebSocket.
+ upgrader := websocket.Upgrader{
+ CheckOrigin: func(r *http.Request) bool { return true },
+ }
+ conn, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ t.Errorf("server WS upgrade error: %v", err)
+ return
+ }
+ defer conn.Close()
+
+ // Handle the connection.
+ wsPingTestHandler(t, conn, shutdown, sendPing)
+ })
+
+ // Start the server.
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal("can't listen:", err)
+ }
+ srv.Addr = listener.Addr().String()
+ go srv.Serve(listener)
+ return &srv
+}
+
+func wsPingTestHandler(t *testing.T, conn *websocket.Conn, shutdown, sendPing <-chan struct{}) {
+ // Canned responses for the eth_subscribe call in TestClientWebsocketPing.
+ const (
+ subResp = `{"jsonrpc":"2.0","id":1,"result":"0x00"}`
+ subNotify = `{"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x00","result":1}}`
+ )
+
+ // Handle subscribe request.
+ if _, _, err := conn.ReadMessage(); err != nil {
+ t.Errorf("server read error: %v", err)
+ return
+ }
+ if err := conn.WriteMessage(websocket.TextMessage, []byte(subResp)); err != nil {
+ t.Errorf("server write error: %v", err)
+ return
+ }
+
+ // Read from the connection to process control messages.
+ var pongCh = make(chan string)
+ conn.SetPongHandler(func(d string) error {
+ t.Logf("server got pong: %q", d)
+ pongCh <- d
+ return nil
+ })
+ go func() {
+ for {
+ typ, msg, err := conn.ReadMessage()
+ if err != nil {
+ return
+ }
+ t.Logf("server got message (%d): %q", typ, msg)
+ }
+ }()
+
+ // Write messages.
+ var (
+ sendResponse <-chan time.Time
+ wantPong string
+ )
+ for {
+ select {
+ case _, open := <-sendPing:
+ if !open {
+ sendPing = nil
+ }
+ t.Logf("server sending ping")
+ conn.WriteMessage(websocket.PingMessage, []byte("ping"))
+ wantPong = "ping"
+ case data := <-pongCh:
+ if wantPong == "" {
+ t.Errorf("unexpected pong")
+ } else if data != wantPong {
+ t.Errorf("got pong with wrong data %q", data)
+ }
+ wantPong = ""
+ sendResponse = time.NewTimer(200 * time.Millisecond).C
+ case <-sendResponse:
+ t.Logf("server sending response")
+ conn.WriteMessage(websocket.TextMessage, []byte(subNotify))
+ sendResponse = nil
+ case <-shutdown:
+ conn.Close()
+ return
+ }
+ }
+}