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: integrated base signer structure into existing EVM/BTC signers #2357

Merged
merged 22 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
61f7f02
save local new files to remote
ws4charlie Jun 4, 2024
695caa7
initiated base observer
ws4charlie Jun 4, 2024
1500138
move base to chains folder
ws4charlie Jun 4, 2024
2cdff62
moved logger to base package
ws4charlie Jun 5, 2024
d2d19f1
added base signer and logger
ws4charlie Jun 6, 2024
7b358d8
Merge branch 'develop' of https://github.com/zeta-chain/node into ref…
ws4charlie Jun 6, 2024
9da97f9
Merge branch 'develop' into refactor-base-signer-observer
ws4charlie Jun 17, 2024
f6784ee
added changelog entry
ws4charlie Jun 17, 2024
bad53bb
Merge branch 'develop' into refactor-base-signer-observer
ws4charlie Jun 17, 2024
13e6251
integrated base signer into evm/bitcoin; integrated base observer int…
ws4charlie Jun 19, 2024
b51ab8a
integrated base observer to evm and bitcoin chain
ws4charlie Jun 20, 2024
5edd452
Merge branch 'develop' of https://github.com/zeta-chain/node into ref…
ws4charlie Jun 20, 2024
7d1650b
added changelog entry
ws4charlie Jun 20, 2024
0cb55e1
cherry pick base Signer structure integration
ws4charlie Jun 20, 2024
2b4b01c
updated PR number in changelog
ws4charlie Jun 20, 2024
d882b05
updated PR number in changelog
ws4charlie Jun 20, 2024
921ca67
Merge branch 'develop' into refactor-integrate-base-signer
ws4charlie Jun 20, 2024
e87114f
Update changelog.md
ws4charlie Jun 21, 2024
e585bc0
move Mutex to base signer; improve log print and unit test
ws4charlie Jun 21, 2024
d171a44
Merge branch 'develop' of https://github.com/zeta-chain/node into ref…
ws4charlie Jun 21, 2024
6a5bb47
fix code format
ws4charlie Jun 21, 2024
8500a77
Merge branch 'develop' into refactor-integrate-base-signer
ws4charlie Jun 21, 2024
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 changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* [2296](https://github.com/zeta-chain/node/pull/2296) - move `testdata` package to `testutil` to organize test-related utilities
* [2344](https://github.com/zeta-chain/node/pull/2344) - group common data of EVM/Bitcoin signer and observer using base structs
* [2317](https://github.com/zeta-chain/node/pull/2317) - add ValidateOutbound method for cctx orchestrator
* [2357](https://github.com/zeta-chain/node/pull/2357) - integrated base Signer structure into EVM/Bitcoin Signer
ws4charlie marked this conversation as resolved.
Show resolved Hide resolved

### Tests

Expand Down
5 changes: 0 additions & 5 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"sync"
Expand All @@ -13,7 +12,6 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/zeta-chain/zetacore/pkg/chains"
Expand Down Expand Up @@ -61,7 +59,6 @@ func DebugCmd() *cobra.Command {
}
inboundHash := args[0]
var ballotIdentifier string
chainLogger := zerolog.New(io.Discard).Level(zerolog.Disabled)

// create a new zetacore client
client, err := zetacore.NewClient(
Expand Down Expand Up @@ -93,7 +90,6 @@ func DebugCmd() *cobra.Command {
Mu: &sync.Mutex{},
}
evmObserver.WithZetacoreClient(client)
evmObserver.WithLogger(chainLogger)
var ethRPC *ethrpc.EthRPC
var client *ethclient.Client
coinType := coin.CoinType_Cmd
Expand Down Expand Up @@ -172,7 +168,6 @@ func DebugCmd() *cobra.Command {
Mu: &sync.Mutex{},
}
btcObserver.WithZetacoreClient(client)
btcObserver.WithLogger(chainLogger)
btcObserver.WithChain(*chains.GetChainFromChainID(chainID))
connCfg := &rpcclient.ConnConfig{
Host: cfg.BitcoinConfig.RPCHost,
Expand Down
17 changes: 9 additions & 8 deletions cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,22 @@ func CreateZetacoreClient(
return client, nil
}

// CreateSignerMap creates a map of ChainSigners for all chains in the config
func CreateSignerMap(
appContext *context.AppContext,
tss interfaces.TSSSigner,
logger base.Logger,
ts *metrics.TelemetryServer,
) (map[int64]interfaces.ChainSigner, error) {
coreContext := appContext.ZetacoreContext()
zetacoreContext := appContext.ZetacoreContext()
signerMap := make(map[int64]interfaces.ChainSigner)

// EVM signers
for _, evmConfig := range appContext.Config().GetAllEVMConfigs() {
if evmConfig.Chain.IsZetaChain() {
continue
}
evmChainParams, found := coreContext.GetEVMChainParams(evmConfig.Chain.ChainId)
evmChainParams, found := zetacoreContext.GetEVMChainParams(evmConfig.Chain.ChainId)
if !found {
logger.Std.Error().Msgf("ChainParam not found for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -77,15 +78,15 @@ func CreateSignerMap(
erc20CustodyAddress := ethcommon.HexToAddress(evmChainParams.Erc20CustodyContractAddress)
signer, err := evmsigner.NewSigner(
evmConfig.Chain,
evmConfig.Endpoint,
zetacoreContext,
tss,
ts,
logger,
evmConfig.Endpoint,
config.GetConnectorABI(),
config.GetERC20CustodyABI(),
mpiAddress,
erc20CustodyAddress,
coreContext,
logger,
ts)
erc20CustodyAddress)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewEVMSigner error for chain %s", evmConfig.Chain.String())
continue
Expand All @@ -95,7 +96,7 @@ func CreateSignerMap(
// BTC signer
btcChain, btcConfig, enabled := appContext.GetBTCChainAndConfig()
if enabled {
signer, err := btcsigner.NewSigner(btcConfig, tss, logger, ts, coreContext)
signer, err := btcsigner.NewSigner(btcChain, zetacoreContext, tss, ts, logger, btcConfig)
if err != nil {
logger.Std.Error().Err(err).Msgf("NewBTCSigner error for chain %s", btcChain.String())
} else {
Expand Down
9 changes: 5 additions & 4 deletions zetaclient/chains/base/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/zeta-chain/zetacore/zetaclient/chains/base"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
)

func TestInitLogger(t *testing.T) {
Expand All @@ -23,7 +24,7 @@ func TestInitLogger(t *testing.T) {
LogFormat: "json",
LogLevel: 1, // zerolog.InfoLevel,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: testutils.CreateTempDir(t),
},
},
fail: false,
Expand All @@ -34,7 +35,7 @@ func TestInitLogger(t *testing.T) {
LogFormat: "text",
LogLevel: 2, // zerolog.WarnLevel,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: testutils.CreateTempDir(t),
},
},
fail: false,
Expand All @@ -45,7 +46,7 @@ func TestInitLogger(t *testing.T) {
LogFormat: "unknown",
LogLevel: 3, // zerolog.ErrorLevel,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: testutils.CreateTempDir(t),
},
},
fail: false,
Expand All @@ -57,7 +58,7 @@ func TestInitLogger(t *testing.T) {
LogLevel: 4, // zerolog.DebugLevel,
LogSampler: true,
ComplianceConfig: config.ComplianceConfig{
LogPath: createTempDir(t),
LogPath: testutils.CreateTempDir(t),
},
},
},
Expand Down
78 changes: 39 additions & 39 deletions zetaclient/chains/bitcoin/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
Expand All @@ -30,7 +29,6 @@
"github.com/zeta-chain/zetacore/zetaclient/context"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/outboundprocessor"
"github.com/zeta-chain/zetacore/zetaclient/tss"
)

const (
Expand All @@ -45,20 +43,25 @@

// Signer deals with signing BTC transactions and implements the ChainSigner interface
type Signer struct {
tssSigner interfaces.TSSSigner
rpcClient interfaces.BTCRPCClient
logger zerolog.Logger
loggerCompliance zerolog.Logger
ts *metrics.TelemetryServer
coreContext *context.ZetacoreContext
// base.Signer implements the base chain signer
base.Signer

// client is the RPC client to interact with the Bitcoin chain
client interfaces.BTCRPCClient
}

// NewSigner creates a new Bitcoin signer
func NewSigner(
cfg config.BTCConfig,
tssSigner interfaces.TSSSigner,
logger base.Logger,
chain chains.Chain,
zetacoreContext *context.ZetacoreContext,
tss interfaces.TSSSigner,
ts *metrics.TelemetryServer,
coreContext *context.ZetacoreContext) (*Signer, error) {
logger base.Logger,
cfg config.BTCConfig) (*Signer, error) {
// create base signer
baseSigner := base.NewSigner(chain, zetacoreContext, tss, ts, logger)

// create the bitcoin rpc client using the provided config
connCfg := &rpcclient.ConnConfig{
Host: cfg.RPCHost,
User: cfg.RPCUsername,
Expand All @@ -73,12 +76,8 @@
}

return &Signer{
tssSigner: tssSigner,
rpcClient: client,
logger: logger.Std.With().Str("chain", "BTC").Str("module", "BTCSigner").Logger(),
loggerCompliance: logger.Compliance,
ts: ts,
coreContext: coreContext,
Signer: *baseSigner,
client: client,
}, nil
}

Expand Down Expand Up @@ -130,12 +129,12 @@
if remainingSats < 0 {
return fmt.Errorf("remainder value is negative: %d", remainingSats)
} else if remainingSats == nonceMark {
signer.logger.Info().Msgf("adjust remainder value to avoid duplicate nonce-mark: %d", remainingSats)
signer.Logger().Std.Info().Msgf("adjust remainder value to avoid duplicate nonce-mark: %d", remainingSats)
remainingSats--
}

// 1st output: the nonce-mark btc to TSS self
tssAddrP2WPKH := signer.tssSigner.BTCAddressWitnessPubkeyHash()
tssAddrP2WPKH := signer.TSS().BTCAddressWitnessPubkeyHash()
payToSelfScript, err := bitcoin.PayToAddrScript(tssAddrP2WPKH)
if err != nil {
return err
Expand Down Expand Up @@ -182,7 +181,10 @@
// refresh unspent UTXOs and continue with keysign regardless of error
err := observer.FetchUTXOs()
if err != nil {
signer.logger.Error().Err(err).Msgf("SignWithdrawTx: FetchUTXOs error: nonce %d chain %d", nonce, chain.ChainId)
signer.Logger().
Std.Error().
Err(err).
Msgf("SignWithdrawTx: FetchUTXOs error: nonce %d chain %d", nonce, chain.ChainId)

Check warning on line 187 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L184-L187

Added lines #L184 - L187 were not covered by tests
}

// select N UTXOs to cover the total expense
Expand Down Expand Up @@ -216,25 +218,27 @@
return nil, err
}
if sizeLimit < bitcoin.BtcOutboundBytesWithdrawer { // ZRC20 'withdraw' charged less fee from end user
signer.logger.Info().
signer.Logger().Std.Info().

Check warning on line 221 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L221

Added line #L221 was not covered by tests
Msgf("sizeLimit %d is less than BtcOutboundBytesWithdrawer %d for nonce %d", sizeLimit, txSize, nonce)
}
if txSize < bitcoin.OutboundBytesMin { // outbound shouldn't be blocked a low sizeLimit
signer.logger.Warn().
signer.Logger().Std.Warn().

Check warning on line 225 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L225

Added line #L225 was not covered by tests
Msgf("txSize %d is less than outboundBytesMin %d; use outboundBytesMin", txSize, bitcoin.OutboundBytesMin)
txSize = bitcoin.OutboundBytesMin
}
if txSize > bitcoin.OutboundBytesMax { // in case of accident
signer.logger.Warn().
signer.Logger().Std.Warn().

Check warning on line 230 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L230

Added line #L230 was not covered by tests
Msgf("txSize %d is greater than outboundBytesMax %d; use outboundBytesMax", txSize, bitcoin.OutboundBytesMax)
txSize = bitcoin.OutboundBytesMax
}

// fee calculation
// #nosec G701 always in range (checked above)
fees := new(big.Int).Mul(big.NewInt(int64(txSize)), gasPrice)
signer.logger.Info().Msgf("bitcoin outbound nonce %d gasPrice %s size %d fees %s consolidated %d utxos of value %v",
nonce, gasPrice.String(), txSize, fees.String(), consolidatedUtxo, consolidatedValue)
signer.Logger().
Std.Info().
Msgf("bitcoin outbound nonce %d gasPrice %s size %d fees %s consolidated %d utxos of value %v",
nonce, gasPrice.String(), txSize, fees.String(), consolidatedUtxo, consolidatedValue)

Check warning on line 241 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L238-L241

Added lines #L238 - L241 were not covered by tests

// add tx outputs
err = signer.AddWithdrawTxOutputs(tx, to, total, amount, nonceMark, fees, cancelTx)
Expand All @@ -260,11 +264,7 @@
}
}

tssSigner, ok := signer.tssSigner.(*tss.TSS)
if !ok {
return nil, fmt.Errorf("tssSigner is not a TSS")
}
sig65Bs, err := tssSigner.SignBatch(witnessHashes, height, nonce, &chain)
sig65Bs, err := signer.TSS().SignBatch(witnessHashes, height, nonce, chain.ChainId)

Check warning on line 267 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L267

Added line #L267 was not covered by tests
if err != nil {
return nil, fmt.Errorf("SignBatch error: %v", err)
}
Expand All @@ -278,7 +278,7 @@
S: S,
}

pkCompressed := signer.tssSigner.PubKeyCompressedBytes()
pkCompressed := signer.TSS().PubKeyCompressedBytes()

Check warning on line 281 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L281

Added line #L281 was not covered by tests
hashType := txscript.SigHashAll
txWitness := wire.TxWitness{append(sig.Serialize(), byte(hashType)), pkCompressed}
tx.TxIn[ix].Witness = txWitness
Expand All @@ -298,12 +298,12 @@
str := hex.EncodeToString(outBuff.Bytes())
fmt.Printf("BTCSigner: Transaction Data: %s\n", str)

hash, err := signer.rpcClient.SendRawTransaction(signedTx, true)
hash, err := signer.client.SendRawTransaction(signedTx, true)

Check warning on line 301 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L301

Added line #L301 was not covered by tests
if err != nil {
return err
}

signer.logger.Info().Msgf("Broadcasting BTC tx , hash %s ", hash)
signer.Logger().Std.Info().Msgf("Broadcasting BTC tx , hash %s ", hash)

Check warning on line 306 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L306

Added line #L306 was not covered by tests
return nil
}

Expand All @@ -318,11 +318,11 @@
defer func() {
outboundProcessor.EndTryProcess(outboundID)
if err := recover(); err != nil {
signer.logger.Error().Msgf("BTC TryProcessOutbound: %s, caught panic error: %v", cctx.Index, err)
signer.Logger().Std.Error().Msgf("BTC TryProcessOutbound: %s, caught panic error: %v", cctx.Index, err)

Check warning on line 321 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L321

Added line #L321 was not covered by tests
}
}()

logger := signer.logger.With().
logger := signer.Logger().Std.With().

Check warning on line 325 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L325

Added line #L325 was not covered by tests
Str("OutboundID", outboundID).
Str("SendHash", cctx.Index).
Logger()
Expand All @@ -341,7 +341,7 @@
logger.Error().Msgf("chain observer is not a bitcoin observer")
return
}
flags := signer.coreContext.GetCrossChainFlags()
flags := signer.ZetacoreContext().GetCrossChainFlags()

Check warning on line 344 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L344

Added line #L344 was not covered by tests
if !flags.IsOutboundEnabled {
logger.Info().Msgf("outbound is disabled")
return
Expand Down Expand Up @@ -375,7 +375,7 @@
amount := float64(params.Amount.Uint64()) / 1e8

// Add 1 satoshi/byte to gasPrice to avoid minRelayTxFee issue
networkInfo, err := signer.rpcClient.GetNetworkInfo()
networkInfo, err := signer.client.GetNetworkInfo()

Check warning on line 378 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L378

Added line #L378 was not covered by tests
if err != nil {
logger.Error().Err(err).Msgf("cannot get bitcoin network info")
return
Expand All @@ -386,7 +386,7 @@
// compliance check
cancelTx := compliance.IsCctxRestricted(cctx)
if cancelTx {
compliance.PrintComplianceLog(logger, signer.loggerCompliance,
compliance.PrintComplianceLog(logger, signer.Logger().Compliance,

Check warning on line 389 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L389

Added line #L389 was not covered by tests
true, chain.ChainId, cctx.Index, cctx.InboundParams.Sender, params.Receiver, "BTC")
amount = 0.0 // zero out the amount to cancel the tx
}
Expand Down
3 changes: 1 addition & 2 deletions zetaclient/chains/bitcoin/signer/signer_keysign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/btcsuite/btcutil"
"github.com/stretchr/testify/suite"

"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/zetaclient/chains/bitcoin"
"github.com/zeta-chain/zetacore/zetaclient/chains/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/testutils/mocks"
Expand Down Expand Up @@ -147,7 +146,7 @@ func getTSSTX(
return "", err
}

sig65B, err := tss.Sign(witnessHash, 10, 10, &chains.Chain{}, "")
sig65B, err := tss.Sign(witnessHash, 10, 10, 0, "")
R := big.NewInt(0).SetBytes(sig65B[:32])
S := big.NewInt(0).SetBytes(sig65B[32:64])
sig := btcec.Signature{
Expand Down
Loading
Loading