Skip to content

Commit

Permalink
multi: Remove UTXO db in BlockChain and UtxoCache.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rstaudt2 committed May 22, 2021
1 parent 76625b4 commit 9d0af6b
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 43 deletions.
10 changes: 0 additions & 10 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2114,11 +2113,6 @@ type Config struct {
// This field is required.
UtxoBackend UtxoBackend

// 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.
//
Expand Down Expand Up @@ -2190,9 +2184,6 @@ func New(ctx context.Context, config *Config) (*BlockChain, error) {
if config.UtxoBackend == nil {
return nil, AssertError("blockchain.New UTXO backend 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")
}
Expand Down Expand Up @@ -2233,7 +2224,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,
Expand Down
2 changes: 0 additions & 2 deletions blockchain/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ func chainSetup(dbName string, params *chaincfg.Params) (*BlockChain, func(), er
chain, err := New(context.Background(),
&Config{
DB: db,
UtxoDB: utxoDb,
UtxoBackend: utxoBackend,
ChainParams: &paramsCopy,
TimeSource: NewMedianTime(),
SigCache: sigCache,
UtxoCache: NewUtxoCache(&UtxoCacheConfig{
Backend: utxoBackend,
DB: utxoDb,
FlushBlockDB: func() error {
// Don't flush to disk since it is slow and this is used in a lot of
// tests.
Expand Down
1 change: 0 additions & 1 deletion blockchain/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 0 additions & 2 deletions blockchain/fullblocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,11 @@ func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain,
&blockchain.Config{
DB: db,
UtxoBackend: utxoBackend,
UtxoDB: utxoDb,
ChainParams: &paramsCopy,
TimeSource: blockchain.NewMedianTime(),
SigCache: sigCache,
UtxoCache: blockchain.NewUtxoCache(&blockchain.UtxoCacheConfig{
Backend: utxoBackend,
DB: utxoDb,
FlushBlockDB: func() error {
// Don't flush to disk since it is slow and this is used in a lot of
// tests.
Expand Down
28 changes: 9 additions & 19 deletions blockchain/utxocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,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
Expand Down Expand Up @@ -205,11 +201,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
Expand All @@ -235,7 +226,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)),
Expand Down Expand Up @@ -331,12 +321,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
Expand Down
5 changes: 0 additions & 5 deletions blockchain/utxocache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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
})
Expand Down
2 changes: 0 additions & 2 deletions blockchain/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,10 @@ func TestCheckBlockHeaderContext(t *testing.T) {
&Config{
DB: db,
UtxoBackend: utxoBackend,
UtxoDB: utxoDb,
ChainParams: params,
TimeSource: NewMedianTime(),
UtxoCache: NewUtxoCache(&UtxoCacheConfig{
Backend: utxoBackend,
DB: utxoDb,
FlushBlockDB: func() error {
return nil
},
Expand Down
2 changes: 0 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3405,15 +3405,13 @@ func newServer(ctx context.Context, listenAddrs []string, db database.DB,
utxoBackend := blockchain.NewLevelDbUtxoBackend(utxoDb)
utxoCache := blockchain.NewUtxoCache(&blockchain.UtxoCacheConfig{
Backend: utxoBackend,
DB: utxoDb,
FlushBlockDB: s.db.Flush,
MaxSize: uint64(cfg.UtxoCacheMaxSize) * 1024 * 1024,
})
s.chain, err = blockchain.New(ctx,
&blockchain.Config{
DB: s.db,
UtxoBackend: utxoBackend,
UtxoDB: utxoDb,
ChainParams: s.chainParams,
Checkpoints: checkpoints,
TimeSource: s.timeSource,
Expand Down

0 comments on commit 9d0af6b

Please sign in to comment.