-
Notifications
You must be signed in to change notification settings - Fork 128
cache: add cache package and AddressCache for dcrpg #1051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
2a17334
to
e9d77b0
Compare
ed32871
to
1ca4477
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annotated for review.
return | ||
} | ||
|
||
height := c.BlockData.ChainDB.Height() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no reason for DB queries when the best block info is stored in ChainDB.
db/cache/addresscache.go
Outdated
} | ||
} | ||
|
||
// TryLock will attempt to obtain either an exclusive updating lock. Trylock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will revise this comment
func (d *AddressCacheItem) Balance() (*dbtypes.AddressBalance, *BlockID) { | ||
d.RLock() | ||
defer d.RUnlock() | ||
if d.balance == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important to return a nil *BlockID
in the event of a cache miss as checking this pointer is now the method for detecting a cache miss.
db/cache/addresscache.go
Outdated
DevAddress string | ||
} | ||
|
||
// NewAddressCache constructs a AddressCache. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to doc the capacity arg
db/cache/addresscache.go
Outdated
func (ac *AddressCache) Transactions(addr string, N, offset int64, txnType dbtypes.AddrTxnViewType) ([]*dbtypes.AddressRow, *BlockID, error) { | ||
aci := ac.addressCacheItem(addr) | ||
if aci == nil { | ||
return nil, nil, nil /*fmt.Errorf("uninitialized address cache")*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove this comment. It's just a cache miss here, not an "uninitialized address cache".
db/dcrpg/pgblockchain.go
Outdated
@@ -2540,6 +2658,17 @@ func (pgb *ChainDB) StoreBlock(msgBlock *wire.MsgBlock, winningTickets []string, | |||
numVouts = errStk.numVouts + errReg.numVouts | |||
numAddresses = errStk.numAddresses + errReg.numAddresses | |||
|
|||
// Merge the affected addresses. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should explain this is for cache expiration
db/dcrpg/pgblockchain.go
Outdated
@@ -2578,9 +2707,9 @@ func (pgb *ChainDB) StoreBlock(msgBlock *wire.MsgBlock, winningTickets []string, | |||
return | |||
} | |||
|
|||
// If not in batch sync, lazy update the dev fund balance | |||
// If not in batch sync, lazy update the dev fund balance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should update this comment too.
@@ -1585,12 +1616,11 @@ func scanAddressQueryRows(rows *sql.Rows, queryType int) (ids []uint64, addressR | |||
for rows.Next() { | |||
var id uint64 | |||
var addr dbtypes.AddressRow | |||
var txHash sql.NullString | |||
var blockTime dbtypes.TimeDef | |||
var matchingTxHash sql.NullString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
txHash
is never nul or empty. matchingTxHash
is often empty, but I'm not even sure it is allowed to be null. This is just to be safe.
db/dcrpg/queries.go
Outdated
addr.AtomsCredit = addr.Value | ||
case debitQuery: | ||
// addr.IsFunding == false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can remove these two commented lines. IsFunding is set directly by rows.Scan
"", ExpStatusError) | ||
return | ||
} | ||
maxHeight = exp.explorerSource.Height() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another unnecessary DB query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having trouble finding anything to complain about. Looks great.
use vouts primary key in SelectAddressUnspentWithTxn avoid height query in AddressUTXO, using latest stored height instead fix: RetrieveAddressUTXOs forgot the block time rename AddrTxnType to AddrTxnViewType add addresscache.go with AddressCache and AddressCacheItem Use AddressCache in ChainDB. Remove DevFundBalance since it benefits from the general AddressCache. Remove addressCounts since AddressCache includes balance. Remove unnecessary TimeDefs in scanAddressMergedRows and scanAddressQueryRows. To update an address' data, add RetrieveAllMainchainAddressTxns and RetrieveAllMainchainAddressMergedTxns, which do not use offset or N. Add Height() int64 to explorer/explorerDataSource interface to use the no-query getter. This speeds up BlockHashPathOrIndexCtx middleware. make cache package in db/cache set capacity of cap, and respect it when adding items When enforcing capacity, do not purge the project fund cache item. remove redundant and conflicting functions
9c5d147
to
ae0fa96
Compare
Add a new
cache
package, starting with addresscache.go, which includesAddressCache
andAddressCacheItem
.Use
AddressCache
inChainDB
.Remove
DevFundBalance
since it benefits from the generalAddressCache
.Remove
addressCounts
sinceAddressCache
includes balance.To update an address' data, add
RetrieveAllMainchainAddressTxns
andRetrieveAllMainchainAddressMergedTxns
, which do not useoffset
orN
.Also cache transaction view type row counts via the new
(*ChainDB).CountTransactions
function, which use cache or DB queries toget the row counts for any of the supported txn view types.
Also:
vouts
table primary key inSelectAddressUnspentWithTxn
.AddressUTXO
, using latest stored height instead.RetrieveAddressUTXOs
forgot the block timeAddrTxnType
toAddrTxnViewType
.TimeDefs
inscanAddressMergedRows
andscanAddressQueryRows
.Height() int64
toexplorer/explorerDataSource
interface to use theno-query getter. This speeds up
BlockHashPathOrIndexCtx
middleware.