Unverified Commit 641e9ea5 authored by Friedel Ziegelmayer's avatar Friedel Ziegelmayer Committed by GitHub
Browse files

feat: implement AddPieceFromFile and GeneratePieceCommitmentFromFile

Closes #2 
parent 8226f5b5
......@@ -2,6 +2,8 @@ package go_sectorbuilder
import (
"bytes"
"os"
"runtime"
"sort"
"time"
"unsafe"
......@@ -278,21 +280,36 @@ func DestroySectorBuilder(sectorBuilderPtr unsafe.Pointer) {
C.sector_builder_ffi_destroy_sector_builder((*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr))
}
// AddPiece writes the given piece into an unsealed sector and returns the id
// of that sector.
// AddPiece writes the given piece into an unsealed sector and returns the id of that sector.
func AddPiece(
sectorBuilderPtr unsafe.Pointer,
pieceKey string,
pieceSize uint64,
pieceBytes uint64,
piecePath string,
) (sectorID uint64, retErr error) {
) (uint64, error) {
defer elapsed("AddPiece")()
pieceFile, err := os.Open(piecePath)
if err != nil {
return 0, err
}
return AddPieceFromFile(sectorBuilderPtr, pieceKey, pieceBytes, pieceFile)
}
// AddPieceFromFile writes the given piece into an unsealed sector and returns the id of that sector.
func AddPieceFromFile(
sectorBuilderPtr unsafe.Pointer,
pieceKey string,
pieceBytes uint64,
pieceFile *os.File,
) (sectorID uint64, retErr error) {
defer elapsed("AddPieceFromFile")()
cPieceKey := C.CString(pieceKey)
defer C.free(unsafe.Pointer(cPieceKey))
cPiecePath := C.CString(piecePath)
defer C.free(unsafe.Pointer(cPiecePath))
pieceFd := pieceFile.Fd()
// TODO: The UTC time, in seconds, at which the sector builder can safely
// delete the piece. This allows for co-location of pieces with similar time
......@@ -307,12 +324,15 @@ func AddPiece(
resPtr := C.sector_builder_ffi_add_piece(
(*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr),
cPieceKey,
C.uint64_t(pieceSize),
cPiecePath,
C.int(pieceFd),
C.uint64_t(pieceBytes),
C.uint64_t(pieceExpiryUtcSeconds),
)
defer C.sector_builder_ffi_destroy_add_piece_response(resPtr)
// Make sure our filedescriptor stays alive, stayin alive
runtime.KeepAlive(pieceFile)
if resPtr.status_code != 0 {
return 0, errors.New(C.GoString(resPtr.error_msg))
}
......@@ -528,14 +548,27 @@ func VerifyPieceInclusionProof(sectorSize uint64, pieceSize uint64, commP [Commi
}
// GeneratePieceCommitment produces a piece commitment for the provided data
// stored at a given piece path.
func GeneratePieceCommitment(piecePath string, pieceSize uint64) (commP [CommitmentBytesLen]byte, err error) {
cPiecePath := C.CString(piecePath)
defer C.free(unsafe.Pointer(cPiecePath))
// stored at a given path.
func GeneratePieceCommitment(piecePath string, pieceSize uint64) ([CommitmentBytesLen]byte, error) {
pieceFile, err := os.Open(piecePath)
if err != nil {
return [CommitmentBytesLen]byte{}, err
}
resPtr := C.sector_builder_ffi_generate_piece_commitment(cPiecePath, C.uint64_t(pieceSize))
return GeneratePieceCommitmentFromFile(pieceFile, pieceSize)
}
// GeneratePieceCommitmentFromFile produces a piece commitment for the provided data
// stored in a given file.
func GeneratePieceCommitmentFromFile(pieceFile *os.File, pieceSize uint64) (commP [CommitmentBytesLen]byte, err error) {
pieceFd := pieceFile.Fd()
resPtr := C.sector_builder_ffi_generate_piece_commitment(C.int(pieceFd), C.uint64_t(pieceSize))
defer C.sector_builder_ffi_destroy_generate_piece_commitment_response(resPtr)
// Make sure our filedescriptor stays alive, stayin alive
runtime.KeepAlive(pieceFile)
if resPtr.status_code != 0 {
return [CommitmentBytesLen]byte{}, errors.New(C.GoString(resPtr.error_msg))
}
......
......@@ -44,17 +44,23 @@ func TestSectorBuilderLifecycle(t *testing.T) {
// create a piece which consumes all available space in a new, staged
// sector
pieceBytes := make([]byte, maxPieceSize)
_, err = io.ReadFull(rand.Reader, pieceBytes)
read, err := io.ReadFull(rand.Reader, pieceBytes)
require.Equal(t, uint64(read), maxPieceSize)
require.NoError(t, err)
piecePath := requireTempFilePath(t, bytes.NewReader(pieceBytes))
pieceFile := requireTempFile(t, bytes.NewReader(pieceBytes), maxPieceSize)
// generate piece commitment
commP, err := sb.GeneratePieceCommitment(piecePath, maxPieceSize)
commP, err := sb.GeneratePieceCommitmentFromFile(pieceFile, maxPieceSize)
require.NoError(t, err)
// seek to the beginning
_, err = pieceFile.Seek(0, 0)
require.NoError(t, err)
// write a piece to a staged sector, reducing remaining space to 0 and
// triggering the seal job
sectorID, err := sb.AddPiece(ptr, "snoqualmie", maxPieceSize, piecePath)
sectorID, err := sb.AddPieceFromFile(ptr, "snoqualmie", maxPieceSize, pieceFile)
require.NoError(t, err)
stagedSectors, err := sb.GetAllStagedSectors(ptr)
......@@ -137,14 +143,22 @@ func pollForSectorSealingStatus(ptr unsafe.Pointer, sectorID uint64, targetState
}
}
func requireTempFilePath(t *testing.T, fileContentsReader io.Reader) string {
func requireTempFile(t *testing.T, fileContentsReader io.Reader, size uint64) *os.File {
file, err := ioutil.TempFile("", "")
require.NoError(t, err)
_, err = io.Copy(file, fileContentsReader)
written, err := io.Copy(file, fileContentsReader)
require.NoError(t, err)
// check that we wrote everything
require.Equal(t, uint64(written), size)
require.NoError(t, file.Sync())
// seek to the beginning
_, err = file.Seek(0, 0)
require.NoError(t, err)
return file.Name()
return file
}
func requireTempDirPath(t *testing.T) string {
......
Subproject commit 04eaf9fdfdd48ab0b37f523edaba251239c86f8b
Subproject commit e50b189b25c04b118e4c588e8bb30fbd9babe0a2
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment