From 3a97280ae889bb6852ba16e70750a37b2ed08473 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 16 Dec 2015 04:26:23 +0100 Subject: [PATCH 01/32] eth: separate common and full node-specific API and backend service --- accounts/abi/bind/backend.go | 27 +- accounts/abi/bind/backends/nil.go | 19 +- accounts/abi/bind/backends/remote.go | 47 +- accounts/abi/bind/backends/simulated.go | 13 +- accounts/abi/bind/base.go | 19 +- accounts/account_manager.go | 22 + cmd/geth/main.go | 10 +- cmd/gethrpctest/main.go | 2 +- cmd/utils/flags.go | 7 + common/natspec/natspec_e2e_test.go | 4 +- console/console_test.go | 4 +- core/vm/environment.go | 2 + eth/api.go | 1607 ++--------------------- eth/api_backend.go | 201 +++ eth/backend.go | 328 +++-- eth/bind.go | 60 +- eth/cpu_mining.go | 2 +- eth/{ => gasprice}/gasprice.go | 57 +- eth/gpu_mining.go | 2 +- internal/ethapi/api.go | 1542 ++++++++++++++++++++++ internal/ethapi/backend.go | 119 ++ release/release.go | 7 +- 22 files changed, 2296 insertions(+), 1805 deletions(-) create mode 100644 eth/api_backend.go rename eth/{ => gasprice}/gasprice.go (75%) create mode 100644 internal/ethapi/api.go create mode 100644 internal/ethapi/backend.go diff --git a/accounts/abi/bind/backend.go b/accounts/abi/bind/backend.go index 65806aef42..bec742c29c 100644 --- a/accounts/abi/bind/backend.go +++ b/accounts/abi/bind/backend.go @@ -22,6 +22,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "golang.org/x/net/context" ) // ErrNoCode is returned by call and transact operations for which the requested @@ -35,12 +36,12 @@ type ContractCaller interface { // HasCode checks if the contract at the given address has any code associated // with it or not. This is needed to differentiate between contract internal // errors and the local chain being out of sync. - HasCode(contract common.Address, pending bool) (bool, error) + HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) // ContractCall executes an Ethereum contract call with the specified data as // the input. The pending flag requests execution against the pending block, not // the stable head of the chain. - ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) + ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) } // ContractTransactor defines the methods needed to allow operating with contract @@ -50,26 +51,26 @@ type ContractCaller interface { type ContractTransactor interface { // PendingAccountNonce retrieves the current pending nonce associated with an // account. - PendingAccountNonce(account common.Address) (uint64, error) + PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) // SuggestGasPrice retrieves the currently suggested gas price to allow a timely // execution of a transaction. - SuggestGasPrice() (*big.Int, error) + SuggestGasPrice(ctx context.Context) (*big.Int, error) // HasCode checks if the contract at the given address has any code associated // with it or not. This is needed to differentiate between contract internal // errors and the local chain being out of sync. - HasCode(contract common.Address, pending bool) (bool, error) + HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) // EstimateGasLimit 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. - EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) + EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) // SendTransaction injects the transaction into the pending pool for execution. - SendTransaction(tx *types.Transaction) error + SendTransaction(ctx context.Context, tx *types.Transaction) error } // ContractBackend defines the methods needed to allow operating with contract @@ -84,28 +85,28 @@ type ContractBackend interface { // HasCode checks if the contract at the given address has any code associated // with it or not. This is needed to differentiate between contract internal // errors and the local chain being out of sync. - HasCode(contract common.Address, pending bool) (bool, error) + HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) // ContractCall executes an Ethereum contract call with the specified data as // the input. The pending flag requests execution against the pending block, not // the stable head of the chain. - ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) + ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) // PendingAccountNonce retrieves the current pending nonce associated with an // account. - PendingAccountNonce(account common.Address) (uint64, error) + PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) // SuggestGasPrice retrieves the currently suggested gas price to allow a timely // execution of a transaction. - SuggestGasPrice() (*big.Int, error) + SuggestGasPrice(ctx context.Context) (*big.Int, error) // EstimateGasLimit 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. - EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) + EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) // SendTransaction injects the transaction into the pending pool for execution. - SendTransaction(tx *types.Transaction) error + SendTransaction(ctx context.Context, tx *types.Transaction) error } diff --git a/accounts/abi/bind/backends/nil.go b/accounts/abi/bind/backends/nil.go index f10bb61acb..54b222f1f2 100644 --- a/accounts/abi/bind/backends/nil.go +++ b/accounts/abi/bind/backends/nil.go @@ -22,6 +22,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "golang.org/x/net/context" ) // This nil assignment ensures compile time that nilBackend implements bind.ContractBackend. @@ -32,16 +33,22 @@ var _ bind.ContractBackend = (*nilBackend)(nil) // wrappers without calling any methods on them. type nilBackend struct{} -func (*nilBackend) ContractCall(common.Address, []byte, bool) ([]byte, error) { +func (*nilBackend) ContractCall(context.Context, common.Address, []byte, bool) ([]byte, error) { panic("not implemented") } -func (*nilBackend) EstimateGasLimit(common.Address, *common.Address, *big.Int, []byte) (*big.Int, error) { +func (*nilBackend) EstimateGasLimit(context.Context, common.Address, *common.Address, *big.Int, []byte) (*big.Int, error) { + panic("not implemented") +} +func (*nilBackend) HasCode(context.Context, common.Address, bool) (bool, error) { + panic("not implemented") +} +func (*nilBackend) SuggestGasPrice(context.Context) (*big.Int, error) { panic("not implemented") } +func (*nilBackend) PendingAccountNonce(context.Context, common.Address) (uint64, error) { + panic("not implemented") +} +func (*nilBackend) SendTransaction(context.Context, *types.Transaction) error { panic("not implemented") } -func (*nilBackend) HasCode(common.Address, bool) (bool, error) { panic("not implemented") } -func (*nilBackend) SuggestGasPrice() (*big.Int, error) { panic("not implemented") } -func (*nilBackend) PendingAccountNonce(common.Address) (uint64, error) { panic("not implemented") } -func (*nilBackend) SendTransaction(*types.Transaction) error { panic("not implemented") } // NewNilBackend creates a new binding backend that can be used for instantiation // but will panic on any invocation. Its sole purpose is to help testing. diff --git a/accounts/abi/bind/backends/remote.go b/accounts/abi/bind/backends/remote.go index d903cbc8f7..4793143e41 100644 --- a/accounts/abi/bind/backends/remote.go +++ b/accounts/abi/bind/backends/remote.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" ) // This nil assignment ensures compile time that rpcBackend implements bind.ContractBackend. @@ -80,18 +81,23 @@ type failure struct { // // This is currently painfully non-concurrent, but it will have to do until we // find the time for niceties like this :P -func (b *rpcBackend) request(method string, params []interface{}) (json.RawMessage, error) { +func (b *rpcBackend) request(ctx context.Context, method string, params []interface{}) (json.RawMessage, error) { b.lock.Lock() defer b.lock.Unlock() + if ctx == nil { + ctx = context.Background() + } + // Ugly hack to serialize an empty list properly if params == nil { params = []interface{}{} } // Assemble the request object + reqID := int(atomic.AddUint32(&b.autoid, 1)) req := &request{ JSONRPC: "2.0", - ID: int(atomic.AddUint32(&b.autoid, 1)), + ID: reqID, Method: method, Params: params, } @@ -99,8 +105,17 @@ func (b *rpcBackend) request(method string, params []interface{}) (json.RawMessa return nil, err } res := new(response) - if err := b.client.Recv(res); err != nil { - return nil, err + errc := make(chan error, 1) + go func() { + errc <- b.client.Recv(res) + }() + select { + case err := <-errc: + if err != nil { + return nil, err + } + case <-ctx.Done(): + return nil, ctx.Err() } if res.Error != nil { if res.Error.Message == bind.ErrNoCode.Error() { @@ -113,13 +128,13 @@ func (b *rpcBackend) request(method string, params []interface{}) (json.RawMessa // HasCode implements ContractVerifier.HasCode by retrieving any code associated // with the contract from the remote node, and checking its size. -func (b *rpcBackend) HasCode(contract common.Address, pending bool) (bool, error) { +func (b *rpcBackend) HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) { // Execute the RPC code retrieval block := "latest" if pending { block = "pending" } - res, err := b.request("eth_getCode", []interface{}{contract.Hex(), block}) + res, err := b.request(ctx, "eth_getCode", []interface{}{contract.Hex(), block}) if err != nil { return false, err } @@ -133,7 +148,7 @@ func (b *rpcBackend) HasCode(contract common.Address, pending bool) (bool, error // ContractCall implements ContractCaller.ContractCall, delegating the execution of // a contract call to the remote node, returning the reply to for local processing. -func (b *rpcBackend) ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) { +func (b *rpcBackend) ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) { // Pack up the request into an RPC argument args := struct { To common.Address `json:"to"` @@ -147,7 +162,7 @@ func (b *rpcBackend) ContractCall(contract common.Address, data []byte, pending if pending { block = "pending" } - res, err := b.request("eth_call", []interface{}{args, block}) + res, err := b.request(ctx, "eth_call", []interface{}{args, block}) if err != nil { return nil, err } @@ -161,8 +176,8 @@ func (b *rpcBackend) ContractCall(contract common.Address, data []byte, pending // PendingAccountNonce implements ContractTransactor.PendingAccountNonce, delegating // the current account nonce retrieval to the remote node. -func (b *rpcBackend) PendingAccountNonce(account common.Address) (uint64, error) { - res, err := b.request("eth_getTransactionCount", []interface{}{account.Hex(), "pending"}) +func (b *rpcBackend) PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) { + res, err := b.request(ctx, "eth_getTransactionCount", []interface{}{account.Hex(), "pending"}) if err != nil { return 0, err } @@ -179,8 +194,8 @@ func (b *rpcBackend) PendingAccountNonce(account common.Address) (uint64, error) // SuggestGasPrice implements ContractTransactor.SuggestGasPrice, delegating the // gas price oracle request to the remote node. -func (b *rpcBackend) SuggestGasPrice() (*big.Int, error) { - res, err := b.request("eth_gasPrice", nil) +func (b *rpcBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) { + res, err := b.request(ctx, "eth_gasPrice", nil) if err != nil { return nil, err } @@ -197,7 +212,7 @@ func (b *rpcBackend) SuggestGasPrice() (*big.Int, error) { // EstimateGasLimit implements ContractTransactor.EstimateGasLimit, delegating // the gas estimation to the remote node. -func (b *rpcBackend) EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) { +func (b *rpcBackend) EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) { // Pack up the request into an RPC argument args := struct { From common.Address `json:"from"` @@ -211,7 +226,7 @@ func (b *rpcBackend) EstimateGasLimit(sender common.Address, contract *common.Ad Value: rpc.NewHexNumber(value), } // Execute the RPC call and retrieve the response - res, err := b.request("eth_estimateGas", []interface{}{args}) + res, err := b.request(ctx, "eth_estimateGas", []interface{}{args}) if err != nil { return nil, err } @@ -228,12 +243,12 @@ func (b *rpcBackend) EstimateGasLimit(sender common.Address, contract *common.Ad // SendTransaction implements ContractTransactor.SendTransaction, delegating the // raw transaction injection to the remote node. -func (b *rpcBackend) SendTransaction(tx *types.Transaction) error { +func (b *rpcBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error { data, err := rlp.EncodeToBytes(tx) if err != nil { return err } - res, err := b.request("eth_sendRawTransaction", []interface{}{common.ToHex(data)}) + res, err := b.request(ctx, "eth_sendRawTransaction", []interface{}{common.ToHex(data)}) if err != nil { return err } diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 54b1ce6032..490da82a6c 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -27,6 +27,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "golang.org/x/net/context" ) // Default chain configuration which sets homestead phase at block 0 (i.e. no frontier) @@ -80,7 +81,7 @@ func (b *SimulatedBackend) Rollback() { // HasCode implements ContractVerifier.HasCode, checking whether there is any // code associated with a certain account in the blockchain. -func (b *SimulatedBackend) HasCode(contract common.Address, pending bool) (bool, error) { +func (b *SimulatedBackend) HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) { if pending { return len(b.pendingState.GetCode(contract)) > 0, nil } @@ -90,7 +91,7 @@ func (b *SimulatedBackend) HasCode(contract common.Address, pending bool) (bool, // ContractCall implements ContractCaller.ContractCall, executing the specified // contract with the given input data. -func (b *SimulatedBackend) ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) { +func (b *SimulatedBackend) ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) { // Create a copy of the current state db to screw around with var ( block *types.Block @@ -129,20 +130,20 @@ func (b *SimulatedBackend) ContractCall(contract common.Address, data []byte, pe // PendingAccountNonce implements ContractTransactor.PendingAccountNonce, retrieving // the nonce currently pending for the account. -func (b *SimulatedBackend) PendingAccountNonce(account common.Address) (uint64, error) { +func (b *SimulatedBackend) PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) { return b.pendingState.GetOrNewStateObject(account).Nonce(), nil } // SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated // chain doens't have miners, we just return a gas price of 1 for any call. -func (b *SimulatedBackend) SuggestGasPrice() (*big.Int, error) { +func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) { return big.NewInt(1), nil } // EstimateGasLimit implements ContractTransactor.EstimateGasLimit, executing the // requested code against the currently pending block/state and returning the used // gas. -func (b *SimulatedBackend) EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) { +func (b *SimulatedBackend) EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) { // Create a copy of the currently pending state db to screw around with var ( block = b.pendingBlock @@ -177,7 +178,7 @@ func (b *SimulatedBackend) EstimateGasLimit(sender common.Address, contract *com // SendTransaction implements ContractTransactor.SendTransaction, delegating the raw // transaction injection to the remote node. -func (b *SimulatedBackend) SendTransaction(tx *types.Transaction) error { +func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error { blocks, _ := core.GenerateChain(b.blockchain.CurrentBlock(), b.database, 1, func(number int, block *core.BlockGen) { for _, tx := range b.pendingBlock.Transactions() { block.AddTx(tx) diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go index 75e8d5bc85..80948d3f1d 100644 --- a/accounts/abi/bind/base.go +++ b/accounts/abi/bind/base.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "golang.org/x/net/context" ) // SignerFn is a signer function callback when a contract requires a method to @@ -35,6 +36,8 @@ type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, erro // 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 + + Context context.Context // Network context to support cancellation and timeouts (nil = no timeout) } // TransactOpts is the collection of authorization data required to create a @@ -47,6 +50,8 @@ type TransactOpts struct { 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 *big.Int // Gas limit to set for the transaction execution (nil = estimate + 10%) + + 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 @@ -102,7 +107,7 @@ func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string, } // Make sure we have a contract to operate on, and bail out otherwise if (opts.Pending && atomic.LoadUint32(&c.pendingHasCode) == 0) || (!opts.Pending && atomic.LoadUint32(&c.latestHasCode) == 0) { - if code, err := c.caller.HasCode(c.address, opts.Pending); err != nil { + if code, err := c.caller.HasCode(opts.Context, c.address, opts.Pending); err != nil { return err } else if !code { return ErrNoCode @@ -118,7 +123,7 @@ func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string, if err != nil { return err } - output, err := c.caller.ContractCall(c.address, input, opts.Pending) + output, err := c.caller.ContractCall(opts.Context, c.address, input, opts.Pending) if err != nil { return err } @@ -153,7 +158,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i } nonce := uint64(0) if opts.Nonce == nil { - nonce, err = c.transactor.PendingAccountNonce(opts.From) + nonce, err = c.transactor.PendingAccountNonce(opts.Context, opts.From) if err != nil { return nil, fmt.Errorf("failed to retrieve account nonce: %v", err) } @@ -163,7 +168,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i // Figure out the gas allowance and gas price values gasPrice := opts.GasPrice if gasPrice == nil { - gasPrice, err = c.transactor.SuggestGasPrice() + gasPrice, err = c.transactor.SuggestGasPrice(opts.Context) if err != nil { return nil, fmt.Errorf("failed to suggest gas price: %v", err) } @@ -172,7 +177,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i if gasLimit == nil { // Gas estimation cannot succeed without code for method invocations if contract != nil && atomic.LoadUint32(&c.pendingHasCode) == 0 { - if code, err := c.transactor.HasCode(c.address, true); err != nil { + if code, err := c.transactor.HasCode(opts.Context, c.address, true); err != nil { return nil, err } else if !code { return nil, ErrNoCode @@ -180,7 +185,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i atomic.StoreUint32(&c.pendingHasCode, 1) } // If the contract surely has code (or code is not needed), estimate the transaction - gasLimit, err = c.transactor.EstimateGasLimit(opts.From, contract, value, input) + gasLimit, err = c.transactor.EstimateGasLimit(opts.Context, opts.From, contract, value, input) if err != nil { return nil, fmt.Errorf("failed to exstimate gas needed: %v", err) } @@ -199,7 +204,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i if err != nil { return nil, err } - if err := c.transactor.SendTransaction(signedTx); err != nil { + if err := c.transactor.SendTransaction(opts.Context, signedTx); err != nil { return nil, err } return signedTx, nil diff --git a/accounts/account_manager.go b/accounts/account_manager.go index bfb7556d6a..982a2ca6ec 100644 --- a/accounts/account_manager.go +++ b/accounts/account_manager.go @@ -34,6 +34,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rpc" ) var ( @@ -340,3 +342,23 @@ func zeroKey(k *ecdsa.PrivateKey) { b[i] = 0 } } + +// APIs implements node.Service +func (am *Manager) APIs() []rpc.API { + return nil +} + +// Protocols implements node.Service +func (am *Manager) Protocols() []p2p.Protocol { + return nil +} + +// Start implements node.Service +func (am *Manager) Start(srvr *p2p.Server) error { + return nil +} + +// Stop implements node.Service +func (am *Manager) Stop() error { + return nil +} diff --git a/cmd/geth/main.go b/cmd/geth/main.go index c372430f1c..623f8ac811 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -29,6 +29,7 @@ import ( "time" "github.com/ethereum/ethash" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console" @@ -313,11 +314,10 @@ func startNode(ctx *cli.Context, stack *node.Node) { utils.StartNode(stack) // Unlock any account specifically requested - var ethereum *eth.Ethereum - if err := stack.Service(ðereum); err != nil { + var accman *accounts.Manager + if err := stack.Service(&accman); err != nil { utils.Fatalf("ethereum service not running: %v", err) } - accman := ethereum.AccountManager() passwords := utils.MakePasswordList(ctx) accounts := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",") @@ -328,6 +328,10 @@ func startNode(ctx *cli.Context, stack *node.Node) { } // Start auxiliary services if enabled if ctx.GlobalBool(utils.MiningEnabledFlag.Name) { + var ethereum *eth.FullNodeService + if err := stack.Service(ðereum); err != nil { + utils.Fatalf("ethereum service not running: %v", err) + } if err := ethereum.StartMining(ctx.GlobalInt(utils.MinerThreadsFlag.Name), ctx.GlobalString(utils.MiningGPUFlag.Name)); err != nil { utils.Fatalf("Failed to start mining: %v", err) } diff --git a/cmd/gethrpctest/main.go b/cmd/gethrpctest/main.go index 2e07e94260..668efbfc76 100644 --- a/cmd/gethrpctest/main.go +++ b/cmd/gethrpctest/main.go @@ -146,7 +146,7 @@ func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node // RunTest executes the specified test against an already pre-configured protocol // stack to ensure basic checks pass before running RPC tests. func RunTest(stack *node.Node, test *tests.BlockTest) error { - var ethereum *eth.Ethereum + var ethereum *eth.FullNodeService stack.Service(ðereum) blockchain := ethereum.BlockChain() diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 14898b9874..38ba3a9ba0 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -763,6 +763,13 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, if err != nil { Fatalf("Failed to create the protocol stack: %v", err) } + + if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { + return accman, nil + }); err != nil { + Fatalf("Failed to register the account manager service: %v", err) + } + if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil { diff --git a/common/natspec/natspec_e2e_test.go b/common/natspec/natspec_e2e_test.go index 2552605769..ac0bbe784c 100644 --- a/common/natspec/natspec_e2e_test.go +++ b/common/natspec/natspec_e2e_test.go @@ -99,7 +99,7 @@ const ( type testFrontend struct { t *testing.T - ethereum *eth.Ethereum + ethereum *eth.FullNodeService xeth *xe.XEth wait chan *big.Int lastConfirm string @@ -123,7 +123,7 @@ func (self *testFrontend) ConfirmTransaction(tx string) bool { return true } -func testEth(t *testing.T) (ethereum *eth.Ethereum, err error) { +func testEth(t *testing.T) (ethereum *eth.FullNodeService, err error) { tmp, err := ioutil.TempDir("", "natspec-test") if err != nil { diff --git a/console/console_test.go b/console/console_test.go index 7738d0c442..b40db06046 100644 --- a/console/console_test.go +++ b/console/console_test.go @@ -76,7 +76,7 @@ func (p *hookedPrompter) SetWordCompleter(completer WordCompleter) {} type tester struct { workspace string stack *node.Node - ethereum *eth.Ethereum + ethereum *eth.FullNodeService console *Console input *hookedPrompter output *bytes.Buffer @@ -134,7 +134,7 @@ func newTester(t *testing.T, confOverride func(*eth.Config)) *tester { t.Fatalf("failed to create JavaScript console: %v", err) } // Create the final tester and return - var ethereum *eth.Ethereum + var ethereum *eth.FullNodeService stack.Service(ðereum) return &tester{ diff --git a/core/vm/environment.go b/core/vm/environment.go index 747627565e..664887454a 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -73,6 +73,8 @@ type Environment interface { DelegateCall(me ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) // Create a new contract Create(me ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) + + StructLogs() []StructLog } // Vm is the basic interface for an implementation of the EVM. diff --git a/eth/api.go b/eth/api.go index 3cb6c4d103..8fd759a21e 100644 --- a/eth/api.go +++ b/eth/api.go @@ -18,8 +18,6 @@ package eth import ( "bytes" - "encoding/hex" - "encoding/json" "errors" "fmt" "io" @@ -27,166 +25,56 @@ import ( "math/big" "os" "runtime" - "strings" - "sync" - "time" "github.com/ethereum/ethash" - "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/compiler" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/miner" - "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" - "github.com/syndtr/goleveldb/leveldb" - "golang.org/x/net/context" ) -const defaultGas = uint64(90000) - -// blockByNumber is a commonly used helper function which retrieves and returns -// the block for the given block number, capable of handling two special blocks: -// rpc.LatestBlockNumber and rpc.PendingBlockNumber. It returns nil when no block -// could be found. -func blockByNumber(m *miner.Miner, bc *core.BlockChain, blockNr rpc.BlockNumber) *types.Block { - // Pending block is only known by the miner - if blockNr == rpc.PendingBlockNumber { - block, _ := m.Pending() - return block - } - // Otherwise resolve and return the block - if blockNr == rpc.LatestBlockNumber { - return bc.CurrentBlock() - } - return bc.GetBlockByNumber(uint64(blockNr)) +// PublicFullEthereumAPI provides an API to access Ethereum full node-related +// information. +type PublicFullEthereumAPI struct { + e *FullNodeService } -// stateAndBlockByNumber is a commonly used helper function which retrieves and -// returns the state and containing block for the given block number, capable of -// handling two special states: rpc.LatestBlockNumber and rpc.PendingBlockNumber. -// It returns nil when no block or state could be found. -func stateAndBlockByNumber(m *miner.Miner, bc *core.BlockChain, blockNr rpc.BlockNumber, chainDb ethdb.Database) (*state.StateDB, *types.Block, error) { - // Pending state is only known by the miner - if blockNr == rpc.PendingBlockNumber { - block, state := m.Pending() - return state, block, nil - } - // Otherwise resolve the block number and return its state - block := blockByNumber(m, bc, blockNr) - if block == nil { - return nil, nil, nil - } - stateDb, err := state.New(block.Root(), chainDb) - return stateDb, block, err -} - -// PublicEthereumAPI provides an API to access Ethereum related information. -// It offers only methods that operate on public data that is freely available to anyone. -type PublicEthereumAPI struct { - e *Ethereum - gpo *GasPriceOracle -} - -// NewPublicEthereumAPI creates a new Ethereum protocol API. -func NewPublicEthereumAPI(e *Ethereum) *PublicEthereumAPI { - return &PublicEthereumAPI{ - e: e, - gpo: e.gpo, - } -} - -// GasPrice returns a suggestion for a gas price. -func (s *PublicEthereumAPI) GasPrice() *big.Int { - return s.gpo.SuggestPrice() -} - -// GetCompilers returns the collection of available smart contract compilers -func (s *PublicEthereumAPI) GetCompilers() ([]string, error) { - solc, err := s.e.Solc() - if err == nil && solc != nil { - return []string{"Solidity"}, nil - } - - return []string{}, nil -} - -// CompileSolidity compiles the given solidity source -func (s *PublicEthereumAPI) CompileSolidity(source string) (map[string]*compiler.Contract, error) { - solc, err := s.e.Solc() - if err != nil { - return nil, err - } - - if solc == nil { - return nil, errors.New("solc (solidity compiler) not found") - } - - return solc.Compile(source) +// NewPublicFullEthereumAPI creates a new Etheruem protocol API for full nodes. +func NewPublicFullEthereumAPI(e *FullNodeService) *PublicFullEthereumAPI { + return &PublicFullEthereumAPI{e} } // Etherbase is the address that mining rewards will be send to -func (s *PublicEthereumAPI) Etherbase() (common.Address, error) { +func (s *PublicFullEthereumAPI) Etherbase() (common.Address, error) { return s.e.Etherbase() } // Coinbase is the address that mining rewards will be send to (alias for Etherbase) -func (s *PublicEthereumAPI) Coinbase() (common.Address, error) { +func (s *PublicFullEthereumAPI) Coinbase() (common.Address, error) { return s.Etherbase() } -// ProtocolVersion returns the current Ethereum protocol version this node supports -func (s *PublicEthereumAPI) ProtocolVersion() *rpc.HexNumber { - return rpc.NewHexNumber(s.e.EthVersion()) -} - // Hashrate returns the POW hashrate -func (s *PublicEthereumAPI) Hashrate() *rpc.HexNumber { +func (s *PublicFullEthereumAPI) Hashrate() *rpc.HexNumber { return rpc.NewHexNumber(s.e.Miner().HashRate()) } -// Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not -// yet received the latest block headers from its pears. In case it is synchronizing: -// - startingBlock: block number this node started to synchronise from -// - currentBlock: block number this node is currently importing -// - highestBlock: block number of the highest block header this node has received from peers -// - pulledStates: number of state entries processed until now -// - knownStates: number of known state entries that still need to be pulled -func (s *PublicEthereumAPI) Syncing() (interface{}, error) { - origin, current, height, pulled, known := s.e.Downloader().Progress() - - // Return not syncing if the synchronisation already completed - if current >= height { - return false, nil - } - // Otherwise gather the block sync stats - return map[string]interface{}{ - "startingBlock": rpc.NewHexNumber(origin), - "currentBlock": rpc.NewHexNumber(current), - "highestBlock": rpc.NewHexNumber(height), - "pulledStates": rpc.NewHexNumber(pulled), - "knownStates": rpc.NewHexNumber(known), - }, nil -} - // PublicMinerAPI provides an API to control the miner. // It offers only methods that operate on data that pose no security risk when it is publicly accessible. type PublicMinerAPI struct { - e *Ethereum + e *FullNodeService agent *miner.RemoteAgent } // NewPublicMinerAPI create a new PublicMinerAPI instance. -func NewPublicMinerAPI(e *Ethereum) *PublicMinerAPI { +func NewPublicMinerAPI(e *FullNodeService) *PublicMinerAPI { agent := miner.NewRemoteAgent() e.Miner().Register(agent) @@ -232,11 +120,11 @@ func (s *PublicMinerAPI) SubmitHashrate(hashrate rpc.HexNumber, id common.Hash) // PrivateMinerAPI provides private RPC methods to control the miner. // These methods can be abused by external users and must be considered insecure for use by untrusted users. type PrivateMinerAPI struct { - e *Ethereum + e *FullNodeService } // NewPrivateMinerAPI create a new RPC service which controls the miner of this node. -func NewPrivateMinerAPI(e *Ethereum) *PrivateMinerAPI { +func NewPrivateMinerAPI(e *FullNodeService) *PrivateMinerAPI { return &PrivateMinerAPI{e: e} } @@ -303,1199 +191,20 @@ func (s *PrivateMinerAPI) MakeDAG(blockNr rpc.BlockNumber) (bool, error) { return true, nil } -// PublicTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential. -type PublicTxPoolAPI struct { - e *Ethereum +// PrivateFullAdminAPI is the collection of Etheruem full node-related APIs +// exposed over the private admin endpoint. +type PrivateFullAdminAPI struct { + eth *FullNodeService } -// NewPublicTxPoolAPI creates a new tx pool service that gives information about the transaction pool. -func NewPublicTxPoolAPI(e *Ethereum) *PublicTxPoolAPI { - return &PublicTxPoolAPI{e} -} - -// Content returns the transactions contained within the transaction pool. -func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string][]*RPCTransaction { - content := map[string]map[string]map[string][]*RPCTransaction{ - "pending": make(map[string]map[string][]*RPCTransaction), - "queued": make(map[string]map[string][]*RPCTransaction), - } - pending, queue := s.e.TxPool().Content() - - // Flatten the pending transactions - for account, batches := range pending { - dump := make(map[string][]*RPCTransaction) - for nonce, txs := range batches { - nonce := fmt.Sprintf("%d", nonce) - for _, tx := range txs { - dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx)) - } - } - content["pending"][account.Hex()] = dump - } - // Flatten the queued transactions - for account, batches := range queue { - dump := make(map[string][]*RPCTransaction) - for nonce, txs := range batches { - nonce := fmt.Sprintf("%d", nonce) - for _, tx := range txs { - dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx)) - } - } - content["queued"][account.Hex()] = dump - } - return content -} - -// Status returns the number of pending and queued transaction in the pool. -func (s *PublicTxPoolAPI) Status() map[string]*rpc.HexNumber { - pending, queue := s.e.TxPool().Stats() - return map[string]*rpc.HexNumber{ - "pending": rpc.NewHexNumber(pending), - "queued": rpc.NewHexNumber(queue), - } -} - -// Inspect retrieves the content of the transaction pool and flattens it into an -// easily inspectable list. -func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string][]string { - content := map[string]map[string]map[string][]string{ - "pending": make(map[string]map[string][]string), - "queued": make(map[string]map[string][]string), - } - pending, queue := s.e.TxPool().Content() - - // Define a formatter to flatten a transaction into a string - var format = func(tx *types.Transaction) string { - if to := tx.To(); to != nil { - return fmt.Sprintf("%s: %v wei + %v × %v gas", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice()) - } - return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice()) - } - // Flatten the pending transactions - for account, batches := range pending { - dump := make(map[string][]string) - for nonce, txs := range batches { - nonce := fmt.Sprintf("%d", nonce) - for _, tx := range txs { - dump[nonce] = append(dump[nonce], format(tx)) - } - } - content["pending"][account.Hex()] = dump - } - // Flatten the queued transactions - for account, batches := range queue { - dump := make(map[string][]string) - for nonce, txs := range batches { - nonce := fmt.Sprintf("%d", nonce) - for _, tx := range txs { - dump[nonce] = append(dump[nonce], format(tx)) - } - } - content["queued"][account.Hex()] = dump - } - return content -} - -// PublicAccountAPI provides an API to access accounts managed by this node. -// It offers only methods that can retrieve accounts. -type PublicAccountAPI struct { - am *accounts.Manager -} - -// NewPublicAccountAPI creates a new PublicAccountAPI. -func NewPublicAccountAPI(am *accounts.Manager) *PublicAccountAPI { - return &PublicAccountAPI{am: am} -} - -// Accounts returns the collection of accounts this node manages -func (s *PublicAccountAPI) Accounts() []accounts.Account { - return s.am.Accounts() -} - -// PrivateAccountAPI provides an API to access accounts managed by this node. -// It offers methods to create, (un)lock en list accounts. Some methods accept -// passwords and are therefore considered private by default. -type PrivateAccountAPI struct { - am *accounts.Manager - txPool *core.TxPool - txMu *sync.Mutex - gpo *GasPriceOracle -} - -// NewPrivateAccountAPI create a new PrivateAccountAPI. -func NewPrivateAccountAPI(e *Ethereum) *PrivateAccountAPI { - return &PrivateAccountAPI{ - am: e.accountManager, - txPool: e.txPool, - txMu: &e.txMu, - gpo: e.gpo, - } -} - -// ListAccounts will return a list of addresses for accounts this node manages. -func (s *PrivateAccountAPI) ListAccounts() []common.Address { - accounts := s.am.Accounts() - addresses := make([]common.Address, len(accounts)) - for i, acc := range accounts { - addresses[i] = acc.Address - } - return addresses -} - -// NewAccount will create a new account and returns the address for the new account. -func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error) { - acc, err := s.am.NewAccount(password) - if err == nil { - return acc.Address, nil - } - return common.Address{}, err -} - -// ImportRawKey stores the given hex encoded ECDSA key into the key directory, -// encrypting it with the passphrase. -func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (common.Address, error) { - hexkey, err := hex.DecodeString(privkey) - if err != nil { - return common.Address{}, err - } - - acc, err := s.am.ImportECDSA(crypto.ToECDSA(hexkey), password) - return acc.Address, err -} - -// UnlockAccount will unlock the account associated with the given address with -// the given password for duration seconds. If duration is nil it will use a -// default of 300 seconds. It returns an indication if the account was unlocked. -func (s *PrivateAccountAPI) UnlockAccount(addr common.Address, password string, duration *rpc.HexNumber) (bool, error) { - if duration == nil { - duration = rpc.NewHexNumber(300) - } - a := accounts.Account{Address: addr} - d := time.Duration(duration.Int64()) * time.Second - if err := s.am.TimedUnlock(a, password, d); err != nil { - return false, err - } - return true, nil -} - -// LockAccount will lock the account associated with the given address when it's unlocked. -func (s *PrivateAccountAPI) LockAccount(addr common.Address) bool { - return s.am.Lock(addr) == nil -} - -// SignAndSendTransaction will create a transaction from the given arguments and -// tries to sign it with the key associated with args.To. If the given passwd isn't -// able to decrypt the key it fails. -func (s *PrivateAccountAPI) SignAndSendTransaction(args SendTxArgs, passwd string) (common.Hash, error) { - args = prepareSendTxArgs(args, s.gpo) - - s.txMu.Lock() - defer s.txMu.Unlock() - - if args.Nonce == nil { - args.Nonce = rpc.NewHexNumber(s.txPool.State().GetNonce(args.From)) - } - - var tx *types.Transaction - if args.To == nil { - tx = types.NewContractCreation(args.Nonce.Uint64(), args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) - } else { - tx = types.NewTransaction(args.Nonce.Uint64(), *args.To, args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) - } - - signature, err := s.am.SignWithPassphrase(args.From, passwd, tx.SigHash().Bytes()) - if err != nil { - return common.Hash{}, err - } - - return submitTransaction(s.txPool, tx, signature) -} - -// PublicBlockChainAPI provides an API to access the Ethereum blockchain. -// It offers only methods that operate on public data that is freely available to anyone. -type PublicBlockChainAPI struct { - config *core.ChainConfig - bc *core.BlockChain - chainDb ethdb.Database - eventMux *event.TypeMux - muNewBlockSubscriptions sync.Mutex // protects newBlocksSubscriptions - newBlockSubscriptions map[string]func(core.ChainEvent) error // callbacks for new block subscriptions - am *accounts.Manager - miner *miner.Miner - gpo *GasPriceOracle -} - -// NewPublicBlockChainAPI creates a new Etheruem blockchain API. -func NewPublicBlockChainAPI(config *core.ChainConfig, bc *core.BlockChain, m *miner.Miner, chainDb ethdb.Database, gpo *GasPriceOracle, eventMux *event.TypeMux, am *accounts.Manager) *PublicBlockChainAPI { - api := &PublicBlockChainAPI{ - config: config, - bc: bc, - miner: m, - chainDb: chainDb, - eventMux: eventMux, - am: am, - newBlockSubscriptions: make(map[string]func(core.ChainEvent) error), - gpo: gpo, - } - - go api.subscriptionLoop() - - return api -} - -// subscriptionLoop reads events from the global event mux and creates notifications for the matched subscriptions. -func (s *PublicBlockChainAPI) subscriptionLoop() { - sub := s.eventMux.Subscribe(core.ChainEvent{}) - for event := range sub.Chan() { - if chainEvent, ok := event.Data.(core.ChainEvent); ok { - s.muNewBlockSubscriptions.Lock() - for id, notifyOf := range s.newBlockSubscriptions { - if notifyOf(chainEvent) == rpc.ErrNotificationNotFound { - delete(s.newBlockSubscriptions, id) - } - } - s.muNewBlockSubscriptions.Unlock() - } - } -} - -// BlockNumber returns the block number of the chain head. -func (s *PublicBlockChainAPI) BlockNumber() *big.Int { - return s.bc.CurrentHeader().Number -} - -// GetBalance returns the amount of wei for the given address in the state of the -// given block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta -// block numbers are also allowed. -func (s *PublicBlockChainAPI) GetBalance(address common.Address, blockNr rpc.BlockNumber) (*big.Int, error) { - state, _, err := stateAndBlockByNumber(s.miner, s.bc, blockNr, s.chainDb) - if state == nil || err != nil { - return nil, err - } - return state.GetBalance(address), nil -} - -// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all -// transactions in the block are returned in full detail, otherwise only the transaction hash is returned. -func (s *PublicBlockChainAPI) GetBlockByNumber(blockNr rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { - if block := blockByNumber(s.miner, s.bc, blockNr); block != nil { - response, err := s.rpcOutputBlock(block, true, fullTx) - if err == nil && blockNr == rpc.PendingBlockNumber { - // Pending blocks need to nil out a few fields - for _, field := range []string{"hash", "nonce", "logsBloom", "miner"} { - response[field] = nil - } - } - return response, err - } - return nil, nil -} - -// GetBlockByHash returns the requested block. When fullTx is true all transactions in the block are returned in full -// detail, otherwise only the transaction hash is returned. -func (s *PublicBlockChainAPI) GetBlockByHash(blockHash common.Hash, fullTx bool) (map[string]interface{}, error) { - if block := s.bc.GetBlockByHash(blockHash); block != nil { - return s.rpcOutputBlock(block, true, fullTx) - } - return nil, nil -} - -// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index. When fullTx is true -// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned. -func (s *PublicBlockChainAPI) GetUncleByBlockNumberAndIndex(blockNr rpc.BlockNumber, index rpc.HexNumber) (map[string]interface{}, error) { - if block := blockByNumber(s.miner, s.bc, blockNr); block != nil { - uncles := block.Uncles() - if index.Int() < 0 || index.Int() >= len(uncles) { - glog.V(logger.Debug).Infof("uncle block on index %d not found for block #%d", index.Int(), blockNr) - return nil, nil - } - block = types.NewBlockWithHeader(uncles[index.Int()]) - return s.rpcOutputBlock(block, false, false) - } - return nil, nil -} - -// GetUncleByBlockHashAndIndex returns the uncle block for the given block hash and index. When fullTx is true -// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned. -func (s *PublicBlockChainAPI) GetUncleByBlockHashAndIndex(blockHash common.Hash, index rpc.HexNumber) (map[string]interface{}, error) { - if block := s.bc.GetBlockByHash(blockHash); block != nil { - uncles := block.Uncles() - if index.Int() < 0 || index.Int() >= len(uncles) { - glog.V(logger.Debug).Infof("uncle block on index %d not found for block %s", index.Int(), blockHash.Hex()) - return nil, nil - } - block = types.NewBlockWithHeader(uncles[index.Int()]) - return s.rpcOutputBlock(block, false, false) - } - return nil, nil -} - -// GetUncleCountByBlockNumber returns number of uncles in the block for the given block number -func (s *PublicBlockChainAPI) GetUncleCountByBlockNumber(blockNr rpc.BlockNumber) *rpc.HexNumber { - if block := blockByNumber(s.miner, s.bc, blockNr); block != nil { - return rpc.NewHexNumber(len(block.Uncles())) - } - return nil -} - -// GetUncleCountByBlockHash returns number of uncles in the block for the given block hash -func (s *PublicBlockChainAPI) GetUncleCountByBlockHash(blockHash common.Hash) *rpc.HexNumber { - if block := s.bc.GetBlockByHash(blockHash); block != nil { - return rpc.NewHexNumber(len(block.Uncles())) - } - return nil -} - -// NewBlocksArgs allows the user to specify if the returned block should include transactions and in which format. -type NewBlocksArgs struct { - IncludeTransactions bool `json:"includeTransactions"` - TransactionDetails bool `json:"transactionDetails"` -} - -// NewBlocks triggers a new block event each time a block is appended to the chain. It accepts an argument which allows -// the caller to specify whether the output should contain transactions and in what format. -func (s *PublicBlockChainAPI) NewBlocks(ctx context.Context, args NewBlocksArgs) (rpc.Subscription, error) { - notifier, supported := rpc.NotifierFromContext(ctx) - if !supported { - return nil, rpc.ErrNotificationsUnsupported - } - - // create a subscription that will remove itself when unsubscribed/cancelled - subscription, err := notifier.NewSubscription(func(subId string) { - s.muNewBlockSubscriptions.Lock() - delete(s.newBlockSubscriptions, subId) - s.muNewBlockSubscriptions.Unlock() - }) - - if err != nil { - return nil, err - } - - // add a callback that is called on chain events which will format the block and notify the client - s.muNewBlockSubscriptions.Lock() - s.newBlockSubscriptions[subscription.ID()] = func(e core.ChainEvent) error { - notification, err := s.rpcOutputBlock(e.Block, args.IncludeTransactions, args.TransactionDetails) - if err == nil { - return subscription.Notify(notification) - } - glog.V(logger.Warn).Info("unable to format block %v\n", err) - return nil - } - s.muNewBlockSubscriptions.Unlock() - return subscription, nil -} - -// GetCode returns the code stored at the given address in the state for the given block number. -func (s *PublicBlockChainAPI) GetCode(address common.Address, blockNr rpc.BlockNumber) (string, error) { - state, _, err := stateAndBlockByNumber(s.miner, s.bc, blockNr, s.chainDb) - if state == nil || err != nil { - return "", err - } - res := state.GetCode(address) - if len(res) == 0 { // backwards compatibility - return "0x", nil - } - return common.ToHex(res), nil -} - -// GetStorageAt returns the storage from the state at the given address, key and -// block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta block -// numbers are also allowed. -func (s *PublicBlockChainAPI) GetStorageAt(address common.Address, key string, blockNr rpc.BlockNumber) (string, error) { - state, _, err := stateAndBlockByNumber(s.miner, s.bc, blockNr, s.chainDb) - if state == nil || err != nil { - return "0x", err - } - return state.GetState(address, common.HexToHash(key)).Hex(), nil -} - -// callmsg is the message type used for call transactions. -type callmsg struct { - from *state.StateObject - to *common.Address - gas, gasPrice *big.Int - value *big.Int - data []byte -} - -// accessor boilerplate to implement core.Message -func (m callmsg) From() (common.Address, error) { return m.from.Address(), nil } -func (m callmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil } -func (m callmsg) Nonce() uint64 { return m.from.Nonce() } -func (m callmsg) To() *common.Address { return m.to } -func (m callmsg) GasPrice() *big.Int { return m.gasPrice } -func (m callmsg) Gas() *big.Int { return m.gas } -func (m callmsg) Value() *big.Int { return m.value } -func (m callmsg) Data() []byte { return m.data } - -// CallArgs represents the arguments for a call. -type CallArgs struct { - From common.Address `json:"from"` - To *common.Address `json:"to"` - Gas *rpc.HexNumber `json:"gas"` - GasPrice *rpc.HexNumber `json:"gasPrice"` - Value rpc.HexNumber `json:"value"` - Data string `json:"data"` -} - -func (s *PublicBlockChainAPI) doCall(args CallArgs, blockNr rpc.BlockNumber) (string, *big.Int, error) { - // Fetch the state associated with the block number - stateDb, block, err := stateAndBlockByNumber(s.miner, s.bc, blockNr, s.chainDb) - if stateDb == nil || err != nil { - return "0x", nil, err - } - stateDb = stateDb.Copy() - - // Retrieve the account state object to interact with - var from *state.StateObject - if args.From == (common.Address{}) { - accounts := s.am.Accounts() - if len(accounts) == 0 { - from = stateDb.GetOrNewStateObject(common.Address{}) - } else { - from = stateDb.GetOrNewStateObject(accounts[0].Address) - } - } else { - from = stateDb.GetOrNewStateObject(args.From) - } - from.SetBalance(common.MaxBig) - - // Assemble the CALL invocation - msg := callmsg{ - from: from, - to: args.To, - gas: args.Gas.BigInt(), - gasPrice: args.GasPrice.BigInt(), - value: args.Value.BigInt(), - data: common.FromHex(args.Data), - } - if msg.gas == nil { - msg.gas = big.NewInt(50000000) - } - if msg.gasPrice == nil { - msg.gasPrice = s.gpo.SuggestPrice() - } - - // Execute the call and return - vmenv := core.NewEnv(stateDb, s.config, s.bc, msg, block.Header(), s.config.VmConfig) - gp := new(core.GasPool).AddGas(common.MaxBig) - - res, requiredGas, _, err := core.NewStateTransition(vmenv, msg, gp).TransitionDb() - if len(res) == 0 { // backwards compatibility - return "0x", requiredGas, err - } - return common.ToHex(res), requiredGas, err -} - -// Call executes the given transaction on the state for the given block number. -// It doesn't make and changes in the state/blockchain and is useful to execute and retrieve values. -func (s *PublicBlockChainAPI) Call(args CallArgs, blockNr rpc.BlockNumber) (string, error) { - result, _, err := s.doCall(args, blockNr) - return result, err -} - -// EstimateGas returns an estimate of the amount of gas needed to execute the given transaction. -func (s *PublicBlockChainAPI) EstimateGas(args CallArgs) (*rpc.HexNumber, error) { - _, gas, err := s.doCall(args, rpc.PendingBlockNumber) - return rpc.NewHexNumber(gas), err -} - -// rpcOutputBlock 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 (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) { - fields := map[string]interface{}{ - "number": rpc.NewHexNumber(b.Number()), - "hash": b.Hash(), - "parentHash": b.ParentHash(), - "nonce": b.Header().Nonce, - "sha3Uncles": b.UncleHash(), - "logsBloom": b.Bloom(), - "stateRoot": b.Root(), - "miner": b.Coinbase(), - "difficulty": rpc.NewHexNumber(b.Difficulty()), - "totalDifficulty": rpc.NewHexNumber(s.bc.GetTd(b.Hash(), b.NumberU64())), - "extraData": fmt.Sprintf("0x%x", b.Extra()), - "size": rpc.NewHexNumber(b.Size().Int64()), - "gasLimit": rpc.NewHexNumber(b.GasLimit()), - "gasUsed": rpc.NewHexNumber(b.GasUsed()), - "timestamp": rpc.NewHexNumber(b.Time()), - "transactionsRoot": b.TxHash(), - "receiptRoot": b.ReceiptHash(), - } - - if inclTx { - formatTx := func(tx *types.Transaction) (interface{}, error) { - return tx.Hash(), nil - } - - if fullTx { - formatTx = func(tx *types.Transaction) (interface{}, error) { - return newRPCTransaction(b, tx.Hash()) - } - } - - txs := b.Transactions() - transactions := make([]interface{}, len(txs)) - var err error - for i, tx := range b.Transactions() { - 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 -} - -// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction -type RPCTransaction struct { - BlockHash common.Hash `json:"blockHash"` - BlockNumber *rpc.HexNumber `json:"blockNumber"` - From common.Address `json:"from"` - Gas *rpc.HexNumber `json:"gas"` - GasPrice *rpc.HexNumber `json:"gasPrice"` - Hash common.Hash `json:"hash"` - Input string `json:"input"` - Nonce *rpc.HexNumber `json:"nonce"` - To *common.Address `json:"to"` - TransactionIndex *rpc.HexNumber `json:"transactionIndex"` - Value *rpc.HexNumber `json:"value"` -} - -// newRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation -func newRPCPendingTransaction(tx *types.Transaction) *RPCTransaction { - from, _ := tx.FromFrontier() - - return &RPCTransaction{ - From: from, - Gas: rpc.NewHexNumber(tx.Gas()), - GasPrice: rpc.NewHexNumber(tx.GasPrice()), - Hash: tx.Hash(), - Input: fmt.Sprintf("0x%x", tx.Data()), - Nonce: rpc.NewHexNumber(tx.Nonce()), - To: tx.To(), - Value: rpc.NewHexNumber(tx.Value()), - } -} - -// newRPCTransaction returns a transaction that will serialize to the RPC representation. -func newRPCTransactionFromBlockIndex(b *types.Block, txIndex int) (*RPCTransaction, error) { - if txIndex >= 0 && txIndex < len(b.Transactions()) { - tx := b.Transactions()[txIndex] - from, err := tx.FromFrontier() - if err != nil { - return nil, err - } - - return &RPCTransaction{ - BlockHash: b.Hash(), - BlockNumber: rpc.NewHexNumber(b.Number()), - From: from, - Gas: rpc.NewHexNumber(tx.Gas()), - GasPrice: rpc.NewHexNumber(tx.GasPrice()), - Hash: tx.Hash(), - Input: fmt.Sprintf("0x%x", tx.Data()), - Nonce: rpc.NewHexNumber(tx.Nonce()), - To: tx.To(), - TransactionIndex: rpc.NewHexNumber(txIndex), - Value: rpc.NewHexNumber(tx.Value()), - }, nil - } - - return nil, nil -} - -// newRPCTransaction returns a transaction that will serialize to the RPC representation. -func newRPCTransaction(b *types.Block, txHash common.Hash) (*RPCTransaction, error) { - for idx, tx := range b.Transactions() { - if tx.Hash() == txHash { - return newRPCTransactionFromBlockIndex(b, idx) - } - } - - return nil, nil -} - -// PublicTransactionPoolAPI exposes methods for the RPC interface -type PublicTransactionPoolAPI struct { - eventMux *event.TypeMux - chainDb ethdb.Database - gpo *GasPriceOracle - bc *core.BlockChain - miner *miner.Miner - am *accounts.Manager - txPool *core.TxPool - txMu *sync.Mutex - muPendingTxSubs sync.Mutex - pendingTxSubs map[string]rpc.Subscription -} - -// NewPublicTransactionPoolAPI creates a new RPC service with methods specific for the transaction pool. -func NewPublicTransactionPoolAPI(e *Ethereum) *PublicTransactionPoolAPI { - api := &PublicTransactionPoolAPI{ - eventMux: e.eventMux, - gpo: e.gpo, - chainDb: e.chainDb, - bc: e.blockchain, - am: e.accountManager, - txPool: e.txPool, - txMu: &e.txMu, - miner: e.miner, - pendingTxSubs: make(map[string]rpc.Subscription), - } - go api.subscriptionLoop() - - return api -} - -// subscriptionLoop listens for events on the global event mux and creates notifications for subscriptions. -func (s *PublicTransactionPoolAPI) subscriptionLoop() { - sub := s.eventMux.Subscribe(core.TxPreEvent{}) - for event := range sub.Chan() { - tx := event.Data.(core.TxPreEvent) - if from, err := tx.Tx.FromFrontier(); err == nil { - if s.am.HasAddress(from) { - s.muPendingTxSubs.Lock() - for id, sub := range s.pendingTxSubs { - if sub.Notify(tx.Tx.Hash()) == rpc.ErrNotificationNotFound { - delete(s.pendingTxSubs, id) - } - } - s.muPendingTxSubs.Unlock() - } - } - } -} - -func getTransaction(chainDb ethdb.Database, txPool *core.TxPool, txHash common.Hash) (*types.Transaction, bool, error) { - txData, err := chainDb.Get(txHash.Bytes()) - isPending := false - tx := new(types.Transaction) - - if err == nil && len(txData) > 0 { - if err := rlp.DecodeBytes(txData, tx); err != nil { - return nil, isPending, err - } - } else { - // pending transaction? - tx = txPool.GetTransaction(txHash) - isPending = true - } - - return tx, isPending, nil -} - -// GetBlockTransactionCountByNumber returns the number of transactions in the block with the given block number. -func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByNumber(blockNr rpc.BlockNumber) *rpc.HexNumber { - if block := blockByNumber(s.miner, s.bc, blockNr); block != nil { - return rpc.NewHexNumber(len(block.Transactions())) - } - return nil -} - -// GetBlockTransactionCountByHash returns the number of transactions in the block with the given hash. -func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByHash(blockHash common.Hash) *rpc.HexNumber { - if block := s.bc.GetBlockByHash(blockHash); block != nil { - return rpc.NewHexNumber(len(block.Transactions())) - } - return nil -} - -// GetTransactionByBlockNumberAndIndex returns the transaction for the given block number and index. -func (s *PublicTransactionPoolAPI) GetTransactionByBlockNumberAndIndex(blockNr rpc.BlockNumber, index rpc.HexNumber) (*RPCTransaction, error) { - if block := blockByNumber(s.miner, s.bc, blockNr); block != nil { - return newRPCTransactionFromBlockIndex(block, index.Int()) - } - return nil, nil -} - -// GetTransactionByBlockHashAndIndex returns the transaction for the given block hash and index. -func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(blockHash common.Hash, index rpc.HexNumber) (*RPCTransaction, error) { - if block := s.bc.GetBlockByHash(blockHash); block != nil { - return newRPCTransactionFromBlockIndex(block, index.Int()) - } - return nil, nil -} - -// GetTransactionCount returns the number of transactions the given address has sent for the given block number -func (s *PublicTransactionPoolAPI) GetTransactionCount(address common.Address, blockNr rpc.BlockNumber) (*rpc.HexNumber, error) { - state, _, err := stateAndBlockByNumber(s.miner, s.bc, blockNr, s.chainDb) - if state == nil || err != nil { - return nil, err - } - return rpc.NewHexNumber(state.GetNonce(address)), nil -} - -// getTransactionBlockData fetches the meta data for the given transaction from the chain database. This is useful to -// retrieve block information for a hash. It returns the block hash, block index and transaction index. -func getTransactionBlockData(chainDb ethdb.Database, txHash common.Hash) (common.Hash, uint64, uint64, error) { - var txBlock struct { - BlockHash common.Hash - BlockIndex uint64 - Index uint64 - } - - blockData, err := chainDb.Get(append(txHash.Bytes(), 0x0001)) - if err != nil { - return common.Hash{}, uint64(0), uint64(0), err - } - - reader := bytes.NewReader(blockData) - if err = rlp.Decode(reader, &txBlock); err != nil { - return common.Hash{}, uint64(0), uint64(0), err - } - - return txBlock.BlockHash, txBlock.BlockIndex, txBlock.Index, nil -} - -// GetTransactionByHash returns the transaction for the given hash -func (s *PublicTransactionPoolAPI) GetTransactionByHash(txHash common.Hash) (*RPCTransaction, error) { - var tx *types.Transaction - var isPending bool - var err error - - if tx, isPending, err = getTransaction(s.chainDb, s.txPool, txHash); err != nil { - glog.V(logger.Debug).Infof("%v\n", err) - return nil, nil - } else if tx == nil { - return nil, nil - } - - if isPending { - return newRPCPendingTransaction(tx), nil - } - - blockHash, _, _, err := getTransactionBlockData(s.chainDb, txHash) - if err != nil { - glog.V(logger.Debug).Infof("%v\n", err) - return nil, nil - } - - if block := s.bc.GetBlockByHash(blockHash); block != nil { - return newRPCTransaction(block, txHash) - } - - return nil, nil -} - -// GetTransactionReceipt returns the transaction receipt for the given transaction hash. -func (s *PublicTransactionPoolAPI) GetTransactionReceipt(txHash common.Hash) (map[string]interface{}, error) { - receipt := core.GetReceipt(s.chainDb, txHash) - if receipt == nil { - glog.V(logger.Debug).Infof("receipt not found for transaction %s", txHash.Hex()) - return nil, nil - } - - tx, _, err := getTransaction(s.chainDb, s.txPool, txHash) - if err != nil { - glog.V(logger.Debug).Infof("%v\n", err) - return nil, nil - } - - txBlock, blockIndex, index, err := getTransactionBlockData(s.chainDb, txHash) - if err != nil { - glog.V(logger.Debug).Infof("%v\n", err) - return nil, nil - } - - from, err := tx.FromFrontier() - if err != nil { - glog.V(logger.Debug).Infof("%v\n", err) - return nil, nil - } - - fields := map[string]interface{}{ - "root": common.Bytes2Hex(receipt.PostState), - "blockHash": txBlock, - "blockNumber": rpc.NewHexNumber(blockIndex), - "transactionHash": txHash, - "transactionIndex": rpc.NewHexNumber(index), - "from": from, - "to": tx.To(), - "gasUsed": rpc.NewHexNumber(receipt.GasUsed), - "cumulativeGasUsed": rpc.NewHexNumber(receipt.CumulativeGasUsed), - "contractAddress": nil, - "logs": receipt.Logs, - } - - if receipt.Logs == nil { - fields["logs"] = []vm.Logs{} - } - - // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation - if bytes.Compare(receipt.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 { - fields["contractAddress"] = receipt.ContractAddress - } - - return fields, nil -} - -// sign is a helper function that signs a transaction with the private key of the given address. -func (s *PublicTransactionPoolAPI) sign(addr common.Address, tx *types.Transaction) (*types.Transaction, error) { - signature, err := s.am.Sign(addr, tx.SigHash().Bytes()) - if err != nil { - return nil, err - } - return tx.WithSignature(signature) -} - -// SendTxArgs represents the arguments to sumbit a new transaction into the transaction pool. -type SendTxArgs struct { - From common.Address `json:"from"` - To *common.Address `json:"to"` - Gas *rpc.HexNumber `json:"gas"` - GasPrice *rpc.HexNumber `json:"gasPrice"` - Value *rpc.HexNumber `json:"value"` - Data string `json:"data"` - Nonce *rpc.HexNumber `json:"nonce"` -} - -// prepareSendTxArgs is a helper function that fills in default values for unspecified tx fields. -func prepareSendTxArgs(args SendTxArgs, gpo *GasPriceOracle) SendTxArgs { - if args.Gas == nil { - args.Gas = rpc.NewHexNumber(defaultGas) - } - if args.GasPrice == nil { - args.GasPrice = rpc.NewHexNumber(gpo.SuggestPrice()) - } - if args.Value == nil { - args.Value = rpc.NewHexNumber(0) - } - return args -} - -// submitTransaction is a helper function that submits tx to txPool and creates a log entry. -func submitTransaction(txPool *core.TxPool, tx *types.Transaction, signature []byte) (common.Hash, error) { - signedTx, err := tx.WithSignature(signature) - if err != nil { - return common.Hash{}, err - } - - txPool.SetLocal(signedTx) - if err := txPool.Add(signedTx); err != nil { - return common.Hash{}, err - } - - if signedTx.To() == nil { - from, _ := signedTx.From() - addr := crypto.CreateAddress(from, signedTx.Nonce()) - glog.V(logger.Info).Infof("Tx(%s) created: %s\n", signedTx.Hash().Hex(), addr.Hex()) - } else { - glog.V(logger.Info).Infof("Tx(%s) to: %s\n", signedTx.Hash().Hex(), tx.To().Hex()) - } - - return signedTx.Hash(), nil -} - -// SendTransaction creates a transaction for the given argument, sign it and submit it to the -// transaction pool. -func (s *PublicTransactionPoolAPI) SendTransaction(args SendTxArgs) (common.Hash, error) { - args = prepareSendTxArgs(args, s.gpo) - - s.txMu.Lock() - defer s.txMu.Unlock() - - if args.Nonce == nil { - args.Nonce = rpc.NewHexNumber(s.txPool.State().GetNonce(args.From)) - } - - var tx *types.Transaction - if args.To == nil { - tx = types.NewContractCreation(args.Nonce.Uint64(), args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) - } else { - tx = types.NewTransaction(args.Nonce.Uint64(), *args.To, args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) - } - - signature, err := s.am.Sign(args.From, tx.SigHash().Bytes()) - if err != nil { - return common.Hash{}, err - } - - return submitTransaction(s.txPool, tx, signature) -} - -// SendRawTransaction will add the signed transaction to the transaction pool. -// The sender is responsible for signing the transaction and using the correct nonce. -func (s *PublicTransactionPoolAPI) SendRawTransaction(encodedTx string) (string, error) { - tx := new(types.Transaction) - if err := rlp.DecodeBytes(common.FromHex(encodedTx), tx); err != nil { - return "", err - } - - s.txPool.SetLocal(tx) - if err := s.txPool.Add(tx); err != nil { - return "", err - } - - if tx.To() == nil { - from, err := tx.FromFrontier() - if err != nil { - return "", err - } - addr := crypto.CreateAddress(from, tx.Nonce()) - glog.V(logger.Info).Infof("Tx(%x) created: %x\n", tx.Hash(), addr) - } else { - glog.V(logger.Info).Infof("Tx(%x) to: %x\n", tx.Hash(), tx.To()) - } - - return tx.Hash().Hex(), nil -} - -// 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(addr common.Address, hash common.Hash) (string, error) { - signature, error := s.am.Sign(addr, hash[:]) - return common.ToHex(signature), error -} - -// SignTransactionArgs represents the arguments to sign a transaction. -type SignTransactionArgs struct { - From common.Address - To *common.Address - Nonce *rpc.HexNumber - Value *rpc.HexNumber - Gas *rpc.HexNumber - GasPrice *rpc.HexNumber - Data string - - BlockNumber int64 -} - -// Tx is a helper object for argument and return values -type Tx struct { - tx *types.Transaction - - To *common.Address `json:"to"` - From common.Address `json:"from"` - Nonce *rpc.HexNumber `json:"nonce"` - Value *rpc.HexNumber `json:"value"` - Data string `json:"data"` - GasLimit *rpc.HexNumber `json:"gas"` - GasPrice *rpc.HexNumber `json:"gasPrice"` - Hash common.Hash `json:"hash"` -} - -// UnmarshalJSON parses JSON data into tx. -func (tx *Tx) UnmarshalJSON(b []byte) (err error) { - req := struct { - To *common.Address `json:"to"` - From common.Address `json:"from"` - Nonce *rpc.HexNumber `json:"nonce"` - Value *rpc.HexNumber `json:"value"` - Data string `json:"data"` - GasLimit *rpc.HexNumber `json:"gas"` - GasPrice *rpc.HexNumber `json:"gasPrice"` - Hash common.Hash `json:"hash"` - }{} - - if err := json.Unmarshal(b, &req); err != nil { - return err - } - - tx.To = req.To - tx.From = req.From - tx.Nonce = req.Nonce - tx.Value = req.Value - tx.Data = req.Data - tx.GasLimit = req.GasLimit - tx.GasPrice = req.GasPrice - tx.Hash = req.Hash - - data := common.Hex2Bytes(tx.Data) - - if tx.Nonce == nil { - return fmt.Errorf("need nonce") - } - if tx.Value == nil { - tx.Value = rpc.NewHexNumber(0) - } - if tx.GasLimit == nil { - tx.GasLimit = rpc.NewHexNumber(0) - } - if tx.GasPrice == nil { - tx.GasPrice = rpc.NewHexNumber(int64(50000000000)) - } - - if req.To == nil { - tx.tx = types.NewContractCreation(tx.Nonce.Uint64(), tx.Value.BigInt(), tx.GasLimit.BigInt(), tx.GasPrice.BigInt(), data) - } else { - tx.tx = types.NewTransaction(tx.Nonce.Uint64(), *tx.To, tx.Value.BigInt(), tx.GasLimit.BigInt(), tx.GasPrice.BigInt(), data) - } - - return nil -} - -// SignTransactionResult represents a RLP encoded signed transaction. -type SignTransactionResult struct { - Raw string `json:"raw"` - Tx *Tx `json:"tx"` -} - -func newTx(t *types.Transaction) *Tx { - from, _ := t.FromFrontier() - return &Tx{ - tx: t, - To: t.To(), - From: from, - Value: rpc.NewHexNumber(t.Value()), - Nonce: rpc.NewHexNumber(t.Nonce()), - Data: "0x" + common.Bytes2Hex(t.Data()), - GasLimit: rpc.NewHexNumber(t.Gas()), - GasPrice: rpc.NewHexNumber(t.GasPrice()), - Hash: t.Hash(), - } -} - -// SignTransaction will sign the given transaction with the from account. -// The node needs to have the private key of the account corresponding with -// the given from address and it needs to be unlocked. -func (s *PublicTransactionPoolAPI) SignTransaction(args SignTransactionArgs) (*SignTransactionResult, error) { - if args.Gas == nil { - args.Gas = rpc.NewHexNumber(defaultGas) - } - if args.GasPrice == nil { - args.GasPrice = rpc.NewHexNumber(s.gpo.SuggestPrice()) - } - if args.Value == nil { - args.Value = rpc.NewHexNumber(0) - } - - s.txMu.Lock() - defer s.txMu.Unlock() - - if args.Nonce == nil { - args.Nonce = rpc.NewHexNumber(s.txPool.State().GetNonce(args.From)) - } - - var tx *types.Transaction - if args.To == nil { - tx = types.NewContractCreation(args.Nonce.Uint64(), args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) - } else { - tx = types.NewTransaction(args.Nonce.Uint64(), *args.To, args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) - } - - signedTx, err := s.sign(args.From, tx) - if err != nil { - return nil, err - } - - data, err := rlp.EncodeToBytes(signedTx) - if err != nil { - return nil, err - } - - return &SignTransactionResult{"0x" + common.Bytes2Hex(data), newTx(signedTx)}, nil -} - -// PendingTransactions returns the transactions that are in the transaction pool and have a from address that is one of -// the accounts this node manages. -func (s *PublicTransactionPoolAPI) PendingTransactions() []*RPCTransaction { - pending := s.txPool.GetTransactions() - transactions := make([]*RPCTransaction, 0, len(pending)) - for _, tx := range pending { - from, _ := tx.FromFrontier() - if s.am.HasAddress(from) { - transactions = append(transactions, newRPCPendingTransaction(tx)) - } - } - return transactions -} - -// NewPendingTransactions creates a subscription that is triggered each time a transaction enters the transaction pool -// and is send from one of the transactions this nodes manages. -func (s *PublicTransactionPoolAPI) NewPendingTransactions(ctx context.Context) (rpc.Subscription, error) { - notifier, supported := rpc.NotifierFromContext(ctx) - if !supported { - return nil, rpc.ErrNotificationsUnsupported - } - - subscription, err := notifier.NewSubscription(func(id string) { - s.muPendingTxSubs.Lock() - delete(s.pendingTxSubs, id) - s.muPendingTxSubs.Unlock() - }) - - if err != nil { - return nil, err - } - - s.muPendingTxSubs.Lock() - s.pendingTxSubs[subscription.ID()] = subscription - s.muPendingTxSubs.Unlock() - - return subscription, nil -} - -// Resend accepts an existing transaction and a new gas price and limit. It will remove the given transaction from the -// pool and reinsert it with the new gas price and limit. -func (s *PublicTransactionPoolAPI) Resend(tx Tx, gasPrice, gasLimit *rpc.HexNumber) (common.Hash, error) { - - pending := s.txPool.GetTransactions() - for _, p := range pending { - if pFrom, err := p.FromFrontier(); err == nil && pFrom == tx.From && p.SigHash() == tx.tx.SigHash() { - if gasPrice == nil { - gasPrice = rpc.NewHexNumber(tx.tx.GasPrice()) - } - if gasLimit == nil { - gasLimit = rpc.NewHexNumber(tx.tx.Gas()) - } - - var newTx *types.Transaction - if tx.tx.To() == nil { - newTx = types.NewContractCreation(tx.tx.Nonce(), tx.tx.Value(), gasPrice.BigInt(), gasLimit.BigInt(), tx.tx.Data()) - } else { - newTx = types.NewTransaction(tx.tx.Nonce(), *tx.tx.To(), tx.tx.Value(), gasPrice.BigInt(), gasLimit.BigInt(), tx.tx.Data()) - } - - signedTx, err := s.sign(tx.From, newTx) - if err != nil { - return common.Hash{}, err - } - - s.txPool.RemoveTx(tx.Hash) - if err = s.txPool.Add(signedTx); err != nil { - return common.Hash{}, err - } - - return signedTx.Hash(), nil - } - } - - return common.Hash{}, fmt.Errorf("Transaction %#x not found", tx.Hash) -} - -// PrivateAdminAPI is the collection of Etheruem APIs exposed over the private -// admin endpoint. -type PrivateAdminAPI struct { - eth *Ethereum -} - -// NewPrivateAdminAPI creates a new API definition for the private admin methods -// of the Ethereum service. -func NewPrivateAdminAPI(eth *Ethereum) *PrivateAdminAPI { - return &PrivateAdminAPI{eth: eth} -} - -// SetSolc sets the Solidity compiler path to be used by the node. -func (api *PrivateAdminAPI) SetSolc(path string) (string, error) { - solc, err := api.eth.SetSolc(path) - if err != nil { - return "", err - } - return solc.Info(), nil +// NewPrivateAdminAPI creates a new API definition for the full node private +// admin methods of the Ethereum service. +func NewPrivateFullAdminAPI(eth *FullNodeService) *PrivateFullAdminAPI { + return &PrivateFullAdminAPI{eth: eth} } // ExportChain exports the current blockchain into a local file. -func (api *PrivateAdminAPI) ExportChain(file string) (bool, error) { +func (api *PrivateFullAdminAPI) ExportChain(file string) (bool, error) { // Make sure we can create the file to export into out, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) if err != nil { @@ -1521,7 +230,7 @@ func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool { } // ImportChain imports a blockchain from a local file. -func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) { +func (api *PrivateFullAdminAPI) ImportChain(file string) (bool, error) { // Make sure the can access the file to import in, err := os.Open(file) if err != nil { @@ -1562,20 +271,20 @@ func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) { return true, nil } -// PublicDebugAPI is the collection of Etheruem APIs exposed over the public -// debugging endpoint. -type PublicDebugAPI struct { - eth *Ethereum +// PublicFullDebugAPI is the collection of Etheruem full node APIs exposed +// over the public debugging endpoint. +type PublicFullDebugAPI struct { + eth *FullNodeService } -// NewPublicDebugAPI creates a new API definition for the public debug methods -// of the Ethereum service. -func NewPublicDebugAPI(eth *Ethereum) *PublicDebugAPI { - return &PublicDebugAPI{eth: eth} +// NewPublicFullDebugAPI creates a new API definition for the full node- +// related public debug methods of the Ethereum service. +func NewPublicFullDebugAPI(eth *FullNodeService) *PublicFullDebugAPI { + return &PublicFullDebugAPI{eth: eth} } // DumpBlock retrieves the entire state of the database at a given block. -func (api *PublicDebugAPI) DumpBlock(number uint64) (state.World, error) { +func (api *PublicFullDebugAPI) DumpBlock(number uint64) (state.World, error) { block := api.eth.BlockChain().GetBlockByNumber(number) if block == nil { return state.World{}, fmt.Errorf("block #%d not found", number) @@ -1587,81 +296,30 @@ func (api *PublicDebugAPI) DumpBlock(number uint64) (state.World, error) { return stateDb.RawDump(), nil } -// GetBlockRlp retrieves the RLP encoded for of a single block. -func (api *PublicDebugAPI) GetBlockRlp(number uint64) (string, error) { - block := api.eth.BlockChain().GetBlockByNumber(number) - if block == nil { - return "", fmt.Errorf("block #%d not found", number) - } - encoded, err := rlp.EncodeToBytes(block) - if err != nil { - return "", err - } - return fmt.Sprintf("%x", encoded), nil -} - -// PrintBlock retrieves a block and returns its pretty printed form. -func (api *PublicDebugAPI) PrintBlock(number uint64) (string, error) { - block := api.eth.BlockChain().GetBlockByNumber(number) - if block == nil { - return "", fmt.Errorf("block #%d not found", number) - } - return fmt.Sprintf("%s", block), nil -} - -// SeedHash retrieves the seed hash of a block. -func (api *PublicDebugAPI) SeedHash(number uint64) (string, error) { - block := api.eth.BlockChain().GetBlockByNumber(number) - if block == nil { - return "", fmt.Errorf("block #%d not found", number) - } - hash, err := ethash.GetSeedHash(number) - if err != nil { - return "", err - } - return fmt.Sprintf("0x%x", hash), nil -} - -// PrivateDebugAPI is the collection of Etheruem APIs exposed over the private -// debugging endpoint. -type PrivateDebugAPI struct { +// PrivateFullDebugAPI is the collection of Etheruem full node APIs exposed over +// the private debugging endpoint. +type PrivateFullDebugAPI struct { config *core.ChainConfig - eth *Ethereum + eth *FullNodeService } -// NewPrivateDebugAPI creates a new API definition for the private debug methods -// of the Ethereum service. -func NewPrivateDebugAPI(config *core.ChainConfig, eth *Ethereum) *PrivateDebugAPI { - return &PrivateDebugAPI{config: config, eth: eth} -} - -// ChaindbProperty returns leveldb properties of the chain database. -func (api *PrivateDebugAPI) ChaindbProperty(property string) (string, error) { - ldb, ok := api.eth.chainDb.(interface { - LDB() *leveldb.DB - }) - if !ok { - return "", fmt.Errorf("chaindbProperty does not work for memory databases") - } - if property == "" { - property = "leveldb.stats" - } else if !strings.HasPrefix(property, "leveldb.") { - property = "leveldb." + property - } - return ldb.LDB().GetProperty(property) +// NewPrivateFullDebugAPI creates a new API definition for the full node-related +// private debug methods of the Ethereum service. +func NewPrivateFullDebugAPI(config *core.ChainConfig, eth *FullNodeService) *PrivateFullDebugAPI { + return &PrivateFullDebugAPI{config: config, eth: eth} } // BlockTraceResult is the returned value when replaying a block to check for // consensus results and full VM trace logs for all included transactions. type BlockTraceResult struct { - Validated bool `json:"validated"` - StructLogs []structLogRes `json:"structLogs"` - Error string `json:"error"` + Validated bool `json:"validated"` + StructLogs []ethapi.StructLogRes `json:"structLogs"` + Error string `json:"error"` } // TraceBlock processes the given block's RLP but does not import the block in to // the chain. -func (api *PrivateDebugAPI) TraceBlock(blockRlp []byte, config *vm.Config) BlockTraceResult { +func (api *PrivateFullDebugAPI) TraceBlock(blockRlp []byte, config *vm.Config) BlockTraceResult { var block types.Block err := rlp.Decode(bytes.NewReader(blockRlp), &block) if err != nil { @@ -1671,14 +329,14 @@ func (api *PrivateDebugAPI) TraceBlock(blockRlp []byte, config *vm.Config) Block validated, logs, err := api.traceBlock(&block, config) return BlockTraceResult{ Validated: validated, - StructLogs: formatLogs(logs), + StructLogs: ethapi.FormatLogs(logs), Error: formatError(err), } } // TraceBlockFromFile loads the block's RLP from the given file name and attempts to // process it but does not import the block in to the chain. -func (api *PrivateDebugAPI) TraceBlockFromFile(file string, config *vm.Config) BlockTraceResult { +func (api *PrivateFullDebugAPI) TraceBlockFromFile(file string, config *vm.Config) BlockTraceResult { blockRlp, err := ioutil.ReadFile(file) if err != nil { return BlockTraceResult{Error: fmt.Sprintf("could not read file: %v", err)} @@ -1687,7 +345,7 @@ func (api *PrivateDebugAPI) TraceBlockFromFile(file string, config *vm.Config) B } // TraceBlockByNumber processes the block by canonical block number. -func (api *PrivateDebugAPI) TraceBlockByNumber(number uint64, config *vm.Config) BlockTraceResult { +func (api *PrivateFullDebugAPI) TraceBlockByNumber(number uint64, config *vm.Config) BlockTraceResult { // Fetch the block that we aim to reprocess block := api.eth.BlockChain().GetBlockByNumber(number) if block == nil { @@ -1697,13 +355,13 @@ func (api *PrivateDebugAPI) TraceBlockByNumber(number uint64, config *vm.Config) validated, logs, err := api.traceBlock(block, config) return BlockTraceResult{ Validated: validated, - StructLogs: formatLogs(logs), + StructLogs: ethapi.FormatLogs(logs), Error: formatError(err), } } // TraceBlockByHash processes the block by hash. -func (api *PrivateDebugAPI) TraceBlockByHash(hash common.Hash, config *vm.Config) BlockTraceResult { +func (api *PrivateFullDebugAPI) TraceBlockByHash(hash common.Hash, config *vm.Config) BlockTraceResult { // Fetch the block that we aim to reprocess block := api.eth.BlockChain().GetBlockByHash(hash) if block == nil { @@ -1713,7 +371,7 @@ func (api *PrivateDebugAPI) TraceBlockByHash(hash common.Hash, config *vm.Config validated, logs, err := api.traceBlock(block, config) return BlockTraceResult{ Validated: validated, - StructLogs: formatLogs(logs), + StructLogs: ethapi.FormatLogs(logs), Error: formatError(err), } } @@ -1731,7 +389,7 @@ func (t *TraceCollector) AddStructLog(slog vm.StructLog) { } // traceBlock processes the given block but does not save the state. -func (api *PrivateDebugAPI) traceBlock(block *types.Block, config *vm.Config) (bool, []vm.StructLog, error) { +func (api *PrivateFullDebugAPI) traceBlock(block *types.Block, config *vm.Config) (bool, []vm.StructLog, error) { // Validate and reprocess the block var ( blockchain = api.eth.BlockChain() @@ -1763,63 +421,25 @@ func (api *PrivateDebugAPI) traceBlock(block *types.Block, config *vm.Config) (b return true, collector.traces, nil } -// SetHead rewinds the head of the blockchain to a previous block. -func (api *PrivateDebugAPI) SetHead(number uint64) { - api.eth.BlockChain().SetHead(number) +// callmsg is the message type used for call transations. +type callmsg struct { + addr common.Address + nonce uint64 + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte } -// ExecutionResult groups all structured logs emitted by the EVM -// while replaying a transaction in debug mode as well as the amount of -// gas used and the return value -type ExecutionResult struct { - Gas *big.Int `json:"gas"` - ReturnValue string `json:"returnValue"` - StructLogs []structLogRes `json:"structLogs"` -} - -// structLogRes stores a structured log emitted by the EVM while replaying a -// transaction in debug mode -type structLogRes struct { - Pc uint64 `json:"pc"` - Op string `json:"op"` - Gas *big.Int `json:"gas"` - GasCost *big.Int `json:"gasCost"` - Depth int `json:"depth"` - Error string `json:"error"` - Stack []string `json:"stack"` - Memory []string `json:"memory"` - Storage map[string]string `json:"storage"` -} - -// formatLogs formats EVM returned structured logs for json output -func formatLogs(structLogs []vm.StructLog) []structLogRes { - formattedStructLogs := make([]structLogRes, len(structLogs)) - for index, trace := range structLogs { - formattedStructLogs[index] = structLogRes{ - Pc: trace.Pc, - Op: trace.Op.String(), - Gas: trace.Gas, - GasCost: trace.GasCost, - Depth: trace.Depth, - Error: formatError(trace.Err), - Stack: make([]string, len(trace.Stack)), - Storage: make(map[string]string), - } - - for i, stackValue := range trace.Stack { - formattedStructLogs[index].Stack[i] = fmt.Sprintf("%x", common.LeftPadBytes(stackValue.Bytes(), 32)) - } - - for i := 0; i+32 <= len(trace.Memory); i += 32 { - formattedStructLogs[index].Memory = append(formattedStructLogs[index].Memory, fmt.Sprintf("%x", trace.Memory[i:i+32])) - } - - for i, storageValue := range trace.Storage { - formattedStructLogs[index].Storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue) - } - } - return formattedStructLogs -} +// accessor boilerplate to implement core.Message +func (m callmsg) From() (common.Address, error) { return m.addr, nil } +func (m callmsg) FromFrontier() (common.Address, error) { return m.addr, nil } +func (m callmsg) Nonce() uint64 { return m.nonce } +func (m callmsg) To() *common.Address { return m.to } +func (m callmsg) GasPrice() *big.Int { return m.gasPrice } +func (m callmsg) Gas() *big.Int { return m.gas } +func (m callmsg) Value() *big.Int { return m.value } +func (m callmsg) Data() []byte { return m.data } // formatError formats a Go error into either an empty string or the data content // of the error itself. @@ -1832,7 +452,7 @@ func formatError(err error) string { // TraceTransaction returns the structured logs created during the execution of EVM // and returns them as a JSON object. -func (api *PrivateDebugAPI) TraceTransaction(txHash common.Hash, logger *vm.LogConfig) (*ExecutionResult, error) { +func (api *PrivateFullDebugAPI) TraceTransaction(txHash common.Hash, logger *vm.LogConfig) (*ethapi.ExecutionResult, error) { if logger == nil { logger = new(vm.LogConfig) } @@ -1862,7 +482,7 @@ func (api *PrivateDebugAPI) TraceTransaction(txHash common.Hash, logger *vm.LogC return nil, fmt.Errorf("sender retrieval failed: %v", err) } msg := callmsg{ - from: stateDb.GetOrNewStateObject(from), + addr: from, to: tx.To(), gas: tx.Gas(), gasPrice: tx.GasPrice(), @@ -1884,90 +504,11 @@ func (api *PrivateDebugAPI) TraceTransaction(txHash common.Hash, logger *vm.LogC if err != nil { return nil, fmt.Errorf("tracing failed: %v", err) } - return &ExecutionResult{ + return ðapi.ExecutionResult{ Gas: gas, ReturnValue: fmt.Sprintf("%x", ret), - StructLogs: formatLogs(vmenv.StructLogs()), + StructLogs: ethapi.FormatLogs(vmenv.StructLogs()), }, nil } return nil, errors.New("database inconsistency") } - -// TraceCall executes a call and returns the amount of gas, created logs and optionally returned values. -func (s *PublicBlockChainAPI) TraceCall(args CallArgs, blockNr rpc.BlockNumber) (*ExecutionResult, error) { - // Fetch the state associated with the block number - stateDb, block, err := stateAndBlockByNumber(s.miner, s.bc, blockNr, s.chainDb) - if stateDb == nil || err != nil { - return nil, err - } - stateDb = stateDb.Copy() - - // Retrieve the account state object to interact with - var from *state.StateObject - if args.From == (common.Address{}) { - accounts := s.am.Accounts() - if len(accounts) == 0 { - from = stateDb.GetOrNewStateObject(common.Address{}) - } else { - from = stateDb.GetOrNewStateObject(accounts[0].Address) - } - } else { - from = stateDb.GetOrNewStateObject(args.From) - } - from.SetBalance(common.MaxBig) - - // Assemble the CALL invocation - msg := callmsg{ - from: from, - to: args.To, - gas: args.Gas.BigInt(), - gasPrice: args.GasPrice.BigInt(), - value: args.Value.BigInt(), - data: common.FromHex(args.Data), - } - if msg.gas.Cmp(common.Big0) == 0 { - msg.gas = big.NewInt(50000000) - } - if msg.gasPrice.Cmp(common.Big0) == 0 { - msg.gasPrice = new(big.Int).Mul(big.NewInt(50), common.Shannon) - } - - // Execute the call and return - vmenv := core.NewEnv(stateDb, s.config, s.bc, msg, block.Header(), vm.Config{ - Debug: true, - }) - gp := new(core.GasPool).AddGas(common.MaxBig) - - ret, gas, err := core.ApplyMessage(vmenv, msg, gp) - return &ExecutionResult{ - Gas: gas, - ReturnValue: fmt.Sprintf("%x", ret), - StructLogs: formatLogs(vmenv.StructLogs()), - }, nil -} - -// PublicNetAPI offers network related RPC methods -type PublicNetAPI struct { - net *p2p.Server - networkVersion int -} - -// NewPublicNetAPI creates a new net API instance. -func NewPublicNetAPI(net *p2p.Server, networkVersion int) *PublicNetAPI { - return &PublicNetAPI{net, networkVersion} -} - -// Listening returns an indication if the node is listening for network connections. -func (s *PublicNetAPI) Listening() bool { - return true // always listening -} - -// PeerCount returns the number of connected peers -func (s *PublicNetAPI) PeerCount() *rpc.HexNumber { - return rpc.NewHexNumber(s.net.PeerCount()) -} - -// Version returns the current ethereum protocol version. -func (s *PublicNetAPI) Version() string { - return fmt.Sprintf("%d", s.networkVersion) -} diff --git a/eth/api_backend.go b/eth/api_backend.go new file mode 100644 index 0000000000..9dbebb226f --- /dev/null +++ b/eth/api_backend.go @@ -0,0 +1,201 @@ +// 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 . + +package eth + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/eth/gasprice" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/internal/ethapi" + rpc "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" +) + +// EthApiBackend implements ethapi.Backend for full nodes +type EthApiBackend struct { + eth *FullNodeService + gpo *gasprice.GasPriceOracle +} + +func (b *EthApiBackend) SetHead(number uint64) { + b.eth.blockchain.SetHead(number) +} + +func (b *EthApiBackend) HeaderByNumber(blockNr rpc.BlockNumber) *types.Header { + // Pending block is only known by the miner + if blockNr == rpc.PendingBlockNumber { + block, _ := b.eth.miner.Pending() + return block.Header() + } + // Otherwise resolve and return the block + if blockNr == rpc.LatestBlockNumber { + return b.eth.blockchain.CurrentBlock().Header() + } + return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)) +} + +func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { + // Pending block is only known by the miner + if blockNr == rpc.PendingBlockNumber { + block, _ := b.eth.miner.Pending() + return block, nil + } + // Otherwise resolve and return the block + if blockNr == rpc.LatestBlockNumber { + return b.eth.blockchain.CurrentBlock(), nil + } + return b.eth.blockchain.GetBlockByNumber(uint64(blockNr)), nil +} + +func (b *EthApiBackend) StateAndHeaderByNumber(blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) { + // Pending state is only known by the miner + if blockNr == rpc.PendingBlockNumber { + block, state := b.eth.miner.Pending() + return EthApiState{state}, block.Header(), nil + } + // Otherwise resolve the block number and return its state + header := b.HeaderByNumber(blockNr) + if header == nil { + return nil, nil, nil + } + stateDb, err := state.New(header.Root, b.eth.chainDb) + return EthApiState{stateDb}, header, err +} + +func (b *EthApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) { + return b.eth.blockchain.GetBlockByHash(blockHash), nil +} + +func (b *EthApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) { + return core.GetBlockReceipts(b.eth.chainDb, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)), nil +} + +func (b *EthApiBackend) GetTd(blockHash common.Hash) *big.Int { + return b.eth.blockchain.GetTdByHash(blockHash) +} + +func (b *EthApiBackend) GetVMEnv(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header) (vm.Environment, func() error, error) { + stateDb := state.(EthApiState).state.Copy() + addr, _ := msg.From() + from := stateDb.GetOrNewStateObject(addr) + from.SetBalance(common.MaxBig) + vmError := func() error { return nil } + return core.NewEnv(stateDb, b.eth.chainConfig, b.eth.blockchain, msg, header, b.eth.chainConfig.VmConfig), vmError, nil +} + +func (b *EthApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { + b.eth.txMu.Lock() + defer b.eth.txMu.Unlock() + + b.eth.txPool.SetLocal(signedTx) + return b.eth.txPool.Add(signedTx) +} + +func (b *EthApiBackend) RemoveTx(txHash common.Hash) { + b.eth.txMu.Lock() + defer b.eth.txMu.Unlock() + + b.eth.txPool.RemoveTx(txHash) +} + +func (b *EthApiBackend) GetPoolTransactions() types.Transactions { + b.eth.txMu.Lock() + defer b.eth.txMu.Unlock() + + return b.eth.txPool.GetTransactions() +} + +func (b *EthApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction { + b.eth.txMu.Lock() + defer b.eth.txMu.Unlock() + + return b.eth.txPool.GetTransaction(txHash) +} + +func (b *EthApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { + b.eth.txMu.Lock() + defer b.eth.txMu.Unlock() + + return b.eth.txPool.State().GetNonce(addr), nil +} + +func (b *EthApiBackend) Stats() (pending int, queued int) { + b.eth.txMu.Lock() + defer b.eth.txMu.Unlock() + + return b.eth.txPool.Stats() +} + +func (b *EthApiBackend) TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) { + b.eth.txMu.Lock() + defer b.eth.txMu.Unlock() + + return b.eth.TxPool().Content() +} + +func (b *EthApiBackend) Downloader() *downloader.Downloader { + return b.eth.Downloader() +} + +func (b *EthApiBackend) ProtocolVersion() int { + return b.eth.EthVersion() +} + +func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { + return b.gpo.SuggestPrice(), nil +} + +func (b *EthApiBackend) ChainDb() ethdb.Database { + return b.eth.ChainDb() +} + +func (b *EthApiBackend) EventMux() *event.TypeMux { + return b.eth.EventMux() +} + +func (b *EthApiBackend) AccountManager() *accounts.Manager { + return b.eth.AccountManager() +} + +type EthApiState struct { + state *state.StateDB +} + +func (s EthApiState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) { + return s.state.GetBalance(addr), nil +} + +func (s EthApiState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) { + return s.state.GetCode(addr), nil +} + +func (s EthApiState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) { + return s.state.GetState(a, b), nil +} + +func (s EthApiState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) { + return s.state.GetNonce(addr), nil +} diff --git a/eth/backend.go b/eth/backend.go index 006523484d..f0ca38cbd5 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -39,8 +39,10 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/filters" + "github.com/ethereum/go-ethereum/eth/gasprice" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/miner" @@ -101,95 +103,86 @@ type Config struct { TestGenesisState ethdb.Database // Genesis state to seed the database with (testing only!) } -type Ethereum struct { - chainConfig *core.ChainConfig +// FullNodeService implements the Ethereum full node service. +type FullNodeService struct { + chainConfig *core.ChainConfig + // Channel for shutting down the service shutdownChan chan bool // Channel for shutting down the ethereum stopDbUpgrade func() // stop chain db sequential key upgrade - - // DB interfaces - chainDb ethdb.Database // Block chain database - dappDb ethdb.Database // Dapp database - // Handlers txPool *core.TxPool txMu sync.Mutex blockchain *core.BlockChain - accountManager *accounts.Manager - pow *ethash.Ethash protocolManager *ProtocolManager - SolcPath string - solc *compiler.Solidity - gpo *GasPriceOracle + // DB interfaces + chainDb ethdb.Database // Block chain database + dappDb ethdb.Database // Dapp database - GpoMinGasPrice *big.Int - GpoMaxGasPrice *big.Int - GpoFullBlockRatio int - GpobaseStepDown int - GpobaseStepUp int - GpobaseCorrectionFactor int + eventMux *event.TypeMux + pow *ethash.Ethash + httpclient *httpclient.HTTPClient + accountManager *accounts.Manager - httpclient *httpclient.HTTPClient + apiBackend *EthApiBackend - eventMux *event.TypeMux - miner *miner.Miner + miner *miner.Miner + Mining bool + MinerThreads int + AutoDAG bool + autodagquit chan bool + etherbase common.Address + solcPath string + solc *compiler.Solidity - Mining bool - MinerThreads int NatSpec bool - AutoDAG bool PowTest bool - autodagquit chan bool - etherbase common.Address netVersionId int - netRPCService *PublicNetAPI + netRPCService *ethapi.PublicNetAPI } -func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { - // Open the chain database and perform any upgrades needed - chainDb, err := ctx.OpenDatabase("chaindata", config.DatabaseCache, config.DatabaseHandles) +// New creates a new FullNodeService object (including the +// initialisation of the common Ethereum object) +func New(ctx *node.ServiceContext, config *Config) (*FullNodeService, error) { + chainDb, dappDb, err := CreateDBs(ctx, config) if err != nil { return nil, err } - if db, ok := chainDb.(*ethdb.LDBDatabase); ok { - db.Meter("eth/db/chaindata/") + stopDbUpgrade := upgradeSequentialKeys(chainDb) + if err := SetupGenesisBlock(&chainDb, config); err != nil { + return nil, err } + pow, err := CreatePoW(config) + if err != nil { + return nil, err + } + + eth := &FullNodeService{ + chainDb: chainDb, + dappDb: dappDb, + eventMux: ctx.EventMux, + accountManager: config.AccountManager, + pow: pow, + shutdownChan: make(chan bool), + stopDbUpgrade: stopDbUpgrade, + httpclient: httpclient.New(config.DocRoot), + netVersionId: config.NetworkId, + NatSpec: config.NatSpec, + PowTest: config.PowTest, + etherbase: config.Etherbase, + MinerThreads: config.MinerThreads, + AutoDAG: config.AutoDAG, + solcPath: config.SolcPath, + } + if err := upgradeChainDatabase(chainDb); err != nil { return nil, err } if err := addMipmapBloomBins(chainDb); err != nil { return nil, err } - stopDbUpgrade := upgradeSequentialKeys(chainDb) - dappDb, err := ctx.OpenDatabase("dapp", config.DatabaseCache, config.DatabaseHandles) - if err != nil { - return nil, err - } - if db, ok := dappDb.(*ethdb.LDBDatabase); ok { - db.Meter("eth/db/dapp/") - } glog.V(logger.Info).Infof("Protocol Versions: %v, Network Id: %v", ProtocolVersions, config.NetworkId) - // Load up any custom genesis block if requested - if len(config.Genesis) > 0 { - block, err := core.WriteGenesisBlock(chainDb, strings.NewReader(config.Genesis)) - if err != nil { - return nil, err - } - glog.V(logger.Info).Infof("Successfully wrote custom genesis block: %x", block.Hash()) - } - - // Load up a test setup if directly injected - if config.TestGenesisState != nil { - chainDb = config.TestGenesisState - } - if config.TestGenesisBlock != nil { - core.WriteTd(chainDb, config.TestGenesisBlock.Hash(), config.TestGenesisBlock.NumberU64(), config.TestGenesisBlock.Difficulty()) - core.WriteBlock(chainDb, config.TestGenesisBlock) - core.WriteCanonicalHash(chainDb, config.TestGenesisBlock.Hash(), config.TestGenesisBlock.NumberU64()) - core.WriteHeadBlockHash(chainDb, config.TestGenesisBlock.Hash()) - } - if !config.SkipBcVersionCheck { bcVersion := core.GetBlockChainVersion(chainDb) if bcVersion != config.BlockChainVersion && bcVersion != 0 { @@ -197,44 +190,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { } core.WriteBlockChainVersion(chainDb, config.BlockChainVersion) } - glog.V(logger.Info).Infof("Blockchain DB Version: %d", config.BlockChainVersion) - - eth := &Ethereum{ - shutdownChan: make(chan bool), - stopDbUpgrade: stopDbUpgrade, - chainDb: chainDb, - dappDb: dappDb, - eventMux: ctx.EventMux, - accountManager: config.AccountManager, - etherbase: config.Etherbase, - netVersionId: config.NetworkId, - NatSpec: config.NatSpec, - MinerThreads: config.MinerThreads, - SolcPath: config.SolcPath, - AutoDAG: config.AutoDAG, - PowTest: config.PowTest, - GpoMinGasPrice: config.GpoMinGasPrice, - GpoMaxGasPrice: config.GpoMaxGasPrice, - GpoFullBlockRatio: config.GpoFullBlockRatio, - GpobaseStepDown: config.GpobaseStepDown, - GpobaseStepUp: config.GpobaseStepUp, - GpobaseCorrectionFactor: config.GpobaseCorrectionFactor, - httpclient: httpclient.New(config.DocRoot), - } - switch { - case config.PowTest: - glog.V(logger.Info).Infof("ethash used in test mode") - eth.pow, err = ethash.NewForTesting() - if err != nil { - return nil, err - } - case config.PowShared: - glog.V(logger.Info).Infof("ethash used in shared mode") - eth.pow = ethash.NewShared() - - default: - eth.pow = ethash.New() - } // load the genesis block or write a new one if no genesis // block is prenent in the database. @@ -263,8 +218,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { } return nil, err } - eth.gpo = NewGasPriceOracle(eth) - newPool := core.NewTxPool(eth.chainConfig, eth.EventMux(), eth.blockchain.State, eth.blockchain.GasLimit) eth.txPool = newPool @@ -275,37 +228,87 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { eth.miner.SetGasPrice(config.GasPrice) eth.miner.SetExtra(config.ExtraData) + gpoParams := &gasprice.GpoParams{ + GpoMinGasPrice: config.GpoMinGasPrice, + GpoMaxGasPrice: config.GpoMaxGasPrice, + GpoFullBlockRatio: config.GpoFullBlockRatio, + GpobaseStepDown: config.GpobaseStepDown, + GpobaseStepUp: config.GpobaseStepUp, + GpobaseCorrectionFactor: config.GpobaseCorrectionFactor, + } + gpo := gasprice.NewGasPriceOracle(eth.blockchain, chainDb, eth.eventMux, gpoParams) + eth.apiBackend = &EthApiBackend{eth, gpo} + return eth, nil } +// CreateDBs creates the chain and dapp databases for an Ethereum service +func CreateDBs(ctx *node.ServiceContext, config *Config) (chainDb, dappDb ethdb.Database, err error) { + // Open the chain database and perform any upgrades needed + chainDb, err = ctx.OpenDatabase("chaindata", config.DatabaseCache, config.DatabaseHandles) + if err != nil { + return nil, nil, err + } + if db, ok := chainDb.(*ethdb.LDBDatabase); ok { + db.Meter("eth/db/chaindata/") + } + + dappDb, err = ctx.OpenDatabase("dapp", config.DatabaseCache, config.DatabaseHandles) + if err != nil { + return nil, nil, err + } + if db, ok := dappDb.(*ethdb.LDBDatabase); ok { + db.Meter("eth/db/dapp/") + } + return +} + +// SetupGenesisBlock initializes the genesis block for an Ethereum service +func SetupGenesisBlock(chainDb *ethdb.Database, config *Config) error { + // Load up any custom genesis block if requested + if len(config.Genesis) > 0 { + block, err := core.WriteGenesisBlock(*chainDb, strings.NewReader(config.Genesis)) + if err != nil { + return err + } + glog.V(logger.Info).Infof("Successfully wrote custom genesis block: %x", block.Hash()) + } + // Load up a test setup if directly injected + if config.TestGenesisState != nil { + *chainDb = config.TestGenesisState + } + if config.TestGenesisBlock != nil { + core.WriteTd(*chainDb, config.TestGenesisBlock.Hash(), config.TestGenesisBlock.NumberU64(), config.TestGenesisBlock.Difficulty()) + core.WriteBlock(*chainDb, config.TestGenesisBlock) + core.WriteCanonicalHash(*chainDb, config.TestGenesisBlock.Hash(), config.TestGenesisBlock.NumberU64()) + core.WriteHeadBlockHash(*chainDb, config.TestGenesisBlock.Hash()) + } + return nil +} + +// CreatePoW creates the required type of PoW instance for an Ethereum service +func CreatePoW(config *Config) (*ethash.Ethash, error) { + switch { + case config.PowTest: + glog.V(logger.Info).Infof("ethash used in test mode") + return ethash.NewForTesting() + case config.PowShared: + glog.V(logger.Info).Infof("ethash used in shared mode") + return ethash.NewShared(), nil + + default: + return ethash.New(), nil + } +} + // APIs returns the collection of RPC services the ethereum package offers. // NOTE, some of these services probably need to be moved to somewhere else. -func (s *Ethereum) APIs() []rpc.API { - return []rpc.API{ +func (s *FullNodeService) APIs() []rpc.API { + return append(ethapi.GetAPIs(s.apiBackend, &s.solcPath, &s.solc), []rpc.API{ { Namespace: "eth", Version: "1.0", - Service: NewPublicEthereumAPI(s), - Public: true, - }, { - Namespace: "eth", - Version: "1.0", - Service: NewPublicAccountAPI(s.accountManager), - Public: true, - }, { - Namespace: "personal", - Version: "1.0", - Service: NewPrivateAccountAPI(s), - Public: false, - }, { - Namespace: "eth", - Version: "1.0", - Service: NewPublicBlockChainAPI(s.chainConfig, s.blockchain, s.miner, s.chainDb, s.gpo, s.eventMux, s.accountManager), - Public: true, - }, { - Namespace: "eth", - Version: "1.0", - Service: NewPublicTransactionPoolAPI(s), + Service: NewPublicFullEthereumAPI(s), Public: true, }, { Namespace: "eth", @@ -322,11 +325,6 @@ func (s *Ethereum) APIs() []rpc.API { Version: "1.0", Service: NewPrivateMinerAPI(s), Public: false, - }, { - Namespace: "txpool", - Version: "1.0", - Service: NewPublicTxPoolAPI(s), - Public: true, }, { Namespace: "eth", Version: "1.0", @@ -335,16 +333,16 @@ func (s *Ethereum) APIs() []rpc.API { }, { Namespace: "admin", Version: "1.0", - Service: NewPrivateAdminAPI(s), + Service: NewPrivateFullAdminAPI(s), }, { Namespace: "debug", Version: "1.0", - Service: NewPublicDebugAPI(s), + Service: NewPublicFullDebugAPI(s), Public: true, }, { Namespace: "debug", Version: "1.0", - Service: NewPrivateDebugAPI(s.chainConfig, s), + Service: NewPrivateFullDebugAPI(s.chainConfig, s), }, { Namespace: "net", Version: "1.0", @@ -355,14 +353,14 @@ func (s *Ethereum) APIs() []rpc.API { Version: "1.0", Service: ethreg.NewPrivateRegistarAPI(s.chainConfig, s.blockchain, s.chainDb, s.txPool, s.accountManager), }, - } + }...) } -func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) { +func (s *FullNodeService) ResetWithGenesisBlock(gb *types.Block) { s.blockchain.ResetWithGenesisBlock(gb) } -func (s *Ethereum) Etherbase() (eb common.Address, err error) { +func (s *FullNodeService) Etherbase() (eb common.Address, err error) { eb = s.etherbase if (eb == common.Address{}) { firstAccount, err := s.AccountManager().AccountByIndex(0) @@ -375,46 +373,47 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) { } // set in js console via admin interface or wrapper from cli flags -func (self *Ethereum) SetEtherbase(etherbase common.Address) { +func (self *FullNodeService) SetEtherbase(etherbase common.Address) { self.etherbase = etherbase self.miner.SetEtherbase(etherbase) } -func (s *Ethereum) StopMining() { s.miner.Stop() } -func (s *Ethereum) IsMining() bool { return s.miner.Mining() } -func (s *Ethereum) Miner() *miner.Miner { return s.miner } +func (s *FullNodeService) StopMining() { s.miner.Stop() } +func (s *FullNodeService) IsMining() bool { return s.miner.Mining() } +func (s *FullNodeService) Miner() *miner.Miner { return s.miner } -func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager } -func (s *Ethereum) BlockChain() *core.BlockChain { return s.blockchain } -func (s *Ethereum) TxPool() *core.TxPool { return s.txPool } -func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux } -func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb } -func (s *Ethereum) DappDb() ethdb.Database { return s.dappDb } -func (s *Ethereum) IsListening() bool { return true } // Always listening -func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) } -func (s *Ethereum) NetVersion() int { return s.netVersionId } -func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader } +func (s *FullNodeService) AccountManager() *accounts.Manager { return s.accountManager } +func (s *FullNodeService) BlockChain() *core.BlockChain { return s.blockchain } +func (s *FullNodeService) TxPool() *core.TxPool { return s.txPool } +func (s *FullNodeService) EventMux() *event.TypeMux { return s.eventMux } +func (s *FullNodeService) Pow() *ethash.Ethash { return s.pow } +func (s *FullNodeService) ChainDb() ethdb.Database { return s.chainDb } +func (s *FullNodeService) DappDb() ethdb.Database { return s.dappDb } +func (s *FullNodeService) IsListening() bool { return true } // Always listening +func (s *FullNodeService) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) } +func (s *FullNodeService) NetVersion() int { return s.netVersionId } +func (s *FullNodeService) Downloader() *downloader.Downloader { return s.protocolManager.downloader } // Protocols implements node.Service, returning all the currently configured // network protocols to start. -func (s *Ethereum) Protocols() []p2p.Protocol { +func (s *FullNodeService) Protocols() []p2p.Protocol { return s.protocolManager.SubProtocols } // Start implements node.Service, starting all internal goroutines needed by the -// Ethereum protocol implementation. -func (s *Ethereum) Start(srvr *p2p.Server) error { +// FullNodeService protocol implementation. +func (s *FullNodeService) Start(srvr *p2p.Server) error { + s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.NetVersion()) if s.AutoDAG { s.StartAutoDAG() } s.protocolManager.Start() - s.netRPCService = NewPublicNetAPI(srvr, s.NetVersion()) return nil } // Stop implements node.Service, terminating all internal goroutines used by the // Ethereum protocol. -func (s *Ethereum) Stop() error { +func (s *FullNodeService) Stop() error { if s.stopDbUpgrade != nil { s.stopDbUpgrade() } @@ -434,7 +433,7 @@ func (s *Ethereum) Stop() error { } // This function will wait for a shutdown and resumes main thread execution -func (s *Ethereum) WaitForShutdown() { +func (s *FullNodeService) WaitForShutdown() { <-s.shutdownChan } @@ -447,7 +446,7 @@ func (s *Ethereum) WaitForShutdown() { // stop any number of times. // For any more sophisticated pattern of DAG generation, use CLI subcommand // makedag -func (self *Ethereum) StartAutoDAG() { +func (self *FullNodeService) StartAutoDAG() { if self.autodagquit != nil { return // already started } @@ -493,7 +492,7 @@ func (self *Ethereum) StartAutoDAG() { } // stopAutoDAG stops automatic DAG pregeneration by quitting the loop -func (self *Ethereum) StopAutoDAG() { +func (self *FullNodeService) StopAutoDAG() { if self.autodagquit != nil { close(self.autodagquit) self.autodagquit = nil @@ -503,25 +502,10 @@ func (self *Ethereum) StopAutoDAG() { // HTTPClient returns the light http client used for fetching offchain docs // (natspec, source for verification) -func (self *Ethereum) HTTPClient() *httpclient.HTTPClient { +func (self *FullNodeService) HTTPClient() *httpclient.HTTPClient { return self.httpclient } -func (self *Ethereum) Solc() (*compiler.Solidity, error) { - var err error - if self.solc == nil { - self.solc, err = compiler.New(self.SolcPath) - } - return self.solc, err -} - -// set in js console via admin interface or wrapper from cli flags -func (self *Ethereum) SetSolc(solcPath string) (*compiler.Solidity, error) { - self.SolcPath = solcPath - self.solc = nil - return self.Solc() -} - // dagFiles(epoch) returns the two alternative DAG filenames (not a path) // 1) - 2) full-R- func dagFiles(epoch uint64) (string, string) { diff --git a/eth/bind.go b/eth/bind.go index fb7f29f601..7eb15ca1b6 100644 --- a/eth/bind.go +++ b/eth/bind.go @@ -21,8 +21,10 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" ) // ContractBackend implements bind.ContractBackend with direct calls to Ethereum @@ -33,38 +35,44 @@ import ( // object. These should be rewritten to internal Go method calls when the Go API // is refactored to support a clean library use. type ContractBackend struct { - eapi *PublicEthereumAPI // Wrapper around the Ethereum object to access metadata - bcapi *PublicBlockChainAPI // Wrapper around the blockchain to access chain data - txapi *PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data + eapi *ethapi.PublicEthereumAPI // Wrapper around the Ethereum object to access metadata + bcapi *ethapi.PublicBlockChainAPI // Wrapper around the blockchain to access chain data + txapi *ethapi.PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data } // NewContractBackend creates a new native contract backend using an existing // Etheruem object. -func NewContractBackend(eth *Ethereum) *ContractBackend { +func NewContractBackend(eth *FullNodeService) *ContractBackend { return &ContractBackend{ - eapi: NewPublicEthereumAPI(eth), - bcapi: NewPublicBlockChainAPI(eth.chainConfig, eth.blockchain, eth.miner, eth.chainDb, eth.gpo, eth.eventMux, eth.accountManager), - txapi: NewPublicTransactionPoolAPI(eth), + eapi: ethapi.NewPublicEthereumAPI(eth.apiBackend, nil, nil), + bcapi: ethapi.NewPublicBlockChainAPI(eth.apiBackend), + txapi: ethapi.NewPublicTransactionPoolAPI(eth.apiBackend), } } // HasCode implements bind.ContractVerifier.HasCode by retrieving any code associated // with the contract from the local API, and checking its size. -func (b *ContractBackend) HasCode(contract common.Address, pending bool) (bool, error) { +func (b *ContractBackend) HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error) { + if ctx == nil { + ctx = context.Background() + } block := rpc.LatestBlockNumber if pending { block = rpc.PendingBlockNumber } - out, err := b.bcapi.GetCode(contract, block) + out, err := b.bcapi.GetCode(ctx, contract, block) return len(common.FromHex(out)) > 0, err } // ContractCall implements bind.ContractCaller executing an Ethereum contract // call with the specified data as the input. The pending flag requests execution // against the pending block, not the stable head of the chain. -func (b *ContractBackend) ContractCall(contract common.Address, data []byte, pending bool) ([]byte, error) { +func (b *ContractBackend) ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error) { + if ctx == nil { + ctx = context.Background() + } // Convert the input args to the API spec - args := CallArgs{ + args := ethapi.CallArgs{ To: &contract, Data: common.ToHex(data), } @@ -73,21 +81,27 @@ func (b *ContractBackend) ContractCall(contract common.Address, data []byte, pen block = rpc.PendingBlockNumber } // Execute the call and convert the output back to Go types - out, err := b.bcapi.Call(args, block) + out, err := b.bcapi.Call(ctx, args, block) return common.FromHex(out), err } // PendingAccountNonce implements bind.ContractTransactor retrieving the current // pending nonce associated with an account. -func (b *ContractBackend) PendingAccountNonce(account common.Address) (uint64, error) { - out, err := b.txapi.GetTransactionCount(account, rpc.PendingBlockNumber) +func (b *ContractBackend) PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error) { + if ctx == nil { + ctx = context.Background() + } + out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber) return out.Uint64(), err } // SuggestGasPrice implements bind.ContractTransactor retrieving the currently // suggested gas price to allow a timely execution of a transaction. -func (b *ContractBackend) SuggestGasPrice() (*big.Int, error) { - return b.eapi.GasPrice(), nil +func (b *ContractBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) { + if ctx == nil { + ctx = context.Background() + } + return b.eapi.GasPrice(ctx) } // EstimateGasLimit implements bind.ContractTransactor triing to estimate the gas @@ -95,8 +109,11 @@ func (b *ContractBackend) SuggestGasPrice() (*big.Int, error) { // 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. -func (b *ContractBackend) EstimateGasLimit(sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) { - out, err := b.bcapi.EstimateGas(CallArgs{ +func (b *ContractBackend) EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error) { + if ctx == nil { + ctx = context.Background() + } + out, err := b.bcapi.EstimateGas(ctx, ethapi.CallArgs{ From: sender, To: contract, Value: *rpc.NewHexNumber(value), @@ -107,8 +124,11 @@ func (b *ContractBackend) EstimateGasLimit(sender common.Address, contract *comm // SendTransaction implements bind.ContractTransactor injects the transaction // into the pending pool for execution. -func (b *ContractBackend) SendTransaction(tx *types.Transaction) error { +func (b *ContractBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error { + if ctx == nil { + ctx = context.Background() + } raw, _ := rlp.EncodeToBytes(tx) - _, err := b.txapi.SendRawTransaction(common.ToHex(raw)) + _, err := b.txapi.SendRawTransaction(ctx, common.ToHex(raw)) return err } diff --git a/eth/cpu_mining.go b/eth/cpu_mining.go index 3469d394e9..143b9c98a3 100644 --- a/eth/cpu_mining.go +++ b/eth/cpu_mining.go @@ -28,7 +28,7 @@ import ( const disabledInfo = "Set GO_OPENCL and re-build to enable." -func (s *Ethereum) StartMining(threads int, gpus string) error { +func (s *FullNodeService) StartMining(threads int, gpus string) error { eb, err := s.Etherbase() if err != nil { err = fmt.Errorf("Cannot start mining without etherbase address: %v", err) diff --git a/eth/gasprice.go b/eth/gasprice/gasprice.go similarity index 75% rename from eth/gasprice.go rename to eth/gasprice/gasprice.go index ef203f8fef..eb2df4a96e 100644 --- a/eth/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package eth +package gasprice import ( "math/big" @@ -23,6 +23,8 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" ) @@ -39,10 +41,22 @@ type blockPriceInfo struct { baseGasPrice *big.Int } +type GpoParams struct { + GpoMinGasPrice *big.Int + GpoMaxGasPrice *big.Int + GpoFullBlockRatio int + GpobaseStepDown int + GpobaseStepUp int + GpobaseCorrectionFactor int +} + // GasPriceOracle recommends gas prices based on the content of recent // blocks. type GasPriceOracle struct { - eth *Ethereum + chain *core.BlockChain + db ethdb.Database + evmux *event.TypeMux + params *GpoParams initOnce sync.Once minPrice *big.Int lastBaseMutex sync.Mutex @@ -55,17 +69,20 @@ type GasPriceOracle struct { } // NewGasPriceOracle returns a new oracle. -func NewGasPriceOracle(eth *Ethereum) *GasPriceOracle { - minprice := eth.GpoMinGasPrice +func NewGasPriceOracle(chain *core.BlockChain, db ethdb.Database, evmux *event.TypeMux, params *GpoParams) *GasPriceOracle { + minprice := params.GpoMinGasPrice if minprice == nil { minprice = big.NewInt(gpoDefaultMinGasPrice) } minbase := new(big.Int).Mul(minprice, big.NewInt(100)) - if eth.GpobaseCorrectionFactor > 0 { - minbase = minbase.Div(minbase, big.NewInt(int64(eth.GpobaseCorrectionFactor))) + if params.GpobaseCorrectionFactor > 0 { + minbase = minbase.Div(minbase, big.NewInt(int64(params.GpobaseCorrectionFactor))) } return &GasPriceOracle{ - eth: eth, + chain: chain, + db: db, + evmux: evmux, + params: params, blocks: make(map[uint64]*blockPriceInfo), minBase: minbase, minPrice: minprice, @@ -75,14 +92,14 @@ func NewGasPriceOracle(eth *Ethereum) *GasPriceOracle { func (gpo *GasPriceOracle) init() { gpo.initOnce.Do(func() { - gpo.processPastBlocks(gpo.eth.BlockChain()) + gpo.processPastBlocks() go gpo.listenLoop() }) } -func (self *GasPriceOracle) processPastBlocks(chain *core.BlockChain) { +func (self *GasPriceOracle) processPastBlocks() { last := int64(-1) - cblock := chain.CurrentBlock() + cblock := self.chain.CurrentBlock() if cblock != nil { last = int64(cblock.NumberU64()) } @@ -92,7 +109,7 @@ func (self *GasPriceOracle) processPastBlocks(chain *core.BlockChain) { } self.firstProcessed = uint64(first) for i := first; i <= last; i++ { - block := chain.GetBlockByNumber(uint64(i)) + block := self.chain.GetBlockByNumber(uint64(i)) if block != nil { self.processBlock(block) } @@ -101,7 +118,7 @@ func (self *GasPriceOracle) processPastBlocks(chain *core.BlockChain) { } func (self *GasPriceOracle) listenLoop() { - events := self.eth.EventMux().Subscribe(core.ChainEvent{}, core.ChainSplitEvent{}) + events := self.evmux.Subscribe(core.ChainEvent{}, core.ChainSplitEvent{}) defer events.Unsubscribe() for event := range events.Chan() { @@ -136,9 +153,9 @@ func (self *GasPriceOracle) processBlock(block *types.Block) { } if lastBase.Cmp(lp) < 0 { - corr = self.eth.GpobaseStepUp + corr = self.params.GpobaseStepUp } else { - corr = -self.eth.GpobaseStepDown + corr = -self.params.GpobaseStepDown } crand := int64(corr * (900 + rand.Intn(201))) @@ -159,14 +176,14 @@ func (self *GasPriceOracle) processBlock(block *types.Block) { self.lastBase = newBase self.lastBaseMutex.Unlock() - glog.V(logger.Detail).Infof("Processed block #%v, base price is %v\n", block.NumberU64(), newBase.Int64()) + glog.V(logger.Detail).Infof("Processed block #%v, base price is %v\n", i, newBase.Int64()) } // returns the lowers possible price with which a tx was or could have been included func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int { gasUsed := big.NewInt(0) - receipts := core.GetBlockReceipts(self.eth.ChainDb(), block.Hash(), block.NumberU64()) + receipts := core.GetBlockReceipts(self.db, block.Hash(), block.NumberU64()) if len(receipts) > 0 { if cgu := receipts[len(receipts)-1].CumulativeGasUsed; cgu != nil { gasUsed = receipts[len(receipts)-1].CumulativeGasUsed @@ -174,7 +191,7 @@ func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int { } if new(big.Int).Mul(gasUsed, big.NewInt(100)).Cmp(new(big.Int).Mul(block.GasLimit(), - big.NewInt(int64(self.eth.GpoFullBlockRatio)))) < 0 { + big.NewInt(int64(self.params.GpoFullBlockRatio)))) < 0 { // block is not full, could have posted a tx with MinGasPrice return big.NewInt(0) } @@ -201,12 +218,12 @@ func (self *GasPriceOracle) SuggestPrice() *big.Int { price := new(big.Int).Set(self.lastBase) self.lastBaseMutex.Unlock() - price.Mul(price, big.NewInt(int64(self.eth.GpobaseCorrectionFactor))) + price.Mul(price, big.NewInt(int64(self.params.GpobaseCorrectionFactor))) price.Div(price, big.NewInt(100)) if price.Cmp(self.minPrice) < 0 { price.Set(self.minPrice) - } else if self.eth.GpoMaxGasPrice != nil && price.Cmp(self.eth.GpoMaxGasPrice) > 0 { - price.Set(self.eth.GpoMaxGasPrice) + } else if self.params.GpoMaxGasPrice != nil && price.Cmp(self.params.GpoMaxGasPrice) > 0 { + price.Set(self.params.GpoMaxGasPrice) } return price } diff --git a/eth/gpu_mining.go b/eth/gpu_mining.go index 12fa746018..0208bba806 100644 --- a/eth/gpu_mining.go +++ b/eth/gpu_mining.go @@ -33,7 +33,7 @@ import ( "github.com/ethereum/go-ethereum/miner" ) -func (s *Ethereum) StartMining(threads int, gpus string) error { +func (s *FullNodeService) StartMining(threads int, gpus string) error { eb, err := s.Etherbase() if err != nil { err = fmt.Errorf("Cannot start mining without etherbase address: %v", err) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go new file mode 100644 index 0000000000..6e888fc93b --- /dev/null +++ b/internal/ethapi/api.go @@ -0,0 +1,1542 @@ +// 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 . + +package ethapi + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "math/big" + "strings" + "sync" + "time" + + "github.com/ethereum/ethash" + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/compiler" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/rpc" + "github.com/syndtr/goleveldb/leveldb" + "golang.org/x/net/context" +) + +const defaultGas = uint64(90000) + +// PublicEthereumAPI provides an API to access Ethereum related information. +// It offers only methods that operate on public data that is freely available to anyone. +type PublicEthereumAPI struct { + b Backend + solcPath *string + solc **compiler.Solidity +} + +// NewPublicEthereumAPI creates a new Etheruem protocol API. +func NewPublicEthereumAPI(b Backend, solcPath *string, solc **compiler.Solidity) *PublicEthereumAPI { + return &PublicEthereumAPI{b, solcPath, solc} +} + +// GasPrice returns a suggestion for a gas price. +func (s *PublicEthereumAPI) GasPrice(ctx context.Context) (*big.Int, error) { + return s.b.SuggestPrice(ctx) +} + +func (s *PublicEthereumAPI) getSolc() (*compiler.Solidity, error) { + var err error + solc := *s.solc + if solc == nil { + solc, err = compiler.New(*s.solcPath) + } + return solc, err +} + +// GetCompilers returns the collection of available smart contract compilers +func (s *PublicEthereumAPI) GetCompilers() ([]string, error) { + solc, err := s.getSolc() + if err == nil && solc != nil { + return []string{"Solidity"}, nil + } + + return []string{}, nil +} + +// CompileSolidity compiles the given solidity source +func (s *PublicEthereumAPI) CompileSolidity(source string) (map[string]*compiler.Contract, error) { + solc, err := s.getSolc() + if err != nil { + return nil, err + } + + if solc == nil { + return nil, errors.New("solc (solidity compiler) not found") + } + + return solc.Compile(source) +} + +// ProtocolVersion returns the current Ethereum protocol version this node supports +func (s *PublicEthereumAPI) ProtocolVersion() *rpc.HexNumber { + return rpc.NewHexNumber(s.b.ProtocolVersion()) +} + +// Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not +// yet received the latest block headers from its pears. In case it is synchronizing: +// - startingBlock: block number this node started to synchronise from +// - currentBlock: block number this node is currently importing +// - highestBlock: block number of the highest block header this node has received from peers +// - pulledStates: number of state entries processed until now +// - knownStates: number of known state entries that still need to be pulled +func (s *PublicEthereumAPI) Syncing() (interface{}, error) { + origin, current, height, pulled, known := s.b.Downloader().Progress() + + // Return not syncing if the synchronisation already completed + if current >= height { + return false, nil + } + // Otherwise gather the block sync stats + return map[string]interface{}{ + "startingBlock": rpc.NewHexNumber(origin), + "currentBlock": rpc.NewHexNumber(current), + "highestBlock": rpc.NewHexNumber(height), + "pulledStates": rpc.NewHexNumber(pulled), + "knownStates": rpc.NewHexNumber(known), + }, nil +} + +// PublicTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential. +type PublicTxPoolAPI struct { + b Backend +} + +// NewPublicTxPoolAPI creates a new tx pool service that gives information about the transaction pool. +func NewPublicTxPoolAPI(b Backend) *PublicTxPoolAPI { + return &PublicTxPoolAPI{b} +} + +// Content returns the transactions contained within the transaction pool. +func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string][]*RPCTransaction { + content := map[string]map[string]map[string][]*RPCTransaction{ + "pending": make(map[string]map[string][]*RPCTransaction), + "queued": make(map[string]map[string][]*RPCTransaction), + } + pending, queue := s.b.TxPoolContent() + + // Flatten the pending transactions + for account, batches := range pending { + dump := make(map[string][]*RPCTransaction) + for nonce, txs := range batches { + nonce := fmt.Sprintf("%d", nonce) + for _, tx := range txs { + dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx)) + } + } + content["pending"][account.Hex()] = dump + } + // Flatten the queued transactions + for account, batches := range queue { + dump := make(map[string][]*RPCTransaction) + for nonce, txs := range batches { + nonce := fmt.Sprintf("%d", nonce) + for _, tx := range txs { + dump[nonce] = append(dump[nonce], newRPCPendingTransaction(tx)) + } + } + content["queued"][account.Hex()] = dump + } + return content +} + +// Status returns the number of pending and queued transaction in the pool. +func (s *PublicTxPoolAPI) Status() map[string]*rpc.HexNumber { + pending, queue := s.b.Stats() + return map[string]*rpc.HexNumber{ + "pending": rpc.NewHexNumber(pending), + "queued": rpc.NewHexNumber(queue), + } +} + +// Inspect retrieves the content of the transaction pool and flattens it into an +// easily inspectable list. +func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string][]string { + content := map[string]map[string]map[string][]string{ + "pending": make(map[string]map[string][]string), + "queued": make(map[string]map[string][]string), + } + pending, queue := s.b.TxPoolContent() + + // Define a formatter to flatten a transaction into a string + var format = func(tx *types.Transaction) string { + if to := tx.To(); to != nil { + return fmt.Sprintf("%s: %v wei + %v × %v gas", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice()) + } + return fmt.Sprintf("contract creation: %v wei + %v × %v gas", tx.Value(), tx.Gas(), tx.GasPrice()) + } + // Flatten the pending transactions + for account, batches := range pending { + dump := make(map[string][]string) + for nonce, txs := range batches { + nonce := fmt.Sprintf("%d", nonce) + for _, tx := range txs { + dump[nonce] = append(dump[nonce], format(tx)) + } + } + content["pending"][account.Hex()] = dump + } + // Flatten the queued transactions + for account, batches := range queue { + dump := make(map[string][]string) + for nonce, txs := range batches { + nonce := fmt.Sprintf("%d", nonce) + for _, tx := range txs { + dump[nonce] = append(dump[nonce], format(tx)) + } + } + content["queued"][account.Hex()] = dump + } + return content +} + +// PublicAccountAPI provides an API to access accounts managed by this node. +// It offers only methods that can retrieve accounts. +type PublicAccountAPI struct { + am *accounts.Manager +} + +// NewPublicAccountAPI creates a new PublicAccountAPI. +func NewPublicAccountAPI(am *accounts.Manager) *PublicAccountAPI { + return &PublicAccountAPI{am: am} +} + +// Accounts returns the collection of accounts this node manages +func (s *PublicAccountAPI) Accounts() []accounts.Account { + return s.am.Accounts() +} + +// PrivateAccountAPI provides an API to access accounts managed by this node. +// It offers methods to create, (un)lock en list accounts. Some methods accept +// passwords and are therefore considered private by default. +type PrivateAccountAPI struct { + am *accounts.Manager + b Backend +} + +// NewPrivateAccountAPI create a new PrivateAccountAPI. +func NewPrivateAccountAPI(b Backend) *PrivateAccountAPI { + return &PrivateAccountAPI{ + am: b.AccountManager(), + b: b, + } +} + +// ListAccounts will return a list of addresses for accounts this node manages. +func (s *PrivateAccountAPI) ListAccounts() []common.Address { + accounts := s.am.Accounts() + addresses := make([]common.Address, len(accounts)) + for i, acc := range accounts { + addresses[i] = acc.Address + } + return addresses +} + +// NewAccount will create a new account and returns the address for the new account. +func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error) { + acc, err := s.am.NewAccount(password) + if err == nil { + return acc.Address, nil + } + return common.Address{}, err +} + +// ImportRawKey stores the given hex encoded ECDSA key into the key directory, +// encrypting it with the passphrase. +func (s *PrivateAccountAPI) ImportRawKey(privkey string, password string) (common.Address, error) { + hexkey, err := hex.DecodeString(privkey) + if err != nil { + return common.Address{}, err + } + + acc, err := s.am.ImportECDSA(crypto.ToECDSA(hexkey), password) + return acc.Address, err +} + +// UnlockAccount will unlock the account associated with the given address with +// the given password for duration seconds. If duration is nil it will use a +// default of 300 seconds. It returns an indication if the account was unlocked. +func (s *PrivateAccountAPI) UnlockAccount(addr common.Address, password string, duration *rpc.HexNumber) (bool, error) { + if duration == nil { + duration = rpc.NewHexNumber(300) + } + a := accounts.Account{Address: addr} + d := time.Duration(duration.Int64()) * time.Second + if err := s.am.TimedUnlock(a, password, d); err != nil { + return false, err + } + return true, nil +} + +// LockAccount will lock the account associated with the given address when it's unlocked. +func (s *PrivateAccountAPI) LockAccount(addr common.Address) bool { + return s.am.Lock(addr) == nil +} + +// SignAndSendTransaction will create a transaction from the given arguments and +// tries to sign it with the key associated with args.To. If the given passwd isn't +// able to decrypt the key it fails. +func (s *PrivateAccountAPI) SignAndSendTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) { + var err error + args, err = prepareSendTxArgs(ctx, args, s.b) + if err != nil { + return common.Hash{}, err + } + + if args.Nonce == nil { + nonce, err := s.b.GetPoolNonce(ctx, args.From) + if err != nil { + return common.Hash{}, err + } + args.Nonce = rpc.NewHexNumber(nonce) + } + + var tx *types.Transaction + if args.To == nil { + tx = types.NewContractCreation(args.Nonce.Uint64(), args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) + } else { + tx = types.NewTransaction(args.Nonce.Uint64(), *args.To, args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) + } + + signature, err := s.am.SignWithPassphrase(args.From, passwd, tx.SigHash().Bytes()) + if err != nil { + return common.Hash{}, err + } + + return submitTransaction(ctx, s.b, tx, signature) +} + +// PublicBlockChainAPI provides an API to access the Ethereum blockchain. +// It offers only methods that operate on public data that is freely available to anyone. +type PublicBlockChainAPI struct { + b Backend + muNewBlockSubscriptions sync.Mutex // protects newBlocksSubscriptions + newBlockSubscriptions map[string]func(core.ChainEvent) error // callbacks for new block subscriptions +} + +// NewPublicBlockChainAPI creates a new Etheruem blockchain API. +func NewPublicBlockChainAPI(b Backend) *PublicBlockChainAPI { + api := &PublicBlockChainAPI{ + b: b, + newBlockSubscriptions: make(map[string]func(core.ChainEvent) error), + } + + go api.subscriptionLoop() + + return api +} + +// subscriptionLoop reads events from the global event mux and creates notifications for the matched subscriptions. +func (s *PublicBlockChainAPI) subscriptionLoop() { + sub := s.b.EventMux().Subscribe(core.ChainEvent{}) + for event := range sub.Chan() { + if chainEvent, ok := event.Data.(core.ChainEvent); ok { + s.muNewBlockSubscriptions.Lock() + for id, notifyOf := range s.newBlockSubscriptions { + if notifyOf(chainEvent) == rpc.ErrNotificationNotFound { + delete(s.newBlockSubscriptions, id) + } + } + s.muNewBlockSubscriptions.Unlock() + } + } +} + +// BlockNumber returns the block number of the chain head. +func (s *PublicBlockChainAPI) BlockNumber() *big.Int { + return s.b.HeaderByNumber(rpc.LatestBlockNumber).Number +} + +// GetBalance returns the amount of wei for the given address in the state of the +// given block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta +// block numbers are also allowed. +func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*big.Int, error) { + state, _, err := s.b.StateAndHeaderByNumber(blockNr) + if state == nil || err != nil { + return nil, err + } + + return state.GetBalance(ctx, address) +} + +// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all +// transactions in the block are returned in full detail, otherwise only the transaction hash is returned. +func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { + block, err := s.b.BlockByNumber(ctx, blockNr) + if block != nil { + response, err := s.rpcOutputBlock(block, true, fullTx) + if err == nil && blockNr == rpc.PendingBlockNumber { + // Pending blocks need to nil out a few fields + for _, field := range []string{"hash", "nonce", "logsBloom", "miner"} { + response[field] = nil + } + } + return response, err + } + return nil, err +} + +// GetBlockByHash returns the requested block. When fullTx is true all transactions in the block are returned in full +// detail, otherwise only the transaction hash is returned. +func (s *PublicBlockChainAPI) GetBlockByHash(ctx context.Context, blockHash common.Hash, fullTx bool) (map[string]interface{}, error) { + block, err := s.b.GetBlock(ctx, blockHash) + if block != nil { + return s.rpcOutputBlock(block, true, fullTx) + } + return nil, err +} + +// GetUncleByBlockNumberAndIndex returns the uncle block for the given block hash and index. When fullTx is true +// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned. +func (s *PublicBlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index rpc.HexNumber) (map[string]interface{}, error) { + block, err := s.b.BlockByNumber(ctx, blockNr) + if block != nil { + uncles := block.Uncles() + if index.Int() < 0 || index.Int() >= len(uncles) { + glog.V(logger.Debug).Infof("uncle block on index %d not found for block #%d", index.Int(), blockNr) + return nil, nil + } + block = types.NewBlockWithHeader(uncles[index.Int()]) + return s.rpcOutputBlock(block, false, false) + } + return nil, err +} + +// GetUncleByBlockHashAndIndex returns the uncle block for the given block hash and index. When fullTx is true +// all transactions in the block are returned in full detail, otherwise only the transaction hash is returned. +func (s *PublicBlockChainAPI) GetUncleByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index rpc.HexNumber) (map[string]interface{}, error) { + block, err := s.b.GetBlock(ctx, blockHash) + if block != nil { + uncles := block.Uncles() + if index.Int() < 0 || index.Int() >= len(uncles) { + glog.V(logger.Debug).Infof("uncle block on index %d not found for block %s", index.Int(), blockHash.Hex()) + return nil, nil + } + block = types.NewBlockWithHeader(uncles[index.Int()]) + return s.rpcOutputBlock(block, false, false) + } + return nil, err +} + +// GetUncleCountByBlockNumber returns number of uncles in the block for the given block number +func (s *PublicBlockChainAPI) GetUncleCountByBlockNumber(ctx context.Context, blockNr rpc.BlockNumber) *rpc.HexNumber { + if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil { + return rpc.NewHexNumber(len(block.Uncles())) + } + return nil +} + +// GetUncleCountByBlockHash returns number of uncles in the block for the given block hash +func (s *PublicBlockChainAPI) GetUncleCountByBlockHash(ctx context.Context, blockHash common.Hash) *rpc.HexNumber { + if block, _ := s.b.GetBlock(ctx, blockHash); block != nil { + return rpc.NewHexNumber(len(block.Uncles())) + } + return nil +} + +// NewBlocksArgs allows the user to specify if the returned block should include transactions and in which format. +type NewBlocksArgs struct { + IncludeTransactions bool `json:"includeTransactions"` + TransactionDetails bool `json:"transactionDetails"` +} + +// NewBlocks triggers a new block event each time a block is appended to the chain. It accepts an argument which allows +// the caller to specify whether the output should contain transactions and in what format. +func (s *PublicBlockChainAPI) NewBlocks(ctx context.Context, args NewBlocksArgs) (rpc.Subscription, error) { + notifier, supported := rpc.NotifierFromContext(ctx) + if !supported { + return nil, rpc.ErrNotificationsUnsupported + } + + // create a subscription that will remove itself when unsubscribed/cancelled + subscription, err := notifier.NewSubscription(func(subId string) { + s.muNewBlockSubscriptions.Lock() + delete(s.newBlockSubscriptions, subId) + s.muNewBlockSubscriptions.Unlock() + }) + + if err != nil { + return nil, err + } + + // add a callback that is called on chain events which will format the block and notify the client + s.muNewBlockSubscriptions.Lock() + s.newBlockSubscriptions[subscription.ID()] = func(e core.ChainEvent) error { + notification, err := s.rpcOutputBlock(e.Block, args.IncludeTransactions, args.TransactionDetails) + if err == nil { + return subscription.Notify(notification) + } + glog.V(logger.Warn).Info("unable to format block %v\n", err) + return nil + } + s.muNewBlockSubscriptions.Unlock() + return subscription, nil +} + +// GetCode returns the code stored at the given address in the state for the given block number. +func (s *PublicBlockChainAPI) GetCode(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (string, error) { + state, _, err := s.b.StateAndHeaderByNumber(blockNr) + if state == nil || err != nil { + return "", err + } + res, err := state.GetCode(ctx, address) + if len(res) == 0 || err != nil { // backwards compatibility + return "0x", err + } + return common.ToHex(res), nil +} + +// GetStorageAt returns the storage from the state at the given address, key and +// block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta block +// numbers are also allowed. +func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.Address, key string, blockNr rpc.BlockNumber) (string, error) { + state, _, err := s.b.StateAndHeaderByNumber(blockNr) + if state == nil || err != nil { + return "0x", err + } + res, err := state.GetState(ctx, address, common.HexToHash(key)) + if err != nil { + return "0x", err + } + return res.Hex(), nil +} + +// callmsg is the message type used for call transations. +type callmsg struct { + addr common.Address + nonce uint64 + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte +} + +// accessor boilerplate to implement core.Message +func (m callmsg) From() (common.Address, error) { return m.addr, nil } +func (m callmsg) FromFrontier() (common.Address, error) { return m.addr, nil } +func (m callmsg) Nonce() uint64 { return m.nonce } +func (m callmsg) To() *common.Address { return m.to } +func (m callmsg) GasPrice() *big.Int { return m.gasPrice } +func (m callmsg) Gas() *big.Int { return m.gas } +func (m callmsg) Value() *big.Int { return m.value } +func (m callmsg) Data() []byte { return m.data } + +// CallArgs represents the arguments for a call. +type CallArgs struct { + From common.Address `json:"from"` + To *common.Address `json:"to"` + Gas rpc.HexNumber `json:"gas"` + GasPrice rpc.HexNumber `json:"gasPrice"` + Value rpc.HexNumber `json:"value"` + Data string `json:"data"` +} + +func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber) (string, *big.Int, error) { + state, header, err := s.b.StateAndHeaderByNumber(blockNr) + if state == nil || err != nil { + return "0x", common.Big0, err + } + + // Set the account address to interact with + var addr common.Address + if args.From == (common.Address{}) { + accounts := s.b.AccountManager().Accounts() + if len(accounts) == 0 { + addr = common.Address{} + } else { + addr = accounts[0].Address + } + } else { + addr = args.From + } + + // Assemble the CALL invocation + msg := callmsg{ + addr: addr, + to: args.To, + gas: args.Gas.BigInt(), + gasPrice: args.GasPrice.BigInt(), + value: args.Value.BigInt(), + data: common.FromHex(args.Data), + } + + if msg.gas.Cmp(common.Big0) == 0 { + msg.gas = big.NewInt(50000000) + } + + if msg.gasPrice.Cmp(common.Big0) == 0 { + msg.gasPrice = new(big.Int).Mul(big.NewInt(50), common.Shannon) + } + + // Execute the call and return + vmenv, vmError, err := s.b.GetVMEnv(ctx, msg, state, header) + if err != nil { + return "0x", common.Big0, err + } + gp := new(core.GasPool).AddGas(common.MaxBig) + res, gas, err := core.ApplyMessage(vmenv, msg, gp) + if err := vmError(); err != nil { + return "0x", common.Big0, err + } + if len(res) == 0 { // backwards compatability + return "0x", gas, err + } + return common.ToHex(res), gas, err +} + +// Call executes the given transaction on the state for the given block number. +// It doesn't make and changes in the state/blockchain and is usefull to execute and retrieve values. +func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber) (string, error) { + result, _, err := s.doCall(ctx, args, blockNr) + return result, err +} + +// EstimateGas returns an estimate of the amount of gas needed to execute the given transaction. +func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (*rpc.HexNumber, error) { + _, gas, err := s.doCall(ctx, args, rpc.PendingBlockNumber) + return rpc.NewHexNumber(gas), err +} + +// ExecutionResult groups all structured logs emitted by the EVM +// while replaying a transaction in debug mode as well as the amount of +// gas used and the return value +type ExecutionResult struct { + Gas *big.Int `json:"gas"` + ReturnValue string `json:"returnValue"` + StructLogs []StructLogRes `json:"structLogs"` +} + +// StructLogRes stores a structured log emitted by the EVM while replaying a +// transaction in debug mode +type StructLogRes struct { + Pc uint64 `json:"pc"` + Op string `json:"op"` + Gas *big.Int `json:"gas"` + GasCost *big.Int `json:"gasCost"` + Depth int `json:"depth"` + Error error `json:"error"` + Stack []string `json:"stack"` + Memory []string `json:"memory"` + Storage map[string]string `json:"storage"` +} + +// formatLogs formats EVM returned structured logs for json output +func FormatLogs(structLogs []vm.StructLog) []StructLogRes { + formattedStructLogs := make([]StructLogRes, len(structLogs)) + for index, trace := range structLogs { + formattedStructLogs[index] = StructLogRes{ + Pc: trace.Pc, + Op: trace.Op.String(), + Gas: trace.Gas, + GasCost: trace.GasCost, + Depth: trace.Depth, + Error: trace.Err, + Stack: make([]string, len(trace.Stack)), + Storage: make(map[string]string), + } + + for i, stackValue := range trace.Stack { + formattedStructLogs[index].Stack[i] = fmt.Sprintf("%x", common.LeftPadBytes(stackValue.Bytes(), 32)) + } + + for i := 0; i+32 <= len(trace.Memory); i += 32 { + formattedStructLogs[index].Memory = append(formattedStructLogs[index].Memory, fmt.Sprintf("%x", trace.Memory[i:i+32])) + } + + for i, storageValue := range trace.Storage { + formattedStructLogs[index].Storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue) + } + } + return formattedStructLogs +} + +// TraceCall executes a call and returns the amount of gas, created logs and optionally returned values. +func (s *PublicBlockChainAPI) TraceCall(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber) (*ExecutionResult, error) { + state, header, err := s.b.StateAndHeaderByNumber(blockNr) + if state == nil || err != nil { + return nil, err + } + + var addr common.Address + if args.From == (common.Address{}) { + accounts := s.b.AccountManager().Accounts() + if len(accounts) == 0 { + addr = common.Address{} + } else { + addr = accounts[0].Address + } + } else { + addr = args.From + } + + // Assemble the CALL invocation + msg := callmsg{ + addr: addr, + to: args.To, + gas: args.Gas.BigInt(), + gasPrice: args.GasPrice.BigInt(), + value: args.Value.BigInt(), + data: common.FromHex(args.Data), + } + + if msg.gas.Cmp(common.Big0) == 0 { + msg.gas = big.NewInt(50000000) + } + + if msg.gasPrice.Cmp(common.Big0) == 0 { + msg.gasPrice = new(big.Int).Mul(big.NewInt(50), common.Shannon) + } + + // Execute the call and return + vmenv, vmError, err := s.b.GetVMEnv(ctx, msg, state, header) + if err != nil { + return nil, err + } + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, gas, err := core.ApplyMessage(vmenv, msg, gp) + if err := vmError(); err != nil { + return nil, err + } + return &ExecutionResult{ + Gas: gas, + ReturnValue: fmt.Sprintf("%x", ret), + StructLogs: FormatLogs(vmenv.StructLogs()), + }, nil +} + +// rpcOutputBlock 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 (s *PublicBlockChainAPI) rpcOutputBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) { + fields := map[string]interface{}{ + "number": rpc.NewHexNumber(b.Number()), + "hash": b.Hash(), + "parentHash": b.ParentHash(), + "nonce": b.Header().Nonce, + "sha3Uncles": b.UncleHash(), + "logsBloom": b.Bloom(), + "stateRoot": b.Root(), + "miner": b.Coinbase(), + "difficulty": rpc.NewHexNumber(b.Difficulty()), + "totalDifficulty": rpc.NewHexNumber(s.b.GetTd(b.Hash())), + "extraData": fmt.Sprintf("0x%x", b.Extra()), + "size": rpc.NewHexNumber(b.Size().Int64()), + "gasLimit": rpc.NewHexNumber(b.GasLimit()), + "gasUsed": rpc.NewHexNumber(b.GasUsed()), + "timestamp": rpc.NewHexNumber(b.Time()), + "transactionsRoot": b.TxHash(), + "receiptRoot": b.ReceiptHash(), + } + + if inclTx { + formatTx := func(tx *types.Transaction) (interface{}, error) { + return tx.Hash(), nil + } + + if fullTx { + formatTx = func(tx *types.Transaction) (interface{}, error) { + return newRPCTransaction(b, tx.Hash()) + } + } + + txs := b.Transactions() + transactions := make([]interface{}, len(txs)) + var err error + for i, tx := range b.Transactions() { + 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 +} + +// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction +type RPCTransaction struct { + BlockHash common.Hash `json:"blockHash"` + BlockNumber *rpc.HexNumber `json:"blockNumber"` + From common.Address `json:"from"` + Gas *rpc.HexNumber `json:"gas"` + GasPrice *rpc.HexNumber `json:"gasPrice"` + Hash common.Hash `json:"hash"` + Input string `json:"input"` + Nonce *rpc.HexNumber `json:"nonce"` + To *common.Address `json:"to"` + TransactionIndex *rpc.HexNumber `json:"transactionIndex"` + Value *rpc.HexNumber `json:"value"` +} + +// newRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation +func newRPCPendingTransaction(tx *types.Transaction) *RPCTransaction { + from, _ := tx.FromFrontier() + + return &RPCTransaction{ + From: from, + Gas: rpc.NewHexNumber(tx.Gas()), + GasPrice: rpc.NewHexNumber(tx.GasPrice()), + Hash: tx.Hash(), + Input: fmt.Sprintf("0x%x", tx.Data()), + Nonce: rpc.NewHexNumber(tx.Nonce()), + To: tx.To(), + Value: rpc.NewHexNumber(tx.Value()), + } +} + +// newRPCTransaction returns a transaction that will serialize to the RPC representation. +func newRPCTransactionFromBlockIndex(b *types.Block, txIndex int) (*RPCTransaction, error) { + if txIndex >= 0 && txIndex < len(b.Transactions()) { + tx := b.Transactions()[txIndex] + from, err := tx.FromFrontier() + if err != nil { + return nil, err + } + + return &RPCTransaction{ + BlockHash: b.Hash(), + BlockNumber: rpc.NewHexNumber(b.Number()), + From: from, + Gas: rpc.NewHexNumber(tx.Gas()), + GasPrice: rpc.NewHexNumber(tx.GasPrice()), + Hash: tx.Hash(), + Input: fmt.Sprintf("0x%x", tx.Data()), + Nonce: rpc.NewHexNumber(tx.Nonce()), + To: tx.To(), + TransactionIndex: rpc.NewHexNumber(txIndex), + Value: rpc.NewHexNumber(tx.Value()), + }, nil + } + + return nil, nil +} + +// newRPCTransaction returns a transaction that will serialize to the RPC representation. +func newRPCTransaction(b *types.Block, txHash common.Hash) (*RPCTransaction, error) { + for idx, tx := range b.Transactions() { + if tx.Hash() == txHash { + return newRPCTransactionFromBlockIndex(b, idx) + } + } + + return nil, nil +} + +// PublicTransactionPoolAPI exposes methods for the RPC interface +type PublicTransactionPoolAPI struct { + b Backend + muPendingTxSubs sync.Mutex + pendingTxSubs map[string]rpc.Subscription +} + +// NewPublicTransactionPoolAPI creates a new RPC service with methods specific for the transaction pool. +func NewPublicTransactionPoolAPI(b Backend) *PublicTransactionPoolAPI { + api := &PublicTransactionPoolAPI{ + b: b, + pendingTxSubs: make(map[string]rpc.Subscription), + } + + go api.subscriptionLoop() + + return api +} + +// subscriptionLoop listens for events on the global event mux and creates notifications for subscriptions. +func (s *PublicTransactionPoolAPI) subscriptionLoop() { + sub := s.b.EventMux().Subscribe(core.TxPreEvent{}) + for event := range sub.Chan() { + tx := event.Data.(core.TxPreEvent) + if from, err := tx.Tx.FromFrontier(); err == nil { + if s.b.AccountManager().HasAddress(from) { + s.muPendingTxSubs.Lock() + for id, sub := range s.pendingTxSubs { + if sub.Notify(tx.Tx.Hash()) == rpc.ErrNotificationNotFound { + delete(s.pendingTxSubs, id) + } + } + s.muPendingTxSubs.Unlock() + } + } + } +} + +func getTransaction(chainDb ethdb.Database, b Backend, txHash common.Hash) (*types.Transaction, bool, error) { + txData, err := chainDb.Get(txHash.Bytes()) + isPending := false + tx := new(types.Transaction) + + if err == nil && len(txData) > 0 { + if err := rlp.DecodeBytes(txData, tx); err != nil { + return nil, isPending, err + } + } else { + // pending transaction? + tx = b.GetPoolTransaction(txHash) + isPending = true + } + + return tx, isPending, nil +} + +// GetBlockTransactionCountByNumber returns the number of transactions in the block with the given block number. +func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) *rpc.HexNumber { + if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil { + return rpc.NewHexNumber(len(block.Transactions())) + } + return nil +} + +// GetBlockTransactionCountByHash returns the number of transactions in the block with the given hash. +func (s *PublicTransactionPoolAPI) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) *rpc.HexNumber { + if block, _ := s.b.GetBlock(ctx, blockHash); block != nil { + return rpc.NewHexNumber(len(block.Transactions())) + } + return nil +} + +// GetTransactionByBlockNumberAndIndex returns the transaction for the given block number and index. +func (s *PublicTransactionPoolAPI) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, index rpc.HexNumber) (*RPCTransaction, error) { + if block, _ := s.b.BlockByNumber(ctx, blockNr); block != nil { + return newRPCTransactionFromBlockIndex(block, index.Int()) + } + return nil, nil +} + +// GetTransactionByBlockHashAndIndex returns the transaction for the given block hash and index. +func (s *PublicTransactionPoolAPI) GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index rpc.HexNumber) (*RPCTransaction, error) { + if block, _ := s.b.GetBlock(ctx, blockHash); block != nil { + return newRPCTransactionFromBlockIndex(block, index.Int()) + } + return nil, nil +} + +// GetTransactionCount returns the number of transactions the given address has sent for the given block number +func (s *PublicTransactionPoolAPI) GetTransactionCount(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (*rpc.HexNumber, error) { + state, _, err := s.b.StateAndHeaderByNumber(blockNr) + if state == nil || err != nil { + return nil, err + } + nonce, err := state.GetNonce(ctx, address) + if err != nil { + return nil, err + } + return rpc.NewHexNumber(nonce), nil +} + +// getTransactionBlockData fetches the meta data for the given transaction from the chain database. This is useful to +// retrieve block information for a hash. It returns the block hash, block index and transaction index. +func getTransactionBlockData(chainDb ethdb.Database, txHash common.Hash) (common.Hash, uint64, uint64, error) { + var txBlock struct { + BlockHash common.Hash + BlockIndex uint64 + Index uint64 + } + + blockData, err := chainDb.Get(append(txHash.Bytes(), 0x0001)) + if err != nil { + return common.Hash{}, uint64(0), uint64(0), err + } + + reader := bytes.NewReader(blockData) + if err = rlp.Decode(reader, &txBlock); err != nil { + return common.Hash{}, uint64(0), uint64(0), err + } + + return txBlock.BlockHash, txBlock.BlockIndex, txBlock.Index, nil +} + +// GetTransactionByHash returns the transaction for the given hash +func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, txHash common.Hash) (*RPCTransaction, error) { + var tx *types.Transaction + var isPending bool + var err error + + if tx, isPending, err = getTransaction(s.b.ChainDb(), s.b, txHash); err != nil { + glog.V(logger.Debug).Infof("%v\n", err) + return nil, nil + } else if tx == nil { + return nil, nil + } + + if isPending { + return newRPCPendingTransaction(tx), nil + } + + blockHash, _, _, err := getTransactionBlockData(s.b.ChainDb(), txHash) + if err != nil { + glog.V(logger.Debug).Infof("%v\n", err) + return nil, nil + } + + if block, _ := s.b.GetBlock(ctx, blockHash); block != nil { + return newRPCTransaction(block, txHash) + } + + return nil, nil +} + +// GetTransactionReceipt returns the transaction receipt for the given transaction hash. +func (s *PublicTransactionPoolAPI) GetTransactionReceipt(txHash common.Hash) (map[string]interface{}, error) { + receipt := core.GetReceipt(s.b.ChainDb(), txHash) + if receipt == nil { + glog.V(logger.Debug).Infof("receipt not found for transaction %s", txHash.Hex()) + return nil, nil + } + + tx, _, err := getTransaction(s.b.ChainDb(), s.b, txHash) + if err != nil { + glog.V(logger.Debug).Infof("%v\n", err) + return nil, nil + } + + txBlock, blockIndex, index, err := getTransactionBlockData(s.b.ChainDb(), txHash) + if err != nil { + glog.V(logger.Debug).Infof("%v\n", err) + return nil, nil + } + + from, err := tx.FromFrontier() + if err != nil { + glog.V(logger.Debug).Infof("%v\n", err) + return nil, nil + } + + fields := map[string]interface{}{ + "root": common.Bytes2Hex(receipt.PostState), + "blockHash": txBlock, + "blockNumber": rpc.NewHexNumber(blockIndex), + "transactionHash": txHash, + "transactionIndex": rpc.NewHexNumber(index), + "from": from, + "to": tx.To(), + "gasUsed": rpc.NewHexNumber(receipt.GasUsed), + "cumulativeGasUsed": rpc.NewHexNumber(receipt.CumulativeGasUsed), + "contractAddress": nil, + "logs": receipt.Logs, + } + + if receipt.Logs == nil { + fields["logs"] = []vm.Logs{} + } + + // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation + if bytes.Compare(receipt.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 { + fields["contractAddress"] = receipt.ContractAddress + } + + return fields, nil +} + +// sign is a helper function that signs a transaction with the private key of the given address. +func (s *PublicTransactionPoolAPI) sign(addr common.Address, tx *types.Transaction) (*types.Transaction, error) { + signature, err := s.b.AccountManager().Sign(addr, tx.SigHash().Bytes()) + if err != nil { + return nil, err + } + return tx.WithSignature(signature) +} + +// SendTxArgs represents the arguments to sumbit a new transaction into the transaction pool. +type SendTxArgs struct { + From common.Address `json:"from"` + To *common.Address `json:"to"` + Gas *rpc.HexNumber `json:"gas"` + GasPrice *rpc.HexNumber `json:"gasPrice"` + Value *rpc.HexNumber `json:"value"` + Data string `json:"data"` + Nonce *rpc.HexNumber `json:"nonce"` +} + +// prepareSendTxArgs is a helper function that fills in default values for unspecified tx fields. +func prepareSendTxArgs(ctx context.Context, args SendTxArgs, b Backend) (SendTxArgs, error) { + if args.Gas == nil { + args.Gas = rpc.NewHexNumber(defaultGas) + } + if args.GasPrice == nil { + price, err := b.SuggestPrice(ctx) + if err != nil { + return args, err + } + args.GasPrice = rpc.NewHexNumber(price) + } + if args.Value == nil { + args.Value = rpc.NewHexNumber(0) + } + return args, nil +} + +// submitTransaction is a helper function that submits tx to txPool and creates a log entry. +func submitTransaction(ctx context.Context, b Backend, tx *types.Transaction, signature []byte) (common.Hash, error) { + signedTx, err := tx.WithSignature(signature) + if err != nil { + return common.Hash{}, err + } + + if err := b.SendTx(ctx, signedTx); err != nil { + return common.Hash{}, err + } + + if signedTx.To() == nil { + from, _ := signedTx.From() + addr := crypto.CreateAddress(from, signedTx.Nonce()) + glog.V(logger.Info).Infof("Tx(%s) created: %s\n", signedTx.Hash().Hex(), addr.Hex()) + } else { + glog.V(logger.Info).Infof("Tx(%s) to: %s\n", signedTx.Hash().Hex(), tx.To().Hex()) + } + + return signedTx.Hash(), nil +} + +// SendTransaction creates a transaction for the given argument, sign it and submit it to the +// transaction pool. +func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args SendTxArgs) (common.Hash, error) { + var err error + args, err = prepareSendTxArgs(ctx, args, s.b) + if err != nil { + return common.Hash{}, err + } + + if args.Nonce == nil { + nonce, err := s.b.GetPoolNonce(ctx, args.From) + if err != nil { + return common.Hash{}, err + } + args.Nonce = rpc.NewHexNumber(nonce) + } + + var tx *types.Transaction + if args.To == nil { + tx = types.NewContractCreation(args.Nonce.Uint64(), args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) + } else { + tx = types.NewTransaction(args.Nonce.Uint64(), *args.To, args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) + } + + signature, err := s.b.AccountManager().Sign(args.From, tx.SigHash().Bytes()) + if err != nil { + return common.Hash{}, err + } + + return submitTransaction(ctx, s.b, tx, signature) +} + +// SendRawTransaction will add the signed transaction to the transaction pool. +// The sender is responsible for signing the transaction and using the correct nonce. +func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encodedTx string) (string, error) { + tx := new(types.Transaction) + if err := rlp.DecodeBytes(common.FromHex(encodedTx), tx); err != nil { + return "", err + } + + if err := s.b.SendTx(ctx, tx); err != nil { + return "", err + } + + if tx.To() == nil { + from, err := tx.FromFrontier() + if err != nil { + return "", err + } + addr := crypto.CreateAddress(from, tx.Nonce()) + glog.V(logger.Info).Infof("Tx(%x) created: %x\n", tx.Hash(), addr) + } else { + glog.V(logger.Info).Infof("Tx(%x) to: %x\n", tx.Hash(), tx.To()) + } + + return tx.Hash().Hex(), nil +} + +// 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(addr common.Address, hash common.Hash) (string, error) { + signature, error := s.b.AccountManager().Sign(addr, hash[:]) + return common.ToHex(signature), error +} + +// SignTransactionArgs represents the arguments to sign a transaction. +type SignTransactionArgs struct { + From common.Address + To *common.Address + Nonce *rpc.HexNumber + Value *rpc.HexNumber + Gas *rpc.HexNumber + GasPrice *rpc.HexNumber + Data string + + BlockNumber int64 +} + +// Tx is a helper object for argument and return values +type Tx struct { + tx *types.Transaction + + To *common.Address `json:"to"` + From common.Address `json:"from"` + Nonce *rpc.HexNumber `json:"nonce"` + Value *rpc.HexNumber `json:"value"` + Data string `json:"data"` + GasLimit *rpc.HexNumber `json:"gas"` + GasPrice *rpc.HexNumber `json:"gasPrice"` + Hash common.Hash `json:"hash"` +} + +// UnmarshalJSON parses JSON data into tx. +func (tx *Tx) UnmarshalJSON(b []byte) (err error) { + req := struct { + To *common.Address `json:"to"` + From common.Address `json:"from"` + Nonce *rpc.HexNumber `json:"nonce"` + Value *rpc.HexNumber `json:"value"` + Data string `json:"data"` + GasLimit *rpc.HexNumber `json:"gas"` + GasPrice *rpc.HexNumber `json:"gasPrice"` + Hash common.Hash `json:"hash"` + }{} + + if err := json.Unmarshal(b, &req); err != nil { + return err + } + + tx.To = req.To + tx.From = req.From + tx.Nonce = req.Nonce + tx.Value = req.Value + tx.Data = req.Data + tx.GasLimit = req.GasLimit + tx.GasPrice = req.GasPrice + tx.Hash = req.Hash + + data := common.Hex2Bytes(tx.Data) + + if tx.Nonce == nil { + return fmt.Errorf("need nonce") + } + if tx.Value == nil { + tx.Value = rpc.NewHexNumber(0) + } + if tx.GasLimit == nil { + tx.GasLimit = rpc.NewHexNumber(0) + } + if tx.GasPrice == nil { + tx.GasPrice = rpc.NewHexNumber(int64(50000000000)) + } + + if req.To == nil { + tx.tx = types.NewContractCreation(tx.Nonce.Uint64(), tx.Value.BigInt(), tx.GasLimit.BigInt(), tx.GasPrice.BigInt(), data) + } else { + tx.tx = types.NewTransaction(tx.Nonce.Uint64(), *tx.To, tx.Value.BigInt(), tx.GasLimit.BigInt(), tx.GasPrice.BigInt(), data) + } + + return nil +} + +// SignTransactionResult represents a RLP encoded signed transaction. +type SignTransactionResult struct { + Raw string `json:"raw"` + Tx *Tx `json:"tx"` +} + +func newTx(t *types.Transaction) *Tx { + from, _ := t.FromFrontier() + return &Tx{ + tx: t, + To: t.To(), + From: from, + Value: rpc.NewHexNumber(t.Value()), + Nonce: rpc.NewHexNumber(t.Nonce()), + Data: "0x" + common.Bytes2Hex(t.Data()), + GasLimit: rpc.NewHexNumber(t.Gas()), + GasPrice: rpc.NewHexNumber(t.GasPrice()), + Hash: t.Hash(), + } +} + +// SignTransaction will sign the given transaction with the from account. +// The node needs to have the private key of the account corresponding with +// the given from address and it needs to be unlocked. +func (s *PublicTransactionPoolAPI) SignTransaction(ctx context.Context, args SignTransactionArgs) (*SignTransactionResult, error) { + if args.Gas == nil { + args.Gas = rpc.NewHexNumber(defaultGas) + } + if args.GasPrice == nil { + price, err := s.b.SuggestPrice(ctx) + if err != nil { + return nil, err + } + args.GasPrice = rpc.NewHexNumber(price) + } + if args.Value == nil { + args.Value = rpc.NewHexNumber(0) + } + + if args.Nonce == nil { + nonce, err := s.b.GetPoolNonce(ctx, args.From) + if err != nil { + return nil, err + } + args.Nonce = rpc.NewHexNumber(nonce) + } + + var tx *types.Transaction + if args.To == nil { + tx = types.NewContractCreation(args.Nonce.Uint64(), args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) + } else { + tx = types.NewTransaction(args.Nonce.Uint64(), *args.To, args.Value.BigInt(), args.Gas.BigInt(), args.GasPrice.BigInt(), common.FromHex(args.Data)) + } + + signedTx, err := s.sign(args.From, tx) + if err != nil { + return nil, err + } + + data, err := rlp.EncodeToBytes(signedTx) + if err != nil { + return nil, err + } + + return &SignTransactionResult{"0x" + common.Bytes2Hex(data), newTx(signedTx)}, nil +} + +// PendingTransactions returns the transactions that are in the transaction pool and have a from address that is one of +// the accounts this node manages. +func (s *PublicTransactionPoolAPI) PendingTransactions() []*RPCTransaction { + pending := s.b.GetPoolTransactions() + transactions := make([]*RPCTransaction, 0, len(pending)) + for _, tx := range pending { + from, _ := tx.FromFrontier() + if s.b.AccountManager().HasAddress(from) { + transactions = append(transactions, newRPCPendingTransaction(tx)) + } + } + return transactions +} + +// NewPendingTransactions creates a subscription that is triggered each time a transaction enters the transaction pool +// and is send from one of the transactions this nodes manages. +func (s *PublicTransactionPoolAPI) NewPendingTransactions(ctx context.Context) (rpc.Subscription, error) { + notifier, supported := rpc.NotifierFromContext(ctx) + if !supported { + return nil, rpc.ErrNotificationsUnsupported + } + + subscription, err := notifier.NewSubscription(func(id string) { + s.muPendingTxSubs.Lock() + delete(s.pendingTxSubs, id) + s.muPendingTxSubs.Unlock() + }) + + if err != nil { + return nil, err + } + + s.muPendingTxSubs.Lock() + s.pendingTxSubs[subscription.ID()] = subscription + s.muPendingTxSubs.Unlock() + + return subscription, nil +} + +// Resend accepts an existing transaction and a new gas price and limit. It will remove the given transaction from the +// pool and reinsert it with the new gas price and limit. +func (s *PublicTransactionPoolAPI) Resend(ctx context.Context, tx *Tx, gasPrice, gasLimit *rpc.HexNumber) (common.Hash, error) { + + pending := s.b.GetPoolTransactions() + for _, p := range pending { + if pFrom, err := p.FromFrontier(); err == nil && pFrom == tx.From && p.SigHash() == tx.tx.SigHash() { + if gasPrice == nil { + gasPrice = rpc.NewHexNumber(tx.tx.GasPrice()) + } + if gasLimit == nil { + gasLimit = rpc.NewHexNumber(tx.tx.Gas()) + } + + var newTx *types.Transaction + if tx.tx.To() == nil { + newTx = types.NewContractCreation(tx.tx.Nonce(), tx.tx.Value(), gasPrice.BigInt(), gasLimit.BigInt(), tx.tx.Data()) + } else { + newTx = types.NewTransaction(tx.tx.Nonce(), *tx.tx.To(), tx.tx.Value(), gasPrice.BigInt(), gasLimit.BigInt(), tx.tx.Data()) + } + + signedTx, err := s.sign(tx.From, newTx) + if err != nil { + return common.Hash{}, err + } + + s.b.RemoveTx(tx.Hash) + if err = s.b.SendTx(ctx, signedTx); err != nil { + return common.Hash{}, err + } + + return signedTx.Hash(), nil + } + } + + return common.Hash{}, fmt.Errorf("Transaction %#x not found", tx.Hash) +} + +// PrivateAdminAPI is the collection of Etheruem APIs exposed over the private +// admin endpoint. +type PrivateAdminAPI struct { + b Backend + solcPath *string + solc **compiler.Solidity +} + +// NewPrivateAdminAPI creates a new API definition for the private admin methods +// of the Ethereum service. +func NewPrivateAdminAPI(b Backend, solcPath *string, solc **compiler.Solidity) *PrivateAdminAPI { + return &PrivateAdminAPI{b, solcPath, solc} +} + +// SetSolc sets the Solidity compiler path to be used by the node. +func (api *PrivateAdminAPI) SetSolc(path string) (string, error) { + var err error + *api.solcPath = path + *api.solc, err = compiler.New(path) + if err != nil { + return "", err + } + return (*api.solc).Info(), nil +} + +// PublicDebugAPI is the collection of Etheruem APIs exposed over the public +// debugging endpoint. +type PublicDebugAPI struct { + b Backend +} + +// NewPublicDebugAPI creates a new API definition for the public debug methods +// of the Ethereum service. +func NewPublicDebugAPI(b Backend) *PublicDebugAPI { + return &PublicDebugAPI{b: b} +} + +// GetBlockRlp retrieves the RLP encoded for of a single block. +func (api *PublicDebugAPI) GetBlockRlp(ctx context.Context, number uint64) (string, error) { + block, _ := api.b.BlockByNumber(ctx, rpc.BlockNumber(number)) + if block == nil { + return "", fmt.Errorf("block #%d not found", number) + } + encoded, err := rlp.EncodeToBytes(block) + if err != nil { + return "", err + } + return fmt.Sprintf("%x", encoded), 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)) + if block == nil { + return "", fmt.Errorf("block #%d not found", number) + } + return fmt.Sprintf("%s", block), nil +} + +// SeedHash retrieves the seed hash of a block. +func (api *PublicDebugAPI) SeedHash(ctx context.Context, number uint64) (string, error) { + block, _ := api.b.BlockByNumber(ctx, rpc.BlockNumber(number)) + if block == nil { + return "", fmt.Errorf("block #%d not found", number) + } + hash, err := ethash.GetSeedHash(number) + if err != nil { + return "", err + } + return fmt.Sprintf("0x%x", hash), nil +} + +// PrivateDebugAPI is the collection of Etheruem APIs exposed over the private +// debugging endpoint. +type PrivateDebugAPI struct { + b Backend +} + +// NewPrivateDebugAPI creates a new API definition for the private debug methods +// of the Ethereum service. +func NewPrivateDebugAPI(b Backend) *PrivateDebugAPI { + return &PrivateDebugAPI{b: b} +} + +// ChaindbProperty returns leveldb properties of the chain database. +func (api *PrivateDebugAPI) ChaindbProperty(property string) (string, error) { + ldb, ok := api.b.ChainDb().(interface { + LDB() *leveldb.DB + }) + if !ok { + return "", fmt.Errorf("chaindbProperty does not work for memory databases") + } + if property == "" { + property = "leveldb.stats" + } else if !strings.HasPrefix(property, "leveldb.") { + property = "leveldb." + property + } + return ldb.LDB().GetProperty(property) +} + +// SetHead rewinds the head of the blockchain to a previous block. +func (api *PrivateDebugAPI) SetHead(number uint64) { + api.b.SetHead(number) +} + +// PublicNetAPI offers network related RPC methods +type PublicNetAPI struct { + net *p2p.Server + networkVersion int +} + +// NewPublicNetAPI creates a new net API instance. +func NewPublicNetAPI(net *p2p.Server, networkVersion int) *PublicNetAPI { + return &PublicNetAPI{net, networkVersion} +} + +// Listening returns an indication if the node is listening for network connections. +func (s *PublicNetAPI) Listening() bool { + return true // always listening +} + +// PeerCount returns the number of connected peers +func (s *PublicNetAPI) PeerCount() *rpc.HexNumber { + return rpc.NewHexNumber(s.net.PeerCount()) +} + +// Version returns the current ethereum protocol version. +func (s *PublicNetAPI) Version() string { + return fmt.Sprintf("%d", s.networkVersion) +} diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go new file mode 100644 index 0000000000..d112a6aef2 --- /dev/null +++ b/internal/ethapi/backend.go @@ -0,0 +1,119 @@ +// 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 . + +// Package ethapi implements the general Ethereum API functions. +package ethapi + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/compiler" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" +) + +// Backend interface provides the common API services (that are provided by +// both full and light clients) with access to necessary functions. +type Backend interface { + // general Ethereum API + Downloader() *downloader.Downloader + ProtocolVersion() int + SuggestPrice(ctx context.Context) (*big.Int, error) + ChainDb() ethdb.Database + EventMux() *event.TypeMux + AccountManager() *accounts.Manager + // BlockChain API + SetHead(number uint64) + HeaderByNumber(blockNr rpc.BlockNumber) *types.Header + BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) + StateAndHeaderByNumber(blockNr rpc.BlockNumber) (State, *types.Header, error) + GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) + GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) + GetTd(blockHash common.Hash) *big.Int + GetVMEnv(ctx context.Context, msg core.Message, state State, header *types.Header) (vm.Environment, func() error, error) + // TxPool API + SendTx(ctx context.Context, signedTx *types.Transaction) error + RemoveTx(txHash common.Hash) + GetPoolTransactions() types.Transactions + GetPoolTransaction(txHash common.Hash) *types.Transaction + GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) + Stats() (pending int, queued int) + TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) +} + +type State interface { + GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) + GetCode(ctx context.Context, addr common.Address) ([]byte, error) + GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) + GetNonce(ctx context.Context, addr common.Address) (uint64, error) +} + +func GetAPIs(apiBackend Backend, solcPath *string, solc **compiler.Solidity) []rpc.API { + return []rpc.API{ + { + Namespace: "eth", + Version: "1.0", + Service: NewPublicEthereumAPI(apiBackend, solcPath, solc), + Public: true, + }, { + Namespace: "eth", + Version: "1.0", + Service: NewPublicBlockChainAPI(apiBackend), + Public: true, + }, { + Namespace: "eth", + Version: "1.0", + Service: NewPublicTransactionPoolAPI(apiBackend), + Public: true, + }, { + Namespace: "txpool", + Version: "1.0", + Service: NewPublicTxPoolAPI(apiBackend), + Public: true, + }, { + Namespace: "admin", + Version: "1.0", + Service: NewPrivateAdminAPI(apiBackend, solcPath, solc), + }, { + Namespace: "debug", + Version: "1.0", + Service: NewPublicDebugAPI(apiBackend), + Public: true, + }, { + Namespace: "debug", + Version: "1.0", + Service: NewPrivateDebugAPI(apiBackend), + }, { + Namespace: "eth", + Version: "1.0", + Service: NewPublicAccountAPI(apiBackend.AccountManager()), + Public: true, + }, { + Namespace: "personal", + Version: "1.0", + Service: NewPrivateAccountAPI(apiBackend), + Public: false, + }, + } +} diff --git a/release/release.go b/release/release.go index 05b4885b50..07eb9359cf 100644 --- a/release/release.go +++ b/release/release.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" ) // Interval to check for new releases @@ -57,7 +58,7 @@ type ReleaseService struct { // releases and notify the user of such. func NewReleaseService(ctx *node.ServiceContext, config Config) (node.Service, error) { // Retrieve the Ethereum service dependency to access the blockchain - var ethereum *eth.Ethereum + var ethereum *eth.FullNodeService if err := ctx.Service(ðereum); err != nil { return nil, err } @@ -110,7 +111,9 @@ func (r *ReleaseService) checker() { timer.Reset(releaseRecheckInterval) // Retrieve the current version, and handle missing contracts gracefully - version, err := r.oracle.CurrentVersion(nil) + ctx, _ := context.WithTimeout(context.Background(), time.Second*5) + opts := &bind.CallOpts{Context: ctx} + version, err := r.oracle.CurrentVersion(opts) if err != nil { if err == bind.ErrNoCode { glog.V(logger.Debug).Infof("Release oracle not found at %x", r.config.Oracle) From 301a0aaf8b72db6c595765a009156cb642ac71b5 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 13 Jan 2016 19:35:48 +0100 Subject: [PATCH 02/32] light: implemented light chain, VM env and tx pool --- core/database_util.go | 7 +- core/types/block.go | 9 + light/events.go | 33 +++ light/lightchain.go | 430 +++++++++++++++++++++++++++++++++ light/lightchain_test.go | 399 ++++++++++++++++++++++++++++++ light/odr.go | 100 +++++--- light/odr_test.go | 323 +++++++++++++++++++++++++ light/odr_util.go | 112 +++++++++ light/state.go | 33 ++- light/state_object.go | 29 ++- light/state_test.go | 36 +-- light/trie.go | 25 +- light/txpool.go | 506 +++++++++++++++++++++++++++++++++++++++ light/txpool_test.go | 140 +++++++++++ light/vm_env.go | 241 +++++++++++++++++++ 15 files changed, 2340 insertions(+), 83 deletions(-) create mode 100644 light/events.go create mode 100644 light/lightchain.go create mode 100644 light/lightchain_test.go create mode 100644 light/odr_test.go create mode 100644 light/odr_util.go create mode 100644 light/txpool.go create mode 100644 light/txpool_test.go create mode 100644 light/vm_env.go diff --git a/core/database_util.go b/core/database_util.go index 1529d73695..6df2c15c20 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -343,8 +343,13 @@ func WriteBody(db ethdb.Database, hash common.Hash, number uint64, body *types.B if err != nil { return err } + return WriteBodyRLP(db, hash, data) +} + +// WriteBodyRLP writes a serialized body of a block into the database. +func WriteBodyRLP(db ethdb.Database, hash common.Hash, rlp rlp.RawValue) error { key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) - if err := db.Put(key, data); err != nil { + if err := db.Put(key, rlp); err != nil { glog.Fatalf("failed to store block body into database: %v", err) } glog.V(logger.Debug).Infof("stored block body [%x…]", hash.Bytes()[:4]) diff --git a/core/types/block.go b/core/types/block.go index 37b6f3ec17..203f64c653 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -70,6 +70,15 @@ type Header struct { Nonce BlockNonce } +func (h *Header) GetNumber() *big.Int { return new(big.Int).Set(h.Number) } +func (h *Header) GetGasLimit() *big.Int { return new(big.Int).Set(h.GasLimit) } +func (h *Header) GetGasUsed() *big.Int { return new(big.Int).Set(h.GasUsed) } +func (h *Header) GetDifficulty() *big.Int { return new(big.Int).Set(h.Difficulty) } +func (h *Header) GetTime() *big.Int { return new(big.Int).Set(h.Time) } +func (h *Header) GetNumberU64() uint64 { return h.Number.Uint64() } +func (h *Header) GetNonce() uint64 { return binary.BigEndian.Uint64(h.Nonce[:]) } +func (h *Header) GetExtra() []byte { return common.CopyBytes(h.Extra) } + func (h *Header) Hash() common.Hash { return rlpHash(h) } diff --git a/light/events.go b/light/events.go new file mode 100644 index 0000000000..28d91e31d4 --- /dev/null +++ b/light/events.go @@ -0,0 +1,33 @@ +// 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 . + +package light + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +type LightChainSplitEvent struct{ Header *types.Header } + +type LightChainEvent struct { + Header *types.Header + Hash common.Hash +} + +type LightChainSideEvent struct{ Header *types.Header } + +type LightChainHeadEvent struct{ Header *types.Header } diff --git a/light/lightchain.go b/light/lightchain.go new file mode 100644 index 0000000000..8f79597590 --- /dev/null +++ b/light/lightchain.go @@ -0,0 +1,430 @@ +// 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 . +package light + +import ( + "math/big" + "sync" + "sync/atomic" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/pow" + "github.com/ethereum/go-ethereum/rlp" + "github.com/hashicorp/golang-lru" + "golang.org/x/net/context" +) + +var ( + bodyCacheLimit = 256 + blockCacheLimit = 256 +) + +// LightChain represents a canonical chain that by default only handles block +// headers, downloading block bodies and receipts on demand through an ODR +// interface. It only does header validation during chain insertion. +type LightChain struct { + hc *core.HeaderChain + chainDb ethdb.Database + odr OdrBackend + eventMux *event.TypeMux + genesisBlock *types.Block + + mu sync.RWMutex + chainmu sync.RWMutex + procmu sync.RWMutex + + bodyCache *lru.Cache // Cache for the most recent block bodies + bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format + blockCache *lru.Cache // Cache for the most recent entire blocks + + quit chan struct{} + running int32 // running must be called automically + // procInterrupt must be atomically called + procInterrupt int32 // interrupt signaler for block processing + wg sync.WaitGroup + + pow pow.PoW + validator core.HeaderValidator +} + +// NewLightChain returns a fully initialised light chain using information +// available in the database. It initialises the default Ethereum header +// validator. +func NewLightChain(odr OdrBackend, pow pow.PoW, mux *event.TypeMux) (*LightChain, error) { + bodyCache, _ := lru.New(bodyCacheLimit) + bodyRLPCache, _ := lru.New(bodyCacheLimit) + blockCache, _ := lru.New(blockCacheLimit) + + bc := &LightChain{ + chainDb: odr.Database(), + odr: odr, + eventMux: mux, + quit: make(chan struct{}), + bodyCache: bodyCache, + bodyRLPCache: bodyRLPCache, + blockCache: blockCache, + pow: pow, + } + + var err error + bc.hc, err = core.NewHeaderChain(odr.Database(), bc.Validator, bc.getProcInterrupt) + bc.SetValidator(core.NewHeaderValidator(bc.hc, pow)) + if err != nil { + return nil, err + } + + bc.genesisBlock, _ = bc.GetBlockByNumber(NoOdr, 0) + if bc.genesisBlock == nil { + bc.genesisBlock, err = core.WriteDefaultGenesisBlock(odr.Database()) + if err != nil { + return nil, err + } + glog.V(logger.Info).Infoln("WARNING: Wrote default ethereum genesis block") + } + if err := bc.loadLastState(); err != nil { + return nil, err + } + // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain + for hash, _ := range core.BadHashes { + if header := bc.GetHeader(hash); header != nil { + glog.V(logger.Error).Infof("Found bad hash, rewinding chain to block #%d [%x…]", header.Number, header.ParentHash[:4]) + bc.SetHead(header.Number.Uint64() - 1) + glog.V(logger.Error).Infoln("Chain rewind was successful, resuming normal operation") + } + } + return bc, nil +} + +func (self *LightChain) getProcInterrupt() bool { + return atomic.LoadInt32(&self.procInterrupt) == 1 +} + +// Odr returns the ODR backend of the chain +func (self *LightChain) Odr() OdrBackend { + return self.odr +} + +// loadLastState loads the last known chain state from the database. This method +// assumes that the chain manager mutex is held. +func (self *LightChain) loadLastState() error { + if head := core.GetHeadHeaderHash(self.chainDb); head == (common.Hash{}) { + // Corrupt or empty database, init from scratch + self.Reset() + } else { + if header := self.GetHeader(head); header != nil { + self.hc.SetCurrentHeader(header) + } + } + + // Issue a status log and return + headerTd := self.GetTd(self.hc.CurrentHeader().Hash()) + glog.V(logger.Info).Infof("Last header: #%d [%x…] TD=%v", self.hc.CurrentHeader().Number, self.hc.CurrentHeader().Hash().Bytes()[:4], headerTd) + + return nil +} + +// SetHead rewinds the local chain to a new head. Everything above the new +// head will be deleted and the new one set. +func (bc *LightChain) SetHead(head uint64) { + bc.mu.Lock() + defer bc.mu.Unlock() + + bc.hc.SetHead(head) + bc.loadLastState() +} + +// GasLimit returns the gas limit of the current HEAD block. +func (self *LightChain) GasLimit() *big.Int { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.hc.CurrentHeader().GasLimit +} + +// LastBlockHash return the hash of the HEAD block. +func (self *LightChain) LastBlockHash() common.Hash { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.hc.CurrentHeader().Hash() +} + +// Status returns status information about the current chain such as the HEAD Td, +// the HEAD hash and the hash of the genesis block. +func (self *LightChain) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) { + self.mu.RLock() + defer self.mu.RUnlock() + + hash := self.hc.CurrentHeader().Hash() + return self.GetTd(hash), hash, self.genesisBlock.Hash() +} + +// SetValidator sets the validator which is used to validate incoming headers. +func (self *LightChain) SetValidator(validator core.HeaderValidator) { + self.procmu.Lock() + defer self.procmu.Unlock() + self.validator = validator +} + +// Validator returns the current header validator. +func (self *LightChain) Validator() core.HeaderValidator { + self.procmu.RLock() + defer self.procmu.RUnlock() + return self.validator +} + +// State returns a new mutable state based on the current HEAD block. +func (self *LightChain) State() *LightState { + return NewLightState(StateTrieID(self.hc.CurrentHeader()), self.odr) +} + +// Reset purges the entire blockchain, restoring it to its genesis state. +func (bc *LightChain) Reset() { + bc.ResetWithGenesisBlock(bc.genesisBlock) +} + +// ResetWithGenesisBlock purges the entire blockchain, restoring it to the +// specified genesis state. +func (bc *LightChain) ResetWithGenesisBlock(genesis *types.Block) { + // Dump the entire block chain and purge the caches + bc.SetHead(0) + + bc.mu.Lock() + defer bc.mu.Unlock() + + // Prepare the genesis block and reinitialise the chain + if err := core.WriteTd(bc.chainDb, genesis.Hash(), genesis.Difficulty()); err != nil { + glog.Fatalf("failed to write genesis block TD: %v", err) + } + if err := core.WriteBlock(bc.chainDb, genesis); err != nil { + glog.Fatalf("failed to write genesis block: %v", err) + } + bc.genesisBlock = genesis + bc.hc.SetGenesis(bc.genesisBlock.Header()) + bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) +} + +// Accessors + +// Genesis returns the genesis block +func (bc *LightChain) Genesis() *types.Block { + return bc.genesisBlock +} + +// GetBody retrieves a block body (transactions and uncles) from the database +// or ODR service by hash, caching it if found. +func (self *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.Body, error) { + // Short circuit if the body's already in the cache, retrieve otherwise + if cached, ok := self.bodyCache.Get(hash); ok { + body := cached.(*types.Body) + return body, nil + } + body, err := GetBody(ctx, self.odr, hash) + if err != nil { + return nil, err + } + // Cache the found body for next time and return + self.bodyCache.Add(hash, body) + return body, nil +} + +// GetBodyRLP retrieves a block body in RLP encoding from the database or +// ODR service by hash, caching it if found. +func (self *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.RawValue, error) { + // Short circuit if the body's already in the cache, retrieve otherwise + if cached, ok := self.bodyRLPCache.Get(hash); ok { + return cached.(rlp.RawValue), nil + } + body, err := GetBodyRLP(ctx, self.odr, hash) + if err != nil { + return nil, err + } + // Cache the found body for next time and return + self.bodyRLPCache.Add(hash, body) + return body, nil +} + +// HasBlock checks if a block is fully present in the database or not, caching +// it if present. +func (bc *LightChain) HasBlock(hash common.Hash) bool { + blk, _ := bc.GetBlock(NoOdr, hash) + return blk != nil +} + +// GetBlock retrieves a block from the database or ODR service by hash, +// caching it if found. +func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { + // Short circuit if the block's already in the cache, retrieve otherwise + if block, ok := self.blockCache.Get(hash); ok { + return block.(*types.Block), nil + } + block, err := GetBlock(ctx, self.odr, hash) + if err != nil { + return nil, err + } + // Cache the found block for next time and return + self.blockCache.Add(block.Hash(), block) + return block, nil +} + +// GetBlockByNumber retrieves a block from the database or ODR service by +// number, caching it (associated with its hash) if found. +func (self *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error) { + hash := core.GetCanonicalHash(self.chainDb, number) + if hash == (common.Hash{}) { + return nil, nil + } + return self.GetBlock(ctx, hash) +} + +// Stop stops the blockchain service. If any imports are currently in progress +// it will abort them using the procInterrupt. +func (bc *LightChain) Stop() { + if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) { + return + } + close(bc.quit) + atomic.StoreInt32(&bc.procInterrupt, 1) + + bc.wg.Wait() + + glog.V(logger.Info).Infoln("Chain manager stopped") +} + +// Rollback is designed to remove a chain of links from the database that aren't +// certain enough to be valid. +func (self *LightChain) Rollback(chain []common.Hash) { + self.mu.Lock() + defer self.mu.Unlock() + + for i := len(chain) - 1; i >= 0; i-- { + hash := chain[i] + + if self.hc.CurrentHeader().Hash() == hash { + self.hc.SetCurrentHeader(self.GetHeader(self.hc.CurrentHeader().ParentHash)) + } + } +} + +// postChainEvents iterates over the events generated by a chain insertion and +// posts them into the event mux. +func (self *LightChain) postChainEvents(events []interface{}) { + for _, event := range events { + if event, ok := event.(LightChainEvent); ok { + if self.LastBlockHash() == event.Hash { + self.eventMux.Post(LightChainHeadEvent{event.Header}) + } + } + // Fire the insertion events individually too + self.eventMux.Post(event) + } +} + +// InsertHeaderChain attempts to insert the given header chain in to the local +// chain, possibly creating a reorg. If an error is returned, it will return the +// index number of the failing header as well an error describing what went wrong. +// +// The verify parameter can be used to fine tune whether nonce verification +// should be done or not. The reason behind the optional check is because some +// of the header retrieval mechanisms already need to verfy nonces, as well as +// because nonces can be verified sparsely, not needing to check each. +// +// In the case of a light chain, InsertHeaderChain also creates and posts light +// chain events when necessary. +func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { + // Make sure only one thread manipulates the chain at once + self.chainmu.Lock() + defer self.chainmu.Unlock() + + self.wg.Add(1) + defer self.wg.Done() + + var events []interface{} + whFunc := func(header *types.Header) error { + self.mu.Lock() + defer self.mu.Unlock() + + status, err := self.hc.WriteHeader(header) + + switch status { + case core.CanonStatTy: + if glog.V(logger.Debug) { + glog.Infof("[%v] inserted header #%d (%x...).\n", time.Now().UnixNano(), header.Number, header.Hash().Bytes()[0:4]) + } + events = append(events, LightChainEvent{header, header.Hash()}) + + case core.SideStatTy: + if glog.V(logger.Detail) { + glog.Infof("inserted forked header #%d (TD=%v) (%x...).\n", header.Number, header.Difficulty, header.Hash().Bytes()[0:4]) + } + events = append(events, LightChainSideEvent{header}) + + case core.SplitStatTy: + events = append(events, LightChainSplitEvent{header}) + } + + return err + } + i, err := self.hc.InsertHeaderChain(chain, checkFreq, whFunc) + go self.postChainEvents(events) + return i, err +} + +// CurrentHeader retrieves the current head header of the canonical chain. The +// header is retrieved from the HeaderChain's internal cache. +func (self *LightChain) CurrentHeader() *types.Header { + self.mu.RLock() + defer self.mu.RUnlock() + + return self.hc.CurrentHeader() +} + +// GetTd retrieves a block's total difficulty in the canonical chain from the +// database by hash, caching it if found. +func (self *LightChain) GetTd(hash common.Hash) *big.Int { + return self.hc.GetTd(hash) +} + +// GetHeader retrieves a block header from the database by hash, caching it if +// found. +func (self *LightChain) GetHeader(hash common.Hash) *types.Header { + return self.hc.GetHeader(hash) +} + +// HasHeader checks if a block header is present in the database or not, caching +// it if present. +func (bc *LightChain) HasHeader(hash common.Hash) bool { + return bc.hc.HasHeader(hash) +} + +// GetBlockHashesFromHash retrieves a number of block hashes starting at a given +// hash, fetching towards the genesis block. +func (self *LightChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { + return self.hc.GetBlockHashesFromHash(hash, max) +} + +// GetHeaderByNumber retrieves a block header from the database by number, +// caching it (associated with its hash) if found. +func (self *LightChain) GetHeaderByNumber(number uint64) *types.Header { + return self.hc.GetHeaderByNumber(number) +} diff --git a/light/lightchain_test.go b/light/lightchain_test.go new file mode 100644 index 0000000000..5b9b7cdfc6 --- /dev/null +++ b/light/lightchain_test.go @@ -0,0 +1,399 @@ +// 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 . + +package light + +import ( + "fmt" + "math/big" + "runtime" + "testing" + + "github.com/ethereum/ethash" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/pow" + "github.com/hashicorp/golang-lru" + "golang.org/x/net/context" +) + +// So we can deterministically seed different blockchains +var ( + canonicalSeed = 1 + forkSeed = 2 +) + +// makeHeaderChain creates a deterministic chain of headers rooted at parent. +func makeHeaderChain(parent *types.Header, n int, db ethdb.Database, seed int) []*types.Header { + blocks, _ := core.GenerateChain(types.NewBlockWithHeader(parent), db, n, func(i int, b *core.BlockGen) { + b.SetCoinbase(common.Address{0: byte(seed), 19: byte(i)}) + }) + headers := make([]*types.Header, len(blocks)) + for i, block := range blocks { + headers[i] = block.Header() + } + return headers +} + +// newCanonical creates a chain database, and injects a deterministic canonical +// chain. Depending on the full flag, if creates either a full block chain or a +// header only chain. +func newCanonical(n int) (ethdb.Database, *LightChain, error) { + // Create te new chain database + db, _ := ethdb.NewMemDatabase() + evmux := &event.TypeMux{} + + // Initialize a fresh chain with only a genesis block + genesis, _ := core.WriteTestNetGenesisBlock(db) + + blockchain, _ := NewLightChain(&dummyOdr{db: db}, core.FakePow{}, evmux) + // Create and inject the requested chain + if n == 0 { + return db, blockchain, nil + } + // Header-only chain requested + headers := makeHeaderChain(genesis.Header(), n, db, canonicalSeed) + _, err := blockchain.InsertHeaderChain(headers, 1) + return db, blockchain, err +} + +func init() { + runtime.GOMAXPROCS(runtime.NumCPU()) +} + +func thePow() pow.PoW { + pow, _ := ethash.NewForTesting() + return pow +} + +func theLightChain(db ethdb.Database, t *testing.T) *LightChain { + var eventMux event.TypeMux + core.WriteTestNetGenesisBlock(db) + LightChain, err := NewLightChain(&dummyOdr{db: db}, thePow(), &eventMux) + if err != nil { + t.Error("failed creating LightChain:", err) + t.FailNow() + return nil + } + + return LightChain +} + +// Test fork of length N starting from block i +func testFork(t *testing.T, LightChain *LightChain, i, n int, comparator func(td1, td2 *big.Int)) { + // Copy old chain up to #i into a new db + db, LightChain2, err := newCanonical(i) + if err != nil { + t.Fatal("could not make new canonical in testFork", err) + } + // Assert the chains have the same header/block at #i + var hash1, hash2 common.Hash + hash1 = LightChain.GetHeaderByNumber(uint64(i)).Hash() + hash2 = LightChain2.GetHeaderByNumber(uint64(i)).Hash() + if hash1 != hash2 { + t.Errorf("chain content mismatch at %d: have hash %v, want hash %v", i, hash2, hash1) + } + // Extend the newly created chain + var ( + headerChainB []*types.Header + ) + headerChainB = makeHeaderChain(LightChain2.CurrentHeader(), n, db, forkSeed) + if _, err := LightChain2.InsertHeaderChain(headerChainB, 1); err != nil { + t.Fatalf("failed to insert forking chain: %v", err) + } + // Sanity check that the forked chain can be imported into the original + var tdPre, tdPost *big.Int + + tdPre = LightChain.GetTd(LightChain.CurrentHeader().Hash()) + if err := testHeaderChainImport(headerChainB, LightChain); err != nil { + t.Fatalf("failed to import forked header chain: %v", err) + } + tdPost = LightChain.GetTd(headerChainB[len(headerChainB)-1].Hash()) + // Compare the total difficulties of the chains + comparator(tdPre, tdPost) +} + +func printChain(bc *LightChain) { + for i := bc.CurrentHeader().GetNumberU64(); i > 0; i-- { + b := bc.GetHeaderByNumber(uint64(i)) + fmt.Printf("\t%x %v\n", b.Hash(), b.Difficulty) + } +} + +// testHeaderChainImport tries to process a chain of header, writing them into +// the database if successful. +func testHeaderChainImport(chain []*types.Header, LightChain *LightChain) error { + for _, header := range chain { + // Try and validate the header + if err := LightChain.Validator().ValidateHeader(header, LightChain.GetHeader(header.ParentHash), false); err != nil { + return err + } + // Manually insert the header into the database, but don't reorganize (allows subsequent testing) + LightChain.mu.Lock() + core.WriteTd(LightChain.chainDb, header.Hash(), new(big.Int).Add(header.Difficulty, LightChain.GetTd(header.ParentHash))) + core.WriteHeader(LightChain.chainDb, header) + LightChain.mu.Unlock() + } + return nil +} + +// Tests that given a starting canonical chain of a given size, it can be extended +// with various length chains. +func TestExtendCanonicalHeaders(t *testing.T) { + length := 5 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + better := func(td1, td2 *big.Int) { + if td2.Cmp(td1) <= 0 { + t.Errorf("total difficulty mismatch: have %v, expected more than %v", td2, td1) + } + } + // Start fork from current height + testFork(t, processor, length, 1, better) + testFork(t, processor, length, 2, better) + testFork(t, processor, length, 5, better) + testFork(t, processor, length, 10, better) +} + +// Tests that given a starting canonical chain of a given size, creating shorter +// forks do not take canonical ownership. +func TestShorterForkHeaders(t *testing.T) { + length := 10 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + worse := func(td1, td2 *big.Int) { + if td2.Cmp(td1) >= 0 { + t.Errorf("total difficulty mismatch: have %v, expected less than %v", td2, td1) + } + } + // Sum of numbers must be less than `length` for this to be a shorter fork + testFork(t, processor, 0, 3, worse) + testFork(t, processor, 0, 7, worse) + testFork(t, processor, 1, 1, worse) + testFork(t, processor, 1, 7, worse) + testFork(t, processor, 5, 3, worse) + testFork(t, processor, 5, 4, worse) +} + +// Tests that given a starting canonical chain of a given size, creating longer +// forks do take canonical ownership. +func TestLongerForkHeaders(t *testing.T) { + length := 10 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + better := func(td1, td2 *big.Int) { + if td2.Cmp(td1) <= 0 { + t.Errorf("total difficulty mismatch: have %v, expected more than %v", td2, td1) + } + } + // Sum of numbers must be greater than `length` for this to be a longer fork + testFork(t, processor, 0, 11, better) + testFork(t, processor, 0, 15, better) + testFork(t, processor, 1, 10, better) + testFork(t, processor, 1, 12, better) + testFork(t, processor, 5, 6, better) + testFork(t, processor, 5, 8, better) +} + +// Tests that given a starting canonical chain of a given size, creating equal +// forks do take canonical ownership. +func TestEqualForkHeaders(t *testing.T) { + length := 10 + + // Make first chain starting from genesis + _, processor, err := newCanonical(length) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Define the difficulty comparator + equal := func(td1, td2 *big.Int) { + if td2.Cmp(td1) != 0 { + t.Errorf("total difficulty mismatch: have %v, want %v", td2, td1) + } + } + // Sum of numbers must be equal to `length` for this to be an equal fork + testFork(t, processor, 0, 10, equal) + testFork(t, processor, 1, 9, equal) + testFork(t, processor, 2, 8, equal) + testFork(t, processor, 5, 5, equal) + testFork(t, processor, 6, 4, equal) + testFork(t, processor, 9, 1, equal) +} + +// Tests that chains missing links do not get accepted by the processor. +func TestBrokenHeaderChain(t *testing.T) { + // Make chain starting from genesis + db, LightChain, err := newCanonical(10) + if err != nil { + t.Fatalf("failed to make new canonical chain: %v", err) + } + // Create a forked chain, and try to insert with a missing link + chain := makeHeaderChain(LightChain.CurrentHeader(), 5, db, forkSeed)[1:] + if err := testHeaderChainImport(chain, LightChain); err == nil { + t.Errorf("broken header chain not reported") + } +} + +type bproc struct{} + +func (bproc) ValidateHeader(*types.Header, *types.Header, bool) error { return nil } + +func makeHeaderChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Header { + var chain []*types.Header + for i, difficulty := range d { + header := &types.Header{ + Coinbase: common.Address{seed}, + Number: big.NewInt(int64(i + 1)), + Difficulty: big.NewInt(int64(difficulty)), + UncleHash: types.EmptyUncleHash, + TxHash: types.EmptyRootHash, + ReceiptHash: types.EmptyRootHash, + } + if i == 0 { + header.ParentHash = genesis.Hash() + } else { + header.ParentHash = chain[i-1].Hash() + } + chain = append(chain, types.CopyHeader(header)) + } + return chain +} + +type dummyOdr struct { + OdrBackend + db ethdb.Database +} + +func (odr *dummyOdr) Database() ethdb.Database { + return odr.db +} + +func (odr *dummyOdr) Retrieve(ctx context.Context, req OdrRequest) error { + return nil +} + +func chm(genesis *types.Block, db ethdb.Database) *LightChain { + odr := &dummyOdr{db: db} + var eventMux event.TypeMux + bc := &LightChain{odr: odr, chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: core.FakePow{}} + bc.hc, _ = core.NewHeaderChain(db, bc.Validator, bc.getProcInterrupt) + bc.bodyCache, _ = lru.New(100) + bc.bodyRLPCache, _ = lru.New(100) + bc.blockCache, _ = lru.New(100) + bc.SetValidator(bproc{}) + bc.ResetWithGenesisBlock(genesis) + + return bc +} + +// Tests that reorganizing a long difficult chain after a short easy one +// overwrites the canonical numbers and links in the database. +func TestReorgLongHeaders(t *testing.T) { + testReorg(t, []int{1, 2, 4}, []int{1, 2, 3, 4}, 10) +} + +// Tests that reorganizing a short difficult chain after a long easy one +// overwrites the canonical numbers and links in the database. +func TestReorgShortHeaders(t *testing.T) { + testReorg(t, []int{1, 2, 3, 4}, []int{1, 10}, 11) +} + +func testReorg(t *testing.T, first, second []int, td int64) { + // Create a pristine block chain + db, _ := ethdb.NewMemDatabase() + genesis, _ := core.WriteTestNetGenesisBlock(db) + bc := chm(genesis, db) + + // Insert an easy and a difficult chain afterwards + bc.InsertHeaderChain(makeHeaderChainWithDiff(genesis, first, 11), 1) + bc.InsertHeaderChain(makeHeaderChainWithDiff(genesis, second, 22), 1) + // Check that the chain is valid number and link wise + prev := bc.CurrentHeader() + for header := bc.GetHeaderByNumber(bc.CurrentHeader().Number.Uint64() - 1); header.Number.Uint64() != 0; prev, header = header, bc.GetHeaderByNumber(header.Number.Uint64()-1) { + if prev.ParentHash != header.Hash() { + t.Errorf("parent header hash mismatch: have %x, want %x", prev.ParentHash, header.Hash()) + } + } + // Make sure the chain total difficulty is the correct one + want := new(big.Int).Add(genesis.Difficulty(), big.NewInt(td)) + if have := bc.GetTd(bc.CurrentHeader().Hash()); have.Cmp(want) != 0 { + t.Errorf("total difficulty mismatch: have %v, want %v", have, want) + } +} + +// Tests that the insertion functions detect banned hashes. +func TestBadHeaderHashes(t *testing.T) { + // Create a pristine block chain + db, _ := ethdb.NewMemDatabase() + genesis, _ := core.WriteTestNetGenesisBlock(db) + bc := chm(genesis, db) + + // Create a chain, ban a hash and try to import + var err error + headers := makeHeaderChainWithDiff(genesis, []int{1, 2, 4}, 10) + core.BadHashes[headers[2].Hash()] = true + _, err = bc.InsertHeaderChain(headers, 1) + if !core.IsBadHashError(err) { + t.Errorf("error mismatch: want: BadHashError, have: %v", err) + } +} + +// Tests that bad hashes are detected on boot, and the chan rolled back to a +// good state prior to the bad hash. +func TestReorgBadHeaderHashes(t *testing.T) { + // Create a pristine block chain + db, _ := ethdb.NewMemDatabase() + genesis, _ := core.WriteTestNetGenesisBlock(db) + bc := chm(genesis, db) + + // Create a chain, import and ban aferwards + headers := makeHeaderChainWithDiff(genesis, []int{1, 2, 3, 4}, 10) + + if _, err := bc.InsertHeaderChain(headers, 1); err != nil { + t.Fatalf("failed to import headers: %v", err) + } + if bc.CurrentHeader().Hash() != headers[3].Hash() { + t.Errorf("last header hash mismatch: have: %x, want %x", bc.CurrentHeader().Hash(), headers[3].Hash()) + } + core.BadHashes[headers[3].Hash()] = true + defer func() { delete(core.BadHashes, headers[3].Hash()) }() + // Create a new chain manager and check it rolled back the state + ncm, err := NewLightChain(&dummyOdr{db: db}, core.FakePow{}, new(event.TypeMux)) + if err != nil { + t.Fatalf("failed to create new chain manager: %v", err) + } + if ncm.CurrentHeader().Hash() != headers[2].Hash() { + t.Errorf("last header hash mismatch: have: %x, want %x", ncm.CurrentHeader().Hash(), headers[2].Hash()) + } +} diff --git a/light/odr.go b/light/odr.go index 4c69040ef8..a61d2049f1 100644 --- a/light/odr.go +++ b/light/odr.go @@ -20,13 +20,19 @@ package light import ( "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/net/context" ) -// OdrBackend is an interface to a backend service that handles odr retrievals +// NoOdr is the default context passed to an ODR capable function when the ODR +// service is not required. +var NoOdr = context.Background() + +// OdrBackend is an interface to a backend service that handles ODR retrievals type OdrBackend interface { Database() ethdb.Database Retrieve(ctx context.Context, req OdrRequest) error @@ -37,17 +43,44 @@ type OdrRequest interface { StoreResult(db ethdb.Database) } +// TrieID identifies a state or account storage trie +type TrieID struct { + blockHash, root common.Hash + accKey []byte +} + +// StateTrieID returns a TrieID for a state trie belonging to a certain block +// header. +func StateTrieID(header *types.Header) *TrieID { + return &TrieID{ + blockHash: header.Hash(), + accKey: nil, + root: header.Root, + } +} + +// StorageTrieID returns a TrieID for a contract storage trie at a given account +// of a given state trie. It also requires the root hash of the trie for +// checking Merkle proofs. +func StorageTrieID(state *TrieID, addr common.Address, root common.Hash) *TrieID { + return &TrieID{ + blockHash: state.blockHash, + accKey: crypto.Keccak256(addr[:]), + root: root, + } +} + // TrieRequest is the ODR request type for state/storage trie entries type TrieRequest struct { OdrRequest - root common.Hash - key []byte - proof []rlp.RawValue + Id *TrieID + Key []byte + Proof []rlp.RawValue } // StoreResult stores the retrieved data in local database func (req *TrieRequest) StoreResult(db ethdb.Database) { - storeProof(db, req.proof) + storeProof(db, req.Proof) } // storeProof stores the new trie nodes obtained from a merkle proof in the database @@ -61,38 +94,39 @@ func storeProof(db ethdb.Database, proof []rlp.RawValue) { } } -// NodeDataRequest is the ODR request type for node data (used for retrieving contract code) -type NodeDataRequest struct { +// CodeRequest is the ODR request type for retrieving contract code +type CodeRequest struct { OdrRequest - hash common.Hash - data []byte -} - -// GetData returns the retrieved node data after a successful request -func (req *NodeDataRequest) GetData() []byte { - return req.data + Id *TrieID + Hash common.Hash + Data []byte } // StoreResult stores the retrieved data in local database -func (req *NodeDataRequest) StoreResult(db ethdb.Database) { - db.Put(req.hash[:], req.GetData()) +func (req *CodeRequest) StoreResult(db ethdb.Database) { + db.Put(req.Hash[:], req.Data) } -var sha3_nil = crypto.Keccak256Hash(nil) - -// retrieveNodeData tries to retrieve node data with the given hash from the network -func retrieveNodeData(ctx context.Context, odr OdrBackend, hash common.Hash) ([]byte, error) { - if hash == sha3_nil { - return nil, nil - } - res, _ := odr.Database().Get(hash[:]) - if res != nil { - return res, nil - } - r := &NodeDataRequest{hash: hash} - if err := odr.Retrieve(ctx, r); err != nil { - return nil, err - } else { - return r.GetData(), nil - } +// BlockRequest is the ODR request type for retrieving block bodies +type BlockRequest struct { + OdrRequest + Hash common.Hash + Rlp []byte +} + +// StoreResult stores the retrieved data in local database +func (req *BlockRequest) StoreResult(db ethdb.Database) { + core.WriteBodyRLP(db, req.Hash, req.Rlp) +} + +// ReceiptsRequest is the ODR request type for retrieving block bodies +type ReceiptsRequest struct { + OdrRequest + Hash common.Hash + Receipts types.Receipts +} + +// StoreResult stores the retrieved data in local database +func (req *ReceiptsRequest) StoreResult(db ethdb.Database) { + core.WriteBlockReceipts(db, req.Hash, req.Receipts) } diff --git a/light/odr_test.go b/light/odr_test.go new file mode 100644 index 0000000000..d868801b5e --- /dev/null +++ b/light/odr_test.go @@ -0,0 +1,323 @@ +package light + +import ( + "bytes" + "errors" + "math/big" + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" + "golang.org/x/net/context" +) + +var ( + testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) + testBankFunds = big.NewInt(100000000) + + acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") + acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") + acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey) + acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey) + + testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056") + testContractAddr common.Address +) + +type testOdr struct { + OdrBackend + sdb, ldb ethdb.Database + disable bool +} + +func (odr *testOdr) Database() ethdb.Database { + return odr.ldb +} + +var ErrOdrDisabled = errors.New("ODR disabled") + +func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error { + if odr.disable { + return ErrOdrDisabled + } + switch req := req.(type) { + case *BlockRequest: + req.Rlp = core.GetBodyRLP(odr.sdb, req.Hash) + case *ReceiptsRequest: + req.Receipts = core.GetBlockReceipts(odr.sdb, req.Hash) + case *TrieRequest: + t, _ := trie.New(req.Id.root, odr.sdb) + req.Proof = t.Prove(req.Key) + trie.ClearGlobalCache() + case *CodeRequest: + req.Data, _ = odr.sdb.Get(req.Hash[:]) + } + req.StoreResult(odr.ldb) + return nil +} + +type odrTestFn func(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte + +func TestOdrGetBlockLes1(t *testing.T) { testChainOdr(t, 1, 1, odrGetBlock) } + +func odrGetBlock(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + var block *types.Block + if bc != nil { + block = bc.GetBlock(bhash) + } else { + block, _ = lc.GetBlock(ctx, bhash) + } + if block == nil { + return nil + } + rlp, _ := rlp.EncodeToBytes(block) + return rlp +} + +func TestOdrGetReceiptsLes1(t *testing.T) { testChainOdr(t, 1, 1, odrGetReceipts) } + +func odrGetReceipts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + var receipts types.Receipts + if bc != nil { + receipts = core.GetBlockReceipts(db, bhash) + } else { + receipts, _ = GetBlockReceipts(ctx, lc.Odr(), bhash) + } + if receipts == nil { + return nil + } + rlp, _ := rlp.EncodeToBytes(receipts) + return rlp +} + +func TestOdrAccountsLes1(t *testing.T) { testChainOdr(t, 1, 1, odrAccounts) } + +func odrAccounts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678") + acc := []common.Address{testBankAddress, acc1Addr, acc2Addr, dummyAddr} + + trie.ClearGlobalCache() + + var res []byte + for _, addr := range acc { + if bc != nil { + header := bc.GetHeader(bhash) + st, err := state.New(header.Root, db) + if err == nil { + bal := st.GetBalance(addr) + rlp, _ := rlp.EncodeToBytes(bal) + res = append(res, rlp...) + } + } else { + header := lc.GetHeader(bhash) + st := NewLightState(StateTrieID(header), lc.Odr()) + bal, err := st.GetBalance(ctx, addr) + if err == nil { + rlp, _ := rlp.EncodeToBytes(bal) + res = append(res, rlp...) + } + } + } + + return res +} + +func TestOdrContractCallLes1(t *testing.T) { testChainOdr(t, 1, 2, odrContractCall) } + +// fullcallmsg is the message type used for call transations. +type fullcallmsg struct { + from *state.StateObject + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte +} + +// accessor boilerplate to implement core.Message +func (m fullcallmsg) From() (common.Address, error) { return m.from.Address(), nil } +func (m fullcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil } +func (m fullcallmsg) Nonce() uint64 { return m.from.Nonce() } +func (m fullcallmsg) To() *common.Address { return m.to } +func (m fullcallmsg) GasPrice() *big.Int { return m.gasPrice } +func (m fullcallmsg) Gas() *big.Int { return m.gas } +func (m fullcallmsg) Value() *big.Int { return m.value } +func (m fullcallmsg) Data() []byte { return m.data } + +// callmsg is the message type used for call transations. +type lightcallmsg struct { + from *StateObject + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte +} + +// accessor boilerplate to implement core.Message +func (m lightcallmsg) From() (common.Address, error) { return m.from.Address(), nil } +func (m lightcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil } +func (m lightcallmsg) Nonce() uint64 { return m.from.Nonce() } +func (m lightcallmsg) To() *common.Address { return m.to } +func (m lightcallmsg) GasPrice() *big.Int { return m.gasPrice } +func (m lightcallmsg) Gas() *big.Int { return m.gas } +func (m lightcallmsg) Value() *big.Int { return m.value } +func (m lightcallmsg) Data() []byte { return m.data } + +func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { + data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000") + + var res []byte + for i := 0; i < 3; i++ { + data[35] = byte(i) + if bc != nil { + header := bc.GetHeader(bhash) + statedb, err := state.New(header.Root, db) + if err == nil { + from := statedb.GetOrNewStateObject(testBankAddress) + from.SetBalance(common.MaxBig) + + msg := fullcallmsg{ + from: from, + gas: big.NewInt(100000), + gasPrice: big.NewInt(0), + value: big.NewInt(0), + data: data, + to: &testContractAddr, + } + + vmenv := core.NewEnv(statedb, bc, msg, header) + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, _, _ := core.ApplyMessage(vmenv, msg, gp) + res = append(res, ret...) + } + } else { + header := lc.GetHeader(bhash) + state := NewLightState(StateTrieID(header), lc.Odr()) + from, err := state.GetOrNewStateObject(ctx, testBankAddress) + if err == nil { + from.SetBalance(common.MaxBig) + + msg := lightcallmsg{ + from: from, + gas: big.NewInt(100000), + gasPrice: big.NewInt(0), + value: big.NewInt(0), + data: data, + to: &testContractAddr, + } + + vmenv := NewEnv(ctx, state, lc, msg, header) + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, _, _ := core.ApplyMessage(vmenv, msg, gp) + if vmenv.Error() == nil { + res = append(res, ret...) + } + } + } + } + return res +} + +func testChainGen(i int, block *core.BlockGen) { + switch i { + case 0: + // In block 1, the test bank sends account #1 some ether. + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) + block.AddTx(tx) + case 1: + // In block 2, the test bank sends some more ether to account #1. + // acc1Addr passes it on to account #2. + // acc1Addr creates a test contract. + tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey) + nonce := block.TxNonce(acc1Addr) + tx2, _ := types.NewTransaction(nonce, acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key) + nonce++ + tx3, _ := types.NewContractCreation(nonce, big.NewInt(0), big.NewInt(100000), big.NewInt(0), testContractCode).SignECDSA(acc1Key) + testContractAddr = crypto.CreateAddress(acc1Addr, nonce) + block.AddTx(tx1) + block.AddTx(tx2) + block.AddTx(tx3) + case 2: + // Block 3 is empty but was mined by account #2. + block.SetCoinbase(acc2Addr) + block.SetExtra([]byte("yeehaw")) + data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001") + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data).SignECDSA(testBankKey) + block.AddTx(tx) + case 3: + // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data). + b2 := block.PrevBlock(1).Header() + b2.Extra = []byte("foo") + block.AddUncle(b2) + b3 := block.PrevBlock(2).Header() + b3.Extra = []byte("foo") + block.AddUncle(b3) + data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002") + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data).SignECDSA(testBankKey) + block.AddTx(tx) + } +} + +func testChainOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { + var ( + evmux = new(event.TypeMux) + pow = new(core.FakePow) + sdb, _ = ethdb.NewMemDatabase() + ldb, _ = ethdb.NewMemDatabase() + genesis = core.WriteGenesisBlockForTesting(sdb, core.GenesisAccount{testBankAddress, testBankFunds}) + ) + core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{testBankAddress, testBankFunds}) + // Assemble the test environment + blockchain, _ := core.NewBlockChain(sdb, pow, evmux) + gchain, _ := core.GenerateChain(genesis, sdb, 4, testChainGen) + if _, err := blockchain.InsertChain(gchain); err != nil { + panic(err) + } + + odr := &testOdr{sdb: sdb, ldb: ldb} + lightchain, _ := NewLightChain(odr, pow, evmux) + lightchain.SetValidator(bproc{}) + headers := make([]*types.Header, len(gchain)) + for i, block := range gchain { + headers[i] = block.Header() + } + if _, err := lightchain.InsertHeaderChain(headers, 1); err != nil { + panic(err) + } + + test := func(expFail uint64) { + for i := uint64(0); i <= blockchain.CurrentHeader().GetNumberU64(); i++ { + bhash := core.GetCanonicalHash(sdb, i) + b1 := fn(NoOdr, sdb, blockchain, nil, bhash) + ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond) + b2 := fn(ctx, ldb, nil, lightchain, bhash) + eq := bytes.Equal(b1, b2) + exp := i < expFail + if exp && !eq { + t.Errorf("odr mismatch") + } + if !exp && eq { + t.Errorf("unexpected odr match") + } + } + } + + odr.disable = true + // expect retrievals to fail (except genesis block) without a les peer + test(expFail) + odr.disable = false + // expect all retrievals to pass + test(5) + odr.disable = true + // still expect all retrievals to pass, now data should be cached locally + test(5) +} diff --git a/light/odr_util.go b/light/odr_util.go new file mode 100644 index 0000000000..1e1b7f2546 --- /dev/null +++ b/light/odr_util.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 . +package light + +import ( + "bytes" + "errors" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/rlp" + "golang.org/x/net/context" +) + +var sha3_nil = crypto.Keccak256Hash(nil) + +var ErrNoHeader = errors.New("Block header not found") + +// retrieveContractCode tries to retrieve the contract code of the given account +// with the given hash from the network (id points to the storage trie belonging +// to the same account) +func retrieveContractCode(ctx context.Context, odr OdrBackend, id *TrieID, hash common.Hash) ([]byte, error) { + if hash == sha3_nil { + return nil, nil + } + res, _ := odr.Database().Get(hash[:]) + if res != nil { + return res, nil + } + r := &CodeRequest{Id: id, Hash: hash} + if err := odr.Retrieve(ctx, r); err != nil { + return nil, err + } else { + return r.Data, nil + } +} + +// GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. +func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash) (rlp.RawValue, error) { + if data := core.GetBodyRLP(odr.Database(), hash); data != nil { + return data, nil + } + r := &BlockRequest{Hash: hash} + if err := odr.Retrieve(ctx, r); err != nil { + return nil, err + } else { + return r.Rlp, nil + } +} + +// GetBody retrieves the block body (transactons, uncles) corresponding to the +// hash. +func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Body, error) { + data, err := GetBodyRLP(ctx, odr, hash) + if err != nil { + return nil, err + } + body := new(types.Body) + if err := rlp.Decode(bytes.NewReader(data), body); err != nil { + glog.V(logger.Error).Infof("invalid block body RLP for hash %x: %v", hash, err) + return nil, err + } + return body, nil +} + +// GetBlock retrieves an entire block corresponding to the hash, assembling it +// back from the stored header and body. +func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Block, error) { + // Retrieve the block header and body contents + header := core.GetHeader(odr.Database(), hash) + if header == nil { + return nil, ErrNoHeader + } + body, err := GetBody(ctx, odr, hash) + if err != nil { + return nil, err + } + // Reassemble the block and return + return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil +} + +// GetBlockReceipts retrieves the receipts generated by the transactions included +// in a block given by its hash. +func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash) (types.Receipts, error) { + receipts := core.GetBlockReceipts(odr.Database(), hash) + if receipts != nil { + return receipts, nil + } + r := &ReceiptsRequest{Hash: hash} + if err := odr.Retrieve(ctx, r); err != nil { + return nil, err + } else { + return r.Receipts, nil + } +} diff --git a/light/state.go b/light/state.go index e18f9cdc5d..08bb8e5bac 100644 --- a/light/state.go +++ b/light/state.go @@ -33,10 +33,11 @@ var StartingNonce uint64 // state, retrieving unknown parts on-demand from the ODR backend. Changes are // never stored in the local database, only in the memory objects. type LightState struct { - odr OdrBackend - trie *LightTrie - + odr OdrBackend + trie *LightTrie + id *TrieID stateObjects map[string]*StateObject + refund *big.Int } // NewLightState creates a new LightState with the specified root. @@ -44,15 +45,25 @@ type LightState struct { // root is non-existent. In that case, ODR retrieval will always be unsuccessful // and every operation will return with an error or wait for the context to be // cancelled. -func NewLightState(root common.Hash, odr OdrBackend) *LightState { - tr := NewLightTrie(root, odr, true) +func NewLightState(id *TrieID, odr OdrBackend) *LightState { + var tr *LightTrie + if id != nil { + tr = NewLightTrie(id, odr, true) + } return &LightState{ odr: odr, trie: tr, + id: id, stateObjects: make(map[string]*StateObject), + refund: new(big.Int), } } +// AddRefund adds an amount to the refund value collected during a vm execution +func (self *LightState) AddRefund(gas *big.Int) { + self.refund.Add(self.refund, gas) +} + // HasAccount returns true if an account exists at the given address func (self *LightState) HasAccount(ctx context.Context, addr common.Address) (bool, error) { so, err := self.GetStateObject(ctx, addr) @@ -194,7 +205,7 @@ func (self *LightState) GetStateObject(ctx context.Context, addr common.Address) return nil, nil } - stateObject, err = DecodeObject(ctx, addr, self.odr, []byte(data)) + stateObject, err = DecodeObject(ctx, self.id, addr, self.odr, []byte(data)) if err != nil { return nil, err } @@ -258,12 +269,14 @@ func (self *LightState) CreateStateObject(ctx context.Context, addr common.Addre // Copy creates a copy of the state func (self *LightState) Copy() *LightState { // ignore error - we assume state-to-be-copied always exists - state := NewLightState(common.Hash{}, self.odr) + state := NewLightState(nil, self.odr) state.trie = self.trie + state.id = self.id for k, stateObject := range self.stateObjects { state.stateObjects[k] = stateObject.Copy() } + state.refund.Set(self.refund) return state } @@ -272,4 +285,10 @@ func (self *LightState) Copy() *LightState { func (self *LightState) Set(state *LightState) { self.trie = state.trie self.stateObjects = state.stateObjects + self.refund = state.refund +} + +// GetRefund returns the refund value collected during a vm execution +func (self *LightState) GetRefund() *big.Int { + return self.refund } diff --git a/light/state_object.go b/light/state_object.go index 030653c770..374f445216 100644 --- a/light/state_object.go +++ b/light/state_object.go @@ -102,7 +102,7 @@ func NewStateObject(address common.Address, odr OdrBackend) *StateObject { codeHash: emptyCodeHash, storage: make(Storage), } - object.trie = NewLightTrie(common.Hash{}, odr, true) + object.trie = NewLightTrie(nil, odr, true) return object } @@ -181,6 +181,9 @@ func (c *StateObject) SetBalance(amount *big.Int) { c.dirty = true } +// ReturnGas returns the gas back to the origin. Used by the Virtual machine or Closures +func (c *StateObject) ReturnGas(gas, price *big.Int) {} + // Copy creates a copy of the state object func (self *StateObject) Copy() *StateObject { stateObject := NewStateObject(self.Address(), self.odr) @@ -235,6 +238,23 @@ func (self *StateObject) Nonce() uint64 { return self.nonce } +// EachStorage calls a callback function for every key/value pair found +// in the local storage cache. Note that unlike core/state.StateObject, +// light.StateObject only returns cached values and doesn't download the +// entire storage tree. +func (self *StateObject) EachStorage(cb func(key, value []byte)) { + for h, v := range self.storage { + cb([]byte(h), v.Bytes()) + } +} + +// 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 (self *StateObject) Value() *big.Int { + panic("Value on StateObject should never be called") +} + // Encoding type extStateObject struct { @@ -245,7 +265,7 @@ type extStateObject struct { } // DecodeObject decodes an RLP-encoded state object. -func DecodeObject(ctx context.Context, address common.Address, odr OdrBackend, data []byte) (*StateObject, error) { +func DecodeObject(ctx context.Context, stateID *TrieID, address common.Address, odr OdrBackend, data []byte) (*StateObject, error) { var ( obj = &StateObject{address: address, odr: odr, storage: make(Storage)} ext extStateObject @@ -254,9 +274,10 @@ func DecodeObject(ctx context.Context, address common.Address, odr OdrBackend, d if err = rlp.DecodeBytes(data, &ext); err != nil { return nil, err } - obj.trie = NewLightTrie(ext.Root, odr, true) + trieID := StorageTrieID(stateID, address, ext.Root) + obj.trie = NewLightTrie(trieID, odr, true) if !bytes.Equal(ext.CodeHash, emptyCodeHash) { - if obj.code, err = retrieveNodeData(ctx, obj.odr, common.BytesToHash(ext.CodeHash)); err != nil { + if obj.code, err = retrieveContractCode(ctx, obj.odr, trieID, common.BytesToHash(ext.CodeHash)); err != nil { return nil, fmt.Errorf("can't find code for hash %x: %v", ext.CodeHash, err) } } diff --git a/light/state_test.go b/light/state_test.go index 2c2e6daea1..88bf7456b3 100644 --- a/light/state_test.go +++ b/light/state_test.go @@ -22,34 +22,14 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" "golang.org/x/net/context" ) -type testOdr struct { - OdrBackend - sdb, ldb ethdb.Database -} - -func (odr *testOdr) Database() ethdb.Database { - return odr.ldb -} - -func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error { - switch req := req.(type) { - case *TrieRequest: - t, _ := trie.New(req.root, odr.sdb) - req.proof = t.Prove(req.key) - trie.ClearGlobalCache() - case *NodeDataRequest: - req.data, _ = odr.sdb.Get(req.hash[:]) - } - req.StoreResult(odr.ldb) - return nil -} - func makeTestState() (common.Hash, ethdb.Database) { sdb, _ := ethdb.NewMemDatabase() st, _ := state.New(common.Hash{}, sdb) @@ -71,9 +51,11 @@ func makeTestState() (common.Hash, ethdb.Database) { func TestLightStateOdr(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root} + core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} - ls := NewLightState(root, odr) + ls := NewLightState(StateTrieID(header), odr) ctx := context.Background() trie.ClearGlobalCache() @@ -156,9 +138,11 @@ func TestLightStateOdr(t *testing.T) { func TestLightStateSetCopy(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root} + core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} - ls := NewLightState(root, odr) + ls := NewLightState(StateTrieID(header), odr) ctx := context.Background() trie.ClearGlobalCache() @@ -233,9 +217,11 @@ func TestLightStateSetCopy(t *testing.T) { func TestLightStateDelete(t *testing.T) { root, sdb := makeTestState() + header := &types.Header{Root: root} + core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} - ls := NewLightState(root, odr) + ls := NewLightState(StateTrieID(header), odr) ctx := context.Background() trie.ClearGlobalCache() diff --git a/light/trie.go b/light/trie.go index e9c96ea48e..fa9dcbc791 100644 --- a/light/trie.go +++ b/light/trie.go @@ -17,7 +17,6 @@ package light import ( - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" "golang.org/x/net/context" @@ -25,28 +24,28 @@ import ( // LightTrie is an ODR-capable wrapper around trie.SecureTrie type LightTrie struct { - trie *trie.SecureTrie - originalRoot common.Hash - odr OdrBackend - db ethdb.Database + trie *trie.SecureTrie + id *TrieID + odr OdrBackend + db ethdb.Database } // NewLightTrie creates a new LightTrie instance. It doesn't instantly try to // access the db or network and retrieve the root node, it only initializes its // encapsulated SecureTrie at the first actual operation. -func NewLightTrie(root common.Hash, odr OdrBackend, useFakeMap bool) *LightTrie { +func NewLightTrie(id *TrieID, odr OdrBackend, useFakeMap bool) *LightTrie { return &LightTrie{ // SecureTrie is initialized before first request - originalRoot: root, - odr: odr, - db: odr.Database(), + id: id, + odr: odr, + db: odr.Database(), } } // retrieveKey retrieves a single key, returns true and stores nodes in local // database if successful func (t *LightTrie) retrieveKey(ctx context.Context, key []byte) bool { - r := &TrieRequest{root: t.originalRoot, key: key} + r := &TrieRequest{Id: t.id, Key: key} return t.odr.Retrieve(ctx, r) == nil } @@ -79,7 +78,7 @@ func (t *LightTrie) do(ctx context.Context, fallbackKey []byte, fn func() error) func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.originalRoot, t.db) + t.trie, err = trie.NewSecure(t.id.root, t.db) } if err == nil { res, err = t.trie.TryGet(key) @@ -98,7 +97,7 @@ func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.originalRoot, t.db) + t.trie, err = trie.NewSecure(t.id.root, t.db) } if err == nil { err = t.trie.TryUpdate(key, value) @@ -112,7 +111,7 @@ func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) { func (t *LightTrie) Delete(ctx context.Context, key []byte) (err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.originalRoot, t.db) + t.trie, err = trie.NewSecure(t.id.root, t.db) } if err == nil { err = t.trie.TryDelete(key) diff --git a/light/txpool.go b/light/txpool.go new file mode 100644 index 0000000000..6e908f5d61 --- /dev/null +++ b/light/txpool.go @@ -0,0 +1,506 @@ +// 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 . + +package light + +import ( + "fmt" + "sync" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" + "golang.org/x/net/context" +) + +// txPermanent is the number of mined blocks after a mined transaction is +// considered permanent and no rollback is expected +var txPermanent = uint64(500) + +// TxPool implements the transaction pool for light clients, which keeps track +// of the status of locally created transactions, detecting if they are included +// in a block (mined) or rolled back. There are no queued transactions since we +// always receive all locally signed transactions in the same order as they are +// created. +type TxPool struct { + quit chan bool + eventMux *event.TypeMux + events event.Subscription + mu sync.RWMutex + chain *LightChain + odr OdrBackend + chainDb ethdb.Database + relay TxRelayBackend + head common.Hash + nonce map[common.Address]uint64 // "pending" nonce + pending map[common.Hash]*types.Transaction // pending transactions by tx hash + mined map[common.Hash][]*types.Transaction // mined transactions by block hash + clearIdx uint64 // earliest block nr that can contain mined tx info + + homestead bool +} + +// TxRelayBackend provides an interface to the mechanism that forwards transacions +// to the ETH network. The implementations of the functions should be non-blocking. +// +// Send instructs backend to forward new transactions +// NewHead notifies backend about a new head after processed by the tx pool, +// including mined and rolled back transactions since the last event +// Discard notifies backend about transactions that should be discarded either +// because they have been replaced by a re-send or because they have been mined +// long ago and no rollback is expected +type TxRelayBackend interface { + Send(txs types.Transactions) + NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) + Discard(hashes []common.Hash) +} + +// NewTxPool creates a new light transaction pool +func NewTxPool(eventMux *event.TypeMux, chain *LightChain, relay TxRelayBackend) *TxPool { + pool := &TxPool{ + nonce: make(map[common.Address]uint64), + pending: make(map[common.Hash]*types.Transaction), + mined: make(map[common.Hash][]*types.Transaction), + quit: make(chan bool), + eventMux: eventMux, + events: eventMux.Subscribe(LightChainHeadEvent{}), + chain: chain, + relay: relay, + odr: chain.Odr(), + chainDb: chain.Odr().Database(), + head: chain.CurrentHeader().Hash(), + clearIdx: chain.CurrentHeader().GetNumberU64(), + } + go pool.eventLoop() + + return pool +} + +// currentState returns the light state of the current head header +func (pool *TxPool) currentState() *LightState { + return NewLightState(StateTrieID(pool.chain.CurrentHeader()), pool.odr) +} + +// GetNonce returns the "pending" nonce of a given address. It always queries +// the nonce belonging to the latest header too in order to detect if another +// client using the same key sent a transaction. +func (pool *TxPool) GetNonce(ctx context.Context, addr common.Address) (uint64, error) { + nonce, err := pool.currentState().GetNonce(ctx, addr) + if err != nil { + return 0, err + } + sn, ok := pool.nonce[addr] + if ok && sn > nonce { + nonce = sn + } + if !ok || sn < nonce { + pool.nonce[addr] = nonce + } + return nonce, nil +} + +type txBlockData struct { + BlockHash common.Hash + BlockIndex uint64 + Index uint64 +} + +// storeTxBlockData stores the block position of a mined tx in the local db +func (pool *TxPool) storeTxBlockData(txh common.Hash, tbd txBlockData) { + data, _ := rlp.EncodeToBytes(tbd) + pool.chainDb.Put(append(txh[:], byte(1)), data) +} + +// removeTxBlockData removes the stored block position of a rolled back tx +func (pool *TxPool) removeTxBlockData(txh common.Hash) { + pool.chainDb.Delete(append(txh[:], byte(1))) +} + +// txStateChanges stores the recent changes between pending/mined states of +// transactions. True means mined, false means rolled back, no entry means no change +type txStateChanges map[common.Hash]bool + +// setState sets the status of a tx to either recently mined or recently rolled back +func (txc txStateChanges) setState(txHash common.Hash, mined bool) { + val, ent := txc[txHash] + if ent && (val != mined) { + delete(txc, txHash) + } else { + txc[txHash] = mined + } +} + +// getLists creates lists of mined and rolled back tx hashes +func (txc txStateChanges) getLists() (mined []common.Hash, rollback []common.Hash) { + for hash, val := range txc { + if val { + mined = append(mined, hash) + } else { + rollback = append(rollback, hash) + } + } + return +} + +// checkMinedTxs checks newly added blocks for the currently pending transactions +// and marks them as mined if necessary. It also stores block position in the db +// and adds them to the received txStateChanges map. +func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, idx uint64, txc txStateChanges) error { + if len(pool.pending) == 0 { + return nil + } + + receipts, err := GetBlockReceipts(ctx, pool.odr, hash) + if err != nil { + return err + } + list := pool.mined[hash] + for i, receipt := range receipts { + txHash := receipt.TxHash + if tx, ok := pool.pending[txHash]; ok { + pool.storeTxBlockData(txHash, txBlockData{hash, idx, uint64(i)}) + delete(pool.pending, txHash) + list = append(list, tx) + txc.setState(txHash, true) + } + } + if list != nil { + pool.mined[hash] = list + } + return nil +} + +// rollbackTxs marks the transactions contained in recently rolled back blocks +// as rolled back. It also removes block position info from the db and adds them +// to the received txStateChanges map. +func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) { + if list, ok := pool.mined[hash]; ok { + for _, tx := range list { + txHash := tx.Hash() + pool.removeTxBlockData(txHash) + pool.pending[txHash] = tx + txc.setState(txHash, false) + } + delete(pool.mined, hash) + } +} + +// setNewHead sets a new head header, processing (and rolling back if necessary) +// the blocks since the last known head and returns a txStateChanges map containing +// the recently mined and rolled back transaction hashes. If an error (context +// timeout) occurs during checking new blocks, it leaves the locally known head +// at the latest checked block and still returns a valid txStateChanges, making it +// possible to continue checking the missing blocks at the next chain head event +func (pool *TxPool) setNewHead(ctx context.Context, newHeader *types.Header) (txStateChanges, error) { + txc := make(txStateChanges) + oldh := pool.chain.GetHeader(pool.head) + newh := newHeader + // find common ancestor, create list of rolled back and new block hashes + var oldHashes, newHashes []common.Hash + for oldh.Hash() != newh.Hash() { + if oldh.GetNumberU64() >= newh.GetNumberU64() { + oldHashes = append(oldHashes, oldh.Hash()) + oldh = pool.chain.GetHeader(oldh.ParentHash) + } + if oldh.GetNumberU64() < newh.GetNumberU64() { + newHashes = append(newHashes, newh.Hash()) + newh = pool.chain.GetHeader(newh.ParentHash) + } + } + if oldh.GetNumberU64() < pool.clearIdx { + pool.clearIdx = oldh.GetNumberU64() + } + // roll back old blocks + for _, hash := range oldHashes { + pool.rollbackTxs(hash, txc) + } + pool.head = oldh.Hash() + // check mined txs of new blocks (array is in reversed order) + for i := len(newHashes) - 1; i >= 0; i-- { + hash := newHashes[i] + if err := pool.checkMinedTxs(ctx, hash, newHeader.GetNumberU64()-uint64(i), txc); err != nil { + return txc, err + } + pool.head = hash + } + + // clear old mined tx entries of old blocks + if idx := newHeader.GetNumberU64(); idx > pool.clearIdx+txPermanent { + idx2 := idx - txPermanent + for i := pool.clearIdx; i < idx2; i++ { + hash := core.GetCanonicalHash(pool.chainDb, i) + if list, ok := pool.mined[hash]; ok { + hashes := make([]common.Hash, len(list)) + for i, tx := range list { + hashes[i] = tx.Hash() + } + pool.relay.Discard(hashes) + delete(pool.mined, hash) + } + } + pool.clearIdx = idx2 + } + + return txc, nil +} + +// blockCheckTimeout is the time limit for checking new blocks for mined +// transactions. Checking resumes at the next chain head event if timed out. +const blockCheckTimeout = time.Second * 3 + +// eventLoop processes chain head events and also notifies the tx relay backend +// about the new head hash and tx state changes +func (pool *TxPool) eventLoop() { + for ev := range pool.events.Chan() { + switch ev.Data.(type) { + case LightChainHeadEvent: + pool.mu.Lock() + ctx, _ := context.WithTimeout(context.Background(), blockCheckTimeout) + head := pool.chain.CurrentHeader() + txc, _ := pool.setNewHead(ctx, head) + m, r := txc.getLists() + pool.relay.NewHead(pool.head, m, r) + pool.homestead = params.IsHomestead(head.Number) + pool.mu.Unlock() + } + } +} + +// Stop stops the light transaction pool +func (pool *TxPool) Stop() { + close(pool.quit) + pool.events.Unsubscribe() + glog.V(logger.Info).Infoln("Transaction pool stopped") +} + +// Stats returns the number of currently pending (locally created) transactions +func (pool *TxPool) Stats() (pending int) { + pool.mu.RLock() + defer pool.mu.RUnlock() + + pending = len(pool.pending) + return +} + +// validateTx checks whether a transaction is valid according to the consensus rules. +func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error { + // Validate sender + var ( + from common.Address + err error + ) + + // Validate the transaction sender and it's sig. Throw + // if the from fields is invalid. + if from, err = tx.From(); err != nil { + return core.ErrInvalidSender + } + + // Make sure the account exist. Non existent accounts + // haven't got funds and well therefor never pass. + currentState := pool.currentState() + if h, err := currentState.HasAccount(ctx, from); err == nil { + if !h { + return core.ErrNonExistentAccount + } + } else { + return err + } + + // Last but not least check for nonce errors + if n, err := currentState.GetNonce(ctx, from); err == nil { + if n > tx.Nonce() { + return core.ErrNonce + } + } else { + return err + } + + // Check the transaction doesn't exceed the current + // block limit gas. + header := pool.chain.GetHeader(pool.head) + if header.GasLimit.Cmp(tx.Gas()) < 0 { + return core.ErrGasLimit + } + + // Transactions can't be negative. This may never happen + // using RLP decoded transactions but may occur if you create + // a transaction using the RPC for example. + if tx.Value().Cmp(common.Big0) < 0 { + return core.ErrNegativeValue + } + + // Transactor should have enough funds to cover the costs + // cost == V + GP * GL + if b, err := currentState.GetBalance(ctx, from); err == nil { + if b.Cmp(tx.Cost()) < 0 { + return core.ErrInsufficientFunds + } + } else { + return err + } + + // Should supply enough intrinsic gas + if tx.Gas().Cmp(core.IntrinsicGas(tx.Data(), core.MessageCreatesContract(tx), pool.homestead)) < 0 { + return core.ErrIntrinsicGas + } + + return nil +} + +// add validates a new transaction and sets its state pending if processable. +// It also updates the locally stored nonce if necessary. +func (self *TxPool) add(ctx context.Context, tx *types.Transaction) error { + hash := tx.Hash() + + if self.pending[hash] != nil { + return fmt.Errorf("Known transaction (%x)", hash[:4]) + } + err := self.validateTx(ctx, tx) + if err != nil { + return err + } + + if _, ok := self.pending[hash]; !ok { + self.pending[hash] = tx + + nonce := tx.Nonce() + 1 + addr, _ := tx.From() + if nonce > self.nonce[addr] { + self.nonce[addr] = nonce + } + + // Notify the subscribers. This event is posted in a goroutine + // because it's possible that somewhere during the post "Remove transaction" + // gets called which will then wait for the global tx pool lock and deadlock. + go self.eventMux.Post(core.TxPreEvent{tx}) + } + + if glog.V(logger.Debug) { + var toname string + if to := tx.To(); to != nil { + toname = common.Bytes2Hex(to[:4]) + } else { + toname = "[NEW_CONTRACT]" + } + // we can ignore the error here because From is + // verified in ValidateTransaction. + f, _ := tx.From() + from := common.Bytes2Hex(f[:4]) + glog.Infof("(t) %x => %s (%v) %x\n", from, toname, tx.Value, hash) + } + + return nil +} + +// Add adds a transaction to the pool if valid and passes it to the tx relay +// backend +func (self *TxPool) Add(ctx context.Context, tx *types.Transaction) error { + self.mu.Lock() + defer self.mu.Unlock() + + data, err := rlp.EncodeToBytes(tx) + if err != nil { + return err + } + + if err := self.add(ctx, tx); err != nil { + return err + } + self.relay.Send(types.Transactions{tx}) + + self.chainDb.Put(tx.Hash().Bytes(), data) + return nil +} + +// AddTransactions adds all valid transactions to the pool and passes them to +// the tx relay backend +func (self *TxPool) AddTransactions(ctx context.Context, txs []*types.Transaction) { + self.mu.Lock() + defer self.mu.Unlock() + var sendTx types.Transactions + + for _, tx := range txs { + if err := self.add(ctx, tx); err != nil { + glog.V(logger.Debug).Infoln("tx error:", err) + } else { + sendTx = append(sendTx, tx) + h := tx.Hash() + glog.V(logger.Debug).Infof("tx %x\n", h[:4]) + } + } + + if len(sendTx) > 0 { + self.relay.Send(sendTx) + } +} + +// GetTransaction returns a transaction if it is contained in the pool +// and nil otherwise. +func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction { + // check the txs first + if tx, ok := tp.pending[hash]; ok { + return tx + } + return nil +} + +// GetTransactions returns all currently processable transactions. +// The returned slice may be modified by the caller. +func (self *TxPool) GetTransactions() (txs types.Transactions) { + self.mu.Lock() + defer self.mu.Unlock() + + txs = make(types.Transactions, len(self.pending)) + i := 0 + for _, tx := range self.pending { + txs[i] = tx + i++ + } + return txs +} + +// RemoveTransactions removes all given transactions from the pool. +func (self *TxPool) RemoveTransactions(txs types.Transactions) { + self.mu.Lock() + defer self.mu.Unlock() + var hashes []common.Hash + for _, tx := range txs { + //self.RemoveTx(tx.Hash()) + hash := tx.Hash() + delete(self.pending, hash) + self.chainDb.Delete(hash[:]) + hashes = append(hashes, hash) + } + self.relay.Discard(hashes) +} + +// RemoveTx removes the transaction with the given hash from the pool. +func (pool *TxPool) RemoveTx(hash common.Hash) { + pool.mu.Lock() + defer pool.mu.Unlock() + // delete from pending pool + delete(pool.pending, hash) + pool.chainDb.Delete(hash[:]) + pool.relay.Discard([]common.Hash{hash}) +} diff --git a/light/txpool_test.go b/light/txpool_test.go new file mode 100644 index 0000000000..03402f4c65 --- /dev/null +++ b/light/txpool_test.go @@ -0,0 +1,140 @@ +// 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 . + +package light + +import ( + "math" + "math/big" + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/params" + "golang.org/x/net/context" +) + +type testTxRelay struct { + send, nhMined, nhRollback, discard int +} + +func (self *testTxRelay) Send(txs types.Transactions) { + self.send = len(txs) +} + +func (self *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) { + self.nhMined = len(mined) + self.nhRollback = len(rollback) +} + +func (self *testTxRelay) Discard(hashes []common.Hash) { + self.discard = len(hashes) +} + +const poolTestTxs = 1000 +const poolTestBlocks = 100 + +// test tx 0..n-1 +var testTx [poolTestTxs]*types.Transaction + +// txs sent before block i +func sentTx(i int) int { + return int(math.Pow(float64(i)/float64(poolTestBlocks), 0.9) * poolTestTxs) +} + +// txs included in block i or before that (minedTx(i) <= sentTx(i)) +func minedTx(i int) int { + return int(math.Pow(float64(i)/float64(poolTestBlocks), 1.1) * poolTestTxs) +} + +func txPoolTestChainGen(i int, block *core.BlockGen) { + s := minedTx(i) + e := minedTx(i + 1) + for i := s; i < e; i++ { + block.AddTx(testTx[i]) + } +} + +func TestTxPool(t *testing.T) { + for i, _ := range testTx { + testTx[i], _ = types.NewTransaction(uint64(i), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) + } + + var ( + evmux = new(event.TypeMux) + pow = new(core.FakePow) + sdb, _ = ethdb.NewMemDatabase() + ldb, _ = ethdb.NewMemDatabase() + genesis = core.WriteGenesisBlockForTesting(sdb, core.GenesisAccount{testBankAddress, testBankFunds}) + ) + core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{testBankAddress, testBankFunds}) + // Assemble the test environment + blockchain, _ := core.NewBlockChain(sdb, pow, evmux) + gchain, _ := core.GenerateChain(genesis, sdb, poolTestBlocks, txPoolTestChainGen) + if _, err := blockchain.InsertChain(gchain); err != nil { + panic(err) + } + + odr := &testOdr{sdb: sdb, ldb: ldb} + relay := &testTxRelay{} + lightchain, _ := NewLightChain(odr, pow, evmux) + lightchain.SetValidator(bproc{}) + txPermanent = 50 + pool := NewTxPool(evmux, lightchain, relay) + + for ii, block := range gchain { + i := ii + 1 + ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond) + s := sentTx(i - 1) + e := sentTx(i) + for i := s; i < e; i++ { + relay.send = 0 + pool.Add(ctx, testTx[i]) + got := relay.send + exp := 1 + if got != exp { + t.Errorf("relay.Send expected len = %d, got %d", exp, got) + } + } + + relay.nhMined = 0 + relay.nhRollback = 0 + relay.discard = 0 + if _, err := lightchain.InsertHeaderChain([]*types.Header{block.Header()}, 1); err != nil { + panic(err) + } + time.Sleep(time.Millisecond * 10) + + got := relay.nhMined + exp := minedTx(i) - minedTx(i-1) + if got != exp { + t.Errorf("relay.NewHead expected len(mined) = %d, got %d", exp, got) + } + + got = relay.discard + exp = 0 + if i > int(txPermanent)+1 { + exp = minedTx(i-int(txPermanent)-1) - minedTx(i-int(txPermanent)-2) + } + if got != exp { + t.Errorf("relay.Discard expected len = %d, got %d", exp, got) + } + } +} diff --git a/light/vm_env.go b/light/vm_env.go new file mode 100644 index 0000000000..493190d74d --- /dev/null +++ b/light/vm_env.go @@ -0,0 +1,241 @@ +// 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 . + +package light + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "golang.org/x/net/context" +) + +// VMEnv is the light client version of the vm execution environment. +// Unlike other structures, VMEnv holds a context that is applied by state +// retrieval requests through the entire execution. If any state operation +// returns an error, the execution fails. +type VMEnv struct { + vm.Environment + ctx context.Context + state *VMState + header *types.Header + msg core.Message + depth int + chain *LightChain + typ vm.Type + // structured logging + logs []vm.StructLog + err error +} + +// NewEnv creates a new execution environment based on an ODR capable light state +func NewEnv(ctx context.Context, state *LightState, chain *LightChain, msg core.Message, header *types.Header) *VMEnv { + env := &VMEnv{ + chain: chain, + header: header, + msg: msg, + typ: vm.StdVmTy, + } + env.state = &VMState{ctx: ctx, state: state, env: env} + return env +} + +func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f } +func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number } +func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase } +func (self *VMEnv) Time() *big.Int { return self.header.Time } +func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty } +func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit } +func (self *VMEnv) Value() *big.Int { return self.msg.Value() } +func (self *VMEnv) Db() vm.Database { return self.state } +func (self *VMEnv) Depth() int { return self.depth } +func (self *VMEnv) SetDepth(i int) { self.depth = i } +func (self *VMEnv) VmType() vm.Type { return self.typ } +func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t } +func (self *VMEnv) GetHash(n uint64) common.Hash { + for header := self.chain.GetHeader(self.header.ParentHash); header != nil; header = self.chain.GetHeader(header.ParentHash) { + if header.GetNumberU64() == n { + return header.Hash() + } + } + + return common.Hash{} +} + +func (self *VMEnv) AddLog(log *vm.Log) { + //self.state.AddLog(log) +} +func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool { + return self.state.GetBalance(from).Cmp(balance) >= 0 +} + +func (self *VMEnv) MakeSnapshot() vm.Database { + return &VMState{ctx: self.ctx, state: self.state.state.Copy(), env: self} +} + +func (self *VMEnv) SetSnapshot(copy vm.Database) { + self.state.state.Set(copy.(*VMState).state) +} + +func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) { + core.Transfer(from, to, amount) +} + +func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + return core.Call(self, me, addr, data, gas, price, value) +} +func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + return core.CallCode(self, me, addr, data, gas, price, value) +} + +func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) { + return core.Create(self, me, data, gas, price, value) +} + +func (self *VMEnv) StructLogs() []vm.StructLog { + return self.logs +} + +func (self *VMEnv) AddStructLog(log vm.StructLog) { + self.logs = append(self.logs, log) +} + +// Error returns the error (if any) that happened during execution. +func (self *VMEnv) Error() error { + return self.err +} + +// VMState is a wrapper for the light state that holds the actual context and +// passes it to any state operation that requires it. +type VMState struct { + vm.Database + ctx context.Context + state *LightState + env *VMEnv +} + +// errHandler handles and stores any state error that happens during execution. +func (s *VMState) errHandler(err error) { + if err != nil && s.env.err == nil { + s.env.err = err + } +} + +// GetAccount returns the account object of the given account or nil if the +// account does not exist +func (s *VMState) GetAccount(addr common.Address) vm.Account { + so, err := s.state.GetStateObject(s.ctx, addr) + s.errHandler(err) + return so +} + +// CreateAccount creates creates a new account object and takes ownership. +func (s *VMState) CreateAccount(addr common.Address) vm.Account { + so, err := s.state.CreateStateObject(s.ctx, addr) + s.errHandler(err) + return so +} + +// AddBalance adds the given amount to the balance of the specified account +func (s *VMState) AddBalance(addr common.Address, amount *big.Int) { + err := s.state.AddBalance(s.ctx, addr, amount) + s.errHandler(err) +} + +// GetBalance retrieves the balance from the given address or 0 if the account does +// not exist +func (s *VMState) GetBalance(addr common.Address) *big.Int { + res, err := s.state.GetBalance(s.ctx, addr) + s.errHandler(err) + return res +} + +// GetNonce returns the nonce at the given address or 0 if the account does +// not exist +func (s *VMState) GetNonce(addr common.Address) uint64 { + res, err := s.state.GetNonce(s.ctx, addr) + s.errHandler(err) + return res +} + +// SetNonce sets the nonce of the specified account +func (s *VMState) SetNonce(addr common.Address, nonce uint64) { + err := s.state.SetNonce(s.ctx, addr, nonce) + s.errHandler(err) +} + +// GetCode returns the contract code at the given address or nil if the account +// does not exist +func (s *VMState) GetCode(addr common.Address) []byte { + res, err := s.state.GetCode(s.ctx, addr) + s.errHandler(err) + return res +} + +// SetCode sets the contract code at the specified account +func (s *VMState) SetCode(addr common.Address, code []byte) { + err := s.state.SetCode(s.ctx, addr, code) + s.errHandler(err) +} + +// AddRefund adds an amount to the refund value collected during a vm execution +func (s *VMState) AddRefund(gas *big.Int) { + s.state.AddRefund(gas) +} + +// GetRefund returns the refund value collected during a vm execution +func (s *VMState) GetRefund() *big.Int { + return s.state.GetRefund() +} + +// GetState returns the contract storage value at storage address b from the +// contract address a or common.Hash{} if the account does not exist +func (s *VMState) GetState(a common.Address, b common.Hash) common.Hash { + res, err := s.state.GetState(s.ctx, a, b) + s.errHandler(err) + return res +} + +// SetState sets the storage value at storage address key of the account addr +func (s *VMState) SetState(addr common.Address, key common.Hash, value common.Hash) { + err := s.state.SetState(s.ctx, addr, key, value) + s.errHandler(err) +} + +// Delete marks an account to be removed and clears its balance +func (s *VMState) Delete(addr common.Address) bool { + res, err := s.state.Delete(s.ctx, addr) + s.errHandler(err) + return res +} + +// Exist returns true if an account exists at the given address +func (s *VMState) Exist(addr common.Address) bool { + res, err := s.state.HasAccount(s.ctx, addr) + s.errHandler(err) + return res +} + +// IsDeleted returns true if the given account has been marked for deletion +// or false if the account does not exist +func (s *VMState) IsDeleted(addr common.Address) bool { + res, err := s.state.IsDeleted(s.ctx, addr) + s.errHandler(err) + return res +} From 4de92570cc71449021e9301771c5e4b995ae6233 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 23 Dec 2015 00:15:12 +0100 Subject: [PATCH 03/32] les: light client protocol and API --- cmd/geth/main.go | 3 + cmd/geth/usage.go | 3 + cmd/utils/flags.go | 43 +- core/database_util.go | 4 +- eth/backend.go | 32 +- eth/db_upgrade.go | 2 - eth/downloader/downloader.go | 4 +- eth/downloader/downloader_test.go | 2 +- eth/gasprice/lightprice.go | 160 +++++++ eth/handler.go | 2 +- les/api_backend.go | 144 ++++++ les/backend.go | 188 ++++++++ les/flowcontrol/control.go | 170 +++++++ les/flowcontrol/manager.go | 223 +++++++++ les/handler.go | 736 ++++++++++++++++++++++++++++++ les/handler_test.go | 322 +++++++++++++ les/helper_test.go | 316 +++++++++++++ les/metrics.go | 111 +++++ les/odr.go | 245 ++++++++++ les/odr_peerset.go | 119 +++++ les/odr_requests.go | 254 +++++++++++ les/odr_test.go | 223 +++++++++ les/peer.go | 482 +++++++++++++++++++ les/protocol.go | 196 ++++++++ les/request_test.go | 94 ++++ les/server.go | 175 +++++++ les/sync.go | 78 ++++ les/txrelay.go | 156 +++++++ light/lightchain.go | 70 ++- light/lightchain_test.go | 22 +- light/odr.go | 22 +- light/odr_test.go | 33 +- light/odr_util.go | 22 +- light/state_object.go | 15 +- light/state_test.go | 6 +- light/trie.go | 6 +- light/txpool.go | 17 +- light/txpool_test.go | 10 +- light/vm_env.go | 24 +- 39 files changed, 4617 insertions(+), 117 deletions(-) create mode 100644 eth/gasprice/lightprice.go create mode 100644 les/api_backend.go create mode 100644 les/backend.go create mode 100644 les/flowcontrol/control.go create mode 100644 les/flowcontrol/manager.go create mode 100644 les/handler.go create mode 100644 les/handler_test.go create mode 100644 les/helper_test.go create mode 100644 les/metrics.go create mode 100644 les/odr.go create mode 100644 les/odr_peerset.go create mode 100644 les/odr_requests.go create mode 100644 les/odr_test.go create mode 100644 les/peer.go create mode 100644 les/protocol.go create mode 100644 les/request_test.go create mode 100644 les/server.go create mode 100644 les/sync.go create mode 100644 les/txrelay.go diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 623f8ac811..898a20cfe5 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -157,6 +157,9 @@ participating. utils.BlockchainVersionFlag, utils.OlympicFlag, utils.FastSyncFlag, + utils.LightModeFlag, + utils.LightServFlag, + utils.LightPeersFlag, utils.CacheFlag, utils.LightKDFFlag, utils.JSpathFlag, diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index e7ef9e2c7b..5a366e57a2 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -71,6 +71,9 @@ var AppHelpFlagGroups = []flagGroup{ utils.GenesisFileFlag, utils.IdentityFlag, utils.FastSyncFlag, + utils.LightModeFlag, + utils.LightServFlag, + utils.LightPeersFlag, utils.LightKDFFlag, utils.CacheFlag, utils.BlockchainVersionFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 38ba3a9ba0..50d3ca43a7 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -39,6 +39,8 @@ import ( "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/les" + "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/metrics" @@ -157,6 +159,20 @@ var ( Name: "fast", Usage: "Enable fast syncing through state downloads", } + LightModeFlag = cli.BoolFlag{ + Name: "light", + Usage: "Enable light client mode", + } + LightServFlag = cli.IntFlag{ + Name: "lightserv", + Usage: "Maximum percentage of time allowed for serving LES requests (0-90)", + Value: 20, + } + LightPeersFlag = cli.IntFlag{ + Name: "lightpeers", + Usage: "Maximum number of LES client peers", + Value: 10, + } LightKDFFlag = cli.BoolFlag{ Name: "lightkdf", Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength", @@ -691,6 +707,9 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, ChainConfig: MustMakeChainConfig(ctx), Genesis: MakeGenesisBlock(ctx), FastSync: ctx.GlobalBool(FastSyncFlag.Name), + LightMode: ctx.GlobalBool(LightModeFlag.Name), + LightServ: ctx.GlobalInt(LightServFlag.Name), + LightPeers: ctx.GlobalInt(LightPeersFlag.Name), BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name), DatabaseCache: ctx.GlobalInt(CacheFlag.Name), DatabaseHandles: MakeDatabaseHandles(), @@ -734,6 +753,7 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, ethConf.Genesis = core.TestNetGenesisBlock() } state.StartingNonce = 1048576 // (2**20) + light.StartingNonce = 1048576 // (2**20) case ctx.GlobalBool(DevModeFlag.Name): // Override the base network stack configs @@ -769,11 +789,24 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, }); err != nil { Fatalf("Failed to register the account manager service: %v", err) } - - if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - return eth.New(ctx, ethConf) - }); err != nil { - Fatalf("Failed to register the Ethereum service: %v", err) + + if ethConf.LightMode { + if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { + return les.New(ctx, ethConf) + }); err != nil { + Fatalf("Failed to register the Ethereum light node service: %v", err) + } + } else { + if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { + fullNode, err := eth.New(ctx, ethConf) + if fullNode != nil { + ls, _ := les.NewLesServer(fullNode, ethConf) + fullNode.AddLesServer(ls) + } + return fullNode, err + }); err != nil { + Fatalf("Failed to register the Ethereum full node service: %v", err) + } } if shhEnable { if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil { diff --git a/core/database_util.go b/core/database_util.go index 6df2c15c20..8c7aaa0ee8 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -343,11 +343,11 @@ func WriteBody(db ethdb.Database, hash common.Hash, number uint64, body *types.B if err != nil { return err } - return WriteBodyRLP(db, hash, data) + return WriteBodyRLP(db, hash, number, data) } // WriteBodyRLP writes a serialized body of a block into the database. -func WriteBodyRLP(db ethdb.Database, hash common.Hash, rlp rlp.RawValue) error { +func WriteBodyRLP(db ethdb.Database, hash common.Hash, number uint64, rlp rlp.RawValue) error { key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) if err := db.Put(key, rlp); err != nil { glog.Fatalf("failed to store block body into database: %v", err) diff --git a/eth/backend.go b/eth/backend.go index f0ca38cbd5..a7de2b130e 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -67,9 +67,12 @@ var ( type Config struct { ChainConfig *core.ChainConfig // chain configuration - NetworkId int // Network ID to use for selecting peers to connect to - Genesis string // Genesis JSON to seed the chain database with - FastSync bool // Enables the state download based fast synchronisation algorithm + NetworkId int // Network ID to use for selecting peers to connect to + Genesis string // Genesis JSON to seed the chain database with + FastSync bool // Enables the state download based fast synchronisation algorithm + LightMode bool // Running in light client mode + LightServ int // Maximum percentage of time allowed for serving LES requests + LightPeers int // Maximum number of LES client peers BlockChainVersion int SkipBcVersionCheck bool // e.g. blockchain export @@ -103,6 +106,12 @@ type Config struct { TestGenesisState ethdb.Database // Genesis state to seed the database with (testing only!) } +type LesServer interface { + Start() + Stop() + Protocols() []p2p.Protocol +} + // FullNodeService implements the Ethereum full node service. type FullNodeService struct { chainConfig *core.ChainConfig @@ -114,6 +123,7 @@ type FullNodeService struct { txMu sync.Mutex blockchain *core.BlockChain protocolManager *ProtocolManager + ls LesServer // DB interfaces chainDb ethdb.Database // Block chain database dappDb ethdb.Database // Dapp database @@ -140,6 +150,10 @@ type FullNodeService struct { netRPCService *ethapi.PublicNetAPI } +func (s *FullNodeService) AddLesServer(ls LesServer) { + s.ls = ls +} + // New creates a new FullNodeService object (including the // initialisation of the common Ethereum object) func New(ctx *node.ServiceContext, config *Config) (*FullNodeService, error) { @@ -397,7 +411,11 @@ func (s *FullNodeService) Downloader() *downloader.Downloader { return s.protoco // Protocols implements node.Service, returning all the currently configured // network protocols to start. func (s *FullNodeService) Protocols() []p2p.Protocol { - return s.protocolManager.SubProtocols + if s.ls == nil { + return s.protocolManager.SubProtocols + } else { + return append(s.protocolManager.SubProtocols, s.ls.Protocols()...) + } } // Start implements node.Service, starting all internal goroutines needed by the @@ -408,6 +426,9 @@ func (s *FullNodeService) Start(srvr *p2p.Server) error { s.StartAutoDAG() } s.protocolManager.Start() + if s.ls != nil { + s.ls.Start() + } return nil } @@ -419,6 +440,9 @@ func (s *FullNodeService) Stop() error { } s.blockchain.Stop() s.protocolManager.Stop() + if s.ls != nil { + s.ls.Stop() + } s.txPool.Stop() s.miner.Stop() s.eventMux.Stop() diff --git a/eth/db_upgrade.go b/eth/db_upgrade.go index 12de60fe7b..7b7b08a74e 100644 --- a/eth/db_upgrade.go +++ b/eth/db_upgrade.go @@ -50,8 +50,6 @@ func upgradeSequentialKeys(db ethdb.Database) (stopFn func()) { return nil // empty database, nothing to do } - glog.V(logger.Info).Infof("Upgrading chain database to use sequential keys") - stopChn := make(chan struct{}) stoppedChn := make(chan struct{}) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 92124cfeb2..56c56bf2c1 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -172,13 +172,13 @@ type Downloader struct { } // New creates a new downloader to fetch hashes and blocks from remote peers. -func New(stateDb ethdb.Database, mux *event.TypeMux, hasHeader headerCheckFn, hasBlockAndState blockAndStateCheckFn, +func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, hasHeader headerCheckFn, hasBlockAndState blockAndStateCheckFn, getHeader headerRetrievalFn, getBlock blockRetrievalFn, headHeader headHeaderRetrievalFn, headBlock headBlockRetrievalFn, headFastBlock headFastBlockRetrievalFn, commitHeadBlock headBlockCommitterFn, getTd tdRetrievalFn, insertHeaders headerChainInsertFn, insertBlocks blockChainInsertFn, insertReceipts receiptChainInsertFn, rollback chainRollbackFn, dropPeer peerDropFn) *Downloader { dl := &Downloader{ - mode: FullSync, + mode: mode, mux: mux, queue: newQueue(stateDb), peers: newPeerSet(), diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index e9e051ded4..6e6bc3bd47 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -172,7 +172,7 @@ func newTester() *downloadTester { tester.stateDb, _ = ethdb.NewMemDatabase() tester.stateDb.Put(genesis.Root().Bytes(), []byte{0x00}) - tester.downloader = New(tester.stateDb, new(event.TypeMux), tester.hasHeader, tester.hasBlock, tester.getHeader, + tester.downloader = New(FullSync, tester.stateDb, new(event.TypeMux), tester.hasHeader, tester.hasBlock, tester.getHeader, tester.getBlock, tester.headHeader, tester.headBlock, tester.headFastBlock, tester.commitHeadBlock, tester.getTd, tester.insertHeaders, tester.insertBlocks, tester.insertReceipts, tester.rollback, tester.dropPeer) diff --git a/eth/gasprice/lightprice.go b/eth/gasprice/lightprice.go new file mode 100644 index 0000000000..5b420bc9d3 --- /dev/null +++ b/eth/gasprice/lightprice.go @@ -0,0 +1,160 @@ +// 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 . + +package gasprice + +import ( + "math/big" + "sort" + "sync" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" +) + +const ( + LpoAvgCount = 5 + LpoMinCount = 3 + LpoMaxBlocks = 20 + LpoSelect = 50 + LpoDefaultPrice = 20000000000 +) + +// LightPriceOracle recommends gas prices based on the content of recent +// blocks. Suitable for both light and full clients. +type LightPriceOracle struct { + backend ethapi.Backend + lastHead common.Hash + lastPrice *big.Int + cacheLock sync.RWMutex + fetchLock sync.Mutex +} + +// NewLightPriceOracle returns a new oracle. +func NewLightPriceOracle(backend ethapi.Backend) *LightPriceOracle { + return &LightPriceOracle{ + backend: backend, + lastPrice: big.NewInt(LpoDefaultPrice), + } +} + +// SuggestPrice returns the recommended gas price. +func (self *LightPriceOracle) SuggestPrice(ctx context.Context) (*big.Int, error) { + self.cacheLock.RLock() + lastHead := self.lastHead + lastPrice := self.lastPrice + self.cacheLock.RUnlock() + + head := self.backend.HeaderByNumber(rpc.LatestBlockNumber) + headHash := head.Hash() + if headHash == lastHead { + return lastPrice, nil + } + + self.fetchLock.Lock() + defer self.fetchLock.Unlock() + + // try checking the cache again, maybe the last fetch fetched what we need + self.cacheLock.RLock() + lastHead = self.lastHead + lastPrice = self.lastPrice + self.cacheLock.RUnlock() + if headHash == lastHead { + return lastPrice, nil + } + + blockNum := head.GetNumberU64() + chn := make(chan lpResult, LpoMaxBlocks) + sent := 0 + exp := 0 + var lps bigIntArray + for sent < LpoAvgCount && blockNum > 0 { + go self.getLowestPrice(ctx, blockNum, chn) + sent++ + exp++ + blockNum-- + } + maxEmpty := LpoAvgCount - LpoMinCount + for exp > 0 { + res := <-chn + if res.err != nil { + return nil, res.err + } + exp-- + if res.price != nil { + lps = append(lps, res.price) + } else { + if maxEmpty > 0 { + maxEmpty-- + } else { + if blockNum > 0 && sent < LpoMaxBlocks { + go self.getLowestPrice(ctx, blockNum, chn) + sent++ + exp++ + blockNum-- + } + } + } + } + price := lastPrice + if len(lps) > 0 { + sort.Sort(lps) + price = lps[(len(lps)-1)*LpoSelect/100] + } + + self.cacheLock.Lock() + self.lastHead = headHash + self.lastPrice = price + self.cacheLock.Unlock() + return price, nil +} + +type lpResult struct { + price *big.Int + err error +} + +// getLowestPrice calculates the lowest transaction gas price in a given block +// and sends it to the result channel. If the block is empty, price is nil. +func (self *LightPriceOracle) getLowestPrice(ctx context.Context, blockNum uint64, chn chan lpResult) { + block, err := self.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum)) + if block == nil { + chn <- lpResult{nil, err} + return + } + txs := block.Transactions() + if len(txs) == 0 { + chn <- lpResult{nil, nil} + return + } + // find smallest gasPrice + minPrice := txs[0].GasPrice() + for i := 1; i < len(txs); i++ { + price := txs[i].GasPrice() + if price.Cmp(minPrice) < 0 { + minPrice = price + } + } + chn <- lpResult{minPrice, nil} +} + +type bigIntArray []*big.Int + +func (s bigIntArray) Len() int { return len(s) } +func (s bigIntArray) Less(i, j int) bool { return s[i].Cmp(s[j]) < 0 } +func (s bigIntArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] } diff --git a/eth/handler.go b/eth/handler.go index 47a36cc0bf..8d5088a0cc 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -152,7 +152,7 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, return nil, errIncompatibleConfig } // Construct the different synchronisation mechanisms - manager.downloader = downloader.New(chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeaderByHash, + manager.downloader = downloader.New(downloader.FullSync, chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeaderByHash, blockchain.GetBlockByHash, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead, blockchain.GetTdByHash, blockchain.InsertHeaderChain, manager.insertChain, blockchain.InsertReceiptChain, blockchain.Rollback, manager.removePeer) diff --git a/les/api_backend.go b/les/api_backend.go new file mode 100644 index 0000000000..f07d631c79 --- /dev/null +++ b/les/api_backend.go @@ -0,0 +1,144 @@ +// 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 . + +package les + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/eth/gasprice" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/light" + rpc "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" +) + +type LesApiBackend struct { + eth *LightNodeService + gpo *gasprice.LightPriceOracle +} + +func (b *LesApiBackend) SetHead(number uint64) { + b.eth.blockchain.SetHead(number) +} + +func (b *LesApiBackend) HeaderByNumber(blockNr rpc.BlockNumber) *types.Header { + if blockNr == rpc.LatestBlockNumber || blockNr == rpc.PendingBlockNumber { + return b.eth.blockchain.CurrentHeader() + } + + return b.eth.blockchain.GetHeaderByNumber(uint64(blockNr)) +} + +func (b *LesApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Block, error) { + header := b.HeaderByNumber(blockNr) + if header == nil { + return nil, nil + } + return b.GetBlock(ctx, header.Hash()) +} + +func (b *LesApiBackend) StateAndHeaderByNumber(blockNr rpc.BlockNumber) (ethapi.State, *types.Header, error) { + header := b.HeaderByNumber(blockNr) + if header == nil { + return nil, nil, nil + } + return light.NewLightState(light.StateTrieID(header), b.eth.odr), header, nil +} + +func (b *LesApiBackend) GetBlock(ctx context.Context, blockHash common.Hash) (*types.Block, error) { + return b.eth.blockchain.GetBlockByHash(ctx, blockHash) +} + +func (b *LesApiBackend) GetReceipts(ctx context.Context, blockHash common.Hash) (types.Receipts, error) { + return light.GetBlockReceipts(ctx, b.eth.odr, blockHash, core.GetBlockNumber(b.eth.chainDb, blockHash)) +} + +func (b *LesApiBackend) GetTd(blockHash common.Hash) *big.Int { + return b.eth.blockchain.GetTdByHash(blockHash) +} + +func (b *LesApiBackend) GetVMEnv(ctx context.Context, msg core.Message, state ethapi.State, header *types.Header) (vm.Environment, func() error, error) { + stateDb := state.(*light.LightState).Copy() + addr, _ := msg.From() + from, err := stateDb.GetOrNewStateObject(ctx, addr) + if err != nil { + return nil, nil, err + } + from.SetBalance(common.MaxBig) + env := light.NewEnv(ctx, stateDb, b.eth.chainConfig, b.eth.blockchain, msg, header, b.eth.chainConfig.VmConfig) + return env, env.Error, nil +} + +func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { + return b.eth.txPool.Add(ctx, signedTx) +} + +func (b *LesApiBackend) RemoveTx(txHash common.Hash) { + b.eth.txPool.RemoveTx(txHash) +} + +func (b *LesApiBackend) GetPoolTransactions() types.Transactions { + return b.eth.txPool.GetTransactions() +} + +func (b *LesApiBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction { + return b.eth.txPool.GetTransaction(txHash) +} + +func (b *LesApiBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) { + return b.eth.txPool.GetNonce(ctx, addr) +} + +func (b *LesApiBackend) Stats() (pending int, queued int) { + return b.eth.txPool.Stats(), 0 +} + +func (b *LesApiBackend) TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) { + return make(map[common.Address]map[uint64][]*types.Transaction), make(map[common.Address]map[uint64][]*types.Transaction) +} + +func (b *LesApiBackend) Downloader() *downloader.Downloader { + return b.eth.Downloader() +} + +func (b *LesApiBackend) ProtocolVersion() int { + return b.eth.LesVersion() + 10000 +} + +func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { + return b.gpo.SuggestPrice(ctx) +} + +func (b *LesApiBackend) ChainDb() ethdb.Database { + return b.eth.chainDb +} + +func (b *LesApiBackend) EventMux() *event.TypeMux { + return b.eth.eventMux +} + +func (b *LesApiBackend) AccountManager() *accounts.Manager { + return b.eth.accountManager +} diff --git a/les/backend.go b/les/backend.go new file mode 100644 index 0000000000..dcf19b57f5 --- /dev/null +++ b/les/backend.go @@ -0,0 +1,188 @@ +// 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 . + +// Package les implements the Light Ethereum Subprotocol. +package les + +import ( + "errors" + "fmt" + "time" + + "github.com/ethereum/ethash" + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common/compiler" + "github.com/ethereum/go-ethereum/common/httpclient" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/eth/gasprice" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/light" + "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/p2p" + rpc "github.com/ethereum/go-ethereum/rpc" +) + +type LightNodeService struct { + odr *LesOdr + relay *LesTxRelay + chainConfig *core.ChainConfig + // Channel for shutting down the service + shutdownChan chan bool + // Handlers + txPool *light.TxPool + blockchain *light.LightChain + protocolManager *ProtocolManager + // DB interfaces + chainDb ethdb.Database // Block chain database + dappDb ethdb.Database // Dapp database + + apiBackend *LesApiBackend + + eventMux *event.TypeMux + pow *ethash.Ethash + httpclient *httpclient.HTTPClient + accountManager *accounts.Manager + solcPath string + solc *compiler.Solidity + + NatSpec bool + PowTest bool + netVersionId int + netRPCService *ethapi.PublicNetAPI +} + +func New(ctx *node.ServiceContext, config *eth.Config) (*LightNodeService, error) { + chainDb, dappDb, err := eth.CreateDBs(ctx, config) + if err != nil { + return nil, err + } + if err := eth.SetupGenesisBlock(&chainDb, config); err != nil { + return nil, err + } + pow, err := eth.CreatePoW(config) + if err != nil { + return nil, err + } + + odr := NewLesOdr(chainDb) + relay := NewLesTxRelay() + eth := &LightNodeService{ + odr: odr, + relay: relay, + chainDb: chainDb, + dappDb: dappDb, + eventMux: ctx.EventMux, + accountManager: config.AccountManager, + pow: pow, + shutdownChan: make(chan bool), + httpclient: httpclient.New(config.DocRoot), + netVersionId: config.NetworkId, + NatSpec: config.NatSpec, + PowTest: config.PowTest, + solcPath: config.SolcPath, + } + + if config.ChainConfig == nil { + return nil, errors.New("missing chain config") + } + eth.chainConfig = config.ChainConfig + eth.chainConfig.VmConfig = vm.Config{ + EnableJit: config.EnableJit, + ForceJit: config.ForceJit, + } + eth.blockchain, err = light.NewLightChain(odr, eth.chainConfig, eth.pow, eth.eventMux) + if err != nil { + if err == core.ErrNoGenesis { + return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`) + } + return nil, err + } + + eth.txPool = light.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain, eth.relay) + if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.LightMode, config.NetworkId, eth.eventMux, eth.pow, eth.blockchain, nil, chainDb, odr, relay); err != nil { + return nil, err + } + odr.removePeer = eth.protocolManager.removePeer + + eth.apiBackend = &LesApiBackend{eth, nil} + eth.apiBackend.gpo = gasprice.NewLightPriceOracle(eth.apiBackend) + return eth, nil +} + +// APIs returns the collection of RPC services the ethereum package offers. +// NOTE, some of these services probably need to be moved to somewhere else. +func (s *LightNodeService) APIs() []rpc.API { + return append(ethapi.GetAPIs(s.apiBackend, &s.solcPath, &s.solc), []rpc.API{ + { + Namespace: "eth", + Version: "1.0", + Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux), + Public: true, + }, { + Namespace: "net", + Version: "1.0", + Service: s.netRPCService, + Public: true, + }, + }...) +} + +func (s *LightNodeService) ResetWithGenesisBlock(gb *types.Block) { + s.blockchain.ResetWithGenesisBlock(gb) +} + +func (s *LightNodeService) BlockChain() *light.LightChain { return s.blockchain } +func (s *LightNodeService) TxPool() *light.TxPool { return s.txPool } +func (s *LightNodeService) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) } +func (s *LightNodeService) Downloader() *downloader.Downloader { return s.protocolManager.downloader } + +// Protocols implements node.Service, returning all the currently configured +// network protocols to start. +func (s *LightNodeService) Protocols() []p2p.Protocol { + return s.protocolManager.SubProtocols +} + +// Start implements node.Service, starting all internal goroutines needed by the +// Ethereum protocol implementation. +func (s *LightNodeService) Start(srvr *p2p.Server) error { + s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId) + s.protocolManager.Start() + return nil +} + +// Stop implements node.Service, terminating all internal goroutines used by the +// Ethereum protocol. +func (s *LightNodeService) Stop() error { + s.odr.Stop() + s.blockchain.Stop() + s.protocolManager.Stop() + s.txPool.Stop() + + s.eventMux.Stop() + + time.Sleep(time.Millisecond * 200) + s.chainDb.Close() + s.dappDb.Close() + close(s.shutdownChan) + + return nil +} diff --git a/les/flowcontrol/control.go b/les/flowcontrol/control.go new file mode 100644 index 0000000000..c1ed03eb79 --- /dev/null +++ b/les/flowcontrol/control.go @@ -0,0 +1,170 @@ +// 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 . + +// Package flowcontrol implements a client side flow control mechanism +package flowcontrol + +import ( + "sync" + "time" +) + +const fcTimeConst = 1000000 + +type ServerParams struct { + BufLimit, MinRecharge uint64 +} + +type ClientNode struct { + params *ServerParams + bufValue uint64 + lastTime int64 + lock sync.Mutex + cm *ClientManager + cmNode *cmNode +} + +func NewClientNode(cm *ClientManager, params *ServerParams) *ClientNode { + node := &ClientNode{ + cm: cm, + params: params, + bufValue: params.BufLimit, + lastTime: getTime(), + } + node.cmNode = cm.addNode(node) + return node +} + +func (peer *ClientNode) Remove(cm *ClientManager) { + cm.removeNode(peer.cmNode) +} + +func (peer *ClientNode) recalcBV(time int64) { + dt := uint64(time - peer.lastTime) + if time < peer.lastTime { + dt = 0 + } + peer.bufValue += peer.params.MinRecharge * dt / fcTimeConst + if peer.bufValue > peer.params.BufLimit { + peer.bufValue = peer.params.BufLimit + } + peer.lastTime = time +} + +func (peer *ClientNode) AcceptRequest() (uint64, bool) { + peer.lock.Lock() + defer peer.lock.Unlock() + + time := getTime() + peer.recalcBV(time) + return peer.bufValue, peer.cm.accept(peer.cmNode, time) +} + +func (peer *ClientNode) RequestProcessed(cost uint64) (bv, realCost uint64) { + peer.lock.Lock() + defer peer.lock.Unlock() + + time := getTime() + peer.recalcBV(getTime()) + peer.bufValue -= cost + peer.recalcBV(time) + rcValue, rcost := peer.cm.processed(peer.cmNode, time) + if rcValue < peer.params.BufLimit { + bv := peer.params.BufLimit - rcValue + if bv > peer.bufValue { + peer.bufValue = bv + } + } + return peer.bufValue, rcost +} + +type ServerNode struct { + bufEstimate uint64 + lastTime int64 + params *ServerParams + sumCost uint64 // sum of req costs sent to this server + pending map[uint64]uint64 // value = sumCost after sending the given req + lock sync.Mutex +} + +func NewServerNode(params *ServerParams) *ServerNode { + return &ServerNode{ + bufEstimate: params.BufLimit, + lastTime: getTime(), + params: params, + pending: make(map[uint64]uint64), + } +} + +func getTime() int64 { + return time.Now().UnixNano() +} + +func (peer *ServerNode) recalcBLE(time int64) { + dt := uint64(time - peer.lastTime) + if time < peer.lastTime { + dt = 0 + } + peer.bufEstimate += peer.params.MinRecharge * dt / fcTimeConst + if peer.bufEstimate > peer.params.BufLimit { + peer.bufEstimate = peer.params.BufLimit + } + peer.lastTime = time +} + +func (peer *ServerNode) canSend(maxCost uint64) uint64 { + if peer.bufEstimate >= maxCost { + return 0 + } + return (maxCost - peer.bufEstimate) * fcTimeConst / peer.params.MinRecharge +} + +func (peer *ServerNode) CanSend(maxCost uint64) uint64 { + peer.lock.Lock() + defer peer.lock.Unlock() + + return peer.canSend(maxCost) +} + +// blocks until request can be sent +func (peer *ServerNode) SendRequest(reqID, maxCost uint64) { + peer.lock.Lock() + defer peer.lock.Unlock() + + peer.recalcBLE(getTime()) + for peer.bufEstimate < maxCost { + time.Sleep(time.Duration(peer.canSend(maxCost))) + peer.recalcBLE(getTime()) + } + peer.bufEstimate -= maxCost + peer.sumCost += maxCost + if reqID >= 0 { + peer.pending[reqID] = peer.sumCost + } +} + +func (peer *ServerNode) GotReply(reqID, bv uint64) { + peer.lock.Lock() + defer peer.lock.Unlock() + + sc, ok := peer.pending[reqID] + if !ok { + return + } + delete(peer.pending, reqID) + peer.bufEstimate = bv - (peer.sumCost - sc) + peer.lastTime = getTime() +} diff --git a/les/flowcontrol/manager.go b/les/flowcontrol/manager.go new file mode 100644 index 0000000000..9d2ea8067f --- /dev/null +++ b/les/flowcontrol/manager.go @@ -0,0 +1,223 @@ +// 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 . + +// Package flowcontrol implements a client side flow control mechanism +package flowcontrol + +import ( + "sync" + "time" +) + +const rcConst = 1000000 + +type cmNode struct { + node *ClientNode + lastUpdate int64 + reqAccepted int64 + serving, recharging bool + rcWeight uint64 + rcValue, rcDelta int64 + finishRecharge, startValue int64 +} + +func (node *cmNode) update(time int64) { + dt := time - node.lastUpdate + node.rcValue += node.rcDelta * dt / rcConst + node.lastUpdate = time + if node.recharging && time >= node.finishRecharge { + node.recharging = false + node.rcDelta = 0 + node.rcValue = 0 + } +} + +func (node *cmNode) set(serving bool, simReqCnt, sumWeight uint64) { + if node.serving && !serving { + node.recharging = true + sumWeight += node.rcWeight + } + node.serving = serving + if node.recharging && serving { + node.recharging = false + sumWeight -= node.rcWeight + } + + node.rcDelta = 0 + if serving { + node.rcDelta = int64(rcConst / simReqCnt) + } + if node.recharging { + node.rcDelta = -int64(node.node.cm.rcRecharge * node.rcWeight / sumWeight) + node.finishRecharge = node.lastUpdate + node.rcValue*rcConst/(-node.rcDelta) + } +} + +type ClientManager struct { + lock sync.Mutex + nodes map[*cmNode]struct{} + simReqCnt, sumWeight, rcSumValue uint64 + maxSimReq, maxRcSum uint64 + rcRecharge uint64 + resumeQueue chan chan bool + time int64 +} + +func NewClientManager(rcTarget, maxSimReq, maxRcSum uint64) *ClientManager { + cm := &ClientManager{ + nodes: make(map[*cmNode]struct{}), + resumeQueue: make(chan chan bool), + rcRecharge: rcConst * rcConst / (100*rcConst/rcTarget - rcConst), + maxSimReq: maxSimReq, + maxRcSum: maxRcSum, + } + go cm.queueProc() + return cm +} + +func (self *ClientManager) Stop() { + self.lock.Lock() + defer self.lock.Unlock() + + // signal any waiting accept routines to return false + self.nodes = make(map[*cmNode]struct{}) + close(self.resumeQueue) +} + +func (self *ClientManager) addNode(cnode *ClientNode) *cmNode { + time := getTime() + node := &cmNode{ + node: cnode, + lastUpdate: time, + finishRecharge: time, + rcWeight: 1, + } + self.lock.Lock() + defer self.lock.Unlock() + + self.nodes[node] = struct{}{} + self.update(getTime()) + return node +} + +func (self *ClientManager) removeNode(node *cmNode) { + self.lock.Lock() + defer self.lock.Unlock() + + time := getTime() + self.stop(node, time) + delete(self.nodes, node) + self.update(time) +} + +// recalc sumWeight +func (self *ClientManager) updateNodes(time int64) (rce bool) { + var sumWeight, rcSum uint64 + for node, _ := range self.nodes { + rc := node.recharging + node.update(time) + if rc && !node.recharging { + rce = true + } + if node.recharging { + sumWeight += node.rcWeight + } + rcSum += uint64(node.rcValue) + } + self.sumWeight = sumWeight + self.rcSumValue = rcSum + return +} + +func (self *ClientManager) update(time int64) { + for { + firstTime := time + for node, _ := range self.nodes { + if node.recharging && node.finishRecharge < firstTime { + firstTime = node.finishRecharge + } + } + if self.updateNodes(firstTime) { + for node, _ := range self.nodes { + if node.recharging { + node.set(node.serving, self.simReqCnt, self.sumWeight) + } + } + } else { + self.time = time + return + } + } +} + +func (self *ClientManager) canStartReq() bool { + return self.simReqCnt < self.maxSimReq && self.rcSumValue < self.maxRcSum +} + +func (self *ClientManager) queueProc() { + for rc := range self.resumeQueue { + for { + time.Sleep(time.Millisecond * 10) + self.lock.Lock() + self.update(getTime()) + cs := self.canStartReq() + self.lock.Unlock() + if cs { + break + } + } + close(rc) + } +} + +func (self *ClientManager) accept(node *cmNode, time int64) bool { + self.lock.Lock() + defer self.lock.Unlock() + + self.update(time) + if !self.canStartReq() { + resume := make(chan bool) + self.resumeQueue <- resume + self.lock.Unlock() + <-resume + self.lock.Lock() + if _, ok := self.nodes[node]; !ok { + return false // reject if node has been removed or manager has been stopped + } + } + self.simReqCnt++ + node.set(true, self.simReqCnt, self.sumWeight) + node.startValue = node.rcValue + self.update(self.time) + return true +} + +func (self *ClientManager) stop(node *cmNode, time int64) { + if node.serving { + self.update(time) + self.simReqCnt-- + node.set(false, self.simReqCnt, self.sumWeight) + self.update(time) + } +} + +func (self *ClientManager) processed(node *cmNode, time int64) (rcValue, rcCost uint64) { + self.lock.Lock() + defer self.lock.Unlock() + + self.stop(node, time) + return uint64(node.rcValue), uint64(node.rcValue - node.startValue) +} diff --git a/les/handler.go b/les/handler.go new file mode 100644 index 0000000000..fda7f5d876 --- /dev/null +++ b/les/handler.go @@ -0,0 +1,736 @@ +// 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 . + +// Package les implements the Light Ethereum Subprotocol. +package les + +import ( + "errors" + "fmt" + "math/big" + "sync" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/discover" + "github.com/ethereum/go-ethereum/pow" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/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 + + ethVersion = 63 // equivalent eth version for the downloader + + MaxHeaderFetch = 192 // Amount of block headers to be fetched per retrieval request + MaxBodyFetch = 32 // Amount of block bodies to be fetched per retrieval request + MaxReceiptFetch = 128 // Amount of transaction receipts to allow fetching per request + MaxCodeFetch = 64 // Amount of contract codes to allow fetching per request + MaxProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request +) + +// errIncompatibleConfig is returned if the requested protocols and configs are +// not compatible (low protocol version restrictions and high requirements). +var errIncompatibleConfig = errors.New("incompatible configuration") + +func errResp(code errCode, format string, v ...interface{}) error { + return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...)) +} + +type hashFetcherFn func(common.Hash) error + +type BlockChain interface { + HasHeader(hash common.Hash) bool + GetHeader(hash common.Hash, number uint64) *types.Header + GetHeaderByHash(hash common.Hash) *types.Header + CurrentHeader() *types.Header + GetTdByHash(hash common.Hash) *big.Int + InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) + Rollback(chain []common.Hash) + Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) + GetHeaderByNumber(number uint64) *types.Header + GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash + LastBlockHash() common.Hash + Genesis() *types.Block +} + +type txPool interface { + // AddTransactions should add the given transactions to the pool. + AddTransactions([]*types.Transaction) +} + +type ProtocolManager struct { + lightSync bool + txpool txPool + txrelay *LesTxRelay + networkId int + chainConfig *core.ChainConfig + blockchain BlockChain + chainDb ethdb.Database + odr *LesOdr + server *LesServer + + downloader *downloader.Downloader + //fetcher *fetcher.Fetcher + peers *peerSet + + SubProtocols []p2p.Protocol + + eventMux *event.TypeMux + + // channels for fetcher, syncer, txsyncLoop + newPeerCh chan *peer + 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(chainConfig *core.ChainConfig, lightSync bool, networkId int, mux *event.TypeMux, pow pow.PoW, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay) (*ProtocolManager, error) { + // Create the protocol manager with the base fields + manager := &ProtocolManager{ + lightSync: lightSync, + eventMux: mux, + blockchain: blockchain, + chainConfig: chainConfig, + chainDb: chainDb, + networkId: networkId, + txpool: txpool, + txrelay: txrelay, + odr: odr, + peers: newPeerSet(), + newPeerCh: make(chan *peer, 1), + quitSync: make(chan struct{}), + noMorePeers: make(chan struct{}), + } + // Initiate a sub-protocol for every implemented version we can handle + manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions)) + for i, version := range ProtocolVersions { + // Compatible, initialize the sub-protocol + version := version // Closure for the run + manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{ + Name: "les", + Version: version, + Length: ProtocolLengths[i], + Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { + peer := manager.newPeer(int(version), networkId, p, rw) + select { + case manager.newPeerCh <- peer: + manager.wg.Add(1) + defer manager.wg.Done() + return manager.handle(peer) + case <-manager.quitSync: + return p2p.DiscQuitting + } + }, + NodeInfo: func() interface{} { + return manager.NodeInfo() + }, + PeerInfo: func(id discover.NodeID) interface{} { + if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil { + return p.Info() + } + return nil + }, + }) + } + if len(manager.SubProtocols) == 0 { + return nil, errIncompatibleConfig + } + + if lightSync { + glog.V(logger.Debug).Infof("LES: create downloader") + manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, blockchain.HasHeader, nil, blockchain.GetHeaderByHash, + nil, blockchain.CurrentHeader, nil, nil, nil, blockchain.GetTdByHash, + blockchain.InsertHeaderChain, nil, nil, blockchain.Rollback, manager.removePeer) + } + + /*validator := func(block *types.Block, parent *types.Block) error { + return core.ValidateHeader(pow, block.Header(), parent.Header(), true, false) + } + heighter := func() uint64 { + return chainman.LastBlockNumberU64() + } + manager.fetcher = fetcher.New(chainman.GetBlockNoOdr, validator, nil, heighter, chainman.InsertChain, manager.removePeer) + */ + return manager, nil +} + +func (pm *ProtocolManager) removePeer(id string) { + // Short circuit if the peer was already removed + peer := pm.peers.Peer(id) + if peer == nil { + return + } + glog.V(logger.Debug).Infoln("Removing peer", id) + + // Unregister the peer from the downloader and Ethereum peer set + glog.V(logger.Debug).Infof("LES: unregister peer %v", id) + if pm.lightSync { + pm.downloader.UnregisterPeer(id) + pm.odr.UnregisterPeer(peer) + if pm.txrelay != nil { + pm.txrelay.removePeer(id) + } + } + if err := pm.peers.Unregister(id); err != nil { + glog.V(logger.Error).Infoln("Removal failed:", err) + } + // Hard disconnect at the networking layer + if peer != nil { + peer.Peer.Disconnect(p2p.DiscUselessPeer) + } +} + +func (pm *ProtocolManager) Start() { + if pm.lightSync { + // start sync handler + go pm.syncer() + } +} + +func (pm *ProtocolManager) Stop() { + // Showing a log message. During download / process this could actually + // take between 5 to 10 seconds and therefor feedback is required. + glog.V(logger.Info).Infoln("Stopping light ethereum protocol handler...") + + // Quit the sync loop. + // After this send has completed, no new peers will be accepted. + pm.noMorePeers <- struct{}{} + + close(pm.quitSync) // quits syncer, fetcher + + // 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 any process action + pm.wg.Wait() + + glog.V(logger.Info).Infoln("Light ethereum protocol handler stopped") +} + +func (pm *ProtocolManager) newPeer(pv, nv int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { + return newPeer(pv, nv, p, newMeteredMsgWriter(rw)) +} + +// handle is the callback invoked to manage the life cycle of a les peer. When +// this function terminates, the peer is disconnected. +func (pm *ProtocolManager) handle(p *peer) error { + glog.V(logger.Debug).Infof("%v: peer connected [%s]", p, p.Name()) + + // Execute the LES handshake + td, head, genesis := pm.blockchain.Status() + if err := p.Handshake(td, head, genesis, pm.server); err != nil { + glog.V(logger.Debug).Infof("%v: handshake failed: %v", p, err) + return err + } + if rw, ok := p.rw.(*meteredMsgReadWriter); ok { + rw.Init(p.version) + } + // Register the peer locally + glog.V(logger.Detail).Infof("%v: adding peer", p) + if err := pm.peers.Register(p); err != nil { + glog.V(logger.Error).Infof("%v: addition failed: %v", p, err) + return err + } + defer func() { + if pm.server != nil && pm.server.fcManager != nil && p.fcClient != nil { + p.fcClient.Remove(pm.server.fcManager) + } + pm.removePeer(p.id) + }() + + // Register the peer in the downloader. If the downloader considers it banned, we disconnect + glog.V(logger.Debug).Infof("LES: register peer %v", p.id) + if pm.lightSync { + requestHeadersByHash := func(origin common.Hash, amount int, skip int, reverse bool) error { + reqID := pm.odr.getNextReqID() + cost := p.GetRequestCost(GetBlockHeadersMsg, amount) + p.fcServer.SendRequest(reqID, cost) + return p.RequestHeadersByHash(reqID, cost, origin, amount, skip, reverse) + } + requestHeadersByNumber := func(origin uint64, amount int, skip int, reverse bool) error { + reqID := pm.odr.getNextReqID() + cost := p.GetRequestCost(GetBlockHeadersMsg, amount) + p.fcServer.SendRequest(reqID, cost) + return p.RequestHeadersByNumber(reqID, cost, origin, amount, skip, reverse) + } + if err := pm.downloader.RegisterPeer(p.id, ethVersion, p.Head(), + nil, nil, nil, requestHeadersByHash, requestHeadersByNumber, + nil, nil, nil); err != nil { + return err + } + pm.odr.RegisterPeer(p) + if pm.txrelay != nil { + pm.txrelay.addPeer(p) + } + } + + // main loop. handle incoming messages. + for { + if err := pm.handleMsg(p); err != nil { + glog.V(logger.Debug).Infof("%v: message handling failed: %v", p, err) + return err + } + } + return nil +} + +var reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsMsg, SendTxMsg} + +// 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 + } + + var costs *requestCosts + var reqCnt, maxReqs int + + if rc, ok := p.fcCosts[msg.Code]; ok { // check if msg is a supported request type + costs = rc + if p.fcClient == nil { + return errResp(ErrRequestRejected, "") + } + bv, ok := p.fcClient.AcceptRequest() + if !ok || bv < costs.baseCost { + return errResp(ErrRequestRejected, "") + } + d := bv - costs.baseCost + if d/10000 < costs.reqCost { + maxReqs = int(d / costs.reqCost) + } else { + maxReqs = 10000 + } + } + + if msg.Size > ProtocolMaxMsgSize { + return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) + } + defer msg.Discard() + + var deliverMsg *Msg + + // Handle the message depending on its contents + switch msg.Code { + case StatusMsg: + glog.V(logger.Debug).Infof("LES: received StatusMsg from peer %v", p.id) + // Status messages should never arrive after the handshake + return errResp(ErrExtraStatusMsg, "uncontrolled status message") + + // Block header query, collect the requested headers and reply + case GetBlockHeadersMsg: + glog.V(logger.Debug).Infof("LES: received GetBlockHeadersMsg from peer %v", p.id) + // Decode the complex header query + var req struct { + ReqID uint64 + Query getBlockHeadersData + } + if err := msg.Decode(&req); err != nil { + return errResp(ErrDecode, "%v: %v", msg, err) + } + query := req.Query + + hashMode := query.Origin.Hash != (common.Hash{}) + + // 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 { + origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash) + } else { + origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number) + } + if origin == nil { + break + } + number := origin.Number.Uint64() + headers = append(headers, origin) + bytes += estHeaderRlpSize + + // Advance to the next header of the query + switch { + case query.Origin.Hash != (common.Hash{}) && query.Reverse: + // Hash based traversal towards the genesis block + for i := 0; i < int(query.Skip)+1; i++ { + if header := pm.blockchain.GetHeader(query.Origin.Hash, number); header != nil { + query.Origin.Hash = header.ParentHash + number-- + } else { + unknown = true + break + } + } + case query.Origin.Hash != (common.Hash{}) && !query.Reverse: + // Hash based traversal towards the leaf block + if header := pm.blockchain.GetHeaderByNumber(origin.Number.Uint64() + query.Skip + 1); header != nil { + if pm.blockchain.GetBlockHashesFromHash(header.Hash(), query.Skip+1)[query.Skip] == query.Origin.Hash { + query.Origin.Hash = header.Hash() + } 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) + } + } + + bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + query.Amount * costs.reqCost) + pm.server.fcCostStats.update(msg.Code, query.Amount, rcost) + return p.SendBlockHeaders(req.ReqID, bv, headers) + + case BlockHeadersMsg: + if pm.downloader == nil { + return errResp(ErrUnexpectedResponse, "") + } + + glog.V(logger.Debug).Infof("LES: received BlockHeadersMsg from peer %v", p.id) + // A batch of headers arrived to one of our previous requests + var resp struct { + ReqID, BV uint64 + Headers []*types.Header + } + if err := msg.Decode(&resp); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + p.fcServer.GotReply(resp.ReqID, resp.BV) + err := pm.downloader.DeliverHeaders(p.id, resp.Headers) + if err != nil { + glog.V(logger.Debug).Infoln(err) + } + + case GetBlockBodiesMsg: + glog.V(logger.Debug).Infof("LES: received GetBlockBodiesMsg from peer %v", p.id) + // Decode the retrieval message + var req struct { + ReqID uint64 + Hashes []common.Hash + } + if err := msg.Decode(&req); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + // Gather blocks until the fetch or network limits is reached + var ( + bytes int + bodies []rlp.RawValue + ) + reqCnt = len(req.Hashes) + if reqCnt > maxReqs { + return errResp(ErrRequestRejected, "") + } + for _, hash := range req.Hashes { + if bytes >= softResponseLimit || len(bodies) >= MaxBodyFetch { + break + } + // Retrieve the requested block body, stopping if enough was found + if data := core.GetBodyRLP(pm.chainDb, hash, core.GetBlockNumber(pm.chainDb, hash)); len(data) != 0 { + bodies = append(bodies, data) + bytes += len(data) + } + } + bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) + pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) + return p.SendBlockBodiesRLP(req.ReqID, bv, bodies) + + case BlockBodiesMsg: + if pm.odr == nil { + return errResp(ErrUnexpectedResponse, "") + } + + glog.V(logger.Debug).Infof("LES: received BlockBodiesMsg from peer %v", p.id) + // A batch of block bodies arrived to one of our previous requests + var resp struct { + ReqID, BV uint64 + Data []*types.Body + } + if err := msg.Decode(&resp); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + p.fcServer.GotReply(resp.ReqID, resp.BV) + deliverMsg = &Msg{ + MsgType: MsgBlockBodies, + ReqID: resp.ReqID, + Obj: resp.Data, + } + + case GetCodeMsg: + glog.V(logger.Debug).Infof("LES: received GetCodeMsg from peer %v", p.id) + // Decode the retrieval message + var req struct { + ReqID uint64 + Reqs []CodeReq + } + if err := msg.Decode(&req); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + // Gather state data until the fetch or network limits is reached + var ( + bytes int + data [][]byte + ) + reqCnt = len(req.Reqs) + if reqCnt > maxReqs { + return errResp(ErrRequestRejected, "") + } + for _, req := range req.Reqs { + if len(data) >= MaxCodeFetch { + break + } + // Retrieve the requested state entry, stopping if enough was found + if header := core.GetHeader(pm.chainDb, req.BHash, core.GetBlockNumber(pm.chainDb, req.BHash)); header != nil { + if trie, _ := trie.New(header.Root, pm.chainDb); trie != nil { + sdata := trie.Get(req.AccKey) + if so, err := state.DecodeObject(common.Address{}, pm.chainDb, sdata); err == nil { + entry := so.Code() + if bytes+len(entry) >= softResponseLimit { + break + } + data = append(data, entry) + bytes += len(entry) + } + } + } + } + bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) + pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) + return p.SendCode(req.ReqID, bv, data) + + case CodeMsg: + if pm.odr == nil { + return errResp(ErrUnexpectedResponse, "") + } + + glog.V(logger.Debug).Infof("LES: received CodeMsg from peer %v", p.id) + // A batch of node state data arrived to one of our previous requests + var resp struct { + ReqID, BV uint64 + Data [][]byte + } + if err := msg.Decode(&resp); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + p.fcServer.GotReply(resp.ReqID, resp.BV) + deliverMsg = &Msg{ + MsgType: MsgCode, + ReqID: resp.ReqID, + Obj: resp.Data, + } + + case GetReceiptsMsg: + glog.V(logger.Debug).Infof("LES: received GetReceiptsMsg from peer %v", p.id) + // Decode the retrieval message + var req struct { + ReqID uint64 + Hashes []common.Hash + } + if err := msg.Decode(&req); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + // Gather state data until the fetch or network limits is reached + var ( + bytes int + receipts []rlp.RawValue + ) + reqCnt = len(req.Hashes) + if reqCnt > maxReqs { + return errResp(ErrRequestRejected, "") + } + for _, hash := range req.Hashes { + if bytes >= softResponseLimit || len(receipts) >= MaxReceiptFetch { + break + } + // Retrieve the requested block's receipts, skipping if unknown to us + results := core.GetBlockReceipts(pm.chainDb, hash, core.GetBlockNumber(pm.chainDb, 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 { + glog.V(logger.Error).Infof("failed to encode receipt: %v", err) + } else { + receipts = append(receipts, encoded) + bytes += len(encoded) + } + } + bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) + pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) + return p.SendReceiptsRLP(req.ReqID, bv, receipts) + + case ReceiptsMsg: + if pm.odr == nil { + return errResp(ErrUnexpectedResponse, "") + } + + glog.V(logger.Debug).Infof("LES: received ReceiptsMsg from peer %v", p.id) + // A batch of receipts arrived to one of our previous requests + var resp struct { + ReqID, BV uint64 + Receipts []types.Receipts + } + if err := msg.Decode(&resp); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + p.fcServer.GotReply(resp.ReqID, resp.BV) + deliverMsg = &Msg{ + MsgType: MsgReceipts, + ReqID: resp.ReqID, + Obj: resp.Receipts, + } + + case GetProofsMsg: + glog.V(logger.Debug).Infof("LES: received GetProofsMsg from peer %v", p.id) + // Decode the retrieval message + var req struct { + ReqID uint64 + Reqs []ProofReq + } + if err := msg.Decode(&req); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + // Gather state data until the fetch or network limits is reached + var ( + bytes int + proofs proofsData + ) + reqCnt = len(req.Reqs) + if reqCnt > maxReqs { + return errResp(ErrRequestRejected, "") + } + for _, req := range req.Reqs { + if bytes >= softResponseLimit || len(proofs) >= MaxProofsFetch { + break + } + // Retrieve the requested state entry, stopping if enough was found + if header := core.GetHeader(pm.chainDb, req.BHash, core.GetBlockNumber(pm.chainDb, req.BHash)); header != nil { + if tr, _ := trie.New(header.Root, pm.chainDb); tr != nil { + if len(req.AccKey) > 0 { + data := tr.Get(req.AccKey) + tr = nil + if so, err := state.DecodeObject(common.Address{}, pm.chainDb, data); err == nil { + tr, _ = trie.New(common.BytesToHash(so.Root()), pm.chainDb) + } + } + if tr != nil { + proof := tr.Prove(req.Key) + proofs = append(proofs, proof) + bytes += len(proof) + } + } + } + } + bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) + pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) + return p.SendProofs(req.ReqID, bv, proofs) + + case ProofsMsg: + if pm.odr == nil { + return errResp(ErrUnexpectedResponse, "") + } + + glog.V(logger.Debug).Infof("LES: received ProofsMsg from peer %v", p.id) + // A batch of merkle proofs arrived to one of our previous requests + var resp struct { + ReqID, BV uint64 + Data [][]rlp.RawValue + } + if err := msg.Decode(&resp); err != nil { + return errResp(ErrDecode, "msg %v: %v", msg, err) + } + p.fcServer.GotReply(resp.ReqID, resp.BV) + deliverMsg = &Msg{ + MsgType: MsgProofs, + ReqID: resp.ReqID, + Obj: resp.Data, + } + + case SendTxMsg: + if pm.txpool == nil { + return errResp(ErrUnexpectedResponse, "") + } + // Transactions arrived, 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) + } + reqCnt = len(txs) + if reqCnt > maxReqs { + return errResp(ErrRequestRejected, "") + } + pm.txpool.AddTransactions(txs) + _, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) + pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) + + default: + glog.V(logger.Debug).Infof("LES: received unknown message with code %d from peer %v", msg.Code, p.id) + return errResp(ErrInvalidMsgCode, "%v", msg.Code) + } + + if deliverMsg != nil { + return pm.odr.Deliver(p, deliverMsg) + } + + return nil +} + +// NodeInfo retrieves some protocol metadata about the running host node. +func (self *ProtocolManager) NodeInfo() *eth.EthNodeInfo { + return ð.EthNodeInfo{ + Network: self.networkId, + Difficulty: self.blockchain.GetTdByHash(self.blockchain.LastBlockHash()), + Genesis: self.blockchain.Genesis().Hash(), + Head: self.blockchain.LastBlockHash(), + } +} diff --git a/les/handler_test.go b/les/handler_test.go new file mode 100644 index 0000000000..55466178cf --- /dev/null +++ b/les/handler_test.go @@ -0,0 +1,322 @@ +package les + +import ( + "math/rand" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" +) + +func expectResponse(r p2p.MsgReader, msgcode, reqID, bv uint64, data interface{}) error { + type resp struct { + ReqID, BV uint64 + Data interface{} + } + return p2p.ExpectMsg(r, msgcode, resp{reqID, bv, data}) +} + +// Tests that block headers can be retrieved from a remote chain based on user queries. +func TestGetBlockHeadersLes1(t *testing.T) { testGetBlockHeaders(t, 1) } + +func testGetBlockHeaders(t *testing.T, protocol int) { + pm, _, _ := newTestProtocolManagerMust(t, false, downloader.MaxHashFetch+15, nil) + bc := pm.blockchain.(*core.BlockChain) + peer, _ := newTestPeer(t, "peer", protocol, pm, true) + defer peer.close() + + // Create a "random" unknown hash for testing + var unknown common.Hash + for i, _ := range unknown { + unknown[i] = byte(i) + } + // Create a batch of tests for various scenarios + limit := uint64(MaxHeaderFetch) + tests := []struct { + query *getBlockHeadersData // The query to execute for header retrieval + expect []common.Hash // The hashes of the block whose headers are expected + }{ + // A single random block should be retrievable by hash and number too + { + &getBlockHeadersData{Origin: hashOrNumber{Hash: bc.GetBlockByNumber(limit / 2).Hash()}, Amount: 1}, + []common.Hash{bc.GetBlockByNumber(limit / 2).Hash()}, + }, { + &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 1}, + []common.Hash{bc.GetBlockByNumber(limit / 2).Hash()}, + }, + // Multiple headers should be retrievable in both directions + { + &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3}, + []common.Hash{ + bc.GetBlockByNumber(limit / 2).Hash(), + bc.GetBlockByNumber(limit/2 + 1).Hash(), + bc.GetBlockByNumber(limit/2 + 2).Hash(), + }, + }, { + &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3, Reverse: true}, + []common.Hash{ + bc.GetBlockByNumber(limit / 2).Hash(), + bc.GetBlockByNumber(limit/2 - 1).Hash(), + bc.GetBlockByNumber(limit/2 - 2).Hash(), + }, + }, + // Multiple headers with skip lists should be retrievable + { + &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3}, + []common.Hash{ + bc.GetBlockByNumber(limit / 2).Hash(), + bc.GetBlockByNumber(limit/2 + 4).Hash(), + bc.GetBlockByNumber(limit/2 + 8).Hash(), + }, + }, { + &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3, Reverse: true}, + []common.Hash{ + bc.GetBlockByNumber(limit / 2).Hash(), + bc.GetBlockByNumber(limit/2 - 4).Hash(), + bc.GetBlockByNumber(limit/2 - 8).Hash(), + }, + }, + // The chain endpoints should be retrievable + { + &getBlockHeadersData{Origin: hashOrNumber{Number: 0}, Amount: 1}, + []common.Hash{bc.GetBlockByNumber(0).Hash()}, + }, { + &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64()}, Amount: 1}, + []common.Hash{bc.CurrentBlock().Hash()}, + }, + // Ensure protocol limits are honored + { + &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() - 1}, Amount: limit + 10, Reverse: true}, + bc.GetBlockHashesFromHash(bc.CurrentBlock().Hash(), limit), + }, + // Check that requesting more than available is handled gracefully + { + &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() - 4}, Skip: 3, Amount: 3}, + []common.Hash{ + bc.GetBlockByNumber(bc.CurrentBlock().NumberU64() - 4).Hash(), + bc.GetBlockByNumber(bc.CurrentBlock().NumberU64()).Hash(), + }, + }, { + &getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 3, Amount: 3, Reverse: true}, + []common.Hash{ + bc.GetBlockByNumber(4).Hash(), + bc.GetBlockByNumber(0).Hash(), + }, + }, + // Check that requesting more than available is handled gracefully, even if mid skip + { + &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() - 4}, Skip: 2, Amount: 3}, + []common.Hash{ + bc.GetBlockByNumber(bc.CurrentBlock().NumberU64() - 4).Hash(), + bc.GetBlockByNumber(bc.CurrentBlock().NumberU64() - 1).Hash(), + }, + }, { + &getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 2, Amount: 3, Reverse: true}, + []common.Hash{ + bc.GetBlockByNumber(4).Hash(), + bc.GetBlockByNumber(1).Hash(), + }, + }, + // Check that non existing headers aren't returned + { + &getBlockHeadersData{Origin: hashOrNumber{Hash: unknown}, Amount: 1}, + []common.Hash{}, + }, { + &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() + 1}, Amount: 1}, + []common.Hash{}, + }, + } + // Run each of the tests and verify the results against the chain + var reqID uint64 + for i, tt := range tests { + // Collect the headers to expect in the response + headers := []*types.Header{} + for _, hash := range tt.expect { + headers = append(headers, bc.GetHeaderByHash(hash)) + } + // Send the hash request and verify the response + reqID++ + cost := peer.GetRequestCost(GetBlockHeadersMsg, int(tt.query.Amount)) + sendRequest(peer.app, GetBlockHeadersMsg, reqID, cost, tt.query) + if err := expectResponse(peer.app, BlockHeadersMsg, reqID, testBufLimit, headers); err != nil { + t.Errorf("test %d: headers mismatch: %v", i, err) + } + } +} + +// Tests that block contents can be retrieved from a remote chain based on their hashes. +func TestGetBlockBodiesLes1(t *testing.T) { testGetBlockBodies(t, 1) } + +func testGetBlockBodies(t *testing.T, protocol int) { + pm, _, _ := newTestProtocolManagerMust(t, false, downloader.MaxBlockFetch+15, nil) + bc := pm.blockchain.(*core.BlockChain) + peer, _ := newTestPeer(t, "peer", protocol, pm, true) + defer peer.close() + + // Create a batch of tests for various scenarios + limit := MaxBodyFetch + tests := []struct { + random int // Number of blocks to fetch randomly from the chain + explicit []common.Hash // Explicitly requested blocks + available []bool // Availability of explicitly requested blocks + expected int // Total number of existing blocks to expect + }{ + {1, nil, nil, 1}, // A single random block should be retrievable + {10, nil, nil, 10}, // Multiple random blocks should be retrievable + {limit, nil, nil, limit}, // The maximum possible blocks should be retrievable + {limit + 1, nil, nil, limit}, // No more than the possible block count should be returned + {0, []common.Hash{bc.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable + {0, []common.Hash{bc.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable + {0, []common.Hash{common.Hash{}}, []bool{false}, 0}, // A non existent block should not be returned + + // Existing and non-existing blocks interleaved should not cause problems + {0, []common.Hash{ + common.Hash{}, + bc.GetBlockByNumber(1).Hash(), + common.Hash{}, + bc.GetBlockByNumber(10).Hash(), + common.Hash{}, + bc.GetBlockByNumber(100).Hash(), + common.Hash{}, + }, []bool{false, true, false, true, false, true, false}, 3}, + } + // Run each of the tests and verify the results against the chain + var reqID uint64 + for i, tt := range tests { + // Collect the hashes to request, and the response to expect + hashes, seen := []common.Hash{}, make(map[int64]bool) + bodies := []*types.Body{} + + for j := 0; j < tt.random; j++ { + for { + num := rand.Int63n(int64(bc.CurrentBlock().NumberU64())) + if !seen[num] { + seen[num] = true + + block := bc.GetBlockByNumber(uint64(num)) + hashes = append(hashes, block.Hash()) + if len(bodies) < tt.expected { + bodies = append(bodies, &types.Body{Transactions: block.Transactions(), Uncles: block.Uncles()}) + } + break + } + } + } + for j, hash := range tt.explicit { + hashes = append(hashes, hash) + if tt.available[j] && len(bodies) < tt.expected { + block := bc.GetBlockByHash(hash) + bodies = append(bodies, &types.Body{Transactions: block.Transactions(), Uncles: block.Uncles()}) + } + } + reqID++ + // Send the hash request and verify the response + cost := peer.GetRequestCost(GetBlockBodiesMsg, len(hashes)) + sendRequest(peer.app, GetBlockBodiesMsg, reqID, cost, hashes) + if err := expectResponse(peer.app, BlockBodiesMsg, reqID, testBufLimit, bodies); err != nil { + t.Errorf("test %d: bodies mismatch: %v", i, err) + } + } +} + +// Tests that the contract codes can be retrieved based on account addresses. +func TestGetCodeLes1(t *testing.T) { testGetCode(t, 1) } + +func testGetCode(t *testing.T, protocol int) { + // Assemble the test environment + pm, _, _ := newTestProtocolManagerMust(t, false, 4, testChainGen) + bc := pm.blockchain.(*core.BlockChain) + peer, _ := newTestPeer(t, "peer", protocol, pm, true) + defer peer.close() + + var codereqs []*CodeReq + var codes [][]byte + + for i := uint64(0); i <= bc.CurrentBlock().NumberU64(); i++ { + header := bc.GetHeaderByNumber(i) + req := &CodeReq{ + BHash: header.Hash(), + AccKey: crypto.Keccak256(testContractAddr[:]), + } + codereqs = append(codereqs, req) + if i >= testContractDeployed { + codes = append(codes, testContractCodeDeployed) + } + } + + cost := peer.GetRequestCost(GetCodeMsg, len(codereqs)) + sendRequest(peer.app, GetCodeMsg, 42, cost, codereqs) + if err := expectResponse(peer.app, CodeMsg, 42, testBufLimit, codes); err != nil { + t.Errorf("codes mismatch: %v", err) + } +} + +// Tests that the transaction receipts can be retrieved based on hashes. +func TestGetReceiptLes1(t *testing.T) { testGetReceipt(t, 1) } + +func testGetReceipt(t *testing.T, protocol int) { + // Assemble the test environment + pm, db, _ := newTestProtocolManagerMust(t, false, 4, testChainGen) + bc := pm.blockchain.(*core.BlockChain) + peer, _ := newTestPeer(t, "peer", protocol, pm, true) + defer peer.close() + + // Collect the hashes to request, and the response to expect + hashes, receipts := []common.Hash{}, []types.Receipts{} + for i := uint64(0); i <= bc.CurrentBlock().NumberU64(); i++ { + block := bc.GetBlockByNumber(i) + + hashes = append(hashes, block.Hash()) + receipts = append(receipts, core.GetBlockReceipts(db, block.Hash(), block.NumberU64())) + } + // Send the hash request and verify the response + cost := peer.GetRequestCost(GetReceiptsMsg, len(hashes)) + sendRequest(peer.app, GetReceiptsMsg, 42, cost, hashes) + if err := expectResponse(peer.app, ReceiptsMsg, 42, testBufLimit, receipts); err != nil { + t.Errorf("receipts mismatch: %v", err) + } +} + +// Tests that trie merkle proofs can be retrieved +func TestGetProofsLes1(t *testing.T) { testGetReceipt(t, 1) } + +func testGetProofs(t *testing.T, protocol int) { + // Assemble the test environment + pm, db, _ := newTestProtocolManagerMust(t, false, 4, testChainGen) + bc := pm.blockchain.(*core.BlockChain) + peer, _ := newTestPeer(t, "peer", protocol, pm, true) + defer peer.close() + + var proofreqs []ProofReq + var proofs [][]rlp.RawValue + + accounts := []common.Address{testBankAddress, acc1Addr, acc2Addr, common.Address{}} + for i := uint64(0); i <= bc.CurrentBlock().NumberU64(); i++ { + header := bc.GetHeaderByNumber(i) + root := header.Root + trie, _ := trie.NewSecure(root, db) + + for _, acc := range accounts { + req := ProofReq{ + BHash: header.Hash(), + Key: acc[:], + } + proofreqs = append(proofreqs, req) + + proof := trie.Prove(acc[:]) + proofs = append(proofs, proof) + } + } + // Send the proof request and verify the response + cost := peer.GetRequestCost(GetProofsMsg, len(proofreqs)) + sendRequest(peer.app, GetProofsMsg, 42, cost, proofreqs) + if err := expectResponse(peer.app, ProofsMsg, 42, testBufLimit, proofs); err != nil { + t.Errorf("proofs mismatch: %v", err) + } +} diff --git a/les/helper_test.go b/les/helper_test.go new file mode 100644 index 0000000000..5d74370045 --- /dev/null +++ b/les/helper_test.go @@ -0,0 +1,316 @@ +// This file contains some shares testing functionality, common to multiple +// different files and modules being tested. + +package les + +import ( + "crypto/ecdsa" + "crypto/rand" + "math/big" + "sync" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/les/flowcontrol" + "github.com/ethereum/go-ethereum/light" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/discover" + "github.com/ethereum/go-ethereum/params" +) + +var ( + testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) + testBankFunds = big.NewInt(1000000) + + acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") + acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") + acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey) + acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey) + + testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056") + testContractAddr common.Address + testContractCodeDeployed = testContractCode[16:] + testContractDeployed = uint64(2) + + testBufLimit = uint64(100) +) + +/* +contract test { + + uint256[100] data; + + function Put(uint256 addr, uint256 value) { + data[addr] = value; + } + + function Get(uint256 addr) constant returns (uint256 value) { + return data[addr]; + } +} +*/ + +func testChainGen(i int, block *core.BlockGen) { + switch i { + case 0: + // In block 1, the test bank sends account #1 some ether. + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) + block.AddTx(tx) + case 1: + // In block 2, the test bank sends some more ether to account #1. + // acc1Addr passes it on to account #2. + // acc1Addr creates a test contract. + tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey) + nonce := block.TxNonce(acc1Addr) + tx2, _ := types.NewTransaction(nonce, acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key) + nonce++ + tx3, _ := types.NewContractCreation(nonce, big.NewInt(0), big.NewInt(200000), big.NewInt(0), testContractCode).SignECDSA(acc1Key) + testContractAddr = crypto.CreateAddress(acc1Addr, nonce) + block.AddTx(tx1) + block.AddTx(tx2) + block.AddTx(tx3) + case 2: + // Block 3 is empty but was mined by account #2. + block.SetCoinbase(acc2Addr) + block.SetExtra([]byte("yeehaw")) + data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001") + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data).SignECDSA(testBankKey) + block.AddTx(tx) + case 3: + // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data). + b2 := block.PrevBlock(1).Header() + b2.Extra = []byte("foo") + block.AddUncle(b2) + b3 := block.PrevBlock(2).Header() + b3.Extra = []byte("foo") + block.AddUncle(b3) + data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002") + tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data).SignECDSA(testBankKey) + block.AddTx(tx) + } +} + +func testRCL() RequestCostList { + cl := make(RequestCostList, len(reqList)) + for i, code := range reqList { + cl[i].MsgCode = code + cl[i].BaseCost = 0 + cl[i].ReqCost = 0 + } + return cl +} + +// newTestProtocolManager creates a new protocol manager for testing purposes, +// with the given number of blocks already known, and potential notification +// channels for different events. +func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *core.BlockGen)) (*ProtocolManager, ethdb.Database, *LesOdr, error) { + var ( + evmux = new(event.TypeMux) + pow = new(core.FakePow) + db, _ = ethdb.NewMemDatabase() + genesis = core.WriteGenesisBlockForTesting(db, core.GenesisAccount{testBankAddress, testBankFunds}) + chainConfig = &core.ChainConfig{HomesteadBlock: big.NewInt(0)} // homestead set to 0 because of chain maker + odr *LesOdr + chain BlockChain + ) + + if lightSync { + odr = NewLesOdr(db) + chain, _ = light.NewLightChain(odr, chainConfig, pow, evmux) + } else { + blockchain, _ := core.NewBlockChain(db, chainConfig, pow, evmux) + gchain, _ := core.GenerateChain(genesis, db, blocks, generator) + if _, err := blockchain.InsertChain(gchain); err != nil { + panic(err) + } + chain = blockchain + } + + pm, err := NewProtocolManager(chainConfig, lightSync, NetworkId, evmux, pow, chain, nil, db, odr, nil) + if err != nil { + return nil, nil, nil, err + } + if !lightSync { + srv := &LesServer{protocolManager: pm} + pm.server = srv + + srv.defParams = &flowcontrol.ServerParams{ + BufLimit: testBufLimit, + MinRecharge: 1, + } + + srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000) + srv.fcCostStats = newCostStats(nil) + srv.fcCostStats.baseCost = 0 + for _, entry := range srv.fcCostStats.avg { + entry.baseCost = 0 + entry.reqCost = 0 + } + } + pm.Start() + return pm, db, odr, nil +} + +// newTestProtocolManagerMust creates a new protocol manager for testing purposes, +// with the given number of blocks already known, and potential notification +// channels for different events. In case of an error, the constructor force- +// fails the test. +func newTestProtocolManagerMust(t *testing.T, lightSync bool, blocks int, generator func(int, *core.BlockGen)) (*ProtocolManager, ethdb.Database, *LesOdr) { + pm, db, odr, err := newTestProtocolManager(lightSync, blocks, generator) + if err != nil { + t.Fatalf("Failed to create protocol manager: %v", err) + } + return pm, db, odr +} + +// testTxPool is a fake, helper transaction pool for testing purposes +type testTxPool struct { + pool []*types.Transaction // Collection of all transactions + added chan<- []*types.Transaction // Notification channel for new transactions + + lock sync.RWMutex // Protects the transaction pool +} + +// AddTransactions appends a batch of transactions to the pool, and notifies any +// listeners if the addition channel is non nil +func (p *testTxPool) AddTransactions(txs []*types.Transaction) { + p.lock.Lock() + defer p.lock.Unlock() + + p.pool = append(p.pool, txs...) + if p.added != nil { + p.added <- txs + } +} + +// GetTransactions returns all the transactions known to the pool +func (p *testTxPool) GetTransactions() types.Transactions { + p.lock.RLock() + defer p.lock.RUnlock() + + txs := make([]*types.Transaction, len(p.pool)) + copy(txs, p.pool) + + return txs +} + +// newTestTransaction create a new dummy transaction. +func newTestTransaction(from *ecdsa.PrivateKey, nonce uint64, datasize int) *types.Transaction { + tx := types.NewTransaction(nonce, common.Address{}, big.NewInt(0), big.NewInt(100000), big.NewInt(0), make([]byte, datasize)) + tx, _ = tx.SignECDSA(from) + + return tx +} + +// testPeer is a simulated peer to allow testing direct network calls. +type testPeer struct { + net p2p.MsgReadWriter // Network layer reader/writer to simulate remote messaging + app *p2p.MsgPipeRW // Application layer reader/writer to simulate the local side + *peer +} + +// newTestPeer creates a new peer registered at the given protocol manager. +func newTestPeer(t *testing.T, name string, version int, pm *ProtocolManager, shake bool) (*testPeer, <-chan error) { + // Create a message pipe to communicate through + app, net := p2p.MsgPipe() + + // Generate a random id and create the peer + var id discover.NodeID + rand.Read(id[:]) + + peer := pm.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), net) + + // Start the peer on a new thread + errc := make(chan error, 1) + go func() { + select { + case pm.newPeerCh <- peer: + errc <- pm.handle(peer) + case <-pm.quitSync: + errc <- p2p.DiscQuitting + } + }() + tp := &testPeer{ + app: app, + net: net, + peer: peer, + } + // Execute any implicitly requested handshakes and return + if shake { + td, head, genesis := pm.blockchain.Status() + tp.handshake(t, td, head, genesis) + } + return tp, errc +} + +func newTestPeerPair(name string, version int, pm, pm2 *ProtocolManager) (*peer, <-chan error, *peer, <-chan error) { + // Create a message pipe to communicate through + app, net := p2p.MsgPipe() + + // Generate a random id and create the peer + var id discover.NodeID + rand.Read(id[:]) + + peer := pm.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), net) + peer2 := pm2.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), app) + + // Start the peer on a new thread + errc := make(chan error, 1) + errc2 := make(chan error, 1) + go func() { + select { + case pm.newPeerCh <- peer: + errc <- pm.handle(peer) + case <-pm.quitSync: + errc <- p2p.DiscQuitting + } + }() + go func() { + select { + case pm2.newPeerCh <- peer2: + errc2 <- pm2.handle(peer2) + case <-pm2.quitSync: + errc2 <- p2p.DiscQuitting + } + }() + return peer, errc, peer2, errc2 +} + +// handshake simulates a trivial handshake that expects the same state from the +// remote side as we are simulating locally. +func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, genesis common.Hash) { + var expList keyValueList + expList = expList.add("protocolVersion", uint64(p.version)) + expList = expList.add("networkId", uint64(NetworkId)) + expList = expList.add("td", td) + expList = expList.add("bestHash", head) + expList = expList.add("genesisHash", genesis) + sendList := make(keyValueList, len(expList)) + copy(sendList, expList) + expList = expList.add("serveHeaders", nil) + expList = expList.add("serveChainSince", uint64(0)) + expList = expList.add("serveStateSince", uint64(0)) + expList = expList.add("txRelay", nil) + expList = expList.add("flowControl/BL", testBufLimit) + expList = expList.add("flowControl/MRR", uint64(1)) + expList = expList.add("flowControl/MRC", testRCL()) + + if err := p2p.ExpectMsg(p.app, StatusMsg, expList); err != nil { + t.Fatalf("status recv: %v", err) + } + if err := p2p.Send(p.app, StatusMsg, sendList); err != nil { + t.Fatalf("status send: %v", err) + } +} + +// close terminates the local side of the peer, notifying the remote protocol +// manager of termination. +func (p *testPeer) close() { + p.app.Close() +} diff --git a/les/metrics.go b/les/metrics.go new file mode 100644 index 0000000000..88e6726e24 --- /dev/null +++ b/les/metrics.go @@ -0,0 +1,111 @@ +// 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 . + +package les + +import ( + "github.com/ethereum/go-ethereum/metrics" + "github.com/ethereum/go-ethereum/p2p" +) + +var ( + /* propTxnInPacketsMeter = metrics.NewMeter("eth/prop/txns/in/packets") + propTxnInTrafficMeter = metrics.NewMeter("eth/prop/txns/in/traffic") + propTxnOutPacketsMeter = metrics.NewMeter("eth/prop/txns/out/packets") + propTxnOutTrafficMeter = metrics.NewMeter("eth/prop/txns/out/traffic") + propHashInPacketsMeter = metrics.NewMeter("eth/prop/hashes/in/packets") + propHashInTrafficMeter = metrics.NewMeter("eth/prop/hashes/in/traffic") + propHashOutPacketsMeter = metrics.NewMeter("eth/prop/hashes/out/packets") + propHashOutTrafficMeter = metrics.NewMeter("eth/prop/hashes/out/traffic") + propBlockInPacketsMeter = metrics.NewMeter("eth/prop/blocks/in/packets") + propBlockInTrafficMeter = metrics.NewMeter("eth/prop/blocks/in/traffic") + propBlockOutPacketsMeter = metrics.NewMeter("eth/prop/blocks/out/packets") + propBlockOutTrafficMeter = metrics.NewMeter("eth/prop/blocks/out/traffic") + reqHashInPacketsMeter = metrics.NewMeter("eth/req/hashes/in/packets") + reqHashInTrafficMeter = metrics.NewMeter("eth/req/hashes/in/traffic") + reqHashOutPacketsMeter = metrics.NewMeter("eth/req/hashes/out/packets") + reqHashOutTrafficMeter = metrics.NewMeter("eth/req/hashes/out/traffic") + reqBlockInPacketsMeter = metrics.NewMeter("eth/req/blocks/in/packets") + reqBlockInTrafficMeter = metrics.NewMeter("eth/req/blocks/in/traffic") + reqBlockOutPacketsMeter = metrics.NewMeter("eth/req/blocks/out/packets") + reqBlockOutTrafficMeter = metrics.NewMeter("eth/req/blocks/out/traffic") + reqHeaderInPacketsMeter = metrics.NewMeter("eth/req/headers/in/packets") + reqHeaderInTrafficMeter = metrics.NewMeter("eth/req/headers/in/traffic") + reqHeaderOutPacketsMeter = metrics.NewMeter("eth/req/headers/out/packets") + reqHeaderOutTrafficMeter = metrics.NewMeter("eth/req/headers/out/traffic") + reqBodyInPacketsMeter = metrics.NewMeter("eth/req/bodies/in/packets") + reqBodyInTrafficMeter = metrics.NewMeter("eth/req/bodies/in/traffic") + reqBodyOutPacketsMeter = metrics.NewMeter("eth/req/bodies/out/packets") + reqBodyOutTrafficMeter = metrics.NewMeter("eth/req/bodies/out/traffic") + reqStateInPacketsMeter = metrics.NewMeter("eth/req/states/in/packets") + reqStateInTrafficMeter = metrics.NewMeter("eth/req/states/in/traffic") + reqStateOutPacketsMeter = metrics.NewMeter("eth/req/states/out/packets") + reqStateOutTrafficMeter = metrics.NewMeter("eth/req/states/out/traffic") + reqReceiptInPacketsMeter = metrics.NewMeter("eth/req/receipts/in/packets") + reqReceiptInTrafficMeter = metrics.NewMeter("eth/req/receipts/in/traffic") + reqReceiptOutPacketsMeter = metrics.NewMeter("eth/req/receipts/out/packets") + reqReceiptOutTrafficMeter = metrics.NewMeter("eth/req/receipts/out/traffic")*/ + miscInPacketsMeter = metrics.NewMeter("les/misc/in/packets") + miscInTrafficMeter = metrics.NewMeter("les/misc/in/traffic") + miscOutPacketsMeter = metrics.NewMeter("les/misc/out/packets") + miscOutTrafficMeter = metrics.NewMeter("les/misc/out/traffic") +) + +// meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of +// accumulating the above defined metrics based on the data stream contents. +type meteredMsgReadWriter struct { + p2p.MsgReadWriter // Wrapped message stream to meter + version int // Protocol version to select correct meters +} + +// newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the +// metrics system is disabled, this fucntion returns the original object. +func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter { + if !metrics.Enabled { + return rw + } + return &meteredMsgReadWriter{MsgReadWriter: rw} +} + +// Init sets the protocol version used by the stream to know which meters to +// increment in case of overlapping message ids between protocol versions. +func (rw *meteredMsgReadWriter) Init(version int) { + rw.version = version +} + +func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) { + // Read the message and short circuit in case of an error + msg, err := rw.MsgReadWriter.ReadMsg() + if err != nil { + return msg, err + } + // Account for the data traffic + packets, traffic := miscInPacketsMeter, miscInTrafficMeter + packets.Mark(1) + traffic.Mark(int64(msg.Size)) + + return msg, err +} + +func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error { + // Account for the data traffic + packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter + packets.Mark(1) + traffic.Mark(int64(msg.Size)) + + // Send the packet to the p2p layer + return rw.MsgReadWriter.WriteMsg(msg) +} diff --git a/les/odr.go b/les/odr.go new file mode 100644 index 0000000000..4f3c6e74c2 --- /dev/null +++ b/les/odr.go @@ -0,0 +1,245 @@ +// 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 . +package les + +import ( + "sync" + "time" + + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/light" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "golang.org/x/net/context" +) + +var ( + softRequestTimeout = time.Millisecond * 500 + hardRequestTimeout = time.Second * 10 + retryPeers = time.Second * 1 +) + +// peerDropFn is a callback type for dropping a peer detected as malicious. +type peerDropFn func(id string) + +type LesOdr struct { + light.OdrBackend + db ethdb.Database + stop chan struct{} + removePeer peerDropFn + mlock, clock sync.Mutex + sentReqs map[uint64]*sentReq + peers *odrPeerSet + lastReqID uint64 +} + +func NewLesOdr(db ethdb.Database) *LesOdr { + return &LesOdr{ + db: db, + stop: make(chan struct{}), + peers: newOdrPeerSet(), + sentReqs: make(map[uint64]*sentReq), + } +} + +func (odr *LesOdr) Stop() { + close(odr.stop) +} + +func (odr *LesOdr) Database() ethdb.Database { + return odr.db +} + +// validatorFunc is a function that processes a message and returns true if +// it was a meaningful answer to a given request +type validatorFunc func(ethdb.Database, *Msg) bool + +// sentReq is a request waiting for an answer that satisfies its valFunc +type sentReq struct { + valFunc validatorFunc + sentTo map[*peer]chan struct{} + lock sync.RWMutex // protects acces to sentTo + answered chan struct{} // closed and set to nil when any peer answers it +} + +// RegisterPeer registers a new LES peer to the ODR capable peer set +func (self *LesOdr) RegisterPeer(p *peer) error { + return self.peers.register(p) +} + +// UnregisterPeer removes a peer from the ODR capable peer set +func (self *LesOdr) UnregisterPeer(p *peer) { + self.peers.unregister(p) +} + +const ( + MsgBlockBodies = iota + MsgCode + MsgReceipts + MsgProofs +) + +// Msg encodes a LES message that delivers reply data for a request +type Msg struct { + MsgType int + ReqID uint64 + Obj interface{} +} + +// Deliver is called by the LES protocol manager to deliver ODR reply messages to waiting requests +func (self *LesOdr) Deliver(peer *peer, msg *Msg) error { + var delivered chan struct{} + self.mlock.Lock() + req, ok := self.sentReqs[msg.ReqID] + self.mlock.Unlock() + if ok { + req.lock.Lock() + delivered, ok = req.sentTo[peer] + req.lock.Unlock() + } + + if !ok { + return errResp(ErrUnexpectedResponse, "reqID = %v", msg.ReqID) + } + + if req.valFunc(self.db, msg) { + close(delivered) + req.lock.Lock() + if req.answered != nil { + close(req.answered) + req.answered = nil + } + req.lock.Unlock() + return nil + } + return errResp(ErrInvalidResponse, "reqID = %v", msg.ReqID) +} + +func (self *LesOdr) requestPeer(req *sentReq, peer *peer, delivered, timeout chan struct{}, reqWg *sync.WaitGroup) { + stime := time.Now() + defer func() { + req.lock.Lock() + delete(req.sentTo, peer) + req.lock.Unlock() + reqWg.Done() + }() + + select { + case <-delivered: + servTime := uint64(time.Now().Sub(stime)) + self.peers.updateTimeout(peer, false) + self.peers.updateServTime(peer, servTime) + return + case <-time.After(softRequestTimeout): + close(timeout) + if self.peers.updateTimeout(peer, true) { + self.removePeer(peer.id) + } + case <-self.stop: + return + } + + select { + case <-delivered: + servTime := uint64(time.Now().Sub(stime)) + self.peers.updateServTime(peer, servTime) + return + case <-time.After(hardRequestTimeout): + self.removePeer(peer.id) + case <-self.stop: + return + } +} + +// networkRequest sends a request to known peers until an answer is received +// or the context is cancelled +func (self *LesOdr) networkRequest(ctx context.Context, lreq LesOdrRequest) error { + answered := make(chan struct{}) + req := &sentReq{ + valFunc: lreq.Valid, + sentTo: make(map[*peer]chan struct{}), + answered: answered, // reply delivered by any peer + } + reqID := self.getNextReqID() + self.mlock.Lock() + self.sentReqs[reqID] = req + self.mlock.Unlock() + + reqWg := new(sync.WaitGroup) + reqWg.Add(1) + defer reqWg.Done() + go func() { + reqWg.Wait() + self.mlock.Lock() + delete(self.sentReqs, reqID) + self.mlock.Unlock() + }() + + exclude := make(map[*peer]struct{}) + for { + if peer := self.peers.bestPeer(lreq, exclude); peer == nil { + select { + case <-ctx.Done(): + return ctx.Err() + case <-req.answered: + return nil + case <-time.After(retryPeers): + } + } else { + exclude[peer] = struct{}{} + delivered := make(chan struct{}) + timeout := make(chan struct{}) + req.lock.Lock() + req.sentTo[peer] = delivered + req.lock.Unlock() + reqWg.Add(1) + cost := lreq.GetCost(peer) + peer.fcServer.SendRequest(reqID, cost) + go self.requestPeer(req, peer, delivered, timeout, reqWg) + lreq.Request(reqID, peer) + + select { + case <-ctx.Done(): + return ctx.Err() + case <-answered: + return nil + case <-timeout: + } + } + } +} + +// Retrieve tries to fetch an object from the local db, then from the LES network. +// If the network retrieval was successful, it stores the object in local db. +func (self *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) { + lreq := LesRequest(req) + err = self.networkRequest(ctx, lreq) + if err == nil { + // retrieved from network, store in db + req.StoreResult(self.db) + } else { + glog.V(logger.Debug).Infof("networkRequest err = %v", err) + } + return +} + +func (self *LesOdr) getNextReqID() uint64 { + self.clock.Lock() + defer self.clock.Unlock() + + self.lastReqID++ + return self.lastReqID +} diff --git a/les/odr_peerset.go b/les/odr_peerset.go new file mode 100644 index 0000000000..ff6a223543 --- /dev/null +++ b/les/odr_peerset.go @@ -0,0 +1,119 @@ +// 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 . +package les + +import ( + "sync" +) + +const dropTimeoutRatio = 20 + +type odrPeerInfo struct { + reqTimeSum, reqTimeCnt, reqCnt, timeoutCnt uint64 +} + +// odrPeerSet represents the collection of active peer participating in the block +// download procedure. +type odrPeerSet struct { + peers map[*peer]*odrPeerInfo + lock sync.RWMutex +} + +// newPeerSet creates a new peer set top track the active download sources. +func newOdrPeerSet() *odrPeerSet { + return &odrPeerSet{ + peers: make(map[*peer]*odrPeerInfo), + } +} + +// Register injects a new peer into the working set, or returns an error if the +// peer is already known. +func (ps *odrPeerSet) register(p *peer) error { + ps.lock.Lock() + defer ps.lock.Unlock() + + if _, ok := ps.peers[p]; ok { + return errAlreadyRegistered + } + ps.peers[p] = &odrPeerInfo{} + return nil +} + +// Unregister removes a remote peer from the active set, disabling any further +// actions to/from that particular entity. +func (ps *odrPeerSet) unregister(p *peer) error { + ps.lock.Lock() + defer ps.lock.Unlock() + + if _, ok := ps.peers[p]; !ok { + return errNotRegistered + } + delete(ps.peers, p) + return nil +} + +func (ps *odrPeerSet) peerPriority(p *peer, info *odrPeerInfo, req LesOdrRequest) uint64 { + tm := p.fcServer.CanSend(req.GetCost(p)) + if info.reqCnt > 0 { + tm += info.reqTimeSum / info.reqTimeCnt + } + return tm +} + +func (ps *odrPeerSet) bestPeer(req LesOdrRequest, exclude map[*peer]struct{}) *peer { + var best *peer + var bpv uint64 + ps.lock.Lock() + defer ps.lock.Unlock() + + for p, info := range ps.peers { + if _, ok := exclude[p]; !ok { + pv := ps.peerPriority(p, info, req) + if best == nil || pv < bpv { + best = p + bpv = pv + } + } + } + return best +} + +func (ps *odrPeerSet) updateTimeout(p *peer, timeout bool) (drop bool) { + ps.lock.Lock() + defer ps.lock.Unlock() + + if info, ok := ps.peers[p]; ok { + info.reqCnt++ + if timeout { + // check ratio before increase to allow an extra timeout + if info.timeoutCnt*dropTimeoutRatio >= info.reqCnt { + return true + } + info.timeoutCnt++ + } + } + return false +} + +func (ps *odrPeerSet) updateServTime(p *peer, servTime uint64) { + ps.lock.Lock() + defer ps.lock.Unlock() + + if info, ok := ps.peers[p]; ok { + info.reqTimeSum += servTime + info.reqTimeCnt++ + } +} diff --git a/les/odr_requests.go b/les/odr_requests.go new file mode 100644 index 0000000000..797a04f786 --- /dev/null +++ b/les/odr_requests.go @@ -0,0 +1,254 @@ +// 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 . + +// Package light implements on-demand retrieval capable state and chain objects +// for the Ethereum Light Client. +package les + +import ( + "bytes" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/light" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" +) + +type LesOdrRequest interface { + GetCost(*peer) uint64 + Request(uint64, *peer) error + Valid(ethdb.Database, *Msg) bool // if true, keeps the retrieved object +} + +func LesRequest(req light.OdrRequest) LesOdrRequest { + switch r := req.(type) { + case *light.BlockRequest: + return (*BlockRequest)(r) + case *light.ReceiptsRequest: + return (*ReceiptsRequest)(r) + case *light.TrieRequest: + return (*TrieRequest)(r) + case *light.CodeRequest: + return (*CodeRequest)(r) + default: + return nil + } +} + +// BlockRequest is the ODR request type for block bodies +type BlockRequest light.BlockRequest + +// GetCost returns the cost of the given ODR request according to the serving +// peer's cost table (implementation of LesOdrRequest) +func (self *BlockRequest) GetCost(peer *peer) uint64 { + return peer.GetRequestCost(GetBlockBodiesMsg, 1) +} + +// Request sends an ODR request to the LES network (implementation of LesOdrRequest) +func (self *BlockRequest) Request(reqID uint64, peer *peer) error { + glog.V(logger.Debug).Infof("ODR: requesting body of block %08x from peer %v", self.Hash[:4], peer.id) + return peer.RequestBodies(reqID, self.GetCost(peer), []common.Hash{self.Hash}) +} + +// Valid processes an ODR request reply message from the LES network +// returns true and stores results in memory if the message was a valid reply +// to the request (implementation of LesOdrRequest) +func (self *BlockRequest) Valid(db ethdb.Database, msg *Msg) bool { + glog.V(logger.Debug).Infof("ODR: validating body of block %08x", self.Hash[:4]) + if msg.MsgType != MsgBlockBodies { + glog.V(logger.Debug).Infof("ODR: invalid message type") + return false + } + bodies := msg.Obj.([]*types.Body) + if len(bodies) != 1 { + glog.V(logger.Debug).Infof("ODR: invalid number of entries: %d", len(bodies)) + return false + } + body := bodies[0] + header := core.GetHeader(db, self.Hash, self.Number) + if header == nil { + glog.V(logger.Debug).Infof("ODR: header not found for block %08x", self.Hash[:4]) + return false + } + txHash := types.DeriveSha(types.Transactions(body.Transactions)) + if header.TxHash != txHash { + glog.V(logger.Debug).Infof("ODR: header.TxHash %08x does not match received txHash %08x", header.TxHash[:4], txHash[:4]) + return false + } + uncleHash := types.CalcUncleHash(body.Uncles) + if header.UncleHash != uncleHash { + glog.V(logger.Debug).Infof("ODR: header.UncleHash %08x does not match received uncleHash %08x", header.UncleHash[:4], uncleHash[:4]) + return false + } + data, err := rlp.EncodeToBytes(body) + if err != nil { + glog.V(logger.Debug).Infof("ODR: body RLP encode error: %v", err) + return false + } + self.Rlp = data + glog.V(logger.Debug).Infof("ODR: validation successful") + return true +} + +// ReceiptsRequest is the ODR request type for block receipts by block hash +type ReceiptsRequest light.ReceiptsRequest + +// GetCost returns the cost of the given ODR request according to the serving +// peer's cost table (implementation of LesOdrRequest) +func (self *ReceiptsRequest) GetCost(peer *peer) uint64 { + return peer.GetRequestCost(GetReceiptsMsg, 1) +} + +// Request sends an ODR request to the LES network (implementation of LesOdrRequest) +func (self *ReceiptsRequest) Request(reqID uint64, peer *peer) error { + glog.V(logger.Debug).Infof("ODR: requesting receipts for block %08x from peer %v", self.Hash[:4], peer.id) + return peer.RequestReceipts(reqID, self.GetCost(peer), []common.Hash{self.Hash}) +} + +// Valid processes an ODR request reply message from the LES network +// returns true and stores results in memory if the message was a valid reply +// to the request (implementation of LesOdrRequest) +func (self *ReceiptsRequest) Valid(db ethdb.Database, msg *Msg) bool { + glog.V(logger.Debug).Infof("ODR: validating receipts for block %08x", self.Hash[:4]) + if msg.MsgType != MsgReceipts { + glog.V(logger.Debug).Infof("ODR: invalid message type") + return false + } + receipts := msg.Obj.([]types.Receipts) + if len(receipts) != 1 { + glog.V(logger.Debug).Infof("ODR: invalid number of entries: %d", len(receipts)) + return false + } + hash := types.DeriveSha(receipts[0]) + header := core.GetHeader(db, self.Hash, self.Number) + if header == nil { + glog.V(logger.Debug).Infof("ODR: header not found for block %08x", self.Hash[:4]) + return false + } + if !bytes.Equal(header.ReceiptHash[:], hash[:]) { + glog.V(logger.Debug).Infof("ODR: header receipts hash %08x does not match calculated RLP hash %08x", header.ReceiptHash[:4], hash[:4]) + return false + } + self.Receipts = receipts[0] + glog.V(logger.Debug).Infof("ODR: validation successful") + return true +} + +type ProofReq struct { + BHash common.Hash + AccKey, Key []byte + FromLevel uint +} + +// ODR request type for state/storage trie entries, see LesOdrRequest interface +type TrieRequest light.TrieRequest + +// GetCost returns the cost of the given ODR request according to the serving +// peer's cost table (implementation of LesOdrRequest) +func (self *TrieRequest) GetCost(peer *peer) uint64 { + return peer.GetRequestCost(GetProofsMsg, 1) +} + +// Request sends an ODR request to the LES network (implementation of LesOdrRequest) +func (self *TrieRequest) Request(reqID uint64, peer *peer) error { + glog.V(logger.Debug).Infof("ODR: requesting trie root %08x key %08x from peer %v", self.Id.Root[:4], self.Key[:4], peer.id) + req := &ProofReq{ + BHash: self.Id.BlockHash, + AccKey: self.Id.AccKey, + Key: self.Key, + } + return peer.RequestProofs(reqID, self.GetCost(peer), []*ProofReq{req}) +} + +// Valid processes an ODR request reply message from the LES network +// returns true and stores results in memory if the message was a valid reply +// to the request (implementation of LesOdrRequest) +func (self *TrieRequest) Valid(db ethdb.Database, msg *Msg) bool { + glog.V(logger.Debug).Infof("ODR: validating trie root %08x key %08x", self.Id.Root[:4], self.Key[:4]) + + if msg.MsgType != MsgProofs { + glog.V(logger.Debug).Infof("ODR: invalid message type") + return false + } + proofs := msg.Obj.([][]rlp.RawValue) + if len(proofs) != 1 { + glog.V(logger.Debug).Infof("ODR: invalid number of entries: %d", len(proofs)) + return false + } + _, err := trie.VerifyProof(self.Id.Root, self.Key, proofs[0]) + if err != nil { + glog.V(logger.Debug).Infof("ODR: merkle proof verification error: %v", err) + return false + } + self.Proof = proofs[0] + glog.V(logger.Debug).Infof("ODR: validation successful") + return true +} + +type CodeReq struct { + BHash common.Hash + AccKey []byte +} + +// ODR request type for node data (used for retrieving contract code), see LesOdrRequest interface +type CodeRequest light.CodeRequest + +// GetCost returns the cost of the given ODR request according to the serving +// peer's cost table (implementation of LesOdrRequest) +func (self *CodeRequest) GetCost(peer *peer) uint64 { + return peer.GetRequestCost(GetCodeMsg, 1) +} + +// Request sends an ODR request to the LES network (implementation of LesOdrRequest) +func (self *CodeRequest) Request(reqID uint64, peer *peer) error { + glog.V(logger.Debug).Infof("ODR: requesting node data for hash %08x from peer %v", self.Hash[:4], peer.id) + req := &CodeReq{ + BHash: self.Id.BlockHash, + AccKey: self.Id.AccKey, + } + return peer.RequestCode(reqID, self.GetCost(peer), []*CodeReq{req}) +} + +// Valid processes an ODR request reply message from the LES network +// returns true and stores results in memory if the message was a valid reply +// to the request (implementation of LesOdrRequest) +func (self *CodeRequest) Valid(db ethdb.Database, msg *Msg) bool { + glog.V(logger.Debug).Infof("ODR: validating node data for hash %08x", self.Hash[:4]) + if msg.MsgType != MsgCode { + glog.V(logger.Debug).Infof("ODR: invalid message type") + return false + } + reply := msg.Obj.([][]byte) + if len(reply) != 1 { + glog.V(logger.Debug).Infof("ODR: invalid number of entries: %d", len(reply)) + return false + } + data := reply[0] + hash := crypto.Sha3Hash(data) + if !bytes.Equal(self.Hash[:], hash[:]) { + glog.V(logger.Debug).Infof("ODR: requested hash %08x does not match received data hash %08x", self.Hash[:4], hash[:4]) + return false + } + self.Data = data + glog.V(logger.Debug).Infof("ODR: validation successful") + return true +} diff --git a/les/odr_test.go b/les/odr_test.go new file mode 100644 index 0000000000..08d0c64656 --- /dev/null +++ b/les/odr_test.go @@ -0,0 +1,223 @@ +package les + +import ( + "bytes" + "math/big" + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/light" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" + "golang.org/x/net/context" +) + +type odrTestFn func(ctx context.Context, db ethdb.Database, config *core.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte + +func TestOdrGetBlockLes1(t *testing.T) { testOdr(t, 1, 1, odrGetBlock) } + +func odrGetBlock(ctx context.Context, db ethdb.Database, config *core.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { + var block *types.Block + if bc != nil { + block = bc.GetBlockByHash(bhash) + } else { + block, _ = lc.GetBlockByHash(ctx, bhash) + } + if block == nil { + return nil + } + rlp, _ := rlp.EncodeToBytes(block) + return rlp +} + +func TestOdrGetReceiptsLes1(t *testing.T) { testOdr(t, 1, 1, odrGetReceipts) } + +func odrGetReceipts(ctx context.Context, db ethdb.Database, config *core.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { + var receipts types.Receipts + if bc != nil { + receipts = core.GetBlockReceipts(db, bhash, core.GetBlockNumber(db, bhash)) + } else { + receipts, _ = light.GetBlockReceipts(ctx, lc.Odr(), bhash, core.GetBlockNumber(db, bhash)) + } + if receipts == nil { + return nil + } + rlp, _ := rlp.EncodeToBytes(receipts) + return rlp +} + +func TestOdrAccountsLes1(t *testing.T) { testOdr(t, 1, 1, odrAccounts) } + +func odrAccounts(ctx context.Context, db ethdb.Database, config *core.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { + dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678") + acc := []common.Address{testBankAddress, acc1Addr, acc2Addr, dummyAddr} + + trie.ClearGlobalCache() + + var res []byte + for _, addr := range acc { + if bc != nil { + header := bc.GetHeaderByHash(bhash) + st, err := state.New(header.Root, db) + if err == nil { + bal := st.GetBalance(addr) + rlp, _ := rlp.EncodeToBytes(bal) + res = append(res, rlp...) + } + } else { + header := lc.GetHeaderByHash(bhash) + st := light.NewLightState(light.StateTrieID(header), lc.Odr()) + bal, err := st.GetBalance(ctx, addr) + if err == nil { + rlp, _ := rlp.EncodeToBytes(bal) + res = append(res, rlp...) + } + } + } + + return res +} + +func TestOdrContractCallLes1(t *testing.T) { testOdr(t, 1, 2, odrContractCall) } + +// fullcallmsg is the message type used for call transations. +type fullcallmsg struct { + from *state.StateObject + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte +} + +// accessor boilerplate to implement core.Message +func (m fullcallmsg) From() (common.Address, error) { return m.from.Address(), nil } +func (m fullcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil } +func (m fullcallmsg) Nonce() uint64 { return m.from.Nonce() } +func (m fullcallmsg) To() *common.Address { return m.to } +func (m fullcallmsg) GasPrice() *big.Int { return m.gasPrice } +func (m fullcallmsg) Gas() *big.Int { return m.gas } +func (m fullcallmsg) Value() *big.Int { return m.value } +func (m fullcallmsg) Data() []byte { return m.data } + +// callmsg is the message type used for call transations. +type lightcallmsg struct { + from *light.StateObject + to *common.Address + gas, gasPrice *big.Int + value *big.Int + data []byte +} + +// accessor boilerplate to implement core.Message +func (m lightcallmsg) From() (common.Address, error) { return m.from.Address(), nil } +func (m lightcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil } +func (m lightcallmsg) Nonce() uint64 { return m.from.Nonce() } +func (m lightcallmsg) To() *common.Address { return m.to } +func (m lightcallmsg) GasPrice() *big.Int { return m.gasPrice } +func (m lightcallmsg) Gas() *big.Int { return m.gas } +func (m lightcallmsg) Value() *big.Int { return m.value } +func (m lightcallmsg) Data() []byte { return m.data } + +func odrContractCall(ctx context.Context, db ethdb.Database, config *core.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte { + data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000") + + var res []byte + for i := 0; i < 3; i++ { + data[35] = byte(i) + if bc != nil { + header := bc.GetHeaderByHash(bhash) + statedb, err := state.New(header.Root, db) + if err == nil { + from := statedb.GetOrNewStateObject(testBankAddress) + from.SetBalance(common.MaxBig) + + msg := fullcallmsg{ + from: from, + gas: big.NewInt(100000), + gasPrice: big.NewInt(0), + value: big.NewInt(0), + data: data, + to: &testContractAddr, + } + + vmenv := core.NewEnv(statedb, config, bc, msg, header, config.VmConfig) + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, _, _ := core.ApplyMessage(vmenv, msg, gp) + res = append(res, ret...) + } + } else { + header := lc.GetHeaderByHash(bhash) + state := light.NewLightState(light.StateTrieID(header), lc.Odr()) + from, err := state.GetOrNewStateObject(ctx, testBankAddress) + if err == nil { + from.SetBalance(common.MaxBig) + + msg := lightcallmsg{ + from: from, + gas: big.NewInt(100000), + gasPrice: big.NewInt(0), + value: big.NewInt(0), + data: data, + to: &testContractAddr, + } + + vmenv := light.NewEnv(ctx, state, config, lc, msg, header, config.VmConfig) + gp := new(core.GasPool).AddGas(common.MaxBig) + ret, _, _ := core.ApplyMessage(vmenv, msg, gp) + if vmenv.Error() == nil { + res = append(res, ret...) + } + } + } + } + return res +} + +func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { + // Assemble the test environment + pm, db, odr := newTestProtocolManagerMust(t, false, 4, testChainGen) + lpm, ldb, odr := newTestProtocolManagerMust(t, true, 0, nil) + _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm) + select { + case <-time.After(time.Millisecond * 100): + case err := <-err1: + t.Fatalf("peer 1 handshake error: %v", err) + case err := <-err2: + t.Fatalf("peer 1 handshake error: %v", err) + } + + lpm.synchronise(lpeer, true) + + test := func(expFail uint64) { + for i := uint64(0); i <= pm.blockchain.CurrentHeader().GetNumberU64(); i++ { + bhash := core.GetCanonicalHash(db, i) + b1 := fn(light.NoOdr, db, pm.chainConfig, pm.blockchain.(*core.BlockChain), nil, bhash) + ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond) + b2 := fn(ctx, ldb, lpm.chainConfig, nil, lpm.blockchain.(*light.LightChain), bhash) + eq := bytes.Equal(b1, b2) + exp := i < expFail + if exp && !eq { + t.Errorf("odr mismatch") + } + if !exp && eq { + t.Errorf("unexpected odr match") + } + } + } + + // temporarily remove peer to test odr fails + odr.UnregisterPeer(lpeer) + // expect retrievals to fail (except genesis block) without a les peer + test(expFail) + odr.RegisterPeer(lpeer) + // expect all retrievals to pass + test(5) + odr.UnregisterPeer(lpeer) + // still expect all retrievals to pass, now data should be cached locally + test(5) +} diff --git a/les/peer.go b/les/peer.go new file mode 100644 index 0000000000..2c356cdf99 --- /dev/null +++ b/les/peer.go @@ -0,0 +1,482 @@ +// 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 . + +// Package les implements the Light Ethereum Subprotocol. +package les + +import ( + "errors" + "fmt" + "math/big" + "sync" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/les/flowcontrol" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rlp" + "gopkg.in/fatih/set.v0" +) + +var ( + errClosed = errors.New("peer set is closed") + errAlreadyRegistered = errors.New("peer is already registered") + errNotRegistered = errors.New("peer is not registered") +) + +const ( + maxKnownBlocks = 1024 // Maximum block hashes to keep in the known list (prevent DOS) +) + +type peer struct { + *p2p.Peer + + rw p2p.MsgReadWriter + + version int // Protocol version negotiated + network int // Network ID being on + + id string + + head common.Hash + td *big.Int + lock sync.RWMutex + + knownBlocks *set.Set // Set of block hashes known to be known by this peer + + fcClient *flowcontrol.ClientNode // nil if the peer is server only + fcServer *flowcontrol.ServerNode // nil if the peer is client only + fcCosts requestCostTable +} + +func newPeer(version, network int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer { + id := p.ID() + + return &peer{ + Peer: p, + rw: rw, + version: version, + network: network, + id: fmt.Sprintf("%x", id[:8]), + knownBlocks: set.New(), + } +} + +// Info gathers and returns a collection of metadata known about a peer. +func (p *peer) Info() *eth.PeerInfo { + return ð.PeerInfo{ + Version: p.version, + Difficulty: p.Td(), + Head: fmt.Sprintf("%x", p.Head()), + } +} + +// Head retrieves a copy of the current head (most recent) hash of the peer. +func (p *peer) Head() (hash common.Hash) { + p.lock.RLock() + defer p.lock.RUnlock() + + copy(hash[:], p.head[:]) + return hash +} + +// Td retrieves the current total difficulty of a peer. +func (p *peer) Td() *big.Int { + p.lock.RLock() + defer p.lock.RUnlock() + + return new(big.Int).Set(p.td) +} + +func sendRequest(w p2p.MsgWriter, msgcode, reqID, cost uint64, data interface{}) error { + type req struct { + ReqID uint64 + Data interface{} + } + return p2p.Send(w, msgcode, req{reqID, data}) +} + +func sendResponse(w p2p.MsgWriter, msgcode, reqID, bv uint64, data interface{}) error { + type resp struct { + ReqID, BV uint64 + Data interface{} + } + return p2p.Send(w, msgcode, resp{reqID, bv, data}) +} + +func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 { + return p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount) +} + +// SendBlockHeaders sends a batch of block headers to the remote peer. +func (p *peer) SendBlockHeaders(reqID, bv uint64, headers []*types.Header) error { + return sendResponse(p.rw, BlockHeadersMsg, reqID, bv, headers) +} + +// SendBlockBodiesRLP sends a batch of block contents to the remote peer from +// an already RLP encoded format. +func (p *peer) SendBlockBodiesRLP(reqID, bv uint64, bodies []rlp.RawValue) error { + return sendResponse(p.rw, BlockBodiesMsg, reqID, bv, bodies) +} + +// SendCodeRLP sends a batch of arbitrary internal data, corresponding to the +// hashes requested. +func (p *peer) SendCode(reqID, bv uint64, data [][]byte) error { + return sendResponse(p.rw, CodeMsg, reqID, bv, data) +} + +// SendReceiptsRLP sends a batch of transaction receipts, corresponding to the +// ones requested from an already RLP encoded format. +func (p *peer) SendReceiptsRLP(reqID, bv uint64, receipts []rlp.RawValue) error { + return sendResponse(p.rw, ReceiptsMsg, reqID, bv, receipts) +} + +// SendProofs sends a batch of merkle proofs, corresponding to the ones requested. +func (p *peer) SendProofs(reqID, bv uint64, proofs proofsData) error { + return sendResponse(p.rw, ProofsMsg, reqID, bv, proofs) +} + +// 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(reqID, cost uint64, origin common.Hash, amount int, skip int, reverse bool) error { + glog.V(logger.Debug).Infof("%v fetching %d headers from %x, skipping %d (reverse = %v)", p, amount, origin[:4], skip, reverse) + return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &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(reqID, cost, origin uint64, amount int, skip int, reverse bool) error { + glog.V(logger.Debug).Infof("%v fetching %d headers from #%d, skipping %d (reverse = %v)", p, amount, origin, skip, reverse) + return sendRequest(p.rw, GetBlockHeadersMsg, reqID, cost, &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(reqID, cost uint64, hashes []common.Hash) error { + glog.V(logger.Debug).Infof("%v fetching %d block bodies", p, len(hashes)) + return sendRequest(p.rw, GetBlockBodiesMsg, reqID, cost, hashes) +} + +// RequestCode fetches a batch of arbitrary data from a node's known state +// data, corresponding to the specified hashes. +func (p *peer) RequestCode(reqID, cost uint64, reqs []*CodeReq) error { + glog.V(logger.Debug).Infof("%v fetching %v state data", p, len(reqs)) + return sendRequest(p.rw, GetCodeMsg, reqID, cost, reqs) +} + +// RequestReceipts fetches a batch of transaction receipts from a remote node. +func (p *peer) RequestReceipts(reqID, cost uint64, hashes []common.Hash) error { + glog.V(logger.Debug).Infof("%v fetching %v receipts", p, len(hashes)) + return sendRequest(p.rw, GetReceiptsMsg, reqID, cost, hashes) +} + +// RequestProofs fetches a batch of merkle proofs from a remote node. +func (p *peer) RequestProofs(reqID, cost uint64, reqs []*ProofReq) error { + glog.V(logger.Debug).Infof("%v fetching %v proofs", p, len(reqs)) + return sendRequest(p.rw, GetProofsMsg, reqID, cost, reqs) +} + +func (p *peer) SendTxs(cost uint64, txs types.Transactions) error { + glog.V(logger.Debug).Infof("%v relaying %v txs", p, len(txs)) + p.fcServer.SendRequest(0, cost) + return p2p.Send(p.rw, SendTxMsg, txs) +} + +type keyValueEntry struct{ + Key string + Value rlp.RawValue +} +type keyValueList []keyValueEntry +type keyValueMap map[string]rlp.RawValue + +func (l keyValueList) add(key string, val interface{}) keyValueList { + var entry keyValueEntry + entry.Key = key + if val == nil { + val = uint64(0) + } + enc, err := rlp.EncodeToBytes(val) + if err == nil { + entry.Value = enc + } + return append(l, entry) +} + +func (l keyValueList) decode() keyValueMap { + m := make(keyValueMap) + for _, entry := range l { + m[entry.Key] = entry.Value + } + return m +} + +func (m keyValueMap) get(key string, val interface{}) error { + enc, ok := m[key] + if !ok { + return errResp(ErrHandshakeMissingKey, "%s", key) + } + if val == nil { + return nil + } + return rlp.DecodeBytes(enc, val) +} + +func (p *peer) sendReceiveHandshake(sendList keyValueList) (keyValueList, error) { + // Send out own handshake in a new thread + errc := make(chan error, 1) + go func() { + errc <- p2p.Send(p.rw, StatusMsg, sendList) + }() + // In the mean time retrieve the remote status message + msg, err := p.rw.ReadMsg() + if err != nil { + return nil, err + } + if msg.Code != StatusMsg { + return nil, errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg) + } + if msg.Size > ProtocolMaxMsgSize { + return nil, errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) + } + // Decode the handshake + var recvList keyValueList + if err := msg.Decode(&recvList); err != nil { + return nil, errResp(ErrDecode, "msg %v: %v", msg, err) + } + if err := <-errc; err != nil { + return nil, err + } + return recvList, nil +} + +// Handshake executes the les protocol handshake, negotiating version number, +// network IDs, difficulties, head and genesis blocks. +func (p *peer) Handshake(td *big.Int, head common.Hash, genesis common.Hash, server *LesServer) error { + p.lock.Lock() + defer p.lock.Unlock() + + var send keyValueList + send = send.add("protocolVersion", uint64(p.version)) + send = send.add("networkId", uint64(p.network)) + send = send.add("td", td) + send = send.add("bestHash", head) + send = send.add("genesisHash", genesis) + if server != nil { + send = send.add("serveHeaders", nil) + send = send.add("serveChainSince", uint64(0)) + send = send.add("serveStateSince", uint64(0)) + send = send.add("txRelay", nil) + send = send.add("flowControl/BL", server.defParams.BufLimit) + send = send.add("flowControl/MRR", server.defParams.MinRecharge) + list := server.fcCostStats.getCurrentList() + send = send.add("flowControl/MRC", list) + p.fcCosts = list.decode() + } + recvList, err := p.sendReceiveHandshake(send) + if err != nil { + return err + } + recv := recvList.decode() + + var rGenesis, rBest common.Hash + var rVersion, rNetwork uint64 + var rTd *big.Int + + if err := recv.get("protocolVersion", &rVersion); err != nil { + return err + } + if err := recv.get("networkId", &rNetwork); err != nil { + return err + } + if err := recv.get("td", &rTd); err != nil { + return err + } + if err := recv.get("bestHash", &rBest); err != nil { + return err + } + if err := recv.get("genesisHash", &rGenesis); err != nil { + return err + } + + if rGenesis != genesis { + return errResp(ErrGenesisBlockMismatch, "%x (!= %x)", rGenesis, genesis) + } + if int(rNetwork) != p.network { + return errResp(ErrNetworkIdMismatch, "%d (!= %d)", rNetwork, p.network) + } + if int(rVersion) != p.version { + return errResp(ErrProtocolVersionMismatch, "%d (!= %d)", rVersion, p.version) + } + if server != nil { + if recv.get("serveStateSince", nil) == nil { + return errResp(ErrUselessPeer, "wanted client, got server") + } + p.fcClient = flowcontrol.NewClientNode(server.fcManager, server.defParams) + } else { + if recv.get("serveChainSince", nil) != nil { + return errResp(ErrUselessPeer, "peer cannot serve chain") + } + if recv.get("serveStateSince", nil) != nil { + return errResp(ErrUselessPeer, "peer cannot serve state") + } + if recv.get("txRelay", nil) != nil { + return errResp(ErrUselessPeer, "peer cannot relay transactions") + } + params := &flowcontrol.ServerParams{} + if err := recv.get("flowControl/BL", ¶ms.BufLimit); err != nil { + return err + } + if err := recv.get("flowControl/MRR", ¶ms.MinRecharge); err != nil { + return err + } + var MRC RequestCostList + if err := recv.get("flowControl/MRC", &MRC); err != nil { + return err + } + p.fcServer = flowcontrol.NewServerNode(params) + p.fcCosts = MRC.decode() + } + // Configure the remote peer, and sanity check out handshake too + p.td, p.head = rTd, rBest + return nil +} + +// String implements fmt.Stringer. +func (p *peer) String() string { + return fmt.Sprintf("Peer %s [%s]", p.id, + fmt.Sprintf("les/%d", p.version), + ) +} + +// peerSet represents the collection of active peers currently participating in +// the Light 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. +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 + 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() + + if _, ok := ps.peers[id]; !ok { + return errNotRegistered + } + delete(ps.peers, id) + return nil +} + +// AllPeerIDs returns a list of all registered peer IDs +func (ps *peerSet) AllPeerIDs() []string { + ps.lock.RLock() + defer ps.lock.RUnlock() + + res := make([]string, len(ps.peers)) + idx := 0 + for id, _ := range ps.peers { + res[idx] = id + idx++ + } + return res +} + +// 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) +} + +// 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.Td(); bestPeer == nil || td.Cmp(bestTd) > 0 { + bestPeer, bestTd = p, td + } + } + return bestPeer +} + +// AllPeers returns all peers in a list +func (ps *peerSet) AllPeers() []*peer { + ps.lock.RLock() + defer ps.lock.RUnlock() + + list := make([]*peer, len(ps.peers)) + i := 0 + for _, peer := range ps.peers { + list[i] = peer + i++ + } + return list +} + +// 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/les/protocol.go b/les/protocol.go new file mode 100644 index 0000000000..dd63bd3902 --- /dev/null +++ b/les/protocol.go @@ -0,0 +1,196 @@ +// 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 . + +// Package les implements the Light Ethereum Subprotocol. +package les + +import ( + "fmt" + "io" + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/rlp" +) + +// Constants to match up protocol versions and messages +const ( + lpv1 = 1 +) + +// Supported versions of the les protocol (first is primary). +var ProtocolVersions = []uint{lpv1} + +// Number of implemented message corresponding to different protocol versions. +var ProtocolLengths = []uint64{15} + +const ( + NetworkId = 1 + ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message +) + +// les protocol message codes +const ( + // Protocol messages belonging to LPV1 + StatusMsg = 0x00 + NewBlockHashesMsg = 0x01 + GetBlockHeadersMsg = 0x02 + BlockHeadersMsg = 0x03 + GetBlockBodiesMsg = 0x04 + BlockBodiesMsg = 0x05 + GetReceiptsMsg = 0x06 + ReceiptsMsg = 0x07 + GetProofsMsg = 0x08 + ProofsMsg = 0x09 + GetCodeMsg = 0x0a + CodeMsg = 0x0b + SendTxMsg = 0x0c + GetTxHashesMsg = 0x0d + TxHashesMsg = 0x0e +) + +type errCode int + +const ( + ErrMsgTooLarge = iota + ErrDecode + ErrInvalidMsgCode + ErrProtocolVersionMismatch + ErrNetworkIdMismatch + ErrGenesisBlockMismatch + ErrNoStatusMsg + ErrExtraStatusMsg + ErrSuspendedPeer + ErrUselessPeer + ErrRequestRejected + ErrUnexpectedResponse + ErrInvalidResponse + ErrTooManyTimeouts + ErrHandshakeMissingKey +) + +func (e errCode) String() string { + return errorToString[int(e)] +} + +// XXX change once legacy code is out +var errorToString = map[int]string{ + ErrMsgTooLarge: "Message too long", + ErrDecode: "Invalid message", + ErrInvalidMsgCode: "Invalid message code", + ErrProtocolVersionMismatch: "Protocol version mismatch", + ErrNetworkIdMismatch: "NetworkId mismatch", + ErrGenesisBlockMismatch: "Genesis block mismatch", + ErrNoStatusMsg: "No status message", + ErrExtraStatusMsg: "Extra status message", + ErrSuspendedPeer: "Suspended peer", + ErrRequestRejected: "Request rejected", + ErrUnexpectedResponse: "Unexpected response", + ErrInvalidResponse: "Invalid response", + ErrTooManyTimeouts: "Too many request timeouts", + ErrHandshakeMissingKey: "Key missing from handshake message", +} + +type chainManager interface { + GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash) + GetBlock(hash common.Hash) (block *types.Block) + Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) +} + +// statusData is the network packet for the status message. +type statusData struct { + ProtocolVersion uint32 + NetworkId uint32 + TD *big.Int + CurrentBlock common.Hash + GenesisBlock common.Hash + History uint64 + BL, MRR uint64 + MRC RequestCostList +} + +// newBlockHashesData is the network packet for the block announcements. +type newBlockHashesData []struct { + Hash common.Hash // Hash of one particular block being announced + Number uint64 // Number of one particular block being announced +} + +// getBlockHashesData is the network packet for the hash based hash retrieval. +type getBlockHashesData struct { + Hash common.Hash + Amount uint64 +} + +// getBlockHeadersData represents a block header query. +type getBlockHeadersData struct { + Origin hashOrNumber // Block from which to retrieve headers + Amount uint64 // Maximum number of headers to retrieve + Skip uint64 // Blocks to skip between consecutive headers + Reverse bool // Query direction (false = rising towards latest, true = falling towards genesis) +} + +// hashOrNumber is a combined field for specifying an origin block. +type hashOrNumber struct { + Hash common.Hash // Block hash from which to retrieve headers (excludes Number) + Number uint64 // Block hash from which to retrieve headers (excludes Hash) +} + +// EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the +// two contained union fields. +func (hn *hashOrNumber) EncodeRLP(w io.Writer) error { + if hn.Hash == (common.Hash{}) { + return rlp.Encode(w, hn.Number) + } + if hn.Number != 0 { + return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number) + } + return rlp.Encode(w, hn.Hash) +} + +// DecodeRLP is a specialized decoder for hashOrNumber to decode the contents +// into either a block hash or a block number. +func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error { + _, size, _ := s.Kind() + origin, err := s.Raw() + if err == nil { + switch { + case size == 32: + err = rlp.DecodeBytes(origin, &hn.Hash) + case size <= 8: + err = rlp.DecodeBytes(origin, &hn.Number) + default: + err = fmt.Errorf("invalid input size %d for origin", size) + } + } + return err +} + +// newBlockData is the network packet for the block propagation message. +type newBlockData struct { + Block *types.Block + TD *big.Int +} + +// blockBodiesData is the network packet for block content distribution. +type blockBodiesData []*types.Body + +// CodeData is the network response packet for a node data retrieval. +type CodeData []struct { + Value []byte +} + +type proofsData [][]rlp.RawValue diff --git a/les/request_test.go b/les/request_test.go new file mode 100644 index 0000000000..320580c6fc --- /dev/null +++ b/les/request_test.go @@ -0,0 +1,94 @@ +package les + +import ( + "testing" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/light" + "golang.org/x/net/context" +) + +var testBankSecureTrieKey = secAddr(testBankAddress) + +func secAddr(addr common.Address) []byte { + return crypto.Keccak256(addr[:]) +} + +type accessTestFn func(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest + +func TestBlockAccessLes1(t *testing.T) { testAccess(t, 1, tfBlockAccess) } + +func tfBlockAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest { + return &light.BlockRequest{Hash: bhash, Number: number} +} + +func TestReceiptsAccessLes1(t *testing.T) { testAccess(t, 1, tfReceiptsAccess) } + +func tfReceiptsAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest { + return &light.ReceiptsRequest{Hash: bhash, Number: number} +} + +func TestTrieEntryAccessLes1(t *testing.T) { testAccess(t, 1, tfTrieEntryAccess) } + +func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest { + return &light.TrieRequest{Id: light.StateTrieID(core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash))), Key: testBankSecureTrieKey} +} + +func TestCodeAccessLes1(t *testing.T) { testAccess(t, 1, tfCodeAccess) } + +func tfCodeAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest { + header := core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash)) + if header.GetNumberU64() < testContractDeployed { + return nil + } + sti := light.StateTrieID(header) + ci := light.StorageTrieID(sti, testContractAddr, common.Hash{}) + return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)} +} + +func testAccess(t *testing.T, protocol int, fn accessTestFn) { + // Assemble the test environment + pm, db, _ := newTestProtocolManagerMust(t, false, 4, testChainGen) + lpm, ldb, odr := newTestProtocolManagerMust(t, true, 0, nil) + _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm) + select { + case <-time.After(time.Millisecond * 100): + case err := <-err1: + t.Fatalf("peer 1 handshake error: %v", err) + case err := <-err2: + t.Fatalf("peer 1 handshake error: %v", err) + } + + lpm.synchronise(lpeer, true) + + test := func(expFail uint64) { + for i := uint64(0); i <= pm.blockchain.CurrentHeader().GetNumberU64(); i++ { + bhash := core.GetCanonicalHash(db, i) + if req := fn(ldb, bhash, i); req != nil { + ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond) + err := odr.Retrieve(ctx, req) + got := err == nil + exp := i < expFail + if exp && !got { + t.Errorf("object retrieval failed") + } + if !exp && got { + t.Errorf("unexpected object retrieval success") + } + } + } + } + + // temporarily remove peer to test odr fails + odr.UnregisterPeer(lpeer) + // expect retrievals to fail (except genesis block) without a les peer + test(0) + odr.RegisterPeer(lpeer) + // expect all retrievals to pass + test(5) + odr.UnregisterPeer(lpeer) +} diff --git a/les/server.go b/les/server.go new file mode 100644 index 0000000000..53989269cb --- /dev/null +++ b/les/server.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 . + +// Package les implements the Light Ethereum Subprotocol. +package les + +import ( + "sync" + + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/les/flowcontrol" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rlp" +) + +type LesServer struct { + protocolManager *ProtocolManager + fcManager *flowcontrol.ClientManager // nil if our node is client only + fcCostStats *requestCostStats + defParams *flowcontrol.ServerParams +} + +func NewLesServer(eth *eth.FullNodeService, config *eth.Config) (*LesServer, error) { + pm, err := NewProtocolManager(config.ChainConfig, false, config.NetworkId, eth.EventMux(), eth.Pow(), eth.BlockChain(), eth.TxPool(), eth.ChainDb(), nil, nil) + if err != nil { + return nil, err + } + srv := &LesServer{protocolManager: pm} + pm.server = srv + + srv.defParams = &flowcontrol.ServerParams{ + BufLimit: 300000000, + MinRecharge: 50000, + } + srv.fcManager = flowcontrol.NewClientManager(uint64(config.LightServ), 10, 1000000000) + srv.fcCostStats = newCostStats(eth.ChainDb()) + return srv, nil +} + +func (s *LesServer) Protocols() []p2p.Protocol { + return s.protocolManager.SubProtocols +} + +func (s *LesServer) Start() { + s.protocolManager.Start() +} + +func (s *LesServer) Stop() { + s.fcCostStats.store() + s.fcManager.Stop() + go func() { + <-s.protocolManager.noMorePeers + }() + s.protocolManager.Stop() +} + +type requestCosts struct { + baseCost, reqCost uint64 +} + +type requestCostTable map[uint64]*requestCosts + +type RequestCostList []struct { + MsgCode, BaseCost, ReqCost uint64 +} + +func (list RequestCostList) decode() requestCostTable { + table := make(requestCostTable) + for _, e := range list { + table[e.MsgCode] = &requestCosts{ + baseCost: e.BaseCost, + reqCost: e.ReqCost, + } + } + return table +} + +func (table requestCostTable) encode() RequestCostList { + list := make(RequestCostList, len(table)) + for idx, code := range reqList { + list[idx].MsgCode = code + list[idx].BaseCost = table[code].baseCost + list[idx].ReqCost = table[code].reqCost + } + return list +} + +type requestCostStats struct { + lock sync.RWMutex + db ethdb.Database + avg requestCostTable + baseCost uint64 +} + +var rcStatsKey = []byte("requestCostStats") + +func newCostStats(db ethdb.Database) *requestCostStats { + table := make(requestCostTable) + for _, code := range reqList { + table[code] = &requestCosts{0, 100000} + } + + /* if db != nil { + var cl RequestCostList + data, err := db.Get(rcStatsKey) + if err == nil { + err = rlp.DecodeBytes(data, &cl) + } + if err == nil { + t := cl.decode() + for code, entry := range t { + table[code] = entry + } + } + }*/ + + return &requestCostStats{ + db: db, + avg: table, + baseCost: 100000, + } +} + +func (s *requestCostStats) store() { + s.lock.Lock() + defer s.lock.Unlock() + + list := s.avg.encode() + if data, err := rlp.EncodeToBytes(list); err == nil { + s.db.Put(rcStatsKey, data) + } +} + +func (s *requestCostStats) getCurrentList() RequestCostList { + s.lock.Lock() + defer s.lock.Unlock() + + list := make(RequestCostList, len(s.avg)) + for idx, code := range reqList { + list[idx].MsgCode = code + list[idx].BaseCost = s.baseCost + list[idx].ReqCost = s.avg[code].reqCost * 2 + } + return list +} + +func (s *requestCostStats) update(msgCode, reqCnt, cost uint64) { + s.lock.Lock() + defer s.lock.Unlock() + + c, ok := s.avg[msgCode] + if !ok || reqCnt == 0 { + return + } + cost = cost / reqCnt + if cost > c.reqCost { + c.reqCost += (cost - c.reqCost) / 10 + } else { + c.reqCost -= (c.reqCost - cost) / 100 + } +} diff --git a/les/sync.go b/les/sync.go new file mode 100644 index 0000000000..e540d4289f --- /dev/null +++ b/les/sync.go @@ -0,0 +1,78 @@ +// 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 . + +package les + +import ( + "time" + + "github.com/ethereum/go-ethereum/eth/downloader" +) + +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 +) + +// 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.Tick(forceSyncCycle) + 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(), false) + + case <-forceSync: + // Force a sync even if not enough peers are present + go pm.synchronise(pm.peers.BestPeer(), false) + + case <-pm.noMorePeers: + return + } + } +} + +// synchronise tries to sync up our local block chain with a remote peer. +func (pm *ProtocolManager) synchronise(peer *peer, exit bool) { + // Short circuit if no peers are available + if peer == nil { + return + } + // Make sure the peer's TD is higher than our own. + td := pm.blockchain.GetTdByHash(pm.blockchain.LastBlockHash()) + if peer.Td().Cmp(td) > 0 { + for { + if pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) != nil { + return + } + if exit { + return + } + time.Sleep(time.Second * 5) + } + } +} diff --git a/les/txrelay.go b/les/txrelay.go new file mode 100644 index 0000000000..366e69a62c --- /dev/null +++ b/les/txrelay.go @@ -0,0 +1,156 @@ +// 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 . +package les + +import ( + "sync" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +type ltrInfo struct { + tx *types.Transaction + sentTo map[*peer]struct{} +} + +type LesTxRelay struct { + txSent map[common.Hash]*ltrInfo + txPending map[common.Hash]struct{} + ps *peerSet + peerList []*peer + peerStartPos int + lock sync.RWMutex +} + +func NewLesTxRelay() *LesTxRelay { + return &LesTxRelay{ + txSent: make(map[common.Hash]*ltrInfo), + txPending: make(map[common.Hash]struct{}), + ps: newPeerSet(), + } +} + +func (self *LesTxRelay) addPeer(p *peer) { + self.lock.Lock() + defer self.lock.Unlock() + + self.ps.Register(p) + self.peerList = self.ps.AllPeers() +} + +func (self *LesTxRelay) removePeer(id string) { + self.lock.Lock() + defer self.lock.Unlock() + + self.ps.Unregister(id) + self.peerList = self.ps.AllPeers() +} + +// send sends a list of transactions to at most a given number of peers at +// once, never resending any particular transaction to the same peer twice +func (self *LesTxRelay) send(txs types.Transactions, count int) { + sendTo := make(map[*peer]types.Transactions) + + self.peerStartPos++ // rotate the starting position of the peer list + if self.peerStartPos >= len(self.peerList) { + self.peerStartPos = 0 + } + + for _, tx := range txs { + hash := tx.Hash() + ltr, ok := self.txSent[hash] + if !ok { + ltr = <rInfo{ + tx: tx, + sentTo: make(map[*peer]struct{}), + } + self.txSent[hash] = ltr + self.txPending[hash] = struct{}{} + } + + if len(self.peerList) > 0 { + cnt := count + pos := self.peerStartPos + for { + peer := self.peerList[pos] + if _, ok := ltr.sentTo[peer]; !ok { + sendTo[peer] = append(sendTo[peer], tx) + ltr.sentTo[peer] = struct{}{} + cnt-- + } + if cnt == 0 { + break // sent it to the desired number of peers + } + pos++ + if pos == len(self.peerList) { + pos = 0 + } + if pos == self.peerStartPos { + break // tried all available peers + } + } + } + } + + for peer, list := range sendTo { + cost := peer.GetRequestCost(SendTxMsg, len(list)) + go func() { + peer.fcServer.SendRequest(0, cost) + peer.SendTxs(cost, list) + }() + } +} + +func (self *LesTxRelay) Send(txs types.Transactions) { + self.lock.Lock() + defer self.lock.Unlock() + + self.send(txs, 3) +} + +func (self *LesTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) { + self.lock.Lock() + defer self.lock.Unlock() + + for _, hash := range mined { + delete(self.txPending, hash) + } + + for _, hash := range rollback { + self.txPending[hash] = struct{}{} + } + + if len(self.txPending) > 0 { + txs := make(types.Transactions, len(self.txPending)) + i := 0 + for hash, _ := range self.txPending { + txs[i] = self.txSent[hash].tx + i++ + } + self.send(txs, 1) + } +} + +func (self *LesTxRelay) Discard(hashes []common.Hash) { + self.lock.Lock() + defer self.lock.Unlock() + + for _, hash := range hashes { + delete(self.txSent, hash) + delete(self.txPending, hash) + } +} diff --git a/light/lightchain.go b/light/lightchain.go index 8f79597590..a70cb4493a 100644 --- a/light/lightchain.go +++ b/light/lightchain.go @@ -70,7 +70,7 @@ type LightChain struct { // NewLightChain returns a fully initialised light chain using information // available in the database. It initialises the default Ethereum header // validator. -func NewLightChain(odr OdrBackend, pow pow.PoW, mux *event.TypeMux) (*LightChain, error) { +func NewLightChain(odr OdrBackend, config *core.ChainConfig, pow pow.PoW, mux *event.TypeMux) (*LightChain, error) { bodyCache, _ := lru.New(bodyCacheLimit) bodyRLPCache, _ := lru.New(bodyCacheLimit) blockCache, _ := lru.New(blockCacheLimit) @@ -87,8 +87,8 @@ func NewLightChain(odr OdrBackend, pow pow.PoW, mux *event.TypeMux) (*LightChain } var err error - bc.hc, err = core.NewHeaderChain(odr.Database(), bc.Validator, bc.getProcInterrupt) - bc.SetValidator(core.NewHeaderValidator(bc.hc, pow)) + bc.hc, err = core.NewHeaderChain(odr.Database(), config, bc.Validator, bc.getProcInterrupt) + bc.SetValidator(core.NewHeaderValidator(config, bc.hc, pow)) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func NewLightChain(odr OdrBackend, pow pow.PoW, mux *event.TypeMux) (*LightChain } // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain for hash, _ := range core.BadHashes { - if header := bc.GetHeader(hash); header != nil { + if header := bc.GetHeaderByHash(hash); header != nil { glog.V(logger.Error).Infof("Found bad hash, rewinding chain to block #%d [%x…]", header.Number, header.ParentHash[:4]) bc.SetHead(header.Number.Uint64() - 1) glog.V(logger.Error).Infoln("Chain rewind was successful, resuming normal operation") @@ -131,13 +131,14 @@ func (self *LightChain) loadLastState() error { // Corrupt or empty database, init from scratch self.Reset() } else { - if header := self.GetHeader(head); header != nil { + if header := self.GetHeaderByHash(head); header != nil { self.hc.SetCurrentHeader(header) } } // Issue a status log and return - headerTd := self.GetTd(self.hc.CurrentHeader().Hash()) + header := self.hc.CurrentHeader() + headerTd := self.GetTd(header.Hash(), header.Number.Uint64()) glog.V(logger.Info).Infof("Last header: #%d [%x…] TD=%v", self.hc.CurrentHeader().Number, self.hc.CurrentHeader().Hash().Bytes()[:4], headerTd) return nil @@ -149,7 +150,7 @@ func (bc *LightChain) SetHead(head uint64) { bc.mu.Lock() defer bc.mu.Unlock() - bc.hc.SetHead(head) + bc.hc.SetHead(head, nil) bc.loadLastState() } @@ -175,8 +176,9 @@ func (self *LightChain) Status() (td *big.Int, currentBlock common.Hash, genesis self.mu.RLock() defer self.mu.RUnlock() - hash := self.hc.CurrentHeader().Hash() - return self.GetTd(hash), hash, self.genesisBlock.Hash() + header := self.hc.CurrentHeader() + hash := header.Hash() + return self.GetTd(hash, header.Number.Uint64()), hash, self.genesisBlock.Hash() } // SetValidator sets the validator which is used to validate incoming headers. @@ -213,7 +215,7 @@ func (bc *LightChain) ResetWithGenesisBlock(genesis *types.Block) { defer bc.mu.Unlock() // Prepare the genesis block and reinitialise the chain - if err := core.WriteTd(bc.chainDb, genesis.Hash(), genesis.Difficulty()); err != nil { + if err := core.WriteTd(bc.chainDb, genesis.Hash(), genesis.NumberU64(), genesis.Difficulty()); err != nil { glog.Fatalf("failed to write genesis block TD: %v", err) } if err := core.WriteBlock(bc.chainDb, genesis); err != nil { @@ -239,7 +241,7 @@ func (self *LightChain) GetBody(ctx context.Context, hash common.Hash) (*types.B body := cached.(*types.Body) return body, nil } - body, err := GetBody(ctx, self.odr, hash) + body, err := GetBody(ctx, self.odr, hash, self.hc.GetBlockNumber(hash)) if err != nil { return nil, err } @@ -255,7 +257,7 @@ func (self *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.R if cached, ok := self.bodyRLPCache.Get(hash); ok { return cached.(rlp.RawValue), nil } - body, err := GetBodyRLP(ctx, self.odr, hash) + body, err := GetBodyRLP(ctx, self.odr, hash, self.hc.GetBlockNumber(hash)) if err != nil { return nil, err } @@ -267,18 +269,18 @@ func (self *LightChain) GetBodyRLP(ctx context.Context, hash common.Hash) (rlp.R // HasBlock checks if a block is fully present in the database or not, caching // it if present. func (bc *LightChain) HasBlock(hash common.Hash) bool { - blk, _ := bc.GetBlock(NoOdr, hash) + blk, _ := bc.GetBlockByHash(NoOdr, hash) return blk != nil } -// GetBlock retrieves a block from the database or ODR service by hash, +// GetBlock retrieves a block from the database or ODR service by hash and number, // caching it if found. -func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash) (*types.Block, error) { +func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash, number uint64) (*types.Block, error) { // Short circuit if the block's already in the cache, retrieve otherwise if block, ok := self.blockCache.Get(hash); ok { return block.(*types.Block), nil } - block, err := GetBlock(ctx, self.odr, hash) + block, err := GetBlock(ctx, self.odr, hash, number) if err != nil { return nil, err } @@ -287,6 +289,12 @@ func (self *LightChain) GetBlock(ctx context.Context, hash common.Hash) (*types. return block, nil } +// GetBlockByHash retrieves a block from the database or ODR service by hash, +// caching it if found. +func (self *LightChain) GetBlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { + return self.GetBlock(ctx, hash, self.hc.GetBlockNumber(hash)) +} + // GetBlockByNumber retrieves a block from the database or ODR service by // number, caching it (associated with its hash) if found. func (self *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*types.Block, error) { @@ -294,7 +302,7 @@ func (self *LightChain) GetBlockByNumber(ctx context.Context, number uint64) (*t if hash == (common.Hash{}) { return nil, nil } - return self.GetBlock(ctx, hash) + return self.GetBlock(ctx, hash, number) } // Stop stops the blockchain service. If any imports are currently in progress @@ -320,8 +328,8 @@ func (self *LightChain) Rollback(chain []common.Hash) { for i := len(chain) - 1; i >= 0; i-- { hash := chain[i] - if self.hc.CurrentHeader().Hash() == hash { - self.hc.SetCurrentHeader(self.GetHeader(self.hc.CurrentHeader().ParentHash)) + if head := self.hc.CurrentHeader(); head.Hash() == hash { + self.hc.SetCurrentHeader(self.GetHeader(head.ParentHash, head.Number.Uint64()-1)) } } } @@ -400,15 +408,27 @@ func (self *LightChain) CurrentHeader() *types.Header { } // GetTd retrieves a block's total difficulty in the canonical chain from the -// database by hash, caching it if found. -func (self *LightChain) GetTd(hash common.Hash) *big.Int { - return self.hc.GetTd(hash) +// database by hash and number, caching it if found. +func (self *LightChain) GetTd(hash common.Hash, number uint64) *big.Int { + return self.hc.GetTd(hash, number) } -// GetHeader retrieves a block header from the database by hash, caching it if +// GetTdByHash retrieves a block's total difficulty in the canonical chain from the +// database by hash, caching it if found. +func (self *LightChain) GetTdByHash(hash common.Hash) *big.Int { + return self.hc.GetTdByHash(hash) +} + +// GetHeader retrieves a block header from the database by hash and number, +// caching it if found. +func (self *LightChain) GetHeader(hash common.Hash, number uint64) *types.Header { + return self.hc.GetHeader(hash, number) +} + +// GetHeaderByHash retrieves a block header from the database by hash, caching it if // found. -func (self *LightChain) GetHeader(hash common.Hash) *types.Header { - return self.hc.GetHeader(hash) +func (self *LightChain) GetHeaderByHash(hash common.Hash) *types.Header { + return self.hc.GetHeaderByHash(hash) } // HasHeader checks if a block header is present in the database or not, caching diff --git a/light/lightchain_test.go b/light/lightchain_test.go index 5b9b7cdfc6..07294f0684 100644 --- a/light/lightchain_test.go +++ b/light/lightchain_test.go @@ -51,6 +51,10 @@ func makeHeaderChain(parent *types.Header, n int, db ethdb.Database, seed int) [ return headers } +func testChainConfig() *core.ChainConfig { + return &core.ChainConfig{HomesteadBlock: big.NewInt(0)} +} + // newCanonical creates a chain database, and injects a deterministic canonical // chain. Depending on the full flag, if creates either a full block chain or a // header only chain. @@ -62,7 +66,7 @@ func newCanonical(n int) (ethdb.Database, *LightChain, error) { // Initialize a fresh chain with only a genesis block genesis, _ := core.WriteTestNetGenesisBlock(db) - blockchain, _ := NewLightChain(&dummyOdr{db: db}, core.FakePow{}, evmux) + blockchain, _ := NewLightChain(&dummyOdr{db: db}, testChainConfig(), core.FakePow{}, evmux) // Create and inject the requested chain if n == 0 { return db, blockchain, nil @@ -85,7 +89,7 @@ func thePow() pow.PoW { func theLightChain(db ethdb.Database, t *testing.T) *LightChain { var eventMux event.TypeMux core.WriteTestNetGenesisBlock(db) - LightChain, err := NewLightChain(&dummyOdr{db: db}, thePow(), &eventMux) + LightChain, err := NewLightChain(&dummyOdr{db: db}, testChainConfig(), thePow(), &eventMux) if err != nil { t.Error("failed creating LightChain:", err) t.FailNow() @@ -120,11 +124,11 @@ func testFork(t *testing.T, LightChain *LightChain, i, n int, comparator func(td // Sanity check that the forked chain can be imported into the original var tdPre, tdPost *big.Int - tdPre = LightChain.GetTd(LightChain.CurrentHeader().Hash()) + tdPre = LightChain.GetTdByHash(LightChain.CurrentHeader().Hash()) if err := testHeaderChainImport(headerChainB, LightChain); err != nil { t.Fatalf("failed to import forked header chain: %v", err) } - tdPost = LightChain.GetTd(headerChainB[len(headerChainB)-1].Hash()) + tdPost = LightChain.GetTdByHash(headerChainB[len(headerChainB)-1].Hash()) // Compare the total difficulties of the chains comparator(tdPre, tdPost) } @@ -141,12 +145,12 @@ func printChain(bc *LightChain) { func testHeaderChainImport(chain []*types.Header, LightChain *LightChain) error { for _, header := range chain { // Try and validate the header - if err := LightChain.Validator().ValidateHeader(header, LightChain.GetHeader(header.ParentHash), false); err != nil { + if err := LightChain.Validator().ValidateHeader(header, LightChain.GetHeaderByHash(header.ParentHash), false); err != nil { return err } // Manually insert the header into the database, but don't reorganize (allows subsequent testing) LightChain.mu.Lock() - core.WriteTd(LightChain.chainDb, header.Hash(), new(big.Int).Add(header.Difficulty, LightChain.GetTd(header.ParentHash))) + core.WriteTd(LightChain.chainDb, header.Hash(), header.Number.Uint64(), new(big.Int).Add(header.Difficulty, LightChain.GetTdByHash(header.ParentHash))) core.WriteHeader(LightChain.chainDb, header) LightChain.mu.Unlock() } @@ -307,7 +311,7 @@ func chm(genesis *types.Block, db ethdb.Database) *LightChain { odr := &dummyOdr{db: db} var eventMux event.TypeMux bc := &LightChain{odr: odr, chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: core.FakePow{}} - bc.hc, _ = core.NewHeaderChain(db, bc.Validator, bc.getProcInterrupt) + bc.hc, _ = core.NewHeaderChain(db, testChainConfig(), bc.Validator, bc.getProcInterrupt) bc.bodyCache, _ = lru.New(100) bc.bodyRLPCache, _ = lru.New(100) bc.blockCache, _ = lru.New(100) @@ -347,7 +351,7 @@ func testReorg(t *testing.T, first, second []int, td int64) { } // Make sure the chain total difficulty is the correct one want := new(big.Int).Add(genesis.Difficulty(), big.NewInt(td)) - if have := bc.GetTd(bc.CurrentHeader().Hash()); have.Cmp(want) != 0 { + if have := bc.GetTdByHash(bc.CurrentHeader().Hash()); have.Cmp(want) != 0 { t.Errorf("total difficulty mismatch: have %v, want %v", have, want) } } @@ -389,7 +393,7 @@ func TestReorgBadHeaderHashes(t *testing.T) { core.BadHashes[headers[3].Hash()] = true defer func() { delete(core.BadHashes, headers[3].Hash()) }() // Create a new chain manager and check it rolled back the state - ncm, err := NewLightChain(&dummyOdr{db: db}, core.FakePow{}, new(event.TypeMux)) + ncm, err := NewLightChain(&dummyOdr{db: db}, testChainConfig(), core.FakePow{}, new(event.TypeMux)) if err != nil { t.Fatalf("failed to create new chain manager: %v", err) } diff --git a/light/odr.go b/light/odr.go index a61d2049f1..0549223b0b 100644 --- a/light/odr.go +++ b/light/odr.go @@ -45,17 +45,17 @@ type OdrRequest interface { // TrieID identifies a state or account storage trie type TrieID struct { - blockHash, root common.Hash - accKey []byte + BlockHash, Root common.Hash + AccKey []byte } // StateTrieID returns a TrieID for a state trie belonging to a certain block // header. func StateTrieID(header *types.Header) *TrieID { return &TrieID{ - blockHash: header.Hash(), - accKey: nil, - root: header.Root, + BlockHash: header.Hash(), + AccKey: nil, + Root: header.Root, } } @@ -64,9 +64,9 @@ func StateTrieID(header *types.Header) *TrieID { // checking Merkle proofs. func StorageTrieID(state *TrieID, addr common.Address, root common.Hash) *TrieID { return &TrieID{ - blockHash: state.blockHash, - accKey: crypto.Keccak256(addr[:]), - root: root, + BlockHash: state.BlockHash, + AccKey: crypto.Keccak256(addr[:]), + Root: root, } } @@ -111,22 +111,24 @@ func (req *CodeRequest) StoreResult(db ethdb.Database) { type BlockRequest struct { OdrRequest Hash common.Hash + Number uint64 Rlp []byte } // StoreResult stores the retrieved data in local database func (req *BlockRequest) StoreResult(db ethdb.Database) { - core.WriteBodyRLP(db, req.Hash, req.Rlp) + core.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp) } // ReceiptsRequest is the ODR request type for retrieving block bodies type ReceiptsRequest struct { OdrRequest Hash common.Hash + Number uint64 Receipts types.Receipts } // StoreResult stores the retrieved data in local database func (req *ReceiptsRequest) StoreResult(db ethdb.Database) { - core.WriteBlockReceipts(db, req.Hash, req.Receipts) + core.WriteBlockReceipts(db, req.Hash, req.Number, req.Receipts) } diff --git a/light/odr_test.go b/light/odr_test.go index d868801b5e..2add51281d 100644 --- a/light/odr_test.go +++ b/light/odr_test.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" @@ -52,11 +53,11 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error { } switch req := req.(type) { case *BlockRequest: - req.Rlp = core.GetBodyRLP(odr.sdb, req.Hash) + req.Rlp = core.GetBodyRLP(odr.sdb, req.Hash, core.GetBlockNumber(odr.sdb, req.Hash)) case *ReceiptsRequest: - req.Receipts = core.GetBlockReceipts(odr.sdb, req.Hash) + req.Receipts = core.GetBlockReceipts(odr.sdb, req.Hash, core.GetBlockNumber(odr.sdb, req.Hash)) case *TrieRequest: - t, _ := trie.New(req.Id.root, odr.sdb) + t, _ := trie.New(req.Id.Root, odr.sdb) req.Proof = t.Prove(req.Key) trie.ClearGlobalCache() case *CodeRequest: @@ -73,9 +74,9 @@ func TestOdrGetBlockLes1(t *testing.T) { testChainOdr(t, 1, 1, odrGetBlock) } func odrGetBlock(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { var block *types.Block if bc != nil { - block = bc.GetBlock(bhash) + block = bc.GetBlockByHash(bhash) } else { - block, _ = lc.GetBlock(ctx, bhash) + block, _ = lc.GetBlockByHash(ctx, bhash) } if block == nil { return nil @@ -89,9 +90,9 @@ func TestOdrGetReceiptsLes1(t *testing.T) { testChainOdr(t, 1, 1, odrGetReceipts func odrGetReceipts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc *LightChain, bhash common.Hash) []byte { var receipts types.Receipts if bc != nil { - receipts = core.GetBlockReceipts(db, bhash) + receipts = core.GetBlockReceipts(db, bhash, core.GetBlockNumber(db, bhash)) } else { - receipts, _ = GetBlockReceipts(ctx, lc.Odr(), bhash) + receipts, _ = GetBlockReceipts(ctx, lc.Odr(), bhash, core.GetBlockNumber(db, bhash)) } if receipts == nil { return nil @@ -111,7 +112,7 @@ func odrAccounts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc var res []byte for _, addr := range acc { if bc != nil { - header := bc.GetHeader(bhash) + header := bc.GetHeaderByHash(bhash) st, err := state.New(header.Root, db) if err == nil { bal := st.GetBalance(addr) @@ -119,7 +120,7 @@ func odrAccounts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, lc res = append(res, rlp...) } } else { - header := lc.GetHeader(bhash) + header := lc.GetHeaderByHash(bhash) st := NewLightState(StateTrieID(header), lc.Odr()) bal, err := st.GetBalance(ctx, addr) if err == nil { @@ -179,7 +180,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain for i := 0; i < 3; i++ { data[35] = byte(i) if bc != nil { - header := bc.GetHeader(bhash) + header := bc.GetHeaderByHash(bhash) statedb, err := state.New(header.Root, db) if err == nil { from := statedb.GetOrNewStateObject(testBankAddress) @@ -194,13 +195,13 @@ func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain to: &testContractAddr, } - vmenv := core.NewEnv(statedb, bc, msg, header) + vmenv := core.NewEnv(statedb, testChainConfig(), bc, msg, header, vm.Config{}) gp := new(core.GasPool).AddGas(common.MaxBig) ret, _, _ := core.ApplyMessage(vmenv, msg, gp) res = append(res, ret...) } } else { - header := lc.GetHeader(bhash) + header := lc.GetHeaderByHash(bhash) state := NewLightState(StateTrieID(header), lc.Odr()) from, err := state.GetOrNewStateObject(ctx, testBankAddress) if err == nil { @@ -215,7 +216,7 @@ func odrContractCall(ctx context.Context, db ethdb.Database, bc *core.BlockChain to: &testContractAddr, } - vmenv := NewEnv(ctx, state, lc, msg, header) + vmenv := NewEnv(ctx, state, testChainConfig(), lc, msg, header, vm.Config{}) gp := new(core.GasPool).AddGas(common.MaxBig) ret, _, _ := core.ApplyMessage(vmenv, msg, gp) if vmenv.Error() == nil { @@ -241,7 +242,7 @@ func testChainGen(i int, block *core.BlockGen) { nonce := block.TxNonce(acc1Addr) tx2, _ := types.NewTransaction(nonce, acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key) nonce++ - tx3, _ := types.NewContractCreation(nonce, big.NewInt(0), big.NewInt(100000), big.NewInt(0), testContractCode).SignECDSA(acc1Key) + tx3, _ := types.NewContractCreation(nonce, big.NewInt(0), big.NewInt(1000000), big.NewInt(0), testContractCode).SignECDSA(acc1Key) testContractAddr = crypto.CreateAddress(acc1Addr, nonce) block.AddTx(tx1) block.AddTx(tx2) @@ -277,14 +278,14 @@ func testChainOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { ) core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{testBankAddress, testBankFunds}) // Assemble the test environment - blockchain, _ := core.NewBlockChain(sdb, pow, evmux) + blockchain, _ := core.NewBlockChain(sdb, testChainConfig(), pow, evmux) gchain, _ := core.GenerateChain(genesis, sdb, 4, testChainGen) if _, err := blockchain.InsertChain(gchain); err != nil { panic(err) } odr := &testOdr{sdb: sdb, ldb: ldb} - lightchain, _ := NewLightChain(odr, pow, evmux) + lightchain, _ := NewLightChain(odr, testChainConfig(), pow, evmux) lightchain.SetValidator(bproc{}) headers := make([]*types.Header, len(gchain)) for i, block := range gchain { diff --git a/light/odr_util.go b/light/odr_util.go index 1e1b7f2546..2390074d8c 100644 --- a/light/odr_util.go +++ b/light/odr_util.go @@ -53,11 +53,11 @@ func retrieveContractCode(ctx context.Context, odr OdrBackend, id *TrieID, hash } // GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. -func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash) (rlp.RawValue, error) { - if data := core.GetBodyRLP(odr.Database(), hash); data != nil { +func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (rlp.RawValue, error) { + if data := core.GetBodyRLP(odr.Database(), hash, number); data != nil { return data, nil } - r := &BlockRequest{Hash: hash} + r := &BlockRequest{Hash: hash, Number: number} if err := odr.Retrieve(ctx, r); err != nil { return nil, err } else { @@ -67,8 +67,8 @@ func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash) (rlp.RawV // GetBody retrieves the block body (transactons, uncles) corresponding to the // hash. -func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Body, error) { - data, err := GetBodyRLP(ctx, odr, hash) +func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Body, error) { + data, err := GetBodyRLP(ctx, odr, hash, number) if err != nil { return nil, err } @@ -82,13 +82,13 @@ func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Body // GetBlock retrieves an entire block corresponding to the hash, assembling it // back from the stored header and body. -func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Block, error) { +func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Block, error) { // Retrieve the block header and body contents - header := core.GetHeader(odr.Database(), hash) + header := core.GetHeader(odr.Database(), hash, number) if header == nil { return nil, ErrNoHeader } - body, err := GetBody(ctx, odr, hash) + body, err := GetBody(ctx, odr, hash, number) if err != nil { return nil, err } @@ -98,12 +98,12 @@ func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash) (*types.Blo // GetBlockReceipts retrieves the receipts generated by the transactions included // in a block given by its hash. -func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash) (types.Receipts, error) { - receipts := core.GetBlockReceipts(odr.Database(), hash) +func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error) { + receipts := core.GetBlockReceipts(odr.Database(), hash, number) if receipts != nil { return receipts, nil } - r := &ReceiptsRequest{Hash: hash} + r := &ReceiptsRequest{Hash: hash, Number: number} if err := odr.Retrieve(ctx, r); err != nil { return nil, err } else { diff --git a/light/state_object.go b/light/state_object.go index 374f445216..6be7f92ac9 100644 --- a/light/state_object.go +++ b/light/state_object.go @@ -40,7 +40,7 @@ func (self Code) String() string { } // Storage is a memory map cache of a contract storage -type Storage map[string]common.Hash +type Storage map[common.Hash]common.Hash // String returns a string representation of the storage cache func (self Storage) String() (str string) { @@ -135,8 +135,7 @@ func (self *StateObject) Storage() Storage { // GetState returns the storage value at the given address from either the cache // or the trie func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common.Hash, error) { - strkey := key.Str() - value, exists := self.storage[strkey] + value, exists := self.storage[key] if !exists { var err error value, err = self.getAddr(ctx, key) @@ -144,7 +143,7 @@ func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common. return common.Hash{}, err } if (value != common.Hash{}) { - self.storage[strkey] = value + self.storage[key] = value } } @@ -153,7 +152,7 @@ func (self *StateObject) GetState(ctx context.Context, key common.Hash) (common. // SetState sets the storage value at the given address func (self *StateObject) SetState(k, value common.Hash) { - self.storage[k.Str()] = value + self.storage[k] = value self.dirty = true } @@ -238,13 +237,13 @@ func (self *StateObject) Nonce() uint64 { return self.nonce } -// EachStorage calls a callback function for every key/value pair found +// ForEachStorage calls a callback function for every key/value pair found // in the local storage cache. Note that unlike core/state.StateObject, // light.StateObject only returns cached values and doesn't download the // entire storage tree. -func (self *StateObject) EachStorage(cb func(key, value []byte)) { +func (self *StateObject) ForEachStorage(cb func(key, value common.Hash) bool) { for h, v := range self.storage { - cb([]byte(h), v.Bytes()) + cb(h, v) } } diff --git a/light/state_test.go b/light/state_test.go index 88bf7456b3..0aa44726c4 100644 --- a/light/state_test.go +++ b/light/state_test.go @@ -51,7 +51,7 @@ func makeTestState() (common.Hash, ethdb.Database) { func TestLightStateOdr(t *testing.T) { root, sdb := makeTestState() - header := &types.Header{Root: root} + header := &types.Header{Root: root, Number: big.NewInt(0)} core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} @@ -138,7 +138,7 @@ func TestLightStateOdr(t *testing.T) { func TestLightStateSetCopy(t *testing.T) { root, sdb := makeTestState() - header := &types.Header{Root: root} + header := &types.Header{Root: root, Number: big.NewInt(0)} core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} @@ -217,7 +217,7 @@ func TestLightStateSetCopy(t *testing.T) { func TestLightStateDelete(t *testing.T) { root, sdb := makeTestState() - header := &types.Header{Root: root} + header := &types.Header{Root: root, Number: big.NewInt(0)} core.WriteHeader(sdb, header) ldb, _ := ethdb.NewMemDatabase() odr := &testOdr{sdb: sdb, ldb: ldb} diff --git a/light/trie.go b/light/trie.go index fa9dcbc791..6de6b48494 100644 --- a/light/trie.go +++ b/light/trie.go @@ -78,7 +78,7 @@ func (t *LightTrie) do(ctx context.Context, fallbackKey []byte, fn func() error) func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.id.root, t.db) + t.trie, err = trie.NewSecure(t.id.Root, t.db) } if err == nil { res, err = t.trie.TryGet(key) @@ -97,7 +97,7 @@ func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error) func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.id.root, t.db) + t.trie, err = trie.NewSecure(t.id.Root, t.db) } if err == nil { err = t.trie.TryUpdate(key, value) @@ -111,7 +111,7 @@ func (t *LightTrie) Update(ctx context.Context, key, value []byte) (err error) { func (t *LightTrie) Delete(ctx context.Context, key []byte) (err error) { err = t.do(ctx, key, func() (err error) { if t.trie == nil { - t.trie, err = trie.NewSecure(t.id.root, t.db) + t.trie, err = trie.NewSecure(t.id.Root, t.db) } if err == nil { err = t.trie.TryDelete(key) diff --git a/light/txpool.go b/light/txpool.go index 6e908f5d61..b1e3f92e6a 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -28,7 +28,6 @@ import ( "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" - "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/net/context" ) @@ -43,6 +42,7 @@ var txPermanent = uint64(500) // always receive all locally signed transactions in the same order as they are // created. type TxPool struct { + config *core.ChainConfig quit chan bool eventMux *event.TypeMux events event.Subscription @@ -76,8 +76,9 @@ type TxRelayBackend interface { } // NewTxPool creates a new light transaction pool -func NewTxPool(eventMux *event.TypeMux, chain *LightChain, relay TxRelayBackend) *TxPool { +func NewTxPool(config *core.ChainConfig, eventMux *event.TypeMux, chain *LightChain, relay TxRelayBackend) *TxPool { pool := &TxPool{ + config: config, nonce: make(map[common.Address]uint64), pending: make(map[common.Hash]*types.Transaction), mined: make(map[common.Hash][]*types.Transaction), @@ -170,7 +171,7 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, idx uin return nil } - receipts, err := GetBlockReceipts(ctx, pool.odr, hash) + receipts, err := GetBlockReceipts(ctx, pool.odr, hash, idx) if err != nil { return err } @@ -213,18 +214,18 @@ func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) { // possible to continue checking the missing blocks at the next chain head event func (pool *TxPool) setNewHead(ctx context.Context, newHeader *types.Header) (txStateChanges, error) { txc := make(txStateChanges) - oldh := pool.chain.GetHeader(pool.head) + oldh := pool.chain.GetHeaderByHash(pool.head) newh := newHeader // find common ancestor, create list of rolled back and new block hashes var oldHashes, newHashes []common.Hash for oldh.Hash() != newh.Hash() { if oldh.GetNumberU64() >= newh.GetNumberU64() { oldHashes = append(oldHashes, oldh.Hash()) - oldh = pool.chain.GetHeader(oldh.ParentHash) + oldh = pool.chain.GetHeader(oldh.ParentHash, oldh.Number.Uint64()-1) } if oldh.GetNumberU64() < newh.GetNumberU64() { newHashes = append(newHashes, newh.Hash()) - newh = pool.chain.GetHeader(newh.ParentHash) + newh = pool.chain.GetHeader(newh.ParentHash, newh.Number.Uint64()-1) } } if oldh.GetNumberU64() < pool.clearIdx { @@ -280,7 +281,7 @@ func (pool *TxPool) eventLoop() { txc, _ := pool.setNewHead(ctx, head) m, r := txc.getLists() pool.relay.NewHead(pool.head, m, r) - pool.homestead = params.IsHomestead(head.Number) + pool.homestead = pool.config.IsHomestead(head.Number) pool.mu.Unlock() } } @@ -338,7 +339,7 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error // Check the transaction doesn't exceed the current // block limit gas. - header := pool.chain.GetHeader(pool.head) + header := pool.chain.GetHeaderByHash(pool.head) if header.GasLimit.Cmp(tx.Gas()) < 0 { return core.ErrGasLimit } diff --git a/light/txpool_test.go b/light/txpool_test.go index 03402f4c65..352fff35aa 100644 --- a/light/txpool_test.go +++ b/light/txpool_test.go @@ -86,7 +86,7 @@ func TestTxPool(t *testing.T) { ) core.WriteGenesisBlockForTesting(ldb, core.GenesisAccount{testBankAddress, testBankFunds}) // Assemble the test environment - blockchain, _ := core.NewBlockChain(sdb, pow, evmux) + blockchain, _ := core.NewBlockChain(sdb, testChainConfig(), pow, evmux) gchain, _ := core.GenerateChain(genesis, sdb, poolTestBlocks, txPoolTestChainGen) if _, err := blockchain.InsertChain(gchain); err != nil { panic(err) @@ -94,10 +94,10 @@ func TestTxPool(t *testing.T) { odr := &testOdr{sdb: sdb, ldb: ldb} relay := &testTxRelay{} - lightchain, _ := NewLightChain(odr, pow, evmux) + lightchain, _ := NewLightChain(odr, testChainConfig(), pow, evmux) lightchain.SetValidator(bproc{}) txPermanent = 50 - pool := NewTxPool(evmux, lightchain, relay) + pool := NewTxPool(testChainConfig(), evmux, lightchain, relay) for ii, block := range gchain { i := ii + 1 @@ -120,7 +120,7 @@ func TestTxPool(t *testing.T) { if _, err := lightchain.InsertHeaderChain([]*types.Header{block.Header()}, 1); err != nil { panic(err) } - time.Sleep(time.Millisecond * 10) + time.Sleep(time.Millisecond * 30) got := relay.nhMined exp := minedTx(i) - minedTx(i-1) @@ -137,4 +137,4 @@ func TestTxPool(t *testing.T) { t.Errorf("relay.Discard expected len = %d, got %d", exp, got) } } -} +} \ No newline at end of file diff --git a/light/vm_env.go b/light/vm_env.go index 493190d74d..c0a72b6059 100644 --- a/light/vm_env.go +++ b/light/vm_env.go @@ -33,6 +33,8 @@ import ( type VMEnv struct { vm.Environment ctx context.Context + chainConfig *core.ChainConfig + evm *vm.EVM state *VMState header *types.Header msg core.Message @@ -45,17 +47,27 @@ type VMEnv struct { } // NewEnv creates a new execution environment based on an ODR capable light state -func NewEnv(ctx context.Context, state *LightState, chain *LightChain, msg core.Message, header *types.Header) *VMEnv { +func NewEnv(ctx context.Context, state *LightState, chainConfig *core.ChainConfig, chain *LightChain, msg core.Message, header *types.Header, cfg vm.Config) *VMEnv { env := &VMEnv{ + chainConfig: chainConfig, chain: chain, header: header, msg: msg, typ: vm.StdVmTy, } env.state = &VMState{ctx: ctx, state: state, env: env} + + // if no log collector is present set self as the collector + if cfg.Logger.Collector == nil { + cfg.Logger.Collector = env + } + + env.evm = vm.New(env, cfg) return env } +func (self *VMEnv) RuleSet() vm.RuleSet { return self.chainConfig } +func (self *VMEnv) Vm() vm.Vm { return self.evm } func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f } func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number } func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase } @@ -69,7 +81,7 @@ func (self *VMEnv) SetDepth(i int) { self.depth = i } func (self *VMEnv) VmType() vm.Type { return self.typ } func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t } func (self *VMEnv) GetHash(n uint64) common.Hash { - for header := self.chain.GetHeader(self.header.ParentHash); header != nil; header = self.chain.GetHeader(header.ParentHash) { + for header := self.chain.GetHeader(self.header.ParentHash, self.header.Number.Uint64()-1); header != nil; header = self.chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) { if header.GetNumberU64() == n { return header.Hash() } @@ -142,6 +154,10 @@ func (s *VMState) errHandler(err error) { func (s *VMState) GetAccount(addr common.Address) vm.Account { so, err := s.state.GetStateObject(s.ctx, addr) s.errHandler(err) + if err != nil { + // return a dummy state object to avoid panics + so = s.state.newStateObject(addr) + } return so } @@ -149,6 +165,10 @@ func (s *VMState) GetAccount(addr common.Address) vm.Account { func (s *VMState) CreateAccount(addr common.Address) vm.Account { so, err := s.state.CreateStateObject(s.ctx, addr) s.errHandler(err) + if err != nil { + // return a dummy state object to avoid panics + so = s.state.newStateObject(addr) + } return so } From f62fd849d5d5037ba533ab9896691aa7b38c24c8 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 19 Jun 2016 09:22:57 +0200 Subject: [PATCH 04/32] fix --- eth/backend.go | 6 +++--- eth/bind.go | 8 ++++---- eth/downloader/downloader.go | 17 +++++++++++++++-- les/backend.go | 8 ++++---- release/release.go | 16 +++++++++++++--- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index a7de2b130e..b5318f9679 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -133,7 +133,7 @@ type FullNodeService struct { httpclient *httpclient.HTTPClient accountManager *accounts.Manager - apiBackend *EthApiBackend + ApiBackend *EthApiBackend miner *miner.Miner Mining bool @@ -251,7 +251,7 @@ func New(ctx *node.ServiceContext, config *Config) (*FullNodeService, error) { GpobaseCorrectionFactor: config.GpobaseCorrectionFactor, } gpo := gasprice.NewGasPriceOracle(eth.blockchain, chainDb, eth.eventMux, gpoParams) - eth.apiBackend = &EthApiBackend{eth, gpo} + eth.ApiBackend = &EthApiBackend{eth, gpo} return eth, nil } @@ -318,7 +318,7 @@ func CreatePoW(config *Config) (*ethash.Ethash, error) { // APIs returns the collection of RPC services the ethereum package offers. // NOTE, some of these services probably need to be moved to somewhere else. func (s *FullNodeService) APIs() []rpc.API { - return append(ethapi.GetAPIs(s.apiBackend, &s.solcPath, &s.solc), []rpc.API{ + return append(ethapi.GetAPIs(s.ApiBackend, &s.solcPath, &s.solc), []rpc.API{ { Namespace: "eth", Version: "1.0", diff --git a/eth/bind.go b/eth/bind.go index 7eb15ca1b6..040797190a 100644 --- a/eth/bind.go +++ b/eth/bind.go @@ -42,11 +42,11 @@ type ContractBackend struct { // NewContractBackend creates a new native contract backend using an existing // Etheruem object. -func NewContractBackend(eth *FullNodeService) *ContractBackend { +func NewContractBackend(apiBackend ethapi.Backend) *ContractBackend { return &ContractBackend{ - eapi: ethapi.NewPublicEthereumAPI(eth.apiBackend, nil, nil), - bcapi: ethapi.NewPublicBlockChainAPI(eth.apiBackend), - txapi: ethapi.NewPublicTransactionPoolAPI(eth.apiBackend), + eapi: ethapi.NewPublicEthereumAPI(apiBackend, nil, nil), + bcapi: ethapi.NewPublicBlockChainAPI(apiBackend), + txapi: ethapi.NewPublicTransactionPoolAPI(apiBackend), } } diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 56c56bf2c1..7e5a9092c8 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1648,10 +1648,23 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error { for i, header := range rollback { hashes[i] = header.Hash() } - lastHeader, lastFastBlock, lastBlock := d.headHeader().Number, d.headFastBlock().Number(), d.headBlock().Number() + lastHeader, lastFastBlock, lastBlock := d.headHeader().Number, common.Big0, common.Big0 + if d.headFastBlock != nil { + lastFastBlock = d.headFastBlock().Number() + } + if d.headBlock != nil { + lastBlock = d.headBlock().Number() + } d.rollback(hashes) + curFastBlock, curBlock := common.Big0, common.Big0 + if d.headFastBlock != nil { + curFastBlock = d.headFastBlock().Number() + } + if d.headBlock != nil { + curBlock = d.headBlock().Number() + } glog.V(logger.Warn).Infof("Rolled back %d headers (LH: %d->%d, FB: %d->%d, LB: %d->%d)", - len(hashes), lastHeader, d.headHeader().Number, lastFastBlock, d.headFastBlock().Number(), lastBlock, d.headBlock().Number()) + len(hashes), lastHeader, d.headHeader().Number, lastFastBlock, curFastBlock, lastBlock, curBlock) // If we're already past the pivot point, this could be an attack, thread carefully if rollback[len(rollback)-1].Number.Uint64() > pivot { diff --git a/les/backend.go b/les/backend.go index dcf19b57f5..d261a92ecd 100644 --- a/les/backend.go +++ b/les/backend.go @@ -55,7 +55,7 @@ type LightNodeService struct { chainDb ethdb.Database // Block chain database dappDb ethdb.Database // Dapp database - apiBackend *LesApiBackend + ApiBackend *LesApiBackend eventMux *event.TypeMux pow *ethash.Ethash @@ -123,15 +123,15 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightNodeService, error } odr.removePeer = eth.protocolManager.removePeer - eth.apiBackend = &LesApiBackend{eth, nil} - eth.apiBackend.gpo = gasprice.NewLightPriceOracle(eth.apiBackend) + eth.ApiBackend = &LesApiBackend{eth, nil} + eth.ApiBackend.gpo = gasprice.NewLightPriceOracle(eth.ApiBackend) return eth, nil } // APIs returns the collection of RPC services the ethereum package offers. // NOTE, some of these services probably need to be moved to somewhere else. func (s *LightNodeService) APIs() []rpc.API { - return append(ethapi.GetAPIs(s.apiBackend, &s.solcPath, &s.solc), []rpc.API{ + return append(ethapi.GetAPIs(s.ApiBackend, &s.solcPath, &s.solc), []rpc.API{ { Namespace: "eth", Version: "1.0", diff --git a/release/release.go b/release/release.go index 07eb9359cf..60031938aa 100644 --- a/release/release.go +++ b/release/release.go @@ -25,6 +25,8 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/les" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/node" @@ -58,12 +60,20 @@ type ReleaseService struct { // releases and notify the user of such. func NewReleaseService(ctx *node.ServiceContext, config Config) (node.Service, error) { // Retrieve the Ethereum service dependency to access the blockchain + var apiBackend ethapi.Backend var ethereum *eth.FullNodeService - if err := ctx.Service(ðereum); err != nil { - return nil, err + if err := ctx.Service(ðereum); err == nil { + apiBackend = ethereum.ApiBackend + } else { + var ethereum *les.LightNodeService + if err := ctx.Service(ðereum); err == nil { + apiBackend = ethereum.ApiBackend + } else { + return nil, err + } } // Construct the release service - contract, err := NewReleaseOracle(config.Oracle, eth.NewContractBackend(ethereum)) + contract, err := NewReleaseOracle(config.Oracle, eth.NewContractBackend(apiBackend)) if err != nil { return nil, err } From 47eb6b817e8bd343ff6a5a065a3fe125245b3195 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Mon, 20 Jun 2016 00:30:16 +0200 Subject: [PATCH 05/32] console: implemented context timeout --- cmd/geth/consolecmd.go | 36 ++++++++++++++++++++++++------------ console/bridge.go | 32 ++++++++++++++++++++++++++++---- console/console.go | 41 +++++++++++++++++++++++++---------------- rpc/types.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 32 deletions(-) diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 257050a627..6c200824bf 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -22,6 +22,7 @@ import ( "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/console" + "github.com/ethereum/go-ethereum/rpc" "gopkg.in/urfave/cli.v1" ) @@ -67,10 +68,13 @@ func localConsole(ctx *cli.Context) error { defer node.Stop() // 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) - } + client := rpc.NewClientRestartWrapper(func() rpc.Client { + client, err := node.Attach() + if err != nil { + utils.Fatalf("Failed to attach to the inproc geth: %v", err) + } + return client + }) config := console.Config{ DataDir: node.DataDir(), DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), @@ -99,10 +103,14 @@ func localConsole(ctx *cli.Context) error { // console to it. func remoteConsole(ctx *cli.Context) error { // Attach to a remotely running geth instance and start the JavaScript console - client, err := utils.NewRemoteRPCClient(ctx) - if err != nil { - utils.Fatalf("Unable to attach to remote geth: %v", err) - } + client := rpc.NewClientRestartWrapper(func() rpc.Client { + client, err := utils.NewRemoteRPCClient(ctx) + if err != nil { + utils.Fatalf("Unable to attach to remote geth: %v", err) + } + return client + }) + config := console.Config{ DataDir: utils.MustMakeDataDir(ctx), DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), @@ -137,10 +145,14 @@ func ephemeralConsole(ctx *cli.Context) error { defer node.Stop() // 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) - } + client := rpc.NewClientRestartWrapper(func() rpc.Client { + client, err := node.Attach() + if err != nil { + utils.Fatalf("Failed to attach to the inproc geth: %v", err) + } + return client + }) + config := console.Config{ DataDir: node.DataDir(), DocRoot: ctx.GlobalString(utils.JSpathFlag.Name), diff --git a/console/bridge.go b/console/bridge.go index b23e06837d..65f2541b03 100644 --- a/console/bridge.go +++ b/console/bridge.go @@ -26,18 +26,20 @@ import ( "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/rpc" "github.com/robertkrimen/otto" + "golang.org/x/net/context" ) // bridge is a collection of JavaScript utility methods to bride the .js runtime // environment and the Go RPC connection backing the remote method calls. type bridge struct { - client rpc.Client // RPC client to execute Ethereum requests through + client *rpc.ClientRestartWrapper // RPC client to execute Ethereum requests through prompter UserPrompter // Input prompter to allow interactive user feedback printer io.Writer // Output writer to serialize any display strings to + ctx context.Context } // newBridge creates a new JavaScript wrapper around an RPC client. -func newBridge(client rpc.Client, prompter UserPrompter, printer io.Writer) *bridge { +func newBridge(client *rpc.ClientRestartWrapper, prompter UserPrompter, printer io.Writer) *bridge { return &bridge{ client: client, prompter: prompter, @@ -45,6 +47,10 @@ func newBridge(client rpc.Client, prompter UserPrompter, printer io.Writer) *bri } } +func (b *bridge) setContext(ctx context.Context) { + b.ctx = ctx +} + // NewAccount is a wrapper around the personal.newAccount RPC method that uses a // non-echoing password prompt to aquire the passphrase and executes the original // RPC method (saved in jeth.newAccount) with it to actually execute the RPC call. @@ -223,11 +229,29 @@ func (b *bridge) Send(call otto.FunctionCall) (response otto.Value) { for i, req := range reqs { // Execute the RPC request and parse the reply - if err = b.client.Send(&req); err != nil { + client := b.client.Client() + if err = client.Send(&req); err != nil { return newErrorResponse(call, -32603, err.Error(), req.Id) } + errc := make(chan error, 1) + errc2 := make(chan error) + go func(){ + if b.ctx != nil { + select { + case <-b.ctx.Done(): + b.client.Restart() + errc2 <- b.ctx.Err() + case err := <-errc: + errc2 <- err + } + } else { + errc2 <- <-errc + } + }() result := make(map[string]interface{}) - if err = b.client.Recv(&result); err != nil { + errc <- client.Recv(&result) + err := <-errc2 + if err != nil { return newErrorResponse(call, -32603, err.Error(), req.Id) } // Feed the reply back into the JavaScript runtime environment diff --git a/console/console.go b/console/console.go index 00d1fea1d8..449deeaf38 100644 --- a/console/console.go +++ b/console/console.go @@ -26,6 +26,7 @@ import ( "regexp" "sort" "strings" + "time" "github.com/ethereum/go-ethereum/internal/jsre" "github.com/ethereum/go-ethereum/internal/web3ext" @@ -33,6 +34,7 @@ import ( "github.com/mattn/go-colorable" "github.com/peterh/liner" "github.com/robertkrimen/otto" + "golang.org/x/net/context" ) var ( @@ -50,26 +52,27 @@ const DefaultPrompt = "> " // Config is te collection of configurations to fine tune the behavior of the // JavaScript console. type Config struct { - DataDir string // Data directory to store the console history at - DocRoot string // Filesystem path from where to load JavaScript files from - Client rpc.Client // RPC client to execute Ethereum requests through - Prompt string // Input prompt prefix string (defaults to DefaultPrompt) - Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter) - Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout) - Preload []string // Absolute paths to JavaScript files to preload + DataDir string // Data directory to store the console history at + DocRoot string // Filesystem path from where to load JavaScript files from + Client *rpc.ClientRestartWrapper // RPC client to execute Ethereum requests through + Prompt string // Input prompt prefix string (defaults to DefaultPrompt) + Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter) + Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout) + Preload []string // Absolute paths to JavaScript files to preload } // Console is a JavaScript interpreted runtime environment. It is a fully fleged // JavaScript console attached to a running node via an external or in-process RPC // client. type Console struct { - client rpc.Client // RPC client to execute Ethereum requests through - jsre *jsre.JSRE // JavaScript runtime environment running the interpreter - prompt string // Input prompt prefix string - prompter UserPrompter // Input prompter to allow interactive user feedback - histPath string // Absolute path to the console scrollback history - history []string // Scroll history maintained by the console - printer io.Writer // Output writer to serialize any display strings to + client *rpc.ClientRestartWrapper // RPC client to execute Ethereum requests through + jsre *jsre.JSRE // JavaScript runtime environment running the interpreter + prompt string // Input prompt prefix string + prompter UserPrompter // Input prompter to allow interactive user feedback + histPath string // Absolute path to the console scrollback history + history []string // Scroll history maintained by the console + printer io.Writer // Output writer to serialize any display strings to + setContext func(context.Context) } func New(config Config) (*Console, error) { @@ -103,6 +106,7 @@ func New(config Config) (*Console, error) { func (c *Console) init(preload []string) error { // Initialize the JavaScript <-> Go RPC bridge bridge := newBridge(c.client, c.prompter, c.printer) + c.setContext = bridge.setContext c.jsre.Set("jeth", struct{}{}) jethObj, _ := c.jsre.Get("jeth") @@ -127,7 +131,7 @@ func (c *Console) init(preload []string) error { return fmt.Errorf("web3 provider: %v", err) } // Load the supported APIs into the JavaScript runtime environment - apis, err := c.client.SupportedModules() + apis, err := c.client.Client().SupportedModules() if err != nil { return fmt.Errorf("api modules: %v", err) } @@ -253,7 +257,7 @@ func (c *Console) Welcome() { console.log(" datadir: " + admin.datadir); `) // List all the supported modules for the user to call - if apis, err := c.client.SupportedModules(); err == nil { + if apis, err := c.client.Client().SupportedModules(); err == nil { modules := make([]string, 0, len(apis)) for api, version := range apis { modules = append(modules, fmt.Sprintf("%s:%s", api, version)) @@ -347,7 +351,12 @@ func (c *Console) Interactive() { } } } + done := make(chan struct{}) + ctx, _ := context.WithTimeout(context.Background(), time.Second*5) + c.setContext(ctx) c.Evaluate(input) + c.setContext(nil) + close(done) input = "" } } diff --git a/rpc/types.go b/rpc/types.go index a1f36fbd26..5c821d41a7 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -284,3 +284,31 @@ type Client interface { Close() } + +type ClientRestartWrapper struct { + client Client + newClientFn func() Client + mu sync.RWMutex +} + +func NewClientRestartWrapper(newClientFn func() Client) *ClientRestartWrapper { + return &ClientRestartWrapper { + client: newClientFn(), + newClientFn: newClientFn, + } +} + +func (rw *ClientRestartWrapper) Client() Client { + rw.mu.RLock() + defer rw.mu.RUnlock() + + return rw.client +} + +func (rw *ClientRestartWrapper) Restart() { + rw.mu.Lock() + defer rw.mu.Unlock() + + rw.client.Close() + rw.client = rw.newClientFn() +} From 31d04569bdb491f679d506a3cf7487f0dd0ad889 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Mon, 20 Jun 2016 13:38:24 +0200 Subject: [PATCH 06/32] les: downloader can't drop peers (hack) --- les/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/les/handler.go b/les/handler.go index fda7f5d876..c612fd22ee 100644 --- a/les/handler.go +++ b/les/handler.go @@ -170,7 +170,7 @@ func NewProtocolManager(chainConfig *core.ChainConfig, lightSync bool, networkId glog.V(logger.Debug).Infof("LES: create downloader") manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, blockchain.HasHeader, nil, blockchain.GetHeaderByHash, nil, blockchain.CurrentHeader, nil, nil, nil, blockchain.GetTdByHash, - blockchain.InsertHeaderChain, nil, nil, blockchain.Rollback, manager.removePeer) + blockchain.InsertHeaderChain, nil, nil, blockchain.Rollback, func(id string) {}) // manager.removePeer) } /*validator := func(block *types.Block, parent *types.Block) error { From 5ab65c3a1bf7af4b9d60accb7f8cfee74dd5659f Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Mon, 20 Jun 2016 14:54:55 +0200 Subject: [PATCH 07/32] downloader: fixed bug in light mode --- eth/downloader/downloader.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 7e5a9092c8..6e2680c321 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -1712,8 +1712,10 @@ func (d *Downloader) processHeaders(origin uint64, td *big.Int) error { // L: Sync begins, and finds common ancestor at 11 // L: Request new headers up from 11 (R's TD was higher, it must have something) // R: Nothing to give - if !gotHeaders && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 { - return errStallingPeer + if d.mode != LightSync { + if !gotHeaders && td.Cmp(d.getTd(d.headBlock().Hash())) > 0 { + return errStallingPeer + } } // If fast or light syncing, ensure promised headers are indeed delivered. This is // needed to detect scenarios where an attacker feeds a bad pivot and then bails out From 8a2044789add03de9cfc7078e7f0c89b20491a3a Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Tue, 21 Jun 2016 16:39:40 +0200 Subject: [PATCH 08/32] filter 1 --- eth/backend.go | 2 +- eth/filters/api.go | 19 +++++++++++-------- eth/filters/filter.go | 35 ++++++++++++++++++++--------------- les/backend.go | 6 ++++++ 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index b5318f9679..b13d408351 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -342,7 +342,7 @@ func (s *FullNodeService) APIs() []rpc.API { }, { Namespace: "eth", Version: "1.0", - Service: filters.NewPublicFilterAPI(s.chainDb, s.eventMux), + Service: filters.NewPublicFilterAPI(s.ApiBackend), Public: true, }, { Namespace: "admin", diff --git a/eth/filters/api.go b/eth/filters/api.go index 393019f8b0..869cafd937 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/rpc" "golang.org/x/net/context" @@ -50,10 +51,11 @@ const ( // PublicFilterAPI offers support to create and manage filters. This will allow external clients to retrieve various // information related to the Ethereum protocol such als blocks, transactions and logs. type PublicFilterAPI struct { - mux *event.TypeMux + apiBackend ethapi.Backend quit chan struct{} chainDb ethdb.Database + mux *event.TypeMux filterManager *FilterSystem @@ -73,10 +75,11 @@ type PublicFilterAPI struct { } // NewPublicFilterAPI returns a new PublicFilterAPI instance. -func NewPublicFilterAPI(chainDb ethdb.Database, mux *event.TypeMux) *PublicFilterAPI { +func NewPublicFilterAPI(apiBackend ethapi.Backend) *PublicFilterAPI { svc := &PublicFilterAPI{ - mux: mux, - chainDb: chainDb, + apiBackend: apiBackend, + mux: apiBackend.EventMux(), + chainDb: apiBackend.ChainDb(), filterManager: NewFilterSystem(mux), filterMapping: make(map[string]int), logQueue: make(map[int]*logQueue), @@ -141,7 +144,7 @@ func (s *PublicFilterAPI) NewBlockFilter() (string, error) { } s.blockMu.Lock() - filter := New(s.chainDb) + filter := New(s.apiBackend) id, err := s.filterManager.Add(filter, ChainFilter) if err != nil { return "", err @@ -177,7 +180,7 @@ func (s *PublicFilterAPI) NewPendingTransactionFilter() (string, error) { s.transactionMu.Lock() defer s.transactionMu.Unlock() - filter := New(s.chainDb) + filter := New(s.apiBackend) id, err := s.filterManager.Add(filter, PendingTxFilter) if err != nil { return "", err @@ -206,7 +209,7 @@ func (s *PublicFilterAPI) newLogFilter(earliest, latest int64, addresses []commo s.logMu.Lock() defer s.logMu.Unlock() - filter := New(s.chainDb) + filter := New(s.apiBackend) id, err := s.filterManager.Add(filter, LogFilter) if err != nil { return 0, err @@ -432,7 +435,7 @@ func (s *PublicFilterAPI) NewFilter(args NewFilterArgs) (string, error) { // GetLogs returns the logs matching the given argument. func (s *PublicFilterAPI) GetLogs(args NewFilterArgs) []vmlog { - filter := New(s.chainDb) + filter := New(s.apiBackend) filter.SetBeginBlock(args.FromBlock.Int64()) filter.SetEndBlock(args.ToBlock.Int64()) filter.SetAddresses(args.Addresses) diff --git a/eth/filters/filter.go b/eth/filters/filter.go index 995b588fb2..f57d375c66 100644 --- a/eth/filters/filter.go +++ b/eth/filters/filter.go @@ -25,6 +25,8 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/internal/ethapi" + "github.com/ethereum/go-ethereum/rpc" ) type AccountChange struct { @@ -33,6 +35,8 @@ type AccountChange struct { // Filtering interface type Filter struct { + apiBackend ethapi.Backend + created time.Time db ethdb.Database @@ -47,8 +51,11 @@ type Filter struct { // Create a new filter which uses a bloom filter on blocks to figure out whether a particular block // is interesting or not. -func New(db ethdb.Database) *Filter { - return &Filter{db: db} +func New(apiBackend ethapi.Backend) *Filter { + return &Filter{ + apiBackend: apiBackend, + db: apiBackend.ChainDb(), + } } // Set the earliest and latest block for filtering. @@ -72,15 +79,15 @@ func (self *Filter) SetTopics(topics [][]common.Hash) { // Run filters logs with the current parameters set func (self *Filter) Find() vm.Logs { - latestHash := core.GetHeadBlockHash(self.db) - latestBlock := core.GetBlock(self.db, latestHash, core.GetBlockNumber(self.db, latestHash)) + headBlockNumber := self.apiBackend.HeaderByNumber(rpc.LatestBlockNumber).Number.Uint64() + var beginBlockNo uint64 = uint64(self.begin) if self.begin == -1 { - beginBlockNo = latestBlock.NumberU64() + beginBlockNo = headBlockNumber } var endBlockNo uint64 = uint64(self.end) if self.end == -1 { - endBlockNo = latestBlock.NumberU64() + endBlockNo = headBlockNumber } // if no addresses are present we can't make use of fast search which @@ -126,19 +133,17 @@ func (self *Filter) getLogs(start, end uint64) (logs vm.Logs) { var block *types.Block for i := start; i <= end; i++ { - hash := core.GetCanonicalHash(self.db, i) - if hash != (common.Hash{}) { - block = core.GetBlock(self.db, hash, i) - } else { // block not found + header := self.apiBackend.HeaderByNumber(rpc.BlockNumber(i)) + if header == nil { return logs } // Use bloom filtering to see if this block is interesting given the // current parameters - if self.bloomFilter(block) { + if self.bloomFilter(header.Bloom) { // Get the logs of the block var ( - receipts = core.GetBlockReceipts(self.db, block.Hash(), i) + receipts = self.apiBackend.GetReceipts(ctx, header.Hash()) unfiltered vm.Logs ) for _, receipt := range receipts { @@ -202,11 +207,11 @@ Logs: return ret } -func (self *Filter) bloomFilter(block *types.Block) bool { +func (self *Filter) bloomFilter(bloom types.Bloom) bool { if len(self.addresses) > 0 { var included bool for _, addr := range self.addresses { - if types.BloomLookup(block.Bloom(), addr) { + if types.BloomLookup(bloom, addr) { included = true break } @@ -220,7 +225,7 @@ func (self *Filter) bloomFilter(block *types.Block) bool { for _, sub := range self.topics { var included bool for _, topic := range sub { - if (topic == common.Hash{}) || types.BloomLookup(block.Bloom(), topic) { + if (topic == common.Hash{}) || types.BloomLookup(bloom, topic) { included = true break } diff --git a/les/backend.go b/les/backend.go index d261a92ecd..0b9276ec2e 100644 --- a/les/backend.go +++ b/les/backend.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/eth/filters" "github.com/ethereum/go-ethereum/eth/gasprice" "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/ethdb" @@ -137,6 +138,11 @@ func (s *LightNodeService) APIs() []rpc.API { Version: "1.0", Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux), Public: true, + }, { + Namespace: "eth", + Version: "1.0", + Service: filters.NewPublicFilterAPI(s.ApiBackend), + Public: true, }, { Namespace: "net", Version: "1.0", From 9db355154575bd7a7d38f0ee9ac4c410a7e31b40 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 22 Jun 2016 02:37:38 +0200 Subject: [PATCH 09/32] filter 2 --- eth/filters/api.go | 16 +++++++++------- eth/filters/filter.go | 36 ++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/eth/filters/api.go b/eth/filters/api.go index 869cafd937..b9783031fb 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -80,7 +80,7 @@ func NewPublicFilterAPI(apiBackend ethapi.Backend) *PublicFilterAPI { apiBackend: apiBackend, mux: apiBackend.EventMux(), chainDb: apiBackend.ChainDb(), - filterManager: NewFilterSystem(mux), + filterManager: NewFilterSystem(apiBackend.EventMux()), filterMapping: make(map[string]int), logQueue: make(map[int]*logQueue), blockQueue: make(map[int]*hashQueue), @@ -434,14 +434,15 @@ func (s *PublicFilterAPI) NewFilter(args NewFilterArgs) (string, error) { } // GetLogs returns the logs matching the given argument. -func (s *PublicFilterAPI) GetLogs(args NewFilterArgs) []vmlog { +func (s *PublicFilterAPI) GetLogs(ctx context.Context, args NewFilterArgs) ([]vmlog, error) { filter := New(s.apiBackend) filter.SetBeginBlock(args.FromBlock.Int64()) filter.SetEndBlock(args.ToBlock.Int64()) filter.SetAddresses(args.Addresses) filter.SetTopics(args.Topics) - return toRPCLogs(filter.Find(), false) + logs, err := filter.Find(ctx) + return toRPCLogs(logs, false), err } // UninstallFilter removes the filter with the given filter id. @@ -527,17 +528,18 @@ func (s *PublicFilterAPI) logFilterChanged(id int) []vmlog { } // GetFilterLogs returns the logs for the filter with the given id. -func (s *PublicFilterAPI) GetFilterLogs(filterId string) []vmlog { +func (s *PublicFilterAPI) GetFilterLogs(ctx context.Context, filterId string) ([]vmlog, error) { id, ok := s.filterMapping[filterId] if !ok { - return toRPCLogs(nil, false) + return toRPCLogs(nil, false), nil } if filter := s.filterManager.Get(id); filter != nil { - return toRPCLogs(filter.Find(), false) + logs, err := filter.Find(ctx) + return toRPCLogs(logs, false), err } - return toRPCLogs(nil, false) + return toRPCLogs(nil, false), nil } // GetFilterChanges returns the logs for the filter with the given id since last time is was called. diff --git a/eth/filters/filter.go b/eth/filters/filter.go index f57d375c66..b781c65458 100644 --- a/eth/filters/filter.go +++ b/eth/filters/filter.go @@ -17,16 +17,17 @@ package filters import ( - "math" +// "math" "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core" +// "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/internal/ethapi" "github.com/ethereum/go-ethereum/rpc" + "golang.org/x/net/context" ) type AccountChange struct { @@ -78,7 +79,7 @@ func (self *Filter) SetTopics(topics [][]common.Hash) { } // Run filters logs with the current parameters set -func (self *Filter) Find() vm.Logs { +func (self *Filter) Find(ctx context.Context) (vm.Logs, error) { headBlockNumber := self.apiBackend.HeaderByNumber(rpc.LatestBlockNumber).Number.Uint64() var beginBlockNo uint64 = uint64(self.begin) @@ -93,13 +94,13 @@ func (self *Filter) Find() vm.Logs { // if no addresses are present we can't make use of fast search which // uses the mipmap bloom filters to check for fast inclusion and uses // higher range probability in order to ensure at least a false positive - if len(self.addresses) == 0 { - return self.getLogs(beginBlockNo, endBlockNo) - } - return self.mipFind(beginBlockNo, endBlockNo, 0) +// if len(self.addresses) == 0 { + return self.getLogs(ctx, beginBlockNo, endBlockNo) +// } +// return self.mipFind(beginBlockNo, endBlockNo, 0) } -func (self *Filter) mipFind(start, end uint64, depth int) (logs vm.Logs) { +/*func (self *Filter) mipFind(start, end uint64, depth int) (logs vm.Logs) { level := core.MIPMapLevels[depth] // normalise numerator so we can work in level specific batches and // work with the proper range checks @@ -127,25 +128,24 @@ func (self *Filter) mipFind(start, end uint64, depth int) (logs vm.Logs) { } return logs -} - -func (self *Filter) getLogs(start, end uint64) (logs vm.Logs) { - var block *types.Block +}*/ +func (self *Filter) getLogs(ctx context.Context, start, end uint64) (logs vm.Logs, err error) { for i := start; i <= end; i++ { header := self.apiBackend.HeaderByNumber(rpc.BlockNumber(i)) if header == nil { - return logs + return logs, nil } // Use bloom filtering to see if this block is interesting given the // current parameters if self.bloomFilter(header.Bloom) { // Get the logs of the block - var ( - receipts = self.apiBackend.GetReceipts(ctx, header.Hash()) - unfiltered vm.Logs - ) + receipts, err := self.apiBackend.GetReceipts(ctx, header.Hash()) + if err != nil { + return nil, err + } + var unfiltered vm.Logs for _, receipt := range receipts { unfiltered = append(unfiltered, receipt.Logs...) } @@ -153,7 +153,7 @@ func (self *Filter) getLogs(start, end uint64) (logs vm.Logs) { } } - return logs + return logs, nil } func includes(addresses []common.Address, a common.Address) bool { From 3566a7be6a14b251b2ca65b6c6f70d1d5417aed9 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 22 Jun 2016 18:13:19 +0200 Subject: [PATCH 10/32] rpc: debug prints (disabled) --- rpc/server.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rpc/server.go b/rpc/server.go index 69f3271e8e..12e70e4dc1 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -342,7 +342,13 @@ func (s *Server) exec(ctx context.Context, codec ServerCodec, req *serverRequest if req.err != nil { response = codec.CreateErrorResponse(&req.id, req.err) } else { +/*fmt.Println() +fmt.Println("SREQ") +fmt.Println(*req)*/ response, callback = s.handle(ctx, codec, req) +/*fmt.Println("RESP") +fmt.Println(response) +fmt.Println()*/ } if err := codec.Write(response); err != nil { @@ -366,9 +372,15 @@ func (s *Server) execBatch(ctx context.Context, codec ServerCodec, requests []*s responses[i] = codec.CreateErrorResponse(&req.id, req.err) } else { var callback func() +/*fmt.Println() +fmt.Println("SREQ batch") +fmt.Println(*req)*/ if responses[i], callback = s.handle(ctx, codec, req); callback != nil { callbacks = append(callbacks, callback) } +/*fmt.Println("RESP") +fmt.Println(responses[i]) +fmt.Println()*/ } } @@ -396,6 +408,10 @@ func (s *Server) readRequest(codec ServerCodec) ([]*serverRequest, bool, RPCErro // verify requests for i, r := range reqs { +/*fmt.Println() +fmt.Println(time.Now()) +fmt.Println("REQ") +fmt.Println(r)*/ var ok bool var svc *service From 0eef51948148db33b5b2f31c78b948edd2be4bbf Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Thu, 23 Jun 2016 16:10:29 +0200 Subject: [PATCH 11/32] sync 1 --- les/handler.go | 33 +++++++++++++++++++++++++++++++++ les/peer.go | 6 ++++++ les/protocol.go | 1 + les/server.go | 2 ++ 4 files changed, 42 insertions(+) diff --git a/les/handler.go b/les/handler.go index c612fd22ee..7aacdaa5e6 100644 --- a/les/handler.go +++ b/les/handler.go @@ -353,6 +353,13 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { return errResp(ErrExtraStatusMsg, "uncontrolled status message") // Block header query, collect the requested headers and reply + case NewBlockHashesMsg: + var req newBlockHashesData + if err := msg.Decode(&req); err != nil { + return errResp(ErrDecode, "%v: %v", msg, err) + } +fmt.Println("RECEIVED", req[0].Number, req[0].Hash, req[0].Td) + case GetBlockHeadersMsg: glog.V(logger.Debug).Infof("LES: received GetBlockHeadersMsg from peer %v", p.id) // Decode the complex header query @@ -734,3 +741,29 @@ func (self *ProtocolManager) NodeInfo() *eth.EthNodeInfo { Head: self.blockchain.LastBlockHash(), } } + +func (pm *ProtocolManager) broadcastBlockLoop() { + sub := pm.eventMux.Subscribe( core.ChainEvent{}) + go func() { + for { + select { + case ev := <-sub.Chan(): + peers := pm.peers.AllPeers() + if len(peers) > 0 { + header := ev.Data.(core.ChainEvent).Block.Header() + hash := header.Hash() + number := header.Number.Uint64() + td := core.GetTd(pm.chainDb, hash, number) +fmt.Println("BROADCAST", number, hash, td) + announce := newBlockHashesData{{Hash: hash, Number: number, Td: td}} + for _, p := range peers { + go p.SendNewBlockHashes(announce) + } + } + case <-pm.quitSync: + sub.Unsubscribe() + return + } + } + }() +} \ No newline at end of file diff --git a/les/peer.go b/les/peer.go index 2c356cdf99..ea6f64a364 100644 --- a/les/peer.go +++ b/les/peer.go @@ -124,6 +124,12 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 { return p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount) } +// SendNewBlockHashes announces the availability of a number of blocks through +// a hash notification. +func (p *peer) SendNewBlockHashes(request newBlockHashesData) error { + return p2p.Send(p.rw, NewBlockHashesMsg, request) +} + // SendBlockHeaders sends a batch of block headers to the remote peer. func (p *peer) SendBlockHeaders(reqID, bv uint64, headers []*types.Header) error { return sendResponse(p.rw, BlockHeadersMsg, reqID, bv, headers) diff --git a/les/protocol.go b/les/protocol.go index dd63bd3902..ca4dd51b29 100644 --- a/les/protocol.go +++ b/les/protocol.go @@ -127,6 +127,7 @@ type statusData struct { type newBlockHashesData []struct { Hash common.Hash // Hash of one particular block being announced Number uint64 // Number of one particular block being announced + Td *big.Int // Total difficulty of one particular block being announced } // getBlockHashesData is the network packet for the hash based hash retrieval. diff --git a/les/server.go b/les/server.go index 53989269cb..95769657e5 100644 --- a/les/server.go +++ b/les/server.go @@ -39,6 +39,8 @@ func NewLesServer(eth *eth.FullNodeService, config *eth.Config) (*LesServer, err if err != nil { return nil, err } + pm.broadcastBlockLoop() + srv := &LesServer{protocolManager: pm} pm.server = srv From 4d6c54dbf6d609a1a9ce0dea69472fb96727b4a2 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Thu, 23 Jun 2016 19:26:35 +0200 Subject: [PATCH 12/32] sync 2 --- les/handler.go | 18 +++++++++++++----- les/odr_test.go | 2 +- les/protocol.go | 3 ++- les/request_test.go | 2 +- les/sync.go | 21 ++++++++------------- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/les/handler.go b/les/handler.go index 7aacdaa5e6..2189dcd471 100644 --- a/les/handler.go +++ b/les/handler.go @@ -95,7 +95,7 @@ type ProtocolManager struct { server *LesServer downloader *downloader.Downloader - //fetcher *fetcher.Fetcher + fetcher *lightFetcher peers *peerSet SubProtocols []p2p.Protocol @@ -171,6 +171,7 @@ func NewProtocolManager(chainConfig *core.ChainConfig, lightSync bool, networkId manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, blockchain.HasHeader, nil, blockchain.GetHeaderByHash, nil, blockchain.CurrentHeader, nil, nil, nil, blockchain.GetTdByHash, blockchain.InsertHeaderChain, nil, nil, blockchain.Rollback, func(id string) {}) // manager.removePeer) + manager.fetcher = newLightFetcher(odr, blockchain) } /*validator := func(block *types.Block, parent *types.Block) error { @@ -359,6 +360,9 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { return errResp(ErrDecode, "%v: %v", msg, err) } fmt.Println("RECEIVED", req[0].Number, req[0].Hash, req[0].Td) + for _, r := range req { + pm.fetcher.notify(p, r) + } case GetBlockHeadersMsg: glog.V(logger.Debug).Infof("LES: received GetBlockHeadersMsg from peer %v", p.id) @@ -452,9 +456,13 @@ fmt.Println("RECEIVED", req[0].Number, req[0].Hash, req[0].Td) return errResp(ErrDecode, "msg %v: %v", msg, err) } p.fcServer.GotReply(resp.ReqID, resp.BV) - err := pm.downloader.DeliverHeaders(p.id, resp.Headers) - if err != nil { - glog.V(logger.Debug).Infoln(err) + if pm.fetcher.requestedID(resp.ReqID) { + pm.fetcher.deliverHeaders(resp.ReqID, resp.Headers) + } else { + err := pm.downloader.DeliverHeaders(p.id, resp.Headers) + if err != nil { + glog.V(logger.Debug).Infoln(err) + } } case GetBlockBodiesMsg: @@ -757,7 +765,7 @@ func (pm *ProtocolManager) broadcastBlockLoop() { fmt.Println("BROADCAST", number, hash, td) announce := newBlockHashesData{{Hash: hash, Number: number, Td: td}} for _, p := range peers { - go p.SendNewBlockHashes(announce) + p.SendNewBlockHashes(announce) } } case <-pm.quitSync: diff --git a/les/odr_test.go b/les/odr_test.go index 08d0c64656..b70d9f5f28 100644 --- a/les/odr_test.go +++ b/les/odr_test.go @@ -191,7 +191,7 @@ func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { t.Fatalf("peer 1 handshake error: %v", err) } - lpm.synchronise(lpeer, true) + lpm.synchronise(lpeer) test := func(expFail uint64) { for i := uint64(0); i <= pm.blockchain.CurrentHeader().GetNumberU64(); i++ { diff --git a/les/protocol.go b/les/protocol.go index ca4dd51b29..352833c1a0 100644 --- a/les/protocol.go +++ b/les/protocol.go @@ -124,7 +124,8 @@ type statusData struct { } // newBlockHashesData is the network packet for the block announcements. -type newBlockHashesData []struct { +type newBlockHashesData []newBlockHashData +type newBlockHashData struct { Hash common.Hash // Hash of one particular block being announced Number uint64 // Number of one particular block being announced Td *big.Int // Total difficulty of one particular block being announced diff --git a/les/request_test.go b/les/request_test.go index 320580c6fc..0d12b835f7 100644 --- a/les/request_test.go +++ b/les/request_test.go @@ -63,7 +63,7 @@ func testAccess(t *testing.T, protocol int, fn accessTestFn) { t.Fatalf("peer 1 handshake error: %v", err) } - lpm.synchronise(lpeer, true) + lpm.synchronise(lpeer) test := func(expFail uint64) { for i := uint64(0); i <= pm.blockchain.CurrentHeader().GetNumberU64(); i++ { diff --git a/les/sync.go b/les/sync.go index e540d4289f..608e992393 100644 --- a/les/sync.go +++ b/les/sync.go @@ -44,11 +44,11 @@ func (pm *ProtocolManager) syncer() { if pm.peers.Len() < minDesiredPeerCount { break } - go pm.synchronise(pm.peers.BestPeer(), false) + go pm.synchronise(pm.peers.BestPeer()) case <-forceSync: // Force a sync even if not enough peers are present - go pm.synchronise(pm.peers.BestPeer(), false) + go pm.synchronise(pm.peers.BestPeer()) case <-pm.noMorePeers: return @@ -57,22 +57,17 @@ func (pm *ProtocolManager) syncer() { } // synchronise tries to sync up our local block chain with a remote peer. -func (pm *ProtocolManager) synchronise(peer *peer, exit bool) { +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. td := pm.blockchain.GetTdByHash(pm.blockchain.LastBlockHash()) - if peer.Td().Cmp(td) > 0 { - for { - if pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) != nil { - return - } - if exit { - return - } - time.Sleep(time.Second * 5) - } + if peer.Td().Cmp(td) <= 0 { + return + } + if pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) != nil { + return } } From 6e3dc9806dac8d2c71603d3a56ab33dae7549775 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Fri, 24 Jun 2016 19:41:23 +0200 Subject: [PATCH 13/32] sync 3 --- les/handler.go | 8 ++++++-- les/peer.go | 35 +++++++++++++++++++++++------------ les/protocol.go | 4 ++-- les/sync.go | 25 ++++++++++++++++++++----- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/les/handler.go b/les/handler.go index 2189dcd471..d3601964cb 100644 --- a/les/handler.go +++ b/les/handler.go @@ -107,6 +107,9 @@ type ProtocolManager struct { quitSync chan struct{} noMorePeers chan struct{} + syncMu sync.Mutex + syncing uint32 + // wait group is used for graceful shutdowns during downloading // and processing wg sync.WaitGroup @@ -171,7 +174,7 @@ func NewProtocolManager(chainConfig *core.ChainConfig, lightSync bool, networkId manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, blockchain.HasHeader, nil, blockchain.GetHeaderByHash, nil, blockchain.CurrentHeader, nil, nil, nil, blockchain.GetTdByHash, blockchain.InsertHeaderChain, nil, nil, blockchain.Rollback, func(id string) {}) // manager.removePeer) - manager.fetcher = newLightFetcher(odr, blockchain) + manager.fetcher = newLightFetcher(manager) } /*validator := func(block *types.Block, parent *types.Block) error { @@ -252,7 +255,8 @@ func (pm *ProtocolManager) handle(p *peer) error { // Execute the LES handshake td, head, genesis := pm.blockchain.Status() - if err := p.Handshake(td, head, genesis, pm.server); err != nil { + headNum := core.GetBlockNumber(pm.chainDb, head) + if err := p.Handshake(td, head, headNum, genesis, pm.server); err != nil { glog.V(logger.Debug).Infof("%v: handshake failed: %v", p, err) return err } diff --git a/les/peer.go b/les/peer.go index ea6f64a364..0c7920619a 100644 --- a/les/peer.go +++ b/les/peer.go @@ -54,8 +54,8 @@ type peer struct { id string - head common.Hash - td *big.Int + headInfo blockInfo + number uint64 lock sync.RWMutex knownBlocks *set.Set // Set of block hashes known to be known by this peer @@ -92,16 +92,23 @@ func (p *peer) Head() (hash common.Hash) { p.lock.RLock() defer p.lock.RUnlock() - copy(hash[:], p.head[:]) + copy(hash[:], p.headInfo.Hash[:]) return hash } +func (p *peer) headBlockInfo() blockInfo { + p.lock.RLock() + defer p.lock.RUnlock() + + return p.headInfo +} + // Td retrieves the current total difficulty of a peer. func (p *peer) Td() *big.Int { p.lock.RLock() defer p.lock.RUnlock() - return new(big.Int).Set(p.td) + return new(big.Int).Set(p.headInfo.Td) } func sendRequest(w p2p.MsgWriter, msgcode, reqID, cost uint64, data interface{}) error { @@ -273,15 +280,16 @@ func (p *peer) sendReceiveHandshake(sendList keyValueList) (keyValueList, error) // Handshake executes the les protocol handshake, negotiating version number, // network IDs, difficulties, head and genesis blocks. -func (p *peer) Handshake(td *big.Int, head common.Hash, genesis common.Hash, server *LesServer) error { +func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis common.Hash, server *LesServer) error { p.lock.Lock() defer p.lock.Unlock() var send keyValueList send = send.add("protocolVersion", uint64(p.version)) send = send.add("networkId", uint64(p.network)) - send = send.add("td", td) - send = send.add("bestHash", head) + send = send.add("headTd", td) + send = send.add("headHash", head) + send = send.add("headNum", headNum) send = send.add("genesisHash", genesis) if server != nil { send = send.add("serveHeaders", nil) @@ -300,8 +308,8 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, genesis common.Hash, ser } recv := recvList.decode() - var rGenesis, rBest common.Hash - var rVersion, rNetwork uint64 + var rGenesis, rHash common.Hash + var rVersion, rNetwork, rNum uint64 var rTd *big.Int if err := recv.get("protocolVersion", &rVersion); err != nil { @@ -310,10 +318,13 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, genesis common.Hash, ser if err := recv.get("networkId", &rNetwork); err != nil { return err } - if err := recv.get("td", &rTd); err != nil { + if err := recv.get("headTd", &rTd); err != nil { return err } - if err := recv.get("bestHash", &rBest); err != nil { + if err := recv.get("headHash", &rHash); err != nil { + return err + } + if err := recv.get("headNum", &rNum); err != nil { return err } if err := recv.get("genesisHash", &rGenesis); err != nil { @@ -359,7 +370,7 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, genesis common.Hash, ser p.fcCosts = MRC.decode() } // Configure the remote peer, and sanity check out handshake too - p.td, p.head = rTd, rBest + p.headInfo.Td, p.headInfo.Hash, p.headInfo.Number = rTd, rHash, rNum return nil } diff --git a/les/protocol.go b/les/protocol.go index 352833c1a0..30523c8413 100644 --- a/les/protocol.go +++ b/les/protocol.go @@ -124,8 +124,8 @@ type statusData struct { } // newBlockHashesData is the network packet for the block announcements. -type newBlockHashesData []newBlockHashData -type newBlockHashData struct { +type newBlockHashesData []blockInfo +type blockInfo struct { Hash common.Hash // Hash of one particular block being announced Number uint64 // Number of one particular block being announced Td *big.Int // Total difficulty of one particular block being announced diff --git a/les/sync.go b/les/sync.go index 608e992393..c47bad19ef 100644 --- a/les/sync.go +++ b/les/sync.go @@ -17,8 +17,10 @@ package les import ( + "sync/atomic" "time" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/eth/downloader" ) @@ -56,18 +58,31 @@ func (pm *ProtocolManager) syncer() { } } +func (pm *ProtocolManager) needToSync(peerHead blockInfo) bool { + head := pm.blockchain.CurrentHeader() + currentTd := core.GetTd(pm.chainDb, head.Hash(), head.Number.Uint64()) + return peerHead.Td.Cmp(currentTd) > 0 +} + // 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. - td := pm.blockchain.GetTdByHash(pm.blockchain.LastBlockHash()) - if peer.Td().Cmp(td) <= 0 { - return - } - if pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) != nil { + if !pm.needToSync(peer.headBlockInfo()) { return } + + pm.syncMu.Lock() + pm.syncWithoutLock(peer) + pm.syncMu.Unlock() +} + +func (pm *ProtocolManager) syncWithoutLock(peer *peer) { + atomic.StoreUint32(&pm.syncing, 1) + pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) + atomic.StoreUint32(&pm.syncing, 0) } From 12d25262e954c99a69228a72488517ec98e0205e Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sat, 25 Jun 2016 14:49:08 +0200 Subject: [PATCH 14/32] sync 123 --- les/handler.go | 3 ++- les/sync.go | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/les/handler.go b/les/handler.go index d3601964cb..d04299b262 100644 --- a/les/handler.go +++ b/les/handler.go @@ -108,7 +108,8 @@ type ProtocolManager struct { noMorePeers chan struct{} syncMu sync.Mutex - syncing uint32 + syncing bool + syncDone chan struct{} // wait group is used for graceful shutdowns during downloading // and processing diff --git a/les/sync.go b/les/sync.go index c47bad19ef..828ff2ae84 100644 --- a/les/sync.go +++ b/les/sync.go @@ -17,7 +17,6 @@ package les import ( - "sync/atomic" "time" "github.com/ethereum/go-ethereum/core" @@ -76,13 +75,48 @@ func (pm *ProtocolManager) synchronise(peer *peer) { return } + pm.waitSyncLock() + pm.syncWithLockAcquired(peer) +} + +func (pm *ProtocolManager) waitSyncLock() { + for { + chn := pm.getSyncLock(true) + if chn == nil { + break + } + <-chn + } +} + +// getSyncLock either acquires the sync lock and returns nil or returns a channel +// which is closed when the lock is free again +func (pm *ProtocolManager) getSyncLock(acquire bool) chan struct{} { pm.syncMu.Lock() - pm.syncWithoutLock(peer) + defer pm.syncMu.Unlock() + + if pm.syncing { + if pm.syncDone == nil { + pm.syncDone = make(chan struct{}) + } + return pm.syncDone + } else { + pm.syncing = acquire + return nil + } +} + +func (pm *ProtocolManager) releaseSyncLock() { + pm.syncMu.Lock() + pm.syncing = false + if pm.syncDone != nil { + close(pm.syncDone) + pm.syncDone = nil + } pm.syncMu.Unlock() } -func (pm *ProtocolManager) syncWithoutLock(peer *peer) { - atomic.StoreUint32(&pm.syncing, 1) +func (pm *ProtocolManager) syncWithLockAcquired(peer *peer) { pm.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync) - atomic.StoreUint32(&pm.syncing, 0) + pm.releaseSyncLock() } From ba39d7bab73d2e9c2d1c2cb40085390e7bf921c1 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sat, 25 Jun 2016 17:18:49 +0200 Subject: [PATCH 15/32] disable peer drop, fixed td check --- les/backend.go | 1 - les/handler.go | 13 ++++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/les/backend.go b/les/backend.go index 0b9276ec2e..8b2e006d88 100644 --- a/les/backend.go +++ b/les/backend.go @@ -122,7 +122,6 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightNodeService, error if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.LightMode, config.NetworkId, eth.eventMux, eth.pow, eth.blockchain, nil, chainDb, odr, relay); err != nil { return nil, err } - odr.removePeer = eth.protocolManager.removePeer eth.ApiBackend = &LesApiBackend{eth, nil} eth.ApiBackend.gpo = gasprice.NewLightPriceOracle(eth.ApiBackend) diff --git a/les/handler.go b/les/handler.go index d04299b262..6ccbdeed91 100644 --- a/les/handler.go +++ b/les/handler.go @@ -51,6 +51,8 @@ const ( MaxReceiptFetch = 128 // Amount of transaction receipts to allow fetching per request MaxCodeFetch = 64 // Amount of contract codes to allow fetching per request MaxProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request + + disableClientRemovePeer = true ) // errIncompatibleConfig is returned if the requested protocols and configs are @@ -170,14 +172,23 @@ func NewProtocolManager(chainConfig *core.ChainConfig, lightSync bool, networkId return nil, errIncompatibleConfig } + removePeer := manager.removePeer + if disableClientRemovePeer { + removePeer = func(id string) {} + } + if lightSync { glog.V(logger.Debug).Infof("LES: create downloader") manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, blockchain.HasHeader, nil, blockchain.GetHeaderByHash, nil, blockchain.CurrentHeader, nil, nil, nil, blockchain.GetTdByHash, - blockchain.InsertHeaderChain, nil, nil, blockchain.Rollback, func(id string) {}) // manager.removePeer) + blockchain.InsertHeaderChain, nil, nil, blockchain.Rollback, removePeer) manager.fetcher = newLightFetcher(manager) } + if odr != nil { + odr.removePeer = removePeer + } + /*validator := func(block *types.Block, parent *types.Block) error { return core.ValidateHeader(pow, block.Header(), parent.Header(), true, false) } From ce438acb08c60f41921751efb7a3817fe177a06a Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sat, 25 Jun 2016 17:50:21 +0200 Subject: [PATCH 16/32] fetcher --- les/fetcher.go | 207 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 les/fetcher.go diff --git a/les/fetcher.go b/les/fetcher.go new file mode 100644 index 0000000000..f7595cc789 --- /dev/null +++ b/les/fetcher.go @@ -0,0 +1,207 @@ +// 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 . + +// Package les implements the Light Ethereum Subprotocol. +package les + +import ( +"fmt" + "sync" + "time" + + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" +) + +type lightFetcher struct{ + pm *ProtocolManager + odr *LesOdr + chain BlockChain + reqMu sync.RWMutex + requested map[uint64]chan *types.Header + syncPoolMu sync.Mutex + syncPool map[*peer]struct{} + syncPoolNotify chan struct{} +} + +func newLightFetcher(pm *ProtocolManager) *lightFetcher { + f := &lightFetcher{ + pm: pm, + chain: pm.blockchain, + odr: pm.odr, + requested: make(map[uint64]chan *types.Header), + syncPool: make(map[*peer]struct{}), + syncPoolNotify: make(chan struct{}), + } + go f.syncLoop() + return f +} + +func (f *lightFetcher) requestedID(reqID uint64) bool { + f.reqMu.RLock() + _, ok := f.requested[reqID] + f.reqMu.RUnlock() + return ok +} + +func (f *lightFetcher) deliverHeaders(reqID uint64, headers []*types.Header) { + f.reqMu.Lock() + chn := f.requested[reqID] + if len(headers) == 1 { + chn <- headers[0] + } else { + chn <- nil + } + close(chn) + delete(f.requested, reqID) + f.reqMu.Unlock() +} + +func (f *lightFetcher) notify(p *peer, block blockInfo) { + p.lock.Lock() + if block.Td.Cmp(p.headInfo.Td) <= 0 { + p.lock.Unlock() + return + } + p.headInfo = block + p.lock.Unlock() + + head := f.pm.blockchain.CurrentHeader() + currentTd := core.GetTd(f.pm.chainDb, head.Hash(), head.Number.Uint64()) + if block.Td.Cmp(currentTd) > 0 { + f.syncPoolMu.Lock() + f.syncPool[p] = struct{}{} + f.syncPoolMu.Unlock() + f.syncPoolNotify <- struct{}{} + } +} + +func (f *lightFetcher) fetchBestFromPool() *peer { + head := f.pm.blockchain.CurrentHeader() + currentTd := core.GetTd(f.pm.chainDb, head.Hash(), head.Number.Uint64()) + + f.syncPoolMu.Lock() + var best *peer + for p, _ := range f.syncPool { + td := p.Td() + if td.Cmp(currentTd) <= 0 { + delete(f.syncPool, p) + } else { + if best == nil || td.Cmp(best.Td()) > 0 { + best = p + } + } + } + if best != nil { + delete(f.syncPool, best) + } + f.syncPoolMu.Unlock() + return best +} + +func (f *lightFetcher) syncLoop() { + for { + select { + case <-f.syncPoolNotify: + chn := f.pm.getSyncLock(false) + if chn != nil { + go func() { + <-chn + f.syncPoolNotify <- struct{}{} + }() + } else { + if p := f.fetchBestFromPool(); p != nil { + go f.syncWithPeer(p) + go func() { + time.Sleep(softRequestTimeout) + f.syncPoolNotify <- struct{}{} + }() + } + } + case <-f.pm.quitSync: + return + } + } +} + +func (f *lightFetcher) syncWithPeer(p *peer) bool { + headNum := f.chain.CurrentHeader().Number.Uint64() + peerHead := p.headBlockInfo() + + if !f.pm.needToSync(peerHead) { + return true + } + + if peerHead.Number <= headNum+1 { + var header *types.Header + reqID, chn := f.request(p, peerHead) + select { + case header = <-chn: + if header == nil || header.Hash() != peerHead.Hash || + header.Number.Uint64() != peerHead.Number { + // missing or wrong header returned +fmt.Println("removePeer 1") + f.pm.removePeer(p.id) + return false + } + + case <-time.After(hardRequestTimeout): +fmt.Println("removePeer 2") + f.pm.removePeer(p.id) + f.reqMu.Lock() + close(f.requested[reqID]) + delete(f.requested, reqID) + f.reqMu.Unlock() + return false + } + + // got the header, try to insert + f.chain.InsertHeaderChain([]*types.Header{header}, 1) + + defer func() { + // check header td at the end of syncing, drop peer if it was fake + headerTd := core.GetTd(f.pm.chainDb, header.Hash(), header.Number.Uint64()) + if headerTd != nil && headerTd.Cmp(peerHead.Td) != 0 { +fmt.Println("removePeer 3") + f.pm.removePeer(p.id) + } + }() + if !f.pm.needToSync(peerHead) { + return true + } + } + + f.pm.waitSyncLock() + if !f.pm.needToSync(peerHead) { + // synced up by the one we've been waiting to end + f.pm.releaseSyncLock() + return true + } + f.pm.syncWithLockAcquired(p) + return !f.pm.needToSync(peerHead) +} + +func (f *lightFetcher) request(p *peer, block blockInfo) (uint64, chan *types.Header) { + reqID := f.odr.getNextReqID() + f.reqMu.Lock() + chn := make(chan *types.Header, 1) + f.requested[reqID] = chn + f.reqMu.Unlock() + cost := p.GetRequestCost(GetBlockHeadersMsg, 1) + p.fcServer.SendRequest(reqID, cost) + p.RequestHeadersByHash(reqID, cost, block.Hash, 1, 0, false) + return reqID, chn +} From 785439d088ee48c74b5bb70c69ce66d031274c3f Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sat, 25 Jun 2016 18:34:53 +0200 Subject: [PATCH 17/32] using core events, fixed light block filter --- eth/filters/api.go | 8 +++++++- eth/filters/filter_system.go | 3 +++ light/events.go | 33 --------------------------------- light/lightchain.go | 10 +++++----- light/txpool.go | 4 ++-- 5 files changed, 17 insertions(+), 41 deletions(-) delete mode 100644 light/events.go diff --git a/eth/filters/api.go b/eth/filters/api.go index b9783031fb..311cd146e5 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -138,6 +138,7 @@ done: // NewBlockFilter create a new filter that returns blocks that are included into the canonical chain. func (s *PublicFilterAPI) NewBlockFilter() (string, error) { +fmt.Println("NewBlockFilter") externalId, err := newFilterId() if err != nil { return "", err @@ -155,8 +156,10 @@ func (s *PublicFilterAPI) NewBlockFilter() (string, error) { filter.BlockCallback = func(block *types.Block, logs vm.Logs) { s.blockMu.Lock() defer s.blockMu.Unlock() +fmt.Println("callback") if queue := s.blockQueue[id]; queue != nil { +fmt.Println(" add") queue.add(block.Hash()) } } @@ -498,8 +501,11 @@ func (s *PublicFilterAPI) blockFilterChanged(id int) []common.Hash { s.blockMu.Lock() defer s.blockMu.Unlock() +fmt.Println("blockFilterChanged") if s.blockQueue[id] != nil { - return s.blockQueue[id].get() + res := s.blockQueue[id].get() +fmt.Println(" len = ", len(res)) + return res } return nil } diff --git a/eth/filters/filter_system.go b/eth/filters/filter_system.go index 4343dfa21c..104f6e55ae 100644 --- a/eth/filters/filter_system.go +++ b/eth/filters/filter_system.go @@ -134,9 +134,12 @@ func (fs *FilterSystem) filterLoop() { for event := range fs.sub.Chan() { switch ev := event.Data.(type) { case core.ChainEvent: +fmt.Println("ChainEvent") fs.filterMu.RLock() for _, filter := range fs.chainFilters { +fmt.Println("1") if filter.BlockCallback != nil && !filter.created.After(event.Time) { +fmt.Println("2") filter.BlockCallback(ev.Block, ev.Logs) } } diff --git a/light/events.go b/light/events.go deleted file mode 100644 index 28d91e31d4..0000000000 --- a/light/events.go +++ /dev/null @@ -1,33 +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 . - -package light - -import ( - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" -) - -type LightChainSplitEvent struct{ Header *types.Header } - -type LightChainEvent struct { - Header *types.Header - Hash common.Hash -} - -type LightChainSideEvent struct{ Header *types.Header } - -type LightChainHeadEvent struct{ Header *types.Header } diff --git a/light/lightchain.go b/light/lightchain.go index a70cb4493a..f6a1202807 100644 --- a/light/lightchain.go +++ b/light/lightchain.go @@ -338,9 +338,9 @@ func (self *LightChain) Rollback(chain []common.Hash) { // posts them into the event mux. func (self *LightChain) postChainEvents(events []interface{}) { for _, event := range events { - if event, ok := event.(LightChainEvent); ok { + if event, ok := event.(core.ChainEvent); ok { if self.LastBlockHash() == event.Hash { - self.eventMux.Post(LightChainHeadEvent{event.Header}) + self.eventMux.Post(core.ChainHeadEvent{Block: event.Block}) } } // Fire the insertion events individually too @@ -379,16 +379,16 @@ func (self *LightChain) InsertHeaderChain(chain []*types.Header, checkFreq int) if glog.V(logger.Debug) { glog.Infof("[%v] inserted header #%d (%x...).\n", time.Now().UnixNano(), header.Number, header.Hash().Bytes()[0:4]) } - events = append(events, LightChainEvent{header, header.Hash()}) + events = append(events, core.ChainEvent{Block: types.NewBlockWithHeader(header), Hash: header.Hash()}) case core.SideStatTy: if glog.V(logger.Detail) { glog.Infof("inserted forked header #%d (TD=%v) (%x...).\n", header.Number, header.Difficulty, header.Hash().Bytes()[0:4]) } - events = append(events, LightChainSideEvent{header}) + events = append(events, core.ChainSideEvent{Block: types.NewBlockWithHeader(header)}) case core.SplitStatTy: - events = append(events, LightChainSplitEvent{header}) + events = append(events, core.ChainSplitEvent{Block: types.NewBlockWithHeader(header)}) } return err diff --git a/light/txpool.go b/light/txpool.go index b1e3f92e6a..15a2c540b4 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -84,7 +84,7 @@ func NewTxPool(config *core.ChainConfig, eventMux *event.TypeMux, chain *LightCh mined: make(map[common.Hash][]*types.Transaction), quit: make(chan bool), eventMux: eventMux, - events: eventMux.Subscribe(LightChainHeadEvent{}), + events: eventMux.Subscribe(core.ChainHeadEvent{}), chain: chain, relay: relay, odr: chain.Odr(), @@ -274,7 +274,7 @@ const blockCheckTimeout = time.Second * 3 func (pool *TxPool) eventLoop() { for ev := range pool.events.Chan() { switch ev.Data.(type) { - case LightChainHeadEvent: + case core.ChainHeadEvent: pool.mu.Lock() ctx, _ := context.WithTimeout(context.Background(), blockCheckTimeout) head := pool.chain.CurrentHeader() From f085237df30f7f6e955c7c395d44ff75fd4add2d Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 02:10:49 +0200 Subject: [PATCH 18/32] eth: hack to limit number of eth peers --- eth/handler.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/eth/handler.go b/eth/handler.go index 8d5088a0cc..e8282597b3 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -249,6 +249,10 @@ func (pm *ProtocolManager) newPeer(pv int, p *p2p.Peer, rw p2p.MsgReadWriter) *p // 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 { + if pm.peers.Len() >= 20 { + return p2p.DiscTooManyPeers + } + glog.V(logger.Debug).Infof("%v: peer connected [%s]", p, p.Name()) // Execute the Ethereum handshake From c48b7caf6a1928a522a4b705f28a7e48438cb183 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 10:46:23 +0200 Subject: [PATCH 19/32] fixed newPeerCh --- les/handler.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/les/handler.go b/les/handler.go index 6ccbdeed91..741d401efb 100644 --- a/les/handler.go +++ b/les/handler.go @@ -133,7 +133,7 @@ func NewProtocolManager(chainConfig *core.ChainConfig, lightSync bool, networkId txrelay: txrelay, odr: odr, peers: newPeerSet(), - newPeerCh: make(chan *peer, 1), + newPeerCh: make(chan *peer), quitSync: make(chan struct{}), noMorePeers: make(chan struct{}), } @@ -152,6 +152,8 @@ func NewProtocolManager(chainConfig *core.ChainConfig, lightSync bool, networkId case manager.newPeerCh <- peer: manager.wg.Add(1) defer manager.wg.Done() + fmt.Println("enter handler", p.ID()) + defer fmt.Println("exit handler", p.ID()) return manager.handle(peer) case <-manager.quitSync: return p2p.DiscQuitting @@ -230,6 +232,10 @@ func (pm *ProtocolManager) Start() { if pm.lightSync { // start sync handler go pm.syncer() + } else { + go func() { + for range pm.newPeerCh {} + }() } } @@ -268,10 +274,13 @@ func (pm *ProtocolManager) handle(p *peer) error { // Execute the LES handshake td, head, genesis := pm.blockchain.Status() headNum := core.GetBlockNumber(pm.chainDb, head) +fmt.Println("handshake") if err := p.Handshake(td, head, headNum, genesis, pm.server); err != nil { glog.V(logger.Debug).Infof("%v: handshake failed: %v", p, err) +fmt.Println(" err:", err) return err } +fmt.Println(" done") if rw, ok := p.rw.(*meteredMsgReadWriter); ok { rw.Init(p.version) } @@ -316,8 +325,10 @@ func (pm *ProtocolManager) handle(p *peer) error { // main loop. handle incoming messages. for { +fmt.Println("handleMsg") if err := pm.handleMsg(p); err != nil { glog.V(logger.Debug).Infof("%v: message handling failed: %v", p, err) +fmt.Println(" err:", err) return err } } From 64b136b9961c45bd5f10d23fa3b26d3ba36602fe Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 11:09:09 +0200 Subject: [PATCH 20/32] flow control deadlock fix --- les/flowcontrol/manager.go | 2 +- les/odr_peerset.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/les/flowcontrol/manager.go b/les/flowcontrol/manager.go index 9d2ea8067f..c0469e7b66 100644 --- a/les/flowcontrol/manager.go +++ b/les/flowcontrol/manager.go @@ -190,8 +190,8 @@ func (self *ClientManager) accept(node *cmNode, time int64) bool { self.update(time) if !self.canStartReq() { resume := make(chan bool) - self.resumeQueue <- resume self.lock.Unlock() + self.resumeQueue <- resume <-resume self.lock.Lock() if _, ok := self.nodes[node]; !ok { diff --git a/les/odr_peerset.go b/les/odr_peerset.go index ff6a223543..0323ce27fc 100644 --- a/les/odr_peerset.go +++ b/les/odr_peerset.go @@ -67,7 +67,7 @@ func (ps *odrPeerSet) unregister(p *peer) error { func (ps *odrPeerSet) peerPriority(p *peer, info *odrPeerInfo, req LesOdrRequest) uint64 { tm := p.fcServer.CanSend(req.GetCost(p)) - if info.reqCnt > 0 { + if info.reqTimeCnt > 0 { tm += info.reqTimeSum / info.reqTimeCnt } return tm From a09e88d4bfee6e147246cd5f4eef5340eaadd360 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 11:25:34 +0200 Subject: [PATCH 21/32] disable drop, remove prints --- eth/filters/api.go | 5 ----- eth/filters/filter_system.go | 3 --- les/fetcher.go | 4 +++- les/handler.go | 3 +-- 4 files changed, 4 insertions(+), 11 deletions(-) diff --git a/eth/filters/api.go b/eth/filters/api.go index 311cd146e5..fd1cbd9137 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -138,7 +138,6 @@ done: // NewBlockFilter create a new filter that returns blocks that are included into the canonical chain. func (s *PublicFilterAPI) NewBlockFilter() (string, error) { -fmt.Println("NewBlockFilter") externalId, err := newFilterId() if err != nil { return "", err @@ -156,10 +155,8 @@ fmt.Println("NewBlockFilter") filter.BlockCallback = func(block *types.Block, logs vm.Logs) { s.blockMu.Lock() defer s.blockMu.Unlock() -fmt.Println("callback") if queue := s.blockQueue[id]; queue != nil { -fmt.Println(" add") queue.add(block.Hash()) } } @@ -501,10 +498,8 @@ func (s *PublicFilterAPI) blockFilterChanged(id int) []common.Hash { s.blockMu.Lock() defer s.blockMu.Unlock() -fmt.Println("blockFilterChanged") if s.blockQueue[id] != nil { res := s.blockQueue[id].get() -fmt.Println(" len = ", len(res)) return res } return nil diff --git a/eth/filters/filter_system.go b/eth/filters/filter_system.go index 104f6e55ae..4343dfa21c 100644 --- a/eth/filters/filter_system.go +++ b/eth/filters/filter_system.go @@ -134,12 +134,9 @@ func (fs *FilterSystem) filterLoop() { for event := range fs.sub.Chan() { switch ev := event.Data.(type) { case core.ChainEvent: -fmt.Println("ChainEvent") fs.filterMu.RLock() for _, filter := range fs.chainFilters { -fmt.Println("1") if filter.BlockCallback != nil && !filter.created.After(event.Time) { -fmt.Println("2") filter.BlockCallback(ev.Block, ev.Logs) } } diff --git a/les/fetcher.go b/les/fetcher.go index f7595cc789..f8c997162d 100644 --- a/les/fetcher.go +++ b/les/fetcher.go @@ -159,8 +159,10 @@ fmt.Println("removePeer 1") } case <-time.After(hardRequestTimeout): + if !disableClientRemovePeer { fmt.Println("removePeer 2") - f.pm.removePeer(p.id) + f.pm.removePeer(p.id) + } f.reqMu.Lock() close(f.requested[reqID]) delete(f.requested, reqID) diff --git a/les/handler.go b/les/handler.go index 741d401efb..2e88f74f19 100644 --- a/les/handler.go +++ b/les/handler.go @@ -325,10 +325,9 @@ fmt.Println(" done") // main loop. handle incoming messages. for { -fmt.Println("handleMsg") if err := pm.handleMsg(p); err != nil { glog.V(logger.Debug).Infof("%v: message handling failed: %v", p, err) -fmt.Println(" err:", err) +fmt.Println("handleMsg err:", err) return err } } From 78a077372c432e8d6c626f3851dae7ee4ec45b55 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 11:41:20 +0200 Subject: [PATCH 22/32] light fetcher shutdown --- les/fetcher.go | 7 +++++-- les/sync.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/les/fetcher.go b/les/fetcher.go index f8c997162d..ac6fb0021c 100644 --- a/les/fetcher.go +++ b/les/fetcher.go @@ -115,6 +115,8 @@ func (f *lightFetcher) fetchBestFromPool() *peer { func (f *lightFetcher) syncLoop() { for { select { + case <-f.pm.quitSync: + return case <-f.syncPoolNotify: chn := f.pm.getSyncLock(false) if chn != nil { @@ -131,13 +133,14 @@ func (f *lightFetcher) syncLoop() { }() } } - case <-f.pm.quitSync: - return } } } func (f *lightFetcher) syncWithPeer(p *peer) bool { + f.pm.wg.Add(1) + defer f.pm.wg.Done() + headNum := f.chain.CurrentHeader().Number.Uint64() peerHead := p.headBlockInfo() diff --git a/les/sync.go b/les/sync.go index 828ff2ae84..9407df125c 100644 --- a/les/sync.go +++ b/les/sync.go @@ -60,7 +60,7 @@ func (pm *ProtocolManager) syncer() { func (pm *ProtocolManager) needToSync(peerHead blockInfo) bool { head := pm.blockchain.CurrentHeader() currentTd := core.GetTd(pm.chainDb, head.Hash(), head.Number.Uint64()) - return peerHead.Td.Cmp(currentTd) > 0 + return currentTd != nil && peerHead.Td.Cmp(currentTd) > 0 } // synchronise tries to sync up our local block chain with a remote peer. From 74009742739649695c0fafd1e6ea37d73d225e36 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 12:59:13 +0200 Subject: [PATCH 23/32] broadcast ChainHeadEvents, removed prints --- les/handler.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/les/handler.go b/les/handler.go index 2e88f74f19..7195c64e78 100644 --- a/les/handler.go +++ b/les/handler.go @@ -152,8 +152,6 @@ func NewProtocolManager(chainConfig *core.ChainConfig, lightSync bool, networkId case manager.newPeerCh <- peer: manager.wg.Add(1) defer manager.wg.Done() - fmt.Println("enter handler", p.ID()) - defer fmt.Println("exit handler", p.ID()) return manager.handle(peer) case <-manager.quitSync: return p2p.DiscQuitting @@ -274,13 +272,10 @@ func (pm *ProtocolManager) handle(p *peer) error { // Execute the LES handshake td, head, genesis := pm.blockchain.Status() headNum := core.GetBlockNumber(pm.chainDb, head) -fmt.Println("handshake") if err := p.Handshake(td, head, headNum, genesis, pm.server); err != nil { glog.V(logger.Debug).Infof("%v: handshake failed: %v", p, err) -fmt.Println(" err:", err) return err } -fmt.Println(" done") if rw, ok := p.rw.(*meteredMsgReadWriter); ok { rw.Init(p.version) } @@ -385,7 +380,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { if err := msg.Decode(&req); err != nil { return errResp(ErrDecode, "%v: %v", msg, err) } -fmt.Println("RECEIVED", req[0].Number, req[0].Hash, req[0].Td) +//fmt.Println("RECEIVED", req[0].Number, req[0].Hash, req[0].Td) for _, r := range req { pm.fetcher.notify(p, r) } @@ -777,18 +772,18 @@ func (self *ProtocolManager) NodeInfo() *eth.EthNodeInfo { } func (pm *ProtocolManager) broadcastBlockLoop() { - sub := pm.eventMux.Subscribe( core.ChainEvent{}) + sub := pm.eventMux.Subscribe( core.ChainHeadEvent{}) go func() { for { select { case ev := <-sub.Chan(): peers := pm.peers.AllPeers() if len(peers) > 0 { - header := ev.Data.(core.ChainEvent).Block.Header() + header := ev.Data.(core.ChainHeadEvent).Block.Header() hash := header.Hash() number := header.Number.Uint64() td := core.GetTd(pm.chainDb, hash, number) -fmt.Println("BROADCAST", number, hash, td) +//fmt.Println("BROADCAST", number, hash, td) announce := newBlockHashesData{{Hash: hash, Number: number, Td: td}} for _, p := range peers { p.SendNewBlockHashes(announce) From 02d4975edf95d86c973e7178c02080bdda886bc1 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 15:20:02 +0200 Subject: [PATCH 24/32] txpoolcontent --- les/api_backend.go | 2 +- light/txpool.go | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/les/api_backend.go b/les/api_backend.go index f07d631c79..d2afe9818b 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -116,7 +116,7 @@ func (b *LesApiBackend) Stats() (pending int, queued int) { } func (b *LesApiBackend) TxPoolContent() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) { - return make(map[common.Address]map[uint64][]*types.Transaction), make(map[common.Address]map[uint64][]*types.Transaction) + return b.eth.txPool.Content() } func (b *LesApiBackend) Downloader() *downloader.Downloader { diff --git a/light/txpool.go b/light/txpool.go index 15a2c540b4..5b2e8d58dd 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -469,8 +469,8 @@ func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction { // GetTransactions returns all currently processable transactions. // The returned slice may be modified by the caller. func (self *TxPool) GetTransactions() (txs types.Transactions) { - self.mu.Lock() - defer self.mu.Unlock() + self.mu.RLock() + defer self.mu.RUnlock() txs = make(types.Transactions, len(self.pending)) i := 0 @@ -481,6 +481,29 @@ func (self *TxPool) GetTransactions() (txs types.Transactions) { return txs } +// Content retrieves the data content of the transaction pool, returning all the +// pending as well as queued transactions, grouped by account and nonce. +func (self *TxPool) Content() (map[common.Address]map[uint64][]*types.Transaction, map[common.Address]map[uint64][]*types.Transaction) { + self.mu.RLock() + defer self.mu.RUnlock() + + // Retrieve all the pending transactions and sort by account and by nonce + pending := make(map[common.Address]map[uint64][]*types.Transaction) + for _, tx := range self.pending { + account, _ := tx.From() + + owned, ok := pending[account] + if !ok { + owned = make(map[uint64][]*types.Transaction) + pending[account] = owned + } + owned[tx.Nonce()] = append(owned[tx.Nonce()], tx) + } + // There are no queued transactions in a light pool, just return an empty map + queued := make(map[common.Address]map[uint64][]*types.Transaction) + return pending, queued +} + // RemoveTransactions removes all given transactions from the pool. func (self *TxPool) RemoveTransactions(txs types.Transactions) { self.mu.Lock() From 4ad808f4dcf1077acf712457c64874e344c87f54 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Sun, 26 Jun 2016 17:57:16 +0200 Subject: [PATCH 25/32] using monotonic clock --- .../aristanetworks/goarista/AUTHORS | 25 +++ .../aristanetworks/goarista/COPYING | 177 ++++++++++++++++++ .../aristanetworks/goarista/README.md | 66 +++++++ .../goarista/atime/issue15006.s | 6 + .../aristanetworks/goarista/atime/nanotime.go | 26 +++ .../goarista/atime/nanotime_test.go | 22 +++ common/mclock/mclock.go | 30 +++ les/flowcontrol/control.go | 4 +- les/odr.go | 7 +- 9 files changed, 359 insertions(+), 4 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/aristanetworks/goarista/AUTHORS create mode 100644 Godeps/_workspace/src/github.com/aristanetworks/goarista/COPYING create mode 100644 Godeps/_workspace/src/github.com/aristanetworks/goarista/README.md create mode 100644 Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/issue15006.s create mode 100644 Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime.go create mode 100644 Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime_test.go create mode 100644 common/mclock/mclock.go diff --git a/Godeps/_workspace/src/github.com/aristanetworks/goarista/AUTHORS b/Godeps/_workspace/src/github.com/aristanetworks/goarista/AUTHORS new file mode 100644 index 0000000000..5bb93cb3fa --- /dev/null +++ b/Godeps/_workspace/src/github.com/aristanetworks/goarista/AUTHORS @@ -0,0 +1,25 @@ +All contributors are required to sign a "Contributor License Agreement" at + + +The following organizations and people have contributed code to this library. +(Please keep both lists sorted alphabetically.) + + +Arista Networks, Inc. + + +Benoit Sigoure +Fabrice Rabaute + + + +The list of individual contributors for code currently in HEAD can be obtained +at any time with the following script: + +find . -type f \ +| while read i; do \ + git blame -t $i 2>/dev/null; \ + done \ +| sed 's/^[0-9a-f]\{8\} [^(]*(\([^)]*\) [-+0-9 ]\{14,\}).*/\1/;s/ *$//' \ +| awk '{a[$0]++; t++} END{for(n in a) print n}' \ +| sort diff --git a/Godeps/_workspace/src/github.com/aristanetworks/goarista/COPYING b/Godeps/_workspace/src/github.com/aristanetworks/goarista/COPYING new file mode 100644 index 0000000000..f433b1a53f --- /dev/null +++ b/Godeps/_workspace/src/github.com/aristanetworks/goarista/COPYING @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/Godeps/_workspace/src/github.com/aristanetworks/goarista/README.md b/Godeps/_workspace/src/github.com/aristanetworks/goarista/README.md new file mode 100644 index 0000000000..9ad95ba9aa --- /dev/null +++ b/Godeps/_workspace/src/github.com/aristanetworks/goarista/README.md @@ -0,0 +1,66 @@ +# Arista Go library [![Build Status](https://travis-ci.org/aristanetworks/goarista.svg?branch=master)](https://travis-ci.org/aristanetworks/goarista) [![codecov.io](http://codecov.io/github/aristanetworks/goarista/coverage.svg?branch=master)](http://codecov.io/github/aristanetworks/goarista?branch=master) [![GoDoc](https://godoc.org/github.com/aristanetworks/goarista?status.png)](https://godoc.org/github.com/aristanetworks/goarista) [![Go Report Card](https://goreportcard.com/badge/github.com/aristanetworks/goarista)](https://goreportcard.com/report/github.com/aristanetworks/goarista) + +## areflect + +Helper functions to work with the `reflect` package. Contains +`ForceExport()`, which bypasses the check in `reflect.Value` that +prevents accessing unexported attributes. + +## atime + +Provides access to a fast monotonic clock source, to fill in the gap in the +[Go standard library, which lacks one](https://github.com/golang/go/issues/12914). +Don't use `time.Now()` in code that needs to time things or otherwise assume +that time passes at a constant rate, instead use `atime.Nanotime()`. + +## cmd + +### occli + +Simple CLI client for the OpenConfig gRPC interface that prints the response +protobufs in text form or JSON. + +### ockafka + +Client for the OpenConfig gRPC interface that publishes updates to Kafka. + +### ocredis + +Client for the OpenConfig gRPC interface that publishes updates to Redis +using both [Redis' hashes](http://redis.io/topics/data-types-intro#hashes) +(one per container / entity / collection) and [Redis' Pub/Sub](http://redis.io/topics/pubsub) +mechanism, so that one can [subscribe](http://redis.io/commands/subscribe) to +incoming updates being applied on the hash maps. + +## dscp + +Provides `ListenTCPWithTOS()`, which is a replacement for `net.ListenTCP()` +that allows specifying the ToS (Type of Service), to specify DSCP / ECN / +class of service flags to use for incoming connections. + +## key + +Provides a common type used across various Arista projects, named `key.Key`, +which is used to work around the fact that Go can't let one +use a non-hashable type as a key to a `map`, and we sometimes need to use +a `map[string]interface{}` (or something containing one) as a key to maps. +As a result, we frequently use `map[key.Key]interface{}` instead of just +`map[interface{}]interface{}` when we need a generic key-value collection. + +## monitor + +A library to help expose monitoring metrics on top of the +[`expvar`](https://golang.org/pkg/expvar/) infrastructure. + +## netns + +`netns.Do(namespace, cb)` provides a handy mechanism to execute the given +callback `cb` in the given [network namespace](https://lwn.net/Articles/580893/). + +## test + +This is a [Go](http://golang.org/) library to help in writing unit tests. + +## Examples + +TBD diff --git a/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/issue15006.s b/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/issue15006.s new file mode 100644 index 0000000000..66109f4f31 --- /dev/null +++ b/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/issue15006.s @@ -0,0 +1,6 @@ +// Copyright (C) 2016 Arista Networks, Inc. +// Use of this source code is governed by the Apache License 2.0 +// that can be found in the COPYING file. + +// This file is intentionally empty. +// It's a workaround for https://github.com/golang/go/issues/15006 diff --git a/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime.go b/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime.go new file mode 100644 index 0000000000..1ede420485 --- /dev/null +++ b/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime.go @@ -0,0 +1,26 @@ +// Copyright (C) 2016 Arista Networks, Inc. +// Use of this source code is governed by the Apache License 2.0 +// that can be found in the COPYING file. + +// Package atime provides a fast monotonic clock source. +package atime + +import "unsafe" + +// Make goimports import the unsafe package, which is required to be able +// to use //go:linkname +var _ = unsafe.Sizeof(0) + +//go:noescape +//go:linkname nanotime runtime.nanotime +func nanotime() int64 + +// NanoTime returns the current time in nanoseconds from a monotonic clock. +// The time returned is based on some arbitrary platform-specific point in the +// past. The time returned is guaranteed to increase monotonically at a +// constant rate, unlike time.Now() from the Go standard library, which may +// slow down, speed up, jump forward or backward, due to NTP activity or leap +// seconds. +func NanoTime() uint64 { + return uint64(nanotime()) +} diff --git a/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime_test.go b/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime_test.go new file mode 100644 index 0000000000..15d53421f4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/aristanetworks/goarista/atime/nanotime_test.go @@ -0,0 +1,22 @@ +// Copyright (C) 2016 Arista Networks, Inc. +// Use of this source code is governed by the Apache License 2.0 +// that can be found in the COPYING file. + +// Package atime provides a fast monotonic clock source. +package atime_test + +import ( + "testing" + + . "github.com/aristanetworks/goarista/atime" +) + +func TestNanoTime(t *testing.T) { + for i := 0; i < 100; i++ { + t1 := NanoTime() + t2 := NanoTime() + if t1 >= t2 { + t.Fatalf("t1=%d should have been strictly less than t2=%d", t1, t2) + } + } +} diff --git a/common/mclock/mclock.go b/common/mclock/mclock.go new file mode 100644 index 0000000000..7604293774 --- /dev/null +++ b/common/mclock/mclock.go @@ -0,0 +1,30 @@ +// 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 . + +// package mclock is a wrapper for a monotonic clock source +package mclock + +import ( + "time" + + "github.com/aristanetworks/goarista/atime" +) + +type AbsTime time.Duration // absolute monotonic time + +func Now() AbsTime { + return AbsTime(atime.NanoTime()) +} diff --git a/les/flowcontrol/control.go b/les/flowcontrol/control.go index c1ed03eb79..25c5b3688d 100644 --- a/les/flowcontrol/control.go +++ b/les/flowcontrol/control.go @@ -20,6 +20,8 @@ package flowcontrol import ( "sync" "time" + + "github.com/ethereum/go-ethereum/common/mclock" ) const fcTimeConst = 1000000 @@ -110,7 +112,7 @@ func NewServerNode(params *ServerParams) *ServerNode { } func getTime() int64 { - return time.Now().UnixNano() + return int64(mclock.Now()) } func (peer *ServerNode) recalcBLE(time int64) { diff --git a/les/odr.go b/les/odr.go index 4f3c6e74c2..f1e5ee5072 100644 --- a/les/odr.go +++ b/les/odr.go @@ -19,6 +19,7 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/logger" @@ -129,7 +130,7 @@ func (self *LesOdr) Deliver(peer *peer, msg *Msg) error { } func (self *LesOdr) requestPeer(req *sentReq, peer *peer, delivered, timeout chan struct{}, reqWg *sync.WaitGroup) { - stime := time.Now() + stime := mclock.Now() defer func() { req.lock.Lock() delete(req.sentTo, peer) @@ -139,7 +140,7 @@ func (self *LesOdr) requestPeer(req *sentReq, peer *peer, delivered, timeout cha select { case <-delivered: - servTime := uint64(time.Now().Sub(stime)) + servTime := uint64(mclock.Now()-stime) self.peers.updateTimeout(peer, false) self.peers.updateServTime(peer, servTime) return @@ -154,7 +155,7 @@ func (self *LesOdr) requestPeer(req *sentReq, peer *peer, delivered, timeout cha select { case <-delivered: - servTime := uint64(time.Now().Sub(stime)) + servTime := uint64(mclock.Now()-stime) self.peers.updateServTime(peer, servTime) return case <-time.After(hardRequestTimeout): From 658292f93431898d628a5149cb58d129c4798be8 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Mon, 27 Jun 2016 02:54:59 +0200 Subject: [PATCH 26/32] using lightchaindata db dir --- cmd/geth/chaincmd.go | 4 ++-- cmd/geth/main.go | 2 +- cmd/utils/flags.go | 17 +++++++++++++---- eth/backend.go | 6 +++--- ethdb/database.go | 10 ++++++---- les/backend.go | 2 +- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 321551ce05..fe63dfcca6 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -128,7 +128,7 @@ func removeDB(ctx *cli.Context) error { fmt.Println("Removing chaindata...") start := time.Now() - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata")) + os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), utils.ChainDbName(ctx))) fmt.Printf("Removed in %v\n", time.Since(start)) } else { @@ -153,7 +153,7 @@ func upgradeDB(ctx *cli.Context) error { utils.Fatalf("Unable to export chain for reimport %s", err) } chainDb.Close() - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata")) + os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), utils.ChainDbName(ctx))) // Import the chain file. chain, chainDb = utils.MakeChain(ctx) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 898a20cfe5..f32d341b5d 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -291,7 +291,7 @@ func initGenesis(ctx *cli.Context) error { utils.Fatalf("must supply path to genesis JSON file") } - chainDb, err := ethdb.NewLDBDatabase(filepath.Join(utils.MustMakeDataDir(ctx), "chaindata"), 0, 0) + chainDb, err := ethdb.NewLDBDatabase(filepath.Join(utils.MustMakeDataDir(ctx), utils.ChainDbName(ctx)), 0, 0) if err != nil { utils.Fatalf("could not open database: %v", err) } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 50d3ca43a7..2d2281d984 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -708,8 +708,8 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, Genesis: MakeGenesisBlock(ctx), FastSync: ctx.GlobalBool(FastSyncFlag.Name), LightMode: ctx.GlobalBool(LightModeFlag.Name), - LightServ: ctx.GlobalInt(LightServFlag.Name), - LightPeers: ctx.GlobalInt(LightPeersFlag.Name), + LightServ: ctx.GlobalInt(LightServFlag.Name), + LightPeers: ctx.GlobalInt(LightPeersFlag.Name), BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name), DatabaseCache: ctx.GlobalInt(CacheFlag.Name), DatabaseHandles: MakeDatabaseHandles(), @@ -789,7 +789,7 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, }); err != nil { Fatalf("Failed to register the account manager service: %v", err) } - + if ethConf.LightMode { if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return les.New(ctx, ethConf) @@ -866,15 +866,24 @@ func MustMakeChainConfigFromDb(ctx *cli.Context, db ethdb.Database) *core.ChainC return &core.ChainConfig{HomesteadBlock: homesteadBlockNo} } +func ChainDbName(ctx *cli.Context) string { + if ctx.GlobalBool(LightModeFlag.Name) { + return "lightchaindata" + } else { + return "chaindata" + } +} + // MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails. func MakeChainDatabase(ctx *cli.Context) ethdb.Database { var ( datadir = MustMakeDataDir(ctx) cache = ctx.GlobalInt(CacheFlag.Name) handles = MakeDatabaseHandles() + name = ChainDbName(ctx) ) - chainDb, err := ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles) + chainDb, err := ethdb.NewLDBDatabase(filepath.Join(datadir, name), cache, handles) if err != nil { Fatalf("Could not open database: %v", err) } diff --git a/eth/backend.go b/eth/backend.go index b13d408351..360acacfc9 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -157,7 +157,7 @@ func (s *FullNodeService) AddLesServer(ls LesServer) { // New creates a new FullNodeService object (including the // initialisation of the common Ethereum object) func New(ctx *node.ServiceContext, config *Config) (*FullNodeService, error) { - chainDb, dappDb, err := CreateDBs(ctx, config) + chainDb, dappDb, err := CreateDBs(ctx, config, "chaindata") if err != nil { return nil, err } @@ -257,9 +257,9 @@ func New(ctx *node.ServiceContext, config *Config) (*FullNodeService, error) { } // CreateDBs creates the chain and dapp databases for an Ethereum service -func CreateDBs(ctx *node.ServiceContext, config *Config) (chainDb, dappDb ethdb.Database, err error) { +func CreateDBs(ctx *node.ServiceContext, config *Config, name string) (chainDb, dappDb ethdb.Database, err error) { // Open the chain database and perform any upgrades needed - chainDb, err = ctx.OpenDatabase("chaindata", config.DatabaseCache, config.DatabaseHandles) + chainDb, err = ctx.OpenDatabase(name, config.DatabaseCache, config.DatabaseHandles) if err != nil { return nil, nil, err } diff --git a/ethdb/database.go b/ethdb/database.go index dffb42e2b0..83b6896311 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -39,15 +39,17 @@ var OpenFileLimit = 64 // cacheRatio specifies how the total alloted cache is distributed between the // various system databases. var cacheRatio = map[string]float64{ - "dapp": 0.0, - "chaindata": 1.0, + "dapp": 0.0, + "chaindata": 1.0, + "lightchaindata": 1.0, } // handleRatio specifies how the total alloted file descriptors is distributed // between the various system databases. var handleRatio = map[string]float64{ - "dapp": 0.0, - "chaindata": 1.0, + "dapp": 0.0, + "chaindata": 1.0, + "lightchaindata": 1.0, } type LDBDatabase struct { diff --git a/les/backend.go b/les/backend.go index 8b2e006d88..2eb2429a0e 100644 --- a/les/backend.go +++ b/les/backend.go @@ -72,7 +72,7 @@ type LightNodeService struct { } func New(ctx *node.ServiceContext, config *eth.Config) (*LightNodeService, error) { - chainDb, dappDb, err := eth.CreateDBs(ctx, config) + chainDb, dappDb, err := eth.CreateDBs(ctx, config, "lightchaindata") if err != nil { return nil, err } From 827737b0f6fac90f5047eca5fb81a4bc3e738067 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Mon, 27 Jun 2016 15:56:15 +0200 Subject: [PATCH 27/32] add default peer --- cmd/geth/main.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index f32d341b5d..18377ac9d9 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -41,6 +41,7 @@ import ( "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/release" "github.com/ethereum/go-ethereum/rlp" @@ -316,6 +317,14 @@ func startNode(ctx *cli.Context, stack *node.Node) { // Start up the node itself utils.StartNode(stack) + if ctx.GlobalBool(utils.LightModeFlag.Name) { + // add default light server; test phase only + node, err := discover.ParseNode("enode://201aa667e0b75462c8837708dbc3c91b43f84d233efda2f4e2c5ae0ea237d646db656375b394fb35d841cf8ea2814e3629af4821d3b0204508f7eb8cea8e7f31@40.118.3.223:30303") + if err == nil { + stack.Server().AddPeer(node) + } + } + // Unlock any account specifically requested var accman *accounts.Manager if err := stack.Service(&accman); err != nil { From 31e9f2f650e266a7b83af208e6797f01b4cab044 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Mon, 27 Jun 2016 16:08:50 +0200 Subject: [PATCH 28/32] always nodiscover in light mode --- cmd/utils/flags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 2d2281d984..a5892245c5 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -674,7 +674,7 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte, DataDir: MustMakeDataDir(ctx), PrivateKey: MakeNodeKey(ctx), Name: MakeNodeName(name, version, ctx), - NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name), + NoDiscovery: ctx.GlobalBool(NoDiscoverFlag.Name) || ctx.GlobalBool(LightModeFlag.Name), // light client hack BootstrapNodes: MakeBootstrapNodes(ctx), ListenAddr: MakeListenAddress(ctx), NAT: MakeNAT(ctx), From 947d69621081a9187059abe07393be54f2eca804 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Tue, 28 Jun 2016 16:47:07 +0200 Subject: [PATCH 29/32] added testnet azure server --- cmd/geth/main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 18377ac9d9..de696f22f1 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -319,7 +319,11 @@ func startNode(ctx *cli.Context, stack *node.Node) { if ctx.GlobalBool(utils.LightModeFlag.Name) { // add default light server; test phase only - node, err := discover.ParseNode("enode://201aa667e0b75462c8837708dbc3c91b43f84d233efda2f4e2c5ae0ea237d646db656375b394fb35d841cf8ea2814e3629af4821d3b0204508f7eb8cea8e7f31@40.118.3.223:30303") + url := "enode://201aa667e0b75462c8837708dbc3c91b43f84d233efda2f4e2c5ae0ea237d646db656375b394fb35d841cf8ea2814e3629af4821d3b0204508f7eb8cea8e7f31@40.118.3.223:30303" + if ctx.GlobalBool(utils.TestNetFlag.Name) { + url = "enode://2737bebb1e70cf682553c974d9551b74a917cb4f61292150abc10d2c122c8d369c82cb2b71ff107120ea2547419d2d9e998c637d45a6ff57bb01e83cfc1d5115@40.118.3.223:30304" + } + node, err := discover.ParseNode(url) if err == nil { stack.Server().AddPeer(node) } From 8c3701c7c50a6344e7ceb24c2f357bd605184d49 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 29 Jun 2016 02:43:36 +0200 Subject: [PATCH 30/32] fixed fetcher semaphores --- les/fetcher.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/les/fetcher.go b/les/fetcher.go index ac6fb0021c..c18fdbfb33 100644 --- a/les/fetcher.go +++ b/les/fetcher.go @@ -20,6 +20,7 @@ package les import ( "fmt" "sync" + "sync/atomic" "time" "github.com/ethereum/go-ethereum/core" @@ -35,6 +36,7 @@ type lightFetcher struct{ syncPoolMu sync.Mutex syncPool map[*peer]struct{} syncPoolNotify chan struct{} + syncPoolNotified uint32 } func newLightFetcher(pm *ProtocolManager) *lightFetcher { @@ -85,7 +87,9 @@ func (f *lightFetcher) notify(p *peer, block blockInfo) { f.syncPoolMu.Lock() f.syncPool[p] = struct{}{} f.syncPoolMu.Unlock() - f.syncPoolNotify <- struct{}{} + if atomic.SwapUint32(&f.syncPoolNotified, 1) == 0 { + f.syncPoolNotify <- struct{}{} + } } } @@ -113,24 +117,32 @@ func (f *lightFetcher) fetchBestFromPool() *peer { } func (f *lightFetcher) syncLoop() { + f.pm.wg.Add(1) + defer f.pm.wg.Done() + for { select { case <-f.pm.quitSync: return case <-f.syncPoolNotify: + atomic.StoreUint32(&f.syncPoolNotified, 0) chn := f.pm.getSyncLock(false) if chn != nil { - go func() { - <-chn - f.syncPoolNotify <- struct{}{} - }() + if atomic.SwapUint32(&f.syncPoolNotified, 1) == 0 { + go func() { + <-chn + f.syncPoolNotify <- struct{}{} + }() + } } else { if p := f.fetchBestFromPool(); p != nil { go f.syncWithPeer(p) - go func() { - time.Sleep(softRequestTimeout) - f.syncPoolNotify <- struct{}{} - }() + if atomic.SwapUint32(&f.syncPoolNotified, 1) == 0 { + go func() { + time.Sleep(softRequestTimeout) + f.syncPoolNotify <- struct{}{} + }() + } } } } From 68ae0c411d031f3b679d8ee7cc6ed49fa05e2f87 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 29 Jun 2016 18:28:28 +0200 Subject: [PATCH 31/32] fixed call nonce/empty trie bug --- internal/ethapi/api.go | 10 ++++++++++ light/state_object.go | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 6e888fc93b..adb97e4070 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -579,10 +579,15 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr } else { addr = args.From } + nonce, err := state.GetNonce(ctx, addr) + if err != nil { + return "0x", common.Big0, err + } // Assemble the CALL invocation msg := callmsg{ addr: addr, + nonce: nonce, to: args.To, gas: args.Gas.BigInt(), gasPrice: args.GasPrice.BigInt(), @@ -698,10 +703,15 @@ func (s *PublicBlockChainAPI) TraceCall(ctx context.Context, args CallArgs, bloc } else { addr = args.From } + nonce, err := state.GetNonce(ctx, addr) + if err != nil { + return nil, err + } // Assemble the CALL invocation msg := callmsg{ addr: addr, + nonce: nonce, to: args.To, gas: args.Gas.BigInt(), gasPrice: args.GasPrice.BigInt(), diff --git a/light/state_object.go b/light/state_object.go index 6be7f92ac9..0eb7d68b2c 100644 --- a/light/state_object.go +++ b/light/state_object.go @@ -102,7 +102,7 @@ func NewStateObject(address common.Address, odr OdrBackend) *StateObject { codeHash: emptyCodeHash, storage: make(Storage), } - object.trie = NewLightTrie(nil, odr, true) + object.trie = NewLightTrie(&TrieID{}, odr, true) return object } From 5937563956f9ba6ddc62b24eb51c7dcee11714fb Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Wed, 29 Jun 2016 22:50:44 +0200 Subject: [PATCH 32/32] fixed mined tx detection and storage --- core/blockchain.go | 58 +++++++++++++++++++++++------------------- core/database_util.go | 10 ++++++++ internal/ethapi/api.go | 3 +++ light/txpool.go | 29 ++++++++++++++++++--- 4 files changed, 71 insertions(+), 29 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 95bada5eed..62ebecaf1c 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -618,6 +618,37 @@ func (self *BlockChain) Rollback(chain []common.Hash) { } } +// SetReceiptsData computes all the non-consensus fields of the receipts +func SetReceiptsData(block *types.Block, receipts types.Receipts) { + transactions, logIndex := block.Transactions(), uint(0) + + for j := 0; j < len(receipts); j++ { + // The transaction hash can be retrieved from the transaction itself + receipts[j].TxHash = transactions[j].Hash() + + // The contract address can be derived from the transaction itself + if MessageCreatesContract(transactions[j]) { + from, _ := transactions[j].From() + receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce()) + } + // The used gas can be calculated based on previous receipts + if j == 0 { + receipts[j].GasUsed = new(big.Int).Set(receipts[j].CumulativeGasUsed) + } else { + receipts[j].GasUsed = new(big.Int).Sub(receipts[j].CumulativeGasUsed, receipts[j-1].CumulativeGasUsed) + } + // The derived log fields can simply be set from the block and transaction + for k := 0; k < len(receipts[j].Logs); k++ { + receipts[j].Logs[k].BlockNumber = block.NumberU64() + receipts[j].Logs[k].BlockHash = block.Hash() + receipts[j].Logs[k].TxHash = receipts[j].TxHash + receipts[j].Logs[k].TxIndex = uint(j) + receipts[j].Logs[k].Index = logIndex + logIndex++ + } + } +} + // InsertReceiptChain attempts to complete an already existing header chain with // transaction and receipt data. func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) { @@ -659,32 +690,7 @@ func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain continue } // Compute all the non-consensus fields of the receipts - transactions, logIndex := block.Transactions(), uint(0) - for j := 0; j < len(receipts); j++ { - // The transaction hash can be retrieved from the transaction itself - receipts[j].TxHash = transactions[j].Hash() - - // The contract address can be derived from the transaction itself - if MessageCreatesContract(transactions[j]) { - from, _ := transactions[j].From() - receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce()) - } - // The used gas can be calculated based on previous receipts - if j == 0 { - receipts[j].GasUsed = new(big.Int).Set(receipts[j].CumulativeGasUsed) - } else { - receipts[j].GasUsed = new(big.Int).Sub(receipts[j].CumulativeGasUsed, receipts[j-1].CumulativeGasUsed) - } - // The derived log fields can simply be set from the block and transaction - for k := 0; k < len(receipts[j].Logs); k++ { - receipts[j].Logs[k].BlockNumber = block.NumberU64() - receipts[j].Logs[k].BlockHash = block.Hash() - receipts[j].Logs[k].TxHash = receipts[j].TxHash - receipts[j].Logs[k].TxIndex = uint(j) - receipts[j].Logs[k].Index = logIndex - logIndex++ - } - } + SetReceiptsData(block, receipts) // Write all the data out into the database if err := WriteBody(self.chainDb, block.Hash(), block.NumberU64(), block.Body()); err != nil { errs[index] = fmt.Errorf("failed to write block body: %v", err) diff --git a/core/database_util.go b/core/database_util.go index 8c7aaa0ee8..7456d32dd2 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -447,6 +447,16 @@ func WriteTransactions(db ethdb.Database, block *types.Block) error { return nil } +// WriteReceipt stores a single transaction receipt into the database. +func WriteReceipt(db ethdb.Database, receipt *types.Receipt) error { + storageReceipt := (*types.ReceiptForStorage)(receipt) + data, err := rlp.EncodeToBytes(storageReceipt) + if err != nil { + return err + } + return db.Put(append(receiptsPrefix, receipt.TxHash.Bytes()...), data) +} + // WriteReceipts stores a batch of transaction receipts into the database. func WriteReceipts(db ethdb.Database, receipts types.Receipts) error { batch := db.NewBatch() diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index adb97e4070..eb81919280 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1024,6 +1024,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionByHash(ctx context.Context, txH // GetTransactionReceipt returns the transaction receipt for the given transaction hash. func (s *PublicTransactionPoolAPI) GetTransactionReceipt(txHash common.Hash) (map[string]interface{}, error) { +//fmt.Println("API GetTransactionReceipt", txHash) receipt := core.GetReceipt(s.b.ChainDb(), txHash) if receipt == nil { glog.V(logger.Debug).Infof("receipt not found for transaction %s", txHash.Hex()) @@ -1031,12 +1032,14 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(txHash common.Hash) (ma } tx, _, err := getTransaction(s.b.ChainDb(), s.b, txHash) +//fmt.Println("getTransaction", err) if err != nil { glog.V(logger.Debug).Infof("%v\n", err) return nil, nil } txBlock, blockIndex, index, err := getTransactionBlockData(s.b.ChainDb(), txHash) +//fmt.Println("getTransactionBlockData", txBlock, blockIndex, index, err) if err != nil { glog.V(logger.Debug).Infof("%v\n", err) return nil, nil diff --git a/light/txpool.go b/light/txpool.go index 5b2e8d58dd..8b33b22506 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -128,12 +128,14 @@ type txBlockData struct { // storeTxBlockData stores the block position of a mined tx in the local db func (pool *TxPool) storeTxBlockData(txh common.Hash, tbd txBlockData) { +//fmt.Println("storeTxBlockData", txh, tbd) data, _ := rlp.EncodeToBytes(tbd) pool.chainDb.Put(append(txh[:], byte(1)), data) } // removeTxBlockData removes the stored block position of a rolled back tx func (pool *TxPool) removeTxBlockData(txh common.Hash) { +//fmt.Println("removeTxBlockData", txh) pool.chainDb.Delete(append(txh[:], byte(1))) } @@ -167,18 +169,38 @@ func (txc txStateChanges) getLists() (mined []common.Hash, rollback []common.Has // and marks them as mined if necessary. It also stores block position in the db // and adds them to the received txStateChanges map. func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, idx uint64, txc txStateChanges) error { +//fmt.Println("checkMinedTxs") if len(pool.pending) == 0 { return nil } +//fmt.Println("len(pool) =", len(pool.pending)) - receipts, err := GetBlockReceipts(ctx, pool.odr, hash, idx) + block, err := GetBlock(ctx, pool.odr, hash, idx) + var receipts types.Receipts if err != nil { +//fmt.Println(err) return err } +//fmt.Println("len(block.Transactions()) =", len(block.Transactions())) + list := pool.mined[hash] - for i, receipt := range receipts { - txHash := receipt.TxHash + for i, tx := range block.Transactions() { + txHash := tx.Hash() +//fmt.Println(" txHash:", txHash) if tx, ok := pool.pending[txHash]; ok { +//fmt.Println("TX FOUND") + if receipts == nil { + receipts, err = GetBlockReceipts(ctx, pool.odr, hash, idx) + if err != nil { + return err + } + if len(receipts) != len(block.Transactions()) { + panic(nil) // should never happen if hashes did match + } + core.SetReceiptsData(block, receipts) + } +//fmt.Println("WriteReceipt", receipts[i].TxHash) + core.WriteReceipt(pool.chainDb, receipts[i]) pool.storeTxBlockData(txHash, txBlockData{hash, idx, uint64(i)}) delete(pool.pending, txHash) list = append(list, tx) @@ -428,6 +450,7 @@ func (self *TxPool) Add(ctx context.Context, tx *types.Transaction) error { if err := self.add(ctx, tx); err != nil { return err } +//fmt.Println("Send", tx.Hash()) self.relay.Send(types.Transactions{tx}) self.chainDb.Put(tx.Hash().Bytes(), data)