gitlab.com/flarenetwork/coreth@v0.1.1/core/state_manager_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package core
     5  
     6  import (
     7  	"math/big"
     8  	"testing"
     9  
    10  	"gitlab.com/flarenetwork/coreth/core/types"
    11  
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  type MockTrieDB struct {
    17  	LastReference   common.Hash
    18  	LastDereference common.Hash
    19  	LastCommit      common.Hash
    20  }
    21  
    22  func (t *MockTrieDB) Reference(child common.Hash, parent common.Hash) {
    23  	t.LastReference = child
    24  }
    25  func (t *MockTrieDB) Dereference(root common.Hash) {
    26  	t.LastDereference = root
    27  }
    28  func (t *MockTrieDB) Commit(root common.Hash, report bool, callback func(common.Hash)) error {
    29  	t.LastCommit = root
    30  	return nil
    31  }
    32  func (t *MockTrieDB) Size() (common.StorageSize, common.StorageSize) {
    33  	return 0, 0
    34  }
    35  func (t *MockTrieDB) Cap(limit common.StorageSize) error {
    36  	return nil
    37  }
    38  
    39  func TestCappedMemoryTrieWriter(t *testing.T) {
    40  	m := &MockTrieDB{}
    41  	w := NewTrieWriter(m, &CacheConfig{Pruning: true})
    42  	assert := assert.New(t)
    43  	for i := 0; i < commitInterval+1; i++ {
    44  		bigI := big.NewInt(int64(i))
    45  		block := types.NewBlock(
    46  			&types.Header{
    47  				Root:   common.BigToHash(bigI),
    48  				Number: bigI,
    49  			},
    50  			nil, nil, nil, nil, nil, true,
    51  		)
    52  
    53  		assert.NoError(w.InsertTrie(block))
    54  		assert.Equal(block.Root(), m.LastReference, "should not have referenced block on insert")
    55  		assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on insert")
    56  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on insert")
    57  		m.LastReference = common.Hash{}
    58  
    59  		w.AcceptTrie(block)
    60  		assert.Equal(common.Hash{}, m.LastReference, "should not have referenced block on accept")
    61  		if i < tipBufferSize {
    62  			assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on accept")
    63  		} else {
    64  			assert.Equal(common.BigToHash(big.NewInt(int64(i-tipBufferSize))), m.LastDereference, "should have dereferenced old block on last accept")
    65  			m.LastDereference = common.Hash{}
    66  		}
    67  		if i < commitInterval {
    68  			assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on accept")
    69  		} else {
    70  			assert.Equal(block.Root(), m.LastCommit, "should have committed block after commitInterval")
    71  			m.LastCommit = common.Hash{}
    72  		}
    73  
    74  		w.RejectTrie(block)
    75  		assert.Equal(common.Hash{}, m.LastReference, "should not have referenced block on reject")
    76  		assert.Equal(block.Root(), m.LastDereference, "should have dereferenced block on reject")
    77  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on reject")
    78  		m.LastDereference = common.Hash{}
    79  	}
    80  }
    81  
    82  func TestNoPruningTrieWriter(t *testing.T) {
    83  	m := &MockTrieDB{}
    84  	w := NewTrieWriter(m, &CacheConfig{})
    85  	assert := assert.New(t)
    86  	for i := 0; i < tipBufferSize+1; i++ {
    87  		bigI := big.NewInt(int64(i))
    88  		block := types.NewBlock(
    89  			&types.Header{
    90  				Root:   common.BigToHash(bigI),
    91  				Number: bigI,
    92  			},
    93  			nil, nil, nil, nil, nil, true,
    94  		)
    95  
    96  		assert.NoError(w.InsertTrie(block))
    97  		assert.Equal(common.Hash{}, m.LastReference, "should not have referenced block on insert")
    98  		assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on insert")
    99  		assert.Equal(block.Root(), m.LastCommit, "should have committed block on insert")
   100  		m.LastCommit = common.Hash{}
   101  
   102  		w.AcceptTrie(block)
   103  		assert.Equal(common.Hash{}, m.LastReference, "should not have referenced block on accept")
   104  		assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on accept")
   105  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on accept")
   106  
   107  		w.RejectTrie(block)
   108  		assert.Equal(common.Hash{}, m.LastReference, "should not have referenced block on reject")
   109  		assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on reject")
   110  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on reject")
   111  	}
   112  }