From a7e9294806ef1266cac2306e30aa1de394ac2e52 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Tue, 16 Jul 2024 13:45:19 +0000 Subject: [PATCH 01/21] add new blockchain hooks --- .../config/gasSchedules/gasScheduleV1.toml | 5 ++ .../config/gasSchedules/gasScheduleV2.toml | 4 + .../config/gasSchedules/gasScheduleV3.toml | 4 + .../config/gasSchedules/gasScheduleV4.toml | 4 + .../config/gasSchedules/gasScheduleV5.toml | 4 + .../config/gasSchedules/gasScheduleV6.toml | 4 + .../config/gasSchedules/gasScheduleV7.toml | 4 + .../config/gasSchedules/gasScheduleV8.toml | 4 + epochStart/metachain/systemSCs_test.go | 1 + factory/api/apiResolverFactory.go | 1 + factory/processing/blockProcessorCreator.go | 2 + go.mod | 12 +-- go.sum | 24 +++--- integrationTests/testProcessorNode.go | 3 + integrationTests/vm/testInitializer.go | 11 +++ integrationTests/vm/wasm/utils.go | 3 + process/common.go | 26 +++++++ process/interface.go | 4 + process/smartContract/hooks/blockChainHook.go | 73 +++++++++++++++++++ .../hooks/blockChainHook_test.go | 2 + testscommon/blockChainHookStub.go | 40 ++++++++++ 21 files changed, 217 insertions(+), 18 deletions(-) diff --git a/cmd/node/config/gasSchedules/gasScheduleV1.toml b/cmd/node/config/gasSchedules/gasScheduleV1.toml index 7fca1d6a7d2..9d64a932e9b 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV1.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV1.toml @@ -113,6 +113,11 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 1000 + EpochStartBlockTimeStamp = 1000 + EpochStartBlockNonce = 1000 + EpochStartBlockRound = 1000 + [EthAPICost] UseGas = 100 diff --git a/cmd/node/config/gasSchedules/gasScheduleV2.toml b/cmd/node/config/gasSchedules/gasScheduleV2.toml index bfc53d1b91d..703ce7470c0 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV2.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV2.toml @@ -113,6 +113,10 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 10000 + EpochStartBlockTimeStamp = 10000 + EpochStartBlockNonce = 10000 + EpochStartBlockRound = 10000 [EthAPICost] UseGas = 100 diff --git a/cmd/node/config/gasSchedules/gasScheduleV3.toml b/cmd/node/config/gasSchedules/gasScheduleV3.toml index eb88204bf5e..175cf5b7d17 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV3.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV3.toml @@ -113,6 +113,10 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 10000 + EpochStartBlockTimeStamp = 10000 + EpochStartBlockNonce = 10000 + EpochStartBlockRound = 10000 [EthAPICost] UseGas = 100 diff --git a/cmd/node/config/gasSchedules/gasScheduleV4.toml b/cmd/node/config/gasSchedules/gasScheduleV4.toml index f41a7a8d940..3908d47b544 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV4.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV4.toml @@ -113,6 +113,10 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 10000 + EpochStartBlockTimeStamp = 10000 + EpochStartBlockNonce = 10000 + EpochStartBlockRound = 10000 [EthAPICost] UseGas = 100 diff --git a/cmd/node/config/gasSchedules/gasScheduleV5.toml b/cmd/node/config/gasSchedules/gasScheduleV5.toml index 34b4336b32c..cb4a4db0ec2 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV5.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV5.toml @@ -113,6 +113,10 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 10000 + EpochStartBlockTimeStamp = 10000 + EpochStartBlockNonce = 10000 + EpochStartBlockRound = 10000 [EthAPICost] UseGas = 100 diff --git a/cmd/node/config/gasSchedules/gasScheduleV6.toml b/cmd/node/config/gasSchedules/gasScheduleV6.toml index 99ff15c8482..d0e9d9814f1 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV6.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV6.toml @@ -113,6 +113,10 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 10000 + EpochStartBlockTimeStamp = 10000 + EpochStartBlockNonce = 10000 + EpochStartBlockRound = 10000 [EthAPICost] UseGas = 100 diff --git a/cmd/node/config/gasSchedules/gasScheduleV7.toml b/cmd/node/config/gasSchedules/gasScheduleV7.toml index 250d89117cf..37581a22668 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV7.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV7.toml @@ -114,6 +114,10 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 10000 + EpochStartBlockTimeStamp = 10000 + EpochStartBlockNonce = 10000 + EpochStartBlockRound = 10000 [EthAPICost] UseGas = 100 diff --git a/cmd/node/config/gasSchedules/gasScheduleV8.toml b/cmd/node/config/gasSchedules/gasScheduleV8.toml index 7a0c11de4e9..b28e469aa48 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV8.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV8.toml @@ -114,6 +114,10 @@ GetCodeMetadata = 10000 IsBuiltinFunction = 10000 IsReservedFunctionName = 10000 + GetRoundTime = 10000 + EpochStartBlockTimeStamp = 10000 + EpochStartBlockNonce = 10000 + EpochStartBlockRound = 10000 [EthAPICost] UseGas = 100 diff --git a/epochStart/metachain/systemSCs_test.go b/epochStart/metachain/systemSCs_test.go index c97d3cdbdd6..2e81f7bc711 100644 --- a/epochStart/metachain/systemSCs_test.go +++ b/epochStart/metachain/systemSCs_test.go @@ -841,6 +841,7 @@ func createFullArgumentsForSystemSCProcessing(enableEpochsConfig config.EnableEp GasSchedule: gasScheduleNotifier, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: nodesSetup, } defaults.FillGasMapInternal(gasSchedule, 1) diff --git a/factory/api/apiResolverFactory.go b/factory/api/apiResolverFactory.go index dfefa56ff94..61c3208abc9 100644 --- a/factory/api/apiResolverFactory.go +++ b/factory/api/apiResolverFactory.go @@ -411,6 +411,7 @@ func createScQueryElement( MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), Accounts: accountsAdapterApi, BlockChain: apiBlockchain, + NodesSetup: args.coreComponents.GenesisNodesSetup(), } var vmFactory process.VirtualMachinesContainerFactory diff --git a/factory/processing/blockProcessorCreator.go b/factory/processing/blockProcessorCreator.go index 4c952f4bb25..52a5940a99b 100644 --- a/factory/processing/blockProcessorCreator.go +++ b/factory/processing/blockProcessorCreator.go @@ -1071,6 +1071,7 @@ func (pcf *processComponentsFactory) createVMFactoryShard( GasSchedule: pcf.gasSchedule, Counter: counter, MissingTrieNodesNotifier: notifier, + NodesSetup: pcf.coreData.GenesisNodesSetup(), } blockChainHookImpl, err := hooks.NewBlockChainHookImpl(argsHook) @@ -1123,6 +1124,7 @@ func (pcf *processComponentsFactory) createVMFactoryMeta( GasSchedule: pcf.gasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), + NodesSetup: pcf.coreData.GenesisNodesSetup(), } blockChainHookImpl, err := hooks.NewBlockChainHookImpl(argsHook) diff --git a/go.mod b/go.mod index 8ea57d19fbf..28a6e3ec09c 100644 --- a/go.mod +++ b/go.mod @@ -19,13 +19,13 @@ require ( github.com/multiversx/mx-chain-crypto-go v1.2.12-0.20240508074452-cc21c1b505df github.com/multiversx/mx-chain-es-indexer-go v1.7.2-0.20240703134111-bda0024613cc github.com/multiversx/mx-chain-logger-go v1.0.15-0.20240508072523-3f00a726af57 - github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240509103754-9e8129721f00 + github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240716122141-cb25f6e7cef0 github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240508073549-dcb8e6e0370f - github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240715100647-8ce0ec25ff1d - github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240715111121-ec175dad3ac8 - github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240509103859-89de3c5da36b - github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240509104009-598a37ff36b9 - github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240509104102-2a6a709b4041 + github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240716084819-890cf285a584 + github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240716125541-f2321722ffd8 + github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240716132123-55d87f4f76a2 + github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240716132655-84c237b4ff86 + github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240716131510-9655bc0017ec github.com/pelletier/go-toml v1.9.3 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.14.0 diff --git a/go.sum b/go.sum index 5c81848fe6c..25804b76010 100644 --- a/go.sum +++ b/go.sum @@ -395,20 +395,20 @@ github.com/multiversx/mx-chain-es-indexer-go v1.7.2-0.20240703134111-bda0024613c github.com/multiversx/mx-chain-es-indexer-go v1.7.2-0.20240703134111-bda0024613cc/go.mod h1:yMq9q5VdN7jBaErRGQ0T8dkZwbBtfQYmqGbD/Ese1us= github.com/multiversx/mx-chain-logger-go v1.0.15-0.20240508072523-3f00a726af57 h1:g9t410dqjcb7UUptbVd/H6Ua12sEzWU4v7VplyNvRZ0= github.com/multiversx/mx-chain-logger-go v1.0.15-0.20240508072523-3f00a726af57/go.mod h1:cY6CIXpndW5g5PTPn4WzPwka/UBEf+mgw+PSY5pHGAU= -github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240509103754-9e8129721f00 h1:hFEcbGBtXu8UyB9BMhmAIH2R8BtV/NOq/rsxespLCN8= -github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240509103754-9e8129721f00/go.mod h1:pnIIfWopbDMQ1EW5Ddc6KDMqv8Qtx+hxbH9rorHpCyo= +github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240716122141-cb25f6e7cef0 h1:kyf4meBS6n8Km9ds2OQIn/V1u67AzVv0cALct1l4z9s= +github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240716122141-cb25f6e7cef0/go.mod h1:IelKhXNV/5Q2SlgMYsf+Dh+WCtIJcss0evvX22s5stw= github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240508073549-dcb8e6e0370f h1:yd/G8iPBGOEAwbaS8zndJpO6bQk7Tk72ZhmlqRasThI= github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240508073549-dcb8e6e0370f/go.mod h1:E6nfj9EQzGxWDGM3Dn6eZWRC3qFy1G8IqOsYsBOcgWw= -github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240715100647-8ce0ec25ff1d h1:GqwJaWDgWFuHx4AsUBMwpHWzY4afyTbWBk0nwYG6lsY= -github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240715100647-8ce0ec25ff1d/go.mod h1:RgGmPei0suQcFTHfO4cS5dxJSiokp2SM5lmNgp1icMo= -github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240715111121-ec175dad3ac8 h1:yWqReDIF3P7Y37nonIip7uVVUERFCJIWlIvM3G2qb38= -github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240715111121-ec175dad3ac8/go.mod h1:AKygEQlZe9F2YdO8VKK8QCWb7UTCuN2KclFcEfFo0m4= -github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240509103859-89de3c5da36b h1:puYO0lUyPGA5kZqsiDjGa+daDGQwj9xFs0S5urhZjU8= -github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240509103859-89de3c5da36b/go.mod h1:SY95hGdAIc8YCGb4uNSy1ux8V8qQbF1ReZJDwQ6AqEo= -github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240509104009-598a37ff36b9 h1:rrkgAS58jRXc6LThPHY5fm3AnFoUa0VUiYkH5czdlYg= -github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240509104009-598a37ff36b9/go.mod h1:TiOTsz2kxHadU0It7okOwcynyNPePXzjyl7lnpGLlUQ= -github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240509104102-2a6a709b4041 h1:k0xkmCrJiQzsWk4ZM3oNQ31lheiDvd1qQnNwnyuZzXU= -github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240509104102-2a6a709b4041/go.mod h1:XeZNaDMV0hbDlm3JtW0Hj3mCWKaB/XecQlCzEjiK5L8= +github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240716084819-890cf285a584 h1:f4ybUYrES2XzumD9KsLzXfy/QdR9LiX1nJ4Nz2eBID4= +github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240716084819-890cf285a584/go.mod h1:RgGmPei0suQcFTHfO4cS5dxJSiokp2SM5lmNgp1icMo= +github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240716125541-f2321722ffd8 h1:jPLHRA1BMz0oTvxHuEbFXKWwJ7SYy0gFHTWHNKWG2BQ= +github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240716125541-f2321722ffd8/go.mod h1:8Eso3RjhK1VM/e3pVnmvx+JlAlE7APmls6lA+rP65co= +github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240716132123-55d87f4f76a2 h1:69pa9Kf7MiTr9iXkNT66tstJLgUq0eLzlmXCU6my3wM= +github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240716132123-55d87f4f76a2/go.mod h1:AczUpKXgHsYqVYmZpoHI5IILAjaJrZoNpyTeCExIeaM= +github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240716132655-84c237b4ff86 h1:hvM9g67RRK3WNyjWL4nmT9/2KaI3CDSSFr0jgEyw9VY= +github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240716132655-84c237b4ff86/go.mod h1:GwV1inaPJtY3QX9C0rsT1kUsaIM/ZfXzX2DSSFSA0D0= +github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240716131510-9655bc0017ec h1:peZ8Mg6Vjg7/M3/R2aksUmWxwlM8FvfNWms8Oa2Cdkw= +github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240716131510-9655bc0017ec/go.mod h1:XdebtaCIcnF4jAxOynYKI9MjerqhLeH29s3Jht2cpzs= github.com/multiversx/mx-components-big-int v1.0.0 h1:Wkr8lSzK2nDqixOrrBa47VNuqdhV1m/aJhaP1EMaiS8= github.com/multiversx/mx-components-big-int v1.0.0/go.mod h1:maIEMgHlNE2u78JaDD0oLzri+ShgU4okHfzP3LWGdQM= github.com/multiversx/protobuf v1.3.2 h1:RaNkxvGTGbA0lMcnHAN24qE1G1i+Xs5yHA6MDvQ4mSM= diff --git a/integrationTests/testProcessorNode.go b/integrationTests/testProcessorNode.go index a7468de8485..0064d170ea1 100644 --- a/integrationTests/testProcessorNode.go +++ b/integrationTests/testProcessorNode.go @@ -900,6 +900,7 @@ func (tpn *TestProcessorNode) createFullSCQueryService(gasMap map[string]map[str GasSchedule: gasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: tpn.NodesSetup, } var apiBlockchain data.ChainHandler @@ -1642,6 +1643,7 @@ func (tpn *TestProcessorNode) initInnerProcessors(gasMap map[string]map[string]u GasSchedule: gasSchedule, Counter: counter, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: tpn.NodesSetup, } maxGasLimitPerBlock := uint64(0xFFFFFFFFFFFFFFFF) @@ -1869,6 +1871,7 @@ func (tpn *TestProcessorNode) initMetaInnerProcessors(gasMap map[string]map[stri GasSchedule: gasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: tpn.NodesSetup, } var signVerifier vm.MessageSignVerifier diff --git a/integrationTests/vm/testInitializer.go b/integrationTests/vm/testInitializer.go index 4ffd57197ca..d7a32b9a947 100644 --- a/integrationTests/vm/testInitializer.go +++ b/integrationTests/vm/testInitializer.go @@ -393,6 +393,8 @@ func CreateTxProcessorWithOneSCExecutorMockVM( defaults.FillGasMapInternal(gasSchedule, 1) gasScheduleNotifier := mock.NewGasScheduleNotifierMock(gasSchedule) + nodesSetup := &genesisMocks.NodesSetupStub{} + builtInFuncs := vmcommonBuiltInFunctions.NewBuiltInFunctionContainer() datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ @@ -415,6 +417,7 @@ func CreateTxProcessorWithOneSCExecutorMockVM( GasSchedule: gasScheduleNotifier, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: nodesSetup, } blockChainHook, _ := hooks.NewBlockChainHookImpl(args) @@ -503,6 +506,7 @@ func CreateTxProcessorWithOneSCExecutorMockVM( // CreateOneSCExecutorMockVM - func CreateOneSCExecutorMockVM(accnts state.AccountsAdapter) vmcommon.VMExecutionHandler { datapool := dataRetrieverMock.NewPoolsHolderMock() + nodesSetup := &genesisMocks.NodesSetupStub{} args := hooks.ArgBlockChainHook{ Accounts: accnts, PubkeyConv: pubkeyConv, @@ -523,6 +527,7 @@ func CreateOneSCExecutorMockVM(accnts state.AccountsAdapter) vmcommon.VMExecutio GasSchedule: CreateMockGasScheduleNotifier(), Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: nodesSetup, } blockChainHook, _ := hooks.NewBlockChainHookImpl(args) vm, _ := mock.NewOneSCExecutorMockVM(blockChainHook, integrationtests.TestHasher) @@ -573,6 +578,8 @@ func CreateVMAndBlockchainHookAndDataPool( esdtTransferParser, _ := parsers.NewESDTTransferParser(integrationtests.TestMarshalizer) counter, _ := counters.NewUsageCounter(esdtTransferParser) + nodesSetup := &genesisMocks.NodesSetupStub{} + datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ Accounts: accnts, @@ -594,6 +601,7 @@ func CreateVMAndBlockchainHookAndDataPool( GasSchedule: gasSchedule, Counter: counter, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: nodesSetup, } maxGasLimitPerBlock := uint64(0xFFFFFFFFFFFFFFFF) @@ -664,6 +672,8 @@ func CreateVMAndBlockchainHookMeta( argsBuiltIn.AutomaticCrawlerAddresses = integrationTests.GenerateOneAddressPerShard(argsBuiltIn.ShardCoordinator) builtInFuncFactory, _ := builtInFunctions.CreateBuiltInFunctionsFactory(argsBuiltIn) + nodesSetup := &genesisMocks.NodesSetupStub{} + datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ Accounts: validatorAccounts, @@ -684,6 +694,7 @@ func CreateVMAndBlockchainHookMeta( GasSchedule: gasSchedule, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: nodesSetup, } economicsData, err := createEconomicsData(config.EnableEpochs{}) diff --git a/integrationTests/vm/wasm/utils.go b/integrationTests/vm/wasm/utils.go index bfe7b4b7ca9..b7aeb76a284 100644 --- a/integrationTests/vm/wasm/utils.go +++ b/integrationTests/vm/wasm/utils.go @@ -50,6 +50,7 @@ import ( dataRetrieverMock "github.com/multiversx/mx-chain-go/testscommon/dataRetriever" "github.com/multiversx/mx-chain-go/testscommon/dblookupext" "github.com/multiversx/mx-chain-go/testscommon/epochNotifier" + "github.com/multiversx/mx-chain-go/testscommon/genesisMocks" "github.com/multiversx/mx-chain-go/testscommon/guardianMocks" "github.com/multiversx/mx-chain-go/testscommon/integrationtests" "github.com/multiversx/mx-chain-go/testscommon/marshallerMock" @@ -280,6 +281,7 @@ func (context *TestContext) initVMAndBlockchainHook() { blockchainMock := &testscommon.ChainHandlerStub{} chainStorer := &storageStubs.ChainStorerStub{} + nodesSetup := &genesisMocks.NodesSetupStub{} datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ Accounts: context.Accounts, @@ -313,6 +315,7 @@ func (context *TestContext) initVMAndBlockchainHook() { GasSchedule: gasSchedule, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: nodesSetup, } vmFactoryConfig := config.VirtualMachineConfig{ diff --git a/process/common.go b/process/common.go index f06e0d00091..b6f56ddf418 100644 --- a/process/common.go +++ b/process/common.go @@ -384,6 +384,32 @@ func GetMetaHeaderFromStorageWithNonce( return hdr, hash, nil } +func GetBlockHeaderForEpochStartFromStorage( + epoch uint32, + shard uint32, + storageService dataRetriever.StorageService, + uint64Converter typeConverters.Uint64ByteSliceConverter, + marshalizer marshal.Marshalizer, +) (data.HeaderHandler, []byte, error) { + storer, err := storageService.GetStorer(dataRetriever.BlockHeaderUnit) + if err != nil { + return nil, nil, err + } + + identifier := core.EpochStartIdentifier(epoch) + headerBytes, err := storer.GetFromEpoch([]byte(identifier), epoch) + if err != nil { + return nil, nil, err + } + + header, err := UnmarshalShardHeader(marshalizer, headerBytes) + if err != nil { + return nil, nil, err + } + + return GetHeaderFromStorageWithNonce(header.GetNonce(), shard, storageService, uint64Converter, marshalizer) +} + // GetTransactionHandler gets the transaction with a given sender/receiver shardId and txHash func GetTransactionHandler( senderShardID uint32, diff --git a/process/interface.go b/process/interface.go index 747103f26ca..e04f2dfd8d1 100644 --- a/process/interface.go +++ b/process/interface.go @@ -512,6 +512,10 @@ type BlockChainHookHandler interface { CurrentTimeStamp() uint64 CurrentRandomSeed() []byte CurrentEpoch() uint32 + RoundTime() uint64 + EpochStartBlockNonce() uint64 + EpochStartBlockRound() uint64 + EpochStartBlockTimeStamp() uint64 NewAddress(creatorAddress []byte, creatorNonce uint64, vmType []byte) ([]byte, error) ProcessBuiltInFunction(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error) SaveNFTMetaDataToSystemAccount(tx data.TransactionHandler) error diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 6e590484992..41c357bd019 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -64,6 +64,7 @@ type ArgBlockChainHook struct { GasSchedule core.GasScheduleNotifier Counter BlockChainHookCounter MissingTrieNodesNotifier common.MissingTrieNodesNotifier + NodesSetup sharding.GenesisNodesSetupHandler } // BlockChainHookImpl is a wrapper over AccountsAdapter that satisfy vmcommon.BlockchainHook interface @@ -81,6 +82,7 @@ type BlockChainHookImpl struct { globalSettingsHandler vmcommon.ESDTGlobalSettingsHandler enableEpochsHandler common.EnableEpochsHandler counter BlockChainHookCounter + nodesSetup sharding.GenesisNodesSetupHandler mutCurrentHdr sync.RWMutex currentHdr data.HeaderHandler @@ -126,6 +128,7 @@ func NewBlockChainHookImpl( gasSchedule: args.GasSchedule, counter: args.Counter, missingTrieNodesNotifier: args.MissingTrieNodesNotifier, + nodesSetup: args.NodesSetup, } err = blockChainHookImpl.makeCompiledSCStorage() @@ -393,6 +396,76 @@ func (bh *BlockChainHookImpl) LastEpoch() uint32 { return 0 } +func (bh *BlockChainHookImpl) getCurrentEpochStartBlockHeader() (data.HeaderHandler, error) { + currentEpoch := bh.CurrentEpoch() + + if currentEpoch == 0 { + return bh.blockChain.GetGenesisHeader(), nil + } + + epochStartBlock, _, err := process.GetBlockHeaderForEpochStartFromStorage( + currentEpoch, + bh.shardCoordinator.SelfId(), + bh.storageService, + bh.uint64Converter, + bh.marshalizer, + ) + + if err != nil { + return nil, err + } + + return epochStartBlock, nil +} + +// RoundTime returns the duration of a round +func (bh *BlockChainHookImpl) RoundTime() uint64 { + if bh.nodesSetup == nil { + return 0 + } + + return bh.nodesSetup.GetRoundDuration() +} + +// EpochStartBlockTimeStamp returns the timestamp of the first block of the current epoch +func (bh *BlockChainHookImpl) EpochStartBlockTimeStamp() uint64 { + defer stopMeasure(startMeasure("EpochStartBlockTimeStamp")) + + epochStartBlock, err := bh.getCurrentEpochStartBlockHeader() + + if err != nil { + return 0 + } + + return epochStartBlock.GetTimeStamp() +} + +// EpochStartBlockNonce returns the nonce of the first block of the current epoch +func (bh *BlockChainHookImpl) EpochStartBlockNonce() uint64 { + defer stopMeasure(startMeasure("EpochStartBlockNonce")) + + epochStartBlock, err := bh.getCurrentEpochStartBlockHeader() + + if err != nil { + return 0 + } + + return epochStartBlock.GetNonce() +} + +// EpochStartBlockRound returns the round of the first block of the current epoch +func (bh *BlockChainHookImpl) EpochStartBlockRound() uint64 { + defer stopMeasure(startMeasure("EpochStartBlockRound")) + + epochStartBlock, err := bh.getCurrentEpochStartBlockHeader() + + if err != nil { + return 0 + } + + return epochStartBlock.GetRound() +} + // GetStateRootHash returns the state root hash from the last committed block func (bh *BlockChainHookImpl) GetStateRootHash() []byte { rootHash := bh.blockChain.GetCurrentBlockRootHash() diff --git a/process/smartContract/hooks/blockChainHook_test.go b/process/smartContract/hooks/blockChainHook_test.go index 92636c1baf0..03fceeeae3f 100644 --- a/process/smartContract/hooks/blockChainHook_test.go +++ b/process/smartContract/hooks/blockChainHook_test.go @@ -29,6 +29,7 @@ import ( dataRetrieverMock "github.com/multiversx/mx-chain-go/testscommon/dataRetriever" "github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock" "github.com/multiversx/mx-chain-go/testscommon/epochNotifier" + "github.com/multiversx/mx-chain-go/testscommon/genesisMocks" "github.com/multiversx/mx-chain-go/testscommon/marshallerMock" stateMock "github.com/multiversx/mx-chain-go/testscommon/state" storageStubs "github.com/multiversx/mx-chain-go/testscommon/storage" @@ -69,6 +70,7 @@ func createMockBlockChainHookArgs() hooks.ArgBlockChainHook { GasSchedule: testscommon.NewGasScheduleNotifierMock(make(map[string]map[string]uint64)), Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + NodesSetup: &genesisMocks.NodesSetupStub{}, } return arguments } diff --git a/testscommon/blockChainHookStub.go b/testscommon/blockChainHookStub.go index bb0ce046f2a..644bdbbf379 100644 --- a/testscommon/blockChainHookStub.go +++ b/testscommon/blockChainHookStub.go @@ -24,6 +24,10 @@ type BlockChainHookStub struct { CurrentTimeStampCalled func() uint64 CurrentRandomSeedCalled func() []byte CurrentEpochCalled func() uint32 + RoundTimeCalled func() uint64 + EpochStartBlockNonceCalled func() uint64 + EpochStartBlockRoundCalled func() uint64 + EpochStartBlockTimeStampCalled func() uint64 ProcessBuiltInFunctionCalled func(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error) GetBuiltinFunctionNamesCalled func() vmcommon.FunctionNames GetBuiltinFunctionsContainerCalled func() vmcommon.BuiltInFunctionContainer @@ -191,6 +195,42 @@ func (stub *BlockChainHookStub) CurrentEpoch() uint32 { return 0 } +// RoundTime - +func (stub *BlockChainHookStub) RoundTime() uint64 { + if stub.RoundTimeCalled != nil { + return stub.RoundTimeCalled() + } + + return 0 +} + +// EpochStartBlockNonce - +func (stub *BlockChainHookStub) EpochStartBlockNonce() uint64 { + if stub.EpochStartBlockNonceCalled != nil { + return stub.EpochStartBlockNonceCalled() + } + + return 0 +} + +// EpochStartBlockRound - +func (stub *BlockChainHookStub) EpochStartBlockRound() uint64 { + if stub.EpochStartBlockRoundCalled != nil { + return stub.EpochStartBlockRoundCalled() + } + + return 0 +} + +// EpochStartBlockTimeStamp - +func (stub *BlockChainHookStub) EpochStartBlockTimeStamp() uint64 { + if stub.EpochStartBlockTimeStampCalled != nil { + return stub.EpochStartBlockTimeStampCalled() + } + + return 0 +} + // NewAddress - func (stub *BlockChainHookStub) NewAddress(creatorAddress []byte, creatorNonce uint64, vmType []byte) ([]byte, error) { if stub.NewAddressCalled != nil { From f060fde39db57767e8150a59d7a48f2fc1ee1ccb Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 26 Jul 2024 07:26:06 +0000 Subject: [PATCH 02/21] use cached epoch start block in vm blockchain hook --- .../disabled/disabledEpochStartTrigger.go | 5 ++ epochStart/interface.go | 1 + epochStart/metachain/systemSCs_test.go | 1 + epochStart/metachain/trigger.go | 5 ++ epochStart/shardchain/trigger.go | 8 ++ factory/api/apiResolverFactory.go | 1 + factory/mock/epochStartTriggerStub.go | 10 +++ factory/processing/blockProcessorCreator.go | 6 ++ factory/processing/export_test.go | 4 +- factory/processing/processComponents.go | 4 +- .../txSimulatorProcessComponents.go | 13 +++- .../txSimulatorProcessComponents_test.go | 7 +- genesis/process/genesisBlockCreator.go | 2 + .../mock/endOfEpochTriggerStub.go | 9 +++ integrationTests/testProcessorNode.go | 3 + .../vm/staking/systemSCCreator.go | 1 + integrationTests/vm/testInitializer.go | 4 + integrationTests/vm/wasm/utils.go | 1 + node/mock/endOfEpochTriggerStub.go | 10 +++ process/common.go | 26 ------- process/interface.go | 1 + process/mock/endOfEpochTriggerStub.go | 9 +++ process/smartContract/hooks/blockChainHook.go | 78 ++++++++----------- .../hooks/blockChainHook_test.go | 1 + process/smartContract/hooks/interface.go | 10 ++- testscommon/epochStartTriggerStub.go | 9 +++ 26 files changed, 146 insertions(+), 83 deletions(-) diff --git a/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go b/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go index e4878a812c1..4042e35ed36 100644 --- a/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go +++ b/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go @@ -54,6 +54,11 @@ func (e *epochStartTrigger) EpochStartMetaHdrHash() []byte { return nil } +// EpochStartHdr - +func (e *epochStartTrigger) EpochStartHdr() data.HeaderHandler { + return nil +} + // GetSavedStateKey - func (e *epochStartTrigger) GetSavedStateKey() []byte { return nil diff --git a/epochStart/interface.go b/epochStart/interface.go index 37df49df292..274fe0ca336 100644 --- a/epochStart/interface.go +++ b/epochStart/interface.go @@ -22,6 +22,7 @@ type TriggerHandler interface { Update(round uint64, nonce uint64) EpochStartRound() uint64 EpochStartMetaHdrHash() []byte + EpochStartHdr() data.HeaderHandler GetSavedStateKey() []byte LoadState(key []byte) error SetProcessed(header data.HeaderHandler, body data.BodyHandler) diff --git a/epochStart/metachain/systemSCs_test.go b/epochStart/metachain/systemSCs_test.go index 2e81f7bc711..a93251f774a 100644 --- a/epochStart/metachain/systemSCs_test.go +++ b/epochStart/metachain/systemSCs_test.go @@ -841,6 +841,7 @@ func createFullArgumentsForSystemSCProcessing(enableEpochsConfig config.EnableEp GasSchedule: gasScheduleNotifier, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, NodesSetup: nodesSetup, } diff --git a/epochStart/metachain/trigger.go b/epochStart/metachain/trigger.go index 5dedb1f1cda..95e96fd78ec 100644 --- a/epochStart/metachain/trigger.go +++ b/epochStart/metachain/trigger.go @@ -403,6 +403,11 @@ func (t *trigger) EpochStartMetaHdrHash() []byte { return t.epochStartMetaHash } +// EpochStartHdr returns the header of the epoch start block +func (t *trigger) EpochStartHdr() data.HeaderHandler { + return t.epochStartMeta +} + // GetSavedStateKey returns the last saved trigger state key func (t *trigger) GetSavedStateKey() []byte { return t.triggerStateKey diff --git a/epochStart/shardchain/trigger.go b/epochStart/shardchain/trigger.go index 496702b8d81..cc098eced85 100644 --- a/epochStart/shardchain/trigger.go +++ b/epochStart/shardchain/trigger.go @@ -1102,6 +1102,14 @@ func (t *trigger) EpochStartMetaHdrHash() []byte { return t.epochMetaBlockHash } +// EpochStartHdr returns the epoch start header +func (t *trigger) EpochStartHdr() data.HeaderHandler { + t.mutTrigger.RLock() + defer t.mutTrigger.RUnlock() + + return t.epochStartShardHeader +} + // GetSavedStateKey returns the last saved trigger state key func (t *trigger) GetSavedStateKey() []byte { return t.triggerStateKey diff --git a/factory/api/apiResolverFactory.go b/factory/api/apiResolverFactory.go index 61c3208abc9..3ca1f2df824 100644 --- a/factory/api/apiResolverFactory.go +++ b/factory/api/apiResolverFactory.go @@ -411,6 +411,7 @@ func createScQueryElement( MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), Accounts: accountsAdapterApi, BlockChain: apiBlockchain, + EpochStartTrigger: args.processComponents.EpochStartTrigger(), NodesSetup: args.coreComponents.GenesisNodesSetup(), } diff --git a/factory/mock/epochStartTriggerStub.go b/factory/mock/epochStartTriggerStub.go index 71808706455..e121fa4d11d 100644 --- a/factory/mock/epochStartTriggerStub.go +++ b/factory/mock/epochStartTriggerStub.go @@ -12,6 +12,7 @@ type EpochStartTriggerStub struct { IsEpochStartCalled func() bool EpochCalled func() uint32 MetaEpochCalled func() uint32 + EpochStartHdrCalled func() data.HeaderHandler ReceivedHeaderCalled func(handler data.HeaderHandler) UpdateCalled func(round uint64, nonce uint64) ProcessedCalled func(header data.HeaderHandler) @@ -49,6 +50,15 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } +// EpochStartHdr - +func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { + if e.EpochStartHdrCalled != nil { + return e.EpochStartHdrCalled() + } + + return nil +} + // GetSavedStateKey - func (e *EpochStartTriggerStub) GetSavedStateKey() []byte { return []byte("epoch start trigger key") diff --git a/factory/processing/blockProcessorCreator.go b/factory/processing/blockProcessorCreator.go index 57117dfa35f..40c92d7d41e 100644 --- a/factory/processing/blockProcessorCreator.go +++ b/factory/processing/blockProcessorCreator.go @@ -163,6 +163,7 @@ func (pcf *processComponentsFactory) newShardBlockProcessor( pcf.config.SmartContractsStorage, builtInFuncFactory.NFTStorageHandler(), builtInFuncFactory.ESDTGlobalSettingsHandler(), + epochStartTrigger, ) if err != nil { return nil, err @@ -505,6 +506,7 @@ func (pcf *processComponentsFactory) newMetaBlockProcessor( pcf.config.SmartContractsStorage, builtInFuncFactory.NFTStorageHandler(), builtInFuncFactory.ESDTGlobalSettingsHandler(), + epochStartTrigger, ) if err != nil { return nil, err @@ -1064,6 +1066,7 @@ func (pcf *processComponentsFactory) createVMFactoryShard( configSCStorage config.StorageConfig, nftStorageHandler vmcommon.SimpleESDTNFTStorageHandler, globalSettingsHandler vmcommon.ESDTGlobalSettingsHandler, + epochStartTriggerHandler process.EpochStartTriggerHandler, ) (process.VirtualMachinesContainerFactory, error) { counter, err := counters.NewUsageCounter(esdtTransferParser) if err != nil { @@ -1091,6 +1094,7 @@ func (pcf *processComponentsFactory) createVMFactoryShard( GasSchedule: pcf.gasSchedule, Counter: counter, MissingTrieNodesNotifier: notifier, + EpochStartTrigger: epochStartTriggerHandler, NodesSetup: pcf.coreData.GenesisNodesSetup(), } @@ -1122,6 +1126,7 @@ func (pcf *processComponentsFactory) createVMFactoryMeta( configSCStorage config.StorageConfig, nftStorageHandler vmcommon.SimpleESDTNFTStorageHandler, globalSettingsHandler vmcommon.ESDTGlobalSettingsHandler, + epochStartTriggerHandler process.EpochStartTriggerHandler, ) (process.VirtualMachinesContainerFactory, error) { argsHook := hooks.ArgBlockChainHook{ Accounts: accounts, @@ -1144,6 +1149,7 @@ func (pcf *processComponentsFactory) createVMFactoryMeta( GasSchedule: pcf.gasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), + EpochStartTrigger: epochStartTriggerHandler, NodesSetup: pcf.coreData.GenesisNodesSetup(), } diff --git a/factory/processing/export_test.go b/factory/processing/export_test.go index c82f01b3f6f..2b6aa7f9f16 100644 --- a/factory/processing/export_test.go +++ b/factory/processing/export_test.go @@ -53,6 +53,6 @@ func (pcf *processComponentsFactory) NewBlockProcessor( } // CreateAPITransactionEvaluator - -func (pcf *processComponentsFactory) CreateAPITransactionEvaluator(relayedV3TxProcessor process.RelayedTxV3Processor) (factory.TransactionEvaluator, process.VirtualMachinesContainerFactory, error) { - return pcf.createAPITransactionEvaluator(relayedV3TxProcessor) +func (pcf *processComponentsFactory) CreateAPITransactionEvaluator(relayedV3TxProcessor process.RelayedTxV3Processor, epochStartTrigger process.EpochStartTriggerHandler) (factory.TransactionEvaluator, process.VirtualMachinesContainerFactory, error) { + return pcf.createAPITransactionEvaluator(relayedV3TxProcessor, epochStartTrigger) } diff --git a/factory/processing/processComponents.go b/factory/processing/processComponents.go index 6f711a502ae..5976a7f3e8a 100644 --- a/factory/processing/processComponents.go +++ b/factory/processing/processComponents.go @@ -135,6 +135,7 @@ type processComponents struct { sentSignaturesTracker process.SentSignaturesTracker epochSystemSCProcessor process.EpochStartSystemSCProcessor relayedTxV3Processor process.RelayedTxV3Processor + epochStartTriggerHanlder epochStart.TriggerHandler } // ProcessComponentsFactoryArgs holds the arguments needed to create a process components factory @@ -723,7 +724,7 @@ func (pcf *processComponentsFactory) Create() (*processComponents, error) { return nil, err } - apiTransactionEvaluator, vmFactoryForTxSimulate, err := pcf.createAPITransactionEvaluator(relayedTxV3Processor) + apiTransactionEvaluator, vmFactoryForTxSimulate, err := pcf.createAPITransactionEvaluator(relayedTxV3Processor, epochStartTrigger) if err != nil { return nil, fmt.Errorf("%w when assembling components for the transactions simulator processor", err) } @@ -777,6 +778,7 @@ func (pcf *processComponentsFactory) Create() (*processComponents, error) { receiptsRepository: receiptsRepository, sentSignaturesTracker: sentSignaturesTracker, relayedTxV3Processor: relayedTxV3Processor, + epochStartTriggerHanlder: epochStartTrigger, }, nil } diff --git a/factory/processing/txSimulatorProcessComponents.go b/factory/processing/txSimulatorProcessComponents.go index 21fe2ddc073..16e7883d57a 100644 --- a/factory/processing/txSimulatorProcessComponents.go +++ b/factory/processing/txSimulatorProcessComponents.go @@ -27,7 +27,7 @@ import ( datafield "github.com/multiversx/mx-chain-vm-common-go/parsers/dataField" ) -func (pcf *processComponentsFactory) createAPITransactionEvaluator(relayedTxV3Processor process.RelayedTxV3Processor) (factory.TransactionEvaluator, process.VirtualMachinesContainerFactory, error) { +func (pcf *processComponentsFactory) createAPITransactionEvaluator(relayedTxV3Processor process.RelayedTxV3Processor, epochStartTrigger process.EpochStartTriggerHandler) (factory.TransactionEvaluator, process.VirtualMachinesContainerFactory, error) { simulationAccountsDB, err := transactionEvaluator.NewSimulationAccountsDB(pcf.state.AccountsAdapterAPI()) if err != nil { return nil, nil, err @@ -47,7 +47,7 @@ func (pcf *processComponentsFactory) createAPITransactionEvaluator(relayedTxV3Pr return nil, nil, err } - txSimulatorProcessorArgs, vmContainerFactory, txTypeHandler, err := pcf.createArgsTxSimulatorProcessor(simulationAccountsDB, vmOutputCacher, txLogsProcessor, relayedTxV3Processor) + txSimulatorProcessorArgs, vmContainerFactory, txTypeHandler, err := pcf.createArgsTxSimulatorProcessor(simulationAccountsDB, vmOutputCacher, txLogsProcessor, relayedTxV3Processor, epochStartTrigger) if err != nil { return nil, nil, err } @@ -90,12 +90,13 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessor( vmOutputCacher storage.Cacher, txLogsProcessor process.TransactionLogProcessor, relayedTxV3Processor process.RelayedTxV3Processor, + epochStartTrigger process.EpochStartTriggerHandler, ) (transactionEvaluator.ArgsTxSimulator, process.VirtualMachinesContainerFactory, process.TxTypeHandler, error) { shardID := pcf.bootstrapComponents.ShardCoordinator().SelfId() if shardID == core.MetachainShardId { - return pcf.createArgsTxSimulatorProcessorForMeta(accountsAdapter, vmOutputCacher, txLogsProcessor) + return pcf.createArgsTxSimulatorProcessorForMeta(accountsAdapter, vmOutputCacher, txLogsProcessor, epochStartTrigger) } else { - return pcf.createArgsTxSimulatorProcessorShard(accountsAdapter, vmOutputCacher, txLogsProcessor, relayedTxV3Processor) + return pcf.createArgsTxSimulatorProcessorShard(accountsAdapter, vmOutputCacher, txLogsProcessor, relayedTxV3Processor, epochStartTrigger) } } @@ -103,6 +104,7 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorForMeta( accountsAdapter state.AccountsAdapter, vmOutputCacher storage.Cacher, txLogsProcessor process.TransactionLogProcessor, + epochStartTrigger process.EpochStartTriggerHandler, ) (transactionEvaluator.ArgsTxSimulator, process.VirtualMachinesContainerFactory, process.TxTypeHandler, error) { args := transactionEvaluator.ArgsTxSimulator{} @@ -138,6 +140,7 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorForMeta( pcf.config.SmartContractsStorageSimulate, builtInFuncFactory.NFTStorageHandler(), builtInFuncFactory.ESDTGlobalSettingsHandler(), + epochStartTrigger, ) if err != nil { return args, nil, nil, err @@ -254,6 +257,7 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorShard( vmOutputCacher storage.Cacher, txLogsProcessor process.TransactionLogProcessor, relayedTxV3Processor process.RelayedTxV3Processor, + epochStartTrigger process.EpochStartTriggerHandler, ) (transactionEvaluator.ArgsTxSimulator, process.VirtualMachinesContainerFactory, process.TxTypeHandler, error) { args := transactionEvaluator.ArgsTxSimulator{} @@ -304,6 +308,7 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorShard( smartContractStorageSimulate, builtInFuncFactory.NFTStorageHandler(), builtInFuncFactory.ESDTGlobalSettingsHandler(), + epochStartTrigger, ) if err != nil { return args, nil, nil, err diff --git a/factory/processing/txSimulatorProcessComponents_test.go b/factory/processing/txSimulatorProcessComponents_test.go index 37944768bfe..a551a50f9d3 100644 --- a/factory/processing/txSimulatorProcessComponents_test.go +++ b/factory/processing/txSimulatorProcessComponents_test.go @@ -7,6 +7,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-go/factory/processing" "github.com/multiversx/mx-chain-go/process/mock" + "github.com/multiversx/mx-chain-go/testscommon" "github.com/multiversx/mx-chain-go/testscommon/components" "github.com/multiversx/mx-chain-go/testscommon/processMocks" "github.com/stretchr/testify/assert" @@ -27,7 +28,7 @@ func TestManagedProcessComponents_createAPITransactionEvaluator(t *testing.T) { processArgs.Config.VMOutputCacher.Type = "invalid" pcf, _ := processing.NewProcessComponentsFactory(processArgs) - apiTransactionEvaluator, vmContainerFactory, err := pcf.CreateAPITransactionEvaluator(&processMocks.RelayedTxV3ProcessorMock{}) + apiTransactionEvaluator, vmContainerFactory, err := pcf.CreateAPITransactionEvaluator(&processMocks.RelayedTxV3ProcessorMock{}, &testscommon.EpochStartTriggerStub{}) assert.NotNil(t, err) assert.True(t, check.IfNil(apiTransactionEvaluator)) assert.True(t, check.IfNil(vmContainerFactory)) @@ -37,7 +38,7 @@ func TestManagedProcessComponents_createAPITransactionEvaluator(t *testing.T) { processArgs := components.GetProcessComponentsFactoryArgs(shardCoordinatorForShardID2) pcf, _ := processing.NewProcessComponentsFactory(processArgs) - apiTransactionEvaluator, vmContainerFactory, err := pcf.CreateAPITransactionEvaluator(&processMocks.RelayedTxV3ProcessorMock{}) + apiTransactionEvaluator, vmContainerFactory, err := pcf.CreateAPITransactionEvaluator(&processMocks.RelayedTxV3ProcessorMock{}, &testscommon.EpochStartTriggerStub{}) assert.Nil(t, err) assert.False(t, check.IfNil(apiTransactionEvaluator)) assert.False(t, check.IfNil(vmContainerFactory)) @@ -46,7 +47,7 @@ func TestManagedProcessComponents_createAPITransactionEvaluator(t *testing.T) { processArgs := components.GetProcessComponentsFactoryArgs(shardCoordinatorForMetachain) pcf, _ := processing.NewProcessComponentsFactory(processArgs) - apiTransactionEvaluator, vmContainerFactory, err := pcf.CreateAPITransactionEvaluator(&processMocks.RelayedTxV3ProcessorMock{}) + apiTransactionEvaluator, vmContainerFactory, err := pcf.CreateAPITransactionEvaluator(&processMocks.RelayedTxV3ProcessorMock{}, &testscommon.EpochStartTriggerStub{}) assert.Nil(t, err) assert.False(t, check.IfNil(apiTransactionEvaluator)) assert.False(t, check.IfNil(vmContainerFactory)) diff --git a/genesis/process/genesisBlockCreator.go b/genesis/process/genesisBlockCreator.go index 3f5e559888f..58328e2e727 100644 --- a/genesis/process/genesisBlockCreator.go +++ b/genesis/process/genesisBlockCreator.go @@ -16,6 +16,7 @@ import ( "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/dataRetriever/blockchain" + epochStart "github.com/multiversx/mx-chain-go/epochStart/bootstrap/disabled" factoryBlock "github.com/multiversx/mx-chain-go/factory/block" "github.com/multiversx/mx-chain-go/genesis" "github.com/multiversx/mx-chain-go/genesis/process/disabled" @@ -447,6 +448,7 @@ func (gbc *genesisBlockCreator) computeDNSAddresses(enableEpochsConfig config.En GasSchedule: gbc.arg.GasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), + EpochStartTrigger: epochStart.NewEpochStartTrigger(), } blockChainHook, err := hooks.NewBlockChainHookImpl(argsHook) if err != nil { diff --git a/integrationTests/mock/endOfEpochTriggerStub.go b/integrationTests/mock/endOfEpochTriggerStub.go index ee0f2be8eab..61d3bf8a37c 100644 --- a/integrationTests/mock/endOfEpochTriggerStub.go +++ b/integrationTests/mock/endOfEpochTriggerStub.go @@ -12,6 +12,7 @@ type EpochStartTriggerStub struct { IsEpochStartCalled func() bool EpochCalled func() uint32 MetaEpochCalled func() uint32 + EpochStartHdrCalled func() data.HeaderHandler ReceivedHeaderCalled func(handler data.HeaderHandler) UpdateCalled func(round uint64, nonce uint64) ProcessedCalled func(header data.HeaderHandler) @@ -49,6 +50,14 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } +// EpochStartHdr - +func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { + if e.EpochStartHdrCalled != nil { + return e.EpochStartHdrCalled() + } + return nil +} + // GetSavedStateKey - func (e *EpochStartTriggerStub) GetSavedStateKey() []byte { return []byte("epoch start trigger key") diff --git a/integrationTests/testProcessorNode.go b/integrationTests/testProcessorNode.go index 500b3822f49..7b3aadbe6f7 100644 --- a/integrationTests/testProcessorNode.go +++ b/integrationTests/testProcessorNode.go @@ -901,6 +901,7 @@ func (tpn *TestProcessorNode) createFullSCQueryService(gasMap map[string]map[str GasSchedule: gasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: tpn.EpochStartTrigger, NodesSetup: tpn.NodesSetup, } @@ -1652,6 +1653,7 @@ func (tpn *TestProcessorNode) initInnerProcessors(gasMap map[string]map[string]u GasSchedule: gasSchedule, Counter: counter, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: tpn.EpochStartTrigger, NodesSetup: tpn.NodesSetup, } @@ -1890,6 +1892,7 @@ func (tpn *TestProcessorNode) initMetaInnerProcessors(gasMap map[string]map[stri GasSchedule: gasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: tpn.EpochStartTrigger, NodesSetup: tpn.NodesSetup, } diff --git a/integrationTests/vm/staking/systemSCCreator.go b/integrationTests/vm/staking/systemSCCreator.go index cf18140797a..966f60561f2 100644 --- a/integrationTests/vm/staking/systemSCCreator.go +++ b/integrationTests/vm/staking/systemSCCreator.go @@ -177,6 +177,7 @@ func createBlockChainHook( GasSchedule: gasScheduleNotifier, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, } blockChainHook, _ := hooks.NewBlockChainHookImpl(argsHook) diff --git a/integrationTests/vm/testInitializer.go b/integrationTests/vm/testInitializer.go index b47f1b6650e..fa8e15ecc34 100644 --- a/integrationTests/vm/testInitializer.go +++ b/integrationTests/vm/testInitializer.go @@ -419,6 +419,7 @@ func CreateTxProcessorWithOneSCExecutorMockVM( GasSchedule: gasScheduleNotifier, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, NodesSetup: nodesSetup, } @@ -533,6 +534,7 @@ func CreateOneSCExecutorMockVM(accnts state.AccountsAdapter) vmcommon.VMExecutio GasSchedule: CreateMockGasScheduleNotifier(), Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, NodesSetup: nodesSetup, } blockChainHook, _ := hooks.NewBlockChainHookImpl(args) @@ -607,6 +609,7 @@ func CreateVMAndBlockchainHookAndDataPool( GasSchedule: gasSchedule, Counter: counter, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, NodesSetup: nodesSetup, } @@ -700,6 +703,7 @@ func CreateVMAndBlockchainHookMeta( GasSchedule: gasSchedule, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, NodesSetup: nodesSetup, } diff --git a/integrationTests/vm/wasm/utils.go b/integrationTests/vm/wasm/utils.go index 8c19bb6848b..05382aba27c 100644 --- a/integrationTests/vm/wasm/utils.go +++ b/integrationTests/vm/wasm/utils.go @@ -316,6 +316,7 @@ func (context *TestContext) initVMAndBlockchainHook() { GasSchedule: gasSchedule, Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, NodesSetup: nodesSetup, } diff --git a/node/mock/endOfEpochTriggerStub.go b/node/mock/endOfEpochTriggerStub.go index 1f50dddbf1c..5ca23408b3c 100644 --- a/node/mock/endOfEpochTriggerStub.go +++ b/node/mock/endOfEpochTriggerStub.go @@ -11,6 +11,7 @@ type EpochStartTriggerStub struct { IsEpochStartCalled func() bool EpochCalled func() uint32 MetaEpochCalled func() uint32 + EpochStartHdrCalled func() data.HeaderHandler ReceivedHeaderCalled func(handler data.HeaderHandler) UpdateCalled func(round uint64, nonce uint64) ProcessedCalled func(header data.HeaderHandler) @@ -58,6 +59,15 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } +// EpochStartHdr - +func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { + if e.EpochStartHdrCalled != nil { + return e.EpochStartHdrCalled() + } + + return nil +} + // Revert - func (e *EpochStartTriggerStub) Revert(_ data.HeaderHandler) { } diff --git a/process/common.go b/process/common.go index fda03545a2f..e8c9c7504ff 100644 --- a/process/common.go +++ b/process/common.go @@ -384,32 +384,6 @@ func GetMetaHeaderFromStorageWithNonce( return hdr, hash, nil } -func GetBlockHeaderForEpochStartFromStorage( - epoch uint32, - shard uint32, - storageService dataRetriever.StorageService, - uint64Converter typeConverters.Uint64ByteSliceConverter, - marshalizer marshal.Marshalizer, -) (data.HeaderHandler, []byte, error) { - storer, err := storageService.GetStorer(dataRetriever.BlockHeaderUnit) - if err != nil { - return nil, nil, err - } - - identifier := core.EpochStartIdentifier(epoch) - headerBytes, err := storer.GetFromEpoch([]byte(identifier), epoch) - if err != nil { - return nil, nil, err - } - - header, err := UnmarshalShardHeader(marshalizer, headerBytes) - if err != nil { - return nil, nil, err - } - - return GetHeaderFromStorageWithNonce(header.GetNonce(), shard, storageService, uint64Converter, marshalizer) -} - // GetTransactionHandler gets the transaction with a given sender/receiver shardId and txHash func GetTransactionHandler( senderShardID uint32, diff --git a/process/interface.go b/process/interface.go index 457a6907605..d8d2dd1f63d 100644 --- a/process/interface.go +++ b/process/interface.go @@ -469,6 +469,7 @@ type EpochStartTriggerHandler interface { Epoch() uint32 MetaEpoch() uint32 EpochStartRound() uint64 + EpochStartHdr() data.HeaderHandler SetProcessed(header data.HeaderHandler, body data.BodyHandler) RevertStateToBlock(header data.HeaderHandler) error EpochStartMetaHdrHash() []byte diff --git a/process/mock/endOfEpochTriggerStub.go b/process/mock/endOfEpochTriggerStub.go index 594415f28e4..2f3866c7d00 100644 --- a/process/mock/endOfEpochTriggerStub.go +++ b/process/mock/endOfEpochTriggerStub.go @@ -15,6 +15,7 @@ type EpochStartTriggerStub struct { UpdateCalled func(round uint64, nonce uint64) ProcessedCalled func(header data.HeaderHandler) EpochStartRoundCalled func() uint64 + EpochStartHdrCalled func() data.HeaderHandler EpochFinalityAttestingRoundCalled func() uint64 EpochStartMetaHdrHashCalled func() []byte } @@ -83,6 +84,14 @@ func (e *EpochStartTriggerStub) EpochStartRound() uint64 { return 0 } +// ReceivedHeader - +func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { + if e.EpochStartHdrCalled != nil { + return e.EpochStartHdrCalled() + } + return nil +} + // Update - func (e *EpochStartTriggerStub) Update(round uint64, nonce uint64) { if e.UpdateCalled != nil { diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 41c357bd019..8f02ef538f8 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -64,6 +64,7 @@ type ArgBlockChainHook struct { GasSchedule core.GasScheduleNotifier Counter BlockChainHookCounter MissingTrieNodesNotifier common.MissingTrieNodesNotifier + EpochStartTrigger EpochStartTriggerHandler NodesSetup sharding.GenesisNodesSetupHandler } @@ -82,11 +83,15 @@ type BlockChainHookImpl struct { globalSettingsHandler vmcommon.ESDTGlobalSettingsHandler enableEpochsHandler common.EnableEpochsHandler counter BlockChainHookCounter + epochStartTrigger EpochStartTriggerHandler nodesSetup sharding.GenesisNodesSetupHandler mutCurrentHdr sync.RWMutex currentHdr data.HeaderHandler + mutEpochStartHdr sync.RWMutex + epochStartHdr data.HeaderHandler + compiledScPool storage.Cacher compiledScStorage storage.Storer configSCStorage config.StorageConfig @@ -128,6 +133,7 @@ func NewBlockChainHookImpl( gasSchedule: args.GasSchedule, counter: args.Counter, missingTrieNodesNotifier: args.MissingTrieNodesNotifier, + epochStartTrigger: args.EpochStartTrigger, nodesSetup: args.NodesSetup, } @@ -396,28 +402,6 @@ func (bh *BlockChainHookImpl) LastEpoch() uint32 { return 0 } -func (bh *BlockChainHookImpl) getCurrentEpochStartBlockHeader() (data.HeaderHandler, error) { - currentEpoch := bh.CurrentEpoch() - - if currentEpoch == 0 { - return bh.blockChain.GetGenesisHeader(), nil - } - - epochStartBlock, _, err := process.GetBlockHeaderForEpochStartFromStorage( - currentEpoch, - bh.shardCoordinator.SelfId(), - bh.storageService, - bh.uint64Converter, - bh.marshalizer, - ) - - if err != nil { - return nil, err - } - - return epochStartBlock, nil -} - // RoundTime returns the duration of a round func (bh *BlockChainHookImpl) RoundTime() uint64 { if bh.nodesSetup == nil { @@ -429,41 +413,26 @@ func (bh *BlockChainHookImpl) RoundTime() uint64 { // EpochStartBlockTimeStamp returns the timestamp of the first block of the current epoch func (bh *BlockChainHookImpl) EpochStartBlockTimeStamp() uint64 { - defer stopMeasure(startMeasure("EpochStartBlockTimeStamp")) - - epochStartBlock, err := bh.getCurrentEpochStartBlockHeader() - - if err != nil { - return 0 - } + bh.mutEpochStartHdr.RLock() + defer bh.mutEpochStartHdr.RUnlock() - return epochStartBlock.GetTimeStamp() + return bh.epochStartHdr.GetTimeStamp() } // EpochStartBlockNonce returns the nonce of the first block of the current epoch func (bh *BlockChainHookImpl) EpochStartBlockNonce() uint64 { - defer stopMeasure(startMeasure("EpochStartBlockNonce")) - - epochStartBlock, err := bh.getCurrentEpochStartBlockHeader() - - if err != nil { - return 0 - } + bh.mutEpochStartHdr.RLock() + defer bh.mutEpochStartHdr.RUnlock() - return epochStartBlock.GetNonce() + return bh.epochStartHdr.GetNonce() } // EpochStartBlockRound returns the round of the first block of the current epoch func (bh *BlockChainHookImpl) EpochStartBlockRound() uint64 { - defer stopMeasure(startMeasure("EpochStartBlockRound")) - - epochStartBlock, err := bh.getCurrentEpochStartBlockHeader() - - if err != nil { - return 0 - } + bh.mutEpochStartHdr.RLock() + defer bh.mutEpochStartHdr.RUnlock() - return epochStartBlock.GetRound() + return bh.epochStartHdr.GetRound() } // GetStateRootHash returns the state root hash from the last committed block @@ -827,9 +796,26 @@ func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) { bh.mutCurrentHdr.Lock() bh.currentHdr = hdr + bh.updateEpochStartHeader(hdr) bh.mutCurrentHdr.Unlock() } +func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) { + bh.mutEpochStartHdr.Lock() + defer bh.mutEpochStartHdr.Unlock() + + if hdr.IsStartOfEpochBlock() { + bh.epochStartHdr = hdr + return + } + + if bh.epochStartHdr.GetEpoch() == hdr.GetEpoch() { + return + } + + bh.epochStartHdr = bh.epochStartTrigger.EpochStartHdr() +} + // SaveCompiledCode saves the compiled code to cache and storage func (bh *BlockChainHookImpl) SaveCompiledCode(codeHash []byte, code []byte) { bh.compiledScPool.Put(codeHash, code, len(code)) diff --git a/process/smartContract/hooks/blockChainHook_test.go b/process/smartContract/hooks/blockChainHook_test.go index 03fceeeae3f..5d0dd50fcde 100644 --- a/process/smartContract/hooks/blockChainHook_test.go +++ b/process/smartContract/hooks/blockChainHook_test.go @@ -70,6 +70,7 @@ func createMockBlockChainHookArgs() hooks.ArgBlockChainHook { GasSchedule: testscommon.NewGasScheduleNotifierMock(make(map[string]map[string]uint64)), Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, + EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, NodesSetup: &genesisMocks.NodesSetupStub{}, } return arguments diff --git a/process/smartContract/hooks/interface.go b/process/smartContract/hooks/interface.go index 5eb3430e585..748b3457d1b 100644 --- a/process/smartContract/hooks/interface.go +++ b/process/smartContract/hooks/interface.go @@ -1,6 +1,9 @@ package hooks -import vmcommon "github.com/multiversx/mx-chain-vm-common-go" +import ( + "github.com/multiversx/mx-chain-core-go/data" + vmcommon "github.com/multiversx/mx-chain-vm-common-go" +) // BlockChainHookCounter defines the operations of a blockchain hook counter handler type BlockChainHookCounter interface { @@ -11,3 +14,8 @@ type BlockChainHookCounter interface { GetCounterValues() map[string]uint64 IsInterfaceNil() bool } + +// EpochStartTriggerHandler defines the operations of an epoch start trigger handler needed by the blockchain hook +type EpochStartTriggerHandler interface { + EpochStartHdr() data.HeaderHandler +} diff --git a/testscommon/epochStartTriggerStub.go b/testscommon/epochStartTriggerStub.go index ca06842fae0..ec64e5377fb 100644 --- a/testscommon/epochStartTriggerStub.go +++ b/testscommon/epochStartTriggerStub.go @@ -11,6 +11,7 @@ type EpochStartTriggerStub struct { IsEpochStartCalled func() bool EpochCalled func() uint32 MetaEpochCalled func() uint32 + EpochStartHdrCalled func() data.HeaderHandler ReceivedHeaderCalled func(handler data.HeaderHandler) UpdateCalled func(round uint64, nonce uint64) ProcessedCalled func(header data.HeaderHandler) @@ -56,6 +57,14 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } +// EpochStartHdr - +func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { + if e.EpochStartHdrCalled != nil { + return e.EpochStartHdrCalled() + } + return nil +} + // GetSavedStateKey - func (e *EpochStartTriggerStub) GetSavedStateKey() []byte { return []byte("epoch start trigger key") From a4b93beb9b4f30c0ec1d48e197f997ce922202d1 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 26 Jul 2024 07:54:21 +0000 Subject: [PATCH 03/21] tpn nil epoch start trigger --- integrationTests/testProcessorNode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/integrationTests/testProcessorNode.go b/integrationTests/testProcessorNode.go index 7b3aadbe6f7..88d51d11552 100644 --- a/integrationTests/testProcessorNode.go +++ b/integrationTests/testProcessorNode.go @@ -535,6 +535,7 @@ func newBaseTestProcessorNode(args ArgTestProcessorNode) *TestProcessorNode { AppStatusHandler: appStatusHandler, PeersRatingMonitor: peersRatingMonitor, TxExecutionOrderHandler: ordering.NewOrderedCollection(), + EpochStartTrigger: &mock.EpochStartTriggerStub{}, } tpn.NodeKeys = args.NodeKeys From c8ef463cf64a82fa76325c43ce80ba6f2433344a Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 26 Jul 2024 08:06:55 +0000 Subject: [PATCH 04/21] use round handler to get round duration in blockchain hook --- epochStart/metachain/systemSCs_test.go | 2 +- factory/api/apiResolverFactory.go | 2 +- factory/processing/blockProcessorCreator.go | 4 ++-- integrationTests/testProcessorNode.go | 6 +++--- integrationTests/vm/testInitializer.go | 15 ++++----------- integrationTests/vm/wasm/utils.go | 4 +--- process/smartContract/hooks/blockChainHook.go | 12 +++++------- .../smartContract/hooks/blockChainHook_test.go | 3 +-- process/smartContract/hooks/interface.go | 7 +++++++ 9 files changed, 25 insertions(+), 30 deletions(-) diff --git a/epochStart/metachain/systemSCs_test.go b/epochStart/metachain/systemSCs_test.go index a93251f774a..3d45c686a8d 100644 --- a/epochStart/metachain/systemSCs_test.go +++ b/epochStart/metachain/systemSCs_test.go @@ -842,7 +842,7 @@ func createFullArgumentsForSystemSCProcessing(enableEpochsConfig config.EnableEp Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, - NodesSetup: nodesSetup, + RoundHandler: &testscommon.RoundHandlerMock{}, } defaults.FillGasMapInternal(gasSchedule, 1) diff --git a/factory/api/apiResolverFactory.go b/factory/api/apiResolverFactory.go index 3ca1f2df824..5bdaba38dcb 100644 --- a/factory/api/apiResolverFactory.go +++ b/factory/api/apiResolverFactory.go @@ -412,7 +412,7 @@ func createScQueryElement( Accounts: accountsAdapterApi, BlockChain: apiBlockchain, EpochStartTrigger: args.processComponents.EpochStartTrigger(), - NodesSetup: args.coreComponents.GenesisNodesSetup(), + RoundHandler: args.processComponents.RoundHandler(), } var vmFactory process.VirtualMachinesContainerFactory diff --git a/factory/processing/blockProcessorCreator.go b/factory/processing/blockProcessorCreator.go index 40c92d7d41e..d6f4f109e1b 100644 --- a/factory/processing/blockProcessorCreator.go +++ b/factory/processing/blockProcessorCreator.go @@ -1095,7 +1095,7 @@ func (pcf *processComponentsFactory) createVMFactoryShard( Counter: counter, MissingTrieNodesNotifier: notifier, EpochStartTrigger: epochStartTriggerHandler, - NodesSetup: pcf.coreData.GenesisNodesSetup(), + RoundHandler: pcf.coreData.RoundHandler(), } blockChainHookImpl, err := hooks.NewBlockChainHookImpl(argsHook) @@ -1150,7 +1150,7 @@ func (pcf *processComponentsFactory) createVMFactoryMeta( Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), EpochStartTrigger: epochStartTriggerHandler, - NodesSetup: pcf.coreData.GenesisNodesSetup(), + RoundHandler: pcf.coreData.RoundHandler(), } blockChainHookImpl, err := hooks.NewBlockChainHookImpl(argsHook) diff --git a/integrationTests/testProcessorNode.go b/integrationTests/testProcessorNode.go index 88d51d11552..bb23b751a57 100644 --- a/integrationTests/testProcessorNode.go +++ b/integrationTests/testProcessorNode.go @@ -903,7 +903,7 @@ func (tpn *TestProcessorNode) createFullSCQueryService(gasMap map[string]map[str Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: tpn.EpochStartTrigger, - NodesSetup: tpn.NodesSetup, + RoundHandler: tpn.RoundHandler, } var apiBlockchain data.ChainHandler @@ -1655,7 +1655,7 @@ func (tpn *TestProcessorNode) initInnerProcessors(gasMap map[string]map[string]u Counter: counter, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: tpn.EpochStartTrigger, - NodesSetup: tpn.NodesSetup, + RoundHandler: tpn.RoundHandler, } maxGasLimitPerBlock := uint64(0xFFFFFFFFFFFFFFFF) @@ -1894,7 +1894,7 @@ func (tpn *TestProcessorNode) initMetaInnerProcessors(gasMap map[string]map[stri Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: tpn.EpochStartTrigger, - NodesSetup: tpn.NodesSetup, + RoundHandler: tpn.RoundHandler, } var signVerifier vm.MessageSignVerifier diff --git a/integrationTests/vm/testInitializer.go b/integrationTests/vm/testInitializer.go index fa8e15ecc34..534b43e38e8 100644 --- a/integrationTests/vm/testInitializer.go +++ b/integrationTests/vm/testInitializer.go @@ -395,8 +395,6 @@ func CreateTxProcessorWithOneSCExecutorMockVM( defaults.FillGasMapInternal(gasSchedule, 1) gasScheduleNotifier := mock.NewGasScheduleNotifierMock(gasSchedule) - nodesSetup := &genesisMocks.NodesSetupStub{} - builtInFuncs := vmcommonBuiltInFunctions.NewBuiltInFunctionContainer() datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ @@ -420,7 +418,7 @@ func CreateTxProcessorWithOneSCExecutorMockVM( Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, - NodesSetup: nodesSetup, + RoundHandler: &testscommon.RoundHandlerMock{}, } blockChainHook, _ := hooks.NewBlockChainHookImpl(args) @@ -513,7 +511,6 @@ func CreateTxProcessorWithOneSCExecutorMockVM( // CreateOneSCExecutorMockVM - func CreateOneSCExecutorMockVM(accnts state.AccountsAdapter) vmcommon.VMExecutionHandler { datapool := dataRetrieverMock.NewPoolsHolderMock() - nodesSetup := &genesisMocks.NodesSetupStub{} args := hooks.ArgBlockChainHook{ Accounts: accnts, PubkeyConv: pubkeyConv, @@ -535,7 +532,7 @@ func CreateOneSCExecutorMockVM(accnts state.AccountsAdapter) vmcommon.VMExecutio Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, - NodesSetup: nodesSetup, + RoundHandler: &testscommon.RoundHandlerMock{}, } blockChainHook, _ := hooks.NewBlockChainHookImpl(args) vm, _ := mock.NewOneSCExecutorMockVM(blockChainHook, integrationtests.TestHasher) @@ -586,8 +583,6 @@ func CreateVMAndBlockchainHookAndDataPool( esdtTransferParser, _ := parsers.NewESDTTransferParser(integrationtests.TestMarshalizer) counter, _ := counters.NewUsageCounter(esdtTransferParser) - nodesSetup := &genesisMocks.NodesSetupStub{} - datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ Accounts: accnts, @@ -610,7 +605,7 @@ func CreateVMAndBlockchainHookAndDataPool( Counter: counter, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, - NodesSetup: nodesSetup, + RoundHandler: &testscommon.RoundHandlerMock{}, } maxGasLimitPerBlock := uint64(0xFFFFFFFFFFFFFFFF) @@ -681,8 +676,6 @@ func CreateVMAndBlockchainHookMeta( argsBuiltIn.AutomaticCrawlerAddresses = integrationTests.GenerateOneAddressPerShard(argsBuiltIn.ShardCoordinator) builtInFuncFactory, _ := builtInFunctions.CreateBuiltInFunctionsFactory(argsBuiltIn) - nodesSetup := &genesisMocks.NodesSetupStub{} - datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ Accounts: validatorAccounts, @@ -704,7 +697,7 @@ func CreateVMAndBlockchainHookMeta( Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, - NodesSetup: nodesSetup, + RoundHandler: &testscommon.RoundHandlerMock{}, } economicsData, err := createEconomicsData(config.EnableEpochs{}, 1) diff --git a/integrationTests/vm/wasm/utils.go b/integrationTests/vm/wasm/utils.go index 05382aba27c..dd1ef3a3b2d 100644 --- a/integrationTests/vm/wasm/utils.go +++ b/integrationTests/vm/wasm/utils.go @@ -50,7 +50,6 @@ import ( dataRetrieverMock "github.com/multiversx/mx-chain-go/testscommon/dataRetriever" "github.com/multiversx/mx-chain-go/testscommon/dblookupext" "github.com/multiversx/mx-chain-go/testscommon/epochNotifier" - "github.com/multiversx/mx-chain-go/testscommon/genesisMocks" "github.com/multiversx/mx-chain-go/testscommon/guardianMocks" "github.com/multiversx/mx-chain-go/testscommon/integrationtests" "github.com/multiversx/mx-chain-go/testscommon/marshallerMock" @@ -282,7 +281,6 @@ func (context *TestContext) initVMAndBlockchainHook() { blockchainMock := &testscommon.ChainHandlerStub{} chainStorer := &storageStubs.ChainStorerStub{} - nodesSetup := &genesisMocks.NodesSetupStub{} datapool := dataRetrieverMock.NewPoolsHolderMock() args := hooks.ArgBlockChainHook{ Accounts: context.Accounts, @@ -317,7 +315,7 @@ func (context *TestContext) initVMAndBlockchainHook() { Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, - NodesSetup: nodesSetup, + RoundHandler: &testscommon.RoundHandlerMock{}, } vmFactoryConfig := config.VirtualMachineConfig{ diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 8f02ef538f8..78a63a6b940 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -65,7 +65,7 @@ type ArgBlockChainHook struct { Counter BlockChainHookCounter MissingTrieNodesNotifier common.MissingTrieNodesNotifier EpochStartTrigger EpochStartTriggerHandler - NodesSetup sharding.GenesisNodesSetupHandler + RoundHandler RoundHandler } // BlockChainHookImpl is a wrapper over AccountsAdapter that satisfy vmcommon.BlockchainHook interface @@ -84,7 +84,7 @@ type BlockChainHookImpl struct { enableEpochsHandler common.EnableEpochsHandler counter BlockChainHookCounter epochStartTrigger EpochStartTriggerHandler - nodesSetup sharding.GenesisNodesSetupHandler + roundHandler RoundHandler mutCurrentHdr sync.RWMutex currentHdr data.HeaderHandler @@ -134,7 +134,7 @@ func NewBlockChainHookImpl( counter: args.Counter, missingTrieNodesNotifier: args.MissingTrieNodesNotifier, epochStartTrigger: args.EpochStartTrigger, - nodesSetup: args.NodesSetup, + roundHandler: args.RoundHandler, } err = blockChainHookImpl.makeCompiledSCStorage() @@ -404,11 +404,9 @@ func (bh *BlockChainHookImpl) LastEpoch() uint32 { // RoundTime returns the duration of a round func (bh *BlockChainHookImpl) RoundTime() uint64 { - if bh.nodesSetup == nil { - return 0 - } + roundDuration := bh.roundHandler.TimeDuration() - return bh.nodesSetup.GetRoundDuration() + return uint64(roundDuration.Milliseconds()) } // EpochStartBlockTimeStamp returns the timestamp of the first block of the current epoch diff --git a/process/smartContract/hooks/blockChainHook_test.go b/process/smartContract/hooks/blockChainHook_test.go index 5d0dd50fcde..8d5a0ac4b5a 100644 --- a/process/smartContract/hooks/blockChainHook_test.go +++ b/process/smartContract/hooks/blockChainHook_test.go @@ -29,7 +29,6 @@ import ( dataRetrieverMock "github.com/multiversx/mx-chain-go/testscommon/dataRetriever" "github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock" "github.com/multiversx/mx-chain-go/testscommon/epochNotifier" - "github.com/multiversx/mx-chain-go/testscommon/genesisMocks" "github.com/multiversx/mx-chain-go/testscommon/marshallerMock" stateMock "github.com/multiversx/mx-chain-go/testscommon/state" storageStubs "github.com/multiversx/mx-chain-go/testscommon/storage" @@ -71,7 +70,7 @@ func createMockBlockChainHookArgs() hooks.ArgBlockChainHook { Counter: &testscommon.BlockChainHookCounterStub{}, MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, - NodesSetup: &genesisMocks.NodesSetupStub{}, + RoundHandler: &testscommon.RoundHandlerMock{}, } return arguments } diff --git a/process/smartContract/hooks/interface.go b/process/smartContract/hooks/interface.go index 748b3457d1b..3f62c5f192d 100644 --- a/process/smartContract/hooks/interface.go +++ b/process/smartContract/hooks/interface.go @@ -1,6 +1,8 @@ package hooks import ( + "time" + "github.com/multiversx/mx-chain-core-go/data" vmcommon "github.com/multiversx/mx-chain-vm-common-go" ) @@ -19,3 +21,8 @@ type BlockChainHookCounter interface { type EpochStartTriggerHandler interface { EpochStartHdr() data.HeaderHandler } + +// RoundHandler defines the operations of a round handler needed by the blockchain hook +type RoundHandler interface { + TimeDuration() time.Duration +} From 933456187619ef717d8585cce0bd11b7a21b129c Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 26 Jul 2024 08:54:59 +0000 Subject: [PATCH 05/21] fix nil pointer epoch start block header --- process/smartContract/hooks/blockChainHook.go | 1 + 1 file changed, 1 insertion(+) diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 78a63a6b940..2f82b666f6d 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -144,6 +144,7 @@ func NewBlockChainHookImpl( blockChainHookImpl.ClearCompiledCodes() blockChainHookImpl.currentHdr = &block.Header{} + blockChainHookImpl.epochStartHdr = &block.Header{} blockChainHookImpl.mapActivationEpochs = createMapActivationEpochs(&args.EnableEpochs) blockChainHookImpl.vmContainer = containers.NewVirtualMachinesContainer() From b9eef0c01a11ecbd12cf2888d76738c1de2ee191 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Mon, 29 Jul 2024 07:56:43 +0000 Subject: [PATCH 06/21] nil checks for RoundHandler and EpochStartTriggerHandler --- process/smartContract/hooks/blockChainHook.go | 6 ++++++ process/smartContract/hooks/errors.go | 6 ++++++ process/smartContract/hooks/interface.go | 2 ++ 3 files changed, 14 insertions(+) diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 2f82b666f6d..15a2dfaefa7 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -227,6 +227,12 @@ func checkForNil(args ArgBlockChainHook) error { if check.IfNil(args.MissingTrieNodesNotifier) { return ErrNilMissingTrieNodesNotifier } + if check.IfNil(args.EpochStartTrigger) { + return ErrNilEpochStartTriggerHandler + } + if check.IfNil(args.RoundHandler) { + return ErrNilRoundHandler + } return nil } diff --git a/process/smartContract/hooks/errors.go b/process/smartContract/hooks/errors.go index 43894f455e0..bc20d58ea3f 100644 --- a/process/smartContract/hooks/errors.go +++ b/process/smartContract/hooks/errors.go @@ -19,3 +19,9 @@ var ErrNilBlockchainHookCounter = errors.New("nil blockchain hook counter") // ErrNilMissingTrieNodesNotifier signals that a nil missing trie nodes notifier was provided var ErrNilMissingTrieNodesNotifier = errors.New("nil missing trie nodes notifier") + +// ErrNilEpochStartTriggerHandler signals that a nil epoch start trigger handler was provided +var ErrNilEpochStartTriggerHandler = errors.New("nil epoch start trigger handler") + +// ErrNilRoundHandler signals that a nil round handler was provided +var ErrNilRoundHandler = errors.New("nil round handler") diff --git a/process/smartContract/hooks/interface.go b/process/smartContract/hooks/interface.go index 3f62c5f192d..090b132af69 100644 --- a/process/smartContract/hooks/interface.go +++ b/process/smartContract/hooks/interface.go @@ -20,9 +20,11 @@ type BlockChainHookCounter interface { // EpochStartTriggerHandler defines the operations of an epoch start trigger handler needed by the blockchain hook type EpochStartTriggerHandler interface { EpochStartHdr() data.HeaderHandler + IsInterfaceNil() bool } // RoundHandler defines the operations of a round handler needed by the blockchain hook type RoundHandler interface { TimeDuration() time.Duration + IsInterfaceNil() bool } From f94bc3cee55e3556fee3b1fe7c08e08245df3320 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Mon, 29 Jul 2024 08:16:40 +0000 Subject: [PATCH 07/21] fix more RoundHandler nils in tests --- genesis/process/disabled/roundHandler.go | 45 +++++++++++++++++++ genesis/process/genesisBlockCreator.go | 1 + genesis/process/metaGenesisBlockCreator.go | 3 ++ genesis/process/shardGenesisBlockCreator.go | 3 ++ .../vm/staking/systemSCCreator.go | 1 + 5 files changed, 53 insertions(+) create mode 100644 genesis/process/disabled/roundHandler.go diff --git a/genesis/process/disabled/roundHandler.go b/genesis/process/disabled/roundHandler.go new file mode 100644 index 00000000000..a4e718820f3 --- /dev/null +++ b/genesis/process/disabled/roundHandler.go @@ -0,0 +1,45 @@ +package disabled + +import "time" + +// RequestHandler implements the RequestHandler interface but does nothing as it is disabled +type RoundHandler struct { +} + +// BeforeGenesis - +func (rh *RoundHandler) BeforeGenesis() bool { + return false +} + +// Index - +func (rh *RoundHandler) Index() int64 { + return 0 +} + +// TimeDuration - +func (rh *RoundHandler) TimeDuration() time.Duration { + return 0 +} + +// TimeStamp - +func (rh *RoundHandler) TimeStamp() time.Time { + return time.Unix(0, 0) +} + +// UpdateRound - +func (rh *RoundHandler) UpdateRound(genesisRoundTimeStamp time.Time, timeStamp time.Time) { +} + +// RemainingTime - +func (rh *RoundHandler) RemainingTime(startTime time.Time, maxTime time.Duration) time.Duration { + return 0 +} + +// IncrementIndex - +func (rh *RoundHandler) IncrementIndex() { +} + +// IsInterfaceNil returns true if there is no value under the interface +func (rh *RoundHandler) IsInterfaceNil() bool { + return rh == nil +} diff --git a/genesis/process/genesisBlockCreator.go b/genesis/process/genesisBlockCreator.go index 58328e2e727..977b60d23f5 100644 --- a/genesis/process/genesisBlockCreator.go +++ b/genesis/process/genesisBlockCreator.go @@ -449,6 +449,7 @@ func (gbc *genesisBlockCreator) computeDNSAddresses(enableEpochsConfig config.En Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), EpochStartTrigger: epochStart.NewEpochStartTrigger(), + RoundHandler: &disabled.RoundHandler{}, } blockChainHook, err := hooks.NewBlockChainHookImpl(argsHook) if err != nil { diff --git a/genesis/process/metaGenesisBlockCreator.go b/genesis/process/metaGenesisBlockCreator.go index 78546562736..6b561297b7c 100644 --- a/genesis/process/metaGenesisBlockCreator.go +++ b/genesis/process/metaGenesisBlockCreator.go @@ -23,6 +23,7 @@ import ( "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/dataRetriever" "github.com/multiversx/mx-chain-go/dataRetriever/blockchain" + epochStart "github.com/multiversx/mx-chain-go/epochStart/bootstrap/disabled" "github.com/multiversx/mx-chain-go/genesis" "github.com/multiversx/mx-chain-go/genesis/process/disabled" "github.com/multiversx/mx-chain-go/process" @@ -337,6 +338,8 @@ func createProcessorsForMetaGenesisBlock(arg ArgsGenesisBlockCreator, enableEpoc GasSchedule: arg.GasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), + EpochStartTrigger: epochStart.NewEpochStartTrigger(), + RoundHandler: &disabled.RoundHandler{}, } pubKeyVerifier, err := disabled.NewMessageSignVerifier(arg.BlockSignKeyGen) diff --git a/genesis/process/shardGenesisBlockCreator.go b/genesis/process/shardGenesisBlockCreator.go index b44ed14c207..d983be95ae6 100644 --- a/genesis/process/shardGenesisBlockCreator.go +++ b/genesis/process/shardGenesisBlockCreator.go @@ -18,6 +18,7 @@ import ( "github.com/multiversx/mx-chain-go/common/holders" "github.com/multiversx/mx-chain-go/config" "github.com/multiversx/mx-chain-go/dataRetriever/blockchain" + epochStart "github.com/multiversx/mx-chain-go/epochStart/bootstrap/disabled" "github.com/multiversx/mx-chain-go/genesis" "github.com/multiversx/mx-chain-go/genesis/process/disabled" "github.com/multiversx/mx-chain-go/genesis/process/intermediate" @@ -400,6 +401,8 @@ func createProcessorsForShardGenesisBlock(arg ArgsGenesisBlockCreator, enableEpo GasSchedule: arg.GasSchedule, Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), + EpochStartTrigger: epochStart.NewEpochStartTrigger(), + RoundHandler: &disabled.RoundHandler{}, } esdtTransferParser, err := parsers.NewESDTTransferParser(arg.Core.InternalMarshalizer()) if err != nil { diff --git a/integrationTests/vm/staking/systemSCCreator.go b/integrationTests/vm/staking/systemSCCreator.go index 966f60561f2..32d42fc550e 100644 --- a/integrationTests/vm/staking/systemSCCreator.go +++ b/integrationTests/vm/staking/systemSCCreator.go @@ -178,6 +178,7 @@ func createBlockChainHook( Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: &testscommon.MissingTrieNodesNotifierStub{}, EpochStartTrigger: &testscommon.EpochStartTriggerStub{}, + RoundHandler: &testscommon.RoundHandlerMock{}, } blockChainHook, _ := hooks.NewBlockChainHookImpl(argsHook) From 8d9114d84151a5393ec77ba2c9c42090ff12a341 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Mon, 29 Jul 2024 14:03:53 +0000 Subject: [PATCH 08/21] deep copy header when returning from trigger --- .../disabled/disabledEpochStartTrigger.go | 6 ++-- epochStart/interface.go | 2 +- epochStart/metachain/trigger.go | 22 +++++++++++--- epochStart/shardchain/trigger.go | 19 +++++++++--- factory/mock/epochStartTriggerStub.go | 8 ++--- factory/processing/blockProcessorCreator.go | 2 +- .../mock/endOfEpochTriggerStub.go | 8 ++--- node/mock/endOfEpochTriggerStub.go | 8 ++--- process/interface.go | 2 +- process/mock/endOfEpochTriggerStub.go | 8 ++--- process/smartContract/hooks/blockChainHook.go | 30 ++++++++++++++----- process/smartContract/hooks/errors.go | 3 ++ process/smartContract/hooks/interface.go | 2 +- testscommon/epochStartTriggerStub.go | 8 ++--- 14 files changed, 86 insertions(+), 42 deletions(-) diff --git a/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go b/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go index 4042e35ed36..9eb293e910f 100644 --- a/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go +++ b/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go @@ -54,9 +54,9 @@ func (e *epochStartTrigger) EpochStartMetaHdrHash() []byte { return nil } -// EpochStartHdr - -func (e *epochStartTrigger) EpochStartHdr() data.HeaderHandler { - return nil +// LastCommitedEpochStartHdr - +func (e *epochStartTrigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { + return nil, nil } // GetSavedStateKey - diff --git a/epochStart/interface.go b/epochStart/interface.go index 274fe0ca336..c08a8ab06e6 100644 --- a/epochStart/interface.go +++ b/epochStart/interface.go @@ -22,7 +22,7 @@ type TriggerHandler interface { Update(round uint64, nonce uint64) EpochStartRound() uint64 EpochStartMetaHdrHash() []byte - EpochStartHdr() data.HeaderHandler + LastCommitedEpochStartHdr() (data.HeaderHandler, error) GetSavedStateKey() []byte LoadState(key []byte) error SetProcessed(header data.HeaderHandler, body data.BodyHandler) diff --git a/epochStart/metachain/trigger.go b/epochStart/metachain/trigger.go index 95e96fd78ec..a39f4f94e24 100644 --- a/epochStart/metachain/trigger.go +++ b/epochStart/metachain/trigger.go @@ -21,7 +21,7 @@ import ( "github.com/multiversx/mx-chain-go/epochStart" "github.com/multiversx/mx-chain-go/process" "github.com/multiversx/mx-chain-go/storage" - "github.com/multiversx/mx-chain-logger-go" + logger "github.com/multiversx/mx-chain-logger-go" ) var log = logger.GetOrCreate("epochStart/metachain") @@ -403,9 +403,23 @@ func (t *trigger) EpochStartMetaHdrHash() []byte { return t.epochStartMetaHash } -// EpochStartHdr returns the header of the epoch start block -func (t *trigger) EpochStartHdr() data.HeaderHandler { - return t.epochStartMeta +// LastCommitedEpochStartHdr returns the header of the epoch start block +func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { + t.mutTrigger.RLock() + defer t.mutTrigger.RUnlock() + + // marshal + unmarshal deep copy + headerBytes, err := t.marshaller.Marshal(t.epochStartMeta) + if err != nil { + return nil, err + } + + header, err := process.UnmarshalMetaHeader(t.marshaller, headerBytes) + if err != nil { + return nil, err + } + + return header, nil } // GetSavedStateKey returns the last saved trigger state key diff --git a/epochStart/shardchain/trigger.go b/epochStart/shardchain/trigger.go index cc098eced85..5f494b4444f 100644 --- a/epochStart/shardchain/trigger.go +++ b/epochStart/shardchain/trigger.go @@ -24,7 +24,7 @@ import ( "github.com/multiversx/mx-chain-go/epochStart" "github.com/multiversx/mx-chain-go/process" "github.com/multiversx/mx-chain-go/storage" - "github.com/multiversx/mx-chain-logger-go" + logger "github.com/multiversx/mx-chain-logger-go" ) var log = logger.GetOrCreate("epochStart/shardchain") @@ -1102,12 +1102,23 @@ func (t *trigger) EpochStartMetaHdrHash() []byte { return t.epochMetaBlockHash } -// EpochStartHdr returns the epoch start header -func (t *trigger) EpochStartHdr() data.HeaderHandler { +// LastCommitedEpochStartHdr returns the epoch start header +func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { t.mutTrigger.RLock() defer t.mutTrigger.RUnlock() - return t.epochStartShardHeader + // marshal + unmarshal deep copy + headerBytes, err := t.marshaller.Marshal(t.epochStartShardHeader) + if err != nil { + return nil, err + } + + header, err := process.UnmarshalShardHeader(t.marshaller, headerBytes) + if err != nil { + return nil, err + } + + return header, nil } // GetSavedStateKey returns the last saved trigger state key diff --git a/factory/mock/epochStartTriggerStub.go b/factory/mock/epochStartTriggerStub.go index e121fa4d11d..a151e0a17ea 100644 --- a/factory/mock/epochStartTriggerStub.go +++ b/factory/mock/epochStartTriggerStub.go @@ -50,13 +50,13 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } -// EpochStartHdr - -func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { +// LastCommitedEpochStartHdr - +func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled() + return e.EpochStartHdrCalled(), nil } - return nil + return nil, nil } // GetSavedStateKey - diff --git a/factory/processing/blockProcessorCreator.go b/factory/processing/blockProcessorCreator.go index d6f4f109e1b..cc8c6973781 100644 --- a/factory/processing/blockProcessorCreator.go +++ b/factory/processing/blockProcessorCreator.go @@ -1150,7 +1150,7 @@ func (pcf *processComponentsFactory) createVMFactoryMeta( Counter: counters.NewDisabledCounter(), MissingTrieNodesNotifier: syncer.NewMissingTrieNodesNotifier(), EpochStartTrigger: epochStartTriggerHandler, - RoundHandler: pcf.coreData.RoundHandler(), + RoundHandler: pcf.coreData.RoundHandler(), // TODO: @laurci - this needs to be replaced when changing the round duration } blockChainHookImpl, err := hooks.NewBlockChainHookImpl(argsHook) diff --git a/integrationTests/mock/endOfEpochTriggerStub.go b/integrationTests/mock/endOfEpochTriggerStub.go index 61d3bf8a37c..e2490e20ef9 100644 --- a/integrationTests/mock/endOfEpochTriggerStub.go +++ b/integrationTests/mock/endOfEpochTriggerStub.go @@ -50,12 +50,12 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } -// EpochStartHdr - -func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { +// LastCommitedEpochStartHdr - +func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled() + return e.EpochStartHdrCalled(), nil } - return nil + return nil, nil } // GetSavedStateKey - diff --git a/node/mock/endOfEpochTriggerStub.go b/node/mock/endOfEpochTriggerStub.go index 5ca23408b3c..3f9c198576b 100644 --- a/node/mock/endOfEpochTriggerStub.go +++ b/node/mock/endOfEpochTriggerStub.go @@ -59,13 +59,13 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } -// EpochStartHdr - -func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { +// LastCommitedEpochStartHdr - +func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled() + return e.EpochStartHdrCalled(), nil } - return nil + return nil, nil } // Revert - diff --git a/process/interface.go b/process/interface.go index d8d2dd1f63d..df38dbd7269 100644 --- a/process/interface.go +++ b/process/interface.go @@ -469,7 +469,7 @@ type EpochStartTriggerHandler interface { Epoch() uint32 MetaEpoch() uint32 EpochStartRound() uint64 - EpochStartHdr() data.HeaderHandler + LastCommitedEpochStartHdr() (data.HeaderHandler, error) SetProcessed(header data.HeaderHandler, body data.BodyHandler) RevertStateToBlock(header data.HeaderHandler) error EpochStartMetaHdrHash() []byte diff --git a/process/mock/endOfEpochTriggerStub.go b/process/mock/endOfEpochTriggerStub.go index 2f3866c7d00..df03c7f1268 100644 --- a/process/mock/endOfEpochTriggerStub.go +++ b/process/mock/endOfEpochTriggerStub.go @@ -84,12 +84,12 @@ func (e *EpochStartTriggerStub) EpochStartRound() uint64 { return 0 } -// ReceivedHeader - -func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { +// LastCommitedEpochStartHdr - +func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled() + return e.EpochStartHdrCalled(), nil } - return nil + return nil, nil } // Update - diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 15a2dfaefa7..a8db0c2cc49 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -65,7 +65,7 @@ type ArgBlockChainHook struct { Counter BlockChainHookCounter MissingTrieNodesNotifier common.MissingTrieNodesNotifier EpochStartTrigger EpochStartTriggerHandler - RoundHandler RoundHandler + RoundHandler RoundHandler // TODO: @laurci - this needs to be replaced when changing the round duration } // BlockChainHookImpl is a wrapper over AccountsAdapter that satisfy vmcommon.BlockchainHook interface @@ -84,7 +84,7 @@ type BlockChainHookImpl struct { enableEpochsHandler common.EnableEpochsHandler counter BlockChainHookCounter epochStartTrigger EpochStartTriggerHandler - roundHandler RoundHandler + roundHandler RoundHandler // TODO: @laurci - this needs to be replaced when changing the round duration mutCurrentHdr sync.RWMutex currentHdr data.HeaderHandler @@ -411,6 +411,7 @@ func (bh *BlockChainHookImpl) LastEpoch() uint32 { // RoundTime returns the duration of a round func (bh *BlockChainHookImpl) RoundTime() uint64 { + // TODO: @laurci - this needs to be replaced when changing the round duration roundDuration := bh.roundHandler.TimeDuration() return uint64(roundDuration.Milliseconds()) @@ -801,24 +802,39 @@ func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) { bh.mutCurrentHdr.Lock() bh.currentHdr = hdr - bh.updateEpochStartHeader(hdr) + err := bh.updateEpochStartHeader(hdr) + if err != nil { + log.Debug("BlockChainHookImpl.SetCurrentHeader: updateEpochStartHeader", "error", err) + } + bh.mutCurrentHdr.Unlock() } -func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) { +func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) error { bh.mutEpochStartHdr.Lock() defer bh.mutEpochStartHdr.Unlock() if hdr.IsStartOfEpochBlock() { bh.epochStartHdr = hdr - return + return nil } if bh.epochStartHdr.GetEpoch() == hdr.GetEpoch() { - return + return nil } - bh.epochStartHdr = bh.epochStartTrigger.EpochStartHdr() + epochStartHdr, err := bh.epochStartTrigger.LastCommitedEpochStartHdr() + if err != nil { + return err + } + + if epochStartHdr.GetEpoch() != hdr.GetEpoch() { + return ErrLastCommitedEpochStartHdrMismatch + } + + bh.epochStartHdr = epochStartHdr + + return nil } // SaveCompiledCode saves the compiled code to cache and storage diff --git a/process/smartContract/hooks/errors.go b/process/smartContract/hooks/errors.go index bc20d58ea3f..73c8a2072da 100644 --- a/process/smartContract/hooks/errors.go +++ b/process/smartContract/hooks/errors.go @@ -25,3 +25,6 @@ var ErrNilEpochStartTriggerHandler = errors.New("nil epoch start trigger handler // ErrNilRoundHandler signals that a nil round handler was provided var ErrNilRoundHandler = errors.New("nil round handler") + +// ErrLastCommitedEpochStartHdrMismatch signals that the current header epoch and last commited epoch start header epoch do not match +var ErrLastCommitedEpochStartHdrMismatch = errors.New("current header epoch and last commited epoch start header epoch do not match") diff --git a/process/smartContract/hooks/interface.go b/process/smartContract/hooks/interface.go index 090b132af69..53038df7997 100644 --- a/process/smartContract/hooks/interface.go +++ b/process/smartContract/hooks/interface.go @@ -19,7 +19,7 @@ type BlockChainHookCounter interface { // EpochStartTriggerHandler defines the operations of an epoch start trigger handler needed by the blockchain hook type EpochStartTriggerHandler interface { - EpochStartHdr() data.HeaderHandler + LastCommitedEpochStartHdr() (data.HeaderHandler, error) IsInterfaceNil() bool } diff --git a/testscommon/epochStartTriggerStub.go b/testscommon/epochStartTriggerStub.go index ec64e5377fb..87493a51b3b 100644 --- a/testscommon/epochStartTriggerStub.go +++ b/testscommon/epochStartTriggerStub.go @@ -57,12 +57,12 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { return nil } -// EpochStartHdr - -func (e *EpochStartTriggerStub) EpochStartHdr() data.HeaderHandler { +// LastCommitedEpochStartHdr - +func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled() + return e.EpochStartHdrCalled(), nil } - return nil + return nil, nil } // GetSavedStateKey - From 0684c299572e307d1ce45fbad953561559a54370 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Mon, 29 Jul 2024 14:22:23 +0000 Subject: [PATCH 09/21] epochStartHdr nil error check --- .../disabled/disabledEpochStartTrigger.go | 3 ++- factory/mock/epochStartTriggerStub.go | 25 ++++++++++--------- .../mock/endOfEpochTriggerStub.go | 25 ++++++++++--------- node/mock/endOfEpochTriggerStub.go | 25 ++++++++++--------- process/mock/endOfEpochTriggerStub.go | 9 ++++--- process/smartContract/hooks/blockChainHook.go | 4 +++ process/smartContract/hooks/errors.go | 3 +++ testscommon/epochStartTriggerStub.go | 9 ++++--- 8 files changed, 58 insertions(+), 45 deletions(-) diff --git a/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go b/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go index 9eb293e910f..a83acce4317 100644 --- a/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go +++ b/epochStart/bootstrap/disabled/disabledEpochStartTrigger.go @@ -2,6 +2,7 @@ package disabled import ( "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-core-go/data/block" ) type epochStartTrigger struct { @@ -56,7 +57,7 @@ func (e *epochStartTrigger) EpochStartMetaHdrHash() []byte { // LastCommitedEpochStartHdr - func (e *epochStartTrigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { - return nil, nil + return &block.HeaderV2{}, nil } // GetSavedStateKey - diff --git a/factory/mock/epochStartTriggerStub.go b/factory/mock/epochStartTriggerStub.go index a151e0a17ea..e8bc68f89b3 100644 --- a/factory/mock/epochStartTriggerStub.go +++ b/factory/mock/epochStartTriggerStub.go @@ -3,20 +3,21 @@ package mock import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-go/epochStart" ) // EpochStartTriggerStub - type EpochStartTriggerStub struct { - ForceEpochStartCalled func(round uint64) - IsEpochStartCalled func() bool - EpochCalled func() uint32 - MetaEpochCalled func() uint32 - EpochStartHdrCalled func() data.HeaderHandler - ReceivedHeaderCalled func(handler data.HeaderHandler) - UpdateCalled func(round uint64, nonce uint64) - ProcessedCalled func(header data.HeaderHandler) - EpochStartRoundCalled func() uint64 + ForceEpochStartCalled func(round uint64) + IsEpochStartCalled func() bool + EpochCalled func() uint32 + MetaEpochCalled func() uint32 + LastCommitedEpochStartHdrCalled func() (data.HeaderHandler, error) + ReceivedHeaderCalled func(handler data.HeaderHandler) + UpdateCalled func(round uint64, nonce uint64) + ProcessedCalled func(header data.HeaderHandler) + EpochStartRoundCalled func() uint64 } // RevertStateToBlock - @@ -52,11 +53,11 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { // LastCommitedEpochStartHdr - func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { - if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled(), nil + if e.LastCommitedEpochStartHdrCalled != nil { + return e.LastCommitedEpochStartHdrCalled() } - return nil, nil + return &block.HeaderV2{}, nil } // GetSavedStateKey - diff --git a/integrationTests/mock/endOfEpochTriggerStub.go b/integrationTests/mock/endOfEpochTriggerStub.go index e2490e20ef9..e71a4cc4502 100644 --- a/integrationTests/mock/endOfEpochTriggerStub.go +++ b/integrationTests/mock/endOfEpochTriggerStub.go @@ -3,20 +3,21 @@ package mock import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-go/epochStart" ) // EpochStartTriggerStub - type EpochStartTriggerStub struct { - ForceEpochStartCalled func(round uint64) - IsEpochStartCalled func() bool - EpochCalled func() uint32 - MetaEpochCalled func() uint32 - EpochStartHdrCalled func() data.HeaderHandler - ReceivedHeaderCalled func(handler data.HeaderHandler) - UpdateCalled func(round uint64, nonce uint64) - ProcessedCalled func(header data.HeaderHandler) - EpochStartRoundCalled func() uint64 + ForceEpochStartCalled func(round uint64) + IsEpochStartCalled func() bool + EpochCalled func() uint32 + MetaEpochCalled func() uint32 + LastCommitedEpochStartHdrCalled func() (data.HeaderHandler, error) + ReceivedHeaderCalled func(handler data.HeaderHandler) + UpdateCalled func(round uint64, nonce uint64) + ProcessedCalled func(header data.HeaderHandler) + EpochStartRoundCalled func() uint64 } // RevertStateToBlock - @@ -52,10 +53,10 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { // LastCommitedEpochStartHdr - func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { - if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled(), nil + if e.LastCommitedEpochStartHdrCalled != nil { + return e.LastCommitedEpochStartHdrCalled() } - return nil, nil + return &block.HeaderV2{}, nil } // GetSavedStateKey - diff --git a/node/mock/endOfEpochTriggerStub.go b/node/mock/endOfEpochTriggerStub.go index 3f9c198576b..f00d4fe43a5 100644 --- a/node/mock/endOfEpochTriggerStub.go +++ b/node/mock/endOfEpochTriggerStub.go @@ -3,19 +3,20 @@ package mock import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-core-go/data/block" ) // EpochStartTriggerStub - type EpochStartTriggerStub struct { - ForceEpochStartCalled func(round uint64) - IsEpochStartCalled func() bool - EpochCalled func() uint32 - MetaEpochCalled func() uint32 - EpochStartHdrCalled func() data.HeaderHandler - ReceivedHeaderCalled func(handler data.HeaderHandler) - UpdateCalled func(round uint64, nonce uint64) - ProcessedCalled func(header data.HeaderHandler) - EpochStartRoundCalled func() uint64 + ForceEpochStartCalled func(round uint64) + IsEpochStartCalled func() bool + EpochCalled func() uint32 + MetaEpochCalled func() uint32 + LastCommitedEpochStartHdrCalled func() (data.HeaderHandler, error) + ReceivedHeaderCalled func(handler data.HeaderHandler) + UpdateCalled func(round uint64, nonce uint64) + ProcessedCalled func(header data.HeaderHandler) + EpochStartRoundCalled func() uint64 } // RevertStateToBlock - @@ -61,11 +62,11 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { // LastCommitedEpochStartHdr - func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { - if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled(), nil + if e.LastCommitedEpochStartHdrCalled != nil { + return e.LastCommitedEpochStartHdrCalled() } - return nil, nil + return &block.HeaderV2{}, nil } // Revert - diff --git a/process/mock/endOfEpochTriggerStub.go b/process/mock/endOfEpochTriggerStub.go index df03c7f1268..f5fe9beda81 100644 --- a/process/mock/endOfEpochTriggerStub.go +++ b/process/mock/endOfEpochTriggerStub.go @@ -3,6 +3,7 @@ package mock import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-core-go/data/block" ) // EpochStartTriggerStub - @@ -15,7 +16,7 @@ type EpochStartTriggerStub struct { UpdateCalled func(round uint64, nonce uint64) ProcessedCalled func(header data.HeaderHandler) EpochStartRoundCalled func() uint64 - EpochStartHdrCalled func() data.HeaderHandler + LastCommitedEpochStartHdrCalled func() (data.HeaderHandler, error) EpochFinalityAttestingRoundCalled func() uint64 EpochStartMetaHdrHashCalled func() []byte } @@ -86,10 +87,10 @@ func (e *EpochStartTriggerStub) EpochStartRound() uint64 { // LastCommitedEpochStartHdr - func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { - if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled(), nil + if e.LastCommitedEpochStartHdrCalled != nil { + return e.LastCommitedEpochStartHdrCalled() } - return nil, nil + return &block.HeaderV2{}, nil } // Update - diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index a8db0c2cc49..8b5415bd803 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -828,6 +828,10 @@ func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) err return err } + if epochStartHdr == nil { + return ErrNilLastCommitedEpochStartHdr + } + if epochStartHdr.GetEpoch() != hdr.GetEpoch() { return ErrLastCommitedEpochStartHdrMismatch } diff --git a/process/smartContract/hooks/errors.go b/process/smartContract/hooks/errors.go index 73c8a2072da..9fee23fbc2b 100644 --- a/process/smartContract/hooks/errors.go +++ b/process/smartContract/hooks/errors.go @@ -26,5 +26,8 @@ var ErrNilEpochStartTriggerHandler = errors.New("nil epoch start trigger handler // ErrNilRoundHandler signals that a nil round handler was provided var ErrNilRoundHandler = errors.New("nil round handler") +// ErrNilLastCommitedEpochStartHdr signals that a nil last commited epoch start header was provided +var ErrNilLastCommitedEpochStartHdr = errors.New("nil last commited epoch start header") + // ErrLastCommitedEpochStartHdrMismatch signals that the current header epoch and last commited epoch start header epoch do not match var ErrLastCommitedEpochStartHdrMismatch = errors.New("current header epoch and last commited epoch start header epoch do not match") diff --git a/testscommon/epochStartTriggerStub.go b/testscommon/epochStartTriggerStub.go index 87493a51b3b..8adc8073f2c 100644 --- a/testscommon/epochStartTriggerStub.go +++ b/testscommon/epochStartTriggerStub.go @@ -3,6 +3,7 @@ package testscommon import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data" + "github.com/multiversx/mx-chain-core-go/data/block" ) // EpochStartTriggerStub - @@ -11,7 +12,7 @@ type EpochStartTriggerStub struct { IsEpochStartCalled func() bool EpochCalled func() uint32 MetaEpochCalled func() uint32 - EpochStartHdrCalled func() data.HeaderHandler + LastCommitedEpochStartHdrCalled func() (data.HeaderHandler, error) ReceivedHeaderCalled func(handler data.HeaderHandler) UpdateCalled func(round uint64, nonce uint64) ProcessedCalled func(header data.HeaderHandler) @@ -59,10 +60,10 @@ func (e *EpochStartTriggerStub) EpochStartMetaHdrHash() []byte { // LastCommitedEpochStartHdr - func (e *EpochStartTriggerStub) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { - if e.EpochStartHdrCalled != nil { - return e.EpochStartHdrCalled(), nil + if e.LastCommitedEpochStartHdrCalled != nil { + return e.LastCommitedEpochStartHdrCalled() } - return nil, nil + return &block.HeaderV2{}, nil } // GetSavedStateKey - From 53f4563aac1ef1809f2e5d77789682179e71ff1f Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Tue, 30 Jul 2024 07:43:45 +0000 Subject: [PATCH 10/21] propagate SetCurrentHeader errors --- .../vm/delegation/changeOwner_test.go | 3 +- .../vm/delegation/delegationMulti_test.go | 26 ++-- .../vm/delegation/delegationScenarios_test.go | 114 +++++++++++------- .../testMetaProcessorWithCustomNodesConfig.go | 15 ++- integrationTests/vm/txsFee/scCalls_test.go | 15 ++- .../vm/txsFee/validatorSC_test.go | 16 ++- process/block/metablock.go | 13 +- process/block/shardblock.go | 11 +- process/interface.go | 2 +- process/smartContract/hooks/blockChainHook.go | 11 +- .../hooks/blockChainHook_test.go | 24 ++-- process/smartContract/hooks/errors.go | 3 + process/smartContract/scQueryService.go | 6 +- .../transactionSimulator.go | 5 +- testscommon/blockChainHookStub.go | 6 +- 15 files changed, 178 insertions(+), 92 deletions(-) diff --git a/integrationTests/vm/delegation/changeOwner_test.go b/integrationTests/vm/delegation/changeOwner_test.go index c634452ea9c..9caae7742c5 100644 --- a/integrationTests/vm/delegation/changeOwner_test.go +++ b/integrationTests/vm/delegation/changeOwner_test.go @@ -83,7 +83,8 @@ func testDelegationChangeOwnerOnAccountHandler(t *testing.T, epochToTest uint32) tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: epochToTest, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + require.Nil(t, err) delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(2000), firstOwner) verifyDelegatorsStake(t, tpn, "getUserActiveStake", [][]byte{firstOwner}, delegationScAddress, big.NewInt(2000)) diff --git a/integrationTests/vm/delegation/delegationMulti_test.go b/integrationTests/vm/delegation/delegationMulti_test.go index b0eef67dcaa..a620bb24296 100644 --- a/integrationTests/vm/delegation/delegationMulti_test.go +++ b/integrationTests/vm/delegation/delegationMulti_test.go @@ -36,7 +36,8 @@ func TestDelegationSystemClaimMulti(t *testing.T) { tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) ownerAddresses := getAddresses(numContracts) @@ -73,11 +74,11 @@ func TestDelegationSystemClaimMulti(t *testing.T) { verifyValidatorSCStake(t, tpn, delegationScAddresses[1], big.NewInt(5000)) for i := range delegationScAddresses { - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(1000), 1, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(2000), 2, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(3000), 3, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(4000), 4, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(5000), 5, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(1000), 1, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(2000), 2, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(3000), 3, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(4000), 4, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(5000), 5, 1) } for i := range delegationScAddresses { @@ -148,7 +149,8 @@ func TestDelegationSystemRedelegateMulti(t *testing.T) { tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) ownerAddresses := getAddresses(numContracts) @@ -185,11 +187,11 @@ func TestDelegationSystemRedelegateMulti(t *testing.T) { verifyValidatorSCStake(t, tpn, delegationScAddresses[1], big.NewInt(5000)) for i := range delegationScAddresses { - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(1000), 1, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(2000), 2, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(3000), 3, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(4000), 4, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(5000), 5, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(1000), 1, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(2000), 2, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(3000), 3, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(4000), 4, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(5000), 5, 1) } for i := range delegationScAddresses { diff --git a/integrationTests/vm/delegation/delegationScenarios_test.go b/integrationTests/vm/delegation/delegationScenarios_test.go index 4b9dbd07fba..45c479cec06 100644 --- a/integrationTests/vm/delegation/delegationScenarios_test.go +++ b/integrationTests/vm/delegation/delegationScenarios_test.go @@ -13,7 +13,8 @@ import ( "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/data/rewardTx" "github.com/multiversx/mx-chain-core-go/data/transaction" - "github.com/multiversx/mx-chain-crypto-go" + crypto "github.com/multiversx/mx-chain-crypto-go" + "github.com/multiversx/mx-chain-crypto-go/signing" "github.com/multiversx/mx-chain-crypto-go/signing/mcl" mclsig "github.com/multiversx/mx-chain-crypto-go/signing/mcl/singlesig" @@ -45,7 +46,8 @@ func TestDelegationSystemNodesOperationsTestBackwardComp(t *testing.T) { serviceFee := big.NewInt(1000) value := big.NewInt(1000) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + require.Nil(t, err) tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: 3, @@ -103,7 +105,8 @@ func TestDelegationSystemNodesOperations(t *testing.T) { EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + require.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -149,7 +152,9 @@ func TestDelegationSystemNodesOperations(t *testing.T) { returnedCode, _ = processTransaction(tpn, tpn.OwnAccount.Address, delegationScAddress, txData, big.NewInt(0)) assert.Equal(t, vmcommon.UserError, returnedCode) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 10000000}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 10000000}) + require.Nil(t, err) + // unBond nodes txData = txDataForFunc("unBondNodes", blsKeys[:numNodesToStake]) returnedCode, err = processTransaction(tpn, tpn.OwnAccount.Address, delegationScAddress, txData, big.NewInt(0)) @@ -188,7 +193,8 @@ func TestDelegationSystemReStakeNodes(t *testing.T) { EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -258,7 +264,8 @@ func TestDelegationChangeConfig(t *testing.T) { EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -320,7 +327,8 @@ func TestDelegationSystemDelegateUnDelegateFromTopUpWithdraw(t *testing.T) { tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -357,7 +365,8 @@ func TestDelegationSystemDelegateUnDelegateFromTopUpWithdraw(t *testing.T) { verifyDelegatorsStake(t, tpn, "getUserActiveStake", delegators[:numDelegators-2], delegationScAddress, big.NewInt(0)) verifyDelegatorsStake(t, tpn, "getUserUnStakedValue", delegators[:numDelegators-2], delegationScAddress, big.NewInt(delegationVal)) - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 1}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 1}) + assert.Nil(t, err) // withdraw unDelegated delegators should withdraw after unBond period has passed processMultipleWithdraws(t, tpn, delegators[:numDelegators-2], delegationScAddress, vmcommon.Ok) @@ -384,7 +393,8 @@ func TestDelegationSystemDelegateUnDelegateOnlyPartOfDelegation(t *testing.T) { tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -421,7 +431,8 @@ func TestDelegationSystemDelegateUnDelegateOnlyPartOfDelegation(t *testing.T) { verifyDelegatorsStake(t, tpn, "getUserActiveStake", delegators[:numDelegators-2], delegationScAddress, big.NewInt(delegationVal/2)) verifyDelegatorsStake(t, tpn, "getUserUnStakedValue", delegators[:numDelegators-2], delegationScAddress, big.NewInt(delegationVal/2)) - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 1}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 1}) + assert.Nil(t, err) // withdraw unDelegated delegators should withdraw after unBond period has passed processMultipleWithdraws(t, tpn, delegators[:numDelegators-2], delegationScAddress, vmcommon.Ok) @@ -450,7 +461,8 @@ func TestDelegationSystemMultipleDelegationContractsAndSameBlsKeysShouldNotWork( tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) ownerAddresses := getAddresses(numContracts) for i := range ownerAddresses { @@ -528,7 +540,8 @@ func TestDelegationSystemMultipleDelegationContractsAndSameDelegators(t *testing tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) ownerAddresses := getAddresses(numContracts) for i := range ownerAddresses { @@ -569,7 +582,8 @@ func TestDelegationSystemMultipleDelegationContractsAndSameDelegators(t *testing verifyDelegatorsStake(t, tpn, "getUserUnStakedValue", firstTwoDelegators, delegationScAddresses[i], big.NewInt(delegationVal)) } - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 1}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 1}) + assert.Nil(t, err) for i := range delegationScAddresses { processMultipleTransactions(t, tpn, firstTwoDelegators, delegationScAddresses[i], "withdraw", big.NewInt(0)) @@ -598,7 +612,8 @@ func TestDelegationRewardsComputationAfterChangeServiceFee(t *testing.T) { tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -628,23 +643,24 @@ func TestDelegationRewardsComputationAfterChangeServiceFee(t *testing.T) { assert.Nil(t, err) assert.Equal(t, vmcommon.Ok, returnedCode) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(1000), 1, 1) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(2000), 2, 1) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(1000), 1, 1) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(2000), 2, 1) txData = "changeServiceFee@" + hex.EncodeToString(big.NewInt(20000).Bytes()) // 20% returnedCode, err = processTransaction(tpn, tpn.OwnAccount.Address, delegationScAddress, txData, big.NewInt(0)) assert.Equal(t, vmcommon.Ok, returnedCode) assert.Nil(t, err) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(1000), 3, 1) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(2000), 4, 1) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(1000), 3, 1) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(2000), 4, 1) checkRewardData(t, tpn, delegationScAddress, 1, 1000, 5000, serviceFee) checkRewardData(t, tpn, delegationScAddress, 2, 2000, 5000, serviceFee) checkRewardData(t, tpn, delegationScAddress, 3, 1000, 5000, big.NewInt(20000)) checkRewardData(t, tpn, delegationScAddress, 4, 2000, 5000, big.NewInt(20000)) - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Nonce: 5, Epoch: 4}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Nonce: 5, Epoch: 4}) + assert.Nil(t, err) checkDelegatorReward(t, tpn, delegationScAddress, delegators[0], 1530) checkDelegatorReward(t, tpn, delegationScAddress, delegators[1], 1530) @@ -706,7 +722,8 @@ func TestDelegationUnJail(t *testing.T) { tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -774,7 +791,8 @@ func TestDelegationSystemDelegateSameUsersAFewTimes(t *testing.T) { tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) validatorAcc := getAsUserAccount(tpn, vm.ValidatorSCAddress) genesisBalance := validatorAcc.GetBalance() @@ -840,7 +858,8 @@ func TestDelegationSystemMultipleDelegationContractsAndSameDelegatorsClaimReward tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) ownerAddresses := getAddresses(numContracts) @@ -875,11 +894,11 @@ func TestDelegationSystemMultipleDelegationContractsAndSameDelegatorsClaimReward } for i := range delegationScAddresses { - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(1000), 1, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(2000), 2, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(3000), 3, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(4000), 4, 1) - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(5000), 5, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(1000), 1, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(2000), 2, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(3000), 3, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(4000), 4, 1) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(5000), 5, 1) } for i := range delegationScAddresses { @@ -932,7 +951,8 @@ func TestDelegationSystemMultipleDelegationContractsAndSameDelegatorsClaimReward verifyDelegatorsStake(t, tpn, "getUserUnStakedValue", lastTwoDelegators, delegationScAddresses[i], big.NewInt(0)) } - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 5, Nonce: 50}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 5, Nonce: 50}) + assert.Nil(t, err) for i := range delegationScAddresses { processMultipleWithdraws(t, tpn, firstTwoDelegators, delegationScAddresses[i], vmcommon.UserError) @@ -966,7 +986,7 @@ func TestDelegationSystemMultipleDelegationContractsAndSameDelegatorsClaimReward } for i := range delegationScAddresses { - addRewardsToDelegation(tpn, delegationScAddresses[i], big.NewInt(100), 6, 50) + addRewardsToDelegation(t, tpn, delegationScAddresses[i], big.NewInt(100), 6, 50) } for i := range delegationScAddresses { @@ -995,7 +1015,8 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t tpn.EpochNotifier.CheckEpoch(&testscommon.HeaderHandlerStub{ EpochField: integrationTests.UnreachableEpoch + 1, }) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(3000), tpn.OwnAccount.Address) @@ -1020,7 +1041,7 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t assert.Nil(t, err) assert.Equal(t, vmcommon.Ok, returnedCode) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(200), 1, 1) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(200), 1, 1) checkRewardData(t, tpn, delegationScAddress, 1, 200, 5000, serviceFee) checkDelegatorReward(t, tpn, delegationScAddress, delegators[0], 36) @@ -1031,7 +1052,7 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t txData = "unDelegate" + "@" + intToString(uint32(delegationVal)) processMultipleTransactions(t, tpn, delegators, delegationScAddress, txData, big.NewInt(0)) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(100), 2, 1) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(100), 2, 1) checkRewardData(t, tpn, delegationScAddress, 2, 100, 3000, serviceFee) checkDelegatorReward(t, tpn, delegationScAddress, delegators[0], 36) @@ -1043,7 +1064,8 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t verifyDelegatorsStake(t, tpn, "getUserActiveStake", [][]byte{tpn.OwnAccount.Address}, delegationScAddress, big.NewInt(3000)) verifyDelegatorsStake(t, tpn, "getUserUnStakedValue", [][]byte{tpn.OwnAccount.Address}, delegationScAddress, big.NewInt(0)) - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 2, Nonce: 50}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 2, Nonce: 50}) + assert.Nil(t, err) // withdraw unDelegated delegators processMultipleTransactions(t, tpn, delegators, delegationScAddress, "withdraw", big.NewInt(0)) @@ -1051,7 +1073,7 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t verifyDelegatorsStake(t, tpn, "getUserActiveStake", delegators, delegationScAddress, big.NewInt(0)) verifyDelegatorsStake(t, tpn, "getUserUnStakedValue", delegators, delegationScAddress, big.NewInt(0)) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(300), 3, 50) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(300), 3, 50) checkRewardData(t, tpn, delegationScAddress, 3, 300, 3000, serviceFee) txData = "claimRewards" @@ -1070,7 +1092,8 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t assert.Nil(t, err) assert.Equal(t, vmcommon.Ok, returnedCode) - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 3, Nonce: 100}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 3, Nonce: 100}) + assert.Nil(t, err) // unBond 2 nodes txData = txDataForFunc("unBondNodes", blsKeys) @@ -1078,7 +1101,7 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t assert.Nil(t, err) assert.Equal(t, vmcommon.Ok, returnedCode) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(400), 4, 100) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(400), 4, 100) checkRewardData(t, tpn, delegationScAddress, 4, 400, 3000, serviceFee) verifyDelegatorIsDeleted(t, tpn, delegators, delegationScAddress) @@ -1091,13 +1114,14 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t verifyDelegatorsStake(t, tpn, "getUserActiveStake", [][]byte{tpn.OwnAccount.Address}, delegationScAddress, big.NewInt(0)) verifyDelegatorsStake(t, tpn, "getUserUnStakedValue", [][]byte{tpn.OwnAccount.Address}, delegationScAddress, big.NewInt(3000)) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(500), 5, 100) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(500), 5, 100) checkRewardData(t, tpn, delegationScAddress, 5, 500, 0, serviceFee) verifyDelegatorIsDeleted(t, tpn, delegators, delegationScAddress) checkDelegatorReward(t, tpn, delegationScAddress, tpn.OwnAccount.Address, 928) - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 5, Nonce: 150}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: 5, Nonce: 150}) + assert.Nil(t, err) // withdraw unDelegated owner processMultipleTransactions(t, tpn, [][]byte{tpn.OwnAccount.Address}, delegationScAddress, "withdraw", big.NewInt(0)) @@ -1112,7 +1136,7 @@ func TestDelegationSystemDelegateUnDelegateReceiveRewardsWhenAllIsUndelegated(t verifyDelegatorIsDeleted(t, tpn, delegators, delegationScAddress) - addRewardsToDelegation(tpn, delegationScAddress, big.NewInt(600), 6, 150) + addRewardsToDelegation(t, tpn, delegationScAddress, big.NewInt(600), 6, 150) checkRewardData(t, tpn, delegationScAddress, 6, 600, 0, serviceFee) verifyDelegatorIsDeleted(t, tpn, delegators, delegationScAddress) @@ -1138,7 +1162,8 @@ func TestDelegationSystemCleanUpContract(t *testing.T) { }) delegationVal := int64(1000) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + err := tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 1}) + assert.Nil(t, err) // create new delegation contract delegationScAddress := deployNewSc(t, tpn, maxDelegationCap, serviceFee, big.NewInt(1000), tpn.OwnAccount.Address) @@ -1184,7 +1209,9 @@ func TestDelegationSystemCleanUpContract(t *testing.T) { returnedCode, _ = processTransaction(tpn, tpn.OwnAccount.Address, delegationScAddress, txData, big.NewInt(0)) assert.Equal(t, vmcommon.UserError, returnedCode) - tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 10000000}) + err = tpn.BlockchainHook.SetCurrentHeader(&block.MetaBlock{Nonce: 10000000}) + assert.Nil(t, err) + // unBond nodes txData = txDataForFunc("unBondNodes", blsKeys[:numNodesToStake-3]) returnedCode, err = processTransaction(tpn, tpn.OwnAccount.Address, delegationScAddress, txData, big.NewInt(0)) @@ -1290,8 +1317,9 @@ func checkRewardData( assert.Equal(t, expectedServiceFee.Bytes(), epoch0RewardData[2]) } -func addRewardsToDelegation(tpn *integrationTests.TestProcessorNode, recvAddr []byte, value *big.Int, epoch uint32, nonce uint64) { - tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: epoch, Nonce: nonce}) +func addRewardsToDelegation(t *testing.T, tpn *integrationTests.TestProcessorNode, recvAddr []byte, value *big.Int, epoch uint32, nonce uint64) { + err := tpn.BlockchainHook.SetCurrentHeader(&block.Header{Epoch: epoch, Nonce: nonce}) + assert.Nil(t, err) tx := &rewardTx.RewardTx{ Round: 0, diff --git a/integrationTests/vm/staking/testMetaProcessorWithCustomNodesConfig.go b/integrationTests/vm/staking/testMetaProcessorWithCustomNodesConfig.go index f9e9da84a8d..a52102fd966 100644 --- a/integrationTests/vm/staking/testMetaProcessorWithCustomNodesConfig.go +++ b/integrationTests/vm/staking/testMetaProcessorWithCustomNodesConfig.go @@ -94,7 +94,8 @@ type NodesRegisterData struct { // Block will be committed + call to validator system sc will be made to stake all nodes func (tmp *TestMetaProcessor) ProcessStake(t *testing.T, nodes map[string]*NodesRegisterData) { header := tmp.createNewHeader(t, tmp.currentRound) - tmp.BlockChainHook.SetCurrentHeader(header) + err := tmp.BlockChainHook.SetCurrentHeader(header) + require.Nil(t, err) txHashes := make([][]byte, 0) for owner, registerData := range nodes { @@ -128,7 +129,8 @@ func (tmp *TestMetaProcessor) doStake( // Block will be committed + call to validator system sc will be made to stake all nodes func (tmp *TestMetaProcessor) ProcessReStake(t *testing.T, blsKeys [][]byte) { header := tmp.createNewHeader(t, tmp.currentRound) - tmp.BlockChainHook.SetCurrentHeader(header) + err := tmp.BlockChainHook.SetCurrentHeader(header) + require.Nil(t, err) txHashes := make([][]byte, 0) for _, blsKey := range blsKeys { @@ -195,7 +197,8 @@ func createStakeArgs(blsKeys [][]byte) [][]byte { // Block will be committed + call to validator system sc will be made to unStake all nodes func (tmp *TestMetaProcessor) ProcessUnStake(t *testing.T, nodes map[string][][]byte) { header := tmp.createNewHeader(t, tmp.currentRound) - tmp.BlockChainHook.SetCurrentHeader(header) + err := tmp.BlockChainHook.SetCurrentHeader(header) + require.Nil(t, err) txHashes := make([][]byte, 0) for owner, blsKeys := range nodes { @@ -229,7 +232,8 @@ func (tmp *TestMetaProcessor) doUnStake( // Block will be committed + call to validator system sc will be made to jail all nodes func (tmp *TestMetaProcessor) ProcessJail(t *testing.T, blsKeys [][]byte) { header := tmp.createNewHeader(t, tmp.currentRound) - tmp.BlockChainHook.SetCurrentHeader(header) + err := tmp.BlockChainHook.SetCurrentHeader(header) + require.Nil(t, err) scrs := tmp.doJail(t, blsKeys) txHashes := tmp.addTxsToCacher(scrs) @@ -258,7 +262,8 @@ func (tmp *TestMetaProcessor) doJail( // Block will be committed + call to validator system sc will be made to unJail all nodes func (tmp *TestMetaProcessor) ProcessUnJail(t *testing.T, blsKeys [][]byte) { header := tmp.createNewHeader(t, tmp.currentRound) - tmp.BlockChainHook.SetCurrentHeader(header) + err := tmp.BlockChainHook.SetCurrentHeader(header) + require.Nil(t, err) txHashes := make([][]byte, 0) for _, blsKey := range blsKeys { diff --git a/integrationTests/vm/txsFee/scCalls_test.go b/integrationTests/vm/txsFee/scCalls_test.go index c17474eb9e3..608115d842e 100644 --- a/integrationTests/vm/txsFee/scCalls_test.go +++ b/integrationTests/vm/txsFee/scCalls_test.go @@ -461,9 +461,10 @@ func TestScCallBuyNFT_OneFailedTxAndOneOkTx(t *testing.T) { blockChainHook := testContext.BlockchainHook.(process.BlockChainHookHandler) t.Run("transaction that fails", func(t *testing.T) { utils.CleanAccumulatedIntermediateTransactions(t, testContext) - blockChainHook.SetCurrentHeader(&block.Header{ + err := blockChainHook.SetCurrentHeader(&block.Header{ TimeStamp: 1635880560, }) + require.Nil(t, err) txData, errDecode := hex.DecodeString("6275794e6674406338403435353035353465346235333264333433363632333133383336406533") require.Nil(t, errDecode) @@ -488,9 +489,10 @@ func TestScCallBuyNFT_OneFailedTxAndOneOkTx(t *testing.T) { }) t.Run("transaction that succeed", func(t *testing.T) { utils.CleanAccumulatedIntermediateTransactions(t, testContext) - blockChainHook.SetCurrentHeader(&block.Header{ + err := blockChainHook.SetCurrentHeader(&block.Header{ TimeStamp: 1635880566, // next timestamp }) + require.Nil(t, err) txData, errDecode := hex.DecodeString("6275794e6674403264403435353035353465346235333264333433363632333133383336403337") require.Nil(t, errDecode) @@ -534,9 +536,10 @@ func TestScCallBuyNFT_TwoOkTxs(t *testing.T) { blockChainHook := testContext.BlockchainHook.(process.BlockChainHookHandler) t.Run("first transaction that succeed", func(t *testing.T) { utils.CleanAccumulatedIntermediateTransactions(t, testContext) - blockChainHook.SetCurrentHeader(&block.Header{ + err := blockChainHook.SetCurrentHeader(&block.Header{ TimeStamp: 1635880566, // next timestamp }) + require.Nil(t, err) txData, errDecode := hex.DecodeString("6275794e6674403264403435353035353465346235333264333433363632333133383336403337") require.Nil(t, errDecode) @@ -561,9 +564,10 @@ func TestScCallBuyNFT_TwoOkTxs(t *testing.T) { }) t.Run("second transaction that succeed", func(t *testing.T) { utils.CleanAccumulatedIntermediateTransactions(t, testContext) - blockChainHook.SetCurrentHeader(&block.Header{ + err := blockChainHook.SetCurrentHeader(&block.Header{ TimeStamp: 1635880572, // next timestamp }) + require.Nil(t, err) txData, errDecode := hex.DecodeString("6275794e6674403434403435353035353465346235333264333433363632333133383336403531") require.Nil(t, errDecode) @@ -609,9 +613,10 @@ func TestScCallDistributeStakingRewards_ShouldWork(t *testing.T) { blockChainHook := testContext.BlockchainHook.(process.BlockChainHookHandler) utils.CleanAccumulatedIntermediateTransactions(t, testContext) - blockChainHook.SetCurrentHeader(&block.Header{ + err = blockChainHook.SetCurrentHeader(&block.Header{ TimeStamp: 1668430842, }) + require.Nil(t, err) txData, errDecode := hex.DecodeString("646973747269627574655f7374616b696e675f72657761726473") require.Nil(t, errDecode) diff --git a/integrationTests/vm/txsFee/validatorSC_test.go b/integrationTests/vm/txsFee/validatorSC_test.go index c54025a90b1..b8a479086e5 100644 --- a/integrationTests/vm/txsFee/validatorSC_test.go +++ b/integrationTests/vm/txsFee/validatorSC_test.go @@ -60,7 +60,9 @@ func TestValidatorsSC_DoStakePutInQueueUnStakeAndUnBondShouldRefund(t *testing.T defer testContextMeta.Close() stakingcommon.SaveNodesConfig(testContextMeta.Accounts, testContextMeta.Marshalizer, 1, 1, 1) - testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + err = testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + require.Nil(t, err) + saveDelegationManagerConfig(testContextMeta) gasLimit := uint64(4000) @@ -128,7 +130,8 @@ func TestValidatorsSC_DoStakePutInQueueUnStakeAndUnBondTokensShouldRefund(t *tes stakingcommon.SaveNodesConfig(testContextMeta.Accounts, testContextMeta.Marshalizer, 1, 1, 1) saveDelegationManagerConfig(testContextMeta) - testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + err = testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + require.Nil(t, err) gasLimit := uint64(4000) sndAddr := []byte("12345678901234567890123456789012") @@ -178,7 +181,8 @@ func testValidatorsSCDoStakeWithTopUpValueTryToUnStakeTokensAndUnBondTokens(t *t stakingcommon.SaveNodesConfig(testContextMeta.Accounts, testContextMeta.Marshalizer, 1, 1, 1) saveDelegationManagerConfig(testContextMeta) - testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 0}) + err = testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 0}) + require.Nil(t, err) gasLimit := uint64(4000) sndAddr := []byte("12345678901234567890123456789012") @@ -216,7 +220,8 @@ func TestValidatorsSC_ToStakePutInQueueUnStakeAndUnBondShouldRefundUnBondTokens( stakingcommon.SaveNodesConfig(testContextMeta.Accounts, testContextMeta.Marshalizer, 1, 1, 1) saveDelegationManagerConfig(testContextMeta) - testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + err = testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + require.Nil(t, err) gasLimit := uint64(4000) sndAddr := []byte("12345678901234567890123456789012") @@ -273,7 +278,8 @@ func TestValidatorsSC_ToStakePutInQueueUnStakeNodesAndUnBondNodesShouldRefund(t stakingcommon.SaveNodesConfig(testContextMeta.Accounts, testContextMeta.Marshalizer, 1, 1, 1) saveDelegationManagerConfig(testContextMeta) - testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + err = testContextMeta.BlockchainHook.(*hooks.BlockChainHookImpl).SetCurrentHeader(&block.MetaBlock{Epoch: 1}) + require.Nil(t, err) gasLimit := uint64(4000) sndAddr := []byte("12345678901234567890123456789012") diff --git a/process/block/metablock.go b/process/block/metablock.go index fb53f1207d7..b72ef7992f9 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -273,7 +273,11 @@ func (mp *metaProcessor) ProcessBlock( return err } - mp.blockChainHook.SetCurrentHeader(header) + err = mp.blockChainHook.SetCurrentHeader(header) + if err != nil { + return err + } + mp.epochStartTrigger.Update(header.GetRound(), header.GetNonce()) err = mp.checkEpochCorrectness(header) @@ -733,7 +737,10 @@ func (mp *metaProcessor) CreateBlock( metaHdr.SoftwareVersion = []byte(mp.headerIntegrityVerifier.GetVersion(metaHdr.Epoch)) mp.epochNotifier.CheckEpoch(metaHdr) - mp.blockChainHook.SetCurrentHeader(initialHdr) + err := mp.blockChainHook.SetCurrentHeader(initialHdr) + if err != nil { + return nil, nil, err + } var body data.BodyHandler @@ -742,7 +749,7 @@ func (mp *metaProcessor) CreateBlock( return nil, nil, process.ErrAccountStateDirty } - err := mp.processIfFirstBlockAfterEpochStart() + err = mp.processIfFirstBlockAfterEpochStart() if err != nil { return nil, nil, err } diff --git a/process/block/shardblock.go b/process/block/shardblock.go index a73b4e0be6d..0a750dfc126 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -233,7 +233,10 @@ func (sp *shardProcessor) ProcessBlock( return err } - sp.blockChainHook.SetCurrentHeader(header) + err = sp.blockChainHook.SetCurrentHeader(header) + if err != nil { + return err + } sp.txCoordinator.RequestBlockTransactions(body) requestedMetaHdrs, requestedFinalityAttestingMetaHdrs := sp.requestMetaHeaders(header) @@ -846,7 +849,11 @@ func (sp *shardProcessor) CreateBlock( } sp.epochNotifier.CheckEpoch(shardHdr) - sp.blockChainHook.SetCurrentHeader(shardHdr) + err = sp.blockChainHook.SetCurrentHeader(shardHdr) + if err != nil { + return nil, nil, err + } + body, processedMiniBlocksDestMeInfo, err := sp.createBlockBody(shardHdr, haveTime) if err != nil { return nil, nil, err diff --git a/process/interface.go b/process/interface.go index df38dbd7269..a731166c954 100644 --- a/process/interface.go +++ b/process/interface.go @@ -529,7 +529,7 @@ type BlockChainHookHandler interface { IsPaused(tokenID []byte) bool IsLimitedTransfer(tokenID []byte) bool NumberOfShards() uint32 - SetCurrentHeader(hdr data.HeaderHandler) + SetCurrentHeader(hdr data.HeaderHandler) error SaveCompiledCode(codeHash []byte, code []byte) GetCompiledCode(codeHash []byte) (bool, []byte) IsPayable(sndAddress []byte, recvAddress []byte) (bool, error) diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 8b5415bd803..2f03fd4de0b 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -795,19 +795,21 @@ func createSuffixMask(creatorAddress []byte) []byte { } // SetCurrentHeader sets current header to be used by smart contracts -func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) { +func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) error { if check.IfNil(hdr) { - return + return ErrNilCurrentHeader } bh.mutCurrentHdr.Lock() + defer bh.mutCurrentHdr.Unlock() + bh.currentHdr = hdr err := bh.updateEpochStartHeader(hdr) if err != nil { - log.Debug("BlockChainHookImpl.SetCurrentHeader: updateEpochStartHeader", "error", err) + return err } - bh.mutCurrentHdr.Unlock() + return nil } func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) error { @@ -833,6 +835,7 @@ func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) err } if epochStartHdr.GetEpoch() != hdr.GetEpoch() { + fmt.Printf("EpochStartHdr: %v, hdr: %v\n", epochStartHdr.GetEpoch(), hdr.GetEpoch()) return ErrLastCommitedEpochStartHdrMismatch } diff --git a/process/smartContract/hooks/blockChainHook_test.go b/process/smartContract/hooks/blockChainHook_test.go index 8d5a0ac4b5a..1889f21f1db 100644 --- a/process/smartContract/hooks/blockChainHook_test.go +++ b/process/smartContract/hooks/blockChainHook_test.go @@ -1018,6 +1018,16 @@ func TestBlockChainHookImpl_GettersFromCurrentHeader(t *testing.T) { timestamp := uint64(1234) randSeed := []byte("a") epoch := uint32(7) + + epochStartHdr := &block.Header{ + Nonce: nonce, + Round: round, + TimeStamp: timestamp, + RandSeed: randSeed, + Epoch: epoch, + EpochStartMetaHash: []byte("meta"), + } + hdr := &block.Header{ Nonce: nonce, Round: round, @@ -1029,19 +1039,19 @@ func TestBlockChainHookImpl_GettersFromCurrentHeader(t *testing.T) { args := createMockBlockChainHookArgs() bh, _ := hooks.NewBlockChainHookImpl(args) - bh.SetCurrentHeader(hdr) - assert.Equal(t, nonce, bh.CurrentNonce()) - assert.Equal(t, round, bh.CurrentRound()) - assert.Equal(t, timestamp, bh.CurrentTimeStamp()) - assert.Equal(t, epoch, bh.CurrentEpoch()) - assert.Equal(t, randSeed, bh.CurrentRandomSeed()) + err := bh.SetCurrentHeader(epochStartHdr) + require.Nil(t, err) - bh.SetCurrentHeader(nil) + err = bh.SetCurrentHeader(hdr) + require.Nil(t, err) assert.Equal(t, nonce, bh.CurrentNonce()) assert.Equal(t, round, bh.CurrentRound()) assert.Equal(t, timestamp, bh.CurrentTimeStamp()) assert.Equal(t, epoch, bh.CurrentEpoch()) assert.Equal(t, randSeed, bh.CurrentRandomSeed()) + + err = bh.SetCurrentHeader(nil) + require.Equal(t, hooks.ErrNilCurrentHeader, err) } func TestBlockChainHookImpl_SaveNFTMetaDataToSystemAccount(t *testing.T) { diff --git a/process/smartContract/hooks/errors.go b/process/smartContract/hooks/errors.go index 9fee23fbc2b..f7fbd728861 100644 --- a/process/smartContract/hooks/errors.go +++ b/process/smartContract/hooks/errors.go @@ -26,6 +26,9 @@ var ErrNilEpochStartTriggerHandler = errors.New("nil epoch start trigger handler // ErrNilRoundHandler signals that a nil round handler was provided var ErrNilRoundHandler = errors.New("nil round handler") +// ErrNilCurrentHeader signals that a nil current header was provided +var ErrNilCurrentHeader = errors.New("nil current header") + // ErrNilLastCommitedEpochStartHdr signals that a nil last commited epoch start header was provided var ErrNilLastCommitedEpochStartHdr = errors.New("nil last commited epoch start header") diff --git a/process/smartContract/scQueryService.go b/process/smartContract/scQueryService.go index b23c8c9d13d..08f97ea2581 100644 --- a/process/smartContract/scQueryService.go +++ b/process/smartContract/scQueryService.go @@ -206,7 +206,11 @@ func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice ui if err != nil { return nil, nil, err } - service.blockChainHook.SetCurrentHeader(blockHeader) + + err = service.blockChainHook.SetCurrentHeader(blockHeader) + if err != nil { + return nil, nil, err + } } shouldCheckRootHashChanges := query.SameScState diff --git a/process/transactionEvaluator/transactionSimulator.go b/process/transactionEvaluator/transactionSimulator.go index c87e79b0472..7812c67aa8e 100644 --- a/process/transactionEvaluator/transactionSimulator.go +++ b/process/transactionEvaluator/transactionSimulator.go @@ -107,7 +107,10 @@ func (ts *transactionSimulator) ProcessTx(tx *transaction.Transaction, currentHe txStatus := transaction.TxStatusPending failReason := "" - ts.blockChainHook.SetCurrentHeader(currentHeader) + err := ts.blockChainHook.SetCurrentHeader(currentHeader) + if err != nil { + return nil, err + } retCode, err := ts.txProcessor.ProcessTransaction(tx) if err != nil { diff --git a/testscommon/blockChainHookStub.go b/testscommon/blockChainHookStub.go index 644bdbbf379..bcde7bb44cb 100644 --- a/testscommon/blockChainHookStub.go +++ b/testscommon/blockChainHookStub.go @@ -44,7 +44,7 @@ type BlockChainHookStub struct { NumberOfShardsCalled func() uint32 GetSnapshotCalled func() int RevertToSnapshotCalled func(snapshot int) error - SetCurrentHeaderCalled func(hdr data.HeaderHandler) + SetCurrentHeaderCalled func(hdr data.HeaderHandler) error DeleteCompiledCodeCalled func(codeHash []byte) SaveNFTMetaDataToSystemAccountCalled func(tx data.TransactionHandler) error CloseCalled func() error @@ -322,10 +322,12 @@ func (stub *BlockChainHookStub) NumberOfShards() uint32 { } // SetCurrentHeader - -func (stub *BlockChainHookStub) SetCurrentHeader(hdr data.HeaderHandler) { +func (stub *BlockChainHookStub) SetCurrentHeader(hdr data.HeaderHandler) error { if stub.SetCurrentHeaderCalled != nil { stub.SetCurrentHeaderCalled(hdr) } + + return nil } // SaveCompiledCode - From 0534fc4f5b9ad77c5593359898bf2c45f2650159 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Tue, 30 Jul 2024 14:42:06 +0000 Subject: [PATCH 11/21] remove log; epoch start hdr check if nil --- process/smartContract/hooks/blockChainHook.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 2f03fd4de0b..152d958a807 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -830,12 +830,11 @@ func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) err return err } - if epochStartHdr == nil { + if check.IfNil(epochStartHdr) { return ErrNilLastCommitedEpochStartHdr } if epochStartHdr.GetEpoch() != hdr.GetEpoch() { - fmt.Printf("EpochStartHdr: %v, hdr: %v\n", epochStartHdr.GetEpoch(), hdr.GetEpoch()) return ErrLastCommitedEpochStartHdrMismatch } From 1f07ae273108d8a4b8ee732266254c21266b3af9 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Wed, 31 Jul 2024 14:12:10 +0000 Subject: [PATCH 12/21] simplify error handling in LastCommitedEpochStartHdr impl --- epochStart/metachain/trigger.go | 7 +------ epochStart/shardchain/trigger.go | 7 +------ process/smartContract/hooks/blockChainHook.go | 7 +------ 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/epochStart/metachain/trigger.go b/epochStart/metachain/trigger.go index a39f4f94e24..3bd429ae846 100644 --- a/epochStart/metachain/trigger.go +++ b/epochStart/metachain/trigger.go @@ -414,12 +414,7 @@ func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { return nil, err } - header, err := process.UnmarshalMetaHeader(t.marshaller, headerBytes) - if err != nil { - return nil, err - } - - return header, nil + return process.UnmarshalMetaHeader(t.marshaller, headerBytes) } // GetSavedStateKey returns the last saved trigger state key diff --git a/epochStart/shardchain/trigger.go b/epochStart/shardchain/trigger.go index 5f494b4444f..f48ab596c3c 100644 --- a/epochStart/shardchain/trigger.go +++ b/epochStart/shardchain/trigger.go @@ -1113,12 +1113,7 @@ func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) { return nil, err } - header, err := process.UnmarshalShardHeader(t.marshaller, headerBytes) - if err != nil { - return nil, err - } - - return header, nil + return process.UnmarshalShardHeader(t.marshaller, headerBytes) } // GetSavedStateKey returns the last saved trigger state key diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 152d958a807..9ba7ee2ebd8 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -804,12 +804,7 @@ func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) error { defer bh.mutCurrentHdr.Unlock() bh.currentHdr = hdr - err := bh.updateEpochStartHeader(hdr) - if err != nil { - return err - } - - return nil + return bh.updateEpochStartHeader(hdr) } func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) error { From 1877e0cf41a03c2a0f4c83d33101236694d6d5e9 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Wed, 31 Jul 2024 17:29:51 +0000 Subject: [PATCH 13/21] get epoch start block from storage for sc query --- process/interface.go | 1 + process/smartContract/hooks/blockChainHook.go | 11 ++++- process/smartContract/scQueryService.go | 49 +++++++++++++++++++ process/smartContract/scQueryService_test.go | 11 +++++ testscommon/blockChainHookStub.go | 8 +++ 5 files changed, 78 insertions(+), 2 deletions(-) diff --git a/process/interface.go b/process/interface.go index a731166c954..3abcd18ac04 100644 --- a/process/interface.go +++ b/process/interface.go @@ -530,6 +530,7 @@ type BlockChainHookHandler interface { IsLimitedTransfer(tokenID []byte) bool NumberOfShards() uint32 SetCurrentHeader(hdr data.HeaderHandler) error + SetEpochStartHeader(hdr data.HeaderHandler) SaveCompiledCode(codeHash []byte, code []byte) GetCompiledCode(codeHash []byte) (bool, []byte) IsPayable(sndAddress []byte, recvAddress []byte) (bool, error) diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 9ba7ee2ebd8..6efa3645be2 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -804,10 +804,10 @@ func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) error { defer bh.mutCurrentHdr.Unlock() bh.currentHdr = hdr - return bh.updateEpochStartHeader(hdr) + return bh.updateEpochStartHeaderFromCurrentHeader(hdr) } -func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) error { +func (bh *BlockChainHookImpl) updateEpochStartHeaderFromCurrentHeader(hdr data.HeaderHandler) error { bh.mutEpochStartHdr.Lock() defer bh.mutEpochStartHdr.Unlock() @@ -838,6 +838,13 @@ func (bh *BlockChainHookImpl) updateEpochStartHeader(hdr data.HeaderHandler) err return nil } +func (bh *BlockChainHookImpl) SetEpochStartHeader(header data.HeaderHandler) { + bh.mutEpochStartHdr.Lock() + defer bh.mutEpochStartHdr.Unlock() + + bh.epochStartHdr = header +} + // SaveCompiledCode saves the compiled code to cache and storage func (bh *BlockChainHookImpl) SaveCompiledCode(codeHash []byte, code []byte) { bh.compiledScPool.Put(codeHash, code, len(code)) diff --git a/process/smartContract/scQueryService.go b/process/smartContract/scQueryService.go index 08f97ea2581..bd38bc112e4 100644 --- a/process/smartContract/scQueryService.go +++ b/process/smartContract/scQueryService.go @@ -34,6 +34,9 @@ var logQueryService = logger.GetOrCreate("process/smartcontract.queryService") // MaxGasLimitPerQuery - each unit is the equivalent of 1 nanosecond processing time const MaxGasLimitPerQuery = 300_000_000_000 +// EpochStartHdrCacheSize - the size of the cache for the EpochStartHdr +const EpochStartHdrCacheSize = 5 + // SCQueryService can execute Get functions over SC to fetch stored values type SCQueryService struct { vmContainer process.VirtualMachinesContainer @@ -53,6 +56,7 @@ type SCQueryService struct { hasher hashing.Hasher uint64ByteSliceConverter typeConverters.Uint64ByteSliceConverter isInHistoricalBalancesMode bool + epochStartHdrCache map[uint32]data.HeaderHandler } // ArgsNewSCQueryService defines the arguments needed for the sc query service @@ -105,6 +109,7 @@ func NewSCQueryService( hasher: args.Hasher, uint64ByteSliceConverter: args.Uint64ByteSliceConverter, isInHistoricalBalancesMode: args.IsInHistoricalBalancesMode, + epochStartHdrCache: make(map[uint32]data.HeaderHandler, EpochStartHdrCacheSize), }, nil } @@ -207,6 +212,12 @@ func (service *SCQueryService) executeScCall(query *process.SCQuery, gasPrice ui return nil, nil, err } + epochStartHdr, err := service.getEpochStartBlockHdr(blockHeader.GetEpoch()) + if err != nil { + return nil, nil, err + } + service.blockChainHook.SetEpochStartHeader(epochStartHdr) + err = service.blockChainHook.SetCurrentHeader(blockHeader) if err != nil { return nil, nil, err @@ -322,6 +333,44 @@ func (service *SCQueryService) getBlockHeaderByHash(headerHash []byte) (data.Hea return header, nil } +func (service *SCQueryService) getEpochStartBlockHdr(epoch uint32) (data.HeaderHandler, error) { + if header, ok := service.epochStartHdrCache[epoch]; ok { + return header, nil + } + + storer, err := service.storageService.GetStorer(dataRetriever.BlockHeaderUnit) + if err != nil { + return nil, err + } + + identifier := core.EpochStartIdentifier(epoch) + headerBytes, err := storer.GetFromEpoch([]byte(identifier), epoch) + if err != nil { + return nil, err + } + + shardId := service.shardCoordinator.SelfId() + + header, err := process.UnmarshalHeader(shardId, service.marshaller, headerBytes) + if err != nil { + return nil, err + } + + if len(service.epochStartHdrCache) >= EpochStartHdrCacheSize { + oldestEpoch := uint32(1<<32 - 1) + for e := range service.epochStartHdrCache { + if e < oldestEpoch { + oldestEpoch = e + } + } + + delete(service.epochStartHdrCache, oldestEpoch) + } + + service.epochStartHdrCache[epoch] = header + return header, nil +} + func (service *SCQueryService) getOptionalEpochByHash(hash []byte) (core.OptionalUint32, error) { if !service.historyRepository.IsEnabled() { return core.OptionalUint32{}, nil diff --git a/process/smartContract/scQueryService_test.go b/process/smartContract/scQueryService_test.go index 4889dc87ac5..b6f182382cb 100644 --- a/process/smartContract/scQueryService_test.go +++ b/process/smartContract/scQueryService_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data" "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/data/transaction" @@ -412,6 +413,15 @@ func TestExecuteQuery_ShouldReceiveQueryCorrectly(t *testing.T) { GetStorerCalled: func(unitType dataRetriever.UnitType) (storage.Storer, error) { return &storageStubs.StorerStub{ GetFromEpochCalled: func(key []byte, epoch uint32) ([]byte, error) { + if string(key) == core.EpochStartIdentifier(epoch) { + hdr := &block.Header{ + RootHash: []byte("epoch start"), + EpochStartMetaHash: []byte("meta"), + } + buff, _ := argsNewSCQuery.Marshaller.Marshal(hdr) + return buff, nil + } + counter++ if counter > 2 { return nil, fmt.Errorf("no scheduled") @@ -469,6 +479,7 @@ func TestExecuteQuery_ShouldReceiveQueryCorrectly(t *testing.T) { } _, _, err := target.ExecuteQuery(&query) + assert.Nil(t, err) assert.True(t, runWasCalled) assert.True(t, recreateTrieFromEpochWasCalled) assert.False(t, recreateTrieWasCalled) diff --git a/testscommon/blockChainHookStub.go b/testscommon/blockChainHookStub.go index bcde7bb44cb..6808fc983ba 100644 --- a/testscommon/blockChainHookStub.go +++ b/testscommon/blockChainHookStub.go @@ -45,6 +45,7 @@ type BlockChainHookStub struct { GetSnapshotCalled func() int RevertToSnapshotCalled func(snapshot int) error SetCurrentHeaderCalled func(hdr data.HeaderHandler) error + SetEpochStartHeaderCalled func(hdr data.HeaderHandler) DeleteCompiledCodeCalled func(codeHash []byte) SaveNFTMetaDataToSystemAccountCalled func(tx data.TransactionHandler) error CloseCalled func() error @@ -330,6 +331,13 @@ func (stub *BlockChainHookStub) SetCurrentHeader(hdr data.HeaderHandler) error { return nil } +// SetEpochStartHeader - +func (stub *BlockChainHookStub) SetEpochStartHeader(hdr data.HeaderHandler) { + if stub.SetEpochStartHeaderCalled != nil { + stub.SetEpochStartHeaderCalled(hdr) + } +} + // SaveCompiledCode - func (stub *BlockChainHookStub) SaveCompiledCode(codeHash []byte, code []byte) { if stub.SaveCompiledCodeCalled != nil { From 6f64220b25592dc09ace83f94974fc0b87dfd34f Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Thu, 1 Aug 2024 07:11:28 +0000 Subject: [PATCH 14/21] add comment for SetEpochStartHeader --- process/smartContract/hooks/blockChainHook.go | 1 + 1 file changed, 1 insertion(+) diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 6efa3645be2..8ecb89bc389 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -838,6 +838,7 @@ func (bh *BlockChainHookImpl) updateEpochStartHeaderFromCurrentHeader(hdr data.H return nil } +// SetEpochStartHeader sets the epoch start header to be used by smart contracts func (bh *BlockChainHookImpl) SetEpochStartHeader(header data.HeaderHandler) { bh.mutEpochStartHdr.Lock() defer bh.mutEpochStartHdr.Unlock() From a333893227649c2284096d200391bb50b9c06a76 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 2 Aug 2024 08:22:47 +0000 Subject: [PATCH 15/21] rc/1.7.2 update dependencies --- go.mod | 20 ++++++++++---------- go.sum | 40 ++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 86b8b269068..355fb58693c 100644 --- a/go.mod +++ b/go.mod @@ -15,17 +15,17 @@ require ( github.com/klauspost/cpuid/v2 v2.2.5 github.com/mitchellh/mapstructure v1.5.0 github.com/multiversx/mx-chain-communication-go v1.0.15-0.20240508074652-e128a1c05c8e - github.com/multiversx/mx-chain-core-go v1.2.21-0.20240725065431-6e9bfee5a4c6 - github.com/multiversx/mx-chain-crypto-go v1.2.12-0.20240725071000-c3212540166f + github.com/multiversx/mx-chain-core-go v1.2.21 + github.com/multiversx/mx-chain-crypto-go v1.2.12 github.com/multiversx/mx-chain-es-indexer-go v1.7.2-0.20240716122746-98808ec1d4da - github.com/multiversx/mx-chain-logger-go v1.0.15-0.20240725065747-176bd697c775 - github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240725072925-89c927c8b6a6 - github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240725070753-aa7fb322ebdf - github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240725072715-8806f1301087 - github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240725073737-3f682a6c59db - github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240725073104-85ec99cb9260 - github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240725073322-952f3197e2e2 - github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240725073616-3b96f06509cf + github.com/multiversx/mx-chain-logger-go v1.0.15 + github.com/multiversx/mx-chain-scenario-go v1.4.5-0.20240802080531-0906745c04b2 + github.com/multiversx/mx-chain-storage-go v1.0.16 + github.com/multiversx/mx-chain-vm-common-go v1.5.14-0.20240802080126-92fc435bbbef + github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802081705-37385bd4ca7d + github.com/multiversx/mx-chain-vm-v1_2-go v1.2.69-0.20240802080749-8c7fb01e9626 + github.com/multiversx/mx-chain-vm-v1_3-go v1.3.70-0.20240802081005-dfe4847a49a6 + github.com/multiversx/mx-chain-vm-v1_4-go v1.4.99-0.20240802081230-d639ac390258 github.com/pelletier/go-toml v1.9.3 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.14.0 diff --git a/go.sum b/go.sum index 42b5345a7a0..00daff12743 100644 --- a/go.sum +++ b/go.sum @@ -387,28 +387,28 @@ github.com/multiversx/concurrent-map v0.1.4 h1:hdnbM8VE4b0KYJaGY5yJS2aNIW9TFFsUY github.com/multiversx/concurrent-map v0.1.4/go.mod h1:8cWFRJDOrWHOTNSqgYCUvwT7c7eFQ4U2vKMOp4A/9+o= github.com/multiversx/mx-chain-communication-go v1.0.15-0.20240508074652-e128a1c05c8e h1:Tsmwhu+UleE+l3buPuqXSKTqfu5FbPmzQ4MjMoUvCWA= github.com/multiversx/mx-chain-communication-go v1.0.15-0.20240508074652-e128a1c05c8e/go.mod h1:2yXl18wUbuV3cRZr7VHxM1xo73kTaC1WUcu2kx8R034= -github.com/multiversx/mx-chain-core-go v1.2.21-0.20240725065431-6e9bfee5a4c6 h1:Q7uUjTYTrt8Mw9oq5JWPv+WHhpxHTv6lhZZlhPuNcoQ= -github.com/multiversx/mx-chain-core-go v1.2.21-0.20240725065431-6e9bfee5a4c6/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE= -github.com/multiversx/mx-chain-crypto-go v1.2.12-0.20240725071000-c3212540166f h1:jydjrmVFvSllBOTppveOAkLITpOYKk0kma5z0bfDImI= -github.com/multiversx/mx-chain-crypto-go v1.2.12-0.20240725071000-c3212540166f/go.mod h1:9aSp//uBSvqFdzh4gvYISraoruhr1FCTXgPQalQ687k= +github.com/multiversx/mx-chain-core-go v1.2.21 h1:+XVKznPTlUU5EFS1A8chtS8fStW60upRIyF4Pgml19I= +github.com/multiversx/mx-chain-core-go v1.2.21/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE= +github.com/multiversx/mx-chain-crypto-go v1.2.12 h1:zWip7rpUS4CGthJxfKn5MZfMfYPjVjIiCID6uX5BSOk= +github.com/multiversx/mx-chain-crypto-go v1.2.12/go.mod h1:HzcPpCm1zanNct/6h2rIh+MFrlXbjA5C8+uMyXj3LI4= github.com/multiversx/mx-chain-es-indexer-go v1.7.2-0.20240716122746-98808ec1d4da h1:PRJLylGD/RRJg3kVc38YJDeAkDBqzXL2B1a+TLQGrYw= github.com/multiversx/mx-chain-es-indexer-go v1.7.2-0.20240716122746-98808ec1d4da/go.mod h1:rEQ0HPBp0Rg7in8TrC+vncV03yyWWTSTur2sbVGUtUw= -github.com/multiversx/mx-chain-logger-go v1.0.15-0.20240725065747-176bd697c775 h1:a8LOfz3p4MQfRtbF00rGDAJiebziwtSfVmBHIaHBDdY= -github.com/multiversx/mx-chain-logger-go v1.0.15-0.20240725065747-176bd697c775/go.mod h1:owPYyrK7RcsLx9eOCAZQ22fIyW6BE7ttJr4XIhFIbQw= -github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240725072925-89c927c8b6a6 h1:QGQjSlPix5nBtCkcdyKo0b2sRYXwYF/GBtccOqDbU6Y= -github.com/multiversx/mx-chain-scenario-go v1.4.4-0.20240725072925-89c927c8b6a6/go.mod h1:MvJiMtuyGq43aS9eOgF+xQUWk0hYxvCQqLrT77bhBaE= -github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240725070753-aa7fb322ebdf h1:L9K7Xzq5SZz6k55R7HrafiRcU+c8/PqozJxys65G4bI= -github.com/multiversx/mx-chain-storage-go v1.0.16-0.20240725070753-aa7fb322ebdf/go.mod h1:ptvW/8r6bam55mVpeVZbyvvvydYM0DQwcPOH0W4Xyx8= -github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240725072715-8806f1301087 h1:ovxs8X50iBL9TOkn0qHrkuXrBS1Y/EWfQOYmFEaXRNs= -github.com/multiversx/mx-chain-vm-common-go v1.5.13-0.20240725072715-8806f1301087/go.mod h1:nNGN+rdLRN8Nd6OhFGrkEZS5Ipj5IQCvFT0L/iQbOpU= -github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240725073737-3f682a6c59db h1:ZSvHaMsoL0hNfaVBsBZskUdMEaKu+Fdrx3KZrSBbkio= -github.com/multiversx/mx-chain-vm-go v1.5.30-0.20240725073737-3f682a6c59db/go.mod h1:CFOSVrsHOzaO5YX2L/wyjP76L+BE/9rh+SereQV3pHA= -github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240725073104-85ec99cb9260 h1:Ny3s7dw2oF6AVq4kZYmhNYWvAuLEbd48JPPIC6tFzOA= -github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68-0.20240725073104-85ec99cb9260/go.mod h1:NFRX6UrkBMb28HFKZyKwH894uxfrZyfuFqMF1KBVqFw= -github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240725073322-952f3197e2e2 h1:TM45+UXZV5DYOHlbGiHyQm44hOlBid8g9qfvYqopILs= -github.com/multiversx/mx-chain-vm-v1_3-go v1.3.69-0.20240725073322-952f3197e2e2/go.mod h1:Ntfq9tUV3I5k6SS/OpW4HSO6AlZbs/xxgB2poOuc2pg= -github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240725073616-3b96f06509cf h1:axwaSswcaw8pituLVAu4IWlGNtYwXvUMYy+MGPwmxuY= -github.com/multiversx/mx-chain-vm-v1_4-go v1.4.98-0.20240725073616-3b96f06509cf/go.mod h1:2TjMTiVFkh5wFImEEFZl+k5MU8bh2287btJuVCR3sL0= +github.com/multiversx/mx-chain-logger-go v1.0.15 h1:HlNdK8etyJyL9NQ+6mIXyKPEBo+wRqOwi3n+m2QIHXc= +github.com/multiversx/mx-chain-logger-go v1.0.15/go.mod h1:t3PRKaWB1M+i6gUfD27KXgzLJJC+mAQiN+FLlL1yoGQ= +github.com/multiversx/mx-chain-scenario-go v1.4.5-0.20240802080531-0906745c04b2 h1:7/4jvB5DBQqKL4udY4RjcwHZ7sRH20fXM3Nj830Rv84= +github.com/multiversx/mx-chain-scenario-go v1.4.5-0.20240802080531-0906745c04b2/go.mod h1:5lkUsHiCCX1rJOx0d763OIlD85y85xCYU0Zlzasq6OM= +github.com/multiversx/mx-chain-storage-go v1.0.16 h1:l2lJq+EAN3YwLbjJrnoKfFd1/1Xmo9DcAUECND2obLs= +github.com/multiversx/mx-chain-storage-go v1.0.16/go.mod h1:uM/z7YyqTOD3wgyH8TfapyEl5sb+7x/Jaxne4cfG4HI= +github.com/multiversx/mx-chain-vm-common-go v1.5.14-0.20240802080126-92fc435bbbef h1:6fxfLOgKwZ2Rn5MIl9TMaeYmyPigC2XQRCASC4P0PHQ= +github.com/multiversx/mx-chain-vm-common-go v1.5.14-0.20240802080126-92fc435bbbef/go.mod h1:OSvFbzdWThfRbLZbUsEr7bikBSaLrPJQ2iUm9jw9nXQ= +github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802081705-37385bd4ca7d h1:frcayZNEfv6xWZbsgpVw2v/Dc5mFHkXEadL+S8YRcUA= +github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802081705-37385bd4ca7d/go.mod h1:vvZp8fyUC51ejSrJjok8UhK+9hISa+16cOB7Zkl6150= +github.com/multiversx/mx-chain-vm-v1_2-go v1.2.69-0.20240802080749-8c7fb01e9626 h1:GB9p93A8Ne4VF9DTZvfqcUrdD3wb18jztuR1LmncglQ= +github.com/multiversx/mx-chain-vm-v1_2-go v1.2.69-0.20240802080749-8c7fb01e9626/go.mod h1:GVHTbVnB+HL9cjR1hgtteNg/rQCNhgJFwbglu/ZawhE= +github.com/multiversx/mx-chain-vm-v1_3-go v1.3.70-0.20240802081005-dfe4847a49a6 h1:5MWUZrTXcjUC4Vr9pVbyE7ouPwdgfFRrE1nCOWEyhlI= +github.com/multiversx/mx-chain-vm-v1_3-go v1.3.70-0.20240802081005-dfe4847a49a6/go.mod h1:99pSZuN4eaMgkWbEoGTgHjj+915MjOL3ST6xBHciiZA= +github.com/multiversx/mx-chain-vm-v1_4-go v1.4.99-0.20240802081230-d639ac390258 h1:M5KVjwm45RV+pQ1wniRMFpSFIDOagUxrr30a5HQJmRs= +github.com/multiversx/mx-chain-vm-v1_4-go v1.4.99-0.20240802081230-d639ac390258/go.mod h1:wF/NMaV1oBV7eJy2DXZzjY8agppaZ/3oswdCqC9U38o= github.com/multiversx/mx-components-big-int v1.0.0 h1:Wkr8lSzK2nDqixOrrBa47VNuqdhV1m/aJhaP1EMaiS8= github.com/multiversx/mx-components-big-int v1.0.0/go.mod h1:maIEMgHlNE2u78JaDD0oLzri+ShgU4okHfzP3LWGdQM= github.com/multiversx/protobuf v1.3.2 h1:RaNkxvGTGbA0lMcnHAN24qE1G1i+Xs5yHA6MDvQ4mSM= From fc46b8bd69a7c27fc98c9feaba2cfcf253b0f275 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 2 Aug 2024 08:43:19 +0000 Subject: [PATCH 16/21] managed buffer small ints gas cost --- cmd/node/config/gasSchedules/gasScheduleV1.toml | 4 ++++ cmd/node/config/gasSchedules/gasScheduleV2.toml | 4 ++++ cmd/node/config/gasSchedules/gasScheduleV3.toml | 4 ++++ cmd/node/config/gasSchedules/gasScheduleV4.toml | 4 ++++ cmd/node/config/gasSchedules/gasScheduleV5.toml | 4 ++++ cmd/node/config/gasSchedules/gasScheduleV6.toml | 4 ++++ cmd/node/config/gasSchedules/gasScheduleV7.toml | 4 ++++ cmd/node/config/gasSchedules/gasScheduleV8.toml | 4 ++++ 8 files changed, 32 insertions(+) diff --git a/cmd/node/config/gasSchedules/gasScheduleV1.toml b/cmd/node/config/gasSchedules/gasScheduleV1.toml index 9d64a932e9b..3ac69284df4 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV1.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV1.toml @@ -242,6 +242,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV2.toml b/cmd/node/config/gasSchedules/gasScheduleV2.toml index 703ce7470c0..0226da710f1 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV2.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV2.toml @@ -241,6 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV3.toml b/cmd/node/config/gasSchedules/gasScheduleV3.toml index 175cf5b7d17..2612a180a86 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV3.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV3.toml @@ -241,6 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV4.toml b/cmd/node/config/gasSchedules/gasScheduleV4.toml index 3908d47b544..4362f032fa8 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV4.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV4.toml @@ -241,6 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV5.toml b/cmd/node/config/gasSchedules/gasScheduleV5.toml index cb4a4db0ec2..8726dc62dcc 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV5.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV5.toml @@ -241,6 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV6.toml b/cmd/node/config/gasSchedules/gasScheduleV6.toml index d0e9d9814f1..d3e2be8cc4b 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV6.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV6.toml @@ -241,6 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV7.toml b/cmd/node/config/gasSchedules/gasScheduleV7.toml index 37581a22668..d8fb218b105 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV7.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV7.toml @@ -242,6 +242,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV8.toml b/cmd/node/config/gasSchedules/gasScheduleV8.toml index b28e469aa48..475f7335b1e 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV8.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV8.toml @@ -242,6 +242,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 From 5d877d811b843f55a4d7260811d8f29f351c325a Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 2 Aug 2024 08:44:22 +0000 Subject: [PATCH 17/21] formatting fixes --- cmd/node/config/gasSchedules/gasScheduleV1.toml | 8 ++++---- cmd/node/config/gasSchedules/gasScheduleV2.toml | 8 ++++---- cmd/node/config/gasSchedules/gasScheduleV3.toml | 8 ++++---- cmd/node/config/gasSchedules/gasScheduleV4.toml | 8 ++++---- cmd/node/config/gasSchedules/gasScheduleV5.toml | 8 ++++---- cmd/node/config/gasSchedules/gasScheduleV7.toml | 8 ++++---- cmd/node/config/gasSchedules/gasScheduleV8.toml | 8 ++++---- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cmd/node/config/gasSchedules/gasScheduleV1.toml b/cmd/node/config/gasSchedules/gasScheduleV1.toml index 3ac69284df4..90e2385431c 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV1.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV1.toml @@ -242,10 +242,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 - MBufferToSmallIntUnsigned = 10000 - MBufferToSmallIntSigned = 10000 - MBufferFromSmallIntUnsigned = 10000 - MBufferFromSmallIntSigned = 10000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV2.toml b/cmd/node/config/gasSchedules/gasScheduleV2.toml index 0226da710f1..8e6ed529ff5 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV2.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV2.toml @@ -241,10 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 - MBufferToSmallIntUnsigned = 10000 - MBufferToSmallIntSigned = 10000 - MBufferFromSmallIntUnsigned = 10000 - MBufferFromSmallIntSigned = 10000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV3.toml b/cmd/node/config/gasSchedules/gasScheduleV3.toml index 2612a180a86..3d56e90ec9c 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV3.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV3.toml @@ -241,10 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 - MBufferToSmallIntUnsigned = 10000 - MBufferToSmallIntSigned = 10000 - MBufferFromSmallIntUnsigned = 10000 - MBufferFromSmallIntSigned = 10000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV4.toml b/cmd/node/config/gasSchedules/gasScheduleV4.toml index 4362f032fa8..7aa80296c7e 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV4.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV4.toml @@ -241,10 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 - MBufferToSmallIntUnsigned = 10000 - MBufferToSmallIntSigned = 10000 - MBufferFromSmallIntUnsigned = 10000 - MBufferFromSmallIntSigned = 10000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV5.toml b/cmd/node/config/gasSchedules/gasScheduleV5.toml index 8726dc62dcc..5b618359e8e 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV5.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV5.toml @@ -241,10 +241,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 - MBufferToSmallIntUnsigned = 10000 - MBufferToSmallIntSigned = 10000 - MBufferFromSmallIntUnsigned = 10000 - MBufferFromSmallIntSigned = 10000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV7.toml b/cmd/node/config/gasSchedules/gasScheduleV7.toml index d8fb218b105..7c2bbedd2c0 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV7.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV7.toml @@ -242,10 +242,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 - MBufferToSmallIntUnsigned = 10000 - MBufferToSmallIntSigned = 10000 - MBufferFromSmallIntUnsigned = 10000 - MBufferFromSmallIntSigned = 10000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 diff --git a/cmd/node/config/gasSchedules/gasScheduleV8.toml b/cmd/node/config/gasSchedules/gasScheduleV8.toml index 475f7335b1e..95962e4e80e 100644 --- a/cmd/node/config/gasSchedules/gasScheduleV8.toml +++ b/cmd/node/config/gasSchedules/gasScheduleV8.toml @@ -242,10 +242,10 @@ MBufferSetRandom = 6000 MBufferToBigFloat = 2000 MBufferFromBigFloat = 2000 - MBufferToSmallIntUnsigned = 10000 - MBufferToSmallIntSigned = 10000 - MBufferFromSmallIntUnsigned = 10000 - MBufferFromSmallIntSigned = 10000 + MBufferToSmallIntUnsigned = 10000 + MBufferToSmallIntSigned = 10000 + MBufferFromSmallIntUnsigned = 10000 + MBufferFromSmallIntSigned = 10000 [BigFloatAPICost] BigFloatNewFromParts = 3000 From 4ac3ab52fa7a113a36bd274a381b04646a17a015 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Fri, 2 Aug 2024 11:33:50 +0000 Subject: [PATCH 18/21] update vm version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 355fb58693c..27be613ef8b 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/multiversx/mx-chain-scenario-go v1.4.5-0.20240802080531-0906745c04b2 github.com/multiversx/mx-chain-storage-go v1.0.16 github.com/multiversx/mx-chain-vm-common-go v1.5.14-0.20240802080126-92fc435bbbef - github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802081705-37385bd4ca7d + github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802112736-2b9f5cbac845 github.com/multiversx/mx-chain-vm-v1_2-go v1.2.69-0.20240802080749-8c7fb01e9626 github.com/multiversx/mx-chain-vm-v1_3-go v1.3.70-0.20240802081005-dfe4847a49a6 github.com/multiversx/mx-chain-vm-v1_4-go v1.4.99-0.20240802081230-d639ac390258 diff --git a/go.sum b/go.sum index 00daff12743..7d4e58d78b7 100644 --- a/go.sum +++ b/go.sum @@ -401,8 +401,8 @@ github.com/multiversx/mx-chain-storage-go v1.0.16 h1:l2lJq+EAN3YwLbjJrnoKfFd1/1X github.com/multiversx/mx-chain-storage-go v1.0.16/go.mod h1:uM/z7YyqTOD3wgyH8TfapyEl5sb+7x/Jaxne4cfG4HI= github.com/multiversx/mx-chain-vm-common-go v1.5.14-0.20240802080126-92fc435bbbef h1:6fxfLOgKwZ2Rn5MIl9TMaeYmyPigC2XQRCASC4P0PHQ= github.com/multiversx/mx-chain-vm-common-go v1.5.14-0.20240802080126-92fc435bbbef/go.mod h1:OSvFbzdWThfRbLZbUsEr7bikBSaLrPJQ2iUm9jw9nXQ= -github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802081705-37385bd4ca7d h1:frcayZNEfv6xWZbsgpVw2v/Dc5mFHkXEadL+S8YRcUA= -github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802081705-37385bd4ca7d/go.mod h1:vvZp8fyUC51ejSrJjok8UhK+9hISa+16cOB7Zkl6150= +github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802112736-2b9f5cbac845 h1:4BaV2RsJwW6HjG9PM/Q9Zv02JkvltpEb04xIOPlTaio= +github.com/multiversx/mx-chain-vm-go v1.5.31-0.20240802112736-2b9f5cbac845/go.mod h1:py/I+k7DGaXjFAWiytDpcTTSJFwhXoWBt+SBPeTwNPI= github.com/multiversx/mx-chain-vm-v1_2-go v1.2.69-0.20240802080749-8c7fb01e9626 h1:GB9p93A8Ne4VF9DTZvfqcUrdD3wb18jztuR1LmncglQ= github.com/multiversx/mx-chain-vm-v1_2-go v1.2.69-0.20240802080749-8c7fb01e9626/go.mod h1:GVHTbVnB+HL9cjR1hgtteNg/rQCNhgJFwbglu/ZawhE= github.com/multiversx/mx-chain-vm-v1_3-go v1.3.70-0.20240802081005-dfe4847a49a6 h1:5MWUZrTXcjUC4Vr9pVbyE7ouPwdgfFRrE1nCOWEyhlI= From a6a1af7f30d09f6f1f2787393b60e6bae6b0b001 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Wed, 7 Aug 2024 07:19:30 +0000 Subject: [PATCH 19/21] chainsimulator tests fixes --- factory/consensus/consensusComponents_test.go | 3 ++- factory/interface.go | 1 + factory/mock/processComponentsStub.go | 6 +++++ .../processing/processComponentsHandler.go | 8 +++++++ .../mock/processComponentsStub.go | 6 +++++ node/chainSimulator/chainSimulator.go | 22 +++++++++++++++++++ .../components/processComponents.go | 7 ++++++ node/node_test.go | 1 + process/block/metablock.go | 11 +++++----- process/block/shardblock.go | 7 +++--- process/smartContract/scQueryService.go | 8 +++++-- testscommon/components/default.go | 1 + 12 files changed, 70 insertions(+), 11 deletions(-) diff --git a/factory/consensus/consensusComponents_test.go b/factory/consensus/consensusComponents_test.go index a7b00e6a347..c907bc84951 100644 --- a/factory/consensus/consensusComponents_test.go +++ b/factory/consensus/consensusComponents_test.go @@ -8,7 +8,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data" - "github.com/multiversx/mx-chain-crypto-go" + crypto "github.com/multiversx/mx-chain-crypto-go" "github.com/multiversx/mx-chain-go/common" "github.com/multiversx/mx-chain-go/consensus" retriever "github.com/multiversx/mx-chain-go/dataRetriever" @@ -141,6 +141,7 @@ func createMockConsensusComponentsFactoryArgs() consensusComp.ConsensusComponent HeaderIntegrVerif: &mock.HeaderIntegrityVerifierStub{}, FallbackHdrValidator: &testscommon.FallBackHeaderValidatorStub{}, SentSignaturesTrackerInternal: &testscommon.SentSignatureTrackerStub{}, + BlockchainHookField: &testscommon.BlockChainHookStub{}, }, StateComponents: &factoryMocks.StateComponentsMock{ StorageManagers: map[string]common.StorageManager{ diff --git a/factory/interface.go b/factory/interface.go index ec92f478992..5dd1c7c07e1 100644 --- a/factory/interface.go +++ b/factory/interface.go @@ -312,6 +312,7 @@ type ProcessComponentsHolder interface { SentSignaturesTracker() process.SentSignaturesTracker EpochSystemSCProcessor() process.EpochStartSystemSCProcessor RelayedTxV3Processor() process.RelayedTxV3Processor + BlockchainHook() process.BlockChainHookWithAccountsAdapter IsInterfaceNil() bool } diff --git a/factory/mock/processComponentsStub.go b/factory/mock/processComponentsStub.go index 4a52413b7cd..733a22c07cb 100644 --- a/factory/mock/processComponentsStub.go +++ b/factory/mock/processComponentsStub.go @@ -59,6 +59,7 @@ type ProcessComponentsMock struct { SentSignaturesTrackerInternal process.SentSignaturesTracker EpochSystemSCProcessorInternal process.EpochStartSystemSCProcessor RelayedTxV3ProcessorField process.RelayedTxV3Processor + BlockchainHookField process.BlockChainHookWithAccountsAdapter } // Create - @@ -296,6 +297,11 @@ func (pcm *ProcessComponentsMock) RelayedTxV3Processor() process.RelayedTxV3Proc return pcm.RelayedTxV3ProcessorField } +// BlockchainHook - +func (pcm *ProcessComponentsMock) BlockchainHook() process.BlockChainHookWithAccountsAdapter { + return pcm.BlockchainHookField +} + // IsInterfaceNil - func (pcm *ProcessComponentsMock) IsInterfaceNil() bool { return pcm == nil diff --git a/factory/processing/processComponentsHandler.go b/factory/processing/processComponentsHandler.go index 94f21a5b8f2..bd7b9d3196c 100644 --- a/factory/processing/processComponentsHandler.go +++ b/factory/processing/processComponentsHandler.go @@ -73,6 +73,14 @@ func (m *managedProcessComponents) Close() error { return nil } +// BlockChainHookImpl returns the block chain hook +func (m *managedProcessComponents) BlockchainHook() process.BlockChainHookWithAccountsAdapter { + m.mutProcessComponents.Lock() + defer m.mutProcessComponents.Unlock() + + return m.processComponents.vmFactoryForProcessing.BlockChainHookImpl() +} + // CheckSubcomponents verifies all subcomponents func (m *managedProcessComponents) CheckSubcomponents() error { m.mutProcessComponents.RLock() diff --git a/integrationTests/mock/processComponentsStub.go b/integrationTests/mock/processComponentsStub.go index 04dad00a52c..db385df074d 100644 --- a/integrationTests/mock/processComponentsStub.go +++ b/integrationTests/mock/processComponentsStub.go @@ -62,6 +62,7 @@ type ProcessComponentsStub struct { SentSignaturesTrackerInternal process.SentSignaturesTracker EpochSystemSCProcessorInternal process.EpochStartSystemSCProcessor RelayedTxV3ProcessorField process.RelayedTxV3Processor + BlockchainHookField process.BlockChainHookWithAccountsAdapter } // Create - @@ -308,6 +309,11 @@ func (pcs *ProcessComponentsStub) RelayedTxV3Processor() process.RelayedTxV3Proc return pcs.RelayedTxV3ProcessorField } +// BlockchainHook - +func (pcs *ProcessComponentsStub) BlockchainHook() process.BlockChainHookWithAccountsAdapter { + return pcs.BlockchainHookField +} + // IsInterfaceNil - func (pcs *ProcessComponentsStub) IsInterfaceNil() bool { return pcs == nil diff --git a/node/chainSimulator/chainSimulator.go b/node/chainSimulator/chainSimulator.go index 742d040c8c8..ff3309655c3 100644 --- a/node/chainSimulator/chainSimulator.go +++ b/node/chainSimulator/chainSimulator.go @@ -22,6 +22,7 @@ import ( "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-core-go/core/sharding" "github.com/multiversx/mx-chain-core-go/data/api" + "github.com/multiversx/mx-chain-core-go/data/block" "github.com/multiversx/mx-chain-core-go/data/endProcess" "github.com/multiversx/mx-chain-core-go/data/transaction" crypto "github.com/multiversx/mx-chain-crypto-go" @@ -173,7 +174,28 @@ func (s *simulator) createChainHandlers(args ArgsBaseChainSimulator) error { if err != nil { return err } + + epochStartBlockHeader := &block.MetaBlock{ + Nonce: args.InitialNonce, + Epoch: args.InitialEpoch, + Round: uint64(args.InitialRound), + TimeStamp: uint64(node.GetCoreComponents().RoundHandler().TimeStamp().Unix()), + } + node.GetProcessComponents().BlockchainHook().SetEpochStartHeader(epochStartBlockHeader) + } else { + epochStartBlockHeader := &block.HeaderV2{ + Header: &block.Header{ + Nonce: args.InitialNonce, + Epoch: args.InitialEpoch, + Round: uint64(args.InitialRound), + TimeStamp: uint64(node.GetCoreComponents().RoundHandler().TimeStamp().Unix()), + }, + } + node.GetProcessComponents().BlockchainHook().SetEpochStartHeader(epochStartBlockHeader) } + + // TODO: + } s.initialWalletKeys = outputConfigs.InitialWallets diff --git a/node/chainSimulator/components/processComponents.go b/node/chainSimulator/components/processComponents.go index 0fab8e922c9..0cb567c3472 100644 --- a/node/chainSimulator/components/processComponents.go +++ b/node/chainSimulator/components/processComponents.go @@ -102,6 +102,7 @@ type processComponentsHolder struct { sentSignatureTracker process.SentSignaturesTracker epochStartSystemSCProcessor process.EpochStartSystemSCProcessor relayedTxV3Processor process.RelayedTxV3Processor + blockchainHook process.BlockChainHookWithAccountsAdapter managedProcessComponentsCloser io.Closer } @@ -273,6 +274,7 @@ func CreateProcessComponents(args ArgsProcessComponentsHolder) (*processComponen sentSignatureTracker: managedProcessComponents.SentSignaturesTracker(), epochStartSystemSCProcessor: managedProcessComponents.EpochSystemSCProcessor(), relayedTxV3Processor: managedProcessComponents.RelayedTxV3Processor(), + blockchainHook: managedProcessComponents.BlockchainHook(), managedProcessComponentsCloser: managedProcessComponents, } @@ -518,6 +520,11 @@ func (p *processComponentsHolder) RelayedTxV3Processor() process.RelayedTxV3Proc return p.relayedTxV3Processor } +// BlockchainHook returns the blockchain hook +func (p *processComponentsHolder) BlockchainHook() process.BlockChainHookWithAccountsAdapter { + return p.blockchainHook +} + // Close will call the Close methods on all inner components func (p *processComponentsHolder) Close() error { return p.managedProcessComponentsCloser.Close() diff --git a/node/node_test.go b/node/node_test.go index b584dc2370b..9607ff5ff62 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -5298,6 +5298,7 @@ func getDefaultProcessComponents() *factoryMock.ProcessComponentsMock { ScheduledTxsExecutionHandlerInternal: &testscommon.ScheduledTxsExecutionStub{}, HistoryRepositoryInternal: &dblookupext.HistoryRepositoryStub{}, RelayedTxV3ProcessorField: &processMocks.RelayedTxV3ProcessorMock{}, + BlockchainHookField: &testscommon.BlockChainHookStub{}, } } diff --git a/process/block/metablock.go b/process/block/metablock.go index b72ef7992f9..d783481b4f2 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -737,10 +737,6 @@ func (mp *metaProcessor) CreateBlock( metaHdr.SoftwareVersion = []byte(mp.headerIntegrityVerifier.GetVersion(metaHdr.Epoch)) mp.epochNotifier.CheckEpoch(metaHdr) - err := mp.blockChainHook.SetCurrentHeader(initialHdr) - if err != nil { - return nil, nil, err - } var body data.BodyHandler @@ -749,7 +745,7 @@ func (mp *metaProcessor) CreateBlock( return nil, nil, process.ErrAccountStateDirty } - err = mp.processIfFirstBlockAfterEpochStart() + err := mp.processIfFirstBlockAfterEpochStart() if err != nil { return nil, nil, err } @@ -778,6 +774,11 @@ func (mp *metaProcessor) CreateBlock( mp.requestHandler.SetEpoch(metaHdr.GetEpoch()) + err = mp.blockChainHook.SetCurrentHeader(metaHdr) + if err != nil { + return nil, nil, err + } + return metaHdr, body, nil } diff --git a/process/block/shardblock.go b/process/block/shardblock.go index 0a750dfc126..7b1e71b1267 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -849,17 +849,18 @@ func (sp *shardProcessor) CreateBlock( } sp.epochNotifier.CheckEpoch(shardHdr) - err = sp.blockChainHook.SetCurrentHeader(shardHdr) + + body, processedMiniBlocksDestMeInfo, err := sp.createBlockBody(shardHdr, haveTime) if err != nil { return nil, nil, err } - body, processedMiniBlocksDestMeInfo, err := sp.createBlockBody(shardHdr, haveTime) + finalBody, err := sp.applyBodyToHeader(shardHdr, body, processedMiniBlocksDestMeInfo) if err != nil { return nil, nil, err } - finalBody, err := sp.applyBodyToHeader(shardHdr, body, processedMiniBlocksDestMeInfo) + err = sp.blockChainHook.SetCurrentHeader(shardHdr) if err != nil { return nil, nil, err } diff --git a/process/smartContract/scQueryService.go b/process/smartContract/scQueryService.go index bd38bc112e4..16c89791658 100644 --- a/process/smartContract/scQueryService.go +++ b/process/smartContract/scQueryService.go @@ -334,6 +334,12 @@ func (service *SCQueryService) getBlockHeaderByHash(headerHash []byte) (data.Hea } func (service *SCQueryService) getEpochStartBlockHdr(epoch uint32) (data.HeaderHandler, error) { + shardId := service.shardCoordinator.SelfId() + + if epoch == 0 { + return service.mainBlockChain.GetGenesisHeader(), nil + } + if header, ok := service.epochStartHdrCache[epoch]; ok { return header, nil } @@ -349,8 +355,6 @@ func (service *SCQueryService) getEpochStartBlockHdr(epoch uint32) (data.HeaderH return nil, err } - shardId := service.shardCoordinator.SelfId() - header, err := process.UnmarshalHeader(shardId, service.marshaller, headerBytes) if err != nil { return nil, err diff --git a/testscommon/components/default.go b/testscommon/components/default.go index 8e1942037dd..ee5f398e6f6 100644 --- a/testscommon/components/default.go +++ b/testscommon/components/default.go @@ -159,5 +159,6 @@ func GetDefaultProcessComponents(shardCoordinator sharding.Coordinator) *mock.Pr }, HardforkTriggerField: &testscommon.HardforkTriggerStub{}, RelayedTxV3ProcessorField: &processMocks.RelayedTxV3ProcessorMock{}, + BlockchainHookField: &testscommon.BlockChainHookStub{}, } } From e77049107ed83d8a932613d5ec0ab85c62998388 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Wed, 7 Aug 2024 14:12:05 +0000 Subject: [PATCH 20/21] more chain simulator tests fixing --- node/chainSimulator/chainSimulator.go | 3 --- process/block/metablock.go | 15 ++++++++++----- process/block/shardblock.go | 6 +++--- testscommon/chainHandlerStub.go | 3 ++- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/node/chainSimulator/chainSimulator.go b/node/chainSimulator/chainSimulator.go index ff3309655c3..f7e004168f4 100644 --- a/node/chainSimulator/chainSimulator.go +++ b/node/chainSimulator/chainSimulator.go @@ -193,9 +193,6 @@ func (s *simulator) createChainHandlers(args ArgsBaseChainSimulator) error { } node.GetProcessComponents().BlockchainHook().SetEpochStartHeader(epochStartBlockHeader) } - - // TODO: - } s.initialWalletKeys = outputConfigs.InitialWallets diff --git a/process/block/metablock.go b/process/block/metablock.go index d783481b4f2..b38f3046c72 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -756,11 +756,21 @@ func (mp *metaProcessor) CreateBlock( return nil, nil, err } + err = mp.blockChainHook.SetCurrentHeader(metaHdr) + if err != nil { + return nil, nil, err + } + body, err = mp.createEpochStartBody(metaHdr) if err != nil { return nil, nil, err } } else { + err = mp.blockChainHook.SetCurrentHeader(metaHdr) + if err != nil { + return nil, nil, err + } + body, err = mp.createBlockBody(metaHdr, haveTime) if err != nil { return nil, nil, err @@ -774,11 +784,6 @@ func (mp *metaProcessor) CreateBlock( mp.requestHandler.SetEpoch(metaHdr.GetEpoch()) - err = mp.blockChainHook.SetCurrentHeader(metaHdr) - if err != nil { - return nil, nil, err - } - return metaHdr, body, nil } diff --git a/process/block/shardblock.go b/process/block/shardblock.go index 7b1e71b1267..19cc381a280 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -850,17 +850,17 @@ func (sp *shardProcessor) CreateBlock( sp.epochNotifier.CheckEpoch(shardHdr) - body, processedMiniBlocksDestMeInfo, err := sp.createBlockBody(shardHdr, haveTime) + err = sp.blockChainHook.SetCurrentHeader(shardHdr) if err != nil { return nil, nil, err } - finalBody, err := sp.applyBodyToHeader(shardHdr, body, processedMiniBlocksDestMeInfo) + body, processedMiniBlocksDestMeInfo, err := sp.createBlockBody(shardHdr, haveTime) if err != nil { return nil, nil, err } - err = sp.blockChainHook.SetCurrentHeader(shardHdr) + finalBody, err := sp.applyBodyToHeader(shardHdr, body, processedMiniBlocksDestMeInfo) if err != nil { return nil, nil, err } diff --git a/testscommon/chainHandlerStub.go b/testscommon/chainHandlerStub.go index 9ec80363abf..1c27ea15808 100644 --- a/testscommon/chainHandlerStub.go +++ b/testscommon/chainHandlerStub.go @@ -24,7 +24,8 @@ func (stub *ChainHandlerStub) GetGenesisHeader() data.HeaderHandler { if stub.GetGenesisHeaderCalled != nil { return stub.GetGenesisHeaderCalled() } - return nil + + return &HeaderHandlerStub{} } // SetGenesisHeader - From 2a402b5f7f3ac30fbae965b534515c6e25f41e29 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Wed, 14 Aug 2024 14:15:54 +0000 Subject: [PATCH 21/21] add warn logs for error cases in updateEpochStartHeaderFromCurrentHeader --- process/smartContract/hooks/blockChainHook.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/process/smartContract/hooks/blockChainHook.go b/process/smartContract/hooks/blockChainHook.go index 8ecb89bc389..4c9a8f338cf 100644 --- a/process/smartContract/hooks/blockChainHook.go +++ b/process/smartContract/hooks/blockChainHook.go @@ -822,14 +822,17 @@ func (bh *BlockChainHookImpl) updateEpochStartHeaderFromCurrentHeader(hdr data.H epochStartHdr, err := bh.epochStartTrigger.LastCommitedEpochStartHdr() if err != nil { + log.Warn("BlockChainHookImpl.updateEpochStartHeaderFromCurrentHeader: epochStartTrigger.LastCommitedEpochStartHdr", "error", err) return err } if check.IfNil(epochStartHdr) { + log.Warn("BlockChainHookImpl.updateEpochStartHeaderFromCurrentHeader: epochStartHdr is nil") return ErrNilLastCommitedEpochStartHdr } if epochStartHdr.GetEpoch() != hdr.GetEpoch() { + log.Warn("BlockChainHookImpl.updateEpochStartHeaderFromCurrentHeader: epochStartHdr.GetEpoch() != hdr.GetEpoch()", "epochStartHdr", epochStartHdr.GetEpoch(), "hdr", hdr.GetEpoch()) return ErrLastCommitedEpochStartHdrMismatch }