Browse Source

Improvements and some fixed bugs

PascalCoin 3 years ago
parent
commit
3c905b2b9b

+ 1 - 0
src/core/UPCAbstractMem.pas

@@ -1022,6 +1022,7 @@ var
   LPointer: TAbstractMemPosition;
   LPointer: TAbstractMemPosition;
   LIndex: integer;
   LIndex: integer;
 begin
 begin
+  LPointer := 0;
   AItem.accumulatedWork := 0;
   AItem.accumulatedWork := 0;
   Move(ABytes[0], AItem.operationBlock.block, 4);
   Move(ABytes[0], AItem.operationBlock.block, 4);
   Move(ABytes[4], LPointer, 4);
   Move(ABytes[4], LPointer, 4);

+ 50 - 0
src/core/UPCDataTypes.pas

@@ -47,6 +47,7 @@ type
      function FromSerialized(const AStream : TStream) : Boolean; overload;
      function FromSerialized(const AStream : TStream) : Boolean; overload;
      function LoadFromTBytes(const ABytes : TBytes; var AStartIndex : Integer) : Boolean;
      function LoadFromTBytes(const ABytes : TBytes; var AStartIndex : Integer) : Boolean;
      function IsEqualTo(const ACompareTo : TECDSA_Public) : Boolean;
      function IsEqualTo(const ACompareTo : TECDSA_Public) : Boolean;
