mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 13:46:43 +00:00
core: add test for ChainManager Export
This commit is contained in:
parent
55bdcfaeac
commit
df92e76f0f
1 changed files with 70 additions and 0 deletions
|
|
@ -17,7 +17,9 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
|
@ -500,6 +502,74 @@ func TestGenesisMismatch(t *testing.T) {
|
|||
}
|
||||
*/
|
||||
|
||||
func TestExport(t *testing.T) {
|
||||
db, _ := ethdb.NewMemDatabase()
|
||||
genesis, err := WriteTestNetGenesisBlock(db, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
bc := chm(genesis, db)
|
||||
|
||||
chain := makeChainWithDiff(genesis, []int{1, 2, 31}, 10)
|
||||
bc.InsertChain(chain)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
bc.Export(buf)
|
||||
|
||||
stream := rlp.NewStream(buf, 0)
|
||||
|
||||
decode := func(decoded *types.Block) {
|
||||
if err := stream.Decode(decoded); err != nil && err != io.EOF {
|
||||
t.Errorf("RLP decoding failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
res := &types.Block{}
|
||||
decode(res) // genesis
|
||||
|
||||
for _, block := range chain {
|
||||
res = &types.Block{}
|
||||
decode(res)
|
||||
if block.Header().Hash() != res.Header().Hash() {
|
||||
t.Errorf("block hash mismatch: want: %x, have: %x", block.Header().Hash(), res.Header().Hash())
|
||||
}
|
||||
}
|
||||
|
||||
// export errors
|
||||
buf = new(bytes.Buffer)
|
||||
err = bc.ExportN(buf, 1, 0)
|
||||
if err == nil {
|
||||
t.Error("expected export error")
|
||||
}
|
||||
if buf.Len() != 0 {
|
||||
t.Error("expected no writing to export writer")
|
||||
}
|
||||
|
||||
// failure while exporting blocks
|
||||
DeleteCanonicalHash(db, chain[1].NumberU64())
|
||||
buf = new(bytes.Buffer)
|
||||
err = bc.ExportN(buf, 0, 3)
|
||||
if err == nil {
|
||||
t.Error("expected export error")
|
||||
}
|
||||
|
||||
stream = rlp.NewStream(buf, 0)
|
||||
res = &types.Block{}
|
||||
decode(res)
|
||||
if res.NumberU64() != 0 {
|
||||
t.Errorf("block number mismatch: want: 0, have: %d", res.NumberU64())
|
||||
}
|
||||
|
||||
res = &types.Block{}
|
||||
decode(res)
|
||||
|
||||
err = stream.Decode(res)
|
||||
if err != io.EOF {
|
||||
t.Errorf("expected no more blocks, have block number: %d", res.NumberU64())
|
||||
}
|
||||
}
|
||||
|
||||
// failpow returns false from Verify for a certain block number.
|
||||
type failpow struct{ num uint64 }
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue