mirror of
https://github.com/0glabs/0g-chain.git
synced 2025-02-23 12:36:45 +00:00
Merge pull request #108 from 0g-wh/suggestion_gas_price
update txn selection algo
This commit is contained in:
commit
4f53e59af7
@ -265,41 +265,47 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan
|
|||||||
}
|
}
|
||||||
|
|
||||||
remaing := gasPriceSuggestionBlockNum * int64(maxBlockGas)
|
remaing := gasPriceSuggestionBlockNum * int64(maxBlockGas)
|
||||||
for len(txnInfoMap) > 0 && remaing > 0 {
|
var lastProcessedTx *txnInfo
|
||||||
|
|
||||||
// pop each sender's first continuous decreasing subsequence
|
for remaing > 0 && len(txnInfoMap) > 0 {
|
||||||
txnCnt := 0
|
// Find the highest gas price among the first transaction of each account
|
||||||
senderNonceSortedSliceGroup := make([][]*txnInfo, 0, senderCnt)
|
var highestGasPrice *big.Int
|
||||||
for sender := range txnInfoMap {
|
var selectedSender string
|
||||||
endIndex := findFirstContinuousDecreasingSubsequence(txnInfoMap[sender])
|
|
||||||
appendSlice := txnInfoMap[sender][:endIndex]
|
// Compare first transaction (lowest nonce) from each account
|
||||||
senderNonceSortedSliceGroup = append(senderNonceSortedSliceGroup, appendSlice)
|
for sender, txns := range txnInfoMap {
|
||||||
txnCnt += len(appendSlice)
|
if len(txns) == 0 {
|
||||||
txnInfoMap[sender] = txnInfoMap[sender][endIndex:]
|
|
||||||
if len(txnInfoMap[sender]) == 0 {
|
|
||||||
delete(txnInfoMap, sender)
|
delete(txnInfoMap, sender)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// First tx has lowest nonce due to earlier sorting
|
||||||
|
if highestGasPrice == nil || txns[0].gasPrice.Cmp(highestGasPrice) > 0 {
|
||||||
|
highestGasPrice = txns[0].gasPrice
|
||||||
|
selectedSender = sender
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var gasPriceSortedSlice []*txnInfo
|
if selectedSender == "" {
|
||||||
if len(senderNonceSortedSliceGroup) > 0 {
|
break
|
||||||
gasPriceSortedSlice = make([]*txnInfo, 0, len(senderNonceSortedSliceGroup[0]))
|
|
||||||
for i := range senderNonceSortedSliceGroup {
|
|
||||||
gasPriceSortedSlice = mergeSort(len(gasPriceSortedSlice)+len(senderNonceSortedSliceGroup[i]), gasPriceSortedSlice, senderNonceSortedSliceGroup[i])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range gasPriceSortedSlice {
|
// Process the selected transaction
|
||||||
remaing -= int64(gasPriceSortedSlice[i].gasLimit)
|
selectedTx := txnInfoMap[selectedSender][0]
|
||||||
if remaing <= 0 {
|
remaing -= int64(selectedTx.gasLimit)
|
||||||
h.feemarketKeeper.SetSuggestionGasPrice(ctx, gasPriceSortedSlice[i].gasPrice)
|
lastProcessedTx = selectedTx
|
||||||
break
|
|
||||||
}
|
// Remove processed transaction
|
||||||
|
txnInfoMap[selectedSender] = txnInfoMap[selectedSender][1:]
|
||||||
|
if len(txnInfoMap[selectedSender]) == 0 {
|
||||||
|
delete(txnInfoMap, selectedSender)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if remaing > 0 {
|
if lastProcessedTx != nil && remaing <= 0 {
|
||||||
h.feemarketKeeper.SetSuggestionGasPrice(ctx, big.NewInt(0))
|
h.feemarketKeeper.SetSuggestionGasPrice(ctx, lastProcessedTx.gasPrice)
|
||||||
|
} else {
|
||||||
|
h.feemarketKeeper.SetSuggestionGasPrice(ctx, big.NewInt(1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user