Merge pull request #70 from filecoin-project/asr/refactor
extract GetRequiredPadding
This commit is contained in:
commit
937eba4785
@ -598,45 +598,65 @@ func GeneratePieceCIDFromFile(proofType abi.RegisteredSealProof, piece io.Reader
|
|||||||
return pieceCID, werr()
|
return pieceCID, werr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateUnsealedCID(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) {
|
func GetRequiredPadding(oldLength abi.PaddedPieceSize, newPieceLength abi.PaddedPieceSize) ([]abi.PaddedPieceSize, abi.PaddedPieceSize) {
|
||||||
allPieces := make([]abi.PieceInfo, 0, len(pieces))
|
|
||||||
var sum abi.PaddedPieceSize
|
|
||||||
|
|
||||||
padTo := func(s abi.PaddedPieceSize, trailing bool) {
|
padPieces := make([]abi.PaddedPieceSize, 0)
|
||||||
// pad remaining space with 0 CommPs
|
|
||||||
toFill := uint64(-sum % s)
|
toFill := uint64(-oldLength % newPieceLength)
|
||||||
if trailing && sum == 0 {
|
|
||||||
toFill = uint64(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
n := bits.OnesCount64(toFill)
|
n := bits.OnesCount64(toFill)
|
||||||
|
var sum abi.PaddedPieceSize
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
next := bits.TrailingZeros64(toFill)
|
next := bits.TrailingZeros64(toFill)
|
||||||
psize := uint64(1) << uint(next)
|
psize := uint64(1) << uint(next)
|
||||||
toFill ^= psize
|
toFill ^= psize
|
||||||
|
|
||||||
padded := abi.PaddedPieceSize(psize)
|
padded := abi.PaddedPieceSize(psize)
|
||||||
allPieces = append(allPieces, abi.PieceInfo{
|
padPieces = append(padPieces, padded)
|
||||||
Size: padded,
|
|
||||||
PieceCID: zerocomm.ZeroPieceCommitment(padded.Unpadded()),
|
|
||||||
})
|
|
||||||
sum += padded
|
sum += padded
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for _, p := range pieces {
|
return padPieces, sum
|
||||||
padTo(p.Size, false)
|
}
|
||||||
|
|
||||||
allPieces = append(allPieces, p)
|
|
||||||
sum += p.Size
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func GenerateUnsealedCID(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) {
|
||||||
ssize, err := proofType.SectorSize()
|
ssize, err := proofType.SectorSize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid.Undef, err
|
return cid.Undef, err
|
||||||
}
|
}
|
||||||
|
|
||||||
padTo(abi.PaddedPieceSize(ssize), true)
|
pssize := abi.PaddedPieceSize(ssize)
|
||||||
|
allPieces := make([]abi.PieceInfo, 0, len(pieces))
|
||||||
|
if len(pieces) == 0 {
|
||||||
|
allPieces = append(allPieces, abi.PieceInfo{
|
||||||
|
Size: pssize,
|
||||||
|
PieceCID: zerocomm.ZeroPieceCommitment(pssize.Unpadded()),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
var sum abi.PaddedPieceSize
|
||||||
|
|
||||||
|
padTo := func(pads []abi.PaddedPieceSize) {
|
||||||
|
for _, p := range pads {
|
||||||
|
allPieces = append(allPieces, abi.PieceInfo{
|
||||||
|
Size: p,
|
||||||
|
PieceCID: zerocomm.ZeroPieceCommitment(p.Unpadded()),
|
||||||
|
})
|
||||||
|
|
||||||
|
sum += p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range pieces {
|
||||||
|
ps, _ := GetRequiredPadding(sum, p.Size)
|
||||||
|
padTo(ps)
|
||||||
|
|
||||||
|
allPieces = append(allPieces, p)
|
||||||
|
sum += p.Size
|
||||||
|
}
|
||||||
|
|
||||||
|
ps, _ := GetRequiredPadding(sum, pssize)
|
||||||
|
padTo(ps)
|
||||||
|
}
|
||||||
|
|
||||||
return ffi.GenerateUnsealedCID(proofType, allPieces)
|
return ffi.GenerateUnsealedCID(proofType, allPieces)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user