From 6785a879726c8356014fa09b9c020a78bdd41235 Mon Sep 17 00:00:00 2001 From: Pierre Seznec Date: Mon, 2 Oct 2023 12:23:38 +0200 Subject: [PATCH 1/2] fix storage append in vm-mock --- vm-mock/vm.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/vm-mock/vm.js b/vm-mock/vm.js index f08f2e3b..0ae444be 100644 --- a/vm-mock/vm.js +++ b/vm-mock/vm.js @@ -364,7 +364,7 @@ export default function createMockedABI( assembly_script_append_data(keyPtr, valuePtr) { const address = contractAddress; const key = ptrToUint8ArrayString(keyPtr); - const newValue = byteArrToUTF8String(getArrayBuffer(valuePtr)); + const newValue = getArrayBuffer(valuePtr); if (!ledger.has(address)) { throw new Error('Runtime error: address parsing error: ' + address); @@ -376,14 +376,18 @@ export default function createMockedABI( throw new Error('Runtime error: data entry not found'); } - const oldValue = byteArrToUTF8String(addressStorage.get(key)); - addressStorage.set(key, stringToByteArray(oldValue + newValue)); + const oldValue = addressStorage.get(key); + const concat = new Uint8Array(oldValue.byteLength + newValue.byteLength); + concat.set(new Uint8Array(oldValue), 0); + concat.set(new Uint8Array(newValue), oldValue.byteLength); + + addressStorage.set(key, concat); }, assembly_script_append_data_for(addressPtr, keyPtr, valuePtr) { const address = ptrToString(addressPtr); const key = ptrToUint8ArrayString(keyPtr); - const newValue = byteArrToUTF8String(getArrayBuffer(valuePtr)); + const newValue = getArrayBuffer(valuePtr); if (!ledger.has(address)) { throw new Error('Runtime error: address parsing error: ' + address); @@ -395,8 +399,12 @@ export default function createMockedABI( throw new Error('Runtime error: data entry not found'); } - const oldValue = byteArrToUTF8String(addressStorage.get(key)); - addressStorage.set(key, stringToByteArray(oldValue + newValue)); + const oldValue = addressStorage.get(key); + const concat = new Uint8Array(oldValue.byteLength + newValue.byteLength); + concat.set(new Uint8Array(oldValue), 0); + concat.set(new Uint8Array(newValue), oldValue.byteLength); + + addressStorage.set(key, concat); }, assembly_script_get_call_stack() { From f2f7a0271a5f26d046f7cd97a0b716b804358ae4 Mon Sep 17 00:00:00 2001 From: Pierre Seznec Date: Mon, 2 Oct 2023 13:27:13 +0200 Subject: [PATCH 2/2] support prefix for vm-mock getKeys --- vm-mock/vm.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/vm-mock/vm.js b/vm-mock/vm.js index 0ae444be..714844fb 100644 --- a/vm-mock/vm.js +++ b/vm-mock/vm.js @@ -524,22 +524,34 @@ export default function createMockedABI( console.log(a); }, - assembly_script_get_keys_for(aPtr) { + assembly_script_get_keys_for(aPtr, prefix) { const a = ptrToString(aPtr); if (!ledger.has(a)) return newArrayBuffer(''); const addressStorage = ledger.get(a).storage; - const keysArr = Array.from(addressStorage.keys()); + let keysArr = Array.from(addressStorage.keys()); + if(prefix) { + keysArr = keysArr.filter((key) => { + const prefixStr = ptrToUint8ArrayString(prefix); + return key.startsWith(prefixStr); + }); + } const keys = serializeKeys(keysArr); return newArrayBuffer(keys); }, - assembly_script_get_keys() { + assembly_script_get_keys(prefix) { const addressStorage = ledger.get(contractAddress).storage; - const keysArr = Array.from(addressStorage.keys()); - const keys = serializeKeys(keysArr); + let keysArr = Array.from(addressStorage.keys()); + if(prefix) { + keysArr = keysArr.filter((key) => { + const prefixStr = ptrToUint8ArrayString(prefix); + return key.startsWith(prefixStr); + }); + } + const keys = serializeKeys(keysArr); return newArrayBuffer(keys); },