frontend: implement GetLatestBlock, GetBlock; refactoring
This commit is contained in:
63
frontend/log.go
Normal file
63
frontend/log.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/peer"
|
||||
)
|
||||
|
||||
var log *logrus.Entry
|
||||
var logger = logrus.New()
|
||||
|
||||
func init() {
|
||||
logger.SetFormatter(&logrus.TextFormatter{
|
||||
//DisableColors: true,
|
||||
FullTimestamp: true,
|
||||
DisableLevelTruncation: true,
|
||||
})
|
||||
|
||||
log = logger.WithFields(logrus.Fields{
|
||||
"app": "frontend-grpc",
|
||||
})
|
||||
}
|
||||
|
||||
func LoggingInterceptor() grpc.ServerOption {
|
||||
return grpc.UnaryInterceptor(logInterceptor)
|
||||
}
|
||||
|
||||
func logInterceptor(
|
||||
ctx context.Context,
|
||||
req interface{},
|
||||
info *grpc.UnaryServerInfo,
|
||||
handler grpc.UnaryHandler,
|
||||
) (interface{}, error) {
|
||||
reqLog := loggerFromContext(ctx)
|
||||
start := time.Now()
|
||||
|
||||
resp, err := handler(ctx, req)
|
||||
|
||||
entry := reqLog.WithFields(logrus.Fields{
|
||||
"method": info.FullMethod,
|
||||
"duration": time.Since(start),
|
||||
"error": err,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
entry.Error("call failed")
|
||||
} else {
|
||||
entry.Info("method called")
|
||||
}
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func loggerFromContext(ctx context.Context) *logrus.Entry {
|
||||
// TODO: anonymize the addresses. cryptopan?
|
||||
if peerInfo, ok := peer.FromContext(ctx); ok {
|
||||
return log.WithFields(logrus.Fields{"peer_addr": peerInfo.Addr})
|
||||
}
|
||||
return log.WithFields(logrus.Fields{"peer_addr": "unknown"})
|
||||
}
|
||||
78
frontend/service.go
Normal file
78
frontend/service.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"github.com/gtank/ctxd/rpc"
|
||||
"github.com/gtank/ctxd/storage"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoImpl = errors.New("not yet implemented")
|
||||
ErrUnspecified = errors.New("request for unspecified identifier")
|
||||
)
|
||||
|
||||
// the service type
|
||||
type sqlStreamer struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewSQLiteStreamer(dbPath string) (rpc.CompactTxStreamerServer, error) {
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Creates our tables if they don't already exist.
|
||||
err = storage.CreateTables(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &sqlStreamer{db}, nil
|
||||
}
|
||||
|
||||
func (s *sqlStreamer) GetLatestBlock(ctx context.Context, placeholder *rpc.ChainSpec) (*rpc.BlockID, error) {
|
||||
// the ChainSpec type is an empty placeholder
|
||||
height, err := storage.GetCurrentHeight(ctx, s.db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO: also return block hashes here
|
||||
return &rpc.BlockID{Height: uint64(height)}, nil
|
||||
}
|
||||
|
||||
func (s *sqlStreamer) GetBlock(ctx context.Context, id *rpc.BlockID) (*rpc.CompactBlock, error) {
|
||||
if id.Height == 0 && id.Hash == nil {
|
||||
return nil, ErrUnspecified
|
||||
}
|
||||
|
||||
// Precedence: a hash is more specific than a height. If we have it, use it first.
|
||||
if id.Hash != nil {
|
||||
leHashString := hex.EncodeToString(id.Hash)
|
||||
return storage.GetBlockByHash(ctx, s.db, leHashString)
|
||||
}
|
||||
|
||||
// we have a height and not a hash
|
||||
if int(id.Height) > 0 {
|
||||
return storage.GetBlock(ctx, s.db, int(id.Height))
|
||||
}
|
||||
|
||||
return nil, ErrUnspecified
|
||||
}
|
||||
|
||||
func (s *sqlStreamer) GetBlockRange(*rpc.BlockRange, rpc.CompactTxStreamer_GetBlockRangeServer) error {
|
||||
return ErrNoImpl
|
||||
}
|
||||
|
||||
func (s *sqlStreamer) GetTransaction(context.Context, *rpc.TxFilter) (*rpc.RawTransaction, error) {
|
||||
return nil, ErrNoImpl
|
||||
}
|
||||
func (s *sqlStreamer) SendTransaction(context.Context, *rpc.RawTransaction) (*rpc.SendResponse, error) {
|
||||
return nil, ErrNoImpl
|
||||
}
|
||||
Reference in New Issue
Block a user