本文整理汇总了Golang中github.com/skycoin/encoder.Serialize函数的典型用法代码示例。如果您正苦于以下问题:Golang Serialize函数的具体用法?Golang Serialize怎么用?Golang Serialize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Serialize函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: hashInner
// Hashes only the Transaction Inputs & Outputs
func (self *Transaction) hashInner() SHA256 {
// WARNING -- using encoder to calculate hash is prone to error.
// Encoder connot be considered stable.
b1 := encoder.Serialize(self.In)
b2 := encoder.Serialize(self.Out)
b3 := append(b1, b2...)
return SumSHA256(b3)
}
开发者ID:up4k,项目名称:skycoin,代码行数:9,代码来源:transactions.go
示例2: TestPingMessage
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()
}
开发者ID:up4k,项目名称:skycoin,代码行数:33,代码来源:messages_test.go
示例3: TestGivePeersMessage
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()
}
开发者ID:up4k,项目名称:skycoin,代码行数:32,代码来源:messages_test.go
示例4: TestGetPeersMessage
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()
}
开发者ID:up4k,项目名称:skycoin,代码行数:59,代码来源:messages_test.go
示例5: TestTransactionsSize
func TestTransactionsSize(t *testing.T) {
txns := makeTransactions(t, 10)
size := 0
for _, tx := range txns {
size += len(encoder.Serialize(&tx))
}
assert.NotEqual(t, size, 0)
assert.Equal(t, txns.Size(), size)
}
开发者ID:RagnarDanneskjold,项目名称:skycoin,代码行数:9,代码来源:transactions_test.go
示例6: TestBlockBodySize
func TestBlockBodySize(t *testing.T) {
b := makeNewBlock()
addTransactionToBlock(t, &b)
assert.Equal(t, b.Size(), b.Body.Size())
assert.Equal(t, b.Body.Size(), b.Body.Transactions.Size())
size := 0
for _, x := range b.Body.Transactions {
size += len(encoder.Serialize(&x))
}
assert.NotEqual(t, size, 0)
assert.Equal(t, b.Size(), size)
}
开发者ID:RagnarDanneskjold,项目名称:skycoin,代码行数:12,代码来源:blockchain_test.go
示例7: TestIntroductionMessageHandle
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()
}
开发者ID:up4k,项目名称:skycoin,代码行数:52,代码来源:messages_test.go
示例8: TestGetBlocksMessageHandle
func TestGetBlocksMessageHandle(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
m := NewGetBlocksMessage(uint64(1))
assert.Equal(t, m.LastBlock, uint64(1))
testSimpleMessageHandler(t, d, m)
// Test serialization
m = NewGetBlocksMessage(uint64(106))
b := encoder.Serialize(m)
m2 := GetBlocksMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, *m, m2)
}
开发者ID:up4k,项目名称:skycoin,代码行数:14,代码来源:visor_test.go
示例9: Save
func (self *BlockSigs) Save(filename string) error {
// Convert the Sigs map to an array of element
sigs := make([]BlockSigSerialized, len(self.Sigs))
i := 0
for k, v := range self.Sigs {
sigs[i] = BlockSigSerialized{
BkSeq: k,
Sig: v,
}
i++
}
bss := BlockSigsSerialized{sigs}
data := encoder.Serialize(bss)
return util.SaveBinary(filename, data, 0644)
}
开发者ID:up4k,项目名称:skycoin,代码行数:15,代码来源:blocksigs.go
示例10: Enc_block
//encode block to bytes
func Enc_block(sb SignedBlock) []byte {
var prefix []byte = make([]byte, 16)
b := encoder.Serialize(sb)
var dln uint64 = uint64(len(b) + 24) //length include prefix
var seq uint64 = uint64(sb.Block.Head.BkSeq)
var chk uint64 = FnvsHash(b)
le_PutUint64(prefix[0:8], dln)
le_PutUint64(prefix[8:16], seq)
le_PutUint64(prefix[16:24], chk)
b = append(prefix[:], b...)
return b
}
开发者ID:RagnarDanneskjold,项目名称:skycoin,代码行数:17,代码来源:blockchain_file.go
示例11: TestPongMessage
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)
}
开发者ID:up4k,项目名称:skycoin,代码行数:16,代码来源:messages_test.go
示例12: TestGiveBlocksMessageHandle
func TestGiveBlocksMessageHandle(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
_, mv := setupVisor()
blocks := []visor.SignedBlock{mv.GetGenesisBlock()}
m := NewGiveBlocksMessage(blocks)
assert.Equal(t, m.Blocks, blocks)
testSimpleMessageHandler(t, d, m)
// Test serialization
bks, err := makeBlocks(mv, 4)
assert.Nil(t, err)
blocks = append(blocks, bks...)
m = NewGiveBlocksMessage(blocks)
b := encoder.Serialize(m)
m2 := GiveBlocksMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, *m, m2)
}
开发者ID:up4k,项目名称:skycoin,代码行数:19,代码来源:visor_test.go
示例13: TestGiveTxnsMessageHandle
func TestGiveTxnsMessageHandle(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
tx := createUnconfirmedTxn()
txns := coin.Transactions{tx.Txn}
m := NewGiveTxnsMessage(txns)
assert.Equal(t, m.Txns, txns)
testSimpleMessageHandler(t, d, m)
// Test serialization
tx = createUnconfirmedTxn()
txns = append(txns, tx.Txn)
tx = createUnconfirmedTxn()
txns = append(txns, tx.Txn)
m = NewGiveTxnsMessage(txns)
assert.Equal(t, len(m.Txns), 3)
b := encoder.Serialize(m)
m2 := GiveTxnsMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, *m, m2)
}
开发者ID:up4k,项目名称:skycoin,代码行数:21,代码来源:visor_test.go
示例14: TestAnnounceTxnsMessageHandle
func TestAnnounceTxnsMessageHandle(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
tx := createUnconfirmedTxn()
txns := []coin.SHA256{tx.Txn.Hash()}
m := NewAnnounceTxnsMessage(txns)
assert.Equal(t, m.Txns, txns)
testSimpleMessageHandler(t, d, m)
// Test serialization
tx = createUnconfirmedTxn()
txns = append(txns, tx.Txn.Hash())
tx = createUnconfirmedTxn()
txns = append(txns, tx.Txn.Hash())
m = NewAnnounceTxnsMessage(txns)
assert.Equal(t, len(m.Txns), 3)
b := encoder.Serialize(m)
m2 := AnnounceTxnsMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, *m, m2)
}
开发者ID:up4k,项目名称:skycoin,代码行数:21,代码来源:visor_test.go
示例15: Save
func (self *SerializedBlockchain) Save(filename string) error {
data := encoder.Serialize(self)
return util.SaveBinary(filename, data, 0644)
}
开发者ID:RagnarDanneskjold,项目名称:skycoin,代码行数:4,代码来源:serialization.go
示例16: Hash
func (self *BlockHeader) Hash() SHA256 {
b1 := encoder.Serialize(*self)
return SumDoubleSHA256(b1)
}
开发者ID:up4k,项目名称:skycoin,代码行数:4,代码来源:blockchain.go
示例17: Bytes
func (self *BlockBody) Bytes() []byte {
return encoder.Serialize(*self)
}
开发者ID:up4k,项目名称:skycoin,代码行数:3,代码来源:blockchain.go
示例18: SnapshotHash
// Returns hash of UxBody + UxHead
func (self *UxOut) SnapshotHash() SHA256 {
b1 := encoder.Serialize(self.Body) //body
b2 := encoder.Serialize(self.Head) //time, bkseq
b3 := append(b1, b2...)
return SumSHA256(b3)
}
开发者ID:RagnarDanneskjold,项目名称:skycoin,代码行数:7,代码来源:outputs.go
示例19: Hash
func (self *UxBody) Hash() SHA256 {
return SumSHA256(encoder.Serialize(self))
}
开发者ID:RagnarDanneskjold,项目名称:skycoin,代码行数:3,代码来源:outputs.go
示例20:
}
}
}()
n, e = encoder.DeserializeRawToValue(msg, v)
return
}
// Packgs a Message into []byte containing length, id and data
var encodeMessage = func(msg Message) []byte {
t := reflect.ValueOf(msg).Elem().Type()
msgId, succ := MessageIdMap[t]
if !succ {
txt := "Attempted to serialize message struct not in MessageIdMap: %v"
log.Panicf(txt, msg)
}
bMsg := encoder.Serialize(msg)
// message length
bLen := encoder.SerializeAtomic(uint32(len(bMsg) + len(msgId)))
m := make([]byte, 0)
m = append(m, bLen...) // length prefix
m = append(m, msgId[:]...) // message id
m = append(m, bMsg...) // message bytes
return m
}
// Sends []byte over a net.Conn
var sendByteMessage = func(conn net.Conn, msg []byte,
timeout time.Duration) error {
deadline := time.Time{}
if timeout != 0 {
开发者ID:keepwalking1234,项目名称:skycoin,代码行数:31,代码来源:dispatcher.go
注:本文中的github.com/skycoin/encoder.Serialize函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论