Skip to content

Commit

Permalink
Merge 697aea6 into 29a59c4
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusmihaic committed May 9, 2022
2 parents 29a59c4 + 697aea6 commit 4635333
Show file tree
Hide file tree
Showing 12 changed files with 675 additions and 175 deletions.
2 changes: 1 addition & 1 deletion epochStart/metachain/systemSCs.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func calcNormRand(randomness []byte, expectedLen int) []byte {
randLen := len(rand)

if expectedLen > randLen {
repeatedCt := expectedLen/randLen + 1
repeatedCt := expectedLen/randLen + 1 // todo: fix possible div by 0
rand = bytes.Repeat(randomness, repeatedCt)
}

Expand Down
36 changes: 9 additions & 27 deletions epochStart/metachain/systemSCs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ func TestSystemSCProcessor_ProcessSystemSmartContractUnStakeFromDelegationContra
args.Marshalizer,
)
allKeys := [][]byte{[]byte("stakedPubKey0"), []byte("waitingPubKey"), []byte("stakedPubKey1"), []byte("stakedPubKey2"), []byte("stakedPubKey3")}
stakingcommon.AddValidatorData(args.UserAccountsDB, delegationAddr, allKeys, big.NewInt(3000), args.Marshalizer)
stakingcommon.AddValidatorData(args.UserAccountsDB, delegationAddr, allKeys[2:], big.NewInt(3000), args.Marshalizer)
addDelegationData(args.UserAccountsDB, delegationAddr, allKeys, args.Marshalizer)
_, _ = args.UserAccountsDB.Commit()

Expand Down Expand Up @@ -1369,20 +1369,11 @@ func TestSystemSCProcessor_ProcessSystemSmartContractShouldUnStakeFromAdditional
contract, _ := scContainer.Get(vm.FirstDelegationSCAddress)
_ = scContainer.Add(delegationAddr, contract)

prepareStakingContractWithData(
args.UserAccountsDB,
[]byte("stakedPubKey0"),
[]byte("waitingPubKey"),
args.Marshalizer,
delegationAddr,
delegationAddr,
)
listOfKeysInWaiting := [][]byte{[]byte("waitingPubKey"), []byte("waitingPubKe1"), []byte("waitingPubKe2"), []byte("waitingPubKe3"), []byte("waitingPubKe4")}
allStakedKeys := append(listOfKeysInWaiting, []byte("stakedPubKey0"), []byte("stakedPubKey1"), []byte("stakedPubKey2"), []byte("stakedPubKey3"))

stakingcommon.AddStakingData(args.UserAccountsDB, delegationAddr, delegationAddr, [][]byte{[]byte("stakedPubKey1"), []byte("stakedPubKey2"), []byte("stakedPubKey3")}, args.Marshalizer)
listOfKeysInWaiting := [][]byte{[]byte("waitingPubKe1"), []byte("waitingPubKe2"), []byte("waitingPubKe3"), []byte("waitingPubKe4")}
allStakedKeys := append(listOfKeysInWaiting, []byte("waitingPubKey"), []byte("stakedPubKey0"), []byte("stakedPubKey1"), []byte("stakedPubKey2"), []byte("stakedPubKey3"))
stakingcommon.RegisterValidatorKeys(args.UserAccountsDB, delegationAddr, delegationAddr, allStakedKeys, big.NewInt(4000), args.Marshalizer)
stakingcommon.AddKeysToWaitingList(args.UserAccountsDB, listOfKeysInWaiting, args.Marshalizer, delegationAddr, delegationAddr)
stakingcommon.AddValidatorData(args.UserAccountsDB, delegationAddr, allStakedKeys, big.NewInt(4000), args.Marshalizer)
addDelegationData(args.UserAccountsDB, delegationAddr, allStakedKeys, args.Marshalizer)
_, _ = args.UserAccountsDB.Commit()

Expand Down Expand Up @@ -1712,37 +1703,28 @@ func TestSystemSCProcessor_ProcessSystemSmartContractStakingV4Init(t *testing.T)

owner1ListPubKeysWaiting := [][]byte{[]byte("waitingPubKe0"), []byte("waitingPubKe1"), []byte("waitingPubKe2")}
owner1ListPubKeysStaked := [][]byte{[]byte("stakedPubKey0"), []byte("stakedPubKey1")}
owner1AllPubKeys := append(owner1ListPubKeysWaiting, owner1ListPubKeysWaiting...)
owner1AllPubKeys := append(owner1ListPubKeysWaiting, owner1ListPubKeysStaked...)

owner2ListPubKeysWaiting := [][]byte{[]byte("waitingPubKe3"), []byte("waitingPubKe4")}
owner2ListPubKeysStaked := [][]byte{[]byte("stakedPubKey2")}
owner2AllPubKeys := append(owner2ListPubKeysWaiting, owner2ListPubKeysStaked...)

