Browse Source

Implement PIP-0030 on Safebox for Protocol v5

PascalCoin 6 years ago
parent
commit
0f760ef6ed
1 changed files with 24 additions and 4 deletions
  1. 24 4
      src/core/UAccounts.pas

+ 24 - 4
src/core/UAccounts.pas

@@ -24,7 +24,7 @@ interface
 
 
 uses
 uses
   Classes, SysUtils, UConst, UCrypto, SyncObjs, UThread, UBaseTypes,
   Classes, SysUtils, UConst, UCrypto, SyncObjs, UThread, UBaseTypes,
-  UPCOrderedLists, UPCDataTypes,
+  UPCOrderedLists, UPCDataTypes, UPCSafeBoxRootHash,
   {$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF};
   {$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF};
 
 
 {$I config.inc}
 {$I config.inc}
@@ -76,6 +76,7 @@ Type
     Class Procedure CalcProofOfWork(const operationBlock : TOperationBlock; out PoW : TRawBytes);
     Class Procedure CalcProofOfWork(const operationBlock : TOperationBlock; out PoW : TRawBytes);
     Class Function IsValidMinerBlockPayload(const newBlockPayload : TRawBytes) : Boolean;
     Class Function IsValidMinerBlockPayload(const newBlockPayload : TRawBytes) : Boolean;
     class procedure GetRewardDistributionForNewBlock(const OperationBlock : TOperationBlock; out acc_0_miner_reward, acc_4_dev_reward : Int64; out acc_4_for_dev : Boolean);
     class procedure GetRewardDistributionForNewBlock(const OperationBlock : TOperationBlock; out acc_0_miner_reward, acc_4_dev_reward : Int64; out acc_4_for_dev : Boolean);
+    class Function CalcSafeBoxHash(ABlocksHashBuffer : TBytesBuffer; protocol_version : Integer) : TRawBytes;
   end;
   end;
 
 
   TAccount = Record
   TAccount = Record
@@ -333,6 +334,7 @@ Type
     Function GetMinimumAvailableSnapshotBlock : Integer;
     Function GetMinimumAvailableSnapshotBlock : Integer;
     Function HasSnapshotForBlock(block_number : Cardinal) : Boolean;
     Function HasSnapshotForBlock(block_number : Cardinal) : Boolean;
     Property OrderedAccountKeysList : TOrderedAccountKeysList read FOrderedAccountKeysList;
     Property OrderedAccountKeysList : TOrderedAccountKeysList read FOrderedAccountKeysList;
+    property BufferBlocksHash: TBytesBuffer read FBufferBlocksHash; // Warning: For testing purposes, do not use this property! TODO delete or put in protected mode
   End;
   End;
 
 
 
 
@@ -680,6 +682,26 @@ begin
   end;
   end;
 end;
 end;
 
 
+class function TPascalCoinProtocol.CalcSafeBoxHash(ABlocksHashBuffer: TBytesBuffer; protocol_version: Integer): TRawBytes;
+begin
+  // If No buffer to hash is because it's fist block... so use Genesis: CT_Genesis_Magic_String_For_Old_Block_Hash
+  if (ABlocksHashBuffer.Length=0) then Result := TPCSafebox.InitialSafeboxHash
+  else begin
+
+    // Protection
+    Assert((ABlocksHashBuffer.Length MOD 32)=0,'ABlocksHashBuffer invalid length not modulo 32 = 0');
+    Assert((ABlocksHashBuffer.Length>0),'ABlocksHashBuffer length = 0');
+
+    if protocol_version<=CT_PROTOCOL_4 then begin
+      // A single SHA256 over the full size is made
+      Result := TCrypto.DoSha256(ABlocksHashBuffer.Memory,ABlocksHashBuffer.Length);
+    end else begin
+      // Implementation of PIP-0030, SafeboxHash will be calculated based on a MerkleTree obtaining a SafeboxRootHash
+      Result := TPCSafeboxRootHash.CalcSafeBoxRootHash(ABlocksHashBuffer);
+    end;
+  end;
+end;
+
 class procedure TPascalCoinProtocol.CalcProofOfWork(const operationBlock: TOperationBlock; out PoW: TRawBytes);
 class procedure TPascalCoinProtocol.CalcProofOfWork(const operationBlock: TOperationBlock; out PoW: TRawBytes);
 var ms : TMemoryStream;
 var ms : TMemoryStream;
   accKeyRaw : TRawBytes;
   accKeyRaw : TRawBytes;
@@ -2067,9 +2089,7 @@ function TPCSafeBox.CalcSafeBoxHash: TRawBytes;
 begin
 begin
   StartThreadSafe;
   StartThreadSafe;
   try
   try
-    // If No buffer to hash is because it's firts block... so use Genesis: CT_Genesis_Magic_String_For_Old_Block_Hash
-    if (FBufferBlocksHash.Length=0) then Result := InitialSafeboxHash
-    else Result := TCrypto.DoSha256(FBufferBlocksHash.Memory,FBufferBlocksHash.Length);
+    Result := TPascalCoinProtocol.CalcSafeBoxHash(FBufferBlocksHash,CurrentProtocol);
   finally
   finally
     EndThreadSave;
     EndThreadSave;
   end;
   end;