Skip to content

Commit

Permalink
Merge pull request #5958 from multiversx/deep-query-metachain
Browse files Browse the repository at this point in the history
Deep queries on metachain
  • Loading branch information
iulianpascalau committed Feb 14, 2024
2 parents dc797c7 + 7eac8b7 commit 8019dec
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 233 deletions.
15 changes: 13 additions & 2 deletions factory/api/apiResolverFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ func createScQueryElement(
) (process.SCQueryService, error) {
var err error

selfShardID := args.processComponents.ShardCoordinator().SelfId()

pkConverter := args.coreComponents.AddressPubKeyConverter()
automaticCrawlerAddressesStrings := args.generalConfig.BuiltInFunctions.AutomaticCrawlerAddresses
convertedAddresses, errDecode := factory.DecodeAddresses(pkConverter, automaticCrawlerAddressesStrings)
Expand All @@ -356,7 +358,7 @@ func createScQueryElement(
return nil, errDecode
}

apiBlockchain, err := blockchain.NewBlockChain(disabled.NewAppStatusHandler())
apiBlockchain, err := createBlockchainForScQuery(selfShardID)
if err != nil {
return nil, err
}

Check warning on line 364 in factory/api/apiResolverFactory.go

View check run for this annotation

Codecov / codecov/patch

factory/api/apiResolverFactory.go#L364

Added line #L364 was not covered by tests
Expand Down Expand Up @@ -415,7 +417,7 @@ func createScQueryElement(

var vmFactory process.VirtualMachinesContainerFactory
maxGasForVmQueries := args.generalConfig.VirtualMachine.GasConfig.ShardMaxGasPerVmQuery
if args.processComponents.ShardCoordinator().SelfId() == core.MetachainShardId {
if selfShardID == core.MetachainShardId {
maxGasForVmQueries = args.generalConfig.VirtualMachine.GasConfig.MetaMaxGasPerVmQuery
vmFactory, err = createMetaVmContainerFactory(args, argsHook)

Check warning on line 422 in factory/api/apiResolverFactory.go

View check run for this annotation

Codecov / codecov/patch

factory/api/apiResolverFactory.go#L422

Added line #L422 was not covered by tests
} else {
Expand Down Expand Up @@ -463,6 +465,15 @@ func createScQueryElement(
return smartContract.NewSCQueryService(argsNewSCQueryService)
}

func createBlockchainForScQuery(selfShardID uint32) (data.ChainHandler, error) {
isMetachain := selfShardID == core.MetachainShardId
if isMetachain {
return blockchain.NewMetaChain(disabled.NewAppStatusHandler())
}

return blockchain.NewBlockChain(disabled.NewAppStatusHandler())
}

func createMetaVmContainerFactory(args scQueryElementArgs, argsHook hooks.ArgBlockChainHook) (process.VirtualMachinesContainerFactory, error) {
blockChainHookImpl, errBlockChainHook := hooks.NewBlockChainHookImpl(argsHook)
if errBlockChainHook != nil {
Expand Down
20 changes: 20 additions & 0 deletions factory/api/apiResolverFactory_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api_test

import (
"fmt"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -448,5 +449,24 @@ func TestCreateApiResolver_createScQueryElement(t *testing.T) {
require.True(t, strings.Contains(strings.ToLower(err.Error()), "hasher"))
require.Nil(t, scQueryService)
})
}

func TestCreateApiResolver_createBlockchainForScQuery(t *testing.T) {
t.Parallel()

t.Run("for metachain", func(t *testing.T) {
t.Parallel()

apiBlockchain, err := api.CreateBlockchainForScQuery(core.MetachainShardId)
require.NoError(t, err)
require.Equal(t, "*blockchain.metaChain", fmt.Sprintf("%T", apiBlockchain))
})

t.Run("for shard", func(t *testing.T) {
t.Parallel()

apiBlockchain, err := api.CreateBlockchainForScQuery(0)
require.NoError(t, err)
require.Equal(t, "*blockchain.blockChain", fmt.Sprintf("%T", apiBlockchain))
})
}
5 changes: 5 additions & 0 deletions factory/api/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/factory"
"github.com/multiversx/mx-chain-go/process"
Expand Down Expand Up @@ -47,3 +48,7 @@ func CreateScQueryElement(args SCQueryElementArgs) (process.SCQueryService, erro
guardedAccountHandler: args.GuardedAccountHandler,
})
}

func CreateBlockchainForScQuery(selfShardID uint32) (data.ChainHandler, error) {
return createBlockchainForScQuery(selfShardID)
}
113 changes: 113 additions & 0 deletions integrationTests/miniNetwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package integrationTests

import (
"encoding/hex"
"fmt"
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/data/transaction"
)

// MiniNetwork is a mini network, useful for some integration tests
type MiniNetwork struct {
Round uint64
Nonce uint64

Nodes []*TestProcessorNode
ShardNode *TestProcessorNode
MetachainNode *TestProcessorNode
Users map[string]*TestWalletAccount
}

// NewMiniNetwork creates a MiniNetwork
func NewMiniNetwork() *MiniNetwork {
n := &MiniNetwork{}

nodes := CreateNodes(
1,
1,
1,
)

n.Nodes = nodes
n.ShardNode = nodes[0]
n.MetachainNode = nodes[1]
n.Users = make(map[string]*TestWalletAccount)

return n
}

// Stop stops the mini network
func (n *MiniNetwork) Stop() {
n.ShardNode.Close()
n.MetachainNode.Close()
}

// FundAccount funds an account
func (n *MiniNetwork) FundAccount(address []byte, value *big.Int) {
shard := n.MetachainNode.ShardCoordinator.ComputeId(address)

if shard == n.MetachainNode.ShardCoordinator.SelfId() {
MintAddress(n.MetachainNode.AccntState, address, value)
} else {
MintAddress(n.ShardNode.AccntState, address, value)
}
}

// AddUser adds a user (account) to the mini network
func (n *MiniNetwork) AddUser(balance *big.Int) *TestWalletAccount {
user := CreateTestWalletAccount(n.ShardNode.ShardCoordinator, 0)
n.Users[string(user.Address)] = user
n.FundAccount(user.Address, balance)
return user
}

// Start starts the mini network
func (n *MiniNetwork) Start() {
n.Round = 1
n.Nonce = 1
}

// Continue advances processing with a number of rounds
func (n *MiniNetwork) Continue(t *testing.T, numRounds int) {
idxProposers := []int{0, 1}

for i := int64(0); i < int64(numRounds); i++ {
n.Nonce, n.Round = ProposeAndSyncOneBlock(t, n.Nodes, idxProposers, n.Round, n.Nonce)
}
}

// SendTransaction sends a transaction
func (n *MiniNetwork) SendTransaction(
senderPubkey []byte,
receiverPubkey []byte,
value *big.Int,
data string,
additionalGasLimit uint64,
) (string, error) {
sender, ok := n.Users[string(senderPubkey)]
if !ok {
return "", fmt.Errorf("unknown sender: %s", hex.EncodeToString(senderPubkey))
}

tx := &transaction.Transaction{
Nonce: sender.Nonce,
Value: new(big.Int).Set(value),
SndAddr: sender.Address,
RcvAddr: receiverPubkey,
Data: []byte(data),
GasPrice: MinTxGasPrice,
GasLimit: MinTxGasLimit + uint64(len(data)) + additionalGasLimit,
ChainID: ChainID,
Version: MinTransactionVersion,
}

txBuff, _ := tx.GetDataForSigning(TestAddressPubkeyConverter, TestTxSignMarshalizer, TestTxSignHasher)
tx.Signature, _ = sender.SingleSigner.Sign(sender.SkTxSign, txBuff)
txHash, err := n.ShardNode.SendTransaction(tx)

sender.Nonce++

return txHash, err
}
71 changes: 0 additions & 71 deletions integrationTests/oneNodeNetwork.go

This file was deleted.

2 changes: 1 addition & 1 deletion integrationTests/testNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type GasScheduleMap = map[string]map[string]uint64
// TestNetwork wraps a set of TestProcessorNodes along with a set of test
// Wallets, instantiates them, controls them and provides operations with them;
// designed to be used in integration tests.
// TODO combine TestNetwork with the preexisting TestContext and OneNodeNetwork
// TODO combine TestNetwork with the preexisting TestContext and MiniNetwork
// into a single struct containing the functionality of all three
type TestNetwork struct {
NumShards int
Expand Down

0 comments on commit 8019dec

Please sign in to comment.