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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!(shwap/store): decouple Q1Q4 and ODS files #3643

Merged
merged 8 commits into from
Aug 14, 2024
27 changes: 17 additions & 10 deletions share/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import (
"github.com/celestiaorg/rsmt2d"
)

// EmptyEDSDataHash returns DataHash of the empty block EDS.
func EmptyEDSDataHash() DataHash {
initEmpty()
return emptyBlockDataHash
}

// EmptyEDSRoots returns AxisRoots of the empty block EDS.
func EmptyEDSRoots() *AxisRoots {
initEmpty()
return emptyBlockRoot
return emptyBlockRoots
}

// EmptyEDS returns the EDS of the empty block data square.
Expand All @@ -30,17 +36,18 @@ func EmptyBlockShares() []Share {
}

var (
emptyMu sync.Mutex
emptyBlockRoot *AxisRoots
emptyBlockEDS *rsmt2d.ExtendedDataSquare
emptyBlockShares []Share
emptyMu sync.Mutex
emptyBlockDataHash DataHash
emptyBlockRoots *AxisRoots
emptyBlockEDS *rsmt2d.ExtendedDataSquare
emptyBlockShares []Share
)

// initEmpty enables lazy initialization for constant empty block data.
func initEmpty() {
emptyMu.Lock()
defer emptyMu.Unlock()
if emptyBlockRoot != nil {
if emptyBlockRoots != nil {
return
}

Expand All @@ -54,16 +61,16 @@ func initEmpty() {
}
emptyBlockEDS = eds

emptyBlockRoot, err = NewAxisRoots(eds)
emptyBlockRoots, err = NewAxisRoots(eds)
if err != nil {
panic(fmt.Errorf("failed to create empty DAH: %w", err))
}
minDAH := da.MinDataAvailabilityHeader()
if !bytes.Equal(minDAH.Hash(), emptyBlockRoot.Hash()) {
if !bytes.Equal(minDAH.Hash(), emptyBlockRoots.Hash()) {
panic(fmt.Sprintf("mismatch in calculated minimum DAH and minimum DAH from celestia-app, "+
"expected %s, got %s", minDAH.String(), emptyBlockRoot.String()))
"expected %s, got %s", minDAH.String(), emptyBlockRoots.String()))
}

// precompute Hash, so it's cached internally to avoid potential races
emptyBlockRoot.Hash()
emptyBlockDataHash = emptyBlockRoots.Hash()
}
3 changes: 3 additions & 0 deletions share/new_eds/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/celestiaorg/celestia-node/share/shwap"
)

// EmptyAccessor is an accessor of an empty EDS block.
var EmptyAccessor = &Rsmt2D{ExtendedDataSquare: share.EmptyEDS()}

// Accessor is an interface for accessing extended data square data.
type Accessor interface {
// Size returns square size of the Accessor.
Expand Down
2 changes: 1 addition & 1 deletion share/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (dh DataHash) String() string {

// IsEmptyEDS check whether DataHash corresponds to the root of an empty block EDS.
func (dh DataHash) IsEmptyEDS() bool {
return bytes.Equal(EmptyEDSRoots().Hash(), dh)
return bytes.Equal(EmptyEDSDataHash(), dh)
}

// NewSHA256Hasher returns a new instance of a SHA-256 hasher.
Expand Down
8 changes: 8 additions & 0 deletions store/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package file

const (
// writeBufferSize defines buffer size for optimized batched writes into the file system.
// TODO(@Wondertan): Consider making it configurable
writeBufferSize = 64 << 10
filePermissions = 0o600
)
14 changes: 9 additions & 5 deletions store/file/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package file

import (
"encoding/binary"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -37,17 +36,14 @@ type fileType uint8

const (
ods fileType = iota
q1q4
q4
)

func readHeader(r io.Reader) (*headerV0, error) {
// read first byte to determine the fileVersion
var version headerVersion
err := binary.Read(r, binary.LittleEndian, &version)
if err != nil {
if errors.Is(err, io.EOF) {
return nil, ErrEmptyFile
}
return nil, fmt.Errorf("readHeader: %w", err)
}

Expand All @@ -70,6 +66,14 @@ func writeHeader(w io.Writer, h *headerV0) error {
return err
}

func (h *headerV0) SquareSize() int {
return int(h.squareSize)
}

func (h *headerV0) ShareSize() int {
return int(h.shareSize)
}

func (h *headerV0) Size() int {
// header size + 1 byte for header fileVersion
return headerVOSize + 1
Expand Down
Loading
Loading