Example #1
0
func TestBinaryDecode(t *testing.T) {

	privAccount := GenPrivAccount()
	pubKey := privAccount.PubKey
	privKey := privAccount.PrivKey

	msg := CRandBytes(128)
	sig := privKey.Sign(msg)
	t.Logf("msg: %X, sig: %X", msg, sig)

	buf, n, err := new(bytes.Buffer), new(int64), new(error)
	binary.WriteBinary(sig, buf, n, err)
	if *err != nil {
		t.Fatalf("Failed to write Signature: %v", err)
	}

	if len(buf.Bytes()) != ed25519.SignatureSize+3 {
		// 1 byte TypeByte, 2 bytes length, 64 bytes signature bytes
		t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
	}
	if buf.Bytes()[0] != SignatureTypeEd25519 {
		t.Fatalf("Unexpected signature type byte")
	}

	sig2, ok := binary.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
	if !ok || *err != nil {
		t.Fatalf("Failed to read Signature: %v", err)
	}

	// Test the signature
	if !pubKey.VerifyBytes(msg, sig2) {
		t.Errorf("Account message signature verification failed")
	}
}
Example #2
0
func TestReadWrite(t *testing.T) {
	height, round := uint(1), uint(2)
	_, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)

	// Make a POL with +2/3 votes.
	blockHash := RandBytes(32)
	pol := &POL{
		Height: height, Round: round, BlockHash: blockHash,
		Votes: make([]POLVoteSignature, valSet.Size()),
	}
	voteProto := &types.Vote{
		Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash,
	}
	for i := 0; i < 7; i++ {
		signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
	}

	// Write it to a buffer.
	buf, n, err := new(bytes.Buffer), new(int64), new(error)
	binary.WriteBinary(pol, buf, n, err)
	if *err != nil {
		t.Fatalf("Failed to write POL: %v", *err)
	}

	// Read from buffer.
	pol2 := binary.ReadBinary(&POL{}, buf, n, err).(*POL)
	if *err != nil {
		t.Fatalf("Failed to read POL: %v", *err)
	}

	// Check that validation succeeds.
	if err := pol2.Verify(valSet); err != nil {
		t.Errorf("POL.Verify() failed: %v", err)
	}
}
Example #3
0
func peerHandshake(conn net.Conn, ourNodeInfo *types.NodeInfo) (*types.NodeInfo, error) {
	var peerNodeInfo = new(types.NodeInfo)
	var wg sync.WaitGroup
	var err1 error
	var err2 error
	wg.Add(2)
	go func() {
		var n int64
		binary.WriteBinary(ourNodeInfo, conn, &n, &err1)
		wg.Done()
	}()
	go func() {
		var n int64
		binary.ReadBinary(peerNodeInfo, conn, &n, &err2)
		log.Info("Peer handshake", "peerNodeInfo", peerNodeInfo)
		wg.Done()
	}()
	wg.Wait()
	if err1 != nil {
		return nil, err1
	}
	if err2 != nil {
		return nil, err2
	}
	return peerNodeInfo, nil
}
Example #4
0
// General Convenience
func HashFromBinary(item interface{}) []byte {
	hasher, n, err := sha256.New(), new(int64), new(error)
	binary.WriteBinary(item, hasher, n, err)
	if *err != nil {
		panic(err)
	}
	return hasher.Sum(nil)
}
Example #5
0
// Writes next msgPacket to w.
// Not goroutine-safe
func (ch *Channel) writeMsgPacketTo(w io.Writer) (n int64, err error) {
	packet := ch.nextMsgPacket()
	binary.WriteByte(packetTypeMsg, w, &n, &err)
	binary.WriteBinary(packet, w, &n, &err)
	if err != nil {
		ch.recentlySent += n
	}
	return
}
Example #6
0
func (s *State) Save() {
	s.accounts.Save()
	s.validatorInfos.Save()
	buf, n, err := new(bytes.Buffer), new(int64), new(error)
	binary.WriteUvarint(s.LastBlockHeight, buf, n, err)
	binary.WriteByteSlice(s.LastBlockHash, buf, n, err)
	binary.WriteBinary(s.LastBlockParts, buf, n, err)
	binary.WriteTime(s.LastBlockTime, buf, n, err)
	binary.WriteBinary(s.BondedValidators, buf, n, err)
	binary.WriteBinary(s.LastBondedValidators, buf, n, err)
	binary.WriteBinary(s.UnbondingValidators, buf, n, err)
	binary.WriteByteSlice(s.accounts.Hash(), buf, n, err)
	binary.WriteByteSlice(s.validatorInfos.Hash(), buf, n, err)
	if *err != nil {
		panic(*err)
	}
	s.DB.Set(stateKey, buf.Bytes())
}
Example #7
0
// NOTE: hash is nil if required fields are missing.
func (h *Header) Hash() []byte {
	if len(h.StateHash) == 0 {
		return nil
	}

	buf := new(bytes.Buffer)
	hasher, n, err := sha256.New(), new(int64), new(error)
	binary.WriteBinary(h, buf, n, err)
	if *err != nil {
		panic(err)
	}
	hasher.Write(buf.Bytes())
	hash := hasher.Sum(nil)
	return hash
}
Example #8
0
func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
	binary.WriteBinary(o.(*ValidatorInfo), w, n, err)
}
Example #9
0
func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
	binary.WriteBinary(o.(*Validator), w, n, err)
}
Example #10
0
func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
	binary.WriteBinary(o.(*Account), w, n, err)
}