From e12a6a7fc56d9d29f854587d31a465f47ee6eeca Mon Sep 17 00:00:00 2001 From: jed Date: Fri, 7 Sep 2012 14:04:02 -0700 Subject: [PATCH] changed contract --- newcoin.vcxproj | 2 + newcoin.vcxproj.filters | 6 + src/Interpreter.cpp | 276 +++++++--------------------------------- src/Interpreter.h | 15 +-- src/LedgerFormats.cpp | 32 ++--- 5 files changed, 70 insertions(+), 261 deletions(-) diff --git a/newcoin.vcxproj b/newcoin.vcxproj index 6f7548d10..0f21cebef 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -124,6 +124,7 @@ + @@ -216,6 +217,7 @@ + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index fcf37fc26..f569d1ac4 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -291,6 +291,9 @@ Source Files + + Source Files + @@ -542,6 +545,9 @@ Header Files + + Header Files + diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index 1e3c4edc8..3c539cf30 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -1,4 +1,5 @@ #include "Interpreter.h" +#include "Operation.h" #include "Config.h" /* @@ -8,220 +9,6 @@ We also need to charge for each op namespace Script { - -////////////////////////////////////////////////////////////////////////// -/// Operations - -int Operation::getFee() -{ - return(theConfig.FEE_CONTRACT_OPERATION); -} - -// this is just an Int in the code -class IntOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer data=interpreter->getIntData(); - if(data->isInt32()) - { - interpreter->pushStack( data ); - return(true); - } - return(false); - } -}; - -class FloatOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer data=interpreter->getFloatData(); - if(data->isFloat()) - { - interpreter->pushStack( data ); - return(true); - } - return(false); - } -}; - -class Uint160Op : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer data=interpreter->getUint160Data(); - if(data->isUint160()) - { - interpreter->pushStack( data ); - return(true); - } - return(false); - } -}; - -class AddOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer data1=interpreter->popStack(); - Data::pointer data2=interpreter->popStack(); - if( (data1->isInt32() || data1->isFloat()) && - (data2->isInt32() || data2->isFloat()) ) - { - if(data1->isFloat() || data2->isFloat()) interpreter->pushStack(Data::pointer(new FloatData(data1->getFloat()+data2->getFloat()))); - else interpreter->pushStack(Data::pointer(new IntData(data1->getInt()+data2->getInt()))); - return(true); - }else - { - return(false); - } - } -}; - -class SubOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer data1=interpreter->popStack(); - Data::pointer data2=interpreter->popStack(); - if( (data1->isInt32() || data1->isFloat()) && - (data2->isInt32() || data2->isFloat()) ) - { - if(data1->isFloat() || data2->isFloat()) interpreter->pushStack(Data::pointer(new FloatData(data1->getFloat()-data2->getFloat()))); - else interpreter->pushStack(Data::pointer(new IntData(data1->getInt()-data2->getInt()))); - return(true); - }else - { - return(false); - } - } -}; - - -class StartBlockOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer offset=interpreter->getIntData(); - return(interpreter->startBlock(offset->getInt())); - } -}; - -class EndBlockOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - return(interpreter->endBlock()); - } -}; - -class StopOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - interpreter->stop(); - return(true); - } -}; - -class AcceptDataOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer data=interpreter->popStack(); - if(data->isInt32()) - { - interpreter->pushStack( interpreter->getAcceptData(data->getInt()) ); - return(true); - } - return(false); - } -}; - -class JumpIfOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer offset=interpreter->getIntData(); - Data::pointer cond=interpreter->popStack(); - if(cond->isBool() && offset->isInt32()) - { - if(cond->isTrue()) - { - return(interpreter->jumpTo(offset->getInt())); - } - return(true); - } - return(false); - } -}; - -class JumpOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer offset=interpreter->getIntData(); - if(offset->isInt32()) - { - return(interpreter->jumpTo(offset->getInt())); - } - return(false); - } -}; - -class SendXNSOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer sourceID=interpreter->popStack(); - Data::pointer destID=interpreter->popStack(); - Data::pointer amount=interpreter->popStack(); - if(sourceID->isUint160() && destID->isUint160() && amount->isInt32() && interpreter->canSign(sourceID->getUint160())) - { - // make sure: - // source is either, this contract, issuer, or acceptor - - // TODO do the send - //interpreter->pushStack( send result); - - return(true); - } - - return(false); - } -}; - -class GetDataOp : public Operation -{ -public: - bool work(Interpreter* interpreter) - { - Data::pointer index=interpreter->popStack(); - if(index->isInt32()) - { - interpreter->pushStack( interpreter->getContractData(index->getInt())); - return(true); - } - - return(false); - } -}; - -////////////////////////////////////////////////////////////////////////// - Interpreter::Interpreter() { mContract=NULL; @@ -234,32 +21,55 @@ Interpreter::Interpreter() mBlockJump=0; mFunctionTable.resize(NUM_OF_OPS); - + /* mFunctionTable[INT_OP]=new IntOp(); mFunctionTable[FLOAT_OP]=new FloatOp(); mFunctionTable[UINT160_OP]=new Uint160Op(); mFunctionTable[BOOL_OP]=new Uint160Op(); + mFunctionTable[PATH_OP]=new Uint160Op(); mFunctionTable[ADD_OP]=new AddOp(); mFunctionTable[SUB_OP]=new SubOp(); - - /*PATH_OP, - ADD_OP,SUB_OP,MUL_OP,DIV_OP,MOD_OP, - GTR_OP,LESS_OP,EQUAL_OP,NOT_EQUAL_OP, - AND_OP,OR_OP,NOT_OP, - JUMP_OP, JUMPIF_OP, - STOP_OP, CANCEL_OP, - - BLOCK_OP, BLOCK_END_OP, - SEND_XNS_OP,SEND_OP,REMOVE_CONTRACT_OP,FEE_OP,CHANGE_CONTRACT_OWNER_OP, - STOP_REMOVE_OP, - SET_DATA_OP,GET_DATA_OP, GET_NUM_DATA_OP, - SET_REGISTER_OP,GET_REGISTER_OP, - GET_ISSUER_ID_OP, GET_OWNER_ID_OP, GET_LEDGER_TIME_OP, GET_LEDGER_NUM_OP, GET_RAND_FLOAT_OP, - GET_XNS_ESCROWED_OP, GET_RIPPLE_ESCROWED_OP, GET_RIPPLE_ESCROWED_CURRENCY_OP, GET_RIPPLE_ESCROWED_ISSUER, - GET_ACCEPT_DATA_OP, GET_ACCEPTOR_ID_OP, GET_CONTRACT_ID_OP, - */ - + mFunctionTable[MUL_OP]=new MulOp(); + mFunctionTable[DIV_OP]=new DivOp(); + mFunctionTable[MOD_OP]=new ModOp(); + mFunctionTable[GTR_OP]=new GtrOp(); + mFunctionTable[LESS_OP]=new LessOp(); + mFunctionTable[EQUAL_OP]=new SubOp(); + mFunctionTable[NOT_EQUAL_OP]=new SubOp(); + mFunctionTable[AND_OP]=new SubOp(); + mFunctionTable[OR_OP]=new SubOp(); + mFunctionTable[NOT_OP]=new SubOp(); + mFunctionTable[JUMP_OP]=new SubOp(); + mFunctionTable[JUMPIF_OP]=new SubOp(); + mFunctionTable[STOP_OP]=new SubOp(); + mFunctionTable[CANCEL_OP]=new SubOp(); + mFunctionTable[BLOCK_OP]=new SubOp(); + mFunctionTable[BLOCK_END_OP]=new SubOp(); + mFunctionTable[SEND_XNS_OP]=new SendXNSOp(); + mFunctionTable[SEND_OP]=new SendOp(); + mFunctionTable[REMOVE_CONTRACT_OP]=new SubOp(); + mFunctionTable[FEE_OP]=new SubOp(); + mFunctionTable[CHANGE_CONTRACT_OWNER_OP]=new SubOp(); + mFunctionTable[STOP_REMOVE_OP]=new SubOp(); + mFunctionTable[SET_DATA_OP]=new SubOp(); + mFunctionTable[GET_DATA_OP]=new SubOp(); + mFunctionTable[GET_NUM_DATA_OP]=new SubOp(); + mFunctionTable[SET_REGISTER_OP]=new SubOp(); + mFunctionTable[GET_REGISTER_OP]=new SubOp(); + mFunctionTable[GET_ISSUER_ID_OP]=new SubOp(); + mFunctionTable[GET_OWNER_ID_OP]=new SubOp(); + mFunctionTable[GET_LEDGER_TIME_OP]=new SubOp(); + mFunctionTable[GET_LEDGER_NUM_OP]=new SubOp(); + mFunctionTable[GET_RAND_FLOAT_OP]=new SubOp(); + mFunctionTable[GET_XNS_ESCROWED_OP]=new SubOp(); + mFunctionTable[GET_RIPPLE_ESCROWED_OP]=new SubOp(); + mFunctionTable[GET_RIPPLE_ESCROWED_CURRENCY_OP]=new SubOp(); + mFunctionTable[GET_RIPPLE_ESCROWED_ISSUER]=new GetRippleEscrowedIssuerOp(); + mFunctionTable[GET_ACCEPT_DATA_OP]=new AcceptDataOp(); + mFunctionTable[GET_ACCEPTOR_ID_OP]=new GetAcceptorIDOp(); + mFunctionTable[GET_CONTRACT_ID_OP]=new GetContractIDOp(); + */ } Data::pointer Interpreter::popStack() diff --git a/src/Interpreter.h b/src/Interpreter.h index d09f6d220..7a0c1d825 100644 --- a/src/Interpreter.h +++ b/src/Interpreter.h @@ -10,20 +10,9 @@ namespace Script { -class Interpreter; - +class Operation; // Contracts are non typed have variable data types - -class Operation -{ -public: - // returns false if there was an error - virtual bool work(Interpreter* interpreter)=0; - - virtual int getFee(); -}; - class Interpreter { std::vector mFunctionTable; @@ -42,7 +31,7 @@ class Interpreter public: - enum { INT_OP,FLOAT_OP,UINT160_OP,BOOL_OP,PATH_OP, + enum { INT_OP=1,FLOAT_OP,UINT160_OP,BOOL_OP,PATH_OP, ADD_OP,SUB_OP,MUL_OP,DIV_OP,MOD_OP, GTR_OP,LESS_OP,EQUAL_OP,NOT_EQUAL_OP, AND_OP,OR_OP,NOT_OP, diff --git a/src/LedgerFormats.cpp b/src/LedgerFormats.cpp index e1376ea3a..79744a79e 100644 --- a/src/LedgerFormats.cpp +++ b/src/LedgerFormats.cpp @@ -25,6 +25,23 @@ LedgerEntryFormat LedgerFormats[]= { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, + { "Contract", ltCONTRACT, { + { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, + { S_FIELD(Account), STI_ACCOUNT, SOE_REQUIRED, 0 }, + { S_FIELD(Balance), STI_AMOUNT, SOE_REQUIRED, 0 }, + { S_FIELD(LastTxnID), STI_HASH256, SOE_REQUIRED, 0 }, + { S_FIELD(LastTxnSeq), STI_UINT32, SOE_REQUIRED, 0 }, + { S_FIELD(Issuer), STI_ACCOUNT, SOE_REQUIRED, 0 }, + { S_FIELD(Owner), STI_ACCOUNT, SOE_REQUIRED, 0 }, + { S_FIELD(Expiration), STI_UINT32, SOE_REQUIRED, 0 }, + { S_FIELD(BondAmount), STI_UINT32, SOE_REQUIRED, 0 }, + { S_FIELD(CreateCode), STI_VL, SOE_REQUIRED, 0 }, + { S_FIELD(FundCode), STI_VL, SOE_REQUIRED, 0 }, + { S_FIELD(RemoveCode), STI_VL, SOE_REQUIRED, 0 }, + { S_FIELD(ExpireCode), STI_VL, SOE_REQUIRED, 0 }, + { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, + { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } + }, { "DirectoryNode", ltDIR_NODE, { { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, { S_FIELD(Indexes), STI_VECTOR256, SOE_REQUIRED, 0 }, @@ -77,21 +94,6 @@ LedgerEntryFormat LedgerFormats[]= { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } }, - { "Contract", ltCONTRACT, { - { S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 }, - { S_FIELD(Issuer), STI_ACCOUNT, SOE_REQUIRED, 0 }, - { S_FIELD(Owner), STI_ACCOUNT, SOE_REQUIRED, 0 }, - { S_FIELD(Expiration), STI_UINT32, SOE_REQUIRED, 0 }, - { S_FIELD(BondAmount), STI_UINT32, SOE_REQUIRED, 0 }, - { S_FIELD(StampEscrow), STI_UINT32, SOE_REQUIRED, 0 }, - { S_FIELD(RippleEscrow), STI_AMOUNT, SOE_REQUIRED, 0 }, - { S_FIELD(CreateCode), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(FundCode), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(RemoveCode), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(ExpireCode), STI_VL, SOE_REQUIRED, 0 }, - { S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 }, - { sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } } - }, { NULL, ltINVALID } };