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

DRAFT: weavevm piecestore integration #1448

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .go-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.20.14
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ require (
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11
)

require github.com/jackc/puddle/v2 v2.2.1 // indirect
require (
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
)

require (
cosmossdk.io/api v0.4.0 // indirect
Expand Down Expand Up @@ -286,7 +289,7 @@ require (
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/protobuf v1.33.0
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
100 changes: 99 additions & 1 deletion store/piecestore/piece/piece_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"path"
"path/filepath"
"runtime"
"strconv"
"time"

"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/store/piecestore/storage"
weaveVMtypes "github.com/bnb-chain/greenfield-storage-provider/store/piecestore/storage/weavevm/types"
)

// NewPieceStore returns an instance of PieceStore
Expand Down Expand Up @@ -57,6 +60,14 @@ func checkConfig(cfg *storage.PieceStoreConfig) error {
cfg.Store.BucketURL = p
cfg.Store.BucketURL += "/"
}

if cfg.Store.Storage == storage.WeavevmStore {
weavevmConfig, err := getWeaveVMConfigFromEnv()
if err != nil {
return fmt.Errorf("failed to get weavevm config from env vars: %w", err)
}
cfg.Store.WeavevmConfig = *weavevmConfig
}
return nil
}

Expand All @@ -66,6 +77,93 @@ func overrideConfigFromEnv(cfg *storage.PieceStoreConfig) {
}
}

func getWeaveVMConfigFromEnv() (*weaveVMtypes.Config, error) {
cfg := &weaveVMtypes.Config{
// Set defaults
RetryAttempts: 3,
RetryDelay: time.Millisecond * 100,
Timeout: time.Second * 3,
}

// Required: Endpoint
endpoint := os.Getenv(storage.WeaveVMEndpoint)
if endpoint == "" {
return nil, fmt.Errorf("%s environment variable is required", storage.WeaveVMEndpoint)
}
cfg.Endpoint = endpoint

// Required: ChainID
chainID := os.Getenv(storage.WeaveVMChainID)
if chainID == "" {
return nil, fmt.Errorf("%s environment variable is required", storage.WeaveVMChainID)
}
id, err := strconv.ParseInt(chainID, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid %s: %w", storage.WeaveVMChainID, err)
}
cfg.ChainID = id

// Authentication method
web3Endpoint := os.Getenv(storage.WeaveVMWeb3Endpoint)
privateKey := os.Getenv(storage.WeaveVMPrivateKey)

if web3Endpoint != "" {
cfg.Web3SignerEndpoint = web3Endpoint

// Web3Signer TLS config - all files must be provided if using TLS
tlsCert := os.Getenv(storage.WeaveVMWeb3TLSCert)
tlsKey := os.Getenv(storage.WeaveVMWeb3TLSKey)
tlsCACert := os.Getenv(storage.WeaveVMWeb3TLSCACert)

if tlsCert != "" || tlsKey != "" || tlsCACert != "" {
// If any TLS env var is set, all must be set
if tlsCert == "" || tlsKey == "" || tlsCACert == "" {
return nil, fmt.Errorf("all TLS files must be provided when using Web3Signer with TLS: cert, key and CA cert")
}
cfg.Web3SignerTLSCertFile = tlsCert
cfg.Web3SignerTLSKeyFile = tlsKey
cfg.Web3SignerTLSCACertFile = tlsCACert
}
} else if privateKey != "" {
cfg.PrivateKeyHex = privateKey
} else {
return nil, fmt.Errorf("either %s or %s must be provided", storage.WeaveVMWeb3Endpoint, storage.WeaveVMPrivateKey)
}

// Optional params
if timeoutStr := os.Getenv(storage.WeaveVMTimeout); timeoutStr != "" {
timeout, err := strconv.ParseInt(timeoutStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid %s: %w", storage.WeaveVMTimeout, err)
}
cfg.Timeout = time.Second * time.Duration(timeout)
}

if attemptsStr := os.Getenv(storage.WeaveVMRetryAttempts); attemptsStr != "" {
attempts, err := strconv.Atoi(attemptsStr)
if err != nil {
return nil, fmt.Errorf("invalid %s: %w", storage.WeaveVMRetryAttempts, err)
}
if attempts < 0 {
return nil, fmt.Errorf("%s must be non-negative", storage.WeaveVMRetryAttempts)
}
cfg.RetryAttempts = attempts
}

if delayStr := os.Getenv(storage.WeaveVMRetryDelay); delayStr != "" {
delay, err := strconv.ParseInt(delayStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid %s: %w", storage.WeaveVMRetryDelay, err)
}
if delay < 0 {
return nil, fmt.Errorf("%s must be non-negative", storage.WeaveVMRetryDelay)
}
cfg.RetryDelay = time.Millisecond * time.Duration(delay)
}

return cfg, nil
}

