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(zetaclient): extract base observer and base signer from BTC #3394

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 8 additions & 3 deletions cmd/zetaclientd/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,21 @@ func InboundGetBallot(_ *cobra.Command, args []string) error {
return errors.Wrap(err, "unable to open database")
}

observer, err := btcobserver.NewObserver(
baseObserver, err := base.NewObserver(
*chain.RawChain(),
rpcClient,
*chain.Params(),
client,
nil,
100,
nil,
database,
baseLogger,
nil,
)
if err != nil {
return errors.Wrap(err, "unable to create base observer")
}

observer, err := btcobserver.New(*chain.RawChain(), baseObserver, rpcClient)
if err != nil {
return errors.Wrap(err, "unable to create btc observer")
}
Expand Down
35 changes: 2 additions & 33 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ import (
"github.com/rs/zerolog"

"github.com/zeta-chain/node/pkg/chains"
observertypes "github.com/zeta-chain/node/x/observer/types"
"github.com/zeta-chain/node/zetaclient/chains/base"
"github.com/zeta-chain/node/zetaclient/chains/bitcoin/common"
"github.com/zeta-chain/node/zetaclient/chains/interfaces"
"github.com/zeta-chain/node/zetaclient/db"
"github.com/zeta-chain/node/zetaclient/metrics"
clienttypes "github.com/zeta-chain/node/zetaclient/types"
)

Expand Down Expand Up @@ -63,9 +59,6 @@ type RPC interface {
}

const (
// btcBlocksPerDay represents Bitcoin blocks per days for LRU block cache size
btcBlocksPerDay = 144

// RegnetStartBlock is the hardcoded start block for regnet
RegnetStartBlock = 100

Expand Down Expand Up @@ -125,32 +118,8 @@ type Observer struct {
logger Logger
}

// NewObserver returns a new Bitcoin chain observer
func NewObserver(
chain chains.Chain,
rpc RPC,
chainParams observertypes.ChainParams,
zetacoreClient interfaces.ZetacoreClient,
tss interfaces.TSSSigner,
database *db.DB,
logger base.Logger,
ts *metrics.TelemetryServer,
) (*Observer, error) {
// create base observer
baseObserver, err := base.NewObserver(
chain,
chainParams,
zetacoreClient,
tss,
btcBlocksPerDay,
ts,
database,
logger,
)
if err != nil {
return nil, errors.Wrapf(err, "unable to create base observer")
}

// New BTC Observer constructor.
func New(chain chains.Chain, baseObserver *base.Observer, rpc RPC) (*Observer, error) {
// get the bitcoin network params
netParams, err := chains.BitcoinNetParamsFromChainID(chain.ChainId)
if err != nil {
Expand Down
19 changes: 12 additions & 7 deletions zetaclient/chains/bitcoin/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,20 @@ func Test_NewObserver(t *testing.T) {
defer tt.after()
}

// create observer
ob, err := observer.NewObserver(
baseObserver, err := base.NewObserver(
tt.chain,
tt.btcClient,
tt.chainParams,
tt.coreClient,
tt.tss,
100,
tt.ts,
database,
tt.logger,
tt.ts,
)
require.NoError(t, err)

// create observer
ob, err := observer.New(tt.chain, baseObserver, tt.btcClient)
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
if tt.errorMessage != "" {
require.ErrorContains(t, err, tt.errorMessage)
require.Nil(t, ob)
Expand Down Expand Up @@ -324,18 +326,21 @@ func newTestSuite(t *testing.T, chain chains.Chain) *testSuite {

log := zerolog.New(zerolog.NewTestWriter(t))

ob, err := observer.NewObserver(
baseObserver, err := base.NewObserver(
chain,
client,
chainParams,
zetacore,
nil,
100,
nil,
database,
base.Logger{Std: log, Compliance: log},
nil,
)
require.NoError(t, err)

ob, err := observer.New(chain, baseObserver, client)
require.NoError(t, err)

return &testSuite{
ctx: ctx,
Observer: ob,
Expand Down
5 changes: 4 additions & 1 deletion zetaclient/chains/bitcoin/observer/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ func MockBTCObserverMainnet(t *testing.T, tss interfaces.TSSSigner) *Observer {
logger := zerolog.New(zerolog.NewTestWriter(t))
baseLogger := base.Logger{Std: logger, Compliance: logger}

baseObserver, err := base.NewObserver(chain, params, nil, tss, 100, nil, database, baseLogger)
require.NoError(t, err)

// create Bitcoin observer
ob, err := NewObserver(chain, btcClient, params, nil, tss, database, baseLogger, nil)
ob, err := New(chain, baseObserver, btcClient)
require.NoError(t, err)

return ob
Expand Down
7 changes: 2 additions & 5 deletions zetaclient/chains/bitcoin/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ type Signer struct {
}

// New creates a new Bitcoin signer
func New(chain chains.Chain, tss interfaces.TSSSigner, rpc RPC, logger base.Logger) *Signer {
return &Signer{
Signer: base.NewSigner(chain, tss, logger),
rpc: rpc,
}
func New(baseSigner *base.Signer, rpc RPC) *Signer {
return &Signer{Signer: baseSigner, rpc: rpc}
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
}

// AddWithdrawTxOutputs adds the 3 outputs to the withdraw tx
Expand Down
14 changes: 6 additions & 8 deletions zetaclient/chains/bitcoin/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@ func (s *BTCSignerSuite) SetUpTest(c *C) {

tss := mocks.NewTSSFromPrivateKey(c, privateKey)

s.btcSigner = New(
chains.Chain{},
tss,
mocks.NewBitcoinClient(cWrapper{c}),
base.DefaultLogger(),
)
baseSigner := base.NewSigner(chains.Chain{}, tss, base.DefaultLogger())

s.btcSigner = New(baseSigner, mocks.NewBitcoinClient(cWrapper{c}))
}

func (s *BTCSignerSuite) TestP2PH(c *C) {
Expand Down Expand Up @@ -229,13 +226,14 @@ func (s *BTCSignerSuite) TestP2WPH(c *C) {

func TestAddWithdrawTxOutputs(t *testing.T) {
// Create test signer and receiver address
signer := New(
baseSigner := base.NewSigner(
chains.BitcoinMainnet,
mocks.NewTSS(t).FakePubKey(testutils.TSSPubKeyMainnet),
mocks.NewBitcoinClient(t),
base.DefaultLogger(),
)

signer := New(baseSigner, mocks.NewBitcoinClient(t))

// tss address and script
tssAddr, err := signer.TSS().PubKey().AddressBTC(chains.BitcoinMainnet.ChainId)
require.NoError(t, err)
Expand Down
49 changes: 35 additions & 14 deletions zetaclient/orchestrator/v2_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"github.com/pkg/errors"

"github.com/zeta-chain/node/zetaclient/chains/base"
"github.com/zeta-chain/node/zetaclient/chains/bitcoin"
"github.com/zeta-chain/node/zetaclient/chains/bitcoin/client"
btcobserver "github.com/zeta-chain/node/zetaclient/chains/bitcoin/observer"
Expand All @@ -13,6 +14,8 @@
"github.com/zeta-chain/node/zetaclient/db"
)

const btcBlocksPerDay = 144

func (oc *V2) bootstrapBitcoin(ctx context.Context, chain zctx.Chain) (*bitcoin.Bitcoin, error) {
// should not happen
if !chain.IsBitcoin() {
Expand All @@ -34,37 +37,55 @@
return nil, errors.Wrap(err, "unable to create rpc client")
}

var (
rawChain = chain.RawChain()
dbName = btcDatabaseFileName(*rawChain)
)

baseObserver, err := oc.newBaseObserver(chain, dbName)
if err != nil {
return nil, errors.Wrap(err, "unable to create base observer")
}

Check warning on line 48 in zetaclient/orchestrator/v2_bootstrap.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/orchestrator/v2_bootstrap.go#L47-L48

Added lines #L47 - L48 were not covered by tests

observer, err := btcobserver.New(*rawChain, baseObserver, rpcClient)
if err != nil {
return nil, errors.Wrap(err, "unable to create observer")
}

Check warning on line 53 in zetaclient/orchestrator/v2_bootstrap.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/orchestrator/v2_bootstrap.go#L52-L53

Added lines #L52 - L53 were not covered by tests

baseSigner := oc.newBaseSigner(chain)
signer := btcsigner.New(baseSigner, rpcClient)

return bitcoin.New(oc.scheduler, observer, signer), nil
}

func (oc *V2) newBaseObserver(chain zctx.Chain, dbName string) (*base.Observer, error) {
var (
rawChain = chain.RawChain()
rawChainParams = chain.Params()
)

dbName := btcDatabaseFileName(*rawChain)

database, err := db.NewFromSqlite(oc.deps.DBPath, dbName, true)
if err != nil {
return nil, errors.Wrapf(err, "unable to open database %s", dbName)
}

// TODO extract base observer
// TODO extract base signer
// https://github.com/zeta-chain/node/issues/3331
blocksCacheSize := base.DefaultBlockCacheSize
if chain.IsBitcoin() {
blocksCacheSize = btcBlocksPerDay
}

observer, err := btcobserver.NewObserver(
return base.NewObserver(
*rawChain,
rpcClient,
*rawChainParams,
oc.deps.Zetacore,
oc.deps.TSS,
blocksCacheSize,
oc.deps.Telemetry,
database,
oc.logger.base,
oc.deps.Telemetry,
)
if err != nil {
return nil, errors.Wrap(err, "unable to create observer")
}

signer := btcsigner.New(*rawChain, oc.deps.TSS, rpcClient, oc.logger.base)
}
swift1337 marked this conversation as resolved.
Show resolved Hide resolved

return bitcoin.New(oc.scheduler, observer, signer), nil
func (oc *V2) newBaseSigner(chain zctx.Chain) *base.Signer {
return base.NewSigner(*chain.RawChain(), oc.deps.TSS, oc.logger.base)
}
Loading