Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTScriptChai.cpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTScriptChai.cpp
4  *
5  */
6 
7 /************************************************************
8  -----BEGIN PGP SIGNED MESSAGE-----
9  Hash: SHA1
10 
11  * OPEN TRANSACTIONS
12  *
13  * Financial Cryptography and Digital Cash
14  * Library, Protocol, API, Server, CLI, GUI
15  *
16  * -- Anonymous Numbered Accounts.
17  * -- Untraceable Digital Cash.
18  * -- Triple-Signed Receipts.
19  * -- Cheques, Vouchers, Transfers, Inboxes.
20  * -- Basket Currencies, Markets, Payment Plans.
21  * -- Signed, XML, Ricardian-style Contracts.
22  * -- Scripted smart contracts.
23  *
24  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
25  *
26  * EMAIL:
28  *
29  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
30  *
31  * KEY FINGERPRINT (PGP Key in license file):
32  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
33  *
34  * OFFICIAL PROJECT WIKI(s):
35  * https://github.com/FellowTraveler/Moneychanger
36  * https://github.com/FellowTraveler/Open-Transactions/wiki
37  *
38  * WEBSITE:
39  * http://www.OpenTransactions.org/
40  *
41  * Components and licensing:
42  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
43  * -- otlib.........A class library.......LICENSE:...LAGPLv3
44  * -- otapi.........A client API..........LICENSE:...LAGPLv3
45  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
46  * -- otserver......Server Application....LICENSE:....AGPLv3
47  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
48  *
49  * All of the above OT components were designed and written by
50  * Fellow Traveler, with the exception of Moneychanger, which
51  * was contracted out to Vicky C ([email protected]).
52  * The open-source community has since actively contributed.
53  *
54  * -----------------------------------------------------
55  *
56  * LICENSE:
57  * This program is free software: you can redistribute it
58  * and/or modify it under the terms of the GNU Affero
59  * General Public License as published by the Free Software
60  * Foundation, either version 3 of the License, or (at your
61  * option) any later version.
62  *
63  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
64  * section 7: (This paragraph applies only to the LAGPLv3
65  * components listed above.) If you modify this Program, or
66  * any covered work, by linking or combining it with other
67  * code, such other code is not for that reason alone subject
68  * to any of the requirements of the GNU Affero GPL version 3.
69  * (==> This means if you are only using the OT API, then you
70  * don't have to open-source your code--only your changes to
71  * Open-Transactions itself must be open source. Similar to
72  * LGPLv3, except it applies to software-as-a-service, not
73  * just to distributing binaries.)
74  *
75  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
76  * used by Open Transactions: This program is released under
77  * the AGPL with the additional exemption that compiling,
78  * linking, and/or using OpenSSL is allowed. The same is true
79  * for any other open source libraries included in this
80  * project: complete waiver from the AGPL is hereby granted to
81  * compile, link, and/or use them with Open-Transactions,
82  * according to their own terms, as long as the rest of the
83  * Open-Transactions terms remain respected, with regard to
84  * the Open-Transactions code itself.
85  *
86  * Lucre License:
87  * This code is also "dual-license", meaning that Ben Lau-
88  * rie's license must also be included and respected, since
89  * the code for Lucre is also included with Open Transactions.
90  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
91  * The Laurie requirements are light, but if there is any
92  * problem with his license, simply remove the Lucre code.
93  * Although there are no other blind token algorithms in Open
94  * Transactions (yet. credlib is coming), the other functions
95  * will continue to operate.
96  * See Lucre on Github: https://github.com/benlaurie/lucre
97  * -----------------------------------------------------
98  * You should have received a copy of the GNU Affero General
99  * Public License along with this program. If not, see:
100  * http://www.gnu.org/licenses/
101  *
102  * If you would like to use this software outside of the free
103  * software license, please contact FellowTraveler.
104  * (Unfortunately many will run anonymously and untraceably,
105  * so who could really stop them?)
106  *
107  * DISCLAIMER:
108  * This program is distributed in the hope that it will be
109  * useful, but WITHOUT ANY WARRANTY; without even the implied
110  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
111  * PURPOSE. See the GNU Affero General Public License for
112  * more details.
113 
114  -----BEGIN PGP SIGNATURE-----
115  Version: GnuPG v1.4.9 (Darwin)
116 
117  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
118  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
119  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
120  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
121  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
122  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
123  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
124  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
125  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
126  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
127  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
128  kamH0Y/n11lCvo1oQxM+
129  =uSzz
130  -----END PGP SIGNATURE-----
131  **************************************************************/
132 
133 #include "stdafx.hpp"
134 
135 #include "OTLog.hpp"
136 #include "OTParty.hpp"
137 #include "OTPartyAccount.hpp"
138 #include "OTVariable.hpp"
139 
140 #ifdef OT_USE_SCRIPT_CHAI
141 #include "OTScriptChai.hpp"
142 #include <chaiscript/chaiscript.hpp>
143 #ifdef OT_USE_CHAI_STDLIB
144 #include <chaiscript/chaiscript_stdlib.hpp>
145 #endif
146 
147 namespace opentxs
148 {
149 
150 bool OTScriptChai::ExecuteScript(OTVariable* pReturnVar)
151 {
152  using namespace chaiscript;
153 
154  OT_ASSERT(nullptr != chai);
155 
156  if (m_str_script.size() > 0) {
157 
158  /*
159  chai->add(user_type<OTParty>(), "OTParty");
160  chai->add(constructor<OTParty()>(), "OTParty");
161  chai->add(constructor<OTParty(const OTParty&)>(), "OTParty");
162  chai->add(fun<OTParty&(OTParty::*)(const
163  OTParty&)>(&OTParty::operator=), "=");
164 
165  chai->add(fun(&OTParty::GetPartyName), "GetPartyName");
166  chai->add(fun(&OTParty::GetNymID), "GetNymID");
167  chai->add(fun(&OTParty::GetEntityID), "GetEntityID");
168  chai->add(fun(&OTParty::GetPartyID), "GetPartyID");
169  chai->add(fun(&OTParty::HasActiveAgent), "HasActiveAgent");
170  */
171 
172  // etc
173 
174  // chai->add(m); // Here we add the OTParty class to the
175  // chaiscript engine.
176 
177  for (auto& it : m_mapParties) {
178  OTParty* pParty = it.second;
179  OT_ASSERT(nullptr != pParty);
180 
181  std::string party_name = pParty->GetPartyName();
182 
183  // std::cerr << " TESTING PARTY: " << party_name <<
184  // std::endl;
185  // chai->add(chaiscript::var(&d), "d");
186 
187  // Currently I don't make the entire party available -- just his ID.
188  //
189  // Update: The client side uses constant variables only (a block or
190  // two down
191  // from here) and it stores the user's ID, and acct IDs, directly in
192  // those
193  // variables based on name. Whereas the server side passes in
194  // Parties and
195  // PartyAccounts, and only the names are made available inside the
196  // scripts.
197  // This way the scripts must entirely rely on the server-side API,
198  // functions
199  // such as move_funds(from_name, to_name), which expect a name, and
200  // translate
201  // only internally to resolve the ID.
202  // (Contrast this with client-side scripts, which actually have the
203  // real ID
204  // available inside the script, and which can call any OT API
205  // function that
206  // exists...)
207  //
208  chai->add_global_const(const_var(party_name),
209  party_name.c_str()); // Why name and not
210  // ID? See comment
211  // just above.
212  }
213 
214  for (auto& it : m_mapAccounts) {
215  OTPartyAccount* pAcct = it.second;
216  OT_ASSERT(nullptr != pAcct);
217 
218  std::string acct_name = pAcct->GetName().Get();
219 
220  // std::cerr << " TESTING ACCOUNT: " << acct_name <<
221  // std::endl;
222  // chai->add(chaiscript::var(&d), "d");
223 
224  // Currently I don't make the entire account available -- just his
225  // ID.
226  //
227  chai->add_global_const(const_var(acct_name),
228  acct_name.c_str()); // See comment in
229  // above block for
230  // party name.
231  }
232 
233  /*
234  enum OTVariable_Access
235  {
236  Var_Constant, // Constant -- you cannot change this value.
237  Var_Persistent, // Persistent -- changing value doesn't require
238  notice to parties.
239  Var_Important, // Important -- changing value requires
240  notice to parties.
241  Var_Error_Access // should never happen.
242  };
243 
244  OTVariable_Access GetAccess() const { return m_Access; }
245 
246  int64_t& GetValueLong() { return m_lValue; }
247  bool& GetValueBool() { return m_bValue; }
248  std::string& GetValueString() { return m_str_Value; }
249  */
250 
251  for (auto& it : m_mapVariables) {
252  const std::string var_name = it.first;
253  OTVariable* pVar = it.second;
254  OT_ASSERT((nullptr != pVar) && (var_name.size() > 0));
255 
256  switch (pVar->GetType()) {
258  int32_t& nValue = pVar->GetValueInteger();
259 
261  pVar->GetAccess()) // no pointer here, since it's constant.
262  chai->add_global_const(const_var(pVar->CopyValueInteger()),
263  var_name.c_str());
264  else
265  chai->add(var(&nValue), // passing ptr here so the
266  // script can modify this
267  // variable if it wants.
268  var_name.c_str());
269  } break;
270 
271  case OTVariable::Var_Bool: {
272  bool& bValue = pVar->GetValueBool();
273 
275  pVar->GetAccess()) // no pointer here, since it's constant.
276  chai->add_global_const(const_var(pVar->CopyValueBool()),
277  var_name.c_str());
278  else
279  chai->add(var(&bValue), // passing ptr here so the
280  // script can modify this
281  // variable if it wants.
282  var_name.c_str());
283  } break;
284 
285  case OTVariable::Var_String: {
286  std::string& str_Value = pVar->GetValueString();
287 
289  pVar->GetAccess()) // no pointer here, since it's constant.
290  {
291  chai->add_global_const(const_var(pVar->CopyValueString()),
292  var_name.c_str());
293 
294  // otErr << "\n\n\nOTSCRIPT DEBUGGING
295  // (const var added to script): %s\n\n\n",
296  // str_Value.c_str());
297  }
298  else {
299  chai->add(var(&str_Value), // passing ptr here so the
300  // script can modify this
301  // variable if it wants.
302  var_name.c_str());
303 
304  // otErr << "\n\n\nOTSCRIPT DEBUGGING
305  // var added to script: %s \n\n\n", str_Value.c_str());
306  }
307  } break;
308 
309  default:
310  otErr << "OTScriptChai::ExecuteScript: Failure: Unknown "
311  "variable type for variable: " << var_name << "\n";
312  return false;
313  }
314  }
315 
316  // Here we add the mapOfParties user-defined type to the chaiscript
317  // engine.
318  // chai->add(user_type<mapOfParties>(), "mapOfParties");
319 
320  // Here we add the m_mapParties member variable itself
321  // chai->add_global_const(const_var(m_mapParties),
322  // "Parties");
323 
324  try {
325  if (nullptr == pReturnVar) // Nothing to return.
326  chai->eval(m_str_script.c_str(),
327  exception_specification<const std::exception&>(),
328  m_str_display_filename);
329 
330  else // There's a return variable.
331  {
332  switch (pReturnVar->GetType()) {
334  int32_t nResult = chai->eval<int32_t>(
335  m_str_script.c_str(),
336  exception_specification<const std::exception&>(),
337  m_str_display_filename);
338  pReturnVar->SetValue(nResult);
339  } break;
340 
341  case OTVariable::Var_Bool: {
342  bool bResult = chai->eval<bool>(
343  m_str_script.c_str(),
344  exception_specification<const std::exception&>(),
345  m_str_display_filename);
346  pReturnVar->SetValue(bResult);
347  } break;
348 
349  case OTVariable::Var_String: {
350  std::string str_Result = chai->eval<std::string>(
351  m_str_script.c_str(),
352  exception_specification<const std::exception&>(),
353  m_str_display_filename);
354  pReturnVar->SetValue(str_Result);
355  } break;
356 
357  default:
358  otErr << "OTScriptChai::ExecuteScript: Unknown return type "
359  "passed in, "
360  "unable to service it.\n";
361  return false;
362  } // switch
363  } // else return variable.
364  } // try
365  catch (const chaiscript::exception::eval_error& ee) {
366  // Error in script parsing / execution
367  otErr << "OTScriptChai::ExecuteScript: \n Caught "
368  "chaiscript::exception::eval_error: \n " << ee.reason
369  << ". \n File: " << ee.filename
370  << "\n"
371  " Start position, line: " << ee.start_position.line
372  << " column: " << ee.start_position.column
373  << "\n"
374  " End position, line: " << ee.end_position.line
375  << " column: " << ee.end_position.column << "\n\n";
376 
377  std::cout << ee.what();
378  if (ee.call_stack.size() > 0) {
379  std::cout << "during evaluation at ("
380  << ee.call_stack[0]->start.line << ", "
381  << ee.call_stack[0]->start.column << ")";
382  }
383  std::cout << std::endl;
384  std::cout << std::endl;
385 
386  // std::cout << ee.what();
387  if (ee.call_stack.size() > 0) {
388  // std::cout << "during evaluation at (" <<
389  // *(ee.call_stack[0]->filename) << " " <<
390  // ee.call_stack[0]->start.line << ", " <<
391  // ee.call_stack[0]->start.column << ")";
392 
393  // const std::string text;
394  // boost::shared_ptr<const std::string> filename;
395 
396  for (size_t j = 1; j < ee.call_stack.size(); ++j) {
397  if (ee.call_stack[j]->identifier !=
398  chaiscript::AST_Node_Type::Block &&
399  ee.call_stack[j]->identifier !=
400  chaiscript::AST_Node_Type::File) {
401  std::cout << std::endl;
402  std::cout << " from " << *(ee.call_stack[j]->filename)
403  << " (" << ee.call_stack[j]->start.line
404  << ", " << ee.call_stack[j]->start.column
405  << ") : ";
406  std::cout << ee.call_stack[j]->text << std::endl;
407  }
408  }
409  }
410  std::cout << std::endl;
411 
412  return false;
413  }
414  catch (const chaiscript::exception::bad_boxed_cast& e) {
415  // Error unboxing return value
416  otErr << "OTScriptChai::ExecuteScript: Caught "
417  "chaiscript::exception::bad_boxed_cast : "
418  << ((e.what() != nullptr) ? e.what()
419  : "e.what() returned null, sorry")
420  << ".\n";
421  return false;
422  }
423  catch (const std::exception& e) {
424  // Error explicitly thrown from script
425  otErr << "OTScriptChai::ExecuteScript: Caught std::exception "
426  "exception: " << ((e.what() != nullptr)
427  ? e.what()
428  : "e.what() returned null, sorry")
429  << "\n";
430  return false;
431  }
432  // catch (chaiscript::Boxed_Value bv)
433  catch (...) {
434  // int32_t i = chaiscript::boxed_cast<int32_t>(bv);
435  otErr << "OTScriptChai::ExecuteScript: Caught exception.\n";
436  return false;
437  }
438  }
439 
440  return true;
441 }
442 
443 #if !defined(OT_USE_CHAI_STDLIB)
444 
445 OTScriptChai::OTScriptChai()
446  : OTScript()
447  , chai(new chaiscript::ChaiScript())
448 {
449 }
450 
451 OTScriptChai::OTScriptChai(const OTString& strValue)
452  : OTScript(strValue)
453  , chai(new chaiscript::ChaiScript())
454 {
455 }
456 
457 OTScriptChai::OTScriptChai(const char* new_string)
458  : OTScript(new_string)
459  , chai(new chaiscript::ChaiScript())
460 {
461 }
462 
463 OTScriptChai::OTScriptChai(const char* new_string, size_t sizeLength)
464  : OTScript(new_string, sizeLength)
465  , chai(new chaiscript::ChaiScript())
466 {
467 }
468 
469 OTScriptChai::OTScriptChai(const std::string& new_string)
470  : OTScript(new_string)
471  , chai(new chaiscript::ChaiScript())
472 {
473 }
474 
475 #else
476 
477 OTScriptChai::OTScriptChai()
478  : OTScript()
479  , chai(new chaiscript::ChaiScript(chaiscript::Std_Lib::library()))
480 {
481 }
482 
483 OTScriptChai::OTScriptChai(const OTString& strValue)
484  : OTScript(strValue)
485  , chai(new chaiscript::ChaiScript(chaiscript::Std_Lib::library()))
486 {
487 }
488 
489 OTScriptChai::OTScriptChai(const char* new_string)
490  : OTScript(new_string)
491  , chai(new chaiscript::ChaiScript(chaiscript::Std_Lib::library()))
492 {
493 }
494 
495 OTScriptChai::OTScriptChai(const char* new_string, size_t sizeLength)
496  : OTScript(new_string, sizeLength)
497  , chai(new chaiscript::ChaiScript(chaiscript::Std_Lib::library()))
498 {
499 }
500 
501 OTScriptChai::OTScriptChai(const std::string& new_string)
502  : OTScript(new_string)
503  , chai(new chaiscript::ChaiScript(chaiscript::Std_Lib::library()))
504 {
505 }
506 
507 #endif // OT_USE_CHAI_STDLIB
508 
509 OTScriptChai::~OTScriptChai()
510 {
511  if (nullptr != chai) delete chai;
512 }
513 
514 } // namespace opentxs
515 
516 #endif // OT_USE_SCRIPT_CHAI
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTLOG_IMPORT OTLogStream otErr