From 006c21efc7af8bdf04d003ef256d8e2eb30006bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= <peterke@gmail.com>
Date: Fri, 8 Mar 2019 15:56:20 +0200
Subject: [PATCH] cmd, core, eth, les, node: chain freezer on top of db rework

---
 cmd/geth/chaincmd.go            |   7 +-
 cmd/geth/main.go                |   1 +
 cmd/geth/usage.go               |   1 +
 cmd/utils/flags.go              |   9 +-
 core/rawdb/accessors_chain.go   |  82 ++++++---
 core/rawdb/accessors_indexes.go |   4 +-
 core/rawdb/database.go          |  57 ++++++-
 core/rawdb/freezer.go           | 276 +++++++++++++++++++++++++++++++
 core/rawdb/freezer_table.go     | 284 ++++++++++++++++++++++++++++++++
 core/rawdb/table.go             |   6 +
 eth/backend.go                  |   2 +-
 eth/config.go                   |   1 +
 ethdb/database.go               |  15 +-
 node/node.go                    |  20 +++
 node/service.go                 |  25 ++-
 15 files changed, 755 insertions(+), 35 deletions(-)
 create mode 100644 core/rawdb/freezer.go
 create mode 100644 core/rawdb/freezer_table.go

diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go
index 582f0b768..809f5cf4a 100644
--- a/cmd/geth/chaincmd.go
+++ b/cmd/geth/chaincmd.go
@@ -368,9 +368,12 @@ func exportPreimages(ctx *cli.Context) error {
 
 func copyDb(ctx *cli.Context) error {
 	// Ensure we have a source chain directory to copy
-	if len(ctx.Args()) != 1 {
+	if len(ctx.Args()) < 1 {
 		utils.Fatalf("Source chaindata directory path argument missing")
 	}
+	if len(ctx.Args()) < 2 {
+		utils.Fatalf("Source ancient chain directory path argument missing")
+	}
 	// Initialize a new chain for the running node to sync into
 	stack := makeFullNode(ctx)
 	defer stack.Close()
@@ -385,7 +388,7 @@ func copyDb(ctx *cli.Context) error {
 	dl := downloader.New(0, chainDb, syncBloom, new(event.TypeMux), chain, nil, nil)
 
 	// Create a source peer to satisfy downloader requests from
-	db, err := rawdb.NewLevelDBDatabase(ctx.Args().First(), ctx.GlobalInt(utils.CacheFlag.Name)/2, 256, "")
+	db, err := rawdb.NewLevelDBDatabaseWithFreezer(ctx.Args().First(), ctx.GlobalInt(utils.CacheFlag.Name)/2, 256, ctx.Args().Get(1), "")
 	if err != nil {
 		return err
 	}
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index 838029333..dc63f2302 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -62,6 +62,7 @@ var (
 		utils.BootnodesV4Flag,
 		utils.BootnodesV5Flag,
 		utils.DataDirFlag,
+		utils.AncientFlag,
 		utils.KeyStoreDirFlag,
 		utils.ExternalSignerFlag,
 		utils.NoUSBFlag,
diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go
index 7ec1ab03f..67b0027f2 100644
--- a/cmd/geth/usage.go
+++ b/cmd/geth/usage.go
@@ -69,6 +69,7 @@ var AppHelpFlagGroups = []flagGroup{
 		Flags: []cli.Flag{
 			configFileFlag,
 			utils.DataDirFlag,
+			utils.AncientFlag,
 			utils.KeyStoreDirFlag,
 			utils.NoUSBFlag,
 			utils.NetworkIdFlag,
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 2dc45cbba..c40da85b0 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -117,6 +117,10 @@ var (
 		Usage: "Data directory for the databases and keystore",
 		Value: DirectoryString{node.DefaultDataDir()},
 	}
+	AncientFlag = DirectoryFlag{
+		Name:  "datadir.ancient",
+		Usage: "Data directory for ancient chain segments (default = inside chaindata)",
+	}
 	KeyStoreDirFlag = DirectoryFlag{
 		Name:  "keystore",
 		Usage: "Directory for the keystore (default = inside the datadir)",
@@ -1378,6 +1382,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
 		cfg.DatabaseCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
 	}
 	cfg.DatabaseHandles = makeDatabaseHandles()
+	if ctx.GlobalIsSet(AncientFlag.Name) {
+		cfg.DatabaseFreezer = ctx.GlobalString(AncientFlag.Name)
+	}
 
 	if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
 		Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
@@ -1566,7 +1573,7 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
 	if ctx.GlobalString(SyncModeFlag.Name) == "light" {
 		name = "lightchaindata"
 	}
-	chainDb, err := stack.OpenDatabase(name, cache, handles, "")
+	chainDb, err := stack.OpenDatabaseWithFreezer(name, cache, handles, "", "")
 	if err != nil {
 		Fatalf("Could not open database: %v", err)
 	}
diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go
index cc0591a4c..103f18f78 100644
--- a/core/rawdb/accessors_chain.go
+++ b/core/rawdb/accessors_chain.go
@@ -30,8 +30,11 @@ import (
 )
 
 // ReadCanonicalHash retrieves the hash assigned to a canonical block number.
-func ReadCanonicalHash(db ethdb.Reader, number uint64) common.Hash {
-	data, _ := db.Get(headerHashKey(number))
+func ReadCanonicalHash(db ethdb.AncientReader, number uint64) common.Hash {
+	data, _ := db.Ancient("hashes", number)
+	if len(data) == 0 {
+		data, _ = db.Get(headerHashKey(number))
+	}
 	if len(data) == 0 {
 		return common.Hash{}
 	}
@@ -52,6 +55,24 @@ func DeleteCanonicalHash(db ethdb.Writer, number uint64) {
 	}
 }
 
+// readAllHashes retrieves all the hashes assigned to blocks at a certain heights,
+// both canonical and reorged forks included.
+//
+// This method is a helper for the chain reader. It should never be exposed to the
+// outside world.
+func readAllHashes(db ethdb.Iteratee, number uint64) []common.Hash {
+	prefix := headerKeyPrefix(number)
+
+	hashes := make([]common.Hash, 0, 1)
+	it := db.NewIteratorWithPrefix(prefix)
+	for it.Next() {
+		if key := it.Key(); len(key) == len(prefix)+32 {
+			hashes = append(hashes, common.BytesToHash(key[len(key)-32:]))
+		}
+	}
+	return hashes
+}
+
 // ReadHeaderNumber returns the header number assigned to a hash.
 func ReadHeaderNumber(db ethdb.Reader, hash common.Hash) *uint64 {
 	data, _ := db.Get(headerNumberKey(hash))
@@ -129,13 +150,19 @@ func WriteFastTrieProgress(db ethdb.Writer, count uint64) {
 }
 
 // ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
-func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
-	data, _ := db.Get(headerKey(number, hash))
+func ReadHeaderRLP(db ethdb.AncientReader, hash common.Hash, number uint64) rlp.RawValue {
+	data, _ := db.Ancient("headers", number)
+	if len(data) == 0 {
+		data, _ = db.Get(headerKey(number, hash))
+	}
 	return data
 }
 
 // HasHeader verifies the existence of a block header corresponding to the hash.
-func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
+func HasHeader(db ethdb.AncientReader, hash common.Hash, number uint64) bool {
+	if has, err := db.Ancient("hashes", number); err == nil && common.BytesToHash(has) == hash {
+		return true
+	}
 	if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
 		return false
 	}
@@ -143,7 +170,7 @@ func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
 }
 
 // ReadHeader retrieves the block header corresponding to the hash.
-func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header {
+func ReadHeader(db ethdb.AncientReader, hash common.Hash, number uint64) *types.Header {
 	data := ReadHeaderRLP(db, hash, number)
 	if len(data) == 0 {
 		return nil
@@ -197,8 +224,11 @@ func deleteHeaderWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64)
 }
 
 // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
-func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
-	data, _ := db.Get(blockBodyKey(number, hash))
+func ReadBodyRLP(db ethdb.AncientReader, hash common.Hash, number uint64) rlp.RawValue {
+	data, _ := db.Ancient("bodies", number)
+	if len(data) == 0 {
+		data, _ = db.Get(blockBodyKey(number, hash))
+	}
 	return data
 }
 
@@ -210,7 +240,10 @@ func WriteBodyRLP(db ethdb.Writer, hash common.Hash, number uint64, rlp rlp.RawV
 }
 
 // HasBody verifies the existence of a block body corresponding to the hash.
-func HasBody(db ethdb.Reader, hash common.Hash, number uint64) bool {
+func HasBody(db ethdb.AncientReader, hash common.Hash, number uint64) bool {
+	if has, err := db.Ancient("hashes", number); err == nil && common.BytesToHash(has) == hash {
+		return true
+	}
 	if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
 		return false
 	}
@@ -218,7 +251,7 @@ func HasBody(db ethdb.Reader, hash common.Hash, number uint64) bool {
 }
 
 // ReadBody retrieves the block body corresponding to the hash.
-func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body {
+func ReadBody(db ethdb.AncientReader, hash common.Hash, number uint64) *types.Body {
 	data := ReadBodyRLP(db, hash, number)
 	if len(data) == 0 {
 		return nil
@@ -248,13 +281,16 @@ func DeleteBody(db ethdb.Writer, hash common.Hash, number uint64) {
 }
 
 // ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding.
-func ReadTdRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
-	data, _ := db.Get(headerTDKey(number, hash))
+func ReadTdRLP(db ethdb.AncientReader, hash common.Hash, number uint64) rlp.RawValue {
+	data, _ := db.Ancient("diffs", number)
+	if len(data) == 0 {
+		data, _ = db.Get(headerTDKey(number, hash))
+	}
 	return data
 }
 
 // ReadTd retrieves a block's total difficulty corresponding to the hash.
-func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int {
+func ReadTd(db ethdb.AncientReader, hash common.Hash, number uint64) *big.Int {
 	data := ReadTdRLP(db, hash, number)
 	if len(data) == 0 {
 		return nil
@@ -287,7 +323,10 @@ func DeleteTd(db ethdb.Writer, hash common.Hash, number uint64) {
 
 // HasReceipts verifies the existence of all the transaction receipts belonging
 // to a block.
-func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool {
+func HasReceipts(db ethdb.AncientReader, hash common.Hash, number uint64) bool {
+	if has, err := db.Ancient("hashes", number); err == nil && common.BytesToHash(has) == hash {
+		return true
+	}
 	if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil {
 		return false
 	}
@@ -295,15 +334,18 @@ func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool {
 }
 
 // ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
-func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
-	data, _ := db.Get(blockReceiptsKey(number, hash))
+func ReadReceiptsRLP(db ethdb.AncientReader, hash common.Hash, number uint64) rlp.RawValue {
+	data, _ := db.Ancient("receipts", number)
+	if len(data) == 0 {
+		data, _ = db.Get(blockReceiptsKey(number, hash))
+	}
 	return data
 }
 
 // ReadRawReceipts retrieves all the transaction receipts belonging to a block.
 // The receipt metadata fields are not guaranteed to be populated, so they
 // should not be used. Use ReadReceipts instead if the metadata is needed.
-func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
+func ReadRawReceipts(db ethdb.AncientReader, hash common.Hash, number uint64) types.Receipts {
 	// Retrieve the flattened receipt slice
 	data := ReadReceiptsRLP(db, hash, number)
 	if len(data) == 0 {
@@ -329,7 +371,7 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec
 // The current implementation populates these metadata fields by reading the receipts'
 // corresponding block body, so if the block body is not found it will return nil even
 // if the receipt itself is stored.
-func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts {
+func ReadReceipts(db ethdb.AncientReader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts {
 	// We're deriving many fields from the block body, retrieve beside the receipt
 	receipts := ReadRawReceipts(db, hash, number)
 	if receipts == nil {
@@ -377,7 +419,7 @@ func DeleteReceipts(db ethdb.Writer, hash common.Hash, number uint64) {
 //
 // Note, due to concurrent download of header and block body the header and thus
 // canonical hash can be stored in the database but the body data not (yet).
-func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
+func ReadBlock(db ethdb.AncientReader, hash common.Hash, number uint64) *types.Block {
 	header := ReadHeader(db, hash, number)
 	if header == nil {
 		return nil
@@ -413,7 +455,7 @@ func deleteBlockWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64)
 }
 
 // FindCommonAncestor returns the last common ancestor of two block headers
-func FindCommonAncestor(db ethdb.Reader, a, b *types.Header) *types.Header {
+func FindCommonAncestor(db ethdb.AncientReader, a, b *types.Header) *types.Header {
 	for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
 		a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
 		if a == nil {
diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go
index 423145a76..666e3edff 100644
--- a/core/rawdb/accessors_indexes.go
+++ b/core/rawdb/accessors_indexes.go
@@ -69,7 +69,7 @@ func DeleteTxLookupEntry(db ethdb.Writer, hash common.Hash) {
 
 // ReadTransaction retrieves a specific transaction from the database, along with
 // its added positional metadata.
-func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
+func ReadTransaction(db ethdb.AncientReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
 	blockNumber := ReadTxLookupEntry(db, hash)
 	if blockNumber == nil {
 		return nil, common.Hash{}, 0, 0
@@ -94,7 +94,7 @@ func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, com
 
 // ReadReceipt retrieves a specific transaction receipt from the database, along with
 // its added positional metadata.
-func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
+func ReadReceipt(db ethdb.AncientReader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
 	// Retrieve the context of the receipt based on the transaction hash
 	blockNumber := ReadTxLookupEntry(db, hash)
 	if blockNumber == nil {
diff --git a/core/rawdb/database.go b/core/rawdb/database.go
index b4c5dea70..0f994c3fd 100644
--- a/core/rawdb/database.go
+++ b/core/rawdb/database.go
@@ -22,10 +22,44 @@ import (
 	"github.com/ethereum/go-ethereum/ethdb/memorydb"
 )
 
+// freezerdb is a databse wrapper that enabled freezer data retrievals.
+type freezerdb struct {
+	ethdb.KeyValueStore
+	ethdb.Ancienter
+}
+
+// nofreezedb is a database wrapper that disables freezer data retrievals.
+type nofreezedb struct {
+	ethdb.KeyValueStore
+}
+
+// Frozen returns nil as we don't have a backing chain freezer.
+func (db *nofreezedb) Ancient(kind string, number uint64) ([]byte, error) {
+	return nil, errOutOfBounds
+}
+
 // NewDatabase creates a high level database on top of a given key-value data
 // store without a freezer moving immutable chain segments into cold storage.
 func NewDatabase(db ethdb.KeyValueStore) ethdb.Database {
-	return db
+	return &nofreezedb{
+		KeyValueStore: db,
+	}
+}
+
+// NewDatabaseWithFreezer creates a high level database on top of a given key-
+// value data store with a freezer moving immutable chain segments into cold
+// storage.
+func NewDatabaseWithFreezer(db ethdb.KeyValueStore, freezer string, namespace string) (ethdb.Database, error) {
+	frdb, err := newFreezer(freezer, namespace)
+	if err != nil {
+		return nil, err
+	}
+	go frdb.freeze(db)
+
+	return &freezerdb{
+		KeyValueStore: db,
+		Ancienter:     frdb,
+	}, nil
 }
 
 // NewMemoryDatabase creates an ephemeral in-memory key-value database without a
@@ -34,9 +68,9 @@ func NewMemoryDatabase() ethdb.Database {
 	return NewDatabase(memorydb.New())
 }
 
-// NewMemoryDatabaseWithCap creates an ephemeral in-memory key-value database with
-// an initial starting capacity, but without a freezer moving immutable chain
-// segments into cold storage.
+// NewMemoryDatabaseWithCap creates an ephemeral in-memory key-value database
+// with an initial starting capacity, but without a freezer moving immutable
+// chain segments into cold storage.
 func NewMemoryDatabaseWithCap(size int) ethdb.Database {
 	return NewDatabase(memorydb.NewWithCap(size))
 }
@@ -50,3 +84,18 @@ func NewLevelDBDatabase(file string, cache int, handles int, namespace string) (
 	}
 	return NewDatabase(db), nil
 }
+
+// NewLevelDBDatabaseWithFreezer creates a persistent key-value database with a
+// freezer moving immutable chain segments into cold storage.
+func NewLevelDBDatabaseWithFreezer(file string, cache int, handles int, freezer string, namespace string) (ethdb.Database, error) {
+	kvdb, err := leveldb.New(file, cache, handles, namespace)
+	if err != nil {
+		return nil, err
+	}
+	frdb, err := NewDatabaseWithFreezer(kvdb, freezer, namespace)
+	if err != nil {
+		kvdb.Close()
+		return nil, err
+	}
+	return frdb, nil
+}
diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go
new file mode 100644
index 000000000..4f227e3b7
--- /dev/null
+++ b/core/rawdb/freezer.go
@@ -0,0 +1,276 @@
+// Copyright 2018 The go-ethereum Authors
+// 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"
+	"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"
+)
+
+// errUnknownTable is returned if the user attempts to read from a table that is
+// not tracked by the freezer.
+var errUnknownTable = errors.New("unknown table")
+
+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
+
+	// freezerBlockGraduation is the number of confirmations a block must achieve
+	// before it becomes elligible for chain freezing. This must exceed any chain
+	// reorg depth, since the freezer also deletes all block siblings.
+	freezerBlockGraduation = 60000
+
+	// 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
+)
+
+// 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 {
+	tables map[string]*freezerTable // Data tables for storing everything
+	frozen uint64                   // Number of blocks already frozen
+}
+
+// newFreezer creates a chain freezer that moves ancient chain data into
+// append-only flat file containers.
+func newFreezer(datadir string, namespace string) (*freezer, error) {
+	// Create the initial freezer object
+	var (
+		readMeter  = metrics.NewRegisteredMeter(namespace+"ancient/read", nil)
+		writeMeter = metrics.NewRegisteredMeter(namespace+"ancient/write", nil)
+	)
+	// Open all the supported data tables
+	freezer := &freezer{
+		tables: make(map[string]*freezerTable),
+	}
+	for _, name := range []string{"hashes", "headers", "bodies", "receipts", "diffs"} {
+		table, err := newTable(datadir, name, readMeter, writeMeter)
+		if err != nil {
+			for _, table := range freezer.tables {
+				table.Close()
+			}
+			return nil, err
+		}
+		freezer.tables[name] = table
+	}
+	// Truncate all data tables to the same length
+	freezer.frozen = math.MaxUint64
+	for _, table := range freezer.tables {
+		if freezer.frozen > table.items {
+			freezer.frozen = table.items
+		}
+	}
+	for _, table := range freezer.tables {
+		if err := table.truncate(freezer.frozen); err != nil {
+			for _, table := range freezer.tables {
+				table.Close()
+			}
+			return nil, err
+		}
+	}
+	return freezer, nil
+}
+
+// Close terminates the chain freezer, unmapping all the data files.
+func (f *freezer) Close() error {
+	var errs []error
+	for _, table := range f.tables {
+		if err := table.Close(); err != nil {
+			errs = append(errs, err)
+		}
+	}
+	if errs != nil {
+		return fmt.Errorf("%v", errs)
+	}
+	return nil
+}
+
+// sync flushes all data tables to disk.
+func (f *freezer) sync() error {
+	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
+}
+
+// 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
+}
+
+// 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}
+
+	for {
+		// Retrieve the freezing threshold. In theory we're interested only in full
+		// blocks post-sync, but that would keep the live database enormous during
+		// dast sync. By picking the fast block, we still get to deep freeze all the
+		// final immutable data without having to wait for sync to finish.
+		hash := ReadHeadFastBlockHash(nfdb)
+		if hash == (common.Hash{}) {
+			log.Debug("Current fast block hash unavailable") // new chain, empty database
+			time.Sleep(freezerRecheckInterval)
+			continue
+		}
+		number := ReadHeaderNumber(nfdb, hash)
+		switch {
+		case number == nil:
+			log.Error("Current fast block number unavailable", "hash", hash)
+			time.Sleep(freezerRecheckInterval)
+			continue
+
+		case *number < freezerBlockGraduation:
+			log.Debug("Current fast block not old enough", "number", *number, "hash", hash, "delay", freezerBlockGraduation)
+			time.Sleep(freezerRecheckInterval)
+			continue
+
+		case *number-freezerBlockGraduation <= f.frozen:
+			log.Debug("Ancient blocks frozen already", "number", *number, "hash", hash, "frozen", f.frozen)
+			time.Sleep(freezerRecheckInterval)
+			continue
+		}
+		head := ReadHeader(nfdb, hash, *number)
+		if head == nil {
+			log.Error("Current fast block unavailable", "number", *number, "hash", hash)
+			time.Sleep(freezerRecheckInterval)
+			continue
+		}
+		// Seems we have data ready to be frozen, process in usable batches
+		limit := *number - freezerBlockGraduation
+		if limit-f.frozen > freezerBatchLimit {
+			limit = f.frozen + freezerBatchLimit
+		}
+		var (
+			start    = time.Now()
+			first    = f.frozen
+			ancients = make([]common.Hash, 0, limit)
+		)
+		for f.frozen < limit {
+			// Retrieves all the components of the canonical block
+			hash := ReadCanonicalHash(nfdb, f.frozen)
+			if hash == (common.Hash{}) {
+				log.Error("Canonical hash missing, can't freeze", "number", f.frozen)
+				break
+			}
+			header := ReadHeaderRLP(nfdb, hash, f.frozen)
+			if len(header) == 0 {
+				log.Error("Block header missing, can't freeze", "number", f.frozen, "hash", hash)
+				break
+			}
+			body := ReadBodyRLP(nfdb, hash, f.frozen)
+			if len(body) == 0 {
+				log.Error("Block body missing, can't freeze", "number", f.frozen, "hash", hash)
+				break
+			}
+			receipts := ReadReceiptsRLP(nfdb, hash, f.frozen)
+			if len(receipts) == 0 {
+				log.Error("Block receipts missing, can't freeze", "number", f.frozen, "hash", hash)
+				break
+			}
+			td := ReadTdRLP(nfdb, hash, f.frozen)
+			if len(td) == 0 {
+				log.Error("Total difficulty missing, can't freeze", "number", f.frozen, "hash", hash)
+				break
+			}
+			// Inject all the components into the relevant data tables
+			if err := f.tables["hashes"].Append(f.frozen, hash[:]); err != nil {
+				log.Error("Failed to deep freeze hash", "number", f.frozen, "hash", hash, "err", err)
+				break
+			}
+			if err := f.tables["headers"].Append(f.frozen, header); err != nil {
+				log.Error("Failed to deep freeze header", "number", f.frozen, "hash", hash, "err", err)
+				break
+			}
+			if err := f.tables["bodies"].Append(f.frozen, body); err != nil {
+				log.Error("Failed to deep freeze body", "number", f.frozen, "hash", hash, "err", err)
+				break
+			}
+			if err := f.tables["receipts"].Append(f.frozen, receipts); err != nil {
+				log.Error("Failed to deep freeze receipts", "number", f.frozen, "hash", hash, "err", err)
+				break
+			}
+			if err := f.tables["diffs"].Append(f.frozen, td); err != nil {
+				log.Error("Failed to deep freeze difficulty", "number", f.frozen, "hash", hash, "err", err)
+				break
+			}
+			log.Trace("Deep froze ancient block", "number", f.frozen, "hash", hash)
+			atomic.AddUint64(&f.frozen, 1) // Only modify atomically
+			ancients = append(ancients, hash)
+		}
+		// Batch of blocks have been frozen, flush them before wiping from leveldb
+		if err := f.sync(); err != nil {
+			log.Crit("Failed to flush frozen tables", "err", err)
+		}
+		// Wipe out all data from the active database
+		batch := db.NewBatch()
+		for number := first; number < f.frozen; number++ {
+			for _, hash := range readAllHashes(db, number) {
+				if hash == ancients[number-first] {
+					deleteBlockWithoutNumber(batch, hash, number)
+				} else {
+					DeleteBlock(batch, hash, number)
+				}
+			}
+		}
+		if err := batch.Write(); err != nil {
+			log.Crit("Failed to delete frozen items", "err", err)
+		}
+		// 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 {
+			time.Sleep(freezerRecheckInterval)
+		}
+	}
+}
diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go
new file mode 100644
index 000000000..546db0c65
--- /dev/null
+++ b/core/rawdb/freezer_table.go
@@ -0,0 +1,284 @@
+// Copyright 2018 The go-ethereum Authors
+// 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 (
+	"encoding/binary"
+	"errors"
+	"fmt"
+	"os"
+	"path/filepath"
+	"sync"
+
+	"github.com/ethereum/go-ethereum/common"
+	"github.com/ethereum/go-ethereum/log"
+	"github.com/ethereum/go-ethereum/metrics"
+	"github.com/golang/snappy"
+)
+
+var (
+	// errClosed is returned if an operation attempts to read from or write to the
+	// freezer table after it has already been closed.
+	errClosed = errors.New("closed")
+
+	// errOutOfBounds is returned if the item requested is not contained within the
+	// freezer table.
+	errOutOfBounds = errors.New("out of bounds")
+)
+
+// freezerTable represents a single chained data table within the freezer (e.g. blocks).
+// It consists of a data file (snappy encoded arbitrary data blobs) and an index
+// file (uncompressed 64 bit indices into the data file).
+type freezerTable struct {
+	content *os.File // File descriptor for the data content of the table
+	offsets *os.File // File descriptor for the index file of the table
+
+	items uint64 // Number of items stored in the table
+	bytes uint64 // Number of content bytes stored in the table
+
+	readMeter  metrics.Meter // Meter for measuring the effective amount of data read
+	writeMeter metrics.Meter // Meter for measuring the effective amount of data written
+
+	logger log.Logger   // Logger with database path and table name ambedded
+	lock   sync.RWMutex // Mutex protecting the data file descriptors
+}
+
+// newTable opens a freezer table, creating the data and index files if they are
+// non existent. Both files are truncated to the shortest common length to ensure
+// they don't go out of sync.
+func newTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter) (*freezerTable, error) {
+	// Ensure the containing directory exists and open the two data files
+	if err := os.MkdirAll(path, 0755); err != nil {
+		return nil, err
+	}
+	content, err := os.OpenFile(filepath.Join(path, name+".dat"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
+	if err != nil {
+		return nil, err
+	}
+	offsets, err := os.OpenFile(filepath.Join(path, name+".idx"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
+	if err != nil {
+		content.Close()
+		return nil, err
+	}
+	// Create the table and repair any past inconsistency
+	tab := &freezerTable{
+		content:    content,
+		offsets:    offsets,
+		readMeter:  readMeter,
+		writeMeter: writeMeter,
+		logger:     log.New("database", path, "table", name),
+	}
+	if err := tab.repair(); err != nil {
+		offsets.Close()
+		content.Close()
+		return nil, err
+	}
+	return tab, nil
+}
+
+// repair cross checks the content and the offsets file and truncates them to
+// be in sync with each other after a potential crash / data loss.
+func (t *freezerTable) repair() error {
+	// Create a temporary offset buffer to init files with and read offsts into
+	offset := make([]byte, 8)
+
+	// If we've just created the files, initialize the offsets with the 0 index
+	stat, err := t.offsets.Stat()
+	if err != nil {
+		return err
+	}
+	if stat.Size() == 0 {
+		if _, err := t.offsets.Write(offset); err != nil {
+			return err
+		}
+	}
+	// Ensure the offsets are a multiple of 8 bytes
+	if overflow := stat.Size() % 8; overflow != 0 {
+		t.offsets.Truncate(stat.Size() - overflow) // New file can't trigger this path
+	}
+	// Retrieve the file sizes and prepare for truncation
+	if stat, err = t.offsets.Stat(); err != nil {
+		return err
+	}
+	offsetsSize := stat.Size()
+
+	if stat, err = t.content.Stat(); err != nil {
+		return err
+	}
+	contentSize := stat.Size()
+
+	// Keep truncating both files until they come in sync
+	t.offsets.ReadAt(offset, offsetsSize-8)
+	contentExp := int64(binary.LittleEndian.Uint64(offset))
+
+	for contentExp != contentSize {
+		// Truncate the content file to the last offset pointer
+		if contentExp < contentSize {
+			t.logger.Warn("Truncating dangling content", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize))
+			if err := t.content.Truncate(contentExp); err != nil {
+				return err
+			}
+			contentSize = contentExp
+		}
+		// Truncate the offsets to point within the content file
+		if contentExp > contentSize {
+			t.logger.Warn("Truncating dangling offsets", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize))
+			if err := t.offsets.Truncate(offsetsSize - 8); err != nil {
+				return err
+			}
+			offsetsSize -= 8
+
+			t.offsets.ReadAt(offset, offsetsSize-8)
+			contentExp = int64(binary.LittleEndian.Uint64(offset))
+		}
+	}
+	// Ensure all reparation changes have been written to disk
+	if err := t.offsets.Sync(); err != nil {
+		return err
+	}
+	if err := t.content.Sync(); err != nil {
+		return err
+	}
+	// Update the item and byte counters and return
+	t.items = uint64(offsetsSize/8 - 1) // last index points to the end of the data file
+	t.bytes = uint64(contentSize)
+
+	t.logger.Debug("Chain freezer table opened", "items", t.items, "size", common.StorageSize(t.bytes))
+	return nil
+}
+
+// truncate discards any recent data above the provided threashold number.
+func (t *freezerTable) truncate(items uint64) error {
+	// If out item count is corrent, don't do anything
+	if t.items <= items {
+		return nil
+	}
+	// Something's out of sync, truncate the table's offset index
+	t.logger.Warn("Truncating freezer table", "items", t.items, "limit", items)
+	if err := t.offsets.Truncate(int64(items+1) * 8); err != nil {
+		return err
+	}
+	// Calculate the new expected size of the data file and truncate it
+	offset := make([]byte, 8)
+	t.offsets.ReadAt(offset, int64(items)*8)
+	expected := binary.LittleEndian.Uint64(offset)
+
+	if err := t.content.Truncate(int64(expected)); err != nil {
+		return err
+	}
+	// All data files truncated, set internal counters and return
+	t.items, t.bytes = items, expected
+	return nil
+}
+
+// Close unmaps all active memory mapped regions.
+func (t *freezerTable) Close() error {
+	t.lock.Lock()
+	defer t.lock.Unlock()
+
+	var errs []error
+	if err := t.offsets.Close(); err != nil {
+		errs = append(errs, err)
+	}
+	t.offsets = nil
+
+	if err := t.content.Close(); err != nil {
+		errs = append(errs, err)
+	}
+	t.content = nil
+
+	if errs != nil {
+		return fmt.Errorf("%v", errs)
+	}
+	return nil
+}
+
+// Append injects a binary blob at the end of the freezer table. The item index
+// is a precautionary parameter to ensure data correctness, but the table will
+// reject already existing data.
+//
+// Note, this method will *not* flush any data to disk so be sure to explicitly
+// fsync before irreversibly deleting data from the database.
+func (t *freezerTable) Append(item uint64, blob []byte) error {
+	// Ensure the table is still accessible
+	if t.offsets == nil || t.content == nil {
+		return errClosed
+	}
+	// Ensure only the next item can be written, nothing else
+	if t.items != item {
+		panic(fmt.Sprintf("appending unexpected item: want %d, have %d", t.items, item))
+	}
+	// Encode the blob and write it into the data file
+	blob = snappy.Encode(nil, blob)
+	if _, err := t.content.Write(blob); err != nil {
+		return err
+	}
+	t.bytes += uint64(len(blob))
+
+	offset := make([]byte, 8)
+	binary.LittleEndian.PutUint64(offset, t.bytes)
+	if _, err := t.offsets.Write(offset); err != nil {
+		return err
+	}
+	t.items++
+
+	t.writeMeter.Mark(int64(len(blob) + 8)) // 8 = 1 x 8 byte offset
+	return nil
+}
+
+// Retrieve looks up the data offset of an item with the given index and retrieves
+// the raw binary blob from the data file.
+func (t *freezerTable) Retrieve(item uint64) ([]byte, error) {
+	t.lock.RLock()
+	defer t.lock.RUnlock()
+
+	// Ensure the table and the item is accessible
+	if t.offsets == nil || t.content == nil {
+		return nil, errClosed
+	}
+	if t.items <= item {
+		return nil, errOutOfBounds
+	}
+	// Item reachable, retrieve the data content boundaries
+	offset := make([]byte, 8)
+	if _, err := t.offsets.ReadAt(offset, int64(item*8)); err != nil {
+		return nil, err
+	}
+	start := binary.LittleEndian.Uint64(offset)
+
+	if _, err := t.offsets.ReadAt(offset, int64((item+1)*8)); err != nil {
+		return nil, err
+	}
+	end := binary.LittleEndian.Uint64(offset)
+
+	// Retrieve the data itself, decompress and return
+	blob := make([]byte, end-start)
+	if _, err := t.content.ReadAt(blob, int64(start)); err != nil {
+		return nil, err
+	}
+	t.readMeter.Mark(int64(len(blob) + 16)) // 16 = 2 x 8 byte offset
+	return snappy.Decode(nil, blob)
+}
+
+// Sync pushes any pending data from memory out to disk. This is an expensive
+// operation, so use it with care.
+func (t *freezerTable) Sync() error {
+	if err := t.offsets.Sync(); err != nil {
+		return err
+	}
+	return t.content.Sync()
+}
diff --git a/core/rawdb/table.go b/core/rawdb/table.go
index 0e50db7c9..0b5e08b20 100644
--- a/core/rawdb/table.go
+++ b/core/rawdb/table.go
@@ -50,6 +50,12 @@ func (t *table) Get(key []byte) ([]byte, error) {
 	return t.db.Get(append([]byte(t.prefix), key...))
 }
 
+// Ancient is a noop passthrough that just forwards the request to the underlying
+// database.
+func (t *table) Ancient(kind string, number uint64) ([]byte, error) {
+	return t.db.Ancient(kind, number)
+}
+
 // Put inserts the given value into the database at a prefixed version of the
 // provided key.
 func (t *table) Put(key []byte, value []byte) error {
diff --git a/eth/backend.go b/eth/backend.go
index f69615776..6b9c98bf2 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -120,7 +120,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
 	log.Info("Allocated trie memory caches", "clean", common.StorageSize(config.TrieCleanCache)*1024*1024, "dirty", common.StorageSize(config.TrieDirtyCache)*1024*1024)
 
 	// Assemble the Ethereum object
-	chainDb, err := ctx.OpenDatabase("chaindata", config.DatabaseCache, config.DatabaseHandles, "eth/db/chaindata/")
+	chainDb, err := ctx.OpenDatabaseWithFreezer("chaindata", config.DatabaseCache, config.DatabaseHandles, config.DatabaseFreezer, "eth/db/chaindata/")
 	if err != nil {
 		return nil, err
 	}
diff --git a/eth/config.go b/eth/config.go
index fbe6597b6..ccd5674a7 100644
--- a/eth/config.go
+++ b/eth/config.go
@@ -114,6 +114,7 @@ type Config struct {
 	SkipBcVersionCheck bool `toml:"-"`
 	DatabaseHandles    int  `toml:"-"`
 	DatabaseCache      int
+	DatabaseFreezer    string
 
 	TrieCleanCache int
 	TrieDirtyCache int
diff --git a/ethdb/database.go b/ethdb/database.go
index bab99aed1..764e304e3 100644
--- a/ethdb/database.go
+++ b/ethdb/database.go
@@ -67,10 +67,23 @@ type KeyValueStore interface {
 	io.Closer
 }
 
+// Ancienter wraps the Ancient method for a backing immutable chain data store.
+type Ancienter interface {
+	// Ancient retrieves an ancient binary blob from the append-only immutable files.
+	Ancient(kind string, number uint64) ([]byte, error)
+}
+
+// AncientReader contains the methods required to access both key-value as well as
+// immutable ancient data.
+type AncientReader interface {
+	Reader
+	Ancienter
+}
+
 // Database contains all the methods required by the high level database to not
 // only access the key-value data store but also the chain freezer.
 type Database interface {
-	Reader
+	AncientReader
 	Writer
 	Batcher
 	Iteratee
diff --git a/node/node.go b/node/node.go
index 78bb492f0..08daeeee0 100644
--- a/node/node.go
+++ b/node/node.go
@@ -614,6 +614,26 @@ func (n *Node) OpenDatabase(name string, cache, handles int, namespace string) (
 	return rawdb.NewLevelDBDatabase(n.config.ResolvePath(name), cache, handles, namespace)
 }
 
+// OpenDatabaseWithFreezer opens an existing database with the given name (or
+// creates one if no previous can be found) from within the node's data directory,
+// also attaching a chain freezer to it that moves ancient chain data from the
+// database to immutable append-only files. If the node is an ephemeral one, a
+// memory database is returned.
+func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, freezer, namespace string) (ethdb.Database, error) {
+	if n.config.DataDir == "" {
+		return rawdb.NewMemoryDatabase(), nil
+	}
+	root := n.config.ResolvePath(name)
+
+	switch {
+	case freezer == "":
+		freezer = filepath.Join(root, "ancient")
+	case !filepath.IsAbs(freezer):
+		freezer = n.config.ResolvePath(freezer)
+	}
+	return rawdb.NewLevelDBDatabaseWithFreezer(root, cache, handles, freezer, namespace)
+}
+
 // ResolvePath returns the absolute path of a resource in the instance directory.
 func (n *Node) ResolvePath(x string) string {
 	return n.config.ResolvePath(x)
diff --git a/node/service.go b/node/service.go
index 24f809743..4dea00995 100644
--- a/node/service.go
+++ b/node/service.go
@@ -17,6 +17,7 @@
 package node
 
 import (
+	"path/filepath"
 	"reflect"
 
 	"github.com/ethereum/go-ethereum/accounts"
@@ -44,11 +45,27 @@ func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, nam
 	if ctx.config.DataDir == "" {
 		return rawdb.NewMemoryDatabase(), nil
 	}
-	db, err := rawdb.NewLevelDBDatabase(ctx.config.ResolvePath(name), cache, handles, namespace)
-	if err != nil {
-		return nil, err
+	return rawdb.NewLevelDBDatabase(ctx.config.ResolvePath(name), cache, handles, namespace)
+}
+
+// OpenDatabaseWithFreezer opens an existing database with the given name (or
+// creates one if no previous can be found) from within the node's data directory,
+// also attaching a chain freezer to it that moves ancient chain data from the
+// database to immutable append-only files. If the node is an ephemeral one, a
+// memory database is returned.
+func (ctx *ServiceContext) OpenDatabaseWithFreezer(name string, cache int, handles int, freezer string, namespace string) (ethdb.Database, error) {
+	if ctx.config.DataDir == "" {
+		return rawdb.NewMemoryDatabase(), nil
+	}
+	root := ctx.config.ResolvePath(name)
+
+	switch {
+	case freezer == "":
+		freezer = filepath.Join(root, "ancient")
+	case !filepath.IsAbs(freezer):
+		freezer = ctx.config.ResolvePath(freezer)
 	}
-	return db, nil
+	return rawdb.NewLevelDBDatabaseWithFreezer(root, cache, handles, freezer, namespace)
 }
 
 // ResolvePath resolves a user path into the data directory if that was relative
-- 
GitLab