From e68420f460912176c4e2e27fac47f3caa4053918 Mon Sep 17 00:00:00 2001 From: envin Date: Sun, 10 Nov 2024 23:05:07 +0100 Subject: [PATCH] test(eos): test set chain id function --- cpp/test/adapter-local.test.js | 7 +++++++ cpp/test/adapter-non-local.test.js | 7 +++++++ cpp/test/adapter.test.js | 31 ++++++++++++++++++++++++++++++ cpp/test/pam.test.js | 15 ++++++++++++++- cpp/test/utils/errors.js | 7 +++++-- 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/cpp/test/adapter-local.test.js b/cpp/test/adapter-local.test.js index 8f73d2f..0de095a 100644 --- a/cpp/test/adapter-local.test.js +++ b/cpp/test/adapter-local.test.js @@ -48,6 +48,7 @@ describe('Adapter Testing - Local deployment', () => { '000000000000000000000000bcf063a9eb18bc3c6eb005791c61801b7cb16fe4' const evmTopicZero = '66756e6473206172652073616675207361667520736166752073616675202e2e' + const EOSChainId = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906' const token = { symbol: symbol, @@ -198,6 +199,12 @@ describe('Adapter Testing - Local deployment', () => { .send(active(adapter.account)) }) + it('Should set the local chain id successfully', async () => { + await adapter.contract.actions + .setchainid([EOSChainId]) + .send(active(adapter.account)) + }) + it('Should add the tee public key successfully', async () => { const teePubKey = fromEthersPublicKey( evmEA.signingKey.compressedPublicKey, diff --git a/cpp/test/adapter-non-local.test.js b/cpp/test/adapter-non-local.test.js index cc5c6ba..618bbac 100644 --- a/cpp/test/adapter-non-local.test.js +++ b/cpp/test/adapter-non-local.test.js @@ -50,6 +50,7 @@ describe('Adapter Testing - Non Local Deployment', () => { '000000000000000000000000bcf063a9eb18bc3c6eb005791c61801b7cb16fe4' const evmTopicZero = '66756e6473206172652073616675207361667520736166752073616675202e2e' + const EOSChainId = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906' const token = { symbol, @@ -181,6 +182,12 @@ describe('Adapter Testing - Non Local Deployment', () => { .send(active(adapter.account)) }) + it('Should set the local chain id successfully', async () => { + await adapter.contract.actions + .setchainid([EOSChainId]) + .send(active(adapter.account)) + }) + it('Should add the tee public key successfully', async () => { const teePubKey = fromEthersPublicKey( evmEA.signingKey.compressedPublicKey, diff --git a/cpp/test/adapter.test.js b/cpp/test/adapter.test.js index 8a35bae..95d9ab0 100644 --- a/cpp/test/adapter.test.js +++ b/cpp/test/adapter.test.js @@ -25,6 +25,7 @@ const { TimePointSec } = require('@wharfkit/antelope') const TEE_ADDRESS_CHANGE_GRACE_PERIOD_MS = 172800 * 1000 const TABLE_STORAGE = 'storage' const TABLE_TEE = 'tee' +const TABLE_LOCAL_CHAIN_ID = 'chainid' describe('Adapter tests', () => { const symbol = 'TST' @@ -48,6 +49,7 @@ describe('Adapter tests', () => { '000000000000000000000000bcf063a9eb18bc3c6eb005791c61801b7cb16fe4' const evmTopicZero = '66756e6473206172652073616675207361667520736166752073616675202e2e' + const EOSChainId = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906' const token = { symbol, @@ -367,6 +369,35 @@ describe('Adapter tests', () => { }) }) + describe('adapter::setchainid', () => { + it('Should throw if called by not authorized account', async () => { + const action = adapter.contract.actions + .setchainid([EOSChainId]) + .send(active(evil)) + + await expectToThrow(action, errors.AUTH_MISSING(adapter.account)) + }) + + it('Should set the local chain id correctly', async () => { + const chain_id = 'ababba' + await adapter.contract.actions + .setchainid([chain_id]) + .send(active(adapter.account)) + + const local_chain_id = getSingletonInstance(adapter.contract, TABLE_LOCAL_CHAIN_ID) + expect(local_chain_id.chain_id).to.be.equal(chain_id) + }) + + it('Should update the local chain id correctly', async () => { + await adapter.contract.actions + .setchainid([EOSChainId]) + .send(active(adapter.account)) + + const local_chain_id = getSingletonInstance(adapter.contract, 'chainid') + expect(local_chain_id.chain_id).to.be.equal(EOSChainId) + }) + }) + describe('adapter::settee', () => { it('Should throw if called by not authorized account', async () => { const action = adapter.contract.actions diff --git a/cpp/test/pam.test.js b/cpp/test/pam.test.js index 1fb6b0c..73a726d 100644 --- a/cpp/test/pam.test.js +++ b/cpp/test/pam.test.js @@ -47,6 +47,7 @@ describe('PAM testing', () => { const evmEmitter = '0x5623D0aF4bfb6F7B18d6618C166d518E4357ceE2' const evmTopic0 = '0x66756e6473206172652073616675207361667520736166752073616675202e2e' + const EOSChainId = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906' const recipient = 'recipient' @@ -104,12 +105,24 @@ describe('PAM testing', () => { ) }) + it('Should reject when the local chain id is not set', async () => { + const action = pam.contract.actions + .isauthorized([operation, metadata]) + .send(active(user)) + + await expectToThrow(action, errors.LOCAL_CHAIN_NOT_SET) + }) + it('Should reject when the public key is not set', async () => { + await adapter.contract.actions + .setchainid([EOSChainId]) + .send(active(adapter.account)) + const action = pam.contract.actions .isauthorized([operation, metadata]) .send(active(user)) - await expectToThrow(action, errors.SINGLETON_NOT_EXISTING) + await expectToThrow(action, errors.TEE_NOT_SET) }) it('Should reject when the origin_chain_id is not set', async () => { diff --git a/cpp/test/utils/errors.js b/cpp/test/utils/errors.js index ee2a23f..5d9c7ee 100644 --- a/cpp/test/utils/errors.js +++ b/cpp/test/utils/errors.js @@ -27,7 +27,9 @@ const INVALID_MINFEE_SYMBOL = eosio_assert('invalid minimum fee symbol') const NOT_INITIALIZED = eosio_assert('adapter contract not initialized') -const SINGLETON_NOT_EXISTING = eosio_assert('singleton does not exist') +const TEE_NOT_SET = eosio_assert('tee singleton not set') + +const LOCAL_CHAIN_NOT_SET = eosio_assert('local chain id singleton not set') const ORIGIN_CHAINID_NOT_REGISTERED = eosio_assert( 'origin chain_id not registered', @@ -93,7 +95,8 @@ module.exports = { INVALID_MINFEE_SYMBOL, NOT_INITIALIZED, SYMBOL_NOT_FOUND, - SINGLETON_NOT_EXISTING, + TEE_NOT_SET, + LOCAL_CHAIN_NOT_SET, ORIGIN_CHAINID_NOT_REGISTERED, INVALID_SIGNATURE, UNEXPECTED_EMITTER,