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

refactor: simply the IsSendOutTxProcessed func #1989

Merged
merged 10 commits into from
Apr 16, 2024
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 @@
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) {

Check warning on line 508 in zetaclient/bitcoin/bitcoin_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_client.go#L508

Added line #L508 was not covered by tests
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)

Check warning on line 515 in zetaclient/bitcoin/bitcoin_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_client.go#L515

Added line #L515 was not covered by tests

ob.Mu.Lock()
txnHash, broadcasted := ob.broadcastedTx[outTxID]
Expand All @@ -537,7 +537,7 @@
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)

Check warning on line 540 in zetaclient/bitcoin/bitcoin_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_client.go#L540

Added line #L540 was not covered by tests
return true, false, nil
}
// included
Expand All @@ -548,7 +548,7 @@
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)

Check warning on line 551 in zetaclient/bitcoin/bitcoin_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_client.go#L551

Added line #L551 was not covered by tests
}

// 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 @@
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)

Check warning on line 576 in zetaclient/bitcoin/bitcoin_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_client.go#L576

Added line #L576 was not covered by tests
} 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)

Check warning on line 578 in zetaclient/bitcoin/bitcoin_client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/bitcoin/bitcoin_client.go#L578

Added line #L578 was not covered by tests
}
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
Loading