Skip to content

Commit

Permalink
chore: add tests for reward and power actor state extracters (#83)
Browse files Browse the repository at this point in the history
* chore: add tests for reward and power actor state extracters

* fix: Add some non-zero values to test subjects

Co-authored-by: Mike Greenberg <[email protected]>
  • Loading branch information
iand and placer14 authored Oct 11, 2020
1 parent 4c57122 commit 44b41e4
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 3 deletions.
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
Expand Down Expand Up @@ -1297,7 +1296,6 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -1404,7 +1402,6 @@ github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.0.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
Expand Down
36 changes: 36 additions & 0 deletions tasks/actorstate/actorstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
"github.com/filecoin-project/lotus/chain/types"
bstore "github.com/filecoin-project/lotus/lib/blockstore"
samarket "github.com/filecoin-project/specs-actors/actors/builtin/market"
sa0power "github.com/filecoin-project/specs-actors/actors/builtin/power"
sa0reward "github.com/filecoin-project/specs-actors/actors/builtin/reward"
"github.com/filecoin-project/specs-actors/actors/util/adt"
sa2power "github.com/filecoin-project/specs-actors/v2/actors/builtin/power"
sa2reward "github.com/filecoin-project/specs-actors/v2/actors/builtin/reward"
"github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -235,3 +239,35 @@ func (m *MockAPI) createBalanceTable(balances map[address.Address]balance) ([2]c

return [2]cid.Cid{escrowRootCid, lockedRootCid}, nil
}

func (m *MockAPI) newEmptyPowerStateV0() (*sa0power.State, error) {
emptyClaimsMap, err := adt.MakeEmptyMap(m.store).Root()
if err != nil {
return nil, err
}
cronEventQueueMMap, err := adt.MakeEmptyMultimap(m.store).Root()
if err != nil {
return nil, err
}
return sa0power.ConstructState(emptyClaimsMap, cronEventQueueMMap), nil
}

func (m *MockAPI) newEmptyPowerStateV2() (*sa2power.State, error) {
emptyClaimsMap, err := adt.MakeEmptyMap(m.store).Root()
if err != nil {
return nil, err
}
cronEventQueueMMap, err := adt.MakeEmptyMultimap(m.store).Root()
if err != nil {
return nil, err
}
return sa2power.ConstructState(emptyClaimsMap, cronEventQueueMMap), nil
}

func (m *MockAPI) newEmptyRewardStateV0(currRealizedPower abi.StoragePower) (*sa0reward.State, error) {
return sa0reward.ConstructState(currRealizedPower), nil
}

func (m *MockAPI) newEmptyRewardStateV2(currRealizedPower abi.StoragePower) (*sa2reward.State, error) {
return sa2reward.ConstructState(currRealizedPower), nil
}
131 changes: 131 additions & 0 deletions tasks/actorstate/power_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package actorstate

