-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rpc): fraud.Proof (un)marshalling and subscription as chan
- Loading branch information
1 parent
9c996a7
commit 6f75e7d
Showing
10 changed files
with
190 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package fraud | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/libp2p/go-libp2p-core/host" | ||
pubsub "github.com/libp2p/go-libp2p-pubsub" | ||
"go.uber.org/fx" | ||
|
||
"github.com/celestiaorg/celestia-node/fraud" | ||
"github.com/celestiaorg/celestia-node/header" | ||
"github.com/celestiaorg/celestia-node/nodebuilder/p2p" | ||
) | ||
|
||
// NewModule constructs a fraud proof service with the syncer disabled. | ||
func NewModule( | ||
lc fx.Lifecycle, | ||
sub *pubsub.PubSub, | ||
host host.Host, | ||
hstore header.Store, | ||
ds datastore.Batching, | ||
network p2p.Network, | ||
) (Module, fraud.Service, error) { | ||
return newFraudService(lc, sub, host, hstore, ds, false, string(network)) | ||
} | ||
|
||
// ModuleWithSyncer constructs fraud proof service with enabled syncer. | ||
func ModuleWithSyncer( | ||
lc fx.Lifecycle, | ||
sub *pubsub.PubSub, | ||
host host.Host, | ||
hstore header.Store, | ||
ds datastore.Batching, | ||
network p2p.Network, | ||
) (Module, fraud.Service, error) { | ||
return newFraudService(lc, sub, host, hstore, ds, true, string(network)) | ||
} | ||
|
||
func newFraudService( | ||
lc fx.Lifecycle, | ||
sub *pubsub.PubSub, | ||
host host.Host, | ||
hstore header.Store, | ||
ds datastore.Batching, | ||
isEnabled bool, | ||
protocolSuffix string, | ||
) (Module, fraud.Service, error) { | ||
pservice := fraud.NewProofService(sub, host, hstore.GetByHeight, ds, isEnabled, protocolSuffix) | ||
lc.Append(fx.Hook{ | ||
OnStart: pservice.Start, | ||
OnStop: pservice.Stop, | ||
}) | ||
return &Service{ | ||
Service: pservice, | ||
}, pservice, nil | ||
} | ||
|
||
// Lifecycle controls the lifecycle of service depending on fraud proofs. | ||
// It starts the service only if no fraud-proof exists and stops the service automatically | ||
// if a proof arrives after the service was started. | ||
func Lifecycle( | ||
startCtx, lifecycleCtx context.Context, | ||
p fraud.ProofType, | ||
fraudModule fraud.Service, | ||
start, stop func(context.Context) error, | ||
) error { | ||
proofs, err := fraudModule.Get(startCtx, p) | ||
switch err { | ||
default: | ||
return err | ||
case nil: | ||
return &fraud.ErrFraudExists{Proof: proofs} | ||
case datastore.ErrNotFound: | ||
} | ||
err = start(startCtx) | ||
if err != nil { | ||
return err | ||
} | ||
// handle incoming Fraud Proofs | ||
go fraud.OnProof(lifecycleCtx, fraudModule, p, func(fraud.Proof) { | ||
if err := stop(lifecycleCtx); err != nil { | ||
log.Error(err) | ||
} | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.