github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/consense/dpoa/utils.go (about)

     1  package dpoa
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/sha256"
     6  	"crypto/sha512"
     7  	"encoding/binary"
     8  	"encoding/json"
     9  	"errors"
    10  	"fmt"
    11  	"math"
    12  
    13  	"github.com/sixexorg/magnetic-ring/common"
    14  
    15  	//"github.com/sixexorg/magnetic-ring/core/signature"
    16  	//"github.com/sixexorg/magnetic-ring/consensus/vbft/config"
    17  	//"github.com/ontio/ontology-crypto/vrf"
    18  	//"github.com/ontio/ontology-crypto/keypair"
    19  	"github.com/sixexorg/magnetic-ring/consense/dpoa/comm"
    20  	"github.com/sixexorg/magnetic-ring/crypto"
    21  )
    22  
    23  func hashData(data []byte) common.Hash {
    24  	t := sha256.Sum256(data)
    25  	f := sha256.Sum256(t[:])
    26  	return common.Hash(f)
    27  }
    28  
    29  type Partions struct {
    30  	NoPartins   []string
    31  	HasPations  []string
    32  	Fails       []string
    33  	AllPartions []string
    34  }
    35  
    36  type seedData struct {
    37  	BlockNum        uint64      `json:"block_num"`
    38  	PrevBlockSigner string      `json:"prev_block_proposer"` // TODO: change to NodeID
    39  	BlockRoot       common.Hash `json:"block_root"`
    40  	VrfValue        []byte      `json:"vrf_value"`
    41  }
    42  
    43  func getParticipantSelectionSeed(block *comm.Block) comm.VRFValue {
    44  	data, err := json.Marshal(&seedData{
    45  		BlockNum:        block.GetBlockNum() + 1,
    46  		PrevBlockSigner: block.GetProposer(),
    47  		BlockRoot:       block.Block.Header.BlockRoot,
    48  		VrfValue:        block.GetVrfValue(),
    49  	})
    50  	if err != nil {
    51  		return comm.VRFValue{}
    52  	}
    53  
    54  	t := sha512.Sum512(data)
    55  	f := sha512.Sum512(t[:])
    56  	return comm.VRFValue(f)
    57  }
    58  
    59  func CheckSigners(pmsg *comm.ViewtimeoutMsg, fromid string) error {
    60  	pb, _ := json.Marshal(pmsg.RawData)
    61  
    62  	buf, err := common.Hex2Bytes(fromid)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	pub, _ := crypto.UnmarshalPubkey(buf)
    68  	if _, err := pub.Verify(pb, pmsg.Signature); err != nil {
    69  		return err
    70  	}
    71  	//pub, _ := vconfig.Pubkey(fromid)
    72  	//if err := signature.Verify(pub, pb, pmsg.Signature); err != nil{
    73  	//	return  err
    74  	//}
    75  
    76  	return nil
    77  }
    78  
    79  func computeVrf(sk crypto.PrivateKey, blkNum uint64, prevVrf []byte) ([]byte, []byte, error) {
    80  	data, err := json.Marshal(&comm.VrfData{
    81  		BlockNum: blkNum,
    82  		PrevVrf:  prevVrf,
    83  	})
    84  	if err != nil {
    85  		return nil, nil, fmt.Errorf("computeVrf failed to marshal vrfData: %s", err)
    86  	}
    87  	//fmt.Println("computeVrfcomputeVrfcomputeVrfcomputeVrfcomputeVrfcomputeVrfcomputeVrfcomputeVrf",data)
    88  	hash, proof := sk.Evaluate(data)
    89  
    90  	return hash[:], proof, nil
    91  }
    92  
    93  func GetIndex(arr []string, p string) (int, error) {
    94  	for k, v := range arr {
    95  		if p == v {
    96  			return k, nil
    97  		}
    98  	}
    99  	return -1, errors.New(fmt.Sprintf("GetIndex arr:%v p:%v", arr, p))
   100  }
   101  
   102  func GetNode(arr []string, index int) (string, error) {
   103  	for k, v := range arr {
   104  		if k == index {
   105  			return v, nil
   106  		}
   107  	}
   108  	return "", errors.New(fmt.Sprintf("GetIndex arr:%v p:%v", arr, index))
   109  }
   110  
   111  func calcParticipant(vrf comm.VRFValue, dposTable []uint32, k uint32) uint32 {
   112  	var v1, v2 uint32
   113  	bIdx := k / 8
   114  	bits1 := k % 8
   115  	bits2 := 8 + bits1 // L - 8 + bits1
   116  	if k >= 512 {
   117  		return math.MaxUint32
   118  	}
   119  	// FIXME:
   120  	// take 16bits random variable from vrf, if len(dposTable) is not power of 2,
   121  	// this algorithm will break the fairness of vrf. to be fixed
   122  	v1 = uint32(vrf[bIdx]) >> bits1
   123  	if bIdx+1 < uint32(len(vrf)) {
   124  		v2 = uint32(vrf[bIdx+1])
   125  	} else {
   126  		v2 = uint32(vrf[0])
   127  	}
   128  
   129  	v2 = v2 & ((1 << bits2) - 1)
   130  	v := (v2 << (8 - bits1)) + v1
   131  	v = v % uint32(len(dposTable))
   132  	return dposTable[v]
   133  }
   134  
   135  func calcParticipantPeers(vrf comm.VRFValue, dposTable []string) []string {
   136  	return dposTable
   137  }
   138  
   139  func int2Byte(num interface{}) []byte {
   140  	var buffer bytes.Buffer
   141  	if err := binary.Write(&buffer, binary.BigEndian, num); err != nil {
   142  		return nil
   143  	}
   144  
   145  	return buffer.Bytes()
   146  }
   147  
   148  func byte2Int(data []byte) uint32 {
   149  	var buffer bytes.Buffer
   150  	_, err := buffer.Write(data)
   151  	if err != nil {
   152  		return 0
   153  	}
   154  	var i uint16
   155  	err = binary.Read(&buffer, binary.BigEndian, &i)
   156  	if err != nil {
   157  		return 0
   158  	}
   159  	return uint32(i)
   160  }