Skip to content

Commit

Permalink
Merge pull request #6 from binance-chain/disable_service
Browse files Browse the repository at this point in the history
[R4R]disable service when network is congest
  • Loading branch information
yutianwu authored Sep 16, 2021
2 parents 7af1538 + 63575c2 commit eaf52ab
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"math/big"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -55,6 +56,9 @@ const (
UnHealthyTimeout = 5 * time.Second
MaxBundleBlockDelay = 1200
MaxBundleTimeDelay = 60 * 60 // second
MaxOracleBlocks = 21
DropBlocks = 3
MaxGasUsedRatio = 90
)

// PublicEthereumAPI provides an API to access Ethereum related information.
Expand Down Expand Up @@ -2317,6 +2321,33 @@ func (s *PrivateTxBundleAPI) BundlePrice(ctx context.Context) (*big.Int, error)
// SendBundle will add the signed transaction to the transaction pool.
// The sender is responsible for signing the transaction and using the correct nonce and ensuring validity
func (s *PrivateTxBundleAPI) SendBundle(ctx context.Context, args SendBundleArgs) (common.Hash, error) {
gasUsedRatio := make([]int, 0, MaxOracleBlocks)
block := s.b.CurrentBlock()
var err error
for i := 0; i < MaxOracleBlocks && block.NumberU64() > 1; i++ {
gasUsedRatio = append(gasUsedRatio, int(block.GasUsed()*100/block.GasLimit()))
block, err = s.b.BlockByHash(context.Background(), block.ParentHash())
if err != nil {
break
}
}
sort.Ints(gasUsedRatio)
validGasUsedRatio := gasUsedRatio
if len(gasUsedRatio) > DropBlocks {
validGasUsedRatio = gasUsedRatio[DropBlocks:]
}
if len(validGasUsedRatio) == 0 {
return common.Hash{}, errors.New("no enough example ratio")
}
var totalRatio int
for _, ratio := range validGasUsedRatio {
totalRatio += ratio
}
averageRatio := totalRatio / len(validGasUsedRatio)
if averageRatio >= MaxGasUsedRatio {
return common.Hash{}, errors.New("the network is congested, please try later")
}

var txs types.Transactions
if len(args.Txs) == 0 {
return common.Hash{}, errors.New("bundle missing txs")
Expand Down Expand Up @@ -2347,7 +2378,6 @@ func (s *PrivateTxBundleAPI) SendBundle(ctx context.Context, args SendBundleArgs
}
txs = append(txs, tx)
}

var minTimestamp, maxTimestamp uint64
if args.MinTimestamp != nil {
minTimestamp = *args.MinTimestamp
Expand Down

0 comments on commit eaf52ab

Please sign in to comment.