bindings_test.go 7.21 KB
Newer Older
1
2
3
4
5
6
7
8
package go_sectorbuilder_test

import (
	"bytes"
	"crypto/rand"
	"errors"
	"io"
	"io/ioutil"
9
	"math/big"
10
11
12
13
14
15
	"os"
	"testing"
	"time"
	"unsafe"

	sb "github.com/filecoin-project/go-sectorbuilder"
16
	"github.com/filecoin-project/go-sectorbuilder/sealed_sector_health"
17
	"github.com/filecoin-project/go-sectorbuilder/sealing_state"
18
19
20
21
22

	"github.com/stretchr/testify/require"
)

func TestSectorBuilderLifecycle(t *testing.T) {
23
24
25
26
27
28
29
30
31
32
33
34
	ticketA := sb.SealTicket{
		BlockHeight: 0,
		TicketBytes: [32]byte{},
	}

	ticketB := sb.SealTicket{
		BlockHeight: 10,
		TicketBytes: [32]byte{1, 2, 3},
	}

	proverID := [32]byte{6, 7, 8}

35
36
37
38
39
40
41
42
43
	metadataDir := requireTempDirPath(t)
	defer require.NoError(t, os.Remove(metadataDir))

	sealedSectorDir := requireTempDirPath(t)
	defer require.NoError(t, os.Remove(sealedSectorDir))

	stagedSectorDir := requireTempDirPath(t)
	defer require.NoError(t, os.Remove(stagedSectorDir))

44
45
46
47
	sectorCacheRootDir := requireTempDirPath(t)
	defer require.NoError(t, os.Remove(sectorCacheRootDir))

	ptr, err := sb.InitSectorBuilder(1024, 2, 0, metadataDir, proverID, sealedSectorDir, stagedSectorDir, sectorCacheRootDir, 1)
48
49
50
	require.NoError(t, err)
	defer sb.DestroySectorBuilder(ptr)

51
52
53
54
55
	// verify that we've not yet sealed a sector
	sealedSectors, err := sb.GetAllSealedSectorsWithHealth(ptr)
	require.NoError(t, err)
	require.Equal(t, 0, len(sealedSectors), "expected to see zero sealed sectors")

56
57
58
59
60
61
62
	// compute the max user-bytes that can fit into a staged sector such that
	// bit-padding ("preprocessing") expands the file to $SECTOR_SIZE
	maxPieceSize := sb.GetMaxUserBytesPerStagedSector(1024)

	// create a piece which consumes all available space in a new, staged
	// sector
	pieceBytes := make([]byte, maxPieceSize)
63
64
65
	read, err := io.ReadFull(rand.Reader, pieceBytes)
	require.Equal(t, uint64(read), maxPieceSize)

66
	require.NoError(t, err)
67
68
69
70
	pieceFileA := requireTempFile(t, bytes.NewReader(pieceBytes), maxPieceSize)

	require.NoError(t, err)
	pieceFileB := requireTempFile(t, bytes.NewReader(pieceBytes), maxPieceSize)
71
72

	// generate piece commitment
73
	commP, err := sb.GeneratePieceCommitmentFromFile(pieceFileA, maxPieceSize)
74
75
76
	require.NoError(t, err)

	// seek to the beginning
77
	_, err = pieceFileA.Seek(0, 0)
78
79
	require.NoError(t, err)

80
81
	// write a piece to a staged sector, reducing remaining space to 0
	sectorIDA, err := sb.AddPieceFromFile(ptr, "snoqualmie", maxPieceSize, pieceFileA)
82
83
	require.NoError(t, err)

Sidney Keese's avatar
Sidney Keese committed
84
	stagedSectors, err := sb.GetAllStagedSectors(ptr)
85
	require.NoError(t, err)
Sidney Keese's avatar
Sidney Keese committed
86
87
88
89
	require.Equal(t, 1, len(stagedSectors))
	stagedSector := stagedSectors[0]
	require.Equal(t, uint64(1), stagedSector.SectorID)

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	// block until the sector is ready for us to begin sealing
	statusA, err := pollForSectorSealingStatus(ptr, sectorIDA, sealing_state.ReadyForSealing, time.Minute)
	require.NoError(t, err)

	// seal all staged sectors
	go func() {
		// blocks until sealing has completed
		meta, err := sb.SealAllStagedSectors(ptr, ticketA)
		require.NoError(t, err)
		require.Equal(t, 1, len(meta))
		require.Equal(t, 1, len(meta[0].Pieces), "expected to see the one piece we added")
		require.Equal(t, stagedSector.SectorID, meta[0].SectorID)
	}()

	// block until the sector begins to seal
	_, err = pollForSectorSealingStatus(ptr, sectorIDA, sealing_state.Sealing, 15*time.Second)
106
	require.NoError(t, err)
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

	// write a second piece to a staged sector, reducing remaining space to 0
	sectorIDB, err := sb.AddPieceFromFile(ptr, "duvall", maxPieceSize, pieceFileB)
	require.NoError(t, err)

	go func() {
		meta, err := sb.SealSector(ptr, sectorIDB, ticketB)
		require.NoError(t, err)
		require.Equal(t, sectorIDB, meta.SectorID)
	}()

	// block until both sectors have successfully sealed
	statusA, err = pollForSectorSealingStatus(ptr, sectorIDA, sealing_state.Sealed, 30*time.Minute)
	require.NoError(t, err)
	require.Equal(t, ticketA, statusA.Ticket)

	statusB, err := pollForSectorSealingStatus(ptr, sectorIDB, sealing_state.Sealed, 30*time.Minute)
	require.NoError(t, err)
	require.Equal(t, ticketB, statusB.Ticket)
126
127

	// verify the seal proof
128
	isValid, err := sb.VerifySeal(1024, statusA.CommR, statusA.CommD, proverID, ticketA.TicketBytes, sectorIDA, statusA.Proof)
129
130
131
132
	require.NoError(t, err)
	require.True(t, isValid)

	// verify the piece inclusion proof
133
	isValid, err = sb.VerifyPieceInclusionProof(1024, maxPieceSize, commP, statusA.CommD, statusA.Pieces[0].InclusionProof)
134
135
136
	require.NoError(t, err)
	require.True(t, isValid)

137
138
	// enforces sort ordering of SectorInfo tuples
	sectorInfo := sb.NewSortedSectorInfo(sb.SectorInfo{
139
140
		SectorID: statusA.SectorID,
		CommR:    statusA.CommR,
141
142
	})

143
	// generate a PoSt
144
	proofs, err := sb.GeneratePoSt(ptr, sectorInfo, [32]byte{}, []uint64{})
145
146
147
	require.NoError(t, err)

	// verify the PoSt
148
	isValid, err = sb.VerifyPoSt(1024, sectorInfo, [32]byte{}, proofs, []uint64{})
149
	require.NoError(t, err)
150
151
	require.True(t, isValid)

152
	sealedSectors, err = sb.GetAllSealedSectorsWithHealth(ptr)
153
	require.NoError(t, err)
154
155
156
157
158
159
160
	require.Equal(t, 2, len(sealedSectors), "expected to see two sealed sectors")
	for _, sealedSector := range sealedSectors {
		require.Equal(t, sealed_sector_health.Ok, sealedSector.Health)
	}

	// both sealed sectors contain the same data, so either will suffice
	require.Equal(t, commP, sealedSectors[0].CommD)
Sidney Keese's avatar
Sidney Keese committed
161

162
163
164
	// unseal the sector and retrieve the client's piece, verifying that the
	// retrieved bytes match what we originally wrote to the staged sector
	unsealedPieceBytes, err := sb.ReadPieceFromSealedSector(ptr, "snoqualmie")
165
	require.NoError(t, err)
166
167
168
	require.Equal(t, pieceBytes, unsealedPieceBytes)
}

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
func TestJsonMarshalSymmetry(t *testing.T) {
	for i := 0; i < 100; i++ {
		xs := make([]sb.SectorInfo, 10)
		for j := 0; j < 10; j++ {
			var x sb.SectorInfo
			_, err := io.ReadFull(rand.Reader, x.CommR[:])
			require.NoError(t, err)

			n, err := rand.Int(rand.Reader, big.NewInt(500))
			require.NoError(t, err)
			x.SectorID = n.Uint64()
			xs[j] = x
		}
		toSerialize := sb.NewSortedSectorInfo(xs...)

		serialized, err := toSerialize.MarshalJSON()
		require.NoError(t, err)

		var fromSerialized sb.SortedSectorInfo
		err = fromSerialized.UnmarshalJSON(serialized)
		require.NoError(t, err)

		require.Equal(t, toSerialize, fromSerialized)
	}
}

195
func pollForSectorSealingStatus(ptr unsafe.Pointer, sectorID uint64, targetState sealing_state.State, timeout time.Duration) (status sb.SectorSealingStatus, retErr error) {
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
	timeoutCh := time.After(timeout)

	tick := time.Tick(5 * time.Second)

	for {
		select {
		case <-timeoutCh:
			retErr = errors.New("timed out waiting for sector to finish sealing")
			return
		case <-tick:
			sealingStatus, err := sb.GetSectorSealingStatusByID(ptr, sectorID)
			if err != nil {
				retErr = err
				return
			}

212
			if sealingStatus.State == targetState {
213
214
215
216
217
218
219
				status = sealingStatus
				return
			}
		}
	}
}

220
func requireTempFile(t *testing.T, fileContentsReader io.Reader, size uint64) *os.File {
221
222
223
	file, err := ioutil.TempFile("", "")
	require.NoError(t, err)

224
225
226
227
228
229
230
231
232
	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)
233
234
	require.NoError(t, err)

235
	return file
236
237
238
239
240
241
242
243
}

func requireTempDirPath(t *testing.T) string {
	dir, err := ioutil.TempDir("", "")
	require.NoError(t, err)

	return dir
}