Setup initial shard state

pull/556/head
Rongjian Lan 6 years ago
parent 644a37f2c7
commit c54e225dc4
  1. 25
      core/resharding.go
  2. 18
      core/resharding_test.go
  3. 16
      internal/utils/contract/constants.go
  4. 15
      test/crypto/bls/main.go

@ -2,11 +2,15 @@ package core
import ( import (
"encoding/binary" "encoding/binary"
"encoding/hex"
"math" "math"
"math/rand" "math/rand"
"sort" "sort"
"strconv" "strconv"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/internal/utils/contract"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
) )
@ -129,7 +133,7 @@ func GetShardingStateFromBlockChain(bc *BlockChain, epoch uint64) *ShardingState
// TODO: currently, we just mock everything // TODO: currently, we just mock everything
func CalculateNewShardState(bc *BlockChain, epoch uint64) types.ShardState { func CalculateNewShardState(bc *BlockChain, epoch uint64) types.ShardState {
if epoch == FirstEpoch { if epoch == FirstEpoch {
return fakeGetInitShardState(6, 10) return getInitShardState(3, 10)
} }
ss := GetShardingStateFromBlockChain(bc, epoch-1) ss := GetShardingStateFromBlockChain(bc, epoch-1)
newNodeList := fakeNewNodeList(int64(ss.rnd)) newNodeList := fakeNewNodeList(int64(ss.rnd))
@ -151,16 +155,19 @@ func (ss *ShardingState) calculateKickoutRate(newNodeList []types.NodeID) float6
return math.Max(0.1, math.Min(rate, 1.0)) return math.Max(0.1, math.Min(rate, 1.0))
} }
// remove later after bootstrap codes ready // getInitShardState returns the initial shard state at genesis.
func fakeGetInitShardState(numberOfShards, numOfNodes int) types.ShardState { func getInitShardState(numberOfShards, numNodesPerShard int) types.ShardState {
rand.Seed(int64(InitialSeed))
shardState := types.ShardState{} shardState := types.ShardState{}
for i := 0; i < numberOfShards; i++ { for i := 0; i < numberOfShards; i++ {
sid := uint32(i) com := types.Committee{ShardID: uint32(i)}
com := types.Committee{ShardID: sid} if i == 0 {
for j := 0; j < numOfNodes; j++ { for j := 0; j < numNodesPerShard; j++ {
nid := strconv.Itoa(int(rand.Int63())) priKey := bls.SecretKey{}
com.NodeList = append(com.NodeList, types.NodeID(nid)) priKey.SetHexString(contract.InitialBeaconChainAccounts[i].Private)
addrBytes := priKey.GetPublicKey().GetAddress()
address := hex.EncodeToString(addrBytes[:])
com.NodeList = append(com.NodeList, types.NodeID(address))
}
} }
shardState = append(shardState, com) shardState = append(shardState, com)
} }

@ -2,12 +2,30 @@ package core
import ( import (
"fmt" "fmt"
"math/rand"
"strconv"
"testing" "testing"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// remove later after bootstrap codes ready
func fakeGetInitShardState(numberOfShards, numOfNodes int) types.ShardState {
rand.Seed(int64(InitialSeed))
shardState := types.ShardState{}
for i := 0; i < numberOfShards; i++ {
sid := uint32(i)
com := types.Committee{ShardID: sid}
for j := 0; j < numOfNodes; j++ {
nid := strconv.Itoa(int(rand.Int63()))
com.NodeList = append(com.NodeList, types.NodeID(nid))
}
shardState = append(shardState, com)
}
return shardState
}
func TestFakeNewNodeList(t *testing.T) { func TestFakeNewNodeList(t *testing.T) {
nodeList := fakeNewNodeList(42) nodeList := fakeNewNodeList(42)
fmt.Println("newNodeList: ", nodeList) fmt.Println("newNodeList: ", nodeList)

@ -38,7 +38,7 @@ var GenesisBeaconAccountPublicKey = GenesisBeaconAccountPriKey.PublicKey
// DeployedContractAddress is the deployed contract address of the staking smart contract in beacon chain. // DeployedContractAddress is the deployed contract address of the staking smart contract in beacon chain.
var DeployedContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(GenesisBeaconAccountPublicKey), uint64(0)) var DeployedContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(GenesisBeaconAccountPublicKey), uint64(0))
// InitialBeaconChainAccounts are the accounts for the initial beacon chain node. // InitialBeaconChainAccounts are the ECSDA accounts for the initial beacon chain nodes.
var InitialBeaconChainAccounts = [...]DeployAccount{ var InitialBeaconChainAccounts = [...]DeployAccount{
{Address: "0xE2bD4413172C98d5094B94de1A8AC6a383d68b84", Private: "e401343197a852f361e38ce6b46c99f1d6d1f80499864c6ae7effee42b46ab6b", Public: "0xE2bD4413172C98d5094B94de1A8AC6a383d68b84"}, {Address: "0xE2bD4413172C98d5094B94de1A8AC6a383d68b84", Private: "e401343197a852f361e38ce6b46c99f1d6d1f80499864c6ae7effee42b46ab6b", Public: "0xE2bD4413172C98d5094B94de1A8AC6a383d68b84"},
{Address: "0x183418934Fd8A97c98E086151317B2df6259b8A8", Private: "a7d764439a7619f703c97ee2a2cf0be2cd62ad4c9deebd5423d6f28de417b907", Public: "0x183418934Fd8A97c98E086151317B2df6259b8A8"}, {Address: "0x183418934Fd8A97c98E086151317B2df6259b8A8", Private: "a7d764439a7619f703c97ee2a2cf0be2cd62ad4c9deebd5423d6f28de417b907", Public: "0x183418934Fd8A97c98E086151317B2df6259b8A8"},
@ -52,6 +52,20 @@ var InitialBeaconChainAccounts = [...]DeployAccount{
{Address: "0x47DF74d43fa4Ca1D814897eBD8Cf07e6bA024f75", Private: "d5a62757aa51f2ae883c5cb8631d312750ecc31f3934fffd95bcd969bd190c94", Public: "0x47DF74d43fa4Ca1D814897eBD8Cf07e6bA024f75"}, {Address: "0x47DF74d43fa4Ca1D814897eBD8Cf07e6bA024f75", Private: "d5a62757aa51f2ae883c5cb8631d312750ecc31f3934fffd95bcd969bd190c94", Public: "0x47DF74d43fa4Ca1D814897eBD8Cf07e6bA024f75"},
} }
// InitialBeaconChainBLSAccounts are the BLS accounts for the initial beacon chain nodes.
var InitialBeaconChainBLSAccounts = [...]DeployAccount{
{Address: "", Private: "66acb3a7c990be4b06709058fdef8122b7ecdbaf023e56ccf8cdf671c5333646", Public: ""},
{Address: "", Private: "5e9e2fffbf7cfad085d7b0147d2acd680cfd8b8d62daa9c39370185ba0207920", Public: ""},
{Address: "", Private: "56714bb94188c335d1243fa3d17fd50ff63a1a9bf740faecd97996f3a0737e87", Public: ""},
{Address: "", Private: "3de4fbe27f453b254014094108bf11b0a2ba4144585bf4cb10332155476339e9", Public: ""},
{Address: "", Private: "18c62ae8cd2a37e50b8fc89c53458ad6d9482f413d50086199d309ad1062684c", Public: ""},
{Address: "", Private: "2ca85157154a7df9fcdb4a404c7a8ac0675bbff7e841237b0de4645a9dcaca1c", Public: ""},
{Address: "", Private: "1dc77bb20378c7cacf82dd6fb7d5dedbb2c0855d85e0eb2d7d6df05bbb5da65d", Public: ""},
{Address: "", Private: "185bcaede728332c088645b31b988404512eeeb02413360cac6e30c9ca002661", Public: ""},
{Address: "", Private: "18b5d0d89b4575e002e4fd41e46fa628a248e1caa55fa887acd3c446b89058e3", Public: ""},
{Address: "", Private: "2221983e3f69897d54fcaa3cb131f4c729592f1b10faf247c0772e7b6476c1fd", Public: ""},
}
// NewNodeAccounts is the accounts used for new node to stake and join the network. // NewNodeAccounts is the accounts used for new node to stake and join the network.
var NewNodeAccounts = [...]DeployAccount{ var NewNodeAccounts = [...]DeployAccount{
{Address: "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723", Private: "27978f895b11d9c737e1ab1623fde722c04b4f9ccb4ab776bf15932cc72d7c66", Public: "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723"}, {Address: "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723", Private: "27978f895b11d9c737e1ab1623fde722c04b4f9ccb4ab776bf15932cc72d7c66", Public: "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723"},

@ -1,13 +1,9 @@
package main package main
import ( import (
"crypto/ecdsa"
"crypto/rand"
"encoding/hex"
"log" "log"
"time" "time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
) )
@ -21,18 +17,11 @@ func main() {
var aggPub *bls.PublicKey var aggPub *bls.PublicKey
startTime := time.Now() startTime := time.Now()
for i := 0; i < 1000; i++ { for i := 0; i < 10; i++ {
var sec bls.SecretKey var sec bls.SecretKey
sec.SetByCSPRNG() sec.SetByCSPRNG()
log.Printf("Secret Key: %s", sec.GetHexString())
if i == 0 {
testECKey, _ := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
log.Printf("Secret Key: 0x%s", sec.GetHexString())
log.Printf("Secret Key: 0x%s", hex.EncodeToString(sec.GetLittleEndian()))
log.Printf("Secret Key Length: %d", len(sec.GetLittleEndian()))
log.Printf("Secret Key: 0x%s", hex.EncodeToString(testECKey.D.Bytes()))
log.Printf("Secret Key Length: %d", len(testECKey.D.Bytes()))
}
if i == 0 { if i == 0 {
aggSig = sec.Sign(m) aggSig = sec.Sign(m)
aggPub = sec.GetPublicKey() aggPub = sec.GetPublicKey()

Loading…
Cancel
Save