Skip to content

Commit b928687

Browse files
authored
refactor(database): use point in block nonce operations (#996)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 3b71728 commit b928687

File tree

7 files changed

+27
-31
lines changed

7 files changed

+27
-31
lines changed

database/block_nonce.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414

1515
package database
1616

17-
// GetBlockNonce fetches the block nonce for a given slot/hash
17+
import (
18+
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
19+
)
20+
21+
// GetBlockNonce fetches the block nonce for a given chain point
1822
func (d *Database) GetBlockNonce(
19-
blockHash []byte,
20-
slotNumber uint64,
23+
point ocommon.Point,
2124
txn *Txn,
2225
) ([]byte, error) {
2326
if txn == nil {
24-
return d.metadata.GetBlockNonce(blockHash, slotNumber, nil)
27+
return d.metadata.GetBlockNonce(point, nil)
2528
}
26-
return d.metadata.GetBlockNonce(blockHash, slotNumber, txn.Metadata())
29+
return d.metadata.GetBlockNonce(point, txn.Metadata())
2730
}
2831

2932
// DeleteBlockNoncesBeforeSlot removes all block_nonces older than the given slot number

database/plugin/metadata/sqlite/block_nonce.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"errors"
1919

2020
"github.com/blinklabs-io/dingo/database/models"
21+
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
2122
"gorm.io/gorm"
2223
)
2324

@@ -52,25 +53,18 @@ func (d *MetadataStoreSqlite) SetBlockNonce(
5253

5354
// GetBlockNonce retrieves the block nonce for a specific block
5455
func (d *MetadataStoreSqlite) GetBlockNonce(
55-
blockHash []byte,
56-
slotNumber uint64,
56+
point ocommon.Point,
5757
txn *gorm.DB,
5858
) ([]byte, error) {
5959
ret := models.BlockNonce{}
60-
if txn != nil {
61-
result := txn.Where("hash = ? AND slot = ?", blockHash, slotNumber).
62-
First(&ret)
63-
if result.Error != nil {
64-
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
65-
return nil, result.Error
66-
}
67-
}
68-
} else {
69-
result := d.DB().Where("hash = ? AND slot = ?", blockHash, slotNumber).First(&ret)
70-
if result.Error != nil {
71-
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
72-
return nil, result.Error
73-
}
60+
if txn == nil {
61+
txn = d.DB()
62+
}
63+
result := txn.Where("hash = ? AND slot = ?", point.Hash, point.Slot).
64+
First(&ret)
65+
if result.Error != nil {
66+
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
67+
return nil, result.Error
7468
}
7569
}
7670
return ret.Nonce, nil

database/plugin/metadata/store.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ type MetadataStore interface {
5959
*gorm.DB,
6060
) (*models.Account, error)
6161
GetBlockNonce(
62-
[]byte, // blockHash
63-
uint64, // slotNumber
62+
ocommon.Point,
6463
*gorm.DB,
6564
) ([]byte, error)
6665
GetDatum(

database/pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package database
1616

1717
import (
18-
"github.com/blinklabs-io/dingo/database/models"
1918
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
19+
"github.com/blinklabs-io/dingo/database/models"
2020
)
2121

2222
// GetPool returns a pool by its key hash

ledger/chainsync.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ func (ls *LedgerState) calculateEpochNonce(
286286
return nil, fmt.Errorf("lookup block before slot: %w", err)
287287
}
288288
blockBeforeStabilityWindowNonce, err := ls.db.GetBlockNonce(
289-
blockBeforeStabilityWindow.Hash,
290-
blockBeforeStabilityWindow.Slot,
289+
ocommon.Point{
290+
Hash: blockBeforeStabilityWindow.Hash,
291+
Slot: blockBeforeStabilityWindow.Slot,
292+
},
291293
txn,
292294
)
293295
if err != nil {

ledger/state.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,7 @@ func (ls *LedgerState) loadTip() error {
969969
// Load tip block and set cached block nonce
970970
if ls.currentTip.Point.Slot > 0 {
971971
tipNonce, err := ls.db.GetBlockNonce(
972-
tmpTip.Point.Hash,
973-
tmpTip.Point.Slot,
972+
tmpTip.Point,
974973
nil,
975974
)
976975
if err != nil {

ledger/view.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ func (lv *LedgerView) UtxoById(
6464
}
6565

6666
func (lv *LedgerView) PoolRegistration(
67-
pkh []byte,
67+
pkh lcommon.PoolKeyHash,
6868
) ([]lcommon.PoolRegistrationCertificate, error) {
69-
poolKeyHash := lcommon.PoolKeyHash(lcommon.NewBlake2b224(pkh))
70-
return lv.ls.db.GetPoolRegistrations(poolKeyHash, lv.txn)
69+
return lv.ls.db.GetPoolRegistrations(pkh, lv.txn)
7170
}
7271

7372
func (lv *LedgerView) StakeRegistration(

0 commit comments

Comments
 (0)