block_cache_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package core
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/core/types"
  7. )
  8. func newChain(size int) (chain []*types.Block) {
  9. var parentHash common.Hash
  10. for i := 0; i < size; i++ {
  11. block := types.NewBlock(parentHash, common.Address{}, common.Hash{}, new(big.Int), 0, "")
  12. block.Header().Number = big.NewInt(int64(i))
  13. chain = append(chain, block)
  14. parentHash = block.Hash()
  15. }
  16. return
  17. }
  18. func insertChainCache(cache *BlockCache, chain []*types.Block) {
  19. for _, block := range chain {
  20. cache.Push(block)
  21. }
  22. }
  23. func TestNewBlockCache(t *testing.T) {
  24. chain := newChain(3)
  25. cache := NewBlockCache(2)
  26. insertChainCache(cache, chain)
  27. if cache.hashes[0] != chain[1].Hash() {
  28. t.Error("oldest block incorrect")
  29. }
  30. }
  31. func TestInclusion(t *testing.T) {
  32. chain := newChain(3)
  33. cache := NewBlockCache(3)
  34. insertChainCache(cache, chain)
  35. for _, block := range chain {
  36. if b := cache.Get(block.Hash()); b == nil {
  37. t.Errorf("getting %x failed", block.Hash())
  38. }
  39. }
  40. }