This repository has been archived by the owner on Oct 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EIP 100 Difficulty adjustment and testing (#36)
* Set up testing framework for difficulty * Set up framework for testing difficulty * Implemented EIP 100 and set up testing config * Set up testing framework for difficulty * Set up framework for testing difficulty * Implemented EIP 100 and set up testing config * Cleaned up and moved params to file * Fixed usages of CalcDifficulty * Moved parsing of hex or decimal strings functions to common package
- Loading branch information
1 parent
08ef773
commit fe17e9e
Showing
9 changed files
with
311 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
package params | ||
|
||
import "math/big" | ||
|
||
const ( | ||
QuadCoeffDiv uint64 = 512 // Divisor for the quadratic particle of the memory cost equation. | ||
|
||
StackLimit uint64 = 1024 // Maximum size of VM stack allowed. | ||
MemoryGas uint64 = 3 // Times the address of the (highest referenced byte in memory + 1). NOTE: referencing happens on read, write and in instructions such as RETURN and CALL. | ||
) | ||
|
||
var ( | ||
DifficultyBoundDivisor = big.NewInt(2048) // The bound divisor of the difficulty, used in the update calculations. | ||
|
||
MinimumDifficulty = big.NewInt(131072) // The minimum that the difficulty may ever be. | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2015 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 <http://www.gnu.org/licenses/>. | ||
|
||
package tests | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestETHDifficulty(t *testing.T) { | ||
fileNames, _ := filepath.Glob(filepath.Join(ethBasicTestDir, "*")) | ||
|
||
supportedTests := map[string]bool{ | ||
// "difficulty.json": true, // Testing ETH mainnet config | ||
"difficultyFrontier.json": true, | ||
"difficultyHomestead.json": true, | ||
"difficultyByzantium.json": true, | ||
} | ||
|
||
// Loop through each file | ||
for _, fn := range fileNames { | ||
fileName := fn[strings.LastIndex(fn, "/")+1 : len(fn)] | ||
|
||
if !supportedTests[fileName] { | ||
continue | ||
} | ||
|
||
t.Run(fileName, func(t *testing.T) { | ||
config := ChainConfigs[fileName] | ||
tests := make(map[string]DifficultyTest) | ||
|
||
if err := readJsonFile(fn, &tests); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Loop through each test in file | ||
for key, test := range tests { | ||
// Subtest within the JSON file | ||
t.Run(key, func(t *testing.T) { | ||
if err := test.runDifficulty(t, &config); err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
|
||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2015 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 <http://www.gnu.org/licenses/>. | ||
|
||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/eth-classic/go-ethereum/common" | ||
"github.com/eth-classic/go-ethereum/common/hexutil" | ||
"github.com/eth-classic/go-ethereum/core" | ||
"github.com/eth-classic/go-ethereum/core/types" | ||
"github.com/eth-classic/go-ethereum/params" | ||
) | ||
|
||
// DifficultyTest is the structure of JSON from test files | ||
type DifficultyTest struct { | ||
ParentTimestamp string `json:"parentTimestamp"` | ||
ParentDifficulty string `json:"parentDifficulty"` | ||
UncleHash common.Hash `json:"parentUncles"` | ||
CurrentTimestamp string `json:"currentTimestamp"` | ||
CurrentBlockNumber string `json:"currentBlockNumber"` | ||
CurrentDifficulty string `json:"currentDifficulty"` | ||
} | ||
|
||
func (test *DifficultyTest) runDifficulty(t *testing.T, config *core.ChainConfig) error { | ||
currentNumber, _ := hexutil.HexOrDecimalToBigInt(test.CurrentBlockNumber) | ||
parentNumber := new(big.Int).Sub(currentNumber, big.NewInt(1)) | ||
parentTimestamp, _ := hexutil.HexOrDecimalToBigInt(test.ParentTimestamp) | ||
parentDifficulty, _ := hexutil.HexOrDecimalToBigInt(test.ParentDifficulty) | ||
currentTimestamp, _ := hexutil.HexOrDecimalToUint64(test.CurrentTimestamp) | ||
|
||
parent := &types.Header{ | ||
Number: parentNumber, | ||
Time: parentTimestamp, | ||
Difficulty: parentDifficulty, | ||
UncleHash: test.UncleHash, | ||
} | ||
|
||
// Check to make sure difficulty is above minimum | ||
if parentDifficulty.Cmp(params.MinimumDifficulty) < 0 { | ||
t.Skip("difficulty below minimum") | ||
return nil | ||
} | ||
|
||
actual := core.CalcDifficulty(config, currentTimestamp, parent) | ||
exp, _ := hexutil.HexOrDecimalToBigInt(test.CurrentDifficulty) | ||
|
||
if actual.Cmp(exp) != 0 { | ||
return fmt.Errorf("parent[time %v diff %v unclehash:%x] child[time %v number %v] diff %v != expected %v", | ||
test.ParentTimestamp, test.ParentDifficulty, test.UncleHash, | ||
test.CurrentTimestamp, test.CurrentBlockNumber, actual, exp) | ||
} | ||
return nil | ||
|
||
} |
Oops, something went wrong.