Skip to content

Commit

Permalink
Removed New Block notif commands from bss and bfg
Browse files Browse the repository at this point in the history
  • Loading branch information
AL-CT committed Jan 8, 2025
1 parent a93a0b2 commit 9c9bb12
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 299 deletions.
2 changes: 0 additions & 2 deletions api/bfgapi/bfgapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const (
CmdBTCFinalityByKeystonesRequest = "bfgapi-btc-finality-by-keystones-request"
CmdBTCFinalityByKeystonesResponse = "bfgapi-btc-finality-by-keystones-response"
CmdBTCFinalityNotification = "bfgapi-btc-finality-notification"
CmdBTCNewBlockNotification = "bfgapi-btc-new-block-notification"
CmdL2KeystonesNotification = "bfgapi-l2-keystones-notification"
CmdL2KeystonesRequest = "bfgapi-l2-keystones-request"
CmdL2KeystonesResponse = "bfgapi-l2-keystones-response"
Expand Down Expand Up @@ -183,7 +182,6 @@ var commands = map[protocol.Command]reflect.Type{
CmdBTCFinalityByKeystonesRequest: reflect.TypeOf(BTCFinalityByKeystonesRequest{}),
CmdBTCFinalityByKeystonesResponse: reflect.TypeOf(BTCFinalityByKeystonesResponse{}),
CmdBTCFinalityNotification: reflect.TypeOf(BTCFinalityNotification{}),
CmdBTCNewBlockNotification: reflect.TypeOf(BTCNewBlockNotification{}),
CmdL2KeystonesNotification: reflect.TypeOf(L2KeystonesNotification{}),
CmdL2KeystonesRequest: reflect.TypeOf(L2KeystonesRequest{}),
CmdL2KeystonesResponse: reflect.TypeOf(L2KeystonesResponse{}),
Expand Down
4 changes: 1 addition & 3 deletions api/bssapi/bssapi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand Down Expand Up @@ -110,7 +110,6 @@ const (
CmdBTCFinalityByKeystonesRequest protocol.Command = "bssapi-btc-finality-by-keystones-request"
CmdBTCFinalityByKeystonesResponse protocol.Command = "bssapi-btc-finality-by-keystones-response"
CmdBTCFinalityNotification protocol.Command = "bssapi-btc-finality-notification"
CmdBTCNewBlockNotification protocol.Command = "bssapi-btc-new-block-notification"
)

// commands contains the command key and type. This is used during RPC calls.
Expand All @@ -126,7 +125,6 @@ var commands = map[protocol.Command]reflect.Type{
CmdBTCFinalityByKeystonesRequest: reflect.TypeOf(BTCFinalityByKeystonesRequest{}),
CmdBTCFinalityByKeystonesResponse: reflect.TypeOf(BTCFinalityByKeystonesResponse{}),
CmdBTCFinalityNotification: reflect.TypeOf(BTCFinalityNotification{}),
CmdBTCNewBlockNotification: reflect.TypeOf(BTCNewBlockNotification{}),
}

// apiCmd is an empty structure used to satisfy the protocol.API interface.
Expand Down
283 changes: 0 additions & 283 deletions e2e/e2e_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3064,76 +3064,6 @@ func TestGetFinalitiesByL2KeystoneBFG(t *testing.T) {
}
}

// TestNotifyOnNewBtcBlockBFGClients tests that upon getting a new btc block,
// in this case from (mock) electrs, that a new btc block
// notification will be sent to all clients connected to BFG
// 1. connect client
// 2. wait for notification
func TestNotifyOnNewBtcBlockBFGClients(t *testing.T) {
db, pgUri, sdb, cleanup := createTestDB(context.Background(), t)
defer func() {
db.Close()
sdb.Close()
cleanup()
}()

ctx, cancel := defaultTestContext()
defer cancel()

l2Keystone := hemi.L2Keystone{
Version: 1,
L1BlockNumber: 5,
L2BlockNumber: 44,
ParentEPHash: fillOutBytes("parentephash", 32),
PrevKeystoneEPHash: fillOutBytes("prevkeystoneephash", 32),
StateRoot: fillOutBytes("stateroot", 32),
EPHash: fillOutBytes("ephash", 32),
}

btx := createBtcTx(t, 199, &l2Keystone, minerPrivateKeyBytes)

electrsAddr, cleanupE := createMockElectrsServer(ctx, t, &l2Keystone, btx)
defer cleanupE()
if err := EnsureCanConnectTCP(
t,
electrsAddr,
mockElectrsConnectTimeout,
); err != nil {
t.Fatal(err)
}

_, _, bfgWsurl, _ := createBfgServer(ctx, t, pgUri, electrsAddr, 1)

// 1
c, _, err := websocket.Dial(ctx, bfgWsurl, nil)
if err != nil {
t.Fatal(err)
}
defer c.CloseNow()

assertPing(ctx, t, c, bfgapi.CmdPingRequest)

// 2
retries := 2
found := false
for range retries {
// 2
var v protocol.Message
if err = wsjson.Read(ctx, c, &v); err != nil {
panic(fmt.Sprintf("error reading from ws: %s", err))
}

if v.Header.Command == bfgapi.CmdBTCNewBlockNotification {
found = true
break
}
}

if !found {
t.Fatalf("never received expected command: %s", bfgapi.CmdBTCNewBlockNotification)
}
}

// TestNotifyOnNewBtcFinalityBFGClients tests that upon getting a new btc block,
// in this case from (mock) electrs, that a finality notification will be sent
// to all clients connected to BFG
Expand Down Expand Up @@ -3480,76 +3410,6 @@ func TestOtherBFGSavesL2KeystonesOnNotifications(t *testing.T) {
wg.Wait()
}

// TestNotifyOnNewBtcBlockBSSClients tests that upon getting a new btc block,
// in this case from (mock) electrs, that a new btc notification
// will be sent to all clients connected to BSS
// 1. connect client
// 2. wait for notification
func TestNotifyOnNewBtcBlockBSSClients(t *testing.T) {
db, pgUri, sdb, cleanup := createTestDB(context.Background(), t)
defer func() {
db.Close()
sdb.Close()
cleanup()
}()

ctx, cancel := defaultTestContext()
defer cancel()

l2Keystone := hemi.L2Keystone{
Version: 1,
L1BlockNumber: 5,
L2BlockNumber: 44,
ParentEPHash: fillOutBytes("parentephash", 32),
PrevKeystoneEPHash: fillOutBytes("prevkeystoneephash", 32),
StateRoot: fillOutBytes("stateroot", 32),
EPHash: fillOutBytes("ephash", 32),
}

btx := createBtcTx(t, 199, &l2Keystone, minerPrivateKeyBytes)

electrsAddr, cleanupE := createMockElectrsServer(ctx, t, &l2Keystone, btx)
defer cleanupE()
if err := EnsureCanConnectTCP(
t,
electrsAddr,
mockElectrsConnectTimeout,
); err != nil {
t.Fatal(err)
}

_, _, bfgWsurl, _ := createBfgServer(ctx, t, pgUri, electrsAddr, 1)
_, _, bssWsurl := createBssServer(ctx, t, bfgWsurl)

// 1
c, _, err := websocket.Dial(ctx, bssWsurl, nil)
if err != nil {
t.Fatal(err)
}
defer c.CloseNow()

assertPing(ctx, t, c, bssapi.CmdPingRequest)

retries := 2
found := false
for range retries {
// 2
var v protocol.Message
if err = wsjson.Read(ctx, c, &v); err != nil {
panic(fmt.Sprintf("error reading from ws: %s", err))
}

if v.Header.Command == bssapi.CmdBTCNewBlockNotification {
found = true
break
}
}

if !found {
t.Fatalf("never received expected command: %s", bssapi.CmdBTCNewBlockNotification)
}
}

// TestNotifyOnNewBtcFinalityBSSClients tests that upon getting a new btc block,
// in this case from (mock) electrs, that a new finality notification
// will be sent to all clients connected to BSS
Expand Down Expand Up @@ -3620,149 +3480,6 @@ func TestNotifyOnNewBtcFinalityBSSClients(t *testing.T) {
}
}

