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

core/parsigdb: improve key uniqueness #3283

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
aeb9bd5
core/parsigdb: improve key uniqueness
gsora Sep 12, 2024
6cb6ab6
core/parsigdb: review comments
gsora Sep 16, 2024
5230169
core/tracker: debug print what node operator pushed when parsigdb is …
gsora Sep 16, 2024
c1a97df
core/parsigdb: gate hash tree root keying behind feature flag
gsora Sep 18, 2024
5ba714f
core/tracker: debug print what node operator pushed when parsigdb is …
gsora Sep 16, 2024
93b2fc4
core/parsigdb: gate hash tree root keying behind feature flag
gsora Sep 18, 2024
d1d1457
core/tracker: debug print what node operator pushed when parsigdb is …
gsora Sep 16, 2024
491afd5
core/parsigdb: fix tests
gsora Sep 18, 2024
ae9a3a6
Merge remote-tracking branch 'refs/remotes/origin/gsora/parsigdb-dedu…
gsora Sep 18, 2024
25447e7
*: fix flaky testpeers test (#3267)
KaloyanTanev Sep 10, 2024
703e898
build(deps): Bump golang.org/x/crypto from 0.26.0 to 0.27.0 (#3265)
dependabot[bot] Sep 10, 2024
1aebb1b
cmd: increase testpeers tests timeouts (#3268)
KaloyanTanev Sep 10, 2024
c16b545
build(deps): Bump go.opentelemetry.io/otel/trace from 1.28.0 to 1.29.…
dependabot[bot] Sep 10, 2024
c63c61f
build(deps): Bump go.opentelemetry.io/otel/exporters/stdout/stdouttra…
dependabot[bot] Sep 10, 2024
605c9d1
build(deps): Bump github.com/ferranbt/fastssz from 0.1.3 to 0.1.4 (#3…
dependabot[bot] Sep 10, 2024
5bfa7cc
build(deps): Bump golang.org/x/tools from 0.23.0 to 0.25.0 (#3270)
dependabot[bot] Sep 11, 2024
5ded523
build(deps): Bump go.opentelemetry.io/contrib/instrumentation/net/htt…
dependabot[bot] Sep 11, 2024
2655c22
build(deps): Bump go.opentelemetry.io/otel from 1.29.0 to 1.30.0 (#3274)
dependabot[bot] Sep 11, 2024
2a8430d
build(deps): Bump go.opentelemetry.io/otel/exporters/stdout/stdouttra…
dependabot[bot] Sep 11, 2024
13036ff
cmd: fix test peers out of range (#3280)
KaloyanTanev Sep 12, 2024
cce6f86
*: add --all for exit sign command (#3272)
KaloyanTanev Sep 12, 2024
32cae8f
docs: fix typos (#3236)
cratiu222 Sep 17, 2024
8ec9d9a
build(deps): Bump go.opentelemetry.io/contrib/instrumentation/net/htt…
dependabot[bot] Sep 18, 2024
d1a2dc1
build(deps): Bump github.com/prometheus/client_golang from 1.20.3 to …
dependabot[bot] Sep 18, 2024
01274ba
*: disable intrange linter (#3282)
gsora Sep 18, 2024
5011e64
fix tests
gsora Sep 18, 2024
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
6 changes: 6 additions & 0 deletions app/featureset/featureset.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const (
// The feature gets automatically enabled when the current network is gnosis|chiado,
// unless the user disabled this feature explicitly.
GnosisBlockHotfix Feature = "gnosis_block_hotfix"

// HardenedParSigDB enables keying ParSigDB elements with their hash tree root
// on top of the core.Duty object.
// Since this is a breaking change, we'll gradually enable it in later releases.
HardenedParSigDB Feature = "hardened_par_sig_db"
)

var (
Expand All @@ -56,6 +61,7 @@ var (
AggSigDBV2: statusAlpha,
JSONRequests: statusAlpha,
GnosisBlockHotfix: statusAlpha,
HardenedParSigDB: statusAlpha,
// Add all features and there status here.
}

Expand Down
30 changes: 29 additions & 1 deletion core/parsigdb/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/featureset"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/core"
Expand Down Expand Up @@ -83,7 +84,16 @@ func (db *MemDB) StoreExternal(ctx context.Context, duty core.Duty, signedSet co
output := make(map[core.PubKey][]core.ParSignedData)

for pubkey, sig := range signedSet {
sigs, ok, err := db.store(key{Duty: duty, PubKey: pubkey}, sig)
root, err := rootFromParSigDataSet(duty, sig)
if err != nil {
return errors.Wrap(err, "cannot hash tree root ParSignedData")
}

sigs, ok, err := db.store(key{
Duty: duty,
PubKey: pubkey,
Root: root,
}, sig)
if err != nil {
return err
} else if !ok {
Expand Down Expand Up @@ -224,6 +234,23 @@ func getThresholdMatching(typ core.DutyType, sigs []core.ParSignedData, threshol
return nil, false, nil
}

// rootFromParSigDataSet returns the MessageRoot of data only if the duty is supported,
// and only if data is not a core.Signature.
func rootFromParSigDataSet(duty core.Duty, data core.ParSignedData) ([32]byte, error) {
if !featureset.Enabled(featureset.HardenedParSigDB) {
return [32]byte{}, nil
}

// known limitation: sync message and contributions might have different hashes,
// ignore them for now.
// signature duties don't have a MessageRoot implementation at all, by design.
if duty.Type == core.DutySyncMessage || duty.Type == core.DutySyncContribution || duty.Type == core.DutySignature {
gsora marked this conversation as resolved.
Show resolved Hide resolved
return [32]byte{}, nil
}

return data.MessageRoot()
}

func parSignedDataEqual(x, y core.ParSignedData) (bool, error) {
xjson, err := json.Marshal(x)
if err != nil {
Expand All @@ -240,4 +267,5 @@ func parSignedDataEqual(x, y core.ParSignedData) (bool, error) {
type key struct {
Duty core.Duty
PubKey core.PubKey
Root [32]byte
}
137 changes: 137 additions & 0 deletions core/parsigdb/memory_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,83 @@ import (
"github.com/obolnetwork/charon/testutil"
)

func TestRootMatching(t *testing.T) {
t.Run("matching roots yield threshold data", func(t *testing.T) {
deadliner := newTestDeadliner()
db := NewMemDB(2, deadliner)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go db.Trim(ctx)

timesCalled := 0
db.SubscribeThreshold(func(_ context.Context, _ core.Duty, _ map[core.PubKey][]core.ParSignedData) error {
timesCalled++

return nil
})

pubkey := testutil.RandomCorePubKey(t)

duty := core.NewProposerDuty(uint64(42))

blockData := testutil.RandomDenebVersionedSignedProposal()

for i := range 2 {
full, err := core.NewPartialVersionedSignedProposal(blockData, i+1)
require.NoError(t, err)

if i == 0 {
require.NoError(t, db.StoreInternal(ctx, duty, core.ParSignedDataSet{
pubkey: full,
}))
} else {
require.NoError(t, db.StoreExternal(ctx, duty, core.ParSignedDataSet{
pubkey: full,
}))
}
}

require.Equal(t, 1, timesCalled)
})

t.Run("non-matching roots yield no threshold data", func(t *testing.T) {
deadliner := newTestDeadliner()
db := NewMemDB(2, deadliner)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go db.Trim(ctx)

timesCalled := 0
db.SubscribeThreshold(func(_ context.Context, _ core.Duty, _ map[core.PubKey][]core.ParSignedData) error {
timesCalled++

return nil
})

duty := core.NewProposerDuty(uint64(42))
pubkey := testutil.RandomCorePubKey(t)

for i := range 2 {
full, err := core.NewPartialVersionedSignedProposal(testutil.RandomDenebVersionedSignedProposal(), i+1)
require.NoError(t, err)

if i == 0 {
require.NoError(t, db.StoreInternal(ctx, duty, core.ParSignedDataSet{
pubkey: full,
}))
} else {
require.NoError(t, db.StoreExternal(ctx, duty, core.ParSignedDataSet{
pubkey: full,
}))
}
}

require.Zero(t, timesCalled)
})
}

func TestGetThresholdMatching(t *testing.T) {
const n = 4

Expand Down Expand Up @@ -149,6 +226,66 @@ func TestMemDBThreshold(t *testing.T) {
require.Equal(t, 2, timesCalled)
}

func Test_rootFromParSigDataSet(t *testing.T) {
blockData := testutil.RandomDenebVersionedSignedProposal()
full, err := core.NewPartialVersionedSignedProposal(blockData, 1)
require.NoError(t, err)

tests := []struct {
name string
duty core.Duty
data core.ParSignedData
shouldBeZero bool
wantErr bool
}{
{
"ok",
core.NewProposerDuty(uint64(42)),
full,
false,
false,
},
{
"sync message",
core.NewSyncMessageDuty(uint64(42)),
full,
true,
false,
},
{
"sync contribution",
core.NewSyncContributionDuty(uint64(42)),
full,
true,
false,
},
{
"signature",
core.NewSignatureDuty(uint64(42)),
full,
true,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := rootFromParSigDataSet(tt.duty, tt.data)

if tt.shouldBeZero {
require.Zero(t, got)
} else {
require.NotZero(t, got)
}

if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

func newTestDeadliner() *testDeadliner {
return &testDeadliner{
ch: make(chan core.Duty),
Expand Down
31 changes: 31 additions & 0 deletions core/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package tracker

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -104,10 +105,19 @@ type Tracker struct {

// participationReporter instruments duty peer participation.
participationReporter func(ctx context.Context, duty core.Duty, failed bool, participatedShares map[int]int, unexpectedPeers map[int]int, expectedPerPeer int)

// peers is the set of peers contained in the cluster.
peers map[int]p2p.Peer
}

// New returns a new Tracker. The deleter deadliner must return well after analyser deadliner since duties of the same slot are often analysed together.
func New(analyser core.Deadliner, deleter core.Deadliner, peers []p2p.Peer, fromSlot uint64) *Tracker {
pm := make(map[int]p2p.Peer, len(peers))

for _, p := range peers {
pm[p.Index] = p
}

t := &Tracker{
input: make(chan event),
events: make(map[core.Duty][]event),
Expand All @@ -118,6 +128,7 @@ func New(analyser core.Deadliner, deleter core.Deadliner, peers []p2p.Peer, from
parSigReporter: reportParSigs,
failedDutyReporter: newFailedDutyReporter(),
participationReporter: newParticipationReporter(peers),
peers: pm,
}

return t
Expand Down Expand Up @@ -156,6 +167,26 @@ func (t *Tracker) Run(ctx context.Context) error {
continue // Ignore unsupported duties
}

// For parsigdb inconsistent reason, print what roots where generated by what peer
if failed && reason == reasonBugParSigDBInconsistent {
for pubkey, parsig := range parsigs {
for root, partials := range parsig {
var peers []string
for _, p := range partials {
peers = append(peers, t.peers[p.ShareIdx].Name)
}

log.Debug(
ctx,
"Detected inconsistent ParSigDB data for duty",
z.Str("pubkey", pubkey.String()),
z.Str("root", hex.EncodeToString(root[:])),
z.Any("peers", peers),
)
}
}
}

t.failedDutyReporter(ctx, duty, failed, failedStep, reason, failedErr)

// Analyse peer participation
Expand Down
Loading