Skip to content

Commit

Permalink
Replace block metadata topics with ListenToBlockMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Apr 23, 2024
1 parent 06040e8 commit 1f68a84
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 76 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/iotaledger/hive.go/log v0.0.0-20240419094509-31dbb7270ad9
github.com/iotaledger/hive.go/runtime v0.0.0-20240419094509-31dbb7270ad9
github.com/iotaledger/hive.go/web v0.0.0-20240216135101-261e99d9d84a
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240419103152-aa12c9f5bd66
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240419095729-912f1c2df45d
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240423111221-4248ffa5d850
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240423105148-cc9e62fe4f63
github.com/iotaledger/iota.go/v4 v4.0.0-20240419095144-054bd7d2ba61
github.com/labstack/echo/v4 v4.11.4
github.com/mochi-mqtt/server/v2 v2.4.6
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ github.com/iotaledger/hive.go/stringify v0.0.0-20240419094509-31dbb7270ad9 h1:NJ
github.com/iotaledger/hive.go/stringify v0.0.0-20240419094509-31dbb7270ad9/go.mod h1:O4p7UmsfoeLqtAUwrKbq0lXMxjY/MLQSpZSavvvvGig=
github.com/iotaledger/hive.go/web v0.0.0-20240216135101-261e99d9d84a h1:aqMFY9c+VMevIB/Hc3Om9OZsXViNAbl4xrF8yTiYn0I=
github.com/iotaledger/hive.go/web v0.0.0-20240216135101-261e99d9d84a/go.mod h1:qDs633y8GLKk9HegRrvuD2edmyCbbkgiluqrG6ko0yo=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240419103152-aa12c9f5bd66 h1:28gH76448EjukxCz1H0OIbM0Yeoq0HP2jk4+v1tDcWQ=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240419103152-aa12c9f5bd66/go.mod h1:aQWBB1p5CLWKFWBTXB6TwSGZu3piuNHTjhWYyE3H22I=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240419095729-912f1c2df45d h1:aTLIfyVtJHLMKgYEUY0tPNBv+B522JZbttH1DslX2ck=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240419095729-912f1c2df45d/go.mod h1:YYko1kTtJgfETXQqWHgJkHQv6gGYGDxjnwDC6FbXxic=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240423111221-4248ffa5d850 h1:PBFs3UuwpCdd7jqHozVx2/UMJCQ6fwZeIzkedv1bum4=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240423111221-4248ffa5d850/go.mod h1:kk+TNI0FkHRkSHuLXMkAmnbdxZjmizZgVo1vE2fXXJ8=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240423105148-cc9e62fe4f63 h1:vt8LvpthPv2iVgIDzHN0N3Gee5+KEmqm/3eeF5G6hyA=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240423105148-cc9e62fe4f63/go.mod h1:YYko1kTtJgfETXQqWHgJkHQv6gGYGDxjnwDC6FbXxic=
github.com/iotaledger/iota.go/v4 v4.0.0-20240419095144-054bd7d2ba61 h1:vC1YXh2b8WleeAJvqf76PtBDvOXNIaI2Xdn0eLi2YFU=
github.com/iotaledger/iota.go/v4 v4.0.0-20240419095144-054bd7d2ba61/go.mod h1:ui1VcUuBHzN4eO0VR89UKqkiYR443VsznFMJgY2YRUQ=
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
Expand Down
12 changes: 10 additions & 2 deletions pkg/mqtt/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ func (s *Server) listenToBlocks(ctx context.Context) error {
}

