mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-31 23:41:22 +00:00
af07c13730
* add main.go * interop readme * proper visibility * standardize and abstract into simpler funcs * formatting * no os pkg * add test * no panics anywhere, properly and nicely handle errors * proper comments * fix broken test * readme * comment * recommend ssz * install * tool now works * README * build * readme * 64 validators * rem print * register the no powchain flag * work on mock eth1 start * common interface * getting closer with the interface defs * only two uses of powchain * remove powchain dependency * remove powchain dependency * common powchain interface * proper comment in case of flag * proper args into rpc services * rename fields * pass in mock flag into RPC * conforms to iface * use client instead of block fetcher iface * broken tests * block fetcher * finalized * resolved broken build * fix build * comment * fix tests * tests pass * resolved confs * took them out * rename into smaller interfaces * resolve some confs * ensure tests pass * properly utilize mock instead of localized mock * res lint * lint * finish test for mock eth1data * run gazelle * include flag again * fix broken build * disable powchain * dont dial eth1 nodes * reenable pow * use smaller interfaces, standardize naming * abstract mock into its own package * faulty mock lint * fix stutter in lint * rpc tests all passing * use mocks for operations * no more mocks in the entire rpc package * no mock * viz * testonly
231 lines
5.9 KiB
Go
231 lines
5.9 KiB
Go
package powchain
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"math/big"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
|
dbutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
|
|
)
|
|
|
|
var endpoint = "ws://127.0.0.1"
|
|
|
|
func TestLatestMainchainInfo_OK(t *testing.T) {
|
|
testAcc, err := contracts.Setup()
|
|
if err != nil {
|
|
t.Fatalf("Unable to set up simulated backend %v", err)
|
|
}
|
|
beaconDB := dbutil.SetupDB(t)
|
|
defer dbutil.TeardownDB(t, beaconDB)
|
|
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
|
|
Endpoint: endpoint,
|
|
DepositContract: testAcc.ContractAddr,
|
|
BlockFetcher: &goodFetcher{},
|
|
Reader: &goodReader{},
|
|
Logger: &goodLogger{},
|
|
HTTPLogger: &goodLogger{},
|
|
ContractBackend: testAcc.Backend,
|
|
BeaconDB: beaconDB,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err)
|
|
}
|
|
testAcc.Backend.Commit()
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
web3Service.run(web3Service.ctx.Done())
|
|
<-exitRoutine
|
|
}()
|
|
|
|
header := &gethTypes.Header{
|
|
Number: big.NewInt(42),
|
|
Time: 308534400,
|
|
}
|
|
|
|
web3Service.headerChan <- header
|
|
web3Service.cancel()
|
|
exitRoutine <- true
|
|
|
|
if web3Service.blockHeight.Cmp(header.Number) != 0 {
|
|
t.Errorf("block number not set, expected %v, got %v", header.Number, web3Service.blockHeight)
|
|
}
|
|
|
|
if web3Service.blockHash.Hex() != header.Hash().Hex() {
|
|
t.Errorf("block hash not set, expected %v, got %v", header.Hash().Hex(), web3Service.blockHash.Hex())
|
|
}
|
|
|
|
if web3Service.blockTime != time.Unix(int64(header.Time), 0) {
|
|
t.Errorf("block time not set, expected %v, got %v", time.Unix(int64(header.Time), 0), web3Service.blockTime)
|
|
}
|
|
|
|
blockInfoExistsInCache, info, err := web3Service.blockCache.BlockInfoByHash(web3Service.blockHash)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !blockInfoExistsInCache {
|
|
t.Error("Expected block info to exist in cache")
|
|
}
|
|
if info.Hash != web3Service.blockHash {
|
|
t.Errorf(
|
|
"Expected block info hash to be %v, got %v",
|
|
web3Service.blockHash,
|
|
info.Hash,
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestBlockHashByHeight_ReturnsHash(t *testing.T) {
|
|
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
|
|
Endpoint: endpoint,
|
|
BlockFetcher: &goodFetcher{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err)
|
|
}
|
|
ctx := context.Background()
|
|
|
|
block := gethTypes.NewBlock(
|
|
&gethTypes.Header{
|
|
Number: big.NewInt(15),
|
|
Time: 150,
|
|
},
|
|
[]*gethTypes.Transaction{},
|
|
[]*gethTypes.Header{},
|
|
[]*gethTypes.Receipt{},
|
|
)
|
|
wanted := block.Hash()
|
|
|
|
hash, err := web3Service.BlockHashByHeight(ctx, big.NewInt(0))
|
|
if err != nil {
|
|
t.Fatalf("Could not get block hash with given height %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(hash.Bytes(), wanted.Bytes()) {
|
|
t.Fatalf("Block hash did not equal expected hash, expected: %v, got: %v", wanted, hash)
|
|
}
|
|
|
|
exists, _, err := web3Service.blockCache.BlockInfoByHash(wanted)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !exists {
|
|
t.Error("Expected block info to be cached")
|
|
}
|
|
}
|
|
|
|
func TestBlockExists_ValidHash(t *testing.T) {
|
|
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
|
|
Endpoint: endpoint,
|
|
BlockFetcher: &goodFetcher{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err)
|
|
}
|
|
|
|
block := gethTypes.NewBlock(
|
|
&gethTypes.Header{
|
|
Number: big.NewInt(0),
|
|
},
|
|
[]*gethTypes.Transaction{},
|
|
[]*gethTypes.Header{},
|
|
[]*gethTypes.Receipt{},
|
|
)
|
|
|
|
exists, height, err := web3Service.BlockExists(context.Background(), block.Hash())
|
|
if err != nil {
|
|
t.Fatalf("Could not get block hash with given height %v", err)
|
|
}
|
|
|
|
if !exists {
|
|
t.Fatal("Expected BlockExists to return true.")
|
|
}
|
|
if height.Cmp(block.Number()) != 0 {
|
|
t.Fatalf("Block height did not equal expected height, expected: %v, got: %v", big.NewInt(42), height)
|
|
}
|
|
|
|
exists, _, err = web3Service.blockCache.BlockInfoByHeight(height)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !exists {
|
|
t.Error("Expected block to be cached")
|
|
}
|
|
}
|
|
|
|
func TestBlockExists_InvalidHash(t *testing.T) {
|
|
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
|
|
Endpoint: endpoint,
|
|
BlockFetcher: &goodFetcher{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err)
|
|
}
|
|
|
|
_, _, err = web3Service.BlockExists(context.Background(), common.BytesToHash([]byte{0}))
|
|
if err == nil {
|
|
t.Fatal("Expected BlockExists to error with invalid hash")
|
|
}
|
|
}
|
|
|
|
func TestBlockExists_UsesCachedBlockInfo(t *testing.T) {
|
|
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
|
|
Endpoint: endpoint,
|
|
BlockFetcher: nil, // nil blockFetcher would panic if cached value not used
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unable to setup web3 ETH1.0 chain service: %v", err)
|
|
}
|
|
|
|
block := gethTypes.NewBlock(
|
|
&gethTypes.Header{
|
|
Number: big.NewInt(0),
|
|
},
|
|
[]*gethTypes.Transaction{},
|
|
[]*gethTypes.Header{},
|
|
[]*gethTypes.Receipt{},
|
|
)
|
|
|
|
if err := web3Service.blockCache.AddBlock(block); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
exists, height, err := web3Service.BlockExists(context.Background(), block.Hash())
|
|
if err != nil {
|
|
t.Fatalf("Could not get block hash with given height %v", err)
|
|
}
|
|
|
|
if !exists {
|
|
t.Fatal("Expected BlockExists to return true.")
|
|
}
|
|
if height.Cmp(block.Number()) != 0 {
|
|
t.Fatalf("Block height did not equal expected height, expected: %v, got: %v", big.NewInt(42), height)
|
|
}
|
|
}
|
|
|
|
func TestBlockNumberByTimestamp(t *testing.T) {
|
|
web3Service, err := NewService(context.Background(), &Web3ServiceConfig{
|
|
Endpoint: endpoint,
|
|
BlockFetcher: &goodFetcher{},
|
|
Client: nil,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
bn, err := web3Service.BlockNumberByTimestamp(ctx, 150000 /* time */)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if bn.Cmp(big.NewInt(0)) == 0 {
|
|
t.Error("Returned a block with zero number, expected to be non zero")
|
|
}
|
|
}
|