Skip to content
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

ChainDB support address index #559

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 84 additions & 5 deletions database/chaindb/chaindb_index.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package chaindb

import (
"encoding/binary"
"github.com/Qitmeer/qng/common/hash"
"github.com/Qitmeer/qng/consensus/model"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/database/common"
"github.com/Qitmeer/qng/database/rawdb"
"github.com/Qitmeer/qng/meerdag"
"math"
"github.com/Qitmeer/qng/services/index"
"github.com/ethereum/go-ethereum/ethdb"
)

func (cdb *ChainDB) PutTxIdxEntrys(sblock *types.SerializedBlock, block model.Block) error {
Expand Down Expand Up @@ -127,25 +129,102 @@ func (cdb *ChainDB) CleanInvalidTxIdx() error {
}

func (cdb *ChainDB) GetAddrIdxTip() (*hash.Hash, uint, error) {
return nil, math.MaxUint32, nil
return rawdb.ReadAddrIdxTip(cdb.db)
}

func (cdb *ChainDB) PutAddrIdxTip(bh *hash.Hash, order uint) error {
return nil
return rawdb.WriteAddrIdxTip(cdb.db, bh, order)
}

func (cdb *ChainDB) PutAddrIdx(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error {
// Build all of the address to transaction mappings in a local map.
addrsToTxns := make(index.WriteAddrIdxData)
index.AddrIndexBlock(addrsToTxns, sblock, stxos)
b := &bucket{db: cdb.db}
// Add all of the index entries for each address.
for addrKey, txIdxs := range addrsToTxns {
for _, txIdx := range txIdxs {
// Switch to using the newest block ID for the stake transactions,
// since these are not from the parent. Offset the index to be
// correct for the location in this given block.
err := index.DBPutAddrIndexEntry(b, addrKey,
uint32(block.GetID()), types.TxLoc{TxStart: txIdx, TxLen: 0}, rawdb.AddridxPrefix)
if err != nil {
return err
}
}
}
return nil
}

func (cdb *ChainDB) GetTxForAddress(addr types.Address, numToSkip, numRequested uint32, reverse bool) ([]*common.RetrievedTx, uint32, error) {
return nil, 0, nil
addrKey, err := index.AddrToKey(addr)
if err != nil {
return nil, 0, err
}
fetchBlockHash := func(id []byte) (*hash.Hash, error) {
// Deserialize and populate the result.
blockid := uint64(binary.LittleEndian.Uint32(id))
return meerdag.DBGetDAGBlockHashByID(cdb, blockid)
}
fetchAddrLevelData := func(key []byte) []byte {
levelData, _ := cdb.db.Get(key)
return levelData
}
regions, dbSkipped, err := index.DBFetchAddrIndexEntries(fetchBlockHash, fetchAddrLevelData,
addrKey, numToSkip, numRequested, reverse, rawdb.AddridxPrefix)
if err != nil {
return nil, 0, err
}
addressTxns := []*common.RetrievedTx{}
for _, r := range regions {
block, err := cdb.GetBlock(r.Hash)
if err != nil {
return nil, 0, err
}
addressTxns = append(addressTxns, &common.RetrievedTx{
BlkHash: r.Hash,
Tx: block.Transactions()[r.Offset],
})
}
return addressTxns, dbSkipped, nil
}

func (cdb *ChainDB) DeleteAddrIdx(sblock *types.SerializedBlock, stxos [][]byte) error {
// Build all of the address to transaction mappings in a local map.
addrsToTxns := make(index.WriteAddrIdxData)
index.AddrIndexBlock(addrsToTxns, sblock, stxos)
b := &bucket{db: cdb.db}

for addrKey, txIdxs := range addrsToTxns {
err := index.DBRemoveAddrIndexEntries(b, addrKey, len(txIdxs), rawdb.AddridxPrefix)
if err != nil {
return err
}
}
return nil
}

func (cdb *ChainDB) CleanAddrIdx(finish bool) error {
return nil
if finish {
return nil
}
return rawdb.CleanAddrIdx(cdb.DB())
}

type bucket struct {
db ethdb.Database
}

func (b *bucket) Get(key []byte) []byte {
value, _ := b.db.Get(key)
return value
}

func (b *bucket) Put(key []byte, value []byte) error {
return b.db.Put(key, value)
}

func (b *bucket) Delete(key []byte) error {
return b.db.Delete(key)
}
Loading
Loading