2020-11-13 16:16:47 +00:00
|
|
|
package snapshotsync
|
2020-10-13 12:56:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2020-11-28 14:26:28 +00:00
|
|
|
"github.com/ledgerwatch/lmdb-go/lmdb"
|
2020-10-13 12:56:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeadersGenerateIndex(t *testing.T) {
|
2021-05-18 12:13:16 +00:00
|
|
|
snPath := t.TempDir()
|
2021-05-20 16:46:12 +00:00
|
|
|
snVK := ethdb.NewMDBX().Path(snPath).MustOpen()
|
2020-10-13 12:56:16 +00:00
|
|
|
defer os.RemoveAll(snPath)
|
|
|
|
headers := generateHeaders(10)
|
2021-03-21 13:15:25 +00:00
|
|
|
err := snVK.Update(context.Background(), func(tx ethdb.RwTx) error {
|
2020-10-13 12:56:16 +00:00
|
|
|
for _, header := range headers {
|
|
|
|
headerBytes, innerErr := rlp.EncodeToBytes(header)
|
|
|
|
if innerErr != nil {
|
|
|
|
panic(innerErr)
|
|
|
|
}
|
2021-04-02 06:36:49 +00:00
|
|
|
innerErr = tx.Put(dbutils.HeadersBucket, dbutils.HeaderKey(header.Number.Uint64(), header.Hash()), headerBytes)
|
2020-10-13 12:56:16 +00:00
|
|
|
if innerErr != nil {
|
|
|
|
panic(innerErr)
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := tx.RwCursor(dbutils.HeadersSnapshotInfoBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
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()
|
|
|
|
|
2021-05-20 16:46:12 +00:00
|
|
|
db := ethdb.NewMDBX().InMem().WithBucketsConfig(ethdb.DefaultBucketConfigs).MustOpen()
|
|
|
|
defer db.Close()
|
2020-10-13 12:56:16 +00:00
|
|
|
//we need genesis
|
2020-10-24 17:05:12 +00:00
|
|
|
err = rawdb.WriteCanonicalHash(ethdb.NewObjectDatabase(db), headers[0].Hash(), headers[0].Number.Uint64())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-17 09:54:29 +00:00
|
|
|
var snKV ethdb.RwKV
|
2021-05-20 16:46:12 +00:00
|
|
|
snKV = ethdb.NewMDBX().Path(snPath).Flags(func(flags uint) uint { return flags | lmdb.Readonly }).WithBucketsConfig(ethdb.DefaultBucketConfigs).MustOpen()
|
|
|
|
defer snKV.Close()
|
2020-10-13 12:56:16 +00:00
|
|
|
|
2021-05-08 08:45:40 +00:00
|
|
|
snKV = ethdb.NewSnapshotKV().SnapshotDB([]string{dbutils.HeadersSnapshotInfoBucket, dbutils.HeadersBucket}, snKV).DB(db).Open()
|
2021-04-02 11:22:25 +00:00
|
|
|
snDb := ethdb.NewObjectDatabase(snKV)
|
2021-04-08 11:03:33 +00:00
|
|
|
err = GenerateHeaderIndexes(context.Background(), snDb)
|
2020-10-13 12:56:16 +00:00
|
|
|
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, err1 := rawdb.ReadCanonicalHash(snDB, header.Number.Uint64())
|
|
|
|
if err1 != nil {
|
|
|
|
t.Errorf("reading canonical hash for block %d: %v", header.Number.Uint64(), err1)
|
|
|
|
}
|
|
|
|
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")
|
2020-10-24 06:57:09 +00:00
|
|
|
} else {
|
|
|
|
td, err := rawdb.ReadTd(snDB, header.Hash(), header.Number.Uint64())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if td.Cmp(td) != 0 {
|
|
|
|
t.Error(i, header.Hash(), header.Number.Uint64(), "td incorrect")
|
|
|
|
}
|
2020-10-13 12:56:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|