Skip to content

Commit

Permalink
support web3signer and make signing configurable (privte key or remot…
Browse files Browse the repository at this point in the history
…e signer)
  • Loading branch information
NazariiDenha committed Sep 26, 2024
1 parent a1d1cbc commit 7a4ab5e
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 142 deletions.
23 changes: 19 additions & 4 deletions rollup/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
"l1_base_fee_default": 15000000000,
"l1_blob_base_fee_default": 1
},
"gas_oracle_sender_private_key": "1313131313131313131313131313131313131313131313131313131313131313"
"gas_oracle_sender_signer_config": {
"signer_type": "RemoteSigner",
"remoteSignerUrl": "http://127.0.0.1:9000",
"signer_address": "0x03a1Bba60B5Aa37094cf16123AdD674c01589488"
}
}
},
"l2_config": {
Expand Down Expand Up @@ -60,9 +64,20 @@
"enable_test_env_bypass_features": true,
"finalize_batch_without_proof_timeout_sec": 7200,
"finalize_bundle_without_proof_timeout_sec": 7200,
"gas_oracle_sender_private_key": "1313131313131313131313131313131313131313131313131313131313131313",
"commit_sender_private_key": "1414141414141414141414141414141414141414141414141414141414141414",
"finalize_sender_private_key": "1515151515151515151515151515151515151515151515151515151515151515",
"gas_oracle_sender_signer_config": {
"signer_type": "RemoteSigner",
"remoteSignerUrl": "http://127.0.0.1:9000",
"signer_address": "0x03a1Bba60B5Aa37094cf16123AdD674c01589488"
},
"commit_sender_signer_config": {
"signer_type": "PrivateKey",
"private_key": "1414141414141414141414141414141414141414141414141414141414141414"
},
"finalize_sender_signer_config": {
"signer_type": "RemoteSigner",
"remoteSignerUrl": "http://127.0.0.1:9000",
"signer_address": "0x33e0F539E31B35170FAaA062af703b76a8282bf7"
},
"l1_commit_gas_limit_multiplier": 1.2
},
"chunk_proposer_config": {
Expand Down
24 changes: 12 additions & 12 deletions rollup/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ func TestConfig(t *testing.T) {
assert.NoError(t, err)

os.Setenv("SCROLL_ROLLUP_DB_CONFIG_DSN", "postgres://test:test@postgresql:5432/scroll?sslmode=disable")
os.Setenv("SCROLL_ROLLUP_L1_CONFIG_RELAYER_CONFIG_GAS_ORACLE_SENDER_PRIVATE_KEY", "1616161616161616161616161616161616161616161616161616161616161616")
os.Setenv("SCROLL_ROLLUP_L2_CONFIG_RELAYER_CONFIG_GAS_ORACLE_SENDER_PRIVATE_KEY", "1717171717171717171717171717171717171717171717171717171717171717")
os.Setenv("SCROLL_ROLLUP_L2_CONFIG_RELAYER_CONFIG_COMMIT_SENDER_PRIVATE_KEY", "1818181818181818181818181818181818181818181818181818181818181818")
os.Setenv("SCROLL_ROLLUP_L2_CONFIG_RELAYER_CONFIG_FINALIZE_SENDER_PRIVATE_KEY", "1919191919191919191919191919191919191919191919191919191919191919")
os.Setenv("SCROLL_ROLLUP_L1_CONFIG_RELAYER_CONFIG_GAS_ORACLE_SENDER_SIGNER_CONFIG_SIGNER_ADDRESS", "0x03a1Bba60B5Aa37094cf16123AdD674c01589489")
os.Setenv("SCROLL_ROLLUP_L2_CONFIG_RELAYER_CONFIG_GAS_ORACLE_SENDER_SIGNER_CONFIG_SIGNER_ADDRESS", "0x03a1Bba60B5Aa37094cf16123AdD674c01589480")
os.Setenv("SCROLL_ROLLUP_L2_CONFIG_RELAYER_CONFIG_COMMIT_SENDER_SIGNER_CONFIG_PRIVATE_KEY", "1818181818181818181818181818181818181818181818181818181818181818")
os.Setenv("SCROLL_ROLLUP_L2_CONFIG_RELAYER_CONFIG_FINALIZE_SENDER_SIGNER_CONFIG_SIGNER_ADDRESS", "0x33e0F539E31B35170FAaA062af703b76a8282bf8")

cfg2, err := NewConfig("../../conf/config.json")
assert.NoError(t, err)

assert.NotEqual(t, cfg.DBConfig.DSN, cfg2.DBConfig.DSN)
assert.NotEqual(t, cfg.L1Config.RelayerConfig.GasOracleSenderPrivateKey, cfg2.L1Config.RelayerConfig.GasOracleSenderPrivateKey)
assert.NotEqual(t, cfg.L2Config.RelayerConfig.GasOracleSenderPrivateKey, cfg2.L2Config.RelayerConfig.GasOracleSenderPrivateKey)
assert.NotEqual(t, cfg.L2Config.RelayerConfig.CommitSenderPrivateKey, cfg2.L2Config.RelayerConfig.CommitSenderPrivateKey)
assert.NotEqual(t, cfg.L2Config.RelayerConfig.FinalizeSenderPrivateKey, cfg2.L2Config.RelayerConfig.FinalizeSenderPrivateKey)
assert.NotEqual(t, cfg.L1Config.RelayerConfig.GasOracleSenderSignerConfig, cfg2.L1Config.RelayerConfig.GasOracleSenderSignerConfig)
assert.NotEqual(t, cfg.L2Config.RelayerConfig.GasOracleSenderSignerConfig, cfg2.L2Config.RelayerConfig.GasOracleSenderSignerConfig)
assert.NotEqual(t, cfg.L2Config.RelayerConfig.CommitSenderSignerConfig, cfg2.L2Config.RelayerConfig.CommitSenderSignerConfig)
assert.NotEqual(t, cfg.L2Config.RelayerConfig.FinalizeSenderSignerConfig, cfg2.L2Config.RelayerConfig.FinalizeSenderSignerConfig)

assert.Equal(t, cfg2.DBConfig.DSN, "postgres://test:test@postgresql:5432/scroll?sslmode=disable")
assert.Equal(t, "1616161616161616161616161616161616161616161616161616161616161616", cfg2.L1Config.RelayerConfig.GasOracleSenderPrivateKey)
assert.Equal(t, "1717171717171717171717171717171717171717171717171717171717171717", cfg2.L2Config.RelayerConfig.GasOracleSenderPrivateKey)
assert.Equal(t, "1818181818181818181818181818181818181818181818181818181818181818", cfg2.L2Config.RelayerConfig.CommitSenderPrivateKey)
assert.Equal(t, "1919191919191919191919191919191919191919191919191919191919191919", cfg2.L2Config.RelayerConfig.FinalizeSenderPrivateKey)
assert.Equal(t, "0x03a1Bba60B5Aa37094cf16123AdD674c01589489", cfg2.L1Config.RelayerConfig.GasOracleSenderSignerConfig.SignerAddress)
assert.Equal(t, "0x03a1Bba60B5Aa37094cf16123AdD674c01589480", cfg2.L2Config.RelayerConfig.GasOracleSenderSignerConfig.SignerAddress)
assert.Equal(t, "1818181818181818181818181818181818181818181818181818181818181818", cfg2.L2Config.RelayerConfig.CommitSenderSignerConfig.PrivateKey)
assert.Equal(t, "0x33e0F539E31B35170FAaA062af703b76a8282bf8", cfg2.L2Config.RelayerConfig.FinalizeSenderSignerConfig.SignerAddress)
})
}
17 changes: 13 additions & 4 deletions rollup/internal/config/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ type RelayerConfig struct {
ChainMonitor *ChainMonitor `json:"chain_monitor"`
// L1CommitGasLimitMultiplier multiplier for fallback gas limit in commitBatch txs
L1CommitGasLimitMultiplier float64 `json:"l1_commit_gas_limit_multiplier,omitempty"`
// The private key of the relayer
GasOracleSenderPrivateKey string `json:"gas_oracle_sender_private_key"`
CommitSenderPrivateKey string `json:"commit_sender_private_key"`
FinalizeSenderPrivateKey string `json:"finalize_sender_private_key"`

// Configs of transaction signers (GasOracle, Commit, Finalize)
GasOracleSenderSignerConfig *SignerConfig `json:"gas_oracle_sender_signer_config"`
CommitSenderSignerConfig *SignerConfig `json:"commit_sender_signer_config"`
FinalizeSenderSignerConfig *SignerConfig `json:"finalize_sender_signer_config"`

// Indicates if bypass features specific to testing environments are enabled.
EnableTestEnvBypassFeatures bool `json:"enable_test_env_bypass_features"`
Expand All @@ -84,3 +85,11 @@ type GasOracleConfig struct {
L1BaseFeeDefault uint64 `json:"l1_base_fee_default"`
L1BlobBaseFeeDefault uint64 `json:"l1_blob_base_fee_default"`
}

// SignerConfig - config of signer, contains type and private_key/address,remoteUrl depending on type
type SignerConfig struct {
SignerType string `json:"signer_type"` // type of signer can be PrivateKey or RemoteSigner
PrivateKey string `json:"private_key"` // private key of signer in case of PrivateKey signerType
RemoteSignerUrl string `json:"remote_signer_url"` // remote signer url (web3signer) if case of RemoteSigner signerType
SignerAddress string `json:"signer_address"` // address of signer
}
12 changes: 2 additions & 10 deletions rollup/internal/controller/relayer/l1_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/scroll-tech/go-ethereum/accounts/abi"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/params"
"gorm.io/gorm"
Expand Down Expand Up @@ -57,15 +55,9 @@ func NewLayer1Relayer(ctx context.Context, db *gorm.DB, cfg *config.RelayerConfi

switch serviceType {
case ServiceTypeL1GasOracle:
pKey, err := crypto.ToECDSA(common.FromHex(cfg.GasOracleSenderPrivateKey))
gasOracleSender, err := sender.NewSender(ctx, cfg.SenderConfig, cfg.GasOracleSenderSignerConfig, "l1_relayer", "gas_oracle_sender", types.SenderTypeL1GasOracle, db, reg)
if err != nil {
return nil, fmt.Errorf("new gas oracle sender failed, err: %v", err)
}

gasOracleSender, err = sender.NewSender(ctx, cfg.SenderConfig, pKey, "l1_relayer", "gas_oracle_sender", types.SenderTypeL1GasOracle, db, reg)
if err != nil {
addr := crypto.PubkeyToAddress(pKey.PublicKey)
return nil, fmt.Errorf("new gas oracle sender failed for address %s, err: %v", addr.Hex(), err)
return nil, fmt.Errorf("new gas oracle sender failed, err: %w", err)
}

// Ensure test features aren't enabled on the scroll mainnet.
Expand Down
54 changes: 6 additions & 48 deletions rollup/internal/controller/relayer/l2_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package relayer

import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/scroll-tech/go-ethereum/accounts/abi"
"github.com/scroll-tech/go-ethereum/common"
gethTypes "github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
Expand Down Expand Up @@ -76,18 +74,13 @@ type Layer2Relayer struct {

// NewLayer2Relayer will return a new instance of Layer2RelayerClient
func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.DB, cfg *config.RelayerConfig, chainCfg *params.ChainConfig, initGenesis bool, serviceType ServiceType, reg prometheus.Registerer) (*Layer2Relayer, error) {
gasOracleSenderPrivateKey, commitSenderPrivateKey, finalizeSenderPrivateKey, err := parsePrivateKeys(cfg)
if err != nil {
return nil, fmt.Errorf("failed to parse private keys provided by config, err: %v", err)
}

var gasOracleSender, commitSender, finalizeSender *sender.Sender
switch serviceType {
case ServiceTypeL2GasOracle:
gasOracleSender, err = sender.NewSender(ctx, cfg.SenderConfig, gasOracleSenderPrivateKey, "l2_relayer", "gas_oracle_sender", types.SenderTypeL2GasOracle, db, reg)
gasOracleSender, err := sender.NewSender(ctx, cfg.SenderConfig, cfg.GasOracleSenderSignerConfig, "l2_relayer", "gas_oracle_sender", types.SenderTypeL2GasOracle, db, reg)
if err != nil {
addr := crypto.PubkeyToAddress(gasOracleSenderPrivateKey.PublicKey)
return nil, fmt.Errorf("new gas oracle sender failed for address %s, err: %w", addr.Hex(), err)
return nil, fmt.Errorf("new gas oracle sender failed, err: %w", err)
}

// Ensure test features aren't enabled on the ethereum mainnet.
Expand All @@ -96,16 +89,14 @@ func NewLayer2Relayer(ctx context.Context, l2Client *ethclient.Client, db *gorm.
}

case ServiceTypeL2RollupRelayer:
commitSender, err = sender.NewSender(ctx, cfg.SenderConfig, commitSenderPrivateKey, "l2_relayer", "commit_sender", types.SenderTypeCommitBatch, db, reg)
commitSender, err := sender.NewSender(ctx, cfg.SenderConfig, cfg.CommitSenderSignerConfig, "l2_relayer", "commit_sender", types.SenderTypeCommitBatch, db, reg)
if err != nil {
addr := crypto.PubkeyToAddress(commitSenderPrivateKey.PublicKey)
return nil, fmt.Errorf("new commit sender failed for address %s, err: %w", addr.Hex(), err)
return nil, fmt.Errorf("new commit sender failed, err: %w", err)
}

finalizeSender, err = sender.NewSender(ctx, cfg.SenderConfig, finalizeSenderPrivateKey, "l2_relayer", "finalize_sender", types.SenderTypeFinalizeBatch, db, reg)
finalizeSender, err = sender.NewSender(ctx, cfg.SenderConfig, cfg.FinalizeSenderSignerConfig, "l2_relayer", "finalize_sender", types.SenderTypeFinalizeBatch, db, reg)
if err != nil {
addr := crypto.PubkeyToAddress(finalizeSenderPrivateKey.PublicKey)
return nil, fmt.Errorf("new finalize sender failed for address %s, err: %w", addr.Hex(), err)
return nil, fmt.Errorf("new finalize sender failed, err: %w", err)
}

// Ensure test features aren't enabled on the ethereum mainnet.
Expand Down Expand Up @@ -1286,36 +1277,3 @@ func (r *Layer2Relayer) StopSenders() {
r.finalizeSender.Stop()
}
}

func parsePrivateKeys(cfg *config.RelayerConfig) (*ecdsa.PrivateKey, *ecdsa.PrivateKey, *ecdsa.PrivateKey, error) {
parseKey := func(hexKey string) (*ecdsa.PrivateKey, error) {
return crypto.ToECDSA(common.FromHex(hexKey))
}

gasOracleKey, err := parseKey(cfg.GasOracleSenderPrivateKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("parse gas oracle sender private key failed: %w", err)
}

commitKey, err := parseKey(cfg.CommitSenderPrivateKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("parse commit sender private key failed: %w", err)
}

finalizeKey, err := parseKey(cfg.FinalizeSenderPrivateKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("parse finalize sender private key failed: %w", err)
}

// Check if all three private keys are different
addrGasOracle := crypto.PubkeyToAddress(gasOracleKey.PublicKey)
addrCommit := crypto.PubkeyToAddress(commitKey.PublicKey)
addrFinalize := crypto.PubkeyToAddress(finalizeKey.PublicKey)

if addrGasOracle == addrCommit || addrGasOracle == addrFinalize || addrCommit == addrFinalize {
return nil, nil, nil, fmt.Errorf("gas oracle, commit, and finalize sender addresses must be different. Got: Gas Oracle=%s, Commit=%s, Finalize=%s",
addrGasOracle.Hex(), addrCommit.Hex(), addrFinalize.Hex())
}

return gasOracleKey, commitKey, finalizeKey, nil
}
10 changes: 5 additions & 5 deletions rollup/internal/controller/sender/estimategas.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func (s *Sender) estimateLegacyGas(to *common.Address, data []byte, fallbackGasL

gasLimit, _, err := s.estimateGasLimit(to, data, nil, gasPrice, nil, nil, nil)
if err != nil {
log.Error("estimateLegacyGas estimateGasLimit failure", "gas price", gasPrice, "from", s.auth.From.String(),
"nonce", s.auth.Nonce.Uint64(), "to address", to.String(), "fallback gas limit", fallbackGasLimit, "error", err)
log.Error("estimateLegacyGas estimateGasLimit failure", "gas price", gasPrice, "from", s.transactionSigner.GetAddr().String(),
"nonce", s.transactionSigner.GetNonce(), "to address", to.String(), "fallback gas limit", fallbackGasLimit, "error", err)
if fallbackGasLimit == 0 {
return nil, err
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func (s *Sender) estimateDynamicGas(to *common.Address, data []byte, baseFee uin
gasLimit, accessList, err := s.estimateGasLimit(to, data, nil, nil, gasTipCap, gasFeeCap, nil)
if err != nil {
log.Error("estimateDynamicGas estimateGasLimit failure",
"from", s.auth.From.String(), "nonce", s.auth.Nonce.Uint64(), "to address", to.String(),
"from", s.transactionSigner.GetAddr().String(), "nonce", s.transactionSigner.GetNonce(), "to address", to.String(),
"fallback gas limit", fallbackGasLimit, "error", err)
if fallbackGasLimit == 0 {
return nil, err
Expand Down Expand Up @@ -93,7 +93,7 @@ func (s *Sender) estimateBlobGas(to *common.Address, data []byte, sidecar *gethT
gasLimit, accessList, err := s.estimateGasLimit(to, data, sidecar, nil, gasTipCap, gasFeeCap, blobGasFeeCap)
if err != nil {
log.Error("estimateBlobGas estimateGasLimit failure",
"from", s.auth.From.String(), "nonce", s.auth.Nonce.Uint64(), "to address", to.String(),
"from", s.transactionSigner.GetAddr().String(), "nonce", s.transactionSigner.GetNonce(), "to address", to.String(),
"fallback gas limit", fallbackGasLimit, "error", err)
if fallbackGasLimit == 0 {
return nil, err
Expand All @@ -117,7 +117,7 @@ func (s *Sender) estimateBlobGas(to *common.Address, data []byte, sidecar *gethT

func (s *Sender) estimateGasLimit(to *common.Address, data []byte, sidecar *gethTypes.BlobTxSidecar, gasPrice, gasTipCap, gasFeeCap, blobGasFeeCap *big.Int) (uint64, *types.AccessList, error) {
msg := ethereum.CallMsg{
From: s.auth.From,
From: s.transactionSigner.GetAddr(),
To: to,
GasPrice: gasPrice,
GasTipCap: gasTipCap,
Expand Down
Loading

0 comments on commit 7a4ab5e

Please sign in to comment.