package interop import ( "github.com/pkg/errors" "github.com/prysmaticlabs/go-ssz" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" "github.com/prysmaticlabs/prysm/shared/bls" "github.com/prysmaticlabs/prysm/shared/hashutil" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/trieutil" ) var ( domainDeposit = [4]byte{3, 0, 0, 0} genesisForkVersion = []byte{0, 0, 0, 0} // This is the recommended mock eth1 block hash according to the Eth2 interop guidelines. // https://github.com/ethereum/eth2.0-pm/blob/a085c9870f3956d6228ed2a40cd37f0c6580ecd7/interop/mocked_start/README.md mockEth1BlockHash = []byte{66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66} ) // GenerateGenesisState deterministically given a genesis time and number of validators. // If a genesis time of 0 is supplied it is set to the current time. func GenerateGenesisState(genesisTime, numValidators uint64) (*pb.BeaconState, []*ethpb.Deposit, error) { privKeys, pubKeys, err := DeterministicallyGenerateKeys(0 /*startIndex*/, numValidators) if err != nil { return nil, nil, errors.Wrapf(err, "could not deterministically generate keys for %d validators", numValidators) } depositDataItems, depositDataRoots, err := DepositDataFromKeys(privKeys, pubKeys) if err != nil { return nil, nil, errors.Wrap(err, "could not generate deposit data from keys") } trie, err := trieutil.GenerateTrieFromItems( depositDataRoots, int(params.BeaconConfig().DepositContractTreeDepth), ) if err != nil { return nil, nil, errors.Wrap(err, "could not generate Merkle trie for deposit proofs") } deposits, err := GenerateDepositsFromData(depositDataItems, trie) if err != nil { return nil, nil, errors.Wrap(err, "could not generate deposits from the deposit data provided") } root := trie.Root() if genesisTime == 0 { genesisTime = uint64(roughtime.Now().Unix()) } beaconState, err := state.GenesisBeaconState(deposits, genesisTime, ðpb.Eth1Data{ DepositRoot: root[:], DepositCount: uint64(len(deposits)), BlockHash: mockEth1BlockHash, }) if err != nil { return nil, nil, errors.Wrap(err, "could not generate genesis state") } return beaconState, deposits, nil } // GenerateDepositsFromData a list of deposit items by creating proofs for each of them from a sparse Merkle trie. func GenerateDepositsFromData(depositDataItems []*ethpb.Deposit_Data, trie *trieutil.MerkleTrie) ([]*ethpb.Deposit, error) { deposits := make([]*ethpb.Deposit, len(depositDataItems)) for i, item := range depositDataItems { proof, err := trie.MerkleProof(i) if err != nil { return nil, errors.Wrapf(err, "could not generate proof for deposit %d", i) } deposits[i] = ðpb.Deposit{ Proof: proof, Data: item, } } return deposits, nil } // DepositDataFromKeys generates a list of deposit data items from a set of BLS validator keys. func DepositDataFromKeys(privKeys []*bls.SecretKey, pubKeys []*bls.PublicKey) ([]*ethpb.Deposit_Data, [][]byte, error) { dataRoots := make([][]byte, len(privKeys)) depositDataItems := make([]*ethpb.Deposit_Data, len(privKeys)) for i := 0; i < len(privKeys); i++ { data, err := createDepositData(privKeys[i], pubKeys[i]) if err != nil { return nil, nil, errors.Wrapf(err, "could not create deposit data for key: %#x", privKeys[i].Marshal()) } hash, err := ssz.HashTreeRoot(data) if err != nil { return nil, nil, errors.Wrap(err, "could not hash tree root deposit data item") } dataRoots[i] = hash[:] depositDataItems[i] = data } return depositDataItems, dataRoots, nil } // Generates a deposit data item from BLS keys and signs the hash tree root of the data. func createDepositData(privKey *bls.SecretKey, pubKey *bls.PublicKey) (*ethpb.Deposit_Data, error) { di := ðpb.Deposit_Data{ PublicKey: pubKey.Marshal(), WithdrawalCredentials: withdrawalCredentialsHash(pubKey.Marshal()), Amount: params.BeaconConfig().MaxEffectiveBalance, } sr, err := ssz.SigningRoot(di) if err != nil { return nil, err } domain := bls.Domain(domainDeposit[:], genesisForkVersion) di.Signature = privKey.Sign(sr[:], domain).Marshal() return di, nil } // withdrawalCredentialsHash forms a 32 byte hash of the withdrawal public // address. // // The specification is as follows: // withdrawal_credentials[:1] == BLS_WITHDRAWAL_PREFIX_BYTE // withdrawal_credentials[1:] == hash(withdrawal_pubkey)[1:] // where withdrawal_credentials is of type bytes32. func withdrawalCredentialsHash(pubKey []byte) []byte { h := hashutil.Hash(pubKey) return append([]byte{blsWithdrawalPrefixByte}, h[1:]...)[:32] }