forked from CyberMiles/travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
378 lines (323 loc) · 9.64 KB
/
store.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
package app
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"math/big"
"path"
"path/filepath"
"strings"
"github.com/CyberMiles/travis/utils"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/ripemd160"
"github.com/ethereum/go-ethereum/common"
"github.com/tendermint/iavl"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tDB "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"github.com/CyberMiles/travis/modules/governance"
"github.com/CyberMiles/travis/modules/stake"
"github.com/CyberMiles/travis/sdk/dbm"
"github.com/CyberMiles/travis/sdk/errors"
sm "github.com/CyberMiles/travis/sdk/state"
"github.com/tendermint/go-amino"
)
// DefaultHistorySize is how many blocks of history to store for ABCI queries
const DefaultHistorySize = -1
var cdc = amino.NewCodec()
// StoreApp contains a data store and all chainState needed
// to perform queries and handshakes.
//
// It should be embeded in another struct for CheckTx,
// DeliverTx and initializing state from the genesis.
type StoreApp struct {
// Name is what is returned from chainState
Name string
// this is the database state
chainState *sm.ChainState
state *sm.State
// cached validator changes from DeliverTx
pending []abci.Validator
// height is last committed block, DeliverTx is the next one
height int64
TotalUsedGasFee *big.Int
logger log.Logger
}
// NewStoreApp creates a data store to handle queries
func NewStoreApp(appName, dbName string, cacheSize int, logger log.Logger) (*StoreApp, error) {
state, err := loadState(dbName, cacheSize, DefaultHistorySize)
if err != nil {
return nil, err
}
app := &StoreApp{
Name: appName,
state: state,
height: state.LatestHeight(),
chainState: sm.NewChainState(),
TotalUsedGasFee: big.NewInt(0),
logger: logger.With("module", "app"),
}
return app, nil
}
// GetChainID returns the currently stored chain
func (app *StoreApp) GetChainID() string {
return app.chainState.GetChainID(app.state.Committed())
}
func (app *StoreApp) SetChainId(chainId string) {
app.chainState.SetChainID(app.Append(), chainId)
}
// Logger returns the application base logger
func (app *StoreApp) Logger() log.Logger {
return app.logger
}
// Hash gets the last hash stored in the database
func (app *StoreApp) Hash() []byte {
return app.state.LatestHash()
}
// Committed returns the committed state,
// also exposing historical queries
// func (app *StoreApp) Committed() *Bonsai {
// return app.state.committed
// }
// Append returns the working state for DeliverTx
func (app *StoreApp) Append() sm.SimpleDB {
return app.state.Append()
}
// Check returns the working state for Chec
// kTx
func (app *StoreApp) Check() sm.SimpleDB {
return app.state.Check()
}
// CommittedHeight gets the last block height committed
// to the db
func (app *StoreApp) CommittedHeight() int64 {
return app.height
}
// WorkingHeight gets the current block we are writing
func (app *StoreApp) WorkingHeight() int64 {
return app.height + 1
}
// 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 *StoreApp) Info(req abci.RequestInfo) abci.ResponseInfo {
hash := app.Hash()
app.logger.Info("Info synced",
"height", app.CommittedHeight(),
"hash", fmt.Sprintf("%X", hash))
return abci.ResponseInfo{
Data: app.Name,
LastBlockHeight: app.CommittedHeight(),
LastBlockAppHash: hash,
}
}
// SetOption - ABCI
func (app *StoreApp) SetOption(res abci.RequestSetOption) abci.ResponseSetOption {
return abci.ResponseSetOption{Log: "Not Implemented"}
}
// Query - ABCI
func (app *StoreApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
if len(reqQuery.Data) == 0 {
resQuery.Log = "Query cannot be zero length"
resQuery.Code = errors.CodeTypeEncodingErr
return
}
// set the query response height to current
tree := app.state.Committed()
height := reqQuery.Height
if height == 0 {
withProof := app.CommittedHeight() - 1
if tree.Tree.VersionExists(withProof) {
height = withProof
} else {
height = app.CommittedHeight()
}
}
resQuery.Height = height
switch reqQuery.Path {
case "/store", "/key": // Get by key
key := reqQuery.Data // Data holds the key bytes
resQuery.Key = key
value := app.state.Check().Get(key)
resQuery.Value = value
if reqQuery.Prove {
value, proof, err := tree.GetVersionedWithProof(key, height)
if err != nil {
resQuery.Log = err.Error()
break
}
resQuery.Value = value
resQuery.Proof = proof.ComputeRootHash()
} else {
_, value := tree.GetVersioned(key, height)
resQuery.Value = value
}
case "/validators":
candidates := stake.QueryCandidates()
b, _ := json.Marshal(candidates)
resQuery.Value = b
case "/validator":
address := common.HexToAddress(string(reqQuery.Data))
candidate := stake.QueryCandidateByAddress(address)
if candidate != nil {
b, _ := json.Marshal(candidate)
resQuery.Value = b
} else {
resQuery.Value = []byte{}
}
case "/delegator":
address := common.HexToAddress(string(reqQuery.Data))
delegations := stake.QueryDelegationsByAddress(address)
for _, d := range delegations {
validator := stake.QueryCandidateById(d.CandidateId)
if validator != nil {
d.ValidatorAddress = validator.OwnerAddress
d.PubKey = validator.PubKey
}
}
b, _ := json.Marshal(delegations)
resQuery.Value = b
case "/governance/proposals":
proposals := governance.QueryProposals()
b, _ := json.Marshal(proposals)
resQuery.Value = b
case "/awardInfo":
_, value := tree.GetVersioned(utils.AwardInfosKey, height)
var awardInfos stake.AwardInfos
err := cdc.UnmarshalBinary(value, &awardInfos)
if err != nil {
resQuery.Log = err.Error()
break
}
b, _ := json.Marshal(awardInfos)
resQuery.Value = b
default:
resQuery.Code = errors.CodeTypeUnknownRequest
resQuery.Log = cmn.Fmt("Unexpected Query path: %v", reqQuery.Path)
}
return
}
// Commit implements abci.Application
func (app *StoreApp) Commit() (res abci.ResponseCommit) {
app.height++
hash, err := app.state.Commit(app.height)
if err != nil {
// die if we can't commit, not to recover
panic(err)
}
app.logger.Debug("Commit synced",
"height", app.height,
"hash", fmt.Sprintf("%X", hash),
)
if app.state.Size() == 0 {
return abci.ResponseCommit{}
}
return abci.ResponseCommit{Data: hash}
}
// EndBlock - ABCI
// Returns a list of all validator changes made in this block
func (app *StoreApp) EndBlock(_ abci.RequestEndBlock) (res abci.ResponseEndBlock) {
res.ValidatorUpdates = app.pending
app.pending = nil
return
}
// AddValChange is meant to be called by apps on DeliverTx
// results, this is added to the cache for the endblock
// changeset
func (app *StoreApp) AddValChange(diffs []abci.Validator) {
for _, d := range diffs {
idx := pubKeyIndex(d, app.pending)
if idx >= 0 {
app.pending[idx] = d
} else {
app.pending = append(app.pending, d)
}
}
}
// return index of list with validator of same PubKey, or -1 if no match
func pubKeyIndex(val abci.Validator, list []abci.Validator) int {
for i, v := range list {
if bytes.Equal(val.PubKey.Data, v.PubKey.Data) {
return i
}
}
return -1
}
func loadState(dbName string, cacheSize int, historySize int64) (*sm.State, error) {
// memory backed case, just for testing
if dbName == "" {
tree := iavl.NewVersionedTree(tDB.NewMemDB(), 0)
return sm.NewState(tree, historySize), nil
}
// Expand the path fully
dbPath, err := filepath.Abs(dbName)
if err != nil {
return nil, errors.ErrInternal("Invalid Database Name")
}
// Some external calls accidently add a ".db", which is now removed
dbPath = strings.TrimSuffix(dbPath, path.Ext(dbPath))
// Split the database name into it's components (dir, name)
dir := path.Dir(dbPath)
name := path.Base(dbPath)
// Open database called "dir/name.db", if it doesn't exist it will be created
db := tDB.NewDB(name, tDB.LevelDBBackend, dir)
tree := iavl.NewVersionedTree(db, cacheSize)
if _, err = tree.Load(); err != nil {
return nil, errors.ErrInternal("Loading tree: " + err.Error())
}
return sm.NewState(tree, historySize), nil
}
func (app *StoreApp) GetOldDbHash() []byte {
db, _ := dbm.Sqliter.GetDB()
tables := []string{"candidates", "delegations", "governance_proposal", "governance_vote", "candidate_account_update_requests", "slashes"}
hashes := make([]byte, len(tables))
for _, table := range tables {
hashes = append(hashes, getTableHash(db, table)...)
}
return hashing(hashes)
}
func (app *StoreApp) GetDbHash() []byte {
db, _ := dbm.Sqliter.GetDB()
tables := []string{"candidates", "delegations", "governance_proposal", "governance_vote", "candidate_account_update_requests", "slashes"}
hashes := make([]byte, len(tables))
for _, table := range tables {
hashes = append(hashes, getTableHash(db, table)...)
}
return hashing(hashes)
}
func getTableHash(db *sql.DB, table string) []byte {
stmt, err := db.Prepare("select hash from " + table + " where 1=1 order by hash")
if err != nil {
fmt.Println(err)
}
defer stmt.Close()
rows, err := stmt.Query()
if err != nil {
panic(err)
}
var hash string
hashes := make([]byte, 80)
for rows.Next() {
err = rows.Scan(&hash)
if err != nil {
panic(err)
}
hashes = append(hashes, []byte(hash)...)
}
err = rows.Err()
if err != nil {
panic(err)
}
//fmt.Printf("Table %s, hash: %s\n", table, common.Bytes2Hex(hashing(hashes)))
return hashing(hashes)
}
func hashing(h []byte) []byte {
hasher := ripemd160.New()
buf := new(bytes.Buffer)
buf.Write(h)
hasher.Write(buf.Bytes())
return hasher.Sum(nil)
}