diff options
-rw-r--r-- | accounts/abi/bind/base.go | 9 | ||||
-rw-r--r-- | accounts/abi/bind/bind_test.go | 45 | ||||
-rw-r--r-- | accounts/abi/bind/template.go | 53 | ||||
-rw-r--r-- | cmd/geth/main.go | 7 | ||||
-rw-r--r-- | cmd/geth/usage.go | 2 | ||||
-rw-r--r-- | cmd/utils/flags.go | 27 | ||||
-rw-r--r-- | common/types.go | 13 | ||||
-rw-r--r-- | common/types_test.go | 22 | ||||
-rw-r--r-- | eth/api.go | 8 | ||||
-rw-r--r-- | eth/backend.go | 4 |
10 files changed, 162 insertions, 28 deletions
diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go index 053b4ccc0..06621c5ad 100644 --- a/accounts/abi/bind/base.go +++ b/accounts/abi/bind/base.go @@ -108,8 +108,7 @@ func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string, return c.abi.Unpack(result, method, output) } -// Transact invokes the (paid) contract method with params as input values and -// value as the fund transfer to the contract. +// 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...) @@ -119,6 +118,12 @@ func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...in 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) { diff --git a/accounts/abi/bind/bind_test.go b/accounts/abi/bind/bind_test.go index 37b8ef5a7..12c849669 100644 --- a/accounts/abi/bind/bind_test.go +++ b/accounts/abi/bind/bind_test.go @@ -201,9 +201,9 @@ var bindTests = []struct { `Tupler`, ` contract Tupler { - function tuple() returns (string a, int b, bytes32 c) { - return ("Hi", 1, sha3("")); - } + function tuple() returns (string a, int b, bytes32 c) { + return ("Hi", 1, sha3("")); + } } `, `606060405260dc8060106000396000f3606060405260e060020a60003504633175aae28114601a575b005b600060605260c0604052600260809081527f486900000000000000000000000000000000000000000000000000000000000060a05260017fc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a47060e0829052610100819052606060c0908152600261012081905281906101409060a09080838184600060046012f1505081517fffff000000000000000000000000000000000000000000000000000000000000169091525050604051610160819003945092505050f3`, @@ -228,6 +228,45 @@ var bindTests = []struct { } `, }, + // Tests that anonymous default methods can be correctly invoked + { + `Defaulter`, + ` + contract Defaulter { + address public caller; + + function() { + caller = msg.sender; + } + } + `, + `6060604052606a8060106000396000f360606040523615601d5760e060020a6000350463fc9c8d3981146040575b605e6000805473ffffffffffffffffffffffffffffffffffffffff191633179055565b606060005473ffffffffffffffffffffffffffffffffffffffff1681565b005b6060908152602090f3`, + `[{"constant":true,"inputs":[],"name":"caller","outputs":[{"name":"","type":"address"}],"type":"function"}]`, + ` + // Generate a new random account and a funded simulator + key := crypto.NewKey(rand.Reader) + sim := backends.NewSimulatedBackend(core.GenesisAccount{Address: key.Address, Balance: big.NewInt(10000000000)}) + + // Convert the tester key to an authorized transactor for ease of use + auth := bind.NewKeyedTransactor(key) + + // Deploy a default method invoker contract and execute its default method + _, _, defaulter, err := DeployDefaulter(auth, sim) + if err != nil { + t.Fatalf("Failed to deploy defaulter contract: %v", err) + } + if _, err := (&DefaulterRaw{defaulter}).Transfer(auth); err != nil { + t.Fatalf("Failed to invoke default method: %v", err) + } + sim.Commit() + + if caller, err := defaulter.Caller(nil); err != nil { + t.Fatalf("Failed to call address retriever: %v", err) + } else if (caller != key.Address) { + t.Fatalf("Address mismatch: have %v, want %v", caller, key.Address) + } + `, + }, } // Tests that packages generated by the binder can be successfully compiled and diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go index f1a10137c..36ac1d78d 100644 --- a/accounts/abi/bind/template.go +++ b/accounts/abi/bind/template.go @@ -110,6 +110,21 @@ package {{.Package}} 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.(bind.ContractCaller), backend.(bind.ContractTransactor)) @@ -146,6 +161,44 @@ package {{.Package}} return bind.NewBoundContract(address, parsed, caller, transactor), 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 .Calls}} // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}. // diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 5d5ab4559..645743c13 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -312,6 +312,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso utils.MiningEnabledFlag, utils.MiningGPUFlag, utils.AutoDAGFlag, + utils.TargetGasLimitFlag, utils.NATFlag, utils.NatspecEnabledFlag, utils.NoDiscoverFlag, @@ -359,6 +360,12 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso go metrics.CollectProcessMetrics(3 * time.Second) utils.SetupNetwork(ctx) + + // Deprecation warning. + if ctx.GlobalIsSet(utils.GenesisFileFlag.Name) { + common.PrintDepricationWarning("--genesis is deprecated. Switch to use 'geth init /path/to/file'") + } + return nil } diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index a31532bea..e2adf7305 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -121,10 +121,10 @@ var AppHelpFlagGroups = []flagGroup{ Flags: []cli.Flag{ utils.MiningEnabledFlag, utils.MinerThreadsFlag, - utils.TargetGasLimitFlag, utils.MiningGPUFlag, utils.AutoDAGFlag, utils.EtherbaseFlag, + utils.TargetGasLimitFlag, utils.GasPriceFlag, utils.ExtraDataFlag, }, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index faba32ce0..2f10938e3 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -784,23 +784,22 @@ func MustMakeChainConfig(ctx *cli.Context) *core.ChainConfig { ) defer db.Close() - chainConfig, err := core.GetChainConfig(db, genesis.Hash()) - if err != nil { - if err != core.ChainConfigNotFoundErr { + if genesis != nil { + // Exsting genesis block, use stored config if available. + storedConfig, err := core.GetChainConfig(db, genesis.Hash()) + if err == nil { + return storedConfig + } else if err != core.ChainConfigNotFoundErr { Fatalf("Could not make chain configuration: %v", err) } - var homesteadBlockNo *big.Int - if ctx.GlobalBool(TestNetFlag.Name) { - homesteadBlockNo = params.TestNetHomesteadBlock - } else { - homesteadBlockNo = params.MainNetHomesteadBlock - } - - chainConfig = &core.ChainConfig{ - HomesteadBlock: homesteadBlockNo, - } } - return chainConfig + var homesteadBlockNo *big.Int + if ctx.GlobalBool(TestNetFlag.Name) { + homesteadBlockNo = params.TestNetHomesteadBlock + } else { + homesteadBlockNo = params.MainNetHomesteadBlock + } + return &core.ChainConfig{HomesteadBlock: homesteadBlockNo} } // MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails. diff --git a/common/types.go b/common/types.go index b1666d733..fec986164 100644 --- a/common/types.go +++ b/common/types.go @@ -19,10 +19,12 @@ package common import ( "encoding/hex" "encoding/json" + "errors" "fmt" "math/big" "math/rand" "reflect" + "strings" ) const ( @@ -30,6 +32,8 @@ const ( AddressLength = 20 ) +var hashJsonLengthErr = errors.New("common: unmarshalJSON failed: hash must be exactly 32 bytes") + type ( Hash [HashLength]byte Address [AddressLength]byte @@ -58,6 +62,15 @@ func (h *Hash) UnmarshalJSON(input []byte) error { if length >= 2 && input[0] == '"' && input[length-1] == '"' { input = input[1 : length-1] } + // strip "0x" for length check + if len(input) > 1 && strings.ToLower(string(input[:2])) == "0x" { + input = input[2:] + } + + // validate the length of the input hash + if len(input) != HashLength*2 { + return hashJsonLengthErr + } h.SetBytes(FromHex(string(input))) return nil } diff --git a/common/types_test.go b/common/types_test.go index edf8d4d14..f2dfbf0c9 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -29,3 +29,25 @@ func TestBytesConversion(t *testing.T) { t.Errorf("expected %x got %x", exp, hash) } } + +func TestHashJsonValidation(t *testing.T) { + var h Hash + var tests = []struct { + Prefix string + Size int + Error error + }{ + {"", 2, hashJsonLengthErr}, + {"", 62, hashJsonLengthErr}, + {"", 66, hashJsonLengthErr}, + {"", 65, hashJsonLengthErr}, + {"0X", 64, nil}, + {"0x", 64, nil}, + {"0x", 62, hashJsonLengthErr}, + } + for i, test := range tests { + if err := h.UnmarshalJSON(append([]byte(test.Prefix), make([]byte, test.Size)...)); err != test.Error { + t.Error(i, "expected", test.Error, "got", err) + } + } +} diff --git a/eth/api.go b/eth/api.go index af03c096d..a257639ba 100644 --- a/eth/api.go +++ b/eth/api.go @@ -1103,10 +1103,10 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(encodedTx string) (string, return tx.Hash().Hex(), nil } -// Sign will sign the given data string with the given address. The account corresponding with the address needs to -// be unlocked. -func (s *PublicTransactionPoolAPI) Sign(address common.Address, data string) (string, error) { - signature, error := s.am.Sign(accounts.Account{Address: address}, common.HexToHash(data).Bytes()) +// Sign signs the given hash using the key that matches the address. The key must be unlocked in order to sign the +// hash. +func (s *PublicTransactionPoolAPI) Sign(address common.Address, hash common.Hash) (string, error) { + signature, error := s.am.Sign(accounts.Account{Address: address}, hash[:]) return common.ToHex(signature), error } diff --git a/eth/backend.go b/eth/backend.go index 3c3440a53..26af7ff91 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -169,10 +169,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { // Load up any custom genesis block if requested if len(config.Genesis) > 0 { - // Using println instead of glog to make sure it **always** displays regardless of - // verbosity settings. - common.PrintDepricationWarning("--genesis is deprecated. Switch to use 'geth init /path/to/file'") - block, err := core.WriteGenesisBlock(chainDb, strings.NewReader(config.Genesis)) if err != nil { return nil, err |