Skip to content

Commit

Permalink
refactor: remove unused
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Sep 12, 2024
1 parent 452d47e commit bd43152
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 140 deletions.
259 changes: 141 additions & 118 deletions eth/rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,67 @@
package backend_test

import (
"bufio"
"math/big"
"os"
"testing"

"crypto/ecdsa"

tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
gethcommon "github.com/ethereum/go-ethereum/common"
gethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/v2/app"
"github.com/NibiruChain/nibiru/v2/app/appconst"
"github.com/NibiruChain/nibiru/v2/eth"
"github.com/NibiruChain/nibiru/v2/eth/crypto/hd"
"github.com/NibiruChain/nibiru/v2/eth/encoding"
"github.com/NibiruChain/nibiru/v2/eth/rpc"
"github.com/NibiruChain/nibiru/v2/eth/rpc/backend"

// "github.com/NibiruChain/nibiru/v2/x/common/testutil"
"github.com/NibiruChain/nibiru/v2/x/common/testutil/genesis"
"github.com/NibiruChain/nibiru/v2/x/common/testutil/testapp"
"github.com/NibiruChain/nibiru/v2/x/common/testutil/testnetwork"
"github.com/NibiruChain/nibiru/v2/x/evm"
evmtest "github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
)

// --------------------------------------------------------------------
// ------------------------------------------------ Imports using mocks
// --------------------------------------------------------------------
// import (
// "bufio"
// "math/big"
// "os"
// "testing"

// "crypto/ecdsa"

// tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
// "github.com/cosmos/cosmos-sdk/crypto/keyring"
// sdk "github.com/cosmos/cosmos-sdk/types"
// "github.com/ethereum/go-ethereum/common"
// gethcommon "github.com/ethereum/go-ethereum/common"
// gethcore "github.com/ethereum/go-ethereum/core/types"
// "github.com/ethereum/go-ethereum/crypto"
// "github.com/stretchr/testify/suite"

// "github.com/NibiruChain/nibiru/v2/app"
// "github.com/NibiruChain/nibiru/v2/app/appconst"
// "github.com/NibiruChain/nibiru/v2/eth"
// "github.com/NibiruChain/nibiru/v2/eth/crypto/hd"
// "github.com/NibiruChain/nibiru/v2/eth/encoding"
// "github.com/NibiruChain/nibiru/v2/eth/rpc"
// "github.com/NibiruChain/nibiru/v2/eth/rpc/backend"

// "github.com/NibiruChain/nibiru/v2/x/common/testutil/genesis"
// "github.com/NibiruChain/nibiru/v2/x/common/testutil/testapp"
// "github.com/NibiruChain/nibiru/v2/x/common/testutil/testnetwork"
// "github.com/NibiruChain/nibiru/v2/x/evm"
// "github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
// )

