Parcourir la source

Add constraint on size of I64 magnitude, to make the ranges of the constructors equal

Tom Pointon il y a 3 ans
Parent
commit
95e0880f8c
2 fichiers modifiés avec 15 ajouts et 1 suppressions
  1. 5 1
      aptos/contracts/sources/error.move
  2. 10 0
      aptos/contracts/sources/i64.move

+ 5 - 1
aptos/contracts/sources/error.move

@@ -80,6 +80,10 @@ module pyth::error {
     }
     
    public fun invalid_governance_magic_value(): u64 {
-    error::invalid_argument(20)
+        error::invalid_argument(20)
+   }
+
+   public fun magnitude_too_large(): u64 {
+        error::invalid_argument(21)
    }
 }

+ 10 - 0
aptos/contracts/sources/i64.move

@@ -1,6 +1,8 @@
 module pyth::i64 {
     use pyth::error;
         
+    const MAX_MAGNITUDE: u64 = (1 << 63) - 1;
+
     /// As Move does not support negative numbers natively, we use our own internal
     /// representation.
     struct I64 has copy, drop, store {
@@ -9,6 +11,8 @@ module pyth::i64 {
     }
 
     public fun new(magnitude: u64, negative: bool): I64 {
+        assert!(magnitude <= MAX_MAGNITUDE, error::magnitude_too_large());
+
         I64 {
             magnitude: magnitude,
             negative: negative,
@@ -53,6 +57,12 @@ module pyth::i64 {
         inverted + 1
     }
 
+    #[test]
+    #[expected_failure(abort_code = 65557)]
+    fun test_magnitude_too_large() {
+        new(0x8000000000000000, false);
+    }
+
     #[test]
     fun test_from_u64_positive() {
         assert!(from_u64(0x64673) == new(0x64673, false), 1);