Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
bless
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Bear committed Oct 12, 2023
1 parent 0ea1357 commit e2f8f06
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 12 deletions.
8 changes: 4 additions & 4 deletions contracts/scripts/DeployAndCallERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ pragma solidity ^0.8.17;

import "../lib/forge-std/src/Script.sol";
import "../src/testing/SolmateERC20.sol";
import "../src/testing/ConsumeGas.sol";

contract DeployAndCallERC20 is Script {
function run() public {
address dropAddress = address(12);
uint256 quantity = 50000;

vm.startBroadcast();
ConsumeGas drop = new ConsumeGas();
SolmateERC20 drop = new SolmateERC20();

for (uint256 i = 0; i < 10; i++) {
drop.consumeGas(500000);
for (uint256 i = 0; i < 66; i++) {
quantity += 50000;
drop.mint(dropAddress, quantity);
}

vm.stopBroadcast();
Expand Down
46 changes: 46 additions & 0 deletions contracts/scripts/DeployAndConsumeGas.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2023 Berachain Foundation
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

pragma solidity ^0.8.17;

import "../lib/forge-std/src/Script.sol";
import "../src/testing/SolmateERC20.sol";
import "../src/testing/ConsumeGas.sol";

contract DeployAndConsumeGas is Script {
function run() public {
address dropAddress = address(12);
uint256 quantity = 50000;

vm.startBroadcast();
ConsumeGas drop = new ConsumeGas();

for (uint256 i = 0; i < 1000; i++) {
drop.consumeGas(300000);
}

vm.stopBroadcast();
}
}
24 changes: 24 additions & 0 deletions cosmos/x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,32 @@ package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// Precommit runs on the Cosmo-SDK lifecycle Precommit().
func (k *Keeper) Precommit(ctx context.Context) error {
// Verify that the EVM block was written.
// TODO: Set/GetHead to set and get the canonical head.
blockNum := uint64(sdk.UnwrapSDKContext(ctx).BlockHeight())
block := k.chain.GetBlockByNumber(blockNum)
if block == nil {
panic(
fmt.Sprintf("EVM BLOCK FAILURE AT BLOCK %d", blockNum),
)
} else if block.NumberU64() != blockNum {
panic(
fmt.Sprintf(
"EVM BLOCK [%d] DOES NOT MATCH COMET BLOCK [%d]", block.NumberU64(), blockNum,
),
)
}
return nil
}

// PrepareCheckState runs on the Cosmos-SDK lifecycle PrepareCheckState().
func (k *Keeper) PrepareCheckState(ctx context.Context) error {
k.sp.Prepare(ctx)
return nil
Expand Down
1 change: 1 addition & 0 deletions cosmos/x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
type Blockchain interface {
PreparePlugins(context.Context)
core.ChainWriter
core.ChainReader
}

type Keeper struct {
Expand Down
24 changes: 16 additions & 8 deletions cosmos/x/evm/keeper/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,32 @@ import (
"github.com/ethereum/go-ethereum/beacon/engine"

evmtypes "pkg.berachain.dev/polaris/cosmos/x/evm/types"
"pkg.berachain.dev/polaris/eth/core/types"
)

func (k *Keeper) ProcessPayloadEnvelope(
ctx context.Context, msg *evmtypes.WrappedPayloadEnvelope,
) (*evmtypes.WrappedPayloadEnvelopeResponse, error) {
var err error
var block *types.Block

// TODO: maybe we just consume the block gas limit and call it a day?
sCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sCtx.GasMeter()
blockGasMeter := sCtx.BlockGasMeter()

// Reset GasMeter to 0.
gasMeter.RefundGas(gasMeter.GasConsumed(), "reset before evm block")
blockGasMeter.RefundGas(blockGasMeter.GasConsumed(), "reset before evm block")
defer gasMeter.ConsumeGas(gasMeter.GasConsumed(), "reset after evm")

envelope := engine.ExecutionPayloadEnvelope{}
err := envelope.UnmarshalJSON(msg.Data)
err = envelope.UnmarshalJSON(msg.Data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal payload envelope: %w", err)
}

sCtx := sdk.UnwrapSDKContext(ctx)
gasMeter := sCtx.GasMeter()

block, err := engine.ExecutableDataToBlock(*envelope.ExecutionPayload, nil, nil)
block, err = engine.ExecutableDataToBlock(*envelope.ExecutionPayload, nil, nil)
if err != nil {
k.Logger(sCtx).Error("failed to build evm block", "err", err)
return nil, err
Expand All @@ -55,9 +66,6 @@ func (k *Keeper) ProcessPayloadEnvelope(
return nil, err
}

// Consume the gas used by the execution of the ethereum block.
gasMeter.ConsumeGas(block.GasUsed(), "block gas used")

return &evmtypes.WrappedPayloadEnvelopeResponse{}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions cosmos/x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ConsensusVersion = 1
var (
_ appmodule.HasServices = AppModule{}
_ appmodule.HasPrepareCheckState = AppModule{}
_ appmodule.HasPrecommit = AppModule{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)
Expand Down Expand Up @@ -128,3 +129,7 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
func (am AppModule) PrepareCheckState(ctx context.Context) error {
return am.keeper.PrepareCheckState(ctx)
}

func (am AppModule) Precommit(ctx context.Context) error {
return am.keeper.Precommit(ctx)
}
3 changes: 3 additions & 0 deletions e2e/testapp/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func MakeAppConfig(bech32Prefix string) depinject.Config {
stakingtypes.ModuleName,
genutiltypes.ModuleName,
},
Precommiters: []string{
evmtypes.ModuleName,
},
OverrideStoreKeys: []*runtimev1alpha1.StoreKeyConfig{
{
ModuleName: authtypes.ModuleName,
Expand Down

0 comments on commit e2f8f06

Please sign in to comment.