mimblewimble / grin

Minimal implementation of the Mimblewimble protocol.
https://grin.mw/
Apache License 2.0
5.04k stars 991 forks source link

grin wallet send cause recipient existed money missed #1306

Closed garyyu closed 6 years ago

garyyu commented 6 years ago

Bug. It happens each time when call this send.

Sender:

$ ./grin wallet send -d http://xxx:13415 3.33 -c 6 -s smallest
Aug 01 15:03:59.058 INFO This is Grin version 0.3.0 (git testnet3-54-g84e46c2), built for x86_64-apple-darwin by rustc 1.27.1 (5f2b325f6 2018-07-07).
Aug 01 15:03:59.064 DEBG Built with profile "release", features "" on Tue, 31 Jul 2018 11:02:42 GMT.
Aug 01 15:03:59.064 INFO Found configuration file at /Volumes/ramdisk/grin/grin.toml
Aug 01 15:03:59.064 INFO Using LMDB Backend for wallet
Aug 01 15:03:59.065 INFO Using LMDB Backend for wallet
Aug 01 15:03:59.065 DEBG Using wallet seed file at: ./wallet.seed
Aug 01 15:03:59.091 DEBG Refreshing wallet outputs
Aug 01 15:03:59.184 DEBG Building input (spending coinbase): 60000000000, 062c7cdc42c4789fc67a
Aug 01 15:03:59.195 DEBG Building an output: 56662000000, 423fe7a2e7d3d837bd1e
Aug 01 15:03:59.290 DEBG Posting transaction slate to http://xxx:13415/v1/wallet/foreign/receive_tx
Aug 01 15:03:59.530 DEBG Validating final transaction
Aug 01 15:03:59.617 INFO Tx created: 3.330000000 grin to http://xxx:13415 (strategy 'smallest')
Aug 01 15:03:59.709 INFO Tx sent

Recipient: (before this sending)

$ ./target/release/grin wallet info

____ Wallet Summary Info as of 32251 ____

 Total                            | 2644.384000000 
 Awaiting Confirmation            | 9.900000000 
 Immature Coinbase                | 900.000000000 
 Currently Spendable              | 1734.484000000 
 ---------                        | --------- 
 (Locked by previous transaction) | 0.000000000 

$ ./target/release/grin wallet outputs

Wallet Outputs - Block Height: 32251
-------------------------------------------------------------------------------------------------------------------------------------------------
 Key Id                Child Key Index  Block Height  Locked Until  Status       Is Coinbase?  Num. of Confirmations  Value          Transaction 
=================================================================================================================================================
...
-------------------------------------------------------------------------------------------------------------------------------------------------
 eef3264d87788aaa85fe  28               20598         21598         Unspent      true          11654                  60.000000000   5 
-------------------------------------------------------------------------------------------------------------------------------------------------
...
-------------------------------------------------------------------------------------------------------------------------------------------------

Recipient: (after this sending)

$ ./target/release/grin wallet info

____ Wallet Summary Info as of 32251 ____

 Total                            | 2587.714000000 
 Awaiting Confirmation            | 13.230000000 
 Immature Coinbase                | 900.000000000 
 Currently Spendable              | 1674.484000000 
 ---------                        | --------- 
 (Locked by previous transaction) | 0.000000000 

$ ./target/release/grin wallet outputs

Wallet Outputs - Block Height: 32251
-------------------------------------------------------------------------------------------------------------------------------------------------
 Key Id                Child Key Index  Block Height  Locked Until  Status       Is Coinbase?  Num. of Confirmations  Value          Transaction 
=================================================================================================================================================
...
-------------------------------------------------------------------------------------------------------------------------------------------------
 eef3264d87788aaa85fe  28               32252         0             Unconfirmed  false         0                      3.330000000    64 
-------------------------------------------------------------------------------------------------------------------------------------------------
...

As you see, the recipient's Currently Spendable has been changed from 1734.484 to 1674.484, and 60 grin missed.

Look into the recipient's outputs, that 60 grin coinbase output has been changed as normal output and value is changed from 60 to 3.33.

If I do this sending again, this issue is always reproduced, for the moment. I already tested for 3 times.

yeastplume commented 6 years ago

It looks like 'Child Key Index' or n_child was reused locally in the wallet here for some reason (which it shouldn't be, there's an index that's always incremented). I can try to reproduce locally but likely there is something odd about the dataset.. quite possibly that it was grin wallet restore d (which isn't 100% functional yet and is probably not setting the child key index counter properly)

garyyu commented 6 years ago

Indeed! After a wallet restore, the deriv_idx is left as 0, that's the root cause of overwriting in future wallet receiving.

Try to fix it in https://github.com/mimblewimble/grin/pull/1374.