Skip to content

Commit

Permalink
refactor: simply the IsSendOutTxProcessed func (#1989)
Browse files Browse the repository at this point in the history
* initial commit to simply IsSendOutTxProcessed func

* simplified IsSendOutTxProcessed() method and added unit tests

* fix unit test

* converted archived cctxs JSON files to GoLang variables; resolved a few PR comments

* fixed CI unit test failure

* added comment for the return values of function ParseAndCheckZetaEvent

---------

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
ws4charlie and lumtis authored Apr 16, 2024
1 parent 58a1b23 commit 62ec009
Show file tree
Hide file tree
Showing 49 changed files with 1,791 additions and 791 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* [1936](https://github.com/zeta-chain/node/pull/1936) - refactor common package into subpackages and rename to pkg
* [1966](https://github.com/zeta-chain/node/pull/1966) - move TSS vote message from crosschain to observer
* [1853](https://github.com/zeta-chain/node/pull/1853) - refactor vote inbound tx and vote outbound tx
* [1989](https://github.com/zeta-chain/node/pull/1989) - simplify `IsSendOutTxProcessed` method and add unit tests
* [2013](https://github.com/zeta-chain/node/pull/2013) - rename `GasPriceVoter` message to `VoteGasPrice`

### Features
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func DebugCmd() *cobra.Command {
ob := evm.ChainClient{
Mu: &sync.Mutex{},
}
ob.WithZetaClient(bridge)
ob.WithZetaBridge(bridge)
ob.WithLogger(chainLogger)
var ethRPC *ethrpc.EthRPC
var client *ethclient.Client
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func start(_ *cobra.Command, _ []string) error {

// Initialize core parameters from zetacore
appContext := appcontext.NewAppContext(corecontext.NewZetaCoreContext(cfg), cfg)
err = zetaBridge.UpdateZetaCoreContext(appContext.ZetaCoreContext(), true)
err = zetaBridge.UpdateZetaCoreContext(appContext.ZetaCoreContext(), true, startLogger)
if err != nil {
startLogger.Error().Err(err).Msg("Error getting core parameters")
return err
Expand Down
14 changes: 7 additions & 7 deletions zetaclient/bitcoin/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,15 @@ func (ob *BTCChainClient) ConfirmationsThreshold(amount *big.Int) int64 {
return int64(ob.GetChainParams().ConfirmationCount)
}

// IsSendOutTxProcessed returns isIncluded(or inMempool), isConfirmed, Error
func (ob *BTCChainClient) IsSendOutTxProcessed(cctx *types.CrossChainTx, logger zerolog.Logger) (bool, bool, error) {
// IsOutboundProcessed returns isIncluded(or inMempool), isConfirmed, Error
func (ob *BTCChainClient) IsOutboundProcessed(cctx *types.CrossChainTx, logger zerolog.Logger) (bool, bool, error) {
params := *cctx.GetCurrentOutTxParam()
sendHash := cctx.Index
nonce := cctx.GetCurrentOutTxParam().OutboundTxTssNonce

// get broadcasted outtx and tx result
outTxID := ob.GetTxID(nonce)
logger.Info().Msgf("IsSendOutTxProcessed %s", outTxID)
logger.Info().Msgf("IsOutboundProcessed %s", outTxID)

ob.Mu.Lock()
txnHash, broadcasted := ob.broadcastedTx[outTxID]
Expand All @@ -537,7 +537,7 @@ func (ob *BTCChainClient) IsSendOutTxProcessed(cctx *types.CrossChainTx, logger
if txResult == nil { // check failed, try again next time
return false, false, nil
} else if inMempool { // still in mempool (should avoid unnecessary Tss keysign)
ob.logger.OutTx.Info().Msgf("IsSendOutTxProcessed: outTx %s is still in mempool", outTxID)
ob.logger.OutTx.Info().Msgf("IsOutboundProcessed: outTx %s is still in mempool", outTxID)
return true, false, nil
}
// included
Expand All @@ -548,7 +548,7 @@ func (ob *BTCChainClient) IsSendOutTxProcessed(cctx *types.CrossChainTx, logger
if res == nil {
return false, false, nil
}
ob.logger.OutTx.Info().Msgf("IsSendOutTxProcessed: setIncludedTx succeeded for outTx %s", outTxID)
ob.logger.OutTx.Info().Msgf("IsOutboundProcessed: setIncludedTx succeeded for outTx %s", outTxID)
}

// It's safe to use cctx's amount to post confirmation because it has already been verified in observeOutTx()
Expand All @@ -573,9 +573,9 @@ func (ob *BTCChainClient) IsSendOutTxProcessed(cctx *types.CrossChainTx, logger
coin.CoinType_Gas,
)
if err != nil {
logger.Error().Err(err).Msgf("IsSendOutTxProcessed: error confirming bitcoin outTx %s, nonce %d ballot %s", res.TxID, nonce, ballot)
logger.Error().Err(err).Msgf("IsOutboundProcessed: error confirming bitcoin outTx %s, nonce %d ballot %s", res.TxID, nonce, ballot)
} else if zetaHash != "" {
logger.Info().Msgf("IsSendOutTxProcessed: confirmed Bitcoin outTx %s, zeta tx hash %s nonce %d ballot %s", res.TxID, zetaHash, nonce, ballot)
logger.Info().Msgf("IsOutboundProcessed: confirmed Bitcoin outTx %s, zeta tx hash %s nonce %d ballot %s", res.TxID, zetaHash, nonce, ballot)
}
return true, true, nil
}
Expand Down
17 changes: 8 additions & 9 deletions zetaclient/compliance/compliance_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package compliance

import (
"path"
"testing"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
)

func TestCctxRestricted(t *testing.T) {
// load archived cctx
var cctx crosschaintypes.CrossChainTx
testutils.LoadObjectFromJSONFile(t, &cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_1_6270.json"))
chain := chains.EthChain()
cctx := testutils.LoadCctxByNonce(t, chain.ChainId, 6270)

// create config
cfg := config.Config{
Expand All @@ -24,29 +23,29 @@ func TestCctxRestricted(t *testing.T) {
t.Run("should return true if sender is restricted", func(t *testing.T) {
cfg.ComplianceConfig.RestrictedAddresses = []string{cctx.InboundTxParams.Sender}
config.LoadComplianceConfig(cfg)
require.True(t, IsCctxRestricted(&cctx))
require.True(t, IsCctxRestricted(cctx))
})
t.Run("should return true if receiver is restricted", func(t *testing.T) {
cfg.ComplianceConfig.RestrictedAddresses = []string{cctx.GetCurrentOutTxParam().Receiver}
config.LoadComplianceConfig(cfg)
require.True(t, IsCctxRestricted(&cctx))
require.True(t, IsCctxRestricted(cctx))
})
t.Run("should return false if sender and receiver are not restricted", func(t *testing.T) {
// restrict other address
cfg.ComplianceConfig.RestrictedAddresses = []string{"0x27104b8dB4aEdDb054fCed87c346C0758Ff5dFB1"}
config.LoadComplianceConfig(cfg)
require.False(t, IsCctxRestricted(&cctx))
require.False(t, IsCctxRestricted(cctx))
})
t.Run("should be able to restrict coinbase address", func(t *testing.T) {
cfg.ComplianceConfig.RestrictedAddresses = []string{ethcommon.Address{}.String()}
config.LoadComplianceConfig(cfg)
cctx.InboundTxParams.Sender = ethcommon.Address{}.String()
require.True(t, IsCctxRestricted(&cctx))
require.True(t, IsCctxRestricted(cctx))
})
t.Run("should ignore empty address", func(t *testing.T) {
cfg.ComplianceConfig.RestrictedAddresses = []string{""}
config.LoadComplianceConfig(cfg)
cctx.InboundTxParams.Sender = ""
require.False(t, IsCctxRestricted(&cctx))
require.False(t, IsCctxRestricted(cctx))
})
}
Loading

0 comments on commit 62ec009

Please sign in to comment.