owner3ListPubKeysWaiting := [][]byte{[]byte("waitingPubKe5"), []byte("waitingPubKe6")}

prepareStakingContractWithData(
args.UserAccountsDB,
owner1ListPubKeysStaked[0],
owner1ListPubKeysWaiting[0],
args.Marshalizer,
owner1,
owner1,
)

// Owner1 has 2 staked nodes (one eligible, one waiting) in shard0 + 3 nodes in staking queue.
// It has enough stake so that all his staking queue nodes will be selected in the auction list
stakingcommon.AddKeysToWaitingList(args.UserAccountsDB, owner1ListPubKeysWaiting[1:], args.Marshalizer, owner1, owner1)
stakingcommon.AddValidatorData(args.UserAccountsDB, owner1, owner1AllPubKeys[1:], big.NewInt(5000), args.Marshalizer)
stakingcommon.AddKeysToWaitingList(args.UserAccountsDB, owner1ListPubKeysWaiting, args.Marshalizer, owner1, owner1)
stakingcommon.RegisterValidatorKeys(args.UserAccountsDB, owner1, owner1, owner1AllPubKeys, big.NewInt(5000), args.Marshalizer)

// Owner2 has 1 staked node (eligible) in shard1 + 2 nodes in staking queue.
// It has enough stake for only ONE node from staking queue to be selected in the auction list
stakingcommon.AddKeysToWaitingList(args.UserAccountsDB, owner2ListPubKeysWaiting, args.Marshalizer, owner2, owner2)
stakingcommon.AddValidatorData(args.UserAccountsDB, owner2, owner2AllPubKeys, big.NewInt(1500), args.Marshalizer)
stakingcommon.RegisterValidatorKeys(args.UserAccountsDB, owner2, owner2, owner2AllPubKeys, big.NewInt(2500), args.Marshalizer)

// Owner3 has 0 staked node + 2 nodes in staking queue.
// It has enough stake so that all his staking queue nodes will be selected in the auction list
stakingcommon.AddKeysToWaitingList(args.UserAccountsDB, owner3ListPubKeysWaiting, args.Marshalizer, owner3, owner3)
stakingcommon.AddValidatorData(args.UserAccountsDB, owner3, owner3ListPubKeysWaiting, big.NewInt(2000), args.Marshalizer)
stakingcommon.RegisterValidatorKeys(args.UserAccountsDB, owner3, owner3, owner3ListPubKeysWaiting, big.NewInt(2000), args.Marshalizer)

validatorsInfo := state.NewShardValidatorsInfoMap()
_ = validatorsInfo.Add(createValidatorInfo(owner1ListPubKeysStaked[0], common.EligibleList, owner1, 0))
Expand Down
137 changes: 137 additions & 0 deletions integrationTests/vm/staking/baseTestMetaProcessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package staking

import (
arwenConfig "github.com/ElrondNetwork/arwen-wasm-vm/v1_4/config"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/epochStart/metachain"
"github.com/ElrondNetwork/elrond-go/factory"
"github.com/ElrondNetwork/elrond-go/integrationTests"
vmFactory "github.com/ElrondNetwork/elrond-go/process/factory"
"github.com/ElrondNetwork/elrond-go/process/mock"
"github.com/ElrondNetwork/elrond-go/sharding/nodesCoordinator"
"github.com/ElrondNetwork/elrond-go/vm/systemSmartContracts/defaults"
)

