Skip to content

Commit

Permalink
Fix db.Account fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Jun 5, 2020
1 parent d3cd976 commit 890a91e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 28 deletions.
7 changes: 7 additions & 0 deletions server/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package account
import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"

"decred.org/dcrdex/server/account/pki"
Expand Down Expand Up @@ -31,6 +32,12 @@ func (aid AccountID) String() string {
return hex.EncodeToString(aid[:])
}

// MarshalJSON satisfies the json.Marshaller interface, and will marshal the
// id to a hex string.
func (aid AccountID) MarshalJSON() ([]byte, error) {
return json.Marshal(aid.String())
}

// Value implements the sql/driver.Valuer interface.
func (aid AccountID) Value() (driver.Value, error) {
return aid[:], nil // []byte
Expand Down
1 change: 1 addition & 0 deletions server/admin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (s *Server) apiAccounts(w http.ResponseWriter, _ *http.Request) {
accts, err := s.core.Accounts()
if err != nil {
http.Error(w, fmt.Sprintf("failed to retrieve accounts: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, accts)
}
36 changes: 27 additions & 9 deletions server/admin/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"crypto/elliptic"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -21,7 +22,9 @@ import (
"testing"
"time"

"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/encode"
"decred.org/dcrdex/server/account"
"decred.org/dcrdex/server/db"
"decred.org/dcrdex/server/market"
"github.com/decred/dcrd/certgen"
Expand Down Expand Up @@ -729,13 +732,28 @@ func TestAccounts(t *testing.T) {
t.Errorf("incorrect response body: %q", respBody)
}

accountIDSlice, err := hex.DecodeString("0a9912205b2cbab0c25c2de30bda9074de0ae23b065489a99199bad763f102cc")
if err != nil {
t.Fatal(err)
}
var accountID account.AccountID
copy(accountID[:], accountIDSlice)
pubkey, err := hex.DecodeString("0204988a498d5d19514b217e872b4dbd1cf071d365c4879e64ed5919881c97eb19")
if err != nil {
t.Fatal(err)
}
feeCoin, err := hex.DecodeString("6e515ff861f2016fd0da2f3eccdf8290c03a9d116bfba2f6729e648bdc6e5aed00000005")
if err != nil {
t.Fatal(err)
}

// An account.
acct := &db.Account{
AccountID: "0a9912205b2cbab0c25c2de30bda9074de0ae23b065489a99199bad763f102cc",
Pubkey: "0204988a498d5d19514b217e872b4dbd1cf071d365c4879e64ed5919881c97eb19",
AccountID: accountID,
Pubkey: dex.Bytes(pubkey),
FeeAddress: "DsdQFmH3azyoGKJHt2ArJNxi35LCEgMqi8k",
FeeCoin: "6e515ff861f2016fd0da2f3eccdf8290c03a9d116bfba2f6729e648bdc6e5aed00000005",
BrokenRule: byte(255),
FeeCoin: dex.Bytes(feeCoin),
BrokenRule: account.Rule(byte(255)),
}
core.accounts = append(core.accounts, acct)

Expand All @@ -751,11 +769,11 @@ func TestAccounts(t *testing.T) {

exp := `[
{
"AccountID": "0a9912205b2cbab0c25c2de30bda9074de0ae23b065489a99199bad763f102cc",
"Pubkey": "0204988a498d5d19514b217e872b4dbd1cf071d365c4879e64ed5919881c97eb19",
"FeeAddress": "DsdQFmH3azyoGKJHt2ArJNxi35LCEgMqi8k",
"FeeCoin": "6e515ff861f2016fd0da2f3eccdf8290c03a9d116bfba2f6729e648bdc6e5aed00000005",
"BrokenRule": 255
"accountid": "0a9912205b2cbab0c25c2de30bda9074de0ae23b065489a99199bad763f102cc",
"pubkey": "0204988a498d5d19514b217e872b4dbd1cf071d365c4879e64ed5919881c97eb19",
"feeaddress": "DsdQFmH3azyoGKJHt2ArJNxi35LCEgMqi8k",
"feecoin": "6e515ff861f2016fd0da2f3eccdf8290c03a9d116bfba2f6729e648bdc6e5aed00000005",
"brokenrule": 255
}
]
`
Expand Down
10 changes: 2 additions & 8 deletions server/db/driver/pg/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package pg

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

Expand Down Expand Up @@ -45,8 +44,7 @@ func (a *Archiver) Account(aid account.AccountID) (*account.Account, bool, bool)
return acct, isPaid, isOpen
}

// Accounts returns data for all accounts. Byte array fields in the database are
// encoded as hex strings.
// Accounts returns data for all accounts.
func (a *Archiver) Accounts() ([]*db.Account, error) {
stmt := fmt.Sprintf(internal.SelectAllAccounts, a.tables.accounts)
rows, err := a.db.Query(stmt)
Expand All @@ -57,14 +55,10 @@ func (a *Archiver) Accounts() ([]*db.Account, error) {
var accts []*db.Account
for rows.Next() {
a := new(db.Account)
id, pubkey, feeCoin := []byte{}, []byte{}, []byte{}
err = rows.Scan(&id, &pubkey, &a.FeeAddress, &feeCoin, &a.BrokenRule)
err = rows.Scan(&a.AccountID, &a.Pubkey, &a.FeeAddress, &a.FeeCoin, &a.BrokenRule)
if err != nil {
return nil, err
}
a.AccountID = hex.EncodeToString(id)
a.Pubkey = hex.EncodeToString(pubkey)
a.FeeCoin = hex.EncodeToString(feeCoin)
accts = append(accts, a)
}
if err = rows.Err(); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions server/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ type AccountArchiver interface {
// account, completing the registration process.
PayAccount(account.AccountID, []byte) error

// Accounts returns data for all accounts. Byte array fields in the
// database are encoded as hex strings.
// Accounts returns data for all accounts.
Accounts() ([]*Account, error)
}

Expand Down
18 changes: 11 additions & 7 deletions server/db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

package db

// Account holds data returned by Accounts. Byte array fields in the database
// are encoded as hex strings.
import (
"decred.org/dcrdex/dex"
"decred.org/dcrdex/server/account"
)

// Account holds data returned by Accounts.
type Account struct {
AccountID string
Pubkey string
FeeAddress string
FeeCoin string
BrokenRule byte
AccountID account.AccountID `json:"accountid"`
Pubkey dex.Bytes `json:"pubkey"`
FeeAddress string `json:"feeaddress"`
FeeCoin dex.Bytes `json:"feecoin"`
BrokenRule account.Rule `json:"brokenrule"`
}
3 changes: 1 addition & 2 deletions server/dex/dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,7 @@ func (dm *DEX) SuspendMarket(name string, tSusp time.Time, persistBooks bool) *m
// TODO: resume by relaunching the market subsystems (Run)
// Resume / ResumeMarket

// Accounts returns data for all accounts. Byte array fields in the database are
// encoded as hex strings.
// Accounts returns data for all accounts.
func (dm *DEX) Accounts() ([]*db.Account, error) {
return dm.storage.Accounts()
}

0 comments on commit 890a91e

Please sign in to comment.