Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

feat(BUX-418/166): default miners, retrieve bump (merkle paths) from arc #62

Merged
merged 4 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions broadcast/internal/arc/arc_query_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"net/http"

"github.com/libsv/go-bc"

"github.com/bitcoin-sv/go-broadcast-client/broadcast"
arcutils "github.com/bitcoin-sv/go-broadcast-client/broadcast/internal/arc/utils"
"github.com/bitcoin-sv/go-broadcast-client/httpclient"
Expand Down Expand Up @@ -68,21 +66,11 @@ func decodeQueryResponseBody(resp *http.Response, arc *ArcClient) (*broadcast.Qu
return nil, err
}

var merklePath *bc.MerklePath

if base.MerklePath != "" {
merklePath, err = bc.NewMerklePathFromStr(base.MerklePath)
if err != nil {
return nil, broadcast.ErrUnableToDecodeMerklePath
}
}

model := &broadcast.QueryTxResponse{
BaseResponse: broadcast.BaseResponse{
Miner: arc.apiURL,
},
BaseTxResponse: base,
MerklePath: merklePath,
}

return model, nil
Expand Down
7 changes: 2 additions & 5 deletions broadcast/internal/arc/arc_query_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"context"
"errors"
"fmt"
"github.com/rs/zerolog"
"io"
"net/http"
"testing"

"github.com/libsv/go-bc"
"github.com/rs/zerolog"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

Expand Down Expand Up @@ -95,7 +95,6 @@ func TestQueryTransaction(t *testing.T) {
}

func TestDecodeQueryResponseBody(t *testing.T) {
mp, _ := bc.NewMerklePathFromStr(fixtures.TxMerklePath)
testCases := []struct {
name string
httpResponse *http.Response
Expand All @@ -112,7 +111,6 @@ func TestDecodeQueryResponseBody(t *testing.T) {
BaseTxResponse: broadcast.BaseTxResponse{
MerklePath: fixtures.TxMerklePath,
},
MerklePath: mp,
},
},
{
Expand All @@ -129,7 +127,6 @@ func TestDecodeQueryResponseBody(t *testing.T) {
BaseResponse: broadcast.BaseResponse{
Miner: "http://example.com",
},
MerklePath: nil,
},
},
}
Expand Down
138 changes: 4 additions & 134 deletions broadcast/internal/arc/arc_submit_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package arc

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"

"github.com/libsv/go-bc"
"github.com/libsv/go-bt/v2"

"github.com/bitcoin-sv/go-broadcast-client/broadcast"
arc_utils "github.com/bitcoin-sv/go-broadcast-client/broadcast/internal/arc/utils"
"github.com/bitcoin-sv/go-broadcast-client/httpclient"
Expand Down Expand Up @@ -155,9 +150,8 @@ func submitBatchTransactions(ctx context.Context, arc *ArcClient, txs []*broadca
}

