|
|
@ -1,17 +1,18 @@ |
|
|
|
package beaconchain |
|
|
|
package beaconchain |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"math/rand" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/dedis/kyber" |
|
|
|
"github.com/dedis/kyber" |
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/bcconn" |
|
|
|
"github.com/harmony-one/harmony/crypto/pki" |
|
|
|
"github.com/harmony-one/harmony/crypto/pki" |
|
|
|
"github.com/harmony-one/harmony/log" |
|
|
|
"github.com/harmony-one/harmony/log" |
|
|
|
"github.com/harmony-one/harmony/node" |
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
"github.com/harmony-one/harmony/p2p" |
|
|
|
proto_identity "github.com/harmony-one/harmony/proto/identity" |
|
|
|
proto_identity "github.com/harmony-one/harmony/proto/identity" |
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/utils" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var mutex sync.Mutex |
|
|
|
var mutex sync.Mutex |
|
|
@ -19,83 +20,79 @@ var identityPerBlock = 100000 |
|
|
|
|
|
|
|
|
|
|
|
// BeaconChain (Blockchain) keeps Identities per epoch, currently centralized!
|
|
|
|
// BeaconChain (Blockchain) keeps Identities per epoch, currently centralized!
|
|
|
|
type BeaconChain struct { |
|
|
|
type BeaconChain struct { |
|
|
|
//Identities []*IdentityBlock //No need to have the identity block as of now
|
|
|
|
Leaders []*bcconn.NodeInfo |
|
|
|
Identities []*node.Node |
|
|
|
log log.Logger |
|
|
|
log log.Logger |
|
|
|
ShardLeaderMap map[int]*bcconn.NodeInfo |
|
|
|
PeerToShardMap map[*node.Node]int |
|
|
|
PubKey kyber.Point |
|
|
|
ShardLeaderMap map[int]*node.Node |
|
|
|
NumberOfShards int |
|
|
|
PubKey kyber.Point |
|
|
|
NumberOfNodesAdded int |
|
|
|
NumberOfShards int |
|
|
|
IP string |
|
|
|
NumberOfLeadersAdded int |
|
|
|
Port string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// New return BeaconChain.
|
|
|
|
//Init
|
|
|
|
func New(filename string) *BeaconChain { |
|
|
|
func New(numShards int, ip, port string) *BeaconChain { |
|
|
|
idc := BeaconChain{} |
|
|
|
bc := BeaconChain{} |
|
|
|
//idc.NumberOfShards = readConfigFile(filename)
|
|
|
|
bc.log = log.New() |
|
|
|
idc.log = log.New() |
|
|
|
bc.NumberOfShards = numShards |
|
|
|
idc.NumberOfShards = 2 |
|
|
|
bc.PubKey = generateIDCKeys() |
|
|
|
idc.PubKey = generateIDCKeys() |
|
|
|
bc.NumberOfNodesAdded = 0 |
|
|
|
return &idc |
|
|
|
bc.Port = port |
|
|
|
} |
|
|
|
bc.IP = ip |
|
|
|
|
|
|
|
return &bc |
|
|
|
func readConfigFile(filename string) int { |
|
|
|
|
|
|
|
return 2 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func generateIDCKeys() kyber.Point { |
|
|
|
func generateIDCKeys() kyber.Point { |
|
|
|
priKey := pki.GetPrivateKeyFromInt(10) |
|
|
|
r := rand.Intn(1000) |
|
|
|
|
|
|
|
priKey := pki.GetPrivateKeyFromInt(r) |
|
|
|
pubkey := pki.GetPublicKeyFromPrivateKey(priKey) |
|
|
|
pubkey := pki.GetPublicKeyFromPrivateKey(priKey) |
|
|
|
return pubkey |
|
|
|
return pubkey |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AcceptConnections welcomes new connections
|
|
|
|
//AcceptConnections welcomes new connections
|
|
|
|
func (IDC *BeaconChain) AcceptConnections(b []byte) { |
|
|
|
func (bc *BeaconChain) AcceptConnections(b []byte) { |
|
|
|
NewNode := node.DeserializeNode(b) |
|
|
|
Node := bcconn.DeserializeNodeInfo(b) |
|
|
|
fmt.Println(NewNode) |
|
|
|
bc.log.Info("Obtained node information, updating local information") |
|
|
|
} |
|
|
|
bc.NumberOfNodesAdded = bc.NumberOfNodesAdded + 1 |
|
|
|
|
|
|
|
_, isLeader := utils.AllocateShard(bc.NumberOfNodesAdded, bc.NumberOfShards) |
|
|
|
|
|
|
|
if isLeader { |
|
|
|
|
|
|
|
bc.Leaders = append(bc.Leaders, Node) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
|
|
|
|
func (IDC *BeaconChain) registerNode(Node *node.Node) { |
|
|
|
**IMPORTANT** |
|
|
|
IDC.Identities = append(IDC.Identities, Node) |
|
|
|
|
|
|
|
fmt.Println(IDC.Identities) |
|
|
|
|
|
|
|
//IDC.CommunicatePublicKeyToNode(Node.SelfPeer)
|
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CommunicatePublicKeyToNode communicates public key to node.
|
|
|
|
Note that public key is not in kyber.Scalar form it is in byte form. |
|
|
|
func (IDC *BeaconChain) CommunicatePublicKeyToNode(peer p2p.Peer) { |
|
|
|
Use following conversion to get back the actual key |
|
|
|
pbkey := pki.GetBytesFromPublicKey(IDC.PubKey) |
|
|
|
//peer.PubKey = crypto.Ed25519Curve.Point()
|
|
|
|
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, pbkey[:]) |
|
|
|
//err = peer.PubKey.UnmarshalBinary(p.PubKey[:])
|
|
|
|
p2p.SendMessage(peer, msgToSend) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//StartServer a server and process the request by a handler.
|
|
|
|
**/ |
|
|
|
func (IDC *BeaconChain) StartServer() { |
|
|
|
response := bcconn.ResponseRandomNumber{NumberOfShards: bc.NumberOfShards, NumberOfNodesAdded: bc.NumberOfNodesAdded, Leaders: bc.Leaders} |
|
|
|
IDC.log.Info("Starting IDC server...") //log.Info does nothing for me! (ak)
|
|
|
|
msg := bcconn.SerializeRandomInfo(response) |
|
|
|
IDC.listenOnPort() |
|
|
|
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg) |
|
|
|
|
|
|
|
p2p.SendMessage(Node.Self, msgToSend) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (IDC *BeaconChain) listenOnPort() { |
|
|
|
//StartServer a server and process the request by a handler.
|
|
|
|
addr := net.JoinHostPort("127.0.0.1", "8081") |
|
|
|
func (bc *BeaconChain) StartServer() { |
|
|
|
|
|
|
|
bc.log.Info("Starting Beaconchain server ...") |
|
|
|
|
|
|
|
ip := bc.IP |
|
|
|
|
|
|
|
port := bc.Port |
|
|
|
|
|
|
|
addr := net.JoinHostPort(ip, port) |
|
|
|
listen, err := net.Listen("tcp", addr) |
|
|
|
listen, err := net.Listen("tcp", addr) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
IDC.log.Crit("Socket listen port failed") |
|
|
|
bc.log.Crit("Socket listen port failed") |
|
|
|
os.Exit(1) |
|
|
|
os.Exit(1) |
|
|
|
} else { |
|
|
|
|
|
|
|
IDC.log.Info("Identity chain is now listening ..") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
defer listen.Close() |
|
|
|
defer listen.Close() |
|
|
|
for { |
|
|
|
for { |
|
|
|
IDC.log.Info("I am accepting connections now") |
|
|
|
bc.log.Info("beacon chain is now listening ..") |
|
|
|
conn, err := listen.Accept() |
|
|
|
conn, err := listen.Accept() |
|
|
|
fmt.Println(conn) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
IDC.log.Crit("Error listening on port. Exiting", "8081") |
|
|
|
bc.log.Crit("Error listening on port. Exiting", "8081") |
|
|
|
continue |
|
|
|
continue |
|
|
|
} else { |
|
|
|
|
|
|
|
IDC.log.Info("I am accepting connections now") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
go IDC.BeaconChainHandler(conn) |
|
|
|
go bc.BeaconChainHandler(conn) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|