<commit_before>// Master Protocol transaction code

#include "base58.h"
#include "rpcserver.h"
#include "init.h"
#include "util.h"
#include "wallet.h"

#include <stdint.h>
#include <string.h>
#include <map>

#include <fstream>
#include <algorithm>

#include <vector>

#include <utility>
#include <string>

#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/find.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"

#include "leveldb/db.h"
#include "leveldb/write_batch.h"

#include <openssl/sha.h>

#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::int128_t;
using boost::multiprecision::cpp_int;
using namespace std;
using namespace boost;
using namespace boost::assign;
using namespace json_spirit;
using namespace leveldb;

#include "mastercore.h"

using namespace mastercore;

#include "mastercore_dex.h"
#include "mastercore_tx.h"
#include "mastercore_sp.h"

// initial packet interpret step
int CMPTransaction::step1()
{
  if (PACKET_SIZE_CLASS_A > pkt_size)  // class A could be 19 bytes
  {
    fprintf(mp_fp, "%s() ERROR PACKET TOO SMALL; size = %d, line %d, file: %s\n", __FUNCTION__, pkt_size, __LINE__, __FILE__);
    return -(PKT_ERROR -1);
  }

  // collect version
  memcpy(&version, &pkt[0], 2);
  swapByteOrder16(version);

  // blank out version bytes in the packet
  pkt[0]=0; pkt[1]=0;
  
  memcpy(&type, &pkt[0], 4);

  swapByteOrder32(type);

  fprintf(mp_fp, "version: %d, Class %s\n", version, !multi ? "A":"B");
  fprintf(mp_fp, "\t            type: %u (%s)\n", type, c_strMasterProtocolTXType(type));

  return (type);
}

// extract alert info for alert packets
int CMPTransaction::step2_Alert()
{
  const char *p = 4 + (char *)&pkt;
  std::vector<std::string>spstr;
  int error_code = 0;
  char alertString[SP_STRING_FIELD_LEN];

  // log, but don't include message until authorized
  fprintf(mp_fp, "DEBUG ALERT : Alert detected from %s\n",sender.c_str());
  printf("DEBUG ALERT : Alert detected from %s\n",sender.c_str());

  // is sender authorized?
  bool authorized = false;
  if (
     (sender=="mMichaelsAddress") || // Michael
     (sender=="mfaiZGBkY4mBqt3PHPD2qWgbaafGa7vR64") || //Faiz
     (sender=="mCraigAddress") || // Craig
     (sender=="mpZATHupfCLqet5N1YL48ByCM1ZBfddbGJ") //Zathras
     ) authorized = true;

  if(!authorized)
  {
      // not authorized, ignore alert
      fprintf(mp_fp, "\t      alert auth: false\n");
      return (PKT_ERROR -912);
  }
  else
  {
      // authorized, decode and make sure there are 4 tokens, then replace global_alert_message
      spstr.push_back(std::string(p));
      memcpy(alertString, spstr[0].c_str(), std::min(spstr[0].length(),sizeof(alertString)-1));

      std::vector<std::string> vstr;
      boost::split(vstr, alertString, boost::is_any_of(":"), token_compress_on);
      fprintf(mp_fp, "\t      alert auth: true\n");
      fprintf(mp_fp, "\t    alert sender: %s\n", sender.c_str());

      if (4 != vstr.size())
      {
          // there are not 4 tokens in the alert, badly formed alert and must discard
          fprintf(mp_fp, "\t    packet error: badly formed alert != 4 tokens\n");
          return (PKT_ERROR -911);
      }
      else
      {
          uint64_t expiryBlock;
          uint64_t expiryTime;
          uint64_t expiryVersion;
          string alertMessage;
          expiryBlock = atoi(vstr[0]);
          expiryTime = atoi(vstr[1]);
          expiryVersion = atoi(vstr[2]);
          alertMessage = vstr[3];
          fprintf(mp_fp, "\t    expiry block: %llu\n",(unsigned long long)expiryBlock);
          fprintf(mp_fp, "\t     expiry time: %llu\n",(unsigned long long)expiryTime);
          fprintf(mp_fp, "\t  expiry version: %llu\n",(unsigned long long)expiryVersion);
          fprintf(mp_fp, "\t   alert message: %s\n", alertMessage.c_str());
          // copy the alert string into the global_alert_message and return a 0 rc
//          global_alert_messaga=alertString;
          return 0;
      }
  }
}

// extract Value for certain types of packets
int CMPTransaction::step2_Value()
{
  memcpy(&nValue, &pkt[8], 8);
  swapByteOrder64(nValue);

  // here we are copying nValue into nNewValue to be stored into our leveldb later: MP_txlist
  nNewValue = nValue;

  memcpy(&currency, &pkt[4], 4);
  swapByteOrder32(currency);
<commit_msg>Switch to boost lexical cast<commit_after>// Master Protocol transaction code

#include "base58.h"
#include "rpcserver.h"
#include "init.h"
#include "util.h"
#include "wallet.h"

#include <stdint.h>
#include <string.h>
#include <map>

#include <fstream>
#include <algorithm>

#include <vector>

#include <utility>
#include <string>

#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/find.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"

#include "leveldb/db.h"
#include "leveldb/write_batch.h"

#include <openssl/sha.h>

#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::int128_t;
using boost::multiprecision::cpp_int;
using namespace std;
using namespace boost;
using namespace boost::assign;
using namespace json_spirit;
using namespace leveldb;

#include "mastercore.h"

using namespace mastercore;

#include "mastercore_dex.h"
#include "mastercore_tx.h"
#include "mastercore_sp.h"

// initial packet interpret step
int CMPTransaction::step1()
{
  if (PACKET_SIZE_CLASS_A > pkt_size)  // class A could be 19 bytes
  {
    fprintf(mp_fp, "%s() ERROR PACKET TOO SMALL; size = %d, line %d, file: %s\n", __FUNCTION__, pkt_size, __LINE__, __FILE__);
    return -(PKT_ERROR -1);
  }

  // collect version
  memcpy(&version, &pkt[0], 2);
  swapByteOrder16(version);

  // blank out version bytes in the packet
  pkt[0]=0; pkt[1]=0;
  
  memcpy(&type, &pkt[0], 4);

  swapByteOrder32(type);

  fprintf(mp_fp, "version: %d, Class %s\n", version, !multi ? "A":"B");
  fprintf(mp_fp, "\t            type: %u (%s)\n", type, c_strMasterProtocolTXType(type));

  return (type);
}

// extract alert info for alert packets
int CMPTransaction::step2_Alert()
{
  const char *p = 4 + (char *)&pkt;
  std::vector<std::string>spstr;
  int error_code = 0;
  char alertString[SP_STRING_FIELD_LEN];

  // is sender authorized?
  bool authorized = false;
  if (
     (sender=="mMichaelsAddress") || // Michael
     (sender=="mfaiZGBkY4mBqt3PHPD2qWgbaafGa7vR64") || //Faiz
     (sender=="mCraigAddress") || // Craig
     (sender=="mpZATHupfCLqet5N1YL48ByCM1ZBfddbGJ") //Zathras
     ) authorized = true;

  if(!authorized)
  {
      // not authorized, ignore alert
      fprintf(mp_fp, "\t      alert auth: false\n");
      return (PKT_ERROR -912);
  }
  else
  {
      // authorized, decode and make sure there are 4 tokens, then replace global_alert_message
      spstr.push_back(std::string(p));
      memcpy(alertString, spstr[0].c_str(), std::min(spstr[0].length(),sizeof(alertString)-1));

      std::vector<std::string> vstr;
      boost::split(vstr, alertString, boost::is_any_of(":"), token_compress_on);
      fprintf(mp_fp, "\t      alert auth: true\n");
      fprintf(mp_fp, "\t    alert sender: %s\n", sender.c_str());

      if (4 != vstr.size())
      {
          // there are not 4 tokens in the alert, badly formed alert and must discard
          fprintf(mp_fp, "\t    packet error: badly formed alert != 4 tokens\n");
          return (PKT_ERROR -911);
      }
      else
      {
          uint64_t expiryBlock;
          uint64_t expiryTime;
          uint64_t expiryVersion;
          string alertMessage;
          expiryBlock = boost::lexical_cast<uint64_t>(vstr[0]);
          expiryTime = boost::lexical_cast<uint64_t>(vstr[1]);
          expiryVersion = boost::lexical_cast<uint64_t>(vstr[2]);
          alertMessage = vstr[3];
          fprintf(mp_fp, "\t    expiry block: %llu\n",(unsigned long long)expiryBlock);
          fprintf(mp_fp, "\t     expiry time: %llu\n",(unsigned long long)expiryTime);
          fprintf(mp_fp, "\t  expiry version: %llu\n",(unsigned long long)expiryVersion);
          fprintf(mp_fp, "\t   alert message: %s\n", alertMessage.c_str());
          // copy the alert string into the global_alert_message and return a 0 rc
//          global_alert_messaga=alertString;
          return 0;
      }
  }
}

// extract Value for certain types of packets
int CMPTransaction::step2_Value()
{
  memcpy(&nValue, &pkt[8], 8);
  swapByteOrder64(nValue);

  // here we are copying nValue into nNewValue to be stored into our leveldb later: MP_txlist
  nNewValue = nValue;

  memcpy(&currency, &pkt[4], 4);
  swapByteOrder32(currency);

{'fraction_non_alphanumeric': 0.14014811490125673, 'fraction_numerical': 0.01615798922800718, 'mean_word_length': 3.526663280853225, 'pattern_counts': {'":': 4, '<': 44, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1838406', 'n_tokens_mistral': 3188, 'n_tokens_neox': 3102, 'n_words': 750}