/
checkpoints.go
287 lines (244 loc) · 7.72 KB
/
checkpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2019 The Energi Core Authors
// This file is part of the Energi Core library.
//
// The Energi Core 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 Energi Core 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 Energi Core library. If not, see <http://www.gnu.org/licenses/>.
package api
import (
"context"
"math/big"
"github.com/energicryptocurrency/go-energi/accounts"
"github.com/energicryptocurrency/go-energi/accounts/abi/bind"
"github.com/energicryptocurrency/go-energi/common"
energi_abi "github.com/energicryptocurrency/go-energi/energi/abi"
energi_common "github.com/energicryptocurrency/go-energi/energi/common"
energi_params "github.com/energicryptocurrency/go-energi/energi/params"
"github.com/energicryptocurrency/go-energi/log"
"github.com/energicryptocurrency/go-energi/rpc"
)
type CheckpointAdminAPI struct {
backend Backend
}
func NewCheckpointAdminAPI(b Backend) *CheckpointAdminAPI {
return &CheckpointAdminAPI{b}
}
func (b *CheckpointAdminAPI) CheckpointLocal(
number uint64,
hash common.Hash,
) error {
return b.backend.AddLocalCheckpoint(number, hash)
}
type CheckpointRegistryAPI struct {
backend Backend
cpCache *energi_common.CacheStorage
proxyAddr common.Address
}
func NewCheckpointRegistryAPI(b Backend) *CheckpointRegistryAPI {
r := &CheckpointRegistryAPI{
backend: b,
cpCache: energi_common.NewCacheStorage(),
proxyAddr: energi_params.Energi_CheckpointRegistry,
}
b.OnSyncedHeadUpdates(func() {
r.CheckpointInfo()
})
return r
}
const (
checkpointCallGas uint64 = 3000000
)
type CheckpointInfo struct {
Number uint64
Hash common.Hash
Since uint64
SigCount uint64
}
type AllCheckpointInfo struct {
Registry []CheckpointInfo
Active []CheckpointInfo
}
// returns all the checkpoints from cache (or contract)
func (b *CheckpointRegistryAPI) CheckpointInfo() (res *AllCheckpointInfo, err error) {
var data interface{}
data, err = b.cpCache.Get(b.backend, b.checkpointInfo)
if err != nil || data == nil {
log.Error("CheckpointInfo failed", "err", err)
return
}
res = data.(*AllCheckpointInfo)
return
}
// lists the existing checkpoint addresses from contract
func (b *CheckpointRegistryAPI) Checkpoints() ([]common.Address, error) {
// initialize contract caller
registry, callOpts, err := checkpointRegistryCaller(b.backend, b.proxyAddr)
if err != nil {
return nil, err
}
// call "checkpoints" function on contract
checkpointAddresses, err := registry.Checkpoints(callOpts)
if err != nil {
log.Error("CheckpointRegsitryAPI::Checkpoints", "err", err)
return nil, err
}
return checkpointAddresses, nil
}
// executes command to propose checkpoint
func (b *CheckpointRegistryAPI) CheckpointRemove(
number uint64,
hash common.Hash,
password *string,
) (txhash common.Hash, err error) {
// request registry caller and signer
registry, _, err := b.registry(password, b.backend.ChainConfig().Energi.CPPSigner)
if err != nil {
return
}
tx, err := registry.Remove(new(big.Int).SetUint64(number), hash)
if tx != nil {
txhash = tx.Hash()
log.Info("Note: please wait until the proposal TX gets into a block!", "tx", txhash.Hex())
}
return
}
// returns existing checkpoints' info
func (b *CheckpointRegistryAPI) checkpointInfo(num *big.Int) (interface{}, error) {
// get existing checkpoint addresses from contract
addresses, err := b.Checkpoints()
if err != nil {
log.Error("Failed", "err", err)
return nil, err
}
res := &AllCheckpointInfo{}
res.Registry = make([]CheckpointInfo, 0, len(addresses))
for _, addr := range addresses {
cp, err := energi_abi.NewICheckpointV2Caller(
addr, b.backend.(bind.ContractCaller))
if err != nil {
log.Error("Failed", "err", err)
continue
}
info, err := cp.Info(&bind.CallOpts{Pending: true, GasLimit: energi_params.UnlimitedGas})
if err != nil {
log.Warn("Info error", "cp", addr, "err", err)
continue
}
sigs, err := cp.Signatures(&bind.CallOpts{Pending: true, GasLimit: energi_params.UnlimitedGas})
if err != nil {
log.Warn("Proposals error", "addr", addr, "err", err)
continue
}
res.Registry = append(res.Registry, CheckpointInfo{
Number: info.Number.Uint64(),
Hash: info.Hash,
Since: info.Since.Uint64(),
SigCount: uint64(len(sigs)),
})
}
local := b.backend.ListCheckpoints()
res.Active = make([]CheckpointInfo, 0, len(local))
for _, cp := range local {
res.Active = append(res.Active, CheckpointInfo{
Number: cp.Number,
Hash: cp.Hash,
Since: cp.Since,
SigCount: cp.SigCount,
})
}
return res, nil
}
// initializes registry which is used to propose checkpoint
func (b *CheckpointRegistryAPI) registry(password *string, from common.Address) (
session *energi_abi.ICheckpointRegistryV2Session,
hashsig func(common.Hash) ([]byte, error),
err error,
) {
contract, err := energi_abi.NewICheckpointRegistryV2(energi_params.Energi_CheckpointRegistry, b.backend.(bind.ContractBackend))
if err != nil {
return nil, nil, err
}
hashsig = func(h common.Hash) ([]byte, error) {
account := accounts.Account{Address: from}
wallet, err := b.backend.AccountManager().Find(account)
if err != nil {
return nil, err
}
if password == nil {
return wallet.SignHash(account, h.Bytes())
}
return wallet.SignHashWithPassphrase(account, *password, h.Bytes())
}
session = &energi_abi.ICheckpointRegistryV2Session{
Contract: contract,
CallOpts: bind.CallOpts{
Pending: true,
From: from,
GasLimit: energi_params.UnlimitedGas,
},
TransactOpts: bind.TransactOpts{
From: from,
Signer: createSignerCallback(b.backend, password),
GasLimit: checkpointCallGas,
},
}
return
}
// executes command to propose checkpoint
func (b *CheckpointRegistryAPI) CheckpointPropose(
number uint64,
hash common.Hash,
password *string,
) (txhash common.Hash, err error) {
// check if proposed block number hash corresponds to the block in current chain
if h, _ := b.backend.HeaderByNumber(context.Background(), rpc.BlockNumber(number)); h == nil {
log.Error("Block not found on local node", "number", number)
return
} else if h.Hash() != hash {
log.Error("Block mismatch on local node", "number", number, "hash", hash, "block", h.Hash())
return
}
registry, hashsig, err := b.registry(password, b.backend.ChainConfig().Energi.CPPSigner)
if err != nil {
return
}
bnum := new(big.Int).SetUint64(number)
tosig, err := registry.SignatureBase(bnum, hash)
if err != nil {
return
}
sig, err := hashsig(tosig)
if err != nil {
return
}
// NOTE: compatibility with ecrecover opcode.
sig[64] += 27
tx, err := registry.Propose(bnum, hash, sig)
if tx != nil {
txhash = tx.Hash()
log.Info("Note: please wait until the proposal TX gets into a block!", "tx", txhash.Hex())
}
return
}
// function returns already initialized checkpoint Icheckpoint registry caller
func checkpointRegistryCaller(backend Backend, proxyAddr common.Address) (*energi_abi.ICheckpointRegistryV2Caller, *bind.CallOpts, error) {
registry, err := energi_abi.NewICheckpointRegistryV2Caller(proxyAddr, backend.(bind.ContractCaller))
if err != nil {
log.Error("Creating NewICheckpointRegistryV2Caller Failed", "err", err)
return nil, nil, err
}
callOpts := &bind.CallOpts{
Pending: true,
GasLimit: energi_params.UnlimitedGas,
}
return registry, callOpts, nil
}