bindings.go 21.9 KB
Newer Older
1
package go_sectorbuilder
2
3

import (
4
	"bytes"
5
	"encoding/json"
6
7
	"os"
	"runtime"
8
	"sort"
9
10
11
	"time"
	"unsafe"

12
	"github.com/filecoin-project/go-sectorbuilder/sealed_sector_health"
13
	"github.com/filecoin-project/go-sectorbuilder/sealing_state"
14

15
16
17
18
	logging "github.com/ipfs/go-log"
	"github.com/pkg/errors"
)

19
20
21
// #cgo LDFLAGS: ${SRCDIR}/libsector_builder_ffi.a
// #cgo pkg-config: ${SRCDIR}/sector_builder_ffi.pc
// #include "./sector_builder_ffi.h"
22
23
24
25
26
27
28
29
30
31
32
import "C"

var log = logging.Logger("libsectorbuilder") // nolint: deadcode

func elapsed(what string) func() {
	start := time.Now()
	return func() {
		log.Debugf("%s took %v\n", what, time.Since(start))
	}
}

33
34
35
36
37
38
// SortedSectorInfo is a slice of SectorInfo sorted (lexicographically,
// ascending) by replica commitment (CommR).
type SortedSectorInfo struct {
	f []SectorInfo
}

39
40
41
42
43
44
// SealTicket commits a sector to a subchain.
type SealTicket struct {
	BlockHeight uint64
	TicketBytes [32]byte
}

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// NewSortedSectorInfo returns a SortedSectorInfo
func NewSortedSectorInfo(sectorInfo ...SectorInfo) SortedSectorInfo {
	fn := func(i, j int) bool {
		return bytes.Compare(sectorInfo[i].CommR[:], sectorInfo[j].CommR[:]) == -1
	}

	sort.Slice(sectorInfo[:], fn)

	return SortedSectorInfo{
		f: sectorInfo,
	}
}

// Values returns the sorted SectorInfo as a slice
func (s *SortedSectorInfo) Values() []SectorInfo {
	return s.f
}

63
64
65
66
67
68
69
70
71
72
73
74
75
76
// MarshalJSON JSON-encodes and serializes the SortedSectorInfo.
func (s SortedSectorInfo) MarshalJSON() ([]byte, error) {
	return json.Marshal(s.f)
}

// UnmarshalJSON parses the JSON-encoded byte slice and stores the result in the
// value pointed to by s.f. Note that this method allows for construction of a
// SortedSectorInfo which violates its invariant (that its SectorInfo are sorted
// in some defined way). Callers should take care to never provide a byte slice
// which would violate this invariant.
func (s *SortedSectorInfo) UnmarshalJSON(b []byte) error {
	return json.Unmarshal(b, &s.f)
}

77
78
type SectorInfo struct {
	SectorID uint64
79
	CommR    [CommitmentBytesLen]byte
80
81
}

82
83
84
// CommitmentBytesLen is the number of bytes in a CommR, CommD, CommP, and CommRStar.
const CommitmentBytesLen = 32

85
86
87
88
89
90
91
// StagedSectorMetadata is a sector into which we write user piece-data before
// sealing. Note: SectorID is unique across all staged and sealed sectors for a
// storage miner actor.
type StagedSectorMetadata struct {
	SectorID uint64
}

Sidney Keese's avatar
Sidney Keese committed
92
93
94
95
96
97
98
99
// SealedSectorMetadata represents a sector in the builder that has been sealed.
type SealedSectorMetadata struct {
	SectorID  uint64
	CommD     [CommitmentBytesLen]byte
	CommR     [CommitmentBytesLen]byte
	CommRStar [CommitmentBytesLen]byte
	Proof     []byte
	Pieces    []PieceMetadata
100
	Health    sealed_sector_health.Health
101
	Ticket    SealTicket
Sidney Keese's avatar
Sidney Keese committed
102
103
}

104
105
106
// SectorSealingStatus communicates how far along in the sealing process a
// sector has progressed.
type SectorSealingStatus struct {
107
108
109
110
111
112
113
	SectorID     uint64
	State        sealing_state.State
	SealErrorMsg string                   // will be nil unless State == Failed
	CommD        [CommitmentBytesLen]byte // will be empty unless State == Sealed
	CommR        [CommitmentBytesLen]byte // will be empty unless State == Sealed
	Proof        []byte                   // will be empty unless State == Sealed
	Pieces       []PieceMetadata          // will be empty unless State == Sealed
114
	Ticket       SealTicket               // will be empty unless State == Sealed
115
116
117
118
119
120
121
}

// PieceMetadata represents a piece stored by the sector builder.
type PieceMetadata struct {
	Key            string
	Size           uint64
	InclusionProof []byte
122
	CommP          [CommitmentBytesLen]byte
123
124
125
126
127
128
}

// VerifySeal returns true if the sealing operation from which its inputs were
// derived was valid, and false if not.
func VerifySeal(
	sectorSize uint64,
129
130
	commR [CommitmentBytesLen]byte,
	commD [CommitmentBytesLen]byte,
131
132
	proverID [32]byte,
	ticket [32]byte,
laser's avatar
laser committed
133
	sectorID uint64,
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	proof []byte,
) (bool, error) {
	defer elapsed("VerifySeal")()

	commDCBytes := C.CBytes(commD[:])
	defer C.free(commDCBytes)

	commRCBytes := C.CBytes(commR[:])
	defer C.free(commRCBytes)

	proofCBytes := C.CBytes(proof[:])
	defer C.free(proofCBytes)

	proverIDCBytes := C.CBytes(proverID[:])
	defer C.free(proverIDCBytes)

150
151
152
	ticketCBytes := C.CBytes(ticket[:])
	defer C.free(ticketCBytes)

153
	// a mutable pointer to a VerifySealResponse C-struct
154
	resPtr := C.sector_builder_ffi_verify_seal(
155
		C.uint64_t(sectorSize),
156
157
		(*[CommitmentBytesLen]C.uint8_t)(commRCBytes),
		(*[CommitmentBytesLen]C.uint8_t)(commDCBytes),
158
		(*[32]C.uint8_t)(proverIDCBytes),
laser's avatar
laser committed
159
		C.uint64_t(sectorID),
160
		(*[32]C.uint8_t)(ticketCBytes),
161
162
		(*C.uint8_t)(proofCBytes),
		C.size_t(len(proof)),
163
	)
164
165
166
167
168
169
170
171
172
173
174
175
176
	defer C.sector_builder_ffi_destroy_verify_seal_response(resPtr)

	if resPtr.status_code != 0 {
		return false, errors.New(C.GoString(resPtr.error_msg))
	}

	return bool(resPtr.is_valid), nil
}

// VerifyPoSt returns true if the PoSt-generation operation from which its
// inputs were derived was valid, and false if not.
func VerifyPoSt(
	sectorSize uint64,
177
	sectorInfo SortedSectorInfo,
laser's avatar
laser committed
178
179
	challengeSeed [32]byte,
	proof []byte,
180
181
182
183
	faults []uint64,
) (bool, error) {
	defer elapsed("VerifyPoSt")()

184
185
186
187
188
189
190
191
	// CommRs and sector ids must be provided to C.verify_post in the same order
	// that they were provided to the C.generate_post
	sortedCommRs := make([][CommitmentBytesLen]byte, len(sectorInfo.Values()))
	sortedSectorIds := make([]uint64, len(sectorInfo.Values()))
	for idx, v := range sectorInfo.Values() {
		sortedCommRs[idx] = v.CommR
		sortedSectorIds[idx] = v.SectorID
	}
192
193

	// flattening the byte slice makes it easier to copy into the C heap
194
195
	flattened := make([]byte, CommitmentBytesLen*len(sortedCommRs))
	for idx, commR := range sortedCommRs {
196
		copy(flattened[(CommitmentBytesLen*idx):(CommitmentBytesLen*(1+idx))], commR[:])
197
198
199
200
201
202
203
204
205
	}

	// copy bytes from Go to C heap
	flattenedCommRsCBytes := C.CBytes(flattened)
	defer C.free(flattenedCommRsCBytes)

	challengeSeedCBytes := C.CBytes(challengeSeed[:])
	defer C.free(challengeSeedCBytes)

laser's avatar
laser committed
206
207
	proofCBytes := C.CBytes(proof)
	defer C.free(proofCBytes)
208
209

	// allocate fixed-length array of uint64s in C heap
laser's avatar
laser committed
210
211
212
	sectorIdsPtr, sectorIdsSize := cUint64s(sortedSectorIds)
	defer C.free(unsafe.Pointer(sectorIdsPtr))

213
214
215
216
	faultsPtr, faultsSize := cUint64s(faults)
	defer C.free(unsafe.Pointer(faultsPtr))

	// a mutable pointer to a VerifyPoStResponse C-struct
217
	resPtr := C.sector_builder_ffi_verify_post(
218
		C.uint64_t(sectorSize),
219
		(*[CommitmentBytesLen]C.uint8_t)(challengeSeedCBytes),
laser's avatar
laser committed
220
221
		sectorIdsPtr,
		sectorIdsSize,
222
223
		faultsPtr,
		faultsSize,
laser's avatar
laser committed
224
225
226
227
		(*C.uint8_t)(flattenedCommRsCBytes),
		C.size_t(len(flattened)),
		(*C.uint8_t)(proofCBytes),
		C.size_t(len(proof)),
228
	)
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
	defer C.sector_builder_ffi_destroy_verify_post_response(resPtr)

	if resPtr.status_code != 0 {
		return false, errors.New(C.GoString(resPtr.error_msg))
	}

	return bool(resPtr.is_valid), nil
}

