parent
c7b63df577
commit
6632cea6de
@ -1,82 +0,0 @@ |
||||
package waitnode |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/sha256" |
||||
"encoding/gob" |
||||
"log" |
||||
|
||||
"github.com/simple-rules/harmony-benchmark/p2p" |
||||
"github.com/simple-rules/harmony-benchmark/utils" |
||||
) |
||||
|
||||
//WaitNode is for nodes waiting to join consensus
|
||||
type WaitNode struct { |
||||
Peer p2p.Peer |
||||
ID uint16 |
||||
SeedPeers p2p.Peer |
||||
} |
||||
|
||||
// StartServer a server and process the request by a handler.
|
||||
func (node *WaitNode) StartServer() { |
||||
log.Printf("Starting waitnode on server %s and port %s", node.Peer.Ip, node.Peer.Port) |
||||
} |
||||
|
||||
// //ConnectIdentityChain connects to identity chain
|
||||
// func (node *WaitNode) ConnectIdentityChain(peer p2p.Peer) {
|
||||
// pow := NewProofOfWork(10)
|
||||
// nonce := pow.Run()
|
||||
// if pow.FinalNonce != uint32(nonce) {
|
||||
// fmt.Println("Something wrong with POW")
|
||||
// }
|
||||
// p2p.SendMessage(peer, identity.ConstructIdentityMessage(identity.REGISTER, node.SerializeWaitNode()))
|
||||
// }
|
||||
|
||||
//Constructs node-id by hashing the IP.
|
||||
func calculateHash(num string) []byte { |
||||
var hashes [][]byte |
||||
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(num)) |
||||
hash := sha256.Sum256(bytes.Join(hashes, []byte{})) |
||||
return hash[:] |
||||
} |
||||
|
||||
// //SerializePOW serializes the node
|
||||
// func SerializePOW(pow ProofOfWork) []byte {
|
||||
// var result bytes.Buffer
|
||||
// encoder := gob.NewEncoder(&pow)
|
||||
// err := encoder.Encode(pow)
|
||||
// if err != nil {
|
||||
// log.Panic(err.Error())
|
||||
// }
|
||||
// return pow.Bytes()
|
||||
// }
|
||||
|
||||
//SerializeWaitNode serializes the node
|
||||
func (node *WaitNode) SerializeWaitNode() []byte { |
||||
var result bytes.Buffer |
||||
encoder := gob.NewEncoder(&result) |
||||
err := encoder.Encode(node) |
||||
if err != nil { |
||||
log.Panic(err.Error()) |
||||
} |
||||
return result.Bytes() |
||||
} |
||||
|
||||
// DeserializeWaitNode deserializes the node
|
||||
func DeserializeWaitNode(d []byte) *WaitNode { |
||||
var wn WaitNode |
||||
decoder := gob.NewDecoder(bytes.NewReader(d)) |
||||
err := decoder.Decode(&wn) |
||||
if err != nil { |
||||
log.Panic(err) |
||||
} |
||||
return &wn |
||||
} |
||||
|
||||
// New Create a new Node
|
||||
func New(Peer p2p.Peer) *WaitNode { |
||||
node := WaitNode{} |
||||
node.Peer = Peer |
||||
node.ID = utils.GetUniqueIdFromPeer(Peer) |
||||
return &node |
||||
} |
@ -1,18 +0,0 @@ |
||||
package waitnode |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/simple-rules/harmony-benchmark/p2p" |
||||
) |
||||
|
||||
func TestNewNode(test *testing.T) { |
||||
p := p2p.Peer{Ip: "127.0.0.1", Port: "8080"} |
||||
wn := New(p) |
||||
b := wn.SerializeWaitNode() |
||||
wnd := DeserializeWaitNode(b) |
||||
if *wn != *wnd { |
||||
test.Error("Serialization is not working") |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue