// 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) }
// 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}) }
func (b *Block) EncodeRLP(w io.Writer) error { return rlp.Encode(w, extblock{ Header: b.header, Txs: b.transactions, Uncles: b.uncles, }) }
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 }
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}) }
func (self *Value) EncodeRLP(w io.Writer) error { if self == nil { w.Write(rlp.EmptyList) return nil } else { return rlp.Encode(w, self.Val) } }
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, }) }
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) }
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) }
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() }
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, }) }
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 }
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 }
// 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) } }
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 }
func (tx *Transaction) EncodeRLP(w io.Writer) error { return rlp.Encode(w, &tx.data) }
// EncodeRLP encodes a full node into the consensus RLP format. func (n fullNode) EncodeRLP(w io.Writer) error { return rlp.Encode(w, n.Children) }
func rlpHash(x interface{}) (h common.Hash) { hw := sha3.NewKeccak256() rlp.Encode(hw, x) hw.Sum(h[:0]) return h }
func (l *Log) EncodeRLP(w io.Writer) error { return rlp.Encode(w, []interface{}{l.Address, l.Topics, l.Data}) }
func (self *Receipt) EncodeRLP(w io.Writer) error { return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs}) }
func (self *Log) EncodeRLP(w io.Writer) error { return rlp.Encode(w, []interface{}{self.Address, self.Topics, self.Data}) }
// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt // into an RLP stream. func (r *Receipt) EncodeRLP(w io.Writer) error { return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs}) }
// EncodeRLP implements rlp.Encoder. func (c *StateObject) EncodeRLP(w io.Writer) error { return rlp.Encode(w, c.data) }