Remove Tx Throttling logic

pull/1806/head
Rongjian Lan 5 years ago
parent 41d2c4a6b2
commit 77623dcaee
  1. 40
      internal/configs/sharding/fixedschedule.go
  2. 46
      internal/configs/sharding/localnet.go
  3. 46
      internal/configs/sharding/mainnet.go
  4. 42
      internal/configs/sharding/pangaea.go
  5. 67
      internal/configs/sharding/shardingconfig.go
  6. 46
      internal/configs/sharding/testnet.go
  7. 4
      node/node.go
  8. 43
      node/worker/worker.go

@ -2,9 +2,6 @@ package shardingconfig
import (
"math/big"
"time"
"github.com/harmony-one/harmony/common/denominations"
)
const (
@ -55,43 +52,6 @@ func (s fixedSchedule) RandomnessStartingEpoch() uint64 {
return mainnetRandomnessStartingEpoch
}
func (s fixedSchedule) MaxTxAmountLimit() *big.Int {
amountBigInt := big.NewInt(mainnetMaxTxAmountLimit)
amountBigInt = amountBigInt.Mul(amountBigInt, big.NewInt(denominations.One))
return amountBigInt
}
func (s fixedSchedule) MaxNumRecentTxsPerAccountLimit() uint64 {
return mainnetMaxNumRecentTxsPerAccountLimit
}
func (s fixedSchedule) MaxTxPoolSizeLimit() int {
return mainnetMaxTxPoolSizeLimit
}
func (s fixedSchedule) MaxNumTxsPerBlockLimit() int {
return mainnetMaxNumTxsPerBlockLimit
}
func (s fixedSchedule) RecentTxDuration() time.Duration {
return mainnetRecentTxDuration
}
func (s fixedSchedule) EnableTxnThrottling() bool {
return mainnetEnableTxnThrottling
}
func (s fixedSchedule) TxsThrottleConfig() *TxsThrottleConfig {
return &TxsThrottleConfig{
MaxTxAmountLimit: s.MaxTxAmountLimit(),
MaxNumRecentTxsPerAccountLimit: s.MaxNumRecentTxsPerAccountLimit(),
MaxTxPoolSizeLimit: s.MaxTxPoolSizeLimit(),
MaxNumTxsPerBlockLimit: s.MaxNumTxsPerBlockLimit(),
RecentTxDuration: s.RecentTxDuration(),
EnableTxnThrottling: s.EnableTxnThrottling(),
}
}
func (s fixedSchedule) GetNetworkID() NetworkID {
return DevNet
}

@ -3,9 +3,7 @@ package shardingconfig
import (
"fmt"
"math/big"
"time"
"github.com/harmony-one/harmony/common/denominations"
"github.com/harmony-one/harmony/internal/genesis"
)
@ -26,13 +24,6 @@ const (
localnetConsensusRatio = float64(0.1)
localnetRandomnessStartingEpoch = 0
localnetMaxTxAmountLimit = 1e3 // unit is in One
localnetMaxNumRecentTxsPerAccountLimit = 1e2
localnetMaxTxPoolSizeLimit = 8000
localnetMaxNumTxsPerBlockLimit = 1000
localnetRecentTxDuration = time.Hour
localnetEnableTxnThrottling = false
)
func (localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
@ -97,43 +88,6 @@ func (ls localnetSchedule) RandomnessStartingEpoch() uint64 {
return localnetRandomnessStartingEpoch
}
func (ls localnetSchedule) MaxTxAmountLimit() *big.Int {
amountBigInt := big.NewInt(localnetMaxTxAmountLimit)
amountBigInt = amountBigInt.Mul(amountBigInt, big.NewInt(denominations.One))
return amountBigInt
}
func (ls localnetSchedule) MaxNumRecentTxsPerAccountLimit() uint64 {
return localnetMaxNumRecentTxsPerAccountLimit
}
func (ls localnetSchedule) MaxTxPoolSizeLimit() int {
return localnetMaxTxPoolSizeLimit
}
func (ls localnetSchedule) MaxNumTxsPerBlockLimit() int {
return localnetMaxNumTxsPerBlockLimit
}
func (ls localnetSchedule) RecentTxDuration() time.Duration {
return localnetRecentTxDuration
}
func (ls localnetSchedule) EnableTxnThrottling() bool {
return localnetEnableTxnThrottling
}
func (ls localnetSchedule) TxsThrottleConfig() *TxsThrottleConfig {
return &TxsThrottleConfig{
MaxTxAmountLimit: ls.MaxTxAmountLimit(),
MaxNumRecentTxsPerAccountLimit: ls.MaxNumRecentTxsPerAccountLimit(),
MaxTxPoolSizeLimit: ls.MaxTxPoolSizeLimit(),
MaxNumTxsPerBlockLimit: ls.MaxNumTxsPerBlockLimit(),
RecentTxDuration: ls.RecentTxDuration(),
EnableTxnThrottling: ls.EnableTxnThrottling(),
}
}
func (ls localnetSchedule) GetNetworkID() NetworkID {
return LocalNet
}

@ -2,9 +2,7 @@ package shardingconfig
import (
"math/big"
"time"
"github.com/harmony-one/harmony/common/denominations"
"github.com/harmony-one/harmony/internal/genesis"
)
@ -29,13 +27,6 @@ const (
mainnetV1_4Epoch = 46
mainnetV1_5Epoch = 54
mainnetMaxTxAmountLimit = 1e3 // unit is interface{} One
mainnetMaxNumRecentTxsPerAccountLimit = 1e2
mainnetMaxTxPoolSizeLimit = 8000
mainnetMaxNumTxsPerBlockLimit = 1000
mainnetRecentTxDuration = time.Hour
mainnetEnableTxnThrottling = false
// MainNetHTTPPattern is the http pattern for mainnet.
MainNetHTTPPattern = "https://api.s%d.t.hmny.io"
// MainNetWSPattern is the websocket pattern for mainnet.
@ -135,43 +126,6 @@ func (ms mainnetSchedule) RandomnessStartingEpoch() uint64 {
return mainnetRandomnessStartingEpoch
}
func (ms mainnetSchedule) MaxTxAmountLimit() *big.Int {
amountBigInt := big.NewInt(mainnetMaxTxAmountLimit)
amountBigInt = amountBigInt.Mul(amountBigInt, big.NewInt(denominations.One))
return amountBigInt
}
func (ms mainnetSchedule) MaxNumRecentTxsPerAccountLimit() uint64 {
return mainnetMaxNumRecentTxsPerAccountLimit
}
func (ms mainnetSchedule) MaxTxPoolSizeLimit() int {
return mainnetMaxTxPoolSizeLimit
}
func (ms mainnetSchedule) MaxNumTxsPerBlockLimit() int {
return mainnetMaxNumTxsPerBlockLimit
}
func (ms mainnetSchedule) RecentTxDuration() time.Duration {
return mainnetRecentTxDuration
}
func (ms mainnetSchedule) EnableTxnThrottling() bool {
return mainnetEnableTxnThrottling
}
func (ms mainnetSchedule) TxsThrottleConfig() *TxsThrottleConfig {
return &TxsThrottleConfig{
MaxTxAmountLimit: ms.MaxTxAmountLimit(),
MaxNumRecentTxsPerAccountLimit: ms.MaxNumRecentTxsPerAccountLimit(),
MaxTxPoolSizeLimit: ms.MaxTxPoolSizeLimit(),
MaxNumTxsPerBlockLimit: ms.MaxNumTxsPerBlockLimit(),
RecentTxDuration: ms.RecentTxDuration(),
EnableTxnThrottling: ms.EnableTxnThrottling(),
}
}
func (ms mainnetSchedule) GetNetworkID() NetworkID {
return MainNet
}

@ -2,11 +2,8 @@ package shardingconfig
import (
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/common/denominations"
"github.com/harmony-one/harmony/internal/genesis"
)
@ -15,8 +12,6 @@ const (
PangaeaHTTPPattern = "https://api.s%d.pga.hmny.io"
// PangaeaWSPattern is the websocket pattern for pangaea.
PangaeaWSPattern = "wss://ws.s%d.pga.hmny.io"
// transaction throttling disabled on pangaea network
pangaeaEnableTxnThrottling = false
)
// PangaeaSchedule is the Pangaea sharding configuration schedule.
@ -64,43 +59,6 @@ func (ps pangaeaSchedule) RandomnessStartingEpoch() uint64 {
return mainnetRandomnessStartingEpoch
}
func (ps pangaeaSchedule) MaxTxAmountLimit() *big.Int {
amountBigInt := big.NewInt(mainnetMaxTxAmountLimit)
amountBigInt = amountBigInt.Mul(amountBigInt, big.NewInt(denominations.One))
return amountBigInt
}
func (ps pangaeaSchedule) MaxNumRecentTxsPerAccountLimit() uint64 {
return mainnetMaxNumRecentTxsPerAccountLimit
}
func (ps pangaeaSchedule) MaxTxPoolSizeLimit() int {
return mainnetMaxTxPoolSizeLimit
}
func (ps pangaeaSchedule) MaxNumTxsPerBlockLimit() int {
return mainnetMaxNumTxsPerBlockLimit
}
func (ps pangaeaSchedule) RecentTxDuration() time.Duration {
return mainnetRecentTxDuration
}
func (ps pangaeaSchedule) EnableTxnThrottling() bool {
return pangaeaEnableTxnThrottling
}
func (ps pangaeaSchedule) TxsThrottleConfig() *TxsThrottleConfig {
return &TxsThrottleConfig{
MaxTxAmountLimit: ps.MaxTxAmountLimit(),
MaxNumRecentTxsPerAccountLimit: ps.MaxNumRecentTxsPerAccountLimit(),
MaxTxPoolSizeLimit: ps.MaxTxPoolSizeLimit(),
MaxNumTxsPerBlockLimit: ps.MaxNumTxsPerBlockLimit(),
RecentTxDuration: ps.RecentTxDuration(),
EnableTxnThrottling: ps.EnableTxnThrottling(),
}
}
func (pangaeaSchedule) GetNetworkID() NetworkID {
return Pangaea
}

@ -5,7 +5,6 @@ package shardingconfig
import (
"fmt"
"math/big"
"time"
"github.com/harmony-one/harmony/internal/genesis"
)
@ -37,27 +36,6 @@ type Schedule interface {
//RandomnessStartingEpoch returns starting epoch of randonness generation
RandomnessStartingEpoch() uint64
// Max amount limit for a valid transaction
MaxTxAmountLimit() *big.Int
// Max number of transactions of a particular account per block level
MaxNumRecentTxsPerAccountLimit() uint64
// Max total number of transactions allowed as pending transactions in transaction pool
MaxTxPoolSizeLimit() int
// Max total number of transactions allowed to be processed per block
MaxNumTxsPerBlockLimit() int
// How long "recent" means for transaction in time Duration unit
RecentTxDuration() time.Duration
// EnableTxnThrottling is the switch for transaction throttling
EnableTxnThrottling() bool
// configuration for throttling pending transactions
TxsThrottleConfig() *TxsThrottleConfig
// GetNetworkID() return networkID type.
GetNetworkID() NetworkID
@ -90,51 +68,6 @@ type Instance interface {
ReshardingEpoch() []*big.Int
}
// TxThrottleFlag is the throttling flag for each transaction
// Refer below enum declaration for more context.
type TxThrottleFlag int
// TxThrottleFlag is determined per transaction
// during the new block proposal and pending transactions throttling
const (
TxSelect TxThrottleFlag = iota
TxUnselect
TxInvalid
)
func (result TxThrottleFlag) String() string {
switch result {
case TxSelect:
return "TxSelect"
case TxUnselect:
return "TxUnselect"
case TxInvalid:
return "TxInvalid"
}
return "TxThrottleUnknown"
}
// TxsThrottleConfig contains configuration for throttling pending transactions per node block
type TxsThrottleConfig struct {
// Max amount limit for a valid transaction
MaxTxAmountLimit *big.Int
// Max number of transactions of a particular account for the past hour
RecentTxDuration time.Duration
// Max number of transactions of a particular account for the past hour
MaxNumRecentTxsPerAccountLimit uint64
// Max total number of transactions allowed as pending transactions in transaction pool
MaxTxPoolSizeLimit int
// Max total number of transactions allowed to be processed per block
MaxNumTxsPerBlockLimit int
// EnableTxnThrottling is the switch for transaction throttling
EnableTxnThrottling bool
}
// genShardingStructure return sharding structure, given shard number and its patterns.
func genShardingStructure(shardNum, shardID int, httpPattern, wsPattern string) []map[string]interface{} {
res := []map[string]interface{}{}

@ -2,9 +2,7 @@ package shardingconfig
import (
"math/big"
"time"
"github.com/harmony-one/harmony/common/denominations"
"github.com/harmony-one/harmony/internal/genesis"
)
@ -20,13 +18,6 @@ const (
testnetVdfDifficulty = 10000 // This takes about 20s to finish the vdf
testnetMaxTxAmountLimit = 1e3 // unit is in One
testnetMaxNumRecentTxsPerAccountLimit = 1e2
testnetMaxTxPoolSizeLimit = 8000
testnetMaxNumTxsPerBlockLimit = 1000
testnetRecentTxDuration = time.Hour
testnetEnableTxnThrottling = false
// TestNetHTTPPattern is the http pattern for testnet.
TestNetHTTPPattern = "https://api.s%d.b.hmny.io"
// TestNetWSPattern is the websocket pattern for testnet.
@ -72,43 +63,6 @@ func (ts testnetSchedule) RandomnessStartingEpoch() uint64 {
return mainnetRandomnessStartingEpoch
}
func (ts testnetSchedule) MaxTxAmountLimit() *big.Int {
amountBigInt := big.NewInt(testnetMaxTxAmountLimit)
amountBigInt = amountBigInt.Mul(amountBigInt, big.NewInt(denominations.One))
return amountBigInt
}
func (ts testnetSchedule) MaxNumRecentTxsPerAccountLimit() uint64 {
return testnetMaxNumRecentTxsPerAccountLimit
}
func (ts testnetSchedule) MaxTxPoolSizeLimit() int {
return testnetMaxTxPoolSizeLimit
}
func (ts testnetSchedule) MaxNumTxsPerBlockLimit() int {
return testnetMaxNumTxsPerBlockLimit
}
func (ts testnetSchedule) RecentTxDuration() time.Duration {
return testnetRecentTxDuration
}
func (ts testnetSchedule) EnableTxnThrottling() bool {
return testnetEnableTxnThrottling
}
func (ts testnetSchedule) TxsThrottleConfig() *TxsThrottleConfig {
return &TxsThrottleConfig{
MaxTxAmountLimit: ts.MaxTxAmountLimit(),
MaxNumRecentTxsPerAccountLimit: ts.MaxNumRecentTxsPerAccountLimit(),
MaxTxPoolSizeLimit: ts.MaxTxPoolSizeLimit(),
MaxNumTxsPerBlockLimit: ts.MaxNumTxsPerBlockLimit(),
RecentTxDuration: ts.RecentTxDuration(),
EnableTxnThrottling: ts.EnableTxnThrottling(),
}
}
func (ts testnetSchedule) GetNetworkID() NetworkID {
return TestNet
}

@ -51,8 +51,6 @@ const (
)
const (
// TxPoolLimit is the limit of transaction pool.
TxPoolLimit = 20000
// NumTryBroadCast is the number of times trying to broadcast
NumTryBroadCast = 3
// ClientRxQueueSize is the number of client messages to queue before tail-dropping.
@ -289,7 +287,7 @@ func (node *Node) addPendingTransactions(newTxs types.Transactions) {
// Add new staking transactions to the pending staking transaction list.
func (node *Node) addPendingStakingTransactions(newStakingTxs staking.StakingTransactions) {
txPoolLimit := core.ShardingSchedule.MaxTxPoolSizeLimit()
txPoolLimit := 1000 // TODO: incorporate staking txn into TxPool
node.pendingStakingTxMutex.Lock()
for _, tx := range newStakingTxs {
if _, ok := node.pendingStakingTransactions[tx.Hash()]; !ok {

@ -17,7 +17,6 @@ import (
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/values"
"github.com/harmony-one/harmony/core/vm"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/utils"
@ -54,48 +53,6 @@ type Worker struct {
gasCeil uint64
}
// Returns a tuple where the first value is the txs sender account address,
// the second is the throttling result enum for the transaction of interest.
// Throttling happens based on the amount, frequency, etc.
func (w *Worker) throttleTxs(selected types.Transactions, recentTxsStats types.RecentTxsStats, txsThrottleConfig *shardingconfig.TxsThrottleConfig, tx *types.Transaction) (common.Address, shardingconfig.TxThrottleFlag) {
var sender common.Address
msg, err := tx.AsMessage(types.MakeSigner(w.config, w.chain.CurrentBlock().Epoch()))
if err != nil {
utils.Logger().Error().Err(err).Str("txId", tx.Hash().Hex()).Msg("Error when parsing tx into message")
} else {
sender = msg.From()
}
// do not throttle transactions if disabled
if !txsThrottleConfig.EnableTxnThrottling {
return sender, shardingconfig.TxSelect
}
// already selected max num txs
if len(selected) > txsThrottleConfig.MaxNumTxsPerBlockLimit {
utils.Logger().Info().Str("txId", tx.Hash().Hex()).Int("MaxNumTxsPerBlockLimit", txsThrottleConfig.MaxNumTxsPerBlockLimit).Msg("Throttling tx with max num txs per block limit")
return sender, shardingconfig.TxUnselect
}
// throttle a single sender sending too many transactions in one block
if tx.Value().Cmp(txsThrottleConfig.MaxTxAmountLimit) > 0 {
utils.Logger().Info().Str("txId", tx.Hash().Hex()).Uint64("MaxTxAmountLimit", txsThrottleConfig.MaxTxAmountLimit.Uint64()).Uint64("txAmount", tx.Value().Uint64()).Msg("Throttling tx with max amount limit")
return sender, shardingconfig.TxInvalid
}
// throttle too large transaction
var numTxsPastHour uint64
for _, blockTxsCounts := range recentTxsStats {
numTxsPastHour += blockTxsCounts[sender]
}
if numTxsPastHour >= txsThrottleConfig.MaxNumRecentTxsPerAccountLimit {
utils.Logger().Info().Str("txId", tx.Hash().Hex()).Uint64("MaxNumRecentTxsPerAccountLimit", txsThrottleConfig.MaxNumRecentTxsPerAccountLimit).Msg("Throttling tx with max txs per account in a single block limit")
return sender, shardingconfig.TxInvalid
}
return sender, shardingconfig.TxSelect
}
// CommitTransactions commits transactions for new block.
func (w *Worker) CommitTransactions(pendingNormal map[common.Address]types.Transactions, pendingStaking staking.StakingTransactions, coinbase common.Address) error {

Loading…
Cancel
Save