Skip to content

Commit a0e6d24

Browse files
committed
Merge pull request ethereum#1762 from Gustav-Simonsson/temp_v1.0.4
1.0.4
2 parents f0c7af0 + 19b5f3c commit a0e6d24

21 files changed

+406
-68
lines changed

cmd/evm/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ type VMEnv struct {
152152

153153
depth int
154154
Gas *big.Int
155-
time uint64
155+
time *big.Int
156156
logs []vm.StructLog
157157
}
158158

@@ -161,15 +161,15 @@ func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int) *VM
161161
state: state,
162162
transactor: &transactor,
163163
value: value,
164-
time: uint64(time.Now().Unix()),
164+
time: big.NewInt(time.Now().Unix()),
165165
}
166166
}
167167

168168
func (self *VMEnv) State() *state.StateDB { return self.state }
169169
func (self *VMEnv) Origin() common.Address { return *self.transactor }
170170
func (self *VMEnv) BlockNumber() *big.Int { return common.Big0 }
171171
func (self *VMEnv) Coinbase() common.Address { return *self.transactor }
172-
func (self *VMEnv) Time() uint64 { return self.time }
172+
func (self *VMEnv) Time() *big.Int { return self.time }
173173
func (self *VMEnv) Difficulty() *big.Int { return common.Big1 }
174174
func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
175175
func (self *VMEnv) Value() *big.Int { return self.value }

