public inbox for bitcoindev@googlegroups.com
 help / color / mirror / Atom feed
From: "Michael Grønager" <gronager@ceptacle.com>
To: Pieter Wuille <pieter.wuille@gmail.com>
Cc: Bitcoin Dev <bitcoin-development@lists.sourceforge.net>
Subject: Re: [Bitcoin-development] Announcement: libcoin
Date: Fri, 3 Feb 2012 10:52:22 +0100	[thread overview]
Message-ID: <8CC24D9C-4207-4493-8C95-C5738B65C8B6@ceptacle.com> (raw)
In-Reply-To: <CAPg+sBg16OPdyi3MQ+sfBR+z_ArP6c8KpU36pDA0MEdXpk9fxQ@mail.gmail.com>

> Hello Michael,
> 
> I'm impressed by your refactorings, and hope some of them can make it into the Satoshi codebase.

Thanks:)

>  I am however not sure what you've said above is safe. In particular, how do you guarantee that no other thread modifies the blockchain structure while you are performing your query on it? Does the query code operate on a const copy of the structure, or is there guaranteed only one thread accessing it?

The BlockChain class encapsulates all access to the blockchain and only give you access to certain restricted queries on the chain. Actually that was the case already in the satoshi client, I have only tried to formalize and encapsulate these queries in the code instead of having all the code poking around directly in the database and the blockfile.

I should note that the database still keeps a mutex to protect reads from writes.

I agree that constness alone does not guarantee thread safety, it is one of the things to use to get there. Great care should be taken not to read a value that is being changed at the same time, at least if that will render the result unusable.

The list of allowed queries are the const public methods of BlockChain. Some examples :
1.    bool isSpent(Coin coin) const;
2.    int getNumSpent(uint256 hash) const ;
3.    uint256 spentIn(Coin coin) const;

    /// Check if the hash of a block belongs to a block in the main chain:
4.    bool isInMainChain(const uint256 hash) const;
    
    /// Get the best height
5.    int getBestHeight() const { return _bestIndex->nHeight; }

Or e.g.:
6.    void getBlock(const uint256 hash, Block& block) const;

1-3. can be used to check if a COutPoint (now Coin) has been spent etc... This will only generate sane results, even if the two threads are both active on the same data structures.
Same goes for 4. and 6. copies a block from the block file to the Block& provided so no issues here either. 

I do, however, admit, that an extra review of all the public const methods would be wise, to ensure that I have not overlooked something. I'll open an issue on this and use a cold winter night on looking them over again.

Cheers,

Michael


> 
> I've been thinking about moving to read-write locks that allow multiple threads reading the datastructure simultaneously, but removing the locking all together sounds wrong to me.
> 
> -- 
> Pieter

Michael Gronager, PhD
Director, Ceptacle
Jens Juels Gade 33
2100 Copenhagen E
Mobile: +45 31 45 14 01
E-mail: gronager@ceptacle.com
Web: http://www.ceptacle.com/




  reply	other threads:[~2012-02-03  9:52 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-01 14:18 [Bitcoin-development] Announcement: libcoin Michael Grønager
2012-02-01 14:59 ` Gregory Maxwell
2012-02-01 15:50   ` Michael Grønager
2012-02-01 16:06     ` Jorge Timón
2012-02-01 15:02 ` Wladimir
2012-02-01 15:52   ` Michael Grønager
2012-02-01 15:09 ` slush
2012-02-01 15:57   ` Michael Grønager
2012-02-01 23:50     ` grarpamp
2012-02-02  8:32       ` Michael Grønager
2012-02-02 11:34         ` Craig B Agricola
2012-02-03  0:19         ` Pieter Wuille
2012-02-03  9:52           ` Michael Grønager [this message]
2012-02-01 15:26 ` Luke-Jr
2012-02-01 15:58   ` Michael Grønager
2012-02-01 16:15     ` Luke-Jr
2012-02-01 16:21       ` Michael Grønager
2012-02-01 16:23       ` Aidan Thornton
2012-02-01 16:20   ` Michael Grønager
2012-02-01 16:23     ` Luke-Jr
2012-02-01 17:37     ` Luke-Jr
2012-02-01 17:51       ` Michael Grønager
     [not found] ` <CAAS2fgSQZ1wv=OXnBnGbKnLTZXbn909umpPBaZDF2g6vy8katA@mail.gmail.com>
2012-02-02 17:12   ` Gregory Maxwell
2012-02-02 17:36     ` Gregory Maxwell
2012-02-02 17:46       ` Gregory Maxwell
2012-02-23 17:31 ` Martinx - ジェームズ
2012-02-23 19:48   ` Michael Grønager
2012-02-23 20:01   ` Michael Grønager
2012-02-23 20:35   ` Michael Grønager
2012-02-23 23:29     ` Martinx - ジェームズ
2012-02-24  2:17       ` Martinx - ジェームズ
2012-02-24  7:44         ` Michael Grønager
2012-02-24 16:17         ` Michael Grønager
2012-02-24 18:49           ` Martinx - ジェームズ
2012-02-24 19:40             ` Michael Grønager
2012-02-24 19:57             ` Michael Grønager
2012-02-25  2:11               ` Martinx - ジェームズ
2012-02-26 17:57                 ` Michael Grønager
2012-02-27 19:03                   ` Martinx - ジェームズ
2012-02-27 21:03                     ` Michael Grønager
2012-02-28  9:03                     ` Michael Grønager
     [not found]                       ` <CAJSM8J15LBiT9ojrPDE1-TXqmBLXcVvAmWw0e=5nQfLtMQ42Zg@mail.gmail.com>
     [not found]                         ` <8CEEE576-37DF-4101-9593-73D5FB66D52F@ceptacle.com>
2012-03-22 10:48                           ` Martinx - ジェームズ
2012-03-22 11:34                             ` Michael Grønager
2012-03-28  7:59                               ` Martinx - ジェームズ
2012-03-22 10:50                       ` Martinx - ジェームズ
2012-03-22 11:35                         ` Michael Grønager
2012-03-22 16:34                           ` Peter Vessenes
2012-03-27  9:58                             ` Martinx - ジェームズ
     [not found]                               ` <B4616B21-7C05-4793-8452-376EE4122BEC@ceptacle.com>
2012-04-15  4:32                                 ` Martinx - ジェームズ
2012-07-16 20:14 ` Martinx - ジェームズ
2012-09-12 23:27   ` Martinx - ジェームズ

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8CC24D9C-4207-4493-8C95-C5738B65C8B6@ceptacle.com \
    --to=gronager@ceptacle.com \
    --cc=bitcoin-development@lists.sourceforge.net \
    --cc=pieter.wuille@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox