transforms.go 11.9 KB
Newer Older
1
package go_sectorbuilder
2
3
4

import (
	"unsafe"
Sidney Keese's avatar
Sidney Keese committed
5

6
7
	"github.com/filecoin-project/go-sectorbuilder/sealed_sector_health"

Sidney Keese's avatar
Sidney Keese committed
8
	"github.com/pkg/errors"
9
10
)

11
12
13
// #cgo LDFLAGS: ${SRCDIR}/libsector_builder_ffi.a
// #cgo pkg-config: ${SRCDIR}/sector_builder_ffi.pc
// #include "./sector_builder_ffi.h"
14
15
16
17
18
19
20
import "C"

// SingleProofPartitionProofLen denotes the number of bytes in a proof generated
// with a single partition. The number of bytes in a proof increases linearly
// with the number of partitions used when creating that proof.
const SingleProofPartitionProofLen = 192

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
func cPublicPieceInfo(src []PublicPieceInfo) (*C.sector_builder_ffi_FFIPublicPieceInfo, C.size_t) {
	srcCSizeT := C.size_t(len(src))

	// allocate array in C heap
	cPublicPieceInfos := C.malloc(srcCSizeT * C.sizeof_sector_builder_ffi_FFIPublicPieceInfo)

	// create a Go slice backed by the C-array
	xs := (*[1 << 30]C.sector_builder_ffi_FFIPublicPieceInfo)(cPublicPieceInfos)
	for i, v := range src {
		xs[i] = C.sector_builder_ffi_FFIPublicPieceInfo{
			num_bytes: C.uint64_t(v.Size),
			comm_p:    *(*[32]C.uint8_t)(unsafe.Pointer(&v.CommP)),
		}
	}

	return (*C.sector_builder_ffi_FFIPublicPieceInfo)(cPublicPieceInfos), srcCSizeT
}

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
func cPieceMetadata(src []PieceMetadata) (*C.sector_builder_ffi_FFIPieceMetadata, C.size_t) {
	srcCSizeT := C.size_t(len(src))

	// allocate array in C heap
	cPieceMetadata := C.malloc(srcCSizeT * C.sizeof_sector_builder_ffi_FFIPieceMetadata)

	// create a Go slice backed by the C-array
	xs := (*[1 << 30]C.sector_builder_ffi_FFIPieceMetadata)(cPieceMetadata)
	for i, v := range src {
		xs[i] = C.sector_builder_ffi_FFIPieceMetadata{
			piece_key: C.CString(v.Key),
			num_bytes: C.uint64_t(v.Size),
			comm_p:    *(*[32]C.uint8_t)(unsafe.Pointer(&v.CommP)),
		}
	}

	return (*C.sector_builder_ffi_FFIPieceMetadata)(cPieceMetadata), srcCSizeT
}

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
func cUint64s(src []uint64) (*C.uint64_t, C.size_t) {
	srcCSizeT := C.size_t(len(src))

	// allocate array in C heap
	cUint64s := C.malloc(srcCSizeT * C.sizeof_uint64_t)

	// create a Go slice backed by the C-array
	pp := (*[1 << 30]C.uint64_t)(cUint64s)
	for i, v := range src {
		pp[i] = C.uint64_t(v)
	}

	return (*C.uint64_t)(cUint64s), srcCSizeT
}

73
func cSectorClass(sectorSize uint64, poRepProofPartitions uint8) C.sector_builder_ffi_FFISectorClass {
74
75
76
	return C.sector_builder_ffi_FFISectorClass{
		sector_size:            C.uint64_t(sectorSize),
		porep_proof_partitions: C.uint8_t(poRepProofPartitions),
77
78
79
80
81
82
83
84
85
86
	}
}

