Skip to content

Commit

Permalink
validation troolean in coda_net2
Browse files Browse the repository at this point in the history
  • Loading branch information
emberian committed Sep 30, 2020
1 parent d3d3ea0 commit 764c81e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
31 changes: 19 additions & 12 deletions src/app/libp2p_helper/src/libp2p_helper/main.go
Expand Up @@ -43,7 +43,7 @@ type subscription struct {
}

type validationStatus struct {
Completion chan bool
Completion chan string
TimedOutAt *time.Time
}

Expand Down Expand Up @@ -365,15 +365,15 @@ func (s *subscribeMsg) run(app *app) (interface{}, error) {
return nil, needsDHT()
}
app.P2p.Pubsub.Join(s.Topic)
err := app.P2p.Pubsub.RegisterTopicValidator(s.Topic, func(ctx context.Context, id peer.ID, msg *pubsub.Message) bool {
err := app.P2p.Pubsub.RegisterTopicValidator(s.Topic, func(ctx context.Context, id peer.ID, msg *pubsub.Message) pubsub.ValidationResult {
if id == app.P2p.Me {
// messages from ourself are valid.
app.P2p.Logger.Info("would have validated but it's from us!")
return true
return pubsub.ValidationAccept
}

seqno := <-seqs
ch := make(chan bool, 1)
ch := make(chan string, 1)
app.ValidatorMutex.Lock()
app.Validators[seqno] = new(validationStatus)
(*app.Validators[seqno]).Completion = ch
Expand All @@ -388,7 +388,7 @@ func (s *subscribeMsg) run(app *app) (interface{}, error) {
app.ValidatorMutex.Lock()
defer app.ValidatorMutex.Unlock()
delete(app.Validators, seqno)
return false
return pubsub.ValidationIgnore
}

app.writeMsg(validateUpcall{
Expand Down Expand Up @@ -417,17 +417,24 @@ func (s *subscribeMsg) run(app *app) (interface{}, error) {

if app.UnsafeNoTrustIP {
app.P2p.Logger.Info("validated anyway!")
return true
return pubsub.ValidationAccept
}
app.P2p.Logger.Info("unvalidated :(")
return false
return pubsub.ValidationReject
case res := <-ch:
if !res {
switch res {
case "reject":
app.P2p.Logger.Info("why u fail to validate :(")
} else {
return pubsub.ValidationReject
case "accept":
app.P2p.Logger.Info("validated!")
return pubsub.ValidationAccept
case "ignore":
app.P2p.Logger.Info("ignoring valid message!")
return pubsub.ValidationIgnore
}
return res
app.P2p.Logger.Info("ignoring message that falled off the end!")
return pubsub.ValidationIgnore
}
}, pubsub.WithValidatorTimeout(validationTimeout))

Expand Down Expand Up @@ -503,8 +510,8 @@ type validateUpcall struct {
}

type validationCompleteMsg struct {
Seqno int `json:"seqno"`
Valid bool `json:"is_valid"`
Seqno int `json:"seqno"`
Valid string `json:"is_valid"`
}

func (r *validationCompleteMsg) run(app *app) (interface{}, error) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/coda_net2/coda_net2.ml
Expand Up @@ -324,7 +324,7 @@ module Helper = struct
end

module Validation_complete = struct
type input = {seqno: int; is_valid: bool} [@@deriving yojson]
type input = {seqno: int; action: string} [@@deriving yojson]

type output = string [@@deriving yojson]

Expand Down Expand Up @@ -771,7 +771,7 @@ module Helper = struct
(let open Deferred.Let_syntax in
let raw_data = Data.to_string m.data in
let decoded = sub.decode raw_data in
let%bind is_valid =
let%bind action =
match decoded with
| Ok data ->
sub.validator (wrap m.sender data)
Expand All @@ -791,7 +791,7 @@ module Helper = struct
return false
in
match%map
do_rpc t (module Rpcs.Validation_complete) {seqno; is_valid}
do_rpc t (module Rpcs.Validation_complete) {seqno; is_valid= match is_valid with `Accept -> "accept" | `Reject -> "reject" | `Ignore -> "ignore" }
with
| Ok "validationComplete success" ->
()
Expand Down
2 changes: 1 addition & 1 deletion src/lib/coda_net2/coda_net2.mli
Expand Up @@ -142,7 +142,7 @@ module Pubsub : sig
val subscribe :
net
-> string
-> should_forward_message:(string Envelope.Incoming.t -> bool Deferred.t)
-> should_forward_message:(string Envelope.Incoming.t -> [`Accept|`Reject|`Ignore] Deferred.t)
-> string Subscription.t Deferred.Or_error.t

(** Like [subscribe], but knows how to stringify/destringify
Expand Down

0 comments on commit 764c81e

Please sign in to comment.