当前位置: 首页>>代码示例>>Golang>>正文


Golang rlp.Encode函数代码示例

本文整理汇总了Golang中github.com/ethereum/go-ethereum/rlp.Encode函数的典型用法代码示例。如果您正苦于以下问题:Golang Encode函数的具体用法?Golang Encode怎么用?Golang Encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: EncodeRLP

// EncodeRLP is a specialized encoder for hashOrNumber to encode only one of the
// two contained union fields.
func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
	if hn.Hash == (common.Hash{}) {
		return rlp.Encode(w, hn.Number)
	}
	if hn.Number != 0 {
		return fmt.Errorf("both origin hash (%x) and number (%d) provided", hn.Hash, hn.Number)
	}
	return rlp.Encode(w, hn.Hash)
}
开发者ID:NikonMcFly,项目名称:go-ethereum,代码行数:11,代码来源:protocol.go

示例2: EncodeRLP

func (self Block) EncodeRLP(w io.Writer) error {
	return rlp.Encode(w, extblock{
		Header: self.header,
		Txs:    self.transactions,
		Uncles: self.uncles,
	})
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:7,代码来源:block.go

示例3: store

func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
	// Don't store hashes or empty nodes.
	if _, isHash := n.(hashNode); n == nil || isHash {
		return n, nil
	}
	// Generate the RLP encoding of the node
	h.tmp.Reset()
	if err := rlp.Encode(h.tmp, n); err != nil {
		panic("encode error: " + err.Error())
	}
	if h.tmp.Len() < 32 && !force {
		return n, nil // Nodes smaller than 32 bytes are stored inside their parent
	}
	// Larger nodes are replaced by their hash and stored in the database.
	hash, _ := n.cache()
	if hash == nil {
		h.sha.Reset()
		h.sha.Write(h.tmp.Bytes())
		hash = hashNode(h.sha.Sum(nil))
	}
	if db != nil {
		return hash, db.Put(hash, h.tmp.Bytes())
	}
	return hash, nil
}
开发者ID:Codzart,项目名称:go-ethereum,代码行数:25,代码来源:trie.go

示例4: EncodeRLP

func (self *ReceiptForStorage) EncodeRLP(w io.Writer) error {
	storageLogs := make([]*state.LogForStorage, len(self.logs))
	for i, log := range self.logs {
		storageLogs[i] = (*state.LogForStorage)(log)
	}
	return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, storageLogs, self.GasUsed})
}
开发者ID:nellyk,项目名称:go-ethereum,代码行数:7,代码来源:receipt.go

示例5: EncodeRLP

// EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
// into an RLP stream.
func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
	logs := make([]*vm.LogForStorage, len(r.Logs))
	for i, log := range r.Logs {
		logs[i] = (*vm.LogForStorage)(log)
	}
	return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.TxHash, r.ContractAddress, logs, r.GasUsed})
}
开发者ID:j4ustin,项目名称:go-ethereum,代码行数:9,代码来源:receipt.go

示例6: EncodeRLP

func (self *Value) EncodeRLP(w io.Writer) error {
	if self == nil {
		w.Write(rlp.EmptyList)
		return nil
	} else {
		return rlp.Encode(w, self.Val)
	}
}
开发者ID:j4ustin,项目名称:go-ethereum,代码行数:8,代码来源:value.go

示例7: EncodeRLP

func (b *StorageBlock) EncodeRLP(w io.Writer) error {
	return rlp.Encode(w, storageblock{
		Header: b.header,
		Txs:    b.transactions,
		Uncles: b.uncles,
		TD:     b.Td,
	})
}
开发者ID:nilcons-contrib,项目名称:go-ethereum,代码行数:8,代码来源:block.go

示例8: Size

func (tx *Transaction) Size() common.StorageSize {
	if size := tx.size.Load(); size != nil {
		return size.(common.StorageSize)
	}
	c := writeCounter(0)
	rlp.Encode(&c, &tx.data)
	tx.size.Store(common.StorageSize(c))
	return common.StorageSize(c)
}
开发者ID:nellyk,项目名称:go-ethereum,代码行数:9,代码来源:transaction.go

示例9: Size

func (b *Block) Size() common.StorageSize {
	if size := b.size.Load(); size != nil {
		return size.(common.StorageSize)
	}
	c := writeCounter(0)
	rlp.Encode(&c, b)
	b.size.Store(common.StorageSize(c))
	return common.StorageSize(c)
}
开发者ID:NikonMcFly,项目名称:go-ethereum,代码行数:9,代码来源:block.go

示例10: DeriveSha

