Skip to content

Commit f7cdea2

Browse files
rjl493456442karalabe
authored andcommitted
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing cmd, les, node: remove callback mechanism cmd, node: remove callback definition les: simplify the registrar les: expose checkpoint rpc services in the light client les, light: don't store untrusted receipt cmd, contracts, les: discard stale checkpoint cmd, contracts/registrar: loose restriction of registeration cmd, contracts: add replay-protection all: off-chain multi-signature contract params: deploy checkpoint contract for rinkeby cmd/registrar: add raw signing mode for registrar cmd/registrar, contracts/registrar, les: fixed messages * cmd/registrar, contracts/registrar: fix lints * accounts/abi/bind, les: address comments * cmd, contracts, les, light, params: minor checkpoint sync cleanups * cmd, eth, les, light: move checkpoint config to config file * cmd, eth, les, params: address comments * eth, les, params: address comments * cmd: polish up the checkpoint admin CLI * cmd, contracts, params: deploy new version contract * cmd/checkpoint-admin: add another flag for clef mode signing * cmd, contracts, les: rename and regen checkpoint oracle with abigen
1 parent 702f52f commit f7cdea2

49 files changed

Lines changed: 2859 additions & 381 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎accounts/abi/bind/backends/simulated.go‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ import (
4545
// This nil assignment ensures compile time that SimulatedBackend implements bind.ContractBackend.
4646
var _ bind.ContractBackend = (*SimulatedBackend)(nil)
4747

48-
var errBlockNumberUnsupported = errors.New("SimulatedBackend cannot access blocks other than the latest block")
49-
var errGasEstimationFailed = errors.New("gas required exceeds allowance or always failing transaction")
48+
var (
49+
errBlockNumberUnsupported = errors.New("simulatedBackend cannot access blocks other than the latest block")
50+
errGasEstimationFailed = errors.New("gas required exceeds allowance or always failing transaction")
51+
)
5052

5153
// SimulatedBackend implements bind.ContractBackend, simulating a blockchain in
5254
// the background. Its main purpose is to allow easily testing contract bindings.
@@ -63,10 +65,9 @@ type SimulatedBackend struct {
6365
config *params.ChainConfig
6466
}
6567

66-
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
67-
// for testing purposes.
68-
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
69-
database := rawdb.NewMemoryDatabase()
68+
// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
69+
// and uses a simulated blockchain for testing purposes.
70+
func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
7071
genesis := core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: gasLimit, Alloc: alloc}
7172
genesis.MustCommit(database)
7273
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, ethash.NewFaker(), vm.Config{}, nil)
@@ -81,6 +82,12 @@ func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBac
8182
return backend
8283
}
8384

85+
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
86+
// for testing purposes.
87+
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
88+
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit)
89+
}
90+
8491
// Commit imports all the pending transactions as a single block and starts a
8592
// fresh new state.
8693
func (b *SimulatedBackend) Commit() {
@@ -424,6 +431,11 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
424431
return nil
425432
}
426433

