Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: sync from mockmain #1265

Merged
merged 15 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func start(_ *cobra.Command, _ []string) error {
err := telemetryServer.Start()
if err != nil {
startLogger.Error().Err(err).Msg("telemetryServer error")
panic("telemetryServer error")
}
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,91 @@ package keeper

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/zeta-chain/zetacore/common"
)

func (k Keeper) BlockOneDeploySystemContracts(_ context.Context) error {
func (k Keeper) BlockOneDeploySystemContracts(goCtx context.Context) error {
ctx := sdk.UnwrapSDKContext(goCtx)

// setup uniswap v2 factory
uniswapV2Factory, err := k.DeployUniswapV2Factory(ctx)
if err != nil {
return sdkerrors.Wrapf(err, "failed to DeployUniswapV2Factory")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute("UniswapV2Factory", uniswapV2Factory.String()),
),
)

// setup WZETA contract
wzeta, err := k.DeployWZETA(ctx)
if err != nil {
return sdkerrors.Wrapf(err, "failed to DeployWZetaContract")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute("DeployWZetaContract", wzeta.String()),
),
)

router, err := k.DeployUniswapV2Router02(ctx, uniswapV2Factory, wzeta)
if err != nil {
return sdkerrors.Wrapf(err, "failed to DeployUniswapV2Router02")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute("DeployUniswapV2Router02", router.String()),
),
)

connector, err := k.DeployConnectorZEVM(ctx, wzeta)
if err != nil {
return sdkerrors.Wrapf(err, "failed to DeployConnectorZEVM")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute("DeployConnectorZEVM", connector.String()),
),
)
ctx.Logger().Info("Deployed Connector ZEVM at " + connector.String())

SystemContractAddress, err := k.DeploySystemContract(ctx, wzeta, uniswapV2Factory, router)
if err != nil {
return sdkerrors.Wrapf(err, "failed to SystemContractAddress")
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage,
sdk.NewAttribute("SystemContractAddress", SystemContractAddress.String()),
),
)

// set the system contract
system, _ := k.GetSystemContract(ctx)
system.SystemContract = SystemContractAddress.String()
k.SetSystemContract(ctx, system)
//err = k.SetGasPrice(ctx, big.NewInt(1337), big.NewInt(1))
if err != nil {
return err
}
_, err = k.SetupChainGasCoinAndPool(ctx, common.EthChain().ChainId, "ETH", "ETH", 18)
if err != nil {
return errorsmod.Wrapf(err, fmt.Sprintf("failed to setupChainGasCoinAndPool for %s", common.EthChain().ChainName))
}

_, err = k.SetupChainGasCoinAndPool(ctx, common.BscMainnetChain().ChainId, "BNB", "BNB", 18)
if err != nil {
return errorsmod.Wrapf(err, fmt.Sprintf("failed to setupChainGasCoinAndPool for %s", common.BscMainnetChain().ChainName))
}
_, err = k.SetupChainGasCoinAndPool(ctx, common.BtcMainnetChain().ChainId, "BTC", "BTC", 8)
if err != nil {
return errorsmod.Wrapf(err, fmt.Sprintf("failed to setupChainGasCoinAndPool for %s", common.BtcMainnetChain().ChainName))
}
return nil
}
func (k Keeper) TestUpdateSystemContractAddress(_ context.Context) error {
Expand Down
42 changes: 21 additions & 21 deletions zetaclient/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,24 @@ func (ob *BitcoinChainClient) IsSendOutTxProcessed(sendHash string, nonce uint64
res, included := ob.includedTxResults[outTxID]
ob.mu.Unlock()

// Get original cctx parameters
params, err := ob.GetPendingCctxParams(nonce)
if err != nil {
ob.logger.ObserveOutTx.Info().Msgf("IsSendOutTxProcessed: can't find pending cctx for nonce %d", nonce)
return false, false, err
}

if !included {
if !broadcasted {
return false, false, nil
}
// Get original cctx parameters
params, err := ob.GetPendingCctxParams(nonce)
if err != nil {
ob.logger.ObserveOutTx.Info().Msgf("IsSendOutTxProcessed: can't find pending cctx for nonce %d", nonce)
return false, false, nil
// If the broadcasted outTx is nonce 0, just wait for inclusion and don't schedule more keysign
// Schedule more than one keysign for nonce 0 can lead to duplicate payments.
// One purpose of nonce mark UTXO is to avoid duplicate payment based on the fact that Bitcoin
// prevents double spending of same UTXO. However, for nonce 0, we don't have a prior nonce (e.g., -1)
// for the signer to check against when making the payment. Signer treats nonce 0 as a special case in downstream code.
if nonce == 0 {
return true, false, nil
}

// Try including this outTx broadcasted by myself
Expand All @@ -378,23 +387,13 @@ func (ob *BitcoinChainClient) IsSendOutTxProcessed(sendHash string, nonce uint64
ob.logger.ObserveOutTx.Info().Msgf("IsSendOutTxProcessed: checkNSaveIncludedTx succeeded for outTx %s", outTxID)
}

var amount float64
if res.Amount > 0 {
ob.logger.ObserveOutTx.Warn().Msg("IsSendOutTxProcessed: res.Amount > 0")
amount = res.Amount
} else if res.Amount == 0 {
ob.logger.ObserveOutTx.Error().Msg("IsSendOutTxProcessed: res.Amount == 0")
return false, false, nil
} else {
amount = -res.Amount
}

amountInSat, _ := big.NewFloat(amount * 1e8).Int(nil)
// It's safe to use cctx's amount to post confirmation because it has already been verified in observeOutTx()
amountInSat := params.Amount.BigInt()
if res.Confirmations < ob.ConfirmationsThreshold(amountInSat) {
return true, false, nil
}

logger.Debug().Msgf("Bitcoin outTx confirmed: txid %s, amount %f\n", res.TxID, res.Amount)
logger.Debug().Msgf("Bitcoin outTx confirmed: txid %s, amount %s\n", res.TxID, amountInSat.String())
zetaHash, err := ob.zetaClient.PostReceiveConfirmation(
sendHash,
res.TxID,
Expand Down Expand Up @@ -460,7 +459,10 @@ func (ob *BitcoinChainClient) PostGasPrice() error {
return fmt.Errorf("error getting gas price: %s", feeResult.Errors)
}
gasPrice := big.NewFloat(0)
gasPriceU64, _ := gasPrice.Mul(big.NewFloat(*feeResult.FeeRate), big.NewFloat(1e8)).Uint64()
// feerate from RPC is BTC/KB, convert it to satoshi/byte
// FIXME: in zetacore the gaslimit(vsize in BTC) is 100 which is too low for a typical outbound tx
// until we fix the gaslimit in BTC, we need to multiply the feerate by 20 to make sure the tx is confirmed
gasPriceU64, _ := gasPrice.Mul(big.NewFloat(*feeResult.FeeRate), big.NewFloat(20*1e5)).Uint64()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should bring this change to develop now that we can update gas limit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, we hold off merging to develop until we remove this;
we can remove this adjustment after MsgUpdate gas limit of bitcoin chain

bn, err := ob.rpcClient.GetBlockCount()
if err != nil {
return err
Expand All @@ -472,8 +474,6 @@ func (ob *BitcoinChainClient) PostGasPrice() error {
return err
}
_ = zetaHash
//ob.logger.WatchGasPrice.Debug().Msgf("PostGasPrice zeta tx: %s", zetaHash)
_ = feeResult
return nil
}

Expand Down
Loading