Skip to content

Commit

Permalink
core: add more comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jingjunLi committed Mar 28, 2024
1 parent dc9c50f commit 30d1328
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 57 deletions.
2 changes: 0 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"crypto/ecdsa"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/log"
"math/big"
"math/rand"
"os"
Expand Down Expand Up @@ -798,7 +797,6 @@ func TestReorgBadBlockHashes(t *testing.T) {

func testReorgBadHashes(t *testing.T, full bool, scheme string, pipeline bool) {
// Create a pristine chain and database
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true)))
genDb, gspec, blockchain, err := newCanonical(ethash.NewFaker(), 0, full, scheme, pipeline)
if err != nil {
t.Fatalf("failed to create pristine chain: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func checkBlobSidecarsRLP(have, want types.BlobSidecars) error {
func TestAncientStorage(t *testing.T) {
// Freezer style fast import the chain.
frdir := t.TempDir()
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false, false, false, false)
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), t.TempDir(), frdir, "", false, false, false, false)
if err != nil {
t.Fatalf("failed to create database with ancient backend")
}
Expand Down Expand Up @@ -657,7 +657,7 @@ func TestHashesInRange(t *testing.T) {
func BenchmarkWriteAncientBlocks(b *testing.B) {
// Open freezer database.
frdir := b.TempDir()
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false, false, false, false)
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), b.TempDir(), frdir, "", false, false, false, false)
if err != nil {
b.Fatalf("failed to create database with ancient backend")
}
Expand Down Expand Up @@ -1001,7 +1001,7 @@ func TestHeadersRLPStorage(t *testing.T) {
// Have N headers in the freezer
frdir := t.TempDir()

db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false, false, false, false)
db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), t.TempDir(), frdir, "", false, false, false, false)
if err != nil {
t.Fatalf("failed to create database with ancient backend")
}
Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/accessors_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package rawdb

