commit
f286f71a83
@ -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)
|
||||
// }
|
||||
|
Loading…
Reference in new issue