-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(client/v2): broadcast logic #22282
Open
JulianToledano
wants to merge
20
commits into
main
Choose a base branch
from
julian/client-v2-broadcast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+688
−20
Open
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cb00e03
add: broadcast logic
JulianToledano 2025987
lint :)
JulianToledano 015eab4
Merge branch 'main' into julian/client-v2-broadcast
JulianToledano 39b5126
fix: make sure interfaces are implemented
JulianToledano 49598dd
del: server/v2 dependency
JulianToledano 8656f1f
fix: tests
JulianToledano b33e5a2
CHANGELOG
JulianToledano c13fe79
update: factory register
JulianToledano 4bd1fbb
update: factory and types public
JulianToledano bee57fa
update: mock
JulianToledano b0094f3
Merge branch 'main' into julian/client-v2-broadcast
JulianToledano 71e1c4a
./scripts/go-mod-tidy-all.sh
JulianToledano dc9e1f4
make mocks
JulianToledano ea3ffb8
update: rabbit enablements
JulianToledano cb472db
fix: create makes use of context
JulianToledano 4ecd77f
update: use broadcaster in v2/tx
JulianToledano 00fe5a3
update: remove factory + public
JulianToledano 86b8d58
lint + typo
JulianToledano c840773
lint :/
JulianToledano 5b3db1e
update: rabbit enhancements
JulianToledano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package broadcast | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"cosmossdk.io/client/v2/broadcast/types" | ||
"cosmossdk.io/client/v2/internal/comet" | ||
) | ||
|
||
var _ types.BroadcasterFactory = &Factory{} | ||
|
||
// Factory is a factory for creating Broadcaster instances. | ||
type Factory struct { | ||
engines map[string]types.NewBroadcasterFn | ||
} | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Create creates a new Broadcaster based on the given consensus type. | ||
func (f *Factory) Create(_ context.Context, consensus, url string, opts ...types.Option) (types.Broadcaster, error) { | ||
JulianToledano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
creator, ok := f.engines[consensus] | ||
if !ok { | ||
return nil, fmt.Errorf("invalid consensus type: %s", consensus) | ||
} | ||
return creator(url, opts...) | ||
} | ||
|
||
// Register adds a new BroadcasterCreator for a given consensus type to the factory. | ||
func (f *Factory) Register(consensus string, creator types.NewBroadcasterFn) { | ||
f.engines[consensus] = creator | ||
} | ||
|
||
// NewFactory creates and returns a new Factory instance with a default CometBFT broadcaster. | ||
func NewFactory() Factory { | ||
JulianToledano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Factory{ | ||
engines: map[string]types.NewBroadcasterFn{ | ||
"comet": func(url string, opts ...types.Option) (types.Broadcaster, error) { | ||
return comet.NewCometBFTBroadcaster(url, opts...) | ||
}, | ||
}, | ||
} | ||
} |
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,88 @@ | ||
package broadcast | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"cosmossdk.io/client/v2/broadcast/types" | ||
"cosmossdk.io/client/v2/internal/comet" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec/testutil" | ||
) | ||
|
||
type testBFT struct{} | ||
|
||
func (t testBFT) Broadcast(_ context.Context, _ []byte) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
func (t testBFT) Consensus() string { | ||
return "testBFT" | ||
} | ||
|
||
func Test_newBroadcaster(t *testing.T) { | ||
f := NewFactory() | ||
tests := []struct { | ||
name string | ||
consensus string | ||
opts []types.Option | ||
want types.Broadcaster | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "comet", | ||
consensus: "comet", | ||
opts: []types.Option{ | ||
comet.WithJsonCodec(testutil.CodecOptions{}.NewCodec()), | ||
}, | ||
want: &comet.CometBFTBroadcaster{}, | ||
}, | ||
{ | ||
name: "unsupported_consensus", | ||
consensus: "unsupported", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := f.Create(context.Background(), tt.consensus, "localhost:26657", tt.opts...) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.NotNil(t, got) | ||
require.IsType(t, tt.want, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFactory_Register(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
consensus string | ||
creator types.NewBroadcasterFn | ||
}{ | ||
{ | ||
name: "register new broadcaster", | ||
consensus: "testBFT", | ||
creator: func(url string, opts ...types.Option) (types.Broadcaster, error) { | ||
return testBFT{}, nil | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f := NewFactory() | ||
f.Register(tt.consensus, tt.creator) | ||
|
||
b, err := f.Create(context.Background(), tt.consensus, "localhost:26657") | ||
require.NoError(t, err) | ||
require.NotNil(t, b) | ||
|
||
require.Equal(t, tt.consensus, b.Consensus()) | ||
}) | ||
} | ||
} |
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,32 @@ | ||
package types | ||
|
||
import "context" | ||
|
||
type ( | ||
// Broadcaster defines an interface for broadcasting transactions to the consensus engine. | ||
Broadcaster interface { | ||
// Broadcast sends a transaction to the network and returns the result. | ||
// | ||
// It returns a byte slice containing the formatted result that will be | ||
// passed to the output writer, and an error if the broadcast failed. | ||
Broadcast(ctx context.Context, txBytes []byte) ([]byte, error) | ||
|
||
// Consensus returns the consensus engine identifier for this Broadcaster. | ||
Consensus() string | ||
} | ||
|
||
// NewBroadcasterFn is a function type for creating Broadcaster instances. | ||
NewBroadcasterFn func(url string, opts ...Option) (Broadcaster, error) | ||
|
||
// BroadcasterFactory defines an interface for creating and registering Broadcasters. | ||
BroadcasterFactory interface { | ||
// Register adds a new BroadcasterCreator for a given consensus type to the factory. | ||
Register(consensus string, creator NewBroadcasterFn) | ||
// Create instantiates a new Broadcaster based on the given consensus type, URL, and options. | ||
// It returns the created Broadcaster and any error encountered during creation. | ||
Create(ctx context.Context, consensus, url string, opts ...Option) (Broadcaster, error) | ||
} | ||
|
||
// Option is a function that configures a Broadcaster. | ||
Option func(Broadcaster) | ||
) |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it, maybe we don't need a factory at all. When would you require multiple broadcasters? This adds another complexity layer