-
Notifications
You must be signed in to change notification settings - Fork 147
/
main.go
156 lines (125 loc) · 4.09 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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package main
import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"net/http"
"os"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra"
"github.com/iotaledger/wasp/components/app"
"github.com/iotaledger/wasp/packages/evm/jsonrpc"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv/codec"
"github.com/iotaledger/wasp/packages/kv/dict"
"github.com/iotaledger/wasp/packages/metrics"
"github.com/iotaledger/wasp/packages/origin"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/solo"
"github.com/iotaledger/wasp/packages/vm/core/evm/emulator"
"github.com/iotaledger/wasp/tools/wasp-cli/log"
)
type soloContext struct {
cleanup []func()
}
func (s *soloContext) cleanupAll() {
for i := len(s.cleanup) - 1; i >= 0; i-- {
s.cleanup[i]()
}
}
func (s *soloContext) Cleanup(f func()) {
s.cleanup = append(s.cleanup, f)
}
func (*soloContext) Errorf(format string, args ...interface{}) {
log.Printf("error: "+format, args)
}
func (*soloContext) FailNow() {
os.Exit(1)
}
func (s *soloContext) Fatalf(format string, args ...any) {
log.Printf("fatal: "+format, args)
s.FailNow()
}
func (*soloContext) Helper() {
}
func (*soloContext) Logf(format string, args ...any) {
log.Printf(format, args...)
}
func (*soloContext) Name() string {
return "evmemulator"
}
func init() {
parameters.InitL1(parameters.L1ForTesting)
}
var listenAddress string = ":8545"
func main() {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Run: start,
Use: "evmemulator",
Short: "evmemulator runs a JSONRPC server with Solo as backend",
Long: fmt.Sprintf(`evmemulator runs a JSONRPC server with Solo as backend.
evmemulator does the following:
- Starts an ISC chain in a Solo environment
- Initializes 10 ethereum accounts with funds (private keys and addresses printed after init)
- Starts a JSONRPC server at http://localhost:8545 (websocket: ws://localhost:8545/ws)
You can connect any Ethereum tool (eg Metamask) to this JSON-RPC server and use it for testing Ethereum contracts.
Note: chain data is stored in-memory and will be lost upon termination.
`,
),
}
log.Init(cmd)
cmd.PersistentFlags().StringVarP(&listenAddress, "listen", "l", ":8545", "listen address")
err := cmd.Execute()
log.Check(err)
}
func initSolo() (*soloContext, *solo.Chain) {
ctx := &soloContext{}
env := solo.New(ctx, &solo.InitOptions{Debug: log.DebugFlag, PrintStackTrace: log.DebugFlag})
chainOwner, chainOwnerAddr := env.NewKeyPairWithFunds()
chain, _ := env.NewChainExt(chainOwner, 1*isc.Million, "evmemulator", dict.Dict{
origin.ParamChainOwner: isc.NewAgentID(chainOwnerAddr).Bytes(),
origin.ParamEVMChainID: codec.EncodeUint16(1074),
origin.ParamBlockKeepAmount: codec.EncodeInt32(emulator.BlockKeepAll),
origin.ParamWaspVersion: codec.EncodeString(app.Version),
})
return ctx, chain
}
func createAccounts(chain *solo.Chain) (accounts []*ecdsa.PrivateKey) {
log.Printf("creating accounts with funds...\n")
header := []string{"private key", "address"}
var rows [][]string
for i := 0; i < len(solo.EthereumAccounts); i++ {
pk, addr := chain.EthereumAccountByIndexWithL2Funds(i)
accounts = append(accounts, pk)
rows = append(rows, []string{hex.EncodeToString(crypto.FromECDSA(pk)), addr.String()})
}
log.PrintTable(header, rows)
return accounts
}
func start(cmd *cobra.Command, args []string) {
ctx, chain := initSolo()
defer ctx.cleanupAll()
accounts := createAccounts(chain)
jsonRPCServer, err := jsonrpc.NewServer(
chain.EVM(),
jsonrpc.NewAccountManager(accounts),
metrics.NewChainWebAPIMetricsProvider().CreateForChain(chain.ChainID),
jsonrpc.ParametersDefault(),
)
log.Check(err)
mux := http.NewServeMux()
mux.Handle("/ws", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
jsonRPCServer.WebsocketHandler([]string{"*"}).ServeHTTP(w, req)
}))
mux.Handle("/", jsonRPCServer)
s := &http.Server{
Addr: listenAddress,
Handler: mux,
}
log.Printf("starting JSONRPC server on %s...\n", listenAddress)
err = s.ListenAndServe()
log.Check(err)
}