Skip to content

Commit

Permalink
Parameter upgrades, not applied
Browse files Browse the repository at this point in the history
  • Loading branch information
mike31 committed Dec 18, 2017
1 parent 42d6ea4 commit 4410175
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 109 deletions.
82 changes: 82 additions & 0 deletions src/chainparams/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,55 @@ void* mc_MultichainParams::GetParam(const char *param,int* size)
return m_lpData+offset;
}

int mc_MultichainParams::CanBeUpgradedByVersion(const char *param,int version,int size)
{
if(m_lpIndex == NULL)
{
return -1;
}
int index=m_lpIndex->Get(param);
if(index<0)
{
return -2;
}
int offset=m_lpCoord[2 * index + 0];
if(offset<0)
{
return -3;
}

if(size)
{
if(size != m_lpCoord[2 * index + 1])
{
return -4;
}
}

if(version == 0)
{
return m_lpCoord[2 * index + 1];
}

if(strcmp(param,"maximumblocksize") == 0)
{
if(version >= 20002)
{
return m_lpCoord[2 * index + 1];
}
}

if(strcmp(param,"targetblocktime") == 0)
{
if(version >= 20002)
{
return m_lpCoord[2 * index + 1];
}
}

return 0;
}


int mc_MultichainParams::SetParam(const char *param,const char* value,int size)
{
Expand Down Expand Up @@ -454,6 +503,19 @@ double mc_MultichainParams::ParamAccuracy()
}


const mc_OneMultichainParam *mc_MultichainParams::FindParam(const char* param)
{
int i;
for(i=0;i<m_Count;i++)
{
if(strcmp((MultichainParamArray+i)->m_Name,param) == 0)
{
return MultichainParamArray+i;
}
}
return NULL;
}

int mc_MultichainParams::Read(const char* name)
{
return Read(name,0,NULL,0);
Expand Down Expand Up @@ -2003,3 +2065,23 @@ int mc_Features::PerAssetPermissions()
return ret;
}

int mc_Features::ParameterUpgrades()
{
int ret=0;
if(mc_gState->m_NetworkParams->IsProtocolMultichain() == 0)
{
return 0;
}
int protocol=mc_gState->m_NetworkParams->ProtocolVersion();

if(protocol)
{
if(protocol >= 20002)
{
ret=1;
}
}

return ret;
}

3 changes: 2 additions & 1 deletion src/chainparams/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ typedef struct mc_MultichainParams
int Import(const char *name,const char *source_address);
int Set(const char *name,const char *source,int source_size);

int FindParam(const char *param);
const mc_OneMultichainParam *FindParam(const char *param);
void* GetParam(const char *param,int* size);
int64_t GetInt64Param(const char *param);
double GetDoubleParam(const char *param);

int SetParam(const char *param,const char* value,int size);
int SetParam(const char *param,int64_t value);

int CanBeUpgradedByVersion(const char *param,int version,int size);

const char* Name();
const unsigned char* DefaultMessageStart();
Expand Down
1 change: 1 addition & 0 deletions src/chainparams/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ typedef struct mc_Features
int MultipleStreamKeys();
int FixedIsUnspendable();
int PerAssetPermissions();
int ParameterUpgrades();
} mc_Features;

