erigon-pulse/txpool/fetch_test.go

188 lines
6.5 KiB
Go
Raw Normal View History

2021-07-04 17:24:15 +00:00
/*
Copyright 2021 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package txpool
import (
2021-07-12 16:55:47 +00:00
"context"
"encoding/hex"
2021-07-26 00:59:41 +00:00
"fmt"
2021-08-09 07:14:02 +00:00
"io"
2021-07-14 16:14:36 +00:00
"sync"
2021-07-04 17:24:15 +00:00
"testing"
2021-07-12 16:55:47 +00:00
2021-09-18 13:58:20 +00:00
"github.com/ledgerwatch/erigon-lib/common/u256"
"github.com/ledgerwatch/erigon-lib/direct"
2021-08-25 11:51:58 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces"
2021-08-09 04:00:36 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
2021-07-12 16:55:47 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
2021-07-26 02:06:17 +00:00
"github.com/ledgerwatch/erigon-lib/gointerfaces/types"
2021-08-11 04:21:36 +00:00
"github.com/ledgerwatch/erigon-lib/kv/memdb"
types3 "github.com/ledgerwatch/erigon-lib/types"
2021-07-26 02:06:17 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2021-08-09 07:04:22 +00:00
"google.golang.org/grpc"
2021-07-04 17:24:15 +00:00
)
func TestFetch(t *testing.T) {
2021-08-09 07:14:02 +00:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2021-07-26 02:06:17 +00:00
m := NewMockSentry(ctx)
sentryClient := direct.NewSentryClientDirect(direct.ETH66, m)
pool := &PoolMock{}
fetch := NewFetch(ctx, []direct.SentryClient{sentryClient}, pool, &remote.KVClientMock{}, nil, nil, *u256.N1)
2021-07-14 16:14:36 +00:00
var wg sync.WaitGroup
fetch.SetWaitGroup(&wg)
2021-07-26 02:06:17 +00:00
m.StreamWg.Add(2)
2021-08-09 04:00:36 +00:00
fetch.ConnectSentries()
2021-07-26 02:06:17 +00:00
m.StreamWg.Wait()
2021-07-14 16:14:36 +00:00
// Send one transaction id
wg.Add(1)
2021-07-26 02:06:17 +00:00
errs := m.Send(&sentry.InboundMessage{
2021-07-15 19:10:17 +00:00
Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66,
2021-08-03 06:56:46 +00:00
Data: decodeHex("e1a0595e27a835cd79729ff1eeacec3120eeb6ed1464a04ec727aaca734ead961328"),
2022-03-19 04:38:37 +00:00
PeerId: peerID,
2021-07-15 19:10:17 +00:00
})
2021-07-14 16:14:36 +00:00
for i, err := range errs {
if err != nil {
t.Errorf("sending new pool tx hashes 66 (%d): %v", i, err)
}
}
wg.Wait()
2021-07-26 02:06:17 +00:00
2021-07-04 17:24:15 +00:00
}
func TestSendTxPropagate(t *testing.T) {
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
t.Run("few remote byHash", func(t *testing.T) {
2021-07-26 00:57:49 +00:00
m := NewMockSentry(ctx)
send := NewSend(ctx, []direct.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil)
send.BroadcastPooledTxs(testRlps(2))
send.AnnouncePooledTxs(toHashes(1, 42))
2021-07-26 02:06:17 +00:00
calls1 := m.SendMessageToRandomPeersCalls()
require.Equal(t, 1, len(calls1))
calls2 := m.SendMessageToAllCalls()
require.Equal(t, 1, len(calls2))
first := calls1[0].SendMessageToRandomPeersRequest.Data
assert.Equal(t, sentry.MessageId_TRANSACTIONS_66, first.Id)
assert.Equal(t, 5, len(first.Data))
second := calls2[0].OutboundMessageData
assert.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, second.Id)
assert.Equal(t, 68, len(second.Data))
2021-07-26 00:57:49 +00:00
})
t.Run("much remote byHash", func(t *testing.T) {
2021-07-26 00:57:49 +00:00
m := NewMockSentry(ctx)
send := NewSend(ctx, []direct.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil)
list := make(types3.Hashes, p2pTxPacketLimit*3)
2021-07-26 00:59:41 +00:00
for i := 0; i < len(list); i += 32 {
b := []byte(fmt.Sprintf("%x", i))
copy(list[i:i+32], b)
}
send.BroadcastPooledTxs(testRlps(len(list) / 32))
send.AnnouncePooledTxs(list)
calls1 := m.SendMessageToRandomPeersCalls()
require.Equal(t, 1, len(calls1))
calls2 := m.SendMessageToAllCalls()
require.Equal(t, 3, len(calls2))
call1 := calls1[0].SendMessageToRandomPeersRequest.Data
require.Equal(t, sentry.MessageId_TRANSACTIONS_66, call1.Id)
require.True(t, len(call1.Data) > 0)
2021-07-26 00:59:41 +00:00
for i := 0; i < 3; i++ {
call2 := calls2[i].OutboundMessageData
require.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, call2.Id)
require.True(t, len(call2.Data) > 0)
2021-07-26 00:59:41 +00:00
}
2021-07-26 00:57:49 +00:00
})
t.Run("few local byHash", func(t *testing.T) {
2021-07-26 02:06:17 +00:00
m := NewMockSentry(ctx)
m.SendMessageToAllFunc = func(contextMoqParam context.Context, outboundMessageData *sentry.OutboundMessageData) (*sentry.SentPeers, error) {
return &sentry.SentPeers{Peers: make([]*types.H512, 5)}, nil
2021-07-26 02:06:17 +00:00
}
send := NewSend(ctx, []direct.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil)
send.BroadcastPooledTxs(testRlps(2))
send.AnnouncePooledTxs(toHashes(1, 42))
2021-07-26 02:06:17 +00:00
calls := m.SendMessageToAllCalls()
2021-07-26 03:04:53 +00:00
require.Equal(t, 1, len(calls))
2021-07-26 02:06:17 +00:00
first := calls[0].OutboundMessageData
2021-07-26 03:04:53 +00:00
assert.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, first.Id)
assert.Equal(t, 68, len(first.Data))
})
t.Run("sync with new peer", func(t *testing.T) {
m := NewMockSentry(ctx)
m.SendMessageToAllFunc = func(contextMoqParam context.Context, outboundMessageData *sentry.OutboundMessageData) (*sentry.SentPeers, error) {
return &sentry.SentPeers{Peers: make([]*types.H512, 5)}, nil
2021-07-26 03:04:53 +00:00
}
send := NewSend(ctx, []direct.SentryClient{direct.NewSentryClientDirect(direct.ETH66, m)}, nil)
2021-07-26 03:04:53 +00:00
expectPeers := toPeerIDs(1, 2, 42)
send.PropagatePooledTxsToPeersList(expectPeers, toHashes(1, 42))
2021-07-26 03:04:53 +00:00
calls := m.SendMessageByIdCalls()
require.Equal(t, 3, len(calls))
for i, call := range calls {
req := call.SendMessageByIdRequest
assert.Equal(t, expectPeers[i], types3.PeerID(req.PeerId))
2021-07-26 03:04:53 +00:00
assert.Equal(t, sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66, req.Data.Id)
assert.True(t, len(req.Data.Data) > 0)
}
2021-07-26 02:06:17 +00:00
})
}
2021-08-09 07:04:22 +00:00
func decodeHex(in string) []byte {
payload, err := hex.DecodeString(in)
if err != nil {
panic(err)
}
return payload
}
2021-08-09 07:04:22 +00:00
func TestOnNewBlock(t *testing.T) {
2021-08-09 07:14:02 +00:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2021-08-22 10:06:38 +00:00
coreDB, db := memdb.NewTestDB(t), memdb.NewTestDB(t)
2021-08-09 07:04:22 +00:00
2021-08-09 07:14:02 +00:00
i := 0
2021-08-09 07:04:22 +00:00
stream := &remote.KV_StateChangesClientMock{
2021-09-17 02:56:04 +00:00
RecvFunc: func() (*remote.StateChangeBatch, error) {
2021-08-09 07:14:02 +00:00
if i > 0 {
return nil, io.EOF
}
i++
2021-09-17 02:56:04 +00:00
return &remote.StateChangeBatch{
DatabaseViewID: 1,
ChangeBatch: []*remote.StateChange{
{Txs: [][]byte{decodeHex(types3.TxParseMainnetTests[0].PayloadStr), decodeHex(types3.TxParseMainnetTests[1].PayloadStr), decodeHex(types3.TxParseMainnetTests[2].PayloadStr)}, BlockHeight: 1, BlockHash: gointerfaces.ConvertHashToH256([32]byte{})},
2021-09-17 02:56:04 +00:00
},
}, nil
2021-08-09 07:04:22 +00:00
},
}
stateChanges := &remote.KVClientMock{
StateChangesFunc: func(ctx context.Context, in *remote.StateChangeRequest, opts ...grpc.CallOption) (remote.KV_StateChangesClient, error) {
return stream, nil
},
}
pool := &PoolMock{}
fetch := NewFetch(ctx, nil, pool, stateChanges, coreDB, db, *u256.N1)
2021-08-14 08:53:22 +00:00
err := fetch.handleStateChanges(ctx, stateChanges)
2021-08-14 08:54:31 +00:00
assert.ErrorIs(t, io.EOF, err)
2021-08-09 07:04:22 +00:00
assert.Equal(t, 1, len(pool.OnNewBlockCalls()))
assert.Equal(t, 3, len(pool.OnNewBlockCalls()[0].MinedTxs.Txs))
2021-08-09 07:04:22 +00:00
}