Bitcoin Forum
December 12, 2024, 09:51:13 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: API: keeping account balance positive (+knowing in advance the transaction fee)  (Read 1086 times)
Lohoris (OP)
Hero Member
*****
Offline Offline

Activity: 630
Merit: 500


Bitgoblin


View Profile
December 05, 2012, 11:37:16 PM
 #1

(as posted on SE)

I'm using the json API. I need to keep all the account balances positive: I call getbalance first, check if it has enough bitcoins to proceed, and then move or sendfrom.

This approach causes two problems.

Problem 1 is that this is not an atomic operation, so after the first getbalance the balance could actually be changed.

Problem 2 only happens with sendfrom: there might be a transaction fee, hence if the user sends his entire balance, he'll have a negative balance due to that.

Solution to Problem 2 would be to know in advance the transaction fee: I noticed (though I didn't try) I can set it with settxfee, but I found no call to get it, is there?

While I could implement some external lock mechanism to solve Problem 1, I'd rather use a bitcoind native method, though I get it does not exist, does it? Hacking the sendfrom function to have it check the balance before sending would be enough, I guess?

1LohorisJie8bGGG7X4dCS9MAVsTEbzrhu
DefaultTrust is very BAD.
davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1008


1davout


View Profile WWW
December 06, 2012, 12:07:23 AM
 #2

Problem 2 only happens with sendfrom: there might be a transaction fee, hence if the user sends his entire balance, he'll have a negative balance due to that.
No, the transaction will fail if a tx fee causes the account to go negative

acc1dent
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
December 06, 2012, 02:11:47 AM
 #3

I'm sure it is impossible to get a negative ballance
Lohoris (OP)
Hero Member
*****
Offline Offline

Activity: 630
Merit: 500


Bitgoblin


View Profile
December 06, 2012, 07:53:09 AM
 #4

Problem 2 only happens with sendfrom: there might be a transaction fee, hence if the user sends his entire balance, he'll have a negative balance due to that.
No, the transaction will fail if a tx fee causes the account to go negative
No, the account balance will go negative, I just tried.
We're talking about accounts here.
They do go negative.

1LohorisJie8bGGG7X4dCS9MAVsTEbzrhu
DefaultTrust is very BAD.
davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1008


1davout


View Profile WWW
December 06, 2012, 08:00:52 AM
 #5

Problem 2 only happens with sendfrom: there might be a transaction fee, hence if the user sends his entire balance, he'll have a negative balance due to that.
No, the transaction will fail if a tx fee causes the account to go negative
No, the account balance will go negative, I just tried.
We're talking about accounts here.
They do go negative.

Last time I tried it did not work that way, but maybe you're trying with the default account, which is allowed to go negatvie.

Lohoris (OP)
Hero Member
*****
Offline Offline

Activity: 630
Merit: 500


Bitgoblin


View Profile
December 06, 2012, 05:52:50 PM
 #6

Last time I tried it did not work that way, but maybe you're trying with the default account, which is allowed to go negatvie.
no:

Code:
looris@Palace-of-the-Nine-Moons:~/ext-Sviluppo/bitcoin $ bitcoind listaccounts
{
    "" : 0.00000000,
    "bj_1" : 0.06900000,
    "bj_2" : 0.03550000,
    "bj_3" : 0.01550000,
    "bj_7" : 0.00000000
}
looris@Palace-of-the-Nine-Moons:~/ext-Sviluppo/bitcoin $ bitcoind move bj_3 bj_1 0.02
true
looris@Palace-of-the-Nine-Moons:~/ext-Sviluppo/bitcoin $ bitcoind listaccounts
{
    "" : 0.00000000,
    "bj_1" : 0.08900000,
    "bj_2" : 0.03550000,
    "bj_3" : -0.00450000,
    "bj_7" : 0.00000000
}

1LohorisJie8bGGG7X4dCS9MAVsTEbzrhu
DefaultTrust is very BAD.
davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1008


1davout


View Profile WWW
December 06, 2012, 06:07:41 PM
 #7

Last time I tried it did not work that way, but maybe you're trying with the default account, which is allowed to go negatvie.
no:

Code:
looris@Palace-of-the-Nine-Moons:~/ext-Sviluppo/bitcoin $ bitcoind listaccounts
{
    "" : 0.00000000,
    "bj_1" : 0.06900000,
    "bj_2" : 0.03550000,
    "bj_3" : 0.01550000,
    "bj_7" : 0.00000000
}
looris@Palace-of-the-Nine-Moons:~/ext-Sviluppo/bitcoin $ bitcoind move bj_3 bj_1 0.02
true
looris@Palace-of-the-Nine-Moons:~/ext-Sviluppo/bitcoin $ bitcoind listaccounts
{
    "" : 0.00000000,
    "bj_1" : 0.08900000,
    "bj_2" : 0.03550000,
    "bj_3" : -0.00450000,
    "bj_7" : 0.00000000
}

You're doing a "move" which is not the same thing as a "sendtoaddress".

Again, last time I checked transaction fees could not lead a non-default account into negative.

I'm not saying it has not changed, I am giving you the result of my experience.

Last time I checked "move"s between accounts would silently round the amounts to two decimal places, but it has been fixed since then.

The only way to know for sure is to test on the latest versions Smiley

Bitexchange
Newbie
*
Offline Offline

Activity: 28
Merit: 0



View Profile WWW
December 06, 2012, 06:31:17 PM
 #8

sendfrom() makes balances go negative by the fee amount if someone withdraws all coins.
Lohoris (OP)
Hero Member
*****
Offline Offline

Activity: 630
Merit: 500


Bitgoblin


View Profile
December 06, 2012, 07:33:25 PM
 #9

sendfrom() makes balances go negative by the fee amount if someone withdraws all coins.

yeah that is exactly my "problem 2", thanks for understanding that.
I've reported it as a bug, hoping they do consider it a bug.

@davout: yeah, I'm doing a move there. It is "problem 1", listed in the opening post.
I've fixed it hacking bitcoind code, though.

1LohorisJie8bGGG7X4dCS9MAVsTEbzrhu
DefaultTrust is very BAD.
davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1008


1davout


View Profile WWW
December 06, 2012, 07:37:02 PM
 #10

sendfrom() makes balances go negative by the fee amount if someone withdraws all coins.

yeah that is exactly my "problem 2", thanks for understanding that.
I've reported it as a bug, hoping they do consider it a bug.

@davout: yeah, I'm doing a move there. It is "problem 1", listed in the opening post.
I've fixed it hacking bitcoind code, though.
If it's as you say it is indeed a bug that needs to be fixed.
If I was you I would simply not use the accounting feature.
It just doesn't scale well.

Lohoris (OP)
Hero Member
*****
Offline Offline

Activity: 630
Merit: 500


Bitgoblin


View Profile
December 06, 2012, 07:52:52 PM
 #11

If I was you I would simply not use the accounting feature.
Yeah, but that feature is exactly what I need to build a website... I mean, I would have to re-implement it from scratch anyway, so I'd rather not re-invent the wheel...

1LohorisJie8bGGG7X4dCS9MAVsTEbzrhu
DefaultTrust is very BAD.
Bitexchange
Newbie
*
Offline Offline

Activity: 28
Merit: 0



View Profile WWW
December 08, 2012, 02:29:50 AM
 #12

Nobody except me and Lohoris working with the accounts feature?

Maybe it doesnt scale too well for sites as big as davout`s sites, but wouldnt that just be a problem of the current 0.7 implementation?
I mean the feature itself is great and if it gets some attention from developers it could scale well in the future, or am i missing something which totally sucks about accounts anyway?

Because installing a database myself and implementing exactly the same on my own would really look like reinventing the weel for me...

Why doesnt some bigger site instead of writing their own code contrubute better code to the bitcoin client?
(that question goes a bit to people like you davout)

davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1008


1davout


View Profile WWW
December 08, 2012, 10:43:25 AM
 #13

Maybe it doesnt scale too well for sites as big as davout`s sites
It was what made Instawallet absolutely unusably slow in the beginning of 2012.

I used to use it for Bitcoin-Central as a second layer of accounting, but at some point I realized discrepancies, which were due to the silent rounding.

Anyway, I don't mind this feature, but I think it's going to be really great when bitcoind uses a relational database that you can query directly.

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!