Skip to content

Commit 515d943

Browse files
committed
converted vm
1 parent 8ce6a36 commit 515d943

11 files changed

Lines changed: 88 additions & 72 deletions

File tree

‎core/execution.go‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@ import (
44
"math/big"
55
"time"
66

7+
"github.com/ethereum/go-ethereum/common"
78
"github.com/ethereum/go-ethereum/crypto"
89
"github.com/ethereum/go-ethereum/state"
910
"github.com/ethereum/go-ethereum/vm"
1011
)
1112

1213
type Execution struct {
1314
env vm.Environment
14-
address, input []byte
15+
address *common.Address
16+
input []byte
1517
Gas, price, value *big.Int
1618
}
1719

18-
func NewExecution(env vm.Environment, address, input []byte, gas, gasPrice, value *big.Int) *Execution {
20+
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
1921
return &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
2022
}
2123

22-
func (self *Execution) Addr() []byte {
23-
return self.address
24-
}
25-
26-
func (self *Execution) Call(codeAddr []byte, caller vm.ContextRef) ([]byte, error) {
24+
func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
2725
// Retrieve the executing code
2826
code := self.env.State().GetCode(codeAddr)
2927

30-
return self.exec(code, codeAddr, caller)
28+
return self.exec(&codeAddr, code, caller)
3129
}
3230

33-
func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret []byte, err error) {
31+
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
3432
env := self.env
3533
evm := vm.NewVm(env)
3634
if env.Depth() == vm.MaxCallDepth {
@@ -40,14 +38,15 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret
4038
}
4139

4240
vsnapshot := env.State().Copy()
43-
if len(self.address) == 0 {
41+
if self.address == nil {
4442
// Generate a new address
4543
nonce := env.State().GetNonce(caller.Address())
46-
self.address = crypto.CreateAddress(caller.Address(), nonce)
44+
addr := crypto.CreateAddress(caller.Address(), nonce)
45+
self.address = &addr
4746
env.State().SetNonce(caller.Address(), nonce+1)
4847
}
4948

50-
from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address)
49+
from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(*self.address)
5150
err = env.Transfer(from, to, self.value)
5251
if err != nil {
5352
env.State().Set(vsnapshot)
@@ -73,8 +72,8 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret
7372
}
7473

7574
func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
76-
ret, err = self.exec(self.input, nil, caller)
77-
account = self.env.State().GetStateObject(self.address)
75+
ret, err = self.exec(nil, self.input, caller)
76+
account = self.env.State().GetStateObject(*self.address)
7877

7978
return
8079
}

‎core/filter.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package core
22

