fix conflict

pull/76/head
Minh Doan 6 years ago
commit 832796f7ec
  1. 40
      beaconchain/beaconchain.go
  2. 6
      beaconchain/beaconchain_handler.go
  3. 74
      identitychain/identityblock.go
  4. 2
      log/root.go
  5. 21
      runid/run_identity.go

@ -1,7 +1,6 @@
package identitychain package beaconchain
import ( import (
"fmt"
"net" "net"
"os" "os"
"sync" "sync"
@ -11,13 +10,14 @@ import (
"github.com/simple-rules/harmony-benchmark/log" "github.com/simple-rules/harmony-benchmark/log"
"github.com/simple-rules/harmony-benchmark/node" "github.com/simple-rules/harmony-benchmark/node"
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/p2p"
proto_identity "github.com/simple-rules/harmony-benchmark/proto/identity"
) )
var mutex sync.Mutex var mutex sync.Mutex
var identityPerBlock = 100000 var identityPerBlock = 100000
// IdentityChain (Blockchain) keeps Identities per epoch, currently centralized! // BeaconChain (Blockchain) keeps Identities per epoch, currently centralized!
type IdentityChain struct { type BeaconChain struct {
//Identities []*IdentityBlock //No need to have the identity block as of now //Identities []*IdentityBlock //No need to have the identity block as of now
Identities []*node.Node Identities []*node.Node
log log.Logger log log.Logger
@ -29,11 +29,14 @@ type IdentityChain struct {
} }
//Init //Init
func New(filename string) { func New(filename string) *BeaconChain {
idc := IdentityChain{} idc := BeaconChain{}
idc.NumberOfShards = readConfigFile(filename) //idc.NumberOfShards = readConfigFile(filename)
idc.log = log.New()
idc.NumberOfShards = 2
idc.PubKey = generateIDCKeys() idc.PubKey = generateIDCKeys()
idc.StartServer() idc.StartServer()
return &idc
} }
func readConfigFile(filename string) int { func readConfigFile(filename string) int {
@ -47,38 +50,36 @@ func generateIDCKeys() kyber.Point {
} }
//AcceptConnections welcomes new connections //AcceptConnections welcomes new connections
func (IDC *IdentityChain) AcceptConnections(b []byte) { func (IDC *BeaconChain) AcceptConnections(b []byte) {
Node := node.DeserializeWaitNode(b) Node := node.DeserializeWaitNode(b)
IDC.registerNode(Node) //This copies lock value of sync.mutex, we need to have a way around it by creating auxiliary data struct. IDC.registerNode(Node) //This copies lock value of sync.mutex, we need to have a way around it by creating auxiliary data struct.
} }
func (IDC *IdentityChain) registerNode(Node *node.Node) { func (IDC *BeaconChain) registerNode(Node *node.Node) {
IDC.Identities = append(IDC.Identities, Node) IDC.Identities = append(IDC.Identities, Node)
IDC.CommunicatePublicKeyToNode(Node.Self) IDC.CommunicatePublicKeyToNode(Node.Self)
return return
} }
func (IDC *IdentityChain) CommunicatePublicKeyToNode(Peer p2p.Peer) { func (IDC *BeaconChain) CommunicatePublicKeyToNode(Peer p2p.Peer) {
pbkey := pki.GetBytesFromPublicKey(IDC.PubKey) pbkey := pki.GetBytesFromPublicKey(IDC.PubKey)
fmt.Print(pbkey) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, pbkey[:])
//proto_identity.ConstructIdentityMessage(Acknowledge, pbkey) p2p.SendMessage(Peer, msgToSend)
} }
//StartServer a server and process the request by a handler. //StartServer a server and process the request by a handler.
func (IDC *IdentityChain) StartServer() { func (IDC *BeaconChain) StartServer() {
fmt.Println("Starting server...")
IDC.log.Info("Starting IDC server...") //log.Info does nothing for me! (ak) IDC.log.Info("Starting IDC server...") //log.Info does nothing for me! (ak)
IDC.listenOnPort() IDC.listenOnPort()
} }
func (IDC *IdentityChain) listenOnPort() { func (IDC *BeaconChain) listenOnPort() {
addr := net.JoinHostPort("", "8081") addr := net.JoinHostPort("", "8081")
listen, err := net.Listen("tcp4", addr) listen, err := net.Listen("tcp4", addr)
if err != nil { if err != nil {
IDC.log.Crit("Socket listen port failed") IDC.log.Crit("Socket listen port failed") //Log.Crit does nothing for me!
os.Exit(1) os.Exit(1)
} else { } else {
fmt.Println("Starting server...now listening")
IDC.log.Info("Identity chain is now listening ..") //log.Info does nothing for me! (ak) remove this IDC.log.Info("Identity chain is now listening ..") //log.Info does nothing for me! (ak) remove this
} }
defer listen.Close() defer listen.Close()
@ -88,8 +89,9 @@ func (IDC *IdentityChain) listenOnPort() {
IDC.log.Crit("Error listening on port. Exiting", "8081") IDC.log.Crit("Error listening on port. Exiting", "8081")
continue continue
} else { } else {
fmt.Println("I am accepting connections now") IDC.log.Info("I am accepting connections now")
} }
go IDC.IdentityChainHandler(conn) go IDC.BeaconChainHandler(conn)
} }
} }