func cSealPreCommitOutput(src RawSealPreCommitOutput) C.sector_builder_ffi_FFISealPreCommitOutput {
	return C.sector_builder_ffi_FFISealPreCommitOutput{
		comm_d:            *(*[32]C.uint8_t)(unsafe.Pointer(&src.CommD)),
		comm_r:            *(*[32]C.uint8_t)(unsafe.Pointer(&src.CommR)),
		p_aux_comm_c:      *(*[32]C.uint8_t)(unsafe.Pointer(&src.CommC)),
		p_aux_comm_r_last: *(*[32]C.uint8_t)(unsafe.Pointer(&src.CommRLast)),
	}
87
88
}

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
func cCandidates(src []Candidate) (*C.sector_builder_ffi_FFICandidate, C.size_t) {
	srcCSizeT := C.size_t(len(src))

	// allocate array in C heap
	cCandidates := C.malloc(srcCSizeT * C.sizeof_sector_builder_ffi_FFICandidate)

	// create a Go slice backed by the C-array
	pp := (*[1 << 30]C.sector_builder_ffi_FFICandidate)(cCandidates)
	for i, v := range src {
		pp[i] = C.sector_builder_ffi_FFICandidate{
			sector_id:              C.uint64_t(v.SectorID),
			partial_ticket:         *(*[32]C.uint8_t)(unsafe.Pointer(&v.PartialTicket)),
			ticket:                 *(*[32]C.uint8_t)(unsafe.Pointer(&v.Ticket)),
			sector_challenge_index: C.uint64_t(v.SectorChallengeIndex),
		}
	}

	return (*C.sector_builder_ffi_FFICandidate)(cCandidates), srcCSizeT
}

109
func cPrivateReplicaInfos(src []PrivateSectorInfo) (*C.sector_builder_ffi_FFIPrivateReplicaInfo, C.size_t) {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	srcCSizeT := C.size_t(len(src))

	cPrivateReplicas := C.malloc(srcCSizeT * C.sizeof_sector_builder_ffi_FFIPrivateReplicaInfo)

	pp := (*[1 << 30]C.sector_builder_ffi_FFIPrivateReplicaInfo)(cPrivateReplicas)
	for i, v := range src {
		pp[i] = C.sector_builder_ffi_FFIPrivateReplicaInfo{
			cache_dir_path: C.CString(v.CacheDirPath),
			comm_r:         *(*[32]C.uint8_t)(unsafe.Pointer(&v.CommR)),
			replica_path:   C.CString(v.SealedSectorPath),
			sector_id:      C.uint64_t(v.SectorID),
		}
	}

	return (*C.sector_builder_ffi_FFIPrivateReplicaInfo)(cPrivateReplicas), srcCSizeT
}

127
128
129
130
func goBytes(src *C.uint8_t, size C.size_t) []byte {
	return C.GoBytes(unsafe.Pointer(src), C.int(size))
}

131
132
func goSealTicket(src C.sector_builder_ffi_FFISealTicket) SealTicket {
	return SealTicket{
133
		TicketBytes: goCommitment(&src.ticket_bytes[0]),
134
135
136
137
		BlockHeight: uint64(src.block_height),
	}
}

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
func goCandidates(src *C.sector_builder_ffi_FFICandidate, size C.size_t) ([]Candidate, error) {
	candidates := make([]Candidate, size)
	if src == nil || size == 0 {
		return candidates, nil
	}

	ptrs := (*[1 << 30]C.sector_builder_ffi_FFICandidate)(unsafe.Pointer(src))[:size:size]
	for i := 0; i < int(size); i++ {
		candidates[i] = goCandidate(ptrs[i])
	}

	return candidates, nil
}

func goCandidate(src C.sector_builder_ffi_FFICandidate) Candidate {
	return Candidate{
		SectorID:             uint64(src.sector_id),
		PartialTicket:        goCommitment(&src.partial_ticket[0]),
		Ticket:               goCommitment(&src.ticket[0]),
		SectorChallengeIndex: uint64(src.sector_challenge_index),
	}
}

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
func goRawSealPreCommitOutput(src C.sector_builder_ffi_FFISealPreCommitOutput) RawSealPreCommitOutput {
	return RawSealPreCommitOutput{
		CommD:     goCommitment(&src.comm_d[0]),
		CommR:     goCommitment(&src.comm_r[0]),
		CommRLast: goCommitment(&src.p_aux_comm_r_last[0]),
		CommC:     goCommitment(&src.p_aux_comm_c[0]),
	}
}

func goCommitment(src *C.uint8_t) [32]byte {
	slice := C.GoBytes(unsafe.Pointer(src), 32)
	var array [CommitmentBytesLen]byte
	copy(array[:], slice)

	return array
}

