Skip to content

Commit

Permalink
add consensus ip whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
chengzhinei committed Jul 5, 2024
1 parent 09bd020 commit 9c6864e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
17 changes: 17 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ type OecConfig struct {
maxSubscriptionClients int

maxTxLimitPerPeer uint64

consensusIPWhitelist []string
}

const (
Expand Down Expand Up @@ -175,6 +177,7 @@ const (
FlagCsTimeoutPrecommit = "consensus.timeout_precommit"
FlagCsTimeoutPrecommitDelta = "consensus.timeout_precommit_delta"
FlagCsTimeoutCommit = "consensus.timeout_commit"
FlagConsensusIPWhitelist = "consensus.ip_whitelist"
FlagEnableHasBlockPartMsg = "enable-blockpart-ack"
FlagDebugGcInterval = "debug.gc-interval"
FlagCommitGapOffset = "commit-gap-offset"
Expand Down Expand Up @@ -331,6 +334,7 @@ func (c *OecConfig) loadFromConfig() {
c.SetCommitGapHeight(viper.GetInt64(server.FlagCommitGapHeight))
c.SetSentryAddrs(viper.GetString(FlagSentryAddrs))
c.SetNodeKeyWhitelist(viper.GetString(FlagNodeKeyWhitelist))
c.SetConsensusIPWhitelist(viper.GetString(FlagConsensusIPWhitelist))
c.SetEnableWtx(viper.GetBool(FlagEnableWrappedTx))
c.SetEnableAnalyzer(viper.GetBool(trace.FlagEnableAnalyzer))
c.SetDeliverTxsExecuteMode(viper.GetInt(state.FlagDeliverTxsExecMode))
Expand Down Expand Up @@ -511,6 +515,8 @@ func (c *OecConfig) updateFromKVStr(k, v string) {
c.SetPendingPoolBlacklist(v)
case FlagNodeKeyWhitelist:
c.SetNodeKeyWhitelist(v)
case FlagConsensusIPWhitelist:
c.SetConsensusIPWhitelist(v)
case FlagMempoolCheckTxCost:
r, err := strconv.ParseBool(v)
if err != nil {
Expand Down Expand Up @@ -810,6 +816,10 @@ func (c *OecConfig) GetNodeKeyWhitelist() []string {
return c.nodeKeyWhitelist
}

func (c *OecConfig) GetConsensusIPWhitelist() []string {
return c.consensusIPWhitelist
}

func (c *OecConfig) GetMempoolCheckTxCost() bool {
return c.mempoolCheckTxCost
}
Expand All @@ -831,6 +841,13 @@ func (c *OecConfig) SetNodeKeyWhitelist(value string) {
}
}

func (c *OecConfig) SetConsensusIPWhitelist(value string) {
ipList := resolveNodeKeyWhitelist(value)
for _, ip := range ipList {
c.consensusIPWhitelist = append(c.consensusIPWhitelist, strings.TrimSpace(ip))
}
}

func (c *OecConfig) GetSentryAddrs() []string {
return c.sentryAddrs
}
Expand Down
5 changes: 5 additions & 0 deletions libs/tendermint/config/dynamic_config_okchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type IDynamicConfig interface {
GetMaxSubscriptionClients() int
GetPendingPoolBlacklist() string
GetMaxTxLimitPerPeer() uint64
GetConsensusIPWhitelist() []string
}

var DynamicConfig IDynamicConfig = MockDynamicConfig{}
Expand Down Expand Up @@ -233,3 +234,7 @@ func (d MockDynamicConfig) GetPendingPoolBlacklist() string {
func (c MockDynamicConfig) GetMaxTxLimitPerPeer() uint64 {
return DefaultMempoolConfig().MaxTxLimitPerPeer
}

func (c MockDynamicConfig) GetConsensusIPWhitelist() []string {
return []string{}
}
17 changes: 14 additions & 3 deletions libs/tendermint/consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package consensus
import (
"bytes"
"fmt"
"github.com/okex/exchain/libs/tendermint/crypto"
"github.com/okex/exchain/libs/tendermint/libs/automation"
cfg "github.com/okex/exchain/libs/tendermint/config"
"reflect"
"sync"
"time"

"github.com/pkg/errors"

amino "github.com/tendermint/go-amino"

cstypes "github.com/okex/exchain/libs/tendermint/consensus/types"
"github.com/okex/exchain/libs/tendermint/crypto"
"github.com/okex/exchain/libs/tendermint/libs/automation"
"github.com/okex/exchain/libs/tendermint/libs/bits"
tmevents "github.com/okex/exchain/libs/tendermint/libs/events"
"github.com/okex/exchain/libs/tendermint/libs/log"
Expand Down Expand Up @@ -343,6 +343,17 @@ func (conR *Reactor) Receive(chID byte, src p2p.Peer, msgBytes []byte) {
return
}

okIP := false
for _, ip := range cfg.DynamicConfig.GetConsensusIPWhitelist() {
if src.RemoteIP().String() == ip {
okIP = true
break
}
}
if !okIP {
conR.Logger.Error("consensus msg:IP not in whitelist", "IP", src.RemoteIP().String())
}

msg, err := decodeMsg(msgBytes)
if err != nil {
conR.Logger.Error("Error decoding message", "src", src, "chId", chID, "msg", msg, "err", err, "bytes", msgBytes)
Expand Down

0 comments on commit 9c6864e

Please sign in to comment.