+     function GetCopy : TECDSA_Public;
   end;
   end;
 
 
   { TECDSA_Public_Raw is a TECDSA_Public stored in a single TRawBytes
   { TECDSA_Public_Raw is a TECDSA_Public stored in a single TRawBytes
@@ -65,6 +66,7 @@ type
   TECDSA_SIG = record
   TECDSA_SIG = record
      r: TRawBytes;
      r: TRawBytes;
      s: TRawBytes;
      s: TRawBytes;
+     function GetCopy : TECDSA_SIG;
   end;
   end;
   PECDSA_Public = ^TECDSA_Public; // Pointer to a TECDSA_SIG
   PECDSA_Public = ^TECDSA_Public; // Pointer to a TECDSA_SIG
 
 
@@ -91,6 +93,7 @@ type
     function ToSerialized : TBytes;
     function ToSerialized : TBytes;
     function FromSerialized(const ASerialized : TBytes) : Boolean;
     function FromSerialized(const ASerialized : TBytes) : Boolean;
     function LoadFromTBytes(const ABytes : TBytes; var AStartIndex : Integer) : Boolean;
     function LoadFromTBytes(const ABytes : TBytes; var AStartIndex : Integer) : Boolean;
+    function GetCopy : TAccountInfo;
   end;
   end;
 
 
   TOperationBlock = Record
   TOperationBlock = Record
@@ -108,6 +111,7 @@ type
     operations_hash: TRawBytes; // RAW sha256 (32 bytes) of Operations
     operations_hash: TRawBytes; // RAW sha256 (32 bytes) of Operations
     proof_of_work: TRawBytes;   // RAW 32 bytes
     proof_of_work: TRawBytes;   // RAW 32 bytes
     previous_proof_of_work: TRawBytes; // RAW 32 bytes
     previous_proof_of_work: TRawBytes; // RAW 32 bytes
+    function GetCopy : TOperationBlock;
   end;
   end;
 
 
   { TAccount }
   { TAccount }
@@ -125,6 +129,7 @@ type
     account_seal : TRawBytes;  // Protocol 5. PIP-0029 seal of data changes
     account_seal : TRawBytes;  // Protocol 5. PIP-0029 seal of data changes
     procedure Clear;
     procedure Clear;
     function GetLastUpdatedBlock : Cardinal;
     function GetLastUpdatedBlock : Cardinal;
+    function GetCopy : TAccount;
   End;
   End;
   PAccount = ^TAccount;
   PAccount = ^TAccount;
 
 
@@ -199,6 +204,13 @@ begin
   Result := LoadFromTBytes(ASerialized,i);
   Result := LoadFromTBytes(ASerialized,i);
 end;
 end;
 
 
+function TECDSA_Public.GetCopy: TECDSA_Public;
+begin
+  Result.EC_OpenSSL_NID := Self.EC_OpenSSL_NID;
+  Result.x := Copy(Self.x);
+  Result.y := Copy(Self.y);
+end;
+
 function TECDSA_Public.FromSerialized(const AStream: TStream): Boolean;
 function TECDSA_Public.FromSerialized(const AStream: TStream): Boolean;
 begin
 begin
   if AStream.Read(Self.EC_OpenSSL_NID,2)<>2 then Exit(False);
   if AStream.Read(Self.EC_OpenSSL_NID,2)<>2 then Exit(False);
@@ -301,6 +313,14 @@ begin
   Result := LoadFromTBytes(ASerialized,i);
   Result := LoadFromTBytes(ASerialized,i);
 end;
 end;
 
 
+function TAccountInfo.GetCopy: TAccountInfo;
+begin
+  Result := Self;
+  Result.accountKey         := Self.accountKey.GetCopy;
+  Result.new_publicKey      := Self.new_publicKey.GetCopy;
+  Result.hashed_secret      := Copy(Self.hashed_secret);
+end;
+
 function TAccountInfo.LoadFromTBytes(const ABytes: TBytes; var AStartIndex: Integer): Boolean;
 function TAccountInfo.LoadFromTBytes(const ABytes: TBytes; var AStartIndex: Integer): Boolean;
 var w : Word;
 var w : Word;
 begin
 begin
@@ -394,6 +414,15 @@ begin
   Self := CT_Account_NUL;
   Self := CT_Account_NUL;
 end;
 end;
 
 
+function TAccount.GetCopy: TAccount;
+begin
+  Result := Self;
+  Result.accountInfo        := Self.accountInfo.GetCopy;
+  Result.name               := Copy(Self.name);
+  Result.account_data       := Copy(Self.account_data);
+  Result.account_seal       := Copy(Self.account_seal);
+end;
+
 function TAccount.GetLastUpdatedBlock: Cardinal;
 function TAccount.GetLastUpdatedBlock: Cardinal;
 begin
 begin
   if (Self.updated_on_block_passive_mode>Self.updated_on_block_active_mode) then Result := Self.updated_on_block_passive_mode
   if (Self.updated_on_block_passive_mode>Self.updated_on_block_active_mode) then Result := Self.updated_on_block_passive_mode
@@ -438,5 +467,26 @@ end;
 
 
 
 
 
 
+{ TOperationBlock }
+
+function TOperationBlock.GetCopy: TOperationBlock;
+begin
+  Result := Self;
+  Result.account_key              := Self.account_key.GetCopy;
+  Result.block_payload            := Copy(Self.block_payload);
+  Result.initial_safe_box_hash    := Copy(Self.initial_safe_box_hash);
+  Result.operations_hash          := Copy(Self.operations_hash);
+  Result.proof_of_work            := Copy(Self.proof_of_work);
+  Result.previous_proof_of_work   := Copy(Self.previous_proof_of_work);
+end;
+
+{ TECDSA_SIG }
+
+function TECDSA_SIG.GetCopy: TECDSA_SIG;
+begin
+  Result.r := Copy(Self.r);
+  Result.s := Copy(Self.s);
+end;
+
 end.
 end.
 
 

+ 3 - 2
src/libraries/abstractmem/UAbstractMem.pas

@@ -535,6 +535,7 @@ begin
   AAMZone.Clear;
   AAMZone.Clear;
   AAMZone.position := APosition;
   AAMZone.position := APosition;
   AAMZone.size := 0;
   AAMZone.size := 0;
+  if APosition<0 then Exit;
   LZone.position := RoundSize(APosition);
   LZone.position := RoundSize(APosition);
   LZone.size := 0;
   LZone.size := 0;
   if (LZone.position <> APosition) or (LZone.position<HeaderSize)
   if (LZone.position <> APosition) or (LZone.position<HeaderSize)
@@ -1039,7 +1040,7 @@ end;
 
 
 function TMem.AbsoluteRead(const AAbsolutePosition: Int64; var ABuffer; ASize: Integer): Integer;
 function TMem.AbsoluteRead(const AAbsolutePosition: Int64; var ABuffer; ASize: Integer): Integer;
 begin
 begin
-  if AAbsolutePosition>=Length(FMem) then Exit(0)
+  if (AAbsolutePosition>=Length(FMem)) or (AAbsolutePosition<0) then Exit(0)
   else begin
   else begin
     if AAbsolutePosition + ASize > Length(FMem) then Result := Length(FMem) - AAbsolutePosition
     if AAbsolutePosition + ASize > Length(FMem) then Result := Length(FMem) - AAbsolutePosition
     else Result := ASize;
     else Result := ASize;
@@ -1050,7 +1051,7 @@ end;
 function TMem.AbsoluteWrite(const AAbsolutePosition: Int64; const ABuffer; ASize: Integer): Integer;
 function TMem.AbsoluteWrite(const AAbsolutePosition: Int64; const ABuffer; ASize: Integer): Integer;
 begin
 begin
   if ASize=0 then Exit(0);
   if ASize=0 then Exit(0);
-  if (AAbsolutePosition + ASize > Length(FMem)) or (ASize<0) then
+  if (AAbsolutePosition + ASize > Length(FMem)) or (ASize<0) or (AAbsolutePosition<0) then
     raise EAbstractMem.Create(Format('Write out of mem range from %d to %d (max %d)',
     raise EAbstractMem.Create(Format('Write out of mem range from %d to %d (max %d)',
     [AAbsolutePosition,AAbsolutePosition+ASize,High(FMem)]));
     [AAbsolutePosition,AAbsolutePosition+ASize,High(FMem)]));
   Move(ABuffer,FMem[AAbsolutePosition],ASize);
   Move(ABuffer,FMem[AAbsolutePosition],ASize);

+ 2 - 0
src/libraries/abstractmem/UFileMem.pas

@@ -130,6 +130,7 @@ end;
 
 
 function TFileMem.AbsoluteRead(const AAbsolutePosition: Int64; var ABuffer; ASize: Integer): Integer;
 function TFileMem.AbsoluteRead(const AAbsolutePosition: Int64; var ABuffer; ASize: Integer): Integer;
 begin
 begin
+  if (AAbsolutePosition<0) then raise EFileMem.Create(Format('%s.AbsoluteRead out of range %d size %d',[ClassName,AAbsolutePosition,ASize]));
   FFileStream.Position := AAbsolutePosition;
   FFileStream.Position := AAbsolutePosition;
   Result := FFileStream.Read(ABuffer,ASize);
   Result := FFileStream.Read(ABuffer,ASize);
   {$IFDEF ABSTRACTMEM_ENABLE_STATS}
   {$IFDEF ABSTRACTMEM_ENABLE_STATS}
@@ -140,6 +141,7 @@ end;
 
 
 function TFileMem.AbsoluteWrite(const AAbsolutePosition: Int64; const ABuffer; ASize: Integer): Integer;
 function TFileMem.AbsoluteWrite(const AAbsolutePosition: Int64; const ABuffer; ASize: Integer): Integer;
 begin
 begin
+  if (AAbsolutePosition<0) then raise EFileMem.Create(Format('%s.AbsoluteWrite out of range %d size %d',[ClassName,AAbsolutePosition,ASize]));
   FFileStream.Position := AAbsolutePosition;
   FFileStream.Position := AAbsolutePosition;
   Result := FFileStream.Write(ABuffer,ASize);
   Result := FFileStream.Write(ABuffer,ASize);
   CacheIsNOTStable;
   CacheIsNOTStable;