github.com/dim4egster/coreth@v0.10.2/core/genesis_test.go (about)

     1  // (c) 2019-2021, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2017 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package core
    28  
    29  import (
    30  	_ "embed"
    31  	"math/big"
    32  	"reflect"
    33  	"testing"
    34  
    35  	"github.com/davecgh/go-spew/spew"
    36  	"github.com/dim4egster/coreth/consensus/dummy"
    37  	"github.com/dim4egster/coreth/core/rawdb"
    38  	"github.com/dim4egster/coreth/core/vm"
    39  	"github.com/dim4egster/coreth/ethdb"
    40  	"github.com/dim4egster/coreth/params"
    41  	"github.com/ethereum/go-ethereum/common"
    42  )
    43  
    44  func setupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig, common.Hash, error) {
    45  	conf, err := SetupGenesisBlock(db, genesis)
    46  	stored := rawdb.ReadCanonicalHash(db, 0)
    47  	return conf, stored, err
    48  }
    49  
    50  func TestGenesisBlockForTesting(t *testing.T) {
    51  	genesisBlockForTestingHash := common.HexToHash("0xb378f22ccd9ad52c6c42f5d46ef2aad6d6866cfcb778ea97a0b6dfde13387330")
    52  	block := GenesisBlockForTesting(rawdb.NewMemoryDatabase(), common.Address{1}, big.NewInt(1))
    53  	if block.Hash() != genesisBlockForTestingHash {
    54  		t.Errorf("wrong testing genesis hash, got %v, want %v", block.Hash(), genesisBlockForTestingHash)
    55  	}
    56  }
    57  
    58  func TestSetupGenesis(t *testing.T) {
    59  	apricotPhase1Config := *params.TestApricotPhase1Config
    60  	apricotPhase1Config.ApricotPhase1BlockTimestamp = big.NewInt(100)
    61  	var (
    62  		customghash = common.HexToHash("0x1099a11e9e454bd3ef31d688cf21936671966407bc330f051d754b5ce401e7ed")
    63  		customg     = Genesis{
    64  			Config: &apricotPhase1Config,
    65  			Alloc: GenesisAlloc{
    66  				{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
    67  			},
    68  		}
    69  		oldcustomg = customg
    70  	)
    71  
    72  	rollbackApricotPhase1Config := apricotPhase1Config
    73  	rollbackApricotPhase1Config.ApricotPhase1BlockTimestamp = big.NewInt(90)
    74  	oldcustomg.Config = &rollbackApricotPhase1Config
    75  	tests := []struct {
    76  		name       string
    77  		fn         func(ethdb.Database) (*params.ChainConfig, common.Hash, error)
    78  		wantConfig *params.ChainConfig
    79  		wantHash   common.Hash
    80  		wantErr    error
    81  	}{
    82  		{
    83  			name: "genesis without ChainConfig",
    84  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    85  				return setupGenesisBlock(db, new(Genesis))
    86  			},
    87  			wantErr:    errGenesisNoConfig,
    88  			wantConfig: nil,
    89  		},
    90  		{
    91  			name: "no block in DB, genesis == nil",
    92  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
    93  				return setupGenesisBlock(db, nil)
    94  			},
    95  			wantErr:    ErrNoGenesis,
    96  			wantConfig: nil,
    97  		},
    98  		{
    99  			name: "custom block in DB, genesis == nil",
   100  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   101  				customg.MustCommit(db)
   102  				return setupGenesisBlock(db, nil)
   103  			},
   104  			wantErr:    ErrNoGenesis,
   105  			wantHash:   customghash,
   106  			wantConfig: nil,
   107  		},
   108  		{
   109  			name: "compatible config in DB",
   110  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   111  				oldcustomg.MustCommit(db)
   112  				return setupGenesisBlock(db, &customg)
   113  			},
   114  			wantHash:   customghash,
   115  			wantConfig: customg.Config,
   116  		},
   117  		{
   118  			name: "incompatible config for avalanche fork in DB",
   119  			fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
   120  				// Commit the 'old' genesis block with ApricotPhase1 transition at 90.
   121  				// Advance to block #4, past the ApricotPhase1 transition block of customg.
   122  				genesis := oldcustomg.MustCommit(db)
   123  
   124  				bc, _ := NewBlockChain(db, DefaultCacheConfig, oldcustomg.Config, dummy.NewFullFaker(), vm.Config{}, common.Hash{})
   125  				defer bc.Stop()
   126  
   127  				blocks, _, _ := GenerateChain(oldcustomg.Config, genesis, dummy.NewFullFaker(), db, 4, 25, nil)
   128  				bc.InsertChain(blocks)
   129  				bc.CurrentBlock()
   130  				// This should return a compatibility error.
   131  				return setupGenesisBlock(db, &customg)
   132  			},
   133  			wantHash:   customghash,
   134  			wantConfig: customg.Config,
   135  			wantErr: &params.ConfigCompatError{
   136  				What:         "ApricotPhase1 fork block timestamp",
   137  				StoredConfig: big.NewInt(90),
   138  				NewConfig:    big.NewInt(100),
   139  				RewindTo:     89,
   140  			},
   141  		},
   142  	}
   143  
   144  	for _, test := range tests {
   145  		t.Run(test.name, func(t *testing.T) {
   146  			db := rawdb.NewMemoryDatabase()
   147  			config, hash, err := test.fn(db)
   148  			// Check the return values.
   149  			if !reflect.DeepEqual(err, test.wantErr) {
   150  				spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
   151  				t.Errorf("returned error %#v, want %#v", spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   152  			}
   153  			if !reflect.DeepEqual(config, test.wantConfig) {
   154  				t.Errorf("returned %v\nwant     %v", config, test.wantConfig)
   155  			}
   156  			if hash != test.wantHash {
   157  				t.Errorf("returned hash %s, want %s", hash.Hex(), test.wantHash.Hex())
   158  			} else if err == nil {
   159  				// Check database content.
   160  				stored := rawdb.ReadBlock(db, test.wantHash, 0)
   161  				if stored.Hash() != test.wantHash {
   162  					t.Errorf("block in DB has hash %s, want %s", stored.Hash(), test.wantHash)
   163  				}
   164  			}
   165  		})
   166  	}
   167  }