github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/gp_index.go (about) 1 package app 2 3 import ( 4 "math" 5 "math/big" 6 "sort" 7 ) 8 9 type GasPriceIndex struct { 10 RecommendGp *big.Int `json:"recommend-gp"` 11 } 12 13 func CalBlockGasPriceIndex(blockGasPrice []*big.Int, weight int) GasPriceIndex { 14 num := len(blockGasPrice) 15 if num == 0 { 16 return GasPriceIndex{} 17 } 18 19 sort.SliceStable(blockGasPrice, func(i, j int) bool { 20 return blockGasPrice[i].Cmp(blockGasPrice[j]) < 0 21 }) 22 23 idx := int(math.Round(float64(weight) / 100.0 * float64(num))) 24 if idx > 0 { 25 idx -= 1 26 } 27 28 return GasPriceIndex{ 29 RecommendGp: blockGasPrice[idx], 30 } 31 }