2020-09-17 23:08:54 +00:00
|
|
|
package market
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
|
2020-09-17 23:08:54 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2020-09-17 23:08:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
)
|
|
|
|
|
2020-09-18 04:48:50 +00:00
|
|
|
func DiffDealProposals(pre, cur DealProposals) (*DealProposalChanges, error) {
|
2020-09-17 23:08:54 +00:00
|
|
|
results := new(DealProposalChanges)
|
|
|
|
if err := adt.DiffAdtArray(pre.array(), cur.array(), &marketProposalsDiffer{results, pre, cur}); err != nil {
|
|
|
|
return nil, fmt.Errorf("diffing deal states: %w", err)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type marketProposalsDiffer struct {
|
|
|
|
Results *DealProposalChanges
|
|
|
|
pre, cur DealProposals
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *marketProposalsDiffer) Add(key uint64, val *cbg.Deferred) error {
|
|
|
|
dp, err := d.cur.decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Results.Added = append(d.Results.Added, ProposalIDState{abi.DealID(key), *dp})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *marketProposalsDiffer) Modify(key uint64, from, to *cbg.Deferred) error {
|
|
|
|
// short circuit, DealProposals are static
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *marketProposalsDiffer) Remove(key uint64, val *cbg.Deferred) error {
|
|
|
|
dp, err := d.pre.decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Results.Removed = append(d.Results.Removed, ProposalIDState{abi.DealID(key), *dp})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 04:48:50 +00:00
|
|
|
func DiffDealStates(pre, cur DealStates) (*DealStateChanges, error) {
|
2020-09-17 23:08:54 +00:00
|
|
|
results := new(DealStateChanges)
|
|
|
|
if err := adt.DiffAdtArray(pre.array(), cur.array(), &marketStatesDiffer{results, pre, cur}); err != nil {
|
|
|
|
return nil, fmt.Errorf("diffing deal states: %w", err)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type marketStatesDiffer struct {
|
|
|
|
Results *DealStateChanges
|
|
|
|
pre, cur DealStates
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *marketStatesDiffer) Add(key uint64, val *cbg.Deferred) error {
|
|
|
|
ds, err := d.cur.decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Results.Added = append(d.Results.Added, DealIDState{abi.DealID(key), *ds})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *marketStatesDiffer) Modify(key uint64, from, to *cbg.Deferred) error {
|
|
|
|
dsFrom, err := d.pre.decode(from)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dsTo, err := d.cur.decode(to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *dsFrom != *dsTo {
|
|
|
|
d.Results.Modified = append(d.Results.Modified, DealStateChange{abi.DealID(key), dsFrom, dsTo})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *marketStatesDiffer) Remove(key uint64, val *cbg.Deferred) error {
|
|
|
|
ds, err := d.pre.decode(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Results.Removed = append(d.Results.Removed, DealIDState{abi.DealID(key), *ds})
|
|
|
|
return nil
|
|
|
|
}
|