Skip to content

Commit

Permalink
feat: track rolling nonce
Browse files Browse the repository at this point in the history
Calculate and track the rolling nonce (eta_v) for each block
  • Loading branch information
agaffney committed Jan 20, 2025
1 parent a4308c0 commit 02f1505
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
connectrpc.com/connect v1.18.1
connectrpc.com/grpchealth v1.3.0
connectrpc.com/grpcreflect v1.3.0
github.com/blinklabs-io/gouroboros v0.107.1
github.com/blinklabs-io/gouroboros v0.108.0
github.com/blinklabs-io/ouroboros-mock v0.3.6
github.com/dgraph-io/badger/v4 v4.5.0
github.com/glebarez/sqlite v1.11.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/blinklabs-io/gouroboros v0.107.1 h1:BOHByzGwkGCrAkXAxaH3hPiXOPPxyYV1WrB7crJEaUE=
github.com/blinklabs-io/gouroboros v0.107.1/go.mod h1:Igd/hQ3pIhX3/KRM5js3Jz6CF8M8oKN9aO9d9NR7wGo=
github.com/blinklabs-io/gouroboros v0.108.0 h1:ys4A3Mnnr7pZTWjUYGkkVolNKBVUOIqm9bz8DEcUA9c=
github.com/blinklabs-io/gouroboros v0.108.0/go.mod h1:10k4zsSu1Xykvh+J2Vfdz49ul1kfNH+zlnuCmlBcop4=
github.com/blinklabs-io/ouroboros-mock v0.3.6 h1:m/iK4l1EJbiz9kU0u5uzWNEwlskazWuwbJHysr2hhP4=
github.com/blinklabs-io/ouroboros-mock v0.3.6/go.mod h1:S++j/dBxNYG3AlcRaeZvok4X2ih7GrK+VWV1s0SLN3M=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
Expand Down
14 changes: 14 additions & 0 deletions state/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,19 @@ func (ls *LedgerState) processBlockEvent(
}
}
}
// Calculate block rolling nonce
var blockNonce []byte
if ls.currentEra.CalculateEtaVFunc != nil {
tmpNonce, err := ls.currentEra.CalculateEtaVFunc(
ls.config.CardanoNodeConfig,
ls.currentTipBlockNonce,
e.Block,
)
if err != nil {
return err
}
blockNonce = tmpNonce
}
// Add block to database
tmpBlock := models.Block{
Slot: e.Point.Slot,
Expand All @@ -399,6 +412,7 @@ func (ls *LedgerState) processBlockEvent(
// block number isn't stored in the block itself
Number: e.Block.BlockNumber(),
Type: e.Type,
Nonce: blockNonce,
Cbor: e.Block.Cbor(),
}
if err := ls.addBlock(txn, tmpBlock); err != nil {
Expand Down
30 changes: 30 additions & 0 deletions state/eras/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
package eras

import (
"encoding/hex"
"fmt"

"github.com/blinklabs-io/dingo/config/cardano"
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/ledger/allegra"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/shelley"
)

Expand All @@ -31,6 +34,7 @@ var AllegraEraDesc = EraDesc{
PParamsUpdateFunc: PParamsUpdateAllegra,
HardForkFunc: HardForkAllegra,
EpochLengthFunc: EpochLengthShelley,
CalculateEtaVFunc: CalculateEtaVAllegra,
}

func DecodePParamsAllegra(data []byte) (any, error) {
Expand Down Expand Up @@ -82,3 +86,29 @@ func HardForkAllegra(
ret := allegra.UpgradePParams(shelleyPParams)
return ret, nil
}

func CalculateEtaVAllegra(
nodeConfig *cardano.CardanoNodeConfig,
prevBlockNonce []byte,
block ledger.Block,
) ([]byte, error) {
if len(prevBlockNonce) == 0 {
tmpNonce, err := hex.DecodeString(nodeConfig.ShelleyGenesisHash)
if err != nil {
return nil, err
}
prevBlockNonce = tmpNonce
}
h, ok := block.Header().(*allegra.AllegraBlockHeader)
if !ok {
return nil, fmt.Errorf("unexpected block type")
}
tmpNonce, err := lcommon.CalculateRollingNonce(
prevBlockNonce,
h.Body.NonceVrf.Output,
)
if err != nil {
return nil, err
}
return tmpNonce.Bytes(), nil
}
30 changes: 30 additions & 0 deletions state/eras/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
package eras

import (
"encoding/hex"
"fmt"

"github.com/blinklabs-io/dingo/config/cardano"
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/ledger/alonzo"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/mary"
)

Expand All @@ -31,6 +34,7 @@ var AlonzoEraDesc = EraDesc{
PParamsUpdateFunc: PParamsUpdateAlonzo,
HardForkFunc: HardForkAlonzo,
EpochLengthFunc: EpochLengthShelley,
CalculateEtaVFunc: CalculateEtaVAlonzo,
}

func DecodePParamsAlonzo(data []byte) (any, error) {
Expand Down Expand Up @@ -87,3 +91,29 @@ func HardForkAlonzo(
ret.UpdateFromGenesis(alonzoGenesis)
return ret, nil
}

func CalculateEtaVAlonzo(
nodeConfig *cardano.CardanoNodeConfig,
prevBlockNonce []byte,
block ledger.Block,
) ([]byte, error) {
if len(prevBlockNonce) == 0 {
tmpNonce, err := hex.DecodeString(nodeConfig.ShelleyGenesisHash)
if err != nil {
return nil, err
}
prevBlockNonce = tmpNonce
}
h, ok := block.Header().(*alonzo.AlonzoBlockHeader)
if !ok {
return nil, fmt.Errorf("unexpected block type")
}
tmpNonce, err := lcommon.CalculateRollingNonce(
prevBlockNonce,
h.Body.NonceVrf.Output,
)
if err != nil {
return nil, err
}
return tmpNonce.Bytes(), nil
}
30 changes: 30 additions & 0 deletions state/eras/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
package eras

import (
"encoding/hex"
"fmt"

"github.com/blinklabs-io/dingo/config/cardano"
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/ledger/alonzo"
"github.com/blinklabs-io/gouroboros/ledger/babbage"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
)

var BabbageEraDesc = EraDesc{
Expand All @@ -31,6 +34,7 @@ var BabbageEraDesc = EraDesc{
PParamsUpdateFunc: PParamsUpdateBabbage,
HardForkFunc: HardForkBabbage,
EpochLengthFunc: EpochLengthShelley,
CalculateEtaVFunc: CalculateEtaVBabbage,
}

func DecodePParamsBabbage(data []byte) (any, error) {
Expand Down Expand Up @@ -82,3 +86,29 @@ func HardForkBabbage(
ret := babbage.UpgradePParams(alonzoPParams)
return ret, nil
}

func CalculateEtaVBabbage(
nodeConfig *cardano.CardanoNodeConfig,
prevBlockNonce []byte,
block ledger.Block,
) ([]byte, error) {
if len(prevBlockNonce) == 0 {
tmpNonce, err := hex.DecodeString(nodeConfig.ShelleyGenesisHash)
if err != nil {
return nil, err
}
prevBlockNonce = tmpNonce
}
h, ok := block.Header().(*babbage.BabbageBlockHeader)
if !ok {
return nil, fmt.Errorf("unexpected block type")
}
tmpNonce, err := lcommon.CalculateRollingNonce(
prevBlockNonce,
h.Body.VrfResult.Output,
)
if err != nil {
return nil, err
}
return tmpNonce.Bytes(), nil
}
30 changes: 30 additions & 0 deletions state/eras/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
package eras

import (
"encoding/hex"
"fmt"

"github.com/blinklabs-io/dingo/config/cardano"
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/ledger/babbage"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/conway"
)

Expand All @@ -31,6 +34,7 @@ var ConwayEraDesc = EraDesc{
PParamsUpdateFunc: PParamsUpdateConway,
HardForkFunc: HardForkConway,
EpochLengthFunc: EpochLengthShelley,
CalculateEtaVFunc: CalculateEtaVConway,
}

func DecodePParamsConway(data []byte) (any, error) {
Expand Down Expand Up @@ -87,3 +91,29 @@ func HardForkConway(
ret.UpdateFromGenesis(conwayGenesis)
return ret, nil
}

func CalculateEtaVConway(
nodeConfig *cardano.CardanoNodeConfig,
prevBlockNonce []byte,
block ledger.Block,
) ([]byte, error) {
if len(prevBlockNonce) == 0 {
tmpNonce, err := hex.DecodeString(nodeConfig.ShelleyGenesisHash)
if err != nil {
return nil, err
}
prevBlockNonce = tmpNonce
}
h, ok := block.Header().(*conway.ConwayBlockHeader)
if !ok {
return nil, fmt.Errorf("unexpected block type")
}
tmpNonce, err := lcommon.CalculateRollingNonce(
prevBlockNonce,
h.Body.VrfResult.Output,
)
if err != nil {
return nil, err
}
return tmpNonce.Bytes(), nil
}
7 changes: 6 additions & 1 deletion state/eras/eras.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package eras

import "github.com/blinklabs-io/dingo/config/cardano"
import (
"github.com/blinklabs-io/dingo/config/cardano"

"github.com/blinklabs-io/gouroboros/ledger"
)

type EraDesc struct {
Id uint
Expand All @@ -24,6 +28,7 @@ type EraDesc struct {
PParamsUpdateFunc func(any, any) (any, error)
HardForkFunc func(*cardano.CardanoNodeConfig, any) (any, error)
EpochLengthFunc func(*cardano.CardanoNodeConfig) (uint, uint, error)
CalculateEtaVFunc func(*cardano.CardanoNodeConfig, []byte, ledger.Block) ([]byte, error)
}

var Eras = []EraDesc{
Expand Down
30 changes: 30 additions & 0 deletions state/eras/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
package eras

import (
"encoding/hex"
"fmt"

"github.com/blinklabs-io/dingo/config/cardano"
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/ledger/allegra"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/mary"
)

Expand All @@ -31,6 +34,7 @@ var MaryEraDesc = EraDesc{
PParamsUpdateFunc: PParamsUpdateMary,
HardForkFunc: HardForkMary,
EpochLengthFunc: EpochLengthShelley,
CalculateEtaVFunc: CalculateEtaVMary,
}

func DecodePParamsMary(data []byte) (any, error) {
Expand Down Expand Up @@ -82,3 +86,29 @@ func HardForkMary(
ret := mary.UpgradePParams(allegraPParams)
return ret, nil
}

func CalculateEtaVMary(
nodeConfig *cardano.CardanoNodeConfig,
prevBlockNonce []byte,
block ledger.Block,
) ([]byte, error) {
if len(prevBlockNonce) == 0 {
tmpNonce, err := hex.DecodeString(nodeConfig.ShelleyGenesisHash)
if err != nil {
return nil, err
}
prevBlockNonce = tmpNonce
}
h, ok := block.Header().(*mary.MaryBlockHeader)
if !ok {
return nil, fmt.Errorf("unexpected block type")
}
tmpNonce, err := lcommon.CalculateRollingNonce(
prevBlockNonce,
h.Body.NonceVrf.Output,
)
if err != nil {
return nil, err
}
return tmpNonce.Bytes(), nil
}
31 changes: 31 additions & 0 deletions state/eras/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package eras

import (
"encoding/hex"
"fmt"

"github.com/blinklabs-io/dingo/config/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/shelley"
)

Expand All @@ -30,6 +34,7 @@ var ShelleyEraDesc = EraDesc{
PParamsUpdateFunc: PParamsUpdateShelley,
HardForkFunc: HardForkShelley,
EpochLengthFunc: EpochLengthShelley,
CalculateEtaVFunc: CalculateEtaVShelley,
}

func DecodePParamsShelley(data []byte) (any, error) {
Expand Down Expand Up @@ -93,3 +98,29 @@ func EpochLengthShelley(
uint(shelleyGenesis.EpochLength),
nil
}

func CalculateEtaVShelley(
nodeConfig *cardano.CardanoNodeConfig,
prevBlockNonce []byte,
block ledger.Block,
) ([]byte, error) {
if len(prevBlockNonce) == 0 {
tmpNonce, err := hex.DecodeString(nodeConfig.ShelleyGenesisHash)
if err != nil {
return nil, err
}
prevBlockNonce = tmpNonce
}
h, ok := block.Header().(*shelley.ShelleyBlockHeader)
if !ok {
return nil, fmt.Errorf("unexpected block type")
}
tmpNonce, err := lcommon.CalculateRollingNonce(
prevBlockNonce,
h.Body.NonceVrf.Output,
)
if err != nil {
return nil, err
}
return tmpNonce.Bytes(), nil
}
1 change: 1 addition & 0 deletions state/models/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Block struct {
Number uint64 `gorm:"index"`
Hash []byte `gorm:"index:slot_hash"`
Type uint
Nonce []byte
Cbor []byte `gorm:"-"` // This is here for convenience but not represented in the metadata DB
}

Expand Down
Loading

0 comments on commit 02f1505

Please sign in to comment.