Skip to content

Commit 9db4662

Browse files
committed
Merge remote-tracking branch 'upstream/main' into fix-boilerplates
2 parents 87d7c68 + 84d98a6 commit 9db4662

File tree

33 files changed

+2435
-173
lines changed

33 files changed

+2435
-173
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@
2828
- [\#576](https://github.com/cosmos/evm/pull/576) Parse logs from the txResult.Data and avoid emitting EVM events to cosmos-sdk events.
2929
- [\#584](https://github.com/cosmos/evm/pull/584) Fill block hash and timestamp for json rpc.
3030
- [\#582](https://github.com/cosmos/evm/pull/582) Add block max-gas (from genesis.json) and new min-tip (from app.toml/flags) ingestion into mempool config
31+
- [\#580](https://github.com/cosmos/evm/pull/580) add appside mempool e2e test
3132
- [\#598](https://github.com/cosmos/evm/pull/598) Reduce number of times CreateQueryContext in mempool.
3233
- [\#606](https://github.com/cosmos/evm/pull/606) Regenerate mock file for bank keeper related test.
34+
- [\#609](https://github.com/cosmos/evm/pull/609) Make `erc20Keeper` optional in the EVM keeper
3335
- [\#624](https://github.com/cosmos/evm/pull/624) Cleanup unnecessary `fix-revert-gas-refund-height`.
36+
- [\#635](https://github.com/cosmos/evm/pull/635) Move DefaultStaticPrecompiles to /evm and allow projects to set it by default alongside the keeper.
3437
- [\#577](https://github.com/cosmos/evm/pull/577) Cleanup precompiles boilerplate code.
3538

3639
### FEATURES
3740

3841
- [\#346](https://github.com/cosmos/evm/pull/346) Add eth_createAccessList method and implementation
3942
- [\#502](https://github.com/cosmos/evm/pull/502) Add block time in derived logs.
40-
- [\#588](https://github.com/cosmos/evm/pull/588) go-ethereum metrics are now available in Cosmos SDK's telemetry server at host:port/geth/metrics (default localhost:1317/geth/metrics).
43+
- [\#633](https://github.com/cosmos/evm/pull/633) go-ethereum metrics are now emitted on a separate server. default address: 127.0.0.1:8100.
4144

4245
### STATE BREAKING
4346

contrib/images/evmd-env/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ RUN addgroup -g 1025 nonroot
2222
RUN adduser -D nonroot -u 1025 -G nonroot
2323

2424
# Set up the runtime environment
25-
EXPOSE 26656 26657 1317 9090
25+
EXPOSE 26656 26657 1317 9090 26660 8545 8100
2626
STOPSIGNAL SIGTERM
2727
VOLUME /evmd
2828
WORKDIR /evmd

docs/migrations/v0.4.0_to_v0.5.0_UNRELEASED.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,39 @@ mempoolConfig.CosmosPoolConfig = &cosmosCfg
8080
mempoolConfig.BroadCastTxFn = func(txs []*ethtypes.Transaction) error { return nil }
8181
```
8282

83+
### Default Precompiles
84+
85+
Default precompiles have been moved to `/evm/precompiles/types/defaults.go` and the function name was
86+
changed to `DefaultStaticPrecompiles`. The function signature has also changed, and now takes pointers
87+
as inputs for the `Erc20Keeper` and `TransferKeeper`. Finally, the `WithStaticPrecompiles` builder
88+
function can now happen *alongside the keeper instantiation*, and not after. The new wiring is shown below:
89+
90+
```go
91+
app.EVMKeeper = evmkeeper.NewKeeper(
92+
appCodec, keys[evmtypes.StoreKey], tkeys[evmtypes.TransientKey], keys,
93+
authtypes.NewModuleAddress(govtypes.ModuleName),
94+
app.AccountKeeper,
95+
app.PreciseBankKeeper,
96+
app.StakingKeeper,
97+
app.FeeMarketKeeper,
98+
&app.ConsensusParamsKeeper,
99+
&app.Erc20Keeper,
100+
tracer,
101+
).WithStaticPrecompiles(
102+
precompiletypes.DefaultStaticPrecompiles(
103+
*app.StakingKeeper,
104+
app.DistrKeeper,
105+
app.BankKeeper,
106+
&app.Erc20Keeper, // UPDATED
107+
&app.TransferKeeper, // UPDATED
108+
app.IBCKeeper.ChannelKeeper,
109+
app.GovKeeper,
110+
app.SlashingKeeper,
111+
appCodec,
112+
),
113+
)
114+
```
115+
83116
---
84117

85118
## 3) Build & quick tests

evmd/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ For the sake of this example, we'll be using Metamask:
4343

4444
1. Use the following seed phrase when adding a new wallet:
4545
`gesture inject test cycle original hollow east ridge hen combine
46-
junk child baconzero hope comfort vacuum milk pitch cage oppose
46+
junk child bacon zero hope comfort vacuum milk pitch cage oppose
4747
unhappy lunar seat`
4848
2. On the top left of the Metamask extension, click the Network button.
4949
3. Click Add custom network from the bottom of the modal.

evmd/app.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
precompiletypes "github.com/cosmos/evm/precompiles/types"
78
"io"
89

910
"os"
@@ -486,6 +487,18 @@ func NewExampleApp(
486487
&app.ConsensusParamsKeeper,
487488
&app.Erc20Keeper,
488489
tracer,
490+
).WithStaticPrecompiles(
491+
precompiletypes.DefaultStaticPrecompiles(
492+
*app.StakingKeeper,
493+
app.DistrKeeper,
494+
app.BankKeeper,
495+
&app.Erc20Keeper,
496+
&app.TransferKeeper,
497+
app.IBCKeeper.ChannelKeeper,
498+
app.GovKeeper,
499+
app.SlashingKeeper,
500+
appCodec,
501+
),
489502
)
490503

491504
app.Erc20Keeper = erc20keeper.NewKeeper(
@@ -561,23 +574,6 @@ func NewExampleApp(
561574
// Override the ICS20 app module
562575
transferModule := transfer.NewAppModule(app.TransferKeeper)
563576

564-
// NOTE: we are adding all available Cosmos EVM EVM extensions.
565-
// Not all of them need to be enabled, which can be configured on a per-chain basis.
566-
app.EVMKeeper.WithStaticPrecompiles(
567-
NewAvailableStaticPrecompiles(
568-
*app.StakingKeeper,
569-
app.DistrKeeper,
570-
app.PreciseBankKeeper,
571-
app.Erc20Keeper,
572-
app.TransferKeeper,
573-
app.IBCKeeper.ChannelKeeper,
574-
app.EVMKeeper,
575-
app.GovKeeper,
576-
app.SlashingKeeper,
577-
app.AppCodec(),
578-
),
579-
)
580-
581577
/**** Module Options ****/
582578

583579
// NOTE: Any module instantiated in the module manager that is later modified

evmd/cmd/evmd/cmd/testnet.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ func initTestnetFiles(
288288
sdkGRPCPort = 9090
289289

290290
// evmGRPC = 9900 // TODO: maybe need this? idk.
291-
evmJSONRPC = 8545
292-
evmJSONRPCWS = 8546
293-
evmJSONRPCMetrics = 6065
291+
evmJSONRPC = 8545
292+
evmJSONRPCWS = 8546
293+
evmJSONRPCMetrics = 6065
294+
evmGethMetricsPort = 8100
294295
)
295296
p2pPortStart := 26656
296297

@@ -317,6 +318,7 @@ func initTestnetFiles(
317318
evmCfg.JSONRPC.Address = fmt.Sprintf("127.0.0.1:%d", evmJSONRPC+evmPortOffset)
318319
evmCfg.JSONRPC.MetricsAddress = fmt.Sprintf("127.0.0.1:%d", evmJSONRPCMetrics+evmPortOffset)
319320
evmCfg.JSONRPC.WsAddress = fmt.Sprintf("127.0.0.1:%d", evmJSONRPCWS+evmPortOffset)
321+
evmCfg.EVM.GethMetricsAddress = fmt.Sprintf("127.0.0.1:%d", evmGethMetricsPort+evmPortOffset)
320322
} else {
321323
evmCfg.JSONRPC.WsAddress = fmt.Sprintf("0.0.0.0:%d", evmJSONRPCWS)
322324
evmCfg.JSONRPC.Address = fmt.Sprintf("0.0.0.0:%d", evmJSONRPC)

metrics/geth.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package metrics
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
"time"
8+
9+
gethmetrics "github.com/ethereum/go-ethereum/metrics"
10+
gethprom "github.com/ethereum/go-ethereum/metrics/prometheus"
11+
12+
"cosmossdk.io/log"
13+
)
14+
15+
// StartGethMetricServer starts the geth metrics server on the specified address.
16+
func StartGethMetricServer(ctx context.Context, log log.Logger, addr string) error {
17+
mux := http.NewServeMux()
18+
mux.Handle("/metrics", gethprom.Handler(gethmetrics.DefaultRegistry))
19+
20+
server := &http.Server{
21+
Addr: addr,
22+
Handler: mux,
23+
ReadTimeout: 10 * time.Second,
24+
WriteTimeout: 10 * time.Second,
25+
ReadHeaderTimeout: 10 * time.Second,
26+
}
27+
28+
errCh := make(chan error, 1)
29+
30+
go func() {
31+
log.Info("starting geth metrics server...", "address", addr)
32+
errCh <- server.ListenAndServe()
33+
}()
34+
35+
// Start a blocking select to wait for an indication to stop the server or that
36+
// the server failed to start properly.
37+
select {
38+
case <-ctx.Done():
39+
// The calling process canceled or closed the provided context, so we must
40+
// gracefully stop the metrics server.
41+
log.Info("stopping geth metrics server...", "address", addr)
42+
43+
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
44+
defer cancel()
45+
46+
if err := server.Shutdown(shutdownCtx); err != nil {
47+
log.Error("geth metrics server shutdown error", "err", err)
48+
return err
49+
}
50+
return nil
51+
52+
case err := <-errCh:
53+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
54+
log.Error("failed to start geth metrics server", "err", err)
55+
return err
56+
}
57+
return nil
58+
}
59+
}

evmd/precompiles.go renamed to precompiles/types/defaults.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
package evmd
1+
package types
22

33
import (
44
"fmt"
55
"maps"
66

7-
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
8-
sdk "github.com/cosmos/cosmos-sdk/types"
97
"github.com/ethereum/go-ethereum/common"
108
"github.com/ethereum/go-ethereum/core/vm"
119

@@ -20,11 +18,13 @@ import (
2018
stakingprecompile "github.com/cosmos/evm/precompiles/staking"
2119
erc20Keeper "github.com/cosmos/evm/x/erc20/keeper"
2220
transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper"
23-
evmkeeper "github.com/cosmos/evm/x/vm/keeper"
2421
channelkeeper "github.com/cosmos/ibc-go/v10/modules/core/04-channel/keeper"
2522

2623
"cosmossdk.io/core/address"
24+
2725
"github.com/cosmos/cosmos-sdk/codec"
26+
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
27+
sdktypes "github.com/cosmos/cosmos-sdk/types"
2828
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
2929
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
3030
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
@@ -42,9 +42,9 @@ type Optionals struct {
4242

4343
func defaultOptionals() Optionals {
4444
return Optionals{
45-
AddressCodec: addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
46-
ValidatorAddrCodec: addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
47-
ConsensusAddrCodec: addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
45+
AddressCodec: addresscodec.NewBech32Codec(sdktypes.GetConfig().GetBech32AccountAddrPrefix()),
46+
ValidatorAddrCodec: addresscodec.NewBech32Codec(sdktypes.GetConfig().GetBech32ValidatorAddrPrefix()),
47+
ConsensusAddrCodec: addresscodec.NewBech32Codec(sdktypes.GetConfig().GetBech32ConsensusAddrPrefix()),
4848
}
4949
}
5050

@@ -70,17 +70,16 @@ func WithConsensusAddrCodec(codec address.Codec) Option {
7070

7171
const bech32PrecompileBaseGas = 6_000
7272

73-
// NewAvailableStaticPrecompiles returns the list of all available static precompiled contracts from Cosmos EVM.
73+
// DefaultStaticPrecompiles returns the list of all available static precompiled contracts from Cosmos EVM.
7474
//
7575
// NOTE: this should only be used during initialization of the Keeper.
76-
func NewAvailableStaticPrecompiles(
76+
func DefaultStaticPrecompiles(
7777
stakingKeeper stakingkeeper.Keeper,
7878
distributionKeeper distributionkeeper.Keeper,
7979
bankKeeper cmn.BankKeeper,
80-
erc20Keeper erc20Keeper.Keeper,
81-
transferKeeper transferkeeper.Keeper,
80+
erc20Keeper *erc20Keeper.Keeper,
81+
transferKeeper *transferkeeper.Keeper,
8282
channelKeeper *channelkeeper.Keeper,
83-
evmKeeper *evmkeeper.Keeper,
8483
govKeeper govkeeper.Keeper,
8584
slashingKeeper slashingkeeper.Keeper,
8685
codec codec.Codec,

rpc/backend/call_tx.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ func (b *Backend) SendRawTransaction(data hexutil.Bytes) (common.Hash, error) {
160160
b.Logger.Debug("transaction queued due to nonce gap", "hash", txHash.Hex())
161161
return txHash, nil
162162
}
163+
if b.Mempool != nil && strings.Contains(err.Error(), mempool.ErrNonceLow.Error()) {
164+
from, err := ethSigner.Sender(tx)
165+
if err != nil {
166+
return common.Hash{}, fmt.Errorf("failed to get sender address: %w", err)
167+
}
168+
nonce, err := b.getAccountNonce(from, false, b.ClientCtx.Height, b.Logger)
169+
if err != nil {
170+
return common.Hash{}, fmt.Errorf("failed to get sender's current nonce: %w", err)
171+
}
172+
173+
// SendRawTransaction returns error when tx.Nonce is lower than committed nonce
174+
if tx.Nonce() < nonce {
175+
return common.Hash{}, err
176+
}
177+
178+
// SendRawTransaction does not return error when committed nonce <= tx.Nonce < pending nonce
179+
return txHash, nil
180+
}
181+
163182
b.Logger.Error("failed to broadcast tx", "error", err.Error())
164183
return txHash, fmt.Errorf("failed to broadcast transaction: %w", err)
165184
}

server/config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"errors"
55
"fmt"
6+
"net/netip"
67
"path"
78
"time"
89

@@ -66,6 +67,9 @@ const (
6667
// DefaultEVMMinTip is the default minimum priority fee for the mempool
6768
DefaultEVMMinTip = 0
6869

70+
// DefaultGethMetricsAddress is the default port for the geth metrics server.
71+
DefaultGethMetricsAddress = "127.0.0.1:8100"
72+
6973
// DefaultGasCap is the default cap on gas that can be used in eth_call/estimateGas
7074
DefaultGasCap uint64 = 25_000_000
7175

@@ -145,6 +149,8 @@ type EVMConfig struct {
145149
EVMChainID uint64 `mapstructure:"evm-chain-id"`
146150
// MinTip defines the minimum priority fee for the mempool
147151
MinTip uint64 `mapstructure:"min-tip"`
152+
// GethMetricsAddress is the address the geth metrics server will bind to. Default 127.0.0.1:8100
153+
GethMetricsAddress string `mapstructure:"geth-metrics-address"`
148154
}
149155

150156
// JSONRPCConfig defines configuration for the EVM RPC server.
@@ -213,6 +219,7 @@ func DefaultEVMConfig() *EVMConfig {
213219
EVMChainID: DefaultEVMChainID,
214220
EnablePreimageRecording: DefaultEnablePreimageRecording,
215221
MinTip: DefaultEVMMinTip,
222+
GethMetricsAddress: DefaultGethMetricsAddress,
216223
}
217224
}
218225

@@ -222,6 +229,10 @@ func (c EVMConfig) Validate() error {
222229
return fmt.Errorf("invalid tracer type %s, available types: %v", c.Tracer, evmTracers)
223230
}
224231

232+
if _, err := netip.ParseAddrPort(c.GethMetricsAddress); err != nil {
233+
return fmt.Errorf("invalid geth metrics address %q: %w", c.GethMetricsAddress, err)
234+
}
235+
225236
return nil
226237
}
227238

0 commit comments

Comments
 (0)