-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
193 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mock | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Layr-Labs/eigenda/api/clients" | ||
corev2 "github.com/Layr-Labs/eigenda/core/v2" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockNodeClientV2 struct { | ||
mock.Mock | ||
} | ||
|
||
var _ clients.NodeClientV2 = (*MockNodeClientV2)(nil) | ||
|
||
func NewNodeClientV2() *MockNodeClientV2 { | ||
return &MockNodeClientV2{} | ||
} | ||
|
||
func (c *MockNodeClientV2) StoreChunks(ctx context.Context, certs []*corev2.BlobCertificate) ([][]byte, error) { | ||
args := c.Called() | ||
var signatures [][]byte | ||
if args.Get(0) != nil { | ||
signatures = (args.Get(0)).([][]byte) | ||
} | ||
return signatures, args.Error(1) | ||
} | ||
|
||
func (c *MockNodeClientV2) Close() error { | ||
args := c.Called() | ||
return args.Error(0) | ||
} |
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,104 @@ | ||
package clients | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
commonpb "github.com/Layr-Labs/eigenda/api/grpc/common/v2" | ||
nodegrpc "github.com/Layr-Labs/eigenda/api/grpc/node/v2" | ||
corev2 "github.com/Layr-Labs/eigenda/core/v2" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type NodeClientV2Config struct { | ||
Hostname string | ||
Port string | ||
UseSecureGrpcFlag bool | ||
} | ||
|
||
type NodeClientV2 interface { | ||
StoreChunks(ctx context.Context, certs []*corev2.BlobCertificate) (signatures [][]byte, err error) | ||
Close() error | ||
} | ||
|
||
type nodeClientV2 struct { | ||
config *NodeClientV2Config | ||
initOnce sync.Once | ||
conn *grpc.ClientConn | ||
|
||
dispersalClient nodegrpc.DispersalClient | ||
} | ||
|
||
var _ NodeClientV2 = (*nodeClientV2)(nil) | ||
|
||
func NewNodeClientV2(config *NodeClientV2Config) (*nodeClientV2, error) { | ||
if config == nil || config.Hostname == "" || config.Port == "" { | ||
return nil, fmt.Errorf("invalid config: %v", config) | ||
} | ||
return &nodeClientV2{ | ||
config: config, | ||
}, nil | ||
} | ||
|
||
func (c *nodeClientV2) StoreChunks(ctx context.Context, certs []*corev2.BlobCertificate) (signatures [][]byte, err error) { | ||
if err = c.initOnceGrpcConnection(); err != nil { | ||
return nil, err | ||
} | ||
|
||
blobCerts := make([]*commonpb.BlobCertificate, len(certs)) | ||
for i, cert := range certs { | ||
blobCerts[i], err = cert.ToProtobuf() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert blob certificate to protobuf: %v", err) | ||
} | ||
} | ||
|
||
// Call the gRPC method to store chunks | ||
response, err := c.dispersalClient.StoreChunks(ctx, &nodegrpc.StoreChunksRequest{ | ||
BlobCertificates: blobCerts, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Extract signatures from the response | ||
if response == nil { | ||
return nil, fmt.Errorf("received nil response from StoreChunks") | ||
} | ||
|
||
signatures = make([][]byte, len(certs)) | ||
for i, sig := range response.GetSignatures() { | ||
signatures[i] = sig.GetValue() | ||
} | ||
|
||
return signatures, nil | ||
} | ||
|
||
// Close closes the grpc connection to the disperser server. | ||
// It is thread safe and can be called multiple times. | ||
func (c *nodeClientV2) Close() error { | ||
if c.conn != nil { | ||
err := c.conn.Close() | ||
c.conn = nil | ||
c.dispersalClient = nil | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *nodeClientV2) initOnceGrpcConnection() error { | ||
var initErr error | ||
c.initOnce.Do(func() { | ||
addr := fmt.Sprintf("%v:%v", c.config.Hostname, c.config.Port) | ||
dialOptions := getGrpcDialOptions(c.config.UseSecureGrpcFlag) | ||
conn, err := grpc.Dial(addr, dialOptions...) | ||
if err != nil { | ||
initErr = err | ||
return | ||
} | ||
c.conn = conn | ||
c.dispersalClient = nodegrpc.NewDispersalClient(conn) | ||
}) | ||
return initErr | ||
} |
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