Skip to content

Commit

Permalink
fixed bug where qchain was not computed for first tick
Browse files Browse the repository at this point in the history
  • Loading branch information
0xluk committed Mar 11, 2024
1 parent d225753 commit 391a777
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (p *Processor) processOneByOne() error {
}

val := validator.New(client, p.ps)
err = val.ValidateTick(ctx, nextTick)
err = val.ValidateTick(ctx, uint64(tickInfo.InitialTick), nextTick)
if err != nil {
return errors.Wrapf(err, "validating tick %d", nextTick)
}
Expand Down
30 changes: 24 additions & 6 deletions validator/qchain/qchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"github.com/qubic/go-node-connector/types"
)

func ComputeAndStore(ctx context.Context, store *store.PebbleStore, tickNumber uint64, quorumVote types.QuorumTickVote) error {
lastQChainDigest, err := store.GetQChainDigest(ctx, tickNumber-1)
func ComputeAndStore(ctx context.Context, store *store.PebbleStore, initialEpochTick, tickNumber uint64, quorumVote types.QuorumTickVote) error {
prevDigest, err := getPrevQChainDigest(ctx, store, initialEpochTick, tickNumber)
if err != nil {
return errors.Wrapf(err, "getting qChain digest for last tick: %d\n", tickNumber-1)
return errors.Wrap(err, "getting prev qChain digest")
}
currentDigest, err := computeCurrentTickDigest(ctx, quorumVote, lastQChainDigest)

currentDigest, err := computeCurrentTickDigest(ctx, quorumVote, prevDigest)
if err != nil {
return errors.Wrap(err, "computing current tick digest")
}
Expand All @@ -25,7 +26,24 @@ func ComputeAndStore(ctx context.Context, store *store.PebbleStore, tickNumber u
return nil
}

func computeCurrentTickDigest(ctx context.Context, vote types.QuorumTickVote, lastQChainDigest []byte) ([32]byte, error) {
func getPrevQChainDigest(ctx context.Context, store *store.PebbleStore, initialEpochTick, tickNumber uint64) ([32]byte, error) {
// if this is the first tick, there is no previous qChain digest, so we are using an empty one
if tickNumber == initialEpochTick {
return [32]byte{}, nil
}

previousTickQChainDigestStored, err := store.GetQChainDigest(ctx, tickNumber-1)
if err != nil {
return [32]byte{}, errors.Wrapf(err, "getting qChain digest for last tick: %d\n", tickNumber-1)
}

var previousTickQChainDigest [32]byte
copy(previousTickQChainDigest[:], previousTickQChainDigestStored)

return previousTickQChainDigest, nil
}

func computeCurrentTickDigest(ctx context.Context, vote types.QuorumTickVote, previousTickQChainDigest [32]byte) ([32]byte, error) {
qChain := QChain{
ComputorIndex: vote.ComputorIndex,
Epoch: vote.Epoch,
Expand All @@ -42,8 +60,8 @@ func computeCurrentTickDigest(ctx context.Context, vote types.QuorumTickVote, la
PreviousUniverseDigest: vote.PreviousUniverseDigest,
PreviousComputerDigest: vote.PreviousComputerDigest,
TxDigest: vote.TxDigest,
PreviousTickQChainDigest: previousTickQChainDigest,
}
copy(qChain.PreviousTickQChainDigest[:], lastQChainDigest[:])

digest, err := qChain.Digest()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func New(qu *qubic.Client, store *store.PebbleStore) *Validator {
return &Validator{qu: qu, store: store}
}

func (v *Validator) ValidateTick(ctx context.Context, tickNumber uint64) error {
func (v *Validator) ValidateTick(ctx context.Context, initialEpochTick, tickNumber uint64) error {
quorumVotes, err := v.qu.GetQuorumVotes(ctx, uint32(tickNumber))
if err != nil {
return errors.Wrap(err, "getting quorum tick data")
Expand Down Expand Up @@ -120,7 +120,7 @@ func (v *Validator) ValidateTick(ctx context.Context, tickNumber uint64) error {

log.Printf("Stored %d transactions\n", len(transactions))

err = qchain.ComputeAndStore(ctx, v.store, tickNumber, alignedVotes[0])
err = qchain.ComputeAndStore(ctx, v.store, initialEpochTick, tickNumber, alignedVotes[0])
if err != nil {
return errors.Wrap(err, "computing and storing qChain")
}
Expand Down

0 comments on commit 391a777

Please sign in to comment.