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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save prev signature on db only if chained mode is enabled #895

Merged
merged 9 commits into from
Jan 11, 2022
11 changes: 10 additions & 1 deletion chain/beacon/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ type chainStore struct {
func newChainStore(l log.Logger, cf *Config, cl net.ProtocolClient, c *cryptoStore, store chain.Store, t *ticker) *chainStore {
// we make sure the chain is increasing monotically
as := newAppendStore(store)

//
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
ss := newSchemeStore(as, cf.Group.Scheme)
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved

// we write some stats about the timing when new beacon is saved
ds := newDiscrepancyStore(as, l, c.GetGroup())
ds := newDiscrepancyStore(ss, l, c.GetGroup())

// we can register callbacks on it
cbs := NewCallbackStore(ds)

// we give the final append store to the syncer
syncer := NewSyncer(l, cbs, c.chain, cl)

//
verifier := chain.NewVerifier(cf.Group.Scheme)

Expand Down Expand Up @@ -152,11 +159,13 @@ func (c *chainStore) runAggregator() {
break
}
cache.FlushRounds(partial.p.GetRound())

newBeacon := &chain.Beacon{
Round: roundCache.round,
PreviousSig: roundCache.prev,
Signature: finalSig,
}

c.l.Infow("", "beacon_id", beaconID, "aggregated_beacon", newBeacon.Round)
if c.tryAppend(lastBeacon, newBeacon) {
lastBeacon = newBeacon
Expand Down
36 changes: 35 additions & 1 deletion chain/beacon/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"github.com/drand/drand/common/scheme"

"github.com/drand/drand/chain"
"github.com/drand/drand/key"
"github.com/drand/drand/log"
Expand Down Expand Up @@ -43,12 +45,44 @@ func (a *appendStore) Put(b *chain.Beacon) error {
if b.Round != a.last.Round+1 {
return fmt.Errorf("invalid round inserted: last %d, new %d", a.last.Round, b.Round)
}
if !bytes.Equal(a.last.Signature, b.PreviousSig) {
if err := a.Store.Put(b); err != nil {
return err
}
a.last = b
return nil
}

// schemeStore is a store that run different checks depending on what scheme is being used.
type schemeStore struct {
chain.Store
sch scheme.Scheme
last *chain.Beacon
sync.Mutex
}

func newSchemeStore(s chain.Store, sch scheme.Scheme) chain.Store {
last, _ := s.Last()
return &schemeStore{
Store: s,
last: last,
sch: sch,
}
}

func (a *schemeStore) Put(b *chain.Beacon) error {
a.Lock()
defer a.Unlock()

if a.sch.DecouplePrevSig {
b.PreviousSig = nil
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
} else if !bytes.Equal(a.last.Signature, b.PreviousSig) {
return fmt.Errorf("invalid previous signature")
}

if err := a.Store.Put(b); err != nil {
return err
}

a.last = b
return nil
}
Expand Down