MCPcopy
hub / github.com/canopy-network/canopy / BytesToBlockHash

Method BytesToBlockHash

lib/block.go:204–222  ·  view source on GitHub ↗

BytesToBlockHash() converts block bytes into a block hash

(blockBytes []byte)

Source from the content-addressed store, hash-verified

202
203// BytesToBlockHash() converts block bytes into a block hash
204func (x *Block) BytesToBlockHash(blockBytes []byte) (hash []byte, err ErrorI) {
205 // ensure the block isn't empty
206 if blockBytes == nil {
207 // exit with error
208 return nil, ErrNilBlock()
209 }
210 // get the block header field
211 blockHeaderWithHash, e := codec.GetRawProtoField(blockBytes, 1)
212 if e != nil {
213 return nil, ErrProtoParse(e)
214 }
215 // nullify the hash included in the block header
216 blockHeaderWithoutHash, e := codec.NullifyProtoField(blockHeaderWithHash, 2)
217 if e != nil {
218 return nil, ErrProtoParse(e)
219 }
220 // exit
221 return crypto.Hash(blockHeaderWithoutHash), nil
222}
223
224// jsonBlock is the Block implementation of json.Marshaller and json.Unmarshaler
225type jsonBlock struct {

Callers 2

CheckBasicMethod · 0.80
BlockToHashMethod · 0.80

Calls 5

GetRawProtoFieldFunction · 0.92
NullifyProtoFieldFunction · 0.92
HashFunction · 0.92
ErrNilBlockFunction · 0.85
ErrProtoParseFunction · 0.85

Tested by

no test coverage detected