Skip to content

Commit

Permalink
Return ErrReplacementUnderpriced when price is no higher than already…
Browse files Browse the repository at this point in the history
… existing one. (#1908)
  • Loading branch information
ohijiho committed Sep 26, 2023
1 parent 5f6ba3e commit 8aaae11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
3 changes: 2 additions & 1 deletion txpool/slot_gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package txpool
import (
"sync/atomic"

"github.com/0xPolygon/polygon-edge/types"
"github.com/armon/go-metrics"

"github.com/0xPolygon/polygon-edge/types"
)

const (
Expand Down
13 changes: 7 additions & 6 deletions txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"sync/atomic"
"time"

"github.com/armon/go-metrics"
"github.com/golang/protobuf/ptypes/any"
"github.com/hashicorp/go-hclog"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/grpc"

"github.com/0xPolygon/polygon-edge/blockchain"
"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/forkmanager"
Expand All @@ -15,11 +21,6 @@ import (
"github.com/0xPolygon/polygon-edge/state/runtime"
"github.com/0xPolygon/polygon-edge/txpool/proto"
"github.com/0xPolygon/polygon-edge/types"
"github.com/armon/go-metrics"
"github.com/golang/protobuf/ptypes/any"
"github.com/hashicorp/go-hclog"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/grpc"
)

const (
Expand Down Expand Up @@ -824,7 +825,7 @@ func (p *TxPool) addTx(origin txOrigin, tx *types.Transaction) error {
// if tx with same nonce does exist and has same or better gas price -> return error
metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1)

return ErrUnderpriced
return ErrReplacementUnderpriced
}

slotsFree += slotsRequired(oldTxWithSameNonce) // add old tx slots
Expand Down
17 changes: 9 additions & 8 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes/any"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/helper/tests"
"github.com/0xPolygon/polygon-edge/state"
"github.com/0xPolygon/polygon-edge/state/runtime"
"github.com/0xPolygon/polygon-edge/txpool/proto"
"github.com/0xPolygon/polygon-edge/types"
"github.com/golang/protobuf/ptypes/any"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -324,7 +325,7 @@ func TestAddTxErrors(t *testing.T) {

assert.ErrorIs(t,
pool.addTx(local, tx),
ErrUnderpriced,
ErrReplacementUnderpriced,
)
})

Expand Down Expand Up @@ -845,7 +846,7 @@ func TestAddTx(t *testing.T) {
// check the account nonce before promoting
assert.Equal(t, uint64(0), pool.accounts.get(addr1).getNonce())

assert.ErrorIs(t, pool.addTx(local, tx1), ErrUnderpriced)
assert.ErrorIs(t, pool.addTx(local, tx1), ErrReplacementUnderpriced)

// execute the enqueue handlers
<-pool.promoteReqCh
Expand Down Expand Up @@ -919,7 +920,7 @@ func TestAddTx(t *testing.T) {
pool.gauge.read(),
)

assert.ErrorIs(t, pool.addTx(local, tx1), ErrUnderpriced)
assert.ErrorIs(t, pool.addTx(local, tx1), ErrReplacementUnderpriced)

acc := pool.accounts.get(addr1)
acc.nonceToTx.lock()
Expand Down Expand Up @@ -3679,7 +3680,7 @@ func TestAddTx_TxReplacement(t *testing.T) {

assert.ErrorIs(t, pool.addTx(local, tx1), ErrUnderpriced)
assert.ErrorIs(t, pool.addTx(local, tx2), ErrUnderpriced)
assert.ErrorIs(t, pool.addTx(local, tx3), ErrUnderpriced)
assert.ErrorIs(t, pool.addTx(local, tx3), ErrReplacementUnderpriced)
assert.ErrorIs(t, pool.addTx(local, tx4), ErrUnderpriced)

// Success
Expand Down

0 comments on commit 8aaae11

Please sign in to comment.