github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/storage/jet/jetdrop_size.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package jet 18 19 import ( 20 "bytes" 21 "context" 22 "encoding/binary" 23 "io" 24 25 "github.com/insolar/insolar/core" 26 "github.com/pkg/errors" 27 "github.com/ugorji/go/codec" 28 ) 29 30 func encode(data interface{}) []byte { 31 var buf bytes.Buffer 32 enc := codec.NewEncoder(&buf, &codec.CborHandle{}) 33 enc.MustEncode(data) 34 return buf.Bytes() 35 } 36 37 // DropSize contains info about size of drop 38 type DropSize struct { 39 JetID core.RecordID 40 PulseNo core.PulseNumber 41 DropSize uint64 42 Signature []byte 43 } 44 45 func (ds *DropSize) serializeDropSize() []byte { 46 result := make([]byte, 0, 64) 47 48 buff := make([]byte, 8) 49 binary.LittleEndian.PutUint64(buff, ds.DropSize) 50 result = append(result, buff...) 51 52 buff = make([]byte, 4) 53 binary.LittleEndian.PutUint32(buff, uint32(ds.PulseNo)) 54 result = append(result, buff...) 55 56 result = append(result, ds.JetID.Bytes()...) 57 58 return result 59 } 60 61 // WriteHashData writes DropSize data to provided writer. This data is used to calculate DropSize's hash. 62 func (ds *DropSize) WriteHashData(w io.Writer) (int, error) { 63 return w.Write(ds.serializeDropSize()) 64 } 65 66 // DropSizeHistory is chain of drop sizes 67 type DropSizeHistory []DropSize 68 69 // DeserializeJetDropSizeHistory deserializes DropSizeHistory 70 func DeserializeJetDropSizeHistory(ctx context.Context, buff []byte) (DropSizeHistory, error) { 71 dec := codec.NewDecoder(bytes.NewReader(buff), &codec.CborHandle{}) 72 var dropSizes = DropSizeHistory{} 73 74 err := dec.Decode(&dropSizes) 75 if err != nil { 76 return nil, errors.Wrapf(err, "[ DeserializeJetDropSizeHistory ] Can't decode DropSizeHistory") 77 } 78 79 return dropSizes, nil 80 } 81 82 // Bytes serializes DropSizeHistory 83 func (dropSizeHistory DropSizeHistory) Bytes() []byte { 84 return encode(dropSizeHistory) 85 }