import (
"context"
"testing"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/types"
powermodel "github.com/filecoin-project/sentinel-visor/model/actors/power"
sa0builtin "github.com/filecoin-project/specs-actors/actors/builtin"
sa0smoothing "github.com/filecoin-project/specs-actors/actors/util/smoothing"
sa2builtin "github.com/filecoin-project/specs-actors/v2/actors/builtin"
sa2smoothing "github.com/filecoin-project/specs-actors/v2/actors/util/smoothing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPowerExtractV0(t *testing.T) {
ctx := context.Background()

mapi := NewMockAPI()

state, err := mapi.newEmptyPowerStateV0()
require.NoError(t, err)

state.TotalRawBytePower = abi.NewStoragePower(1000)
state.TotalBytesCommitted = abi.NewStoragePower(2000)
state.TotalQualityAdjPower = abi.NewStoragePower(3000)
state.TotalQABytesCommitted = abi.NewStoragePower(4000)
state.TotalPledgeCollateral = abi.NewTokenAmount(5000)
state.ThisEpochRawBytePower = abi.NewStoragePower(6000)
state.ThisEpochQualityAdjPower = abi.NewStoragePower(7000)
state.ThisEpochPledgeCollateral = abi.NewTokenAmount(8000)
state.ThisEpochQAPowerSmoothed = sa0smoothing.NewEstimate(big.NewInt(800_000_000_000), big.NewInt(6_000_000_000))
state.MinerCount = 10
state.MinerAboveMinPowerCount = 20

stateCid, err := mapi.Store().Put(ctx, state)
require.NoError(t, err)

minerAddr, err := address.NewFromString("t00")
require.NoError(t, err)
stateTs, err := mockTipset(minerAddr, 1)
require.NoError(t, err)

info := ActorInfo{
Actor: types.Actor{Code: sa0builtin.StoragePowerActorCodeID, Head: stateCid},
Address: power.Address,
TipSet: stateTs.Key(),
}

mapi.setActor(stateTs.Key(), power.Address, &types.Actor{Code: sa0builtin.StoragePowerActorCodeID, Head: stateCid})

ex := StoragePowerExtractor{}
res, err := ex.Extract(ctx, info, mapi)
require.NoError(t, err)

cp, ok := res.(*powermodel.ChainPower)
require.True(t, ok)
require.NotNil(t, cp)

assert.EqualValues(t, info.ParentStateRoot.String(), cp.StateRoot, "StateRoot")
assert.EqualValues(t, state.TotalRawBytePower.String(), cp.TotalRawBytesPower, "TotalRawBytesPower")
assert.EqualValues(t, state.TotalQualityAdjPower.String(), cp.TotalQABytesPower, "TotalQABytesPower")
assert.EqualValues(t, state.TotalBytesCommitted.String(), cp.TotalRawBytesCommitted, "TotalRawBytesCommitted")
assert.EqualValues(t, state.TotalQABytesCommitted.String(), cp.TotalQABytesCommitted, "TotalQABytesCommitted")
assert.EqualValues(t, state.TotalPledgeCollateral.String(), cp.TotalPledgeCollateral, "TotalPledgeCollateral")
assert.EqualValues(t, state.ThisEpochQAPowerSmoothed.PositionEstimate.String(), cp.QASmoothedPositionEstimate, "QASmoothedPositionEstimate")
assert.EqualValues(t, state.ThisEpochQAPowerSmoothed.VelocityEstimate.String(), cp.QASmoothedVelocityEstimate, "QASmoothedVelocityEstimate")
assert.EqualValues(t, state.MinerCount, cp.MinerCount, "MinerCount")
assert.EqualValues(t, state.MinerAboveMinPowerCount, cp.ParticipatingMinerCount, "ParticipatingMinerCount")
}

func TestPowerExtractV2(t *testing.T) {
ctx := context.Background()

mapi := NewMockAPI()

state, err := mapi.newEmptyPowerStateV2()
require.NoError(t, err)

state.TotalRawBytePower = abi.NewStoragePower(1000)
state.TotalBytesCommitted = abi.NewStoragePower(0)
state.TotalQualityAdjPower = abi.NewStoragePower(0)
state.TotalQABytesCommitted = abi.NewStoragePower(0)
state.TotalPledgeCollateral = abi.NewTokenAmount(0)
state.ThisEpochRawBytePower = abi.NewStoragePower(0)
state.ThisEpochQualityAdjPower = abi.NewStoragePower(0)
state.ThisEpochPledgeCollateral = abi.NewTokenAmount(0)
state.ThisEpochQAPowerSmoothed = sa2smoothing.NewEstimate(big.NewInt(800_000_000_000), big.NewInt(6_000_000_000))
state.MinerCount = 0
state.MinerAboveMinPowerCount = 0

stateCid, err := mapi.Store().Put(ctx, state)
require.NoError(t, err)

minerAddr, err := address.NewFromString("t00")
require.NoError(t, err)
stateTs, err := mockTipset(minerAddr, 1)
require.NoError(t, err)

info := ActorInfo{
Actor: types.Actor{Code: sa2builtin.StoragePowerActorCodeID, Head: stateCid},
Address: power.Address,
TipSet: stateTs.Key(),
}

mapi.setActor(stateTs.Key(), power.Address, &types.Actor{Code: sa2builtin.StoragePowerActorCodeID, Head: stateCid})

ex := StoragePowerExtractor{}
res, err := ex.Extract(ctx, info, mapi)
require.NoError(t, err)

cp, ok := res.(*powermodel.ChainPower)
require.True(t, ok)
require.NotNil(t, cp)

assert.EqualValues(t, info.ParentStateRoot.String(), cp.StateRoot, "StateRoot")
assert.EqualValues(t, state.TotalRawBytePower.String(), cp.TotalRawBytesPower, "TotalRawBytesPower")
assert.EqualValues(t, state.TotalQualityAdjPower.String(), cp.TotalQABytesPower, "TotalQABytesPower")
assert.EqualValues(t, state.TotalBytesCommitted.String(), cp.TotalRawBytesCommitted, "TotalRawBytesCommitted")
assert.EqualValues(t, state.TotalQABytesCommitted.String(), cp.TotalQABytesCommitted, "TotalQABytesCommitted")
assert.EqualValues(t, state.TotalPledgeCollateral.String(), cp.TotalPledgeCollateral, "TotalPledgeCollateral")
assert.EqualValues(t, state.ThisEpochQAPowerSmoothed.PositionEstimate.String(), cp.QASmoothedPositionEstimate, "QASmoothedPositionEstimate")
assert.EqualValues(t, state.ThisEpochQAPowerSmoothed.VelocityEstimate.String(), cp.QASmoothedVelocityEstimate, "QASmoothedVelocityEstimate")
assert.EqualValues(t, state.MinerCount, cp.MinerCount, "MinerCount")
assert.EqualValues(t, state.MinerAboveMinPowerCount, cp.ParticipatingMinerCount, "ParticipatingMinerCount")
}
128 changes: 128 additions & 0 deletions tasks/actorstate/reward_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package actorstate

