Created
October 17, 2019 07:22
-
-
Save rjl493456442/b506ad6bff14c34e84fced9b8cdbf1ee to your computer and use it in GitHub Desktop.
les fetcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2019 The go-ethereum Authors | |
| // This file is part of the go-ethereum library. | |
| // | |
| // The go-ethereum library is free software: you can redistribute it and/or modify | |
| // it under the terms of the GNU Lesser General Public License as published by | |
| // the Free Software Foundation, either version 3 of the License, or | |
| // (at your option) any later version. | |
| // | |
| // The go-ethereum library is distributed in the hope that it will be useful, | |
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| // GNU Lesser General Public License for more details. | |
| // | |
| // You should have received a copy of the GNU Lesser General Public License | |
| // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | |
| package main | |
| import ( | |
| "context" | |
| "flag" | |
| "math/big" | |
| "math/rand" | |
| "os" | |
| "strconv" | |
| "time" | |
| "github.com/ethereum/go-ethereum" | |
| "github.com/ethereum/go-ethereum/common" | |
| "github.com/ethereum/go-ethereum/ethclient" | |
| "github.com/ethereum/go-ethereum/log" | |
| "github.com/ethereum/go-ethereum/params" | |
| ) | |
| var ( | |
| r = rand.New(rand.NewSource(int64(time.Now().Nanosecond()))) | |
| ) | |
| func main() { | |
| log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) | |
| flag.Parse() | |
| // Establish a connect with local **light client** via JSON RPC | |
| client, err := ethclient.Dial("http://127.0.0.1:8545") | |
| if err != nil { | |
| panic(err) | |
| } | |
| number, err := strconv.Atoi(flag.Args()[0]) | |
| if err != nil { | |
| panic(err) | |
| } | |
| var t time.Time | |
| // If the requested block before the checkpoint, 2 underlying network | |
| // requests will be made: | |
| // (1) GetHeaderByNumber: validate by local CHT root | |
| // (2) GetBodyRLP: validate by retrieved header | |
| t = time.Now() | |
| old := r.Intn(params.CHTFrequency) + 1 | |
| block, err := client.BlockByNumber(context.Background(), big.NewInt(int64(old))) | |
| if err != nil { | |
| log.Crit("Failed to retrieve block", "error", err) | |
| } | |
| log.Info("Retrieved specified block", "number", old, "hash", block.Hash(), "elapsed", common.PrettyDuration(time.Since(t))) | |
| // Request balance of a specified account, 1 underlying network request | |
| // will be made: | |
| // (1) TrieRequest: validate by local header | |
| t = time.Now() | |
| oracle := params.GoerliCheckpointOracle | |
| balance, err := client.BalanceAt(context.Background(), oracle.Address, big.NewInt(int64(number))) | |
| if err != nil { | |
| log.Crit("Failed to retrieve balance", "error", err) | |
| } | |
| log.Info("Retrieved balance of oracle", "balance", balance, "elapsed", common.PrettyDuration(time.Since(t))) | |
| // Request code of a specified account, 1 underlying network request | |
| // will be made: | |
| // (1) CodeRequest: validate by local header | |
| t = time.Now() | |
| code, err := client.CodeAt(context.Background(), oracle.Address, big.NewInt(int64(number))) | |
| if err != nil { | |
| log.Crit("Failed to retrieve code", "error", err) | |
| } | |
| log.Info("Retrieved code of oracle", "code", common.Bytes2Hex(code), "elapsed", common.PrettyDuration(time.Since(t))) | |
| // Filter all logs of oracle in the past blockchain history | |
| // | |
| // 2 types of underlying network requests will be made | |
| // (1) BloomRequest | |
| // (2) GetBlockReceipts | |
| t = time.Now() | |
| query := ethereum.FilterQuery{ | |
| Addresses: []common.Address{oracle.Address}, | |
| } | |
| logs, err := client.FilterLogs(context.Background(), query) | |
| if err != nil { | |
| log.Crit("Failed to filter logs of oracle", "error", err) | |
| } | |
| log.Info("Retrieved logs of oracle", "number", len(logs), "elapsed", common.PrettyDuration(time.Since(t))) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment