Skip to content

Commit

Permalink
Add a header and block cache. (#188)
Browse files Browse the repository at this point in the history
* Add blockheader and block cache

* Rig up block and header cache setters/getters

* Remove blocks missing cache since the height mapping is now cached via header cache

* Move peer manager out of db

* tbc: move peer management to separate type/file

* ristretto makes my gums bleed; try lru despite being a generic turd

* tbc: use rlock/unlock for rwmutex in peer manager

* tbc: fix slice length in PeersRandom and append in handleAddr(V2)

* Make cache setable

* catch ctx cancel *shakesfistatwire*

* remove unused error

* fixup fallout from lru configuration

* Add option to disable LRU block and header cache

* tbcd: add BlockCache and BlockheaderCache to default config

---------

Co-authored-by: Joshua Sing <joshua@bloq.com>
  • Loading branch information
marcopeereboom and joshuasing authored Aug 13, 2024
1 parent ccaf4d7 commit 95925bd
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 276 deletions.
6 changes: 3 additions & 3 deletions cmd/hemictl/hemictl.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func tbcdb() error {

levelDBHome := "~/.tbcd" // XXX
network := "testnet3"
db, err := level.New(ctx, filepath.Join(levelDBHome, network))
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network)))
if err != nil {
return err
}
Expand All @@ -324,7 +324,7 @@ func tbcdb() error {

levelDBHome := "~/.tbcd" // XXX
network := "testnet3"
db, err := level.New(ctx, filepath.Join(levelDBHome, network))
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network)))
if err != nil {
return err
}
Expand All @@ -342,7 +342,7 @@ func tbcdb() error {

levelDBHome := "~/.tbcd" // XXX
network := "testnet3"
db, err := level.New(ctx, filepath.Join(levelDBHome, network))
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network)))
if err != nil {
return err
}
Expand Down
14 changes: 13 additions & 1 deletion cmd/tbcd/tbcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ var (
Help: "enable auto utxo and tx indexes",
Print: config.PrintAll,
},
"TBC_BLOCK_CACHE": config.Config{
Value: &cfg.BlockCache,
DefaultValue: 250,
Help: "number of cached blocks",
Print: config.PrintAll,
},
"TBC_BLOCKHEADER_CACHE": config.Config{
Value: &cfg.BlockheaderCache,
DefaultValue: int(1e6),
Help: "number of cached blockheaders",
Print: config.PrintAll,
},
"TBC_BLOCK_SANITY": config.Config{
Value: &cfg.BlockSanity,
DefaultValue: false,
Expand All @@ -65,7 +77,7 @@ var (
},
"TBC_MAX_CACHED_TXS": config.Config{
Value: &cfg.MaxCachedTxs,
DefaultValue: 1000000,
DefaultValue: int(1e6),
Help: "maximum cached utxos and/or txs during indexing",
Print: config.PrintAll,
},
Expand Down
1 change: 0 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ var (
ErrDuplicate = DuplicateError("duplicate")
ErrNotFound = NotFoundError("not found")
ErrValidation = ValidationError("validation")
ErrZeroRows = ZeroRowsError("zero rows affected")
)

// ByteArray is a type that corresponds to BYTEA in a database. It supports
Expand Down
16 changes: 1 addition & 15 deletions database/tbcd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ type Database interface {
BlocksByTxId(ctx context.Context, txId []byte) ([]BlockHash, error)
SpentOutputsByTxId(ctx context.Context, txId []byte) ([]SpentInfo, error)

// Peer manager
PeersStats(ctx context.Context) (int, int) // good, bad count
PeersInsert(ctx context.Context, peers []Peer) error // insert or update
PeerDelete(ctx context.Context, host, port string) error // remove peer
PeersRandom(ctx context.Context, count int) ([]Peer, error)

// ScriptHash returns the sha256 of PkScript for the provided outpoint.
BalanceByScriptHash(ctx context.Context, sh ScriptHash) (uint64, error)
ScriptHashByOutpoint(ctx context.Context, op Outpoint) (*ScriptHash, error)
Expand Down Expand Up @@ -139,7 +133,7 @@ func (bh BlockHeader) ParentHash() *chainhash.Hash {
// Block contains a raw bitcoin block and its corresponding hash.
type Block struct {
Hash database.ByteArray
Block database.ByteArray
Block database.ByteArray // this needs to be converted to either wire or btcutil
}

// BlockIdentifier uniquely identifies a block using it's hash and height.
Expand All @@ -154,14 +148,6 @@ type SpentInfo struct {
InputIndex uint32
}

// Peer
type Peer struct {
Host string
Port string
LastAt database.Timestamp `deep:"-"` // Last time connected
CreatedAt database.Timestamp `deep:"-"`
}

// XXX we can probably save a bunch of bcopy if we construct the key directly
// for the db. Peek at the s + t cache which does this.

Expand Down
Loading

0 comments on commit 95925bd

Please sign in to comment.