From 85bb273ad9bb53048ad1ac4c8f2bdd4afb978866 Mon Sep 17 00:00:00 2001 From: Zainen <72581075+zainen@users.noreply.github.com> Date: Fri, 27 Jan 2023 10:46:26 -0800 Subject: [PATCH] feat: start support bytes conversion 2268 (#2316) * feat: start support bytes conversion 2268 close #2268 * feat: add suport local-forging BYTES and NAT and tests' * chore: update test names --------- Co-authored-by: Zainen Suzuki --- ...nstructions-bytes-conversions-contracts.ts | 5 ++ ...instructions-with-bytes-conversion.spec.ts | 56 +++++++++++++++++++ .../taquito-local-forging/src/constants.ts | 2 + packages/taquito-michel-codec/src/binary.ts | 2 + .../src/michelson-typecheck.ts | 10 +++- .../src/michelson-types.ts | 8 ++- .../src/michelson-validator.ts | 2 + .../test/contracts_016.spec.ts | 2 + .../contracts_016/opcodes/bytes_of_int.tz | 25 +++++++++ .../contracts_016/opcodes/bytes_of_nat.tz | 14 +++++ 10 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 integration-tests/data/instructions-bytes-conversions-contracts.ts create mode 100644 integration-tests/instructions-with-bytes-conversion.spec.ts create mode 100644 packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_int.tz create mode 100644 packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_nat.tz diff --git a/integration-tests/data/instructions-bytes-conversions-contracts.ts b/integration-tests/data/instructions-bytes-conversions-contracts.ts new file mode 100644 index 0000000000..c935c2655d --- /dev/null +++ b/integration-tests/data/instructions-bytes-conversions-contracts.ts @@ -0,0 +1,5 @@ +import fs from 'fs'; +import path from 'path'; + +export const bytesAndInt = fs.readFileSync(path.resolve(`${__dirname}/../../packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_int.tz`)).toString(); +export const bytesAndNat = fs.readFileSync(path.resolve(`${__dirname}/../../packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_nat.tz`)).toString(); diff --git a/integration-tests/instructions-with-bytes-conversion.spec.ts b/integration-tests/instructions-with-bytes-conversion.spec.ts new file mode 100644 index 0000000000..b808bcba58 --- /dev/null +++ b/integration-tests/instructions-with-bytes-conversion.spec.ts @@ -0,0 +1,56 @@ +import { CONFIGS } from "./config"; +import { Protocols } from "@taquito/taquito"; +import { bytesAndInt, bytesAndNat } from "./data/instructions-bytes-conversions-contracts"; +import { HttpResponseError } from "@taquito/http-utils"; + +CONFIGS().forEach(({ lib, protocol, setup }) => { + const Tezos = lib; + const limanet = protocol === Protocols.PtLimaPtL ? test : test.skip; + const mumbaiAndAlpha = protocol === Protocols.PtMumbaii || protocol === Protocols.ProtoALpha ? test : test.skip; + + describe(`Test origination of contract with instructions now supporting bytes conversion`, () => { + + beforeEach(async (done) => { + await setup(); + done(); + }); + + mumbaiAndAlpha(`Should be able to originate a contract with BYTES -> INT -> BYTES instructions`, async done => { + const contract = await Tezos.contract.originate({ + code: bytesAndInt, + storage: 0 + }); + await contract.confirmation(); + expect(contract).toBeDefined(); + expect(contract.contractAddress).toContain("KT1"); + expect(contract.status).toEqual('applied'); + done(); + }); + + mumbaiAndAlpha(`Should be able to originate a contract with BYTES -> NAT -> BYTES instructions`, async done => { + const contract = await Tezos.contract.originate({ + code: bytesAndNat, + storage: 0 + }); + await contract.confirmation(); + expect(contract).toBeDefined(); + expect(contract.contractAddress).toContain("KT1"); + expect(contract.status).toEqual('applied'); + done(); + }); + + limanet('Should fail with non-supported BYTES and NAT instructions', async (done) => { + try { + const contract = await Tezos.contract.originate({ + code: bytesAndInt, + storage: 0 + }); + await contract.confirmation(); + } catch (err) { + expect(err).toBeInstanceOf(HttpResponseError); + } + done(); + }); + + }); +}); diff --git a/packages/taquito-local-forging/src/constants.ts b/packages/taquito-local-forging/src/constants.ts index 3ab4665c36..1c03e6b693 100644 --- a/packages/taquito-local-forging/src/constants.ts +++ b/packages/taquito-local-forging/src/constants.ts @@ -210,6 +210,8 @@ export const opMapping: { [key: string]: string } = { '98': 'Lambda_rec', '99': 'LAMBDA_REC', '9a': 'TICKET', + '9b': 'BYTES', + '9c': 'NAT', }; export const opMappingReverse = (() => { diff --git a/packages/taquito-michel-codec/src/binary.ts b/packages/taquito-michel-codec/src/binary.ts index 2ed109f86c..122e47ca0b 100644 --- a/packages/taquito-michel-codec/src/binary.ts +++ b/packages/taquito-michel-codec/src/binary.ts @@ -194,6 +194,8 @@ const primitives: PrimID[] = [ 'Lambda_rec', 'LAMBDA_REC', 'TICKET', + 'BYTES', + 'NAT', ]; const primTags: { [key in PrimID]?: number } & { [key: string]: number | undefined } = diff --git a/packages/taquito-michel-codec/src/michelson-typecheck.ts b/packages/taquito-michel-codec/src/michelson-typecheck.ts index 0f7c7f8676..62db963616 100644 --- a/packages/taquito-michel-codec/src/michelson-typecheck.ts +++ b/packages/taquito-michel-codec/src/michelson-typecheck.ts @@ -1296,9 +1296,17 @@ function functionTypeInternal( return [annotateVar({ prim: 'option', args: [{ prim: 'nat' }] }), ...stack.slice(1)]; case 'INT': - args(0, ['nat', 'bls12_381_fr']); + args(0, ['nat', 'bls12_381_fr', 'bytes']); return [annotateVar({ prim: 'int' }), ...stack.slice(1)]; + case 'BYTES': + args(0, ['nat', 'int']); + return [annotateVar({ prim: 'bytes' }), ...stack.slice(1)]; + + case 'NAT': + args(0, ['bytes']); + return [annotateVar({ prim: 'nat' }), ...stack.slice(1)]; + case 'NEG': { const s = args(0, ['nat', 'int', 'bls12_381_g1', 'bls12_381_g2', 'bls12_381_fr'])[0]; if (s.prim === 'nat' || s.prim === 'int') { diff --git a/packages/taquito-michel-codec/src/michelson-types.ts b/packages/taquito-michel-codec/src/michelson-types.ts index d00bedae93..ffd6cfde2e 100644 --- a/packages/taquito-michel-codec/src/michelson-types.ts +++ b/packages/taquito-michel-codec/src/michelson-types.ts @@ -79,7 +79,9 @@ type MichelsonNoArgInstructionID = | 'XOR' | 'RENAME' | 'OPEN_CHEST' - | 'MIN_BLOCK_TIME'; + | 'MIN_BLOCK_TIME' + | 'BYTES' + | 'NAT'; type MichelsonRegularInstructionID = | 'CONTRACT' @@ -397,6 +399,7 @@ export enum Protocol { PtJakart2 = 'PtJakart2xVj7pYXJBXrqHgd82rdkLey5ZeeGwDgPp9rhQUbSqY', PtKathman = 'PtKathmankSpLLDALzWw7CGD2j2MtyveTwboEYokqUCP4a1LxMg', PtLimaPtL = 'PtLimaPtLMwfNinJi9rCfDPWea8dFgTZ1MeJ9f1m2SRic6ayiwW', + PtMumbaii = 'PtMumbaiiFFEGbew1rRjzSPyzRbA51Tm3RVZL5suHPxSZYDhCEc', ProtoALpha = 'ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK', // temporary protocol hash } @@ -426,7 +429,8 @@ const protoLevel: Record = { PtJakart2xVj7pYXJBXrqHgd82rdkLey5ZeeGwDgPp9rhQUbSqY: 13, PtKathmankSpLLDALzWw7CGD2j2MtyveTwboEYokqUCP4a1LxMg: 14, PtLimaPtLMwfNinJi9rCfDPWea8dFgTZ1MeJ9f1m2SRic6ayiwW: 15, - ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK: 16, + PtMumbaiiFFEGbew1rRjzSPyzRbA51Tm3RVZL5suHPxSZYDhCEc: 16, + ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK: 17, }; export function ProtoGreaterOfEqual(a: ProtocolID, b: ProtocolID): boolean { diff --git a/packages/taquito-michel-codec/src/michelson-validator.ts b/packages/taquito-michel-codec/src/michelson-validator.ts index 65887b2f55..33abbff540 100644 --- a/packages/taquito-michel-codec/src/michelson-validator.ts +++ b/packages/taquito-michel-codec/src/michelson-validator.ts @@ -87,6 +87,8 @@ const noArgInstructionIDs: Record = { OPEN_CHEST: true, SUB_MUTEZ: true, MIN_BLOCK_TIME: true, + BYTES: true, + NAT: true, }; export const instructionIDs: Record = Object.assign( diff --git a/packages/taquito-michel-codec/test/contracts_016.spec.ts b/packages/taquito-michel-codec/test/contracts_016.spec.ts index ab2761fef3..2659091a7d 100644 --- a/packages/taquito-michel-codec/test/contracts_016.spec.ts +++ b/packages/taquito-michel-codec/test/contracts_016.spec.ts @@ -22,6 +22,8 @@ const contracts: { 'not_bytes.tz', 'or_bytes.tz', 'xor_bytes.tz', + 'bytes_of_int.tz', + 'bytes_of_nat.tz', ], }; diff --git a/packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_int.tz b/packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_int.tz new file mode 100644 index 0000000000..761e6d4c44 --- /dev/null +++ b/packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_int.tz @@ -0,0 +1,25 @@ +parameter unit; +storage unit; +code { DROP; + + PUSH int 0; BYTES; PUSH bytes 0x; ASSERT_CMPEQ; + PUSH int 1; BYTES; PUSH bytes 0x01; ASSERT_CMPEQ; + PUSH int 1193046; BYTES; PUSH bytes 0x123456; ASSERT_CMPEQ; + + PUSH bytes 0x123456; INT; PUSH int 1193046; ASSERT_CMPEQ; + PUSH bytes 0x0000123456; INT; PUSH int 1193046; ASSERT_CMPEQ; + PUSH bytes 0x; INT; PUSH int 0; ASSERT_CMPEQ; + PUSH bytes 0x0000; INT; PUSH int 0; ASSERT_CMPEQ; + + PUSH int -128; BYTES; PUSH bytes 0x80; ASSERT_CMPEQ; + PUSH int -129; BYTES; PUSH bytes 0xff7f; ASSERT_CMPEQ; + PUSH int -33024; BYTES; PUSH bytes 0xff7f00; ASSERT_CMPEQ; + PUSH int -4294967296; BYTES; PUSH bytes 0xff00000000; ASSERT_CMPEQ; + + PUSH bytes 0x80; INT; PUSH int -128; ASSERT_CMPEQ; + PUSH bytes 0xff7f; INT; PUSH int -129; ASSERT_CMPEQ; + PUSH bytes 0xff7f00; INT; PUSH int -33024; ASSERT_CMPEQ; + PUSH bytes 0xffffff7f00; INT; PUSH int -33024; ASSERT_CMPEQ; + PUSH bytes 0xff00000000; INT; PUSH int -4294967296; ASSERT_CMPEQ; + + UNIT; NIL operation; PAIR; } diff --git a/packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_nat.tz b/packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_nat.tz new file mode 100644 index 0000000000..c620bc3e23 --- /dev/null +++ b/packages/taquito-michel-codec/test/contracts_016/opcodes/bytes_of_nat.tz @@ -0,0 +1,14 @@ +parameter unit; +storage unit; +code { DROP; + + PUSH nat 0; BYTES; PUSH bytes 0x; ASSERT_CMPEQ; + PUSH nat 1; BYTES; PUSH bytes 0x01; ASSERT_CMPEQ; + PUSH nat 1193046; BYTES; PUSH bytes 0x123456; ASSERT_CMPEQ; + + PUSH bytes 0x123456; NAT; PUSH nat 1193046; ASSERT_CMPEQ; + PUSH bytes 0x0000123456; NAT; PUSH nat 1193046; ASSERT_CMPEQ; + PUSH bytes 0x; NAT; PUSH nat 0; ASSERT_CMPEQ; + PUSH bytes 0x0000; NAT; PUSH nat 0; ASSERT_CMPEQ; + + UNIT; NIL operation; PAIR; }