import (
"context"
"testing"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
"github.com/filecoin-project/lotus/chain/types"
rewardmodel "github.com/filecoin-project/sentinel-visor/model/actors/reward"
sa0builtin "github.com/filecoin-project/specs-actors/actors/builtin"
sa0smoothing "github.com/filecoin-project/specs-actors/actors/util/smoothing"
sa2builtin "github.com/filecoin-project/specs-actors/v2/actors/builtin"
sa2smoothing "github.com/filecoin-project/specs-actors/v2/actors/util/smoothing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRewardExtractV0(t *testing.T) {
ctx := context.Background()

mapi := NewMockAPI()

state, err := mapi.newEmptyRewardStateV0(abi.NewStoragePower(500))
require.NoError(t, err)

state.CumsumBaseline = big.NewInt(1000)
state.CumsumRealized = big.NewInt(2000)
state.EffectiveNetworkTime = abi.ChainEpoch(3000)
state.EffectiveBaselinePower = abi.NewStoragePower(4000)
state.ThisEpochReward = abi.NewTokenAmount(5000)
state.ThisEpochRewardSmoothed = sa0smoothing.NewEstimate(big.NewInt(6000), big.NewInt(60))
state.ThisEpochBaselinePower = abi.NewStoragePower(7000)
state.TotalMined = abi.NewStoragePower(8000)

stateCid, err := mapi.Store().Put(ctx, state)
require.NoError(t, err)

minerAddr, err := address.NewFromString("t00")
require.NoError(t, err)
stateTs, err := mockTipset(minerAddr, 1)
require.NoError(t, err)

info := ActorInfo{
Actor: types.Actor{Code: sa0builtin.RewardActorCodeID, Head: stateCid},
Address: power.Address,
TipSet: stateTs.Key(),
}

mapi.setActor(stateTs.Key(), reward.Address, &types.Actor{Code: sa0builtin.RewardActorCodeID, Head: stateCid})

ex := RewardExtractor{}
res, err := ex.Extract(ctx, info, mapi)
require.NoError(t, err)

cr, ok := res.(*rewardmodel.ChainReward)
require.True(t, ok)
require.NotNil(t, cr)

assert.EqualValues(t, info.ParentStateRoot.String(), cr.StateRoot, "StateRoot")
assert.EqualValues(t, state.CumsumBaseline.String(), cr.CumSumBaseline, "CumSumBaseline")
// TODO: assertion fails due to bug in lotus see https://github.com/filecoin-project/lotus/pull/4268
// assert.EqualValues(t, state.CumsumRealized.String(), cr.CumSumRealized, "CumSumRealized")
assert.EqualValues(t, state.EffectiveBaselinePower.String(), cr.EffectiveBaselinePower, "EffectiveBaselinePower")
assert.EqualValues(t, state.ThisEpochBaselinePower.String(), cr.NewBaselinePower, "NewBaselinePower")
assert.EqualValues(t, state.ThisEpochRewardSmoothed.PositionEstimate.String(), cr.NewRewardSmoothedPositionEstimate, "NewRewardSmoothedPositionEstimate")
assert.EqualValues(t, state.ThisEpochRewardSmoothed.VelocityEstimate.String(), cr.NewRewardSmoothedVelocityEstimate, "NewRewardSmoothedVelocityEstimate")
assert.EqualValues(t, state.TotalMined.String(), cr.TotalMinedReward, "TotalMinedReward")
assert.EqualValues(t, state.ThisEpochReward.String(), cr.NewReward, "NewReward")
assert.EqualValues(t, state.EffectiveNetworkTime, cr.EffectiveNetworkTime, "EffectiveNetworkTime")
}