type BackendSuite struct {
suite.Suite

from common.Address
acc sdk.AccAddress
signer keyring.Signer
// from common.Address
// acc sdk.AccAddress
// signer keyring.Signer

cfg testnetwork.Config
network *testnetwork.Network
Expand Down Expand Up @@ -80,106 +103,106 @@ func (s *BackendSuite) SetupSuite() {
s.NoError(s.network.WaitForNextBlock())
}

// buildEthereumTx returns an example legacy Ethereum transaction
func (s *BackendSuite) buildEthereumTx() (*evm.MsgEthereumTx, []byte) {
ethTxParams := evm.EvmTxArgs{
ChainID: s.ethChainID,
Nonce: uint64(0),
To: &common.Address{},
Amount: big.NewInt(0),
GasLimit: 100000,
GasPrice: big.NewInt(1),
}
msgEthereumTx := evm.NewTx(&ethTxParams)

// A valid msg should have empty `From`
msgEthereumTx.From = s.from.Hex()

txBuilder := s.node.ClientCtx.TxConfig.NewTxBuilder()
err := txBuilder.SetMsgs(msgEthereumTx)
s.Require().NoError(err)

bz, err := s.node.ClientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
s.Require().NoError(err)
return msgEthereumTx, bz
}

// buildFormattedBlock returns a formatted block for testing
func (s *BackendSuite) buildFormattedBlock(
blockRes *tmrpctypes.ResultBlockResults,
resBlock *tmrpctypes.ResultBlock,
fullTx bool,
tx *evm.MsgEthereumTx,
validator sdk.AccAddress,
baseFee *big.Int,
) map[string]interface{} {
header := resBlock.Block.Header
gasLimit := int64(^uint32(0)) // for `MaxGas = -1` (DefaultConsensusParams)
gasUsed := new(big.Int).SetUint64(uint64(blockRes.TxsResults[0].GasUsed))

root := common.Hash{}.Bytes()
receipt := gethcore.NewReceipt(root, false, gasUsed.Uint64())
bloom := gethcore.CreateBloom(gethcore.Receipts{receipt})

ethRPCTxs := []interface{}{}
if tx != nil {
if fullTx {
rpcTx, err := rpc.NewRPCTxFromEthTx(
tx.AsTransaction(),
common.BytesToHash(header.Hash()),
uint64(header.Height),
uint64(0),
baseFee,
s.ethChainID,
)
s.Require().NoError(err)
ethRPCTxs = []interface{}{rpcTx}
} else {
ethRPCTxs = []interface{}{common.HexToHash(tx.Hash)}
}
}

return rpc.FormatBlock(
header,
resBlock.Block.Size(),
gasLimit,
gasUsed,
ethRPCTxs,
bloom,
common.BytesToAddress(validator.Bytes()),
baseFee,
)
}

func (s *BackendSuite) generateTestKeyring(clientDir string) (keyring.Keyring, error) {
buf := bufio.NewReader(os.Stdin)
encCfg := encoding.MakeConfig(app.ModuleBasics)
return keyring.New(
sdk.KeyringServiceName(), // appName
keyring.BackendTest, // backend
clientDir, // rootDir
buf, // userInput
encCfg.Codec, // codec
[]keyring.Option{hd.EthSecp256k1Option()}...,
)
}

func (s *BackendSuite) signAndEncodeEthTx(msgEthereumTx *evm.MsgEthereumTx) []byte {
ethAcc := evmtest.NewEthPrivAcc()
from, priv := ethAcc.EthAddr, ethAcc.PrivKey
signer := evmtest.NewSigner(priv)

ethSigner := gethcore.LatestSigner(s.backend.ChainConfig())
msgEthereumTx.From = from.String()
err := msgEthereumTx.Sign(ethSigner, signer)
s.Require().NoError(err)

tx, err := msgEthereumTx.BuildTx(s.node.ClientCtx.TxConfig.NewTxBuilder(), eth.EthBaseDenom)
s.Require().NoError(err)

txEncoder := s.node.ClientCtx.TxConfig.TxEncoder()
txBz, err := txEncoder(tx)
s.Require().NoError(err)

return txBz
}
// // buildEthereumTx returns an example legacy Ethereum transaction
// func (s *BackendSuite) buildEthereumTx() (*evm.MsgEthereumTx, []byte) {
// ethTxParams := evm.EvmTxArgs{
// ChainID: s.ethChainID,
// Nonce: uint64(0),
// To: &common.Address{},
// Amount: big.NewInt(0),
// GasLimit: 100000,
// GasPrice: big.NewInt(1),
// }
// msgEthereumTx := evm.NewTx(&ethTxParams)

// // A valid msg should have empty `From`
// msgEthereumTx.From = s.from.Hex()

// txBuilder := s.node.ClientCtx.TxConfig.NewTxBuilder()
// err := txBuilder.SetMsgs(msgEthereumTx)
// s.Require().NoError(err)

// bz, err := s.node.ClientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
// s.Require().NoError(err)
// return msgEthereumTx, bz
// }

// // buildFormattedBlock returns a formatted block for testing
// func (s *BackendSuite) buildFormattedBlock(
// blockRes *tmrpctypes.ResultBlockResults,
// resBlock *tmrpctypes.ResultBlock,
// fullTx bool,
// tx *evm.MsgEthereumTx,
// validator sdk.AccAddress,
// baseFee *big.Int,
// ) map[string]interface{} {
// header := resBlock.Block.Header
// gasLimit := int64(^uint32(0)) // for `MaxGas = -1` (DefaultConsensusParams)
// gasUsed := new(big.Int).SetUint64(uint64(blockRes.TxsResults[0].GasUsed))

// root := common.Hash{}.Bytes()
// receipt := gethcore.NewReceipt(root, false, gasUsed.Uint64())
// bloom := gethcore.CreateBloom(gethcore.Receipts{receipt})

// ethRPCTxs := []interface{}{}
// if tx != nil {
// if fullTx {
// rpcTx, err := rpc.NewRPCTxFromEthTx(
// tx.AsTransaction(),
// common.BytesToHash(header.Hash()),
// uint64(header.Height),
// uint64(0),
// baseFee,
// s.ethChainID,
// )
// s.Require().NoError(err)
// ethRPCTxs = []interface{}{rpcTx}
// } else {
// ethRPCTxs = []interface{}{common.HexToHash(tx.Hash)}
// }
// }

// return rpc.FormatBlock(
// header,
// resBlock.Block.Size(),
// gasLimit,
// gasUsed,
// ethRPCTxs,
// bloom,
// common.BytesToAddress(validator.Bytes()),
// baseFee,
// )
// }

// func (s *BackendSuite) generateTestKeyring(clientDir string) (keyring.Keyring, error) {
// buf := bufio.NewReader(os.Stdin)
// encCfg := encoding.MakeConfig(app.ModuleBasics)
// return keyring.New(
// sdk.KeyringServiceName(), // appName
// keyring.BackendTest, // backend
// clientDir, // rootDir
// buf, // userInput
// encCfg.Codec, // codec
// []keyring.Option{hd.EthSecp256k1Option()}...,
// )
// }

// func (s *BackendSuite) signAndEncodeEthTx(msgEthereumTx *evm.MsgEthereumTx) []byte {
// ethAcc := evmtest.NewEthPrivAcc()
// from, priv := ethAcc.EthAddr, ethAcc.PrivKey
// signer := evmtest.NewSigner(priv)

// ethSigner := gethcore.LatestSigner(s.backend.ChainConfig())
// msgEthereumTx.From = from.String()
// err := msgEthereumTx.Sign(ethSigner, signer)
// s.Require().NoError(err)

// tx, err := msgEthereumTx.BuildTx(s.node.ClientCtx.TxConfig.NewTxBuilder(), eth.EthBaseDenom)
// s.Require().NoError(err)

// txEncoder := s.node.ClientCtx.TxConfig.TxEncoder()
// txBz, err := txEncoder(tx)
// s.Require().NoError(err)

// return txBz
// }
3 changes: 2 additions & 1 deletion eth/rpc/backend/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"math/big"

"github.com/NibiruChain/nibiru/v2/eth/rpc"
"github.com/ethereum/go-ethereum/common/hexutil"

"github.com/NibiruChain/nibiru/v2/eth/rpc"
)

