Skip to content

Commit

Permalink
core/parsigdb: improve key uniqueness
Browse files Browse the repository at this point in the history
Instead of only having `(duty, pubkey)` as parsigdb key, also use `core.ParSignedDataSet.Root()` to increase uniqueness factor.

This effectively eliminates the possibility that peers in the network could share duty data for a given slot and validator that doesn't match what was previously decided during consensus.
  • Loading branch information
gsora committed Sep 12, 2024
1 parent 46a5509 commit 3eab5e4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
21 changes: 20 additions & 1 deletion core/parsigdb/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,25 @@ 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)
var (
root [32]byte
err error
)

// known limitation: sync message and contributions might have different hashes,
// ignore them for now.
if duty.Type != core.DutySyncMessage && duty.Type != core.DutySyncContribution {
root, err = sig.MessageRoot()
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 @@ -240,4 +258,5 @@ func parSignedDataEqual(x, y core.ParSignedData) (bool, error) {
type key struct {
Duty core.Duty
PubKey core.PubKey
Root [32]byte
}
77 changes: 77 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 := 0; i < 2; i++ {

Check failure on line 41 in core/parsigdb/memory_internal_test.go

View workflow job for this annotation

GitHub Actions / golangci

for loop can be changed to use an integer range (Go 1.22+) (intrange)
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 := 0; i < 2; i++ {

Check failure on line 77 in core/parsigdb/memory_internal_test.go

View workflow job for this annotation

GitHub Actions / golangci

for loop can be changed to use an integer range (Go 1.22+) (intrange)
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

0 comments on commit 3eab5e4

Please sign in to comment.