func DeriveSha(list DerivableList) common.Hash {
	keybuf := new(bytes.Buffer)
	trie := new(trie.Trie)
	for i := 0; i < list.Len(); i++ {
		keybuf.Reset()
		rlp.Encode(keybuf, uint(i))
		trie.Update(keybuf.Bytes(), list.GetRlp(i))
	}
	return trie.Hash()
}
开发者ID:Codzart,项目名称:go-ethereum,代码行数:10,代码来源:derive_sha.go

示例11: EncodeRLP

func (self *LogForStorage) EncodeRLP(w io.Writer) error {
	return rlp.Encode(w, []interface{}{
		self.Address,
		self.Topics,
		self.Data,
		self.Number,
		self.TxHash,
		self.TxIndex,
		self.BlockHash,
		self.Index,
	})
}
开发者ID:nellyk,项目名称:go-ethereum,代码行数:12,代码来源:log.go

示例12: sealEIP8

func sealEIP8(msg interface{}, h *encHandshake) ([]byte, error) {
	buf := new(bytes.Buffer)
	if err := rlp.Encode(buf, msg); err != nil {
		return nil, err
	}
	// pad with random amount of data. the amount needs to be at least 100 bytes to make
	// the message distinguishable from pre-EIP-8 handshakes.
	pad := padSpace[:mrand.Intn(len(padSpace)-100)+100]
	buf.Write(pad)
	prefix := make([]byte, 2)
	binary.BigEndian.PutUint16(prefix, uint16(buf.Len()+eciesOverhead))

	enc, err := ecies.Encrypt(rand.Reader, h.remotePub, buf.Bytes(), nil, prefix)
	return append(prefix, enc...), err
}
开发者ID:Codzart,项目名称:go-ethereum,代码行数:15,代码来源:rlpx.go

示例13: encodePacket

func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte, error) {
	b := new(bytes.Buffer)
	b.Write(headSpace)
	b.WriteByte(ptype)
	if err := rlp.Encode(b, req); err != nil {
		glog.V(logger.Error).Infoln("error encoding packet:", err)
		return nil, err
	}
	packet := b.Bytes()
	sig, err := crypto.Sign(crypto.Sha3(packet[headSize:]), priv)
	if err != nil {
		glog.V(logger.Error).Infoln("could not sign packet:", err)
		return nil, err
	}
	copy(packet[macSize:], sig)
	// add the hash to the front. Note: this doesn't protect the
	// packet in any way. Our public key will be part of this hash in
	// The future.
	copy(packet, crypto.Sha3(packet[macSize:]))
	return packet, nil
}
开发者ID:ruflin,项目名称:go-ethereum,代码行数:21,代码来源:udp.go

示例14: TestBodyStorage

// Tests block body storage and retrieval operations.
func TestBodyStorage(t *testing.T) {
	db, _ := ethdb.NewMemDatabase()

	// Create a test body to move around the database and make sure it's really new
	body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}

	hasher := sha3.NewKeccak256()
	rlp.Encode(hasher, body)
	hash := common.BytesToHash(hasher.Sum(nil))

	if entry := GetBody(db, hash); entry != nil {
		t.Fatalf("Non existent body returned: %v", entry)
	}
	// Write and verify the body in the database
	if err := WriteBody(db, hash, body); err != nil {
		t.Fatalf("Failed to write body into database: %v", err)
	}
	if entry := GetBody(db, hash); entry == nil {
		t.Fatalf("Stored body not found")
	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
	}
	if entry := GetBodyRLP(db, hash); entry == nil {
		t.Fatalf("Stored body RLP not found")
	} else {
		hasher := sha3.NewKeccak256()
		hasher.Write(entry)

		if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
			t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
		}
	}
	// Delete the body and verify the execution
	DeleteBody(db, hash)
	if entry := GetBody(db, hash); entry != nil {
		t.Fatalf("Deleted body returned: %v", entry)
	}
}
开发者ID:General-Beck,项目名称:go-ethereum,代码行数:39,代码来源:chain_util_test.go

示例15: store

func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
	// Don't store hashes or empty nodes.
	if _, isHash := n.(hashNode); n == nil || isHash {
		return n, nil
	}
	h.tmp.Reset()
	if err := rlp.Encode(h.tmp, n); err != nil {
		panic("encode error: " + err.Error())
	}
	if h.tmp.Len() < 32 && !force {
		// Nodes smaller than 32 bytes are stored inside their parent.
		return n, nil
	}
	// Larger nodes are replaced by their hash and stored in the database.
	h.sha.Reset()
	h.sha.Write(h.tmp.Bytes())
	key := hashNode(h.sha.Sum(nil))
	if db != nil {
		err := db.Put(key, h.tmp.Bytes())
		return key, err
	}
	return key, nil
}
开发者ID:Xiaoyang-Zhu,项目名称:go-ethereum,代码行数:23,代码来源:trie.go


注:本文中的github.com/ethereum/go-ethereum/rlp.Encode函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。