github.com/MetalBlockchain/subnet-evm@v0.4.9/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  	"github.com/MetalBlockchain/subnet-evm/core/types"
    11  
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  type MockTrieDB struct {
    17  	LastDereference common.Hash
    18  	LastCommit      common.Hash
    19  }
    20  
    21  func (t *MockTrieDB) Dereference(root common.Hash) {
    22  	t.LastDereference = root
    23  }
    24  func (t *MockTrieDB) Commit(root common.Hash, report bool, callback func(common.Hash)) error {
    25  	t.LastCommit = root
    26  	return nil
    27  }
    28  func (t *MockTrieDB) Size() (common.StorageSize, common.StorageSize) {
    29  	return 0, 0
    30  }
    31  func (t *MockTrieDB) Cap(limit common.StorageSize) error {
    32  	return nil
    33  }
    34  
    35  func TestCappedMemoryTrieWriter(t *testing.T) {
    36  	m := &MockTrieDB{}
    37  	cacheConfig := &CacheConfig{Pruning: true, CommitInterval: 4096}
    38  	w := NewTrieWriter(m, cacheConfig)
    39  	assert := assert.New(t)
    40  	for i := 0; i < int(cacheConfig.CommitInterval)+1; i++ {
    41  		bigI := big.NewInt(int64(i))
    42  		block := types.NewBlock(
    43  			&types.Header{
    44  				Root:   common.BigToHash(bigI),
    45  				Number: bigI,
    46  			},
    47  			nil, nil, nil, nil,
    48  		)
    49  
    50  		assert.NoError(w.InsertTrie(block))
    51  		assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on insert")
    52  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on insert")
    53  
    54  		w.AcceptTrie(block)
    55  		if i <= tipBufferSize {
    56  			assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on accept")
    57  		} else {
    58  			assert.Equal(common.BigToHash(big.NewInt(int64(i-tipBufferSize))), m.LastDereference, "should have dereferenced old block on last accept")
    59  			m.LastDereference = common.Hash{}
    60  		}
    61  		if i < int(cacheConfig.CommitInterval) {
    62  			assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on accept")
    63  		} else {
    64  			assert.Equal(block.Root(), m.LastCommit, "should have committed block after CommitInterval")
    65  			m.LastCommit = common.Hash{}
    66  		}
    67  
    68  		w.RejectTrie(block)
    69  		assert.Equal(block.Root(), m.LastDereference, "should have dereferenced block on reject")
    70  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on reject")
    71  		m.LastDereference = common.Hash{}
    72  	}
    73  }
    74  
    75  func TestNoPruningTrieWriter(t *testing.T) {
    76  	m := &MockTrieDB{}
    77  	w := NewTrieWriter(m, &CacheConfig{})
    78  	assert := assert.New(t)
    79  	for i := 0; i < tipBufferSize+1; i++ {
    80  		bigI := big.NewInt(int64(i))
    81  		block := types.NewBlock(
    82  			&types.Header{
    83  				Root:   common.BigToHash(bigI),
    84  				Number: bigI,
    85  			},
    86  			nil, nil, nil, nil,
    87  		)
    88  
    89  		assert.NoError(w.InsertTrie(block))
    90  		assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on insert")
    91  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on insert")
    92  
    93  		w.AcceptTrie(block)
    94  		assert.Equal(common.Hash{}, m.LastDereference, "should not have dereferenced block on accept")
    95  		assert.Equal(block.Root(), m.LastCommit, "should have committed block on accept")
    96  		m.LastCommit = common.Hash{}
    97  
    98  		w.RejectTrie(block)
    99  		assert.Equal(block.Root(), m.LastDereference, "should have dereferenced block on reject")
   100  		assert.Equal(common.Hash{}, m.LastCommit, "should not have committed block on reject")
   101  		m.LastDereference = common.Hash{}
   102  	}
   103  }