/
invariants.go
44 lines (36 loc) · 1.54 KB
/
invariants.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package paych
import (
"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/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v8/util/adt"
)
type StateSummary struct {
Redeemed abi.TokenAmount
}
// Checks internal invariants of paych state.
func CheckStateInvariants(st *State, store adt.Store, balance abi.TokenAmount) (*StateSummary, *builtin.MessageAccumulator) {
acc := &builtin.MessageAccumulator{}
paychSummary := &StateSummary{
Redeemed: big.Zero(),
}
acc.Require(st.From.Protocol() == address.ID, "from address is not ID address %v", st.From)
acc.Require(st.To.Protocol() == address.ID, "to address is not ID address %v", st.To)
acc.Require(st.SettlingAt >= st.MinSettleHeight,
"channel is setting at epoch %d before min settle height %d", st.SettlingAt, st.MinSettleHeight)
if lanes, err := adt.AsArray(store, st.LaneStates, LaneStatesAmtBitwidth); err != nil {
acc.Addf("error loading lanes: %v", err)
} else {
var lane LaneState
err = lanes.ForEach(&lane, func(i int64) error {
acc.Require(lane.Redeemed.GreaterThan(big.Zero()), "land %d redeemed is not greater than zero %v", i, lane.Redeemed)
paychSummary.Redeemed = big.Add(paychSummary.Redeemed, lane.Redeemed)
return nil
})
acc.RequireNoError(err, "error iterating lanes")
}
acc.Require(balance.GreaterThanEqual(st.ToSend),
"channel has insufficient funds to send (%v < %v)", balance, st.ToSend)
return paychSummary, acc
}