Skip to content

Commit

Permalink
Merge branch 'dev' into fsc/runtx-store
Browse files Browse the repository at this point in the history
  • Loading branch information
chengzhinei authored Jun 1, 2023
2 parents 429884c + 9c0b8c0 commit 6024a41
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 32 deletions.
25 changes: 24 additions & 1 deletion app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type OkbcConfig struct {

//
commitGapOffset int64

maxSubscriptionClients int
}

const (
Expand Down Expand Up @@ -168,6 +170,7 @@ const (
FlagEnableHasBlockPartMsg = "enable-blockpart-ack"
FlagDebugGcInterval = "debug.gc-interval"
FlagCommitGapOffset = "commit-gap-offset"
FlagMaxSubscriptionClients = "max-subscription-clients"
)

var (
Expand Down Expand Up @@ -320,6 +323,7 @@ func (c *OkbcConfig) loadFromConfig() {
c.SetEnableHasBlockPartMsg(viper.GetBool(FlagEnableHasBlockPartMsg))
c.SetGcInterval(viper.GetInt(FlagDebugGcInterval))
c.SetIavlAcNoBatch(viper.GetBool(tmiavl.FlagIavlCommitAsyncNoBatch))
c.SetMaxSubscriptionClients(viper.GetInt(FlagMaxSubscriptionClients))
}

func resolveNodeKeyWhitelist(plain string) []string {
Expand Down Expand Up @@ -392,7 +396,8 @@ func (c *OkbcConfig) format() string {
commit-gap-height: %d
enable-analyzer: %v
iavl-commit-async-no-batch: %v
active-view-change: %v`, system.ChainName,
active-view-change: %v
max_subscription_clients: %v`, system.ChainName,
c.GetMempoolRecheck(),
c.GetMempoolForceRecheckGap(),
c.GetMempoolSize(),
Expand Down Expand Up @@ -422,6 +427,7 @@ func (c *OkbcConfig) format() string {
c.GetEnableAnalyzer(),
c.GetIavlAcNoBatch(),
c.GetActiveVC(),
c.GetMaxSubscriptionClients(),
)
}

Expand Down Expand Up @@ -688,6 +694,12 @@ func (c *OkbcConfig) updateFromKVStr(k, v string) {
return
}
c.SetCommitGapOffset(r)
case FlagMaxSubscriptionClients:
r, err := strconv.Atoi(v)
if err != nil {
return
}
c.SetMaxSubscriptionClients(r)
}

}
Expand Down Expand Up @@ -1129,3 +1141,14 @@ func (c *OkbcConfig) GetIavlAcNoBatch() bool {
func (c *OkbcConfig) SetIavlAcNoBatch(value bool) {
c.iavlAcNoBatch = value
}

func (c *OkbcConfig) SetMaxSubscriptionClients(v int) {
if v < 0 {
v = 0
}
c.maxSubscriptionClients = v
}

func (c *OkbcConfig) GetMaxSubscriptionClients() int {
return c.maxSubscriptionClients
}
12 changes: 10 additions & 2 deletions app/refund/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ func gasRefund(ik innertx.InnerTxKeeper, ak accountKeeperInterface, sk types.Sup
return nil, nil
}

if tx.GetType() == sdk.StdTxType && ctx.GetOutOfGas() {
return nil, nil
if tmtypes.HigherThanEarth(ctx.BlockHeight()) {
if ctx.GetOutOfGas() {
ctx.GasMeter().SetGas(ctx.GasMeter().Limit())
currentGasMeter.SetGas(gasLimit)
return nil, nil
}
} else {
if tx.GetType() == sdk.StdTxType && ctx.GetOutOfGas() {
return nil, nil
}
}

feeTx, ok := tx.(ante.FeeTx)
Expand Down
2 changes: 2 additions & 0 deletions app/rpc/tests/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/okx/okbchain/app/config"
"github.com/okx/okbchain/app/crypto/ethsecp256k1"
"github.com/okx/okbchain/app/rpc/backend"
cosmos_context "github.com/okx/okbchain/libs/cosmos-sdk/client/context"
Expand Down Expand Up @@ -148,6 +149,7 @@ func (suite *RPCTestSuite) SetupTest() {
viper.Set(flags.FlagKeyringBackend, "test")

viper.Set(rpc.FlagPersonalAPI, true)
viper.Set(config.FlagMaxSubscriptionClients, 100)

senderPv := suite.chain.SenderAccountPVBZ()
genesisAcc = suite.chain.SenderAccount().GetAddress()
Expand Down
4 changes: 4 additions & 0 deletions cmd/client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,9 @@ func RegisterAppFlag(cmd *cobra.Command) {
cmd.Flags().Int(backend.FlagLogsLimit, 0, "Maximum number of logs returned when calling eth_getLogs")
cmd.Flags().Int(backend.FlagLogsTimeout, 60, "Maximum query duration when calling eth_getLogs")
cmd.Flags().Int(websockets.FlagSubscribeLimit, 15, "Maximum subscription on a websocket connection")

// flags for tendermint rpc
cmd.Flags().Int(config.FlagMaxSubscriptionClients, 100, "Maximum number of unique clientIDs that Tendermint RPC server can /subscribe or /broadcast_tx_commit")

wasm.AddModuleInitFlags(cmd)
}
13 changes: 8 additions & 5 deletions libs/cosmos-sdk/baseapp/baseapp_runtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"runtime/debug"

"github.com/pkg/errors"

"github.com/okx/okbchain/libs/system/trace"

sdk "github.com/okx/okbchain/libs/cosmos-sdk/types"
sdkerrors "github.com/okx/okbchain/libs/cosmos-sdk/types/errors"
"github.com/okx/okbchain/libs/system/trace"
abci "github.com/okx/okbchain/libs/tendermint/abci/types"
tmtypes "github.com/okx/okbchain/libs/tendermint/types"
"github.com/pkg/errors"
)

type runTxInfo struct {
Expand Down Expand Up @@ -152,6 +151,10 @@ func (app *BaseApp) runtxWithInfo(info *runTxInfo, mode runTxMode, txBytes []byt
if (tx.GetType() == sdk.StdTxType && isAnteSucceed && err == nil) ||
tx.GetType() == sdk.EvmTxType {
handler.handleDeferRefund(info)
} else {
if tmtypes.HigherThanEarth(info.ctx.BlockHeight()) {
info.ctx.GasMeter().SetGas(info.ctx.GasMeter().Limit())
}
}
}()

Expand Down Expand Up @@ -386,7 +389,7 @@ func (app *BaseApp) runTx_defer_recover(r interface{}, info *runTxInfo) error {
err = sdkerrors.Wrap(
sdkerrors.ErrOutOfGas, fmt.Sprintf(
"out of gas in location: %v; gasWanted: %d, gasUsed: %d",
rType.Descriptor, info.gasWanted, info.ctx.GasMeter().GasConsumed(),
rType.Descriptor, info.gasWanted, info.gasWanted,
),
)

Expand Down
8 changes: 8 additions & 0 deletions libs/cosmos-sdk/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,12 @@ func SetExternalPackageValue(cmd *cobra.Command) {
consensus.SetActiveVC(viper.GetBool(FlagActiveViewChange))

tmtypes.EnableEventBlockTime = viper.GetBool(FlagEventBlockTime)

mptstore.TrieDirtyDisabled = viper.GetBool(mptstore.FlagTrieDirtyDisabled)
mptstore.TrieCacheSize = viper.GetUint(mptstore.FlagTrieCacheSize)
mptstore.TriesInMemory = viper.GetUint(mptstore.FlagTrieInMemory)
mptstore.TrieAsyncDB = viper.GetBool(mptstore.FlagTrieAsyncDB)
mptstore.TrieAsyncDBInitCap = viper.GetInt(mptstore.FlagTrieAsyncDBInitCap)
mptstore.TrieAsyncDBAutoPruningOff = viper.GetBool(mptstore.FlagTrieAsyncDBAutoPruningOff)
mptstore.TrieAsyncDBSyncPruning = viper.GetBool(mptstore.FlagTrieAsyncDBSyncPruning)
}
19 changes: 8 additions & 11 deletions libs/cosmos-sdk/server/start_okchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,15 @@ func RegisterServerFlags(cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(flags.FlagChainID, ChainID, "Chain ID of tendermint node for web3")
cmd.Flags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block) for web3")

cmd.Flags().BoolVar(&mpt.TrieDirtyDisabled, mpt.FlagTrieDirtyDisabled, false, "Disable cache dirty trie nodes")
cmd.Flags().UintVar(&mpt.TrieCacheSize, mpt.FlagTrieCacheSize, 2048, "Size (MB) to cache trie nodes")
cmd.Flags().UintVar(&mpt.TrieNodesLimit, mpt.FlagTrieNodesLimit, 256, "Max node size (MB) cached in triedb")
cmd.Flags().UintVar(&mpt.TrieImgsLimit, mpt.FlagTrieImgsLimit, 4, "Max img size (MB) cached in triedb")
cmd.Flags().UintVar(&mpt.TrieAccStoreCache, mpt.FlagTrieAccStoreCache, 32, "Size (MB) to cache account")
cmd.Flags().UintVar(&mpt.TriesInMemory, mpt.FlagTrieInMemory, 100, "Max cache tire count in Memory")
cmd.Flags().BoolVar(&mpt.TrieAsyncDB, mpt.FlagTrieAsyncDB, true, "Enable async commit to trie db")
cmd.Flags().IntVar(&mpt.TrieAsyncDBInitCap, mpt.FlagTrieAsyncDBInitCap, 200_0000, "Init cap of trie async db")
cmd.Flags().BoolVar(&mpt.TrieAsyncDBAutoPruningOff, mpt.FlagTrieAsyncDBAutoPruningOff, false, "Disable auto prune of trie async db")
cmd.Flags().BoolVar(&mpt.TrieAsyncDBSyncPruning, mpt.FlagTrieAsyncDBSyncPruning, false, "if auto pruning is off and this is on, trie async db will be pruned every block in sync mode")
cmd.Flags().Int64(FlagCommitGapHeight, 10, "Block interval to commit cached data into db, affects iavl & mpt")
cmd.Flags().Bool(mpt.FlagTrieDirtyDisabled, false, "Disable cache dirty trie nodes")
cmd.Flags().Uint(mpt.FlagTrieCacheSize, 2048, "Size (MB) to cache trie nodes")
cmd.Flags().Uint(mpt.FlagTrieInMemory, 100, "Max cache tire count in Memory")
cmd.Flags().Bool(mpt.FlagTrieAsyncDB, true, "Enable async commit to trie db")
cmd.Flags().Int(mpt.FlagTrieAsyncDBInitCap, 200_0000, "Init cap of trie async db")
cmd.Flags().Bool(mpt.FlagTrieAsyncDBAutoPruningOff, false, "Disable auto prune of trie async db")
cmd.Flags().Bool(mpt.FlagTrieAsyncDBSyncPruning, false, "if auto pruning is off and this is on, trie async db will be pruned every block in sync mode")

cmd.Flags().Int64(FlagCommitGapHeight, 10, "Block interval to commit cached data into db, affects iavl & mpt")
cmd.Flags().Int64(FlagFastSyncGap, 20, "Block height interval to switch fast-sync mode")

return cmd
Expand Down
2 changes: 0 additions & 2 deletions libs/cosmos-sdk/store/mpt/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ const (
var (
TrieDirtyDisabled = false
TrieCacheSize uint = 2048 // MB
TrieNodesLimit uint = 256 // MB
TrieImgsLimit uint = 4 // MB
TrieCommitGap int64 = 100
TriesInMemory uint = 100

Expand Down
2 changes: 0 additions & 2 deletions libs/cosmos-sdk/store/mpt/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ const (
)

var (
TrieAccStoreCache uint = 32 // MB

AccountStateRootRetriever StateRootRetriever = EmptyStateRootRetriever{}

applyDelta = false
Expand Down
9 changes: 9 additions & 0 deletions libs/cosmos-sdk/store/types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type GasMeter interface {
GasConsumedToLimit() Gas
Limit() Gas
ConsumeGas(amount Gas, descriptor string)
SetGas(val Gas)
IsPastLimit() bool
IsOutOfGas() bool
}
Expand Down Expand Up @@ -96,6 +97,10 @@ func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) {
}
}

func (g *basicGasMeter) SetGas(val Gas) {
g.consumed = val
}

func (g *basicGasMeter) IsPastLimit() bool {
return g.consumed > g.limit
}
Expand Down Expand Up @@ -148,6 +153,10 @@ func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) {
}
}

func (g *infiniteGasMeter) SetGas(val Gas) {
g.consumed = val
}

func (g *infiniteGasMeter) IsPastLimit() bool {
return false
}
Expand Down
21 changes: 17 additions & 4 deletions libs/tendermint/config/dynamic_config_okchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type IDynamicConfig interface {
GetDynamicGpMaxTxNum() int64
GetDynamicGpMaxGasUsed() int64
GetGasLimitBuffer() uint64
GetMaxSubscriptionClients() int
}

var DynamicConfig IDynamicConfig = MockDynamicConfig{}
Expand All @@ -46,10 +47,11 @@ func SetDynamicConfig(c IDynamicConfig) {
}

type MockDynamicConfig struct {
enableDeleteMinGPTx bool
dynamicGpMode int
dynamicGpMaxTxNum int64
dynamicGpMaxGasUsed int64
enableDeleteMinGPTx bool
dynamicGpMode int
dynamicGpMaxTxNum int64
dynamicGpMaxGasUsed int64
maxSubscriptionClients int
}

func (d MockDynamicConfig) GetMempoolRecheck() bool {
Expand Down Expand Up @@ -205,3 +207,14 @@ func (d MockDynamicConfig) GetDynamicGpMaxGasUsed() int64 {
func (d MockDynamicConfig) GetGasLimitBuffer() uint64 {
return 0
}

func (d MockDynamicConfig) GetMaxSubscriptionClients() int {
return d.maxSubscriptionClients
}

func (d *MockDynamicConfig) SetMaxSubscriptionClients(value int) {
if value < 0 {
return
}
d.maxSubscriptionClients = value
}
9 changes: 9 additions & 0 deletions libs/tendermint/lite/proxy/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/okx/okbchain/libs/tendermint/abci/example/kvstore"
cfg "github.com/okx/okbchain/libs/tendermint/config"
"github.com/okx/okbchain/libs/tendermint/crypto/merkle"
"github.com/okx/okbchain/libs/tendermint/lite"
certclient "github.com/okx/okbchain/libs/tendermint/lite/client"
Expand Down Expand Up @@ -126,6 +127,7 @@ func _TestAppProofs(t *testing.T) {
}

func TestTxProofs(t *testing.T) {
setMocConfig(100)
assert, require := assert.New(t), require.New(t)

cl := rpclocal.New(node)
Expand Down Expand Up @@ -163,3 +165,10 @@ func TestTxProofs(t *testing.T) {
require.Nil(err, "%#v", err)
require.Equal(res.Proof.RootHash, commit.Header.DataHash)
}

func setMocConfig(clientNum int) {
moc := cfg.MockDynamicConfig{}
moc.SetMaxSubscriptionClients(100)

cfg.SetDynamicConfig(moc)
}
12 changes: 12 additions & 0 deletions libs/tendermint/rpc/client/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

abci "github.com/okx/okbchain/libs/tendermint/abci/types"
cfg "github.com/okx/okbchain/libs/tendermint/config"
tmrand "github.com/okx/okbchain/libs/tendermint/libs/rand"
"github.com/okx/okbchain/libs/tendermint/rpc/client"
ctypes "github.com/okx/okbchain/libs/tendermint/rpc/core/types"
Expand All @@ -27,6 +28,7 @@ func MakeTxKV() ([]byte, []byte, []byte) {
}

func TestHeaderEvents(t *testing.T) {
setMocConfig(100)
for i, c := range GetClients() {
i, c := i, c // capture params
t.Run(reflect.TypeOf(c).String(), func(t *testing.T) {
Expand All @@ -49,6 +51,7 @@ func TestHeaderEvents(t *testing.T) {
}

func TestBlockEvents(t *testing.T) {
setMocConfig(100)
for i, c := range GetClients() {
i, c := i, c // capture params
t.Run(reflect.TypeOf(c).String(), func(t *testing.T) {
Expand Down Expand Up @@ -97,6 +100,7 @@ func TestTxEventsSentWithBroadcastTxAsync(t *testing.T) { testTxEventsSent(t, "a
func TestTxEventsSentWithBroadcastTxSync(t *testing.T) { testTxEventsSent(t, "sync") }

func testTxEventsSent(t *testing.T, broadcastMethod string) {
setMocConfig(100)
for i, c := range GetClients() {
i, c := i, c // capture params
t.Run(reflect.TypeOf(c).String(), func(t *testing.T) {
Expand Down Expand Up @@ -150,6 +154,7 @@ func TestClientsResubscribe(t *testing.T) {
}

func TestHTTPReturnsErrorIfClientIsNotRunning(t *testing.T) {
setMocConfig(100)
c := getHTTPClient()

// on Subscribe
Expand All @@ -166,3 +171,10 @@ func TestHTTPReturnsErrorIfClientIsNotRunning(t *testing.T) {
err = c.UnsubscribeAll(context.Background(), "TestHeaderEvents")
assert.Error(t, err)
}

func setMocConfig(clientNum int) {
moc := cfg.MockDynamicConfig{}
moc.SetMaxSubscriptionClients(100)

cfg.SetDynamicConfig(moc)
}
5 changes: 3 additions & 2 deletions libs/tendermint/rpc/core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pkg/errors"

"github.com/okx/okbchain/libs/tendermint/config"
tmpubsub "github.com/okx/okbchain/libs/tendermint/libs/pubsub"
tmquery "github.com/okx/okbchain/libs/tendermint/libs/pubsub/query"
ctypes "github.com/okx/okbchain/libs/tendermint/rpc/core/types"
Expand All @@ -22,8 +23,8 @@ const (
func Subscribe(ctx *rpctypes.Context, query string) (*ctypes.ResultSubscribe, error) {
addr := ctx.RemoteAddr()

if env.EventBus.NumClients() >= env.Config.MaxSubscriptionClients {
return nil, fmt.Errorf("max_subscription_clients %d reached", env.Config.MaxSubscriptionClients)
if env.EventBus.NumClients() >= config.DynamicConfig.GetMaxSubscriptionClients() {
return nil, fmt.Errorf("max_subscription_clients %d reached", config.DynamicConfig.GetMaxSubscriptionClients())
} else if env.EventBus.NumClientSubscriptions(addr) >= env.Config.MaxSubscriptionsPerClient {
return nil, fmt.Errorf("max_subscriptions_per_client %d reached", env.Config.MaxSubscriptionsPerClient)
}
Expand Down
5 changes: 3 additions & 2 deletions libs/tendermint/rpc/core/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"

abci "github.com/okx/okbchain/libs/tendermint/abci/types"
"github.com/okx/okbchain/libs/tendermint/config"
mempl "github.com/okx/okbchain/libs/tendermint/mempool"
ctypes "github.com/okx/okbchain/libs/tendermint/rpc/core/types"
rpctypes "github.com/okx/okbchain/libs/tendermint/rpc/jsonrpc/types"
Expand Down Expand Up @@ -61,8 +62,8 @@ func BroadcastTxSync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcas
func BroadcastTxCommit(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
subscriber := ctx.RemoteAddr()

if env.EventBus.NumClients() >= env.Config.MaxSubscriptionClients {
return nil, fmt.Errorf("max_subscription_clients %d reached", env.Config.MaxSubscriptionClients)
if env.EventBus.NumClients() >= config.DynamicConfig.GetMaxSubscriptionClients() {
return nil, fmt.Errorf("max_subscription_clients %d reached", config.DynamicConfig.GetMaxSubscriptionClients())
} else if env.EventBus.NumClientSubscriptions(subscriber) >= env.Config.MaxSubscriptionsPerClient {
return nil, fmt.Errorf("max_subscriptions_per_client %d reached", env.Config.MaxSubscriptionsPerClient)
}
Expand Down
Loading

0 comments on commit 6024a41

Please sign in to comment.