func goSectorBuilderSealCommitOutput(src *C.sector_builder_ffi_SectorBuilderSealCommitResponse) (SealCommitOutput, error) {
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	commDSlice := goBytes(&src.comm_d[0], CommitmentBytesLen)
	var commD [CommitmentBytesLen]byte
	copy(commD[:], commDSlice)

	commRSlice := goBytes(&src.comm_r[0], CommitmentBytesLen)
	var commR [CommitmentBytesLen]byte
	copy(commR[:], commRSlice)

	proof := goBytes(src.proofs_ptr, src.proofs_len)

	pieces, err := goPieceMetadata(src.pieces_ptr, src.pieces_len)
	if err != nil {
		return SealCommitOutput{}, errors.Wrap(err, "failed to marshal piece metadata")
	}

	return SealCommitOutput{
		SectorID: uint64(src.sector_id),
		CommD:    commD,
		CommR:    commR,
		Proof:    proof,
		Pieces:   pieces,
		Ticket:   goSealTicket(src.seal_ticket),
		Seed:     goSealSeed(src.seal_seed),
	}, nil
}

func goResumeSealCommitOutput(src *C.sector_builder_ffi_ResumeSealCommitResponse) (SealCommitOutput, error) {
	commDSlice := goBytes(&src.comm_d[0], CommitmentBytesLen)
	var commD [CommitmentBytesLen]byte
	copy(commD[:], commDSlice)

	commRSlice := goBytes(&src.comm_r[0], CommitmentBytesLen)
	var commR [CommitmentBytesLen]byte
	copy(commR[:], commRSlice)

	proof := goBytes(src.proofs_ptr, src.proofs_len)

	pieces, err := goPieceMetadata(src.pieces_ptr, src.pieces_len)
	if err != nil {
		return SealCommitOutput{}, errors.Wrap(err, "failed to marshal piece metadata")
	}

	return SealCommitOutput{
		SectorID: uint64(src.sector_id),
		CommD:    commD,
		CommR:    commR,
		Proof:    proof,
		Pieces:   pieces,
		Ticket:   goSealTicket(src.seal_ticket),
		Seed:     goSealSeed(src.seal_seed),
	}, nil
}

232
func goSectorBuilderSealPreCommitOutput(src *C.sector_builder_ffi_SectorBuilderSealPreCommitResponse) (SealPreCommitOutput, error) {
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
	commDSlice := goBytes(&src.comm_d[0], CommitmentBytesLen)
	var commD [CommitmentBytesLen]byte
	copy(commD[:], commDSlice)

	commRSlice := goBytes(&src.comm_r[0], CommitmentBytesLen)
	var commR [CommitmentBytesLen]byte
	copy(commR[:], commRSlice)

	pieces, err := goPieceMetadata(src.pieces_ptr, src.pieces_len)
	if err != nil {
		return SealPreCommitOutput{}, errors.Wrap(err, "failed to marshal piece metadata")
	}

	return SealPreCommitOutput{
		SectorID: uint64(src.sector_id),
		CommD:    commD,
		CommR:    commR,
		Pieces:   pieces,
		Ticket:   goSealTicket(src.seal_ticket),
	}, nil
}

func goResumeSealPreCommitOutput(src *C.sector_builder_ffi_ResumeSealPreCommitResponse) (SealPreCommitOutput, error) {
	commDSlice := goBytes(&src.comm_d[0], CommitmentBytesLen)
	var commD [CommitmentBytesLen]byte
	copy(commD[:], commDSlice)

	commRSlice := goBytes(&src.comm_r[0], CommitmentBytesLen)
	var commR [CommitmentBytesLen]byte
	copy(commR[:], commRSlice)

	pieces, err := goPieceMetadata(src.pieces_ptr, src.pieces_len)
	if err != nil {
		return SealPreCommitOutput{}, errors.Wrap(err, "failed to marshal piece metadata")
	}

	return SealPreCommitOutput{
		SectorID: uint64(src.sector_id),
		CommD:    commD,
		CommR:    commR,
		Pieces:   pieces,
		Ticket:   goSealTicket(src.seal_ticket),
	}, nil
}