33
import (
4-
"bytes"
54
"math"
65

6+
"github.com/ethereum/go-ethereum/common"
77
"github.com/ethereum/go-ethereum/core/types"
88
"github.com/ethereum/go-ethereum/state"
99
)
@@ -16,8 +16,8 @@ type FilterOptions struct {
1616
Earliest int64
1717
Latest int64
1818

19-
Address [][]byte
20-
Topics [][][]byte
19+
Address []common.Address
20+
Topics [][]common.Hash
2121

2222
Skip int
2323
Max int
@@ -29,9 +29,9 @@ type Filter struct {
2929
earliest int64
3030
latest int64
3131
skip int
32-
address [][]byte
32+
address []common.Address
3333
max int
34-
topics [][][]byte
34+
topics [][]common.Hash
3535

3636
BlockCallback func(*types.Block)
3737
PendingCallback func(*types.Block)
@@ -67,11 +67,11 @@ func (self *Filter) SetLatestBlock(latest int64) {
6767
self.latest = latest
6868
}
6969

70-
func (self *Filter) SetAddress(addr [][]byte) {
70+
func (self *Filter) SetAddress(addr []common.Address) {
7171
self.address = addr
7272
}
7373

74-
func (self *Filter) SetTopics(topics [][][]byte) {
74+
func (self *Filter) SetTopics(topics [][]common.Hash) {
7575
self.topics = topics
7676
}
7777

@@ -131,9 +131,9 @@ func (self *Filter) Find() state.Logs {
131131
return logs[skip:]
132132
}
133133

134-
func includes(addresses [][]byte, a []byte) bool {
134+
func includes(addresses []common.Address, a common.Address) bool {
135135
for _, addr := range addresses {
136-
if !bytes.Equal(addr, a) {
136+
if addr != a {
137137
return false
138138
}
139139
}
@@ -151,13 +151,13 @@ Logs:
151151
continue
152152
}
153153

154-
logTopics := make([][]byte, len(self.topics))
154+
logTopics := make([]common.Hash, len(self.topics))
155155
copy(logTopics, log.Topics())
156156

157157
for i, topics := range self.topics {
158158
for _, topic := range topics {
159159
var match bool
160-
if bytes.Equal(log.Topics()[i], topic) {
160+
if log.Topics()[i] == topic {
161161
match = true
162162
}
163163
if !match {
@@ -176,7 +176,7 @@ func (self *Filter) bloomFilter(block *types.Block) bool {
176176
if len(self.address) > 0 {
177177
var included bool
178178
for _, addr := range self.address {
179-
if types.BloomLookup(block.Bloom(), addr) {
179+
if types.BloomLookup(block.Bloom(), addr.Hash()) {
180180
included = true
181181
break
182182
}

‎core/genesis.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"math/big"
77
"os"
88

9+
"github.com/ethereum/go-ethereum/common"
910
"github.com/ethereum/go-ethereum/core/types"
1011
"github.com/ethereum/go-ethereum/crypto"
11-
"github.com/ethereum/go-ethereum/common"
1212
"github.com/ethereum/go-ethereum/state"
1313
)
1414

@@ -26,12 +26,11 @@ var GenesisDiff = big.NewInt(131072)
2626
var GenesisGasLimit = big.NewInt(3141592)
2727

2828
func GenesisBlock(db common.Database) *types.Block {
29-
genesis := types.NewBlock(ZeroHash256, ZeroHash160, nil, GenesisDiff, 42, "")
29+
genesis := types.NewBlock(common.Hash{}, common.Address{}, common.Hash{}, GenesisDiff, 42, "")
3030
genesis.Header().Number = common.Big0
3131
genesis.Header().GasLimit = GenesisGasLimit
3232
genesis.Header().GasUsed = common.Big0
3333
genesis.Header().Time = 0
34-
genesis.Header().MixDigest = make([]byte, 32)
3534

3635
genesis.Td = common.Big0
3736

@@ -49,7 +48,7 @@ func GenesisBlock(db common.Database) *types.Block {
4948
statedb := state.New(genesis.Root(), db)
5049
for addr, account := range accounts {
5150
codedAddr := common.Hex2Bytes(addr)
52-
accountState := statedb.GetAccount(codedAddr)
51+
accountState := statedb.GetAccount(common.BytesToAddress(codedAddr))
5352
accountState.SetBalance(common.Big(account.Balance))
5453
statedb.UpdateStateObject(accountState)
5554
}

‎core/state_transition.go‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var ()
3131
* 6) Derive new state root
3232
*/
3333
type StateTransition struct {
34-
coinbase []byte
34+
coinbase common.Address
3535
msg Message
3636
gas, gasPrice *big.Int
3737
initialGas *big.Int
@@ -119,7 +119,7 @@ func (self *StateTransition) BuyGas() error {
119119

120120
sender := self.From()
121121
if sender.Balance().Cmp(MessageGasValue(self.msg)) < 0 {
122-
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address()[:4], MessageGasValue(self.msg), sender.Balance())
122+
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], MessageGasValue(self.msg), sender.Balance())
123123
}
124124

125125
coinbase := self.Coinbase()
@@ -195,8 +195,9 @@ func (self *StateTransition) transitionState() (ret []byte, usedGas *big.Int, er
195195
vmenv := self.env
196196
var ref vm.ContextRef
197197
if MessageCreatesContract(msg) {
198-
contract := makeContract(msg, self.state)
199-
ret, err, ref = vmenv.Create(sender, contract.Address(), self.msg.Data(), self.gas, self.gasPrice, self.value)
198+
//contract := makeContract(msg, self.state)
199+
//addr := contract.Address()
200+
ret, err, ref = vmenv.Create(sender, self.msg.Data(), self.gas, self.gasPrice, self.value)
200201
if err == nil {
201202
dataGas := big.NewInt(int64(len(ret)))
202203
dataGas.Mul(dataGas, vm.GasCreateByte)
@@ -230,7 +231,7 @@ func (self *StateTransition) refundGas() {
230231
for addr, ref := range self.state.Refunds() {
231232
refund := common.BigMin(uhalf, ref)
232233
self.gas.Add(self.gas, refund)
233-
self.state.AddBalance([]byte(addr), refund.Mul(refund, self.msg.GasPrice()))
234+
self.state.AddBalance(common.StringToAddress(addr), refund.Mul(refund, self.msg.GasPrice()))
234235
}
235236

236237
coinbase.RefundGas(self.gas, self.msg.GasPrice())
@@ -242,10 +243,13 @@ func (self *StateTransition) gasUsed() *big.Int {
242243

243244
// Converts an message in to a state object
244245
func makeContract(msg Message, state *state.StateDB) *state.StateObject {
245-
addr := AddressFromMessage(msg)
246+
/*
247+
addr := AddressFromMessage(msg)
246248
247-
contract := state.GetOrNewStateObject(addr)
248-
contract.SetInitCode(msg.Data())
249+
contract := state.GetOrNewStateObject(addr)
250+
contract.SetInitCode(msg.Data())
249251
250-
return contract
252+
return contract
253+
*/
254+
return nil
251255
}

‎core/types/bloom9.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func bloom9(b []byte) *big.Int {
4747
return r
4848
}
4949

50-
func BloomLookup(bin, topic []byte) bool {
51-
bloom := common.BigD(bin)
52-
cmp := bloom9(crypto.Sha3(topic))
50+
func BloomLookup(bin Bloom, topic common.Hash) bool {
51+
bloom := bin.Big()
52+
cmp := bloom9(crypto.Sha3(topic[:]))
5353

5454
return bloom.And(bloom, cmp).Cmp(cmp) == 0
5555
}

‎core/types/common.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package types
22

3-
import "math/big"
3+
import (
4+
"math/big"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
)
48

59
type BlockProcessor interface {
610
Process(*Block) (*big.Int, error)
@@ -24,3 +28,7 @@ func (b *Bloom) SetBytes(d []byte) {
2428
b[i] = b[i]
2529
}
2630
}
31+
32+
func (b Bloom) Big() *big.Int {
33+
return common.Bytes2Big(b[:])
34+
}

‎core/vm_env.go‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ func NewEnv(state *state.StateDB, chain *ChainManager, msg Message, block *types
2828
}
2929
}
3030

31-
func (self *VMEnv) Origin() common.Address { return self.msg.From() }
32-
func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() }
33-
func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase() }
34-
func (self *VMEnv) Time() int64 { return self.block.Time() }
35-
func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() }
36-
func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() }
37-
func (self *VMEnv) Value() *big.Int { return self.msg.Value() }
38-
func (self *VMEnv) State() *state.StateDB { return self.state }
39-
func (self *VMEnv) Depth() int { return self.depth }
40-
func (self *VMEnv) SetDepth(i int) { self.depth = i }
41-
func (self *VMEnv) VmType() vm.Type { return self.typ }
42-
func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
43-
func (self *VMEnv) GetHash(n uint64) []byte {
31+
func (self *VMEnv) Origin() common.Address { return self.msg.From() }
32+
func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() }
33+
func (self *VMEnv) Coinbase() common.Address { return self.block.Coinbase() }
34+
func (self *VMEnv) Time() int64 { return self.block.Time() }
35+
func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() }
36+
func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() }
37+
func (self *VMEnv) Value() *big.Int { return self.msg.Value() }
38+
func (self *VMEnv) State() *state.StateDB { return self.state }
39+
func (self *VMEnv) Depth() int { return self.depth }
40+
func (self *VMEnv) SetDepth(i int) { self.depth = i }
41+
func (self *VMEnv) VmType() vm.Type { return self.typ }
42+
func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
43+
func (self *VMEnv) GetHash(n uint64) common.Hash {
4444
if block := self.chain.GetBlockByNumber(n); block != nil {
4545
return block.Hash()
4646
}
4747

48-
return nil
48+
return common.Hash{}
4949
}
5050
func (self *VMEnv) AddLog(log state.Log) {
5151
self.state.AddLog(log)
@@ -54,20 +54,21 @@ func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
5454
return vm.Transfer(from, to, amount)
5555
}
5656

57-
func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *Execution {
57+
func (self *VMEnv) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *Execution {
5858
return NewExecution(self, addr, data, gas, price, value)
5959
}
6060

6161
func (self *VMEnv) Call(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
62-
exe := self.vm(addr, data, gas, price, value)
62+
exe := self.vm(&addr, data, gas, price, value)
6363
return exe.Call(addr, me)
6464
}
6565
func (self *VMEnv) CallCode(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
66-
exe := self.vm(me.Address(), data, gas, price, value)
66+
maddr := me.Address()
67+
exe := self.vm(&maddr, data, gas, price, value)
6768
return exe.Call(addr, me)
6869
}
6970

7071
func (self *VMEnv) Create(me vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
71-
exe := self.vm(common.Address{}, data, gas, price, value)
72+
exe := self.vm(nil, data, gas, price, value)
7273
return exe.Create(me)
7374
}

‎crypto/crypto.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/ethereum/go-ethereum/crypto/ecies"
2121
"github.com/ethereum/go-ethereum/crypto/secp256k1"
2222
"github.com/ethereum/go-ethereum/crypto/sha3"
23+
"github.com/ethereum/go-ethereum/rlp"
2324
"golang.org/x/crypto/pbkdf2"
2425
"golang.org/x/crypto/ripemd160"
2526
)
@@ -47,8 +48,10 @@ func Sha3Hash(data ...[]byte) (h common.Hash) {
4748
}
4849

4950
// Creates an ethereum address given the bytes and the nonce
50-
func CreateAddress(b []byte, nonce uint64) []byte {
51-
return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:]
51+
func CreateAddress(b common.Address, nonce uint64) common.Address {
52+
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
53+
return common.BytesToAddress(Sha3(data)[12:])
54+
//return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:]
5255
}
5356

5457
func Sha256(data []byte) []byte {

‎vm/context.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Context struct {
1818
self ContextRef
1919

2020
Code []byte
21-
CodeAddr common.Address
21+
CodeAddr *common.Address
2222

2323
value, Gas, UsedGas, Price *big.Int
2424

@@ -108,7 +108,7 @@ func (self *Context) SetCode(code []byte) {
108108
self.Code = code
109109
}
110110

111-
func (self *Context) SetCallCode(addr common.Address, code []byte) {
111+
func (self *Context) SetCallCode(addr *common.Address, code []byte) {
112112
self.Code = code
113113
self.CodeAddr = addr
114114
}

0 commit comments

Comments
 (0)