refactor filter decision code

This commit is contained in:
LexLuthr 2024-04-03 00:34:03 +04:00 committed by Łukasz Magiera
parent 2951d038a8
commit d1a52cf04d
3 changed files with 114 additions and 215 deletions

View File

@ -15,7 +15,6 @@ import (
"go.opencensus.io/tag"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/journal/alerting"
@ -578,52 +577,23 @@ func (dbi *DBIndex) StorageFindSector(ctx context.Context, s abi.SectorID, ft st
log.Debugf("not selecting on %s, not allowed by file type filters", row.StorageId)
continue
}
if len(row.AllowMiners) > 0 {
found := false
allowMiners := splitString(row.AllowMiners)
for _, m := range allowMiners {
m := m
maddr, err := address.NewFromString(m)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("parsing miner ID: %w", err)
}
if abi.ActorID(mid) == s.Miner {
found = true
break
}
}
if !found {
log.Debugf("not selecting on %s, not allowed by allow miners filters", row.StorageId)
continue
}
allowMiners := splitString(row.AllowMiners)
proceed, err := MinerFilter(allowMiners, false, s.Miner)
if err != nil {
return nil, err
}
if len(row.DenyMiners) > 0 {
found := false
denyMiners := splitString(row.DenyMiners)
for _, m := range denyMiners {
m := m
maddr, err := address.NewFromString(m)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("parsing miner ID: %w", err)
}
if abi.ActorID(mid) == s.Miner {
found = true
break
}
}
if found {
log.Debugf("not selecting on %s, not allowed by deny miners filters", row.StorageId)
continue
}
if !proceed {
log.Debugf("not allocating on %s, miner %s not allowed", row.StorageId, s.Miner.String())
continue
}
denyMiners := splitString(row.DenyMiners)
proceed, err = MinerFilter(denyMiners, true, s.Miner)
if err != nil {
return nil, err
}
if !proceed {
log.Debugf("not allocating on %s, miner %s denied", row.StorageId, s.Miner.String())
continue
}
if allowList != nil {
@ -776,49 +746,23 @@ func (dbi *DBIndex) StorageBestAlloc(ctx context.Context, allocate storiface.Sec
// Matching with 0 as a workaround to avoid having minerID
// present when calling TaskStorage.HasCapacity()
if !(miner == abi.ActorID(0)) {
if len(row.AllowMiners) > 0 {
found := false
allowMiners := splitString(row.AllowMiners)
for _, m := range allowMiners {
m := m
maddr, err := address.NewFromString(m)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("parsing miner ID: %w", err)
}
if abi.ActorID(mid) == miner {
found = true
break
}
}
if !found {
continue
}
allowMiners := splitString(row.AllowMiners)
proceed, err := MinerFilter(allowMiners, false, miner)
if err != nil {
return nil, err
}
if len(row.DenyMiners) > 0 {
found := false
denyMiners := splitString(row.DenyMiners)
for _, m := range denyMiners {
m := m
maddr, err := address.NewFromString(m)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("parsing miner ID: %w", err)
}
if abi.ActorID(mid) == miner {
found = true
break
}
}
if found {
continue
}
if !proceed {
log.Debugf("not allocating on %s, miner %s not allowed", row.StorageId, miner.String())
continue
}
denyMiners := splitString(row.DenyMiners)
proceed, err = MinerFilter(denyMiners, true, miner)
if err != nil {
return nil, err
}
if !proceed {
log.Debugf("not allocating on %s, miner %s denied", row.StorageId, miner.String())
continue
}
}

View File

@ -498,50 +498,24 @@ func (i *MemIndex) StorageFindSector(ctx context.Context, s abi.SectorID, ft sto
continue
}
if len(st.info.AllowMiners) > 0 {
found := false
for _, m := range st.info.AllowMiners {
minerIDStr := m
maddr, err := address.NewFromString(minerIDStr)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("converting miner address to ID: %w", err)
}
if abi.ActorID(mid) == s.Miner {
found = true
break
}
}
if !found {
log.Debugf("not allocating on %s, miner %s not allowed", st.info.ID, s.Miner.String())
continue
}
proceed, err := MinerFilter(st.info.AllowMiners, false, s.Miner)
if err != nil {
return nil, err
}
if len(st.info.DenyMiners) > 0 {
found := false
for _, m := range st.info.DenyMiners {
minerIDStr := m
maddr, err := address.NewFromString(minerIDStr)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("converting miner address to ID: %w", err)
}
if abi.ActorID(mid) == s.Miner {
found = true
break
}
}
if found {
log.Debugf("not allocating on %s, miner %s denied", st.info.ID, s.Miner.String())
continue
}
if !proceed {
log.Debugf("not allocating on %s, miner %s not allowed", st.info.ID, s.Miner.String())
continue
}
proceed, err = MinerFilter(st.info.DenyMiners, true, s.Miner)
if err != nil {
return nil, err
}
if !proceed {
log.Debugf("not allocating on %s, miner %s denied", st.info.ID, s.Miner.String())
continue
}
if spaceReq > uint64(st.fsi.Available) {
@ -699,50 +673,24 @@ func (i *MemIndex) StorageBestAlloc(ctx context.Context, allocate storiface.Sect
continue
}
if len(p.info.AllowMiners) > 0 {
found := false
for _, m := range p.info.AllowMiners {
minerIDStr := m
maddr, err := address.NewFromString(minerIDStr)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("converting miner address to ID: %w", err)
}
if abi.ActorID(mid) == miner {
found = true
break
}
}
if !found {
log.Debugf("not allocating on %s, miner %s not allowed", p.info.ID, miner.String())
continue
}
proceed, err := MinerFilter(p.info.AllowMiners, false, miner)
if err != nil {
return nil, err
}
if len(p.info.DenyMiners) > 0 {
found := false
for _, m := range p.info.DenyMiners {
minerIDStr := m
maddr, err := address.NewFromString(minerIDStr)
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, xerrors.Errorf("converting miner address to ID: %w", err)
}
if abi.ActorID(mid) == miner {
found = true
break
}
}
if found {
log.Debugf("not allocating on %s, miner %s denied", p.info.ID, miner.String())
continue
}
if !proceed {
log.Debugf("not allocating on %s, miner %s not allowed", p.info.ID, miner.String())
continue
}
proceed, err = MinerFilter(p.info.DenyMiners, true, miner)
if err != nil {
return nil, err
}
if !proceed {
log.Debugf("not allocating on %s, miner %s denied", p.info.ID, miner.String())
continue
}
if spaceReq > uint64(p.fsi.Available) {
@ -802,3 +750,41 @@ func (i *MemIndex) FindSector(id abi.SectorID, typ storiface.SectorFileType) ([]
}
var _ SectorIndex = &MemIndex{}
func MinerFilter(miners []string, deny bool, miner abi.ActorID) (bool, error) {
if len(miners) > 0 {
found := false
for _, m := range miners {
minerIDStr := m
maddr, err := address.NewFromString(minerIDStr)
if err != nil {
return false, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return false, xerrors.Errorf("converting miner address to ID: %w", err)
}
if abi.ActorID(mid) == miner {
found = true
break
}
}
// If not found in list
if !found {
// If this is allowed list
if !deny {
return false, nil
}
// If this is denied list
return true, nil
}
// If found in list and
// If this is allowed list
if !deny {
return true, nil
}
// If this is denied list
return false, nil
}
return true, nil
}

View File

@ -14,7 +14,6 @@ import (
"golang.org/x/xerrors"
ffi "github.com/filecoin-project/filecoin-ffi"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/proof"
@ -512,49 +511,19 @@ func (st *Local) AcquireSector(ctx context.Context, sid storiface.SectorRef, exi
if !fileType.Allowed(allowTypes, denyTypes) {
return false, nil
}
if len(allowMiners) > 0 {
found := false
for _, m := range allowMiners {
minerIDStr := m
maddr, err := address.NewFromString(minerIDStr)
if err != nil {
return false, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return false, xerrors.Errorf("converting miner address to ID: %w", err)
}
if abi.ActorID(mid) == miner {
found = true
break
}
}
if !found {
return false, nil
}
proceed, err := MinerFilter(allowMiners, false, miner)
if err != nil {
return false, err
}
if len(denyMiners) > 0 {
found := false
for _, m := range denyMiners {
minerIDStr := m
maddr, err := address.NewFromString(minerIDStr)
if err != nil {
return false, xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return false, xerrors.Errorf("converting miner address to ID: %w", err)
}
if abi.ActorID(mid) == miner {
found = true
break
}
}
if found {
return false, nil
}
if !proceed {
return false, nil
}
proceed, err = MinerFilter(denyMiners, true, miner)
if err != nil {
return false, err
}
if !proceed {
return false, nil
}
return true, nil