|
| 1 | +// Copyright 2015 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package versions |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/big" |
| 22 | + "strconv" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/ethereum/go-ethereum/common" |
| 26 | + "github.com/ethereum/go-ethereum/core" |
| 27 | + "github.com/ethereum/go-ethereum/core/state" |
| 28 | + "github.com/ethereum/go-ethereum/crypto" |
| 29 | + "github.com/ethereum/go-ethereum/eth" |
| 30 | + "github.com/ethereum/go-ethereum/logger" |
| 31 | + "github.com/ethereum/go-ethereum/logger/glog" |
| 32 | + "github.com/ethereum/go-ethereum/node" |
| 33 | + "github.com/ethereum/go-ethereum/p2p" |
| 34 | + "github.com/ethereum/go-ethereum/rpc" |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + jsonlogger = logger.NewJsonLogger() |
| 39 | + // TODO: add Frontier address |
| 40 | + GlobalVersionsAddr = common.HexToAddress("0x40bebcadbb4456db23fda39f261f3b2509096e9e") // test |
| 41 | + dummySender = common.HexToAddress("0x16db48070243bc37a1c59cd5bb977ad7047618be") // test |
| 42 | + getVersionsSignature = "GetVersions()" |
| 43 | + firstCheckTime = time.Second * 4 |
| 44 | + continousCheckTime = time.Second * 600 |
| 45 | +) |
| 46 | + |
| 47 | +type VersionCheck struct { |
| 48 | + serverName string |
| 49 | + timer *time.Timer |
| 50 | + e *eth.Ethereum |
| 51 | + stop chan bool |
| 52 | +} |
| 53 | + |
| 54 | +// Boilerplate to satisfy node.Service interface |
| 55 | +func (v *VersionCheck) Protocols() []p2p.Protocol { |
| 56 | + return []p2p.Protocol{} |
| 57 | +} |
| 58 | + |
| 59 | +func (v *VersionCheck) APIs() []rpc.API { |
| 60 | + return []rpc.API{} |
| 61 | +} |
| 62 | + |
| 63 | +func (v *VersionCheck) Start(server *p2p.Server) error { |
| 64 | + v.serverName = server.Name |
| 65 | + // Check version first time after a few seconds so it shows after |
| 66 | + // other startup messages |
| 67 | + t := time.NewTimer(firstCheckTime) |
| 68 | + v.timer = t |
| 69 | + v.stop = make(chan bool) |
| 70 | + versionCheck := func() { |
| 71 | + for { |
| 72 | + select { |
| 73 | + case <-v.stop: |
| 74 | + close(v.stop) |
| 75 | + return |
| 76 | + case <-v.timer.C: |
| 77 | + _, err := get(v.e, v.serverName) |
| 78 | + if err != nil { |
| 79 | + glog.V(logger.Error).Infof("Could not query geth version contract: %s", err) |
| 80 | + } |
| 81 | + v.timer.Reset(continousCheckTime) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + go versionCheck() |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +func (v *VersionCheck) Stop() error { |
| 90 | + v.stop <- true |
| 91 | + select { |
| 92 | + case <-v.stop: |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func NewVersionCheck(ctx *node.ServiceContext) (node.Service, error) { |
| 98 | + var v VersionCheck |
| 99 | + var e *eth.Ethereum |
| 100 | + // sets e to the Ethereum instance previously started |
| 101 | + // expects double pointer |
| 102 | + ctx.Service(&e) |
| 103 | + v.e = e |
| 104 | + return &v, nil |
| 105 | +} |
| 106 | + |
| 107 | +// query versions list from the (custom) accessor in the versions contract |
| 108 | +func get(e *eth.Ethereum, clientVersion string) (string, error) { |
| 109 | + // TODO: move common/registrar abiSignature to some util package |
| 110 | + abi := crypto.Sha3([]byte(getVersionsSignature))[:4] |
| 111 | + res, _, err := simulateCall( |
| 112 | + e, |
| 113 | + &dummySender, |
| 114 | + &GlobalVersionsAddr, |
| 115 | + big.NewInt(3000000), // gasLimit |
| 116 | + big.NewInt(1), // gasPrice |
| 117 | + big.NewInt(0), // value |
| 118 | + abi) |
| 119 | + if err != nil { |
| 120 | + return "", err |
| 121 | + } |
| 122 | + |
| 123 | + // TODO: we use static arrays of size versionCount as workaround |
| 124 | + // until solidity has proper support for returning dynamic arrays |
| 125 | + versionCount := 10 |
| 126 | + |
| 127 | + if len(res) != 2+(64*versionCount*3) { // 0x + three 32-byte fields per version |
| 128 | + return "", fmt.Errorf("unexpected result length from GetVersions") |
| 129 | + } |
| 130 | + |
| 131 | + // TODO: use ABI (after solidity supports returning arrays of arrays and/or structs) |
| 132 | + var versions []string |
| 133 | + var timestamps []uint64 |
| 134 | + var signerCounts []uint64 |
| 135 | + |
| 136 | + // trim 0x |
| 137 | + res = res[2:] |
| 138 | + |
| 139 | + // parse res |
| 140 | + for i := 0; i < versionCount; i++ { |
| 141 | + bytes := common.FromHex(res[:64]) |
| 142 | + versions = append(versions, string(bytes)) |
| 143 | + res = res[64:] |
| 144 | + } |
| 145 | + |
| 146 | + for i := 0; i < versionCount; i++ { |
| 147 | + ts, err := strconv.ParseUint(res[:64], 16, 64) |
| 148 | + if err != nil { |
| 149 | + return "", err |
| 150 | + } |
| 151 | + timestamps = append(timestamps, ts) |
| 152 | + res = res[64:] |
| 153 | + } |
| 154 | + |
| 155 | + for i := 0; i < versionCount; i++ { |
| 156 | + sc, err := strconv.ParseUint(res[:64], 16, 64) |
| 157 | + if err != nil { |
| 158 | + return "", err |
| 159 | + } |
| 160 | + signerCounts = append(signerCounts, sc) |
| 161 | + res = res[64:] |
| 162 | + } |
| 163 | + |
| 164 | + // TODO: version matching logic (e.g. most votes / most recent) |
| 165 | + if versions[0] != clientVersion { |
| 166 | + glog.V(logger.Info).Infof("geth version %s does not match recommended version %s", clientVersion, versions[0]) |
| 167 | + } |
| 168 | + |
| 169 | + return res, nil |
| 170 | +} |
| 171 | + |
| 172 | +func simulateCall(e *eth.Ethereum, from0, to *common.Address, gas, gasPrice, value *big.Int, data []byte) (string, *big.Int, error) { |
| 173 | + stateCopy, err := e.BlockChain().State() |
| 174 | + if err != nil { |
| 175 | + return "", nil, err |
| 176 | + } |
| 177 | + from := stateCopy.GetOrNewStateObject(*from0) |
| 178 | + from.SetBalance(common.MaxBig) |
| 179 | + |
| 180 | + msg := callmsg{ |
| 181 | + from: from, |
| 182 | + to: to, |
| 183 | + gas: gas, |
| 184 | + gasPrice: gasPrice, |
| 185 | + value: value, |
| 186 | + data: data, |
| 187 | + } |
| 188 | + |
| 189 | + // Execute the call and return |
| 190 | + vmenv := core.NewEnv(stateCopy, e.BlockChain(), msg, e.BlockChain().CurrentHeader()) |
| 191 | + gp := new(core.GasPool).AddGas(common.MaxBig) |
| 192 | + |
| 193 | + res, gas, err := core.ApplyMessage(vmenv, msg, gp) |
| 194 | + return common.ToHex(res), gas, err |
| 195 | + |
| 196 | +} |
| 197 | + |
| 198 | +// TODO: consider moving to package common or accounts/abi as it's useful for anyone |
| 199 | +// simulating EVM CALL |
| 200 | +type callmsg struct { |
| 201 | + from *state.StateObject |
| 202 | + to *common.Address |
| 203 | + gas, gasPrice *big.Int |
| 204 | + value *big.Int |
| 205 | + data []byte |
| 206 | +} |
| 207 | + |
| 208 | +// accessor boilerplate to implement core.Message |
| 209 | +func (m callmsg) From() (common.Address, error) { return m.from.Address(), nil } |
| 210 | +func (m callmsg) Nonce() uint64 { return m.from.Nonce() } |
| 211 | +func (m callmsg) To() *common.Address { return m.to } |
| 212 | +func (m callmsg) GasPrice() *big.Int { return m.gasPrice } |
| 213 | +func (m callmsg) Gas() *big.Int { return m.gas } |
| 214 | +func (m callmsg) Value() *big.Int { return m.value } |
| 215 | +func (m callmsg) Data() []byte { return m.data } |
0 commit comments