github.com/eris-ltd/erisdb@v0.25.0/storage/commit_id.go (about) 1 package storage 2 3 import ( 4 "fmt" 5 6 "github.com/hyperledger/burrow/binary" 7 amino "github.com/tendermint/go-amino" 8 ) 9 10 var codec = amino.NewCodec() 11 12 type CommitID struct { 13 Hash binary.HexBytes 14 Version int64 15 } 16 17 func MarshalCommitID(hash []byte, version int64) ([]byte, error) { 18 commitID := CommitID{ 19 Version: version, 20 Hash: hash, 21 } 22 bs, err := codec.MarshalBinaryBare(commitID) 23 if err != nil { 24 return nil, fmt.Errorf("MarshalCommitID() could not encode CommitID %v: %v", commitID, err) 25 } 26 if bs == nil { 27 // Normalise zero value to non-nil so we can store it IAVL tree without panic 28 return []byte{}, nil 29 } 30 return bs, nil 31 } 32 33 func UnmarshalCommitID(bs []byte) (*CommitID, error) { 34 commitID := new(CommitID) 35 err := codec.UnmarshalBinaryBare(bs, commitID) 36 if err != nil { 37 return nil, fmt.Errorf("could not unmarshal CommitID: %v", err) 38 } 39 return commitID, nil 40 } 41 42 func (cid CommitID) String() string { 43 return fmt.Sprintf("Commit{Hash: %v, Version: %v}", cid.Hash, cid.Version) 44 }