// GetMaxUserBytesPerStagedSector returns the number of user bytes that will fit
// into a staged sector. Due to bit-padding, the number of user bytes that will
// fit into the staged sector will be less than number of bytes in sectorSize.
func GetMaxUserBytesPerStagedSector(sectorSize uint64) uint64 {
	defer elapsed("GetMaxUserBytesPerStagedSector")()

	return uint64(C.sector_builder_ffi_get_max_user_bytes_per_staged_sector(C.uint64_t(sectorSize)))
}

// InitSectorBuilder allocates and returns a pointer to a sector builder.
func InitSectorBuilder(
	sectorSize uint64,
	poRepProofPartitions uint8,
	lastUsedSectorID uint64,
	metadataDir string,
253
	proverID [32]byte,
254
255
	sealedSectorDir string,
	stagedSectorDir string,
256
	sectorCacheRootDir string,
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	maxNumOpenStagedSectors uint8,
) (unsafe.Pointer, error) {
	defer elapsed("InitSectorBuilder")()

	cMetadataDir := C.CString(metadataDir)
	defer C.free(unsafe.Pointer(cMetadataDir))

	proverIDCBytes := C.CBytes(proverID[:])
	defer C.free(proverIDCBytes)

	cStagedSectorDir := C.CString(stagedSectorDir)
	defer C.free(unsafe.Pointer(cStagedSectorDir))

	cSealedSectorDir := C.CString(sealedSectorDir)
	defer C.free(unsafe.Pointer(cSealedSectorDir))

273
274
275
	cSectorCacheRootDir := C.CString(sectorCacheRootDir)
	defer C.free(unsafe.Pointer(cSectorCacheRootDir))

laser's avatar
laser committed
276
	class, err := cSectorClass(sectorSize, poRepProofPartitions)
277
278
279
280
	if err != nil {
		return nil, errors.Wrap(err, "failed to get sector class")
	}

281
	resPtr := C.sector_builder_ffi_init_sector_builder(
282
283
284
		class,
		C.uint64_t(lastUsedSectorID),
		cMetadataDir,
285
		(*[32]C.uint8_t)(proverIDCBytes),
286
287
		cSealedSectorDir,
		cStagedSectorDir,
288
		cSectorCacheRootDir,
289
		C.uint8_t(maxNumOpenStagedSectors),
290
	)
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	defer C.sector_builder_ffi_destroy_init_sector_builder_response(resPtr)

	if resPtr.status_code != 0 {
		return nil, errors.New(C.GoString(resPtr.error_msg))
	}

	return unsafe.Pointer(resPtr.sector_builder), nil
}

