Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix equal tx hash by implementing dynamic tx gas limit in special transactions #348

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions miner/gas_limit_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
16 changes: 12 additions & 4 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}