-
Notifications
You must be signed in to change notification settings - Fork 174
/
ledger.go
65 lines (53 loc) · 1.95 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
package state
import (
"strings"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/model/flow"
)
// A Ledger is the storage interface used by the virtual machine to read and write register values.
type Ledger interface {
Set(owner, controller, key string, value flow.RegisterValue)
Get(owner, controller, key string) (flow.RegisterValue, error)
Touch(owner, controller, key string)
Delete(owner, controller, key string)
}
// A MapLedger is a naive ledger storage implementation backed by a simple map.
//
// This implementation is designed for testing purposes.
type MapLedger struct {
Registers map[string]flow.RegisterValue
RegisterTouches map[string]bool
}
func NewMapLedger() *MapLedger {
return &MapLedger{
Registers: make(map[string]flow.RegisterValue),
RegisterTouches: make(map[string]bool),
}
}
func (m MapLedger) Set(owner, controller, key string, value flow.RegisterValue) {
k := RegisterID(owner, controller, key)
m.RegisterTouches[string(k)] = true
m.Registers[string(k)] = value
}
func (m MapLedger) Get(owner, controller, key string) (flow.RegisterValue, error) {
k := RegisterID(owner, controller, key)
m.RegisterTouches[string(k)] = true
return m.Registers[string(k)], nil
}
func (m MapLedger) Touch(owner, controller, key string) {
m.RegisterTouches[string(RegisterID(owner, controller, key))] = true
}
func (m MapLedger) Delete(owner, controller, key string) {
delete(m.Registers, string(RegisterID(owner, controller, key)))
}
func RegisterID(owner, controller, key string) flow.RegisterID {
return fullKeyHash(owner, controller, key)
}
func fullKey(owner, controller, key string) string {
// https://en.wikipedia.org/wiki/C0_and_C1_control_codes#Field_separators
return strings.Join([]string{owner, controller, key}, "\x1F")
}
func fullKeyHash(owner, controller, key string) flow.RegisterID {
hasher := hash.NewSHA2_256()
return flow.RegisterID(hasher.ComputeHash([]byte(fullKey(owner, controller, key))))
}