Skip to content

Commit

Permalink
rebase fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zsfelfoldi committed Oct 8, 2016
1 parent 98b08c3 commit b95846d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
3 changes: 2 additions & 1 deletion light/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"golang.org/x/net/context"
Expand Down Expand Up @@ -156,7 +157,7 @@ func (self *LightState) SetNonce(ctx context.Context, addr common.Address, nonce
func (self *LightState) SetCode(ctx context.Context, addr common.Address, code []byte) error {
stateObject, err := self.GetOrNewStateObject(ctx, addr)
if err == nil && stateObject != nil {
stateObject.SetCode(code)
stateObject.SetCode(crypto.Keccak256Hash(code), code)
}
return err
}
Expand Down
4 changes: 2 additions & 2 deletions light/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ func (self *StateObject) Code() []byte {
}

// SetCode sets the contract code
func (self *StateObject) SetCode(code []byte) {
func (self *StateObject) SetCode(hash common.Hash, code []byte) {
self.code = code
self.codeHash = crypto.Keccak256(code)
self.codeHash = hash[:]
self.dirty = true
}

Expand Down
63 changes: 38 additions & 25 deletions light/vm_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/net/context"
)

Expand All @@ -40,10 +41,7 @@ type VMEnv struct {
msg core.Message
depth int
chain *LightChain
typ vm.Type
// structured logging
logs []vm.StructLog
err error
err error
}

// NewEnv creates a new execution environment based on an ODR capable light state
Expand All @@ -53,7 +51,6 @@ func NewEnv(ctx context.Context, state *LightState, chainConfig *core.ChainConfi
chain: chain,
header: header,
msg: msg,
typ: vm.StdVmTy,
}
env.state = &VMState{ctx: ctx, state: state, env: env}

Expand All @@ -69,12 +66,9 @@ func (self *VMEnv) Coinbase() common.Address { return self.header.Coinbase }
func (self *VMEnv) Time() *big.Int { return self.header.Time }
func (self *VMEnv) Difficulty() *big.Int { return self.header.Difficulty }
func (self *VMEnv) GasLimit() *big.Int { return self.header.GasLimit }
func (self *VMEnv) Value() *big.Int { return self.msg.Value() }
func (self *VMEnv) Db() vm.Database { return self.state }
func (self *VMEnv) Depth() int { return self.depth }
func (self *VMEnv) SetDepth(i int) { self.depth = i }
func (self *VMEnv) VmType() vm.Type { return self.typ }
func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
func (self *VMEnv) GetHash(n uint64) common.Hash {
for header := self.chain.GetHeader(self.header.ParentHash, self.header.Number.Uint64()-1); header != nil; header = self.chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
if header.GetNumberU64() == n {
Expand All @@ -92,12 +86,12 @@ func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
return self.state.GetBalance(from).Cmp(balance) >= 0
}

func (self *VMEnv) MakeSnapshot() vm.Database {
return &VMState{ctx: self.ctx, state: self.state.state.Copy(), env: self}
func (self *VMEnv) SnapshotDatabase() int {
return self.state.SnapshotDatabase()
}

func (self *VMEnv) SetSnapshot(copy vm.Database) {
self.state.state.Set(copy.(*VMState).state)
func (self *VMEnv) RevertToSnapshot(idx int) {
self.state.RevertToSnapshot(idx)
}

func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
Expand All @@ -111,16 +105,12 @@ func (self *VMEnv) CallCode(me vm.ContractRef, addr common.Address, data []byte,
return core.CallCode(self, me, addr, data, gas, price, value)
}

func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
return core.Create(self, me, data, gas, price, value)
func (self *VMEnv) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
return core.DelegateCall(self, me, addr, data, gas, price)
}

func (self *VMEnv) StructLogs() []vm.StructLog {
return self.logs
}

func (self *VMEnv) AddStructLog(log vm.StructLog) {
self.logs = append(self.logs, log)
func (self *VMEnv) Create(me vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
return core.Create(self, me, data, gas, price, value)
}

// Error returns the error (if any) that happened during execution.
Expand All @@ -132,9 +122,10 @@ func (self *VMEnv) Error() error {
// passes it to any state operation that requires it.
type VMState struct {
vm.Database
ctx context.Context
state *LightState
env *VMEnv
ctx context.Context
state *LightState
snapshots []*LightState
env *VMEnv
}

// errHandler handles and stores any state error that happens during execution.
Expand All @@ -144,6 +135,16 @@ func (s *VMState) errHandler(err error) {
}
}

func (self *VMState) SnapshotDatabase() int {
self.snapshots = append(self.snapshots, self.state.Copy())
return len(self.snapshots) - 1
}

func (self *VMState) RevertToSnapshot(idx int) {
self.state.Set(self.snapshots[idx])
self.snapshots = self.snapshots[:idx]
}

// GetAccount returns the account object of the given account or nil if the
// account does not exist
func (s *VMState) GetAccount(addr common.Address) vm.Account {
Expand Down Expand Up @@ -203,6 +204,18 @@ func (s *VMState) GetCode(addr common.Address) []byte {
return res
}

func (s *VMState) GetCodeHash(addr common.Address) common.Hash {
res, err := s.state.GetCode(s.ctx, addr)
s.errHandler(err)
return crypto.Keccak256Hash(res)
}

func (s *VMState) GetCodeSize(addr common.Address) int {
res, err := s.state.GetCode(s.ctx, addr)
s.errHandler(err)
return len(res)
}

// SetCode sets the contract code at the specified account
func (s *VMState) SetCode(addr common.Address, code []byte) {
err := s.state.SetCode(s.ctx, addr, code)
Expand Down Expand Up @@ -234,7 +247,7 @@ func (s *VMState) SetState(addr common.Address, key common.Hash, value common.Ha
}

// Delete marks an account to be removed and clears its balance
func (s *VMState) Delete(addr common.Address) bool {
func (s *VMState) Suicide(addr common.Address) bool {
res, err := s.state.Delete(s.ctx, addr)
s.errHandler(err)
return res
Expand All @@ -249,7 +262,7 @@ func (s *VMState) Exist(addr common.Address) bool {

// IsDeleted returns true if the given account has been marked for deletion
// or false if the account does not exist
func (s *VMState) IsDeleted(addr common.Address) bool {
func (s *VMState) HasSuicided(addr common.Address) bool {
res, err := s.state.IsDeleted(s.ctx, addr)
s.errHandler(err)
return res
Expand Down

0 comments on commit b95846d

Please sign in to comment.