func newTestMetaProcessor(
coreComponents factory.CoreComponentsHolder,
dataComponents factory.DataComponentsHolder,
bootstrapComponents factory.BootstrapComponentsHolder,
statusComponents factory.StatusComponentsHolder,
stateComponents factory.StateComponentsHandler,
nc nodesCoordinator.NodesCoordinator,
maxNodesConfig []config.MaxNodesChangeConfig,
queue [][]byte,
) *TestMetaProcessor {
gasScheduleNotifier := createGasScheduleNotifier()
blockChainHook := createBlockChainHook(
dataComponents, coreComponents,
stateComponents.AccountsAdapter(),
bootstrapComponents.ShardCoordinator(),
gasScheduleNotifier,
)

metaVmFactory := createVMContainerFactory(
coreComponents,
gasScheduleNotifier,
blockChainHook,
stateComponents.PeerAccounts(),
bootstrapComponents.ShardCoordinator(),
nc,
maxNodesConfig[0].MaxNumNodes,
)
vmContainer, _ := metaVmFactory.Create()
systemVM, _ := vmContainer.Get(vmFactory.SystemVirtualMachine)

validatorStatisticsProcessor := createValidatorStatisticsProcessor(
dataComponents,
coreComponents,
nc,
bootstrapComponents.ShardCoordinator(),
stateComponents.PeerAccounts(),
)
scp := createSystemSCProcessor(
nc,
coreComponents,
stateComponents,
bootstrapComponents.ShardCoordinator(),
maxNodesConfig,
validatorStatisticsProcessor,
systemVM,
)

txCoordinator := &mock.TransactionCoordinatorMock{}
epochStartTrigger := createEpochStartTrigger(coreComponents, dataComponents.StorageService())

eligible, _ := nc.GetAllEligibleValidatorsPublicKeys(0)
waiting, _ := nc.GetAllWaitingValidatorsPublicKeys(0)
shuffledOut, _ := nc.GetAllShuffledOutValidatorsPublicKeys(0)

return &TestMetaProcessor{
AccountsAdapter: stateComponents.AccountsAdapter(),
Marshaller: coreComponents.InternalMarshalizer(),
NodesConfig: nodesConfig{
eligible: eligible,
waiting: waiting,
shuffledOut: shuffledOut,
queue: queue,
auction: make([][]byte, 0),
},
MetaBlockProcessor: createMetaBlockProcessor(
nc,
scp,
coreComponents,
dataComponents,
bootstrapComponents,
statusComponents,
stateComponents,
validatorStatisticsProcessor,
blockChainHook,
metaVmFactory,
epochStartTrigger,
vmContainer,
txCoordinator,
),
currentRound: 1,
NodesCoordinator: nc,
ValidatorStatistics: validatorStatisticsProcessor,
EpochStartTrigger: epochStartTrigger,
BlockChainHandler: dataComponents.Blockchain(),
TxCacher: dataComponents.Datapool().CurrentBlockTxs(),
TxCoordinator: txCoordinator,
SystemVM: systemVM,
StateComponents: stateComponents,
BlockChainHook: blockChainHook,
}
}

func createGasScheduleNotifier() core.GasScheduleNotifier {
gasSchedule := arwenConfig.MakeGasMapForTests()
defaults.FillGasMapInternal(gasSchedule, 1)
return mock.NewGasScheduleNotifierMock(gasSchedule)
}

func createEpochStartTrigger(
coreComponents factory.CoreComponentsHolder,
storageService dataRetriever.StorageService,
) integrationTests.TestEpochStartTrigger {
argsEpochStart := &metachain.ArgsNewMetaEpochStartTrigger{
Settings: &config.EpochStartConfig{
MinRoundsBetweenEpochs: 10,
RoundsPerEpoch: 10,
},
Epoch: 0,
EpochStartNotifier: coreComponents.EpochStartNotifierWithConfirm(),
Storage: storageService,
Marshalizer: coreComponents.InternalMarshalizer(),
Hasher: coreComponents.Hasher(),
AppStatusHandler: coreComponents.StatusHandler(),
}

epochStartTrigger, _ := metachain.NewEpochStartTrigger(argsEpochStart)
testTrigger := &metachain.TestTrigger{}
testTrigger.SetTrigger(epochStartTrigger)

return testTrigger
}
6 changes: 3 additions & 3 deletions integrationTests/vm/staking/componentsHolderCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func createComponentHolders(numOfShards uint32) (
statusComponents := createStatusComponents()
stateComponents := createStateComponents(coreComponents)
dataComponents := createDataComponents(coreComponents, numOfShards)
boostrapComponents := createBootstrapComponents(coreComponents.InternalMarshalizer(), numOfShards)
bootstrapComponents := createBootstrapComponents(coreComponents.InternalMarshalizer(), numOfShards)

return coreComponents, dataComponents, boostrapComponents, statusComponents, stateComponents
return coreComponents, dataComponents, bootstrapComponents, statusComponents, stateComponents
}