func TestNotifyMultipleBFGClients(t *testing.T) {
db, pgUri, sdb, cleanup := createTestDB(context.Background(), t)
defer func() {
db.Close()
sdb.Close()
cleanup()
}()

ctx, cancel := defaultTestContext()
defer cancel()

l2Keystone := hemi.L2Keystone{
Version: 1,
L1BlockNumber: 5,
L2BlockNumber: 44,
ParentEPHash: fillOutBytes("parentephash", 32),
PrevKeystoneEPHash: fillOutBytes("prevkeystoneephash", 32),
StateRoot: fillOutBytes("stateroot", 32),
EPHash: fillOutBytes("ephash", 32),
}

btx := createBtcTx(t, 199, &l2Keystone, minerPrivateKeyBytes)

electrsAddr, cleanupE := createMockElectrsServer(ctx, t, &l2Keystone, btx)
defer cleanupE()
if err := EnsureCanConnectTCP(
t,
electrsAddr,
mockElectrsConnectTimeout,
); err != nil {
t.Fatal(err)
}

_, _, bfgWsurl, _ := createBfgServer(ctx, t, pgUri, electrsAddr, 1)

wg := sync.WaitGroup{}

for i := range 10 {
wg.Add(1)
go func() {
defer wg.Done()
c, _, err := websocket.Dial(ctx, bfgWsurl, nil)
if err != nil {
panic(err)
}

// ensure we can safely close 1 and handle the rest
if i == 5 {
c.CloseNow()
return
} else {
defer c.CloseNow()
}

assertPing(ctx, t, c, bfgapi.CmdPingRequest)

var v protocol.Message
if err = wsjson.Read(ctx, c, &v); err != nil {
panic(fmt.Sprintf("error reading from ws: %s", err))
}

if v.Header.Command != bfgapi.CmdBTCNewBlockNotification &&
v.Header.Command != bfgapi.CmdBTCFinalityNotification {
panic(fmt.Sprintf("wrong command: %s", v.Header.Command))
}
}()
}

wg.Wait()
}

