Skip to content

Commit

Permalink
feat: protocol params tracking from genesis configs (#200)
Browse files Browse the repository at this point in the history
* split ledger config into separate type
* basic era tracking
* per-era functions for decoding and upgrading pparams
* persist active pparams in DB
  • Loading branch information
agaffney authored Oct 30, 2024
1 parent 9f39a32 commit 6a7701d
Show file tree
Hide file tree
Showing 17 changed files with 597 additions and 63 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.22
toolchain go1.22.6

require (
github.com/blinklabs-io/gouroboros v0.102.0
github.com/blinklabs-io/ouroboros-mock v0.3.4
github.com/blinklabs-io/gouroboros v0.103.0
github.com/blinklabs-io/ouroboros-mock v0.3.5
github.com/dgraph-io/badger/v4 v4.3.1
github.com/glebarez/sqlite v1.11.0
github.com/kelseyhightower/envconfig v1.4.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ 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.102.0 h1:SSw1SW5a/eunPJdwJHARur/dlGdTrLEq5ScRFZjhen0=
github.com/blinklabs-io/gouroboros v0.102.0/go.mod h1:okgJskD4KXG5itGM95gUl6J+rKbURzaP++HTeCokgkM=
github.com/blinklabs-io/ouroboros-mock v0.3.4 h1:codPfiI5vLeD6YdhKL5VwYSzy2N3Dsgx6xjcLsqFaJQ=
github.com/blinklabs-io/ouroboros-mock v0.3.4/go.mod h1:e/wgG1ZYVenroN2XEMXy7DgEfdmP7KXVRHIQKuh8E/0=
github.com/blinklabs-io/gouroboros v0.103.0 h1:mA/RJ7CeZEE41RQNeMoip/2BbcB1WC2O8Nd+92m4vHY=
github.com/blinklabs-io/gouroboros v0.103.0/go.mod h1:wjiNCbZ2uQy9DGfLCgEgqagHxNBAv5UYsOdRBgoi3SU=
github.com/blinklabs-io/ouroboros-mock v0.3.5 h1:/KWbSoH8Pjrd9uxOH7mVbI7XFsDCNW/O9FtLlvJDUpQ=
github.com/blinklabs-io/ouroboros-mock v0.3.5/go.mod h1:JtUQ3Luo22hCnGBxuxNp6JaUx63VxidxWwmcaVMremw=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
9 changes: 6 additions & 3 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ func (n *Node) Run() error {
}
// Load state
state, err := state.NewLedgerState(
n.config.dataDir,
n.eventBus,
n.config.logger,
state.LedgerStateConfig{
DataDir: n.config.dataDir,
EventBus: n.eventBus,
Logger: n.config.logger,
CardanoNodeConfig: n.config.cardanoNodeConfig,
},
)
if err != nil {
return fmt.Errorf("failed to load state database: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions state/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (ci *ChainIterator) Next(blocking bool) (*ChainIteratorResult, error) {
return nil, nil
}
// Wait for new block or a rollback
blockSubId, blockChan := ci.ls.eventBus.Subscribe(ChainBlockEventType)
rollbackSubId, rollbackChan := ci.ls.eventBus.Subscribe(
blockSubId, blockChan := ci.ls.config.EventBus.Subscribe(ChainBlockEventType)
rollbackSubId, rollbackChan := ci.ls.config.EventBus.Subscribe(
ChainRollbackEventType,
)
// Release read lock while we wait for new event
Expand All @@ -111,7 +111,7 @@ func (ci *ChainIterator) Next(blocking bool) (*ChainIteratorResult, error) {
ret.Point = rollbackData.Point
ret.Rollback = true
}
ci.ls.eventBus.Unsubscribe(ChainBlockEventType, blockSubId)
ci.ls.eventBus.Unsubscribe(ChainRollbackEventType, rollbackSubId)
ci.ls.config.EventBus.Unsubscribe(ChainBlockEventType, blockSubId)
ci.ls.config.EventBus.Unsubscribe(ChainRollbackEventType, rollbackSubId)
return ret, nil
}
48 changes: 48 additions & 0 deletions state/eras/allegra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eras

import (
"fmt"

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

var AllegraEraDesc = EraDesc{
Id: allegra.EraIdAllegra,
Name: allegra.EraNameAllegra,
DecodePParamsFunc: DecodePParamsAllegra,
HardForkFunc: HardForkAllegra,
}

func DecodePParamsAllegra(data []byte) (any, error) {
var ret allegra.AllegraProtocolParameters
if _, err := cbor.Decode(data, &ret); err != nil {
return nil, err
}
return ret, nil
}

func HardForkAllegra(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
shelleyPParams, ok := prevPParams.(shelley.ShelleyProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
}
ret := allegra.UpgradePParams(shelleyPParams)
return ret, nil
}
53 changes: 53 additions & 0 deletions state/eras/alonzo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eras

import (
"fmt"

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

var AlonzoEraDesc = EraDesc{
Id: alonzo.EraIdAlonzo,
Name: alonzo.EraNameAlonzo,
DecodePParamsFunc: DecodePParamsAlonzo,
HardForkFunc: HardForkAlonzo,
}

func DecodePParamsAlonzo(data []byte) (any, error) {
var ret alonzo.AlonzoProtocolParameters
if _, err := cbor.Decode(data, &ret); err != nil {
return nil, err
}
return ret, nil
}

func HardForkAlonzo(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
maryPParams, ok := prevPParams.(mary.MaryProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
}
ret := alonzo.UpgradePParams(maryPParams)
alonzoGenesis, err := nodeConfig.AlonzoGenesis()
if err != nil {
return nil, err
}
ret.UpdateFromGenesis(alonzoGenesis)
return ret, nil
}
48 changes: 48 additions & 0 deletions state/eras/babbage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eras

import (
"fmt"

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

var BabbageEraDesc = EraDesc{
Id: babbage.EraIdBabbage,
Name: babbage.EraNameBabbage,
DecodePParamsFunc: DecodePParamsBabbage,
HardForkFunc: HardForkBabbage,
}

func DecodePParamsBabbage(data []byte) (any, error) {
var ret babbage.BabbageProtocolParameters
if _, err := cbor.Decode(data, &ret); err != nil {
return nil, err
}
return ret, nil
}

func HardForkBabbage(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
alonzoPParams, ok := prevPParams.(alonzo.AlonzoProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
}
ret := babbage.UpgradePParams(alonzoPParams)
return ret, nil
}
22 changes: 22 additions & 0 deletions state/eras/byron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eras

import "github.com/blinklabs-io/gouroboros/ledger/byron"

var ByronEraDesc = EraDesc{
Id: byron.EraIdByron,
Name: byron.EraNameByron,
}
53 changes: 53 additions & 0 deletions state/eras/conway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eras

import (
"fmt"

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

var ConwayEraDesc = EraDesc{
Id: conway.EraIdConway,
Name: conway.EraNameConway,
DecodePParamsFunc: DecodePParamsConway,
HardForkFunc: HardForkConway,
}

func DecodePParamsConway(data []byte) (any, error) {
var ret conway.ConwayProtocolParameters
if _, err := cbor.Decode(data, &ret); err != nil {
return nil, err
}
return ret, nil
}

func HardForkConway(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
babbagePParams, ok := prevPParams.(babbage.BabbageProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
}
ret := conway.UpgradePParams(babbagePParams)
conwayGenesis, err := nodeConfig.ConwayGenesis()
if err != nil {
return nil, err
}
ret.UpdateFromGenesis(conwayGenesis)
return ret, nil
}
34 changes: 34 additions & 0 deletions state/eras/eras.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eras

import "github.com/blinklabs-io/node/config/cardano"

type EraDesc struct {
Id uint
Name string
DecodePParamsFunc func([]byte) (any, error)
HardForkFunc func(*cardano.CardanoNodeConfig, any) (any, error)
}

var Eras = []EraDesc{
ByronEraDesc,
ShelleyEraDesc,
AllegraEraDesc,
MaryEraDesc,
AlonzoEraDesc,
BabbageEraDesc,
ConwayEraDesc,
}
48 changes: 48 additions & 0 deletions state/eras/mary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eras

import (
"fmt"

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

var MaryEraDesc = EraDesc{
Id: mary.EraIdMary,
Name: mary.EraNameMary,
DecodePParamsFunc: DecodePParamsMary,
HardForkFunc: HardForkMary,
}

func DecodePParamsMary(data []byte) (any, error) {
var ret mary.MaryProtocolParameters
if _, err := cbor.Decode(data, &ret); err != nil {
return nil, err
}
return ret, nil
}

func HardForkMary(nodeConfig *cardano.CardanoNodeConfig, prevPParams any) (any, error) {
allegraPParams, ok := prevPParams.(allegra.AllegraProtocolParameters)
if !ok {
return nil, fmt.Errorf("previous PParams (%T) are not expected type", prevPParams)
}
ret := mary.UpgradePParams(allegraPParams)
return ret, nil
}
Loading

0 comments on commit 6a7701d

Please sign in to comment.