// DestroySectorBuilder deallocates the sector builder associated with the
// provided pointer. This function will panic if the provided pointer is null
// or if the sector builder has been previously deallocated.
func DestroySectorBuilder(sectorBuilderPtr unsafe.Pointer) {
	defer elapsed("DestroySectorBuilder")()

	C.sector_builder_ffi_destroy_sector_builder((*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr))
}

309
// AddPiece writes the given piece into an unsealed sector and returns the id of that sector.
310
311
312
func AddPiece(
	sectorBuilderPtr unsafe.Pointer,
	pieceKey string,
313
	pieceBytes uint64,
314
	piecePath string,
315
) (uint64, error) {
316
317
	defer elapsed("AddPiece")()

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
	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")()

335
336
337
	cPieceKey := C.CString(pieceKey)
	defer C.free(unsafe.Pointer(cPieceKey))

338
	pieceFd := pieceFile.Fd()
339

340
341
342
343
344
345
346
347
348
349
	// 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
	// constraints, and allows the sector builder to remove sectors containing
	// pieces whose deals have expired.
	//
	// This value is currently ignored by the sector builder.
	//
	// https://github.com/filecoin-project/rust-fil-sector-builder/issues/32
	pieceExpiryUtcSeconds := 0

350
	resPtr := C.sector_builder_ffi_add_piece(
351
352
		(*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr),
		cPieceKey,
353
354
		C.int(pieceFd),
		C.uint64_t(pieceBytes),
355
		C.uint64_t(pieceExpiryUtcSeconds),
356
	)
357
358
	defer C.sector_builder_ffi_destroy_add_piece_response(resPtr)

359
360
361
	// Make sure our filedescriptor stays alive, stayin alive
	runtime.KeepAlive(pieceFile)

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
	if resPtr.status_code != 0 {
		return 0, errors.New(C.GoString(resPtr.error_msg))
	}

	return uint64(resPtr.sector_id), nil
}

// ReadPieceFromSealedSector produces a byte buffer containing the piece
// associated with the provided key. If the key is not associated with any piece
// yet sealed into a sector, an error will be returned.
func ReadPieceFromSealedSector(sectorBuilderPtr unsafe.Pointer, pieceKey string) ([]byte, error) {
	defer elapsed("ReadPieceFromSealedSector")()

	cPieceKey := C.CString(pieceKey)
	defer C.free(unsafe.Pointer(cPieceKey))

378
379
380
381
	resPtr := C.sector_builder_ffi_read_piece_from_sealed_sector(
		(*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr),
		cPieceKey,
	)
382
383
384
385
386
387
388
389
390
	defer C.sector_builder_ffi_destroy_read_piece_from_sealed_sector_response(resPtr)

	if resPtr.status_code != 0 {
		return nil, errors.New(C.GoString(resPtr.error_msg))
	}

	return goBytes(resPtr.data_ptr, resPtr.data_len), nil
}

391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// SealSector seals the sector with the provided id, blocking until sealing
// completes. If no staged sector exists in the ReadyToSeal state with such an
// id, an error will be returned.
func SealSector(sectorBuilderPtr unsafe.Pointer, sectorID uint64, ticket SealTicket) (SealedSectorMetadata, error) {
	defer elapsed("SealSector")()

	cTicketBytes := C.CBytes(ticket.TicketBytes[:])
	defer C.free(cTicketBytes)

	cSealTicket := C.sector_builder_ffi_FFISealTicket{
		block_height: C.uint64_t(ticket.BlockHeight),
		ticket_bytes: *(*[32]C.uint8_t)(cTicketBytes),
	}

	resPtr := C.sector_builder_ffi_seal_sector((*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr), C.uint64_t(sectorID), cSealTicket)
	defer C.sector_builder_ffi_destroy_seal_sector_response(resPtr)

	if resPtr.status_code != 0 {
		return SealedSectorMetadata{}, errors.New(C.GoString(resPtr.error_msg))
	}

	meta, err := goSealedSectorMetadata((*C.sector_builder_ffi_FFISealedSectorMetadata)(unsafe.Pointer(&resPtr.meta)), 1)
	if err != nil {
		return SealedSectorMetadata{}, err
	}

	return meta[0], nil
}

