bdledger-apis/test/bdchain/api/mockserver/server.go
2018-10-10 11:56:23 +08:00

192 lines
4.0 KiB
Go

package main
import (
"bytes"
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"bdchain/api/grpc/common"
"bdchain/api/grpc/txledger"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/empty"
)
/////////////////////////////////////////////////////////////////////////////
// server
type server struct{}
/////////////////////////////////////////////////////////////////////////////
// grpcServer
type grpcServer server
func (s *grpcServer) ClientVersion(
c context.Context,
r *empty.Empty,
) (res *common.ClientVersionResponse, err error) {
switch {
case s == nil:
err = toStatusError(ErrInvalidServer)
case r == nil:
err = toStatusError(ErrInvalidRequest)
default:
res = &common.ClientVersionResponse{
Version: "TxLedgerGo/v0.0.1alpha/darwin/go1.11",
}
}
return
}
func (s *grpcServer) CreateLedger(
c context.Context,
r *txledger.CreateLedgerRequest,
) (res *txledger.CreateLedgerResponse, err error) {
switch {
case s == nil:
err = toStatusError(ErrInvalidServer)
case r == nil:
err = toStatusError(ErrInvalidRequest)
case r.Name == "":
err = toStatusError(ErrEmptyName)
case r.Name == "test":
res = &txledger.CreateLedgerResponse{
Ok: true,
}
default:
err = toStatusError(ErrTestNotImplemented)
}
return
}
func (s *grpcServer) GetLedgers(
c context.Context,
r *empty.Empty,
) (res *txledger.GetLedgersResponse, err error) {
switch {
case s == nil:
err = toStatusError(ErrInvalidServer)
case r == nil:
err = toStatusError(ErrInvalidRequest)
default:
res = &txledger.GetLedgersResponse{
Ledgers: []string{
"first",
"second",
"third",
},
}
}
return
}
func (s *grpcServer) SendTransaction(
c context.Context,
r *txledger.SendTransactionRequest,
) (res *txledger.SendTransactionResponse, err error) {
switch {
case s == nil:
err = toStatusError(ErrInvalidServer)
case r == nil:
fallthrough
case r.Transaction == nil:
err = toStatusError(ErrInvalidRequest)
case r.Ledger != "test":
err = toStatusError(ErrTestNotImplemented)
default:
type Input struct {
Type common.TransactionType
From []byte
To []byte
Data []byte
}
type Output struct {
Hash []byte
Error error
}
type Case struct {
I Input
O Output
}
Merge := func(
p *status.Status,
cs ...*status.Status,
) *status.Status {
msg := []proto.Message{}
for _, c := range cs {
msg = append(msg, c.Proto())
}
r, err := p.WithDetails(msg...)
if err != nil {
panic(err)
}
return r
}
tx := r.Transaction
for _, c := range [...]Case{
{
I: Input{
Type: common.TransactionType_MESSAGE,
From: []byte{
0xf0, 0x0d, 0xca, 0xfe, 0xf0, 0x0d, 0xca, 0xfe,
0xf0, 0x0d, 0xca, 0xfe, 0xf0, 0x0d, 0xca, 0xfe,
0xf0, 0x0d, 0xca, 0xfe,
},
To: []byte{
0xfe, 0xed, 0xba, 0xbe, 0xfe, 0xed, 0xba, 0xbe,
0xfe, 0xed, 0xba, 0xbe, 0xfe, 0xed, 0xba, 0xbe,
0xfe, 0xed, 0xba, 0xbe,
},
Data: []byte{
0xde, 0xad, 0xbe, 0xef,
},
},
O: Output{
Hash: []byte{
0xd1, 0x5e, 0xa5, 0xed, 0xd1, 0x5e, 0xa5, 0xed,
0xd1, 0x5e, 0xa5, 0xed, 0xd1, 0x5e, 0xa5, 0xed,
0xd1, 0x5e, 0xa5, 0xed, 0xd1, 0x5e, 0xa5, 0xed,
0xd1, 0x5e, 0xa5, 0xed, 0xd1, 0x5e, 0xa5, 0xed,
},
Error: nil,
},
},
{
I: Input{
Type: common.TransactionType_MESSAGE,
From: nil,
To: []byte{
0x50, 0xba, 0xda, 0x55,
},
Data: nil,
},
O: Output{
Hash: nil,
Error: Merge(
status.New(codes.InvalidArgument, ErrMultiple.Error()),
status.New(codes.InvalidArgument, ErrEmptyFrom.Error()),
status.New(codes.InvalidArgument, ErrInvalidTo.Error()),
).Err(),
},
},
} {
if c.I.Type == tx.Type &&
bytes.Equal(c.I.From, tx.From) &&
bytes.Equal(c.I.To, tx.To) &&
bytes.Equal(c.I.Data, tx.Data) {
if c.O.Error != nil {
err = c.O.Error
} else {
res = &txledger.SendTransactionResponse{Hash: c.O.Hash}
}
return
}
}
err = toStatusError(ErrTestNotImplemented)
}
return
}