Skip to content

Commit

Permalink
fix incorrect max calculation in uint256 domain (#775)
Browse files Browse the repository at this point in the history
* fix incorrect max calculation in  uint256 domain

* add test for max uint256

* fixed negative test
  • Loading branch information
brennanjl authored May 28, 2024
1 parent 327a34f commit 8e0c11b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions internal/sql/pg/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ END$$;`

sqlCreateUUIDExtension = `CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`

// have to run this in a DO block because you cannot do CREATE DOMAIN IF NOT EXISTS
// have to run this in a DO block because you cannot do CREATE DOMAIN IF NOT EXISTS.
// We have to hard-code the string and then convert to numeric instead of using 2^256-1,
// because Postgres will not precisely evaluate 2^256-1.
sqlCreateUint256Domain = `
DO $$ BEGIN
CREATE DOMAIN uint256 AS NUMERIC(78)
CHECK (VALUE >= 0 AND VALUE < 2^256)
CHECK (VALUE >= 0 AND VALUE <= '115792089237316195423570985008687907853269984665640564039457584007913129639935'::NUMERIC(78))
CHECK (SCALE(VALUE) = 0);
EXCEPTION
WHEN duplicate_object THEN null;
Expand Down
10 changes: 9 additions & 1 deletion test/specifications/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ func testDecimal(ctx context.Context, t *testing.T, execute ProcedureDSL, dbid s

func testUint256(ctx context.Context, t *testing.T, execute ProcedureDSL, dbid string, callType string) {
// execute store
uint256 := types.Uint256FromInt(123)
uint256, err := types.Uint256FromString("115792089237316195423570985008687907853269984665640564039457584007913129639935") // max uint256
require.NoError(t, err)
uint256Arr := []any{types.Uint256FromInt(456), types.Uint256FromInt(789)}

signMu.Lock()
Expand All @@ -167,6 +168,13 @@ func testUint256(ctx context.Context, t *testing.T, execute ProcedureDSL, dbid s
}
}
assert.Equal(t, 1, count)

// test that greater than max uint256 is not allowed.
// we pass it as a string since our uint256 type will also error if it exceeds max uint256
res, err = execute.Execute(ctx, dbid, callType+"_store_uint256s", []any{"115792089237316195423570985008687907853269984665640564039457584007913129639936", uint256Arr})
require.NoError(t, err)

ExpectTxfail(t, execute, ctx, res)
}

func testText(ctx context.Context, t *testing.T, execute ProcedureDSL, dbid string, callType string) {
Expand Down

0 comments on commit 8e0c11b

Please sign in to comment.