|
| 1 | +package block |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "sync/atomic" |
| 9 | + |
| 10 | + "cosmossdk.io/log" |
| 11 | + ds "github.com/ipfs/go-datastore" |
| 12 | + |
| 13 | + "github.com/rollkit/rollkit/pkg/store" |
| 14 | +) |
| 15 | + |
| 16 | +// pendingBase is a generic struct for tracking items (headers, data, etc.) |
| 17 | +// that need to be published to the DA layer in order. It handles persistence |
| 18 | +// of the last submitted height and provides methods for retrieving pending items. |
| 19 | +type pendingBase[T any] struct { |
| 20 | + logger log.Logger |
| 21 | + store store.Store |
| 22 | + metaKey string |
| 23 | + fetch func(ctx context.Context, store store.Store, height uint64) (T, error) |
| 24 | + lastHeight atomic.Uint64 |
| 25 | +} |
| 26 | + |
| 27 | +// newPendingBase constructs a new pendingBase for a given type. |
| 28 | +func newPendingBase[T any](store store.Store, logger log.Logger, metaKey string, fetch func(ctx context.Context, store store.Store, height uint64) (T, error)) (*pendingBase[T], error) { |
| 29 | + pb := &pendingBase[T]{ |
| 30 | + store: store, |
| 31 | + logger: logger, |
| 32 | + metaKey: metaKey, |
| 33 | + fetch: fetch, |
| 34 | + } |
| 35 | + if err := pb.init(); err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + return pb, nil |
| 39 | +} |
| 40 | + |
| 41 | +// getPending returns a sorted slice of pending items of type T. |
| 42 | +func (pb *pendingBase[T]) getPending(ctx context.Context) ([]T, error) { |
| 43 | + lastSubmitted := pb.lastHeight.Load() |
| 44 | + height, err := pb.store.Height(ctx) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + if lastSubmitted == height { |
| 49 | + return nil, nil |
| 50 | + } |
| 51 | + if lastSubmitted > height { |
| 52 | + return nil, fmt.Errorf("height of last submitted item (%d) is greater than height of last item (%d)", lastSubmitted, height) |
| 53 | + } |
| 54 | + pending := make([]T, 0, height-lastSubmitted) |
| 55 | + for i := lastSubmitted + 1; i <= height; i++ { |
| 56 | + item, err := pb.fetch(ctx, pb.store, i) |
| 57 | + if err != nil { |
| 58 | + return pending, err |
| 59 | + } |
| 60 | + pending = append(pending, item) |
| 61 | + } |
| 62 | + return pending, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (pb *pendingBase[T]) isEmpty() bool { |
| 66 | + height, err := pb.store.Height(context.Background()) |
| 67 | + if err != nil { |
| 68 | + pb.logger.Error("failed to get height in isEmpty", "err", err) |
| 69 | + return false |
| 70 | + } |
| 71 | + return height == pb.lastHeight.Load() |
| 72 | +} |
| 73 | + |
| 74 | +func (pb *pendingBase[T]) numPending() uint64 { |
| 75 | + height, err := pb.store.Height(context.Background()) |
| 76 | + if err != nil { |
| 77 | + pb.logger.Error("failed to get height in numPending", "err", err) |
| 78 | + return 0 |
| 79 | + } |
| 80 | + return height - pb.lastHeight.Load() |
| 81 | +} |
| 82 | + |
| 83 | +func (pb *pendingBase[T]) setLastSubmittedHeight(ctx context.Context, newLastSubmittedHeight uint64) { |
| 84 | + lsh := pb.lastHeight.Load() |
| 85 | + if newLastSubmittedHeight > lsh && pb.lastHeight.CompareAndSwap(lsh, newLastSubmittedHeight) { |
| 86 | + bz := make([]byte, 8) |
| 87 | + binary.LittleEndian.PutUint64(bz, newLastSubmittedHeight) |
| 88 | + err := pb.store.SetMetadata(ctx, pb.metaKey, bz) |
| 89 | + if err != nil { |
| 90 | + pb.logger.Error("failed to store height of latest item submitted to DA", "err", err) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func (pb *pendingBase[T]) init() error { |
| 96 | + raw, err := pb.store.GetMetadata(context.Background(), pb.metaKey) |
| 97 | + if errors.Is(err, ds.ErrNotFound) { |
| 98 | + return nil |
| 99 | + } |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + if len(raw) != 8 { |
| 104 | + return fmt.Errorf("invalid length of last submitted height: %d, expected 8", len(raw)) |
| 105 | + } |
| 106 | + lsh := binary.LittleEndian.Uint64(raw) |
| 107 | + if lsh == 0 { |
| 108 | + return nil |
| 109 | + } |
| 110 | + pb.lastHeight.CompareAndSwap(0, lsh) |
| 111 | + return nil |
| 112 | +} |
0 commit comments