func (s *Server) listenToAcceptedBlocksMetadata(ctx context.Context) error {
return s.NodeBridge.ListenToAcceptedBlocks(ctx, func(blockMetadata *api.BlockMetadataResponse) error {
return s.NodeBridge.ListenToBlockMetadata(ctx, func(blockMetadata *api.BlockMetadataResponse) error {
if blockMetadata.BlockState != api.BlockStateAccepted {
return nil
}

if err := s.publishBlockMetadataOnTopicsIfSubscribed(func() (*api.BlockMetadataResponse, error) { return blockMetadata, nil },
api.EventAPITopicBlockMetadataAccepted,
GetTopicBlockMetadata(blockMetadata.BlockID),
Expand All @@ -422,7 +426,11 @@ func (s *Server) listenToAcceptedBlocksMetadata(ctx context.Context) error {
}

func (s *Server) listenToConfirmedBlocksMetadata(ctx context.Context) error {
return s.NodeBridge.ListenToConfirmedBlocks(ctx, func(blockMetadata *api.BlockMetadataResponse) error {
return s.NodeBridge.ListenToBlockMetadata(ctx, func(blockMetadata *api.BlockMetadataResponse) error {
if blockMetadata.BlockState != api.BlockStateConfirmed {
return nil
}

if err := s.publishBlockMetadataOnTopicsIfSubscribed(func() (*api.BlockMetadataResponse, error) { return blockMetadata, nil },
api.EventAPITopicBlockMetadataConfirmed,
GetTopicBlockMetadata(blockMetadata.BlockID),
Expand Down
8 changes: 4 additions & 4 deletions pkg/testsuite/mqtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func TestMqttTopics(t *testing.T) {
ts.MockAddBlockMetadata(blockMetadataResponse.BlockID, blockMetadataResponse)
},
postSubscribeFunc: func() {
ts.ReceiveAcceptedBlock(lo.PanicOnErr(inx.WrapBlockMetadata(blockMetadataResponse)))
ts.ReceiveBlockMetadata(inx.WrapBlockMetadata(blockMetadataResponse))
},
}
}(),
Expand All @@ -402,7 +402,7 @@ func TestMqttTopics(t *testing.T) {
jsonTarget: lo.PanicOnErr(ts.API().JSONEncode(blockMetadataResponse)),
rawTarget: lo.PanicOnErr(ts.API().Encode(blockMetadataResponse)),
postSubscribeFunc: func() {
ts.ReceiveAcceptedBlock(lo.PanicOnErr(inx.WrapBlockMetadata(blockMetadataResponse)))
ts.ReceiveBlockMetadata(inx.WrapBlockMetadata(blockMetadataResponse))
},
}
}(),
Expand All @@ -411,7 +411,7 @@ func TestMqttTopics(t *testing.T) {
func() *test {
blockMetadataResponse := &api.BlockMetadataResponse{
BlockID: tpkg.RandBlockID(),
BlockState: api.BlockStateAccepted,
BlockState: api.BlockStateConfirmed,
}

return &test{
Expand All @@ -429,7 +429,7 @@ func TestMqttTopics(t *testing.T) {
jsonTarget: lo.PanicOnErr(ts.API().JSONEncode(blockMetadataResponse)),
rawTarget: lo.PanicOnErr(ts.API().Encode(blockMetadataResponse)),
postSubscribeFunc: func() {
ts.ReceiveConfirmedBlock(lo.PanicOnErr(inx.WrapBlockMetadata(blockMetadataResponse)))
ts.ReceiveBlockMetadata(inx.WrapBlockMetadata(blockMetadataResponse))
},
}
}(),
Expand Down
59 changes: 13 additions & 46 deletions pkg/testsuite/nodebridge_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ type MockedNodeBridge struct {
mockedOutputs map[iotago.OutputID]*nodebridge.Output

mockedStreamListenToBlocks *MockedStream[MockedBlock]
mockedStreamListenToAcceptedBlocks *MockedStream[inx.BlockMetadata]
mockedStreamListenToConfirmedBlocks *MockedStream[inx.BlockMetadata]
mockedStreamListenToBlockMetadata *MockedStream[inx.BlockMetadata]
mockedStreamListenToCommitments *MockedStream[MockedCommitment]
mockedStreamListenToLedgerUpdates *MockedStream[nodebridge.LedgerUpdate]
mockedStreamListenToAcceptedTransactions *MockedStream[nodebridge.AcceptedTransaction]
Expand Down Expand Up @@ -164,38 +163,15 @@ func (m *MockedNodeBridge) ListenToBlocks(ctx context.Context, consumer func(blo
return nil
}

func (m *MockedNodeBridge) ListenToAcceptedBlocks(ctx context.Context, consumer func(blockMetadata *api.BlockMetadataResponse) error) error {
if m.mockedStreamListenToAcceptedBlocks == nil {
require.FailNow(m.t, "ListenToAcceptedBlocks mock not initialized")
func (m *MockedNodeBridge) ListenToBlockMetadata(ctx context.Context, consumer func(blockMetadata *api.BlockMetadataResponse) error) error {
if m.mockedStreamListenToBlockMetadata == nil {
require.FailNow(m.t, "ListenToBlockMetadata mock not initialized")
}

err := nodebridge.ListenToStream(ctx, m.mockedStreamListenToAcceptedBlocks.receiverFunc(), func(inxBlockMetadata *inx.BlockMetadata) error {
blockMetadata, err := inxBlockMetadata.Unwrap()
if err != nil {
return err
}

return consumer(blockMetadata)
})
require.NoError(m.t, err, "ListenToAcceptedBlocks failed")

return nil
}

func (m *MockedNodeBridge) ListenToConfirmedBlocks(ctx context.Context, consumer func(blockMetadata *api.BlockMetadataResponse) error) error {
if m.mockedStreamListenToConfirmedBlocks == nil {
require.FailNow(m.t, "ListenToConfirmedBlocks mock not initialized")
}

err := nodebridge.ListenToStream(ctx, m.mockedStreamListenToConfirmedBlocks.receiverFunc(), func(inxBlockMetadata *inx.BlockMetadata) error {
blockMetadata, err := inxBlockMetadata.Unwrap()
if err != nil {
return err
}

return consumer(blockMetadata)
err := nodebridge.ListenToStream(ctx, m.mockedStreamListenToBlockMetadata.receiverFunc(), func(inxBlockMetadata *inx.BlockMetadata) error {
return consumer(inxBlockMetadata.Unwrap())
})
require.NoError(m.t, err, "ListenToConfirmedBlocks failed")
require.NoError(m.t, err, "ListenToBlockMetadata failed")

return nil
}
Expand Down Expand Up @@ -304,13 +280,9 @@ func (m *MockedNodeBridge) MockClear() {
m.mockedStreamListenToBlocks.Close()
m.mockedStreamListenToBlocks = nil
}
if m.mockedStreamListenToAcceptedBlocks != nil {
m.mockedStreamListenToAcceptedBlocks.Close()
m.mockedStreamListenToAcceptedBlocks = nil
}
if m.mockedStreamListenToConfirmedBlocks != nil {
m.mockedStreamListenToConfirmedBlocks.Close()
m.mockedStreamListenToConfirmedBlocks = nil
if m.mockedStreamListenToBlockMetadata != nil {
m.mockedStreamListenToBlockMetadata.Close()
m.mockedStreamListenToBlockMetadata = nil
}
if m.mockedStreamListenToCommitments != nil {
m.mockedStreamListenToCommitments.Close()
Expand Down Expand Up @@ -376,14 +348,9 @@ func (m *MockedNodeBridge) MockListenToBlocks() *MockedStream[MockedBlock] {
return m.mockedStreamListenToBlocks
}

func (m *MockedNodeBridge) MockListenToAcceptedBlocks() *MockedStream[inx.BlockMetadata] {
m.mockedStreamListenToAcceptedBlocks = InitMockedStream[inx.BlockMetadata]()
return m.mockedStreamListenToAcceptedBlocks
}

func (m *MockedNodeBridge) MockListenToConfirmedBlocks() *MockedStream[inx.BlockMetadata] {
m.mockedStreamListenToConfirmedBlocks = InitMockedStream[inx.BlockMetadata]()
return m.mockedStreamListenToConfirmedBlocks
func (m *MockedNodeBridge) MockListenToBlockMetadata() *MockedStream[inx.BlockMetadata] {
m.mockedStreamListenToBlockMetadata = InitMockedStream[inx.BlockMetadata]()
return m.mockedStreamListenToBlockMetadata
}

func (m *MockedNodeBridge) MockListenToCommitments() *MockedStream[MockedCommitment] {
Expand Down
17 changes: 5 additions & 12 deletions pkg/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type TestSuite struct {
server *mqtt.Server

mockedStreamListenToBlocks *MockedStream[MockedBlock]
mockedStreamListenToAcceptedBlocks *MockedStream[inx.BlockMetadata]
mockedStreamListenToConfirmedBlocks *MockedStream[inx.BlockMetadata]
mockedStreamListenToBlockMetadata *MockedStream[inx.BlockMetadata]
mockedStreamListenToCommitments *MockedStream[MockedCommitment]
mockedStreamListenToLedgerUpdates *MockedStream[nodebridge.LedgerUpdate]
mockedStreamListenToAcceptedTransactions *MockedStream[nodebridge.AcceptedTransaction]
Expand Down Expand Up @@ -61,8 +60,7 @@ func NewTestSuite(t *testing.T) *TestSuite {
server: server,

mockedStreamListenToBlocks: bridge.MockListenToBlocks(),
mockedStreamListenToAcceptedBlocks: bridge.MockListenToAcceptedBlocks(),
mockedStreamListenToConfirmedBlocks: bridge.MockListenToConfirmedBlocks(),
mockedStreamListenToBlockMetadata: bridge.MockListenToBlockMetadata(),
mockedStreamListenToCommitments: bridge.MockListenToCommitments(),
mockedStreamListenToLedgerUpdates: bridge.MockListenToLedgerUpdates(),
mockedStreamListenToAcceptedTransactions: bridge.MockListenToAcceptedTransactions(),
Expand Down Expand Up @@ -90,8 +88,7 @@ func (ts *TestSuite) Reset() {
ts.broker.MockClear()

ts.mockedStreamListenToBlocks = ts.nodeBridge.MockListenToBlocks()
ts.mockedStreamListenToAcceptedBlocks = ts.nodeBridge.MockListenToAcceptedBlocks()
ts.mockedStreamListenToConfirmedBlocks = ts.nodeBridge.MockListenToConfirmedBlocks()
ts.mockedStreamListenToBlockMetadata = ts.nodeBridge.MockListenToBlockMetadata()
ts.mockedStreamListenToCommitments = ts.nodeBridge.MockListenToCommitments()
ts.mockedStreamListenToLedgerUpdates = ts.nodeBridge.MockListenToLedgerUpdates()
ts.mockedStreamListenToAcceptedTransactions = ts.nodeBridge.MockListenToAcceptedTransactions()
Expand Down Expand Up @@ -137,12 +134,8 @@ func (ts *TestSuite) ReceiveBlock(block *MockedBlock) {
ts.mockedStreamListenToBlocks.Receive(block)
}

func (ts *TestSuite) ReceiveAcceptedBlock(metadata *inx.BlockMetadata) {
ts.mockedStreamListenToAcceptedBlocks.Receive(metadata)
}

func (ts *TestSuite) ReceiveConfirmedBlock(metadata *inx.BlockMetadata) {
ts.mockedStreamListenToConfirmedBlocks.Receive(metadata)
func (ts *TestSuite) ReceiveBlockMetadata(metadata *inx.BlockMetadata) {
ts.mockedStreamListenToBlockMetadata.Receive(metadata)
}

func (ts *TestSuite) ReceiveCommitment(commitment *MockedCommitment) {
Expand Down
4 changes: 2 additions & 2 deletions tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ require (
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240419094509-31dbb7270ad9 // indirect
github.com/iotaledger/hive.go/stringify v0.0.0-20240419094509-31dbb7270ad9 // indirect
github.com/iotaledger/hive.go/web v0.0.0-20240216135101-261e99d9d84a // indirect
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240419103152-aa12c9f5bd66 // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240419095729-912f1c2df45d // indirect
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240423111221-4248ffa5d850 // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240423105148-cc9e62fe4f63 // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20240419095144-054bd7d2ba61 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions tools/gendoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ github.com/iotaledger/hive.go/stringify v0.0.0-20240419094509-31dbb7270ad9 h1:NJ
github.com/iotaledger/hive.go/stringify v0.0.0-20240419094509-31dbb7270ad9/go.mod h1:O4p7UmsfoeLqtAUwrKbq0lXMxjY/MLQSpZSavvvvGig=
github.com/iotaledger/hive.go/web v0.0.0-20240216135101-261e99d9d84a h1:aqMFY9c+VMevIB/Hc3Om9OZsXViNAbl4xrF8yTiYn0I=
github.com/iotaledger/hive.go/web v0.0.0-20240216135101-261e99d9d84a/go.mod h1:qDs633y8GLKk9HegRrvuD2edmyCbbkgiluqrG6ko0yo=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240419103152-aa12c9f5bd66 h1:28gH76448EjukxCz1H0OIbM0Yeoq0HP2jk4+v1tDcWQ=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240419103152-aa12c9f5bd66/go.mod h1:aQWBB1p5CLWKFWBTXB6TwSGZu3piuNHTjhWYyE3H22I=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240419095729-912f1c2df45d h1:aTLIfyVtJHLMKgYEUY0tPNBv+B522JZbttH1DslX2ck=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240419095729-912f1c2df45d/go.mod h1:YYko1kTtJgfETXQqWHgJkHQv6gGYGDxjnwDC6FbXxic=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240423111221-4248ffa5d850 h1:PBFs3UuwpCdd7jqHozVx2/UMJCQ6fwZeIzkedv1bum4=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240423111221-4248ffa5d850/go.mod h1:kk+TNI0FkHRkSHuLXMkAmnbdxZjmizZgVo1vE2fXXJ8=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240423105148-cc9e62fe4f63 h1:vt8LvpthPv2iVgIDzHN0N3Gee5+KEmqm/3eeF5G6hyA=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240423105148-cc9e62fe4f63/go.mod h1:YYko1kTtJgfETXQqWHgJkHQv6gGYGDxjnwDC6FbXxic=
github.com/iotaledger/iota.go/v4 v4.0.0-20240419095144-054bd7d2ba61 h1:vC1YXh2b8WleeAJvqf76PtBDvOXNIaI2Xdn0eLi2YFU=
github.com/iotaledger/iota.go/v4 v4.0.0-20240419095144-054bd7d2ba61/go.mod h1:ui1VcUuBHzN4eO0VR89UKqkiYR443VsznFMJgY2YRUQ=
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
Expand Down

0 comments on commit 1f68a84

Please sign in to comment.