-
Notifications
You must be signed in to change notification settings - Fork 86
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
admin: Add account endpoint. #455
Conversation
server/db/driver/pg/accounts.go
Outdated
var accountID, pubkey, feeCoin []byte | ||
var brokenRule byte | ||
for rows.Next() { | ||
a := new(db.Account) | ||
err = rows.Scan(&a.AccountID, &a.Pubkey, &a.FeeAddress, &a.FeeCoin, &a.BrokenRule) | ||
err = rows.Scan(&accountID, &pubkey, &a.FeeAddress, &feeCoin, &brokenRule) | ||
if err != nil { | ||
return nil, err | ||
} | ||
copy(a.AccountID[:], accountID) | ||
a.Pubkey = dex.Bytes(pubkey) | ||
a.FeeCoin = dex.Bytes(feeCoin) | ||
a.BrokenRule = account.Rule(brokenRule) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops:
failed to retrieve accounts: sql: Scan error on column index 3, name "fee_coin": unsupported Scan, storing driver.Value type <nil> into type *dex.Bytes
I suppose it is because sql doesn't work well with custom types? Have removed all custom types from the Scan
just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Custom types need either (1) built-in underlying type, or (2) define Scan and Value methods to satisfy sql.Scanner and sql.Valuer interfaces. We have those define for many types. I suggest looking at those for examples. e.g. for AccountID
:
dcrdex/server/account/account.go
Lines 41 to 59 in 95d8c3f
// Value implements the sql/driver.Valuer interface. | |
func (aid AccountID) Value() (driver.Value, error) { | |
return aid[:], nil // []byte | |
} | |
// Scan implements the sql.Scanner interface. | |
func (aid *AccountID) Scan(src interface{}) error { | |
switch src := src.(type) { | |
case []byte: | |
copy(aid[:], src) | |
return nil | |
//case string: | |
// case nil: | |
// *oid = nil | |
// return nil | |
} | |
return fmt.Errorf("cannot convert %T to AccountID", src) | |
} |
However, the error here indicates that dex.Bytes
, which is just []byte
doesn't handle NULL values in the table. Note that the NULL issue is so common that there are standard types like sql.NullString
and sql.NullInt64
for this reason. For byte slices, I think it should just work with NULLs. How do you get this error (what code and test)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can repro the error by changing getAccount
to use dex.Bytes
instead of []byte
. I'm OK with Scanning into a []byte
and just converting like you have. But we can definitely scan into an account.AccountID directly because we have implemented sql.Scanner for that and it's the primary key that can never be null.
Also broken_rule
has a non-NULL default, so that's OK to scan directly into an account.Rule too I believe.
As for FeeAddress
, we'll probably want to scan into a sql.NullString
to be safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a Scan
method to dex.Bytes
looks to has solved the problem. Is this alright? It seems odd that this is needed since scanning when the value is not null used the underlying type's Scan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is odd, but we had to do something like this with the Preimage scanner I believe.
@@ -89,6 +90,14 @@ func TestAccounts(t *testing.T) { | |||
t.Fatal("accounts has unexpected data") | |||
} | |||
|
|||
anAcct, err := archie.AccountInfo(accts[0].AccountID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also test Accounts
and AccountInfo
before PayAccount
to test the null cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a separate nullifying section.
82b4c60
to
057bba8
Compare
dex/marshal.go
Outdated
func (b *Bytes) Scan(src interface{}) error { | ||
switch src := src.(type) { | ||
case []byte: | ||
*b = Bytes(src) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a brutal gotcha here. You actually need to create a new bytes slice and copy
the bytes because the sql driver reuses src and the underlying buffer changes all the data you already scanned in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. That saves a bunch of headaches down the road.
// The Account ID cannot be null. broken_rule has a default value of 0 | ||
// and is unexpected to become null. | ||
nullAccounts := `UPDATE %s | ||
SET | ||
pubkey = null , | ||
fee_address = null, | ||
fee_coin = null;` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with these tests, although note that that the account id is the hash of the pubkey, so they will both be set always unless the DB is corrupted. Still the db scheme doesn't prevent pubkey from being null so I think this is a good test.
For posterity's sake, the results, which could be augmented with an individual account's active swaps at some point:
|
Part of #329