erigon-pulse/cmd/rpcdaemon/commands/eth_ming_test.go

65 lines
2.1 KiB
Go
Raw Normal View History

package commands
import (
"math/big"
"testing"
"time"
"github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
2021-09-29 01:36:25 +00:00
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
2021-06-29 10:00:22 +00:00
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
2021-11-14 04:08:52 +00:00
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
2021-06-27 06:41:21 +00:00
"github.com/ledgerwatch/erigon/turbo/stages"
"github.com/stretchr/testify/require"
)
func TestPendingBlock(t *testing.T) {
2021-06-29 10:00:22 +00:00
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, stages.Mock(t))
mining := txpool.NewMiningClient(conn)
ff := rpchelper.New(ctx, nil, nil, mining, func() {})
2021-09-29 01:36:25 +00:00
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
2021-11-14 04:08:52 +00:00
api := NewEthAPI(NewBaseApi(ff, stateCache, snapshotsync.NewBlockReader(), false), nil, nil, nil, mining, 5000000)
expect := uint64(12345)
b, err := rlp.EncodeToBytes(types.NewBlockWithHeader(&types.Header{Number: big.NewInt(int64(expect))}))
require.NoError(t, err)
ch := make(chan *types.Block, 1)
2021-06-29 10:00:22 +00:00
id := ff.SubscribePendingBlock(ch)
defer ff.UnsubscribePendingBlock(id)
2021-06-29 10:00:22 +00:00
ff.HandlePendingBlock(&txpool.OnPendingBlockReply{RplBlock: b})
block := api.pendingBlock()
require.Equal(t, block.NumberU64(), expect)
select {
case got := <-ch:
require.Equal(t, expect, got.NumberU64())
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout waiting for expected notification")
}
}
func TestPendingLogs(t *testing.T) {
2021-06-29 10:00:22 +00:00
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, stages.Mock(t))
mining := txpool.NewMiningClient(conn)
ff := rpchelper.New(ctx, nil, nil, mining, func() {})
expect := []byte{211}
ch := make(chan types.Logs, 1)
defer close(ch)
2021-06-29 10:00:22 +00:00
id := ff.SubscribePendingLogs(ch)
defer ff.UnsubscribePendingLogs(id)
b, err := rlp.EncodeToBytes([]*types.Log{{Data: expect}})
require.NoError(t, err)
2021-06-29 10:00:22 +00:00
ff.HandlePendingLogs(&txpool.OnPendingLogsReply{RplLogs: b})
select {
case logs := <-ch:
require.Equal(t, expect, logs[0].Data)
case <-time.After(100 * time.Millisecond):
t.Fatalf("timeout waiting for expected notification")
}
}