cmd/geth/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import (
5050

5151
const (
5252
ClientIdentifier = "Geth"
53-
Version = "1.0.1"
53+
Version = "1.0.3"
5454
)
5555

5656
var (

core/block_processor.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ type BlockProcessor struct {
5757
eventMux *event.TypeMux
5858
}
5959

60+
// TODO: type GasPool big.Int
61+
//
62+
// GasPool is implemented by state.StateObject. This is a historical
63+
// coincidence. Gas tracking should move out of StateObject.
64+
65+
// GasPool tracks the amount of gas available during
66+
// execution of the transactions in a block.
67+
type GasPool interface {
68+
AddGas(gas, price *big.Int)
69+
SubGas(gas, price *big.Int) error
70+
}
71+
6072
func NewBlockProcessor(db, extra common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
6173
sm := &BlockProcessor{
6274
db: db,
@@ -66,28 +78,24 @@ func NewBlockProcessor(db, extra common.Database, pow pow.PoW, chainManager *Cha
6678
bc: chainManager,
6779
eventMux: eventMux,
6880
}
69-
7081
return sm
7182
}
7283

7384
func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block, transientProcess bool) (receipts types.Receipts, err error) {
74-
coinbase := statedb.GetOrNewStateObject(block.Coinbase())
75-
coinbase.SetGasLimit(block.GasLimit())
85+
gp := statedb.GetOrNewStateObject(block.Coinbase())
86+
gp.SetGasLimit(block.GasLimit())
7687

7788
// Process the transactions on to parent state
78-
receipts, err = sm.ApplyTransactions(coinbase, statedb, block, block.Transactions(), transientProcess)
89+
receipts, err = sm.ApplyTransactions(gp, statedb, block, block.Transactions(), transientProcess)
7990
if err != nil {
8091
return nil, err
8192
}
8293

8394
return receipts, nil
8495
}
8596

86-
func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
87-
// If we are mining this block and validating we want to set the logs back to 0
88-
89-
cb := statedb.GetStateObject(coinbase.Address())
90-
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
97+
func (self *BlockProcessor) ApplyTransaction(gp GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
98+
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, gp)
9199
if err != nil {
92100
return nil, nil, err
93101
}
@@ -122,7 +130,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager {
122130
return self.bc
123131
}
124132

125-
func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
133+
func (self *BlockProcessor) ApplyTransactions(gp GasPool, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
126134
var (
127135
receipts types.Receipts
128136
totalUsedGas = big.NewInt(0)
@@ -134,7 +142,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
134142
for i, tx := range txs {
135143
statedb.StartRecord(tx.Hash(), block.Hash(), i)
136144

137-
receipt, txGas, err := self.ApplyTransaction(coinbase, statedb, header, tx, totalUsedGas, transientProcess)
145+
receipt, txGas, err := self.ApplyTransaction(gp, statedb, header, tx, totalUsedGas, transientProcess)
138146
if err != nil {
139147
return nil, err
140148
}
@@ -207,7 +215,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
207215
txs := block.Transactions()
208216

209217
// Block validation
210-
if err = ValidateHeader(sm.Pow, header, parent, false); err != nil {
218+
if err = ValidateHeader(sm.Pow, header, parent, false, false); err != nil {
211219
return
212220
}
213221

@@ -331,7 +339,7 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
331339
return UncleError("uncle[%d](%x)'s parent is not ancestor (%x)", i, hash[:4], uncle.ParentHash[0:4])
332340
}
333341

334-
if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash], true); err != nil {
342+
if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
335343
return ValidationError(fmt.Sprintf("uncle[%d](%x) header invalid: %v", i, hash[:4], err))
336344
}
337345
}
@@ -374,19 +382,25 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
374382

375383
// See YP section 4.3.4. "Block Header Validity"
376384
// Validates a block. Returns an error if the block is invalid.
377-
func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, checkPow bool) error {
385+
func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, checkPow, uncle bool) error {
378386
if big.NewInt(int64(len(block.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
379387
return fmt.Errorf("Block extra data too long (%d)", len(block.Extra))
380388
}
381389

382-
if block.Time > uint64(time.Now().Unix()) {
383-
return BlockFutureErr
390+
if uncle {
391+
if block.Time.Cmp(common.MaxBig) == 1 {
392+
return BlockTSTooBigErr
393+
}
394+
} else {
395+
if block.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
396+
return BlockFutureErr
397+
}
384398
}
385-
if block.Time <= parent.Time() {
399+
if block.Time.Cmp(parent.Time()) != 1 {
386400
return BlockEqualTSErr
387401
}
388402

389-
expd := CalcDifficulty(block.Time, parent.Time(), parent.Number(), parent.Difficulty())
403+
expd := CalcDifficulty(block.Time.Uint64(), parent.Time().Uint64(), parent.Number(), parent.Difficulty())
390404
if expd.Cmp(block.Difficulty) != 0 {
391405
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
392406
}

core/block_processor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func TestNumber(t *testing.T) {
4848
statedb := state.New(chain.Genesis().Root(), chain.stateDb)
4949
header := makeHeader(chain.Genesis(), statedb)
5050
header.Number = big.NewInt(3)
51-
err := ValidateHeader(pow, header, chain.Genesis(), false)
51+
err := ValidateHeader(pow, header, chain.Genesis(), false, false)
5252
if err != BlockNumberErr {
5353
t.Errorf("expected block number error, got %q", err)
5454
}
5555

5656
header = makeHeader(chain.Genesis(), statedb)
57-
err = ValidateHeader(pow, header, chain.Genesis(), false)
57+
err = ValidateHeader(pow, header, chain.Genesis(), false, false)
5858
if err == BlockNumberErr {
5959
t.Errorf("didn't expect block number error")
6060
}

core/chain_makers.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,21 @@ func GenerateChain(parent *types.Block, db common.Database, n int, gen func(int,
166166
}
167167

168168
func makeHeader(parent *types.Block, state *state.StateDB) *types.Header {
169-
time := parent.Time() + 10 // block time is fixed at 10 seconds
169+
var time *big.Int
170+
if parent.Time() == nil {
171+
time = big.NewInt(10)
172+
} else {
173+
time = new(big.Int).Add(parent.Time(), big.NewInt(10)) // block time is fixed at 10 seconds
174+
}
170175
return &types.Header{
171176
Root: state.Root(),
172177
ParentHash: parent.Hash(),
173178
Coinbase: parent.Coinbase(),
174-
Difficulty: CalcDifficulty(time, parent.Time(), parent.Number(), parent.Difficulty()),
179+
Difficulty: CalcDifficulty(time.Uint64(), new(big.Int).Sub(time, big.NewInt(10)).Uint64(), parent.Number(), parent.Difficulty()),
175180
GasLimit: CalcGasLimit(parent),
176181
GasUsed: new(big.Int),
177182
Number: new(big.Int).Add(parent.Number(), common.Big1),
178-
Time: uint64(time),
183+
Time: time,
179184
}
180185
}
181186

core/chain_manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,8 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
600600
// Allow up to MaxFuture second in the future blocks. If this limit
601601
// is exceeded the chain is discarded and processed at a later time
602602
// if given.
603-
if max := uint64(time.Now().Unix()) + maxTimeFutureBlocks; block.Time() > max {
603+
max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks)
604+
if block.Time().Cmp(max) == 1 {
604605
return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
605606
}
606607

core/error.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525
)
2626

2727
var (
28-
BlockNumberErr = errors.New("block number invalid")
29-
BlockFutureErr = errors.New("block time is in the future")
30-
BlockEqualTSErr = errors.New("block time stamp equal to previous")
28+
BlockNumberErr = errors.New("block number invalid")
29+
BlockFutureErr = errors.New("block time is in the future")
30+
BlockTSTooBigErr = errors.New("block time too big")
31+
BlockEqualTSErr = errors.New("block time stamp equal to previous")
3132
)
3233

3334
// Parent error. In case a parent is unknown this error will be thrown

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*typ
7373
difficulty := common.String2Big(genesis.Difficulty)
7474
block := types.NewBlock(&types.Header{
7575
Nonce: types.EncodeNonce(common.String2Big(genesis.Nonce).Uint64()),
76-
Time: common.String2Big(genesis.Timestamp).Uint64(),
76+
Time: common.String2Big(genesis.Timestamp),
7777
ParentHash: common.HexToHash(genesis.ParentHash),
7878
Extra: common.FromHex(genesis.ExtraData),
7979
GasLimit: common.String2Big(genesis.GasLimit),

core/state_transition.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ import (
4545
* 6) Derive new state root
4646
*/
4747
type StateTransition struct {
48-
coinbase common.Address
48+
gp GasPool
4949
msg Message
5050
gas, gasPrice *big.Int
5151
initialGas *big.Int
5252
value *big.Int
5353
data []byte
5454
state *state.StateDB
5555

56-
cb, rec, sen *state.StateObject
57-
5856
env vm.Environment
5957
}
6058

@@ -96,13 +94,13 @@ func IntrinsicGas(data []byte) *big.Int {
9694
return igas
9795
}
9896

99-
func ApplyMessage(env vm.Environment, msg Message, coinbase *state.StateObject) ([]byte, *big.Int, error) {
100-
return NewStateTransition(env, msg, coinbase).transitionState()
97+
func ApplyMessage(env vm.Environment, msg Message, gp GasPool) ([]byte, *big.Int, error) {
98+
return NewStateTransition(env, msg, gp).transitionState()
10199
}
102100

103-
func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateObject) *StateTransition {
101+
func NewStateTransition(env vm.Environment, msg Message, gp GasPool) *StateTransition {
104102
return &StateTransition{
105-
coinbase: coinbase.Address(),
103+
gp: gp,
106104
env: env,
107105
msg: msg,
108106
gas: new(big.Int),
@@ -111,13 +109,9 @@ func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateOb
111109
value: msg.Value(),
112110
data: msg.Data(),
113111
state: env.State(),
114-
cb: coinbase,
115112
}
116113
}
117114

118-
func (self *StateTransition) Coinbase() *state.StateObject {
119-
return self.state.GetOrNewStateObject(self.coinbase)
120-
}
121115
func (self *StateTransition) From() (*state.StateObject, error) {
122116
f, err := self.msg.From()
123117
if err != nil {
@@ -160,7 +154,7 @@ func (self *StateTransition) BuyGas() error {
160154
if sender.Balance().Cmp(mgval) < 0 {
161155
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], mgval, sender.Balance())
162156
}
163-
if err = self.Coinbase().SubGas(mgas, self.gasPrice); err != nil {
157+
if err = self.gp.SubGas(mgas, self.gasPrice); err != nil {
164158
return err
165159
}
166160
self.AddGas(mgas)
@@ -241,13 +235,12 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
241235
}
242236

243237
self.refundGas()
244-
self.state.AddBalance(self.coinbase, new(big.Int).Mul(self.gasUsed(), self.gasPrice))
238+
self.state.AddBalance(self.env.Coinbase(), new(big.Int).Mul(self.gasUsed(), self.gasPrice))
245239

246240
return ret, self.gasUsed(), err
247241
}
248242

249243
func (self *StateTransition) refundGas() {
250-
coinbase := self.Coinbase()
251244
sender, _ := self.From() // err already checked
252245
// Return remaining gas
253246
remaining := new(big.Int).Mul(self.gas, self.gasPrice)
@@ -258,7 +251,7 @@ func (self *StateTransition) refundGas() {
258251
self.gas.Add(self.gas, refund)
259252
self.state.AddBalance(sender.Address(), refund.Mul(refund, self.gasPrice))
260253

261-
coinbase.AddGas(self.gas, self.gasPrice)
254+
self.gp.AddGas(self.gas, self.gasPrice)
262255
}
263256

264257
func (self *StateTransition) gasUsed() *big.Int {

core/types/block.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Header struct {
6060
Number *big.Int // The block number
6161
GasLimit *big.Int // Gas limit
6262
GasUsed *big.Int // Gas used
63-
Time uint64 // Creation time
63+
Time *big.Int // Creation time
6464
Extra []byte // Extra data
6565
MixDigest common.Hash // for quick difficulty verification
6666
Nonce BlockNonce
@@ -94,7 +94,7 @@ func (h *Header) UnmarshalJSON(data []byte) error {
9494
Coinbase string
9595
Difficulty string
9696
GasLimit string
97-
Time uint64
97+
Time *big.Int
9898
Extra string
9999
}
100100
dec := json.NewDecoder(bytes.NewReader(data))
@@ -210,6 +210,9 @@ func NewBlockWithHeader(header *Header) *Block {
210210

211211
func copyHeader(h *Header) *Header {
212212
cpy := *h
213+
if cpy.Time = new(big.Int); h.Time != nil {
214+
cpy.Time.Set(h.Time)
215+
}
213216
if cpy.Difficulty = new(big.Int); h.Difficulty != nil {
214217
cpy.Difficulty.Set(h.Difficulty)
215218
}
@@ -301,13 +304,13 @@ func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number)
301304
func (b *Block) GasLimit() *big.Int { return new(big.Int).Set(b.header.GasLimit) }
302305
func (b *Block) GasUsed() *big.Int { return new(big.Int).Set(b.header.GasUsed) }
303306
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
307+
func (b *Block) Time() *big.Int { return new(big.Int).Set(b.header.Time) }
304308

305309
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
306310
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }
307311
func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.header.Nonce[:]) }
308312
func (b *Block) Bloom() Bloom { return b.header.Bloom }
309313
func (b *Block) Coinbase() common.Address { return b.header.Coinbase }
310-
func (b *Block) Time() uint64 { return b.header.Time }
311314
func (b *Block) Root() common.Hash { return b.header.Root }
312315
func (b *Block) ParentHash() common.Hash { return b.header.ParentHash }
313316
func (b *Block) TxHash() common.Hash { return b.header.TxHash }

core/types/block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestBlockEncoding(t *testing.T) {
4747
check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
4848
check("Hash", block.Hash(), common.HexToHash("0a5843ac1cb04865017cb35a57b50b07084e5fcee39b5acadade33149f4fff9e"))
4949
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
50-
check("Time", block.Time(), uint64(1426516743))
50+
check("Time", block.Time(), big.NewInt(1426516743))
5151
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
5252

5353
tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), big.NewInt(50000), big.NewInt(10), nil)

core/vm/environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Environment interface {
3333
BlockNumber() *big.Int
3434
GetHash(n uint64) common.Hash
3535
Coinbase() common.Address
36-
Time() uint64
36+
Time() *big.Int
3737
Difficulty() *big.Int
3838
GasLimit() *big.Int
3939
Transfer(from, to Account, amount *big.Int) error

core/vm/vm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
461461
case TIMESTAMP:
462462
time := self.env.Time()
463463

464-
stack.push(new(big.Int).SetUint64(time))
464+
stack.push(new(big.Int).Set(time))
465465

466466
case NUMBER:
467467
number := self.env.BlockNumber()

core/vm_env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, header *type
4949
func (self *VMEnv) Origin() common.Address { f, _ := self.msg.From(); return f }
5050
func (self *VMEnv) BlockNumber() *big.Int { return self.header.Number }
5151
func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
52-
func (self *VMEnv) Time() uint64 { return self.header.Time }
52+
func (self *VMEnv) Time() *big.Int { return self.header.Time }
5353
func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty }
5454
func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit }
5555
func (self *VMEnv) Value() *big.Int { return self.msg.Value() }

0 commit comments

Comments
 (0)