import (
"encoding/binary"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -153,11 +154,10 @@ func WriteTrieJournal(db ethdb.KeyValueWriter, journal []byte) {

// DeleteTrieJournal deletes the serialized in-memory trie nodes of layers saved at
// the last shutdown.
func DeleteTrieJournal(db ethdb.Database) {
func DeleteTrieJournal(db ethdb.KeyValueWriter) {
if err := db.Delete(trieJournalKey); err != nil {
log.Crit("Failed to remove tries journal", "err", err)
}
//db.JournalDelete()
}

// ReadStateHistoryMeta retrieves the metadata corresponding to the specified
Expand Down
63 changes: 43 additions & 20 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type freezerdb struct {
diffStore ethdb.KeyValueStore
stateStore ethdb.Database
dbPath string
journalfd *os.File
journalFd *os.File
}

func (frdb *freezerdb) StateStoreReader() ethdb.Reader {
Expand Down Expand Up @@ -140,61 +140,76 @@ func (frdb *freezerdb) SetupFreezerEnv(env *ethdb.FreezerEnv) error {
return frdb.AncientFreezer.SetupFreezerEnv(env)
}

func (frdb *freezerdb) journalPath() string {
return frdb.dbPath + "/" + JournalFile
}

// NewJournalWriter creates a new journal writer.
func (frdb *freezerdb) NewJournalWriter() io.Writer {
var err error
path := frdb.dbPath + "/" + JournalFile
frdb.journalfd, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
path := frdb.journalPath()
frdb.journalFd, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return nil
}
log.Info("NewJournalReader", "path", path)
return frdb.journalfd
return frdb.journalFd
}

// NewJournalReader creates a new journal reader.
func (frdb *freezerdb) NewJournalReader() (*rlp.Stream, error) {
var err error
path := frdb.dbPath + "/" + JournalFile
log.Info("NewJournalReader", "path", path)
path := frdb.journalPath()

frdb.journalfd, err = os.Open(path)
frdb.journalFd, err = os.Open(path)
if errors.Is(err, fs.ErrNotExist) {
return nil, errMissJournal
}
if err != nil {
return nil, errMissJournal
return nil, err
}
return rlp.NewStream(frdb.journalfd, 0), nil
return rlp.NewStream(frdb.journalFd, 0), nil
}

// JournalWriterSync flushes the journal writer.
func (frdb *freezerdb) JournalWriterSync() {
}

// JournalDelete deletes the journal.
func (frdb *freezerdb) JournalDelete() {
path := frdb.dbPath + "/" + JournalFile
path := frdb.journalPath()
_, err := os.Stat(path)
if os.IsNotExist(err) {
return
}
errRemove := os.Remove(path)
if errRemove != nil {
log.Crit("Failed to remote tries journal", "err", err)
log.Crit("Failed to remove tries journal", "path", path, "err", err)
}
}

// JournalClose closes the journal.
func (frdb *freezerdb) JournalClose() {
frdb.journalfd.Close()
frdb.journalFd.Close()
}

// JournalSize returns the size of the journal.
func (frdb *freezerdb) JournalSize() uint64 {
return 1000
if frdb.journalFd == nil {
fileInfo, err := frdb.journalFd.Stat()
if err != nil {
log.Crit("Failed to stat journal", "err", err)
}
return uint64(fileInfo.Size())
}

return 0
}

// nofreezedb is a database wrapper that disables freezer data retrievals.
type nofreezedb struct {
ethdb.KeyValueStore
diffStore ethdb.KeyValueStore
stateStore ethdb.Database
dbPath string
journalBuf bytes.Buffer
}

Expand Down Expand Up @@ -312,20 +327,27 @@ func (db *nofreezedb) AncientDatadir() (string, error) {
}

func (db *nofreezedb) NewJournalWriter() io.Writer {
// Create a buffer to store encoded data
return &db.journalBuf
}

func (db *nofreezedb) stateDB() ethdb.Database {
var stateDb ethdb.Database = db
if db.stateStore != nil {
stateDb = db.stateStore
}
return stateDb
}

func (db *nofreezedb) NewJournalReader() (*rlp.Stream, error) {
journal := ReadTrieJournal(db.stateStore)
journal := ReadTrieJournal(db.stateDB())
if len(journal) == 0 {
return nil, errMissJournal
}
return rlp.NewStream(bytes.NewReader(journal), 0), nil
}

func (db *nofreezedb) JournalWriterSync() {
WriteTrieJournal(db, db.journalBuf.Bytes())
WriteTrieJournal(db.stateDB(), db.journalBuf.Bytes())
db.journalBuf.Reset()
}

Expand All @@ -339,7 +361,7 @@ func (db *nofreezedb) JournalClose() {
}

func (db *nofreezedb) JournalSize() uint64 {
return 1000
return uint64(db.journalBuf.Len())
}

func (db *nofreezedb) SetupFreezerEnv(env *ethdb.FreezerEnv) error {
Expand Down Expand Up @@ -416,7 +438,7 @@ func NewDatabaseWithFreezer(db ethdb.KeyValueStore, dbPath, ancient string, name
KeyValueStore: db,
AncientStore: frdb,
AncientFreezer: frdb,
dbPath: ancient,
dbPath: dbPath,
}, nil
}

Expand Down Expand Up @@ -533,6 +555,7 @@ func NewDatabaseWithFreezer(db ethdb.KeyValueStore, dbPath, ancient string, name
KeyValueStore: db,
AncientStore: frdb,
AncientFreezer: frdb,
dbPath: dbPath,
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion core/rawdb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package rawdb

import (
"io"

"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
"io"
)

// table is a wrapper around a database that prefixes each key access with a pre-
Expand Down
11 changes: 11 additions & 0 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,22 @@ type StateStore interface {
}

type Journal interface {
// NewJournalWriter creates a new journal writer.
NewJournalWriter() io.Writer

// NewJournalReader creates a new journal reader.
NewJournalReader() (*rlp.Stream, error)

// JournalWriterSync flushes the journal writer.
JournalWriterSync()

// JournalDelete deletes the journal.
JournalDelete()

// JournalClose closes the journal.
JournalClose()

// JournalSize returns the size of the journal.
JournalSize() uint64
}

Expand Down
3 changes: 2 additions & 1 deletion ethdb/remotedb/remotedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
package remotedb

import (
"io"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"io"
)

// Database is a key-value lookup for a remote database via debug_dbGet.
Expand Down
1 change: 1 addition & 0 deletions triedb/pathdb/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ func TestJournal(t *testing.T) {
//func TestCorruptedJournal(t *testing.T) {
// tester := newTester(t, 0)
// defer tester.release()
// log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true)))
//
// if err := tester.db.Journal(tester.lastHash()); err != nil {
// t.Errorf("Failed to journal, err: %v", err)
Expand Down
Loading

0 comments on commit 30d1328

Please sign in to comment.