+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/checkproto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

- name: Set up protoc
run: |
PROTOC_VERSION=27.3
PROTOC_VERSION=28.1
PROTOC_GEN_VERSION=v1.34.2
PROTOC_GRPC_VERSION=v1.4.0

Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
build

build
cmd/manager/img

.cov

*.pem

dist/
results.zip
*.zip
*.spec
*.tar

Expand Down
10 changes: 5 additions & 5 deletions agent/agent.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agent/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ service AgentService {
rpc Algo(stream AlgoRequest) returns (AlgoResponse) {}
rpc Data(stream DataRequest) returns (DataResponse) {}
rpc Result(ResultRequest) returns (stream ResultResponse) {}
rpc Attestation(AttestationRequest) returns (AttestationResponse) {}
rpc Attestation(AttestationRequest) returns (stream AttestationResponse) {}
}

message AlgoRequest {
Expand Down
86 changes: 56 additions & 30 deletions agent/agent_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 39 additions & 7 deletions agent/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
"bytes"
"context"
"errors"
"fmt"
"io"

"github.com/go-kit/kit/transport/grpc"
"github.com/ultravioletrs/cocos/agent"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

const bufferSize = 1024 * 1024
const (
bufferSize = 1024 * 1024
FileSizeKey = "file-size"
)

var _ agent.AgentServiceServer = (*grpcServer)(nil)

Expand Down Expand Up @@ -156,12 +161,16 @@
}
rr := res.(*agent.ResultResponse)

reusltBuffer := bytes.NewBuffer(rr.File)
if err := stream.SetHeader(metadata.New(map[string]string{FileSizeKey: fmt.Sprint(len(rr.File))})); err != nil {
return status.Error(codes.Internal, err.Error())
}

Check warning on line 166 in agent/api/grpc/server.go

View check run for this annotation

Codecov / codecov/patch

agent/api/grpc/server.go#L165-L166

Added lines #L165 - L166 were not covered by tests

resultBuffer := bytes.NewBuffer(rr.File)

buf := make([]byte, bufferSize)

for {
n, err := reusltBuffer.Read(buf)
n, err := resultBuffer.Read(buf)
if err == io.EOF {
break
}
Expand All @@ -177,11 +186,34 @@
return nil
}

func (s *grpcServer) Attestation(ctx context.Context, req *agent.AttestationRequest) (*agent.AttestationResponse, error) {
_, res, err := s.attestation.ServeGRPC(ctx, req)
func (s *grpcServer) Attestation(req *agent.AttestationRequest, stream agent.AgentService_AttestationServer) error {
_, res, err := s.attestation.ServeGRPC(stream.Context(), req)
if err != nil {
return nil, err
return err

Check warning on line 192 in agent/api/grpc/server.go

View check run for this annotation

Codecov / codecov/patch

agent/api/grpc/server.go#L192

Added line #L192 was not covered by tests
}
rr := res.(*agent.AttestationResponse)
return rr, nil

if err := stream.SetHeader(metadata.New(map[string]string{FileSizeKey: fmt.Sprint(len(rr.File))})); err != nil {
return status.Error(codes.Internal, err.Error())
}

Check warning on line 198 in agent/api/grpc/server.go

View check run for this annotation

Codecov / codecov/patch

agent/api/grpc/server.go#L197-L198

Added lines #L197 - L198 were not covered by tests

attestationBuffer := bytes.NewBuffer(rr.File)

buf := make([]byte, bufferSize)

for {
n, err := attestationBuffer.Read(buf)
if err == io.EOF {
break
}
if err != nil {
return status.Error(codes.Internal, err.Error())
}

Check warning on line 211 in agent/api/grpc/server.go

View check run for this annotation

Codecov / codecov/patch

agent/api/grpc/server.go#L210-L211

Added lines #L210 - L211 were not covered by tests

if err := stream.Send(&agent.AttestationResponse{File: buf[:n]}); err != nil {
return status.Error(codes.Internal, err.Error())
}

Check warning on line 215 in agent/api/grpc/server.go

View check run for this annotation

Codecov / codecov/patch

agent/api/grpc/server.go#L214-L215

Added lines #L214 - L215 were not covered by tests
}

return nil
}
30 changes: 28 additions & 2 deletions agent/api/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ultravioletrs/cocos/agent"
"github.com/ultravioletrs/cocos/agent/mocks"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

type MockAgentService_AlgoServer struct {
Expand Down Expand Up @@ -64,11 +65,34 @@ func (m *MockAgentService_ResultServer) Context() context.Context {
return m.ctx
}

func (m *MockAgentService_ResultServer) SetHeader(metadata.MD) error {
return nil
}

func (m *MockAgentService_ResultServer) Send(resp *agent.ResultResponse) error {
args := m.Called(resp)
return args.Error(0)
}

type MockAgentService_AttestationServer struct {
grpc.ServerStream
mock.Mock
ctx context.Context
}

func (m *MockAgentService_AttestationServer) Context() context.Context {
return m.ctx
}

func (m *MockAgentService_AttestationServer) Send(resp *agent.AttestationResponse) error {
args := m.Called(resp)
return args.Error(0)
}

func (m *MockAgentService_AttestationServer) SetHeader(metadata.MD) error {
return nil
}

func TestAlgo(t *testing.T) {
mockService := new(mocks.Service)
server := NewServer(mockService)
Expand Down Expand Up @@ -124,12 +148,14 @@ func TestAttestation(t *testing.T) {
mockService := new(mocks.Service)
server := NewServer(mockService)

mockStream := &MockAgentService_AttestationServer{ctx: context.Background()}
mockStream.On("Send", mock.AnythingOfType("*agent.AttestationResponse")).Return(nil)

reportData := [agent.ReportDataSize]byte{}
mockService.On("Attestation", mock.Anything, reportData).Return([]byte("attestation data"), nil)

resp, err := server.Attestation(context.Background(), &agent.AttestationRequest{ReportData: reportData[:]})
err := server.Attestation(&agent.AttestationRequest{ReportData: reportData[:]}, mockStream)
assert.NoError(t, err)
assert.Equal(t, []byte("attestation data"), resp.File)

mockService.AssertExpectations(t)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/manager.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/manager/manager_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载