Skip to content

Commit

Permalink
Fix data race in tests (#6877)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon authored Nov 14, 2023
1 parent 790d5ee commit 00c6d22
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions x/txfees/keeper/mempool-1559/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"sync"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -95,6 +96,11 @@ func (e *EipState) startBlock(height int64) {
}
}

func (e EipState) Clone() EipState {
e.CurBaseFee = e.CurBaseFee.Clone()
return e
}

// deliverTxCode runs on every transaction in the feedecorator ante handler and sums the gas of each transaction
func (e *EipState) deliverTxCode(ctx sdk.Context, tx sdk.FeeTx) {
if ctx.BlockHeight() != e.lastBlockHeight {
Expand Down Expand Up @@ -133,7 +139,7 @@ func (e *EipState) updateBaseFee(height int64) {
e.CurBaseFee = MaxBaseFee.Clone()
}

go e.tryPersist()
go e.Clone().tryPersist()
}

// GetCurBaseFee returns a clone of the CurBaseFee to avoid overwriting the initial value in
Expand All @@ -148,9 +154,13 @@ func (e *EipState) GetCurRecheckBaseFee() osmomath.Dec {
return e.CurBaseFee.Clone().Quo(sdk.NewDec(RecheckFeeConstant))
}

var rwMtx = sync.Mutex{}

// tryPersist persists the eip1559 state to disk in the form of a json file
// we do this in case a node stops and it can continue functioning as normal
func (e *EipState) tryPersist() {
func (e EipState) tryPersist() {
rwMtx.Lock()
defer rwMtx.Unlock()
bz, err := json.Marshal(e)
if err != nil {
fmt.Println("Error marshalling eip1559 state", err)
Expand All @@ -167,6 +177,8 @@ func (e *EipState) tryPersist() {
// tryLoad reads eip1559 state from disk and initializes the CurEipState to
// the previous state when a node is restarted
func (e *EipState) tryLoad() osmomath.Dec {
rwMtx.Lock()
defer rwMtx.Unlock()
bz, err := os.ReadFile(e.BackupFilePath)
if err != nil {
fmt.Println("Error reading eip1559 state", err)
Expand Down

0 comments on commit 00c6d22

Please sign in to comment.