From d720a4d57aa91ebe6c109a758089f7f95e11b0b4 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Sat, 31 Aug 2024 19:57:25 -0700 Subject: [PATCH 1/3] deps: update jape --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6f50f94..0d3fb4e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 80ba106..cd83f41 100644 --- a/go.sum +++ b/go.sum @@ -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= From 1eae42fd50dfc66e6c672a6a26149025ae991e82 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Sat, 31 Aug 2024 19:58:09 -0700 Subject: [PATCH 2/3] api: return 204 for empty handlers --- api/server.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/api/server.go b/api/server.go index 661d69c..51df847 100644 --- a/api/server.go +++ b/api/server.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -425,6 +432,8 @@ func (s *server) rescanHandlerPOST(jc jape.Context) { s.scanInfo.Error = &msg } }() + + jc.EmptyResonse() } func (s *server) walletsAddressHandlerPUT(jc jape.Context) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { From b256f1a2e9bb04998ecc114905c1d5cd56207d07 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Sat, 31 Aug 2024 20:02:27 -0700 Subject: [PATCH 3/3] api: add test --- api/api_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/api/api_test.go b/api/api_test.go index 1610e69..d8f3191 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -1,6 +1,7 @@ package api_test import ( + "bytes" "context" "encoding/hex" "encoding/json" @@ -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) + } +}