github.com/ethersphere/bee/v2@v2.2.0/pkg/postage/batch.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package postage 6 7 import ( 8 "encoding/binary" 9 "math/big" 10 ) 11 12 // Batch represents a postage batch, a payment on the blockchain. 13 type Batch struct { 14 ID []byte // batch ID 15 Value *big.Int // normalised balance of the batch 16 Start uint64 // block number the batch was created 17 Owner []byte // owner's ethereum address 18 Depth uint8 // batch depth, i.e., size = 2^{depth} 19 BucketDepth uint8 // the depth of neighbourhoods t 20 Immutable bool // if the batch allows adding new capacity (dilution) 21 } 22 23 // MarshalBinary implements BinaryMarshaller. It will attempt to serialize the 24 // postage batch to a byte slice. 25 // serialised as ID(32)|big endian value(32)|start block(8)|owner addr(20)|BucketDepth(1)|depth(1)|immutable(1) 26 func (b *Batch) MarshalBinary() ([]byte, error) { 27 out := make([]byte, 95) 28 copy(out, b.ID) 29 value := b.Value.Bytes() 30 copy(out[64-len(value):], value) 31 binary.BigEndian.PutUint64(out[64:72], b.Start) 32 copy(out[72:], b.Owner) 33 out[92] = b.BucketDepth 34 out[93] = b.Depth 35 if b.Immutable { 36 out[94] = 1 37 } 38 return out, nil 39 } 40 41 // UnmarshalBinary implements BinaryUnmarshaller. It will attempt deserialize 42 // the given byte slice into the batch. 43 func (b *Batch) UnmarshalBinary(buf []byte) error { 44 b.ID = buf[:32] 45 b.Value = big.NewInt(0).SetBytes(buf[32:64]) 46 b.Start = binary.BigEndian.Uint64(buf[64:72]) 47 b.Owner = buf[72:92] 48 b.BucketDepth = buf[92] 49 b.Depth = buf[93] 50 b.Immutable = buf[94] > 0 51 return nil 52 }