diff --git a/miner/gas_limit_test.go b/miner/gas_limit_test.go new file mode 100644 index 000000000000..3a36bf0ca8c8 --- /dev/null +++ b/miner/gas_limit_test.go @@ -0,0 +1,45 @@ +package miner + +import ( + "github.com/XinFinOrg/XDPoSChain/common" + "github.com/XinFinOrg/XDPoSChain/core/types" + "github.com/XinFinOrg/XDPoSChain/crypto/sha3" + "github.com/XinFinOrg/XDPoSChain/rlp" + "log" + "math/big" + "testing" + "time" +) + +func TestDynamicGasLimit(t *testing.T) { + const testTries = 100 + storage := make(map[string]uint64) + var nonce uint64 = 0 + for i := 0; i < testTries; i++ { + h := common.Hash{} + a := big.NewInt(0) + address := common.Address{} + data := make([]byte, 0) + dynamicTxMatchGasLimit := getDynamicTxGasLimit() + tx := types.NewTransaction(nonce, address, a, dynamicTxMatchGasLimit, a, data) + hw := sha3.NewKeccak256() + if err := rlp.Encode(hw, tx); err != nil { + return + } + hw.Sum(h[:0]) + checkForUnique(&storage, h.Hex()) + storage[h.Hex()] = dynamicTxMatchGasLimit + // actually time between blocks in blockchain approximately 2 sec + time.Sleep(time.Second * 2) + // there is no problem with triple and more transaction but in test case were generated 100 transaction with same nonce + nonce = uint64(len(storage) / 100) + } + log.Println("40000000 loops pass good, no unique hash") +} + +func checkForUnique(storage *map[string]uint64, hash string) { + value, ok := (*storage)[hash] + if ok { + log.Fatal(value, len(*storage)) + } +} diff --git a/miner/worker.go b/miner/worker.go index bc3982094e87..298399eeee86 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -700,7 +700,8 @@ func (self *worker) commitNewWork() { return } nonce := work.state.GetNonce(self.coinbase) - tx := types.NewTransaction(nonce, common.HexToAddress(common.XDCXAddr), big.NewInt(0), txMatchGasLimit, big.NewInt(0), txMatchBytes) + gasLimit := getDynamicTxGasLimit() + tx := types.NewTransaction(nonce, common.HexToAddress(common.XDCXAddr), big.NewInt(0), gasLimit, big.NewInt(0), txMatchBytes) txM, err := wallet.SignTx(accounts.Account{Address: self.coinbase}, tx, self.config.ChainId) if err != nil { log.Error("Fail to create tx matches", "error", err) @@ -725,7 +726,8 @@ func (self *worker) commitNewWork() { return } nonce := work.state.GetNonce(self.coinbase) - lendingTx := types.NewTransaction(nonce, common.HexToAddress(common.XDCXLendingAddress), big.NewInt(0), txMatchGasLimit, big.NewInt(0), lendingDataBytes) + gasLimit := getDynamicTxGasLimit() + lendingTx := types.NewTransaction(nonce, common.HexToAddress(common.XDCXLendingAddress), big.NewInt(0), gasLimit, big.NewInt(0), lendingDataBytes) signedLendingTx, err := wallet.SignTx(accounts.Account{Address: self.coinbase}, lendingTx, self.config.ChainId) if err != nil { log.Error("Fail to create lending tx", "error", err) @@ -746,7 +748,8 @@ func (self *worker) commitNewWork() { return } nonce := work.state.GetNonce(self.coinbase) - finalizedTx := types.NewTransaction(nonce, common.HexToAddress(common.XDCXLendingFinalizedTradeAddress), big.NewInt(0), txMatchGasLimit, big.NewInt(0), finalizedTradeData) + gasLimit := getDynamicTxGasLimit() + finalizedTx := types.NewTransaction(nonce, common.HexToAddress(common.XDCXLendingFinalizedTradeAddress), big.NewInt(0), gasLimit, big.NewInt(0), finalizedTradeData) signedFinalizedTx, err := wallet.SignTx(accounts.Account{Address: self.coinbase}, finalizedTx, self.config.ChainId) if err != nil { log.Error("Fail to create lending tx", "error", err) @@ -775,7 +778,8 @@ func (self *worker) commitNewWork() { XDCxStateRoot := work.tradingState.IntermediateRoot() LendingStateRoot := work.lendingState.IntermediateRoot() txData := append(XDCxStateRoot.Bytes(), LendingStateRoot.Bytes()...) - tx := types.NewTransaction(work.state.GetNonce(self.coinbase), common.HexToAddress(common.TradingStateAddr), big.NewInt(0), txMatchGasLimit, big.NewInt(0), txData) + gasLimit := getDynamicTxGasLimit() + tx := types.NewTransaction(work.state.GetNonce(self.coinbase), common.HexToAddress(common.TradingStateAddr), big.NewInt(0), gasLimit, big.NewInt(0), txData) txStateRoot, err := wallet.SignTx(accounts.Account{Address: self.coinbase}, tx, self.config.ChainId) if err != nil { log.Error("Fail to create tx state root", "error", err) @@ -1092,3 +1096,7 @@ func (env *Work) commitTransaction(balanceFee map[common.Address]*big.Int, tx *t return nil, receipt.Logs, tokenFeeUsed, gas } + +func getDynamicTxGasLimit() uint64 { + return uint64(time.Now().UnixNano() / int64(time.Millisecond) % txMatchGasLimit) +}