github.com/ubiq/go-ethereum@v3.0.1+incompatible/consensus/ubqhash/consensus_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package ubqhash
    18  
    19  import (
    20  	// "encoding/json"
    21  	"math/big"
    22  	// "os"
    23  	// "path/filepath"
    24  	"testing"
    25  
    26  	// "github.com/ubiq/go-ubiq/common/math"
    27  	// "github.com/ubiq/go-ubiq/core"
    28  	// "github.com/ubiq/go-ubiq/core/types"
    29  	// "github.com/ubiq/go-ubiq/core/vm"
    30  	// "github.com/ubiq/go-ubiq/ethdb"
    31  	"github.com/ubiq/go-ubiq/params"
    32  )
    33  
    34  // TODO: write new difficulty tests
    35  /*
    36  type diffTest struct {
    37  	ParentTimestamp    uint64
    38  	ParentDifficulty   *big.Int
    39  	CurrentTimestamp   uint64
    40  	CurrentBlocknumber *big.Int
    41  	CurrentDifficulty  *big.Int
    42  }
    43  
    44  func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
    45  	var ext struct {
    46  		ParentTimestamp    string
    47  		ParentDifficulty   string
    48  		CurrentTimestamp   string
    49  		CurrentBlocknumber string
    50  		CurrentDifficulty  string
    51  	}
    52  	if err := json.Unmarshal(b, &ext); err != nil {
    53  		return err
    54  	}
    55  
    56  	d.ParentTimestamp = math.MustParseUint64(ext.ParentTimestamp)
    57  	d.ParentDifficulty = math.MustParseBig256(ext.ParentDifficulty)
    58  	d.CurrentTimestamp = math.MustParseUint64(ext.CurrentTimestamp)
    59  	d.CurrentBlocknumber = math.MustParseBig256(ext.CurrentBlocknumber)
    60  	d.CurrentDifficulty = math.MustParseBig256(ext.CurrentDifficulty)
    61  
    62  	return nil
    63  }
    64  
    65  func TestCalcDifficulty(t *testing.T) {
    66  	file, err := os.Open(filepath.Join("..", "..", "tests", "testdata", "BasicTests", "difficulty.json"))
    67  	if err != nil {
    68  		t.Skip(err)
    69  	}
    70  	defer file.Close()
    71  
    72  	tests := make(map[string]diffTest)
    73  	err = json.NewDecoder(file).Decode(&tests)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	config := &params.ChainConfig{HomesteadBlock: big.NewInt(1150000)}
    79  
    80  	var (
    81  		testdb    = ethdb.NewMemDatabase()
    82  		gspec     = &core.Genesis{Config: config}
    83  		genesis   = gspec.MustCommit(testdb)
    84  		blocks, _ = core.GenerateChain(config, genesis, NewFaker(), testdb, 88, nil)
    85  	)
    86  
    87  	headers := make([]*types.Header, len(blocks))
    88  	for i, block := range blocks {
    89  		headers[i] = block.Header()
    90  	}
    91  	// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
    92  	chain, _ := core.NewBlockChain(testdb, nil, config, NewFaker(), vm.Config{}, nil)
    93  	defer chain.Stop()
    94  
    95  	for name, test := range tests {
    96  		number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
    97  		diff := CalcDifficulty(chain, test.CurrentTimestamp, &types.Header{
    98  			Number:     number,
    99  			Time:       test.ParentTimestamp,
   100  			Difficulty: test.ParentDifficulty,
   101  		})
   102  		if diff.Cmp(test.CurrentDifficulty) != 0 {
   103  			t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
   104  		}
   105  	}
   106  }*/
   107  
   108  func TestCalcBaseBlockReward(t *testing.T) {
   109  	config := *params.MainnetChainConfig
   110  	_, reward := CalcBaseBlockReward(config.Ubqhash, big.NewInt(1))
   111  	if reward.Cmp(big.NewInt(8e+18)) != 0 {
   112  		t.Error("TestCalcBaseBlockReward 8 (start)", "failed. Expected", big.NewInt(8e+18), "and calculated", reward)
   113  	}
   114  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(358363))
   115  	if reward.Cmp(big.NewInt(8e+18)) != 0 {
   116  		t.Error("TestCalcBaseBlockReward 8 (end)", "failed. Expected", big.NewInt(8e+18), "and calculated", reward)
   117  	}
   118  
   119  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(358364))
   120  	if reward.Cmp(big.NewInt(7e+18)) != 0 {
   121  		t.Error("TestCalcBaseBlockReward 7 (start)", "failed. Expected", big.NewInt(7e+18), "and calculated", reward)
   122  	}
   123  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(716727))
   124  	if reward.Cmp(big.NewInt(7e+18)) != 0 {
   125  		t.Error("TestCalcBaseBlockReward 7 (end)", "failed. Expected", big.NewInt(7e+18), "and calculated", reward)
   126  	}
   127  
   128  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(716728))
   129  	if reward.Cmp(big.NewInt(6e+18)) != 0 {
   130  		t.Error("TestCalcBaseBlockReward 6 (start)", "failed. Expected", big.NewInt(6e+18), "and calculated", reward)
   131  	}
   132  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(1075090))
   133  	if reward.Cmp(big.NewInt(6e+18)) != 0 {
   134  		t.Error("TestCalcBaseBlockReward 6 (end)", "failed. Expected", big.NewInt(6e+18), "and calculated", reward)
   135  	}
   136  
   137  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(1075091))
   138  	if reward.Cmp(big.NewInt(5e+18)) != 0 {
   139  		t.Error("TestCalcBaseBlockReward 5 (start)", "failed. Expected", big.NewInt(5e+18), "and calculated", reward)
   140  	}
   141  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(1433454))
   142  	if reward.Cmp(big.NewInt(5e+18)) != 0 {
   143  		t.Error("TestCalcBaseBlockReward 5 (end)", "failed. Expected", big.NewInt(5e+18), "and calculated", reward)
   144  	}
   145  
   146  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(1433455))
   147  	if reward.Cmp(big.NewInt(4e+18)) != 0 {
   148  		t.Error("TestCalcBaseBlockReward 4 (start)", "failed. Expected", big.NewInt(4e+18), "and calculated", reward)
   149  	}
   150  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(1791818))
   151  	if reward.Cmp(big.NewInt(4e+18)) != 0 {
   152  		t.Error("TestCalcBaseBlockReward 4 (end)", "failed. Expected", big.NewInt(4e+18), "and calculated", reward)
   153  	}
   154  
   155  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(1791819))
   156  	if reward.Cmp(big.NewInt(3e+18)) != 0 {
   157  		t.Error("TestCalcBaseBlockReward 3 (start)", "failed. Expected", big.NewInt(3e+18), "and calculated", reward)
   158  	}
   159  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(2150181))
   160  	if reward.Cmp(big.NewInt(3e+18)) != 0 {
   161  		t.Error("TestCalcBaseBlockReward 3 (end)", "failed. Expected", big.NewInt(3e+18), "and calculated", reward)
   162  	}
   163  
   164  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(2150182))
   165  	if reward.Cmp(big.NewInt(2e+18)) != 0 {
   166  		t.Error("TestCalcBaseBlockReward 2 (start)", "failed. Expected", big.NewInt(2e+18), "and calculated", reward)
   167  	}
   168  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(2508545))
   169  	if reward.Cmp(big.NewInt(2e+18)) != 0 {
   170  		t.Error("TestCalcBaseBlockReward 2 (end)", "failed. Expected", big.NewInt(2e+18), "and calculated", reward)
   171  	}
   172  
   173  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(2508546))
   174  	if reward.Cmp(big.NewInt(1e+18)) != 0 {
   175  		t.Error("TestCalcBaseBlockReward 1 (start)", "failed. Expected", big.NewInt(1e+18), "and calculated", reward)
   176  	}
   177  }
   178  
   179  func TestCalcUncleBlockReward(t *testing.T) {
   180  	config := params.MainnetChainConfig
   181  	reward := big.NewInt(8e+18)
   182  	// depth 1
   183  	u := CalcUncleBlockReward(config, big.NewInt(5), big.NewInt(4), reward)
   184  	if u.Cmp(big.NewInt(4e+18)) != 0 {
   185  		t.Error("TestCalcUncleBlockReward 8", "failed. Expected", big.NewInt(4e+18), "and calculated", u)
   186  	}
   187  
   188  	// depth 2
   189  	u = CalcUncleBlockReward(config, big.NewInt(8), big.NewInt(6), reward)
   190  	if u.Cmp(big.NewInt(0)) != 0 {
   191  		t.Error("TestCalcUncleBlockReward 8", "failed. Expected", big.NewInt(0), "and calculated", u)
   192  	}
   193  
   194  	// depth 3 (before negative fix)
   195  	u = CalcUncleBlockReward(config, big.NewInt(8), big.NewInt(5), reward)
   196  	if u.Cmp(big.NewInt(-4e+18)) != 0 {
   197  		t.Error("TestCalcUncleBlockReward 8", "failed. Expected", big.NewInt(-4e+18), "and calculated", u)
   198  	}
   199  
   200  	// depth 3 (after negative fix)
   201  	u = CalcUncleBlockReward(config, big.NewInt(10), big.NewInt(7), reward)
   202  	if u.Cmp(big.NewInt(0)) != 0 {
   203  		t.Error("TestCalcUncleBlockReward 8", "failed. Expected", big.NewInt(0), "and calculated", u)
   204  	}
   205  
   206  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(358364))
   207  	expected := big.NewInt(35e+17)
   208  	// depth 1 (after stepdown)
   209  	u = CalcUncleBlockReward(config, big.NewInt(8), big.NewInt(7), reward)
   210  	if u.Cmp(expected) != 0 {
   211  		t.Error("TestCalcUncleBlockReward 7", "failed. Expected", expected, "and calculated", u)
   212  	}
   213  
   214  	_, reward = CalcBaseBlockReward(config.Ubqhash, big.NewInt(1075091))
   215  	expected = big.NewInt(25e+17)
   216  	// depth 1 (after stepdown)
   217  	u = CalcUncleBlockReward(config, big.NewInt(8), big.NewInt(7), reward)
   218  	if u.Cmp(expected) != 0 {
   219  		t.Error("TestCalcUncleBlockReward 5", "failed. Expected", expected, "and calculated", u)
   220  	}
   221  
   222  }