2020-01-15 07:52:59 +00:00
|
|
|
package mempool
|
2019-10-24 09:29:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-12-13 21:03:04 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/internal/random"
|
2019-10-24 09:29:55 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FeerStub struct {
|
|
|
|
lowPriority bool
|
|
|
|
sysFee util.Fixed8
|
|
|
|
netFee util.Fixed8
|
|
|
|
perByteFee util.Fixed8
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeerStub) NetworkFee(*transaction.Transaction) util.Fixed8 {
|
|
|
|
return fs.netFee
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeerStub) IsLowPriority(*transaction.Transaction) bool {
|
|
|
|
return fs.lowPriority
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeerStub) FeePerByte(*transaction.Transaction) util.Fixed8 {
|
|
|
|
return fs.perByteFee
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeerStub) SystemFee(*transaction.Transaction) util.Fixed8 {
|
|
|
|
return fs.sysFee
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMemPoolAddRemoveWithFeer(t *testing.T, fs Feer) {
|
|
|
|
mp := NewMemPool(10)
|
|
|
|
tx := newMinerTX()
|
|
|
|
_, ok := mp.TryGetValue(tx.Hash())
|
|
|
|
require.Equal(t, false, ok)
|
2020-02-05 11:24:36 +00:00
|
|
|
require.NoError(t, mp.Add(tx, fs))
|
2019-10-24 09:29:55 +00:00
|
|
|
// Re-adding should fail.
|
2020-02-05 11:24:36 +00:00
|
|
|
require.Error(t, mp.Add(tx, fs))
|
2019-10-24 09:29:55 +00:00
|
|
|
tx2, ok := mp.TryGetValue(tx.Hash())
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, tx, tx2)
|
|
|
|
mp.Remove(tx.Hash())
|
|
|
|
_, ok = mp.TryGetValue(tx.Hash())
|
|
|
|
require.Equal(t, false, ok)
|
|
|
|
// Make sure nothing left in the mempool after removal.
|
2020-02-05 14:13:35 +00:00
|
|
|
assert.Equal(t, 0, len(mp.verifiedMap))
|
|
|
|
assert.Equal(t, 0, len(mp.unverifiedMap))
|
|
|
|
assert.Equal(t, 0, len(mp.verifiedTxes))
|
|
|
|
assert.Equal(t, 0, len(mp.unverifiedTxes))
|
2019-10-24 09:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemPoolAddRemove(t *testing.T) {
|
|
|
|
var fs = &FeerStub{lowPriority: false}
|
|
|
|
t.Run("low priority", func(t *testing.T) { testMemPoolAddRemoveWithFeer(t, fs) })
|
|
|
|
fs.lowPriority = true
|
|
|
|
t.Run("high priority", func(t *testing.T) { testMemPoolAddRemoveWithFeer(t, fs) })
|
|
|
|
}
|
2019-12-13 21:03:04 +00:00
|
|
|
|
|
|
|
func TestMemPoolVerify(t *testing.T) {
|
|
|
|
mp := NewMemPool(10)
|
|
|
|
tx := newMinerTX()
|
|
|
|
inhash1 := random.Uint256()
|
|
|
|
tx.Inputs = append(tx.Inputs, transaction.Input{PrevHash: inhash1, PrevIndex: 0})
|
|
|
|
require.Equal(t, true, mp.Verify(tx))
|
2020-02-05 11:24:36 +00:00
|
|
|
require.NoError(t, mp.Add(tx, &FeerStub{}))
|
2019-12-13 21:03:04 +00:00
|
|
|
|
|
|
|
tx2 := newMinerTX()
|
|
|
|
inhash2 := random.Uint256()
|
|
|
|
tx2.Inputs = append(tx2.Inputs, transaction.Input{PrevHash: inhash2, PrevIndex: 0})
|
|
|
|
require.Equal(t, true, mp.Verify(tx2))
|
2020-02-05 11:24:36 +00:00
|
|
|
require.NoError(t, mp.Add(tx2, &FeerStub{}))
|
2019-12-13 21:03:04 +00:00
|
|
|
|
|
|
|
tx3 := newMinerTX()
|
|
|
|
// Different index number, but the same PrevHash as in tx1.
|
|
|
|
tx3.Inputs = append(tx3.Inputs, transaction.Input{PrevHash: inhash1, PrevIndex: 1})
|
|
|
|
require.Equal(t, true, mp.Verify(tx3))
|
|
|
|
// The same input as in tx2.
|
|
|
|
tx3.Inputs = append(tx3.Inputs, transaction.Input{PrevHash: inhash2, PrevIndex: 0})
|
|
|
|
require.Equal(t, false, mp.Verify(tx3))
|
|
|
|
}
|
2020-01-15 07:52:59 +00:00
|
|
|
|
|
|
|
func newMinerTX() *transaction.Transaction {
|
|
|
|
return &transaction.Transaction{
|
|
|
|
Type: transaction.MinerType,
|
|
|
|
Data: &transaction.MinerTX{},
|
|
|
|
}
|
|
|
|
}
|