-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
187 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ func (t *validatorTestSuite) SetupTest() { | |
nil, | ||
nil, | ||
nil, | ||
nil, | ||
) | ||
t.gomockCtrl = gomockCtrl | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package services | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Giulio2002/bls" | ||
"github.com/ledgerwatch/erigon/cl/beacon/beaconevents" | ||
"github.com/ledgerwatch/erigon/cl/beacon/synced_data" | ||
"github.com/ledgerwatch/erigon/cl/clparams" | ||
"github.com/ledgerwatch/erigon/cl/cltypes" | ||
"github.com/ledgerwatch/erigon/cl/fork" | ||
"github.com/ledgerwatch/erigon/cl/pool" | ||
"github.com/ledgerwatch/erigon/cl/utils" | ||
) | ||
|
||
type blsToExecutionChangeService struct { | ||
operationsPool pool.OperationsPool | ||
emitters *beaconevents.Emitters | ||
syncedDataManager *synced_data.SyncedDataManager | ||
beaconCfg *clparams.BeaconChainConfig | ||
} | ||
|
||
func NewBLSToExecutionChangeService( | ||
operationsPool pool.OperationsPool, | ||
emitters *beaconevents.Emitters, | ||
syncedDataManager *synced_data.SyncedDataManager, | ||
beaconCfg *clparams.BeaconChainConfig, | ||
) BLSToExecutionChangeService { | ||
return &blsToExecutionChangeService{ | ||
operationsPool: operationsPool, | ||
emitters: emitters, | ||
syncedDataManager: syncedDataManager, | ||
beaconCfg: beaconCfg, | ||
} | ||
} | ||
|
||
func (s *blsToExecutionChangeService) ProcessMessage(ctx context.Context, subnet *uint64, msg *cltypes.SignedBLSToExecutionChange) error { | ||
defer s.emitters.Publish("bls_to_execution_change", msg) | ||
|
||
// [IGNORE] The signed_bls_to_execution_change is the first valid signed bls to execution change received | ||
// for the validator with index signed_bls_to_execution_change.message.validator_index. | ||
if s.operationsPool.BLSToExecutionChangesPool.Has(msg.Signature) { | ||
return nil | ||
} | ||
change := msg.Message | ||
|
||
state := s.syncedDataManager.HeadState() | ||
if state == nil { | ||
return nil | ||
} | ||
|
||
// [IGNORE] current_epoch >= CAPELLA_FORK_EPOCH, where current_epoch is defined by the current wall-clock time. | ||
if !(state.Version() >= clparams.CapellaVersion) { | ||
return ErrIgnore | ||
} | ||
|
||
// ref: https://github.com/ethereum/consensus-specs/blob/dev/specs/capella/beacon-chain.md#new-process_bls_to_execution_change | ||
// assert address_change.validator_index < len(state.validators) | ||
validator, err := state.ValidatorForValidatorIndex(int(change.ValidatorIndex)) | ||
if err != nil { | ||
return fmt.Errorf("unable to retrieve state: %v", err) | ||
} | ||
wc := validator.WithdrawalCredentials() | ||
|
||
// assert validator.withdrawal_credentials[:1] == BLS_WITHDRAWAL_PREFIX | ||
if wc[0] != byte(s.beaconCfg.BLSWithdrawalPrefixByte) { | ||
return fmt.Errorf("invalid withdrawal credentials prefix") | ||
} | ||
|
||
// assert validator.withdrawal_credentials[1:] == hash(address_change.from_bls_pubkey)[1:] | ||
// Perform full validation if requested. | ||
// Check the validator's withdrawal credentials against the provided message. | ||
hashedFrom := utils.Sha256(change.From[:]) | ||
if !bytes.Equal(hashedFrom[1:], wc[1:]) { | ||
return fmt.Errorf("invalid withdrawal credentials") | ||
} | ||
|
||
// assert bls.Verify(address_change.from_bls_pubkey, signing_root, signed_address_change.signature) | ||
genesisValidatorRoot := state.GenesisValidatorsRoot() | ||
domain, err := fork.ComputeDomain(s.beaconCfg.DomainBLSToExecutionChange[:], utils.Uint32ToBytes4(uint32(s.beaconCfg.GenesisForkVersion)), genesisValidatorRoot) | ||
if err != nil { | ||
return err | ||
} | ||
signedRoot, err := fork.ComputeSigningRoot(change, domain) | ||
if err != nil { | ||
return err | ||
} | ||
valid, err := bls.Verify(msg.Signature[:], signedRoot[:], change.From[:]) | ||
if err != nil { | ||
return err | ||
} | ||
if !valid { | ||
return fmt.Errorf("invalid signature") | ||
} | ||
s.operationsPool.BLSToExecutionChangesPool.Insert(msg.Signature, msg) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
cl/phase1/network/services/mock_services/bls_to_execution_change_service_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters