ThinBlockBuilder::ThinBlockBuilder(const CMerkleBlock& m, const TxFinder& txFinder) :
    missing(NOT_BUILDING)
{
    thinBlock.nVersion = m.header.nVersion;
    thinBlock.nBits = m.header.nBits;
    thinBlock.nNonce = m.header.nNonce;
    thinBlock.nTime = m.header.nTime;
    thinBlock.hashMerkleRoot = m.header.hashMerkleRoot;
    thinBlock.hashPrevBlock = m.header.hashPrevBlock;
    wantedTxs = getHashes(m);

    missing = wantedTxs.size();
    typedef std::vector<uint256>::const_iterator auto_;
    for (auto_ h = wantedTxs.begin(); h != wantedTxs.end(); ++h) {
        CTransaction tx = txFinder(*h);
        if (!tx.IsNull())
            --missing;
        // keep null txs, we'll download them later.
        // coinbase is guaranteed to be missing
        thinBlock.vtx.push_back(tx);
    }
    LogPrint("thin", "%d out of %d txs missing\n", missing, wantedTxs.size());
}
ThinBlockBuilder::TXAddRes ThinBlockBuilder::addTransaction(const CTransaction& tx) {
    assert(isValid());
    assert(!tx.IsNull());
    typedef std::vector<uint256>::iterator auto_;
    auto_ loc = std::find(
            wantedTxs.begin(), wantedTxs.end(), tx.GetHash());

    if (loc == wantedTxs.end()){
        // TX does not belong to block
        return TX_UNWANTED;
    }

    size_t offset = std::distance(wantedTxs.begin(), loc);

    if (!thinBlock.vtx[offset].IsNull()) {
        // We already have this one.
        return TX_DUP;
    }

    thinBlock.vtx[offset] = tx;
    missing--;
    return TX_ADDED;
}
Example #3
0
bool
CreateGameTransactions (CNameDB& nameDb, const GameState& gameState,
                        const StepResult& stepResult,
                        std::vector<CTransaction>& outvgametx)
{
  if (fDebug)
    printf ("Constructing game tx @%d...\n", gameState.nHeight);

  // Create resulting game transactions
  // Transaction hashes must be unique
  outvgametx.clear ();

  CTransaction txNew;
  txNew.SetGameTx ();

  // Destroy name-coins of killed players
  const PlayerSet& killedPlayers = stepResult.GetKilledPlayers ();
  const KilledByMap& killedBy = stepResult.GetKilledBy ();
  txNew.vin.reserve (killedPlayers.size ());
  BOOST_FOREACH(const PlayerID &victim, killedPlayers)
    {
      const vchType vchName = vchFromString (victim);
      CTransaction tx;
      if (!GetTxOfNameAtHeight (nameDb, vchName, gameState.nHeight, tx))
        return error ("Game engine killed a non-existing player %s",
                      victim.c_str ());

      if (fDebug)
        printf ("  killed: %s\n", victim.c_str ());

      CTxIn txin(tx.GetHash (), IndexOfNameOutput (tx));

      /* List all killers, if player was simultaneously killed by several
         other players.  If the reason was not KILLED_DESTRUCT, handle
         it also.  If multiple reasons apply, the game tx is constructed
         for the first reason according to the ordering inside of KilledByMap.
         (Which in turn is determined by the enum values for KILLED_*.)  */

      typedef KilledByMap::const_iterator Iter;
      const std::pair<Iter, Iter> iters = killedBy.equal_range (victim);
      if (iters.first == iters.second)
        return error ("No reason for killed player %s", victim.c_str ());
      const KilledByInfo::Reason reason = iters.first->second.reason;

      /* Unless we have destruct, there should be exactly one entry with
         the "first" reason.  There may be multiple entries for different
         reasons, for instance, killed by poison and staying in spawn
         area at the same time.  */
      {
        Iter it = iters.first;
        ++it;
        if (reason != KilledByInfo::KILLED_DESTRUCT && it != iters.second
            && reason == it->second.reason)
          return error ("Multiple same-reason, non-destruct killed-by"
                        " entries for %s", victim.c_str ());
      }

      switch (reason)
        {
        case KilledByInfo::KILLED_DESTRUCT:
          txin.scriptSig << vchName << GAMEOP_KILLED_BY;
          for (Iter it = iters.first; it != iters.second; ++it)
            {
              if (it->second.reason != KilledByInfo::KILLED_DESTRUCT)
                {
                  assert (it != iters.first);
                  break;
                }
              txin.scriptSig << vchFromString (it->second.killer.ToString ());
            }
          break;

        case KilledByInfo::KILLED_SPAWN:
          txin.scriptSig << vchName << GAMEOP_KILLED_BY;
          break;

        case KilledByInfo::KILLED_POISON:
          txin.scriptSig << vchName << GAMEOP_KILLED_POISON;
          break;

        default:
          assert (false);
        }

      txNew.vin.push_back (txin);
    }
  if (!txNew.IsNull ())
    {
      outvgametx.push_back (txNew);
      if (fDebug)
        printf ("Game tx for killed players: %s\n", txNew.GetHashForLog ());
    }

  /* Pay bounties to the players who collected them.  The transaction
     inputs are just "dummy" containing informational messages.  */
  txNew.SetNull ();
  txNew.SetGameTx ();
  txNew.vin.reserve (stepResult.bounties.size ());
  txNew.vout.reserve (stepResult.bounties.size ());

  BOOST_FOREACH(const CollectedBounty& bounty, stepResult.bounties)
    {
      const vchType vchName = vchFromString (bounty.character.player);
      CTransaction tx;
      if (!GetTxOfNameAtHeight (nameDb, vchName, gameState.nHeight, tx))
        return error ("Game engine created bounty for non-existing player");

      CTxOut txout;
      txout.nValue = bounty.loot.nAmount;

      if (!bounty.address.empty ())
        {
          /* Player-provided addresses are validated before accepting them,
             so failing here is ok.  */
          if (!txout.scriptPubKey.SetBitcoinAddress (bounty.address))
            return error ("Failed to set player-provided address for bounty");
        }
      else
        {
          // TODO: Maybe pay to the script of the name-tx without extracting the address first
          // (see source of GetNameAddress - it obtains the script by calling RemoveNameScriptPrefix)
          uint160 addr;
          if (!GetNameAddress (tx, addr))
            return error("Cannot get name address for bounty");
          txout.scriptPubKey.SetBitcoinAddress (addr);
        }

      txNew.vout.push_back (txout);

      CTxIn txin;
      txin.scriptSig
        << vchName << GAMEOP_COLLECTED_BOUNTY
        << bounty.character.index
        << bounty.loot.firstBlock
        << bounty.loot.lastBlock
        << bounty.loot.collectedFirstBlock
        << bounty.loot.collectedLastBlock;
      txNew.vin.push_back (txin);
    }
  if (!txNew.IsNull ())
    {
      outvgametx.push_back (txNew);
      if (fDebug)
        printf ("Game tx for bounties: %s\n", txNew.GetHashForLog ());
    }

  return true;
}
Example #4
0
bool CreateGameTransactions(CNameDB *pnameDb, const GameState &gameState, const StepResult &stepResult, std::vector<CTransaction> &outvgametx)
{
    // Create resulting game transactions
    // Transaction hashes must be unique
    outvgametx.clear();

    CTransaction txNew;
    txNew.SetGameTx();

    // Destroy name-coins of killed players
    txNew.vin.reserve(stepResult.killedPlayers.size());
    BOOST_FOREACH(const PlayerID &victim, stepResult.killedPlayers)
    {
        std::vector<unsigned char> vchName = vchFromString(victim);
        CTransaction tx;
        if (!pnameDb || !GetTxOfNameAtHeight(*pnameDb, vchName, gameState.nHeight, tx))
            return error("Game engine killed a non-existing player %s", victim.c_str());

        CTxIn txin(tx.GetHash(), IndexOfNameOutput(tx));

        txin.scriptSig << vchName << GAMEOP_KILLED_BY;

        // List all killers, if player was simultaneously killed by several other players
        typedef std::multimap<PlayerID, CharacterID>::const_iterator Iter;
        std::pair<Iter, Iter> iters = stepResult.killedBy.equal_range(victim);
        for (Iter it = iters.first; it != iters.second; ++it)
            txin.scriptSig << vchFromString(it->second.ToString());
        txNew.vin.push_back(txin);
    }
    if (!txNew.IsNull())
        outvgametx.push_back(txNew);

    // Pay bounties to the players who collected them
    txNew.SetNull();
    txNew.SetGameTx();
    txNew.vin.reserve(stepResult.bounties.size());      // Dummy inputs that contain informational messages only (one per each output)
    txNew.vout.reserve(stepResult.bounties.size());

    BOOST_FOREACH(const PAIRTYPE(CharacterID, CollectedLootInfo) &bounty, stepResult.bounties)
    {
        std::vector<unsigned char> vchName = vchFromString(bounty.first.player);
        CTransaction tx;
        if (!pnameDb || !GetTxOfNameAtHeight(*pnameDb, vchName, gameState.nHeight, tx))
            return error("Game engine created bounty for non-existing player");

        CTxOut txout;
        txout.nValue = bounty.second.nAmount;

        // Note: we only use the resulting game state to pay rewards. If we need to pay them to just-killed players,
        // this function should be modified to accept two game states, or the game state must be augmented with deadPlayers array.

        std::map<PlayerID, PlayerState>::const_iterator mi = gameState.players.find(bounty.first.player);
        if (mi == gameState.players.end())
            return error("Game engine created bounty for non-existing (dead?) player");

        if (!mi->second.address.empty())
        {
            // Player-provided addresses are validated before accepting them, so failing
            // here is ok
            if (!txout.scriptPubKey.SetBitcoinAddress(mi->second.address))
                return error("Failed to set player-provided address for bounty");
        }
        else
        {
            // TODO: Maybe pay to the script of the name-tx without extracting the address first
            // (see source of GetNameAddress - it obtains the script by calling RemoveNameScriptPrefix)
            uint160 addr;
            if (!GetNameAddress(tx, addr))
                return error("Cannot get name address for bounty");
            txout.scriptPubKey.SetBitcoinAddress(addr);
        }

        txNew.vout.push_back(txout);

        CTxIn txin;
        txin.scriptSig << vchName << GAMEOP_COLLECTED_BOUNTY
                << bounty.first.index
                << bounty.second.firstBlock
                << bounty.second.lastBlock
                << bounty.second.collectedFirstBlock
                << bounty.second.collectedLastBlock
            ;
        txNew.vin.push_back(txin);
    }
    if (!txNew.IsNull())
        outvgametx.push_back(txNew);

    return true;
}