Skip to content

Commit

Permalink
Merge pull request #48 from qubic/dev
Browse files Browse the repository at this point in the history
Remove flag for calculating historical empty ticks.
  • Loading branch information
LINCKODE authored Jul 30, 2024
2 parents cc281fa + 9fda8c9 commit 9ab07c0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
31 changes: 3 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"github.com/ardanlabs/conf"
"github.com/cockroachdb/pebble"
Expand Down Expand Up @@ -50,10 +49,6 @@ func run() error {
StorageFolder string `conf:"default:store"`
ProcessTickTimeout time.Duration `conf:"default:5s"`
}
EmptyTicks struct {
CalculateAll bool `conf:"default:false"`
Timeout time.Duration `conf:"default:30m"`
}
}

if err := conf.Parse(os.Args[1:], prefix, &cfg); err != nil {
Expand Down Expand Up @@ -90,29 +85,9 @@ func run() error {

ps := store.NewPebbleStore(db, nil)

if cfg.EmptyTicks.CalculateAll == true {

ctx, cancel := context.WithTimeout(context.Background(), cfg.EmptyTicks.Timeout)
defer cancel()
epochs, err := ps.GetLastProcessedTicksPerEpoch(ctx)
if err != nil {
return errors.Wrap(err, "getting epoch list from db")
}

for epoch, _ := range epochs {
fmt.Printf("Calculating empty ticks for epoch %d\n", epoch)
emptyTicksPerEpoch, err := tick.CalculateEmptyTicksForEpoch(ctx, ps, epoch)
if err != nil {
return errors.Wrapf(err, "calculating empty ticks for epoch %d", epoch)
}

err = ps.SetEmptyTicksPerEpoch(epoch, emptyTicksPerEpoch)
if err != nil {
return errors.Wrap(err, "saving emptyTickCount to database")
}
}

return nil
err = tick.CalculateEmptyTicksForAllEpochs(ps)
if err != nil {
return errors.Wrap(err, "calculating empty ticks for all epochs")
}

p, err := qubic.NewPoolConnection(qubic.PoolConfig{
Expand Down
12 changes: 7 additions & 5 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func (s *PebbleStore) GetProcessedTickIntervals(ctx context.Context) ([]*protobu
return processedTickIntervals, nil
}

func (s *PebbleStore) SetEmptyTicksPerEpoch(epoch uint32, emptyTicksCount uint32) error {
func (s *PebbleStore) SetEmptyTicksForEpoch(epoch uint32, emptyTicksCount uint32) error {
key := emptyTicksPerEpochKey(epoch)

value := make([]byte, 4)
Expand All @@ -673,13 +673,13 @@ func (s *PebbleStore) SetEmptyTicksPerEpoch(epoch uint32, emptyTicksCount uint32
return nil
}

func (s *PebbleStore) GetEmptyTicksPerEpoch(epoch uint32) (uint32, error) {
func (s *PebbleStore) GetEmptyTicksForEpoch(epoch uint32) (uint32, error) {
key := emptyTicksPerEpochKey(epoch)

value, closer, err := s.db.Get(key)
if err != nil {
if errors.Is(err, pebble.ErrNotFound) {
return 0, nil
return 0, err
}

return 0, errors.Wrapf(err, "getting emptyTickCount for epoch %d", epoch)
Expand All @@ -696,9 +696,11 @@ func (s *PebbleStore) GetEmptyTicksForEpochs(epochs []uint32) (map[uint32]uint32
emptyTickMap := make(map[uint32]uint32, len(epochs))

for _, epoch := range epochs {
emptyTicks, err := s.GetEmptyTicksPerEpoch(epoch)
emptyTicks, err := s.GetEmptyTicksForEpoch(epoch)
if err != nil {
return nil, errors.Wrapf(err, "getting empty ticks for epoch %d", epoch)
if !errors.Is(err, pebble.ErrNotFound) {
return nil, errors.Wrapf(err, "getting empty ticks for epoch %d", epoch)
}
}
emptyTickMap[epoch] = emptyTicks
}
Expand Down
37 changes: 37 additions & 0 deletions validator/tick/empty_tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package tick
import (
"context"
"fmt"
"github.com/cockroachdb/pebble"
"github.com/pkg/errors"
"github.com/qubic/go-archiver/protobuff"
"github.com/qubic/go-archiver/store"
"github.com/qubic/go-node-connector/types"
"google.golang.org/protobuf/proto"
"time"
)

var emptyTickData = &protobuff.TickData{}
Expand Down Expand Up @@ -65,3 +67,38 @@ func CheckIfTickIsEmpty(tickData types.TickData) (bool, error) {

return CheckIfTickIsEmptyProto(data), nil
}

func CalculateEmptyTicksForAllEpochs(ps *store.PebbleStore) error {

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()

epochs, err := ps.GetLastProcessedTicksPerEpoch(ctx)
if err != nil {
return errors.Wrap(err, "getting epoch list from db")
}

for epoch, _ := range epochs {

_, err := ps.GetEmptyTicksForEpoch(epoch)
if err == nil {
return nil // We have the empty ticks
}
if !errors.Is(err, pebble.ErrNotFound) {
return errors.Wrap(err, "checking if epoch has empty ticks") // Some other error occured
}

fmt.Printf("Calculating empty ticks for epoch %d\n", epoch)
emptyTicksPerEpoch, err := CalculateEmptyTicksForEpoch(ctx, ps, epoch)
if err != nil {
return errors.Wrapf(err, "calculating empty ticks for epoch %d", epoch)
}

err = ps.SetEmptyTicksForEpoch(epoch, emptyTicksPerEpoch)
if err != nil {
return errors.Wrap(err, "saving emptyTickCount to database")
}

}
return nil
}
9 changes: 6 additions & 3 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/cockroachdb/pebble"
"github.com/pkg/errors"
"github.com/qubic/go-archiver/protobuff"
"github.com/qubic/go-archiver/store"
Expand Down Expand Up @@ -163,9 +164,11 @@ func (v *Validator) ValidateTick(ctx context.Context, initialEpochTick, tickNumb
}

if isEmpty {
emptyTicks, err := v.store.GetEmptyTicksPerEpoch(uint32(epoch))
emptyTicks, err := v.store.GetEmptyTicksForEpoch(uint32(epoch))
if err != nil {
return errors.Wrap(err, "getting empty ticks for current epoch")
if !errors.Is(err, pebble.ErrNotFound) {
return errors.Wrap(err, "getting empty ticks for current epoch")
}
}

if emptyTicks == 0 {
Expand All @@ -174,7 +177,7 @@ func (v *Validator) ValidateTick(ctx context.Context, initialEpochTick, tickNumb

emptyTicks += 1

err = v.store.SetEmptyTicksPerEpoch(uint32(epoch), emptyTicks)
err = v.store.SetEmptyTicksForEpoch(uint32(epoch), emptyTicks)
if err != nil {
return errors.Wrap(err, "setting current ticks for current epoch")
}
Expand Down

0 comments on commit 9ab07c0

Please sign in to comment.