예제 #1
0
//decode block to bytes
func Dec_block(b []byte) (SignedBlock, error) {
	if len(b) < 16 {
		log.Panic()
	}

	var dln uint64 = le_Uint64(b[0:8])
	var seq uint64 = le_Uint64(b[8:16])
	var chk uint64 = le_Uint64(b[16:24])

	if dln != uint64(len(b)) {
		log.Panic("Dec_block, length check failed")
	}

	b = b[24:] //cleave off header

	if chk != FnvsHash(b) {
		log.Panic("Dec_block, checksum failed")
	}

	var sb SignedBlock
	err := encoder.DeserializeRaw(b, &sb)
	if err != nil {
		log.Panic("Dec_block, deserialization failed")
	}

	if seq != sb.Block.Head.BkSeq {
		log.Panic("Dec_block, seq mismatch")
	}

	return sb, nil
}
예제 #2
0
func TestPingMessage(t *testing.T) {
	d := newDefaultDaemon()
	defer shutdown(d)
	p := d.Pool.Pool
	m := &PingMessage{}
	testSimpleMessageHandler(t, d, m)

	m.c = messageContext(addr)
	go p.ConnectionWriteLoop(m.c.Conn)
	defer m.c.Conn.Close()
	assert.NotPanics(t, func() { m.Process(d) })
	// A pong message should have been sent
	wait()
	assert.Equal(t, len(p.SendResults), 1)
	if len(p.SendResults) == 0 {
		t.Fatalf("SendResults empty, would block")
	}
	sr := <-p.SendResults
	assert.Equal(t, sr.Connection, m.c.Conn)
	assert.Nil(t, sr.Error)
	_, ok := sr.Message.(*PongMessage)
	assert.True(t, ok)
	assert.False(t, m.c.Conn.LastSent.IsZero())

	// Test serialization
	mm := PingMessage{}
	b := encoder.Serialize(mm)
	m2 := PingMessage{}
	assert.Nil(t, encoder.DeserializeRaw(b, &m2))
	assert.Equal(t, mm, m2)

	gnet.EraseMessages()
}
예제 #3
0
func TransactionDeserialize(b []byte) Transaction {
	t := Transaction{}
	if err := encoder.DeserializeRaw(b, &t); err != nil {
		log.Panic("Failed to deserialize transaction")
	}
	return t
}
예제 #4
0
func TestGivePeersMessage(t *testing.T) {
	d := newDefaultDaemon()
	defer shutdown(d)
	addrs := []string{addr, addrb, "7"}
	peers := make([]*pex.Peer, 3)
	for i, addr := range addrs {
		peers[i] = &pex.Peer{Addr: addr}
	}
	m := NewGivePeersMessage(peers)
	assert.Equal(t, len(m.GetPeers()), 2)
	testSimpleMessageHandler(t, d, m)
	assert.Equal(t, m.GetPeers()[0], addrs[0])
	assert.Equal(t, m.GetPeers()[1], addrs[1])

	// Test disabled
	d.Peers.Config.Disabled = true
	m.Process(d)
	assert.Equal(t, len(d.Peers.Peers.Peerlist), 0)

	// Test serialization
	m = NewGivePeersMessage(peers)
	b := encoder.Serialize(m)
	m2 := GivePeersMessage{}
	assert.Nil(t, encoder.DeserializeRaw(b, &m2))
	assert.Equal(t, *m, m2)

	// Peers should be added to the pex when processed
	d.Peers.Config.Disabled = false
	m.Process(d)
	assert.Equal(t, len(d.Peers.Peers.Peerlist), 2)
	gnet.EraseMessages()
}
예제 #5
0
func TestGetPeersMessage(t *testing.T) {
	d := newDefaultDaemon()
	defer shutdown(d)
	p := d.Pool
	m := NewGetPeersMessage()
	testSimpleMessageHandler(t, d, m)
	d.Peers.Peers.AddPeer(addr)
	q, err := d.Peers.Peers.AddPeer(addrb)
	assert.Nil(t, err)
	q.Private = true
	d.Peers.Config.ReplyCount = 100
	m.c = messageContext(addr)

	// Peers disabled
	d.Peers.Config.Disabled = true
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.True(t, m.c.Conn.LastSent.IsZero())

	// Peers enabled
	d.Peers.Config.Disabled = false
	m.c = messageContext(addr)
	defer m.c.Conn.Close()
	go p.Pool.ConnectionWriteLoop(m.c.Conn)
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 1)
	if len(p.Pool.SendResults) == 0 {
		t.Fatal("SendResults empty, would block")
	}
	sr := <-p.Pool.SendResults
	assert.Nil(t, sr.Error)
	assert.Equal(t, sr.Connection, m.c.Conn)
	msg, ok := sr.Message.(*GivePeersMessage)
	assert.True(t, ok)
	// Private peer should not be included
	ipAddr, err := NewIPAddr(addr)
	assert.Nil(t, err)
	assert.Equal(t, msg.Peers, []IPAddr{ipAddr})
	assert.False(t, m.c.Conn.LastSent.IsZero())

	// If no peers, nothing should happen
	m.c.Conn.LastSent = util.ZeroTime()
	delete(d.Peers.Peers.Peerlist, addr)
	assert.NotPanics(t, func() { m.Process(d) })
	wait()
	assert.Equal(t, len(p.Pool.SendResults), 0)
	assert.True(t, m.c.Conn.LastSent.IsZero())

	// Test serialization
	m = NewGetPeersMessage()
	b := encoder.Serialize(m)
	m2 := GetPeersMessage{}
	assert.Nil(t, encoder.DeserializeRaw(b, &m2))
	assert.Equal(t, *m, m2)

	gnet.EraseMessages()
}
예제 #6
0
func (tx *Transaction) Deserialize(r io.Reader) error {
	d, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}

	if err := encoder.DeserializeRaw(d, tx); err != nil {
		return err
	}
	return nil
}
예제 #7
0
func TestIntroductionMessageHandle(t *testing.T) {
	d := newDefaultDaemon()
	defer shutdown(d)
	mc := messageContext(addr)
	m := NewIntroductionMessage(d.Messages.Mirror, d.Config.Version,
		d.Pool.Pool.Config.Port)

	// Test valid handling
	m.Mirror = d.Messages.Mirror + 1
	err := m.Handle(mc, d)
	assert.Nil(t, err)
	if len(d.messageEvents) == 0 {
		t.Fatalf("messageEvent is empty")
	}
	<-d.messageEvents
	assert.True(t, m.valid)
	m.valid = false

	// Test matching mirror
	m.Mirror = d.Messages.Mirror
	err = m.Handle(mc, d)
	assert.Equal(t, err, DisconnectSelf)
	m.Mirror = d.Messages.Mirror + 1
	assert.False(t, m.valid)

	// Test mismatched d.Config.Version
	m.Version = d.Config.Version + 1
	err = m.Handle(mc, d)
	assert.Equal(t, err, DisconnectInvalidVersion)
	assert.False(t, m.valid)

	// Test serialization
	m = NewIntroductionMessage(d.Messages.Mirror, d.Config.Version,
		d.Pool.Pool.Config.Port)
	b := encoder.Serialize(m)
	m2 := IntroductionMessage{}
	assert.Nil(t, encoder.DeserializeRaw(b, &m2))
	assert.Equal(t, *m, m2)

	// Test already connected
	d.mirrorConnections[m.Mirror] = make(map[string]uint16)
	d.mirrorConnections[m.Mirror][addrIP] = addrPort + 1
	err = m.Handle(mc, d)
	assert.Equal(t, err, DisconnectConnectedTwice)
	delete(d.mirrorConnections, m.Mirror)
	assert.False(t, m.valid)

	for len(d.messageEvents) > 0 {
		<-d.messageEvents
	}
	gnet.EraseMessages()
}
예제 #8
0
func TestPongMessage(t *testing.T) {
	cmsgs := NewMessagesConfig()
	cmsgs.Register()
	m := &PongMessage{}
	// Pongs dont do anything
	assert.Nil(t, m.Handle(messageContext(addr), nil))
	gnet.EraseMessages()

	// Test serialization
	mm := PongMessage{}
	b := encoder.Serialize(mm)
	m2 := PongMessage{}
	assert.Nil(t, encoder.DeserializeRaw(b, &m2))
	assert.Equal(t, mm, m2)

}
예제 #9
0
파일: chain.go 프로젝트: skycoin/skycoin
//creates block from byte array
func BlockFromBytes(data []byte) (Block, error) {
	var b Block
	return b, encoder.DeserializeRaw(data, &b)
}
예제 #10
0
func Deserialize(msg []byte, obj interface{}) error {
	msg = msg[2:] //pop off prefix byte
	err := encoder.DeserializeRaw(msg, obj)
	return err
}