Skip to content

Commit

Permalink
clean up old ethereum les wallet code
Browse files Browse the repository at this point in the history
We couldn't build with Go 1.23 because a dependency was using
//go:linkname in a way that is now restricted.
  • Loading branch information
buck54321 committed Aug 21, 2024
1 parent 4a33a2a commit ba138e1
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 809 deletions.
5 changes: 2 additions & 3 deletions client/asset/eth/chaincfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"decred.org/dcrdex/dex"
dexeth "decred.org/dcrdex/dex/networks/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
ethcore "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -100,9 +99,9 @@ func ETHConfig(net dex.Network) (c ethconfig.Config, err error) {
switch net {
// Ethereum
case dex.Mainnet:
c.Genesis = core.DefaultGenesisBlock()
c.Genesis = ethcore.DefaultGenesisBlock()
case dex.Testnet:
c.Genesis = core.DefaultSepoliaGenesisBlock()
c.Genesis = ethcore.DefaultSepoliaGenesisBlock()
case dex.Simnet:
c.Genesis, err = readSimnetGenesisFile()
if err != nil {
Expand Down
43 changes: 41 additions & 2 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ const (
stateUpdateTick = time.Second * 5
// maxUnindexedTxs is the number of pending txs we will allow to be
// unverified on-chain before we halt broadcasting of new txs.
maxUnindexedTxs = 10
peerCountTicker = 5 * time.Second // no rpc calls here
maxUnindexedTxs = 10
peerCountTicker = 5 * time.Second // no rpc calls here
contractVersionNewest = ^uint32(0)
)

var (
Expand Down Expand Up @@ -6091,3 +6092,41 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t

return nil
}

// newTxOpts is a constructor for a TransactOpts.
func newTxOpts(ctx context.Context, from common.Address, val, maxGas uint64, maxFeeRate, gasTipCap *big.Int) *bind.TransactOpts {
// We'll enforce dexeth.MinGasTipCap since the server does, but this isn't
// necessarily a constant for all networks or under all conditions.
minGasWei := dexeth.GweiToWei(dexeth.MinGasTipCap)
if gasTipCap.Cmp(minGasWei) < 0 {
gasTipCap.Set(minGasWei)
}
// This is enforced by concensus. We shouldn't be able to get here with a
// swap tx.
if gasTipCap.Cmp(maxFeeRate) > 0 {
gasTipCap.Set(maxFeeRate)
}
return &bind.TransactOpts{
Context: ctx,
From: from,
Value: dexeth.GweiToWei(val),
GasFeeCap: maxFeeRate,
GasTipCap: gasTipCap,
GasLimit: maxGas,
}
}

func gases(contractVer uint32, versionedGases map[uint32]*dexeth.Gases) *dexeth.Gases {
if contractVer != contractVersionNewest {
return versionedGases[contractVer]
}
var bestVer uint32
var bestGases *dexeth.Gases
for ver, gases := range versionedGases {
if ver >= bestVer {
bestGases = gases
bestVer = ver
}
}
return bestGases
}
5 changes: 5 additions & 0 deletions client/asset/eth/multirpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -72,6 +73,10 @@ var (
// when given an HTTP(S) provider URL. Can be disabled for testing
// ((*MRPCTest).TestRPC).
forceTryWS = true
// https://github.com/ethereum/go-ethereum/blob/16341e05636fd088aa04a27fca6dc5cda5dbab8f/eth/backend.go#L110-L113
// ultimately results in a minimum fee rate by the filter applied at
// https://github.com/ethereum/go-ethereum/blob/4ebeca19d739a243dc0549bcaf014946cde95c4f/core/tx_pool.go#L626
minGasPrice = ethconfig.Defaults.Miner.GasPrice
)

// TODO: Handle rate limiting? From the docs:
Expand Down
141 changes: 0 additions & 141 deletions client/asset/eth/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,16 @@ import (
"bytes"
"crypto/ecdsa"
"fmt"
"path/filepath"

"decred.org/dcrdex/dex"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/les"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/params"
)

const maxPeers = 10

type nodeConfig struct {
net dex.Network
listenAddr, appDir string
logger dex.Logger
}

// ethLogger satisfies geth's logger interface.
type ethLogger struct {
dl dex.Logger
Expand Down Expand Up @@ -121,115 +106,6 @@ func (el *ethLogger) Crit(msg string, ctx ...any) {
// Check that *ethLogger satisfies the log.Logger interface.
var _ log.Logger = (*ethLogger)(nil)

// prepareNode sets up a geth node, but does not start it.
func prepareNode(cfg *nodeConfig) (*node.Node, error) {
stackConf := &node.Config{
DataDir: cfg.appDir,
// KeyStoreDir is set the same as the geth default, but we rely on this
// location for Exists, so protect against future geth changes.
KeyStoreDir: filepath.Join(cfg.appDir, "keystore"),
}

stackConf.Logger = &ethLogger{dl: cfg.logger}

stackConf.P2P.MaxPeers = maxPeers
var key *ecdsa.PrivateKey
var err error
if key, err = crypto.GenerateKey(); err != nil {
return nil, err
}
stackConf.P2P.PrivateKey = key
stackConf.P2P.ListenAddr = cfg.listenAddr
stackConf.P2P.NAT = nat.Any()

var urls []string
switch cfg.net {
case dex.Simnet:
urls = []string{
"enode://897c84f6e4f18195413c1d02927e6a4093f5e7574b52bdec6f20844c4f1f6dd3f16036a9e600bd8681ab50fd8dd144df4a6ba9dd8722bb578a86aaa8222c964f@127.0.0.1:30304", // alpha
"enode://b1d3e358ee5c9b268e911f2cab47bc12d0e65c80a6d2b453fece34facc9ac3caed14aa3bc7578166bb08c5bc9719e5a2267ae14e0b42da393f4d86f6d5829061@127.0.0.1:30305", // beta
"enode://b1c14deee09b9d5549c90b7b30a35c812a56bf6afea5873b05d7a1bcd79c7b0848bcfa982faf80cc9e758a3a0d9b470f0a002840d365050fd5bf45052a6ec313@127.0.0.1:30306", // gamma
"enode://ca414c361d1a38716170923e4900d9dc9203dbaf8fdcaee73e1f861df9fdf20a1453b76fd218c18bc6f3c7e13cbca0b3416af02a53b8e31188faa45aab398d1c@127.0.0.1:30307", // delta
}
case dex.Testnet:
urls = params.SepoliaBootnodes
case dex.Mainnet:
urls = params.MainnetBootnodes
default:
return nil, fmt.Errorf("unknown network ID: %d", uint8(cfg.net))
}

for _, url := range urls {
node, err := enode.Parse(enode.ValidSchemes, url)
if err != nil {
return nil, fmt.Errorf("Bootstrap URL %q invalid: %v", url, err)
}
stackConf.P2P.BootstrapNodes = append(stackConf.P2P.BootstrapNodes, node)
}

if cfg.net != dex.Simnet {
for _, url := range params.V5Bootnodes {
node, err := enode.Parse(enode.ValidSchemes, url)
if err != nil {
return nil, fmt.Errorf("Bootstrap v5 URL %q invalid: %v", url, err)
}
stackConf.P2P.BootstrapNodesV5 = append(stackConf.P2P.BootstrapNodesV5, node)
}
}

node, err := node.New(stackConf)
if err != nil {
return nil, err
}

ks := keystore.NewKeyStore(node.KeyStoreDir(), keystore.LightScryptN, keystore.LightScryptP)
node.AccountManager().AddBackend(ks)

return node, nil
}

// startNode starts a geth node.
func startNode(_ /* chainID */ int64, node *node.Node, network dex.Network) (*les.LightEthereum, error) {
ethCfg, err := ETHConfig(network)
if err != nil {
return nil, err
}

ethCfg.SyncMode = downloader.LightSync

// Eth has a default RPCTxFeeCap of one eth. This prevents txn with a
// total gas fee higher than than one eth to fail when sending. Setting
// the value to zero removes the limit completely.
ethCfg.RPCTxFeeCap = 0

leth, err := les.New(node, &ethCfg)
if err != nil {
return nil, err
}

if err := node.Start(); err != nil {
return nil, err
}

return leth, nil
}

// importKeyToNode imports an private key into an ethereum node that can be
// unlocked with password.
func importKeyToNode(node *node.Node, privateKey, password []byte) error {
priv, err := crypto.ToECDSA(privateKey)
if err != nil {
return err
}
backends := node.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
return fmt.Errorf("importKeyToNode: expected at least 1 keystore backend")
}
ks := backends[0].(*keystore.KeyStore)

return importKeyToKeyStore(ks, priv, password)
}

func importKeyToKeyStore(ks *keystore.KeyStore, priv *ecdsa.PrivateKey, pw []byte) error {
accounts := ks.Accounts()
if len(accounts) == 0 {
Expand All @@ -248,14 +124,6 @@ func importKeyToKeyStore(ks *keystore.KeyStore, priv *ecdsa.PrivateKey, pw []byt
return nil
}

func exportKeyStoreFromNode(node *node.Node) (*keystore.KeyStore, error) {
backends := node.AccountManager().Backends(keystore.KeyStoreType)
if len(backends) == 0 {
return nil, fmt.Errorf("exportKeyStoreFromNode: expected at least 1 keystore backend")
}
return backends[0].(*keystore.KeyStore), nil
}

// accountCredentials captures the account-specific geth interfaces.
type accountCredentials struct {
ks *keystore.KeyStore
Expand All @@ -264,15 +132,6 @@ type accountCredentials struct {
wallet accounts.Wallet
}

// nodeCredentials parses the accountCredentials from the node.
func nodeCredentials(node *node.Node) (*accountCredentials, error) {
ks, err := exportKeyStoreFromNode(node)
if err != nil {
return nil, fmt.Errorf("exportKeyStoreFromNode error: %v", err)
}
return credentialsFromKeyStore(ks)
}

func pathCredentials(dir string) (*accountCredentials, error) {
// TODO: Use StandardScryptN and StandardScryptP?
return credentialsFromKeyStore(keystore.NewKeyStore(dir, keystore.LightScryptN, keystore.LightScryptP))
Expand Down
Loading

0 comments on commit ba138e1

Please sign in to comment.