Skip to content

Commit

Permalink
add bulk transactions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Jan 9, 2024
1 parent bc1b19d commit 6a3ba73
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ func (c *Client) Transaction(id types.TransactionID) (resp types.Transaction, er
err = c.c.GET(fmt.Sprintf("/explorer/transactions/%s", id), &resp)
return
}

// Transactions returns the transactions with the specified IDs.
func (c *Client) Transactions(ids []types.TransactionID) (resp types.Transaction, err error) {
err = c.c.POST("/explorer/transactions", ids, &resp)

Check failure on line 90 in api/client.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

Client has wrong response type for POST /explorer/transactions (got *go.sia.tech/core/types.Transaction, should be *[]go.sia.tech/core/types.Transaction)

Check failure on line 90 in api/client.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.20)

Analyzer warning in api

Client has wrong response type for POST /explorer/transactions (got *go.sia.tech/core/types.Transaction, should be *[]go.sia.tech/core/types.Transaction)

Check failure on line 90 in api/client.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

Client has wrong response type for POST /explorer/transactions (got *go.sia.tech/core/types.Transaction, should be *[]go.sia.tech/core/types.Transaction)

Check failure on line 90 in api/client.go

View workflow job for this annotation

GitHub Actions / test (macos-latest, 1.20)

Analyzer warning in api

Client has wrong response type for POST /explorer/transactions (got *go.sia.tech/core/types.Transaction, should be *[]go.sia.tech/core/types.Transaction)
return
}
26 changes: 26 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"errors"
"net/http"
"sync"

Expand Down Expand Up @@ -177,6 +178,30 @@ func (s *server) explorerTransactionsIDHandler(jc jape.Context) {
jc.Encode(txn)
}

func (s *server) explorerTransactionsHandler(jc jape.Context) {
var (
errTooManyIDs = errors.New("too many IDs provided (provide less than 5000)")
)

var ids []types.TransactionID
if jc.Decode(&ids) != nil {
return
} else if len(ids) > 5000 {
jc.Error(errTooManyIDs, http.StatusBadRequest)
return
}

var txns []types.Transaction
for _, id := range ids {
txn, err := s.e.Transaction(id)
if jc.Check("failed to get transaction", err) != nil {
return
}
txns = append(txns, txn)
}
jc.Encode(txns)
}

// NewServer returns an HTTP handler that serves the explored API.
func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
srv := server{
Expand All @@ -197,5 +222,6 @@ func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
"GET /explorer/block/id/:id": srv.explorerBlockHandler,
"GET /explorer/block/height/:height": srv.explorerBlockHeightHandler,
"GET /explorer/transactions/:id": srv.explorerTransactionsIDHandler,
"POST /explorer/transactions": srv.explorerTransactionsHandler,
})
}
2 changes: 2 additions & 0 deletions persist/sqlite/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ CREATE TABLE transactions (
transaction_id BLOB UNIQUE NOT NULL
);

CREATE INDEX transactions_index ON transactions(transaction_id);

CREATE TABLE block_transactions (
block_id BLOB REFERENCES blocks(id) ON DELETE CASCADE NOT NULL,
transaction_id INTEGER REFERENCES transactions(id) ON DELETE CASCADE NOT NULL,
Expand Down

0 comments on commit 6a3ba73

Please sign in to comment.