Skip to content

Commit

Permalink
Add support for "one-of" attestors alongside "each"
Browse files Browse the repository at this point in the history
  • Loading branch information
Maelkum committed Oct 7, 2023
1 parent fa08d6c commit 698f420
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
14 changes: 0 additions & 14 deletions models/execute/request.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package execute

import "github.com/libp2p/go-libp2p/core/peer"

// Request describes an execution request.
type Request struct {
FunctionID string `json:"function_id"`
Expand Down Expand Up @@ -49,15 +47,3 @@ type ResultAggregation struct {
Type string `json:"type,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
}

type Attributes struct {
// Values specify which attributes the node in question should have.
// At the moment we support strict equality only, so no `if RAM >= 16GB` types of conditions.
Values []Parameter `json:"values,omitempty"`

// Should we accept nodes whose attributes are not attested?
AttestationRequired bool `json:"attestation_required,omitempty"`

// Explicitly request specific attestors.
Attestors []peer.ID `json:"attestors,omitempty"`
}
25 changes: 25 additions & 0 deletions models/execute/request_attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package execute

import (
"github.com/libp2p/go-libp2p/core/peer"
)

type Attributes struct {
// Values specify which attributes the node in question should have.
// At the moment we support strict equality only, so no `if RAM >= 16GB` types of conditions.
Values []Parameter `json:"values,omitempty"`

// Should we accept nodes whose attributes are not attested?
AttestationRequired bool `json:"attestation_required,omitempty"`

// Explicitly request specific attestors.
Attestors AttributeAttestors `json:"attestors,omitempty"`
}

type AttributeAttestors struct {
// Each of the listed attestors should be found.
Each []peer.ID `json:"each,omitempty"`

// Any one of these attestors should be found.
OneOf []peer.ID `json:"one_of,omitempty"`
}
29 changes: 24 additions & 5 deletions node/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"

"github.com/blocklessnetwork/b7s-attributes/attributes"
"github.com/blocklessnetwork/b7s/models/blockless"
"github.com/blocklessnetwork/b7s/models/execute"
)

Expand Down Expand Up @@ -62,22 +63,40 @@ func haveAttributes(have attributes.Attestation, want execute.Attributes) error
return errors.New("attestors required but none found")
}

// If the client wants specific attestors, check if they're present.
if len(want.Attestors) > 0 {

attestors := make(map[peer.ID]struct{}, len(have.Attestors))
// If we need to check attestors, create a map of them now.
var attestors map[peer.ID]struct{}
if len(want.Attestors.Each) > 0 || len(want.Attestors.OneOf) > 0 {
attestors = make(map[peer.ID]struct{}, len(have.Attestors))
for _, attestor := range have.Attestors {
attestors[attestor.Signer] = struct{}{}
}
}

for _, wa := range want.Attestors {
// If the client wants specific attestors, check if they're present.
if len(want.Attestors.Each) > 0 {
for _, wa := range want.Attestors.Each {
_, ok := attestors[wa]
if !ok {
return fmt.Errorf("attestor %s explicitly requested but not found", wa.String())
}
}
}

// If the client wants some of these attestors, check if at least one if found.
if len(want.Attestors.OneOf) > 0 {
var found bool
for _, wa := range want.Attestors.OneOf {
_, ok := attestors[wa]
if ok {
found = true
break
}
}
if !found {
return fmt.Errorf("at least one attestor wanted but none found (wanted: %s)", blockless.PeerIDsToStr(want.Attestors.OneOf))
}
}

// It doesn't make a lot of sense to require attestors without wanting specific attributes,
// but if that's the case, and there's no attributes wanted, we're done now.
if len(want.Values) == 0 {
Expand Down

0 comments on commit 698f420

Please sign in to comment.