2019-07-22 04:17:27 -05:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2019-03-08 07:56:20 -06:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 09:07:44 -05:00
|
|
|
"os"
|
2019-03-14 01:59:47 -05:00
|
|
|
"path/filepath"
|
2020-08-03 12:40:46 -05:00
|
|
|
"sync"
|
2019-03-08 07:56:20 -06:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2019-05-15 06:33:33 -05:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-03-14 01:59:47 -05:00
|
|
|
"github.com/prometheus/tsdb/fileutil"
|
2019-03-08 07:56:20 -06:00
|
|
|
)
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
var (
|
2021-03-22 13:06:30 -05:00
|
|
|
// errReadOnly is returned if the freezer is opened in read only mode. All the
|
|
|
|
// mutations are disallowed.
|
|
|
|
errReadOnly = errors.New("read only")
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
// errUnknownTable is returned if the user attempts to read from a table that is
|
|
|
|
// not tracked by the freezer.
|
|
|
|
errUnknownTable = errors.New("unknown table")
|
|
|
|
|
|
|
|
// errOutOrderInsertion is returned if the user attempts to inject out-of-order
|
|
|
|
// binary blobs into the freezer.
|
|
|
|
errOutOrderInsertion = errors.New("the append operation is out-order")
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 09:07:44 -05:00
|
|
|
|
|
|
|
// errSymlinkDatadir is returned if the ancient directory specified by user
|
|
|
|
// is a symbolic link.
|
|
|
|
errSymlinkDatadir = errors.New("symbolic link datadir is not supported")
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
)
|
2019-03-08 07:56:20 -06:00
|
|
|
|
|
|
|
const (
|
|
|
|
// freezerRecheckInterval is the frequency to check the key-value database for
|
|
|
|
// chain progression that might permit new blocks to be frozen into immutable
|
|
|
|
// storage.
|
|
|
|
freezerRecheckInterval = time.Minute
|
|
|
|
|
|
|
|
// freezerBatchLimit is the maximum number of blocks to freeze in one batch
|
|
|
|
// before doing an fsync and deleting it from the key-value store.
|
|
|
|
freezerBatchLimit = 30000
|
2021-09-07 05:31:17 -05:00
|
|
|
|
|
|
|
// freezerTableSize defines the maximum size of freezer data files.
|
|
|
|
freezerTableSize = 2 * 1000 * 1000 * 1000
|
2019-03-08 07:56:20 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// freezer is an memory mapped append-only database to store immutable chain data
|
|
|
|
// into flat files:
|
|
|
|
//
|
|
|
|
// - The append only nature ensures that disk writes are minimized.
|
|
|
|
// - The memory mapping ensures we can max out system memory for caching without
|
|
|
|
// reserving it for go-ethereum. This would also reduce the memory requirements
|
|
|
|
// of Geth, and thus also GC overhead.
|
|
|
|
type freezer struct {
|
2019-05-16 17:45:05 -05:00
|
|
|
// WARNING: The `frozen` field is accessed atomically. On 32 bit platforms, only
|
|
|
|
// 64-bit aligned fields can be atomic. The struct is guaranteed to be so aligned,
|
|
|
|
// so take advantage of that (https://golang.org/pkg/sync/atomic/#pkg-note-BUG).
|
2020-08-20 05:01:24 -05:00
|
|
|
frozen uint64 // Number of blocks already frozen
|
|
|
|
threshold uint64 // Number of recent blocks not to freeze (params.FullImmutabilityThreshold apart from tests)
|
2019-05-16 17:45:05 -05:00
|
|
|
|
2021-10-25 09:24:27 -05:00
|
|
|
// This lock synchronizes writers and the truncate operation, as well as
|
|
|
|
// the "atomic" (batched) read operations.
|
|
|
|
writeLock sync.RWMutex
|
2021-09-07 05:31:17 -05:00
|
|
|
writeBatch *freezerBatch
|
|
|
|
|
2021-03-22 13:06:30 -05:00
|
|
|
readonly bool
|
2019-03-14 01:59:47 -05:00
|
|
|
tables map[string]*freezerTable // Data tables for storing everything
|
|
|
|
instanceLock fileutil.Releaser // File-system lock to prevent double opens
|
2020-08-20 05:01:24 -05:00
|
|
|
|
|
|
|
trigger chan chan struct{} // Manual blocking freeze trigger, test determinism
|
|
|
|
|
|
|
|
quit chan struct{}
|
2021-05-17 18:30:01 -05:00
|
|
|
wg sync.WaitGroup
|
2020-08-20 05:01:24 -05:00
|
|
|
closeOnce sync.Once
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFreezer creates a chain freezer that moves ancient chain data into
|
|
|
|
// append-only flat file containers.
|
2021-09-07 05:31:17 -05:00
|
|
|
//
|
|
|
|
// The 'tables' argument defines the data tables. If the value of a map
|
|
|
|
// entry is true, snappy compression is disabled for the table.
|
|
|
|
func newFreezer(datadir string, namespace string, readonly bool, maxTableSize uint32, tables map[string]bool) (*freezer, error) {
|
2019-03-08 07:56:20 -06:00
|
|
|
// Create the initial freezer object
|
|
|
|
var (
|
2019-09-10 06:39:07 -05:00
|
|
|
readMeter = metrics.NewRegisteredMeter(namespace+"ancient/read", nil)
|
|
|
|
writeMeter = metrics.NewRegisteredMeter(namespace+"ancient/write", nil)
|
|
|
|
sizeGauge = metrics.NewRegisteredGauge(namespace+"ancient/size", nil)
|
2019-03-08 07:56:20 -06:00
|
|
|
)
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 09:07:44 -05:00
|
|
|
// Ensure the datadir is not a symbolic link if it exists.
|
|
|
|
if info, err := os.Lstat(datadir); !os.IsNotExist(err) {
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
log.Warn("Symbolic link ancient database is not supported", "path", datadir)
|
|
|
|
return nil, errSymlinkDatadir
|
|
|
|
}
|
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
// Leveldb uses LOCK as the filelock filename. To prevent the
|
|
|
|
// name collision, we use FLOCK as the lock name.
|
|
|
|
lock, _, err := fileutil.Flock(filepath.Join(datadir, "FLOCK"))
|
2019-03-14 01:59:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-08 07:56:20 -06:00
|
|
|
// Open all the supported data tables
|
|
|
|
freezer := &freezer{
|
2021-03-22 13:06:30 -05:00
|
|
|
readonly: readonly,
|
2020-08-20 05:01:24 -05:00
|
|
|
threshold: params.FullImmutabilityThreshold,
|
2019-03-14 01:59:47 -05:00
|
|
|
tables: make(map[string]*freezerTable),
|
|
|
|
instanceLock: lock,
|
2020-08-20 05:01:24 -05:00
|
|
|
trigger: make(chan chan struct{}),
|
2020-05-11 07:11:17 -05:00
|
|
|
quit: make(chan struct{}),
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
|
|
|
|
// Create the tables.
|
|
|
|
for name, disableSnappy := range tables {
|
|
|
|
table, err := newTable(datadir, name, readMeter, writeMeter, sizeGauge, maxTableSize, disableSnappy)
|
2019-03-08 07:56:20 -06:00
|
|
|
if err != nil {
|
|
|
|
for _, table := range freezer.tables {
|
|
|
|
table.Close()
|
|
|
|
}
|
2019-03-14 01:59:47 -05:00
|
|
|
lock.Release()
|
2019-03-08 07:56:20 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
freezer.tables[name] = table
|
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
|
|
|
|
// Truncate all tables to common length.
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
if err := freezer.repair(); err != nil {
|
|
|
|
for _, table := range freezer.tables {
|
|
|
|
table.Close()
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
lock.Release()
|
|
|
|
return nil, err
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
|
|
|
|
// Create the write batch.
|
|
|
|
freezer.writeBatch = newFreezerBatch(freezer)
|
|
|
|
|
2021-03-22 13:06:30 -05:00
|
|
|
log.Info("Opened ancient database", "database", datadir, "readonly", readonly)
|
2019-03-08 07:56:20 -06:00
|
|
|
return freezer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close terminates the chain freezer, unmapping all the data files.
|
|
|
|
func (f *freezer) Close() error {
|
2021-09-07 05:31:17 -05:00
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
2019-03-08 07:56:20 -06:00
|
|
|
var errs []error
|
2020-08-03 12:40:46 -05:00
|
|
|
f.closeOnce.Do(func() {
|
2021-03-22 13:06:30 -05:00
|
|
|
close(f.quit)
|
2021-05-17 18:30:01 -05:00
|
|
|
// Wait for any background freezing to stop
|
|
|
|
f.wg.Wait()
|
2020-08-03 12:40:46 -05:00
|
|
|
for _, table := range f.tables {
|
|
|
|
if err := table.Close(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := f.instanceLock.Release(); err != nil {
|
2019-03-08 07:56:20 -06:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2020-08-03 12:40:46 -05:00
|
|
|
})
|
2019-03-08 07:56:20 -06:00
|
|
|
if errs != nil {
|
|
|
|
return fmt.Errorf("%v", errs)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
// HasAncient returns an indicator whether the specified ancient data exists
|
|
|
|
// in the freezer.
|
|
|
|
func (f *freezer) HasAncient(kind string, number uint64) (bool, error) {
|
|
|
|
if table := f.tables[kind]; table != nil {
|
|
|
|
return table.has(number), nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ancient retrieves an ancient binary blob from the append-only immutable files.
|
|
|
|
func (f *freezer) Ancient(kind string, number uint64) ([]byte, error) {
|
|
|
|
if table := f.tables[kind]; table != nil {
|
|
|
|
return table.Retrieve(number)
|
|
|
|
}
|
|
|
|
return nil, errUnknownTable
|
|
|
|
}
|
|
|
|
|
2021-10-25 09:24:27 -05:00
|
|
|
// AncientRange retrieves multiple items in sequence, starting from the index 'start'.
|
2021-08-13 03:51:01 -05:00
|
|
|
// It will return
|
|
|
|
// - at most 'max' items,
|
|
|
|
// - at least 1 item (even if exceeding the maxByteSize), but will otherwise
|
|
|
|
// return as many items as fit into maxByteSize.
|
2021-10-25 09:24:27 -05:00
|
|
|
func (f *freezer) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
|
2021-08-13 03:51:01 -05:00
|
|
|
if table := f.tables[kind]; table != nil {
|
|
|
|
return table.RetrieveItems(start, count, maxBytes)
|
|
|
|
}
|
|
|
|
return nil, errUnknownTable
|
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
// Ancients returns the length of the frozen items.
|
|
|
|
func (f *freezer) Ancients() (uint64, error) {
|
|
|
|
return atomic.LoadUint64(&f.frozen), nil
|
|
|
|
}
|
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 09:07:44 -05:00
|
|
|
// AncientSize returns the ancient size of the specified category.
|
|
|
|
func (f *freezer) AncientSize(kind string) (uint64, error) {
|
2021-09-07 05:31:17 -05:00
|
|
|
// This needs the write lock to avoid data races on table fields.
|
|
|
|
// Speed doesn't matter here, AncientSize is for debugging.
|
2021-10-25 09:24:27 -05:00
|
|
|
f.writeLock.RLock()
|
|
|
|
defer f.writeLock.RUnlock()
|
2021-09-07 05:31:17 -05:00
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 09:07:44 -05:00
|
|
|
if table := f.tables[kind]; table != nil {
|
|
|
|
return table.size()
|
|
|
|
}
|
|
|
|
return 0, errUnknownTable
|
|
|
|
}
|
|
|
|
|
2021-10-25 09:24:27 -05:00
|
|
|
// ReadAncients runs the given read operation while ensuring that no writes take place
|
|
|
|
// on the underlying freezer.
|
|
|
|
func (f *freezer) ReadAncients(fn func(ethdb.AncientReader) error) (err error) {
|
|
|
|
f.writeLock.RLock()
|
|
|
|
defer f.writeLock.RUnlock()
|
|
|
|
return fn(f)
|
|
|
|
}
|
|
|
|
|
2021-09-07 05:31:17 -05:00
|
|
|
// ModifyAncients runs the given write operation.
|
|
|
|
func (f *freezer) ModifyAncients(fn func(ethdb.AncientWriteOp) error) (writeSize int64, err error) {
|
2021-03-22 13:06:30 -05:00
|
|
|
if f.readonly {
|
2021-09-07 05:31:17 -05:00
|
|
|
return 0, errReadOnly
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
|
|
|
// Roll back all tables to the starting position in case of error.
|
|
|
|
prevItem := f.frozen
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2021-09-07 05:31:17 -05:00
|
|
|
// The write operation has failed. Go back to the previous item position.
|
|
|
|
for name, table := range f.tables {
|
|
|
|
err := table.truncate(prevItem)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Freezer table roll-back failed", "table", name, "index", prevItem, "err", err)
|
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-09-07 05:31:17 -05:00
|
|
|
|
|
|
|
f.writeBatch.reset()
|
|
|
|
if err := fn(f.writeBatch); err != nil {
|
|
|
|
return 0, err
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
item, writeSize, err := f.writeBatch.commit()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
atomic.StoreUint64(&f.frozen, item)
|
|
|
|
return writeSize, nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:53:59 -05:00
|
|
|
// TruncateAncients discards any recent data above the provided threshold number.
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
func (f *freezer) TruncateAncients(items uint64) error {
|
2021-03-22 13:06:30 -05:00
|
|
|
if f.readonly {
|
|
|
|
return errReadOnly
|
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
if atomic.LoadUint64(&f.frozen) <= items {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, table := range f.tables {
|
|
|
|
if err := table.truncate(items); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic.StoreUint64(&f.frozen, items)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-29 14:53:59 -05:00
|
|
|
// Sync flushes all data tables to disk.
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
func (f *freezer) Sync() error {
|
2019-03-08 07:56:20 -06:00
|
|
|
var errs []error
|
|
|
|
for _, table := range f.tables {
|
|
|
|
if err := table.Sync(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errs != nil {
|
|
|
|
return fmt.Errorf("%v", errs)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 05:31:17 -05:00
|
|
|
// repair truncates all data tables to the same length.
|
|
|
|
func (f *freezer) repair() error {
|
|
|
|
min := uint64(math.MaxUint64)
|
|
|
|
for _, table := range f.tables {
|
|
|
|
items := atomic.LoadUint64(&table.items)
|
|
|
|
if min > items {
|
|
|
|
min = items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, table := range f.tables {
|
|
|
|
if err := table.truncate(min); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic.StoreUint64(&f.frozen, min)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-08 07:56:20 -06:00
|
|
|
// freeze is a background thread that periodically checks the blockchain for any
|
|
|
|
// import progress and moves ancient data from the fast database into the freezer.
|
|
|
|
//
|
|
|
|
// This functionality is deliberately broken off from block importing to avoid
|
|
|
|
// incurring additional data shuffling delays on block propagation.
|
|
|
|
func (f *freezer) freeze(db ethdb.KeyValueStore) {
|
|
|
|
nfdb := &nofreezedb{KeyValueStore: db}
|
|
|
|
|
2020-08-20 05:01:24 -05:00
|
|
|
var (
|
|
|
|
backoff bool
|
|
|
|
triggered chan struct{} // Used in tests
|
|
|
|
)
|
2019-03-08 07:56:20 -06:00
|
|
|
for {
|
2020-05-11 07:11:17 -05:00
|
|
|
select {
|
|
|
|
case <-f.quit:
|
|
|
|
log.Info("Freezer shutting down")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if backoff {
|
2020-08-20 05:01:24 -05:00
|
|
|
// If we were doing a manual trigger, notify it
|
|
|
|
if triggered != nil {
|
|
|
|
triggered <- struct{}{}
|
|
|
|
triggered = nil
|
|
|
|
}
|
2020-05-11 07:11:17 -05:00
|
|
|
select {
|
|
|
|
case <-time.NewTimer(freezerRecheckInterval).C:
|
|
|
|
backoff = false
|
2020-08-20 05:01:24 -05:00
|
|
|
case triggered = <-f.trigger:
|
|
|
|
backoff = false
|
2020-05-11 07:11:17 -05:00
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
// Retrieve the freezing threshold.
|
|
|
|
hash := ReadHeadBlockHash(nfdb)
|
2019-03-08 07:56:20 -06:00
|
|
|
if hash == (common.Hash{}) {
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
log.Debug("Current full block hash unavailable") // new chain, empty database
|
2020-05-11 07:11:17 -05:00
|
|
|
backoff = true
|
2019-03-08 07:56:20 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
number := ReadHeaderNumber(nfdb, hash)
|
2020-08-20 05:01:24 -05:00
|
|
|
threshold := atomic.LoadUint64(&f.threshold)
|
|
|
|
|
2019-03-08 07:56:20 -06:00
|
|
|
switch {
|
|
|
|
case number == nil:
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
log.Error("Current full block number unavailable", "hash", hash)
|
2020-05-11 07:11:17 -05:00
|
|
|
backoff = true
|
2019-03-08 07:56:20 -06:00
|
|
|
continue
|
|
|
|
|
2020-08-20 05:01:24 -05:00
|
|
|
case *number < threshold:
|
|
|
|
log.Debug("Current full block not old enough", "number", *number, "hash", hash, "delay", threshold)
|
2020-05-11 07:11:17 -05:00
|
|
|
backoff = true
|
2019-03-08 07:56:20 -06:00
|
|
|
continue
|
|
|
|
|
2020-08-20 05:01:24 -05:00
|
|
|
case *number-threshold <= f.frozen:
|
2019-03-08 07:56:20 -06:00
|
|
|
log.Debug("Ancient blocks frozen already", "number", *number, "hash", hash, "frozen", f.frozen)
|
2020-05-11 07:11:17 -05:00
|
|
|
backoff = true
|
2019-03-08 07:56:20 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
head := ReadHeader(nfdb, hash, *number)
|
|
|
|
if head == nil {
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
log.Error("Current full block unavailable", "number", *number, "hash", hash)
|
2020-05-11 07:11:17 -05:00
|
|
|
backoff = true
|
2019-03-08 07:56:20 -06:00
|
|
|
continue
|
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
|
2019-03-08 07:56:20 -06:00
|
|
|
// Seems we have data ready to be frozen, process in usable batches
|
|
|
|
var (
|
|
|
|
start = time.Now()
|
2021-09-07 05:31:17 -05:00
|
|
|
first, _ = f.Ancients()
|
|
|
|
limit = *number - threshold
|
2019-03-08 07:56:20 -06:00
|
|
|
)
|
2021-09-07 05:31:17 -05:00
|
|
|
if limit-first > freezerBatchLimit {
|
|
|
|
limit = first + freezerBatchLimit
|
|
|
|
}
|
|
|
|
ancients, err := f.freezeRange(nfdb, first, limit)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error in block freeze operation", "err", err)
|
|
|
|
backoff = true
|
|
|
|
continue
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
|
2019-03-08 07:56:20 -06:00
|
|
|
// Batch of blocks have been frozen, flush them before wiping from leveldb
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
if err := f.Sync(); err != nil {
|
2019-03-08 07:56:20 -06:00
|
|
|
log.Crit("Failed to flush frozen tables", "err", err)
|
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
|
2019-03-08 07:56:20 -06:00
|
|
|
// Wipe out all data from the active database
|
|
|
|
batch := db.NewBatch()
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
for i := 0; i < len(ancients); i++ {
|
2019-05-27 04:25:24 -05:00
|
|
|
// Always keep the genesis block in active database
|
|
|
|
if first+uint64(i) != 0 {
|
|
|
|
DeleteBlockWithoutNumber(batch, ancients[i], first+uint64(i))
|
|
|
|
DeleteCanonicalHash(batch, first+uint64(i))
|
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
log.Crit("Failed to delete frozen canonical blocks", "err", err)
|
|
|
|
}
|
|
|
|
batch.Reset()
|
2020-08-20 05:01:24 -05:00
|
|
|
|
2021-08-18 05:03:41 -05:00
|
|
|
// Wipe out side chains also and track dangling side chains
|
2020-08-20 05:01:24 -05:00
|
|
|
var dangling []common.Hash
|
2019-03-08 07:56:20 -06:00
|
|
|
for number := first; number < f.frozen; number++ {
|
2019-05-27 04:25:24 -05:00
|
|
|
// Always keep the genesis block in active database
|
|
|
|
if number != 0 {
|
2020-08-20 05:01:24 -05:00
|
|
|
dangling = ReadAllHashes(db, number)
|
|
|
|
for _, hash := range dangling {
|
|
|
|
log.Trace("Deleting side chain", "number", number, "hash", hash)
|
2019-05-27 04:25:24 -05:00
|
|
|
DeleteBlock(batch, hash, number)
|
|
|
|
}
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
log.Crit("Failed to delete frozen side blocks", "err", err)
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
2020-08-20 05:01:24 -05:00
|
|
|
batch.Reset()
|
|
|
|
|
|
|
|
// Step into the future and delete and dangling side chains
|
|
|
|
if f.frozen > 0 {
|
|
|
|
tip := f.frozen
|
|
|
|
for len(dangling) > 0 {
|
|
|
|
drop := make(map[common.Hash]struct{})
|
|
|
|
for _, hash := range dangling {
|
|
|
|
log.Debug("Dangling parent from freezer", "number", tip-1, "hash", hash)
|
|
|
|
drop[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
children := ReadAllHashes(db, tip)
|
|
|
|
for i := 0; i < len(children); i++ {
|
|
|
|
// Dig up the child and ensure it's dangling
|
|
|
|
child := ReadHeader(nfdb, children[i], tip)
|
|
|
|
if child == nil {
|
|
|
|
log.Error("Missing dangling header", "number", tip, "hash", children[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := drop[child.ParentHash]; !ok {
|
|
|
|
children = append(children[:i], children[i+1:]...)
|
|
|
|
i--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Delete all block data associated with the child
|
|
|
|
log.Debug("Deleting dangling block", "number", tip, "hash", children[i], "parent", child.ParentHash)
|
|
|
|
DeleteBlock(batch, children[i], tip)
|
|
|
|
}
|
|
|
|
dangling = children
|
|
|
|
tip++
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
log.Crit("Failed to delete dangling side blocks", "err", err)
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
|
2019-03-08 07:56:20 -06:00
|
|
|
// Log something friendly for the user
|
|
|
|
context := []interface{}{
|
|
|
|
"blocks", f.frozen - first, "elapsed", common.PrettyDuration(time.Since(start)), "number", f.frozen - 1,
|
|
|
|
}
|
|
|
|
if n := len(ancients); n > 0 {
|
|
|
|
context = append(context, []interface{}{"hash", ancients[n-1]}...)
|
|
|
|
}
|
|
|
|
log.Info("Deep froze chain segment", context...)
|
|
|
|
|
|
|
|
// Avoid database thrashing with tiny writes
|
|
|
|
if f.frozen-first < freezerBatchLimit {
|
2020-05-11 07:11:17 -05:00
|
|
|
backoff = true
|
2019-03-08 07:56:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
|
2021-09-07 05:31:17 -05:00
|
|
|
func (f *freezer) freezeRange(nfdb *nofreezedb, number, limit uint64) (hashes []common.Hash, err error) {
|
|
|
|
hashes = make([]common.Hash, 0, limit-number)
|
|
|
|
|
|
|
|
_, err = f.ModifyAncients(func(op ethdb.AncientWriteOp) error {
|
|
|
|
for ; number <= limit; number++ {
|
|
|
|
// Retrieve all the components of the canonical block.
|
|
|
|
hash := ReadCanonicalHash(nfdb, number)
|
|
|
|
if hash == (common.Hash{}) {
|
|
|
|
return fmt.Errorf("canonical hash missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
header := ReadHeaderRLP(nfdb, hash, number)
|
|
|
|
if len(header) == 0 {
|
|
|
|
return fmt.Errorf("block header missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
body := ReadBodyRLP(nfdb, hash, number)
|
|
|
|
if len(body) == 0 {
|
|
|
|
return fmt.Errorf("block body missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
receipts := ReadReceiptsRLP(nfdb, hash, number)
|
|
|
|
if len(receipts) == 0 {
|
|
|
|
return fmt.Errorf("block receipts missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
td := ReadTdRLP(nfdb, hash, number)
|
|
|
|
if len(td) == 0 {
|
|
|
|
return fmt.Errorf("total difficulty missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the batch.
|
|
|
|
if err := op.AppendRaw(freezerHashTable, number, hash[:]); err != nil {
|
|
|
|
return fmt.Errorf("can't write hash to freezer: %v", err)
|
|
|
|
}
|
|
|
|
if err := op.AppendRaw(freezerHeaderTable, number, header); err != nil {
|
|
|
|
return fmt.Errorf("can't write header to freezer: %v", err)
|
|
|
|
}
|
|
|
|
if err := op.AppendRaw(freezerBodiesTable, number, body); err != nil {
|
|
|
|
return fmt.Errorf("can't write body to freezer: %v", err)
|
|
|
|
}
|
|
|
|
if err := op.AppendRaw(freezerReceiptTable, number, receipts); err != nil {
|
|
|
|
return fmt.Errorf("can't write receipts to freezer: %v", err)
|
|
|
|
}
|
|
|
|
if err := op.AppendRaw(freezerDifficultyTable, number, td); err != nil {
|
|
|
|
return fmt.Errorf("can't write td to freezer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes = append(hashes, hash)
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|
2021-09-07 05:31:17 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return hashes, err
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
}
|