// ResumeSealSector resumes sealing for a sector in the Paused state. If no
// staged sector exists in such a state, an error will be returned.
func ResumeSealSector(sectorBuilderPtr unsafe.Pointer, sectorID uint64) (SealedSectorMetadata, error) {
	defer elapsed("ResumeSealSector")()

	resPtr := C.sector_builder_ffi_resume_seal_sector((*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr), C.uint64_t(sectorID))
	defer C.sector_builder_ffi_destroy_resume_seal_sector_response(resPtr)

	if resPtr.status_code != 0 {
		return SealedSectorMetadata{}, errors.New(C.GoString(resPtr.error_msg))
	}

	meta, err := goSealedSectorMetadata((*C.sector_builder_ffi_FFISealedSectorMetadata)(unsafe.Pointer(&resPtr.meta)), 1)
	if err != nil {
		return SealedSectorMetadata{}, err
	}

	return meta[0], nil
}

// SealAllStagedSectors seals all staged sectors and returns sealed sector
// metadata for all successfully sealed sectors.
func SealAllStagedSectors(sectorBuilderPtr unsafe.Pointer, ticket SealTicket) ([]SealedSectorMetadata, error) {
443
444
	defer elapsed("SealAllStagedSectors")()

445
446
447
448
449
450
451
452
453
	cTicketBytes := C.CBytes(ticket.TicketBytes[:])
	defer C.free(cTicketBytes)

	cSealTicket := C.sector_builder_ffi_FFISealTicket{
		block_height: C.uint64_t(ticket.BlockHeight),
		ticket_bytes: *(*[32]C.uint8_t)(cTicketBytes),
	}

	resPtr := C.sector_builder_ffi_seal_all_staged_sectors((*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr), cSealTicket)
454
455
456
	defer C.sector_builder_ffi_destroy_seal_all_staged_sectors_response(resPtr)

	if resPtr.status_code != 0 {
457
		return nil, errors.New(C.GoString(resPtr.error_msg))
458
459
	}

460
461
462
463
464
465
	meta, err := goSealedSectorMetadata(resPtr.meta_ptr, resPtr.meta_len)
	if err != nil {
		return nil, err
	}

	return meta, nil
466
467
468
469
470
471
}

// GetAllStagedSectors returns a slice of all staged sector metadata for the sector builder.
func GetAllStagedSectors(sectorBuilderPtr unsafe.Pointer) ([]StagedSectorMetadata, error) {
	defer elapsed("GetAllStagedSectors")()

472
	resPtr := C.sector_builder_ffi_get_staged_sectors((*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr))
473
474
475
476
477
478
	defer C.sector_builder_ffi_destroy_get_staged_sectors_response(resPtr)

	if resPtr.status_code != 0 {
		return nil, errors.New(C.GoString(resPtr.error_msg))
	}

479
	meta, err := goStagedSectorMetadata(resPtr.sectors_ptr, resPtr.sectors_len)
480
481
482
483
484
485
486
	if err != nil {
		return nil, err
	}

	return meta, nil
}

487
488
489
// GetAllSealedSectors returns a slice of all sealed sector metadata, excluding
// sector health.
func GetAllSealedSectors(sectorBuilderPtr unsafe.Pointer) ([]SealedSectorMetadata, error) {
Sidney Keese's avatar
Sidney Keese committed
490
491
	defer elapsed("GetAllSealedSectors")()

492
493
	return getAllSealedSectors(sectorBuilderPtr, false)
}
Sidney Keese's avatar
Sidney Keese committed
494

495
496
497
498
499
// GetAllSealedSectorsWithHealth returns a slice of all sealed sector metadata
// for the sector builder, including sector health info (which can be expensive
// to compute).
func GetAllSealedSectorsWithHealth(sectorBuilderPtr unsafe.Pointer) ([]SealedSectorMetadata, error) {
	defer elapsed("GetAllSealedSectorsWithHealth")()
Sidney Keese's avatar
Sidney Keese committed
500

501
	return getAllSealedSectors(sectorBuilderPtr, true)
Sidney Keese's avatar
Sidney Keese committed
502
503
}

