bindings_test.go 5.85 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
23
24
25
26
27
28
29
30
31
32
33
34
35

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

func TestSectorBuilderLifecycle(t *testing.T) {
	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))

	ptr, err := sb.InitSectorBuilder(1024, 2, 1, 0, metadataDir, [31]byte{}, sealedSectorDir, stagedSectorDir, 1)
	require.NoError(t, err)
	defer sb.DestroySectorBuilder(ptr)

36
37
38
39
40
	// 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")

41
42
43
44
45
46
47
	// 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)
48
49
50
	read, err := io.ReadFull(rand.Reader, pieceBytes)
	require.Equal(t, uint64(read), maxPieceSize)

51
	require.NoError(t, err)
52
	pieceFile := requireTempFile(t, bytes.NewReader(pieceBytes), maxPieceSize)
53
54

	// generate piece commitment
55
56
57
58
59
	commP, err := sb.GeneratePieceCommitmentFromFile(pieceFile, maxPieceSize)
	require.NoError(t, err)

	// seek to the beginning
	_, err = pieceFile.Seek(0, 0)
60
61
62
63
	require.NoError(t, err)

	// write a piece to a staged sector, reducing remaining space to 0 and
	// triggering the seal job
64
	sectorID, err := sb.AddPieceFromFile(ptr, "snoqualmie", maxPieceSize, pieceFile)
65
66
	require.NoError(t, err)

Sidney Keese's avatar
Sidney Keese committed
67
	stagedSectors, err := sb.GetAllStagedSectors(ptr)
68
	require.NoError(t, err)
Sidney Keese's avatar
Sidney Keese committed
69
70
71
72
	require.Equal(t, 1, len(stagedSectors))
	stagedSector := stagedSectors[0]
	require.Equal(t, uint64(1), stagedSector.SectorID)

73
74
	// block until Groth parameter cache is (lazily) hydrated and sector has
	// been sealed (or timeout)
75
	status, err := pollForSectorSealingStatus(ptr, sectorID, sealing_state.Sealed, time.Minute*30)
76
77
78
79
	require.NoError(t, err)
	require.Equal(t, 1, len(status.Pieces), "expected to see the one piece we added")

	// verify the seal proof
laser's avatar
laser committed
80
	isValid, err := sb.VerifySeal(1024, status.CommR, status.CommD, status.CommRStar, [31]byte{}, sectorID, status.Proof)
81
82
83
84
85
86
87
88
	require.NoError(t, err)
	require.True(t, isValid)

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

89
90
91
92
93
94
	// enforces sort ordering of SectorInfo tuples
	sectorInfo := sb.NewSortedSectorInfo(sb.SectorInfo{
		SectorID: status.SectorID,
		CommR:    status.CommR,
	})

95
	// generate a PoSt
96
	proofs, err := sb.GeneratePoSt(ptr, sectorInfo, [32]byte{}, []uint64{})
97
98
99
	require.NoError(t, err)

	// verify the PoSt
100
	isValid, err = sb.VerifyPoSt(1024, sectorInfo, [32]byte{}, proofs, []uint64{})
101
	require.NoError(t, err)
102
103
	require.True(t, isValid)

104
	sealedSectors, err = sb.GetAllSealedSectorsWithHealth(ptr)
105
	require.NoError(t, err)
Sidney Keese's avatar
Sidney Keese committed
106
107
108
109
	require.Equal(t, 1, len(sealedSectors), "expected to see one sealed sector")
	sealedSector := sealedSectors[0]
	require.Equal(t, uint64(1), sealedSector.SectorID)
	require.Equal(t, 1, len(sealedSector.Pieces))
110
	require.Equal(t, sealed_sector_health.Ok, sealedSector.Health)
Sidney Keese's avatar
Sidney Keese committed
111
	// the piece is the size of the sector, so its piece commitment should be the
Sidney Keese's avatar
Sidney Keese committed
112
	// data commitment
Sidney Keese's avatar
Sidney Keese committed
113
114
	require.Equal(t, commP, sealedSector.CommD)

115
116
117
	// 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")
118
	require.NoError(t, err)
119
120
121
	require.Equal(t, pieceBytes, unsealedPieceBytes)
}

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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)
	}
}

148
func pollForSectorSealingStatus(ptr unsafe.Pointer, sectorID uint64, targetState sealing_state.State, timeout time.Duration) (status sb.SectorSealingStatus, retErr error) {
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	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
			}

165
			if sealingStatus.State == targetState {
166
167
168
169
170
171
172
				status = sealingStatus
				return
			}
		}
	}
}

173
func requireTempFile(t *testing.T, fileContentsReader io.Reader, size uint64) *os.File {
174
175
176
	file, err := ioutil.TempFile("", "")
	require.NoError(t, err)

177
178
179
180
181
182
183
184
185
	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)
186
187
	require.NoError(t, err)

188
	return file
189
190
191
192
193
194
195
196
}

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

	return dir
}