Merge branch 'master' of github.com:simple-rules/harmony-benchmark

pull/61/head
Rongjian Lan 6 years ago
commit f286f71a83
  1. 43
      identitymanage/identitymanage.go
  2. 81
      pow/pow.go
  3. 28
      runwait/run_wait.go
  4. 25
      waitnode/wait_node.go

@ -0,0 +1,43 @@
package identitymanage
// import (
// "bytes"
// "encoding/binary"
// "log"
// "github.com/dedis/kyber"
// )
// // Consensus data containing all info related to one round of consensus process
// type Identity struct {
// priKey kyber.Scalar
// pubKey kyber.Point
// Log log.Logger
// }
// // Construct the response message to send to leader (assumption the consensus data is already verified)
// func (identity *Identity) registerIdentity(msgType proto_consensus.MessageType, response kyber.Scalar) []byte {
// buffer := bytes.NewBuffer([]byte{})
// // 4 byte consensus id
// fourBytes := make([]byte, 4)
// binary.BigEndian.PutUint32(fourBytes, consensus.consensusId)
// buffer.Write(fourBytes)
// // 32 byte block hash
// buffer.Write(consensus.blockHash[:32])
// // 2 byte validator id
// twoBytes := make([]byte, 2)
// binary.BigEndian.PutUint16(twoBytes, consensus.nodeId)
// buffer.Write(twoBytes)
// // 32 byte of response
// response.MarshalTo(buffer)
// // 64 byte of signature on previous data
// signature := consensus.signMessage(buffer.Bytes())
// buffer.Write(signature)
// return proto_identity.ConstructIdentityMessage(msgType, buffer.Bytes())
// }

@ -0,0 +1,81 @@
package pow
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"fmt"
"math"
"math/big"
)
var (
maxNonce = math.MaxUint32
)
const targetBits = 24
// ProofOfWork represents a proof-of-work
type ProofOfWork struct {
Challenge uint32
target *big.Int
FinalNonce uint32
}
// NewProofOfWork builds and returns a ProofOfWork
func NewProofOfWork(c uint32) *ProofOfWork {
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
pow := &ProofOfWork{Challenge: c, target: target, FinalNonce: 0}
return pow
}
func (pow *ProofOfWork) prepareData(nonce uint32) []byte {
challenge := make([]byte, 4)
binary.LittleEndian.PutUint32(challenge, pow.Challenge)
nonceB := make([]byte, 4)
binary.LittleEndian.PutUint32(nonceB, nonce)
data := bytes.Join(
[][]byte{
challenge,
nonceB,
},
[]byte{},
)
return data
}
// Run performs a proof-of-work
func (pow *ProofOfWork) Run() int {
var hashInt big.Int
var hash [32]byte
nonce := 0
for nonce < maxNonce {
data := pow.prepareData(uint32(nonce))
hash = sha256.Sum256(data)
fmt.Printf("\r%x", hash)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(pow.target) == -1 {
pow.FinalNonce = uint32(nonce)
break
} else {
nonce++
}
}
fmt.Print("\n\n")
return nonce
}
// Validate validates block's PoW
func (pow *ProofOfWork) Validate(nonce uint32) bool {
var hashInt big.Int
data := pow.prepareData(nonce)
hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:])
isValid := hashInt.Cmp(pow.target) == -1
return isValid
}

@ -1,18 +1,18 @@
package main
import (
"flag"
// import (
// "flag"
"github.com/simple-rules/harmony-benchmark/p2p"
"github.com/simple-rules/harmony-benchmark/waitnode"
)
// "github.com/simple-rules/harmony-benchmark/p2p"
// "github.com/simple-rules/harmony-benchmark/waitnode"
// )
func main() {
ip := flag.String("ip", "127.0.0.0", "IP of the node")
port := flag.String("port", "8080", "port of the node")
flag.Parse()
peer := p2p.Peer{Ip: *ip, Port: *port}
idcpeer := p2p.Peer{Ip: "localhost", Port: "9000"} //Hardcoded here.
node := waitnode.New(peer)
node.ConnectIdentityChain(idcpeer)
}
// func main() {
// ip := flag.String("ip", "127.0.0.0", "IP of the node")
// port := flag.String("port", "8080", "port of the node")
// flag.Parse()
// peer := p2p.Peer{Ip: *ip, Port: *port}
// idcpeer := p2p.Peer{Ip: "localhost", Port: "9000"} //Hardcoded here.
// node := waitnode.New(peer)
// node.ConnectIdentityChain(idcpeer)
// }

@ -7,7 +7,6 @@ import (
"log"
"github.com/simple-rules/harmony-benchmark/p2p"
"github.com/simple-rules/harmony-benchmark/proto/identity"
"github.com/simple-rules/harmony-benchmark/utils"
)
@ -23,10 +22,15 @@ 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) {
p2p.SendMessage(peer, identity.ConstructIdentityMessage(identity.REGISTER, node.SerializeWaitNode()))
}
// //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 {
@ -36,6 +40,17 @@ func calculateHash(num string) []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

Loading…
Cancel
Save