github.com/aquanetwork/aquachain@v1.7.8/core/genesis_test.go (about)

     1  // Copyright 2017 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The aquachain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/davecgh/go-spew/spew"
    26  	"gitlab.com/aquachain/aquachain/aquadb"
    27  	"gitlab.com/aquachain/aquachain/common"
    28  	"gitlab.com/aquachain/aquachain/consensus/aquahash"
    29  	"gitlab.com/aquachain/aquachain/core/vm"
    30  	"gitlab.com/aquachain/aquachain/params"
    31  )
    32  
    33  func TestDefaultGenesisAlloc(t *testing.T) {
    34  	t.Skip()
    35  	m := DefaultGenesisBlock().Alloc
    36  	fmt.Println("Bad Balances:", len(m))
    37  	//aquawei := big.NewFloat(params.Aqua)
    38  	for k := range m {
    39  		//fmt.Printf("%x %.6f\n", k, new(big.Float).Quo(new(big.Float).SetInt(v.Balance), aquawei))
    40  		fmt.Printf(`"%x",`+"\n", k)
    41  	}
    42  }
    43  func TestDefaultGenesisBlock(t *testing.T) {
    44  	block := DefaultGenesisBlock().ToBlock(nil)
    45  	if block.Hash() != params.MainnetGenesisHash {
    46  		t.Errorf("wrong mainnet genesis hash, got %x, want %x", block.Hash(), params.MainnetGenesisHash)
    47  	}
    48  	block = DefaultTestnetGenesisBlock().ToBlock(nil)
    49  	if block.Hash() != params.TestnetGenesisHash {
    50  		t.Errorf("wrong testnet genesis hash, got %x, want %x", block.Hash(), params.TestnetGenesisHash)
    51  	}
    52  
    53  }
    54  
    55  func TestSetupGenesis(t *testing.T) {
    56  	var (
    57  		customghash = common.HexToHash("0x92f036d05929e5762b8e83ce7c104b37881922732b32fd39253efe1e7e5c2b51")
    58  		customg     = Genesis{
    59  			Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3), HF: params.TestChainConfig.HF, ChainId: big.NewInt(101)},
    60  			Alloc: GenesisAlloc{
    61  				{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
    62  			},
    63  		}
    64  		oldcustomg = customg
    65  	)
    66  	oldcustomg.Config = &params.ChainConfig{HomesteadBlock: big.NewInt(2), HF: params.TestChainConfig.HF, ChainId: big.NewInt(102)}
    67  	tests := []struct {
    68  		name       string
    69  		fn         func(aquadb.Database) (*params.ChainConfig, common.Hash, error)
    70  		wantConfig *params.ChainConfig
    71  		wantHash   common.Hash
    72  		wantErr    error
    73  	}{
    74  		{
    75  			name: "genesis without ChainConfig",
    76  			fn: func(db aquadb.Database) (*params.ChainConfig, common.Hash, error) {
    77  				return SetupGenesisBlock(db, new(Genesis))
    78  			},
    79  			wantErr:    errGenesisNoConfig,
    80  			wantConfig: params.AllAquahashProtocolChanges,
    81  		},
    82  		{
    83  			name: "no block in DB, genesis == nil",
    84  			fn: func(db aquadb.Database) (*params.ChainConfig, common.Hash, error) {
    85  				return SetupGenesisBlock(db, nil)
    86  			},
    87  			wantHash:   params.MainnetGenesisHash,
    88  			wantConfig: params.MainnetChainConfig,
    89  		},
    90  		{
    91  			name: "mainnet block in DB, genesis == nil",
    92  			fn: func(db aquadb.Database) (*params.ChainConfig, common.Hash, error) {
    93  				DefaultGenesisBlock().MustCommit(db)
    94  				return SetupGenesisBlock(db, nil)
    95  			},
    96  			wantHash:   params.MainnetGenesisHash,
    97  			wantConfig: params.MainnetChainConfig,
    98  		},
    99  		{
   100  			name: "custom block in DB, genesis == nil",
   101  			fn: func(db aquadb.Database) (*params.ChainConfig, common.Hash, error) {
   102  				customg.MustCommit(db)
   103  				return SetupGenesisBlock(db, nil)
   104  			},
   105  			wantHash:   customghash,
   106  			wantConfig: customg.Config,
   107  		},
   108  		{
   109  			name: "custom block in DB, genesis == testnet",
   110  			fn: func(db aquadb.Database) (*params.ChainConfig, common.Hash, error) {
   111  				customg.MustCommit(db)
   112  				return SetupGenesisBlock(db, DefaultTestnetGenesisBlock())
   113  			},
   114  			wantErr:    &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash},
   115  			wantHash:   params.TestnetGenesisHash,
   116  			wantConfig: params.TestnetChainConfig,
   117  		},
   118  		{
   119  			name: "compatible config in DB",
   120  			fn: func(db aquadb.Database) (*params.ChainConfig, common.Hash, error) {
   121  				oldcustomg.MustCommit(db)
   122  				return SetupGenesisBlock(db, &customg)
   123  			},
   124  			wantHash:   customghash,
   125  			wantConfig: customg.Config,
   126  		},
   127  		{
   128  			name: "incompatible config in DB",
   129  			fn: func(db aquadb.Database) (*params.ChainConfig, common.Hash, error) {
   130  				// Commit the 'old' genesis block with Homestead transition at #2.
   131  				// Advance to block #4, past the homestead transition block of customg.
   132  				genesis := oldcustomg.MustCommit(db)
   133  
   134  				bc, _ := NewBlockChain(db, nil, oldcustomg.Config, aquahash.NewFullFaker(), vm.Config{})
   135  				defer bc.Stop()
   136  
   137  				blocks, _ := GenerateChain(oldcustomg.Config, genesis, aquahash.NewFaker(), db, 4, nil)
   138  				bc.InsertChain(blocks)
   139  				bc.CurrentBlock()
   140  				// This should return a compatibility error.
   141  				return SetupGenesisBlock(db, &customg)
   142  			},
   143  			wantHash:   customghash,
   144  			wantConfig: customg.Config,
   145  			wantErr: &params.ConfigCompatError{
   146  				What:         "Homestead fork block",
   147  				StoredConfig: big.NewInt(2),
   148  				NewConfig:    big.NewInt(3),
   149  				RewindTo:     1,
   150  			},
   151  		},
   152  	}
   153  
   154  	for _, test := range tests {
   155  		db := aquadb.NewMemDatabase()
   156  		config, hash, err := test.fn(db)
   157  		// Check the return values.
   158  		if !reflect.DeepEqual(err, test.wantErr) {
   159  			spew := spew.ConfigState{DisablePointerAddresses: true, DisableCapacities: true}
   160  			t.Errorf("%s: returned error %#v, want %#v", test.name, spew.NewFormatter(err), spew.NewFormatter(test.wantErr))
   161  		}
   162  		if !reflect.DeepEqual(config, test.wantConfig) {
   163  			t.Errorf("%s:\nreturned %v\nwant     %v", test.name, config, test.wantConfig)
   164  		}
   165  		if hash != test.wantHash {
   166  			t.Errorf("%s: returned hash %s, want %s", test.name, hash.Hex(), test.wantHash.Hex())
   167  		} else if err == nil {
   168  			// Check database content.
   169  			stored := GetBlockNoVersion(db, test.wantHash, 0)
   170  			if stored.SetVersion(config.GetBlockVersion(stored.Number())) != test.wantHash {
   171  				t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash)
   172  			}
   173  		}
   174  	}
   175  }