I'm trying to understand how "invalidateblock"-function works, literally line by line.

Since I'm at basic level in C++, I couldn't explain to myself what's the "&" after "Array" there to achieve though I know of references and pointers and though I've found out that it's a so called "type modifier". Still I'm wondering "Array" is a type just like "int", "char" etc.

Where can I find more information on where Array is defined/explained?What are member functions?

Thanks in advance!

  • 1
    I'm voting to close this question as off-topic because this question is not related to Bitcoin specifically and would be better suited for stackoverflow.com – cdecker May 18 '15 at 19:11
  • Ok, but I asked it here in order to hear developer's answers or comments who have written the code and are represented in Bitcoi.SE. – Aliakbar Ahmadi May 18 '15 at 20:41
  • Can you provide a link to the particular software your question is about? – David Schwartz May 18 '15 at 23:05
  • @DavidSchwartz: I've been looking at rpcblockchain.cpp of bitcoin-0.10.0 – Aliakbar Ahmadi May 19 '15 at 10:43

It's part of the JSON code. An Array is a JSON entity that contains an ordered list of zero or more JSON entities. As its name, "params", suggests, this one contains the parameters to the command. So when you see:

std::string strHash = params[0].get_str();

That gets the first parameter into a string called strHash. This makes sense since the first (and only) parameter to invalidateblock is a hash, as this help text indicates:

        "invalidateblock \"hash\"\n"
        "\nPermanently marks a block as invalid, as if it violated a consensus rule.\n"
        "1. hash   (string, required) the hash of the block to mark as invalid\n"
  • Thanks! But what's the purpose of "&" after "Array"? I think type modifiers, such as "&", are used in programming to convert a data type into another and obviously Array is being converted into a reference type while it's defined in the "invalidateblock"-function so can I assume that this conversion makes references out of Array's params? And if yes, what variables are they references to? What do they have to do with get_str()? – Aliakbar Ahmadi May 19 '15 at 10:55
  • @AliakbarAhmadi It sounds like you need to learn C++ and then come back and look at this code. – David Schwartz May 19 '15 at 16:41
  • I know I'm not using the best technical terms but would you give me a hint to understand: why I need to put a "&" after "Array" though - to my limited knowledge - you could leave it away and simply write ‍‍‍Value invalidateblock(const Array params, bool fHelp) {...‎‎‏}? - I found this but it's not really helping to understand this problem. But I think get_str() is just putting the hash value as a string to params[0]!? – Aliakbar Ahmadi May 19 '15 at 17:10
  • 1
    @AliakbarAhmadi Why do you think you need to do that? In this case, the parameter could be passed by value or by reference, but passing by reference is cheaper. Competent C++ coders reflexively pass "complex" classes by reference when that is possible because it avoids copying the value. – David Schwartz May 19 '15 at 17:16
  • David Schwartz, thanks! Finally you gave the answer and I can sleep well :D +1 – Aliakbar Ahmadi May 19 '15 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.