func createStorage(cfg storage.PieceStoreConfig) (storage.ObjectStorage, error) {
var (
object storage.ObjectStorage
Expand Down Expand Up @@ -108,7 +206,7 @@ func checkBucket(ctx context.Context, store storage.ObjectStorage) error {
}

func setDefaultFileStorePath() string {
var defaultBucket = "/var/piecestore"
defaultBucket := "/var/piecestore"
switch runtime.GOOS {
case "linux":
if os.Getuid() == 0 {
Expand Down
34 changes: 34 additions & 0 deletions store/piecestore/storage/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
LdfsStore = "ldfs"
// MemoryStore defines storage type for memory
MemoryStore = "memory"
// WeavevmStore defines storage type for weavevm
WeavevmStore = "weavevm"
)

// piece store storage config and environment constants
Expand Down Expand Up @@ -72,6 +74,38 @@ const (

// OctetStream is used to indicate the binary files
OctetStream = "application/octet-stream"

// WeaveVM Environment Variables

// WeaveVMEndpoint defines WeaveVM RPC node endpoint
WeaveVMEndpoint = "WEAVEVM_ENDPOINT"

// WeaveVMPrivateKey defines private key in hex format for direct transaction signing
WeaveVMPrivateKey = "WEAVEVM_PRIVATE_KEY"

// WeaveVMChainID defines WeaveVM chain ID for transaction signing
WeaveVMChainID = "WEAVEVM_CHAIN_ID"

// WeaveVMWeb3Endpoint defines Web3Signer endpoint URL
WeaveVMWeb3Endpoint = "WEAVEVM_WEB3_ENDPOINT"

// WeaveVMWeb3TLSCert defines path to Web3Signer TLS certificate file
WeaveVMWeb3TLSCert = "WEAVEVM_WEB3_TLS_CERT"

// WeaveVMWeb3TLSKey defines path to Web3Signer TLS key file
WeaveVMWeb3TLSKey = "WEAVEVM_WEB3_TLS_KEY"

// WeaveVMWeb3TLSCACert defines path to Web3Signer TLS CA certificate file
WeaveVMWeb3TLSCACert = "WEAVEVM_WEB3_TLS_CA_CERT"

// WeaveVMTimeout defines timeout duration for RPC calls in seconds
WeaveVMTimeout = "WEAVEVM_TIMEOUT"

// WeaveVMRetryAttempts defines maximum number of retry attempts
WeaveVMRetryAttempts = "WEAVEVM_RETRY_ATTEMPTS"

// WeaveVMRetryDelay defines delay between retry attempts in milliseconds
WeaveVMRetryDelay = "WEAVEVM_RETRY_DELAY"
)

// define piece store constants.
Expand Down
1 change: 1 addition & 0 deletions store/piecestore/storage/object_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var storageMap = map[string]StorageFn{
LdfsStore: newLdfsStore,
DiskFileStore: newDiskFileStore,
MemoryStore: newMemoryStore,
WeavevmStore: newWeaveVMStore,
}

type DefaultObjectStorage struct{}
Expand Down
7 changes: 7 additions & 0 deletions store/piecestore/storage/storage_config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package storage

import (
weaveVMtypes "github.com/bnb-chain/greenfield-storage-provider/store/piecestore/storage/weavevm/types"
)

// PieceStoreConfig contains some parameters which are used to run PieceStore
type PieceStoreConfig struct {
// Shards store the blocks into N buckets by hash of key
Expand All @@ -22,4 +26,7 @@ type ObjectStorageConfig struct {
TLSInsecureSkipVerify bool `comment:"optional"`
// IAMType is identity and access management type which contains two types: AKSKIAMType/SAIAMType
IAMType string `comment:"required"`

// WeaveVM config
WeavevmConfig weaveVMtypes.Config `comment:"optional"`
}
Loading