Compare commits
9 Commits
v1.1.0
...
release/1.
Author | SHA1 | Date | |
---|---|---|---|
adf2b8d01d | |||
4bf0cbcdd3 | |||
33099eaeb6 | |||
59d25c0c2c | |||
cde06b4009 | |||
587669215b | |||
fc3941d1fd | |||
8f09242d7f | |||
042dcf1b28 |
@ -48,10 +48,10 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
ClientIdentifier = "Geth"
|
ClientIdentifier = "Geth"
|
||||||
Version = "1.1.0"
|
Version = "1.1.3"
|
||||||
VersionMajor = 1
|
VersionMajor = 1
|
||||||
VersionMinor = 1
|
VersionMinor = 1
|
||||||
VersionPatch = 0
|
VersionPatch = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -56,6 +56,18 @@ type BlockProcessor struct {
|
|||||||
eventMux *event.TypeMux
|
eventMux *event.TypeMux
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: type GasPool big.Int
|
||||||
|
//
|
||||||
|
// GasPool is implemented by state.StateObject. This is a historical
|
||||||
|
// coincidence. Gas tracking should move out of StateObject.
|
||||||
|
|
||||||
|
// GasPool tracks the amount of gas available during
|
||||||
|
// execution of the transactions in a block.
|
||||||
|
type GasPool interface {
|
||||||
|
AddGas(gas, price *big.Int)
|
||||||
|
SubGas(gas, price *big.Int) error
|
||||||
|
}
|
||||||
|
|
||||||
func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
|
func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
|
||||||
sm := &BlockProcessor{
|
sm := &BlockProcessor{
|
||||||
chainDb: db,
|
chainDb: db,
|
||||||
@ -64,16 +76,15 @@ func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManag
|
|||||||
bc: chainManager,
|
bc: chainManager,
|
||||||
eventMux: eventMux,
|
eventMux: eventMux,
|
||||||
}
|
}
|
||||||
|
|
||||||
return sm
|
return sm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block, transientProcess bool) (receipts types.Receipts, err error) {
|
func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block, transientProcess bool) (receipts types.Receipts, err error) {
|
||||||
coinbase := statedb.GetOrNewStateObject(block.Coinbase())
|
gp := statedb.GetOrNewStateObject(block.Coinbase())
|
||||||
coinbase.SetGasLimit(block.GasLimit())
|
gp.SetGasLimit(block.GasLimit())
|
||||||
|
|
||||||
// Process the transactions on to parent state
|
// Process the transactions on to parent state
|
||||||
receipts, err = sm.ApplyTransactions(coinbase, statedb, block, block.Transactions(), transientProcess)
|
receipts, err = sm.ApplyTransactions(gp, statedb, block, block.Transactions(), transientProcess)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -81,9 +92,8 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block
|
|||||||
return receipts, nil
|
return receipts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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) {
|
||||||
cb := statedb.GetStateObject(coinbase.Address())
|
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, gp)
|
||||||
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@ -118,7 +128,7 @@ func (self *BlockProcessor) ChainManager() *ChainManager {
|
|||||||
return self.bc
|
return self.bc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
|
func (self *BlockProcessor) ApplyTransactions(gp GasPool, statedb *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, error) {
|
||||||
var (
|
var (
|
||||||
receipts types.Receipts
|
receipts types.Receipts
|
||||||
totalUsedGas = big.NewInt(0)
|
totalUsedGas = big.NewInt(0)
|
||||||
@ -130,7 +140,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
|
|||||||
for i, tx := range txs {
|
for i, tx := range txs {
|
||||||
statedb.StartRecord(tx.Hash(), block.Hash(), i)
|
statedb.StartRecord(tx.Hash(), block.Hash(), i)
|
||||||
|
|
||||||
receipt, txGas, err := self.ApplyTransaction(coinbase, statedb, header, tx, totalUsedGas, transientProcess)
|
receipt, txGas, err := self.ApplyTransaction(gp, statedb, header, tx, totalUsedGas, transientProcess)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -263,6 +263,7 @@ func (self *StateObject) Copy() *StateObject {
|
|||||||
stateObject.gasPool.Set(self.gasPool)
|
stateObject.gasPool.Set(self.gasPool)
|
||||||
stateObject.remove = self.remove
|
stateObject.remove = self.remove
|
||||||
stateObject.dirty = self.dirty
|
stateObject.dirty = self.dirty
|
||||||
|
stateObject.deleted = self.deleted
|
||||||
|
|
||||||
return stateObject
|
return stateObject
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -117,3 +118,106 @@ func (s *StateSuite) TestSnapshot(c *checker.C) {
|
|||||||
|
|
||||||
c.Assert(data1, checker.DeepEquals, res)
|
c.Assert(data1, checker.DeepEquals, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use testing instead of checker because checker does not support
|
||||||
|
// printing/logging in tests (-check.vv does not work)
|
||||||
|
func TestSnapshot2(t *testing.T) {
|
||||||
|
db, _ := ethdb.NewMemDatabase()
|
||||||
|
state := New(common.Hash{}, db)
|
||||||
|
|
||||||
|
stateobjaddr0 := toAddr([]byte("so0"))
|
||||||
|
stateobjaddr1 := toAddr([]byte("so1"))
|
||||||
|
var storageaddr common.Hash
|
||||||
|
|
||||||
|
data0 := common.BytesToHash([]byte{17})
|
||||||
|
data1 := common.BytesToHash([]byte{18})
|
||||||
|
|
||||||
|
state.SetState(stateobjaddr0, storageaddr, data0)
|
||||||
|
state.SetState(stateobjaddr1, storageaddr, data1)
|
||||||
|
|
||||||
|
// db, trie are already non-empty values
|
||||||
|
so0 := state.GetStateObject(stateobjaddr0)
|
||||||
|
so0.balance = big.NewInt(42)
|
||||||
|
so0.nonce = 43
|
||||||
|
so0.gasPool = big.NewInt(44)
|
||||||
|
so0.code = []byte{'c', 'a', 'f', 'e'}
|
||||||
|
so0.codeHash = so0.CodeHash()
|
||||||
|
so0.remove = true
|
||||||
|
so0.deleted = false
|
||||||
|
so0.dirty = false
|
||||||
|
state.SetStateObject(so0)
|
||||||
|
|
||||||
|
// and one with deleted == true
|
||||||
|
so1 := state.GetStateObject(stateobjaddr1)
|
||||||
|
so1.balance = big.NewInt(52)
|
||||||
|
so1.nonce = 53
|
||||||
|
so1.gasPool = big.NewInt(54)
|
||||||
|
so1.code = []byte{'c', 'a', 'f', 'e', '2'}
|
||||||
|
so1.codeHash = so1.CodeHash()
|
||||||
|
so1.remove = true
|
||||||
|
so1.deleted = true
|
||||||
|
so1.dirty = true
|
||||||
|
state.SetStateObject(so1)
|
||||||
|
|
||||||
|
so1 = state.GetStateObject(stateobjaddr1)
|
||||||
|
if so1 != nil {
|
||||||
|
t.Fatalf("deleted object not nil when getting")
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot := state.Copy()
|
||||||
|
state.Set(snapshot)
|
||||||
|
|
||||||
|
so0Restored := state.GetStateObject(stateobjaddr0)
|
||||||
|
so1Restored := state.GetStateObject(stateobjaddr1)
|
||||||
|
// non-deleted is equal (restored)
|
||||||
|
compareStateObjects(so0Restored, so0, t)
|
||||||
|
// deleted should be nil, both before and after restore of state copy
|
||||||
|
if so1Restored != nil {
|
||||||
|
t.Fatalf("deleted object not nil after restoring snapshot")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareStateObjects(so0, so1 *StateObject, t *testing.T) {
|
||||||
|
if so0.address != so1.address {
|
||||||
|
t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address)
|
||||||
|
}
|
||||||
|
if so0.balance.Cmp(so1.balance) != 0 {
|
||||||
|
t.Fatalf("Balance mismatch: have %v, want %v", so0.balance, so1.balance)
|
||||||
|
}
|
||||||
|
if so0.nonce != so1.nonce {
|
||||||
|
t.Fatalf("Nonce mismatch: have %v, want %v", so0.nonce, so1.nonce)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(so0.codeHash, so1.codeHash) {
|
||||||
|
t.Fatalf("CodeHash mismatch: have %v, want %v", so0.codeHash, so1.codeHash)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(so0.code, so1.code) {
|
||||||
|
t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(so0.initCode, so1.initCode) {
|
||||||
|
t.Fatalf("InitCode mismatch: have %v, want %v", so0.initCode, so1.initCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range so1.storage {
|
||||||
|
if so0.storage[k] != v {
|
||||||
|
t.Fatalf("Storage key %s mismatch: have %v, want %v", k, so0.storage[k], v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k, v := range so0.storage {
|
||||||
|
if so1.storage[k] != v {
|
||||||
|
t.Fatalf("Storage key %s mismatch: have %v, want none.", k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if so0.gasPool.Cmp(so1.gasPool) != 0 {
|
||||||
|
t.Fatalf("GasPool mismatch: have %v, want %v", so0.gasPool, so1.gasPool)
|
||||||
|
}
|
||||||
|
if so0.remove != so1.remove {
|
||||||
|
t.Fatalf("Remove mismatch: have %v, want %v", so0.remove, so1.remove)
|
||||||
|
}
|
||||||
|
if so0.deleted != so1.deleted {
|
||||||
|
t.Fatalf("Deleted mismatch: have %v, want %v", so0.deleted, so1.deleted)
|
||||||
|
}
|
||||||
|
if so0.dirty != so1.dirty {
|
||||||
|
t.Fatalf("Dirty mismatch: have %v, want %v", so0.dirty, so1.dirty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -45,7 +45,7 @@ import (
|
|||||||
* 6) Derive new state root
|
* 6) Derive new state root
|
||||||
*/
|
*/
|
||||||
type StateTransition struct {
|
type StateTransition struct {
|
||||||
coinbase common.Address
|
gp GasPool
|
||||||
msg Message
|
msg Message
|
||||||
gas, gasPrice *big.Int
|
gas, gasPrice *big.Int
|
||||||
initialGas *big.Int
|
initialGas *big.Int
|
||||||
@ -53,8 +53,6 @@ type StateTransition struct {
|
|||||||
data []byte
|
data []byte
|
||||||
state *state.StateDB
|
state *state.StateDB
|
||||||
|
|
||||||
cb, rec, sen *state.StateObject
|
|
||||||
|
|
||||||
env vm.Environment
|
env vm.Environment
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,13 +94,13 @@ func IntrinsicGas(data []byte) *big.Int {
|
|||||||
return igas
|
return igas
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApplyMessage(env vm.Environment, msg Message, coinbase *state.StateObject) ([]byte, *big.Int, error) {
|
func ApplyMessage(env vm.Environment, msg Message, gp GasPool) ([]byte, *big.Int, error) {
|
||||||
return NewStateTransition(env, msg, coinbase).transitionState()
|
return NewStateTransition(env, msg, gp).transitionState()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateObject) *StateTransition {
|
func NewStateTransition(env vm.Environment, msg Message, gp GasPool) *StateTransition {
|
||||||
return &StateTransition{
|
return &StateTransition{
|
||||||
coinbase: coinbase.Address(),
|
gp: gp,
|
||||||
env: env,
|
env: env,
|
||||||
msg: msg,
|
msg: msg,
|
||||||
gas: new(big.Int),
|
gas: new(big.Int),
|
||||||
@ -111,13 +109,9 @@ func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateOb
|
|||||||
value: msg.Value(),
|
value: msg.Value(),
|
||||||
data: msg.Data(),
|
data: msg.Data(),
|
||||||
state: env.State(),
|
state: env.State(),
|
||||||
cb: coinbase,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateTransition) Coinbase() *state.StateObject {
|
|
||||||
return self.state.GetOrNewStateObject(self.coinbase)
|
|
||||||
}
|
|
||||||
func (self *StateTransition) From() (*state.StateObject, error) {
|
func (self *StateTransition) From() (*state.StateObject, error) {
|
||||||
f, err := self.msg.From()
|
f, err := self.msg.From()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -160,7 +154,7 @@ func (self *StateTransition) BuyGas() error {
|
|||||||
if sender.Balance().Cmp(mgval) < 0 {
|
if sender.Balance().Cmp(mgval) < 0 {
|
||||||
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], mgval, sender.Balance())
|
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], mgval, sender.Balance())
|
||||||
}
|
}
|
||||||
if err = self.Coinbase().SubGas(mgas, self.gasPrice); err != nil {
|
if err = self.gp.SubGas(mgas, self.gasPrice); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
self.AddGas(mgas)
|
self.AddGas(mgas)
|
||||||
@ -241,13 +235,12 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.refundGas()
|
self.refundGas()
|
||||||
self.state.AddBalance(self.coinbase, new(big.Int).Mul(self.gasUsed(), self.gasPrice))
|
self.state.AddBalance(self.env.Coinbase(), new(big.Int).Mul(self.gasUsed(), self.gasPrice))
|
||||||
|
|
||||||
return ret, self.gasUsed(), err
|
return ret, self.gasUsed(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateTransition) refundGas() {
|
func (self *StateTransition) refundGas() {
|
||||||
coinbase := self.Coinbase()
|
|
||||||
sender, _ := self.From() // err already checked
|
sender, _ := self.From() // err already checked
|
||||||
// Return remaining gas
|
// Return remaining gas
|
||||||
remaining := new(big.Int).Mul(self.gas, self.gasPrice)
|
remaining := new(big.Int).Mul(self.gas, self.gasPrice)
|
||||||
@ -258,7 +251,7 @@ func (self *StateTransition) refundGas() {
|
|||||||
self.gas.Add(self.gas, refund)
|
self.gas.Add(self.gas, refund)
|
||||||
self.state.AddBalance(sender.Address(), refund.Mul(refund, self.gasPrice))
|
self.state.AddBalance(sender.Address(), refund.Mul(refund, self.gasPrice))
|
||||||
|
|
||||||
coinbase.AddGas(self.gas, self.gasPrice)
|
self.gp.AddGas(self.gas, self.gasPrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateTransition) gasUsed() *big.Int {
|
func (self *StateTransition) gasUsed() *big.Int {
|
||||||
|
@ -31,7 +31,9 @@ type Environment interface {
|
|||||||
|
|
||||||
Origin() common.Address
|
Origin() common.Address
|
||||||
BlockNumber() *big.Int
|
BlockNumber() *big.Int
|
||||||
GetHash(n uint64) common.Hash
|
// The n'th hash ago from this block number
|
||||||
|
GetHash(uint64) common.Hash
|
||||||
|
// The handler's address
|
||||||
Coinbase() common.Address
|
Coinbase() common.Address
|
||||||
Time() *big.Int
|
Time() *big.Int
|
||||||
Difficulty() *big.Int
|
Difficulty() *big.Int
|
||||||
|
@ -59,8 +59,10 @@ func (self *VMEnv) SetDepth(i int) { self.depth = i }
|
|||||||
func (self *VMEnv) VmType() vm.Type { return self.typ }
|
func (self *VMEnv) VmType() vm.Type { return self.typ }
|
||||||
func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
|
func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
|
||||||
func (self *VMEnv) GetHash(n uint64) common.Hash {
|
func (self *VMEnv) GetHash(n uint64) common.Hash {
|
||||||
if block := self.chain.GetBlockByNumber(n); block != nil {
|
for block := self.chain.GetBlock(self.header.ParentHash); block != nil; block = self.chain.GetBlock(block.ParentHash()) {
|
||||||
return block.Hash()
|
if block.NumberU64() == n {
|
||||||
|
return block.Hash()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return common.Hash{}
|
return common.Hash{}
|
||||||
|
@ -434,7 +434,7 @@ func (self *worker) commitNewWork() {
|
|||||||
tstart := time.Now()
|
tstart := time.Now()
|
||||||
parent := self.chain.CurrentBlock()
|
parent := self.chain.CurrentBlock()
|
||||||
tstamp := tstart.Unix()
|
tstamp := tstart.Unix()
|
||||||
if parent.Time().Cmp(new(big.Int).SetInt64(tstamp)) != 1 {
|
if parent.Time().Cmp(new(big.Int).SetInt64(tstamp)) >= 0 {
|
||||||
tstamp = parent.Time().Int64() + 1
|
tstamp = parent.Time().Int64() + 1
|
||||||
}
|
}
|
||||||
// this will ensure we're not going off too far in the future
|
// this will ensure we're not going off too far in the future
|
||||||
|
@ -130,8 +130,8 @@ type Env struct {
|
|||||||
initial bool
|
initial bool
|
||||||
Gas *big.Int
|
Gas *big.Int
|
||||||
|
|
||||||
origin common.Address
|
origin common.Address
|
||||||
//parent common.Hash
|
parent common.Hash
|
||||||
coinbase common.Address
|
coinbase common.Address
|
||||||
|
|
||||||
number *big.Int
|
number *big.Int
|
||||||
@ -162,7 +162,7 @@ func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues
|
|||||||
env := NewEnv(state)
|
env := NewEnv(state)
|
||||||
|
|
||||||
env.origin = common.HexToAddress(exeValues["caller"])
|
env.origin = common.HexToAddress(exeValues["caller"])
|
||||||
//env.parent = common.Hex2Bytes(envValues["previousHash"])
|
env.parent = common.HexToHash(envValues["previousHash"])
|
||||||
env.coinbase = common.HexToAddress(envValues["currentCoinbase"])
|
env.coinbase = common.HexToAddress(envValues["currentCoinbase"])
|
||||||
env.number = common.Big(envValues["currentNumber"])
|
env.number = common.Big(envValues["currentNumber"])
|
||||||
env.time = common.Big(envValues["currentTimestamp"])
|
env.time = common.Big(envValues["currentTimestamp"])
|
||||||
@ -173,10 +173,8 @@ func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues
|
|||||||
return env
|
return env
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Env) Origin() common.Address { return self.origin }
|
func (self *Env) Origin() common.Address { return self.origin }
|
||||||
func (self *Env) BlockNumber() *big.Int { return self.number }
|
func (self *Env) BlockNumber() *big.Int { return self.number }
|
||||||
|
|
||||||
//func (self *Env) PrevHash() []byte { return self.parent }
|
|
||||||
func (self *Env) Coinbase() common.Address { return self.coinbase }
|
func (self *Env) Coinbase() common.Address { return self.coinbase }
|
||||||
func (self *Env) Time() *big.Int { return self.time }
|
func (self *Env) Time() *big.Int { return self.time }
|
||||||
func (self *Env) Difficulty() *big.Int { return self.difficulty }
|
func (self *Env) Difficulty() *big.Int { return self.difficulty }
|
||||||
|
Reference in New Issue
Block a user