From f1e921f69d90838b8546e64f429ab16c29ada5d2 Mon Sep 17 00:00:00 2001 From: michielmulders Date: Thu, 23 May 2019 14:06:23 +0200 Subject: [PATCH 1/5] Support {} behavior for asset field accounts --- .../src/modules/chain/components/storage/entities/account.js | 3 ++- .../src/modules/chain/logic/state_store/account_store.js | 1 + framework/test/mocha/fixtures/accounts.js | 4 ++-- framework/test/mocha/functional/http/get/accounts/accounts.js | 1 + .../unit/modules/chain/components/storage/entities/account.js | 4 ++-- framework/test/mocha/unit/modules/chain/logic/account.js | 2 +- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/framework/src/modules/chain/components/storage/entities/account.js b/framework/src/modules/chain/components/storage/entities/account.js index 389bd23bf0e..46f1837b8ef 100644 --- a/framework/src/modules/chain/components/storage/entities/account.js +++ b/framework/src/modules/chain/components/storage/entities/account.js @@ -37,6 +37,7 @@ const defaultCreateValues = { nameExist: false, multiMin: 0, multiLifetime: 0, + asset: {}, }; const readOnlyFields = ['address']; @@ -163,7 +164,7 @@ class ChainAccount extends AccountEntity { accounts = accounts.map(account => { const parsedAccount = _.defaults(account, defaultCreateValues); - parsedAccount.asset = parsedAccount.asset ? parsedAccount.asset : null; + // parsedAccount.asset = parsedAccount.asset ? parsedAccount.asset : null; return parsedAccount; }); diff --git a/framework/src/modules/chain/logic/state_store/account_store.js b/framework/src/modules/chain/logic/state_store/account_store.js index 7ea42fb663e..cc78529f878 100644 --- a/framework/src/modules/chain/logic/state_store/account_store.js +++ b/framework/src/modules/chain/logic/state_store/account_store.js @@ -32,6 +32,7 @@ const defaultAccount = { nameExist: false, multiMin: 0, multiLifetime: 0, + asset: {}, }; class AccountStore { diff --git a/framework/test/mocha/fixtures/accounts.js b/framework/test/mocha/fixtures/accounts.js index e290162214f..2a053d104ce 100644 --- a/framework/test/mocha/fixtures/accounts.js +++ b/framework/test/mocha/fixtures/accounts.js @@ -84,7 +84,7 @@ const Account = stampit({ votedDelegatesPublicKeys: null, membersPublicKeys: null, productivity: 0, - asset: null, + asset: {}, }, init({ isDelegate, @@ -138,7 +138,7 @@ const dbAccount = stampit({ secondSignature: 0, username: null, vote: '0', - asset: null, + asset: {}, }, init({ address, balance }) { this.address = address || this.address; diff --git a/framework/test/mocha/functional/http/get/accounts/accounts.js b/framework/test/mocha/functional/http/get/accounts/accounts.js index 0761add4510..2624717fb7f 100644 --- a/framework/test/mocha/functional/http/get/accounts/accounts.js +++ b/framework/test/mocha/functional/http/get/accounts/accounts.js @@ -449,6 +449,7 @@ describe('GET /accounts', () => { delegate.vote, constansts.supply ); + expect(delegate.approval).to.be.eql(calculatedApproval); }); diff --git a/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js b/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js index 69bff7af66a..8b136b461dd 100644 --- a/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js +++ b/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js @@ -160,7 +160,7 @@ describe('ChainAccount', () => { await AccountEntity.create(account); const mergedObject = Object.assign({}, defaultCreateValues, account); - mergedObject.asset = mergedObject.asset ? mergedObject.asset : null; + // mergedObject.asset = mergedObject.asset ? mergedObject.asset : null; expect(AccountEntity.getValuesSet.firstCall.args[0]).to.be.eql([ mergedObject, @@ -281,7 +281,7 @@ describe('ChainAccount', () => { productivity: 0, votedDelegatesPublicKeys: null, membersPublicKeys: null, - asset: null, + asset: {}, }; expect(accountFromDB).to.be.eql(expectedObject); }); diff --git a/framework/test/mocha/unit/modules/chain/logic/account.js b/framework/test/mocha/unit/modules/chain/logic/account.js index bfa866c2e21..68cfdcbbafa 100644 --- a/framework/test/mocha/unit/modules/chain/logic/account.js +++ b/framework/test/mocha/unit/modules/chain/logic/account.js @@ -45,7 +45,7 @@ const validAccount = { productivity: 0, membersPublicKeys: null, votedDelegatesPublicKeys: null, - asset: null, + asset: {}, }; describe('account', () => { From e014b87c69b79100de04ac9cafa3db2295ad7346 Mon Sep 17 00:00:00 2001 From: michielmulders Date: Thu, 23 May 2019 14:37:47 +0200 Subject: [PATCH 2/5] Remove unused logic object account --- .../src/modules/chain/components/storage/entities/account.js | 2 -- .../unit/modules/chain/components/storage/entities/account.js | 1 - 2 files changed, 3 deletions(-) diff --git a/framework/src/modules/chain/components/storage/entities/account.js b/framework/src/modules/chain/components/storage/entities/account.js index 46f1837b8ef..4e73cd943a6 100644 --- a/framework/src/modules/chain/components/storage/entities/account.js +++ b/framework/src/modules/chain/components/storage/entities/account.js @@ -164,8 +164,6 @@ class ChainAccount extends AccountEntity { accounts = accounts.map(account => { const parsedAccount = _.defaults(account, defaultCreateValues); - // parsedAccount.asset = parsedAccount.asset ? parsedAccount.asset : null; - return parsedAccount; }); diff --git a/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js b/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js index 8b136b461dd..b84300df3a8 100644 --- a/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js +++ b/framework/test/mocha/unit/modules/chain/components/storage/entities/account.js @@ -160,7 +160,6 @@ describe('ChainAccount', () => { await AccountEntity.create(account); const mergedObject = Object.assign({}, defaultCreateValues, account); - // mergedObject.asset = mergedObject.asset ? mergedObject.asset : null; expect(AccountEntity.getValuesSet.firstCall.args[0]).to.be.eql([ mergedObject, From 4a40f7e94bf6555e3c358c11f0040d8207a18bbe Mon Sep 17 00:00:00 2001 From: michielmulders Date: Thu, 23 May 2019 14:58:09 +0200 Subject: [PATCH 3/5] Alter migration for asset field to have default and not null --- .../updates/20190410112400_add_asset_field_mem_accounts.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql index 136dce5a6f5..81fe6d6b337 100644 --- a/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql +++ b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql @@ -19,7 +19,7 @@ */ -- Add asset column to trs table as jsonb -ALTER TABLE "mem_accounts" ADD COLUMN IF NOT EXISTS "asset" jsonb; +ALTER TABLE "mem_accounts" ADD COLUMN IF NOT EXISTS "asset" jsonb NOT NULL DEFAULT '{}'::jsonb; -- Create index for asset field. Using `gin` index as it's more efficient for keys or key/value search. CREATE INDEX IF NOT EXISTS "mem_accounts_asset" ON "mem_accounts" USING gin ("asset"); From 47c744cdac7626ca98dff4ff6fed2b00d04a2c5f Mon Sep 17 00:00:00 2001 From: michielmulders Date: Fri, 24 May 2019 12:23:48 +0200 Subject: [PATCH 4/5] Add migration asset field and modify old migration --- ...410112400_add_asset_field_mem_accounts.sql | 2 +- ...00_add_default_value_asset_mem_account.sql | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql diff --git a/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql index 81fe6d6b337..136dce5a6f5 100644 --- a/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql +++ b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190410112400_add_asset_field_mem_accounts.sql @@ -19,7 +19,7 @@ */ -- Add asset column to trs table as jsonb -ALTER TABLE "mem_accounts" ADD COLUMN IF NOT EXISTS "asset" jsonb NOT NULL DEFAULT '{}'::jsonb; +ALTER TABLE "mem_accounts" ADD COLUMN IF NOT EXISTS "asset" jsonb; -- Create index for asset field. Using `gin` index as it's more efficient for keys or key/value search. CREATE INDEX IF NOT EXISTS "mem_accounts_asset" ON "mem_accounts" USING gin ("asset"); diff --git a/framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql new file mode 100644 index 00000000000..8327b112959 --- /dev/null +++ b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql @@ -0,0 +1,22 @@ +/* + * Copyright © 2019 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + */ + + +/* + DESCRIPTION: Alter null value to empty JSON object {} for asset field for mem_accounts column + PARAMETERS: None +*/ + + -- Alter asset field to empty JSON +UPDATE mem_accounts SET asset = '{}'::json; \ No newline at end of file From 44d3dde05f9f7e978a6aa0b8cc1ad2571f53e9c4 Mon Sep 17 00:00:00 2001 From: michielmulders Date: Mon, 27 May 2019 10:44:09 +0200 Subject: [PATCH 5/5] Add WHERE clause migration asset to object mem_accounts --- .../20190524120600_add_default_value_asset_mem_account.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql index 8327b112959..a6cec8afcea 100644 --- a/framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql +++ b/framework/src/modules/chain/components/storage/sql/migrations/updates/20190524120600_add_default_value_asset_mem_account.sql @@ -19,4 +19,4 @@ */ -- Alter asset field to empty JSON -UPDATE mem_accounts SET asset = '{}'::json; \ No newline at end of file +UPDATE mem_accounts SET asset = '{}'::json WHERE asset IS NULL; \ No newline at end of file