Skip to content

Commit

Permalink
Fix silly reversals
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Jul 9, 2024
1 parent 96bb7ca commit a6ca0e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
12 changes: 8 additions & 4 deletions service/tbc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync"
"time"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"nhooyr.io/websocket"
Expand Down Expand Up @@ -357,14 +358,15 @@ func (s *Server) handleTxByIdRawRequest(ctx context.Context, req *tbcapi.TxByIdR
log.Tracef("handleTxByIdRawRequest")
defer log.Tracef("handleTxByIdRawRequest exit")

if len(req.TxId) != 32 {
txId, err := chainhash.NewHash(req.TxId)
if err != nil {
responseErr := protocol.RequestErrorf("invalid tx id")
return &tbcapi.TxByIdRawResponse{
Error: responseErr,
}, nil
}

tx, err := s.TxById(ctx, [32]byte(req.TxId))
tx, err := s.TxById(ctx, txId)

Check failure on line 369 in service/tbc/rpc.go

View workflow job for this annotation

GitHub Actions / Build (1.22.x)

cannot use txId (variable of type *chainhash.Hash) as tbcd.TxId value in argument to s.TxById

Check failure on line 369 in service/tbc/rpc.go

View workflow job for this annotation

GitHub Actions / Build (1.22.x)

cannot use txId (variable of type *chainhash.Hash) as tbcd.TxId value in argument to s.TxById
if err != nil {
if errors.Is(err, database.ErrNotFound) {
responseErr := protocol.RequestErrorf("tx not found: %s", req.TxId)
Expand Down Expand Up @@ -396,14 +398,15 @@ func (s *Server) handleTxByIdRequest(ctx context.Context, req *tbcapi.TxByIdRequ
log.Tracef("handleTxByIdRequest")
defer log.Tracef("handleTxByIdRequest exit")

if len(req.TxId) != 32 {
txId, err := chainhash.NewHash(req.TxId)
if err != nil {
responseErr := protocol.RequestErrorf("invalid tx id")
return &tbcapi.TxByIdResponse{
Error: responseErr,
}, nil
}

tx, err := s.TxById(ctx, [32]byte(reverseBytes(req.TxId)))
tx, err := s.TxById(ctx, txId)

Check failure on line 409 in service/tbc/rpc.go

View workflow job for this annotation

GitHub Actions / Build (1.22.x)

cannot use txId (variable of type *chainhash.Hash) as tbcd.TxId value in argument to s.TxById

Check failure on line 409 in service/tbc/rpc.go

View workflow job for this annotation

GitHub Actions / Build (1.22.x)

cannot use txId (variable of type *chainhash.Hash) as tbcd.TxId value in argument to s.TxById
if err != nil {
if errors.Is(err, database.ErrNotFound) {
responseErr := protocol.RequestErrorf("tx not found: %s", req.TxId)
Expand Down Expand Up @@ -563,6 +566,7 @@ func wireTxToTBC(w *wire.MsgTx) *tbcapi.Tx {
return tx
}

// XXX this probably should not exist, it means the code is busted instead
func reverseBytes(b []byte) []byte {
slices.Reverse(b)
return b
Expand Down
9 changes: 4 additions & 5 deletions service/tbc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/davecgh/go-spew/spew"
"github.com/docker/go-connections/nat"
"github.com/go-test/deep"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/hemilabs/heminetwork/api/protocol"
"github.com/hemilabs/heminetwork/api/tbcapi"
"github.com/hemilabs/heminetwork/bitcoin"
"github.com/hemilabs/heminetwork/database/tbcd"
)

func TestBlockHeadersByHeightRaw(t *testing.T) {
Expand Down Expand Up @@ -1417,14 +1417,13 @@ func TestTxById(t *testing.T) {
indexAll(ctx, t, tbcServer)

lastErr = nil
txId := getRandomTxId(ctx, t, bitcoindContainer)
txIdBytes, err := hex.DecodeString(txId)
txId, err := chainhash.NewHashFromStr(getRandomTxId(ctx, t, bitcoindContainer))
if err != nil {
t.Fatal(err)
}

err = tbcapi.Write(ctx, tws.conn, "someid", tbcapi.TxByIdRequest{
TxId: txIdBytes,
TxId: txId[:],
})
if err != nil {
lastErr = err
Expand All @@ -1447,7 +1446,7 @@ func TestTxById(t *testing.T) {
t.Fatal(response.Error.Message)
}

tx, err := tbcServer.TxById(ctx, tbcd.TxId(reverseBytes(txIdBytes)))
tx, err := tbcServer.TxById(ctx, txId)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit a6ca0e1

Please sign in to comment.