Skip to content

Commit 27a5622

Browse files
authored
Merge pull request #15028 from karalabe/metropolis-iceage
consensus, core, tests: implement Metropolis EIP 649
2 parents 8596fc5 + b872961 commit 27a5622

8 files changed

Lines changed: 50 additions & 129 deletions

File tree

‎consensus/ethash/consensus.go‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ import (
3636

3737
// Ethash proof-of-work protocol constants.
3838
var (
39-
blockReward *big.Int = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
40-
maxUncles = 2 // Maximum number of uncles allowed in a single block
39+
frontierBlockReward *big.Int = big.NewInt(5e+18) // Block reward in wei for successfully mining a block
40+
metropolisBlockReward *big.Int = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Metropolis
41+
maxUncles = 2 // Maximum number of uncles allowed in a single block
4142
)
4243

4344
// Various error messages to mark blocks invalid. These should be private to
@@ -306,6 +307,7 @@ var (
306307
big9 = big.NewInt(9)
307308
big10 = big.NewInt(10)
308309
bigMinus99 = big.NewInt(-99)
310+
big2999999 = big.NewInt(2999999)
309311
)
310312

311313
// calcDifficultyMetropolis is the difficulty adjustment algorithm. It returns
@@ -346,8 +348,15 @@ func calcDifficultyMetropolis(time uint64, parent *types.Header) *big.Int {
346348
if x.Cmp(params.MinimumDifficulty) < 0 {
347349
x.Set(params.MinimumDifficulty)
348350
}
351+
// calculate a fake block numer for the ice-age delay:
352+
// https://github.com/ethereum/EIPs/pull/669
353+
// fake_block_number = min(0, block.number - 3_000_000
354+
fakeBlockNumber := new(big.Int)
355+
if parent.Number.Cmp(big2999999) >= 0 {
356+
fakeBlockNumber = fakeBlockNumber.Sub(parent.Number, big2999999) // Note, parent is 1 less than the actual block number
357+
}
349358
// for the exponential factor
350-
periodCount := new(big.Int).Add(parent.Number, big1)
359+
periodCount := fakeBlockNumber
351360
periodCount.Div(periodCount, expDiffPeriod)
352361

353362
// the exponential factor, commonly referred to as "the bomb"
@@ -501,7 +510,7 @@ func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header)
501510
// setting the final state and assembling the block.
502511
func (ethash *Ethash) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
503512
// Accumulate any block and uncle rewards and commit the final state root
504-
AccumulateRewards(state, header, uncles)
513+
AccumulateRewards(chain.Config(), state, header, uncles)
505514
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
506515

507516
// Header seems complete, assemble into a block and return
@@ -518,7 +527,13 @@ var (
518527
// reward. The total reward consists of the static block reward and rewards for
519528
// included uncles. The coinbase of each uncle block is also rewarded.
520529
// TODO (karalabe): Move the chain maker into this package and make this private!
521-
func AccumulateRewards(state *state.StateDB, header *types.Header, uncles []*types.Header) {
530+
func AccumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
531+
// Select the correct block reward based on chain progression
532+
blockReward := frontierBlockReward
533+
if config.IsMetropolis(header.Number) {
534+
blockReward = metropolisBlockReward
535+
}
536+
// Accumulate the rewards for the miner and any included uncles
522537
reward := new(big.Int).Set(blockReward)
523538
r := new(big.Int)
524539
for _, uncle := range uncles {

‎core/chain_makers.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, db ethdb.Dat
179179
if gen != nil {
180180
gen(i, b)
181181
}
182-
ethash.AccumulateRewards(statedb, h, b.uncles)
182+
ethash.AccumulateRewards(config, statedb, h, b.uncles)
183183
root, err := statedb.CommitTo(db, config.IsEIP158(h.Number))
184184
if err != nil {
185185
panic(fmt.Sprintf("state write error: %v", err))

‎core/vm/logger.go‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,14 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
128128
}
129129

130130
// capture SSTORE opcodes and determine the changed value and store
131-
// it in the local storage container. NOTE: we do not need to do any
132-
// range checks here because that's already handler prior to calling
133-
// this function.
134-
switch op {
135-
case SSTORE:
131+
// it in the local storage container.
132+
if op == SSTORE && stack.len() >= 2 {
136133
var (
137134
value = common.BigToHash(stack.data[stack.len()-2])
138135
address = common.BigToHash(stack.data[stack.len()-1])
139136
)
140137
l.changedValues[contract.Address()][address] = value
141138
}
142-
143139
// copy a snapstot of the current memory state to a new buffer
144140
var mem []byte
145141
if !l.cfg.DisableMemory {

‎tests/gen_stlog.go‎

Lines changed: 0 additions & 61 deletions
This file was deleted.

‎tests/state_test_util.go‎

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package tests
1818

1919
import (
20-
"bytes"
2120
"encoding/hex"
2221
"encoding/json"
2322
"fmt"
2423
"math/big"
25-
"reflect"
2624
"strings"
2725

2826
"github.com/ethereum/go-ethereum/common"
@@ -33,8 +31,10 @@ import (
3331
"github.com/ethereum/go-ethereum/core/types"
3432
"github.com/ethereum/go-ethereum/core/vm"
3533
"github.com/ethereum/go-ethereum/crypto"
34+
"github.com/ethereum/go-ethereum/crypto/sha3"
3635
"github.com/ethereum/go-ethereum/ethdb"
3736
"github.com/ethereum/go-ethereum/params"
37+
"github.com/ethereum/go-ethereum/rlp"
3838
)
3939

4040
// StateTest checks transaction processing without block context.
@@ -63,7 +63,7 @@ type stJSON struct {
6363

6464
type stPostState struct {
6565
Root common.UnprefixedHash `json:"hash"`
66-
Logs *[]stLog `json:"logs"`
66+
Logs common.UnprefixedHash `json:"logs"`
6767
Indexes struct {
6868
Data int `json:"data"`
6969
Gas int `json:"gas"`
@@ -108,21 +108,6 @@ type stTransactionMarshaling struct {
108108
PrivateKey hexutil.Bytes
109109
}
110110

111-
//go:generate gencodec -type stLog -field-override stLogMarshaling -out gen_stlog.go
112-
113-
type stLog struct {
114-
Address common.Address `json:"address"`
115-
Data []byte `json:"data"`
116-
Topics []common.Hash `json:"topics"`
117-
Bloom string `json:"bloom"`
118-
}
119-
120-
type stLogMarshaling struct {
121-
Address common.UnprefixedAddress
122-
Data hexutil.Bytes
123-
Topics []common.UnprefixedHash
124-
}
125-
126111
// Subtests returns all valid subtests of the test.
127112
func (t *StateTest) Subtests() []StateSubtest {
128113
var sub []StateSubtest
@@ -159,10 +144,8 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) error {
159144
if _, _, _, err := core.ApplyMessage(evm, msg, gaspool); err != nil {
160145
statedb.RevertToSnapshot(snapshot)
161146
}
162-
if post.Logs != nil {
163-
if err := checkLogs(statedb.Logs(), *post.Logs); err != nil {
164-
return err
165-
}
147+
if logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) {
148+
return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
166149
}
167150
root, _ := statedb.CommitTo(db, config.IsEIP158(block.Number()))
168151
if root != common.Hash(post.Root) {
@@ -254,28 +237,9 @@ func (tx *stTransaction) toMessage(ps stPostState) (core.Message, error) {
254237
return msg, nil
255238
}
256239

257-
func checkLogs(have []*types.Log, want []stLog) error {
258-
if len(have) != len(want) {
259-
return fmt.Errorf("logs length mismatch: got %d, want %d", len(have), len(want))
260-
}
261-
for i := range have {
262-
if have[i].Address != want[i].Address {
263-
return fmt.Errorf("log address %d: got %x, want %x", i, have[i].Address, want[i].Address)
264-
}
265-
if !bytes.Equal(have[i].Data, want[i].Data) {
266-
return fmt.Errorf("log data %d: got %x, want %x", i, have[i].Data, want[i].Data)
267-
}
268-
if !reflect.DeepEqual(have[i].Topics, want[i].Topics) {
269-
return fmt.Errorf("log topics %d:\ngot %x\nwant %x", i, have[i].Topics, want[i].Topics)
270-
}
271-
genBloom := math.PaddedBigBytes(types.LogsBloom([]*types.Log{have[i]}), 256)
272-
var wantBloom types.Bloom
273-
if err := hexutil.UnmarshalFixedUnprefixedText("Bloom", []byte(want[i].Bloom), wantBloom[:]); err != nil {
274-
return fmt.Errorf("test log %d has invalid bloom: %v", i, err)
275-
}
276-
if !bytes.Equal(genBloom, wantBloom[:]) {
277-
return fmt.Errorf("bloom mismatch")
278-
}
279-
}
280-
return nil
240+
func rlpHash(x interface{}) (h common.Hash) {
241+
hw := sha3.NewKeccak256()
242+
rlp.Encode(hw, x)
243+
hw.Sum(h[:0])
244+
return h
281245
}

‎tests/testdata‎

Submodule testdata updated 3016 files

‎tests/vm_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func TestVM(t *testing.T) {
2626
t.Parallel()
2727
vmt := new(testMatcher)
2828
vmt.fails("^vmSystemOperationsTest.json/createNameRegistrator$", "fails without parallel execution")
29+
30+
vmt.skipLoad(`^vmPerformanceTest.json`) // log format broken
31+
vmt.skipLoad(`^vmInputLimits(Light)?.json`) // log format broken
32+
2933
vmt.skipShortMode("^vmPerformanceTest.json")
3034
vmt.skipShortMode("^vmInputLimits(Light)?.json")
3135

‎tests/vm_test_util.go‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ func (t *VMTest) UnmarshalJSON(data []byte) error {
4444
}
4545

4646
type vmJSON struct {
47-
Env stEnv `json:"env"`
48-
Exec vmExec `json:"exec"`
49-
Logs []stLog `json:"logs"`
50-
GasRemaining *math.HexOrDecimal64 `json:"gas"`
51-
Out hexutil.Bytes `json:"out"`
52-
Pre core.GenesisAlloc `json:"pre"`
53-
Post core.GenesisAlloc `json:"post"`
54-
PostStateRoot common.Hash `json:"postStateRoot"`
47+
Env stEnv `json:"env"`
48+
Exec vmExec `json:"exec"`
49+
Logs common.UnprefixedHash `json:"logs"`
50+
GasRemaining *math.HexOrDecimal64 `json:"gas"`
51+
Out hexutil.Bytes `json:"out"`
52+
Pre core.GenesisAlloc `json:"pre"`
53+
Post core.GenesisAlloc `json:"post"`
54+
PostStateRoot common.Hash `json:"postStateRoot"`
5555
}
5656

5757
//go:generate gencodec -type vmExec -field-override vmExecMarshaling -out gen_vmexec.go
@@ -109,7 +109,10 @@ func (t *VMTest) Run(vmconfig vm.Config) error {
109109
// if root := statedb.IntermediateRoot(false); root != t.json.PostStateRoot {
110110
// return fmt.Errorf("post state root mismatch, got %x, want %x", root, t.json.PostStateRoot)
111111
// }
112-
return checkLogs(statedb.Logs(), t.json.Logs)
112+
if logs := rlpHash(statedb.Logs()); logs != common.Hash(t.json.Logs) {
113+
return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, t.json.Logs)
114+
}
115+
return nil
113116
}
114117

115118
func (t *VMTest) exec(statedb *state.StateDB, vmconfig vm.Config) ([]byte, uint64, error) {

0 commit comments

Comments
 (0)