Skip to content

Commit

Permalink
Merge branch 'ethereum:master' into portal
Browse files Browse the repository at this point in the history
  • Loading branch information
GrapeBaBa authored Jun 19, 2024
2 parents 9918aba + c11aac2 commit 98047b2
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 43 deletions.
4 changes: 2 additions & 2 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func importChain(ctx *cli.Context) error {
fmt.Printf("Import done in %v.\n\n", time.Since(start))

// Output pre-compaction stats mostly to see the import trashing
showLeveldbStats(db)
showDBStats(db)

// Print the memory statistics used by the importing
mem := new(runtime.MemStats)
Expand All @@ -359,7 +359,7 @@ func importChain(ctx *cli.Context) error {
}
fmt.Printf("Compaction done in %v.\n\n", time.Since(start))

showLeveldbStats(db)
showDBStats(db)
return importErr
}

Expand Down
20 changes: 8 additions & 12 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,13 @@ func checkStateContent(ctx *cli.Context) error {
return nil
}

func showLeveldbStats(db ethdb.KeyValueStater) {
if stats, err := db.Stat("leveldb.stats"); err != nil {
func showDBStats(db ethdb.KeyValueStater) {
stats, err := db.Stat()
if err != nil {
log.Warn("Failed to read database stats", "error", err)
} else {
fmt.Println(stats)
}
if ioStats, err := db.Stat("leveldb.iostats"); err != nil {
log.Warn("Failed to read database iostats", "error", err)
} else {
fmt.Println(ioStats)
return
}
fmt.Println(stats)
}

func dbStats(ctx *cli.Context) error {
Expand All @@ -427,7 +423,7 @@ func dbStats(ctx *cli.Context) error {
db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()

showLeveldbStats(db)
showDBStats(db)
return nil
}

Expand All @@ -439,15 +435,15 @@ func dbCompact(ctx *cli.Context) error {
defer db.Close()

log.Info("Stats before compaction")
showLeveldbStats(db)
showDBStats(db)

log.Info("Triggering compaction")
if err := db.Compact(nil, nil); err != nil {
log.Info("Compact err", "error", err)
return err
}
log.Info("Stats after compaction")
showLeveldbStats(db)
showDBStats(db)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (d *Decimal) UnmarshalJSON(input []byte) error {
if !isString(input) {
return &json.UnmarshalTypeError{Value: "non-string", Type: reflect.TypeOf(uint64(0))}
}
if i, err := strconv.ParseInt(string(input[1:len(input)-1]), 10, 64); err == nil {
if i, err := strconv.ParseUint(string(input[1:len(input)-1]), 10, 64); err == nil {
*d = Decimal(i)
return nil
} else {
Expand Down
27 changes: 27 additions & 0 deletions common/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"math"
"math/big"
"reflect"
"strings"
Expand Down Expand Up @@ -595,3 +596,29 @@ func BenchmarkPrettyDuration(b *testing.B) {
}
b.Logf("Post %s", a)
}

func TestDecimalUnmarshalJSON(t *testing.T) {
// These should error
for _, tc := range []string{``, `"`, `""`, `"-1"`} {
if err := new(Decimal).UnmarshalJSON([]byte(tc)); err == nil {
t.Errorf("input %s should cause error", tc)
}
}
// These should succeed
for _, tc := range []struct {
input string
want uint64
}{
{`"0"`, 0},
{`"9223372036854775807"`, math.MaxInt64},
{`"18446744073709551615"`, math.MaxUint64},
} {
have := new(Decimal)
if err := have.UnmarshalJSON([]byte(tc.input)); err != nil {
t.Errorf("input %q triggered error: %v", tc.input, err)
}
if uint64(*have) != tc.want {
t.Errorf("input %q, have %d want %d", tc.input, *have, tc.want)
}
}
}
6 changes: 3 additions & 3 deletions core/rawdb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ func (t *table) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
}
}

// Stat returns a particular internal stat of the database.
func (t *table) Stat(property string) (string, error) {
return t.db.Stat(property)
// Stat returns the statistic data of the database.
func (t *table) Stat() (string, error) {
return t.db.Stat()
}

// Compact flattens the underlying data store for the given key range. In essence,
Expand Down
4 changes: 2 additions & 2 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type KeyValueWriter interface {

// KeyValueStater wraps the Stat method of a backing data store.
type KeyValueStater interface {
// Stat returns a particular internal stat of the database.
Stat(property string) (string, error)
// Stat returns the statistic data of the database.
Stat() (string, error)
}

// Compacter wraps the Compact method of a backing data store.
Expand Down
54 changes: 46 additions & 8 deletions ethdb/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package leveldb

import (
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -244,14 +243,53 @@ func (db *Database) NewSnapshot() (ethdb.Snapshot, error) {
return &snapshot{db: snap}, nil
}

// Stat returns a particular internal stat of the database.
func (db *Database) Stat(property string) (string, error) {
if property == "" {
property = "leveldb.stats"
} else if !strings.HasPrefix(property, "leveldb.") {
property = "leveldb." + property
// Stat returns the statistic data of the database.
func (db *Database) Stat() (string, error) {
var stats leveldb.DBStats
if err := db.db.Stats(&stats); err != nil {
return "", err
}
return db.db.GetProperty(property)
var (
message string
totalRead int64
totalWrite int64
totalSize int64
totalTables int
totalDuration time.Duration
)
if len(stats.LevelSizes) > 0 {
message += " Level | Tables | Size(MB) | Time(sec) | Read(MB) | Write(MB)\n" +
"-------+------------+---------------+---------------+---------------+---------------\n"
for level, size := range stats.LevelSizes {
read := stats.LevelRead[level]
write := stats.LevelWrite[level]
duration := stats.LevelDurations[level]
tables := stats.LevelTablesCounts[level]

if tables == 0 && duration == 0 {
continue
}
totalTables += tables
totalSize += size
totalRead += read
totalWrite += write
totalDuration += duration
message += fmt.Sprintf(" %3d | %10d | %13.5f | %13.5f | %13.5f | %13.5f\n",
level, tables, float64(size)/1048576.0, duration.Seconds(),
float64(read)/1048576.0, float64(write)/1048576.0)
}
message += "-------+------------+---------------+---------------+---------------+---------------\n"
message += fmt.Sprintf(" Total | %10d | %13.5f | %13.5f | %13.5f | %13.5f\n",
totalTables, float64(totalSize)/1048576.0, totalDuration.Seconds(),
float64(totalRead)/1048576.0, float64(totalWrite)/1048576.0)
message += "-------+------------+---------------+---------------+---------------+---------------\n\n"
}
message += fmt.Sprintf("Read(MB):%.5f Write(MB):%.5f\n", float64(stats.IORead)/1048576.0, float64(stats.IOWrite)/1048576.0)
message += fmt.Sprintf("BlockCache(MB):%.5f FileCache:%d\n", float64(stats.BlockCacheSize)/1048576.0, stats.OpenedTablesCount)
message += fmt.Sprintf("MemoryCompaction:%d Level0Compaction:%d NonLevel0Compaction:%d SeekCompaction:%d\n", stats.MemComp, stats.Level0Comp, stats.NonLevel0Comp, stats.SeekComp)
message += fmt.Sprintf("WriteDelayCount:%d WriteDelayDuration:%s Paused:%t\n", stats.WriteDelayCount, common.PrettyDuration(stats.WriteDelayDuration), stats.WritePaused)
message += fmt.Sprintf("Snapshots:%d Iterators:%d\n", stats.AliveSnapshots, stats.AliveIterators)
return message, nil
}

// Compact flattens the underlying data store for the given key range. In essence,
Expand Down
6 changes: 3 additions & 3 deletions ethdb/memorydb/memorydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ func (db *Database) NewSnapshot() (ethdb.Snapshot, error) {
return newSnapshot(db), nil
}

// Stat returns a particular internal stat of the database.
func (db *Database) Stat(property string) (string, error) {
return "", errors.New("unknown property")
// Stat returns the statistic data of the database.
func (db *Database) Stat() (string, error) {
return "", nil
}

// Compact is not supported on a memory database, but there's no need either as
Expand Down
6 changes: 2 additions & 4 deletions ethdb/pebble/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,8 @@ func upperBound(prefix []byte) (limit []byte) {
}

// Stat returns the internal metrics of Pebble in a text format. It's a developer
// method to read everything there is to read independent of Pebble version.
//
// The property is unused in Pebble as there's only one thing to retrieve.
func (d *Database) Stat(property string) (string, error) {
// method to read everything there is to read, independent of Pebble version.
func (d *Database) Stat() (string, error) {
return d.db.Metrics().String(), nil
}

Expand Down
4 changes: 2 additions & 2 deletions ethdb/remotedb/remotedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (db *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
panic("not supported")
}

func (db *Database) Stat(property string) (string, error) {
panic("not supported")
func (db *Database) Stat() (string, error) {
return "", nil
}

func (db *Database) AncientDatadir() (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2108,8 +2108,8 @@ func (api *DebugAPI) PrintBlock(ctx context.Context, number uint64) (string, err
}

// ChaindbProperty returns leveldb properties of the key-value database.
func (api *DebugAPI) ChaindbProperty(property string) (string, error) {
return api.b.ChainDb().Stat(property)
func (api *DebugAPI) ChaindbProperty() (string, error) {
return api.b.ChainDb().Stat()
}

// ChaindbCompact flattens the entire key-value database into a single level,
Expand Down
1 change: 0 additions & 1 deletion internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ web3._extend({
new web3._extend.Method({
name: 'chaindbProperty',
call: 'debug_chaindbProperty',
params: 1,
outputFormatter: console.log
}),
new web3._extend.Method({
Expand Down
2 changes: 0 additions & 2 deletions trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type Trie struct {
reader *trieReader

// tracer is the tool to track the trie changes.
// It will be reset after each commit operation.
tracer *tracer
}

Expand Down Expand Up @@ -609,7 +608,6 @@ func (t *Trie) Hash() common.Hash {
// Once the trie is committed, it's not usable anymore. A new trie must
// be created with new root and updated trie database for following usage
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
defer t.tracer.reset()
defer func() {
t.committed = true
}()
Expand Down
2 changes: 1 addition & 1 deletion trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ func (s *spongeDb) Delete(key []byte) error { panic("implement
func (s *spongeDb) NewBatch() ethdb.Batch { return &spongeBatch{s} }
func (s *spongeDb) NewBatchWithSize(size int) ethdb.Batch { return &spongeBatch{s} }
func (s *spongeDb) NewSnapshot() (ethdb.Snapshot, error) { panic("implement me") }
func (s *spongeDb) Stat(property string) (string, error) { panic("implement me") }
func (s *spongeDb) Stat() (string, error) { panic("implement me") }
func (s *spongeDb) Compact(start []byte, limit []byte) error { panic("implement me") }
func (s *spongeDb) Close() error { return nil }
func (s *spongeDb) Put(key []byte, value []byte) error {
Expand Down

0 comments on commit 98047b2

Please sign in to comment.