2023-02-22 23:40:56 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2023-06-06 16:40:17 +00:00
|
|
|
"context"
|
2023-03-02 01:05:53 +00:00
|
|
|
"encoding/hex"
|
2023-02-22 23:40:56 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-06-30 05:30:02 +00:00
|
|
|
"math/big"
|
2023-02-22 23:40:56 +00:00
|
|
|
"os"
|
2023-03-02 01:05:53 +00:00
|
|
|
"time"
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-08-02 21:52:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2023-02-22 23:40:56 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
2023-10-31 23:37:40 +00:00
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2023-02-22 23:40:56 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2023-08-02 21:52:48 +00:00
|
|
|
|
2023-04-03 16:58:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2023-03-02 01:05:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2023-08-02 21:52:48 +00:00
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
2023-04-06 17:51:13 +00:00
|
|
|
emtests "github.com/evmos/ethermint/tests"
|
2023-04-04 00:08:45 +00:00
|
|
|
emtypes "github.com/evmos/ethermint/types"
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
"github.com/0glabs/0g-chain/chaincfg"
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/tests/util"
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// SigningAccount wraps details about an account and its private keys.
|
|
|
|
// It exposes functionality for signing and broadcasting transactions.
|
2023-02-22 23:40:56 +00:00
|
|
|
type SigningAccount struct {
|
2023-03-02 01:05:53 +00:00
|
|
|
name string
|
|
|
|
mnemonic string
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
evmPrivKey cryptotypes.PrivKey
|
2023-03-02 01:05:53 +00:00
|
|
|
evmSigner *util.EvmSigner
|
|
|
|
evmReqChan chan<- util.EvmTxRequest
|
|
|
|
evmResChan <-chan util.EvmTxResponse
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
zgChainSigner *util.ZgChainSigner
|
|
|
|
sdkReqChan chan<- util.ZgChainMsgRequest
|
|
|
|
sdkResChan <-chan util.ZgChainMsgResponse
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-04-03 16:58:45 +00:00
|
|
|
EvmAuth *bind.TransactOpts
|
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
EvmAddress common.Address
|
|
|
|
SdkAddress sdk.AccAddress
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-06-30 05:30:02 +00:00
|
|
|
gasDenom string
|
|
|
|
|
2023-02-22 23:40:56 +00:00
|
|
|
l *log.Logger
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
// GetAccount returns the account with the given name or fails.
|
|
|
|
func (chain *Chain) GetAccount(name string) *SigningAccount {
|
|
|
|
acc, found := chain.accounts[name]
|
2023-02-22 23:40:56 +00:00
|
|
|
if !found {
|
2023-03-07 22:37:45 +00:00
|
|
|
chain.t.Fatalf("failed to find account with name %s", name)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
// AddNewSigningAccount sets up a new account with a signer for SDK and EVM transactions.
|
|
|
|
func (chain *Chain) AddNewSigningAccount(name string, hdPath *hd.BIP44Params, chainId, mnemonic string) *SigningAccount {
|
|
|
|
if _, found := chain.accounts[name]; found {
|
|
|
|
chain.t.Fatalf("account with name %s already exists", name)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// 0gChain signing account for SDK side
|
2023-02-22 23:40:56 +00:00
|
|
|
privKeyBytes, err := hd.Secp256k1.Derive()(mnemonic, "", hdPath.String())
|
2023-03-07 22:37:45 +00:00
|
|
|
require.NoErrorf(chain.t, err, "failed to derive private key from mnemonic for %s: %s", name, err)
|
2023-03-02 01:05:53 +00:00
|
|
|
privKey := ðsecp256k1.PrivKey{Key: privKeyBytes}
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
return chain.AddNewSigningAccountFromPrivKey(
|
|
|
|
name,
|
|
|
|
privKey,
|
|
|
|
mnemonic,
|
|
|
|
chainId,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddNewSigningAccountFromPrivKey sets up a new account with a signer for SDK and EVM transactions,
|
|
|
|
// using the given private key.
|
|
|
|
func (chain *Chain) AddNewSigningAccountFromPrivKey(
|
|
|
|
name string,
|
|
|
|
privKey cryptotypes.PrivKey,
|
|
|
|
mnemonic string, // optional
|
|
|
|
chainId string,
|
|
|
|
) *SigningAccount {
|
|
|
|
if _, found := chain.accounts[name]; found {
|
|
|
|
chain.t.Fatalf("account with name %s already exists", name)
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// 0gChain signing account for SDK side
|
|
|
|
zgChainSigner := util.NewZgChainSigner(
|
2023-02-22 23:40:56 +00:00
|
|
|
chainId,
|
2023-04-06 17:51:13 +00:00
|
|
|
chain.EncodingConfig,
|
2023-12-13 17:17:37 +00:00
|
|
|
chain.Grpc.Query.Auth,
|
|
|
|
chain.Grpc.Query.Tx,
|
2023-02-22 23:40:56 +00:00
|
|
|
privKey,
|
|
|
|
100,
|
|
|
|
)
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
sdkReqChan := make(chan util.ZgChainMsgRequest)
|
|
|
|
sdkResChan, err := zgChainSigner.Run(sdkReqChan)
|
2023-03-07 22:37:45 +00:00
|
|
|
require.NoErrorf(chain.t, err, "failed to start signer for account %s: %s", name, err)
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// 0gChain signing account for EVM side
|
2023-03-02 01:05:53 +00:00
|
|
|
evmChainId, err := emtypes.ParseChainID(chainId)
|
2023-03-07 22:37:45 +00:00
|
|
|
require.NoErrorf(chain.t, err, "unable to parse ethermint-compatible chain id from %s", chainId)
|
2023-10-31 23:37:40 +00:00
|
|
|
ecdsaPrivKey, err := crypto.HexToECDSA(hex.EncodeToString(privKey.Bytes()))
|
2023-03-07 22:37:45 +00:00
|
|
|
require.NoError(chain.t, err, "failed to generate ECDSA private key from bytes")
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
evmSigner, err := util.NewEvmSigner(
|
2023-03-07 22:37:45 +00:00
|
|
|
chain.EvmClient,
|
2023-03-02 01:05:53 +00:00
|
|
|
ecdsaPrivKey,
|
|
|
|
evmChainId,
|
|
|
|
)
|
2023-03-07 22:37:45 +00:00
|
|
|
require.NoErrorf(chain.t, err, "failed to create evm signer")
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
evmReqChan := make(chan util.EvmTxRequest)
|
|
|
|
evmResChan := evmSigner.Run(evmReqChan)
|
|
|
|
|
2023-02-22 23:40:56 +00:00
|
|
|
logger := log.New(os.Stdout, fmt.Sprintf("[%s] ", name), log.LstdFlags)
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
chain.accounts[name] = &SigningAccount{
|
2023-03-02 01:05:53 +00:00
|
|
|
name: name,
|
|
|
|
mnemonic: mnemonic,
|
|
|
|
l: logger,
|
|
|
|
|
2023-06-30 05:30:02 +00:00
|
|
|
gasDenom: chain.StakingDenom,
|
|
|
|
|
2023-04-06 17:51:13 +00:00
|
|
|
evmPrivKey: privKey,
|
2023-03-02 01:05:53 +00:00
|
|
|
evmSigner: evmSigner,
|
|
|
|
evmReqChan: evmReqChan,
|
|
|
|
evmResChan: evmResChan,
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
zgChainSigner: zgChainSigner,
|
2024-05-09 18:54:47 +00:00
|
|
|
sdkReqChan: sdkReqChan,
|
|
|
|
sdkResChan: sdkResChan,
|
2023-03-02 01:05:53 +00:00
|
|
|
|
2023-04-03 16:58:45 +00:00
|
|
|
EvmAuth: evmSigner.Auth,
|
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
EvmAddress: evmSigner.Address(),
|
2024-05-01 05:53:58 +00:00
|
|
|
SdkAddress: zgChainSigner.Address(),
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
return chain.accounts[name]
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 05:53:58 +00:00
|
|
|
// SignAndBroadcastZgChainTx sends a request to the signer and awaits its response.
|
|
|
|
func (a *SigningAccount) SignAndBroadcastZgChainTx(req util.ZgChainMsgRequest) util.ZgChainMsgResponse {
|
2023-03-07 22:37:45 +00:00
|
|
|
a.l.Printf("broadcasting sdk tx. has data = %+v\n", req.Data)
|
2023-02-22 23:40:56 +00:00
|
|
|
// send the request to signer
|
2023-03-02 01:05:53 +00:00
|
|
|
a.sdkReqChan <- req
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
// TODO: timeout awaiting the response.
|
2023-02-22 23:40:56 +00:00
|
|
|
// block and await response
|
|
|
|
// response is not returned until the msg is committed to a block
|
2023-03-02 01:05:53 +00:00
|
|
|
res := <-a.sdkResChan
|
2023-02-22 23:40:56 +00:00
|
|
|
|
|
|
|
// error will be set if response is not Code 0 (success) or Code 19 (already in mempool)
|
|
|
|
if res.Err != nil {
|
|
|
|
a.l.Printf("response code: %d error: %s\n", res.Result.Code, res.Result.RawLog)
|
|
|
|
} else {
|
|
|
|
a.l.Printf("response code: %d, hash %s\n", res.Result.Code, res.Result.TxHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
// EvmTxResponse is util.EvmTxResponse that also includes the Receipt, if available
|
|
|
|
type EvmTxResponse struct {
|
|
|
|
util.EvmTxResponse
|
|
|
|
Receipt *ethtypes.Receipt
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignAndBroadcastEvmTx sends a request to the signer and awaits its response.
|
|
|
|
func (a *SigningAccount) SignAndBroadcastEvmTx(req util.EvmTxRequest) EvmTxResponse {
|
|
|
|
a.l.Printf("broadcasting evm tx %+v\n", req.Data)
|
|
|
|
// send the request to signer
|
|
|
|
a.evmReqChan <- req
|
|
|
|
|
|
|
|
// block and await response
|
|
|
|
// response occurs once tx is submitted to pending tx pool.
|
|
|
|
// poll for the receipt to wait for it to be included in a block
|
|
|
|
res := <-a.evmResChan
|
|
|
|
response := EvmTxResponse{
|
|
|
|
EvmTxResponse: res,
|
|
|
|
}
|
|
|
|
// if failed during signing or broadcast, there will never be a receipt.
|
|
|
|
if res.Err != nil {
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't have a tx receipt within a given timeout, fail the request
|
2023-06-30 05:30:02 +00:00
|
|
|
a.l.Printf("awaiting evm tx receipt for tx %s\n", res.TxHash)
|
2023-04-03 16:58:45 +00:00
|
|
|
response.Receipt, response.Err = util.WaitForEvmTxReceipt(a.evmSigner.EvmClient, res.TxHash, 10*time.Second)
|
2023-03-02 01:05:53 +00:00
|
|
|
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// SignRawEvmData signs raw evm data with the SigningAccount's private key.
|
|
|
|
// It does not broadcast the signed data.
|
2024-06-24 21:55:40 +00:00
|
|
|
func (a *SigningAccount) SignRawEvmData(msg []byte) ([]byte, cryptotypes.PubKey, error) {
|
2023-04-06 17:51:13 +00:00
|
|
|
keyringSigner := emtests.NewSigner(a.evmPrivKey)
|
|
|
|
return keyringSigner.SignByAddress(a.SdkAddress, msg)
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
// NewFundedAccount creates a SigningAccount for a random account & funds the account from the whale.
|
|
|
|
func (chain *Chain) NewFundedAccount(name string, funds sdk.Coins) *SigningAccount {
|
2024-06-24 21:55:40 +00:00
|
|
|
mnemonic, err := util.RandomMnemonic()
|
|
|
|
require.NoError(chain.t, err)
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
acc := chain.AddNewSigningAccount(
|
2023-02-22 23:40:56 +00:00
|
|
|
name,
|
2024-05-01 05:53:58 +00:00
|
|
|
hd.CreateHDPath(chaincfg.Bip44CoinType, 0, 0),
|
2023-10-31 23:37:40 +00:00
|
|
|
chain.ChainID,
|
2023-02-22 23:40:56 +00:00
|
|
|
mnemonic,
|
|
|
|
)
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
// don't attempt to fund when no funds are desired
|
|
|
|
if funds.IsZero() {
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
whale := chain.GetAccount(FundedAccountName)
|
2023-08-02 21:52:48 +00:00
|
|
|
|
|
|
|
// check that the whale has the necessary balance to fund account
|
|
|
|
bal := chain.QuerySdkForBalances(whale.SdkAddress)
|
|
|
|
require.Truef(chain.t,
|
|
|
|
bal.IsAllGT(funds),
|
|
|
|
"funded account lacks funds for account %s\nneeds: %s\nhas: %s", name, funds, bal,
|
|
|
|
)
|
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
whale.l.Printf("attempting to fund created account (%s=%s)\n", name, acc.SdkAddress.String())
|
2023-06-30 05:30:02 +00:00
|
|
|
res := whale.BankSend(acc.SdkAddress, funds)
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
require.NoErrorf(chain.t, res.Err, "failed to fund new account %s: %s", name, res.Err)
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-02 01:05:53 +00:00
|
|
|
whale.l.Printf("successfully funded [%s]\n", name)
|
|
|
|
|
2023-02-22 23:40:56 +00:00
|
|
|
return acc
|
|
|
|
}
|
2023-06-06 16:40:17 +00:00
|
|
|
|
|
|
|
// GetNonce fetches the next nonce / sequence number for the account.
|
|
|
|
func (a *SigningAccount) NextNonce() (uint64, error) {
|
|
|
|
return a.evmSigner.EvmClient.PendingNonceAt(context.Background(), a.EvmAddress)
|
|
|
|
}
|
2023-06-30 05:30:02 +00:00
|
|
|
|
|
|
|
// BankSend is a helper method for sending funds via x/bank's MsgSend
|
2024-05-01 05:53:58 +00:00
|
|
|
func (a *SigningAccount) BankSend(to sdk.AccAddress, amount sdk.Coins) util.ZgChainMsgResponse {
|
|
|
|
return a.SignAndBroadcastZgChainTx(
|
|
|
|
util.ZgChainMsgRequest{
|
2023-06-30 05:30:02 +00:00
|
|
|
Msgs: []sdk.Msg{banktypes.NewMsgSend(a.SdkAddress, to, amount)},
|
|
|
|
GasLimit: 2e5, // 200,000 gas
|
2024-05-01 05:53:58 +00:00
|
|
|
FeeAmount: sdk.NewCoins(sdk.NewCoin(a.gasDenom, sdkmath.NewInt(200))), // assume min gas price of .001a0gi
|
2023-06-30 05:30:02 +00:00
|
|
|
Data: fmt.Sprintf("sending %s to %s", amount, to),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransferErc20 is a helper method for sending an erc20 token
|
|
|
|
func (a *SigningAccount) TransferErc20(contract, to common.Address, amount *big.Int) (EvmTxResponse, error) {
|
|
|
|
data := util.BuildErc20TransferCallData(to, amount)
|
|
|
|
nonce, err := a.NextNonce()
|
|
|
|
if err != nil {
|
|
|
|
return EvmTxResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := util.EvmTxRequest{
|
|
|
|
Tx: ethtypes.NewTransaction(nonce, contract, big.NewInt(0), 1e5, big.NewInt(1e10), data),
|
|
|
|
Data: fmt.Sprintf("fund %s with ERC20 balance (%s)", to.Hex(), amount.String()),
|
|
|
|
}
|
|
|
|
|
|
|
|
res := a.SignAndBroadcastEvmTx(req)
|
|
|
|
return res, res.Err
|
|
|
|
}
|