Skip to content

Commit

Permalink
Merge pull request ethereum#2 from aaronbuchwald/encode-blocks
Browse files Browse the repository at this point in the history
Add simple helper for writing/reading blocks from file
  • Loading branch information
aaronbuchwald committed Oct 27, 2021
2 parents 4b0aa75 + 0fde3f8 commit a781ee3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/types/block.go
Expand Up @@ -21,7 +21,9 @@ import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"reflect"
"sync/atomic"
"time"
Expand Down Expand Up @@ -383,3 +385,34 @@ func (b *Block) Hash() common.Hash {
}

type Blocks []*Block

func WriteBlocks(w io.Writer, blocks Blocks) error {
return rlp.Encode(w, blocks)
}

func ReadBlocks(b []byte) ([]*Block, error) {
blocks := new(Blocks)
if err := rlp.DecodeBytes(b, blocks); err != nil {
return nil, err
}
return *blocks, nil
}

func WriteBlocksToFile(filename string, blocks Blocks) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()

return WriteBlocks(f, blocks)
}

func ReadBlocksFromFile(filename string) ([]*Block, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}

return ReadBlocks(b)
}
46 changes: 46 additions & 0 deletions core/types/block_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"hash"
"math/big"
"path"
"reflect"
"testing"

Expand Down Expand Up @@ -217,6 +218,51 @@ func BenchmarkEncodeBlock(b *testing.B) {
}
}

func TestEncodeBlocks(t *testing.T) {
blocks := []*Block{
makeBenchBlock(),
makeBenchBlock(),
makeBenchBlock(),
}

buffer := bytes.NewBuffer(make([]byte, 0, 32000))
if err := WriteBlocks(buffer, blocks); err != nil {
t.Fatal(err)
}

decodedBlocks, err := ReadBlocks(buffer.Bytes())
if err != nil {
t.Fatal(err)
}

if len(blocks) != len(decodedBlocks) {
t.Fatalf("expected decoded blocks (%d) == blocks (%d)", len(decodedBlocks), len(blocks))
}
for i, decodedBlock := range decodedBlocks {
if blocks[i].Hash() != decodedBlock.Hash() {
t.Fatalf("Expected hatches to match at index %d, found decoded block hash: %s, actual hash: %s", i, decodedBlock.Hash(), blocks[i].Hash())
}
}

tempFile := path.Join(t.TempDir(), "blocks")
if err := WriteBlocksToFile(tempFile, blocks); err != nil {
t.Fatal(err)
}

blocksFromFile, err := ReadBlocksFromFile(tempFile)
if err != nil {
t.Fatal(err)
}
if len(blocks) != len(blocksFromFile) {
t.Fatalf("expected blocks from file (%d) == blocks (%d)", len(blocksFromFile), len(blocks))
}
for i, blockFromFile := range blocksFromFile {
if blocks[i].Hash() != blockFromFile.Hash() {
t.Fatalf("Expected hatches to match at index %d, found block from file hash: %s, actual hash: %s", i, blockFromFile.Hash(), blocks[i].Hash())
}
}
}

// testHasher is the helper tool for transaction/receipt list hashing.
// The original hasher is trie, in order to get rid of import cycle,
// use the testing hasher instead.
Expand Down

0 comments on commit a781ee3

Please sign in to comment.