From 890a91e8cec69ededffb5a0d655eed37912e9d42 Mon Sep 17 00:00:00 2001 From: JoeGruff Date: Fri, 5 Jun 2020 12:01:12 +0900 Subject: [PATCH] Fix db.Account fields. --- server/account/account.go | 7 +++++++ server/admin/api.go | 1 + server/admin/server_test.go | 36 ++++++++++++++++++++++++--------- server/db/driver/pg/accounts.go | 10 ++------- server/db/interface.go | 3 +-- server/db/types.go | 18 ++++++++++------- server/dex/dex.go | 3 +-- 7 files changed, 50 insertions(+), 28 deletions(-) diff --git a/server/account/account.go b/server/account/account.go index 8bb7b8e9d9..1a1b16b50e 100644 --- a/server/account/account.go +++ b/server/account/account.go @@ -3,6 +3,7 @@ package account import ( "database/sql/driver" "encoding/hex" + "encoding/json" "fmt" "decred.org/dcrdex/server/account/pki" @@ -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 diff --git a/server/admin/api.go b/server/admin/api.go index 19928a183a..b7beedbdc1 100644 --- a/server/admin/api.go +++ b/server/admin/api.go @@ -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) } diff --git a/server/admin/server_test.go b/server/admin/server_test.go index b589baab31..ad272154f0 100644 --- a/server/admin/server_test.go +++ b/server/admin/server_test.go @@ -7,6 +7,7 @@ import ( "context" "crypto/elliptic" "crypto/sha256" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -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" @@ -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) @@ -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 } ] ` diff --git a/server/db/driver/pg/accounts.go b/server/db/driver/pg/accounts.go index fe906b1487..d1b9fe6a98 100644 --- a/server/db/driver/pg/accounts.go +++ b/server/db/driver/pg/accounts.go @@ -5,7 +5,6 @@ package pg import ( "database/sql" - "encoding/hex" "errors" "fmt" @@ -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) @@ -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 { diff --git a/server/db/interface.go b/server/db/interface.go index 532d7f6892..ebe2651c8f 100644 --- a/server/db/interface.go +++ b/server/db/interface.go @@ -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) } diff --git a/server/db/types.go b/server/db/types.go index a2a1e8d450..f358b47d3f 100644 --- a/server/db/types.go +++ b/server/db/types.go @@ -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"` } diff --git a/server/dex/dex.go b/server/dex/dex.go index c6dba6da6f..f3fc256241 100644 --- a/server/dex/dex.go +++ b/server/dex/dex.go @@ -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() }