typedef struct mc_BlockHeaderInfo
Expand Down
51 changes: 38 additions & 13 deletions src/entities/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,18 +675,21 @@ int mc_AssetDB::InsertEntity(const void* txid, int offset, int entity_type, cons
{
if(script)
{
value_offset=mc_FindSpecialParamInDetailsScript((unsigned char*)script,script_size,MC_ENT_SPRM_UPGRADE_PROTOCOL_VERSION,&value_size);
if(value_offset == script_size)
if(mc_gState->m_Features->ParameterUpgrades() == 0)
{
return MC_ERR_ERROR_IN_SCRIPT;
}
if( (value_size <=0) || (value_size > 4) )
{
return MC_ERR_ERROR_IN_SCRIPT;
}
if((int)mc_GetLE((unsigned char*)script+value_offset,value_size) < 0)
{
return MC_ERR_ERROR_IN_SCRIPT;
value_offset=mc_FindSpecialParamInDetailsScript((unsigned char*)script,script_size,MC_ENT_SPRM_UPGRADE_PROTOCOL_VERSION,&value_size);
if(value_offset == script_size)
{
return MC_ERR_ERROR_IN_SCRIPT;
}
if( (value_size <=0) || (value_size > 4) )
{
return MC_ERR_ERROR_IN_SCRIPT;
}
if((int)mc_GetLE((unsigned char*)script+value_offset,value_size) < 0)
{
return MC_ERR_ERROR_IN_SCRIPT;
}
}
value_offset=mc_FindSpecialParamInDetailsScript((unsigned char*)script,script_size,MC_ENT_SPRM_UPGRADE_START_BLOCK,&value_size);
if(value_offset != script_size)
Expand Down Expand Up @@ -1619,7 +1622,24 @@ int mc_AssetDB::FindEntityByFollowOn(mc_EntityDetails *entity,const unsigned cha
return 0;
}


const unsigned char* mc_EntityDetails::GetParamUpgrades(int *size)
{
uint32_t value_offset;
size_t value_size;

if(m_LedgerRow.m_ScriptSize)
{
value_offset=mc_FindSpecialParamInDetailsScript(m_LedgerRow.m_Script,m_LedgerRow.m_ScriptSize,MC_ENT_SPRM_UPGRADE_CHAIN_PARAMS,&value_size);
if(value_offset != m_LedgerRow.m_ScriptSize)
{
*size=(int)value_size;
return m_LedgerRow.m_Script+value_offset;
}
}

*size=0;
return NULL;
}

const char* mc_EntityDetails::GetName()
{
Expand Down Expand Up @@ -1784,12 +1804,17 @@ int mc_EntityDetails::UpgradeProtocolVersion()
{
unsigned char *ptr;
size_t bytes;
int version;
ptr=(unsigned char *)GetSpecialParam(MC_ENT_SPRM_UPGRADE_PROTOCOL_VERSION,&bytes);
if(ptr)
{
if((bytes>0) && (bytes<=4))
{
return (int)mc_GetLE(ptr,bytes);
version=(int)mc_GetLE(ptr,bytes);
if(version > 0)
{
return version;
}
}
}
return 0;
Expand Down
3 changes: 3 additions & 0 deletions src/entities/asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#define MC_ENT_SPRM_ASSET_MULTIPLE 0x41
#define MC_ENT_SPRM_UPGRADE_PROTOCOL_VERSION 0x42
#define MC_ENT_SPRM_UPGRADE_START_BLOCK 0x43
#define MC_ENT_SPRM_UPGRADE_CHAIN_PARAMS 0x44

#define MC_ENT_FLAG_OFFSET_IS_SET 0x00000001
#define MC_ENT_FLAG_NAME_IS_SET 0x00000010
Expand Down Expand Up @@ -147,6 +148,8 @@ typedef struct mc_EntityDetails
const unsigned char* GetFullRef();
const unsigned char* GetShortRef();
const unsigned char* GetScript();
const unsigned char* GetParamUpgrades(int *size);

int IsUnconfirmedGenesis();
int GetAssetMultiple();
int IsFollowOn();
Expand Down
113 changes: 103 additions & 10 deletions src/rpc/rpcrawdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,73 @@ CScript RawDataScriptCreateStream(Value *param,mc_Script *lpDetails,mc_Script *l
return scriptOpReturn;
}

bool AddParamNameValueToScript(const string param_name,const Value param_value,mc_Script *lpDetailsScript,int version,int *errorCode,string *strError)
{
int64_t value;
string name=param_name;

int size=-1;
unsigned char zero=0;
if(param_value.type() == bool_type)
{
value=param_value.get_bool() ? 1 : 0;
size=1;
}
if(param_value.type() == int_type)
{
value=param_value.get_int64();
size=0;
}
if(size < 0)
{
*errorCode=RPC_INVALID_PARAMETER;
*strError=string("Invalid parameter type");
return false;
}

name.erase(std::remove(name.begin(), name.end(), '-'), name.end());
size=mc_gState->m_NetworkParams->CanBeUpgradedByVersion(name.c_str(),version,size);

if(size == 1)
{
if(param_value.type() != bool_type)
{
*errorCode=RPC_INVALID_PARAMETER;
*strError=string("Invalid parameter type");
return false;
}
}

if(size == -4)
{
*errorCode=RPC_INVALID_PARAMETER;
*strError=string("Invalid parameter type");
return false;
}

if(size < 0)
{
*errorCode=RPC_INVALID_PARAMETER;
*strError=string("Invalid parameter name");
return false;
}

if(size == 0)
{
*errorCode=RPC_NOT_SUPPORTED;
*strError=string("One of parameters cannot be upgraded by this protocol version");
return false;
}

lpDetailsScript->SetData((unsigned char*)name.c_str(),name.size());
lpDetailsScript->SetData((unsigned char*)&zero,1);
lpDetailsScript->SetData((unsigned char*)&size,MC_PRM_PARAM_SIZE_BYTES);
lpDetailsScript->SetData((unsigned char*)&value,size);

return true;
}


CScript RawDataScriptCreateUpgrade(Value *param,mc_Script *lpDetails,mc_Script *lpDetailsScript,int *errorCode,string *strError)
{
CScript scriptOpReturn=CScript();
Expand All @@ -906,10 +973,14 @@ CScript RawDataScriptCreateUpgrade(Value *param,mc_Script *lpDetails,mc_Script *

bool missing_name=true;
bool missing_startblock=true;
bool missing_details=true;

lpDetails->Clear();
lpDetails->AddElement();


lpDetailsScript->Clear();
lpDetailsScript->AddElement();

protocol_version=-1;

BOOST_FOREACH(const Pair& d, param->get_obj())
Expand Down Expand Up @@ -966,7 +1037,7 @@ CScript RawDataScriptCreateUpgrade(Value *param,mc_Script *lpDetails,mc_Script *
}
if(d.name_ == "details")
{
if(protocol_version > 0)
if(!missing_details)
{
*strError=string("details field can appear only once in the object");
}
Expand All @@ -979,7 +1050,10 @@ CScript RawDataScriptCreateUpgrade(Value *param,mc_Script *lpDetails,mc_Script *
{
if( (p.value_.type() == int_type) && (p.value_.get_int() > 0) )
{
protocol_version=p.value_.get_int();
if(protocol_version < 0)
{
protocol_version=p.value_.get_int();
}
}
else
{
Expand All @@ -988,21 +1062,39 @@ CScript RawDataScriptCreateUpgrade(Value *param,mc_Script *lpDetails,mc_Script *
}
else
{
*strError=string("Invalid details");
if(mc_gState->m_Features->ParameterUpgrades())
{
AddParamNameValueToScript(p.name_,p.value_,lpDetailsScript,0,errorCode,strError);
}
else
{
*strError=string("Invalid details");
}
}
}

script = lpDetailsScript->GetData(0,&bytes);
if(strError->size() == 0)
{
if( (protocol_version <= 0) && (bytes == 0) )
{
*strError=string("Missing protocol-version");
}
}

if(strError->size() == 0)
{
if(protocol_version > 0)
{
lpDetails->SetSpecialParamValue(MC_ENT_SPRM_UPGRADE_PROTOCOL_VERSION,(unsigned char*)&protocol_version,4);
}
else
if(bytes)
{
*strError=string("Missing protocol-version");
}
lpDetails->SetSpecialParamValue(MC_ENT_SPRM_UPGRADE_CHAIN_PARAMS,script,bytes);
}
}
}
}
missing_details=false;
field_parsed=true;
}
if(d.name_ == "create")field_parsed=true;
Expand All @@ -1014,16 +1106,17 @@ CScript RawDataScriptCreateUpgrade(Value *param,mc_Script *lpDetails,mc_Script *

if(strError->size() == 0)
{
if(protocol_version < 0)
if(missing_details)
{
*strError=string("Missing protocol-version");
*strError=string("Missing details");
}
}

if(strError->size() == 0)
{
int err;
script=lpDetails->GetData(0,&bytes);
lpDetailsScript->Clear();
err=lpDetailsScript->SetNewEntityType(MC_ENT_TYPE_UPGRADE,0,script,bytes);
if(err)
{
Expand Down
Loading

0 comments on commit 4410175

Please sign in to comment.