From 7adb98fbe8d0cc7699904cf49a64a6c12df8e53f Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 18 Jul 2023 19:43:36 +0800 Subject: [PATCH] update gas fee --- contracts/trc21issuer/simulation/test/main.go | 34 +++++++++---------- contracts/trc21issuer/trc21issuer_test.go | 15 +++----- core/chain_makers.go | 5 +-- core/state_processor.go | 10 ++---- eth/api_tracer.go | 5 +-- miner/worker.go | 10 ++---- 6 files changed, 27 insertions(+), 52 deletions(-) diff --git a/contracts/trc21issuer/simulation/test/main.go b/contracts/trc21issuer/simulation/test/main.go index ef2eb197e8..1ee2aff60e 100644 --- a/contracts/trc21issuer/simulation/test/main.go +++ b/contracts/trc21issuer/simulation/test/main.go @@ -4,15 +4,16 @@ import ( "context" "encoding/json" "fmt" + "log" + "math/big" + "time" + "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind" "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common/hexutil" "github.com/XinFinOrg/XDPoSChain/contracts/trc21issuer" "github.com/XinFinOrg/XDPoSChain/contracts/trc21issuer/simulation" "github.com/XinFinOrg/XDPoSChain/ethclient" - "log" - "math/big" - "time" ) var ( @@ -48,11 +49,10 @@ func airDropTokenToAccountNoXDC() { if err != nil { log.Fatal("can't transaction's receipt ", err, "hash", tx.Hash().Hex()) } - fee := big.NewInt(0).SetUint64(hexutil.MustDecodeUint64(receipt["gasUsed"].(string))) - if hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) > common.TIPTRC21Fee.Uint64() { - fee = fee.Mul(fee, common.TRC21GasPrice) - } - fmt.Println("fee", fee.Uint64(), "number", hexutil.MustDecodeUint64(receipt["blockNumber"].(string))) + gasUsed := hexutil.MustDecodeUint64(receipt["gasUsed"].(string)) + blockNumber := hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) + fee := common.GetGasFee(blockNumber, gasUsed) + fmt.Println("fee", fee.Uint64(), "number", blockNumber) remainFee = big.NewInt(0).Sub(remainFee, fee) //check balance fee balanceIssuerFee, err := trc21IssuerInstance.GetTokenCapacity(trc21TokenAddr) @@ -111,11 +111,10 @@ func testTransferTRC21TokenWithAccountNoXDC() { if err != nil { log.Fatal("can't transaction's receipt ", err, "hash", tx.Hash().Hex()) } - fee := big.NewInt(0).SetUint64(hexutil.MustDecodeUint64(receipt["gasUsed"].(string))) - if hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) > common.TIPTRC21Fee.Uint64() { - fee = fee.Mul(fee, common.TRC21GasPrice) - } - fmt.Println("fee", fee.Uint64(), "number", hexutil.MustDecodeUint64(receipt["blockNumber"].(string))) + gasUsed := hexutil.MustDecodeUint64(receipt["gasUsed"].(string)) + blockNumber := hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) + fee := common.GetGasFee(blockNumber, gasUsed) + fmt.Println("fee", fee.Uint64(), "number", blockNumber) remainFee = big.NewInt(0).Sub(remainFee, fee) //check balance fee balanceIssuerFee, err := trc21IssuerInstance.GetTokenCapacity(trc21TokenAddr) @@ -178,11 +177,10 @@ func testTransferTrc21Fail() { if err != nil { log.Fatal("can't transaction's receipt ", err, "hash", tx.Hash().Hex()) } - fee := big.NewInt(0).SetUint64(hexutil.MustDecodeUint64(receipt["gasUsed"].(string))) - if hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) > common.TIPTRC21Fee.Uint64() { - fee = fee.Mul(fee, common.TRC21GasPrice) - } - fmt.Println("fee", fee.Uint64(), "number", hexutil.MustDecodeUint64(receipt["blockNumber"].(string))) + gasUsed := hexutil.MustDecodeUint64(receipt["gasUsed"].(string)) + blockNumber := hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) + fee := common.GetGasFee(blockNumber, gasUsed) + fmt.Println("fee", fee.Uint64(), "number", blockNumber) remainFee = big.NewInt(0).Sub(remainFee, fee) //check balance fee balanceIssuerFee, err = trc21IssuerInstance.GetTokenCapacity(trc21TokenAddr) diff --git a/contracts/trc21issuer/trc21issuer_test.go b/contracts/trc21issuer/trc21issuer_test.go index e28344caf0..e0744b26d1 100644 --- a/contracts/trc21issuer/trc21issuer_test.go +++ b/contracts/trc21issuer/trc21issuer_test.go @@ -1,13 +1,14 @@ package trc21issuer import ( + "math/big" + "testing" + "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind" "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends" "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/crypto" - "math/big" - "testing" ) var ( @@ -81,10 +82,7 @@ func TestFeeTxWithTRC21Token(t *testing.T) { if err != nil { t.Fatal("can't transaction's receipt ", err, "hash", tx.Hash()) } - fee := big.NewInt(0).SetUint64(receipt.GasUsed) - if receipt.Logs[0].BlockNumber > common.TIPTRC21Fee.Uint64() { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee := common.GetGasFee(receipt.Logs[0].BlockNumber, receipt.GasUsed) remainFee := big.NewInt(0).Sub(minApply, fee) // check balance trc21 again @@ -133,10 +131,7 @@ func TestFeeTxWithTRC21Token(t *testing.T) { if err != nil { t.Fatal("can't transaction's receipt ", err, "hash", tx.Hash()) } - fee = big.NewInt(0).SetUint64(receipt.GasUsed) - if receipt.Logs[0].BlockNumber > common.TIPTRC21Fee.Uint64() { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee = common.GetGasFee(receipt.Logs[0].BlockNumber, receipt.GasUsed) remainFee = big.NewInt(0).Sub(remainFee, fee) //check balance fee balanceIssuerFee, err = trc21Issuer.GetTokenCapacity(trc21TokenAddr) diff --git a/core/chain_makers.go b/core/chain_makers.go index a19cf0c5a5..348d68d168 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -108,10 +108,7 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) { b.txs = append(b.txs, tx) b.receipts = append(b.receipts, receipt) if tokenFeeUsed { - fee := new(big.Int).SetUint64(gas) - if b.header.Number.Cmp(common.TIPTRC21Fee) > 0 { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee := common.GetGasFee(b.header.Number.Uint64(), gas) state.UpdateTRC21Fee(b.statedb, map[common.Address]*big.Int{*tx.To(): new(big.Int).Sub(feeCapacity[*tx.To()], new(big.Int).SetUint64(gas))}, fee) } } diff --git a/core/state_processor.go b/core/state_processor.go index 97e45f3f6a..acfec0c8fd 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -120,10 +120,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra receipts = append(receipts, receipt) allLogs = append(allLogs, receipt.Logs...) if tokenFeeUsed { - fee := new(big.Int).SetUint64(gas) - if block.Header().Number.Cmp(common.TIPTRC21Fee) > 0 { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee := common.GetGasFee(block.Header().Number.Uint64(), gas) balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee) balanceUpdated[*tx.To()] = balanceFee[*tx.To()] totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee) @@ -201,10 +198,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated receipts[i] = receipt allLogs = append(allLogs, receipt.Logs...) if tokenFeeUsed { - fee := new(big.Int).SetUint64(gas) - if block.Header().Number.Cmp(common.TIPTRC21Fee) > 0 { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee := common.GetGasFee(block.Header().Number.Uint64(), gas) balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee) balanceUpdated[*tx.To()] = balanceFee[*tx.To()] totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee) diff --git a/eth/api_tracer.go b/eth/api_tracer.go index be95e78d87..218781b57b 100644 --- a/eth/api_tracer.go +++ b/eth/api_tracer.go @@ -706,10 +706,7 @@ func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int, ree } if tokenFeeUsed { - fee := new(big.Int).SetUint64(gas) - if block.Header().Number.Cmp(common.TIPTRC21Fee) > 0 { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee := common.GetGasFee(block.Header().Number.Uint64(), gas) feeCapacity[*tx.To()] = new(big.Int).Sub(feeCapacity[*tx.To()], fee) balanceUpdated[*tx.To()] = feeCapacity[*tx.To()] totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee) diff --git a/miner/worker.go b/miner/worker.go index 76b5562b00..bc3982094e 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -931,10 +931,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad log.Debug("Add Special Transaction failed, account skipped", "hash", tx.Hash(), "sender", from, "nonce", tx.Nonce(), "to", tx.To(), "err", err) } if tokenFeeUsed { - fee := new(big.Int).SetUint64(gas) - if env.header.Number.Cmp(common.TIPTRC21Fee) > 0 { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee := common.GetGasFee(env.header.Number.Uint64(), gas) balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee) balanceUpdated[*tx.To()] = balanceFee[*tx.To()] totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee) @@ -1049,10 +1046,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad txs.Shift() } if tokenFeeUsed { - fee := new(big.Int).SetUint64(gas) - if env.header.Number.Cmp(common.TIPTRC21Fee) > 0 { - fee = fee.Mul(fee, common.TRC21GasPrice) - } + fee := common.GetGasFee(env.header.Number.Uint64(), gas) balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee) balanceUpdated[*tx.To()] = balanceFee[*tx.To()] totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)