github.com/MetalBlockchain/metalgo@v1.11.9/vms/proposervm/block/parse.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package block 5 6 import ( 7 "fmt" 8 9 "github.com/MetalBlockchain/metalgo/ids" 10 ) 11 12 // Parse a block and verify that the signature attached to the block is valid 13 // for the certificate provided in the block. 14 func Parse(bytes []byte, chainID ids.ID) (Block, error) { 15 block, err := ParseWithoutVerification(bytes) 16 if err != nil { 17 return nil, err 18 } 19 return block, block.verify(chainID) 20 } 21 22 // ParseWithoutVerification parses a block without verifying that the signature 23 // on the block is correct. 24 func ParseWithoutVerification(bytes []byte) (Block, error) { 25 var block Block 26 parsedVersion, err := Codec.Unmarshal(bytes, &block) 27 if err != nil { 28 return nil, err 29 } 30 if parsedVersion != CodecVersion { 31 return nil, fmt.Errorf("expected codec version %d but got %d", CodecVersion, parsedVersion) 32 } 33 return block, block.initialize(bytes) 34 } 35 36 func ParseHeader(bytes []byte) (Header, error) { 37 header := statelessHeader{} 38 parsedVersion, err := Codec.Unmarshal(bytes, &header) 39 if err != nil { 40 return nil, err 41 } 42 if parsedVersion != CodecVersion { 43 return nil, fmt.Errorf("expected codec version %d but got %d", CodecVersion, parsedVersion) 44 } 45 header.bytes = bytes 46 return &header, nil 47 }