func TestNotifyMultipleBSSClients(t *testing.T) {
db, pgUri, sdb, cleanup := createTestDB(context.Background(), t)
defer func() {
db.Close()
sdb.Close()
cleanup()
}()

ctx, cancel := defaultTestContext()
defer cancel()

l2Keystone := hemi.L2Keystone{
Version: 1,
L1BlockNumber: 5,
L2BlockNumber: 44,
ParentEPHash: fillOutBytes("parentephash", 32),
PrevKeystoneEPHash: fillOutBytes("prevkeystoneephash", 32),
StateRoot: fillOutBytes("stateroot", 32),
EPHash: fillOutBytes("ephash", 32),
}

btx := createBtcTx(t, 199, &l2Keystone, minerPrivateKeyBytes)

electrsAddr, cleanupE := createMockElectrsServer(ctx, t, &l2Keystone, btx)
defer cleanupE()
if err := EnsureCanConnectTCP(
t,
electrsAddr,
mockElectrsConnectTimeout,
); err != nil {
t.Fatal(err)
}

_, _, bfgWsurl, _ := createBfgServer(ctx, t, pgUri, electrsAddr, 1)
_, _, bssWsurl := createBssServer(ctx, t, bfgWsurl)

wg := sync.WaitGroup{}

for i := range 10 {
wg.Add(1)
go func() {
defer wg.Done()
c, _, err := websocket.Dial(ctx, bssWsurl, nil)
if err != nil {
panic(err)
}

// ensure we can safely close 1 and handle the rest
if i == 5 {
c.CloseNow()
return
} else {
defer c.CloseNow()
}

assertPing(ctx, t, c, bssapi.CmdPingRequest)

var v protocol.Message
if err = wsjson.Read(ctx, c, &v); err != nil {
panic(fmt.Sprintf("error reading from ws: %s", err))
}

if v.Header.Command != bssapi.CmdBTCNewBlockNotification &&
v.Header.Command != bssapi.CmdBTCFinalityNotification {
panic(fmt.Sprintf("wrong command: %s", v.Header.Command))
}
}()
}

wg.Wait()
}

func createBtcBlock(ctx context.Context, t *testing.T, db bfgd.Database, count int, height int, lastHash []byte, l2BlockNumber uint32) bfgd.BtcBlock {
header := make([]byte, 80)
hash := make([]byte, 32)
Expand Down
12 changes: 1 addition & 11 deletions service/bss/bss.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand Down Expand Up @@ -489,14 +489,6 @@ func (s *Server) handleBtcFinalityNotification() {
s.mtx.Unlock()
}

func (s *Server) handleBtcBlockNotification() {
s.mtx.Lock()
for _, bws := range s.sessions {
go writeNotificationResponse(bws, &bssapi.BTCNewBlockNotification{})
}
s.mtx.Unlock()
}

func handle(service string, mux *http.ServeMux, pattern string, handler func(http.ResponseWriter, *http.Request)) {
mux.HandleFunc(pattern, handler)
log.Infof("handle (%v): %v", service, pattern)
Expand Down Expand Up @@ -537,8 +529,6 @@ func (s *Server) handleBFGWebsocketReadUnauth(ctx context.Context, conn *protoco
}
case bfgapi.CmdBTCFinalityNotification:
go s.handleBtcFinalityNotification()
case bfgapi.CmdBTCNewBlockNotification:
go s.handleBtcBlockNotification()
default:
log.Errorf("unknown command: %v", cmd)
return
Expand Down

0 comments on commit 9c9bb12

Please sign in to comment.