2020-10-29 22:37:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
2021-09-21 18:11:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/runtime/interop"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2020-10-29 22:37:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_genesisStateFromJSONValidators(t *testing.T) {
|
2021-03-08 16:57:30 +00:00
|
|
|
numKeys := 5
|
|
|
|
jsonData := createGenesisDepositData(t, numKeys)
|
2020-10-29 22:37:16 +00:00
|
|
|
jsonInput, err := json.Marshal(jsonData)
|
|
|
|
require.NoError(t, err)
|
|
|
|
genesisState, err := genesisStateFromJSONValidators(
|
|
|
|
bytes.NewReader(jsonInput), 0, /* genesis time defaults to time.Now() */
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for i, val := range genesisState.Validators {
|
2021-03-08 16:57:30 +00:00
|
|
|
assert.DeepEqual(t, fmt.Sprintf("%#x", val.PublicKey), jsonData[i].PubKey)
|
2020-10-29 22:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 16:57:30 +00:00
|
|
|
func createGenesisDepositData(t *testing.T, numKeys int) []*DepositDataJSON {
|
2020-10-29 22:37:16 +00:00
|
|
|
pubKeys := make([]bls.PublicKey, numKeys)
|
|
|
|
privKeys := make([]bls.SecretKey, numKeys)
|
|
|
|
for i := 0; i < numKeys; i++ {
|
2020-10-30 19:06:33 +00:00
|
|
|
randKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2020-10-29 22:37:16 +00:00
|
|
|
privKeys[i] = randKey
|
|
|
|
pubKeys[i] = randKey.PublicKey()
|
|
|
|
}
|
|
|
|
dataList, _, err := interop.DepositDataFromKeys(privKeys, pubKeys)
|
|
|
|
require.NoError(t, err)
|
2021-03-08 16:57:30 +00:00
|
|
|
jsonData := make([]*DepositDataJSON, numKeys)
|
2020-10-29 22:37:16 +00:00
|
|
|
for i := 0; i < numKeys; i++ {
|
2021-03-08 16:57:30 +00:00
|
|
|
dataRoot, err := dataList[i].HashTreeRoot()
|
2020-10-29 22:37:16 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-08 16:57:30 +00:00
|
|
|
jsonData[i] = &DepositDataJSON{
|
|
|
|
PubKey: fmt.Sprintf("%#x", dataList[i].PublicKey),
|
|
|
|
Amount: dataList[i].Amount,
|
|
|
|
WithdrawalCredentials: fmt.Sprintf("%#x", dataList[i].WithdrawalCredentials),
|
|
|
|
DepositDataRoot: fmt.Sprintf("%#x", dataRoot),
|
|
|
|
Signature: fmt.Sprintf("%#x", dataList[i].Signature),
|
2020-10-29 22:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-08 16:57:30 +00:00
|
|
|
return jsonData
|
2020-10-29 22:37:16 +00:00
|
|
|
}
|