From bf42840b1fd9f5bed12e2c5bbd66935b9b968dab Mon Sep 17 00:00:00 2001 From: Iuga Mihai Date: Wed, 30 Aug 2023 16:20:55 +0300 Subject: [PATCH] extra field accounts indexer --- data/account.go | 47 ++++++++++--------- go.mod | 2 +- go.sum | 4 +- .../elasticproc/accounts/accountsProcessor.go | 21 +++++---- .../accounts/accountsProcessor_test.go | 22 +++++---- .../elasticproc/accounts/serialize_test.go | 16 +++---- 6 files changed, 59 insertions(+), 53 deletions(-) diff --git a/data/account.go b/data/account.go index 8e370b13..f28ff087 100644 --- a/data/account.go +++ b/data/account.go @@ -8,29 +8,30 @@ import ( // AccountInfo holds (serializable) data about an account type AccountInfo struct { - Address string `json:"address,omitempty"` - Nonce uint64 `json:"nonce,omitempty"` - Balance string `json:"balance"` - BalanceNum float64 `json:"balanceNum"` - TokenName string `json:"token,omitempty"` - TokenIdentifier string `json:"identifier,omitempty"` - TokenNonce uint64 `json:"tokenNonce,omitempty"` - Properties string `json:"properties,omitempty"` - Frozen bool `json:"frozen,omitempty"` - TotalBalanceWithStake string `json:"totalBalanceWithStake,omitempty"` - TotalBalanceWithStakeNum float64 `json:"totalBalanceWithStakeNum,omitempty"` - Owner string `json:"owner,omitempty"` - UserName string `json:"userName,omitempty"` - DeveloperRewards string `json:"developerRewards,omitempty"` - DeveloperRewardsNum float64 `json:"developerRewardsNum,omitempty"` - Data *TokenMetaData `json:"data,omitempty"` - Timestamp time.Duration `json:"timestamp,omitempty"` - Type string `json:"type,omitempty"` - CurrentOwner string `json:"currentOwner,omitempty"` - ShardID uint32 `json:"shardID"` - IsSender bool `json:"-"` - IsSmartContract bool `json:"-"` - IsNFTCreate bool `json:"-"` + Address string `json:"address,omitempty"` + Nonce uint64 `json:"nonce,omitempty"` + Balance string `json:"balance"` + BalanceNum float64 `json:"balanceNum"` + TokenName string `json:"token,omitempty"` + TokenIdentifier string `json:"identifier,omitempty"` + TokenNonce uint64 `json:"tokenNonce,omitempty"` + Properties string `json:"properties,omitempty"` + Frozen bool `json:"frozen,omitempty"` + Owner string `json:"owner,omitempty"` + UserName string `json:"userName,omitempty"` + DeveloperRewards string `json:"developerRewards,omitempty"` + DeveloperRewardsNum float64 `json:"developerRewardsNum,omitempty"` + Data *TokenMetaData `json:"data,omitempty"` + Timestamp time.Duration `json:"timestamp,omitempty"` + Type string `json:"type,omitempty"` + CurrentOwner string `json:"currentOwner,omitempty"` + ShardID uint32 `json:"shardID"` + RootHash []byte `json:"rootHash,omitempty"` + CodeHash []byte `json:"codeHash,omitempty"` + CodeMetadata []byte `json:"codeMetadata,omitempty"` + IsSender bool `json:"-"` + IsSmartContract bool `json:"-"` + IsNFTCreate bool `json:"-"` } // TokenMetaData holds data about a token metadata diff --git a/go.mod b/go.mod index 0965383a..7301d680 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/gin-contrib/cors v1.4.0 github.com/gin-gonic/gin v1.9.1 github.com/multiversx/mx-chain-communication-go v1.0.6 - github.com/multiversx/mx-chain-core-go v1.2.13 + github.com/multiversx/mx-chain-core-go v1.2.15-0.20230830125719-028435cb3956 github.com/multiversx/mx-chain-logger-go v1.0.13 github.com/multiversx/mx-chain-vm-common-go v1.5.2 github.com/prometheus/client_model v0.4.0 diff --git a/go.sum b/go.sum index 5a77910b..2e536b74 100644 --- a/go.sum +++ b/go.sum @@ -249,8 +249,8 @@ github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/multiversx/mx-chain-communication-go v1.0.6 h1:f2bizRoVuJXBWc32px7pCuzMx4Pgi2tKhUt8BkFV1Fg= github.com/multiversx/mx-chain-communication-go v1.0.6/go.mod h1:+oaUowpq+SqrEmAsMPGwhz44g7L81loWb6AiNQU9Ms4= -github.com/multiversx/mx-chain-core-go v1.2.13 h1:4Svi23hdsoibStFXv0i7lbBWus3kDJPc6CFhrxrKIZ4= -github.com/multiversx/mx-chain-core-go v1.2.13/go.mod h1:BILOGHUOIG5dNNX8cgkzCNfDaVtoYrJRYcPnpxRMH84= +github.com/multiversx/mx-chain-core-go v1.2.15-0.20230830125719-028435cb3956 h1:RlIWOnzWd5zsv7SDcNMiOkvuHiXEFHcdbzvaDfpM5UU= +github.com/multiversx/mx-chain-core-go v1.2.15-0.20230830125719-028435cb3956/go.mod h1:BILOGHUOIG5dNNX8cgkzCNfDaVtoYrJRYcPnpxRMH84= github.com/multiversx/mx-chain-crypto-go v1.2.8 h1:wOgVlUaO5X4L8iEbFjcQcL8SZvv6WZ7LqH73BiRPhxU= github.com/multiversx/mx-chain-logger-go v1.0.13 h1:eru/TETo0MkO4ZTnXsQDKf4PBRpAXmqjT02klNT/JnY= github.com/multiversx/mx-chain-logger-go v1.0.13/go.mod h1:MZJhTAtZTJxT+yK2EHc4ZW3YOHUc1UdjCD0iahRNBZk= diff --git a/process/elasticproc/accounts/accountsProcessor.go b/process/elasticproc/accounts/accountsProcessor.go index 4d90b02d..1c24bfea 100644 --- a/process/elasticproc/accounts/accountsProcessor.go +++ b/process/elasticproc/accounts/accountsProcessor.go @@ -123,16 +123,14 @@ func (ap *accountsProcessor) PrepareRegularAccountsMap(timestamp uint64, account } acc := &data.AccountInfo{ - Address: address, - Nonce: userAccount.UserAccount.Nonce, - Balance: converters.BigIntToString(balance), - BalanceNum: balanceAsFloat, - IsSender: userAccount.IsSender, - IsSmartContract: core.IsSmartContractAddress(addressBytes), - TotalBalanceWithStake: converters.BigIntToString(balance), - TotalBalanceWithStakeNum: balanceAsFloat, - Timestamp: time.Duration(timestamp), - ShardID: shardID, + Address: address, + Nonce: userAccount.UserAccount.Nonce, + Balance: converters.BigIntToString(balance), + BalanceNum: balanceAsFloat, + IsSender: userAccount.IsSender, + IsSmartContract: core.IsSmartContractAddress(addressBytes), + Timestamp: time.Duration(timestamp), + ShardID: shardID, } ap.addAdditionalDataInAccount(userAccount.UserAccount.AdditionalData, acc) @@ -150,6 +148,9 @@ func (ap *accountsProcessor) addAdditionalDataInAccount(additionalData *alteredA account.UserName = additionalData.UserName account.CurrentOwner = additionalData.CurrentOwner + account.RootHash = additionalData.RootHash + account.CodeHash = additionalData.CodeHash + account.CodeMetadata = additionalData.CodeMetadata ap.addDeveloperRewardsInAccount(additionalData, account) } diff --git a/process/elasticproc/accounts/accountsProcessor_test.go b/process/elasticproc/accounts/accountsProcessor_test.go index 71b1657b..b9c8b7bd 100644 --- a/process/elasticproc/accounts/accountsProcessor_test.go +++ b/process/elasticproc/accounts/accountsProcessor_test.go @@ -268,6 +268,11 @@ func TestAccountsProcessor_PrepareAccountsMapEGLD(t *testing.T) { Address: addr, Balance: "1000", Nonce: 1, + AdditionalData: &alteredAccount.AdditionalAccountData{ + CodeHash: []byte("code"), + CodeMetadata: []byte("metadata"), + RootHash: []byte("root"), + }, } egldAccount := &data.Account{ @@ -282,14 +287,15 @@ func TestAccountsProcessor_PrepareAccountsMapEGLD(t *testing.T) { res := ap.PrepareRegularAccountsMap(123, []*data.Account{egldAccount}, 0) require.Equal(t, &data.AccountInfo{ - Address: addr, - Nonce: 1, - Balance: "1000", - BalanceNum: balanceNum, - TotalBalanceWithStake: "1000", - TotalBalanceWithStakeNum: balanceNum, - IsSmartContract: true, - Timestamp: time.Duration(123), + Address: addr, + Nonce: 1, + Balance: "1000", + BalanceNum: balanceNum, + IsSmartContract: true, + Timestamp: time.Duration(123), + CodeHash: []byte("code"), + CodeMetadata: []byte("metadata"), + RootHash: []byte("root"), }, res[addr]) } diff --git a/process/elasticproc/accounts/serialize_test.go b/process/elasticproc/accounts/serialize_test.go index 3a1eb965..52b0dbf4 100644 --- a/process/elasticproc/accounts/serialize_test.go +++ b/process/elasticproc/accounts/serialize_test.go @@ -39,14 +39,12 @@ func TestSerializeAccounts(t *testing.T) { accs := map[string]*data.AccountInfo{ "addr1": { - Address: "addr1", - Nonce: 1, - Balance: "50", - BalanceNum: 0.1, - TotalBalanceWithStake: "50", - TotalBalanceWithStakeNum: 0.1, - IsSmartContract: true, - IsSender: true, + Address: "addr1", + Nonce: 1, + Balance: "50", + BalanceNum: 0.1, + IsSmartContract: true, + IsSender: true, }, } @@ -56,7 +54,7 @@ func TestSerializeAccounts(t *testing.T) { require.Equal(t, 1, len(buffSlice.Buffers())) expectedRes := `{ "update" : {"_index": "accounts", "_id" : "addr1" } } -{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if (ctx._source.containsKey('timestamp')) {if (ctx._source.timestamp <= params.account.timestamp) {ctx._source = params.account}} else {ctx._source = params.account}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"50","balanceNum":0.1,"totalBalanceWithStake":"50","totalBalanceWithStakeNum":0.1,"shardID":0} }},"upsert": {}} +{"scripted_upsert": true, "script": {"source": "if ('create' == ctx.op) {ctx._source = params.account} else {if (ctx._source.containsKey('timestamp')) {if (ctx._source.timestamp <= params.account.timestamp) {ctx._source = params.account}} else {ctx._source = params.account}}","lang": "painless","params": { "account": {"address":"addr1","nonce":1,"balance":"50","balanceNum":0.1,"shardID":0} }},"upsert": {}} ` require.Equal(t, expectedRes, buffSlice.Buffers()[0].String()) }