aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorGustav Simonsson <gustav.simonsson@gmail.com>2015-04-02 11:17:15 +0800
committerGustav Simonsson <gustav.simonsson@gmail.com>2015-04-02 12:22:32 +0800
commitc26c8d3a44cdd45994b6d99777d620565bab8f9c (patch)
tree6ab5a74981e9fb9f869f1ef884a375163b898113 /core
parent516ec28544e0f9c76e18d82742d3ae58cfb59cc1 (diff)
downloaddexon-c26c8d3a44cdd45994b6d99777d620565bab8f9c.tar.gz
dexon-c26c8d3a44cdd45994b6d99777d620565bab8f9c.tar.zst
dexon-c26c8d3a44cdd45994b6d99777d620565bab8f9c.zip
Read most protocol params from common/params.json
* 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
Diffstat (limited to 'core')
-rw-r--r--core/block_processor.go11
-rw-r--r--core/chain_manager.go14
-rw-r--r--core/execution.go3
-rw-r--r--core/genesis.go8
-rw-r--r--core/state_transition.go9
-rw-r--r--core/vm/address.go15
-rw-r--r--core/vm/common.go2
-rw-r--r--core/vm/errors.go3
-rw-r--r--core/vm/gas.go58
-rw-r--r--core/vm/stack.go2
-rw-r--r--core/vm/vm.go43
-rw-r--r--core/vm/vm_jit.go4
12 files changed, 67 insertions, 105 deletions
diff --git a/core/block_processor.go b/core/block_processor.go
index f7f0cd188..18667c449 100644
--- a/core/block_processor.go
+++ b/core/block_processor.go
@@ -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"
@@ -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))
}
@@ -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)
}
diff --git a/core/chain_manager.go b/core/chain_manager.go
index f0d3fd4cf..d97a94b06 100644
--- a/core/chain_manager.go
+++ b/core/chain_manager.go
@@ -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"
)
@@ -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
@@ -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 {
diff --git a/core/execution.go b/core/execution.go
index 93fb03ecc..8134471d1 100644
--- a/core/execution.go
+++ b/core/execution.go
@@ -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 {
@@ -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{}
diff --git a/core/genesis.go b/core/genesis.go
index 7958157a4..13656c40c 100644
--- a/core/genesis.go
+++ b/core/genesis.go
@@ -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"
)
/*
@@ -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
diff --git a/core/state_transition.go b/core/state_transition.go
index 7616686db..1994cabf6 100644
--- a/core/state_transition.go
+++ b/core/state_transition.go
@@ -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
@@ -178,7 +179,7 @@ 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)
}
@@ -186,9 +187,9 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
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)
}
}
@@ -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 {
diff --git a/core/vm/address.go b/core/vm/address.go
index 0b3a95dd0..df801863f 100644
--- a/core/vm/address.go
+++ b/core/vm/address.go
@@ -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 {
@@ -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},
}
}
diff --git a/core/vm/common.go b/core/vm/common.go
index 5ff4e05f2..0a93c3dd9 100644
--- a/core/vm/common.go
+++ b/core/vm/common.go
@@ -20,8 +20,6 @@ const (
JitVmTy
MaxVmTy
- MaxCallDepth = 1025
-
LogTyPretty byte = 0x1
LogTyDiff byte = 0x2
)
diff --git a/core/vm/errors.go b/core/vm/errors.go
index ab011bd62..fc3459de0 100644
--- a/core/vm/errors.go
+++ b/core/vm/errors.go
@@ -2,6 +2,7 @@ package vm
import (
"fmt"
+ "github.com/ethereum/go-ethereum/params"
"math/big"
)
@@ -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 {
diff --git a/core/vm/gas.go b/core/vm/gas.go
index 976333a78..f7abe63f8 100644
--- a/core/vm/gas.go
+++ b/core/vm/gas.go
@@ -2,6 +2,7 @@ package vm
import (
"fmt"
+ "github.com/ethereum/go-ethereum/params"
"math/big"
)
@@ -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 {
@@ -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)
@@ -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},
diff --git a/core/vm/stack.go b/core/vm/stack.go
index 168637708..bb232d0b9 100644
--- a/core/vm/stack.go
+++ b/core/vm/stack.go
@@ -5,8 +5,6 @@ import (
"math/big"
)
-const maxStack = 1024
-
func newStack() *stack {
return &stack{}
}
diff --git a/core/vm/vm.go b/core/vm/vm.go
index 59c64e8a3..6222ef8c2 100644
--- a/core/vm/vm.go
+++ b/core/vm/vm.go
@@ -7,6 +7,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/params"
)
type Vm struct {
@@ -640,7 +641,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
} else {
// gas < len(ret) * CreateDataGas == NO_CODE
dataGas := big.NewInt(int64(len(ret)))
- dataGas.Mul(dataGas, GasCreateByte)
+ dataGas.Mul(dataGas, params.CreateDataGas)
if context.UseGas(dataGas) {
ref.SetCode(ret)
}
@@ -667,7 +668,7 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
args := mem.Get(inOffset.Int64(), inSize.Int64())
if len(value.Bytes()) > 0 {
- gas.Add(gas, GasStipend)
+ gas.Add(gas, params.CallStipend)
}
var (
@@ -759,13 +760,13 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
mSize, mStart := stack.data[stack.len()-2], stack.data[stack.len()-1]
- gas.Add(gas, GasLogBase)
- gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(n)), GasLogTopic))
- gas.Add(gas, new(big.Int).Mul(mSize, GasLogByte))
+ gas.Add(gas, params.LogGas)
+ gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(n)), params.LogTopicGas))
+ gas.Add(gas, new(big.Int).Mul(mSize, params.LogDataGas))
newMemSize = calcMemSize(mStart, mSize)
case EXP:
- gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(len(stack.data[stack.len()-2].Bytes()))), GasExpByte))
+ gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(len(stack.data[stack.len()-2].Bytes()))), params.ExpByteGas))
case SSTORE:
err := stack.require(2)
if err != nil {
@@ -777,19 +778,19 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
val := statedb.GetState(context.Address(), common.BigToHash(x))
if len(val) == 0 && len(y.Bytes()) > 0 {
// 0 => non 0
- g = GasStorageAdd
+ g = params.SstoreSetGas
} else if len(val) > 0 && len(y.Bytes()) == 0 {
- statedb.Refund(self.env.Origin(), RefundStorage)
+ statedb.Refund(self.env.Origin(), params.SstoreRefundGas)
- g = GasStorageMod
+ g = params.SstoreClearGas
} else {
// non 0 => non 0 (or 0 => 0)
- g = GasStorageMod
+ g = params.SstoreClearGas
}
gas.Set(g)
case SUICIDE:
if !statedb.IsDeleted(context.Address()) {
- statedb.Refund(self.env.Origin(), RefundSuicide)
+ statedb.Refund(self.env.Origin(), params.SuicideRefundGas)
}
case MLOAD:
newMemSize = calcMemSize(stack.peek(), u256(32))
@@ -803,22 +804,22 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-2])
words := toWordSize(stack.data[stack.len()-2])
- gas.Add(gas, words.Mul(words, GasSha3Word))
+ gas.Add(gas, words.Mul(words, params.Sha3WordGas))
case CALLDATACOPY:
newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-3])
words := toWordSize(stack.data[stack.len()-3])
- gas.Add(gas, words.Mul(words, GasCopyWord))
+ gas.Add(gas, words.Mul(words, params.CopyGas))
case CODECOPY:
newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-3])
words := toWordSize(stack.data[stack.len()-3])
- gas.Add(gas, words.Mul(words, GasCopyWord))
+ gas.Add(gas, words.Mul(words, params.CopyGas))
case EXTCODECOPY:
newMemSize = calcMemSize(stack.data[stack.len()-2], stack.data[stack.len()-4])
words := toWordSize(stack.data[stack.len()-4])
- gas.Add(gas, words.Mul(words, GasCopyWord))
+ gas.Add(gas, words.Mul(words, params.CopyGas))
case CREATE:
newMemSize = calcMemSize(stack.data[stack.len()-2], stack.data[stack.len()-3])
@@ -827,12 +828,12 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
if op == CALL {
if self.env.State().GetStateObject(common.BigToAddress(stack.data[stack.len()-2])) == nil {
- gas.Add(gas, GasCallNewAccount)
+ gas.Add(gas, params.CallNewAccountGas)
}
}
if len(stack.data[stack.len()-3].Bytes()) > 0 {
- gas.Add(gas, GasCallValueTransfer)
+ gas.Add(gas, params.CallValueTransferGas)
}
x := calcMemSize(stack.data[stack.len()-6], stack.data[stack.len()-7])
@@ -848,13 +849,13 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
oldSize := toWordSize(big.NewInt(int64(mem.Len())))
pow := new(big.Int).Exp(oldSize, common.Big2, Zero)
- linCoef := new(big.Int).Mul(oldSize, GasMemWord)
- quadCoef := new(big.Int).Div(pow, GasQuadCoeffDenom)
+ linCoef := new(big.Int).Mul(oldSize, params.MemoryGas)
+ quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv)
oldTotalFee := new(big.Int).Add(linCoef, quadCoef)
pow.Exp(newMemSizeWords, common.Big2, Zero)
- linCoef = new(big.Int).Mul(newMemSizeWords, GasMemWord)
- quadCoef = new(big.Int).Div(pow, GasQuadCoeffDenom)
+ linCoef = new(big.Int).Mul(newMemSizeWords, params.MemoryGas)
+ quadCoef = new(big.Int).Div(pow, params.QuadCoeffDiv)
newTotalFee := new(big.Int).Add(linCoef, quadCoef)
fee := new(big.Int).Sub(newTotalFee, oldTotalFee)
diff --git a/core/vm/vm_jit.go b/core/vm/vm_jit.go
index 2b88d8620..991ade318 100644
--- a/core/vm/vm_jit.go
+++ b/core/vm/vm_jit.go
@@ -18,8 +18,8 @@ import (
"bytes"
"errors"
"fmt"
- "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/crypto"
"math/big"
"unsafe"
)
@@ -330,7 +330,7 @@ func env_create(_vm unsafe.Pointer, _gas *int64, _value unsafe.Pointer, initData
ret, suberr, ref := vm.env.Create(vm.me, nil, initData, gas, vm.price, value)
if suberr == nil {
dataGas := big.NewInt(int64(len(ret))) // TODO: Nto the best design. env.Create can do it, it has the reference to gas counter
- dataGas.Mul(dataGas, GasCreateByte)
+ dataGas.Mul(dataGas, params.CreateDataGas)
gas.Sub(gas, dataGas)
*result = hash2llvm(ref.Address())
}