Skip to content

Commit

Permalink
core/rawdb, eth, les: fix review concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Nov 24, 2020
1 parent 79a943f commit 2787328
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
32 changes: 17 additions & 15 deletions core/rawdb/accessors_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
func ReadDatabaseVersion(db ethdb.KeyValueReader) *uint64 {
var version uint64

enc, _ := db.Get(databaseVerisionKey)
enc, _ := db.Get(databaseVersionKey)
if len(enc) == 0 {
return nil
}
Expand All @@ -48,7 +48,7 @@ func WriteDatabaseVersion(db ethdb.KeyValueWriter, version uint64) {
if err != nil {
log.Crit("Failed to encode database version", "err", err)
}
if err = db.Put(databaseVerisionKey, enc); err != nil {
if err = db.Put(databaseVersionKey, enc); err != nil {
log.Crit("Failed to store the database version", "err", err)
}
}
Expand Down Expand Up @@ -81,22 +81,24 @@ func WriteChainConfig(db ethdb.KeyValueWriter, hash common.Hash, cfg *params.Cha
}
}

// ucmList is a list of unclean-shutdown-markers, for rlp-encoding to the
// crashList is a list of unclean-shutdown-markers, for rlp-encoding to the
// database
type ucmList struct {
type crashList struct {
Discarded uint64 // how many ucs have we deleted
Recent []uint64 // unix timestamps of 10 latest unclean shutdowns
}

// UpdateUncleanShutdownMarker appends a new unclean shutdown marker and returns
const crashesToKeep = 10

// PushUncleanShutdownMarker appends a new unclean shutdown marker and returns
// the previous data
// - a list of timestamps
// - a count of how many old unclean-shutdowns have been discarded
func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) {
var uncleanShutdowns ucmList
func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) {
var uncleanShutdowns crashList
// Read old data
if data, err := db.Get(uncleanShutdownKey); err != nil {
log.Warn("Error reading USM", "error", err)
log.Warn("Error reading unclean shutdown markers", "error", err)
} else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil {
return nil, 0, err
}
Expand All @@ -105,8 +107,8 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, erro
copy(previous, uncleanShutdowns.Recent)
// Add a new (but cap it)
uncleanShutdowns.Recent = append(uncleanShutdowns.Recent, uint64(time.Now().Unix()))
if l := len(uncleanShutdowns.Recent); l > 11 {
uncleanShutdowns.Recent = uncleanShutdowns.Recent[l-11:]
if l := len(uncleanShutdowns.Recent); l > crashesToKeep+1 {
uncleanShutdowns.Recent = uncleanShutdowns.Recent[l-crashesToKeep-1:]
uncleanShutdowns.Discarded++
}
// And save it again
Expand All @@ -118,14 +120,14 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, erro
return previous, discarded, nil
}

// ClearUncleanShutdowMarker removes the last unclean shutdown marker
func ClearUncleanShutdowMarker(db ethdb.KeyValueStore) {
var uncleanShutdowns ucmList
// PopUncleanShutdownMarker removes the last unclean shutdown marker
func PopUncleanShutdownMarker(db ethdb.KeyValueStore) {
var uncleanShutdowns crashList
// Read old data
if data, err := db.Get(uncleanShutdownKey); err != nil {
log.Warn("Error reading USM", "error", err)
log.Warn("Error reading unclean shutdown markers", "error", err)
} else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil {
log.Error("Error reading USM", "error", err) // Should mos def _not_ happen
log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen
}
if l := len(uncleanShutdowns.Recent); l > 0 {
uncleanShutdowns.Recent = uncleanShutdowns.Recent[:l-1]
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func InspectDatabase(db ethdb.Database) error {
bloomTrieNodes.Add(size)
default:
var accounted bool
for _, meta := range [][]byte{databaseVerisionKey, headHeaderKey, headBlockKey, headFastBlockKey, fastTrieProgressKey} {
for _, meta := range [][]byte{databaseVersionKey, headHeaderKey, headBlockKey, headFastBlockKey, fastTrieProgressKey, uncleanShutdownKey} {
if bytes.Equal(key, meta) {
metadata.Add(size)
accounted = true
Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

// The fields below define the low level database schema prefixing.
var (
// databaseVerisionKey tracks the current database version.
databaseVerisionKey = []byte("DatabaseVersion")
// databaseVersionKey tracks the current database version.
databaseVersionKey = []byte("DatabaseVersion")

// headHeaderKey tracks the latest known header's hash.
headHeaderKey = []byte("LastHeader")
Expand Down
6 changes: 3 additions & 3 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ func New(stack *node.Node, config *Config) (*Ethereum, error) {
stack.RegisterProtocols(eth.Protocols())
stack.RegisterLifecycle(eth)
// Check for unclean shutdown
if uncleanShutdowns, discards, err := rawdb.UpdateUncleanShutdownMarker(chainDb); err != nil {
if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(chainDb); err != nil {
log.Error("Could not update unclean-shutdown-marker list", "error", err)
} else {
if discards > 0 {
log.Warn("Old unclean shutdowns found", "count", discards)
}
for _, tstamp := range uncleanShutdowns {
t := time.Unix(int64(tstamp), 0)
log.Warn("Unclean shutdown detected", "time", t,
log.Warn("Unclean shutdown detected", "booted", t,
"age", common.PrettyAge(t))
}
}
Expand Down Expand Up @@ -557,7 +557,7 @@ func (s *Ethereum) Stop() error {
s.miner.Stop()
s.blockchain.Stop()
s.engine.Close()
rawdb.ClearUncleanShutdowMarker(s.chainDb)
rawdb.PopUncleanShutdownMarker(s.chainDb)
s.chainDb.Close()
s.eventMux.Stop()
return nil
Expand Down
6 changes: 3 additions & 3 deletions les/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ func New(stack *node.Node, config *eth.Config) (*LightEthereum, error) {
stack.RegisterLifecycle(leth)

// Check for unclean shutdown
if uncleanShutdowns, discards, err := rawdb.UpdateUncleanShutdownMarker(chainDb); err != nil {
if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(chainDb); err != nil {
log.Error("Could not update unclean-shutdown-marker list", "error", err)
} else {
if discards > 0 {
log.Warn("Old unclean shutdowns found", "count", discards)
}
for _, tstamp := range uncleanShutdowns {
t := time.Unix(int64(tstamp), 0)
log.Warn("Unclean shutdown detected", "time", t,
log.Warn("Unclean shutdown detected", "booted", t,
"age", common.PrettyAge(t))
}
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func (s *LightEthereum) Stop() error {
s.engine.Close()
s.pruner.close()
s.eventMux.Stop()
rawdb.ClearUncleanShutdowMarker(s.chainDb)
rawdb.PopUncleanShutdownMarker(s.chainDb)
s.chainDb.Close()
s.wg.Wait()
log.Info("Light ethereum stopped")
Expand Down

0 comments on commit 2787328

Please sign in to comment.