-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_chain.go
157 lines (130 loc) · 4.75 KB
/
run_chain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package mipsevm
import (
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"math/big"
"github.com/lightlink-network/minigeth/common"
"github.com/lightlink-network/minigeth/core/vm"
"github.com/lightlink-network/minigeth/crypto"
)
func DeployChain(interpreter *vm.EVMInterpreter, statedb *StateDB) {
bytecode := GetBytecode(false)
from := common.Address{}
to := common.Address{}
gas := uint64(10000000)
input := make([]byte, 0)
contract := vm.NewContract(vm.AccountRef(from), vm.AccountRef(to), common.Big0, gas)
contract.SetCallCode(&to, crypto.Keccak256Hash(bytecode), bytecode)
ret, err := interpreter.Run(contract, input, false)
check(err)
fmt.Println("returned", len(ret))
statedb.Bytecodes[common.HexToAddress("0x1337")] = ret
}
func getTrieNode(str common.Hash, interpreter *vm.EVMInterpreter, statedb *StateDB) []byte {
from := common.Address{}
to := common.HexToAddress("0xBd770416a3345F91E4B34576cb804a576fa48EB1")
gas := uint64(100000000)
input := crypto.Keccak256Hash([]byte("trie(bytes32)")).Bytes()[:4]
input = append(input, str.Bytes()...)
bytecode := statedb.Bytecodes[to]
//fmt.Println("bytecode", len(bytecode))
contract := vm.NewContract(vm.AccountRef(from), vm.AccountRef(to), common.Big0, gas)
contract.SetCallCode(&to, crypto.Keccak256Hash(bytecode), bytecode)
ret, err := interpreter.Run(contract, input, false)
check(err)
//fmt.Println("getTrieNode", str, ret)
return ret[64:]
}
func AddTrieNode(str []byte, interpreter *vm.EVMInterpreter, statedb *StateDB) {
from := common.Address{}
to := common.HexToAddress("0xBd770416a3345F91E4B34576cb804a576fa48EB1")
gas := uint64(100000000)
input := crypto.Keccak256Hash([]byte("AddTrieNode(bytes)")).Bytes()[:4]
// offset
input = append(input, common.BigToHash(big.NewInt(int64(0x20))).Bytes()...)
// length
input = append(input, common.BigToHash(big.NewInt(int64(len(str)))).Bytes()...)
input = append(input, str...)
input = append(input, make([]byte, 0x20-(len(input)%0x20))...)
bytecode := statedb.Bytecodes[to]
//fmt.Println("bytecode", len(bytecode))
contract := vm.NewContract(vm.AccountRef(from), vm.AccountRef(to), common.Big0, gas)
contract.SetCallCode(&to, crypto.Keccak256Hash(bytecode), bytecode)
_, err := interpreter.Run(contract, input, false)
check(err)
}
func RunWithInputAndGas(interpreter *vm.EVMInterpreter, statedb *StateDB, input []byte, gas uint64) ([]byte, uint64, error) {
from := common.Address{}
to := common.HexToAddress("0x1337")
bytecode := statedb.Bytecodes[to]
contract := vm.NewContract(vm.AccountRef(from), vm.AccountRef(to), common.Big0, gas)
contract.SetCallCode(&to, crypto.Keccak256Hash(bytecode), bytecode)
dat, err := interpreter.Run(contract, input, false)
return dat, (gas - contract.Gas), err
}
func ZeroRegisters(ram map[uint32](uint32)) {
for i := uint32(0xC0000000); i < 0xC0000000+36*4; i += 4 {
WriteRam(ram, i, 0)
}
}
func LoadData(dat []byte, ram map[uint32](uint32), base uint32) {
for i := 0; i < len(dat); i += 4 {
value := binary.BigEndian.Uint32(dat[i : i+4])
if value != 0 {
ram[base+uint32(i)] = value
}
}
}
func LoadMappedFile(fn string, ram map[uint32](uint32), base uint32) {
dat, err := ioutil.ReadFile(fn)
check(err)
LoadData(dat, ram, base)
}
func RunFull() {
interpreter, statedb := GetInterpreter(0, true, "")
DeployChain(interpreter, statedb)
ram := make(map[uint32](uint32))
LoadMappedFile("../mipigo/test/test.bin", ram, 0)
totalSteps := 1000
//LoadMappedFile("test/bin/add.bin", ram, 0)
//totalSteps := 12
ZeroRegisters(ram)
ram[0xC000007C] = 0x5EAD0000
root := RamToTrie(ram)
//ParseNode(root, 0)
ioutil.WriteFile("/tmp/cannon/trie.json", TrieToJson(root, -1), 0644)
for k, v := range Preimages {
fmt.Println("AddTrieNode", k)
AddTrieNode(v, interpreter, statedb)
}
fmt.Println("trie is ready, let's run")
fmt.Println("state root", root, "nodes", len(Preimages))
for step := 0; step < totalSteps; step++ {
// it's run o clock
from := common.Address{}
to := common.HexToAddress("0x1337")
bytecode := statedb.Bytecodes[to]
gas := uint64(100000000)
steps := 1
input := crypto.Keccak256Hash([]byte("Steps(bytes32,uint256)")).Bytes()[:4]
input = append(input, root.Bytes()...)
input = append(input, common.BigToHash(big.NewInt(int64(steps))).Bytes()...)
contract := vm.NewContract(vm.AccountRef(from), vm.AccountRef(to), common.Big0, gas)
contract.SetCallCode(&to, crypto.Keccak256Hash(bytecode), bytecode)
dat, err := interpreter.Run(contract, input, false)
if err != nil {
if len(dat) >= 0x24 {
fmt.Println(string(dat[0x24:]))
}
log.Fatal(err)
} else {
root = common.BytesToHash(dat)
fmt.Println("new state root", step, root, "gas used", (gas - contract.Gas))
}
}
/*ParseNode(root, 0, func(t common.Hash) []byte {
return getTrieNode(t, interpreter, statedb)
})*/
}