Merge pull request #61 from simple-rules/idc

WIP [new version of IDC]
pull/69/head
alajko 6 years ago committed by GitHub
commit b2ff625ea0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      identitychain/identityblock.go
  2. 81
      identitychain/identitychain.go
  3. 3
      runwait/run_wait.go

@ -30,6 +30,11 @@ func (b *IdentityBlock) Serialize() []byte {
return result.Bytes() return result.Bytes()
} }
//Get Identities
func (b *IdentityBlock) GetIdentities() []*waitnode.WaitNode {
return b.Identities
}
// DeserializeBlock deserializes a block // DeserializeBlock deserializes a block
func DeserializeBlock(d []byte) *IdentityBlock { func DeserializeBlock(d []byte) *IdentityBlock {
var block IdentityBlock var block IdentityBlock

@ -2,6 +2,8 @@ package identitychain
import ( import (
"fmt" "fmt"
"math"
"math/rand"
"net" "net"
"os" "os"
"sync" "sync"
@ -16,20 +18,83 @@ var identityPerBlock = 100000
// IdentityChain (Blockchain) keeps Identities per epoch, currently centralized! // IdentityChain (Blockchain) keeps Identities per epoch, currently centralized!
type IdentityChain struct { type IdentityChain struct {
Identities []*IdentityBlock Identities []*IdentityBlock
PendingIdentities []*waitnode.WaitNode PendingIdentities []*waitnode.WaitNode
log log.Logger log log.Logger
Peer p2p.Peer Peer p2p.Peer
SelectedIdentitites []*waitnode.WaitNode
EpochNum int
PeerToShardMap map[*waitnode.WaitNode]int
ShardLeaderMap map[int]*waitnode.WaitNode
PubKey string
CurrentEpochStartTime int64
NumberOfShards int
NumberOfNodesInShard int
}
func seekRandomNumber(EpochNum int, SelectedIdentitites []*waitnode.WaitNode) int {
// Broadcast message to all nodes and collect back their numbers, do consensus and get a leader.
// Use leader to generate a random number.
//all here mocked
// interact with "node" and "wait_node"
return rand.Intn(1000)
} }
//GlobalBlockchainConfig stores global level blockchain configurations. //GlobalBlockchainConfig stores global level blockchain configurations.
type GlobalBlockchainConfig struct { type GlobalBlockchainConfig struct {
NumberOfShards int NumberOfShards int
EpochTimeSecs int16 EpochTimeSecs int16
MaxNodesInShard int
}
//Shard
func (IDC *IdentityChain) Shard() {
num := seekRandomNumber(IDC.EpochNum, IDC.SelectedIdentitites)
IDC.CreateShardAssignment(num)
IDC.ElectLeaders()
}
//
func (IDC *IdentityChain) ElectLeaders() {
}
//CreateShardAssignment
func (IDC *IdentityChain) CreateShardAssignment(num int) {
IDC.NumberOfShards = IDC.NumberOfShards + needNewShards()
IDC.SelectedIdentitites = generateRandomPermutations(num, IDC.SelectedIdentitites)
IDC.PeerToShardMap = make(map[*waitnode.WaitNode]int)
numberInOneShard := len(IDC.SelectedIdentitites) / IDC.NumberOfShards
for peerNum := 1; peerNum <= len(IDC.SelectedIdentitites); peerNum++ {
IDC.PeerToShardMap[IDC.SelectedIdentitites[peerNum]] = peerNum / numberInOneShard
}
}
func generateRandomPermutations(num int, SelectedIdentitites []*waitnode.WaitNode) []*waitnode.WaitNode {
src := rand.NewSource(int64(num))
rnd := rand.New(src)
perm := rnd.Perm(len(SelectedIdentitites))
SelectedIdentititesCopy := make([]*waitnode.WaitNode, len(SelectedIdentitites))
for j, i := range perm {
SelectedIdentititesCopy[j] = SelectedIdentitites[i]
}
return SelectedIdentititesCopy
}
// SelectIds
func (IDC *IdentityChain) SelectIds() {
selectNumber := IDC.NumberOfNodesInShard - len(IDC.Identities)
IB := IDC.GetLatestBlock()
currentIDS := IB.GetIdentities()
selectNumber = int(math.Min(float64(len(IDC.PendingIdentities)), float64(selectNumber)))
pending := IDC.PendingIdentities[:selectNumber]
IDC.SelectedIdentitites = append(currentIDS, pending...)
IDC.PendingIdentities = []*waitnode.WaitNode{}
} }
func (IDC *IdentityChain) shard() { //Checks how many new shards we need. Currently we say 0.
return func needNewShards() int {
return 0
} }
// GetLatestBlock gests the latest block at the end of the chain // GetLatestBlock gests the latest block at the end of the chain

@ -1,5 +1,7 @@
package main package main
import "fmt"
// import ( // import (
// "flag" // "flag"
@ -8,6 +10,7 @@ package main
// ) // )
func main() { func main() {
fmt.Println("hello")
// ip := flag.String("ip", "127.0.0.0", "IP of the node") // ip := flag.String("ip", "127.0.0.0", "IP of the node")
// port := flag.String("port", "8080", "port of the node") // port := flag.String("port", "8080", "port of the node")
// flag.Parse() // flag.Parse()

Loading…
Cancel
Save