func createSubmitTxBody(arc *ArcClient, tx *broadcast.Transaction, txFormat broadcast.TransactionFormat) ([]byte, error) {
body, err := formatTxRequest(arc, tx, txFormat)
if err != nil {
return nil, err
body := &SubmitTxRequest{
RawTx: tx.Hex,
}

data, err := json.Marshal(body)
Expand All @@ -171,9 +165,8 @@ func createSubmitTxBody(arc *ArcClient, tx *broadcast.Transaction, txFormat broa
func createSubmitBatchTxsBody(arc *ArcClient, txs []*broadcast.Transaction, txFormat broadcast.TransactionFormat) ([]byte, error) {
rawTxs := make([]*SubmitTxRequest, 0, len(txs))
for _, tx := range txs {
requestTx, err := formatTxRequest(arc, tx, txFormat)
if err != nil {
return nil, err
requestTx := &SubmitTxRequest{
RawTx: tx.Hex,
}
rawTxs = append(rawTxs, requestTx)
}
Expand Down Expand Up @@ -215,17 +208,8 @@ func decodeSubmitResponseBody(resp *http.Response) (*broadcast.SubmittedTx, erro
return nil, broadcast.ErrUnableToDecodeMerklePath
}

var merklePath *bc.MerklePath

if base.MerklePath != "" {
if merklePath, err = bc.NewMerklePathFromStr(base.MerklePath); err != nil {
return nil, broadcast.ErrUnableToDecodeMerklePath
}
}

model := &broadcast.SubmittedTx{
BaseSubmitTxResponse: base,
MerklePath: merklePath,
}

return model, nil
Expand All @@ -240,15 +224,8 @@ func decodeSubmitBatchResponseBody(resp *http.Response) ([]*broadcast.SubmittedT

model := make([]*broadcast.SubmittedTx, 0)
for _, tx := range base {
var merklePath *bc.MerklePath
if tx.MerklePath != "" {
if merklePath, err = bc.NewMerklePathFromStr(tx.MerklePath); err != nil {
return nil, broadcast.ErrUnableToDecodeMerklePath
}
}
model = append(model, &broadcast.SubmittedTx{
BaseSubmitTxResponse: tx,
MerklePath: merklePath,
})
}

Expand All @@ -272,110 +249,3 @@ func validateSubmitTxResponse(model *broadcast.SubmittedTx) error {

return nil
}

func formatTxRequest(arc *ArcClient, tx *broadcast.Transaction, txFormat broadcast.TransactionFormat) (*SubmitTxRequest, error) {
var (
body *SubmitTxRequest
err error
)

switch txFormat {
case broadcast.RawTxFormat:
body, err = rawTxRequest(arc, tx.Hex)
case broadcast.EfFormat:
body, err = efTxRequest(tx.Hex)
case broadcast.BeefFormat:
body, err = beefTxRequest(tx.Hex)
default:
err = fmt.Errorf("unknown transaction format")
}

if err != nil {
return nil, err
}

return body, nil
}

func efTxRequest(rawTx string) (*SubmitTxRequest, error) {
request := &SubmitTxRequest{RawTx: rawTx}

return request, nil
}

func beefTxRequest(rawTx string) (*SubmitTxRequest, error) {
return nil, fmt.Errorf("Submitting transactions in BEEF format is unimplemented yet...")
}

func rawTxRequest(arc *ArcClient, rawTx string) (*SubmitTxRequest, error) {
transaction, err := bt.NewTxFromString(rawTx)
if err != nil {
return nil, err
}

for _, input := range transaction.Inputs {
if err = updateUtxoWithMissingData(arc, input); err != nil {
return nil, err
}
}

request := &SubmitTxRequest{
RawTx: hex.EncodeToString(transaction.ExtendedBytes()),
}
return request, nil
}

func updateUtxoWithMissingData(arc *ArcClient, input *bt.Input) error {
txid := input.PreviousTxIDStr()

pld := httpclient.NewPayload(
httpclient.GET,
fmt.Sprintf("https://junglebus.gorillapool.io/v1/transaction/get/%s", txid),
"",
nil,
)
resp, err := arc.HTTPClient.DoRequest(context.Background(), pld)
if err != nil {
return err
}

var tx *junglebusTransaction
err = arc_utils.DecodeResponseBody(resp.Body, &tx)
if err != nil {
return err
}

actualTx, err := bt.NewTxFromBytes(tx.Transaction)
if err != nil {
return err
}

o := actualTx.Outputs[input.PreviousTxOutIndex]
input.PreviousTxScript = o.LockingScript
input.PreviousTxSatoshis = o.Satoshis
return nil
}

type junglebusTransaction struct {
ID string `json:"id"`
Transaction []byte `json:"transaction"`
BlockHash string `json:"block_hash"`
BlockHeight uint32 `json:"block_height"`
BlockTime uint32 `json:"block_time"`
BlockIndex uint64 `json:"block_index"`

// index data
// input/output types are
// p2pkh, p2sh, token-stas, opreturn, tokenized, metanet, bitcom, run, map, bap, non-standard etc.
Addresses []string `json:"addresses"`
Inputs []string `json:"inputs"`
Outputs []string `json:"outputs"`
InputTypes []string `json:"input_types"`
OutputTypes []string `json:"output_types"`
Contexts []string `json:"contexts"` // optional contexts of output types, only for known protocols
SubContexts []string `json:"sub_contexts"` // optional sub-contexts of output types, only for known protocols
Data []string `json:"data"` // optional data of output types, only for known protocols

// the merkle proof in binary
MerkleProof []byte `json:"merkle_proof"`
}
Loading
Loading