Add Withdrawal Credentials to Validator Record (#2216)

* add fix and unit test

* Update beacon-chain/core/validators/validator_test.go

Co-Authored-By: nisdas <nish1993@hotmail.com>
This commit is contained in:
Nishant Das 2019-04-10 02:03:31 +08:00 committed by Raul Jordan
parent 24dd4cc1e0
commit f2b9269650
2 changed files with 45 additions and 8 deletions

View File

@ -122,12 +122,13 @@ func ProcessDeposit(
// If public key does not exist in the registry, we add a new validator // If public key does not exist in the registry, we add a new validator
// to the beacon state. // to the beacon state.
newValidator := &pb.Validator{ newValidator := &pb.Validator{
Pubkey: pubkey, Pubkey: pubkey,
ActivationEpoch: params.BeaconConfig().FarFutureEpoch, ActivationEpoch: params.BeaconConfig().FarFutureEpoch,
ExitEpoch: params.BeaconConfig().FarFutureEpoch, ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawalEpoch: params.BeaconConfig().FarFutureEpoch, WithdrawalEpoch: params.BeaconConfig().FarFutureEpoch,
SlashedEpoch: params.BeaconConfig().FarFutureEpoch, SlashedEpoch: params.BeaconConfig().FarFutureEpoch,
StatusFlags: 0, StatusFlags: 0,
WithdrawalCredentialsHash32: withdrawalCredentials,
} }
state.ValidatorRegistry = append(state.ValidatorRegistry, newValidator) state.ValidatorRegistry = append(state.ValidatorRegistry, newValidator)
state.ValidatorBalances = append(state.ValidatorBalances, amount) state.ValidatorBalances = append(state.ValidatorBalances, amount)

View File

@ -224,6 +224,42 @@ func TestProcessDeposit_GoodWithdrawalCredentials(t *testing.T) {
ValidatorBalances: balances, ValidatorBalances: balances,
ValidatorRegistry: registry, ValidatorRegistry: registry,
} }
pubkey := []byte{7, 8, 9}
deposit := uint64(1000)
proofOfPossession := []byte{}
withdrawalCredentials := []byte{2}
newState, err := ProcessDeposit(
beaconState,
stateutils.ValidatorIndexMap(beaconState),
pubkey,
deposit,
proofOfPossession,
withdrawalCredentials,
)
if err != nil {
t.Fatalf("Process deposit failed: %v", err)
}
if newState.ValidatorBalances[2] != 1000 {
t.Errorf("Expected balance at index 1 to be 1000, received %d", newState.ValidatorBalances[2])
}
}
func TestProcessDeposit_RepeatedDeposit(t *testing.T) {
registry := []*pb.Validator{
{
Pubkey: []byte{1, 2, 3},
},
{
Pubkey: []byte{4, 5, 6},
WithdrawalCredentialsHash32: []byte{1},
},
}
balances := []uint64{0, 50}
beaconState := &pb.BeaconState{
ValidatorBalances: balances,
ValidatorRegistry: registry,
}
pubkey := []byte{4, 5, 6} pubkey := []byte{4, 5, 6}
deposit := uint64(1000) deposit := uint64(1000)
proofOfPossession := []byte{} proofOfPossession := []byte{}
@ -240,8 +276,8 @@ func TestProcessDeposit_GoodWithdrawalCredentials(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Process deposit failed: %v", err) t.Fatalf("Process deposit failed: %v", err)
} }
if newState.ValidatorBalances[1] != 1000 { if newState.ValidatorBalances[1] != 1050 {
t.Errorf("Expected balance at index 1 to be 1000, received %d", newState.ValidatorBalances[1]) t.Errorf("Expected balance at index 1 to be 1050, received %d", newState.ValidatorBalances[1])
} }
} }