2024-02-13 07:04:56 +00:00
|
|
|
|
package time
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2024-02-18 04:52:19 +00:00
|
|
|
|
"encoding/hex"
|
2024-02-13 07:04:56 +00:00
|
|
|
|
"math/big"
|
2024-02-20 23:12:29 +00:00
|
|
|
|
"sort"
|
2024-02-13 07:04:56 +00:00
|
|
|
|
"sync"
|
|
|
|
|
|
2024-02-16 21:46:54 +00:00
|
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2024-02-13 07:04:56 +00:00
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/config"
|
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/crypto"
|
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
|
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/store"
|
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/tries"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var unknownDistance = new(big.Int).SetBytes([]byte{
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
type pendingFrame struct {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
selector *big.Int
|
2024-02-13 07:04:56 +00:00
|
|
|
|
parentSelector *big.Int
|
2024-03-04 03:20:24 +00:00
|
|
|
|
frameNumber uint64
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DataTimeReel struct {
|
|
|
|
|
rwMutex sync.RWMutex
|
2024-02-16 21:46:54 +00:00
|
|
|
|
running bool
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
2024-10-17 04:51:27 +00:00
|
|
|
|
filter []byte
|
|
|
|
|
engineConfig *config.EngineConfig
|
|
|
|
|
logger *zap.Logger
|
|
|
|
|
clockStore store.ClockStore
|
|
|
|
|
frameProver crypto.FrameProver
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
|
|
|
|
origin []byte
|
|
|
|
|
initialInclusionProof *crypto.InclusionAggregateProof
|
|
|
|
|
initialProverKeys [][]byte
|
|
|
|
|
head *protobufs.ClockFrame
|
|
|
|
|
totalDistance *big.Int
|
|
|
|
|
headDistance *big.Int
|
2024-02-16 21:46:54 +00:00
|
|
|
|
lruFrames *lru.Cache[string, string]
|
2024-10-12 18:55:17 +00:00
|
|
|
|
proverTries []*tries.RollingFrecencyCritbitTrie
|
2024-02-13 07:04:56 +00:00
|
|
|
|
pending map[uint64][]*pendingFrame
|
|
|
|
|
incompleteForks map[uint64][]*pendingFrame
|
2024-03-04 03:20:24 +00:00
|
|
|
|
frames chan *pendingFrame
|
2024-02-13 07:04:56 +00:00
|
|
|
|
newFrameCh chan *protobufs.ClockFrame
|
|
|
|
|
badFrameCh chan *protobufs.ClockFrame
|
|
|
|
|
done chan bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewDataTimeReel(
|
|
|
|
|
filter []byte,
|
|
|
|
|
logger *zap.Logger,
|
|
|
|
|
clockStore store.ClockStore,
|
|
|
|
|
engineConfig *config.EngineConfig,
|
|
|
|
|
frameProver crypto.FrameProver,
|
|
|
|
|
origin []byte,
|
|
|
|
|
initialInclusionProof *crypto.InclusionAggregateProof,
|
|
|
|
|
initialProverKeys [][]byte,
|
|
|
|
|
) *DataTimeReel {
|
|
|
|
|
if filter == nil {
|
|
|
|
|
panic("filter is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if logger == nil {
|
|
|
|
|
panic("logger is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if clockStore == nil {
|
|
|
|
|
panic("clock store is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if engineConfig == nil {
|
|
|
|
|
panic("engine config is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if frameProver == nil {
|
|
|
|
|
panic("frame prover is nil")
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-16 21:46:54 +00:00
|
|
|
|
cache, err := lru.New[string, string](10000)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
return &DataTimeReel{
|
2024-02-16 21:46:54 +00:00
|
|
|
|
running: false,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
logger: logger,
|
|
|
|
|
filter: filter,
|
|
|
|
|
engineConfig: engineConfig,
|
|
|
|
|
clockStore: clockStore,
|
|
|
|
|
frameProver: frameProver,
|
|
|
|
|
origin: origin,
|
|
|
|
|
initialInclusionProof: initialInclusionProof,
|
|
|
|
|
initialProverKeys: initialProverKeys,
|
2024-02-16 21:46:54 +00:00
|
|
|
|
lruFrames: cache,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
pending: make(map[uint64][]*pendingFrame),
|
|
|
|
|
incompleteForks: make(map[uint64][]*pendingFrame),
|
2024-03-04 03:20:24 +00:00
|
|
|
|
frames: make(chan *pendingFrame),
|
2024-02-13 07:04:56 +00:00
|
|
|
|
newFrameCh: make(chan *protobufs.ClockFrame),
|
|
|
|
|
badFrameCh: make(chan *protobufs.ClockFrame),
|
|
|
|
|
done: make(chan bool),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) Start() error {
|
2024-10-12 18:55:17 +00:00
|
|
|
|
frame, tries, err := d.clockStore.GetLatestDataClockFrame(d.filter)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
if err != nil && !errors.Is(err, store.ErrNotFound) {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if frame == nil {
|
2024-10-12 18:55:17 +00:00
|
|
|
|
d.head, d.proverTries = d.createGenesisFrame()
|
2024-02-13 07:04:56 +00:00
|
|
|
|
d.totalDistance = big.NewInt(0)
|
2024-02-16 21:46:54 +00:00
|
|
|
|
d.headDistance = big.NewInt(0)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
} else {
|
|
|
|
|
d.head = frame
|
2024-02-16 21:46:54 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-03-27 08:50:52 +00:00
|
|
|
|
d.totalDistance = big.NewInt(0)
|
2024-10-12 18:55:17 +00:00
|
|
|
|
d.proverTries = tries
|
2024-02-16 21:46:54 +00:00
|
|
|
|
d.headDistance, err = d.GetDistance(frame)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go d.runLoop()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) Head() (*protobufs.ClockFrame, error) {
|
|
|
|
|
return d.head, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert enqueues a structurally valid frame into the time reel. If the frame
|
|
|
|
|
// is the next one in sequence, it advances the reel head forward and emits a
|
|
|
|
|
// new frame on the new frame channel.
|
2024-03-17 21:14:37 +00:00
|
|
|
|
func (d *DataTimeReel) Insert(frame *protobufs.ClockFrame, isSync bool) error {
|
2024-02-16 21:46:54 +00:00
|
|
|
|
if !d.running {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"insert frame",
|
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
|
|
|
|
zap.String("output_tag", hex.EncodeToString(frame.Output[:64])),
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-16 21:46:54 +00:00
|
|
|
|
if d.lruFrames.Contains(string(frame.Output[:64])) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.lruFrames.Add(string(frame.Output[:64]), string(frame.ParentSelector))
|
|
|
|
|
|
2024-03-04 03:20:24 +00:00
|
|
|
|
parent := new(big.Int).SetBytes(frame.ParentSelector)
|
|
|
|
|
selector, err := frame.GetSelector()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distance, _ := d.GetDistance(frame)
|
|
|
|
|
|
|
|
|
|
d.storePending(selector, parent, distance, frame)
|
|
|
|
|
|
2024-03-17 21:14:37 +00:00
|
|
|
|
if !isSync {
|
|
|
|
|
go func() {
|
|
|
|
|
d.frames <- &pendingFrame{
|
|
|
|
|
selector: selector,
|
|
|
|
|
parentSelector: parent,
|
|
|
|
|
frameNumber: frame.FrameNumber,
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
|
func (
|
|
|
|
|
d *DataTimeReel,
|
|
|
|
|
) GetFrameProverTries() []*tries.RollingFrecencyCritbitTrie {
|
|
|
|
|
return d.proverTries
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) NewFrameCh() <-chan *protobufs.ClockFrame {
|
|
|
|
|
return d.newFrameCh
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) BadFrameCh() <-chan *protobufs.ClockFrame {
|
|
|
|
|
return d.badFrameCh
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) Stop() {
|
|
|
|
|
d.done <- true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) createGenesisFrame() (
|
|
|
|
|
*protobufs.ClockFrame,
|
2024-10-12 18:55:17 +00:00
|
|
|
|
[]*tries.RollingFrecencyCritbitTrie,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
) {
|
|
|
|
|
if d.origin == nil {
|
|
|
|
|
panic("origin is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if d.initialInclusionProof == nil {
|
|
|
|
|
panic("initial inclusion proof is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if d.initialProverKeys == nil {
|
|
|
|
|
panic("initial prover keys is nil")
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 07:11:12 +00:00
|
|
|
|
difficulty := d.engineConfig.Difficulty
|
2024-06-08 11:32:45 +00:00
|
|
|
|
if difficulty == 0 || difficulty == 10000 {
|
2024-10-17 04:51:27 +00:00
|
|
|
|
difficulty = 200000
|
2024-02-14 07:11:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
|
frame, tries, err := d.frameProver.CreateDataGenesisFrame(
|
2024-02-13 07:04:56 +00:00
|
|
|
|
d.filter,
|
|
|
|
|
d.origin,
|
2024-02-14 07:11:12 +00:00
|
|
|
|
difficulty,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
d.initialInclusionProof,
|
|
|
|
|
d.initialProverKeys,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
|
selector, err := frame.GetSelector()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
txn, err := d.clockStore.NewTransaction()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
|
err = d.clockStore.StageDataClockFrame(
|
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
2024-02-13 07:04:56 +00:00
|
|
|
|
frame,
|
2024-03-12 07:45:20 +00:00
|
|
|
|
txn,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
txn.Abort()
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := d.clockStore.CommitDataClockFrame(
|
|
|
|
|
d.filter,
|
|
|
|
|
0,
|
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
2024-10-12 18:55:17 +00:00
|
|
|
|
tries,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
txn,
|
|
|
|
|
false,
|
|
|
|
|
); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := txn.Commit(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
|
return frame, tries
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main data consensus loop
|
|
|
|
|
func (d *DataTimeReel) runLoop() {
|
2024-02-16 21:46:54 +00:00
|
|
|
|
d.running = true
|
2024-02-13 07:04:56 +00:00
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case frame := <-d.frames:
|
2024-10-18 06:55:51 +00:00
|
|
|
|
rawFrame, err := d.clockStore.GetStagedDataClockFrame(
|
|
|
|
|
d.filter,
|
|
|
|
|
frame.frameNumber,
|
|
|
|
|
frame.selector.FillBytes(make([]byte, 32)),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"processing frame",
|
|
|
|
|
zap.Uint64("frame_number", rawFrame.FrameNumber),
|
|
|
|
|
zap.String("output_tag", hex.EncodeToString(rawFrame.Output[:64])),
|
|
|
|
|
zap.Uint64("head_number", d.head.FrameNumber),
|
|
|
|
|
zap.String("head_output_tag", hex.EncodeToString(d.head.Output[:64])),
|
|
|
|
|
)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
// Most common scenario: in order – new frame is higher number
|
2024-10-18 06:55:51 +00:00
|
|
|
|
if d.head.FrameNumber < rawFrame.FrameNumber {
|
|
|
|
|
d.logger.Debug("frame is higher")
|
2024-02-18 04:52:19 +00:00
|
|
|
|
|
2024-10-18 06:55:51 +00:00
|
|
|
|
parent := new(big.Int).SetBytes(rawFrame.ParentSelector)
|
|
|
|
|
selector, err := rawFrame.GetSelector()
|
2024-03-04 03:20:24 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distance, err := d.GetDistance(rawFrame)
|
|
|
|
|
if err != nil {
|
2024-10-18 06:55:51 +00:00
|
|
|
|
if !errors.Is(err, store.ErrNotFound) {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.addPending(selector, parent, frame.frameNumber)
|
|
|
|
|
d.processPending(d.head, frame)
|
|
|
|
|
continue
|
2024-03-04 03:20:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headSelector, err := d.head.GetSelector()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-18 06:55:51 +00:00
|
|
|
|
// If the frame has a gap from the head or is not descendent, mark it as
|
|
|
|
|
// pending:
|
|
|
|
|
if rawFrame.FrameNumber-d.head.FrameNumber != 1 {
|
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"frame has has gap, fork choice",
|
|
|
|
|
zap.Bool("has_gap", rawFrame.FrameNumber-d.head.FrameNumber != 1),
|
|
|
|
|
zap.String("parent_selector", parent.Text(16)),
|
|
|
|
|
zap.String("head_selector", headSelector.Text(16)),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
d.forkChoice(rawFrame, distance)
|
|
|
|
|
d.processPending(d.head, frame)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-18 06:55:51 +00:00
|
|
|
|
// Otherwise set it as the next and process all pending
|
|
|
|
|
d.setHead(rawFrame, distance)
|
|
|
|
|
d.processPending(d.head, frame)
|
|
|
|
|
} else if d.head.FrameNumber == rawFrame.FrameNumber {
|
|
|
|
|
// frames are equivalent, no need to act
|
|
|
|
|
if bytes.Equal(d.head.Output, rawFrame.Output) {
|
|
|
|
|
d.logger.Debug("equivalent frame")
|
|
|
|
|
d.processPending(d.head, frame)
|
|
|
|
|
continue
|
2024-03-04 03:20:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distance, err := d.GetDistance(rawFrame)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-10-18 06:55:51 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"frame is same height",
|
|
|
|
|
zap.String("head_distance", d.headDistance.Text(16)),
|
|
|
|
|
zap.String("distance", distance.Text(16)),
|
|
|
|
|
)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
|
|
|
|
// Optimization: if competing frames share a parent we can short-circuit
|
|
|
|
|
// fork choice
|
2024-10-18 06:55:51 +00:00
|
|
|
|
if bytes.Equal(d.head.ParentSelector, rawFrame.ParentSelector) &&
|
|
|
|
|
distance.Cmp(d.headDistance) < 0 {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"frame shares parent, has shorter distance, short circuit",
|
|
|
|
|
)
|
2024-10-18 06:55:51 +00:00
|
|
|
|
d.totalDistance.Sub(d.totalDistance, d.headDistance)
|
2024-03-04 03:20:24 +00:00
|
|
|
|
d.setHead(rawFrame, distance)
|
2024-10-18 06:55:51 +00:00
|
|
|
|
d.processPending(d.head, frame)
|
2024-02-16 09:42:37 +00:00
|
|
|
|
continue
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
2024-10-18 06:55:51 +00:00
|
|
|
|
|
|
|
|
|
// Choose fork
|
|
|
|
|
d.forkChoice(rawFrame, distance)
|
|
|
|
|
d.processPending(d.head, frame)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
} else {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug("frame is lower height")
|
2024-10-18 06:55:51 +00:00
|
|
|
|
|
|
|
|
|
// tag: dusk – we should have some kind of check here to avoid brutal
|
|
|
|
|
// thrashing
|
|
|
|
|
existing, _, err := d.clockStore.GetDataClockFrame(
|
|
|
|
|
d.filter,
|
|
|
|
|
rawFrame.FrameNumber,
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// if this returns an error it's either not found (which shouldn't
|
|
|
|
|
// happen without corruption) or pebble is borked, either way, panic
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// It's a fork, but it's behind. We need to stash it until it catches
|
|
|
|
|
// up (or dies off)
|
|
|
|
|
if !bytes.Equal(existing.Output, rawFrame.Output) {
|
|
|
|
|
d.logger.Debug("is fork, add pending")
|
|
|
|
|
parent, selector, err := rawFrame.GetParentAndSelector()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.addPending(selector, parent, frame.frameNumber)
|
|
|
|
|
d.processPending(d.head, frame)
|
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
case <-d.done:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) addPending(
|
|
|
|
|
selector *big.Int,
|
|
|
|
|
parent *big.Int,
|
2024-03-04 03:20:24 +00:00
|
|
|
|
frameNumber uint64,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
) {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"add pending",
|
|
|
|
|
zap.Uint64("head_frame_number", d.head.FrameNumber),
|
2024-03-04 03:20:24 +00:00
|
|
|
|
zap.Uint64("add_frame_number", frameNumber),
|
2024-02-18 04:52:19 +00:00
|
|
|
|
zap.String("selector", selector.Text(16)),
|
|
|
|
|
zap.String("parent", parent.Text(16)),
|
|
|
|
|
)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
2024-03-04 03:20:24 +00:00
|
|
|
|
if d.head.FrameNumber <= frameNumber {
|
|
|
|
|
if _, ok := d.pending[frameNumber]; !ok {
|
|
|
|
|
d.pending[frameNumber] = []*pendingFrame{}
|
2024-02-16 21:46:54 +00:00
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
2024-02-18 04:52:19 +00:00
|
|
|
|
// avoid heavy thrashing
|
2024-03-04 03:20:24 +00:00
|
|
|
|
for _, frame := range d.pending[frameNumber] {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
if frame.selector.Cmp(selector) == 0 {
|
|
|
|
|
d.logger.Debug("exists in pending already")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 03:20:24 +00:00
|
|
|
|
if d.head.FrameNumber <= frameNumber {
|
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"accumulate in pending",
|
|
|
|
|
zap.Int("pending_neighbors", len(d.pending[frameNumber])),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
d.pending[frameNumber] = append(
|
|
|
|
|
d.pending[frameNumber],
|
|
|
|
|
&pendingFrame{
|
|
|
|
|
selector: selector,
|
|
|
|
|
parentSelector: parent,
|
|
|
|
|
frameNumber: frameNumber,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) storePending(
|
|
|
|
|
selector *big.Int,
|
|
|
|
|
parent *big.Int,
|
|
|
|
|
distance *big.Int,
|
|
|
|
|
frame *protobufs.ClockFrame,
|
|
|
|
|
) {
|
2024-02-16 21:46:54 +00:00
|
|
|
|
// avoid db thrashing
|
2024-03-12 07:45:20 +00:00
|
|
|
|
if existing, err := d.clockStore.GetStagedDataClockFrame(
|
2024-02-16 21:46:54 +00:00
|
|
|
|
frame.Filter,
|
|
|
|
|
frame.FrameNumber,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
2024-02-22 06:07:17 +00:00
|
|
|
|
true,
|
2024-02-16 21:46:54 +00:00
|
|
|
|
); err != nil && existing == nil {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"not stored yet, save data candidate",
|
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
|
|
|
|
zap.String("selector", selector.Text(16)),
|
|
|
|
|
zap.String("parent", parent.Text(16)),
|
|
|
|
|
zap.String("distance", distance.Text(16)),
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-16 21:46:54 +00:00
|
|
|
|
txn, err := d.clockStore.NewTransaction()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-03-12 07:45:20 +00:00
|
|
|
|
err = d.clockStore.StageDataClockFrame(
|
2024-02-16 21:46:54 +00:00
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
|
|
|
|
frame,
|
|
|
|
|
txn,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
txn.Abort()
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if err = txn.Commit(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
2024-02-18 04:52:19 +00:00
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
2024-02-18 04:52:19 +00:00
|
|
|
|
func (d *DataTimeReel) processPending(
|
|
|
|
|
frame *protobufs.ClockFrame,
|
2024-03-04 03:20:24 +00:00
|
|
|
|
lastReceived *pendingFrame,
|
2024-02-18 04:52:19 +00:00
|
|
|
|
) {
|
2024-02-21 08:10:23 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"process pending",
|
|
|
|
|
zap.Int("pending_frame_numbers", len(d.pending)),
|
|
|
|
|
)
|
2024-02-20 23:12:29 +00:00
|
|
|
|
frameNumbers := []uint64{}
|
|
|
|
|
for f := range d.pending {
|
|
|
|
|
frameNumbers = append(frameNumbers, f)
|
2024-02-21 08:10:23 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"pending per frame number",
|
|
|
|
|
zap.Uint64("pending_frame_number", f),
|
|
|
|
|
zap.Int("pending_frames", len(d.pending[f])),
|
|
|
|
|
)
|
2024-02-20 23:12:29 +00:00
|
|
|
|
}
|
|
|
|
|
sort.Slice(frameNumbers, func(i, j int) bool {
|
2024-02-21 08:10:23 +00:00
|
|
|
|
return frameNumbers[i] > frameNumbers[j]
|
2024-02-20 23:12:29 +00:00
|
|
|
|
})
|
2024-02-21 08:10:23 +00:00
|
|
|
|
|
2024-03-04 03:20:24 +00:00
|
|
|
|
lastSelector := lastReceived.selector
|
2024-02-21 08:10:23 +00:00
|
|
|
|
|
2024-02-20 23:12:29 +00:00
|
|
|
|
for _, f := range frameNumbers {
|
|
|
|
|
if f < d.head.FrameNumber {
|
|
|
|
|
delete(d.pending, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextPending := d.pending[f]
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"checking frame set",
|
|
|
|
|
zap.Uint64("pending_frame_number", f),
|
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
|
|
|
|
)
|
|
|
|
|
if f < frame.FrameNumber {
|
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"purging frame set",
|
|
|
|
|
zap.Uint64("pending_frame_number", f),
|
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
2024-02-13 07:04:56 +00:00
|
|
|
|
)
|
2024-02-18 04:52:19 +00:00
|
|
|
|
delete(d.pending, f)
|
|
|
|
|
continue
|
2024-02-16 09:42:37 +00:00
|
|
|
|
}
|
2024-02-18 04:52:19 +00:00
|
|
|
|
// Pull the next
|
2024-02-21 08:10:23 +00:00
|
|
|
|
for len(nextPending) != 0 {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug("try process next")
|
2024-02-16 09:42:37 +00:00
|
|
|
|
next := nextPending[0]
|
2024-02-21 08:10:23 +00:00
|
|
|
|
d.pending[f] = d.pending[f][1:]
|
2024-03-04 03:20:24 +00:00
|
|
|
|
if f == lastReceived.frameNumber && next.selector.Cmp(lastSelector) == 0 {
|
2024-02-21 08:10:23 +00:00
|
|
|
|
d.pending[f] = append(d.pending[f], next)
|
|
|
|
|
if len(d.pending[f]) == 1 {
|
|
|
|
|
nextPending = nil
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
2024-02-16 09:42:37 +00:00
|
|
|
|
|
2024-03-04 03:20:24 +00:00
|
|
|
|
go func() {
|
|
|
|
|
d.frames <- next
|
|
|
|
|
}()
|
|
|
|
|
return
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) setHead(frame *protobufs.ClockFrame, distance *big.Int) {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"set frame to head",
|
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
|
|
|
|
zap.String("output_tag", hex.EncodeToString(frame.Output[:64])),
|
|
|
|
|
zap.Uint64("head_number", d.head.FrameNumber),
|
|
|
|
|
zap.String("head_output_tag", hex.EncodeToString(d.head.Output[:64])),
|
|
|
|
|
)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
txn, err := d.clockStore.NewTransaction()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"save data",
|
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
|
|
|
|
zap.String("distance", distance.Text(16)),
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
|
selector, err := frame.GetSelector()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := d.clockStore.CommitDataClockFrame(
|
|
|
|
|
d.filter,
|
|
|
|
|
frame.FrameNumber,
|
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
2024-10-12 18:55:17 +00:00
|
|
|
|
d.proverTries,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
txn,
|
|
|
|
|
false,
|
|
|
|
|
); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = txn.Commit(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.head = frame
|
2024-03-14 07:18:14 +00:00
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
d.headDistance = distance
|
|
|
|
|
go func() {
|
|
|
|
|
d.newFrameCh <- frame
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) getTotalDistance(frame *protobufs.ClockFrame) *big.Int {
|
2024-03-14 07:18:14 +00:00
|
|
|
|
selector, err := frame.GetSelector()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
total, err := d.clockStore.GetTotalDistance(
|
|
|
|
|
d.filter,
|
|
|
|
|
frame.FrameNumber,
|
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
|
|
|
|
)
|
|
|
|
|
if err == nil && total != nil {
|
|
|
|
|
return total
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
total, err = d.GetDistance(frame)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for index := frame; err == nil &&
|
2024-03-12 07:45:20 +00:00
|
|
|
|
index.FrameNumber > 0; index, err = d.clockStore.GetStagedDataClockFrame(
|
2024-02-13 07:04:56 +00:00
|
|
|
|
d.filter,
|
|
|
|
|
index.FrameNumber-1,
|
|
|
|
|
index.ParentSelector,
|
2024-02-22 06:07:17 +00:00
|
|
|
|
true,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
) {
|
|
|
|
|
distance, err := d.GetDistance(index)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
total.Add(total, distance)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 07:18:14 +00:00
|
|
|
|
d.clockStore.SetTotalDistance(
|
|
|
|
|
d.filter,
|
|
|
|
|
frame.FrameNumber,
|
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
|
|
|
|
total,
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
return total
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) GetDistance(frame *protobufs.ClockFrame) (
|
|
|
|
|
*big.Int,
|
|
|
|
|
error,
|
|
|
|
|
) {
|
2024-10-17 04:51:27 +00:00
|
|
|
|
if frame.FrameNumber == 0 {
|
|
|
|
|
return big.NewInt(0), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prev, _, err := d.clockStore.GetDataClockFrame(
|
|
|
|
|
d.filter,
|
|
|
|
|
frame.FrameNumber-1,
|
|
|
|
|
false,
|
|
|
|
|
)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
if err != nil {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
return unknownDistance, errors.Wrap(err, "get distance")
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 04:51:27 +00:00
|
|
|
|
prevSelector, err := prev.GetSelector()
|
2024-02-13 07:04:56 +00:00
|
|
|
|
if err != nil {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
return unknownDistance, errors.Wrap(err, "get distance")
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
discriminatorNode :=
|
2024-10-17 04:51:27 +00:00
|
|
|
|
d.proverTries[0].FindNearest(prevSelector.FillBytes(make([]byte, 32)))
|
2024-02-13 07:04:56 +00:00
|
|
|
|
discriminator := discriminatorNode.External.Key
|
|
|
|
|
addr, err := frame.GetAddress()
|
|
|
|
|
if err != nil {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
return unknownDistance, errors.Wrap(err, "get distance")
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
distance := new(big.Int).Sub(
|
|
|
|
|
new(big.Int).SetBytes(discriminator),
|
|
|
|
|
new(big.Int).SetBytes(addr),
|
|
|
|
|
)
|
|
|
|
|
distance.Abs(distance)
|
|
|
|
|
|
|
|
|
|
return distance, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataTimeReel) forkChoice(
|
|
|
|
|
frame *protobufs.ClockFrame,
|
|
|
|
|
distance *big.Int,
|
|
|
|
|
) {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"fork choice",
|
|
|
|
|
zap.Uint64("frame_number", frame.FrameNumber),
|
|
|
|
|
zap.String("output_tag", hex.EncodeToString(frame.Output[:64])),
|
|
|
|
|
zap.Uint64("head_number", d.head.FrameNumber),
|
|
|
|
|
zap.String("head_output_tag", hex.EncodeToString(d.head.Output[:64])),
|
|
|
|
|
)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
parentSelector, selector, err := frame.GetParentAndSelector()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leftIndex := d.head
|
|
|
|
|
rightIndex := frame
|
|
|
|
|
leftTotal := new(big.Int).Set(d.headDistance)
|
2024-02-21 08:10:23 +00:00
|
|
|
|
overweight := big.NewInt(0)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
rightTotal := new(big.Int).Set(distance)
|
|
|
|
|
left := d.head.ParentSelector
|
|
|
|
|
right := frame.ParentSelector
|
|
|
|
|
|
2024-02-16 09:42:37 +00:00
|
|
|
|
rightReplaySelectors := [][]byte{}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
2024-02-21 08:10:23 +00:00
|
|
|
|
for rightIndex.FrameNumber > leftIndex.FrameNumber {
|
|
|
|
|
rightReplaySelectors = append(
|
|
|
|
|
append(
|
|
|
|
|
[][]byte{},
|
|
|
|
|
right,
|
|
|
|
|
),
|
|
|
|
|
rightReplaySelectors...,
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
|
rightIndex, err = d.clockStore.GetStagedDataClockFrame(
|
2024-02-21 08:10:23 +00:00
|
|
|
|
d.filter,
|
|
|
|
|
rightIndex.FrameNumber-1,
|
|
|
|
|
rightIndex.ParentSelector,
|
2024-02-22 06:07:17 +00:00
|
|
|
|
true,
|
2024-02-21 08:10:23 +00:00
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// If lineage cannot be verified, set it for later
|
|
|
|
|
if errors.Is(err, store.ErrNotFound) {
|
2024-03-04 03:20:24 +00:00
|
|
|
|
d.addPending(selector, parentSelector, frame.FrameNumber)
|
2024-02-21 08:10:23 +00:00
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
right = rightIndex.ParentSelector
|
|
|
|
|
|
|
|
|
|
rightIndexDistance, err := d.GetDistance(rightIndex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We accumulate right on left when right is longer because we cannot know
|
|
|
|
|
// where the left will lead and don't want it to disadvantage our comparison
|
|
|
|
|
overweight.Add(overweight, rightIndexDistance)
|
|
|
|
|
rightTotal.Add(rightTotal, rightIndexDistance)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
// Walk backwards through the parents, until we find a matching parent
|
|
|
|
|
// selector:
|
|
|
|
|
for !bytes.Equal(left, right) {
|
2024-02-21 08:10:23 +00:00
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"scan backwards",
|
|
|
|
|
zap.String("left_parent", hex.EncodeToString(leftIndex.ParentSelector)),
|
|
|
|
|
zap.String("right_parent", hex.EncodeToString(rightIndex.ParentSelector)),
|
|
|
|
|
)
|
2024-02-18 04:52:19 +00:00
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
rightReplaySelectors = append(
|
|
|
|
|
append(
|
|
|
|
|
[][]byte{},
|
|
|
|
|
right,
|
|
|
|
|
),
|
|
|
|
|
rightReplaySelectors...,
|
|
|
|
|
)
|
2024-03-12 07:45:20 +00:00
|
|
|
|
leftIndex, err = d.clockStore.GetStagedDataClockFrame(
|
2024-02-13 07:04:56 +00:00
|
|
|
|
d.filter,
|
|
|
|
|
leftIndex.FrameNumber-1,
|
|
|
|
|
leftIndex.ParentSelector,
|
2024-02-22 06:07:17 +00:00
|
|
|
|
true,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2024-02-23 03:23:26 +00:00
|
|
|
|
d.logger.Error(
|
|
|
|
|
"store corruption: a discontinuity has been found in your time reel",
|
|
|
|
|
zap.String(
|
|
|
|
|
"selector",
|
|
|
|
|
hex.EncodeToString(leftIndex.ParentSelector),
|
|
|
|
|
),
|
|
|
|
|
zap.Uint64("frame_number", leftIndex.FrameNumber-1),
|
|
|
|
|
)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
|
rightIndex, err = d.clockStore.GetStagedDataClockFrame(
|
2024-02-13 07:04:56 +00:00
|
|
|
|
d.filter,
|
|
|
|
|
rightIndex.FrameNumber-1,
|
|
|
|
|
rightIndex.ParentSelector,
|
2024-02-22 06:07:17 +00:00
|
|
|
|
true,
|
2024-02-13 07:04:56 +00:00
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// If lineage cannot be verified, set it for later
|
|
|
|
|
if errors.Is(err, store.ErrNotFound) {
|
2024-03-04 03:20:24 +00:00
|
|
|
|
d.addPending(selector, parentSelector, frame.FrameNumber)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
left = leftIndex.ParentSelector
|
|
|
|
|
right = rightIndex.ParentSelector
|
|
|
|
|
leftIndexDistance, err := d.GetDistance(leftIndex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rightIndexDistance, err := d.GetDistance(rightIndex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leftTotal.Add(leftTotal, leftIndexDistance)
|
|
|
|
|
rightTotal.Add(rightTotal, rightIndexDistance)
|
|
|
|
|
}
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug("found mutual root")
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
2024-02-16 09:42:37 +00:00
|
|
|
|
frameNumber := rightIndex.FrameNumber
|
|
|
|
|
|
2024-02-21 08:10:23 +00:00
|
|
|
|
overweight.Add(overweight, leftTotal)
|
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
// Choose new fork based on lightest distance sub-tree
|
2024-02-21 08:10:23 +00:00
|
|
|
|
if rightTotal.Cmp(overweight) > 0 {
|
2024-02-18 04:52:19 +00:00
|
|
|
|
d.logger.Debug("proposed fork has greater distance",
|
|
|
|
|
zap.String("right_total", rightTotal.Text(16)),
|
2024-02-21 08:10:23 +00:00
|
|
|
|
zap.String("left_total", overweight.Text(16)),
|
2024-02-18 04:52:19 +00:00
|
|
|
|
)
|
2024-03-04 03:20:24 +00:00
|
|
|
|
d.addPending(selector, parentSelector, frame.FrameNumber)
|
2024-02-18 04:52:19 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-02-13 07:04:56 +00:00
|
|
|
|
|
2024-02-18 04:52:19 +00:00
|
|
|
|
for {
|
|
|
|
|
if len(rightReplaySelectors) == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
next := rightReplaySelectors[0]
|
|
|
|
|
rightReplaySelectors =
|
|
|
|
|
rightReplaySelectors[1:]
|
2024-02-16 09:42:37 +00:00
|
|
|
|
|
|
|
|
|
txn, err := d.clockStore.NewTransaction()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
|
if err := d.clockStore.CommitDataClockFrame(
|
|
|
|
|
d.filter,
|
|
|
|
|
frameNumber,
|
|
|
|
|
next,
|
2024-10-12 18:55:17 +00:00
|
|
|
|
d.proverTries,
|
2024-02-16 09:42:37 +00:00
|
|
|
|
txn,
|
2024-03-08 05:05:04 +00:00
|
|
|
|
rightIndex.FrameNumber < d.head.FrameNumber,
|
2024-02-16 09:42:37 +00:00
|
|
|
|
); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = txn.Commit(); err != nil {
|
|
|
|
|
panic(err)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 04:52:19 +00:00
|
|
|
|
frameNumber++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
txn, err := d.clockStore.NewTransaction()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
2024-02-18 04:52:19 +00:00
|
|
|
|
|
2024-03-12 07:45:20 +00:00
|
|
|
|
if err := d.clockStore.CommitDataClockFrame(
|
|
|
|
|
d.filter,
|
|
|
|
|
frame.FrameNumber,
|
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
2024-10-12 18:55:17 +00:00
|
|
|
|
d.proverTries,
|
2024-02-18 04:52:19 +00:00
|
|
|
|
txn,
|
|
|
|
|
false,
|
|
|
|
|
); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = txn.Commit(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.head = frame
|
|
|
|
|
d.totalDistance.Sub(d.totalDistance, leftTotal)
|
|
|
|
|
d.totalDistance.Add(d.totalDistance, rightTotal)
|
|
|
|
|
d.headDistance = distance
|
|
|
|
|
d.logger.Debug(
|
|
|
|
|
"set total distance",
|
|
|
|
|
zap.String("total_distance", d.totalDistance.Text(16)),
|
|
|
|
|
)
|
2024-03-14 07:18:14 +00:00
|
|
|
|
|
|
|
|
|
d.clockStore.SetTotalDistance(
|
|
|
|
|
d.filter,
|
|
|
|
|
frame.FrameNumber,
|
|
|
|
|
selector.FillBytes(make([]byte, 32)),
|
|
|
|
|
d.totalDistance,
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-18 04:52:19 +00:00
|
|
|
|
go func() {
|
|
|
|
|
d.newFrameCh <- frame
|
|
|
|
|
}()
|
2024-02-13 07:04:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 08:35:13 +00:00
|
|
|
|
func (d *DataTimeReel) GetTotalDistance() *big.Int {
|
|
|
|
|
return new(big.Int).Set(d.totalDistance)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 07:04:56 +00:00
|
|
|
|
var _ TimeReel = (*DataTimeReel)(nil)
|