Skip to content

Commit

Permalink
Merge pull request #521 from lochjin/testnet
Browse files Browse the repository at this point in the history
Optimize acct about utxo database
  • Loading branch information
dindinw authored Sep 7, 2023
2 parents fd83f80 + 386f451 commit 6a36fb9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
1 change: 1 addition & 0 deletions consensus/model/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type DataBase interface {
GetUtxo(key []byte) ([]byte, error)
PutUtxo(key []byte, data []byte) error
DeleteUtxo(key []byte) error
ForeachUtxo(fn func(key []byte, data []byte) error) error
GetTokenState(blockID uint) ([]byte, error)
PutTokenState(blockID uint, data []byte) error
DeleteTokenState(blockID uint) error
Expand Down
4 changes: 4 additions & 0 deletions database/chaindb/chaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func (cdb *ChainDB) DeleteUtxo(key []byte) error {
return nil
}

func (cdb *ChainDB) ForeachUtxo(fn func(key []byte, data []byte) error) error {
return rawdb.ForeachUtxo(cdb.db, fn)
}

func (cdb *ChainDB) GetTokenState(blockID uint) ([]byte, error) {
return rawdb.ReadTokenState(cdb.db, uint64(blockID)), nil
}
Expand Down
15 changes: 15 additions & 0 deletions database/legacychaindb/legacychaindb.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ func (cdb *LegacyChainDB) DeleteUtxo(key []byte) error {
})
}

func (cdb *LegacyChainDB) ForeachUtxo(fn func(key []byte, data []byte) error) error {
return cdb.db.View(func(dbTx legacydb.Tx) error {
meta := dbTx.Metadata()
utxoBucket := meta.Bucket(dbnamespace.UtxoSetBucketName)
cursor := utxoBucket.Cursor()
for ok := cursor.First(); ok; ok = cursor.Next() {
err := fn(cursor.Key(), cursor.Value())
if err != nil {
return err
}
}
return nil
})
}

func (cdb *LegacyChainDB) GetTokenState(blockID uint) ([]byte, error) {
var data []byte
err := cdb.db.View(func(dbTx legacydb.Tx) error {
Expand Down
13 changes: 13 additions & 0 deletions database/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,19 @@ func DeleteUtxo(db ethdb.KeyValueWriter, opd []byte) {
}
}

func ForeachUtxo(db ethdb.KeyValueStore, fn func(opd []byte, data []byte) error) error {
it := db.NewIterator(utxoPrefix, nil)
defer it.Release()

for it.Next() {
err := fn(it.Key(), it.Value())
if err != nil {
return err
}
}
return nil
}

// tokenState

func ReadTokenState(db ethdb.Reader, id uint64) []byte {
Expand Down
46 changes: 20 additions & 26 deletions services/acct/acctmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/Qitmeer/qng/core/address"
"github.com/Qitmeer/qng/core/blockchain"
"github.com/Qitmeer/qng/core/blockchain/utxo"
"github.com/Qitmeer/qng/core/dbnamespace"
"github.com/Qitmeer/qng/core/types"
"github.com/Qitmeer/qng/database/legacydb"
"github.com/Qitmeer/qng/engine/txscript"
Expand Down Expand Up @@ -171,36 +170,31 @@ func (a *AccountManager) rebuild(addrs []string) error {
}
ops := []*types.TxOutPoint{}
entrys := []*utxo.UtxoEntry{}
err := a.chain.Consensus().LegacyDB().View(func(dbTx legacydb.Tx) error {
meta := dbTx.Metadata()
utxoBucket := meta.Bucket(dbnamespace.UtxoSetBucketName)
cursor := utxoBucket.Cursor()
for ok := cursor.First(); ok; ok = cursor.Next() {
op, err := parseOutpoint(cursor.Key())
if err != nil {
return err
}
serializedUtxo := cursor.Value()
// Deserialize the utxo entry and return it.
entry, err := utxo.DeserializeUtxoEntry(serializedUtxo)
err := a.chain.DB().ForeachUtxo(func(key []byte, data []byte) error {
op, err := parseOutpoint(key)
if err != nil {
return err
}
serializedUtxo := data
// Deserialize the utxo entry and return it.
entry, err := utxo.DeserializeUtxoEntry(serializedUtxo)
if err != nil {
return err
}
if entry.IsSpent() {
return nil
}
if len(addrs) > 0 {
addr, _, err := a.checkUtxoEntry(entry, addrs)
if err != nil {
return err
}
if entry.IsSpent() {
continue
}
if len(addrs) > 0 {
addr, _, err := a.checkUtxoEntry(entry, addrs)
if err != nil {
return err
}
if len(addr) <= 0 {
continue
}
if len(addr) <= 0 {
return nil
}
ops = append(ops, op)
entrys = append(entrys, entry)
}
ops = append(ops, op)
entrys = append(entrys, entry)
return nil
})
if err != nil {
Expand Down

0 comments on commit 6a36fb9

Please sign in to comment.