func (s *BackendSuite) TestBlockNumber() {
Expand Down
46 changes: 25 additions & 21 deletions eth/rpc/backend/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sort"
"strings"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -210,22 +209,13 @@ func (b *Backend) processBlock(

// AllTxLogsFromEvents parses all ethereum logs from cosmos events
func AllTxLogsFromEvents(events []abci.Event) (allLogs [][]*gethcore.Log, err error) {

Check warning on line 211 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L211

Added line #L211 was not covered by tests
allLogs = make([][]*gethcore.Log, 0, 4)
eventType := evm.TypeUrlEventTxLog
for _, event := range events {
if event.Type != eventType {
if event.Type != evm.TypeUrlEventTxLog {

Check warning on line 213 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L213

Added line #L213 was not covered by tests
continue
}

typedProtoEvent, err := sdk.ParseTypedEvent(event)
typedEvent, err := new(evm.EventTxLog).FromABCIEvent(event)
if err != nil {
return allLogs, errors.Wrapf(
err, "failed to parse event of type %s", eventType)
}
typedEvent, ok := (typedProtoEvent).(*evm.EventTxLog)
if !ok {
return allLogs, errors.Wrapf(
err, "failed to parse event of type %s", eventType)
return allLogs, err

Check warning on line 218 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L216-L218

Added lines #L216 - L218 were not covered by tests
}

logs, err := ParseTxLogsFromEvent(typedEvent)

Check warning on line 221 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L221

Added line #L221 was not covered by tests
Expand All @@ -239,21 +229,35 @@ func AllTxLogsFromEvents(events []abci.Event) (allLogs [][]*gethcore.Log, err er
}

// TxLogsFromEvents parses ethereum logs from cosmos events for specific msg index
func TxLogsFromEvents(events []abci.Event, msgIndex int) (txLogsForMsgIdx []*gethcore.Log, err error) {
allLogs, err := AllTxLogsFromEvents(events)
if err != nil {
return nil, err
func TxLogsFromEvents(
events []abci.Event, msgIndex int,
) (txLogsForMsgIdx []*gethcore.Log, err error) {

Check warning on line 234 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L234

Added line #L234 was not covered by tests
for _, event := range events {
if event.Type != evm.TypeUrlEventTxLog {

Check warning on line 236 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L236

Added line #L236 was not covered by tests
continue
}

if msgIndex > 0 {
// not the eth tx we want
msgIndex--
continue
}

typedEvent, err := new(evm.EventTxLog).FromABCIEvent(event)
if err != nil {
return txLogsForMsgIdx, err

Check warning on line 248 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L246-L248

Added lines #L246 - L248 were not covered by tests
}
return ParseTxLogsFromEvent(typedEvent)

Check warning on line 250 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L250

Added line #L250 was not covered by tests
}
return allLogs[msgIndex], nil
return nil, fmt.Errorf("eth tx logs not found for message index %d", msgIndex)
}

// ParseTxLogsFromEvent parse tx logs from one event
func ParseTxLogsFromEvent(event *evm.EventTxLog) ([]*gethcore.Log, error) {
evmTxLogs := []*gethcore.Log{}
evmTxLogs := make([]*gethcore.Log, len(event.TxLogs))
for _, txLogStr := range event.TxLogs {
txLog := new(evm.Log)
err := json.Unmarshal([]byte(txLogStr), txLog)
if err != nil {
if err := json.Unmarshal([]byte(txLogStr), txLog); err != nil {

Check warning on line 260 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L256-L260

Added lines #L256 - L260 were not covered by tests
return nil, err
}
evmTxLogs = append(evmTxLogs, txLog.ToEthereum())

Check warning on line 263 in eth/rpc/backend/utils.go

View check run for this annotation

Codecov / codecov/patch

eth/rpc/backend/utils.go#L263

Added line #L263 was not covered by tests
Expand Down
Loading

0 comments on commit bd43152

Please sign in to comment.