Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonNorthey92 committed Aug 27, 2024
1 parent f30295d commit ea6575d
Showing 1 changed file with 75 additions and 79 deletions.
154 changes: 75 additions & 79 deletions service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,99 +319,99 @@ func (s *Server) handleBitcoinBalance(ctx context.Context, bbr *bfgapi.BitcoinBa
}, nil
}

func (s *Server) bitcoinBroadcastWorker(ctxI context.Context, highPriority bool) {
log.Tracef("bitcoinBroadcastWorker")
defer log.Tracef("bitcoinBroadcastWorker exit")
func (s *Server) handleOneBroadcastRequest(ctx context.Context, highPriority bool) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

defer s.wg.Done()
serializedTx, err := s.db.BtcTransactionBroadcastRequestGetNext(ctx, highPriority)
if err != nil {
log.Errorf("error getting next broadcast request: %v", err)
return
}

for {
// if there are no new serialized txs, backoff a bit
if serializedTx == nil {
select {
case <-ctxI.Done():
case <-time.After(5 * time.Second):
return
case <-ctx.Done():
return
default:
}
}

ctx, cancel := context.WithTimeout(ctxI, 5*time.Second)
rr := bytes.NewReader(serializedTx)
mb := wire.MsgTx{}
if err := mb.Deserialize(rr); err != nil {
log.Errorf("failed to deserialize tx: %v", err)
return
}

serializedTx, err := s.db.BtcTransactionBroadcastRequestGetNext(ctx, highPriority)
if err != nil {
log.Errorf("error getting next broadcast request: %v", err)
cancel()
continue
var tl2 *pop.TransactionL2
for _, v := range mb.TxOut {
tl2, err = pop.ParseTransactionL2FromOpReturn(v.PkScript)
if err == nil {
break // Found the pop transaction.
}
}

// if there are no new serialized txs, backoff a bit
if serializedTx == nil {
cancel()
select {
case <-time.After(5 * time.Second):
continue
}
}
if tl2 == nil {
log.Errorf("could not find pop tx")
return
}

rr := bytes.NewReader(serializedTx)
mb := wire.MsgTx{}
if err := mb.Deserialize(rr); err != nil {
log.Errorf("failed to deserialize tx: %v", err)
cancel()
continue
}
publicKeyUncompressed, err := pop.ParsePublicKeyFromSignatureScript(mb.TxIn[0].SignatureScript)
if err != nil {
log.Errorf("could not parse public key from signature script: %v", err)
return
}

var tl2 *pop.TransactionL2
for _, v := range mb.TxOut {
tl2, err = pop.ParseTransactionL2FromOpReturn(v.PkScript)
if err == nil {
break // Found the pop transaction.
}
}
hash := mb.TxHash()

if tl2 == nil {
log.Errorf("could not find pop tx")
cancel()
continue
}
if err := s.db.PopBasisInsertPopMFields(ctx, &bfgd.PopBasis{
BtcTxId: hash[:],
BtcRawTx: database.ByteArray(serializedTx),
PopMinerPublicKey: publicKeyUncompressed,
L2KeystoneAbrevHash: tl2.L2Keystone.Hash(),
}); err != nil {
log.Infof("inserting pop basis: %s", err)
}

publicKeyUncompressed, err := pop.ParsePublicKeyFromSignatureScript(mb.TxIn[0].SignatureScript)
_, err = s.btcClient.Broadcast(ctx, serializedTx)
if err != nil {
log.Errorf("broadcast tx: %s", err)
err = s.db.BtcTransactionBroadcastRequestSetLastError(ctx, mb.TxID(), err.Error())
if err != nil {
log.Errorf("could not parse public key from signature script: %v", err)
log.Errorf("could not delete %v", err)
}
return
}

hash := mb.TxHash()
s.metrics.popBroadcasts.Inc()

if err := s.db.PopBasisInsertPopMFields(ctx, &bfgd.PopBasis{
BtcTxId: hash[:],
BtcRawTx: database.ByteArray(serializedTx),
PopMinerPublicKey: publicKeyUncompressed,
L2KeystoneAbrevHash: tl2.L2Keystone.Hash(),
}); err != nil {
log.Infof("inserting pop basis: %s", err)
}
log.Tracef("hash is %s", hex.EncodeToString(hash[:]))

_, err = s.btcClient.Broadcast(ctx, serializedTx)
if err != nil {
log.Errorf("broadcast tx: %s", err)
err = s.db.BtcTransactionBroadcastRequestSetLastError(ctx, mb.TxID(), err.Error())
if err != nil {
log.Errorf("could not delete %v", err)
}
cancel()
continue
}
err = s.db.BtcTransactionBroadcastRequestConfirmBroadcast(ctx, mb.TxID())
if err != nil {
log.Errorf("could not confirm broadcast: %v", err)
return
}

s.metrics.popBroadcasts.Inc()
log.Infof("successfully broadcast tx %s, for l2 keystone %s", mb.TxID(), hex.EncodeToString(tl2.L2Keystone.Hash()))
}

log.Infof("hash is %s", hex.EncodeToString(hash[:]))
func (s *Server) bitcoinBroadcastWorker(ctx context.Context, highPriority bool) {
log.Tracef("bitcoinBroadcastWorker")
defer log.Tracef("bitcoinBroadcastWorker exit")

err = s.db.BtcTransactionBroadcastRequestConfirmBroadcast(ctx, mb.TxID())
if err != nil {
log.Errorf("could not confirm broadcast: %v", err)
cancel()
continue
}
defer s.wg.Done()

log.Infof("successfully broadcast tx %s, for l2 keystone %s", mb.TxID(), hex.EncodeToString(tl2.L2Keystone.Hash()))
cancel()
for {
select {
case <-ctx.Done():
return
default:
s.handleOneBroadcastRequest(ctx, highPriority)
}
}
}

Expand Down Expand Up @@ -1282,14 +1282,8 @@ func (s *Server) getL2KeystonesCache() []hemi.L2Keystone {
s.mtx.Lock()
defer s.mtx.Unlock()

if s.l2keystonesCache == nil {
return []hemi.L2Keystone{}
}

results := []hemi.L2Keystone{}
for _, v := range s.l2keystonesCache {
results = append(results, v)
}
results := make([]hemi.L2Keystone, len(s.l2keystonesCache))
copy(results, s.l2keystonesCache)

return results
}
Expand All @@ -1304,6 +1298,7 @@ func (s *Server) refreshL2KeystoneCache(ctx context.Context) {
results, err := s.db.L2KeystonesMostRecentN(ctx, 100)
if err != nil {
log.Errorf("error getting keystones %v", err)
return
}

l2Keystones := make([]hemi.L2Keystone, 0, len(results))
Expand Down Expand Up @@ -1429,6 +1424,7 @@ func (s *Server) handleNewL2Keystones(ctx context.Context, nlkr *bfgapi.NewL2Key
err := s.db.L2KeystonesInsert(ctx, ks)
if err != nil {
log.Errorf("error saving keystone %v", err)
return
}

s.refreshL2KeystoneCache(ctx)
Expand Down

0 comments on commit ea6575d

Please sign in to comment.