@ -1,4 +1,4 @@
package identitychain package beaconchain
import ( import (
"fmt" "fmt"
@ -10,9 +10,9 @@ import (
proto_identity "github.com/simple-rules/harmony-benchmark/proto/identity" proto_identity "github.com/simple-rules/harmony-benchmark/proto/identity"
) )
//IdentityChainHandler handles registration of new Identities //BeaconChainHandler handles registration of new Identities
// This could have been its seperate package like consensus, but am avoiding creating a lot of packages. // This could have been its seperate package like consensus, but am avoiding creating a lot of packages.
func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) { func (IDC *BeaconChain) BeaconChainHandler(conn net.Conn) {
content, err := p2p.ReadMessageContent(conn) content, err := p2p.ReadMessageContent(conn)
if err != nil { if err != nil {
IDC.log.Error("Read p2p data failed") IDC.log.Error("Read p2p data failed")

@ -1,74 +0,0 @@
package identitychain
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"log"
"time"
"github.com/simple-rules/harmony-benchmark/node"
"github.com/simple-rules/harmony-benchmark/utils"
)
// IdentityBlock has the information of one node
type IdentityBlock struct {
Timestamp int64
PrevBlockHash [32]byte
NumIdentities int32
Identities []*node.Node
}
// Serialize serializes the block
func (b *IdentityBlock) Serialize() []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
err := encoder.Encode(b)
if err != nil {
log.Panic(err)
}
return result.Bytes()
}
// GetIdentities returns a list of identities.
func (b *IdentityBlock) GetIdentities() []*node.Node {
return b.Identities
}
// DeserializeBlock deserializes a block
func DeserializeBlock(d []byte) *IdentityBlock {
var block IdentityBlock
decoder := gob.NewDecoder(bytes.NewReader(d))
err := decoder.Decode(&block)
if err != nil {
log.Panic(err)
}
return &block
}
// NewBlock creates and returns a new block.
func NewBlock(Identities []*node.Node, prevBlockHash [32]byte) *IdentityBlock {
block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: prevBlockHash, NumIdentities: int32(len(Identities)), Identities: Identities}
return block
}
// CalculateBlockHash returns a hash of the block
func (b *IdentityBlock) CalculateBlockHash() [32]byte {
var hashes [][]byte
var blockHash [32]byte
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.Timestamp))
hashes = append(hashes, b.PrevBlockHash[:])
for _, id := range b.Identities {
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(id))
}
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.NumIdentities))
blockHash = sha256.Sum256(bytes.Join(hashes, []byte{}))
return blockHash
}
// NewGenesisBlock creates and returns genesis Block.
func NewGenesisBlock() *IdentityBlock {
var Ids []*node.Node
block := &IdentityBlock{Timestamp: time.Now().Unix(), PrevBlockHash: [32]byte{}, NumIdentities: 1, Identities: Ids}
return block
}

@ -13,7 +13,7 @@ var (
) )
func init() { func init() {
root.SetHandler(DiscardHandler()) root.SetHandler(StdoutHandler)
} }
// New returns a new logger with the given context. // New returns a new logger with the given context.

@ -1,27 +1,12 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"time"
"github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/beaconchain"
) )
func main() { func main() {
ip := flag.String("ip", "127.0.0.0", "IP of the node") bc := beaconchain.New("temp")
port := flag.String("port", "9000", "port of the node.") fmt.Print(bc)
flag.Parse()
peer := p2p.Peer{Ip: *ip, Port: *port}
fmt.Println(ip)
fmt.Println(peer)
epochTimer := time.NewTicker(10 * time.Second)
go func() {
for t := range epochTimer.C {
fmt.Println("Changing epoch at ", t)
}
}()
} }

Loading…
Cancel
Save