504
505
506
// GetSectorSealingStatusByID produces sector sealing status (staged, sealing in
// progress, sealed, failed) for the provided sector id. If no sector
// corresponding to the provided id exists, this function returns an error.
507
508
509
func GetSectorSealingStatusByID(sectorBuilderPtr unsafe.Pointer, sectorID uint64) (SectorSealingStatus, error) {
	defer elapsed("GetSectorSealingStatusByID")()

510
511
512
513
	resPtr := C.sector_builder_ffi_get_seal_status(
		(*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr),
		C.uint64_t(sectorID),
	)
514
515
516
517
518
519
520
	defer C.sector_builder_ffi_destroy_get_seal_status_response(resPtr)

	if resPtr.status_code != 0 {
		return SectorSealingStatus{}, errors.New(C.GoString(resPtr.error_msg))
	}

	if resPtr.seal_status_code == C.Failed {
521
		return SectorSealingStatus{SectorID: sectorID, State: sealing_state.Failed, SealErrorMsg: C.GoString(resPtr.seal_error_msg)}, nil
522
523
524
525
	} else if resPtr.seal_status_code == C.ReadyForSealing {
		return SectorSealingStatus{SectorID: sectorID, State: sealing_state.ReadyForSealing}, nil
	} else if resPtr.seal_status_code == C.Paused {
		return SectorSealingStatus{SectorID: sectorID, State: sealing_state.Paused}, nil
526
	} else if resPtr.seal_status_code == C.Pending {
527
		return SectorSealingStatus{SectorID: sectorID, State: sealing_state.Pending}, nil
528
	} else if resPtr.seal_status_code == C.Sealing {
529
		return SectorSealingStatus{SectorID: sectorID, State: sealing_state.Sealing}, nil
530
	} else if resPtr.seal_status_code == C.Sealed {
531
532
		commRSlice := goBytes(&resPtr.comm_r[0], CommitmentBytesLen)
		var commR [CommitmentBytesLen]byte
533
534
		copy(commR[:], commRSlice)

535
536
		commDSlice := goBytes(&resPtr.comm_d[0], CommitmentBytesLen)
		var commD [CommitmentBytesLen]byte
537
538
539
540
541
542
543
544
545
546
		copy(commD[:], commDSlice)

		proof := goBytes(resPtr.proof_ptr, resPtr.proof_len)

		ps, err := goPieceMetadata(resPtr.pieces_ptr, resPtr.pieces_len)
		if err != nil {
			return SectorSealingStatus{}, errors.Wrap(err, "failed to marshal from string to cid")
		}

		return SectorSealingStatus{
547
548
549
550
551
552
553
			SectorID: sectorID,
			State:    sealing_state.Sealed,
			CommD:    commD,
			CommR:    commR,
			Proof:    proof,
			Pieces:   ps,
			Ticket:   goSealTicket(resPtr.seal_ticket),
554
555
556
557
558
559
560
561
562
563
		}, nil
	} else {
		// unknown
		return SectorSealingStatus{}, errors.New("unexpected seal status")
	}
}

