commit
de363e2a0b
@ -0,0 +1 @@ |
||||
package blockchain |
@ -0,0 +1,7 @@ |
||||
package main |
||||
|
||||
import "fmt" |
||||
|
||||
func main() { |
||||
fmt.Println("hello world") |
||||
} |
@ -0,0 +1,9 @@ |
||||
package identitychain |
||||
|
||||
// IdentityBlock has the information of one node
|
||||
type IdentityBlock struct { |
||||
ID int |
||||
IP string |
||||
Port string |
||||
NumIdentities int32 |
||||
} |
@ -0,0 +1,53 @@ |
||||
package identitychain |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net" |
||||
"os" |
||||
"sync" |
||||
|
||||
"github.com/simple-rules/harmony-benchmark/log" |
||||
"github.com/simple-rules/harmony-benchmark/waitnode" |
||||
) |
||||
|
||||
var mutex sync.Mutex |
||||
|
||||
// IdentityChain (Blockchain) keeps Identities per epoch, currently centralized!
|
||||
type IdentityChain struct { |
||||
Identities []*IdentityBlock |
||||
PendingIdentities []*waitnode.WaitNode |
||||
log log.Logger |
||||
} |
||||
|
||||
func main() { |
||||
var IDC IdentityChain |
||||
|
||||
go func() { |
||||
genesisBlock := &IdentityBlock{0, "127.0.0.1", "8080", 0} |
||||
mutex.Lock() |
||||
IDC.Identities = append(IDC.Identities, genesisBlock) |
||||
mutex.Unlock() |
||||
|
||||
}() |
||||
} |
||||
|
||||
//IdentityChainHandler handles transactions
|
||||
func (IDC *IdentityChain) IdentityChainHandler(conn net.Conn) { |
||||
fmt.Println("yay") |
||||
} |
||||
func (IDC *IdentityChain) listenOnPort(port string) { |
||||
listen, err := net.Listen("tcp4", ":"+port) |
||||
defer listen.Close() |
||||
if err != nil { |
||||
IDC.log.Crit("Socket listen port failed", "port", port, "err", err) |
||||
os.Exit(1) |
||||
} |
||||
for { |
||||
conn, err := listen.Accept() |
||||
if err != nil { |
||||
IDC.log.Crit("Error listening on port. Exiting.", "port", port) |
||||
continue |
||||
} |
||||
go IDC.IdentityChainHandler(conn) |
||||
} |
||||
} |
@ -0,0 +1,57 @@ |
||||
package waitnode |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"github.com/simple-rules/harmony-benchmark/log" |
||||
) |
||||
|
||||
type address struct { |
||||
IP string |
||||
Port string |
||||
} |
||||
|
||||
//WaitNode is for nodes waiting to join consensus
|
||||
type WaitNode struct { |
||||
Address *address |
||||
Worker string |
||||
ID int |
||||
log log.Logger |
||||
} |
||||
|
||||
func (node *WaitNode) doPoW() { |
||||
node.log.Debug("Node with ID %d and IP %s is doing POW", node.ID, node.Address.IP) |
||||
} |
||||
|
||||
// StartServer a server and process the request by a handler.
|
||||
func (node *WaitNode) StartServer(add address) { |
||||
node.log.Debug("Starting waitnode on server %d", "node", node.ID, "port", add.IP) |
||||
node.connectIdentityChain(add.Port) |
||||
} |
||||
|
||||
func (node *WaitNode) connectIdentityChain(port string) { |
||||
fmt.Println("Connecting to identity chain") |
||||
// listen, err := net.Listen("tcp4", ":"+port)
|
||||
// defer listen.Close()
|
||||
// if err != nil {
|
||||
// node.log.Crit("Socket listen port failed", "port", port, "err", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
// for {
|
||||
// conn, err := listen.Accept()
|
||||
// if err != nil {
|
||||
// node.log.Crit("Error listening on port. Exiting.", "port", port)
|
||||
// continue
|
||||
// }
|
||||
// }
|
||||
} |
||||
|
||||
// New Create a new Node
|
||||
func New(address *address, id int) *WaitNode { |
||||
node := WaitNode{} |
||||
node.Address = address |
||||
node.ID = id |
||||
node.Worker = "pow" |
||||
node.log = log.New() |
||||
return &node |
||||
} |
@ -0,0 +1,20 @@ |
||||
package waitnode |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func TestNewNode(test *testing.T) { |
||||
addressNode := &address{IP: "1", Port: "2"} |
||||
ID := 1 |
||||
node := New(addressNode, ID) |
||||
if node.Address == nil { |
||||
test.Error("Address is not initialized for the node") |
||||
} |
||||
if node.ID != 1 { |
||||
test.Error("ID is not initialized for the node") |
||||
} |
||||
if node.Worker != "pow" { |
||||
test.Error("Worker is not initialized for the node") |
||||
} |
||||
} |
Loading…
Reference in new issue