2019-07-12 23:52:25 +00:00
|
|
|
package actors
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-24 13:39:13 +00:00
|
|
|
"encoding/binary"
|
2019-09-19 13:32:00 +00:00
|
|
|
"fmt"
|
2019-11-28 03:36:34 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-08-29 00:01:46 +00:00
|
|
|
|
2019-09-06 22:40:24 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2019-07-12 20:56:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StorageMinerActorState struct {
|
2019-10-30 15:55:55 +00:00
|
|
|
// PreCommittedSectors is the set of sectors that have been committed to but not
|
|
|
|
// yet had their proofs submitted
|
2019-11-07 12:03:18 +00:00
|
|
|
PreCommittedSectors map[string]*PreCommittedSector
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
// All sectors this miner has committed.
|
2019-11-28 22:50:58 +00:00
|
|
|
//
|
|
|
|
// AMT[sectorID]ffi.PublicSectorInfo
|
2019-09-19 20:24:01 +00:00
|
|
|
Sectors cid.Cid
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-10-30 15:55:55 +00:00
|
|
|
// TODO: Spec says 'StagedCommittedSectors', which one is it?
|
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
// Sectors this miner is currently mining. It is only updated
|
|
|
|
// when a PoSt is submitted (not as each new sector commitment is added).
|
2019-11-28 22:50:58 +00:00
|
|
|
//
|
|
|
|
// AMT[sectorID]ffi.PublicSectorInfo
|
2019-09-19 18:31:25 +00:00
|
|
|
ProvingSet cid.Cid
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-10-30 15:55:55 +00:00
|
|
|
// TODO: these:
|
|
|
|
// SectorTable
|
|
|
|
// SectorExpirationQueue
|
|
|
|
// ChallengeStatus
|
|
|
|
|
|
|
|
// Contains mostly static info about this miner
|
|
|
|
Info cid.Cid
|
|
|
|
|
2019-12-06 14:06:42 +00:00
|
|
|
// Faulty sectors reported since last SubmitPost
|
|
|
|
FaultSet types.BitField
|
2019-09-17 14:20:08 +00:00
|
|
|
|
2019-12-06 14:06:42 +00:00
|
|
|
LastFaultSubmission uint64
|
2019-09-17 14:20:08 +00:00
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
// Amount of power this miner has.
|
|
|
|
Power types.BigInt
|
|
|
|
|
2019-11-14 16:14:52 +00:00
|
|
|
// Active is set to true after the miner has submitted their first PoSt
|
|
|
|
Active bool
|
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
// The height at which this miner was slashed at.
|
2019-11-13 22:40:51 +00:00
|
|
|
SlashedAt uint64
|
2019-08-06 00:01:49 +00:00
|
|
|
|
2019-11-25 04:45:13 +00:00
|
|
|
ElectionPeriodStart uint64
|
2019-08-06 00:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MinerInfo struct {
|
|
|
|
// Account that owns this miner.
|
|
|
|
// - Income and returned collateral are paid to this address.
|
|
|
|
// - This address is also allowed to change the worker address for the miner.
|
|
|
|
Owner address.Address
|
|
|
|
|
|
|
|
// Worker account for this miner.
|
|
|
|
// This will be the key that is used to sign blocks created by this miner, and
|
|
|
|
// sign messages sent on behalf of this miner to commit sectors, submit PoSts, and
|
|
|
|
// other day to day miner activities.
|
|
|
|
Worker address.Address
|
|
|
|
|
|
|
|
// Libp2p identity that should be used when connecting to this miner.
|
|
|
|
PeerID peer.ID
|
|
|
|
|
|
|
|
// Amount of space in each sector committed to the network by this miner.
|
2019-10-16 07:07:16 +00:00
|
|
|
SectorSize uint64
|
2019-10-30 15:55:55 +00:00
|
|
|
|
|
|
|
// SubsectorCount
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 12:03:18 +00:00
|
|
|
type PreCommittedSector struct {
|
|
|
|
Info SectorPreCommitInfo
|
|
|
|
ReceivedEpoch uint64
|
2019-10-24 13:39:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
type StorageMinerConstructorParams struct {
|
2019-07-16 16:40:25 +00:00
|
|
|
Owner address.Address
|
2019-07-12 20:56:41 +00:00
|
|
|
Worker address.Address
|
2019-10-16 07:07:16 +00:00
|
|
|
SectorSize uint64
|
2019-07-12 20:56:41 +00:00
|
|
|
PeerID peer.ID
|
|
|
|
}
|
|
|
|
|
2019-11-07 12:03:18 +00:00
|
|
|
type SectorPreCommitInfo struct {
|
|
|
|
SectorNumber uint64
|
|
|
|
|
|
|
|
CommR []byte // TODO: Spec says CID
|
|
|
|
SealEpoch uint64
|
|
|
|
DealIDs []uint64
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:42:38 +00:00
|
|
|
type maMethods struct {
|
2019-10-21 18:12:11 +00:00
|
|
|
Constructor uint64
|
2019-10-30 15:55:55 +00:00
|
|
|
PreCommitSector uint64
|
|
|
|
ProveCommitSector uint64
|
2019-11-25 04:45:13 +00:00
|
|
|
SubmitFallbackPoSt uint64
|
2019-10-21 18:12:11 +00:00
|
|
|
SlashStorageFault uint64
|
|
|
|
GetCurrentProvingSet uint64
|
|
|
|
ArbitrateDeal uint64
|
|
|
|
DePledge uint64
|
|
|
|
GetOwner uint64
|
|
|
|
GetWorkerAddr uint64
|
|
|
|
GetPower uint64
|
|
|
|
GetPeerID uint64
|
|
|
|
GetSectorSize uint64
|
|
|
|
UpdatePeerID uint64
|
|
|
|
ChangeWorker uint64
|
2019-11-14 16:14:52 +00:00
|
|
|
IsSlashed uint64
|
2019-11-13 23:14:02 +00:00
|
|
|
CheckMiner uint64
|
2019-10-30 15:55:55 +00:00
|
|
|
DeclareFaults uint64
|
2019-10-21 18:12:11 +00:00
|
|
|
SlashConsensusFault uint64
|
2019-11-25 04:45:13 +00:00
|
|
|
SubmitElectionPoSt uint64
|
2019-10-21 18:12:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 04:45:13 +00:00
|
|
|
var MAMethods = maMethods{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
|
2019-07-26 21:42:38 +00:00
|
|
|
|
2019-07-30 22:05:27 +00:00
|
|
|
func loadState(vmctx types.VMContext) (cid.Cid, *StorageMinerActorState, ActorError) {
|
|
|
|
var self StorageMinerActorState
|
|
|
|
oldstate := vmctx.Storage().GetHead()
|
|
|
|
if err := vmctx.Storage().Get(oldstate, &self); err != nil {
|
|
|
|
return cid.Undef, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldstate, &self, nil
|
|
|
|
}
|
|
|
|
|
2019-08-06 00:01:49 +00:00
|
|
|
func loadMinerInfo(vmctx types.VMContext, m *StorageMinerActorState) (*MinerInfo, ActorError) {
|
|
|
|
var mi MinerInfo
|
|
|
|
if err := vmctx.Storage().Get(m.Info, &mi); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mi, nil
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:39:13 +00:00
|
|
|
func uintToStringKey(i uint64) string {
|
|
|
|
buf := make([]byte, 10)
|
|
|
|
n := binary.PutUvarint(buf, i)
|
|
|
|
return string(buf[:n])
|
|
|
|
}
|
|
|
|
|
2019-10-30 15:55:55 +00:00
|
|
|
type SectorProveCommitInfo struct {
|
2019-10-24 13:39:13 +00:00
|
|
|
Proof []byte
|
|
|
|
SectorID uint64
|
|
|
|
DealIDs []uint64
|
|
|
|
}
|
|
|
|
|
2019-12-08 17:32:44 +00:00
|
|
|
func truncateHexPrint(b []byte) string {
|
|
|
|
s := fmt.Sprintf("%x", b)
|
|
|
|
if len(s) > 60 {
|
|
|
|
return s[:20] + "..." + s[len(s)-20:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-11-28 17:44:49 +00:00
|
|
|
type SubmitFallbackPoStParams struct {
|
2019-11-28 12:46:56 +00:00
|
|
|
Proof []byte
|
|
|
|
Candidates []types.EPostTicket
|
2019-09-27 19:47:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 06:07:03 +00:00
|
|
|
func CollateralForPower(power types.BigInt) types.BigInt {
|
|
|
|
return types.BigMul(power, types.NewInt(10))
|
|
|
|
/* TODO: this
|
|
|
|
availableFil = FakeGlobalMethods.GetAvailableFil()
|
|
|
|
totalNetworkPower = StorageMinerActor.GetTotalStorage()
|
|
|
|
numMiners = StorageMarket.GetMinerCount()
|
|
|
|
powerCollateral = availableFil * NetworkConstants.POWER_COLLATERAL_PROPORTION * power / totalNetworkPower
|
|
|
|
perCapitaCollateral = availableFil * NetworkConstants.PER_CAPITA_COLLATERAL_PROPORTION / numMiners
|
|
|
|
collateralRequired = math.Ceil(minerPowerCollateral + minerPerCapitaCollateral)
|
|
|
|
return collateralRequired
|
|
|
|
*/
|
|
|
|
}
|
2019-07-30 00:46:56 +00:00
|
|
|
|
2019-07-30 22:05:27 +00:00
|
|
|
type UpdatePeerIDParams struct {
|
|
|
|
PeerID peer.ID
|
|
|
|
}
|
|
|
|
|
2019-11-13 23:00:11 +00:00
|
|
|
func isLate(height uint64, self *StorageMinerActorState) bool {
|
2019-11-25 04:45:13 +00:00
|
|
|
return self.ElectionPeriodStart > 0 && height >= self.ElectionPeriodStart+build.SlashablePowerDelay
|
2019-11-13 23:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 11:56:17 +00:00
|
|
|
type CheckMinerParams struct {
|
|
|
|
NetworkPower types.BigInt
|
|
|
|
}
|
|
|
|
|
2019-10-30 15:55:55 +00:00
|
|
|
type DeclareFaultsParams struct {
|
2019-09-23 18:41:53 +00:00
|
|
|
Faults types.BitField
|
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
type MinerSlashConsensusFault struct {
|
|
|
|
Slasher address.Address
|
|
|
|
AtHeight uint64
|
|
|
|
SlashedCollateral types.BigInt
|
|
|
|
}
|
|
|
|
|
2019-09-13 00:12:45 +00:00
|
|
|
func slasherShare(total types.BigInt, elapsed uint64) types.BigInt {
|
|
|
|
// [int(pow(1.26, n) * 10) for n in range(30)]
|
|
|
|
fracs := []uint64{10, 12, 15, 20, 25, 31, 40, 50, 63, 80, 100, 127, 160, 201, 254, 320, 403, 508, 640, 807, 1017, 1281, 1614, 2034, 2563, 3230, 4070, 5128, 6462, 8142}
|
|
|
|
const precision = 10000
|
|
|
|
|
|
|
|
var frac uint64
|
|
|
|
if elapsed >= uint64(len(fracs)) {
|
2019-09-20 14:21:00 +00:00
|
|
|
return total
|
2019-09-13 00:12:45 +00:00
|
|
|
} else {
|
|
|
|
frac = fracs[elapsed]
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.BigDiv(
|
|
|
|
types.BigMul(
|
|
|
|
types.NewInt(frac),
|
|
|
|
total,
|
|
|
|
),
|
|
|
|
types.NewInt(precision),
|
|
|
|
)
|
|
|
|
}
|