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: implement p2p protocol and rpc service #221

Merged
merged 22 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
842a805
feat: imple p2p protocol node and rpc service
joeylichang Mar 8, 2023
4a2f09b
feat: p2p rpc server
joeylichang Mar 18, 2023
94d70f0
fix: config port conflict
joeylichang Mar 18, 2023
c086c14
feat: get secondary sp approval from p2p server
joeylichang Mar 18, 2023
d264515
feat: verify approval in gateway
joeylichang Mar 18, 2023
813a9dd
fix: golang ci lint error
joeylichang Mar 18, 2023
a544c3d
fix: integration testing bugs
joeylichang Mar 18, 2023
d90d15a
feat: load p2p private key from ENV var
joeylichang Mar 19, 2023
dcb2d24
feat: implement p2p protocaol msg sign and verify
joeylichang Mar 20, 2023
eddb874
feat: p2p protocol msg sign in signer service
joeylichang Mar 20, 2023
52ec0d1
feat: signer client method for sign p2p protocol msg
joeylichang Mar 20, 2023
75a34ce
feat: signature verify in related modules
joeylichang Mar 20, 2023
e9b8b86
fix: manager grace stop
joeylichang Mar 20, 2023
ccb6c98
fix: object Id proto clone to deep copy panic
joeylichang Mar 21, 2023
14b1a58
feat: p2p create key pairs command
joeylichang Mar 21, 2023
8ec7e7e
feat: p2p config to localup script
joeylichang Mar 21, 2023
765e221
fix: local up script deploy p2p error
joeylichang Mar 21, 2023
edcdf4f
fix: pr comments
joeylichang Mar 21, 2023
7dd1722
fix: pr comments
joeylichang Mar 21, 2023
43d9a69
Merge branch 'develop' into feat-p2p
joeylichang Mar 22, 2023
5518c61
fix: pr comments
joeylichang Mar 22, 2023
399b97e
fix: change p2p config name for folllowing the project specification
joeylichang Mar 22, 2023
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
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ challenge = "localhost:9333"
downloader = "localhost:9233"
gateway = "gnfd.nodereal.com"
metadata = "localhost:9733"
p2p = "localhost:9833"
joeylichang marked this conversation as resolved.
Show resolved Hide resolved
receiver = "localhost:9533"
signer = "localhost:9633"
tasknode = "localhost:9433"
Expand All @@ -93,6 +94,7 @@ challenge = "localhost:9333"
downloader = "localhost:9233"
gateway = "localhost:9033"
metadata = "localhost:9733"
p2p = "localhost:9833"
receiver = "localhost:9533"
signer = "localhost:9633"
tasknode = "localhost:9433"
Expand All @@ -116,6 +118,16 @@ ChainID = "greenfield_9000-1741"
[[ChainConfig.NodeAddr]]
GreenfieldAddresses = ["localhost:9090"]
TendermintAddresses = ["http://localhost:26750"]
# signer configuration
[SignerCfg]
GRPCAddress = "localhost:9633"
APIKey = ""
WhitelistCIDR = ["127.0.0.1/32"]
GasLimit = 210000
OperatorPrivateKey = ""
FundingPrivateKey = ""
SealPrivateKey = ""
ApprovalPrivateKey = ""
# block syncer configuration
# signer configuration
[SignerCfg]
Expand All @@ -127,7 +139,18 @@ SealPrivateKey = "${SP_Seal_PrivKey}"
ApprovalPrivateKey = "${SP_Approval_PrivKey}"
[BlockSyncerCfg]
Modules = ["epoch", "bucket", "object", "payment"]
Dsn = "localhost:3306"
Dsn = "localhost:3308"
# p2p node configuration
[P2PConfig]
Copy link
Collaborator

@sysvm sysvm Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix P2PConfig to P2PCfg to keep the same style with other services

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

