-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
412 lines (355 loc) · 11.6 KB
/
base.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package app
import (
"bytes"
"database/sql"
goerr "errors"
"math/big"
"strings"
"github.com/CyberMiles/travis/modules/governance"
"github.com/CyberMiles/travis/modules/stake"
"github.com/CyberMiles/travis/sdk"
"github.com/CyberMiles/travis/sdk/dbm"
"github.com/CyberMiles/travis/sdk/errors"
"github.com/CyberMiles/travis/sdk/state"
"github.com/CyberMiles/travis/server"
ttypes "github.com/CyberMiles/travis/types"
"github.com/CyberMiles/travis/utils"
"github.com/CyberMiles/travis/version"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
"golang.org/x/crypto/ripemd160"
)
// BaseApp - The ABCI application
type BaseApp struct {
*StoreApp
EthApp *EthermintApplication
checkedTx map[common.Hash]*types.Transaction
ethereum *eth.Ethereum
AbsentValidators *stake.AbsentValidators
ByzantineValidators []abci.Evidence
PresentValidators stake.Validators
blockTime int64
deliverSqlTx *sql.Tx
proposer abci.Validator
}
var (
_ abci.Application = &BaseApp{}
toBeShutdown = false
)
// NewBaseApp extends a StoreApp with a handler and a ticker,
// which it binds to the proper abci calls
func NewBaseApp(store *StoreApp, ethApp *EthermintApplication, ethereum *eth.Ethereum) (*BaseApp, error) {
// init pending proposals
pendingProposals := governance.GetPendingProposals()
if len(pendingProposals) > 0 {
proposalsTS := make(map[string]int64)
proposalsBH := make(map[string]int64)
for _, pp := range pendingProposals {
if pp.ExpireTimestamp > 0 {
proposalsTS[pp.Id] = pp.ExpireTimestamp
} else {
proposalsBH[pp.Id] = pp.ExpireBlockHeight
}
if pp.Type == governance.DEPLOY_LIBENI_PROPOSAL {
dp := governance.GetProposalById(pp.Id)
if dp.Detail["status"] != "ready" {
governance.DownloadLibEni(dp)
}
}
}
utils.PendingProposal.BatchAddTS(proposalsTS)
utils.PendingProposal.BatchAddBH(proposalsBH)
}
b := store.Append().Get(utils.ParamKey)
if b != nil {
utils.LoadParams(b)
}
app := &BaseApp{
StoreApp: store,
EthApp: ethApp,
checkedTx: make(map[common.Hash]*types.Transaction),
ethereum: ethereum,
}
return app, nil
}
// InitChain - ABCI
func (app *StoreApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
return
}
// Info implements abci.Application. It returns the height and hash,
// as well as the abci name and version.
//
// The height is the block that holds the transactions, not the apphash itself.
func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {
ethInfoRes := app.EthApp.Info(req)
if big.NewInt(ethInfoRes.LastBlockHeight).Cmp(bigZero) == 0 {
return ethInfoRes
}
rp := governance.GetRetiringProposal(version.Version)
if rp != nil {
if rp.ExpireBlockHeight <= ethInfoRes.LastBlockHeight {
rp = governance.GetProposalById(rp.Id)
if rp.Detail["status"] == "success" {
server.StopFlag <- true
}
} else if rp.ExpireBlockHeight == ethInfoRes.LastBlockHeight+1 {
if rp.Result == "Approved" {
utils.RetiringProposalId = rp.Id
}
} else {
// check ahead one block
utils.PendingProposal.Add(rp.Id, 0, rp.ExpireBlockHeight-1)
}
}
travisInfoRes := app.StoreApp.Info(req)
travisInfoRes.LastBlockAppHash = finalAppHash(ethInfoRes.LastBlockAppHash, travisInfoRes.LastBlockAppHash, app.StoreApp.GetDbHash(), travisInfoRes.LastBlockHeight, nil)
return travisInfoRes
}
// DeliverTx - ABCI
func (app *BaseApp) DeliverTx(txBytes []byte) abci.ResponseDeliverTx {
tx, err := decodeTx(txBytes)
if err != nil {
app.logger.Error("DeliverTx: Received invalid transaction", "err", err)
return errors.DeliverResult(err)
}
if utils.IsEthTx(tx) {
if checkedTx, ok := app.checkedTx[tx.Hash()]; ok {
tx = checkedTx
} else {
// force cache from of tx
networkId := big.NewInt(int64(app.ethereum.NetVersion()))
signer := types.NewEIP155Signer(networkId)
if _, err := types.Sender(signer, tx); err != nil {
app.logger.Debug("DeliverTx: Received invalid transaction", "tx", tx, "err", err)
return errors.DeliverResult(err)
}
}
resp := app.EthApp.DeliverTx(tx)
app.logger.Debug("EthApp DeliverTx response", "resp", resp)
return resp
}
app.logger.Info("DeliverTx: Received valid transaction", "tx", tx)
ctx := ttypes.NewContext(app.GetChainID(), app.WorkingHeight(), app.blockTime, app.EthApp.DeliverTxState())
return app.deliverHandler(ctx, app.Append(), tx)
}
// CheckTx - ABCI
func (app *BaseApp) CheckTx(txBytes []byte) abci.ResponseCheckTx {
tx, err := decodeTx(txBytes)
if err != nil {
app.logger.Error("CheckTx: Received invalid transaction", "err", err)
return errors.CheckResult(err)
}
if utils.IsEthTx(tx) {
resp := app.EthApp.CheckTx(tx)
app.logger.Debug("EthApp CheckTx response", "resp", resp)
if resp.IsErr() {
return errors.CheckResult(goerr.New(resp.String()))
}
app.checkedTx[tx.Hash()] = tx
return sdk.NewCheck(0, "").ToABCI()
}
app.logger.Info("CheckTx: Received valid transaction", "tx", tx)
ctx := ttypes.NewContext(app.GetChainID(), app.WorkingHeight(), app.blockTime, app.EthApp.checkTxState)
return app.checkHandler(ctx, app.Check(), tx)
}
// BeginBlock - ABCI
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
app.blockTime = req.GetHeader().Time
app.EthApp.BeginBlock(req)
app.PresentValidators = app.PresentValidators[:0]
app.AbsentValidators = stake.LoadAbsentValidators(app.Append())
// init deliver sql tx for statke
db, err := dbm.Sqliter.GetDB()
if err != nil {
panic(err)
}
deliverSqlTx, err := db.Begin()
if err != nil {
panic(err)
}
app.deliverSqlTx = deliverSqlTx
stake.SetDeliverSqlTx(deliverSqlTx)
governance.SetDeliverSqlTx(deliverSqlTx)
// init end
// handle the absent validators
for _, sv := range req.Validators {
var pk ed25519.PubKeyEd25519
copy(pk[:], sv.Validator.PubKey.Data)
pubKey := ttypes.PubKey{pk}
if !sv.SignedLastBlock {
app.AbsentValidators.Add(pubKey, app.WorkingHeight())
} else {
v := stake.GetCandidateByPubKey(pubKey)
if v != nil {
app.PresentValidators = append(app.PresentValidators, v.Validator())
}
}
}
app.AbsentValidators.Clear(app.WorkingHeight())
stake.SaveAbsentValidators(app.Append(), app.AbsentValidators)
app.logger.Info("BeginBlock", "absent_validators", app.AbsentValidators)
app.ByzantineValidators = req.ByzantineValidators
app.proposer = req.Header.Proposer
return abci.ResponseBeginBlock{}
}
// EndBlock - ABCI - triggers Tick actions
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
app.EthApp.EndBlock(req)
utils.BlockGasFee = big.NewInt(0).Add(utils.BlockGasFee, app.TotalUsedGasFee)
// slash Byzantine validators
if len(app.ByzantineValidators) > 0 {
for _, bv := range app.ByzantineValidators {
pk, err := ttypes.GetPubKey(string(bv.Validator.PubKey.Data))
if err != nil {
continue
}
stake.SlashByzantineValidator(pk, app.blockTime, app.WorkingHeight())
}
app.ByzantineValidators = app.ByzantineValidators[:0]
}
// slash the absent validators
for k, v := range app.AbsentValidators.Validators {
pk, err := ttypes.GetPubKey(k)
if err != nil {
continue
}
stake.SlashAbsentValidator(pk, v, app.blockTime, app.WorkingHeight())
}
var backups stake.Validators
for _, bv := range stake.GetBackupValidators() {
// exclude the absent validators
if !app.AbsentValidators.Contains(bv.PubKey) && !app.PresentValidators.Contains(bv.PubKey) {
backups = append(backups, bv.Validator())
}
}
// Deactivate validators that not in the list of preserved validators
if utils.RetiringProposalId != "" {
if proposal := governance.GetProposalById(utils.RetiringProposalId); proposal != nil {
pks := strings.Split(proposal.Detail["preserved_validators"].(string), ",")
vs := stake.GetCandidates().Validators()
inaVs := make(stake.Validators, 0)
abciVs := make([]abci.Validator, 0)
pvSize := 0
for _, v := range vs {
i := 0
for ; i < len(pks); i++ {
if pks[i] == ttypes.PubKeyString(v.PubKey) {
abciVs = append(abciVs, v.ABCIValidator())
pvSize++
break
}
}
if i == len(pks) {
inaVs = append(inaVs, v)
pk := v.PubKey.PubKey.(ed25519.PubKeyEd25519)
abciVs = append(abciVs, abci.Ed25519Validator(pk[:], 0))
}
}
if pvSize >= 1 {
inaVs.Deactivate()
app.AddValChange(abciVs)
toBeShutdown = true
governance.UpdateRetireProgramStatus(utils.RetiringProposalId, "success")
} else {
governance.UpdateRetireProgramStatus(utils.RetiringProposalId, "rejected")
}
} else {
app.logger.Error("Getting invalid RetiringProposalId")
}
}
if !toBeShutdown { // should not update validator set twice if the node is to be shutdown
// calculate the validator set difference
if calVPCheck(app.WorkingHeight()) {
diff, err := stake.UpdateValidatorSet(app.WorkingHeight())
if err != nil {
panic(err)
}
app.AddValChange(diff)
}
}
// block award
// run once per hour
if len(app.PresentValidators) > 0 {
stake.NewAwardDistributor(app.Append(), app.WorkingHeight(), app.PresentValidators, backups, app.logger).Distribute()
}
// block award end
// handle the pending unstake requests
stake.HandlePendingUnstakeRequests(app.WorkingHeight())
// record candidates stakes daily
if calStakeCheck(app.WorkingHeight()) {
// run once a day
stake.RecordCandidateDailyStakes(app.WorkingHeight())
}
// Accumulates the average staking date of all delegations
if calAvgStakingDateCheck(app.WorkingHeight()) {
// run once a day
stake.AccumulateDelegationsAverageStakingDate()
}
return app.StoreApp.EndBlock(req)
}
func (app *BaseApp) Commit() (res abci.ResponseCommit) {
if toBeShutdown {
server.StopFlag <- true
}
app.checkedTx = make(map[common.Hash]*types.Transaction)
ethAppCommit, err := app.EthApp.Commit()
if err != nil {
// Rollback transaction
if app.deliverSqlTx != nil {
err := app.deliverSqlTx.Rollback()
if err != nil {
panic(err)
}
stake.ResetDeliverSqlTx()
governance.ResetDeliverSqlTx()
}
// slash block proposer
var pk ed25519.PubKeyEd25519
copy(pk[:], app.proposer.PubKey.Data)
pubKey := ttypes.PubKey{pk}
stake.SlashBadProposer(pubKey, app.blockTime, app.WorkingHeight())
} else {
if app.deliverSqlTx != nil {
// Commit transaction
err := app.deliverSqlTx.Commit()
if err != nil {
panic(err)
}
stake.ResetDeliverSqlTx()
governance.ResetDeliverSqlTx()
}
}
workingHeight := app.WorkingHeight()
if dirty := utils.CleanParams(); workingHeight == 1 || dirty {
state := app.Append()
state.Set(utils.ParamKey, utils.UnloadParams())
}
// reset store app
app.TotalUsedGasFee = big.NewInt(0)
res = app.StoreApp.Commit()
dbHash := app.StoreApp.GetDbHash()
res.Data = finalAppHash(ethAppCommit.Data, res.Data, dbHash, workingHeight, nil)
return
}
func finalAppHash(ethCommitHash []byte, travisCommitHash []byte, dbHash []byte, workingHeight int64, store *state.SimpleDB) []byte {
hasher := ripemd160.New()
buf := new(bytes.Buffer)
buf.Write(ethCommitHash)
buf.Write(travisCommitHash)
buf.Write(dbHash)
hasher.Write(buf.Bytes())
hash := hasher.Sum(nil)
return hash
}
func calStakeCheck(height int64) bool {
return height%int64(utils.GetParams().CalStakeInterval) == 0
}
func calVPCheck(height int64) bool {
return height == 1 || height%int64(utils.GetParams().CalVPInterval) == 0
}
func calAvgStakingDateCheck(height int64) bool {
return height%int64(utils.GetParams().CalAverageStakingDateInterval) == 0
}