erigon-pulse/turbo/torrent/postprocessing_test.go
ledgerwatch 94360212cb
Download/seed headers and block bodies via BitTorrent (#1195)
* save state

* torrent experiments

* torrent experiment passed

* fixes after merge

* snapshot headers processing passed

* save state

* save state

* download headers works after snapshot processing

* save state

* save state

* save state

* save state

* add lazy load tx to snapshots, increase number of trackers

* save state

* speedup getting info

* change logging

* move to turbo package

* save state

* save state

* save state

* cleanup

* save state

* add test test

* save state

* lmdb debugging

* fix readonly mode

* save state

* fix build

* sync works

* save state

* save state

* save state

* allow cmd stages stageSenders use snapshots

* debugging failed hashing

* remove experimental tests

* remove torrent experimental tests

* fix lint

* extract snapshot wrapper

* metainfo checker

* add remote seeder

* add logs

* update gomod

* remove useless code

* fix lint&remove useless code

* extract verify snapshot to separated command

* skip debug test

* fix test

* change type of seedSnapshot flag

* add eth logger to torrent lib

* skip debug test

* add Close method

* review fixes

* fix lint

Co-authored-by: b00ris <b00ris@mail.ru>
Co-authored-by: alex.sharov <AskAlexSharov@gmail.com>
2020-10-07 13:15:49 +01:00

92 lines
2.9 KiB
Go

package torrent
import (
"context"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/rlp"
"math/big"
"os"
"testing"
)
func TestHeadersGenerateIndex(t *testing.T) {
snPath := os.TempDir() + "/sn"
snVK := ethdb.NewLMDB().Path(snPath).MustOpen()
defer os.RemoveAll(snPath)
headers := generateHeaders(10)
err := snVK.Update(context.Background(), func(tx ethdb.Tx) error {
for _, header := range headers {
headerBytes, innerErr := rlp.EncodeToBytes(header)
if innerErr != nil {
panic(innerErr)
}
innerErr = tx.Cursor(dbutils.HeaderPrefix).Put(dbutils.HeaderKey(header.Number.Uint64(), header.Hash()), headerBytes)
if innerErr != nil {
panic(innerErr)
}
}
c := tx.Cursor(dbutils.SnapshotInfoBucket)
innerErr := c.Put([]byte(dbutils.SnapshotHeadersHeadHash), headers[len(headers)-1].Hash().Bytes())
if innerErr != nil {
return innerErr
}
innerErr = c.Put([]byte(dbutils.SnapshotHeadersHeadNumber), headers[len(headers)-1].Number.Bytes())
if innerErr != nil {
return innerErr
}
return nil
})
if err != nil {
t.Fatal(err)
}
snVK.Close()
db := ethdb.NewLMDB().InMem().WithBucketsConfig(ethdb.DefaultBucketConfigs).MustOpen()
//we need genesis
rawdb.WriteCanonicalHash(ethdb.NewObjectDatabase(db), headers[0].Hash(), headers[0].Number.Uint64())
snKV := ethdb.NewLMDB().Path(snPath).ReadOnly().WithBucketsConfig(ethdb.DefaultBucketConfigs).MustOpen()
snKV = ethdb.NewSnapshotKV().For(dbutils.HeaderPrefix).For(dbutils.SnapshotInfoBucket).SnapshotDB(snKV).DB(db).MustOpen()
err = GenerateHeaderIndexes(context.Background(), ethdb.NewObjectDatabase(snKV))
if err != nil {
t.Fatal(err)
}
snDB := ethdb.NewObjectDatabase(snKV)
td := big.NewInt(0)
for i, header := range headers {
td = td.Add(td, header.Difficulty)
canonical := rawdb.ReadCanonicalHash(snDB, header.Number.Uint64())
if canonical != header.Hash() {
t.Error(i, "canonical not correct", canonical)
}
hasHeader := rawdb.HasHeader(snDB, header.Hash(), header.Number.Uint64())
if !hasHeader {
t.Error(i, header.Hash(), header.Number.Uint64(), "not exists")
}
headerNumber := rawdb.ReadHeaderNumber(snDB, header.Hash())
if headerNumber == nil {
t.Error(i, "empty header number")
} else if *headerNumber != header.Number.Uint64() {
t.Error(i, header.Hash(), header.Number.Uint64(), "header number incorrect")
}
if td == nil {
t.Error(i, "empty td")
} else if td.Cmp(rawdb.ReadTd(snDB, header.Hash(), header.Number.Uint64())) != 0 {
t.Error(i, header.Hash(), header.Number.Uint64(), "td incorrect")
}
}
}
func generateHeaders(n int) []types.Header {
headers := make([]types.Header, n)
for i := uint64(0); i < uint64(n); i++ {
headers[i] = types.Header{Difficulty: new(big.Int).SetUint64(i), Number: new(big.Int).SetUint64(i)}
}
return headers
}