Skip to content

Commit

Permalink
feat: start support bytes conversion 2268 (#2316)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
zainen and Zainen Suzuki authored Jan 27, 2023
1 parent 4e5e9e9 commit 85bb273
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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();
56 changes: 56 additions & 0 deletions integration-tests/instructions-with-bytes-conversion.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});

});
});
2 changes: 2 additions & 0 deletions packages/taquito-local-forging/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/taquito-michel-codec/src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ const primitives: PrimID[] = [
'Lambda_rec',
'LAMBDA_REC',
'TICKET',
'BYTES',
'NAT',
];

const primTags: { [key in PrimID]?: number } & { [key: string]: number | undefined } =
Expand Down
10 changes: 9 additions & 1 deletion packages/taquito-michel-codec/src/michelson-typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
8 changes: 6 additions & 2 deletions packages/taquito-michel-codec/src/michelson-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ type MichelsonNoArgInstructionID =
| 'XOR'
| 'RENAME'
| 'OPEN_CHEST'
| 'MIN_BLOCK_TIME';
| 'MIN_BLOCK_TIME'
| 'BYTES'
| 'NAT';

type MichelsonRegularInstructionID =
| 'CONTRACT'
Expand Down Expand Up @@ -397,6 +399,7 @@ export enum Protocol {
PtJakart2 = 'PtJakart2xVj7pYXJBXrqHgd82rdkLey5ZeeGwDgPp9rhQUbSqY',
PtKathman = 'PtKathmankSpLLDALzWw7CGD2j2MtyveTwboEYokqUCP4a1LxMg',
PtLimaPtL = 'PtLimaPtLMwfNinJi9rCfDPWea8dFgTZ1MeJ9f1m2SRic6ayiwW',
PtMumbaii = 'PtMumbaiiFFEGbew1rRjzSPyzRbA51Tm3RVZL5suHPxSZYDhCEc',
ProtoALpha = 'ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK', // temporary protocol hash
}

Expand Down Expand Up @@ -426,7 +429,8 @@ const protoLevel: Record<ProtocolID, number> = {
PtJakart2xVj7pYXJBXrqHgd82rdkLey5ZeeGwDgPp9rhQUbSqY: 13,
PtKathmankSpLLDALzWw7CGD2j2MtyveTwboEYokqUCP4a1LxMg: 14,
PtLimaPtLMwfNinJi9rCfDPWea8dFgTZ1MeJ9f1m2SRic6ayiwW: 15,
ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK: 16,
PtMumbaiiFFEGbew1rRjzSPyzRbA51Tm3RVZL5suHPxSZYDhCEc: 16,
ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK: 17,
};

export function ProtoGreaterOfEqual(a: ProtocolID, b: ProtocolID): boolean {
Expand Down
2 changes: 2 additions & 0 deletions packages/taquito-michel-codec/src/michelson-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ const noArgInstructionIDs: Record<MichelsonNoArgInstruction['prim'], true> = {
OPEN_CHEST: true,
SUB_MUTEZ: true,
MIN_BLOCK_TIME: true,
BYTES: true,
NAT: true,
};

export const instructionIDs: Record<MichelsonInstruction['prim'], true> = Object.assign(
Expand Down
2 changes: 2 additions & 0 deletions packages/taquito-michel-codec/test/contracts_016.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const contracts: {
'not_bytes.tz',
'or_bytes.tz',
'xor_bytes.tz',
'bytes_of_int.tz',
'bytes_of_nat.tz',
],
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
Original file line number Diff line number Diff line change
@@ -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; }

0 comments on commit 85bb273

Please sign in to comment.