lotus/storage/sector_utils.go

58 lines
1.6 KiB
Go
Raw Normal View History

2019-11-07 18:22:59 +00:00
package storage
import (
"math/bits"
"github.com/filecoin-project/lotus/lib/sectorbuilder"
)
func fillersFromRem(toFill uint64) ([]uint64, error) {
2019-11-08 23:06:07 +00:00
// Convert to in-sector bytes for easier math:
//
// Sector size to user bytes ratio is constant, e.g. for 1024B we have 1016B
// of user-usable data.
//
// (1024/1016 = 128/127)
//
// Given that we can get sector size by simply adding 1/127 of the user
// bytes
//
// (we convert to sector bytes as they are nice round binary numbers)
2019-11-07 18:22:59 +00:00
2019-11-08 23:06:07 +00:00
toFill += toFill / 127
// We need to fill the sector with pieces that are powers of 2. Conveniently
// computers store numbers in binary, which means we can look at 1s to get
// all the piece sizes we need to fill the sector. It also means that number
// of pieces is the number of 1s in the number of remaining bytes to fill
2019-11-07 18:22:59 +00:00
out := make([]uint64, bits.OnesCount64(toFill))
for i := range out {
2019-11-08 23:06:07 +00:00
// Extract the next lowest non-zero bit
2019-11-07 18:22:59 +00:00
next := bits.TrailingZeros64(toFill)
psize := uint64(1) << next
2019-11-08 23:06:07 +00:00
// e.g: if the number is 0b010100, psize will be 0b000100
// set that bit to 0 by XORing it, so the next iteration looks at the
// next bit
2019-11-07 18:22:59 +00:00
toFill ^= psize
2019-11-08 23:06:07 +00:00
// Add the piece size to the list of pieces we need to create
2019-11-07 18:22:59 +00:00
out[i] = sectorbuilder.UserBytesForSectorSize(psize)
}
return out, nil
}
2019-11-08 18:15:13 +00:00
func (m *Miner) ListSectors() ([]SectorInfo, error) {
var sectors []SectorInfo
if err := m.sectors.List(&sectors); err != nil {
return nil, err
}
return sectors, nil
}
func (m *Miner) GetSectorInfo(sid uint64) (SectorInfo, error) {
var out SectorInfo
err := m.sectors.Get(sid, &out)
return out, err
}