gitee.com/liu-zhao234568/cntest@v1.0.0/mobile/ethclient.go (about) 1 // Copyright 2016 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 // Contains a wrapper for the Ethereum client. 18 19 package geth 20 21 import ( 22 "math/big" 23 24 "gitee.com/liu-zhao234568/cntest/core/types" 25 "gitee.com/liu-zhao234568/cntest/ethclient" 26 ) 27 28 // EthereumClient provides access to the Ethereum APIs. 29 type EthereumClient struct { 30 client *ethclient.Client 31 } 32 33 // NewEthereumClient connects a client to the given URL. 34 func NewEthereumClient(rawurl string) (client *EthereumClient, _ error) { 35 rawClient, err := ethclient.Dial(rawurl) 36 return &EthereumClient{rawClient}, err 37 } 38 39 // GetBlockByHash returns the given full block. 40 func (ec *EthereumClient) GetBlockByHash(ctx *Context, hash *Hash) (block *Block, _ error) { 41 rawBlock, err := ec.client.BlockByHash(ctx.context, hash.hash) 42 return &Block{rawBlock}, err 43 } 44 45 // GetBlockByNumber returns a block from the current canonical chain. If number is <0, the 46 // latest known block is returned. 47 func (ec *EthereumClient) GetBlockByNumber(ctx *Context, number int64) (block *Block, _ error) { 48 if number < 0 { 49 rawBlock, err := ec.client.BlockByNumber(ctx.context, nil) 50 return &Block{rawBlock}, err 51 } 52 rawBlock, err := ec.client.BlockByNumber(ctx.context, big.NewInt(number)) 53 return &Block{rawBlock}, err 54 } 55 56 // GetHeaderByHash returns the block header with the given hash. 57 func (ec *EthereumClient) GetHeaderByHash(ctx *Context, hash *Hash) (header *Header, _ error) { 58 rawHeader, err := ec.client.HeaderByHash(ctx.context, hash.hash) 59 return &Header{rawHeader}, err 60 } 61 62 // GetHeaderByNumber returns a block header from the current canonical chain. If number is <0, 63 // the latest known header is returned. 64 func (ec *EthereumClient) GetHeaderByNumber(ctx *Context, number int64) (header *Header, _ error) { 65 if number < 0 { 66 rawHeader, err := ec.client.HeaderByNumber(ctx.context, nil) 67 return &Header{rawHeader}, err 68 } 69 rawHeader, err := ec.client.HeaderByNumber(ctx.context, big.NewInt(number)) 70 return &Header{rawHeader}, err 71 } 72 73 // GetTransactionByHash returns the transaction with the given hash. 74 func (ec *EthereumClient) GetTransactionByHash(ctx *Context, hash *Hash) (tx *Transaction, _ error) { 75 // TODO(karalabe): handle isPending 76 rawTx, _, err := ec.client.TransactionByHash(ctx.context, hash.hash) 77 return &Transaction{rawTx}, err 78 } 79 80 // GetTransactionSender returns the sender address of a transaction. The transaction must 81 // be included in blockchain at the given block and index. 82 func (ec *EthereumClient) GetTransactionSender(ctx *Context, tx *Transaction, blockhash *Hash, index int) (sender *Address, _ error) { 83 addr, err := ec.client.TransactionSender(ctx.context, tx.tx, blockhash.hash, uint(index)) 84 return &Address{addr}, err 85 } 86 87 // GetTransactionCount returns the total number of transactions in the given block. 88 func (ec *EthereumClient) GetTransactionCount(ctx *Context, hash *Hash) (count int, _ error) { 89 rawCount, err := ec.client.TransactionCount(ctx.context, hash.hash) 90 return int(rawCount), err 91 } 92 93 // GetTransactionInBlock returns a single transaction at index in the given block. 94 func (ec *EthereumClient) GetTransactionInBlock(ctx *Context, hash *Hash, index int) (tx *Transaction, _ error) { 95 rawTx, err := ec.client.TransactionInBlock(ctx.context, hash.hash, uint(index)) 96 return &Transaction{rawTx}, err 97 98 } 99 100 // GetTransactionReceipt returns the receipt of a transaction by transaction hash. 101 // Note that the receipt is not available for pending transactions. 102 func (ec *EthereumClient) GetTransactionReceipt(ctx *Context, hash *Hash) (receipt *Receipt, _ error) { 103 rawReceipt, err := ec.client.TransactionReceipt(ctx.context, hash.hash) 104 return &Receipt{rawReceipt}, err 105 } 106 107 // SyncProgress retrieves the current progress of the sync algorithm. If there's 108 // no sync currently running, it returns nil. 109 func (ec *EthereumClient) SyncProgress(ctx *Context) (progress *SyncProgress, _ error) { 110 rawProgress, err := ec.client.SyncProgress(ctx.context) 111 if rawProgress == nil { 112 return nil, err 113 } 114 return &SyncProgress{*rawProgress}, err 115 } 116 117 // NewHeadHandler is a client-side subscription callback to invoke on events and 118 // subscription failure. 119 type NewHeadHandler interface { 120 OnNewHead(header *Header) 121 OnError(failure string) 122 } 123 124 // SubscribeNewHead subscribes to notifications about the current blockchain head 125 // on the given channel. 126 func (ec *EthereumClient) SubscribeNewHead(ctx *Context, handler NewHeadHandler, buffer int) (sub *Subscription, _ error) { 127 // Subscribe to the event internally 128 ch := make(chan *types.Header, buffer) 129 rawSub, err := ec.client.SubscribeNewHead(ctx.context, ch) 130 if err != nil { 131 return nil, err 132 } 133 // Start up a dispatcher to feed into the callback 134 go func() { 135 for { 136 select { 137 case header := <-ch: 138 handler.OnNewHead(&Header{header}) 139 140 case err := <-rawSub.Err(): 141 if err != nil { 142 handler.OnError(err.Error()) 143 } 144 return 145 } 146 } 147 }() 148 return &Subscription{rawSub}, nil 149 } 150 151 // State Access 152 153 // GetBalanceAt returns the wei balance of the given account. 154 // The block number can be <0, in which case the balance is taken from the latest known block. 155 func (ec *EthereumClient) GetBalanceAt(ctx *Context, account *Address, number int64) (balance *BigInt, _ error) { 156 if number < 0 { 157 rawBalance, err := ec.client.BalanceAt(ctx.context, account.address, nil) 158 return &BigInt{rawBalance}, err 159 } 160 rawBalance, err := ec.client.BalanceAt(ctx.context, account.address, big.NewInt(number)) 161 return &BigInt{rawBalance}, err 162 } 163 164 // GetStorageAt returns the value of key in the contract storage of the given account. 165 // The block number can be <0, in which case the value is taken from the latest known block. 166 func (ec *EthereumClient) GetStorageAt(ctx *Context, account *Address, key *Hash, number int64) (storage []byte, _ error) { 167 if number < 0 { 168 return ec.client.StorageAt(ctx.context, account.address, key.hash, nil) 169 } 170 return ec.client.StorageAt(ctx.context, account.address, key.hash, big.NewInt(number)) 171 } 172 173 // GetCodeAt returns the contract code of the given account. 174 // The block number can be <0, in which case the code is taken from the latest known block. 175 func (ec *EthereumClient) GetCodeAt(ctx *Context, account *Address, number int64) (code []byte, _ error) { 176 if number < 0 { 177 return ec.client.CodeAt(ctx.context, account.address, nil) 178 } 179 return ec.client.CodeAt(ctx.context, account.address, big.NewInt(number)) 180 } 181 182 // GetNonceAt returns the account nonce of the given account. 183 // The block number can be <0, in which case the nonce is taken from the latest known block. 184 func (ec *EthereumClient) GetNonceAt(ctx *Context, account *Address, number int64) (nonce int64, _ error) { 185 if number < 0 { 186 rawNonce, err := ec.client.NonceAt(ctx.context, account.address, nil) 187 return int64(rawNonce), err 188 } 189 rawNonce, err := ec.client.NonceAt(ctx.context, account.address, big.NewInt(number)) 190 return int64(rawNonce), err 191 } 192 193 // Filters 194 195 // FilterLogs executes a filter query. 196 func (ec *EthereumClient) FilterLogs(ctx *Context, query *FilterQuery) (logs *Logs, _ error) { 197 rawLogs, err := ec.client.FilterLogs(ctx.context, query.query) 198 if err != nil { 199 return nil, err 200 } 201 // Temp hack due to vm.Logs being []*vm.Log 202 res := make([]*types.Log, len(rawLogs)) 203 for i := range rawLogs { 204 res[i] = &rawLogs[i] 205 } 206 return &Logs{res}, nil 207 } 208 209 // FilterLogsHandler is a client-side subscription callback to invoke on events and 210 // subscription failure. 211 type FilterLogsHandler interface { 212 OnFilterLogs(log *Log) 213 OnError(failure string) 214 } 215 216 // SubscribeFilterLogs subscribes to the results of a streaming filter query. 217 func (ec *EthereumClient) SubscribeFilterLogs(ctx *Context, query *FilterQuery, handler FilterLogsHandler, buffer int) (sub *Subscription, _ error) { 218 // Subscribe to the event internally 219 ch := make(chan types.Log, buffer) 220 rawSub, err := ec.client.SubscribeFilterLogs(ctx.context, query.query, ch) 221 if err != nil { 222 return nil, err 223 } 224 // Start up a dispatcher to feed into the callback 225 go func() { 226 for { 227 select { 228 case log := <-ch: 229 handler.OnFilterLogs(&Log{&log}) 230 231 case err := <-rawSub.Err(): 232 if err != nil { 233 handler.OnError(err.Error()) 234 } 235 return 236 } 237 } 238 }() 239 return &Subscription{rawSub}, nil 240 } 241 242 // Pending State 243 244 // GetPendingBalanceAt returns the wei balance of the given account in the pending state. 245 func (ec *EthereumClient) GetPendingBalanceAt(ctx *Context, account *Address) (balance *BigInt, _ error) { 246 rawBalance, err := ec.client.PendingBalanceAt(ctx.context, account.address) 247 return &BigInt{rawBalance}, err 248 } 249 250 // GetPendingStorageAt returns the value of key in the contract storage of the given account in the pending state. 251 func (ec *EthereumClient) GetPendingStorageAt(ctx *Context, account *Address, key *Hash) (storage []byte, _ error) { 252 return ec.client.PendingStorageAt(ctx.context, account.address, key.hash) 253 } 254 255 // GetPendingCodeAt returns the contract code of the given account in the pending state. 256 func (ec *EthereumClient) GetPendingCodeAt(ctx *Context, account *Address) (code []byte, _ error) { 257 return ec.client.PendingCodeAt(ctx.context, account.address) 258 } 259 260 // GetPendingNonceAt returns the account nonce of the given account in the pending state. 261 // This is the nonce that should be used for the next transaction. 262 func (ec *EthereumClient) GetPendingNonceAt(ctx *Context, account *Address) (nonce int64, _ error) { 263 rawNonce, err := ec.client.PendingNonceAt(ctx.context, account.address) 264 return int64(rawNonce), err 265 } 266 267 // GetPendingTransactionCount returns the total number of transactions in the pending state. 268 func (ec *EthereumClient) GetPendingTransactionCount(ctx *Context) (count int, _ error) { 269 rawCount, err := ec.client.PendingTransactionCount(ctx.context) 270 return int(rawCount), err 271 } 272 273 // Contract Calling 274 275 // CallContract executes a message call transaction, which is directly executed in the VM 276 // of the node, but never mined into the blockchain. 277 // 278 // blockNumber selects the block height at which the call runs. It can be <0, in which 279 // case the code is taken from the latest known block. Note that state from very old 280 // blocks might not be available. 281 func (ec *EthereumClient) CallContract(ctx *Context, msg *CallMsg, number int64) (output []byte, _ error) { 282 if number < 0 { 283 return ec.client.CallContract(ctx.context, msg.msg, nil) 284 } 285 return ec.client.CallContract(ctx.context, msg.msg, big.NewInt(number)) 286 } 287 288 // PendingCallContract executes a message call transaction using the EVM. 289 // The state seen by the contract call is the pending state. 290 func (ec *EthereumClient) PendingCallContract(ctx *Context, msg *CallMsg) (output []byte, _ error) { 291 return ec.client.PendingCallContract(ctx.context, msg.msg) 292 } 293 294 // SuggestGasPrice retrieves the currently suggested gas price to allow a timely 295 // execution of a transaction. 296 func (ec *EthereumClient) SuggestGasPrice(ctx *Context) (price *BigInt, _ error) { 297 rawPrice, err := ec.client.SuggestGasPrice(ctx.context) 298 return &BigInt{rawPrice}, err 299 } 300 301 // EstimateGas tries to estimate the gas needed to execute a specific transaction based on 302 // the current pending state of the backend blockchain. There is no guarantee that this is 303 // the true gas limit requirement as other transactions may be added or removed by miners, 304 // but it should provide a basis for setting a reasonable default. 305 func (ec *EthereumClient) EstimateGas(ctx *Context, msg *CallMsg) (gas int64, _ error) { 306 rawGas, err := ec.client.EstimateGas(ctx.context, msg.msg) 307 return int64(rawGas), err 308 } 309 310 // SendTransaction injects a signed transaction into the pending pool for execution. 311 // 312 // If the transaction was a contract creation use the TransactionReceipt method to get the 313 // contract address after the transaction has been mined. 314 func (ec *EthereumClient) SendTransaction(ctx *Context, tx *Transaction) error { 315 return ec.client.SendTransaction(ctx.context, tx.tx) 316 }