Skip to content

Commit

Permalink
Merge pull request #3237 from oasisprotocol/pro-wh/stable/20.9.x/dupt…
Browse files Browse the repository at this point in the history
…x-backport

backport #3193
  • Loading branch information
pro-wh authored Sep 4, 2020
2 parents 11dd03c + b27da44 commit 50617c0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions .changelog/3192.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/consensus: Add ErrDuplicateTx error for duplicate transactions
3 changes: 3 additions & 0 deletions go/consensus/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ var (
// ErrUnsupported is the error returned when the given method is not supported by the consensus
// backend.
ErrUnsupported = errors.New(moduleName, 4, "consensus: method not supported")

// ErrDuplicateTx is the error returned when the transaction already exists in the mempool.
ErrDuplicateTx = errors.New(moduleName, 5, "consensus: duplicate transaction")
)

// FeatureMask is the consensus backend feature bitmask.
Expand Down
7 changes: 6 additions & 1 deletion go/consensus/tendermint/full/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,12 @@ func (t *fullService) broadcastTxRaw(data []byte) error {
ch <- rsp
close(ch)
}, tmmempool.TxInfo{})
if err != nil {
switch err {
case nil:
case tmmempool.ErrTxInCache:
// Transaction already in the mempool or was recently there.
return consensusAPI.ErrDuplicateTx
default:
return fmt.Errorf("tendermint: failed to submit to local mempool: %w", err)
}

Expand Down
11 changes: 8 additions & 3 deletions go/consensus/tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand All @@ -13,7 +14,6 @@ import (
memorySigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/memory"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
epochtimemock "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/epochtime_mock"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs"
)
Expand Down Expand Up @@ -91,7 +91,7 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)

_, err = backend.EstimateGas(ctx, &consensus.EstimateGasRequest{
Signer: memorySigner.NewTestSigner("estimate gas signer").Public(),
Transaction: transaction.NewTransaction(0, nil, epochtimemock.MethodSetEpoch, 0),
Transaction: transaction.NewTransaction(0, nil, staking.MethodTransfer, &staking.Transfer{}),
})
require.NoError(err, "EstimateGas")

Expand Down Expand Up @@ -123,7 +123,7 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)
err = backend.SubmitTxNoWait(ctx, &transaction.SignedTransaction{})
require.Error(err, "SubmitTxNoWait should fail with invalid transaction")

testTx := transaction.NewTransaction(0, nil, epochtimemock.MethodSetEpoch, epoch)
testTx := transaction.NewTransaction(0, nil, staking.MethodTransfer, &staking.Transfer{})
testSigner := memorySigner.NewTestSigner(fmt.Sprintf("consensus tests tx signer: %T", backend))
testSigTx, err := transaction.Sign(testSigner, testTx)
require.NoError(err, "transaction.Sign")
Expand All @@ -133,6 +133,11 @@ func ConsensusImplementationTests(t *testing.T, backend consensus.ClientBackend)
err = backend.SubmitEvidence(ctx, &consensus.Evidence{})
require.Error(err, "SubmitEvidence should fail with invalid evidence")

// Trigger some duplicate transaction errors.
err = backend.SubmitTxNoWait(ctx, testSigTx)
require.Error(err, "SubmitTxNoWait(duplicate)")
require.True(errors.Is(err, consensus.ErrDuplicateTx), "SubmitTxNoWait should return ErrDuplicateTx on duplicate tx")

// We should be able to do remote state queries. Of course the state format is backend-specific
// so we simply perform some usual storage operations like fetching random keys and iterating
// through everything.
Expand Down

0 comments on commit 50617c0

Please sign in to comment.