aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/storage/mru/updateheader.go
blob: 3ac20c1890acbeaed6e9ae20eaf85d4d10f7258a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package mru

import (
    "github.com/ethereum/go-ethereum/swarm/storage"
)

// updateHeader models the non-payload components of a Resource Update
type updateHeader struct {
    UpdateLookup        // UpdateLookup contains the information required to locate this resource (components of the search key used to find it)
    multihash    bool   // Whether the data in this Resource Update should be interpreted as multihash
    metaHash     []byte // SHA3 hash of the metadata chunk (less ownerAddr). Used to prove ownerhsip of the resource.
}

const metaHashLength = storage.KeyLength

// updateLookupLength bytes
// 1 byte flags (multihash bool for now)
// 32 bytes metaHash
const updateHeaderLength = updateLookupLength + 1 + metaHashLength

// binaryPut serializes the resource header information into the given slice
func (h *updateHeader) binaryPut(serializedData []byte) error {
    if len(serializedData) != updateHeaderLength {
        return NewErrorf(ErrInvalidValue, "Incorrect slice size to serialize updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData))
    }
    if len(h.metaHash) != metaHashLength {
        return NewError(ErrInvalidValue, "updateHeader.binaryPut called without metaHash set")
    }
    if err := h.UpdateLookup.binaryPut(serializedData[:updateLookupLength]); err != nil {
        return err
    }
    cursor := updateLookupLength
    copy(serializedData[cursor:], h.metaHash[:metaHashLength])
    cursor += metaHashLength

    var flags byte
    if h.multihash {
        flags |= 0x01
    }

    serializedData[cursor] = flags
    cursor++

    return nil
}

// binaryLength returns the expected size of this structure when serialized
func (h *updateHeader) binaryLength() int {
    return updateHeaderLength
}

// binaryGet restores the current updateHeader instance from the information contained in the passed slice
func (h *updateHeader) binaryGet(serializedData []byte) error {
    if len(serializedData) != updateHeaderLength {
        return NewErrorf(ErrInvalidValue, "Incorrect slice size to read updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData))
    }

    if err := h.UpdateLookup.binaryGet(serializedData[:updateLookupLength]); err != nil {
        return err
    }
    cursor := updateLookupLength
    h.metaHash = make([]byte, metaHashLength)
    copy(h.metaHash[:storage.KeyLength], serializedData[cursor:cursor+storage.KeyLength])
    cursor += metaHashLength

    flags := serializedData[cursor]
    cursor++

    h.multihash = flags&0x01 != 0

    return nil
}