func createCoreComponents() factory.CoreComponentsHolder {
Expand All @@ -62,7 +62,7 @@ func createCoreComponents() factory.CoreComponentsHolder {
EpochStartNotifierWithConfirmField: notifier.NewEpochStartSubscriptionHandler(),
EpochNotifierField: forking.NewGenericEpochNotifier(),
RaterField: &testscommon.RaterMock{Chance: 5},
AddressPubKeyConverterField: &testscommon.PubkeyConverterMock{},
AddressPubKeyConverterField: testscommon.NewPubkeyConverterMock(addressLength),
EconomicsDataField: stakingcommon.CreateEconomicsData(),
ChanStopNodeProcessField: endProcess.GetDummyEndProcessChannel(),
NodeTypeProviderField: nodetype.NewNodeTypeProvider(core.NodeTypeValidator),
Expand Down
44 changes: 35 additions & 9 deletions integrationTests/vm/staking/metaBlockProcessorCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-core/data/block"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/dataRetriever/dataPool"
"github.com/ElrondNetwork/elrond-go/epochStart"
"github.com/ElrondNetwork/elrond-go/epochStart/metachain"
"github.com/ElrondNetwork/elrond-go/factory"
Expand All @@ -17,6 +16,8 @@ import (
"github.com/ElrondNetwork/elrond-go/process/block/bootstrapStorage"
"github.com/ElrondNetwork/elrond-go/process/block/postprocess"
"github.com/ElrondNetwork/elrond-go/process/mock"
"github.com/ElrondNetwork/elrond-go/process/scToProtocol"
"github.com/ElrondNetwork/elrond-go/process/smartContract"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/sharding/nodesCoordinator"
"github.com/ElrondNetwork/elrond-go/state"
Expand All @@ -37,14 +38,16 @@ func createMetaBlockProcessor(
metaVMFactory process.VirtualMachinesContainerFactory,
epochStartHandler process.EpochStartTriggerHandler,
vmContainer process.VirtualMachinesContainer,
txCoordinator process.TransactionCoordinator,
) process.BlockProcessor {
shardCoordiantor := bootstrapComponents.ShardCoordinator()

blockTracker := createBlockTracker(dataComponents.Blockchain().GetGenesisHeader(), shardCoordiantor)
blockTracker := createBlockTracker(
dataComponents.Blockchain().GetGenesisHeader(),
bootstrapComponents.ShardCoordinator(),
)
epochStartDataCreator := createEpochStartDataCreator(
coreComponents,
dataComponents,
shardCoordiantor,
bootstrapComponents.ShardCoordinator(),
epochStartHandler,
blockTracker,
)
Expand All @@ -59,7 +62,9 @@ func createMetaBlockProcessor(
)

headerValidator := createHeaderValidator(coreComponents)
valInfoCreator := createValidatorInfoCreator(coreComponents, dataComponents, shardCoordiantor)
valInfoCreator := createValidatorInfoCreator(coreComponents, dataComponents, bootstrapComponents.ShardCoordinator())
stakingToPeer := createSCToProtocol(coreComponents, stateComponents, dataComponents.Datapool().CurrentBlockTxs())

args := blproc.ArgMetaProcessor{
ArgBaseProcessor: blproc.ArgBaseProcessor{
CoreComponents: coreComponents,
Expand All @@ -72,7 +77,7 @@ func createMetaBlockProcessor(
FeeHandler: postprocess.NewFeeAccumulator(),
RequestHandler: &testscommon.RequestHandlerStub{},
BlockChainHook: blockChainHook,
TxCoordinator: &mock.TransactionCoordinatorMock{},
TxCoordinator: txCoordinator,
EpochStartTrigger: epochStartHandler,
HeaderValidator: headerValidator,
GasHandler: &mock.GasHandlerMock{},
Expand All @@ -87,13 +92,13 @@ func createMetaBlockProcessor(
VMContainersFactory: metaVMFactory,
VmContainer: vmContainer,
},
SCToProtocol: &mock.SCToProtocolStub{},
SCToProtocol: stakingToPeer,
PendingMiniBlocksHandler: &mock.PendingMiniBlocksHandlerStub{},
EpochStartDataCreator: epochStartDataCreator,
EpochEconomics: &mock.EpochEconomicsStub{},
EpochRewardsCreator: &testscommon.RewardsCreatorStub{
GetLocalTxCacheCalled: func() epochStart.TransactionCacher {
return dataPool.NewCurrentBlockPool()
return dataComponents.Datapool().CurrentBlockTxs()
},
},
EpochValidatorInfoCreator: valInfoCreator,
Expand Down Expand Up @@ -200,3 +205,24 @@ func createHeaderValidator(coreComponents factory.CoreComponentsHolder) epochSta
headerValidator, _ := blproc.NewHeaderValidator(argsHeaderValidator)
return headerValidator
}

func createSCToProtocol(
coreComponents factory.CoreComponentsHolder,
stateComponents factory.StateComponentsHandler,
txCacher dataRetriever.TransactionCacher,
) process.SmartContractToProtocolHandler {
args := scToProtocol.ArgStakingToPeer{
PubkeyConv: coreComponents.AddressPubKeyConverter(),
Hasher: coreComponents.Hasher(),
Marshalizer: coreComponents.InternalMarshalizer(),
PeerState: stateComponents.PeerAccounts(),
BaseState: stateComponents.AccountsAdapter(),
ArgParser: smartContract.NewArgumentParser(),
CurrTxs: txCacher,
RatingsData: &mock.RatingsInfoMock{},
EpochNotifier: coreComponents.EpochNotifier(),
StakingV4InitEpoch: stakingV4InitEpoch,
}
stakingToPeer, _ := scToProtocol.NewStakingToPeer(args)
return stakingToPeer
}

0 comments on commit 4635333

Please sign in to comment.