-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add external subscriber + update morpheuscli rebase main * add external subscriber tests * address cr nits * remove chainID, networkID from initialization message * nit * undo snowctx diff * nit * clean up integration tests + nits * nits * Update external subscriber log to use zap.Uint64 instead of Any
- Loading branch information
1 parent
2aaa5b4
commit 802f358
Showing
18 changed files
with
870 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,8 +27,6 @@ awscpu | |
.vscode* | ||
.env | ||
|
||
*.pb* | ||
|
||
# db* | ||
|
||
*cpu[0-9]* | ||
|
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
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,82 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package externalsubscriber | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ava-labs/avalanchego/utils/logging" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/ava-labs/hypersdk/chain" | ||
"github.com/ava-labs/hypersdk/event" | ||
|
||
pb "github.com/ava-labs/hypersdk/proto/pb/externalsubscriber" | ||
) | ||
|
||
var _ event.Subscription[*chain.StatelessBlock] = (*ExternalSubscriberClient)(nil) | ||
|
||
type ExternalSubscriberClient struct { | ||
conn *grpc.ClientConn | ||
client pb.ExternalSubscriberClient | ||
log logging.Logger | ||
} | ||
|
||
func NewExternalSubscriberClient( | ||
ctx context.Context, | ||
log logging.Logger, | ||
serverAddr string, | ||
genesisBytes []byte, | ||
) (*ExternalSubscriberClient, error) { | ||
// Establish connection to server | ||
conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := pb.NewExternalSubscriberClient(conn) | ||
// Initialize parser | ||
_, err = client.Initialize( | ||
ctx, | ||
&pb.InitializeRequest{ | ||
Genesis: genesisBytes, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
log.Debug("connected to external subscriber server", zap.String("address", serverAddr)) | ||
return &ExternalSubscriberClient{ | ||
conn: conn, | ||
client: client, | ||
log: log, | ||
}, nil | ||
} | ||
|
||
func (e *ExternalSubscriberClient) Accept(blk *chain.StatelessBlock) error { | ||
blockBytes, err := blk.Marshal() | ||
if err != nil { | ||
return err | ||
} | ||
resultsMarshaled, err := chain.MarshalResults(blk.Results()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req := &pb.BlockRequest{ | ||
BlockData: blockBytes, | ||
Results: resultsMarshaled, | ||
} | ||
e.log.Debug("sending accepted block to server", | ||
zap.Stringer("blockID", blk.ID()), | ||
zap.Uint64("blockHeight", blk.Hght), | ||
) | ||
_, err = e.client.AcceptBlock(context.TODO(), req) | ||
return err | ||
} | ||
|
||
func (e *ExternalSubscriberClient) Close() error { | ||
return e.conn.Close() | ||
} |
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,60 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package externalsubscriber | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/ava-labs/hypersdk/chain" | ||
"github.com/ava-labs/hypersdk/event" | ||
"github.com/ava-labs/hypersdk/vm" | ||
) | ||
|
||
const ( | ||
Namespace = "externalSubscriber" | ||
) | ||
|
||
type Config struct { | ||
Enabled bool `json:"enabled"` | ||
ServerAddress string `json:"serverAddress"` | ||
} | ||
|
||
func NewDefaultConfig() Config { | ||
return Config{} | ||
} | ||
|
||
func With() vm.Option { | ||
return vm.NewOption(Namespace, OptionFunc) | ||
} | ||
|
||
func OptionFunc(v *vm.VM, configBytes []byte) error { | ||
config := NewDefaultConfig() | ||
if len(configBytes) > 0 { | ||
if err := json.Unmarshal(configBytes, &config); err != nil { | ||
return err | ||
} | ||
} | ||
if !config.Enabled { | ||
return nil | ||
} | ||
server, err := NewExternalSubscriberClient( | ||
context.TODO(), | ||
v.Logger(), | ||
config.ServerAddress, | ||
v.GenesisBytes, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
blockSubscription := event.SubscriptionFuncFactory[*chain.StatelessBlock]{ | ||
AcceptF: func(blk *chain.StatelessBlock) error { | ||
return server.Accept(blk) | ||
}, | ||
} | ||
|
||
vm.WithBlockSubscriptions(blockSubscription)(v) | ||
return nil | ||
} |
Oops, something went wrong.