Beispiel #1
0
// {
//   // if either of these parameters is set, a custom generator is used
//   difficulty: <number>       // optional
//   secret: <secret>           // optional
// }
Json::Value doProofCreate (RPC::Context& context)
{
    // XXX: Add ability to create proof with arbitrary time
    Json::Value     jvResult (Json::objectValue);

    if (context.params_.isMember ("difficulty") ||
        context.params_.isMember ("secret"))
    {
        // VFALCO TODO why aren't we using the app's factory?
        auto pgGen = make_ProofOfWorkFactory ();

        if (context.params_.isMember ("difficulty"))
        {
            if (!context.params_["difficulty"].isIntegral ())
                return RPC::invalid_field_error ("difficulty");

            int const iDifficulty (context.params_["difficulty"].asInt ());

            if (iDifficulty < 0 ||
                iDifficulty > ProofOfWorkFactory::kMaxDifficulty)
            {
                return RPC::invalid_field_error ("difficulty");
            }

            pgGen->setDifficulty (iDifficulty);
        }

        if (context.params_.isMember ("secret"))
        {
            uint256 uSecret (context.params_["secret"].asString ());
            pgGen->setSecret (uSecret);
        }

        jvResult["token"] = pgGen->getProof ().getToken ();
        jvResult["secret"] = to_string (pgGen->getSecret ());
    }
    else
    {
        jvResult["token"]
                = getApp().getProofOfWorkFactory ().getProof ().getToken ();
    }

    return jvResult;
}
Beispiel #2
0
// {
//   // if either of these parameters is set, a custom generator is used
//   difficulty: <number>       // optional
//   secret: <secret>           // optional
// }
Json::Value RPCHandler::doProofCreate (Json::Value params, Resource::Charge& loadType, Application::ScopedLockType& masterLockHolder)
{
    masterLockHolder.unlock ();
    // XXX: Add ability to create proof with arbitrary time

    Json::Value     jvResult (Json::objectValue);

    if (params.isMember ("difficulty") || params.isMember ("secret"))
    {
        // VFALCO TODO why aren't we using the app's factory?
        std::unique_ptr <ProofOfWorkFactory> pgGen (ProofOfWorkFactory::New ());

        if (params.isMember ("difficulty"))
        {
            if (!params["difficulty"].isIntegral ())
                return RPC::invalid_field_error ("difficulty");

            int const iDifficulty (params["difficulty"].asInt ());

            if (iDifficulty < 0 || iDifficulty > ProofOfWorkFactory::kMaxDifficulty)
                return RPC::invalid_field_error ("difficulty");

            pgGen->setDifficulty (iDifficulty);
        }

        if (params.isMember ("secret"))
        {
            uint256     uSecret (params["secret"].asString ());
            pgGen->setSecret (uSecret);
        }

        jvResult["token"]   = pgGen->getProof ().getToken ();
        jvResult["secret"]  = pgGen->getSecret ().GetHex ();
    }
    else
    {
        jvResult["token"]   = getApp().getProofOfWorkFactory ().getProof ().getToken ();
    }

    return jvResult;
}
Beispiel #3
0
// {
//   token: <token>
//   solution: <solution>
//   // if either of these parameters is set, a custom verifier is used
//   difficulty: <number>       // optional
//   secret: <secret>           // optional
// }
Json::Value doProofVerify (RPC::Context& context)
{
    // XXX Add ability to check proof against arbitrary time

    Json::Value jvResult;

    if (!context.params_.isMember ("token"))
        return RPC::missing_field_error ("token");

    if (!context.params_.isMember ("solution"))
        return RPC::missing_field_error ("solution");

    std::string strToken    = context.params_["token"].asString ();
    uint256 uSolution (context.params_["solution"].asString ());

    PowResult prResult;

    if (context.params_.isMember ("difficulty") ||
        context.params_.isMember ("secret"))
    {
        // VFALCO TODO why aren't we using the app's factory?
        auto pgGen (ProofOfWorkFactory::New ());

        if (context.params_.isMember ("difficulty"))
        {
            if (!context.params_["difficulty"].isIntegral ())
                return RPC::invalid_field_error ("difficulty");

            int iDifficulty = context.params_["difficulty"].asInt ();

            if (iDifficulty < 0 ||
                iDifficulty > ProofOfWorkFactory::kMaxDifficulty)
            {
                return RPC::missing_field_error ("difficulty");
            }

            pgGen->setDifficulty (iDifficulty);
        }

        if (context.params_.isMember ("secret"))
        {
            uint256     uSecret (context.params_["secret"].asString ());
            pgGen->setSecret (uSecret);
        }

        prResult                = pgGen->checkProof (strToken, uSolution);
        jvResult["secret"]      = to_string (pgGen->getSecret ());
    }
    else
    {
        // XXX Proof should not be marked as used from this
        prResult = getApp().getProofOfWorkFactory ().checkProof (
            strToken, uSolution);
    }

    std::string sToken;
    std::string sHuman;

    ProofOfWork::calcResultInfo (prResult, sToken, sHuman);

    jvResult["proof_result"]            = sToken;
    jvResult["proof_result_code"]       = prResult;
    jvResult["proof_result_message"]    = sHuman;

    return jvResult;
}