Skip to content

Commit

Permalink
Merge pull request #172 from SiaFoundation/nate/return-204
Browse files Browse the repository at this point in the history
Return 204 status code
  • Loading branch information
n8maninger authored Sep 1, 2024
2 parents 82ffb64 + b256f1a commit 7a428d8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
68 changes: 68 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api_test

import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -1460,3 +1461,70 @@ func TestAPISecurity(t *testing.T) {
t.Fatal(err)
}
}

func TestAPINoContent(t *testing.T) {
log := zaptest.NewLogger(t)
n, genesisBlock := testNetwork()

// create wallets
dbstore, tipState, err := chain.NewDBStore(chain.NewMemDB(), n, genesisBlock)
if err != nil {
t.Fatal(err)
}
cm := chain.NewManager(dbstore, tipState)

l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
defer l.Close()

ws, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), log.Named("sqlite3"))
if err != nil {
t.Fatal(err)
}
defer ws.Close()

ps, err := sqlite.NewPeerStore(ws)
if err != nil {
t.Fatal(err)
}

s := syncer.New(l, cm, ps, gateway.Header{
GenesisID: genesisBlock.ID(),
UniqueID: gateway.GenerateUniqueID(),
NetAddress: l.Addr().String(),
})
defer s.Close()
go s.Run(context.Background())

wm, err := wallet.NewManager(cm, ws, wallet.WithLogger(log.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer wm.Close()

c := runServer(t, cm, s, wm)

buf, err := json.Marshal(api.TxpoolBroadcastRequest{
Transactions: []types.Transaction{},
V2Transactions: []types.V2Transaction{},
})
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest(http.MethodPost, c.BaseURL()+"/txpool/broadcast", bytes.NewReader(buf))
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Fatalf("expected status %v, got %v", http.StatusNoContent, resp.StatusCode)
} else if resp.ContentLength != 0 {
t.Fatalf("expected no content, got %v bytes", resp.ContentLength)
}
}
16 changes: 15 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ func (s *server) syncerConnectHandler(jc jape.Context) {
return
}
_, err := s.s.Connect(jc.Request.Context(), addr)
jc.Check("couldn't connect to peer", err)
if jc.Check("couldn't connect to peer", err) != nil {
return
}
jc.EmptyResonse()
}

func (s *server) syncerBroadcastBlockHandler(jc jape.Context) {
Expand All @@ -263,6 +266,7 @@ func (s *server) syncerBroadcastBlockHandler(jc jape.Context) {
} else {
s.s.BroadcastV2BlockOutline(gateway.OutlineBlock(b, s.cm.PoolTransactions(), s.cm.V2PoolTransactions()))
}
jc.EmptyResonse()
}

func (s *server) txpoolParentsHandler(jc jape.Context) {
Expand Down Expand Up @@ -305,6 +309,8 @@ func (s *server) txpoolBroadcastHandler(jc jape.Context) {
}
s.s.BroadcastV2TransactionSet(index, tbr.V2Transactions)
}

jc.EmptyResonse()
}

func (s *server) walletsHandler(jc jape.Context) {
Expand Down Expand Up @@ -367,6 +373,7 @@ func (s *server) walletsIDHandlerDELETE(jc jape.Context) {
} else if jc.Check("couldn't remove wallet", err) != nil {
return
}
jc.EmptyResonse()
}

func (s *server) rescanHandlerGET(jc jape.Context) {
Expand Down Expand Up @@ -425,6 +432,8 @@ func (s *server) rescanHandlerPOST(jc jape.Context) {
s.scanInfo.Error = &msg
}
}()

jc.EmptyResonse()
}

func (s *server) walletsAddressHandlerPUT(jc jape.Context) {
Expand All @@ -435,6 +444,7 @@ func (s *server) walletsAddressHandlerPUT(jc jape.Context) {
} else if jc.Check("couldn't add address", s.wm.AddAddress(id, addr)) != nil {
return
}
jc.EmptyResonse()
}

func (s *server) walletsAddressHandlerDELETE(jc jape.Context) {
Expand All @@ -450,6 +460,7 @@ func (s *server) walletsAddressHandlerDELETE(jc jape.Context) {
} else if jc.Check("couldn't remove address", err) != nil {
return
}
jc.EmptyResonse()
}

func (s *server) walletsAddressesHandlerGET(jc jape.Context) {
Expand Down Expand Up @@ -568,6 +579,7 @@ func (s *server) walletsReserveHandler(jc jape.Context) {
if jc.Check("couldn't reserve outputs", s.wm.Reserve(ids, wrr.Duration)) != nil {
return
}
jc.EmptyResonse()
}

func (s *server) walletsReleaseHandler(jc jape.Context) {
Expand All @@ -584,6 +596,7 @@ func (s *server) walletsReleaseHandler(jc jape.Context) {
for _, id := range wrr.SiafundOutputs {
delete(s.used, types.Hash256(id))
}
jc.EmptyResonse()
}

func (s *server) walletsFundHandler(jc jape.Context) {
Expand Down Expand Up @@ -886,6 +899,7 @@ func (s *server) debugMineHandler(jc jape.Context) {
log.Debug("mined block", zap.Stringer("blockID", b.ID()))
n--
}
jc.EmptyResonse()
}

func (s *server) pprofHandler(jc jape.Context) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.22
go.sia.tech/core v0.4.4
go.sia.tech/coreutils v0.3.0
go.sia.tech/jape v0.12.0
go.sia.tech/jape v0.12.1
go.sia.tech/web/walletd v0.23.0
go.uber.org/zap v1.27.0
golang.org/x/term v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ go.sia.tech/core v0.4.4 h1:DYb0/DxgACstJUGgsRJIVtrsTC0mk6GfA6pTxQwzKV0=
go.sia.tech/core v0.4.4/go.mod h1:Zuq0Tn2aIXJyO0bjGu8cMeVWe+vwQnUfZhG1LCmjD5c=
go.sia.tech/coreutils v0.3.0 h1:TutrhfNe8hq0GxWcibSRIVZQpFpBoKId7pFjxdvDIR8=
go.sia.tech/coreutils v0.3.0/go.mod h1:8DNsiy6Xon5R9M/FnaSzAi2wcATh98EsDV3N6iGq4yI=
go.sia.tech/jape v0.12.0 h1:13fBi7c5X8zxTQ05Cd9ZsIfRJgdvGoZqbEzH861z7BU=
go.sia.tech/jape v0.12.0/go.mod h1:wU+h6Wh5olDjkPXjF0tbZ1GDgoZ6VTi4naFw91yyWC4=
go.sia.tech/jape v0.12.1 h1:xr+o9V8FO8ScRqbSaqYf9bjj1UJ2eipZuNcI1nYousU=
go.sia.tech/jape v0.12.1/go.mod h1:wU+h6Wh5olDjkPXjF0tbZ1GDgoZ6VTi4naFw91yyWC4=
go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
go.sia.tech/mux v1.2.0/go.mod h1:Yyo6wZelOYTyvrHmJZ6aQfRoer3o4xyKQ4NmQLJrBSo=
go.sia.tech/web v0.0.0-20240610131903-5611d44a533e h1:oKDz6rUExM4a4o6n/EXDppsEka2y/+/PgFOZmHWQRSI=
Expand Down

0 comments on commit 7a428d8

Please sign in to comment.