Skip to content

Commit

Permalink
Merge pull request #1568 from orbs-network/feature/mgmt-weight-election
Browse files Browse the repository at this point in the history
Election change for no-balance, Weight for Mgmt
  • Loading branch information
noambergIL authored Jun 11, 2020
2 parents 27ac100 + 602b329 commit 10bc77a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
12 changes: 6 additions & 6 deletions services/management/adapter/_data/good.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@
"RefTime": 1582613000,
"Committee":
[
{ "EthAddress": "034320222c534e8be4cd3bf8d70cb66ddda59e32", "OrbsAddress": "a328846cd5b4979d68a8c58a9bdfeee657b34de7", "EffectiveStake": 16578000, "IdentityType": 0 },
{ "EthAddress": "e79489c84518abe2f9b91607734e81fa13c28826", "OrbsAddress": "d27e2e7398e2582f63d0800330010b3e58952ff6", "EffectiveStake": 828363, "IdentityType": 1 }
{ "EthAddress": "034320222c534e8be4cd3bf8d70cb66ddda59e32", "OrbsAddress": "a328846cd5b4979d68a8c58a9bdfeee657b34de7", "Weight": 16578000, "IdentityType": 0 },
{ "EthAddress": "e79489c84518abe2f9b91607734e81fa13c28826", "OrbsAddress": "d27e2e7398e2582f63d0800330010b3e58952ff6", "Weight": 828363, "IdentityType": 1 }
]
},
{
"RefTime": 1582614000,
"Committee":
[
{ "EthAddress": "034320222c534e8be4cd3bf8d70cb66ddda59e32", "OrbsAddress": "a328846cd5b4979d68a8c58a9bdfeee657b34de7", "EffectiveStake": 16578000, "IdentityType": 0 },
{ "EthAddress": "e79489c84518abe2f9b91607734e81fa13c28826", "OrbsAddress": "d27e2e7398e2582f63d0800330010b3e58952ff6", "EffectiveStake": 828363, "IdentityType": 1 }
{ "EthAddress": "034320222c534e8be4cd3bf8d70cb66ddda59e32", "OrbsAddress": "a328846cd5b4979d68a8c58a9bdfeee657b34de7", "Weight": 16578000, "IdentityType": 0 },
{ "EthAddress": "e79489c84518abe2f9b91607734e81fa13c28826", "OrbsAddress": "d27e2e7398e2582f63d0800330010b3e58952ff6", "Weight": 828363, "IdentityType": 1 }
]
},
{
"RefTime": 1582616000,
"Committee":
[
{ "EthAddress": "034320222c534e8be4cd3bf8d70cb66ddda59e32", "OrbsAddress": "a328846cd5b4979d68a8c58a9bdfeee657b34de7", "EffectiveStake": 16578435, "IdentityType": 0 },
{ "EthAddress": "e79489c84518abe2f9b91607734e81fa13c28826", "OrbsAddress": "d27e2e7398e2582f63d0800330010b3e58952ff6", "EffectiveStake": 828363, "IdentityType": 1 }
{ "EthAddress": "034320222c534e8be4cd3bf8d70cb66ddda59e32", "OrbsAddress": "a328846cd5b4979d68a8c58a9bdfeee657b34de7", "Weight": 16578435, "IdentityType": 0 },
{ "EthAddress": "e79489c84518abe2f9b91607734e81fa13c28826", "OrbsAddress": "d27e2e7398e2582f63d0800330010b3e58952ff6", "Weight": 828363, "IdentityType": 1 }
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion services/management/adapter/file_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type topologyNode struct {
type committee struct {
EthAddress string
OrbsAddress string
EffectiveStake uint64
Weight uint64
IdentityType uint
}

Expand Down
12 changes: 7 additions & 5 deletions services/processor/native/repository/_Elections/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (f *harness) setupEthereumStateBeforeProcess(m Mockery) {
f.setupEthereumGuardiansDataBeforeProcess(m)

for _, d := range f.delegators {
mockStakedAndLockedInEthereum(m, f.electionBlock, d.address, d.stake, d.lockedStake)
f.mockStakedAndLockedInEthereum(m, f.electionBlock, d.address, d.stake, d.lockedStake)
}
}

Expand All @@ -198,7 +198,7 @@ func (f *harness) setupEthereumGuardiansDataBeforeProcess(m Mockery) {
if a.isGuardian {
mockGuardianVoteInEthereum(m, f.electionBlock, a.address, a.votedValidators, a.voteBlock)
if a.voteBlock >= _getProcessCurrentElectionEarliestValidVoteBlockNumber() {
mockStakedAndLockedInEthereum(m, f.electionBlock, a.address, a.stake, a.lockedStake)
f.mockStakedAndLockedInEthereum(m, f.electionBlock, a.address, a.stake, a.lockedStake)
}
}
}
Expand All @@ -209,7 +209,7 @@ func (f *harness) setupEthereumValidatorsBeforeProcess(m Mockery) {
validatorAddresses := make([][20]byte, len(f.validators))
for i, a := range f.validators {
validatorAddresses[i] = a.address
mockStakedAndLockedInEthereum(m, f.electionBlock, a.address, a.stake, a.lockedStake)
f.mockStakedAndLockedInEthereum(m, f.electionBlock, a.address, a.stake, a.lockedStake)
mockValidatorOrbsAddressInEthereum(m, f.electionBlock, a.address, a.orbsAddress)
}
mockValidatorsInEthereum(m, f.electionBlock, validatorAddresses)
Expand Down Expand Up @@ -296,8 +296,10 @@ func mockValidatorOrbsAddressInEthereum(m Mockery, blockNumber uint64, validator
}, validatorAddress)
}

func mockStakedAndLockedInEthereum(m Mockery, blockNumber uint64, address [20]byte, stake int, lockedStake int) {
mockStakeInEthereum(m, blockNumber, address, stake)
func (f *harness) mockStakedAndLockedInEthereum(m Mockery, blockNumber uint64, address [20]byte, stake int, lockedStake int) {
if f.electionBlock <= DONT_USE_NON_LOCKED_AFTER_BLOCK {
mockStakeInEthereum(m, blockNumber, address, stake)
}
mockLockedStakeInEthereum(m, blockNumber, address, lockedStake)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ func _collectOneDelegatorStakeFromEthereum(i int) {
fmt.Printf("elections %10d: from ethereum delegator %x , unlocked-stake %d, locked stake %d\n", _getProcessCurrentElectionBlockNumber(), delegator, stake, lockedStake)
}

const DONT_USE_NON_LOCKED_AFTER_BLOCK = uint64(10368900)
func _getStakeAtElection(ethAddr [20]byte) uint64 {
if _getProcessCurrentElectionBlockNumber() > DONT_USE_NON_LOCKED_AFTER_BLOCK {
return 0
}
stake := new(*big.Int)
ethereum.CallMethodAtBlock(_getProcessCurrentElectionBlockNumber(), getTokenEthereumContractAddress(), getTokenAbi(), "balanceOf", stake, ethAddr)
return ((*stake).Div(*stake, ETHEREUM_STAKE_FACTOR)).Uint64()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,52 @@ func TestOrbsVotingContract_processVote_processVoteMachine_CalulateStakesWithLoc
})
}

func TestOrbsVotingContract_processVote_processVoteMachine_BeforeAndAfterOnlyLocked(t *testing.T) {
h := newHarnessBlockBased()
h.electionBlock = 10368900
aRecentVoteBlock := h.electionBlock - 1

var v1, v2, v3, v4, v5 = h.addValidator(), h.addValidator(), h.addValidator(), h.addValidator(), h.addValidator()
var g1, g2, g3, g4 = h.addGuardian(10000), h.addGuardian(10000), h.addGuardian(50), h.addGuardian(10000)

g1.vote(aRecentVoteBlock, v2, v3)
g2.vote(aRecentVoteBlock, v2, v1)
g3.vote(aRecentVoteBlock, v2, v4)
g4.vote(aRecentVoteBlock, v2, v5)

for i := 0; i < 10; i++ {
h.addDelegator(500, g3.address).withLockedStake(1000)
}

// d1 := h.addDelegator(500, g4.address).withLockedStake(100000)
// d2 := h.addDelegator(500, d1.address)
// h.addDelegator(500, d2.address)

InServiceScope(nil, nil, func(m Mockery) {
_init()

// prepare
h.setupOrbsStateBeforeProcessMachine()
h.setupEthereumStateBeforeProcess(m)

// first run
expectedNumOfStateTransitions := len(h.guardians) + len(h.delegators) + len(h.validators) + 2
elected, _ := h.runProcessVoteMachineNtimes(expectedNumOfStateTransitions)
require.EqualValues(t, "", _getVotingProcessState())
require.ElementsMatch(t, [][20]byte{v1.address,v3.address, v4.address, v5.address}, elected)

// second run
h.electionBlock = h.electionBlock+1
_setProcessCurrentElection(h.electionTime, h.electionBlock, h.electionBlock-VOTE_VALID_PERIOD_LENGTH_IN_BLOCKS)
h.setupEthereumStateBeforeProcess(m)
elected, _ = h.runProcessVoteMachineNtimes(expectedNumOfStateTransitions)

// assert
require.EqualValues(t, "", _getVotingProcessState())
require.ElementsMatch(t, [][20]byte{v1.address, v3.address, v5.address}, elected)
})
}

func TestOrbsVotingContract_processVote_processVoteMachineWithTimeBased_CalulateStakes(t *testing.T) {
h := newHarnessTimeBased()
h.electionBlock = 60000
Expand Down Expand Up @@ -257,8 +303,8 @@ func TestOrbsVotingContract_processVote_processVoteMachine_CalulateStakes_Delega
h.setupEthereumValidatorsBeforeProcess(m)
mockGuardiansInEthereum(m, h.electionBlock, h.guardians)
h.setupEthereumGuardiansDataBeforeProcess(m)
mockStakedAndLockedInEthereum(m, h.electionBlock, realD2.address, realD2.stake, realD2.lockedStake)
mockStakedAndLockedInEthereum(m, h.electionBlock, realD3.address, realD3.stake, realD3.lockedStake)
h.mockStakedAndLockedInEthereum(m, h.electionBlock, realD2.address, realD2.stake, realD2.lockedStake)
h.mockStakedAndLockedInEthereum(m, h.electionBlock, realD3.address, realD3.stake, realD3.lockedStake)

// call
expectedNumOfStateTransitions := len(h.guardians) + len(h.delegators) + len(h.validators) + 3
Expand Down Expand Up @@ -322,9 +368,9 @@ func TestOrbsVotingContract_processVote_collectValidatorDataFromEthereum(t *test
// prepare
h.setupOrbsStateBeforeProcessMachine()
mockValidatorOrbsAddressInEthereum(m, h.electionBlock, v1.address, v1.orbsAddress)
mockStakedAndLockedInEthereum(m, h.electionBlock, v1.address, 250, 250)
h.mockStakedAndLockedInEthereum(m, h.electionBlock, v1.address, 250, 250)
mockValidatorOrbsAddressInEthereum(m, h.electionBlock, v2.address, v2.orbsAddress)
mockStakedAndLockedInEthereum(m, h.electionBlock, v2.address, 450, 450)
h.mockStakedAndLockedInEthereum(m, h.electionBlock, v2.address, 450, 450)
_setVotingProcessItem(0)

// call
Expand Down Expand Up @@ -497,7 +543,7 @@ func TestOrbsVotingContract_processVote_collectOneDelegatorStakeFromEthereum_NoS

// prepare
h.setupOrbsStateBeforeProcessMachine()
mockStakedAndLockedInEthereum(m, h.electionBlock, [20]byte{}, 0, 0)
h.mockStakedAndLockedInEthereum(m, h.electionBlock, [20]byte{}, 0, 0)

// call
_collectOneDelegatorStakeFromEthereum(0)
Expand Down

0 comments on commit 10bc77a

Please sign in to comment.