Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
872be7cf
Unverified
Commit
872be7cf
authored
Jan 31, 2022
by
Conner Fromknecht
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: move and export initialization helper methods
parent
6dcb3b35
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
199 additions
and
139 deletions
+199
-139
batch_submitter.go
go/batch-submitter/batch_submitter.go
+8
-139
crypto.go
go/batch-submitter/crypto.go
+67
-0
common.go
go/batch-submitter/dial/common.go
+9
-0
l1_client.go
go/batch-submitter/dial/l1_client.go
+41
-0
l2_client.go
go/batch-submitter/dial/l2_client.go
+41
-0
run_metrics_server.go
go/batch-submitter/metrics/run_metrics_server.go
+21
-0
sentry_log.go
go/batch-submitter/sentry_log.go
+12
-0
No files found.
go/batch-submitter/batch_submitter.go
View file @
872be7cf
...
...
@@ -2,36 +2,19 @@ package batchsubmitter
import
(
"context"
"crypto/ecdsa"
"crypto/tls"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/ethereum-optimism/optimism/go/batch-submitter/dial"
"github.com/ethereum-optimism/optimism/go/batch-submitter/drivers/proposer"
"github.com/ethereum-optimism/optimism/go/batch-submitter/drivers/sequencer"
"github.com/ethereum-optimism/optimism/go/batch-submitter/metrics"
"github.com/ethereum-optimism/optimism/go/batch-submitter/txmgr"
l2ethclient
"github.com/ethereum-optimism/optimism/l2geth/ethclient"
l2rpc
"github.com/ethereum-optimism/optimism/l2geth/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
"github.com/getsentry/sentry-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli"
)
const
(
// defaultDialTimeout is default duration the service will wait on
// startup to make a connection to either the L1 or L2 backends.
defaultDialTimeout
=
5
*
time
.
Second
)
// Main is the entrypoint into the batch submitter service. This method returns
// a closure that executes the service and blocks until the service exits. The
// use of a closure allows the parameters bound to the top-level main package,
...
...
@@ -63,7 +46,7 @@ func Main(gitVersion string) func(ctx *cli.Context) error {
Dsn
:
cfg
.
SentryDsn
,
Environment
:
cfg
.
EthNetworkName
,
Release
:
"batch-submitter@"
+
gitVersion
,
TracesSampleRate
:
t
raceRateToFloat64
(
cfg
.
SentryTraceRate
),
TracesSampleRate
:
T
raceRateToFloat64
(
cfg
.
SentryTraceRate
),
Debug
:
false
,
})
if
err
!=
nil
{
...
...
@@ -83,7 +66,7 @@ func Main(gitVersion string) func(ctx *cli.Context) error {
log
.
Root
()
.
SetHandler
(
log
.
LvlFilterHandler
(
logLevel
,
logHandler
))
// Parse sequencer private key and CTC contract address.
sequencerPrivKey
,
ctcAddress
,
err
:=
p
arseWalletPrivKeyAndContractAddr
(
sequencerPrivKey
,
ctcAddress
,
err
:=
P
arseWalletPrivKeyAndContractAddr
(
"Sequencer"
,
cfg
.
Mnemonic
,
cfg
.
SequencerHDPath
,
cfg
.
SequencerPrivateKey
,
cfg
.
CTCAddress
,
)
...
...
@@ -92,7 +75,7 @@ func Main(gitVersion string) func(ctx *cli.Context) error {
}
// Parse proposer private key and SCC contract address.
proposerPrivKey
,
sccAddress
,
err
:=
p
arseWalletPrivKeyAndContractAddr
(
proposerPrivKey
,
sccAddress
,
err
:=
P
arseWalletPrivKeyAndContractAddr
(
"Proposer"
,
cfg
.
Mnemonic
,
cfg
.
ProposerHDPath
,
cfg
.
ProposerPrivateKey
,
cfg
.
SCCAddress
,
)
...
...
@@ -102,18 +85,18 @@ func Main(gitVersion string) func(ctx *cli.Context) error {
// Connect to L1 and L2 providers. Perform these last since they are the
// most expensive.
l1Client
,
err
:=
dialL1EthClientWithTimeout
(
ctx
,
cfg
.
L1EthRpc
,
cfg
.
DisableHTTP2
)
l1Client
,
err
:=
dial
.
L1EthClientWithTimeout
(
ctx
,
cfg
.
L1EthRpc
,
cfg
.
DisableHTTP2
)
if
err
!=
nil
{
return
err
}
l2Client
,
err
:=
dialL2EthClientWithTimeout
(
ctx
,
cfg
.
L2EthRpc
,
cfg
.
DisableHTTP2
)
l2Client
,
err
:=
dial
.
L2EthClientWithTimeout
(
ctx
,
cfg
.
L2EthRpc
,
cfg
.
DisableHTTP2
)
if
err
!=
nil
{
return
err
}
if
cfg
.
MetricsServerEnable
{
go
runMetrics
Server
(
cfg
.
MetricsHostname
,
cfg
.
MetricsPort
)
go
metrics
.
Run
Server
(
cfg
.
MetricsHostname
,
cfg
.
MetricsPort
)
}
chainID
,
err
:=
l1Client
.
ChainID
(
ctx
)
...
...
@@ -239,117 +222,3 @@ func (b *BatchSubmitter) Stop() {
_
=
service
.
Stop
()
}
}
// parseWalletPrivKeyAndContractAddr returns the wallet private key to use for
// sending transactions as well as the contract address to send to for a
// particular sub-service.
func
parseWalletPrivKeyAndContractAddr
(
name
string
,
mnemonic
string
,
hdPath
string
,
privKeyStr
string
,
contractAddrStr
string
,
)
(
*
ecdsa
.
PrivateKey
,
common
.
Address
,
error
)
{
// Parse wallet private key from either privkey string or BIP39 mnemonic
// and BIP32 HD derivation path.
privKey
,
err
:=
GetConfiguredPrivateKey
(
mnemonic
,
hdPath
,
privKeyStr
)
if
err
!=
nil
{
return
nil
,
common
.
Address
{},
err
}
// Parse the target contract address the wallet will send to.
contractAddress
,
err
:=
ParseAddress
(
contractAddrStr
)
if
err
!=
nil
{
return
nil
,
common
.
Address
{},
err
}
// Log wallet address rather than private key...
walletAddress
:=
crypto
.
PubkeyToAddress
(
privKey
.
PublicKey
)
log
.
Info
(
name
+
" wallet params parsed successfully"
,
"wallet_address"
,
walletAddress
,
"contract_address"
,
contractAddress
)
return
privKey
,
contractAddress
,
nil
}
// runMetricsServer spins up a prometheus metrics server at the provided
// hostname and port.
//
// NOTE: This method MUST be run as a goroutine.
func
runMetricsServer
(
hostname
string
,
port
uint64
)
{
metricsPortStr
:=
strconv
.
FormatUint
(
port
,
10
)
metricsAddr
:=
fmt
.
Sprintf
(
"%s:%s"
,
hostname
,
metricsPortStr
)
http
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
_
=
http
.
ListenAndServe
(
metricsAddr
,
nil
)
}
// dialL1EthClientWithTimeout attempts to dial the L1 provider using the
// provided URL. If the dial doesn't complete within defaultDialTimeout seconds,
// this method will return an error.
func
dialL1EthClientWithTimeout
(
ctx
context
.
Context
,
url
string
,
disableHTTP2
bool
)
(
*
ethclient
.
Client
,
error
)
{
ctxt
,
cancel
:=
context
.
WithTimeout
(
ctx
,
defaultDialTimeout
)
defer
cancel
()
if
strings
.
HasPrefix
(
url
,
"http"
)
{
httpClient
:=
new
(
http
.
Client
)
if
disableHTTP2
{
log
.
Info
(
"Disabled HTTP/2 support in L1 eth client"
)
httpClient
.
Transport
=
&
http
.
Transport
{
TLSNextProto
:
make
(
map
[
string
]
func
(
authority
string
,
c
*
tls
.
Conn
)
http
.
RoundTripper
),
}
}
rpcClient
,
err
:=
rpc
.
DialHTTPWithClient
(
url
,
httpClient
)
if
err
!=
nil
{
return
nil
,
err
}
return
ethclient
.
NewClient
(
rpcClient
),
nil
}
return
ethclient
.
DialContext
(
ctxt
,
url
)
}
// dialL2EthClientWithTimeout attempts to dial the L2 provider using the
// provided URL. If the dial doesn't complete within defaultDialTimeout seconds,
// this method will return an error.
func
dialL2EthClientWithTimeout
(
ctx
context
.
Context
,
url
string
,
disableHTTP2
bool
)
(
*
l2ethclient
.
Client
,
error
)
{
ctxt
,
cancel
:=
context
.
WithTimeout
(
ctx
,
defaultDialTimeout
)
defer
cancel
()
if
strings
.
HasPrefix
(
url
,
"http"
)
{
httpClient
:=
new
(
http
.
Client
)
if
disableHTTP2
{
log
.
Info
(
"Disabled HTTP/2 support in L2 eth client"
)
httpClient
.
Transport
=
&
http
.
Transport
{
TLSNextProto
:
make
(
map
[
string
]
func
(
authority
string
,
c
*
tls
.
Conn
)
http
.
RoundTripper
),
}
}
rpcClient
,
err
:=
l2rpc
.
DialHTTPWithClient
(
url
,
httpClient
)
if
err
!=
nil
{
return
nil
,
err
}
return
l2ethclient
.
NewClient
(
rpcClient
),
nil
}
return
l2ethclient
.
DialContext
(
ctxt
,
url
)
}
// traceRateToFloat64 converts a time.Duration into a valid float64 for the
// Sentry client. The client only accepts values between 0.0 and 1.0, so this
// method clamps anything greater than 1 second to 1.0.
func
traceRateToFloat64
(
rate
time
.
Duration
)
float64
{
rate64
:=
float64
(
rate
)
/
float64
(
time
.
Second
)
if
rate64
>
1.0
{
rate64
=
1.0
}
return
rate64
}
go/batch-submitter/crypto.go
View file @
872be7cf
...
...
@@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/tyler-smith/go-bip39"
)
...
...
@@ -106,3 +107,69 @@ func ParsePrivateKeyStr(privKeyStr string) (*ecdsa.PrivateKey, error) {
hex
:=
strings
.
TrimPrefix
(
privKeyStr
,
"0x"
)
return
crypto
.
HexToECDSA
(
hex
)
}
// ParseWalletPrivKeyAndContractAddr returns the wallet private key to use for
// sending transactions as well as the contract address to send to for a
// particular sub-service.
func
ParseWalletPrivKeyAndContractAddr
(
name
string
,
mnemonic
string
,
hdPath
string
,
privKeyStr
string
,
contractAddrStr
string
,
)
(
*
ecdsa
.
PrivateKey
,
common
.
Address
,
error
)
{
// Parse wallet private key from either privkey string or BIP39 mnemonic
// and BIP32 HD derivation path.
privKey
,
err
:=
GetConfiguredPrivateKey
(
mnemonic
,
hdPath
,
privKeyStr
)
if
err
!=
nil
{
return
nil
,
common
.
Address
{},
err
}
// Parse the target contract address the wallet will send to.
contractAddress
,
err
:=
ParseAddress
(
contractAddrStr
)
if
err
!=
nil
{
return
nil
,
common
.
Address
{},
err
}
// Log wallet address rather than private key...
walletAddress
:=
crypto
.
PubkeyToAddress
(
privKey
.
PublicKey
)
log
.
Info
(
name
+
" wallet params parsed successfully"
,
"wallet_address"
,
walletAddress
,
"contract_address"
,
contractAddress
)
return
privKey
,
contractAddress
,
nil
}
// parseWalletPrivKeyAndContractAddr returns the wallet private key to use for
// sending transactions as well as the contract address to send to for a
// particular sub-service.
func
parseWalletPrivKeyAndContractAddr
(
name
string
,
mnemonic
string
,
hdPath
string
,
privKeyStr
string
,
contractAddrStr
string
,
)
(
*
ecdsa
.
PrivateKey
,
common
.
Address
,
error
)
{
// Parse wallet private key from either privkey string or BIP39 mnemonic
// and BIP32 HD derivation path.
privKey
,
err
:=
GetConfiguredPrivateKey
(
mnemonic
,
hdPath
,
privKeyStr
)
if
err
!=
nil
{
return
nil
,
common
.
Address
{},
err
}
// Parse the target contract address the wallet will send to.
contractAddress
,
err
:=
ParseAddress
(
contractAddrStr
)
if
err
!=
nil
{
return
nil
,
common
.
Address
{},
err
}
// Log wallet address rather than private key...
walletAddress
:=
crypto
.
PubkeyToAddress
(
privKey
.
PublicKey
)
log
.
Info
(
name
+
" wallet params parsed successfully"
,
"wallet_address"
,
walletAddress
,
"contract_address"
,
contractAddress
)
return
privKey
,
contractAddress
,
nil
}
go/batch-submitter/dial/common.go
0 → 100644
View file @
872be7cf
package
dial
import
"time"
const
(
// defaultDialTimeout is default duration the service will wait on
// startup to make a connection to either the L1 or L2 backends.
defaultDialTimeout
=
5
*
time
.
Second
)
go/batch-submitter/dial/l1_client.go
0 → 100644
View file @
872be7cf
package
dial
import
(
"context"
"crypto/tls"
"net/http"
"strings"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
)
// L1EthClientWithTimeout attempts to dial the L1 provider using the
// provided URL. If the dial doesn't complete within defaultDialTimeout seconds,
// this method will return an error.
func
L1EthClientWithTimeout
(
ctx
context
.
Context
,
url
string
,
disableHTTP2
bool
)
(
*
ethclient
.
Client
,
error
)
{
ctxt
,
cancel
:=
context
.
WithTimeout
(
ctx
,
defaultDialTimeout
)
defer
cancel
()
if
strings
.
HasPrefix
(
url
,
"http"
)
{
httpClient
:=
new
(
http
.
Client
)
if
disableHTTP2
{
log
.
Info
(
"Disabled HTTP/2 support in L1 eth client"
)
httpClient
.
Transport
=
&
http
.
Transport
{
TLSNextProto
:
make
(
map
[
string
]
func
(
authority
string
,
c
*
tls
.
Conn
)
http
.
RoundTripper
),
}
}
rpcClient
,
err
:=
rpc
.
DialHTTPWithClient
(
url
,
httpClient
)
if
err
!=
nil
{
return
nil
,
err
}
return
ethclient
.
NewClient
(
rpcClient
),
nil
}
return
ethclient
.
DialContext
(
ctxt
,
url
)
}
go/batch-submitter/dial/l2_client.go
0 → 100644
View file @
872be7cf
package
dial
import
(
"context"
"crypto/tls"
"net/http"
"strings"
"github.com/ethereum-optimism/optimism/l2geth/ethclient"
"github.com/ethereum-optimism/optimism/l2geth/log"
"github.com/ethereum-optimism/optimism/l2geth/rpc"
)
// L2EthClientWithTimeout attempts to dial the L2 provider using the
// provided URL. If the dial doesn't complete within defaultDialTimeout seconds,
// this method will return an error.
func
L2EthClientWithTimeout
(
ctx
context
.
Context
,
url
string
,
disableHTTP2
bool
)
(
*
ethclient
.
Client
,
error
)
{
ctxt
,
cancel
:=
context
.
WithTimeout
(
ctx
,
defaultDialTimeout
)
defer
cancel
()
if
strings
.
HasPrefix
(
url
,
"http"
)
{
httpClient
:=
new
(
http
.
Client
)
if
disableHTTP2
{
log
.
Info
(
"Disabled HTTP/2 support in L2 eth client"
)
httpClient
.
Transport
=
&
http
.
Transport
{
TLSNextProto
:
make
(
map
[
string
]
func
(
authority
string
,
c
*
tls
.
Conn
)
http
.
RoundTripper
),
}
}
rpcClient
,
err
:=
rpc
.
DialHTTPWithClient
(
url
,
httpClient
)
if
err
!=
nil
{
return
nil
,
err
}
return
ethclient
.
NewClient
(
rpcClient
),
nil
}
return
ethclient
.
DialContext
(
ctxt
,
url
)
}
go/batch-submitter/metrics/run_metrics_server.go
0 → 100644
View file @
872be7cf
package
metrics
import
(
"fmt"
"net/http"
"strconv"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// RunServer spins up a prometheus metrics server at the provided hostname and
// port.
//
// NOTE: This method MUST be run as a goroutine.
func
RunServer
(
hostname
string
,
port
uint64
)
{
metricsPortStr
:=
strconv
.
FormatUint
(
port
,
10
)
metricsAddr
:=
fmt
.
Sprintf
(
"%s:%s"
,
hostname
,
metricsPortStr
)
http
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
_
=
http
.
ListenAndServe
(
metricsAddr
,
nil
)
}
go/batch-submitter/sentry_log.go
View file @
872be7cf
...
...
@@ -3,6 +3,7 @@ package batchsubmitter
import
(
"errors"
"io"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/getsentry/sentry-go"
...
...
@@ -36,3 +37,14 @@ func SentryStreamHandler(wr io.Writer, fmtr log.Format) log.Handler {
})
return
log
.
LazyHandler
(
log
.
SyncHandler
(
h
))
}
// TraceRateToFloat64 converts a time.Duration into a valid float64 for the
// Sentry client. The client only accepts values between 0.0 and 1.0, so this
// method clamps anything greater than 1 second to 1.0.
func
TraceRateToFloat64
(
rate
time
.
Duration
)
float64
{
rate64
:=
float64
(
rate
)
/
float64
(
time
.
Second
)
if
rate64
>
1.0
{
rate64
=
1.0
}
return
rate64
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment