Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output endpoint #156

Merged
merged 3 commits into from
Jul 17, 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
32 changes: 32 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type (

Events(eventIDs []types.Hash256) ([]wallet.Event, error)

SiacoinElement(types.SiacoinOutputID) (types.SiacoinElement, error)
SiafundElement(types.SiafundOutputID) (types.SiafundElement, error)

Reserve(ids []types.Hash256, duration time.Duration) error
}
)
Expand Down Expand Up @@ -723,6 +726,32 @@ func (s *server) eventsHandlerGET(jc jape.Context) {
jc.Encode(events[0])
}

func (s *server) outputsSiacoinHandlerGET(jc jape.Context) {
var outputID types.SiacoinOutputID
if jc.DecodeParam("id", &outputID) != nil {
return
}

output, err := s.wm.SiacoinElement(outputID)
if jc.Check("couldn't load output", err) != nil {
return
}
jc.Encode(output)
}

func (s *server) outputsSiafundHandlerGET(jc jape.Context) {
var outputID types.SiafundOutputID
if jc.DecodeParam("id", &outputID) != nil {
return
}

output, err := s.wm.SiafundElement(outputID)
if jc.Check("couldn't load output", err) != nil {
return
}
jc.Encode(output)
}

// NewServer returns an HTTP handler that serves the walletd API.
func NewServer(cm ChainManager, s Syncer, wm WalletManager) http.Handler {
srv := server{
Expand Down Expand Up @@ -774,6 +803,9 @@ func NewServer(cm ChainManager, s Syncer, wm WalletManager) http.Handler {
"GET /addresses/:addr/outputs/siacoin": srv.addressesAddrOutputsSCHandler,
"GET /addresses/:addr/outputs/siafund": srv.addressesAddrOutputsSFHandler,

"GET /outputs/siacoin/:id": srv.outputsSiacoinHandlerGET,
"GET /outputs/siafund/:id": srv.outputsSiafundHandlerGET,

"GET /events/:id": srv.eventsHandlerGET,
})
}
1 change: 1 addition & 0 deletions persist/sqlite/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (d *decodable) Scan(src any) error {
case *[]types.Hash256:
dec := types.NewBufDecoder(src)
types.DecodeSlice(dec, v)
return dec.Err()
default:
return fmt.Errorf("cannot scan %T to %T", src, d.v)
}
Expand Down
4 changes: 2 additions & 2 deletions persist/sqlite/peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func TestBanPeer(t *testing.T) {
}

// ban the peer
ps.Ban(peer, time.Second, "test")
ps.Ban(peer, 5*time.Second, "test")

if banned, err := ps.Banned(peer); err != nil || !banned {
t.Fatal("expected peer to be banned", err)
}

// wait for the ban to expire
time.Sleep(time.Second)
time.Sleep(5 * time.Second)

