diff --git a/consensus/clique/clique_test.go b/consensus/clique/clique_test.go index afcab1d1f7..a53bfa79e3 100644 --- a/consensus/clique/clique_test.go +++ b/consensus/clique/clique_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/lru" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" @@ -122,3 +124,84 @@ func TestSealHash(t *testing.T) { t.Errorf("have %x, want %x", have, want) } } + +type testChainReader struct { +} + +func (t testChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { + return &types.Block{} +} +func (t testChainReader) Config() *params.ChainConfig { + return ¶ms.ChainConfig{} +} +func (t testChainReader) CurrentHeader() *types.Header { + return &types.Header{} +} +func (t testChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { + var rueck *types.Header = nil + if number == 22431048 { + rueck = &types.Header{} + rueck.Number = new(big.Int).SetUint64(number) + rueck.Time = 99 + rueck.Difficulty = big.NewInt(1) + rueck.GasLimit = 5000 + } + return rueck +} +func (t testChainReader) GetHeaderByHash(hash common.Hash) *types.Header { + return &types.Header{} +} +func (t testChainReader) GetHeaderByNumber(number uint64) *types.Header { + return &types.Header{} +} + +func TestVerifyUncles(t *testing.T) { + clique := Clique{} + chain := testChainReader{} + block := types.Block{} + ret := clique.VerifyUncles(chain, &block) + if ret != nil { + t.Errorf("VerifyUncles not successful") + } +} + +func TestPrepare(t *testing.T) { + clique := Clique{} + cc := params.CliqueConfig{} + cc.Epoch = 364032 + clique.config = &cc + cache := lru.NewCache[common.Hash, *Snapshot](0) + s := Snapshot{} + s.Signers = make(map[common.Address]struct{}) + s.Signers[common.Address{}] = struct{}{} + cache.Add(common.Hash{}, &s) + clique.recents = cache + chain := testChainReader{} + header := types.Header{} + header.Number = big.NewInt(22431049) + header.Time = 100 + header.Difficulty = big.NewInt(131072) + header.GasLimit = 5000 + ret := clique.Prepare(chain, &header) + if ret != nil { + t.Errorf("Prepare not successful") + } +} + +func TestAuthorize(t *testing.T) { + clique := Clique{} + signer := common.Address{} + clique.Authorize(signer) + if clique.signer != signer { + t.Errorf("Authorize not successful") + } +} + +func TestCliqueRLP(t *testing.T) { + header := types.Header{} + header.Extra = make([]uint8, 65) + ret := CliqueRLP(&header) + if len(ret) != 496 { + t.Errorf("CliqueRLP not successful") + } +} diff --git a/consensus/ethash/consensus_test.go b/consensus/ethash/consensus_test.go index e3793cd1b0..9244a27b13 100644 --- a/consensus/ethash/consensus_test.go +++ b/consensus/ethash/consensus_test.go @@ -186,3 +186,166 @@ func BenchmarkDifficultyCalculator(b *testing.B) { } }) } + +type testChainHeaderReader struct{} + +func (t testChainHeaderReader) Config() *params.ChainConfig { + return ¶ms.ChainConfig{} +} +func (t testChainHeaderReader) CurrentHeader() *types.Header { + return &types.Header{} +} +func (t testChainHeaderReader) GetHeader(hash common.Hash, number uint64) *types.Header { + var rueck *types.Header = nil + if number == 22431047 { + rueck = &types.Header{} + rueck.Number = new(big.Int).SetUint64(number) + rueck.Time = 99 + rueck.Difficulty = big.NewInt(1) + rueck.GasLimit = 5000 + } + return rueck +} +func (t testChainHeaderReader) GetHeaderByHash(hash common.Hash) *types.Header { + return &types.Header{} +} +func (t testChainHeaderReader) GetHeaderByNumber(number uint64) *types.Header { + return &types.Header{} +} + +func TestVerifyHeader(t *testing.T) { + ethash := Ethash{} + chain := testChainHeaderReader{} + header := types.Header{} + header.Number = big.NewInt(22431048) + header.Time = 100 + header.Difficulty = new(big.Int) + header.Difficulty.SetString("6739986666787659948666753771754907668409286105635143120275902693376", 10) + header.GasLimit = 5000 + + ret := ethash.VerifyHeader(chain, &header) + if ret != nil { + t.Errorf("VerifyHeader not successful") + } +} + +func TestVerifyHeaders(t *testing.T) { + ethash := Ethash{} + chain := testChainHeaderReader{} + var liste []*types.Header + liste = append(liste, &types.Header{}) + liste = append(liste, &types.Header{}) + liste[0].Number = big.NewInt(22431048) + liste[0].Time = 100 + liste[0].Difficulty = new(big.Int) + liste[0].Difficulty.SetString("6739986666787659948666753771754907668409286105635143120275902693376", 10) + liste[0].GasLimit = 5000 + liste[1].Number = big.NewInt(22431049) + liste[1].Time = 101 + liste[1].Difficulty = liste[0].Difficulty + liste[1].GasLimit = 5000 + _, c2 := ethash.VerifyHeaders(chain, liste) + r2 := <-c2 + if r2 != nil { + t.Errorf("VerifyHeaders not successful") + } +} + +type testChainReader struct { +} + +func (t testChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { + return &types.Block{} +} +func (t testChainReader) Config() *params.ChainConfig { + return ¶ms.ChainConfig{} +} +func (t testChainReader) CurrentHeader() *types.Header { + return &types.Header{} +} +func (t testChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { + var rueck *types.Header = nil + if number == 22431047 { + rueck = &types.Header{} + rueck.Number = new(big.Int).SetUint64(number) + rueck.Time = 99 + rueck.Difficulty = big.NewInt(1) + rueck.GasLimit = 5000 + rueck.ParentHash = common.Hash{4} + } else if number == 22431046 { + rueck = &types.Header{} + rueck.Number = new(big.Int).SetUint64(number) + rueck.Time = 98 + rueck.Difficulty = big.NewInt(1) + rueck.GasLimit = 5000 + } + return rueck +} +func (t testChainReader) GetHeaderByHash(hash common.Hash) *types.Header { + return &types.Header{} +} +func (t testChainReader) GetHeaderByNumber(number uint64) *types.Header { + return &types.Header{} +} + +type testTrieHasher struct{} + +func (t testTrieHasher) Reset() {} +func (t testTrieHasher) Update([]byte, []byte) error { return nil } +func (t testTrieHasher) Hash() common.Hash { + ret := common.Hash{} + return ret +} + +func TestVerifyUncles(t *testing.T) { + ethash := Ethash{} + chain := testChainReader{} + + header := types.Header{} + header.Number = big.NewInt(22431048) + header.Time = 101 + header.Difficulty = new(big.Int) + header.Difficulty.SetString("6739986666787659948666753771754907668409286105635143120275902693376", 10) + header.GasLimit = 5000 + header.ParentHash = common.Hash{5} + + body := types.Body{} + var uncles []*types.Header + uncles = append(uncles, &types.Header{}) + uncles[0].Number = new(big.Int).SetUint64(22431047) + uncles[0].Time = 100 + uncles[0].Difficulty = header.Difficulty + uncles[0].GasLimit = 5000 + uncles[0].ParentHash = common.Hash{4} + body.Uncles = uncles + + block := types.NewBlock(&header, &body, nil, testTrieHasher{}) + + ret := ethash.VerifyUncles(chain, block) + if ret != nil { + t.Errorf("VerifyUncles not successful") + } +} + +func TestPrepare(t *testing.T) { + ethash := Ethash{} + chain := testChainReader{} + header := types.Header{} + header.Number = big.NewInt(22431048) + header.Time = 100 + header.Difficulty = big.NewInt(131072) + header.GasLimit = 5000 + ret := ethash.Prepare(chain, &header) + if ret != nil { + t.Errorf("Prepare not successful") + } +} + +func TestSealHash(t *testing.T) { + ethash := Ethash{} + header := types.Header{} + hash := ethash.SealHash(&header) + if len(hash) != 32 { + t.Errorf("SealHash not successful") + } +} diff --git a/consensus/misc/eip4844/eip4844_test.go b/consensus/misc/eip4844/eip4844_test.go index f4e3cb3d9a..376a0158a7 100644 --- a/consensus/misc/eip4844/eip4844_test.go +++ b/consensus/misc/eip4844/eip4844_test.go @@ -127,3 +127,86 @@ func TestFakeExponential(t *testing.T) { } } } + +func TestVerifyEIP4844Header(t *testing.T) { + config := params.ChainConfig{} + londonBlock := big.NewInt(25000000) + config.LondonBlock = londonBlock + bsc := params.BlobScheduleConfig{} + cancum := params.BlobConfig{} + bsc.Cancun = &cancum + cancum.Max = 10 + config.BlobScheduleConfig = &bsc + var cancumTime uint64 = 10000000 + config.CancunTime = &cancumTime + + parent := types.Header{} + parent.Number = new(big.Int).SetUint64(24999999) + parent.Time = 9999999 + parent.Difficulty = big.NewInt(1) + parent.GasLimit = 5000 + header := types.Header{} + header.Number = new(big.Int).SetUint64(25000000) + header.Time = 10000000 + header.Difficulty = big.NewInt(1) + header.GasLimit = 5000 + var excessBlobGas uint64 = 0 + header.ExcessBlobGas = &excessBlobGas + var blobGasUsed uint64 = 131072 + header.BlobGasUsed = &blobGasUsed + + erg := VerifyEIP4844Header(&config, &parent, &header) + if erg != nil { + t.Errorf("VerifyEIP4844Header not successful") + } +} + +func TestMaxBlobsPerBlock(t *testing.T) { + config := params.ChainConfig{} + londonBlock := big.NewInt(25000000) + config.LondonBlock = londonBlock + bsc := params.BlobScheduleConfig{} + cancum := params.BlobConfig{} + bsc.Cancun = &cancum + cancum.Max = 10 + config.BlobScheduleConfig = &bsc + var cancumTime uint64 = 10000000 + config.CancunTime = &cancumTime + time := uint64(10000000) + ret := MaxBlobsPerBlock(&config, time) + if ret != cancum.Max { + t.Errorf("MaxBlobsPerBlock not successful") + } +} + +func TestMaxBlobGasPerBlock(t *testing.T) { + config := params.ChainConfig{} + londonBlock := big.NewInt(25000000) + config.LondonBlock = londonBlock + bsc := params.BlobScheduleConfig{} + cancum := params.BlobConfig{} + bsc.Cancun = &cancum + cancum.Max = 10 + config.BlobScheduleConfig = &bsc + var cancumTime uint64 = 10000000 + config.CancunTime = &cancumTime + time := uint64(10000000) + ret := MaxBlobGasPerBlock(&config, time) + var soll uint64 = uint64(cancum.Max * 131072) + if ret != soll { + t.Errorf("MaxBlobGasPerBlock not successful") + } +} + +func TestLatestMaxBlobsPerBlock(t *testing.T) { + config := params.ChainConfig{} + bsc := params.BlobScheduleConfig{} + cancum := params.BlobConfig{} + bsc.Cancun = &cancum + cancum.Max = 10 + config.BlobScheduleConfig = &bsc + ret := LatestMaxBlobsPerBlock(&config) + if ret != cancum.Max { + t.Errorf("LatestMaxBlobsPerBlock not successful") + } +} diff --git a/core/vm/contract_test.go b/core/vm/contract_test.go new file mode 100644 index 0000000000..e58f7700cc --- /dev/null +++ b/core/vm/contract_test.go @@ -0,0 +1,86 @@ +// Copyright 2017 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package vm + +import ( + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/holiman/uint256" +) + +func TestValidJumpdest(t *testing.T) { + cField := []byte{11, 12} + c := Contract{Code: cField} + destField := [4]uint64{2, 0, 0, 0} + dest := uint256.Int(destField) + ret := c.validJumpdest(&dest) + if ret != false { + t.Errorf("validJumpdest not successful") + } + + c = Contract{} + destField = [4]uint64{0, 1, 0, 0} + dest = uint256.Int(destField) + ret = c.validJumpdest(&dest) + if ret != false { + t.Errorf("validJumpdest not successful") + } + + c = Contract{Code: cField} + destField = [4]uint64{1, 0, 0, 0} + dest = uint256.Int(destField) + ret = c.validJumpdest(&dest) + if ret != false { + t.Errorf("validJumpdest not successful") + } + + cField[1] = byte(JUMPDEST) + c = Contract{Code: cField} + ret = c.validJumpdest(&dest) + if ret != true { + t.Errorf("validJumpdest not successful") + } +} + +func TestIsCode(t *testing.T) { + destField := [4]uint64{1, 0, 0, 0} + cField := []byte{11, byte(JUMPDEST)} + c := Contract{Code: cField} + ret := c.isCode(destField[0]) + if ret != true || c.analysis == nil { + t.Errorf("isCode not successful") + } + + destField = [4]uint64{1, 0, 0, 0} + aField := []byte{0, 0, 0, 0, 0} + c = Contract{analysis: aField} + ret = c.isCode(destField[0]) + if ret != true { + t.Errorf("isCode not successful") + } + + destField = [4]uint64{1, 0, 0, 0} + //aField := []byte{0,0,0,0,0} + jd := make(map[common.Hash]bitvec) + c = Contract{CodeHash: common.Hash{}, jumpdests: jd} + c.CodeHash[0] = 1 + ret = c.isCode(destField[0]) + if ret != true || c.analysis == nil { + t.Errorf("isCode not successful") + } +}