func goSealSeed(src C.sector_builder_ffi_FFISealTicket) SealSeed {
	seedBytesSlice := C.GoBytes(unsafe.Pointer(&src.ticket_bytes[0]), 32)
	var seedBytes [CommitmentBytesLen]byte
	copy(seedBytes[:], seedBytesSlice)

	return SealSeed{
		TicketBytes: seedBytes,
		BlockHeight: uint64(src.block_height),
	}
}

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
func goStagedSectorMetadata(src *C.sector_builder_ffi_FFIStagedSectorMetadata, size C.size_t) ([]StagedSectorMetadata, error) {
	sectors := make([]StagedSectorMetadata, size)
	if src == nil || size == 0 {
		return sectors, nil
	}

	sectorPtrs := (*[1 << 30]C.sector_builder_ffi_FFIStagedSectorMetadata)(unsafe.Pointer(src))[:size:size]
	for i := 0; i < int(size); i++ {
		sectors[i] = StagedSectorMetadata{
			SectorID: uint64(sectorPtrs[i].sector_id),
		}
	}

	return sectors, nil
}

Sidney Keese's avatar
Sidney Keese committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
func goSealedSectorMetadata(src *C.sector_builder_ffi_FFISealedSectorMetadata, size C.size_t) ([]SealedSectorMetadata, error) {
	sectors := make([]SealedSectorMetadata, size)
	if src == nil || size == 0 {
		return sectors, nil
	}

	ptrs := (*[1 << 30]C.sector_builder_ffi_FFISealedSectorMetadata)(unsafe.Pointer(src))[:size:size]
	for i := 0; i < int(size); i++ {
		commDSlice := goBytes(&ptrs[i].comm_d[0], CommitmentBytesLen)
		var commD [CommitmentBytesLen]byte
		copy(commD[:], commDSlice)

		commRSlice := goBytes(&ptrs[i].comm_r[0], CommitmentBytesLen)
		var commR [CommitmentBytesLen]byte
		copy(commR[:], commRSlice)

		proof := goBytes(ptrs[i].proofs_ptr, ptrs[i].proofs_len)

		pieces, err := goPieceMetadata(ptrs[i].pieces_ptr, ptrs[i].pieces_len)
		if err != nil {
			return []SealedSectorMetadata{}, errors.Wrap(err, "failed to marshal piece metadata")
		}

328
329
330
331
332
		health, err := goSealedSectorHealth(ptrs[i].health)
		if err != nil {
			return []SealedSectorMetadata{}, errors.Wrap(err, "failed to marshal sealed sector health")
		}

Sidney Keese's avatar
Sidney Keese committed
333
		sectors[i] = SealedSectorMetadata{
334
335
336
337
338
339
340
			SectorID: uint64(ptrs[i].sector_id),
			CommD:    commD,
			CommR:    commR,
			Proof:    proof,
			Pieces:   pieces,
			Health:   health,
			Ticket:   goSealTicket(ptrs[i].seal_ticket),
341
			Seed:     goSealSeed(ptrs[i].seal_seed),
Sidney Keese's avatar
Sidney Keese committed
342
343
344
345
346
347
		}
	}

	return sectors, nil
}

348
349
350
351
352
353
354
355
func goPieceMetadata(src *C.sector_builder_ffi_FFIPieceMetadata, size C.size_t) ([]PieceMetadata, error) {
	ps := make([]PieceMetadata, size)
	if src == nil || size == 0 {
		return ps, nil
	}

	ptrs := (*[1 << 30]C.sector_builder_ffi_FFIPieceMetadata)(unsafe.Pointer(src))[:size:size]
	for i := 0; i < int(size); i++ {
356
357
358
359
		commPSlice := goBytes(&ptrs[i].comm_p[0], CommitmentBytesLen)
		var commP [CommitmentBytesLen]byte
		copy(commP[:], commPSlice)

360
		ps[i] = PieceMetadata{
361
362
363
			Key:   C.GoString(ptrs[i].piece_key),
			Size:  uint64(ptrs[i].num_bytes),
			CommP: commP,
364
365
366
367
368
		}
	}

	return ps, nil
}
369

370
func goSealedSectorHealth(health C.sector_builder_ffi_FFISealedSectorHealth) (sealed_sector_health.Health, error) {
371
	switch health {
372
	case C.Unknown:
373
		return sealed_sector_health.Unknown, nil
374
	case C.Ok:
375
		return sealed_sector_health.Ok, nil
376
	case C.ErrorInvalidChecksum:
377
		return sealed_sector_health.InvalidChecksum, nil
378
	case C.ErrorInvalidLength:
379
		return sealed_sector_health.InvalidLength, nil
380
	case C.ErrorMissing:
381
		return sealed_sector_health.Missing, nil
382
	default:
383
		return sealed_sector_health.Unknown, errors.Errorf("unhandled sealed sector health: %v", health)
384
385
	}
}