ListenAddress = "127.0.0.1:9933"
# p2p node msg Secp256k1 encryption key, it is different from other SP's addresses
P2PPrivateKey = ""
# p2p node's bootstrap node, format: [node_id1@ip1:port1, node_id2@ip1:port2]
Bootstrap = []
# log configuration
[LogCfg]
Level = "info"
Path = "./gnfd-sp.log"
```

### Start
Expand Down
54 changes: 54 additions & 0 deletions cmd/p2p/p2p.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package p2p

import (
"fmt"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/urfave/cli/v2"
)

var number = &cli.IntFlag{
Name: "n",
Usage: "The number of key pairs",
Value: 1,
}

var P2PCreateKeysCmd = &cli.Command{
Action: p2pCreateKeysAction,
Name: "p2p.create.key",
Usage: "Create Secp256k1 key pairs for encrypting p2p protocol msg and identifying p2p node",
Flags: []cli.Flag{
number,
},
Category: "P2P COMMANDS",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define as a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next PR, the metrics PR has defined the constant, it should be at the same file

Description: `
The p2p.create.key creates 'n' sets of Secp256k1 key pairs, each key pair contains a private key
and a node id, the private key is used to encrypt p2p protocol msg, and the node id is use to public
to other p2p nodes for communication by p2p protocol.`,
}

func p2pCreateKeysAction(ctx *cli.Context) error {
n := ctx.Int(number.Name)
makeKeyPairs := func() (string, string, error) {
privKey, _, err := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
if err != nil {
return "", "", err
}
secp256k1PrivKey := privKey.(*crypto.Secp256k1PrivateKey)
nodeID, err := peer.IDFromPublicKey(secp256k1PrivKey.GetPublic())
if err != nil {
return "", "", err
}
return secp256k1PrivKey.Key.String(), nodeID.String(), err
}
for i := 0; i < n; i++ {
private, nodeId, err := makeKeyPairs()
if err != nil {
return err
}
fmt.Printf("%d private key: %s\n", i, private)
fmt.Printf("%d node id key: %s\n", i, nodeId)
}
return nil
}
23 changes: 16 additions & 7 deletions cmd/storage_provider/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (
"github.com/urfave/cli/v2"

"github.com/bnb-chain/greenfield-storage-provider/cmd/utils"
"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"
"github.com/bnb-chain/greenfield-storage-provider/service/manager"
"github.com/bnb-chain/greenfield-storage-provider/service/receiver"
"github.com/bnb-chain/greenfield-storage-provider/service/signer"

"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"
"github.com/bnb-chain/greenfield-storage-provider/service/gateway"
"github.com/bnb-chain/greenfield-storage-provider/service/manager"
metadata "github.com/bnb-chain/greenfield-storage-provider/service/metadata/service"
"github.com/bnb-chain/greenfield-storage-provider/service/p2p"
"github.com/bnb-chain/greenfield-storage-provider/service/receiver"
"github.com/bnb-chain/greenfield-storage-provider/service/signer"
"github.com/bnb-chain/greenfield-storage-provider/service/tasknode"
"github.com/bnb-chain/greenfield-storage-provider/service/uploader"
)
Expand Down Expand Up @@ -138,6 +138,15 @@ func initService(serviceName string, cfg *config.StorageProviderConfig) (server
if err != nil {
return nil, err
}
case model.P2PService:
p2pCfg, err := cfg.MakeP2PServiceConfig()
if err != nil {
return nil, err
}
server, err = p2p.NewP2PServer(p2pCfg)
if err != nil {
return nil, err
}
default:
log.Errorw("unknown service", "service", serviceName)
return nil, fmt.Errorf("unknown service: %s", serviceName)
Expand Down
7 changes: 5 additions & 2 deletions cmd/storage_provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"os"
"syscall"

"github.com/bnb-chain/greenfield-storage-provider/pkg/lifecycle"
"github.com/urfave/cli/v2"

"github.com/bnb-chain/greenfield-storage-provider/cmd/conf"
"github.com/bnb-chain/greenfield-storage-provider/cmd/p2p"
"github.com/bnb-chain/greenfield-storage-provider/cmd/utils"
"github.com/bnb-chain/greenfield-storage-provider/config"
"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/util"
)
Expand Down Expand Up @@ -50,7 +51,9 @@ func init() {
// config category commands
conf.ConfigDumpCmd,
conf.ConfigUploadCmd,
// miscellaneous commands
// p2p category commands
p2p.P2PCreateKeysCmd,
// miscellaneous category commands
VersionCmd,
utils.ListServiceCmd,
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var (
Value: "./config.toml",
}
ConfigRemoteFlag = &cli.BoolFlag{
Name: "configremote",
Usage: "Flag load config from remote db,if 'configremote' be set, the db.user, " +
"db.password and db.address flags are needed, otherwise use default value",
Name: "config.remote",
Usage: "Flag load config from remote db,if 'config.remote' be set, the db.user, " +
"db.password and db.address flags are needed, otherwise use the default value",
}
ServerFlag = &cli.StringFlag{
Name: "server",
Expand Down
13 changes: 12 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"os"

"github.com/bnb-chain/greenfield-storage-provider/pkg/p2p"
"github.com/naoina/toml"

"github.com/bnb-chain/greenfield-storage-provider/model"
Expand All @@ -29,6 +30,7 @@ type StorageProviderConfig struct {
ChainConfig *gnfd.GreenfieldChainConfig
SignerCfg *signer.SignerConfig
BlockSyncerCfg *blocksyncer.Config
P2PConfig *p2p.NodeConfig
LogCfg *LogConfig
}

Expand All @@ -54,6 +56,7 @@ var DefaultStorageProviderConfig = &StorageProviderConfig{
model.SignerService,
model.MetadataService,
model.ManagerService,
model.P2PService,
},
ListenAddress: map[string]string{
model.GatewayService: model.GatewayHTTPAddress,
Expand All @@ -64,6 +67,7 @@ var DefaultStorageProviderConfig = &StorageProviderConfig{
model.TaskNodeService: model.TaskNodeGRPCAddress,
model.SignerService: model.SignerGRPCAddress,
model.MetadataService: model.MetadataGRPCAddress,
model.P2PService: model.P2PGRPCAddress,
},
Endpoint: map[string]string{
model.GatewayService: "gnfd.nodereal.com",
Expand All @@ -74,13 +78,15 @@ var DefaultStorageProviderConfig = &StorageProviderConfig{
model.TaskNodeService: model.TaskNodeGRPCAddress,
model.SignerService: model.SignerGRPCAddress,
model.MetadataService: model.MetadataGRPCAddress,
model.P2PService: model.P2PGRPCAddress,
},
SpOperatorAddress: hex.EncodeToString([]byte(model.SpOperatorAddress)),
SpDBConfig: DefaultSQLDBConfig,
PieceStoreConfig: DefaultPieceStoreConfig,
ChainConfig: DefaultGreenfieldChainConfig,
SignerCfg: signer.DefaultSignerChainConfig,
BlockSyncerCfg: DefaultBlockSyncerConfig,
P2PConfig: DefaultP2PConfig,
LogCfg: DefaultLogConfig,
}

Expand Down Expand Up @@ -114,7 +120,7 @@ var DefaultGreenfieldChainConfig = &gnfd.GreenfieldChainConfig{
// DefaultBlockSyncerConfig defines the default configuration of BlockSyncer service
var DefaultBlockSyncerConfig = &blocksyncer.Config{
Modules: []string{"epoch", "bucket", "object", "payment"},
Dsn: "localhost:3306",
Dsn: "localhost:3308",
}

type LogConfig struct {
Expand All @@ -129,6 +135,11 @@ var DefaultLogConfig = &LogConfig{
Path: "./gnfd-sp.log",
}

var DefaultP2PConfig = &p2p.NodeConfig{
ListenAddress: model.P2PListenAddress,
PingPeriod: model.DefaultPingPeriod,
}

// LoadConfig loads the config file from path
func LoadConfig(path string) *StorageProviderConfig {
f, err := os.Open(path)
Expand Down
10 changes: 9 additions & 1 deletion config/config_template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ challenge = "localhost:9333"
downloader = "localhost:9233"
gateway = "gnfd.sp.com"
metadata = "localhost:9733"
p2p = "localhost:9833"
receiver = "localhost:9533"
signer = "localhost:9633"
tasknode = "localhost:9433"
Expand All @@ -16,6 +17,7 @@ challenge = "localhost:9333"
downloader = "localhost:9233"
gateway = "localhost:9033"
metadata = "localhost:9733"
p2p = "localhost:9833"
receiver = "localhost:9533"
signer = "localhost:9633"
tasknode = "localhost:9433"
Expand Down Expand Up @@ -55,7 +57,13 @@ ApprovalPrivateKey = ""

[BlockSyncerCfg]
Modules = ["epoch", "bucket", "object", "payment"]
Dsn = "localhost:3306"
Dsn = "localhost:3308"

[P2PConfig]
ListenAddress = "127.0.0.1:9933"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference between this ListenAddress and ListAddress.p2p?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2PConfig.ListenAddress p2p protocol , another is rpc address

P2PPrivateKey = ""
Bootstrap = []
PingPeriod = 2

[LogCfg]
Level = "debug"
Expand Down
26 changes: 26 additions & 0 deletions config/subconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"

"github.com/bnb-chain/greenfield-storage-provider/service/p2p"
tomlconfig "github.com/forbole/juno/v4/cmd/migrate/toml"
databaseconfig "github.com/forbole/juno/v4/database/config"
loggingconfig "github.com/forbole/juno/v4/log/config"
Expand Down Expand Up @@ -173,6 +174,11 @@ func (cfg *StorageProviderConfig) MakeTaskNodeConfig() (*tasknode.TaskNodeConfig
} else {
return nil, fmt.Errorf("missing signer gRPC address configuration for task node service")
}
if _, ok := cfg.Endpoint[model.P2PService]; ok {
snCfg.P2PGrpcAddress = cfg.Endpoint[model.P2PService]
} else {
return nil, fmt.Errorf("missing p2p server gRPC address configuration for task node service")
}
return snCfg, nil
}

Expand Down Expand Up @@ -235,3 +241,23 @@ func (cfg *StorageProviderConfig) MakeBlockSyncerConfig() (*tomlconfig.TomlConfi
},
}, nil
}

// MakeP2PServiceConfig make p2p service config from StorageProviderConfig
func (cfg *StorageProviderConfig) MakeP2PServiceConfig() (*p2p.P2PConfig, error) {
pCfg := &p2p.P2PConfig{
SpOperatorAddress: cfg.SpOperatorAddress,
SpDBConfig: cfg.SpDBConfig,
P2PConfig: cfg.P2PConfig,
}
if _, ok := cfg.ListenAddress[model.P2PService]; ok {
pCfg.GRPCAddress = cfg.ListenAddress[model.P2PService]
} else {
return nil, fmt.Errorf("missing p2p service gRPC address configuration for p2p service")
}
if _, ok := cfg.Endpoint[model.SignerService]; ok {
pCfg.SignerGrpcAddress = cfg.Endpoint[model.SignerService]
} else {
return nil, fmt.Errorf("missing signer gRPC address configuration for p2p service")
}
return pCfg, nil
}
19 changes: 18 additions & 1 deletion deployment/localup/env.info
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,21 @@ CHAIN_HTTP_ENDPOINT="localhost:26750"
# sp config
SP_DEPLOY_DIR="local_env"
SP_NUM=7
SP_START_PORT=10000
SP_START_PORT=10000

# p2p config
NODE0="83524b24cf89d7e829db0b99829ce897f966dc417362de2ae08c4710868e10ff"
NODE1="27ad956979aea27446754702ad53a3385f82840a5ce912a67aeb8178c255ec54"
NODE2="bee33cadbe75dc5db386242d52dc1c4de6dbbfd37d176ed5563f357107d65fed"
NODE3="a6282fcd91d2f3765df7de061048c62ce23d7a1dc09216101d73414042b5681f"
NODE4="0b5a6480fe664671fd01979fc4278fa393f0abc08c95980f4a900056f035ca7e"
NODE5="896caab68f3f3fdbf053ba64a29ce0292c8a5536a2790713b67dd1851f4d77fc"
NODE6="ec09def8ee42ac9c23394080ce5bfcdcb1dc57255048a8afc74e9deaae4399cd"

BOOT0="16Uiu2HAmFs4enUvoa8epNMTvMnwmacuVV8u4jJY7k5WF3ZKfxnRq@127.0.0.1:11933"
BOOT1="16Uiu2HAmUKZHakpHSZfAfdjwAMRbLmFTz6ikmSQ3HZLB3BSU1saj@127.0.0.1:10933"
BOOT2="16Uiu2HAmUKZHakpHSZfAfdjwAMRbLmFTz6ikmSQ3HZLB3BSU1saj@127.0.0.1:10933"
BOOT3="16Uiu2HAmUKZHakpHSZfAfdjwAMRbLmFTz6ikmSQ3HZLB3BSU1saj@127.0.0.1:10933"
BOOT4="16Uiu2HAmUKZHakpHSZfAfdjwAMRbLmFTz6ikmSQ3HZLB3BSU1saj@127.0.0.1:10933"
BOOT5="16Uiu2HAmUKZHakpHSZfAfdjwAMRbLmFTz6ikmSQ3HZLB3BSU1saj@127.0.0.1:10933"
BOOT6="16Uiu2HAmUKZHakpHSZfAfdjwAMRbLmFTz6ikmSQ3HZLB3BSU1saj@127.0.0.1:10933"
8 changes: 8 additions & 0 deletions deployment/localup/localup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ make_config() {
sed -i -e "s/9533/$(($cur_port+533))/g" config.toml
sed -i -e "s/9633/$(($cur_port+633))/g" config.toml
sed -i -e "s/9733/$(($cur_port+733))/g" config.toml
sed -i -e "s/9833/$(($cur_port+833))/g" config.toml
sed -i -e "s/9933/$(($cur_port+933))/g" config.toml
sed -i -e "s/SpOperatorAddress = \".*\"/SpOperatorAddress = \"${OPERATOR_ADDRESS}\"/g" config.toml
sed -i -e "s/OperatorPrivateKey = \".*\"/OperatorPrivateKey = \"${OPERATOR_PRIVATE_KEY}\"/g" config.toml
sed -i -e "s/FundingPrivateKey = \".*\"/FundingPrivateKey = \"${FUNDING_PRIVATE_KEY}\"/g" config.toml
Expand All @@ -91,6 +93,11 @@ make_config() {
sed -i -e "s/localhost\:9090/${CHAIN_GRPC_ENDPOINT}/g" config.toml
sed -i -e "s/localhost\:26750/${CHAIN_HTTP_ENDPOINT}/g" config.toml
echo "succeed to generate config.toml in "${sp_dir}
# p2p
node=NODE${index}
boot=BOOT${index}
sed -i -e "s/P2PPrivateKey = \".*\"/P2PPrivateKey = \"${!node}\"/g" config.toml
sed -i -e "s/Bootstrap = \[\]/Bootstrap = \[\"${!boot}\"\]/g" config.toml
cd - >/dev/null
index=$(($index+1))
done
Expand Down Expand Up @@ -189,6 +196,7 @@ main() {
reset_sp $2
;;
--start)
stop_sp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a restart case to ensure to be clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is okay for the time being, because minimizing the commands of local setup script also reduces the complexity of use, and in the scenario of local setup script, it is not possible to run more than 7 SPs at the same time.

start_sp
;;
--stop)
Expand Down
Loading