Skip to content

Commit

Permalink
Merge branch 'main' into mat/reduce-gas-oracle-txs
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Jan 17, 2025
2 parents ace8c3f + f3ca671 commit 001cc0a
Show file tree
Hide file tree
Showing 33 changed files with 1,366 additions and 1,135 deletions.
835 changes: 5 additions & 830 deletions CHANGELOG.md

Large diffs are not rendered by default.

837 changes: 837 additions & 0 deletions LEGACY-CHANGELOG.md

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions app/evmante/evmante_setup_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,5 @@ func (esc EthSetupContextDecorator) AnteHandle(
WithKVGasConfig(storetypes.GasConfig{}).
WithTransientKVGasConfig(storetypes.GasConfig{})

// Reset transient gas used to prepare the execution of current cosmos tx.
// Transient gas-used is necessary to sum the gas-used of cosmos tx, when it contains multiple eth msgs.
esc.evmKeeper.ResetTransientGasUsed(ctx)
return next(newCtx, tx, simulate)
}
8 changes: 0 additions & 8 deletions app/evmante/evmante_setup_ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ func (s *TestSuite) TestEthSetupContextDecorator() {
s.Require().NoError(stateDB.Commit())
tx := evmtest.HappyCreateContractTx(&deps)

// Set block gas used to non 0 to check that handler resets it
deps.App.EvmKeeper.EvmState.BlockGasUsed.Set(deps.Ctx, 1000)

// Ante handler returns new context
newCtx, err := anteDec.AnteHandle(
deps.Ctx, tx, false, evmtest.NextNoOpAnteHandler,
Expand All @@ -35,9 +32,4 @@ func (s *TestSuite) TestEthSetupContextDecorator() {
defaultGasConfig := storetypes.GasConfig{}
s.Require().Equal(defaultGasConfig, newCtx.KVGasConfig())
s.Require().Equal(defaultGasConfig, newCtx.TransientKVGasConfig())

// Check that block gas used is reset to 0
gas, err := deps.App.EvmKeeper.EvmState.BlockGasUsed.Get(newCtx)
s.Require().NoError(err)
s.Require().Equal(gas, uint64(0))
}
2 changes: 2 additions & 0 deletions eth/indexer/evm_tx_indexer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package indexer_test

import (
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -109,6 +110,7 @@ func TestEVMTxIndexer(t *testing.T) {
{Key: "gas_used", Value: `"21000"`},
{Key: "index", Value: `"0"`},
{Key: "hash", Value: `"14A84ED06282645EFBF080E0B7ED80D8D8D6A36337668A12B5F229F81CDD3F57"`},
{Key: "eth_hash", Value: fmt.Sprintf(`"%s"`, txHash.Hex())},
},
},
},
Expand Down
50 changes: 34 additions & 16 deletions eth/rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"sync"
"testing"
"time"

Expand All @@ -16,6 +17,8 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/v2/x/common/testutil"

"github.com/NibiruChain/nibiru/v2/x/evm/embeds"

"github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
Expand All @@ -33,6 +36,9 @@ import (
"github.com/NibiruChain/nibiru/v2/x/common/testutil/testnetwork"
)

// testMutex is used to synchronize the tests which are broadcasting transactions concurrently
var testMutex sync.Mutex

var (
recipient = evmtest.NewEthPrivAcc().EthAddr
amountToSend = evm.NativeToWei(big.NewInt(1))
Expand Down Expand Up @@ -62,7 +68,9 @@ type BackendSuite struct {
}

func TestBackendSuite(t *testing.T) {
suite.Run(t, new(BackendSuite))
testutil.RetrySuiteRunIfDbClosed(t, func() {
suite.Run(t, new(BackendSuite))
}, 2)
}

func (s *BackendSuite) SetupSuite() {
Expand Down Expand Up @@ -95,7 +103,7 @@ func (s *BackendSuite) SetupSuite() {
// Send Transfer TX and use the results in the tests
s.Require().NoError(err)
transferTxHash = s.SendNibiViaEthTransfer(recipient, amountToSend, true)
blockNumber, blockHash := WaitForReceipt(s, transferTxHash)
blockNumber, blockHash, _ := WaitForReceipt(s, transferTxHash)
s.Require().NotNil(blockNumber)
s.Require().NotNil(blockHash)
transferTxBlockNumber = rpc.NewBlockNumber(blockNumber)
Expand All @@ -104,7 +112,7 @@ func (s *BackendSuite) SetupSuite() {
// Deploy test erc20 contract
deployContractTxHash, contractAddress := s.DeployTestContract(true)
testContractAddress = contractAddress
blockNumber, blockHash = WaitForReceipt(s, deployContractTxHash)
blockNumber, blockHash, _ = WaitForReceipt(s, deployContractTxHash)
s.Require().NotNil(blockNumber)
s.Require().NotNil(blockHash)
deployContractBlockNumber = rpc.NewBlockNumber(blockNumber)
Expand Down Expand Up @@ -167,35 +175,41 @@ func SendTransaction(s *BackendSuite, tx *gethcore.LegacyTx, waitForNextBlock bo
return txHash
}

func WaitForReceipt(s *BackendSuite, txHash gethcommon.Hash) (*big.Int, *gethcommon.Hash) {
// WaitForReceipt waits for a transaction to be included in a block, returns block number, block hash and receipt
func WaitForReceipt(s *BackendSuite, txHash gethcommon.Hash) (*big.Int, *gethcommon.Hash, *backend.TransactionReceipt) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

for {
receipt, err := s.backend.GetTransactionReceipt(txHash)
if err != nil {
return nil, nil
return nil, nil, nil
}
if receipt != nil {
return receipt.BlockNumber, &receipt.BlockHash
return receipt.BlockNumber, &receipt.BlockHash, receipt
}
select {
case <-ctx.Done():
fmt.Println("Timeout reached, transaction not included in a block yet.")
return nil, nil
return nil, nil, nil
default:
time.Sleep(1 * time.Second)
}
}
}

// getCurrentNonce returns the current nonce of the funded account
func (s *BackendSuite) getCurrentNonce(address gethcommon.Address) uint64 {
bn, err := s.backend.BlockNumber()
// getUnibiBalance returns the balance of an address in unibi
func (s *BackendSuite) getUnibiBalance(address gethcommon.Address) *big.Int {
latestBlock := rpc.EthLatestBlockNumber
latestBlockOrHash := rpc.BlockNumberOrHash{BlockNumber: &latestBlock}
balance, err := s.backend.GetBalance(address, latestBlockOrHash)
s.Require().NoError(err)
currentHeight := rpc.BlockNumber(bn)
return evm.WeiToNative(balance.ToInt())
}

nonce, err := s.backend.GetTransactionCount(address, currentHeight)
// getCurrentNonce returns the current nonce of the funded account
func (s *BackendSuite) getCurrentNonce(address gethcommon.Address) uint64 {
nonce, err := s.backend.GetTransactionCount(address, rpc.EthPendingBlockNumber)
s.Require().NoError(err)

return uint64(*nonce)
Expand All @@ -213,15 +227,15 @@ func (s *BackendSuite) broadcastSDKTx(sdkTx sdk.Tx) *sdk.TxResponse {
}

// buildContractCreationTx builds a contract creation transaction
func (s *BackendSuite) buildContractCreationTx(nonce uint64) gethcore.Transaction {
func (s *BackendSuite) buildContractCreationTx(nonce uint64, gasLimit uint64) gethcore.Transaction {
packedArgs, err := embeds.SmartContract_TestERC20.ABI.Pack("")
s.Require().NoError(err)
bytecodeForCall := append(embeds.SmartContract_TestERC20.Bytecode, packedArgs...)

creationTx := &gethcore.LegacyTx{
Nonce: nonce,
Data: bytecodeForCall,
Gas: 1_500_000,
Gas: gasLimit,
GasPrice: big.NewInt(1),
}

Expand All @@ -233,7 +247,11 @@ func (s *BackendSuite) buildContractCreationTx(nonce uint64) gethcore.Transactio
}

// buildContractCallTx builds a contract call transaction
func (s *BackendSuite) buildContractCallTx(nonce uint64, contractAddr gethcommon.Address) gethcore.Transaction {
func (s *BackendSuite) buildContractCallTx(
contractAddr gethcommon.Address,
nonce uint64,
gasLimit uint64,
) gethcore.Transaction {
// recipient := crypto.CreateAddress(s.fundedAccEthAddr, 29381)
transferAmount := big.NewInt(100)

Expand All @@ -247,7 +265,7 @@ func (s *BackendSuite) buildContractCallTx(nonce uint64, contractAddr gethcommon
transferTx := &gethcore.LegacyTx{
Nonce: nonce,
Data: packedArgs,
Gas: 100_000,
Gas: gasLimit,
GasPrice: big.NewInt(1),
To: &contractAddr,
}
Expand Down
Loading

0 comments on commit 001cc0a

Please sign in to comment.