-
Notifications
You must be signed in to change notification settings - Fork 177
/
ledger.go
99 lines (81 loc) · 2.41 KB
/
ledger.go
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
89
90
91
92
93
94
95
96
97
98
99
package flow
import (
"fmt"
"github.com/onflow/flow-go/ledger/common/hash"
"github.com/onflow/flow-go/model/fingerprint"
)
type RegisterID struct {
Owner string
Controller string
Key string
}
func (r *RegisterID) String() string {
return fmt.Sprintf("%x/%x/%x", r.Owner, r.Controller, r.Key)
}
// Bytes returns a bytes representation of the RegisterID.
//
// The encoding uses the injective fingerprint module.
func (r *RegisterID) Bytes() []byte {
return fingerprint.Fingerprint(r)
}
func NewRegisterID(owner, controller, key string) RegisterID {
return RegisterID{
Owner: owner,
Controller: controller,
Key: key,
}
}
// RegisterValue (value part of Register)
type RegisterValue = []byte
type RegisterEntry struct {
Key RegisterID
Value RegisterValue
}
//handy container for sorting
type RegisterEntries []RegisterEntry
func (d RegisterEntries) Len() int {
return len(d)
}
func (d RegisterEntries) Less(i, j int) bool {
if d[i].Key.Owner != d[j].Key.Owner {
return d[i].Key.Owner < d[j].Key.Owner
} else if d[i].Key.Controller != d[j].Key.Controller {
return d[i].Key.Controller < d[j].Key.Controller
}
return d[i].Key.Key < d[j].Key.Key
}
func (d RegisterEntries) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
func (d RegisterEntries) IDs() []RegisterID {
r := make([]RegisterID, len(d))
for i, entry := range d {
r[i] = entry.Key
}
return r
}
func (d RegisterEntries) Values() []RegisterValue {
r := make([]RegisterValue, len(d))
for i, entry := range d {
r[i] = entry.Value
}
return r
}
// StorageProof (proof of a read or update to the state, Merkle path of some sort)
type StorageProof = []byte
// StateCommitment holds the root hash of the tree (Snapshot)
// TODO: solve the circular dependency and define StateCommitment as ledger.State
type StateCommitment hash.Hash
// DummyStateCommitment is an arbitrary value used in function failure cases,
// although it can represent a valid state commitment.
var DummyStateCommitment = StateCommitment(hash.DummyHash)
// ToStateCommitment converts a byte slice into a StateComitment.
// It returns an error if the slice has an invalid length.
func ToStateCommitment(stateBytes []byte) (StateCommitment, error) {
var state StateCommitment
if len(stateBytes) != len(state) {
return DummyStateCommitment, fmt.Errorf("expecting %d bytes but got %d bytes", len(state), len(stateBytes))
}
copy(state[:], stateBytes)
return state, nil
}