Skip to content

Commit

Permalink
Read most protocol params from common/params.json
Browse files Browse the repository at this point in the history
* Add params package with exported variables generated from
  github.com/ethereum/common/blob/master/params.json
* Use params package variables in applicable places
* Add check for minimum gas limit in validation of block's gas limit
* Remove common/params.json from go-ethereum to avoid
  outdated version of it
  • Loading branch information
Gustav Simonsson committed Apr 2, 2015
1 parent 516ec28 commit c26c8d3
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 163 deletions.
11 changes: 5 additions & 6 deletions core/block_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/rlp"
"gopkg.in/fatih/set.v0"
Expand Down Expand Up @@ -252,7 +253,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
// an uncle or anything that isn't on the current block chain.
// Validation validates easy over difficult (dagger takes longer time = difficult)
func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header) error {
if len(block.Extra) > 1024 {
if big.NewInt(int64(len(block.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
return fmt.Errorf("Block extra data too long (%d)", len(block.Extra))
}

Expand All @@ -261,13 +262,11 @@ func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header) error {
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
}

// TODO: use use minGasLimit and gasLimitBoundDivisor from
// https://github.com/ethereum/common/blob/master/params.json
// block.gasLimit - parent.gasLimit <= parent.gasLimit / 1024
// block.gasLimit - parent.gasLimit <= parent.gasLimit / GasLimitBoundDivisor
a := new(big.Int).Sub(block.GasLimit, parent.GasLimit)
a.Abs(a)
b := new(big.Int).Div(parent.GasLimit, big.NewInt(1024))
if !(a.Cmp(b) < 0) {
b := new(big.Int).Div(parent.GasLimit, params.GasLimitBoundDivisor)
if !(a.Cmp(b) < 0) || (block.GasLimit.Cmp(params.MinGasLimit) == -1) {
return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.GasLimit, a, b)
}

Expand Down
14 changes: 6 additions & 8 deletions core/chain_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
)

Expand All @@ -32,18 +33,15 @@ type StateQuery interface {
func CalcDifficulty(block, parent *types.Header) *big.Int {
diff := new(big.Int)

diffBoundDiv := big.NewInt(2048)
min := big.NewInt(131072)

adjust := new(big.Int).Div(parent.Difficulty, diffBoundDiv)
if (block.Time - parent.Time) < 8 {
adjust := new(big.Int).Div(parent.Difficulty, params.DifficultyBoundDivisor)
if big.NewInt(int64(block.Time)-int64(parent.Time)).Cmp(params.DurationLimit) < 0 {
diff.Add(parent.Difficulty, adjust)
} else {
diff.Sub(parent.Difficulty, adjust)
}

if diff.Cmp(min) < 0 {
return min
if diff.Cmp(params.MinimumDifficulty) < 0 {
return params.MinimumDifficulty
}

return diff
Expand Down Expand Up @@ -76,7 +74,7 @@ func CalcGasLimit(parent, block *types.Block) *big.Int {
result := new(big.Int).Add(previous, curInt)
result.Div(result, big.NewInt(1024))

return common.BigMax(GenesisGasLimit, result)
return common.BigMax(params.GenesisGasLimit, result)
}

type ChainManager struct {
Expand Down
3 changes: 2 additions & 1 deletion core/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)

type Execution struct {
Expand Down Expand Up @@ -43,7 +44,7 @@ func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.

env := self.env
evm := self.evm
if env.Depth() == vm.MaxCallDepth {
if env.Depth() > int(params.CallCreateDepth.Int64()) {
caller.ReturnGas(self.Gas, self.price)

return nil, vm.DepthError{}
Expand Down
8 changes: 3 additions & 5 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package core
import (
"encoding/json"
"fmt"
"math/big"
"os"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)

/*
Expand All @@ -18,13 +18,11 @@ import (
var ZeroHash256 = make([]byte, 32)
var ZeroHash160 = make([]byte, 20)
var ZeroHash512 = make([]byte, 64)
var GenesisDiff = big.NewInt(131072)
var GenesisGasLimit = big.NewInt(3141592)

func GenesisBlock(db common.Database) *types.Block {
genesis := types.NewBlock(common.Hash{}, common.Address{}, common.Hash{}, GenesisDiff, 42, "")
genesis := types.NewBlock(common.Hash{}, common.Address{}, common.Hash{}, params.GenesisDifficulty, 42, "")
genesis.Header().Number = common.Big0
genesis.Header().GasLimit = GenesisGasLimit
genesis.Header().GasLimit = params.GenesisGasLimit
genesis.Header().GasUsed = common.Big0
genesis.Header().Time = 0

Expand Down
9 changes: 5 additions & 4 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)

const tryJit = false
Expand Down Expand Up @@ -178,17 +179,17 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
)

// Transaction gas
if err = self.UseGas(vm.GasTx); err != nil {
if err = self.UseGas(params.TxGas); err != nil {
return nil, nil, InvalidTxError(err)
}

// Pay data gas
dgas := new(big.Int)
for _, byt := range self.data {
if byt != 0 {
dgas.Add(dgas, vm.GasTxDataNonzeroByte)
dgas.Add(dgas, params.TxDataNonZeroGas)
} else {
dgas.Add(dgas, vm.GasTxDataZeroByte)
dgas.Add(dgas, params.TxDataZeroGas)
}
}

Expand All @@ -202,7 +203,7 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
ret, err, ref = vmenv.Create(sender, self.msg.Data(), self.gas, self.gasPrice, self.value)
if err == nil {
dataGas := big.NewInt(int64(len(ret)))
dataGas.Mul(dataGas, vm.GasCreateByte)
dataGas.Mul(dataGas, params.CreateDataGas)
if err := self.UseGas(dataGas); err == nil {
ref.SetCode(ret)
} else {
Expand Down
15 changes: 8 additions & 7 deletions core/vm/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)

type Address interface {
Expand All @@ -27,28 +28,28 @@ func PrecompiledContracts() map[string]*PrecompiledAccount {
return map[string]*PrecompiledAccount{
// ECRECOVER
string(common.LeftPadBytes([]byte{1}, 20)): &PrecompiledAccount{func(l int) *big.Int {
return GasEcrecover
return params.EcrecoverGas
}, ecrecoverFunc},

// SHA256
string(common.LeftPadBytes([]byte{2}, 20)): &PrecompiledAccount{func(l int) *big.Int {
n := big.NewInt(int64(l+31) / 32)
n.Mul(n, GasSha256Word)
return n.Add(n, GasSha256Base)
n.Mul(n, params.Sha256WordGas)
return n.Add(n, params.Sha256Gas)
}, sha256Func},

// RIPEMD160
string(common.LeftPadBytes([]byte{3}, 20)): &PrecompiledAccount{func(l int) *big.Int {
n := big.NewInt(int64(l+31) / 32)
n.Mul(n, GasRipemdWord)
return n.Add(n, GasRipemdBase)
n.Mul(n, params.Ripemd160WordGas)
return n.Add(n, params.Ripemd160Gas)
}, ripemd160Func},

string(common.LeftPadBytes([]byte{4}, 20)): &PrecompiledAccount{func(l int) *big.Int {
n := big.NewInt(int64(l+31) / 32)
n.Mul(n, GasIdentityWord)
n.Mul(n, params.IdentityWordGas)

return n.Add(n, GasIdentityBase)
return n.Add(n, params.IdentityGas)
}, memCpy},
}
}
Expand Down
2 changes: 0 additions & 2 deletions core/vm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const (
JitVmTy
MaxVmTy

MaxCallDepth = 1025

LogTyPretty byte = 0x1
LogTyDiff byte = 0x2
)
Expand Down
3 changes: 2 additions & 1 deletion core/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vm

import (
"fmt"
"github.com/ethereum/go-ethereum/params"
"math/big"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func IsStack(err error) bool {
type DepthError struct{}

func (self DepthError) Error() string {
return fmt.Sprintf("Max call depth exceeded (%d)", MaxCallDepth)
return fmt.Sprintf("Max call depth exceeded (%d)", params.CallCreateDepth)
}

func IsDepthErr(err error) bool {
Expand Down
58 changes: 12 additions & 46 deletions core/vm/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vm

import (
"fmt"
"github.com/ethereum/go-ethereum/params"
"math/big"
)

Expand All @@ -13,45 +14,10 @@ var (
GasSlowStep = big.NewInt(10)
GasExtStep = big.NewInt(20)

GasStorageGet = big.NewInt(50)
GasStorageAdd = big.NewInt(20000)
GasStorageMod = big.NewInt(5000)
GasLogBase = big.NewInt(375)
GasLogTopic = big.NewInt(375)
GasLogByte = big.NewInt(8)
GasCreate = big.NewInt(32000)
GasCreateByte = big.NewInt(200)
GasCall = big.NewInt(40)
GasCallValueTransfer = big.NewInt(9000)
GasStipend = big.NewInt(2300)
GasCallNewAccount = big.NewInt(25000)
GasReturn = big.NewInt(0)
GasStop = big.NewInt(0)
GasJumpDest = big.NewInt(1)
GasReturn = big.NewInt(0)
GasStop = big.NewInt(0)

RefundStorage = big.NewInt(15000)
RefundSuicide = big.NewInt(24000)

GasMemWord = big.NewInt(3)
GasQuadCoeffDenom = big.NewInt(512)
GasContractByte = big.NewInt(200)
GasTransaction = big.NewInt(21000)
GasTxDataNonzeroByte = big.NewInt(68)
GasTxDataZeroByte = big.NewInt(4)
GasTx = big.NewInt(21000)
GasExp = big.NewInt(10)
GasExpByte = big.NewInt(10)

GasSha3Base = big.NewInt(30)
GasSha3Word = big.NewInt(6)
GasSha256Base = big.NewInt(60)
GasSha256Word = big.NewInt(12)
GasRipemdBase = big.NewInt(600)
GasRipemdWord = big.NewInt(12)
GasEcrecover = big.NewInt(3000)
GasIdentityBase = big.NewInt(15)
GasIdentityWord = big.NewInt(3)
GasCopyWord = big.NewInt(3)
GasContractByte = big.NewInt(200)
)

func baseCheck(op OpCode, stack *stack, gas *big.Int) error {
Expand All @@ -71,8 +37,8 @@ func baseCheck(op OpCode, stack *stack, gas *big.Int) error {
return err
}

if r.stackPush && len(stack.data)-r.stackPop+1 > 1024 {
return fmt.Errorf("stack limit reached (%d)", maxStack)
if r.stackPush && len(stack.data)-r.stackPop+1 > int(params.StackLimit.Int64()) {
return fmt.Errorf("stack limit reached (%d)", params.StackLimit.Int64())
}

gas.Add(gas, r.gas)
Expand Down Expand Up @@ -145,13 +111,13 @@ var _baseCheck = map[OpCode]req{
BALANCE: {1, GasExtStep, true},
EXTCODESIZE: {1, GasExtStep, true},
EXTCODECOPY: {4, GasExtStep, false},
SLOAD: {1, GasStorageGet, true},
SLOAD: {1, params.SloadGas, true},
SSTORE: {2, Zero, false},
SHA3: {2, GasSha3Base, true},
CREATE: {3, GasCreate, true},
CALL: {7, GasCall, true},
CALLCODE: {7, GasCall, true},
JUMPDEST: {0, GasJumpDest, false},
SHA3: {2, params.Sha3Gas, true},
CREATE: {3, params.CreateGas, true},
CALL: {7, params.CallGas, true},
CALLCODE: {7, params.CallGas, true},
JUMPDEST: {0, params.JumpdestGas, false},
SUICIDE: {1, Zero, false},
RETURN: {2, Zero, false},
PUSH1: {0, GasFastestStep, true},
Expand Down
2 changes: 0 additions & 2 deletions core/vm/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"math/big"
)

const maxStack = 1024

func newStack() *stack {
return &stack{}
}
Expand Down
Loading

0 comments on commit c26c8d3

Please sign in to comment.