func TestRewardExtractV2(t *testing.T) {
ctx := context.Background()

mapi := NewMockAPI()

state, err := mapi.newEmptyRewardStateV2(abi.NewStoragePower(500))
require.NoError(t, err)

state.CumsumBaseline = big.NewInt(1000)
state.CumsumRealized = big.NewInt(2000)
state.EffectiveNetworkTime = abi.ChainEpoch(3000)
state.EffectiveBaselinePower = abi.NewStoragePower(4000)
state.ThisEpochReward = abi.NewTokenAmount(5000)
state.ThisEpochRewardSmoothed = sa2smoothing.NewEstimate(big.NewInt(6000), big.NewInt(60))
state.ThisEpochBaselinePower = abi.NewStoragePower(7000)
state.TotalStoragePowerReward = abi.NewStoragePower(8000)

stateCid, err := mapi.Store().Put(ctx, state)
require.NoError(t, err)

minerAddr, err := address.NewFromString("t00")
require.NoError(t, err)
stateTs, err := mockTipset(minerAddr, 1)
require.NoError(t, err)

info := ActorInfo{
Actor: types.Actor{Code: sa2builtin.RewardActorCodeID, Head: stateCid},
Address: power.Address,
TipSet: stateTs.Key(),
}

mapi.setActor(stateTs.Key(), reward.Address, &types.Actor{Code: sa2builtin.RewardActorCodeID, Head: stateCid})

ex := RewardExtractor{}
res, err := ex.Extract(ctx, info, mapi)
require.NoError(t, err)

cr, ok := res.(*rewardmodel.ChainReward)
require.True(t, ok)
require.NotNil(t, cr)

assert.EqualValues(t, info.ParentStateRoot.String(), cr.StateRoot, "StateRoot")
assert.EqualValues(t, state.CumsumBaseline.String(), cr.CumSumBaseline, "CumSumBaseline")
// TODO: assertion fails due to bug in lotus see https://github.com/filecoin-project/lotus/pull/4268
// assert.EqualValues(t, state.CumsumRealized.String(), cr.CumSumRealized, "CumSumRealized")
assert.EqualValues(t, state.EffectiveBaselinePower.String(), cr.EffectiveBaselinePower, "EffectiveBaselinePower")
assert.EqualValues(t, state.ThisEpochBaselinePower.String(), cr.NewBaselinePower, "NewBaselinePower")
assert.EqualValues(t, state.ThisEpochRewardSmoothed.PositionEstimate.String(), cr.NewRewardSmoothedPositionEstimate, "NewRewardSmoothedPositionEstimate")
assert.EqualValues(t, state.ThisEpochRewardSmoothed.VelocityEstimate.String(), cr.NewRewardSmoothedVelocityEstimate, "NewRewardSmoothedVelocityEstimate")
assert.EqualValues(t, state.TotalStoragePowerReward.String(), cr.TotalMinedReward, "TotalMinedReward")
assert.EqualValues(t, state.ThisEpochReward.String(), cr.NewReward, "NewReward")
assert.EqualValues(t, state.EffectiveNetworkTime, cr.EffectiveNetworkTime, "EffectiveNetworkTime")
}

0 comments on commit 44b41e4

Please sign in to comment.