From c54e225dc469a6a6b22fc153cc7977952b730233 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 11 Mar 2019 14:50:34 -0700 Subject: [PATCH] Setup initial shard state --- core/resharding.go | 25 ++++++++++++++++--------- core/resharding_test.go | 18 ++++++++++++++++++ internal/utils/contract/constants.go | 16 +++++++++++++++- test/crypto/bls/main.go | 15 ++------------- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/core/resharding.go b/core/resharding.go index d4ae206d9..633dd6dc0 100644 --- a/core/resharding.go +++ b/core/resharding.go @@ -2,11 +2,15 @@ package core import ( "encoding/binary" + "encoding/hex" "math" "math/rand" "sort" "strconv" + "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/internal/utils/contract" + "github.com/harmony-one/harmony/core/types" ) @@ -129,7 +133,7 @@ func GetShardingStateFromBlockChain(bc *BlockChain, epoch uint64) *ShardingState // TODO: currently, we just mock everything func CalculateNewShardState(bc *BlockChain, epoch uint64) types.ShardState { if epoch == FirstEpoch { - return fakeGetInitShardState(6, 10) + return getInitShardState(3, 10) } ss := GetShardingStateFromBlockChain(bc, epoch-1) 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)) } -// remove later after bootstrap codes ready -func fakeGetInitShardState(numberOfShards, numOfNodes int) types.ShardState { - rand.Seed(int64(InitialSeed)) +// getInitShardState returns the initial shard state at genesis. +func getInitShardState(numberOfShards, numNodesPerShard int) types.ShardState { 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)) + com := types.Committee{ShardID: uint32(i)} + if i == 0 { + for j := 0; j < numNodesPerShard; j++ { + priKey := bls.SecretKey{} + 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) } diff --git a/core/resharding_test.go b/core/resharding_test.go index d5d92a0ec..5c7dce883 100644 --- a/core/resharding_test.go +++ b/core/resharding_test.go @@ -2,12 +2,30 @@ package core import ( "fmt" + "math/rand" + "strconv" "testing" "github.com/harmony-one/harmony/core/types" "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) { nodeList := fakeNewNodeList(42) fmt.Println("newNodeList: ", nodeList) diff --git a/internal/utils/contract/constants.go b/internal/utils/contract/constants.go index 0f9bbb3f4..4c189f666 100644 --- a/internal/utils/contract/constants.go +++ b/internal/utils/contract/constants.go @@ -38,7 +38,7 @@ var GenesisBeaconAccountPublicKey = GenesisBeaconAccountPriKey.PublicKey // DeployedContractAddress is the deployed contract address of the staking smart contract in beacon chain. 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{ {Address: "0xE2bD4413172C98d5094B94de1A8AC6a383d68b84", Private: "e401343197a852f361e38ce6b46c99f1d6d1f80499864c6ae7effee42b46ab6b", Public: "0xE2bD4413172C98d5094B94de1A8AC6a383d68b84"}, {Address: "0x183418934Fd8A97c98E086151317B2df6259b8A8", Private: "a7d764439a7619f703c97ee2a2cf0be2cd62ad4c9deebd5423d6f28de417b907", Public: "0x183418934Fd8A97c98E086151317B2df6259b8A8"}, @@ -52,6 +52,20 @@ var InitialBeaconChainAccounts = [...]DeployAccount{ {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. var NewNodeAccounts = [...]DeployAccount{ {Address: "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723", Private: "27978f895b11d9c737e1ab1623fde722c04b4f9ccb4ab776bf15932cc72d7c66", Public: "0x1a3e7a44ee21101d7D64FBf29B0F6F1fc295F723"}, diff --git a/test/crypto/bls/main.go b/test/crypto/bls/main.go index 96c38a67c..c46bda6c6 100644 --- a/test/crypto/bls/main.go +++ b/test/crypto/bls/main.go @@ -1,13 +1,9 @@ package main import ( - "crypto/ecdsa" - "crypto/rand" - "encoding/hex" "log" "time" - "github.com/ethereum/go-ethereum/crypto" "github.com/harmony-one/bls/ffi/go/bls" ) @@ -21,18 +17,11 @@ func main() { var aggPub *bls.PublicKey startTime := time.Now() - for i := 0; i < 1000; i++ { + for i := 0; i < 10; i++ { var sec bls.SecretKey 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 { aggSig = sec.Sign(m) aggPub = sec.GetPublicKey()