// GeneratePoSt produces a proof-of-spacetime for the provided replica commitments.
func GeneratePoSt(
	sectorBuilderPtr unsafe.Pointer,
564
	sectorInfo SortedSectorInfo,
565
	challengeSeed [CommitmentBytesLen]byte,
laser's avatar
laser committed
566
567
	faults []uint64,
) ([]byte, error) {
568
569
	defer elapsed("GeneratePoSt")()

570
571
572
573
574
575
576
	// CommRs and sector ids must be provided to C.verify_post in the same order
	// that they were provided to the C.generate_post
	sortedCommRs := make([][CommitmentBytesLen]byte, len(sectorInfo.Values()))
	for idx, v := range sectorInfo.Values() {
		sortedCommRs[idx] = v.CommR
	}

577
	// flattening the byte slice makes it easier to copy into the C heap
578
579
	flattened := make([]byte, CommitmentBytesLen*len(sortedCommRs))
	for idx, commR := range sortedCommRs {
580
		copy(flattened[(CommitmentBytesLen*idx):(CommitmentBytesLen*(1+idx))], commR[:])
581
582
583
584
585
586
587
588
	}

	// copy the Go byte slice into C memory
	cflattened := C.CBytes(flattened)
	defer C.free(cflattened)

	challengeSeedPtr := unsafe.Pointer(&(challengeSeed)[0])

laser's avatar
laser committed
589
590
591
	faultsPtr, faultsSize := cUint64s(faults)
	defer C.free(unsafe.Pointer(faultsPtr))

592
	// a mutable pointer to a GeneratePoStResponse C-struct
593
594
595
596
597
	resPtr := C.sector_builder_ffi_generate_post(
		(*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr),
		(*C.uint8_t)(cflattened),
		C.size_t(len(flattened)),
		(*[CommitmentBytesLen]C.uint8_t)(challengeSeedPtr),
laser's avatar
laser committed
598
599
		faultsPtr,
		faultsSize,
600
	)
601
602
603
	defer C.sector_builder_ffi_destroy_generate_post_response(resPtr)

	if resPtr.status_code != 0 {
laser's avatar
laser committed
604
		return nil, errors.New(C.GoString(resPtr.error_msg))
605
606
	}

laser's avatar
laser committed
607
	return goBytes(resPtr.proof_ptr, resPtr.proof_len), nil
608
}
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639

// VerifyPieceInclusionProof returns true if the piece inclusion proof is valid
// with the given arguments.
func VerifyPieceInclusionProof(sectorSize uint64, pieceSize uint64, commP [CommitmentBytesLen]byte, commD [CommitmentBytesLen]byte, proof []byte) (bool, error) {
	commDCBytes := C.CBytes(commD[:])
	defer C.free(commDCBytes)

	commPCBytes := C.CBytes(commP[:])
	defer C.free(commPCBytes)

	pieceInclusionProofCBytes := C.CBytes(proof)
	defer C.free(pieceInclusionProofCBytes)

	resPtr := C.sector_builder_ffi_verify_piece_inclusion_proof(
		(*[CommitmentBytesLen]C.uint8_t)(commDCBytes),
		(*[CommitmentBytesLen]C.uint8_t)(commPCBytes),
		(*C.uint8_t)(pieceInclusionProofCBytes),
		C.size_t(len(proof)),
		C.uint64_t(pieceSize),
		C.uint64_t(sectorSize),
	)
	defer C.sector_builder_ffi_destroy_verify_piece_inclusion_proof_response(resPtr)

	if resPtr.status_code != 0 {
		return false, errors.New(C.GoString(resPtr.error_msg))
	}

	return bool(resPtr.is_valid), nil
}

// GeneratePieceCommitment produces a piece commitment for the provided data
640
641
642
643
644
645
// 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
	}
646

647
648
649
650
651
652
653
654
655
	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))
656
657
	defer C.sector_builder_ffi_destroy_generate_piece_commitment_response(resPtr)

658
659
660
	// Make sure our filedescriptor stays alive, stayin alive
	runtime.KeepAlive(pieceFile)

661
662
663
664
665
666
667
668
669
670
	if resPtr.status_code != 0 {
		return [CommitmentBytesLen]byte{}, errors.New(C.GoString(resPtr.error_msg))
	}

	commPSlice := goBytes(&resPtr.comm_p[0], CommitmentBytesLen)
	var commitment [CommitmentBytesLen]byte
	copy(commitment[:], commPSlice)

	return commitment, nil
}
671
672
673
674
675
676
677
678
679

func getAllSealedSectors(sectorBuilderPtr unsafe.Pointer, performHealthchecks bool) ([]SealedSectorMetadata, error) {
	resPtr := C.sector_builder_ffi_get_sealed_sectors((*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr), C.bool(performHealthchecks))
	defer C.sector_builder_ffi_destroy_get_sealed_sectors_response(resPtr)

	if resPtr.status_code != 0 {
		return nil, errors.New(C.GoString(resPtr.error_msg))
	}

680
	meta, err := goSealedSectorMetadata(resPtr.meta_ptr, resPtr.meta_len)
681
682
683
684
685
686
	if err != nil {
		return nil, err
	}

	return meta, nil
}