if banned, err := ps.Banned(peer); err != nil || banned {
t.Fatal("expected peer to not be banned", err)
Expand Down
72 changes: 72 additions & 0 deletions persist/sqlite/utxo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package sqlite

import (
"database/sql"
"errors"
"fmt"

"go.sia.tech/core/types"
"go.sia.tech/walletd/wallet"
)

// SiacoinElement returns an unspent Siacoin UTXO by its ID.
func (s *Store) SiacoinElement(id types.SiacoinOutputID) (ele types.SiacoinElement, err error) {
err = s.transaction(func(tx *txn) error {
const query = `SELECT se.id, se.siacoin_value, se.merkle_proof, se.leaf_index, se.maturity_height, sa.sia_address
FROM siacoin_elements se
INNER JOIN sia_addresses sa ON (se.address_id = sa.id)
WHERE se.id=$1 AND spent_index_id IS NULL`

ele, err = scanSiacoinElement(tx.QueryRow(query, encode(id)))
if err != nil {
return err
}

// retrieve the merkle proofs for the siacoin element
if s.indexMode == wallet.IndexModeFull {
proof, err := fillElementProofs(tx, []uint64{ele.LeafIndex})
if err != nil {
return fmt.Errorf("failed to fill element proofs: %w", err)
} else if len(proof) != 1 {
panic("expected exactly one proof") // should never happen
}
ele.MerkleProof = proof[0]
}
return nil
})
if errors.Is(err, sql.ErrNoRows) {
err = wallet.ErrNotFound
}
return
}

// SiafundElement returns an unspent Siafund UTXO by its ID.
func (s *Store) SiafundElement(id types.SiafundOutputID) (ele types.SiafundElement, err error) {
err = s.transaction(func(tx *txn) error {
const query = `SELECT se.id, se.leaf_index, se.merkle_proof, se.siafund_value, se.claim_start, sa.sia_address
FROM siafund_elements se
INNER JOIN sia_addresses sa ON (se.address_id = sa.id)
WHERE se.id=$1 AND spent_index_id IS NULL`

ele, err = scanSiafundElement(tx.QueryRow(query, encode(id)))
if err != nil {
return err
}

// retrieve the merkle proofs for the siafund element
if s.indexMode == wallet.IndexModeFull {
proof, err := fillElementProofs(tx, []uint64{ele.LeafIndex})
if err != nil {
return fmt.Errorf("failed to fill element proofs: %w", err)
} else if len(proof) != 1 {
panic("expected exactly one proof") // should never happen
}
ele.MerkleProof = proof[0]
}
return nil
})
if errors.Is(err, sql.ErrNoRows) {
err = wallet.ErrNotFound
}
return
}
13 changes: 13 additions & 0 deletions wallet/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type (
Events(eventIDs []types.Hash256) ([]Event, error)
AnnotateV1Events(index types.ChainIndex, timestamp time.Time, v1 []types.Transaction) (annotated []Event, err error)

SiacoinElement(types.SiacoinOutputID) (types.SiacoinElement, error)
SiafundElement(types.SiafundOutputID) (types.SiafundElement, error)

SetIndexMode(IndexMode) error
LastCommittedIndex() (types.ChainIndex, error)
}
Expand Down Expand Up @@ -283,6 +286,16 @@ func (m *Manager) IndexMode() IndexMode {
return m.indexMode
}

// SiacoinElement returns the unspent siacoin element with the given id.
func (m *Manager) SiacoinElement(id types.SiacoinOutputID) (types.SiacoinElement, error) {
return m.store.SiacoinElement(id)
}

// SiafundElement returns the unspent siafund element with the given id.
func (m *Manager) SiafundElement(id types.SiafundOutputID) (types.SiafundElement, error) {
return m.store.SiafundElement(id)
}

// Close closes the wallet manager.
func (m *Manager) Close() error {
m.tg.Stop()
Expand Down
15 changes: 15 additions & 0 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,13 @@ func TestFullIndex(t *testing.T) {
if err != nil {
t.Fatal(err)
}
for _, se := range utxos {
if sce, err := wm.SiacoinElement(types.SiacoinOutputID(se.ID)); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(sce, se) {
t.Fatalf("expected %v, got %v", se, sce)
}
}

policy := types.PolicyTypeUnlockConditions(types.StandardUnlockConditions(pk.PublicKey()))
txn := types.V2Transaction{
Expand Down Expand Up @@ -1319,6 +1326,14 @@ func TestFullIndex(t *testing.T) {
t.Fatal(err)
}

for _, se := range sf {
if sfe, err := wm.SiafundElement(types.SiafundOutputID(se.ID)); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(sfe, se) {
t.Fatalf("expected %v, got %v", se, sfe)
}
}

// send the siafunds to the first address
policy = types.PolicyTypeUnlockConditions(types.StandardUnlockConditions(pk2.PublicKey()))
txn = types.V2Transaction{
Expand Down
Loading