Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions src/core/UAccounts.pas
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ TProgressNotifyManyHelper = record helper for TProgressNotifyMany
function DoUpgradeToProtocol3 : Boolean;
function DoUpgradeToProtocol4 : Boolean;
function DoUpgradeToProtocol5 : Boolean;
function DoUpgradeToProtocol6 : Boolean;
function BufferBlocksHash : TBytesBuffer32Safebox;
public
Constructor Create;
Expand Down Expand Up @@ -479,7 +480,7 @@ function Check_Safebox_Names_Consistency(sb : TPCSafeBox; const title :String; v
implementation

uses
ULog, UAccountKeyStorage, math, UCommon, UPCOperationsBlockValidator;
ULog, {$IFnDEF USE_ABSTRACTMEM} UAccountKeyStorage,{$ENDIF} math, UCommon, UPCOperationsBlockValidator;

{ This function is for testing purpose only.
Will check if Account Names are well assigned and stored }
Expand Down Expand Up @@ -1267,7 +1268,7 @@ class function TAccountComp.LoadAccountFromStream(Stream: TStream; var Account:
Result := False;
if (Stream.Size - Stream.Position<8) then Exit;
Stream.Read(LSaved_protocol,SizeOf(LSaved_protocol));
if Not (LSaved_protocol in [CT_PROTOCOL_4,CT_PROTOCOL_5]) then Exit;
if Not (LSaved_protocol in [CT_PROTOCOL_4..CT_PROTOCOL_MAX]) then Exit;
Stream.Read(Account.account,Sizeof(Account.account));
if TStreamOp.ReadAnsiString(Stream,raw) < 0 then Exit;
TAccountComp.RawString2AccountInfo(raw,Account.accountInfo);
Expand Down Expand Up @@ -2577,6 +2578,8 @@ function TPCSafeBox.CanUpgradeToProtocol(newProtocolVersion : Word) : Boolean;
Result := (FCurrentProtocol=CT_PROTOCOL_3) And (BlocksCount >= CT_Protocol_Upgrade_v4_MinBlock);
end else if (newProtocolVersion=CT_PROTOCOL_5) then begin
Result := (FCurrentProtocol=CT_PROTOCOL_4) And (BlocksCount >= CT_Protocol_Upgrade_v5_MinBlock);
end else if (newProtocolVersion=CT_PROTOCOL_6) then begin
Result := (FCurrentProtocol=CT_PROTOCOL_5) And (BlocksCount >= CT_Protocol_Upgrade_v6_MinBlock);
end else Result := False;
end;

Expand Down Expand Up @@ -3345,6 +3348,13 @@ function TPCSafeBox.DoUpgradeToProtocol5: Boolean;
TLog.NewLog(ltInfo,ClassName,'End Upgraded to protocol 5 - New safeboxhash:'+TCrypto.ToHexaString(FSafeBoxHash));
end;

function TPCSafeBox.DoUpgradeToProtocol6: Boolean;
begin
FCurrentProtocol := CT_PROTOCOL_6;
Result := True;
TLog.NewLog(ltInfo,ClassName,'End Upgraded to protocol 6 - New safeboxhash:'+TCrypto.ToHexaString(FSafeBoxHash));
end;

function TPCSafeBox.BufferBlocksHash: TBytesBuffer32Safebox;
begin
{$IFnDEF USE_ABSTRACTMEM}
Expand Down Expand Up @@ -3421,8 +3431,10 @@ function TPCSafeBox.LoadSafeBoxChunkFromStream(Stream : TStream; checkAll : Bool
CT_PROTOCOL_4 : FCurrentProtocol := 3; // In order to allow Upgrade to V4
CT_PROTOCOL_5 : FCurrentProtocol := 5; // In order to upgrade to V4..V5
else
errors := 'Invalid protocol version or corrupted stream ('+IntToStr(sbHeader.protocol)+')';
exit;
if sbHeader.protocol>CT_PROTOCOL_MAX then begin
errors := 'Invalid protocol version or corrupted stream ('+IntToStr(sbHeader.protocol)+')';
exit;
end else FCurrentProtocol := sbHeader.protocol;
end;
if sbHeader.IsAChunk then begin
if (sbHeader.startBlock<>BlocksCount) then begin
Expand Down Expand Up @@ -3616,7 +3628,7 @@ function TPCSafeBox.LoadSafeBoxChunkFromStream(Stream : TStream; checkAll : Bool
finally
LBlockHashRate.Free;
end;
// Upgrade to Protocol 4,5... step:
// Upgrade to Protocol 4,5... step:CT_PROTOCOL_5
if (LBlock.blockchainInfo.protocol_version>FCurrentProtocol) then begin
if (LBlock.blockchainInfo.protocol_version = CT_PROTOCOL_4) then begin
FCurrentProtocol := CT_PROTOCOL_4;
Expand Down Expand Up @@ -3724,7 +3736,7 @@ class function TPCSafeBox.LoadSafeBoxStreamHeader(Stream: TStream; var sbHeader
if (raw.ToPrintable<>CT_MagicIdentificator) then exit;
if Stream.Size<8 then exit;
Stream.Read(w,SizeOf(w));
if not (w in [CT_PROTOCOL_1,CT_PROTOCOL_2,CT_PROTOCOL_3,CT_PROTOCOL_4,CT_PROTOCOL_5]) then exit;
if not (w in [CT_PROTOCOL_1..CT_PROTOCOL_MAX]) then exit;
sbHeader.protocol := w;
Stream.Read(safeBoxBankVersion,2);
if safeBoxBankVersion<>CT_SafeBoxBankVersion then exit;
Expand Down Expand Up @@ -4220,7 +4232,12 @@ function TPCSafeBox.IsValidNewOperationsBlock(const newOperationBlock: TOperatio
errors := 'Invalid PascalCoin protocol version: '+IntToStr( newOperationBlock.protocol_version )+' Current: '+IntToStr(CurrentProtocol)+' Previous:'+IntToStr(lastBlock.protocol_version);
exit;
end;
If (newOperationBlock.protocol_version=CT_PROTOCOL_5) then begin
If (newOperationBlock.protocol_version=CT_PROTOCOL_6) then begin
If (newOperationBlock.block<CT_Protocol_Upgrade_v6_MinBlock) then begin
errors := 'Upgrade to protocol version 6 available at block: '+IntToStr(CT_Protocol_Upgrade_v6_MinBlock);
exit;
end;
end else If (newOperationBlock.protocol_version=CT_PROTOCOL_5) then begin
If (newOperationBlock.block<CT_Protocol_Upgrade_v5_MinBlock) then begin
errors := 'Upgrade to protocol version 5 available at block: '+IntToStr(CT_Protocol_Upgrade_v5_MinBlock);
exit;
Expand Down Expand Up @@ -4255,6 +4272,7 @@ function TPCSafeBox.IsValidNewOperationsBlock(const newOperationBlock: TOperatio
or ((newOperationBlock.block = CT_Protocol_Upgrade_v3_MinBlock) and (newOperationBlock.protocol_version<>CT_PROTOCOL_3))
or ((newOperationBlock.block = CT_Protocol_Upgrade_v4_MinBlock) and (newOperationBlock.protocol_version<>CT_PROTOCOL_4))
or ((newOperationBlock.block = CT_Protocol_Upgrade_v5_MinBlock) and (newOperationBlock.protocol_version<>CT_PROTOCOL_5))
or ((newOperationBlock.block = CT_Protocol_Upgrade_v6_MinBlock) and (newOperationBlock.protocol_version<>CT_PROTOCOL_6))
then begin
errors := Format('In block %d protocol must be upgraded! Current %d',[newOperationBlock.block,newOperationBlock.protocol_version]);
exit;
Expand Down Expand Up @@ -4330,7 +4348,12 @@ class function TPCSafeBox.IsValidOperationBlock(const newOperationBlock: TOperat
// fee: Cannot be checked only with the safebox
// Checking valid protocol version
// protocol available is not checked
if (newOperationBlock.block >= CT_Protocol_Upgrade_v5_MinBlock) then begin
if (newOperationBlock.block >= CT_Protocol_Upgrade_v6_MinBlock) then begin
if Not newOperationBlock.protocol_version = CT_PROTOCOL_6 then begin
errors := Format('Invalid protocol version at block %d Found:%d Expected:%d',[newOperationBlock.block,newOperationBlock.protocol_version,CT_PROTOCOL_6]);
exit;
end;
end else if (newOperationBlock.block >= CT_Protocol_Upgrade_v5_MinBlock) then begin
if Not newOperationBlock.protocol_version = CT_PROTOCOL_5 then begin
errors := Format('Invalid protocol version at block %d Found:%d Expected:%d',[newOperationBlock.block,newOperationBlock.protocol_version,CT_PROTOCOL_5]);
exit;
Expand Down Expand Up @@ -4418,9 +4441,9 @@ function TPCSafeBox.GetActualTargetHash(protocolVersion : Word): TRawBytes;
tsReal := (ts1 - ts2);
If (protocolVersion=CT_PROTOCOL_1) then begin
Result := TPascalCoinProtocol.GetNewTarget(tsTeorical, tsReal,protocolVersion,False,TPascalCoinProtocol.TargetFromCompact(lastBlock.compact_target,lastBlock.protocol_version));
end else if (protocolVersion<=CT_PROTOCOL_5) then begin
end else if (protocolVersion<=CT_PROTOCOL_MAX) then begin
CalcBack := CalcBack DIV CT_CalcNewTargetLimitChange_SPLIT;
If CalcBack=0 then CalcBack := 1;
If CalcBack<=0 then CalcBack := 1;
ts2 := GetBlockInfo(BlocksCount-CalcBack-1).timestamp;
tsTeoricalStop := (CalcBack * CT_NewLineSecondsAvg);
tsRealStop := (ts1 - ts2);
Expand All @@ -4443,7 +4466,7 @@ function TPCSafeBox.GetActualTargetHash(protocolVersion : Word): TRawBytes;
end;
end;
end else begin
Raise Exception.Create('ERROR DEV 20180306-1 Protocol not valid');
Raise Exception.Create('ERROR DEV 20180306-1 Protocol not valid: '+IntToStr(protocolVersion));
end;
end;
end;
Expand Down Expand Up @@ -5079,6 +5102,15 @@ function TPCSafeBoxTransaction.Commit(const operationBlock: TOperationBlock;
end;
end;
end;
if (FFreezedAccounts.FCurrentProtocol<CT_PROTOCOL_6) And (operationBlock.protocol_version=CT_PROTOCOL_6) then begin
// First block with V6 protocol
if FFreezedAccounts.CanUpgradeToProtocol(CT_PROTOCOL_6) then begin
TLog.NewLog(ltInfo,ClassName,'Protocol upgrade to v6');
If not FFreezedAccounts.DoUpgradeToProtocol6 then begin
raise Exception.Create('Cannot upgrade to protocol v6 !');
end;
end;
end;
Result := true;
finally
FFreezedAccounts.EndThreadSave;
Expand Down
6 changes: 6 additions & 0 deletions src/core/UBlockChain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,8 @@ procedure TPCOperationsComp.Clear(DeleteOperations : Boolean);
{$ENDIF}
end else if (FOperationBlock.protocol_version=CT_PROTOCOL_4) And (FBank.SafeBox.CanUpgradeToProtocol(CT_PROTOCOL_5)) then begin
FOperationBlock.protocol_version := CT_PROTOCOL_5; // If minting... upgrade to Protocol 5
end else if (FOperationBlock.protocol_version=CT_PROTOCOL_5) And (FBank.SafeBox.CanUpgradeToProtocol(CT_PROTOCOL_6)) then begin
FOperationBlock.protocol_version := CT_PROTOCOL_6; // If minting... upgrade to Protocol 6
end;
if (FOperationBlock.protocol_version>=CT_PROTOCOL_4) then begin
FOperationsHashTree.Max0feeOperationsBySigner := 1; // Limit to 1 0-fee operation by signer
Expand Down Expand Up @@ -1836,6 +1838,8 @@ procedure TPCOperationsComp.SanitizeOperations;
{$ENDIF}
end else if (FOperationBlock.protocol_version=CT_PROTOCOL_4) And (FBank.SafeBox.CanUpgradeToProtocol(CT_PROTOCOL_5)) then begin
FOperationBlock.protocol_version := CT_PROTOCOL_5; // If minting... upgrade to Protocol 5
end else if (FOperationBlock.protocol_version=CT_PROTOCOL_5) And (FBank.SafeBox.CanUpgradeToProtocol(CT_PROTOCOL_6)) then begin
FOperationBlock.protocol_version := CT_PROTOCOL_6; // If minting... upgrade to Protocol 6
end;
FOperationBlock.block := FBank.BlocksCount;

Expand Down Expand Up @@ -3006,7 +3010,9 @@ function TStorage.SaveBank(forceSave : Boolean): Boolean;
if (Not forceSave) AND (Not TPCSafeBox.MustSafeBoxBeSaved(Bank.BlocksCount)) then exit; // No save
Try
Result := DoSaveBank;
{$IFnDEF USE_ABSTRACTMEM}
FBank.SafeBox.CheckMemory;
{$ENDIF}
Except
On E:Exception do begin
TLog.NewLog(lterror,Classname,'Error saving Bank: '+E.Message);
Expand Down
19 changes: 11 additions & 8 deletions src/core/UConst.pas
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ interface
{$IFDEF PRODUCTION}'00000003A29C32E84A539ADE24397D41D30116A6FAFEC17B7D9CED68A4238C92'{$ELSE}{$IFDEF TESTNET}''{$ELSE}{$ENDIF}{$ENDIF};


CT_NetServer_Port = {$IFDEF PRODUCTION}4004{$ELSE}{$IFDEF TESTNET}4204{$ELSE}{$ENDIF}{$ENDIF};
CT_JSONRPCMinerServer_Port = {$IFDEF PRODUCTION}4009{$ELSE}{$IFDEF TESTNET}4209{$ELSE}{$ENDIF}{$ENDIF};
CT_JSONRPC_Port = {$IFDEF PRODUCTION}4003{$ELSE}{$IFDEF TESTNET}4203{$ELSE}{$ENDIF}{$ENDIF};
CT_NetServer_Port = {$IFDEF PRODUCTION}4004{$ELSE}{$IFDEF TESTNET}4604{$ELSE}{$ENDIF}{$ENDIF};
CT_JSONRPCMinerServer_Port = {$IFDEF PRODUCTION}4009{$ELSE}{$IFDEF TESTNET}4609{$ELSE}{$ENDIF}{$ENDIF};
CT_JSONRPC_Port = {$IFDEF PRODUCTION}4003{$ELSE}{$IFDEF TESTNET}4603{$ELSE}{$ENDIF}{$ENDIF};
CT_AccountsPerBlock = 5;

CT_NewLineSecondsAvg: Cardinal = {$IFDEF PRODUCTION}300{$ELSE}{$IFDEF TESTNET}30{$ELSE}{$ENDIF}{$ENDIF};
Expand Down Expand Up @@ -76,7 +76,7 @@ interface
{$IFDEF PRODUCTION}$16000000{$ELSE}$08000000{$ENDIF};
{$ENDIF}
CT_MinCompactTarget_v5: Cardinal = // Minimum compact target of block if using Protocol 5 or higher
{$IFDEF PRODUCTION}$12000000{$ELSE}{$IFDEF TESTNET}$10000000{$ELSE}{$ENDIF}{$ENDIF};
{$IFDEF PRODUCTION}$12000000{$ELSE}{$IFDEF TESTNET}$0D000000{$ELSE}{$ENDIF}{$ENDIF};


CT_CalcNewTargetBlocksAverage: Cardinal = 100;
Expand Down Expand Up @@ -118,16 +118,19 @@ interface
CT_PROTOCOL_3 = 3;
CT_PROTOCOL_4 = 4;
CT_PROTOCOL_5 = 5;
CT_PROTOCOL_6 = 6;
CT_PROTOCOL_MAX = CT_PROTOCOL_6;
CT_BUILD_PROTOCOL = CT_PROTOCOL_5;

CT_BlockChain_Protocol_Available: Word = 5; // Protocol 5 flag
CT_Protocol_Upgrade_v2_MinBlock = {$IFDEF PRODUCTION}115000{$ELSE}50{$ENDIF};
CT_Protocol_Upgrade_v3_MinBlock = {$IFDEF PRODUCTION}210000{$ELSE}250{$ENDIF};
CT_Protocol_Upgrade_v4_MinBlock = {$IFDEF PRODUCTION}260000{$ELSE}400{$ENDIF};
CT_Protocol_Upgrade_v5_MinBlock = {$IFDEF PRODUCTION}378000{$ELSE}500{$ENDIF};
CT_Protocol_Upgrade_v6_MinBlock = {$IFDEF PRODUCTION}999999999{$ELSE}600{$ENDIF}; // TODO: ALLOW V6 activate setting a valid "min block" value


CT_MagicNetIdentification = {$IFDEF PRODUCTION}$0A043580{$ELSE}$05000004{$ENDIF};
CT_MagicNetIdentification = {$IFDEF PRODUCTION}$0A043580{$ELSE}$06000000{$ENDIF};

CT_NetProtocol_Version: Word = 10;
// IMPORTANT NOTE!!!
Expand All @@ -138,9 +141,9 @@ interface

CT_SafeBoxBankVersion : Word = 3; // Protocol 2 upgraded safebox version from 2 to 3

CT_MagicIdentificator: String = {$IFDEF PRODUCTION}'PascalCoin'{$ELSE}'PascalCoinTESTNET_5.Beta.4'{$ENDIF}; //
CT_MagicIdentificator: String = {$IFDEF PRODUCTION}'PascalCoin'{$ELSE}'PascalCoin_TESTNET_6'{$ENDIF}; //

CT_PascalCoin_Data_Folder : String = {$IFDEF PRODUCTION}'PascalCoin'{$ELSE}'PascalCoin_TESTNET_5.Beta.4'{$ENDIF}; //
CT_PascalCoin_Data_Folder : String = {$IFDEF PRODUCTION}'PascalCoin'{$ELSE}'PascalCoin_TESTNET_6'{$ENDIF}; //

CT_PseudoOp_Reward = $0;
// Value of Operations type in Protocol 1
Expand Down Expand Up @@ -195,7 +198,7 @@ interface
CT_OpSubtype_Data_Signer = 103;
CT_OpSubtype_Data_Receiver = 104;

CT_ClientAppVersion : String = {$IFDEF PRODUCTION}'5.4.beta'{$ELSE}{$IFDEF TESTNET}'TESTNET 5.4.beta'{$ELSE}{$ENDIF}{$ENDIF};
CT_ClientAppVersion : String = {$IFDEF PRODUCTION}'5.4.Beta'{$ELSE}{$IFDEF TESTNET}'TESTNET 6 pre 1'{$ELSE}{$ENDIF}{$ENDIF};

CT_Discover_IPs = {$IFDEF PRODUCTION}'bpascal1.dynamic-dns.net;bpascal2.dynamic-dns.net;pascalcoin1.dynamic-dns.net;pascalcoin2.dynamic-dns.net;pascalcoin1.dns1.us;pascalcoin2.dns1.us;pascalcoin1.dns2.us;pascalcoin2.dns2.us'
{$ELSE}'pascaltestnet1.dynamic-dns.net;pascaltestnet2.dynamic-dns.net;pascaltestnet1.dns1.us;pascaltestnet2.dns1.us'{$ENDIF};
Expand Down
4 changes: 3 additions & 1 deletion src/core/ULog.pas
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ destructor TLog.Destroy;
FThreadSafeLogEvent.Terminate;
FThreadSafeLogEvent.WaitFor;
FreeAndNil(FThreadSafeLogEvent);
_logs.Remove(Self);
if Assigned(_logs) then begin
_logs.Remove(Self);
end;
FreeAndNil(FFileStream);
l := FLogDataList.LockList;
try
Expand Down
4 changes: 2 additions & 2 deletions src/core/UTxMultiOperation.pas
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function TOpMultiOperation.SaveOpToStream(Stream: TStream; SaveExtendedData: Boo
// Will save protocol info
if FProtocolVersion<CT_PROTOCOL_5 then
w := CT_PROTOCOL_3
else w := CT_PROTOCOL_5;
else w := FProtocolVersion;
stream.Write(w,SizeOf(w));
// Save senders count
w := Length(FData.txSenders);
Expand Down Expand Up @@ -402,7 +402,7 @@ function TOpMultiOperation.LoadOpFromStream(Stream: TStream; LoadExtendedData: B
Try
// Read protocol info
stream.Read(LSavedProtocol,SizeOf(LSavedProtocol));
If (Not (LSavedProtocol in [CT_PROTOCOL_3,CT_PROTOCOL_5])) then Raise Exception.Create('Invalid protocol found '+IntToStr(LSavedProtocol));
If (Not (LSavedProtocol in [CT_PROTOCOL_3..CT_PROTOCOL_MAX])) then Raise Exception.Create('Invalid protocol found '+IntToStr(LSavedProtocol));
// Load senders
stream.Read(w,SizeOf(w));
If w>CT_MAX_MultiOperation_Senders then Raise Exception.Create('Max senders');
Expand Down