434+
// Blockchain returns the underlying blockchain.
435+
func (b *SimulatedBackend) Blockchain() *core.BlockChain {
436+
return b.blockchain
437+
}
438+
427439
// callmsg implements core.Message to allow passing it as a transaction simulator.
428440
type callmsg struct {
429441
ethereum.CallMsg

‎accounts/abi/bind/bind_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,12 @@ var bindTests = []struct {
475475
`
476476
"github.com/ethereum/go-ethereum/accounts/abi/bind"
477477
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
478+
"github.com/ethereum/go-ethereum/core"
478479
"github.com/ethereum/go-ethereum/common"
479480
`,
480481
`
481482
// Create a simulator and wrap a non-deployed contract
482-
sim := backends.NewSimulatedBackend(nil, uint64(10000000000))
483+
sim := backends.NewSimulatedBackend(core.GenesisAlloc{}, uint64(10000000000))
483484
484485
nonexistent, err := NewNonExistent(common.Address{}, sim)
485486
if err != nil {

‎accounts/abi/bind/template.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,18 @@ var (
439439
}
440440
}), nil
441441
}
442+
443+
// Parse{{.Normalized.Name}} is a log parse operation binding the contract event 0x{{printf "%x" .Original.Id}}.
444+
//
445+
// Solidity: {{.Original.String}}
446+
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
447+
event := new({{$contract.Type}}{{.Normalized.Name}})
448+
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
449+
return nil, err
450+
}
451+
return event, nil
452+
}
453+
442454
{{end}}
443455
{{end}}
444456
`

‎accounts/abi/bind/util_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func TestWaitDeployed(t *testing.T) {
5656
backend := backends.NewSimulatedBackend(
5757
core.GenesisAlloc{
5858
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
59-
}, 10000000,
59+
},
60+
10000000,
6061
)
6162

6263
// Create the transaction.

‎cmd/checkpoint-admin/common.go‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright 2018 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU 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+
// go-ethereum 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 General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"io/ioutil"
21+
"strconv"
22+
"strings"
23+
24+
"github.com/ethereum/go-ethereum/accounts/keystore"
25+
"github.com/ethereum/go-ethereum/cmd/utils"
26+
"github.com/ethereum/go-ethereum/common"
27+
"github.com/ethereum/go-ethereum/console"
28+
"github.com/ethereum/go-ethereum/contracts/checkpointoracle"
29+
"github.com/ethereum/go-ethereum/ethclient"
30+
"github.com/ethereum/go-ethereum/params"
31+
"github.com/ethereum/go-ethereum/rpc"
32+
"gopkg.in/urfave/cli.v1"
33+
)
34+
35+
// newClient creates a client with specified remote URL.
36+
func newClient(ctx *cli.Context) *ethclient.Client {
37+
client, err := ethclient.Dial(ctx.GlobalString(nodeURLFlag.Name))
38+
if err != nil {
39+
utils.Fatalf("Failed to connect to Ethereum node: %v", err)
40+
}
41+
return client
42+
}
43+
44+
// newRPCClient creates a rpc client with specified node URL.
45+
func newRPCClient(url string) *rpc.Client {
46+
client, err := rpc.Dial(url)
47+
if err != nil {
48+
utils.Fatalf("Failed to connect to Ethereum node: %v", err)
49+
}
50+
return client
51+
}
52+
53+
// getContractAddr retrieves the register contract address through
54+
// rpc request.
55+
func getContractAddr(client *rpc.Client) common.Address {
56+
var addr string
57+
if err := client.Call(&addr, "les_getCheckpointContractAddress"); err != nil {
58+
utils.Fatalf("Failed to fetch checkpoint oracle address: %v", err)
59+
}
60+
return common.HexToAddress(addr)
61+
}
62+
63+
// getCheckpoint retrieves the specified checkpoint or the latest one
64+
// through rpc request.
65+
func getCheckpoint(ctx *cli.Context, client *rpc.Client) *params.TrustedCheckpoint {
66+
var checkpoint *params.TrustedCheckpoint
67+
68+
if ctx.GlobalIsSet(indexFlag.Name) {
69+
var result [3]string
70+
index := uint64(ctx.GlobalInt64(indexFlag.Name))
71+
if err := client.Call(&result, "les_getCheckpoint", index); err != nil {
72+
utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
73+
}
74+
checkpoint = &params.TrustedCheckpoint{
75+
SectionIndex: index,
76+
SectionHead: common.HexToHash(result[0]),
77+
CHTRoot: common.HexToHash(result[1]),
78+
BloomRoot: common.HexToHash(result[2]),
79+
}
80+
} else {
81+
var result [4]string
82+
err := client.Call(&result, "les_latestCheckpoint")
83+
if err != nil {
84+
utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
85+
}
86+
index, err := strconv.ParseUint(result[0], 0, 64)
87+
if err != nil {
88+
utils.Fatalf("Failed to parse checkpoint index %v", err)
89+
}
90+
checkpoint = &params.TrustedCheckpoint{
91+
SectionIndex: index,
92+
SectionHead: common.HexToHash(result[1]),
93+
CHTRoot: common.HexToHash(result[2]),
94+
BloomRoot: common.HexToHash(result[3]),
95+
}
96+
}
97+
return checkpoint
98+
}
99+
100+
// newContract creates a registrar contract instance with specified
101+
// contract address or the default contracts for mainnet or testnet.
102+
func newContract(client *rpc.Client) (common.Address, *checkpointoracle.CheckpointOracle) {
103+
addr := getContractAddr(client)
104+
if addr == (common.Address{}) {
105+
utils.Fatalf("No specified registrar contract address")
106+
}
107+
contract, err := checkpointoracle.NewCheckpointOracle(addr, ethclient.NewClient(client))
108+
if err != nil {
109+
utils.Fatalf("Failed to setup registrar contract %s: %v", addr, err)
110+
}
111+
return addr, contract
112+
}
113+
114+
// promptPassphrase prompts the user for a passphrase.
115+
// Set confirmation to true to require the user to confirm the passphrase.
116+
func promptPassphrase(confirmation bool) string {
117+
passphrase, err := console.Stdin.PromptPassword("Passphrase: ")
118+
if err != nil {
119+
utils.Fatalf("Failed to read passphrase: %v", err)
120+
}
121+
122+
if confirmation {
123+
confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ")
124+
if err != nil {
125+
utils.Fatalf("Failed to read passphrase confirmation: %v", err)
126+
}
127+
if passphrase != confirm {
128+
utils.Fatalf("Passphrases do not match")
129+
}
130+
}
131+
return passphrase
132+
}
133+
134+
// getPassphrase obtains a passphrase given by the user. It first checks the
135+
// --password command line flag and ultimately prompts the user for a
136+
// passphrase.
137+
func getPassphrase(ctx *cli.Context) string {
138+
passphraseFile := ctx.String(utils.PasswordFileFlag.Name)
139+
if passphraseFile != "" {
140+
content, err := ioutil.ReadFile(passphraseFile)
141+
if err != nil {
142+
utils.Fatalf("Failed to read passphrase file '%s': %v",
143+
passphraseFile, err)
144+
}
145+
return strings.TrimRight(string(content), "\r\n")
146+
}
147+
// Otherwise prompt the user for the passphrase.
148+
return promptPassphrase(false)
149+
}
150+
151+
// getKey retrieves the user key through specified key file.
152+
func getKey(ctx *cli.Context) *keystore.Key {
153+
// Read key from file.
154+
keyFile := ctx.GlobalString(keyFileFlag.Name)
155+
keyJson, err := ioutil.ReadFile(keyFile)
156+
if err != nil {
157+
utils.Fatalf("Failed to read the keyfile at '%s': %v", keyFile, err)
158+
}
159+
// Decrypt key with passphrase.
160+
passphrase := getPassphrase(ctx)
161+
key, err := keystore.DecryptKey(keyJson, passphrase)
162+
if err != nil {
163+
utils.Fatalf("Failed to decrypt user key '%s': %v", keyFile, err)
164+
}
165+
return key
166+
}

0 commit comments

Comments
 (0)