-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
252 lines (213 loc) · 8.25 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
)
var chainConfig params.ChainConfig
type AddressUpdateMap struct {
oldAddressToNewAddress map[common.Address]common.Address
newAddressToOldAddress map[common.Address]common.Address
}
type SimplifiedTx struct {
From string
To string
Data string
}
type GethDumpInput struct {
SimplifiedTxs []SimplifiedTx
WalletAddress string
ExecutionManagerAddress string
StateManagerAddress string
CodeHashes map[string]string
}
func (a AddressUpdateMap) getNewAddressIfExists(oldAddress common.Address) (common.Address, bool) {
newAddr, found := a.oldAddressToNewAddress[oldAddress]
return newAddr, found
}
func (a AddressUpdateMap) getNewAddress(oldAddress common.Address) common.Address {
newAddr, _ := a.oldAddressToNewAddress[oldAddress]
return newAddr
}
func (a AddressUpdateMap) associate(oldAddress common.Address, newAddress common.Address) {
fmt.Println("Mapping:", hex.EncodeToString(oldAddress.Bytes()), "to", hex.EncodeToString(newAddress.Bytes()))
a.oldAddressToNewAddress[oldAddress] = newAddress
a.newAddressToOldAddress[newAddress] = oldAddress
}
func (a AddressUpdateMap) associateExisting(oldAddress common.Address, newAddress common.Address) {
fmt.Println("Associating Existing:", hex.EncodeToString(oldAddress.Bytes()), "to", hex.EncodeToString(newAddress.Bytes()))
if _, ok := a.oldAddressToNewAddress[oldAddress]; !ok {
fmt.Println("ERROR! Old address not found")
os.Exit(1)
}
displacedOldAddress := a.newAddressToOldAddress[newAddress]
displacedNewAddress := a.oldAddressToNewAddress[oldAddress]
a.associate(displacedOldAddress, displacedNewAddress)
a.associate(oldAddress, newAddress)
}
func init() {
chainConfig = params.ChainConfig{
ChainID: big.NewInt(1),
HomesteadBlock: new(big.Int),
ByzantiumBlock: new(big.Int),
ConstantinopleBlock: new(big.Int),
DAOForkBlock: new(big.Int),
DAOForkSupport: false,
EIP150Block: new(big.Int),
EIP155Block: new(big.Int),
EIP158Block: new(big.Int),
}
}
var zeroAddress = common.HexToAddress("0000000000000000000000000000000000000000")
var expectedExecutionMgrAddress common.Address
var desiredExecutionMgrAddress = common.HexToAddress("00000000000000000000000000000000dead0000")
var expectedStateMgrAddress common.Address
var desiredStateMgrAddress = common.HexToAddress("00000000000000000000000000000000dead0001")
var startingDeadAddress = common.HexToAddress("00000000000000000000000000000000dead0000")
var l2ToL1MessagePasser = common.HexToAddress("4200000000000000000000000000000000000000")
var l1MessageSender = common.HexToAddress("4200000000000000000000000000000000000001")
var l1CodeHash, l2CodeHash string
const gasLimit = 15000000
func readingGenesisError(err error) {
fmt.Fprintf(os.Stderr, "Error reading genesis initcode: %v\n", err)
os.Exit(1)
}
func main() {
db := state.NewDatabase(rawdb.NewMemoryDatabase())
currentState, _ := state.New(common.Hash{}, db)
// Now get our initcode!
dataStr, err := ioutil.ReadFile("deployment-tx-data.json")
if err != nil {
readingGenesisError(err)
}
var gethDumpInput GethDumpInput
err = json.Unmarshal(dataStr, &gethDumpInput)
deployerAddress := common.HexToAddress(gethDumpInput.WalletAddress)
expectedExecutionMgrAddress = common.HexToAddress(gethDumpInput.ExecutionManagerAddress)
expectedStateMgrAddress = common.HexToAddress(gethDumpInput.StateManagerAddress)
l2CodeHash, _ = gethDumpInput.CodeHashes["l2ToL1MessagePasser"]
l1CodeHash, _ = gethDumpInput.CodeHashes["l1MessageSender"]
// Apply all the transactions to the state
for _, simpleTx := range gethDumpInput.SimplifiedTxs {
txData, err := hexutil.Decode(simpleTx.Data)
if err != nil {
readingGenesisError(err)
}
sender := common.HexToAddress(simpleTx.To)
// Apply to the state
applyMessageToState(currentState, deployerAddress, sender, gasLimit, txData)
}
// Create the dump
theDump := currentState.RawDump(false, false, false)
fmt.Println("Dump root:", theDump.Root)
// Convert the dump to change all addresses to be DEAD
updatedDump := replaceDumpAddresses(theDump)
l2GethStateDumpFilename := "state-dump.hex"
jsonStateDumpFilename := "state-dump.json"
marshaledDump, _ := json.Marshal(updatedDump)
fmt.Println("\nDUMP INCOMING!")
fmt.Println(common.Bytes2Hex(marshaledDump))
fmt.Println("\nDUMP PRINTED! Copy sent to:", l2GethStateDumpFilename)
fmt.Println("JSON string version sent to:", jsonStateDumpFilename)
fmt.Println("To add to L2Geth, copy the dump hex into `ovm_constants.go`")
dumpOutput := []byte(common.Bytes2Hex(marshaledDump))
ioutil.WriteFile(l2GethStateDumpFilename, dumpOutput, 0644)
ioutil.WriteFile(jsonStateDumpFilename, []byte(string(marshaledDump)), 0644)
}
func replaceDumpAddresses(theDump state.Dump) (updatedDump state.Dump) {
// First generate all of the replacement addresses
addressUpdateMap := AddressUpdateMap{
newAddressToOldAddress: make(map[common.Address]common.Address),
oldAddressToNewAddress: make(map[common.Address]common.Address),
}
idx := int64(0)
for oldAddr := range theDump.Accounts {
indexAsBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(indexAsBytes, uint64(idx))
newAddr := common.BytesToAddress(startingDeadAddress.Bytes())
// Replace the bytes in our `newAddr`
for i, theByte := range indexAsBytes[:2] {
newAddr[len(newAddr)-i-1] = theByte
}
addressUpdateMap.associate(oldAddr, newAddr)
idx++
}
// Re-associate the ExecutionMgr and StateMgr addresses to always be dead0000 & dead0001
addressUpdateMap.associateExisting(expectedExecutionMgrAddress, desiredExecutionMgrAddress)
addressUpdateMap.associateExisting(expectedStateMgrAddress, desiredStateMgrAddress)
for address, account := range theDump.Accounts {
switch "0x" + account.CodeHash {
case l1CodeHash:
addressUpdateMap.associateExisting(address, l1MessageSender)
case l2CodeHash:
addressUpdateMap.associateExisting(address, l2ToL1MessagePasser)
}
}
// Next populate an updated dump with all the information we want
updatedDump.Accounts = map[common.Address]state.DumpAccount{}
// Modify the dump to replace addresses with our new addresses
for addr, account := range theDump.Accounts {
updatedDump.Accounts[addressUpdateMap.getNewAddress(addr)] = account
for key, value := range account.Storage {
fmt.Println("Addr", hex.EncodeToString(addr.Bytes()), "Key: ", hex.EncodeToString(key.Bytes()), "Value", value)
if newAddress, found := addressUpdateMap.getNewAddressIfExists(common.HexToAddress(value)); found {
fmt.Println("Replacing", value, "with", hex.EncodeToString(newAddress.Bytes()))
account.Storage[key] = newAddress.String()
}
}
}
return updatedDump
}
func applyMessageToState(currentState *state.StateDB, from common.Address, to common.Address, gasLimit uint64, data []byte) ([]byte, uint64, bool, error) {
header := &types.Header{
Number: big.NewInt(0),
Difficulty: big.NewInt(0),
}
gasPool := core.GasPool(100000000)
// Generate the message
message := types.Message{}
if to == zeroAddress {
// Check if to the zeroAddress, if so, make it nil
message = types.NewMessage(
from,
nil,
currentState.GetNonce(from),
big.NewInt(0),
gasLimit,
big.NewInt(0),
data,
false,
)
} else {
// Otherwise we actually use the `to` field!
message = types.NewMessage(
from,
&to,
currentState.GetNonce(from),
big.NewInt(0),
gasLimit,
big.NewInt(0),
data,
false,
)
}
context := core.NewEVMContext(message, header, nil, &from)
evm := vm.NewEVM(context, currentState, &chainConfig, vm.Config{})
returnValue, gasUsed, failed, err := core.ApplyMessage(evm, message, &gasPool)
fmt.Println("Return val: [HIDDEN]", "Gas used:", gasUsed, "Failed:", failed, "Error:", err)
commitHash, commitErr := currentState.Commit(false)
fmt.Println("Commit hash:", commitHash, "Commit err:", commitErr)
return returnValue, gasUsed, failed, err
}