From 652ce2bbbec76a89d99e8ef8956ba0ab800e9856 Mon Sep 17 00:00:00 2001 From: Ryan Staudt Date: Tue, 18 May 2021 05:45:11 -0500 Subject: [PATCH] multi: Remove UTXO db in BlockChain and UtxoCache. This removes the UTXO db from the BlockChain and UtxoCache types. BlockChain now only accesses the UTXO db through the UtxoCache and the UtxoCache now only accesses the UTXO db through the UtxoBackend. --- blockchain/chain.go | 10 ---------- blockchain/common_test.go | 2 -- blockchain/example_test.go | 1 - blockchain/fullblocks_test.go | 2 -- blockchain/utxocache.go | 28 +++++++++------------------- blockchain/utxocache_test.go | 5 ----- blockchain/validate_test.go | 2 -- server.go | 2 -- 8 files changed, 9 insertions(+), 43 deletions(-) diff --git a/blockchain/chain.go b/blockchain/chain.go index ffe812c2b8..541696358d 100644 --- a/blockchain/chain.go +++ b/blockchain/chain.go @@ -137,7 +137,6 @@ type BlockChain struct { deploymentVers map[string]uint32 db database.DB dbInfo *databaseInfo - utxoDb database.DB chainParams *chaincfg.Params timeSource MedianTimeSource notifications NotificationCallback @@ -2109,11 +2108,6 @@ type Config struct { // This field is required. DB database.DB - // UtxoDB defines the database which houses the UTXO set. - // - // This field is required. - UtxoDB database.DB - // ChainParams identifies which chain parameters the chain is associated // with. // @@ -2182,9 +2176,6 @@ func New(ctx context.Context, config *Config) (*BlockChain, error) { if config.DB == nil { return nil, AssertError("blockchain.New database is nil") } - if config.UtxoDB == nil { - return nil, AssertError("blockchain.New UTXO database is nil") - } if config.ChainParams == nil { return nil, AssertError("blockchain.New chain parameters nil") } @@ -2225,7 +2216,6 @@ func New(ctx context.Context, config *Config) (*BlockChain, error) { checkpointsByHeight: checkpointsByHeight, deploymentVers: deploymentVers, db: config.DB, - utxoDb: config.UtxoDB, chainParams: params, timeSource: config.TimeSource, notifications: config.Notifications, diff --git a/blockchain/common_test.go b/blockchain/common_test.go index 2137152b96..863f4c5cc9 100644 --- a/blockchain/common_test.go +++ b/blockchain/common_test.go @@ -140,13 +140,11 @@ func chainSetup(dbName string, params *chaincfg.Params) (*BlockChain, func(), er chain, err := New(context.Background(), &Config{ DB: db, - UtxoDB: utxoDb, ChainParams: ¶msCopy, TimeSource: NewMedianTime(), SigCache: sigCache, UtxoCache: NewUtxoCache(&UtxoCacheConfig{ Backend: NewLevelDbUtxoBackend(utxoDb), - DB: utxoDb, FlushBlockDB: func() error { // Don't flush to disk since it is slow and this is used in a lot of // tests. diff --git a/blockchain/example_test.go b/blockchain/example_test.go index bc6165e26f..f3c0d5bafe 100644 --- a/blockchain/example_test.go +++ b/blockchain/example_test.go @@ -54,7 +54,6 @@ func ExampleBlockChain_ProcessBlock() { TimeSource: blockchain.NewMedianTime(), UtxoCache: blockchain.NewUtxoCache(&blockchain.UtxoCacheConfig{ Backend: blockchain.NewLevelDbUtxoBackend(db), - DB: db, FlushBlockDB: func() error { return nil }, diff --git a/blockchain/fullblocks_test.go b/blockchain/fullblocks_test.go index 812e0bc377..7e537fa158 100644 --- a/blockchain/fullblocks_test.go +++ b/blockchain/fullblocks_test.go @@ -132,13 +132,11 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, chain, err := blockchain.New(context.Background(), &blockchain.Config{ DB: db, - UtxoDB: utxoDb, ChainParams: ¶msCopy, TimeSource: blockchain.NewMedianTime(), SigCache: sigCache, UtxoCache: blockchain.NewUtxoCache(&blockchain.UtxoCacheConfig{ Backend: blockchain.NewLevelDbUtxoBackend(utxoDb), - DB: utxoDb, FlushBlockDB: func() error { // Don't flush to disk since it is slow and this is used in a lot of // tests. diff --git a/blockchain/utxocache.go b/blockchain/utxocache.go index b3684729e1..a41203041d 100644 --- a/blockchain/utxocache.go +++ b/blockchain/utxocache.go @@ -125,33 +125,29 @@ type UtxoCacher interface { } // UtxoCache is an unspent transaction output cache that sits on top of the -// utxo set database and provides significant runtime performance benefits at +// utxo set backend and provides significant runtime performance benefits at // the cost of some additional memory usage. It drastically reduces the amount // of reading and writing to disk, especially during initial block download when // a very large number of blocks are being processed in quick succession. // // The UtxoCache is a read-through cache. All utxo reads go through the cache. // When there is a cache miss, the cache loads the missing data from the -// database, caches it, and returns it to the caller. +// backend, caches it, and returns it to the caller. // // The UtxoCache is a write-back cache. Writes to the cache are acknowledged -// by the cache immediately but are only periodically flushed to the database. +// by the cache immediately but are only periodically flushed to the backend. // This allows intermediate steps to effectively be skipped. For example, a // utxo that is created and then spent in between flushes never needs to be -// written to the utxo set in the database. +// written to the utxo set in the backend. // -// Due to the write-back nature of the cache, at any given time the database +// Due to the write-back nature of the cache, at any given time the backend // may not be in sync with the cache, and therefore all utxo reads and writes -// MUST go through the cache, and never read or write to the database directly. +// MUST go through the cache, and never read or write to the backend directly. type UtxoCache struct { // backend is the backend that contains the UTXO set. It is set when the // instance is created and is not changed afterward. backend UtxoBackend - // db is the database that contains the utxo set. It is set when the instance - // is created and is not changed afterward. - db database.DB - // flushBlockDB defines the function to use to flush the block database to // disk. The block database is always flushed to disk before the UTXO cache // writes to disk in order to maintain a recoverable state in the event of an @@ -216,11 +212,6 @@ type UtxoCacheConfig struct { // This field is required. Backend UtxoBackend - // DB defines the database which houses the utxo set. - // - // This field is required. - DB database.DB - // FlushBlockDB defines the function to use to flush the block database to // disk. The block database is always flushed to disk before the UTXO cache // writes to disk in order to maintain a recoverable state in the event of an @@ -246,7 +237,6 @@ func NewUtxoCache(config *UtxoCacheConfig) *UtxoCache { return &UtxoCache{ backend: config.Backend, - db: config.DB, flushBlockDB: config.FlushBlockDB, maxSize: config.MaxSize, entries: make(map[wire.OutPoint]*UtxoEntry, uint64(maxEntries)), @@ -342,12 +332,12 @@ func (c *UtxoCache) spendEntry(outpoint wire.OutPoint) { } // If the entry is fresh, and is now being spent, it can safely be removed. - // This is an optimization to skip writing to the database for outputs that - // are added and spent in between flushes to the database. + // This is an optimization to skip writing to the backend for outputs that + // are added and spent in between flushes to the backend. if cachedEntry.isFresh() { // The entry in the map is marked as nil rather than deleting it so that // subsequent lookups for the outpoint will still result in a cache hit and - // avoid querying the database. + // avoid querying the backend. c.entries[outpoint] = nil c.totalEntrySize -= cachedEntry.size() return diff --git a/blockchain/utxocache_test.go b/blockchain/utxocache_test.go index 44d52fdaf0..52ba8cb59e 100644 --- a/blockchain/utxocache_test.go +++ b/blockchain/utxocache_test.go @@ -515,7 +515,6 @@ func TestFetchEntry(t *testing.T) { // Create a utxo cache with the cached entries specified by the test. utxoCache := createTestUtxoCache(t, test.cachedEntries) utxoCache.backend = backend - utxoCache.db = backend.db wantTotalEntrySize := utxoCache.totalEntrySize // Add entries specified by the test to the test backend. @@ -612,7 +611,6 @@ func TestFetchEntries(t *testing.T) { // Create a utxo cache with the cached entries specified by the test. utxoCache := createTestUtxoCache(t, test.cachedEntries) utxoCache.backend = backend - utxoCache.db = backend.db // Add entries specified by the test to the test backend. err := backend.PutUtxos(test.backendEntries, &utxoSetState{}) @@ -962,7 +960,6 @@ func TestMaybeFlush(t *testing.T) { // Create a utxo cache with the cached entries specified by the test. utxoCache := createTestUtxoCache(t, test.cachedEntries) utxoCache.backend = backend - utxoCache.db = backend.db utxoCache.maxSize = test.maxSize utxoCache.lastEvictionHeight = test.lastEvictionHeight utxoCache.lastFlushHash = *test.lastFlushHash @@ -1074,7 +1071,6 @@ func TestInitialize(t *testing.T) { resetTestUtxoCache := func() *testUtxoCache { testUtxoCache := newTestUtxoCache(&UtxoCacheConfig{ Backend: backend, - DB: backend.db, FlushBlockDB: g.chain.db.Flush, MaxSize: 100 * 1024 * 1024, // 100 MiB }) @@ -1223,7 +1219,6 @@ func TestShutdownUtxoCache(t *testing.T) { backend := createTestUtxoBackend(t) testUtxoCache := newTestUtxoCache(&UtxoCacheConfig{ Backend: backend, - DB: backend.db, FlushBlockDB: g.chain.db.Flush, MaxSize: 100 * 1024 * 1024, // 100 MiB }) diff --git a/blockchain/validate_test.go b/blockchain/validate_test.go index a9bf9baf70..6ca181bf6a 100644 --- a/blockchain/validate_test.go +++ b/blockchain/validate_test.go @@ -294,12 +294,10 @@ func TestCheckBlockHeaderContext(t *testing.T) { chain, err := New(context.Background(), &Config{ DB: db, - UtxoDB: utxoDb, ChainParams: params, TimeSource: NewMedianTime(), UtxoCache: NewUtxoCache(&UtxoCacheConfig{ Backend: NewLevelDbUtxoBackend(utxoDb), - DB: utxoDb, FlushBlockDB: func() error { return nil }, diff --git a/server.go b/server.go index 42ba0c6bd2..e1e471239b 100644 --- a/server.go +++ b/server.go @@ -3404,14 +3404,12 @@ func newServer(ctx context.Context, listenAddrs []string, db database.DB, // Create a new block chain instance with the appropriate configuration. utxoCache := blockchain.NewUtxoCache(&blockchain.UtxoCacheConfig{ Backend: blockchain.NewLevelDbUtxoBackend(utxoDb), - DB: utxoDb, FlushBlockDB: s.db.Flush, MaxSize: uint64(cfg.UtxoCacheMaxSize) * 1024 * 1024, }) s.chain, err = blockchain.New(ctx, &blockchain.Config{ DB: s.db, - UtxoDB: utxoDb, ChainParams: s.chainParams, Checkpoints: checkpoints, TimeSource: s.timeSource,