8 "github.com/golang/protobuf/proto"
10 "github.com/bytom/crypto/sha3pool"
11 "github.com/bytom/encoding/blockchain"
12 "github.com/bytom/errors"
15 // Entry is the interface implemented by each addressable unit in a
16 // blockchain: transaction components such as spends, issuances,
17 // outputs, and retirements (among others), plus blockheaders.
18 type Entry interface {
21 // type produces a short human-readable string uniquely identifying
22 // the type of this entry.
25 // writeForHash writes the entry's body for hashing.
26 writeForHash(w io.Writer)
29 var errInvalidValue = errors.New("invalid value")
31 // EntryID computes the identifier of an entry, as the hash of its
32 // body plus some metadata.
33 func EntryID(e Entry) (hash Hash) {
38 // Nil pointer; not the same as nil interface above. (See
39 // https://golang.org/doc/faq#nil_error.)
40 if v := reflect.ValueOf(e); v.Kind() == reflect.Ptr && v.IsNil() {
44 hasher := sha3pool.Get256()
45 defer sha3pool.Put256(hasher)
47 hasher.Write([]byte("entryid:"))
48 hasher.Write([]byte(e.typ()))
49 hasher.Write([]byte{':'})
51 bh := sha3pool.Get256()
52 defer sha3pool.Put256(bh)
56 var innerHash [32]byte
59 hasher.Write(innerHash[:])
65 var byte32zero [32]byte
67 // mustWriteForHash serializes the object c to the writer w, from which
68 // presumably a hash can be extracted.
70 // This function may panic with an error from the underlying writer,
71 // and may produce errors of its own if passed objects whose
72 // hash-serialization formats are not specified. It MUST NOT produce
73 // errors in other cases.
74 func mustWriteForHash(w io.Writer, c interface{}) {
75 if err := writeForHash(w, c); err != nil {
80 func writeForHash(w io.Writer, c interface{}) error {
81 switch v := c.(type) {
83 _, err := w.Write([]byte{v})
84 return errors.Wrap(err, "writing byte for hash")
86 _, err := blockchain.WriteVarint63(w, v)
87 return errors.Wrapf(err, "writing uint64 (%d) for hash", v)
89 _, err := blockchain.WriteVarstr31(w, v)
90 return errors.Wrapf(err, "writing []byte (len %d) for hash", len(v))
92 _, err := blockchain.WriteVarstrList(w, v)
93 return errors.Wrapf(err, "writing [][]byte (len %d) for hash", len(v))
95 _, err := blockchain.WriteVarstr31(w, []byte(v))
96 return errors.Wrapf(err, "writing string (len %d) for hash", len(v))
99 _, err := w.Write(byte32zero[:])
100 return errors.Wrap(err, "writing nil *Hash for hash")
102 _, err := w.Write(v.Bytes())
103 return errors.Wrap(err, "writing *Hash for hash")
106 _, err := w.Write(byte32zero[:])
107 return errors.Wrap(err, "writing nil *AssetID for hash")
109 _, err := w.Write(v.Bytes())
110 return errors.Wrap(err, "writing *AssetID for hash")
112 _, err := v.WriteTo(w)
113 return errors.Wrap(err, "writing Hash for hash")
115 _, err := v.WriteTo(w)
116 return errors.Wrap(err, "writing AssetID for hash")
119 // The two container types in the spec (List and Struct)
120 // correspond to slices and structs in Go. They can't be
121 // handled with type assertions, so we must use reflect.
122 switch v := reflect.ValueOf(c); v.Kind() {
128 return writeForHash(w, elem.Interface())
131 if _, err := blockchain.WriteVarint31(w, uint64(l)); err != nil {
132 return errors.Wrapf(err, "writing slice (len %d) for hash", l)
134 for i := 0; i < l; i++ {
136 if !c.CanInterface() {
137 return errInvalidValue
139 if err := writeForHash(w, c.Interface()); err != nil {
140 return errors.Wrapf(err, "writing slice element %d for hash", i)
147 for i := 0; i < typ.NumField(); i++ {
149 if !c.CanInterface() {
150 return errInvalidValue
152 if err := writeForHash(w, c.Interface()); err != nil {
155 return errors.Wrapf(err, "writing struct field %d (%s.%s) for hash", i, t.Name(), f.Name)
161 return errors.Wrap(fmt.Errorf("bad type %T", c))