|
@ -10,23 +10,34 @@ import ( |
|
|
lerr "github.com/tendermint/tendermint/lite/errors" |
|
|
lerr "github.com/tendermint/tendermint/lite/errors" |
|
|
"github.com/tendermint/tendermint/types" |
|
|
"github.com/tendermint/tendermint/types" |
|
|
dbm "github.com/tendermint/tmlibs/db" |
|
|
dbm "github.com/tendermint/tmlibs/db" |
|
|
|
|
|
log "github.com/tendermint/tmlibs/log" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type DBProvider struct { |
|
|
type DBProvider struct { |
|
|
chainID string |
|
|
|
|
|
db dbm.DB |
|
|
|
|
|
cdc *amino.Codec |
|
|
|
|
|
limit int |
|
|
|
|
|
|
|
|
logger log.Logger |
|
|
|
|
|
label string |
|
|
|
|
|
db dbm.DB |
|
|
|
|
|
cdc *amino.Codec |
|
|
|
|
|
limit int |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func NewDBProvider(db dbm.DB) *DBProvider { |
|
|
|
|
|
|
|
|
func NewDBProvider(label string, db dbm.DB) *DBProvider { |
|
|
//db = dbm.NewDebugDB("db provider "+cmn.RandStr(4), db)
|
|
|
//db = dbm.NewDebugDB("db provider "+cmn.RandStr(4), db)
|
|
|
cdc := amino.NewCodec() |
|
|
cdc := amino.NewCodec() |
|
|
crypto.RegisterAmino(cdc) |
|
|
crypto.RegisterAmino(cdc) |
|
|
dbp := &DBProvider{db: db, cdc: cdc} |
|
|
|
|
|
|
|
|
dbp := &DBProvider{ |
|
|
|
|
|
logger: log.NewNopLogger(), |
|
|
|
|
|
label: label, |
|
|
|
|
|
db: db, |
|
|
|
|
|
cdc: cdc, |
|
|
|
|
|
} |
|
|
return dbp |
|
|
return dbp |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (dbp *DBProvider) SetLogger(logger log.Logger) { |
|
|
|
|
|
dbp.logger = logger.With("label", dbp.label) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func (dbp *DBProvider) SetLimit(limit int) *DBProvider { |
|
|
func (dbp *DBProvider) SetLimit(limit int) *DBProvider { |
|
|
dbp.limit = limit |
|
|
dbp.limit = limit |
|
|
return dbp |
|
|
return dbp |
|
@ -35,6 +46,7 @@ func (dbp *DBProvider) SetLimit(limit int) *DBProvider { |
|
|
// Implements PersistentProvider.
|
|
|
// Implements PersistentProvider.
|
|
|
func (dbp *DBProvider) SaveFullCommit(fc FullCommit) error { |
|
|
func (dbp *DBProvider) SaveFullCommit(fc FullCommit) error { |
|
|
|
|
|
|
|
|
|
|
|
dbp.logger.Info("DBProvider.SaveFullCommit()...", "fc", fc) |
|
|
batch := dbp.db.NewBatch() |
|
|
batch := dbp.db.NewBatch() |
|
|
|
|
|
|
|
|
// Save the fc.validators.
|
|
|
// Save the fc.validators.
|
|
@ -79,6 +91,9 @@ func (dbp *DBProvider) SaveFullCommit(fc FullCommit) error { |
|
|
func (dbp *DBProvider) LatestFullCommit(chainID string, minHeight, maxHeight int64) ( |
|
|
func (dbp *DBProvider) LatestFullCommit(chainID string, minHeight, maxHeight int64) ( |
|
|
FullCommit, error) { |
|
|
FullCommit, error) { |
|
|
|
|
|
|
|
|
|
|
|
dbp.logger.Info("DBProvider.LatestFullCommit()...", |
|
|
|
|
|
"chainID", chainID, "minHeight", minHeight, "maxHeight", maxHeight) |
|
|
|
|
|
|
|
|
if minHeight <= 0 { |
|
|
if minHeight <= 0 { |
|
|
minHeight = 1 |
|
|
minHeight = 1 |
|
|
} |
|
|
} |
|
@ -107,7 +122,15 @@ func (dbp *DBProvider) LatestFullCommit(chainID string, minHeight, maxHeight int |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return FullCommit{}, err |
|
|
return FullCommit{}, err |
|
|
} else { |
|
|
} else { |
|
|
return dbp.fillFullCommit(sh) |
|
|
|
|
|
|
|
|
lfc, err := dbp.fillFullCommit(sh) |
|
|
|
|
|
if err == nil { |
|
|
|
|
|
dbp.logger.Info("DBProvider.LatestFullCommit() found latest.", "height", lfc.Height()) |
|
|
|
|
|
return lfc, nil |
|
|
|
|
|
} else { |
|
|
|
|
|
dbp.logger.Info("DBProvider.LatestFullCommit() got error", "lfc", lfc) |
|
|
|
|
|
dbp.logger.Info(fmt.Sprintf("%+v", err)) |
|
|
|
|
|
return lfc, err |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -155,6 +178,9 @@ func (dbp *DBProvider) fillFullCommit(sh types.SignedHeader) (FullCommit, error) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (dbp *DBProvider) deleteAfterN(chainID string, after int) error { |
|
|
func (dbp *DBProvider) deleteAfterN(chainID string, after int) error { |
|
|
|
|
|
|
|
|
|
|
|
dbp.logger.Info("DBProvider.deleteAfterN()...", "chainID", chainID, "after", after) |
|
|
|
|
|
|
|
|
itr := dbp.db.ReverseIterator( |
|
|
itr := dbp.db.ReverseIterator( |
|
|
signedHeaderKey(chainID, 1<<63-1), |
|
|
signedHeaderKey(chainID, 1<<63-1), |
|
|
signedHeaderKey(chainID, 0), |
|
|
signedHeaderKey(chainID, 0), |
|
@ -163,6 +189,7 @@ func (dbp *DBProvider) deleteAfterN(chainID string, after int) error { |
|
|
|
|
|
|
|
|
var lastHeight int64 = 1<<63 - 1 |
|
|
var lastHeight int64 = 1<<63 - 1 |
|
|
var numSeen = 0 |
|
|
var numSeen = 0 |
|
|
|
|
|
var numDeleted = 0 |
|
|
|
|
|
|
|
|
for itr.Valid() { |
|
|
for itr.Valid() { |
|
|
key := itr.Key() |
|
|
key := itr.Key() |
|
@ -176,9 +203,13 @@ func (dbp *DBProvider) deleteAfterN(chainID string, after int) error { |
|
|
} |
|
|
} |
|
|
if numSeen > after { |
|
|
if numSeen > after { |
|
|
dbp.db.Delete(key) |
|
|
dbp.db.Delete(key) |
|
|
|
|
|
numDeleted += 1 |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
itr.Next() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dbp.logger.Info(fmt.Sprintf("DBProvider.deleteAfterN() deleted %v items\n", numDeleted)) |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|