MCPcopy Create free account
hub / github.com/chain/Core / writeForHash

Function writeForHash

protocol/bc/entry.go:81–166  ·  view source on GitHub ↗
(w io.Writer, c interface{})

Source from the content-addressed store, hash-verified

79}
80
81func writeForHash(w io.Writer, c interface{}) error {
82 switch v := c.(type) {
83 case byte:
84 _, err := w.Write([]byte{v})
85 return errors.Wrap(err, "writing byte for hash")
86 case uint64:
87 _, err := blockchain.WriteVarint63(w, v)
88 return errors.Wrapf(err, "writing uint64 (%d) for hash", v)
89 case []byte:
90 _, err := blockchain.WriteVarstr31(w, v)
91 return errors.Wrapf(err, "writing []byte (len %d) for hash", len(v))
92 case [][]byte:
93 _, err := blockchain.WriteVarstrList(w, v)
94 return errors.Wrapf(err, "writing [][]byte (len %d) for hash", len(v))
95 case string:
96 _, err := blockchain.WriteVarstr31(w, []byte(v))
97 return errors.Wrapf(err, "writing string (len %d) for hash", len(v))
98 case *Hash:
99 if v == nil {
100 _, err := w.Write(byte32zero[:])
101 return errors.Wrap(err, "writing nil *Hash for hash")
102 }
103 _, err := w.Write(v.Bytes())
104 return errors.Wrap(err, "writing *Hash for hash")
105 case *AssetID:
106 if v == nil {
107 _, err := w.Write(byte32zero[:])
108 return errors.Wrap(err, "writing nil *AssetID for hash")
109 }
110 _, err := w.Write(v.Bytes())
111 return errors.Wrap(err, "writing *AssetID for hash")
112 case Hash:
113 _, err := v.WriteTo(w)
114 return errors.Wrap(err, "writing Hash for hash")
115 case AssetID:
116 _, err := v.WriteTo(w)
117 return errors.Wrap(err, "writing AssetID for hash")
118 }
119
120 // The two container types in the spec (List and Struct)
121 // correspond to slices and structs in Go. They can't be
122 // handled with type assertions, so we must use reflect.
123 switch v := reflect.ValueOf(c); v.Kind() {
124 case reflect.Ptr:
125 if v.IsNil() {
126 return nil
127 }
128 elem := v.Elem()
129 return writeForHash(w, elem.Interface())
130 case reflect.Slice:
131 l := v.Len()
132 _, err := blockchain.WriteVarint31(w, uint64(l))
133 if err != nil {
134 return errors.Wrapf(err, "writing slice (len %d) for hash", l)
135 }
136 for i := 0; i < l; i++ {
137 c := v.Index(i)
138 if !c.CanInterface() {

Callers 2

ComputeAssetIDMethod · 0.85
mustWriteForHashFunction · 0.85

Calls 4

WriteMethod · 0.45
BytesMethod · 0.45
WriteToMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected