Skip to content

Commit

Permalink
Merge pull request #378 from alephium/fix-encoding-map-key
Browse files Browse the repository at this point in the history
Fix encoding ByteVec type map key
  • Loading branch information
Lbqds committed Jun 20, 2024
2 parents 871eb6d + 43f96a2 commit afcf096
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 50 deletions.
16 changes: 8 additions & 8 deletions .project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"fullNodeVersion": "v3.1.0",
"fullNodeVersion": "v3.1.4",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
Expand Down Expand Up @@ -193,16 +193,16 @@
},
"InsertIntoMap": {
"sourceFile": "test/map.ral",
"sourceCodeHash": "f2f308aaa25fb2bfd984ce7e8f38ba846908a8c0e1a7396244d40e734327b77f",
"sourceCodeHash": "997a7ecabb3c68882a1c28b4889c252258493c1bcb1877d4afe992a872420d38",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
},
"MapTest": {
"sourceFile": "test/map.ral",
"sourceCodeHash": "f2f308aaa25fb2bfd984ce7e8f38ba846908a8c0e1a7396244d40e734327b77f",
"bytecodeDebugPatch": "=6-2+73=2-2+d8=2-2+69=11-1+a=50+7a7e0214696e73657274206174206d617020706174683a2000=56+7a7e0214696e73657274206174206d617020706174683a2000=227-1+9=124+7a7e021472656d6f7665206174206d617020706174683a2000=46+7a7e021472656d6f7665206174206d617020706174683a2000=6",
"codeHashDebug": "034dec32e1107787800ec476a31b11ed2f5c201c06aa9360d1a1a074d6b29098",
"sourceCodeHash": "997a7ecabb3c68882a1c28b4889c252258493c1bcb1877d4afe992a872420d38",
"bytecodeDebugPatch": "=6-2+a8=1-3+128=2-2+ea=10-2+4025=50+7a7e0214696e73657274206174206d617020706174683a2000=56+7a7e0214696e73657274206174206d617020706174683a2000=54+7a7e0214696e73657274206174206d617020706174683a2000=280-2+33=124+7a7e021472656d6f7665206174206d617020706174683a2000=46+7a7e021472656d6f7665206174206d617020706174683a2000=48+7a7e021472656d6f7665206174206d617020706174683a2000=6",
"codeHashDebug": "8666d70738c42748551e987c3eb46a2d7b1db5d82f5f6b2fdfc95b3906ff7477",
"warnings": [
"No external caller check for function \"MapTest.remove\". Please use \"checkCaller!(...)\" in the function or its callees, or disable it with \"@using(checkExternalCaller = false)\".",
"No external caller check for function \"MapTest.update\". Please use \"checkCaller!(...)\" in the function or its callees, or disable it with \"@using(checkExternalCaller = false)\".",
Expand All @@ -211,7 +211,7 @@
},
"MapValue": {
"sourceFile": "test/map.ral",
"sourceCodeHash": "f2f308aaa25fb2bfd984ce7e8f38ba846908a8c0e1a7396244d40e734327b77f",
"sourceCodeHash": "997a7ecabb3c68882a1c28b4889c252258493c1bcb1877d4afe992a872420d38",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
Expand Down Expand Up @@ -278,7 +278,7 @@
},
"RemoveFromMap": {
"sourceFile": "test/map.ral",
"sourceCodeHash": "f2f308aaa25fb2bfd984ce7e8f38ba846908a8c0e1a7396244d40e734327b77f",
"sourceCodeHash": "997a7ecabb3c68882a1c28b4889c252258493c1bcb1877d4afe992a872420d38",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
Expand Down Expand Up @@ -320,7 +320,7 @@
},
"UpdateMapValue": {
"sourceFile": "test/map.ral",
"sourceCodeHash": "f2f308aaa25fb2bfd984ce7e8f38ba846908a8c0e1a7396244d40e734327b77f",
"sourceCodeHash": "997a7ecabb3c68882a1c28b4889c252258493c1bcb1877d4afe992a872420d38",
"bytecodeDebugPatch": "",
"codeHashDebug": "",
"warnings": []
Expand Down
7 changes: 2 additions & 5 deletions artifacts/test/InsertIntoMap.ral.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "v2.14.7",
"version": "v3.1.4",
"name": "InsertIntoMap",
"bytecodeTemplate": "01010300020010{2}{3}17011700{1}d10e2ca2{1}160016010f0c{0}0100",
"bytecodeTemplate": "01010300020010{2}{3}17011700{1}d10f2ca2{1}160016010f0c{0}0100",
"fieldsSig": {
"names": [
"mapTest",
Expand All @@ -22,9 +22,6 @@
"functions": [
{
"name": "main",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down
21 changes: 7 additions & 14 deletions artifacts/test/MapTest.ral.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"version": "v2.14.7",
"version": "v3.1.4",
"name": "MapTest",
"bytecode": "0003404140a64105010303030016d3a9cdcc691600d1a2140a5f5f6d61705f5f305f5f160047441601b11602d202011600d1a2140a5f5f6d61705f5f315f5f16014044b11602d201010100010400402bd3c50ed2bb0c0d0d140a5f5f6d61705f5f305f5f16004744cb1703160301000c0d0d160301011702170116020d2a0c0e0c140a5f5f6d61705f5f305f5f16004744cb010216020d2a0c0e0c140a5f5f6d61705f5f315f5f16014044cb010201000104004025d392ac08660c0d0d140a5f5f6d61705f5f305f5f16004744cb1703160301000c0d0d160301011702170116000d0c140a5f5f6d61705f5f305f5f16004744cb010316000d0c140a5f5f6d61705f5f315f5f16014044cb0103",
"codeHash": "fa153233f4924fc2fd3e98b43209b838e569154481211662acdade8e20bb48c0",
"bytecode": "0003405c40dc415301030303001fd3a9cdcc691600d1a2140a5f5f6d61705f5f305f5f160047441601b11602d202011600d1a2140a5f5f6d61705f5f315f5f16014044b11602d201011600d1a2140a5f5f6d61705f5f325f5f1402001144b11602d2010101000104004036d3c50ed2bb0c0d0d140a5f5f6d61705f5f305f5f16004744cb1703160301000c0d0d160301011702170116020d2a0c0e0c140a5f5f6d61705f5f305f5f16004744cb010216020d2a0c0e0c140a5f5f6d61705f5f315f5f16014044cb010216020d2a0c0e0c140a5f5f6d61705f5f325f5f1402001144cb01020100010400402dd392ac08660c0d0d140a5f5f6d61705f5f305f5f16004744cb1703160301000c0d0d160301011702170116000d0c140a5f5f6d61705f5f305f5f16004744cb010316000d0c140a5f5f6d61705f5f315f5f16014044cb010316000d0c140a5f5f6d61705f5f325f5f1402001144cb0103",
"codeHash": "6ce8563011315ab1834d01457eb638c0a081c689b0daf1a59f8d914330a8673d",
"fieldsSig": {
"names": [],
"types": [],
Expand All @@ -12,9 +12,6 @@
"functions": [
{
"name": "insert",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [
"key",
"value"
Expand All @@ -31,9 +28,6 @@
},
{
"name": "update",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [
"key"
],
Expand All @@ -47,9 +41,6 @@
},
{
"name": "remove",
"usePreapprovedAssets": false,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [
"key"
],
Expand All @@ -67,11 +58,13 @@
"mapsSig": {
"names": [
"map0",
"map1"
"map1",
"map2"
],
"types": [
"Map[Address,MapValue]",
"Map[U256,U256]"
"Map[U256,U256]",
"Map[ByteVec,U256]"
]
}
}
5 changes: 1 addition & 4 deletions artifacts/test/RemoveFromMap.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.7",
"version": "v3.1.4",
"name": "RemoveFromMap",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0102",
"fieldsSig": {
Expand All @@ -19,9 +19,6 @@
"functions": [
{
"name": "main",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down
5 changes: 1 addition & 4 deletions artifacts/test/UpdateMapValue.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v2.14.7",
"version": "v3.1.4",
"name": "UpdateMapValue",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0101",
"fieldsSig": {
Expand All @@ -19,9 +19,6 @@
"functions": [
{
"name": "main",
"usePreapprovedAssets": true,
"useAssetsInContract": false,
"isPublic": true,
"paramNames": [],
"paramTypes": [],
"paramIsMutable": [],
Expand Down
45 changes: 37 additions & 8 deletions artifacts/ts/MapTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,22 @@ class Factory extends ContractFactory<MapTestInstance, {}> {
TestContractParams<
never,
{ key: Address; value: MapValue },
{ map0?: Map<Address, MapValue>; map1?: Map<bigint, bigint> }
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>,
"initialFields"
>
): Promise<
TestContractResult<
null,
{ map0?: Map<Address, MapValue>; map1?: Map<bigint, bigint> }
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>
> => {
return testMethod(this, "insert", params, getContractByCodeHash);
Expand All @@ -129,14 +137,22 @@ class Factory extends ContractFactory<MapTestInstance, {}> {
TestContractParams<
never,
{ key: Address },
{ map0?: Map<Address, MapValue>; map1?: Map<bigint, bigint> }
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>,
"initialFields"
>
): Promise<
TestContractResult<
null,
{ map0?: Map<Address, MapValue>; map1?: Map<bigint, bigint> }
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>
> => {
return testMethod(this, "update", params, getContractByCodeHash);
Expand All @@ -146,14 +162,22 @@ class Factory extends ContractFactory<MapTestInstance, {}> {
TestContractParams<
never,
{ key: Address },
{ map0?: Map<Address, MapValue>; map1?: Map<bigint, bigint> }
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>,
"initialFields"
>
): Promise<
TestContractResult<
null,
{ map0?: Map<Address, MapValue>; map1?: Map<bigint, bigint> }
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>
> => {
return testMethod(this, "remove", params, getContractByCodeHash);
Expand All @@ -165,8 +189,8 @@ class Factory extends ContractFactory<MapTestInstance, {}> {
export const MapTest = new Factory(
Contract.fromJson(
MapTestContractJson,
"=6-2+73=2-2+d8=2-2+69=11-1+a=50+7a7e0214696e73657274206174206d617020706174683a2000=56+7a7e0214696e73657274206174206d617020706174683a2000=227-1+9=124+7a7e021472656d6f7665206174206d617020706174683a2000=46+7a7e021472656d6f7665206174206d617020706174683a2000=6",
"034dec32e1107787800ec476a31b11ed2f5c201c06aa9360d1a1a074d6b29098",
"=6-2+a8=1-3+128=2-2+ea=10-2+4025=50+7a7e0214696e73657274206174206d617020706174683a2000=56+7a7e0214696e73657274206174206d617020706174683a2000=54+7a7e0214696e73657274206174206d617020706174683a2000=280-2+33=124+7a7e021472656d6f7665206174206d617020706174683a2000=46+7a7e021472656d6f7665206174206d617020706174683a2000=48+7a7e021472656d6f7665206174206d617020706174683a2000=6",
"8666d70738c42748551e987c3eb46a2d7b1db5d82f5f6b2fdfc95b3906ff7477",
AllStructs
)
);
Expand All @@ -188,6 +212,11 @@ export class MapTestInstance extends ContractInstance {
this.contractId,
"map1"
),
map2: new RalphMap<HexString, bigint>(
MapTest.contract,
this.contractId,
"map2"
),
};

async fetchState(): Promise<MapTestTypes.State> {
Expand Down
6 changes: 5 additions & 1 deletion contracts/test/map.ral
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ struct MapValue {
Contract MapTest() {
mapping[Address, MapValue] map0
mapping[U256, U256] map1
mapping[ByteVec, U256] map2

@using(preapprovedAssets = true)
pub fn insert(key: Address, value: MapValue) -> () {
map0.insert!(key, key, value)
map1.insert!(key, value.id, value.balance)
map2.insert!(key, #0011, value.balance)
}

pub fn update(key: Address) -> () {
let value = map0[key]
map0[key].balance = value.balance + 1
map1[value.id] = value.balance + 1
map2[#0011] = value.balance + 1
}

pub fn remove(key: Address) -> () {
let value = map0[key]
map0.remove!(key, key)
map1.remove!(key, value.id)
map2.remove!(key, #0011)
}
}

TxScript InsertIntoMap(mapTest: MapTest, from: Address, value: MapValue) {
mapTest.insert{from -> ALPH: mapEntryDeposit!() * 2}(from, value)
mapTest.insert{from -> ALPH: mapEntryDeposit!() * 3}(from, value)
}

TxScript RemoveFromMap(mapTest: MapTest, key: Address) {
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ services:
condition: service_healthy

alephium:
image: alephium/alephium:v3.1.0
image: alephium/alephium:v3.1.4
restart: unless-stopped
ports:
- 19973:19973/tcp
Expand Down
2 changes: 1 addition & 1 deletion packages/web3/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ function calcWrapperContractId(
group: number
): string {
const prefix = ralph.encodeMapPrefix(mapIndex)
const encodedKey = ralph.primitiveToByteVec(key, keyType)
const encodedKey = ralph.encodeMapKey(key, keyType)
const path = binToHex(prefix) + binToHex(encodedKey)
return subContractId(parentContractId, path, group)
}
Expand Down
11 changes: 11 additions & 0 deletions packages/web3/src/contract/ralph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,17 @@ describe('contract', function () {
)
})

it('should encode map key', () => {
expect(utils.binToHex(ralph.encodeMapKey(true, 'Bool'))).toEqual('01')
expect(utils.binToHex(ralph.encodeMapKey(false, 'Bool'))).toEqual('00')
expect(utils.binToHex(ralph.encodeMapKey(11011n, 'U256'))).toEqual('6b03')
expect(utils.binToHex(ralph.encodeMapKey(11011n, 'I256'))).toEqual('80002b03')
expect(utils.binToHex(ralph.encodeMapKey('1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH', 'Address'))).toEqual(
'00bee85f379545a2ed9f6cceb331288842f378cf0f04012ad4ac8824aae7d6f80a'
)
expect(utils.binToHex(ralph.encodeMapKey('00112233', 'ByteVec'))).toEqual('00112233')
})

// it('should test buildByteCode', async () => {
// const compiled = {
// type: 'TemplateContractByteCode',
Expand Down
4 changes: 2 additions & 2 deletions packages/web3/src/contract/ralph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export function decodePrimitive(value: Uint8Array, type: string): Val {
}
}

export function primitiveToByteVec(value: Val, type: string): Uint8Array {
export function encodeMapKey(value: Val, type: string): Uint8Array {
switch (type) {
case 'Bool':
const byte = toApiBoolean(value) ? 1 : 0
Expand All @@ -402,7 +402,7 @@ export function primitiveToByteVec(value: Val, type: string): Uint8Array {
return encodeU256(BigInt(u256))
case 'ByteVec':
const hexStr = toApiByteVec(value)
return encodeByteVec(hexStr)
return hexToBinUnsafe(hexStr)
case 'Address':
const address = toApiAddress(value)
return encodeAddress(address)
Expand Down
Loading

0 comments on commit afcf096

Please sign in to comment.