removing bcinfo redundancy

pull/251/head
ak 6 years ago
parent 8eee072300
commit bc828b545f
  1. 1
      cmd/beaconchain/main.go
  2. 38
      internal/beaconchain/libs/beaconchain.go
  3. 13
      internal/beaconchain/libs/beaconchain_test.go

@ -37,6 +37,7 @@ func main() {
h := log.StdoutHandler h := log.StdoutHandler
log.Root().SetHandler(h) log.Root().SetHandler(h)
var bc *beaconchain.BeaconChain var bc *beaconchain.BeaconChain
if _, err := os.Stat(*resetFlag); err == nil { if _, err := os.Stat(*resetFlag); err == nil {
bc, err = beaconchain.LoadBeaconChainInfo(*resetFlag) bc, err = beaconchain.LoadBeaconChainInfo(*resetFlag)
if err != nil { if err != nil {

@ -40,17 +40,12 @@ type BCInfo struct {
// BeaconChain (Blockchain) keeps Identities per epoch, currently centralized! // BeaconChain (Blockchain) keeps Identities per epoch, currently centralized!
type BeaconChain struct { type BeaconChain struct {
Leaders []*bcconn.NodeInfo BCInfo BCInfo
log log.Logger log log.Logger
ShardLeaderMap map[int]*bcconn.NodeInfo ShardLeaderMap map[int]*bcconn.NodeInfo
PubKey kyber.Point PubKey kyber.Point
NumberOfShards int
NumberOfNodesAdded int
IP string
Port string
host host.Host host host.Host
state BCState state BCState
saveFile string
rpcServer *beaconchain.Server rpcServer *beaconchain.Server
} }
@ -76,18 +71,18 @@ func (bc *BeaconChain) InitRPCServer() {
// StartRPCServer starts Rpc server. // StartRPCServer starts Rpc server.
func (bc *BeaconChain) StartRPCServer() { func (bc *BeaconChain) StartRPCServer() {
port, err := strconv.Atoi(bc.Port) port, err := strconv.Atoi(bc.BCInfo.Port)
if err != nil { if err != nil {
port = 0 port = 0
} }
bc.log.Info("support_client: StartRpcServer on port:", "port", strconv.Itoa(port+BeaconchainServicePortDiff)) 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. // GetShardLeaderMap returns the map from shard id to leader.
func (bc *BeaconChain) GetShardLeaderMap() map[int]*bcconn.NodeInfo { func (bc *BeaconChain) GetShardLeaderMap() map[int]*bcconn.NodeInfo {
result := make(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 result[i] = leader
} }
return result return result
@ -97,13 +92,13 @@ func (bc *BeaconChain) GetShardLeaderMap() map[int]*bcconn.NodeInfo {
func New(numShards int, ip, port string) *BeaconChain { func New(numShards int, ip, port string) *BeaconChain {
bc := BeaconChain{} bc := BeaconChain{}
bc.log = log.New() bc.log = log.New()
bc.NumberOfShards = numShards
bc.PubKey = generateBCKey() 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}) 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 return &bc
} }
@ -118,11 +113,11 @@ func generateBCKey() kyber.Point {
func (bc *BeaconChain) AcceptNodeInfo(b []byte) *bcconn.NodeInfo { func (bc *BeaconChain) AcceptNodeInfo(b []byte) *bcconn.NodeInfo {
Node := bcconn.DeserializeNodeInfo(b) Node := bcconn.DeserializeNodeInfo(b)
bc.log.Info("New Node Connection", "IP", Node.Self.IP, "Port", Node.Self.Port) bc.log.Info("New Node Connection", "IP", Node.Self.IP, "Port", Node.Self.Port)
bc.NumberOfNodesAdded = bc.NumberOfNodesAdded + 1 bc.BCInfo.NumberOfNodesAdded = bc.BCInfo.NumberOfNodesAdded + 1
shardNum, isLeader := utils.AllocateShard(bc.NumberOfNodesAdded, bc.NumberOfShards) shardNum, isLeader := utils.AllocateShard(bc.BCInfo.NumberOfNodesAdded, bc.BCInfo.NumberOfShards)
if isLeader { if isLeader {
bc.Leaders = append(bc.Leaders, Node) bc.BCInfo.Leaders = append(bc.BCInfo.Leaders, Node)
bc.ShardLeaderMap[shardNum] = Node bc.BCInfo.ShardLeaderMap[shardNum] = Node
} }
go SaveBeaconChainInfo(SaveFile, bc) go SaveBeaconChainInfo(SaveFile, bc)
bc.state = NodeInfoReceived 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 //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) { 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) msg := bcconn.SerializeRandomInfo(response)
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg)
bc.log.Info("Sent Out Msg", "# Nodes", response.NumberOfNodesAdded) bc.log.Info("Sent Out Msg", "# Nodes", response.NumberOfNodesAdded)
@ -175,13 +171,13 @@ func LoadBeaconChainInfo(path string) (*BeaconChain, error) {
// BCtoBCI converts beaconchain into beaconchaininfo // BCtoBCI converts beaconchain into beaconchaininfo
func BCtoBCI(bc *BeaconChain) *BCInfo { 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 return bci
} }
//BCItoBC converts beconchaininfo to beaconchain //BCItoBC converts beconchaininfo to beaconchain
func BCItoBC(bci *BCInfo) *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 return bc
} }

@ -34,11 +34,11 @@ func TestNewNode(t *testing.T) {
t.Error("beacon chain public key not initialized") 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)") 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") 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" beaconport := "8080"
numshards := 1 numshards := 1
bc := New(numshards, ip, beaconport) bc := New(numshards, ip, beaconport)
bc.Leaders = leaders bc.BCInfo.Leaders = leaders
if !reflect.DeepEqual(bc.GetShardLeaderMap(), shardLeaderMap) { if !reflect.DeepEqual(bc.GetShardLeaderMap(), shardLeaderMap) {
t.Error("The function GetShardLeaderMap doesn't work well") t.Error("The function GetShardLeaderMap doesn't work well")
} }
@ -62,7 +62,7 @@ func TestFetchLeaders(t *testing.T) {
beaconport := "8080" beaconport := "8080"
numshards := 1 numshards := 1
bc := New(numshards, ip, beaconport) bc := New(numshards, ip, beaconport)
bc.Leaders = leaders bc.BCInfo.Leaders = leaders
bc.rpcServer = beaconchain.NewServer(bc.GetShardLeaderMap) bc.rpcServer = beaconchain.NewServer(bc.GetShardLeaderMap)
bc.StartRPCServer() bc.StartRPCServer()
port, _ := strconv.Atoi(beaconport) port, _ := strconv.Atoi(beaconport)
@ -86,7 +86,7 @@ func TestAcceptNodeInfo(t *testing.T) {
if !reflect.DeepEqual(node, leader1) { if !reflect.DeepEqual(node, leader1) {
t.Error("Beaconchain is unable to deserialize incoming node info") 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") t.Error("Beaconchain was unable to update the leader array")
} }
@ -119,7 +119,8 @@ func TestSaveBC(t *testing.T) {
ip = "127.0.0.1" ip = "127.0.0.1"
port = "8080" port = "8080"
numshards := 2 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) err := SaveBeaconChainInfo("test.json", bc)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)

Loading…
Cancel
Save