Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: code tidy up #155

Merged
merged 5 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions cmd/storage_provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bnb-chain/greenfield-storage-provider/config"
"github.com/bnb-chain/greenfield-storage-provider/model"
"github.com/bnb-chain/greenfield-storage-provider/pkg/lifecycle"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/service/blocksyncer"
"github.com/bnb-chain/greenfield-storage-provider/service/challenge"
"github.com/bnb-chain/greenfield-storage-provider/service/downloader"
Expand All @@ -19,12 +20,11 @@ import (
"github.com/bnb-chain/greenfield-storage-provider/service/stonenode"
"github.com/bnb-chain/greenfield-storage-provider/service/syncer"
"github.com/bnb-chain/greenfield-storage-provider/service/uploader"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
)

var (
version = flag.Bool("version", false, "print version")
configFile = flag.String("config", "../../config/config.toml", "config file path")
configFile = flag.String("config", "./config.toml", "config file path")
)

// initService init service instance by name and config.
Expand Down Expand Up @@ -119,10 +119,10 @@ func main() {
// 1. init service instance.
service, err := initService(serviceName, cfg)
if err != nil {
log.Errorw("init service failed", "service_name", serviceName, "error", err)
log.Errorw("failed to init service", "service", serviceName, "error", err)
os.Exit(1)
}
log.Debugw("init service success", "service_name", serviceName)
log.Debugw("success to init service ", "service", serviceName)
// 2. register service to lifecycle.
slc.RegisterServices(service)
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/storage_provider/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (

const (
StorageProviderLogo = `Greenfield Storage Provider
__ _ __
_____/ /_____ _________ _____ ____ ____ _________ _ __(_)___/ /__ _____
/ ___/ __/ __ \/ ___/ __ / __ / _ \ / __ \/ ___/ __ \ | / / / __ / _ \/ ___/
(__ ) /_/ /_/ / / / /_/ / /_/ / __/ / /_/ / / / /_/ / |/ / / /_/ / __/ /
/____/\__/\____/_/ \__,_/\__, /\___/ / .___/_/ \____/|___/_/\__,_/\___/_/
/____/ /_/
`
__ _ __
_____/ /_____ _________ _____ ____ ____ _________ _ __(_)___/ /__ _____
/ ___/ __/ __ \/ ___/ __ / __ / _ \ / __ \/ ___/ __ \ | / / / __ / _ \/ ___/
(__ ) /_/ /_/ / / / /_/ / /_/ / __/ / /_/ / / / /_/ / |/ / / /_/ / __/ /
/____/\__/\____/_/ \__,_/\__, /\___/ / .___/_/ \____/|___/_/\__,_/\___/_/
/____/ /_/
`
)

// DumpLogo output greenfield storage provider logo
func DumpLogo() string {
return StorageProviderLogo
}
Expand All @@ -27,6 +28,7 @@ var (
BuildTime string
)

// DumpVersion output the storage provider version information
func DumpVersion() string {
return fmt.Sprintf("Version : %s\n"+
"Branch : %s\n"+
Expand Down
40 changes: 25 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"bufio"
"os"

"github.com/bnb-chain/greenfield-storage-provider/service/blocksyncer"
"github.com/bnb-chain/greenfield-storage-provider/model"
tomlconfig "github.com/forbole/juno/v4/cmd/migrate/toml"
"github.com/naoina/toml"

"github.com/bnb-chain/greenfield-storage-provider/service/challenge"
"github.com/bnb-chain/greenfield-storage-provider/service/downloader"
"github.com/bnb-chain/greenfield-storage-provider/service/gateway"
Expand All @@ -14,10 +17,9 @@ import (
"github.com/bnb-chain/greenfield-storage-provider/service/syncer"
"github.com/bnb-chain/greenfield-storage-provider/service/uploader"
"github.com/bnb-chain/greenfield-storage-provider/util"
tomlconfig "github.com/forbole/juno/v4/cmd/migrate/toml"
"github.com/naoina/toml"
)

// StorageProviderConfig defines the configuration of storage provider
type StorageProviderConfig struct {
Service []string
GatewayCfg *gateway.GatewayConfig
Expand All @@ -31,21 +33,29 @@ type StorageProviderConfig struct {
BlockSyncerCfg *tomlconfig.TomlConfig
}

// DefaultStorageProviderConfig defines the default configuration of storage provider services
var DefaultStorageProviderConfig = &StorageProviderConfig{
GatewayCfg: gateway.DefaultGatewayConfig,
UploaderCfg: uploader.DefaultUploaderConfig,
DownloaderCfg: downloader.DefaultDownloaderConfig,
ChallengeCfg: challenge.DefaultChallengeConfig,
StoneNodeCfg: stonenode.DefaultStoneNodeConfig,
SyncerCfg: syncer.DefaultSyncerConfig,
SignerCfg: signer.DefaultSignerChainConfig,
MetadataCfg: metadata.DefaultMetadataConfig,
BlockSyncerCfg: blocksyncer.DefaultBlockSyncerConfig,
GatewayCfg: gateway.DefaultGatewayConfig,
UploaderCfg: uploader.DefaultUploaderConfig,
DownloaderCfg: downloader.DefaultDownloaderConfig,
ChallengeCfg: challenge.DefaultChallengeConfig,
StoneNodeCfg: stonenode.DefaultStoneNodeConfig,
SyncerCfg: syncer.DefaultSyncerConfig,
SignerCfg: signer.DefaultSignerChainConfig,
Service: []string{
model.GatewayService,
model.UploaderService,
model.DownloaderGrpcAddress,
model.ChallengeService,
model.StoneNodeService,
model.SyncerService,
model.SignerService,
},
}

// LoadConfig loads the config file
func LoadConfig(file string) *StorageProviderConfig {
f, err := os.Open(file)
// LoadConfig loads the config file from path
func LoadConfig(path string) *StorageProviderConfig {
f, err := os.Open(path)
if err != nil {
panic(err)
}
Expand Down
68 changes: 39 additions & 29 deletions model/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,71 @@ package model

// define storage provider include service
const (
GatewayService = "Gateway"
UploaderService = "Uploader"
DownloaderService = "Downloader"
ChallengeService = "Challenge"
StoneNodeService = "StoneNode"
SyncerService = "Syncer"
SignerService = "Signer"
MetadataService = "Metadata"
// GatewayService defines the name of gateway service
GatewayService = "Gateway"
// UploaderService defines the name of uploader service
UploaderService = "Uploader"
// DownloaderService defines the name of downloader service
DownloaderService = "Downloader"
// ChallengeService defines the name of challenge service
ChallengeService = "Challenge"
// StoneNodeService defines the name of stone node service
StoneNodeService = "StoneNode"
// SyncerService defines the name of syncer service
SyncerService = "Syncer"
// SignerService defines the name of signer service
SignerService = "Signer"
// MetadataService defines the name of metadata service
MetadataService = "Metadata"
// BlockSyncerService defines the name of block sync service
BlockSyncerService = "BlockSyncer"
)

// define storage provider service gGRPC default address
const (
UploaderGrpcAddress = "localhost:9133"
// UploaderGrpcAddress default gGRPC address of uploader
UploaderGrpcAddress = "localhost:9133"
// DownloaderGrpcAddress default gGRPC address of downloader
DownloaderGrpcAddress = "localhost:9233"
ChallengeGrpcAddress = "localhost:9333"
StoneNodeGrpcAddress = "localhost:9433"
SyncerGrpcAddress = "localhost:9533"
SignerGrpcAddress = "localhost:9633"
// ChallengeGrpcAddress default gGRPC address of challenge
ChallengeGrpcAddress = "localhost:9333"
// StoneNodeGrpcAddress default gGRPC address of stone node
StoneNodeGrpcAddress = "localhost:9433"
// SyncerGrpcAddress default gGRPC address of syncer
SyncerGrpcAddress = "localhost:9533"
// SignerGrpcAddress default gGRPC address of signer
SignerGrpcAddress = "localhost:9633"
)

// environment constants
const (
// Piece Store constants
// BucketURL defines env variable name for bucket url
BucketURL = "BUCKET_URL"

// AWS environment constants
AWSAccessKey = "AWS_ACCESS_KEY"
AWSSecretKey = "AWS_SECRET_KEY"
// AWSAccessKey defines env variable name for aws assess key
AWSAccessKey = "AWS_ACCESS_KEY"
// AWSSecretKey defines env variable name for aws secret key
AWSSecretKey = "AWS_SECRET_KEY"
// AWSSessionToken defines env variable name for aws session token
AWSSessionToken = "AWS_SESSION_TOKEN"

// MetaDB environment constants
MetaDBUser = "META_DB_USER"
MetaDBPassword = "META_DB_PASSWORD"

// JobDB environment constants
JobDBUser = "JOB_DB_USER"
JobDBPassword = "JOB_DB_PASSWORD"
)

// define cache size
const (
// the maximum number of cached items in service trace queue
// LruCacheLimit define maximum number of cached items in service trace queue
LruCacheLimit = 8192
)

// define piece store constants.
const (
BufPoolSize = 32 << 10
// BufPoolSize define buffer pool size
BufPoolSize = 32 << 10
// ChecksumAlgo define validation Algorithm Name
ChecksumAlgo = "Crc32c"
)

// RPC config
const (
// server and client max send or recv msg size
// MaxCallMsgSize defines gPRCt max send or recv msg size
MaxCallMsgSize = 25 * 1024 * 1024
)

Expand Down
8 changes: 5 additions & 3 deletions model/errors/error_code.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package errors

const (
// RPCErrCode defines storage provider rpc error code
RPCErrCode = 10000

// ErrorCodeBadRequest defines bad request error code
ErrorCodeBadRequest = 40001
ErrorCodeNotFound = 40004

// ErrorCodeNotFound defines not found error code
ErrorCodeNotFound = 40004
// ErrorCodeInternalError defines internal error code
ErrorCodeInternalError = 50001
)
64 changes: 39 additions & 25 deletions model/errors/rpc_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,61 @@ import (

// common error
var (
ErrCacheMiss = errors.New("cache missing")
// ErrCacheMiss defines cache missing error
ErrCacheMiss = errors.New("cache missing")
// ErrSealTimeout defines seal object timeout error
ErrSealTimeout = errors.New("seal object timeout")
)

// piece store errors
var (
NotSupportedMethod = errors.New("unsupported method")
NotSupportedDelimiter = errors.New("unsupported delimiter")
EmptyObjectKey = errors.New("invalid object key")
EmptyMemoryObject = errors.New("memory object not exist")
BucketNotExisted = errors.New("bucket not exist")
// ErrUnsupportMethod defines unsupported method error
ErrUnsupportMethod = errors.New("unsupported method")
// ErrUnsupportDelimiter defines invalid key with delimiter error
ErrUnsupportDelimiter = errors.New("unsupported delimiter")
// ErrInvalidObjectKey defines invalid object key error
ErrInvalidObjectKey = errors.New("invalid object key")
// ErrNotExitObject defines not exist object in memory error
ErrNotExitObject = errors.New("object not exist")
//ErrNotExistBucket defines not exist bucket error
ErrNotExistBucket = errors.New("bucket not exist")
// ErrNoPermissionAccessBucket defines deny access bucket error
ErrNoPermissionAccessBucket = errors.New("deny access bucket")
)

// gateway errors
var (
ErrInternalError = errors.New("internal error")
ErrDuplicateBucket = errors.New("duplicate bucket")
ErrDuplicateObject = errors.New("duplicate object")
ErrObjectTxNotExist = errors.New("object tx not exist")
ErrObjectNotExist = errors.New("object not exist")
ErrObjectIsEmpty = errors.New("object payload is empty")
// ErrInternalError defines storage provider internal error
ErrInternalError = errors.New("internal error")
// ErrDuplicateBucket defines duplicate bucket error
ErrDuplicateBucket = errors.New("duplicate bucket")
// ErrDuplicateObject defines duplicate object error
ErrDuplicateObject = errors.New("duplicate object")
// ErrPayloadZero defines payload size is zero error
ErrPayloadZero = errors.New("object payload is zero")

// signature error
// ErrAuthorizationFormat defines the invalid authorization format error
ErrAuthorizationFormat = errors.New("authorization format error")
ErrRequestConsistent = errors.New("failed to check request consistent")
// ErrRequestConsistent defines the invalid request checksum error
ErrRequestConsistent = errors.New("failed to check request consistent")
// ErrSignatureConsistent defines the invalid signature error
ErrSignatureConsistent = errors.New("failed to check signature consistent")
ErrUnsupportedSignType = errors.New("unsupported signature type")
ErrEmptyReqHeader = errors.New("request header is empty")
ErrReqHeader = errors.New("invalid request header")
// ErrUnsupportSignType defines the unsupported signature type error
ErrUnsupportSignType = errors.New("unsupported signature type")
// ErrEmptyReqHeader defines the empty header error
ErrEmptyReqHeader = errors.New("request header is empty")
// ErrInvalidHeader defines the invalid header error
ErrInvalidHeader = errors.New("invalid request header")
)

// signer service error
var (
ErrIPBlocked = errors.New("ip blocked")
ErrAPIKey = errors.New("invalid api key")
ErrSignMsg = errors.New("sign message with private key failed")
// ErrIPBlocked defines deny request by ip error
ErrIPBlocked = errors.New("ip blocked")
// ErrAPIKey defines invalid signer api key
ErrAPIKey = errors.New("invalid api key")
// ErrSignMsg defines sign msg error by private key
ErrSignMsg = errors.New("sign message with private key failed")
// ErrSealObjectOnChain defines send seal object tx to chain error
ErrSealObjectOnChain = errors.New("send sealObject msg failed")
)

// block syncer service errors
var (
ErrSyncerStopped = errors.New("syncer service has already stopped")
)
Loading