diff --git a/cmd/beaconchain/main.go b/cmd/beaconchain/main.go index a2491cb1c..bd636c6f8 100644 --- a/cmd/beaconchain/main.go +++ b/cmd/beaconchain/main.go @@ -37,6 +37,7 @@ func main() { h := log.StdoutHandler log.Root().SetHandler(h) var bc *beaconchain.BeaconChain + if _, err := os.Stat(*resetFlag); err == nil { bc, err = beaconchain.LoadBeaconChainInfo(*resetFlag) if err != nil { diff --git a/internal/beaconchain/libs/beaconchain.go b/internal/beaconchain/libs/beaconchain.go index e069571f7..c92833c8c 100644 --- a/internal/beaconchain/libs/beaconchain.go +++ b/internal/beaconchain/libs/beaconchain.go @@ -40,18 +40,13 @@ type BCInfo struct { // BeaconChain (Blockchain) keeps Identities per epoch, currently centralized! type BeaconChain struct { - Leaders []*bcconn.NodeInfo - log log.Logger - ShardLeaderMap map[int]*bcconn.NodeInfo - PubKey kyber.Point - NumberOfShards int - NumberOfNodesAdded int - IP string - Port string - host host.Host - state BCState - saveFile string - rpcServer *beaconchain.Server + BCInfo BCInfo + log log.Logger + ShardLeaderMap map[int]*bcconn.NodeInfo + PubKey kyber.Point + host host.Host + state BCState + rpcServer *beaconchain.Server } //SaveFile is to store the file in which beaconchain info will be stored. @@ -76,18 +71,18 @@ func (bc *BeaconChain) InitRPCServer() { // StartRPCServer starts Rpc server. func (bc *BeaconChain) StartRPCServer() { - port, err := strconv.Atoi(bc.Port) + port, err := strconv.Atoi(bc.BCInfo.Port) if err != nil { port = 0 } bc.log.Info("support_client: StartRpcServer on port:", "port", strconv.Itoa(port+BeaconchainServicePortDiff)) - bc.rpcServer.Start(bc.IP, strconv.Itoa(port+BeaconchainServicePortDiff)) + bc.rpcServer.Start(bc.BCInfo.IP, strconv.Itoa(port+BeaconchainServicePortDiff)) } // GetShardLeaderMap returns the map from shard id to leader. func (bc *BeaconChain) GetShardLeaderMap() map[int]*bcconn.NodeInfo { result := make(map[int]*bcconn.NodeInfo) - for i, leader := range bc.Leaders { + for i, leader := range bc.BCInfo.Leaders { result[i] = leader } return result @@ -97,13 +92,13 @@ func (bc *BeaconChain) GetShardLeaderMap() map[int]*bcconn.NodeInfo { func New(numShards int, ip, port string) *BeaconChain { bc := BeaconChain{} bc.log = log.New() - bc.NumberOfShards = numShards bc.PubKey = generateBCKey() - bc.NumberOfNodesAdded = 0 - bc.ShardLeaderMap = make(map[int]*bcconn.NodeInfo) - bc.Port = port - bc.IP = ip bc.host = p2pimpl.NewHost(p2p.Peer{IP: ip, Port: port}) + bcinfo := &BCInfo{NumberOfShards: numShards, NumberOfNodesAdded: 0, + IP: ip, + Port: port, + ShardLeaderMap: make(map[int]*bcconn.NodeInfo)} + bc.BCInfo = *bcinfo return &bc } @@ -118,11 +113,11 @@ func generateBCKey() kyber.Point { func (bc *BeaconChain) AcceptNodeInfo(b []byte) *bcconn.NodeInfo { Node := bcconn.DeserializeNodeInfo(b) bc.log.Info("New Node Connection", "IP", Node.Self.IP, "Port", Node.Self.Port) - bc.NumberOfNodesAdded = bc.NumberOfNodesAdded + 1 - shardNum, isLeader := utils.AllocateShard(bc.NumberOfNodesAdded, bc.NumberOfShards) + bc.BCInfo.NumberOfNodesAdded = bc.BCInfo.NumberOfNodesAdded + 1 + shardNum, isLeader := utils.AllocateShard(bc.BCInfo.NumberOfNodesAdded, bc.BCInfo.NumberOfShards) if isLeader { - bc.Leaders = append(bc.Leaders, Node) - bc.ShardLeaderMap[shardNum] = Node + bc.BCInfo.Leaders = append(bc.BCInfo.Leaders, Node) + bc.BCInfo.ShardLeaderMap[shardNum] = Node } go SaveBeaconChainInfo(SaveFile, bc) bc.state = NodeInfoReceived @@ -131,7 +126,8 @@ func (bc *BeaconChain) AcceptNodeInfo(b []byte) *bcconn.NodeInfo { //RespondRandomness sends a randomness beacon to the node inorder for it process what shard it will be in func (bc *BeaconChain) RespondRandomness(Node *bcconn.NodeInfo) { - response := bcconn.ResponseRandomNumber{NumberOfShards: bc.NumberOfShards, NumberOfNodesAdded: bc.NumberOfNodesAdded, Leaders: bc.Leaders} + bci := bc.BCInfo + response := bcconn.ResponseRandomNumber{NumberOfShards: bci.NumberOfShards, NumberOfNodesAdded: bci.NumberOfNodesAdded, Leaders: bci.Leaders} msg := bcconn.SerializeRandomInfo(response) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg) bc.log.Info("Sent Out Msg", "# Nodes", response.NumberOfNodesAdded) @@ -175,13 +171,13 @@ func LoadBeaconChainInfo(path string) (*BeaconChain, error) { // BCtoBCI converts beaconchain into beaconchaininfo func BCtoBCI(bc *BeaconChain) *BCInfo { - bci := &BCInfo{Leaders: bc.Leaders, ShardLeaderMap: bc.ShardLeaderMap, NumberOfShards: bc.NumberOfShards, NumberOfNodesAdded: bc.NumberOfNodesAdded, IP: bc.IP, Port: bc.Port} + bci := &BCInfo{Leaders: bc.BCInfo.Leaders, ShardLeaderMap: bc.BCInfo.ShardLeaderMap, NumberOfShards: bc.BCInfo.NumberOfShards, NumberOfNodesAdded: bc.BCInfo.NumberOfNodesAdded, IP: bc.BCInfo.IP, Port: bc.BCInfo.Port} return bci } //BCItoBC converts beconchaininfo to beaconchain func BCItoBC(bci *BCInfo) *BeaconChain { - bc := &BeaconChain{Leaders: bci.Leaders, ShardLeaderMap: bci.ShardLeaderMap, NumberOfShards: bci.NumberOfShards, NumberOfNodesAdded: bci.NumberOfNodesAdded, IP: bci.IP, Port: bci.Port} + bc := &BeaconChain{BCInfo: *bci} return bc } diff --git a/internal/beaconchain/libs/beaconchain_test.go b/internal/beaconchain/libs/beaconchain_test.go index 754ca8f25..a180c115f 100644 --- a/internal/beaconchain/libs/beaconchain_test.go +++ b/internal/beaconchain/libs/beaconchain_test.go @@ -34,11 +34,11 @@ func TestNewNode(t *testing.T) { t.Error("beacon chain public key not initialized") } - if bc.NumberOfNodesAdded != 0 { + if bc.BCInfo.NumberOfNodesAdded != 0 { t.Error("beacon chain number of nodes starting with is not zero! (should be zero)") } - if bc.NumberOfShards != numshards { + if bc.BCInfo.NumberOfShards != numshards { t.Error("beacon chain number of shards not initialized to given number of desired shards") } } @@ -49,7 +49,7 @@ func TestShardLeaderMap(t *testing.T) { beaconport := "8080" numshards := 1 bc := New(numshards, ip, beaconport) - bc.Leaders = leaders + bc.BCInfo.Leaders = leaders if !reflect.DeepEqual(bc.GetShardLeaderMap(), shardLeaderMap) { t.Error("The function GetShardLeaderMap doesn't work well") } @@ -62,7 +62,7 @@ func TestFetchLeaders(t *testing.T) { beaconport := "8080" numshards := 1 bc := New(numshards, ip, beaconport) - bc.Leaders = leaders + bc.BCInfo.Leaders = leaders bc.rpcServer = beaconchain.NewServer(bc.GetShardLeaderMap) bc.StartRPCServer() port, _ := strconv.Atoi(beaconport) @@ -86,7 +86,7 @@ func TestAcceptNodeInfo(t *testing.T) { if !reflect.DeepEqual(node, leader1) { t.Error("Beaconchain is unable to deserialize incoming node info") } - if len(bc.Leaders) != 1 { + if len(bc.BCInfo.Leaders) != 1 { t.Error("Beaconchain was unable to update the leader array") } @@ -119,7 +119,8 @@ func TestSaveBC(t *testing.T) { ip = "127.0.0.1" port = "8080" numshards := 2 - bc := &BeaconChain{IP: ip, Port: port, NumberOfShards: numshards} + bci := &BCInfo{IP: ip, Port: port, NumberOfShards: numshards} + bc := &BeaconChain{BCInfo: *bci} err := SaveBeaconChainInfo("test.json", bc) if err != nil { log.Fatalln(err)