|
| 1 | +package commp |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/rand" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/filecoin-project/go-padreader" |
| 14 | + "github.com/filecoin-project/go-state-types/abi" |
| 15 | + |
| 16 | + "github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper" |
| 17 | + "github.com/filecoin-project/lotus/extern/sector-storage/zerocomm" |
| 18 | +) |
| 19 | + |
| 20 | +func TestWriterZero(t *testing.T) { |
| 21 | + for i, s := range []struct { |
| 22 | + writes []int |
| 23 | + expect abi.PaddedPieceSize |
| 24 | + }{ |
| 25 | + {writes: []int{200}, expect: 256}, |
| 26 | + {writes: []int{200, 200}, expect: 512}, |
| 27 | + |
| 28 | + {writes: []int{int(CommPBuf)}, expect: commPBufPad}, |
| 29 | + {writes: []int{int(CommPBuf) * 2}, expect: 2 * commPBufPad}, |
| 30 | + {writes: []int{int(CommPBuf), int(CommPBuf), int(CommPBuf)}, expect: 4 * commPBufPad}, |
| 31 | + {writes: []int{int(CommPBuf), int(CommPBuf), int(CommPBuf), int(CommPBuf), int(CommPBuf), int(CommPBuf), int(CommPBuf), int(CommPBuf), int(CommPBuf)}, expect: 16 * commPBufPad}, |
| 32 | + |
| 33 | + {writes: []int{200, int(CommPBuf)}, expect: 2 * commPBufPad}, |
| 34 | + } { |
| 35 | + s := s |
| 36 | + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 37 | + w := &Writer{} |
| 38 | + var rawSum int64 |
| 39 | + for _, write := range s.writes { |
| 40 | + rawSum += int64(write) |
| 41 | + _, err := w.Write(make([]byte, write)) |
| 42 | + require.NoError(t, err) |
| 43 | + } |
| 44 | + |
| 45 | + p, err := w.Sum() |
| 46 | + require.NoError(t, err) |
| 47 | + require.Equal(t, rawSum, p.PayloadSize) |
| 48 | + require.Equal(t, s.expect, p.PieceSize) |
| 49 | + require.Equal(t, zerocomm.ZeroPieceCommitment(s.expect.Unpadded()).String(), p.PieceCID.String()) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestWriterData(t *testing.T) { |
| 55 | + dataLen := float64(CommPBuf) * 6.78 |
| 56 | + data, _ := ioutil.ReadAll(io.LimitReader(rand.Reader, int64(dataLen))) |
| 57 | + |
| 58 | + pr, sz := padreader.New(bytes.NewReader(data), uint64(dataLen)) |
| 59 | + exp, err := ffiwrapper.GeneratePieceCIDFromFile(abi.RegisteredSealProof_StackedDrg32GiBV1, pr, sz) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + w := &Writer{} |
| 63 | + _, err = io.Copy(w, bytes.NewReader(data)) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + res, err := w.Sum() |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + require.Equal(t, exp.String(), res.PieceCID.String()) |
| 70 | +} |
| 71 | + |
| 72 | +func BenchmarkWriterZero(b *testing.B) { |
| 73 | + buf := make([]byte, int(CommPBuf)*b.N) |
| 74 | + b.SetBytes(int64(CommPBuf)) |
| 75 | + b.ResetTimer() |
| 76 | + |
| 77 | + w := &Writer{} |
| 78 | + |
| 79 | + _, err := w.Write(buf) |
| 80 | + require.NoError(b, err) |
| 81 | + o, err := w.Sum() |
| 82 | + |
| 83 | + b.StopTimer() |
| 84 | + |
| 85 | + require.NoError(b, err) |
| 86 | + require.Equal(b, zerocomm.ZeroPieceCommitment(o.PieceSize.Unpadded()).String(), o.PieceCID.String()) |
| 87 | + require.Equal(b, int64(CommPBuf)*int64(b.N), o.PayloadSize) |
| 88 | +} |
0 commit comments