-
Notifications
You must be signed in to change notification settings - Fork 177
/
ledger.go
154 lines (126 loc) · 3.73 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package flow
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/onflow/flow-go/ledger/common/hash"
"github.com/onflow/flow-go/model/fingerprint"
)
type RegisterID struct {
Owner string
Key string
}
// this function returns a string format of a RegisterID in the form '%x/%x'
// it has been optimized to avoid the memory allocations inside Sprintf
func (r *RegisterID) String() string {
ownerLen := len(r.Owner)
requiredLen := ((ownerLen + len(r.Key)) * 2) + 1
arr := make([]byte, requiredLen)
hex.Encode(arr, []byte(r.Owner))
arr[2*ownerLen] = byte('/')
hex.Encode(arr[(2*ownerLen)+1:], []byte(r.Key))
return string(arr)
}
// 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, key string) RegisterID {
return RegisterID{
Owner: owner,
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
}
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 StateCommitment.
// It returns an error if the slice has an invalid length.
// The returned error indicates that the given byte slice is not a
// valid root hash of an execution state. As the function is
// side-effect free, all failures are simply a no-op.
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
}
func (s StateCommitment) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(s[:]))
}
func (s *StateCommitment) UnmarshalJSON(data []byte) error {
// first, attempt to unmarshal assuming data is a hex string representation
err := s.unmarshalJSONHexString(data)
if err == nil {
return nil
}
// fallback to unmarshalling as [32]byte
return s.unmarshalJSONByteArr(data)
}
func (s *StateCommitment) unmarshalJSONHexString(data []byte) error {
var stateCommitmentHex string
if err := json.Unmarshal(data, &stateCommitmentHex); err != nil {
return err
}
b, err := hex.DecodeString(stateCommitmentHex)
if err != nil {
return err
}
h, err := hash.ToHash(b)
if err != nil {
return err
}
*s = StateCommitment(h)
return nil
}
func (s *StateCommitment) unmarshalJSONByteArr(data []byte) error {
var stateCommitment [32]byte
if err := json.Unmarshal(data, &stateCommitment); err != nil {
return err
}
*s = stateCommitment
return nil
}