Skip to content

Commit

Permalink
take LoadDB() logic out from observer constructors; call LoadDB() onl…
Browse files Browse the repository at this point in the history
…y when activating new observers
  • Loading branch information
ws4charlie committed Jul 1, 2024
1 parent 0514cad commit 632ce52
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 124 deletions.
7 changes: 0 additions & 7 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func NewObserver(
appContext *context.AppContext,
zetacoreClient interfaces.ZetacoreClient,
tss interfaces.TSSSigner,
dbpath string,
logger base.Logger,
ts *metrics.TelemetryServer,
) (*Observer, error) {
Expand Down Expand Up @@ -155,12 +154,6 @@ func NewObserver(
},
}

// load btc chain observer DB
err = ob.LoadDB(dbpath)
if err != nil {
return nil, err
}

return ob, nil
}

Expand Down
25 changes: 5 additions & 20 deletions zetaclient/chains/bitcoin/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ func MockBTCObserver(
nil,
nil,
nil,
dbpath,
base.Logger{},
nil,
)
require.NoError(t, err)

// load db
err = ob.LoadDB(dbpath)
require.NoError(t, err)

return ob
}

Expand All @@ -116,7 +119,6 @@ func Test_NewObserver(t *testing.T) {
appContext *context.AppContext
coreClient interfaces.ZetacoreClient
tss interfaces.TSSSigner
dbpath string
logger base.Logger
ts *metrics.TelemetryServer
fail bool
Expand All @@ -130,7 +132,6 @@ func Test_NewObserver(t *testing.T) {
appContext: nil,
coreClient: nil,
tss: mocks.NewTSSMainnet(),
dbpath: sample.CreateTempDir(t),
logger: base.Logger{},
ts: nil,
fail: false,
Expand All @@ -143,26 +144,11 @@ func Test_NewObserver(t *testing.T) {
appContext: nil,
coreClient: nil,
tss: mocks.NewTSSMainnet(),
dbpath: sample.CreateTempDir(t),
logger: base.Logger{},
ts: nil,
fail: true,
message: "error getting net params",
},
{
name: "should fail on invalid dbpath",
chain: chain,
chainParams: params,
appContext: nil,
coreClient: nil,
btcClient: mocks.NewMockBTCRPCClient().WithBlockCount(100),
tss: mocks.NewTSSMainnet(),
dbpath: "/invalid/dbpath", // invalid dbpath
logger: base.Logger{},
ts: nil,
fail: true,
message: "error creating db path",
},
}

// run tests
Expand All @@ -176,7 +162,6 @@ func Test_NewObserver(t *testing.T) {
tt.appContext,
tt.coreClient,
tt.tss,
tt.dbpath,
tt.logger,
tt.ts,
)
Expand Down Expand Up @@ -254,7 +239,7 @@ func Test_LoadDB(t *testing.T) {

// create observer
dbpath := sample.CreateTempDir(t)
ob, err := observer.NewObserver(chain, btcClient, params, nil, nil, tss, dbpath, base.Logger{}, nil)
ob, err := observer.NewObserver(chain, btcClient, params, nil, nil, tss, base.Logger{}, nil)
require.NoError(t, err)

t.Run("should load db successfully", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/bitcoin/observer/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func MockBTCObserverMainnet(t *testing.T) *Observer {
tss := mocks.NewTSSMainnet()

// create Bitcoin observer
ob, err := NewObserver(chain, btcClient, params, nil, nil, tss, testutils.SQLiteMemory, base.Logger{}, nil)
ob, err := NewObserver(chain, btcClient, params, nil, nil, tss, base.Logger{}, nil)
require.NoError(t, err)

return ob
Expand Down
3 changes: 1 addition & 2 deletions zetaclient/chains/bitcoin/rpc/rpc_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func (suite *BitcoinObserverTestSuite) SetupTest() {
btcClient := mocks.NewMockBTCRPCClient()

// create observer
ob, err := observer.NewObserver(chain, btcClient, params, nil, nil, tss, testutils.SQLiteMemory,
base.DefaultLogger(), nil)
ob, err := observer.NewObserver(chain, btcClient, params, nil, nil, tss, base.DefaultLogger(), nil)
suite.Require().NoError(err)
suite.Require().NotNil(ob)
suite.rpcClient, err = getRPCClient(18332)
Expand Down
38 changes: 18 additions & 20 deletions zetaclient/chains/evm/observer/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Test_CheckAndVoteInboundTokenZeta(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenZeta(tx, receipt, false)
require.NoError(t, err)
require.Equal(t, cctx.InboundParams.BallotIndex, ballot)
Expand All @@ -57,7 +57,7 @@ func Test_CheckAndVoteInboundTokenZeta(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation - 1

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
_, err := ob.CheckAndVoteInboundTokenZeta(tx, receipt, false)
require.ErrorContains(t, err, "not been confirmed")
})
Expand All @@ -73,7 +73,7 @@ func Test_CheckAndVoteInboundTokenZeta(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenZeta(tx, receipt, true)
require.NoError(t, err)
require.Equal(t, "", ballot)
Expand All @@ -97,7 +97,6 @@ func Test_CheckAndVoteInboundTokenZeta(t *testing.T) {
nil,
nil,
nil,
memDBPath,
lastBlock,
mocks.MockChainParams(chainID, confirmation),
)
Expand Down Expand Up @@ -126,7 +125,7 @@ func Test_CheckAndVoteInboundTokenERC20(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenERC20(tx, receipt, false)
require.NoError(t, err)
require.Equal(t, cctx.InboundParams.BallotIndex, ballot)
Expand All @@ -142,7 +141,7 @@ func Test_CheckAndVoteInboundTokenERC20(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation - 1

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
_, err := ob.CheckAndVoteInboundTokenERC20(tx, receipt, false)
require.ErrorContains(t, err, "not been confirmed")
})
Expand All @@ -158,7 +157,7 @@ func Test_CheckAndVoteInboundTokenERC20(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenERC20(tx, receipt, true)
require.NoError(t, err)
require.Equal(t, "", ballot)
Expand All @@ -182,7 +181,6 @@ func Test_CheckAndVoteInboundTokenERC20(t *testing.T) {
nil,
nil,
nil,
memDBPath,
lastBlock,
mocks.MockChainParams(chainID, confirmation),
)
Expand Down Expand Up @@ -211,7 +209,7 @@ func Test_CheckAndVoteInboundTokenGas(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenGas(tx, receipt, false)
require.NoError(t, err)
require.Equal(t, cctx.InboundParams.BallotIndex, ballot)
Expand All @@ -221,7 +219,7 @@ func Test_CheckAndVoteInboundTokenGas(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation - 1

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
_, err := ob.CheckAndVoteInboundTokenGas(tx, receipt, false)
require.ErrorContains(t, err, "not been confirmed")
})
Expand All @@ -231,7 +229,7 @@ func Test_CheckAndVoteInboundTokenGas(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenGas(tx, receipt, false)
require.ErrorContains(t, err, "not TSS address")
require.Equal(t, "", ballot)
Expand All @@ -242,7 +240,7 @@ func Test_CheckAndVoteInboundTokenGas(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenGas(tx, receipt, false)
require.ErrorContains(t, err, "not a successful tx")
require.Equal(t, "", ballot)
Expand All @@ -253,7 +251,7 @@ func Test_CheckAndVoteInboundTokenGas(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(tx))
lastBlock := receipt.BlockNumber.Uint64() + confirmation

ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, lastBlock, chainParam)
ballot, err := ob.CheckAndVoteInboundTokenGas(tx, receipt, false)
require.NoError(t, err)
require.Equal(t, "", ballot)
Expand All @@ -270,7 +268,7 @@ func Test_BuildInboundVoteMsgForZetaSentEvent(t *testing.T) {
cctx := testutils.LoadCctxByInbound(t, chainID, coin.CoinType_Zeta, inboundHash)

// parse ZetaSent event
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, 1, mocks.MockChainParams(1, 1))
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, 1, mocks.MockChainParams(1, 1))
connector := mocks.MockConnectorNonEth(t, chainID)
event := testutils.ParseReceiptZetaSent(receipt, connector)

Expand Down Expand Up @@ -317,7 +315,7 @@ func Test_BuildInboundVoteMsgForDepositedEvent(t *testing.T) {
cctx := testutils.LoadCctxByInbound(t, chainID, coin.CoinType_ERC20, inboundHash)

// parse Deposited event
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, 1, mocks.MockChainParams(1, 1))
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, 1, mocks.MockChainParams(1, 1))
custody := mocks.MockERC20Custody(t, chainID)
event := testutils.ParseReceiptERC20Deposited(receipt, custody)
sender := ethcommon.HexToAddress(tx.From)
Expand Down Expand Up @@ -375,7 +373,7 @@ func Test_BuildInboundVoteMsgForTokenSentToTSS(t *testing.T) {
require.NoError(t, evm.ValidateEvmTransaction(txDonation))

// create test compliance config
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, memDBPath, 1, mocks.MockChainParams(1, 1))
ob := MockEVMObserver(t, chain, nil, nil, nil, nil, 1, mocks.MockChainParams(1, 1))
cfg := &config.Config{
ComplianceConfig: config.ComplianceConfig{},
}
Expand Down Expand Up @@ -445,7 +443,7 @@ func Test_ObserveTSSReceiveInBlock(t *testing.T) {
lastBlock := receipt.BlockNumber.Uint64() + confirmation

t.Run("should observe TSS receive in block", func(t *testing.T) {
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, lastBlock, chainParam)

// feed archived block and receipt
evmJSONRPC.WithBlock(block)
Expand All @@ -454,20 +452,20 @@ func Test_ObserveTSSReceiveInBlock(t *testing.T) {
require.NoError(t, err)
})
t.Run("should not observe on error getting block", func(t *testing.T) {
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, lastBlock, chainParam)
err := ob.ObserveTSSReceiveInBlock(blockNumber)
// error getting block is expected because the mock JSONRPC contains no block
require.ErrorContains(t, err, "error getting block")
})
t.Run("should not observe on error getting receipt", func(t *testing.T) {
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, lastBlock, chainParam)
evmJSONRPC.WithBlock(block)
err := ob.ObserveTSSReceiveInBlock(blockNumber)
// error getting block is expected because the mock evmClient contains no receipt
require.ErrorContains(t, err, "error getting receipt")
})
t.Run("should not observe on error posting vote", func(t *testing.T) {
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, memDBPath, lastBlock, chainParam)
ob := MockEVMObserver(t, chain, evmClient, evmJSONRPC, zetacoreClient, tss, lastBlock, chainParam)

// feed archived block and pause zetacore client
evmJSONRPC.WithBlock(block)
Expand Down
7 changes: 0 additions & 7 deletions zetaclient/chains/evm/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func NewObserver(
appContext *clientcontext.AppContext,
zetacoreClient interfaces.ZetacoreClient,
tss interfaces.TSSSigner,
dbpath string,
logger base.Logger,
ts *metrics.TelemetryServer,
) (*Observer, error) {
Expand Down Expand Up @@ -90,12 +89,6 @@ func NewObserver(
outboundConfirmedTransactions: make(map[string]*ethtypes.Transaction),
}

// open database and load data
err = ob.LoadDB(dbpath)
if err != nil {
return nil, err
}

return ob, nil
}

Expand Down
Loading

0 comments on commit 632ce52

Please sign in to comment.