-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdummy.go
181 lines (156 loc) · 4.64 KB
/
dummy.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package test
import (
"bytes"
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"sync"
"time"
"github.com/rollkit/go-da"
)
// DefaultMaxBlobSize is the default max blob size
const DefaultMaxBlobSize = 64 * 64 * 482
// DummyDA is a simple implementation of in-memory DA. Not production ready! Intended only for testing!
//
// Data is stored in a map, where key is a serialized sequence number. This key is returned as ID.
// Commitments are simply hashes, and proofs are ED25519 signatures.
type DummyDA struct {
mu *sync.Mutex // protects data and height
data map[uint64][]kvp
timestamps map[uint64]time.Time
maxBlobSize uint64
height uint64
privKey ed25519.PrivateKey
pubKey ed25519.PublicKey
}
type kvp struct {
key, value []byte
}
// NewDummyDA create new instance of DummyDA
func NewDummyDA(opts ...func(*DummyDA) *DummyDA) *DummyDA {
da := &DummyDA{
mu: new(sync.Mutex),
data: make(map[uint64][]kvp),
timestamps: make(map[uint64]time.Time),
maxBlobSize: DefaultMaxBlobSize,
}
for _, f := range opts {
da = f(da)
}
da.pubKey, da.privKey, _ = ed25519.GenerateKey(rand.Reader)
return da
}
var _ da.DA = &DummyDA{}
// MaxBlobSize returns the max blob size in bytes.
func (d *DummyDA) MaxBlobSize(ctx context.Context) (uint64, error) {
return d.maxBlobSize, nil
}
// Get returns Blobs for given IDs.
func (d *DummyDA) Get(ctx context.Context, ids []da.ID, _ da.Namespace) ([]da.Blob, error) {
d.mu.Lock()
defer d.mu.Unlock()
blobs := make([]da.Blob, len(ids))
for i, id := range ids {
if len(id) < 8 {
return nil, errors.New("invalid ID")
}
height := binary.LittleEndian.Uint64(id)
found := false
for j := 0; !found && j < len(d.data[height]); j++ {
if bytes.Equal(d.data[height][j].key, id) {
blobs[i] = d.data[height][j].value
found = true
}
}
if !found {
return nil, &da.ErrBlobNotFound{}
}
}
return blobs, nil
}
// GetIDs returns IDs of Blobs at given DA height.
func (d *DummyDA) GetIDs(ctx context.Context, height uint64, _ da.Namespace) (*da.GetIDsResult, error) {
d.mu.Lock()
defer d.mu.Unlock()
if height > d.height {
return nil, &da.ErrFutureHeight{}
}
kvps, ok := d.data[height]
if !ok {
return nil, nil
}
ids := make([]da.ID, len(kvps))
for i, kv := range kvps {
ids[i] = kv.key
}
return &da.GetIDsResult{IDs: ids, Timestamp: d.timestamps[height]}, nil
}
// GetProofs returns inclusion Proofs for all Blobs located in DA at given height.
func (d *DummyDA) GetProofs(ctx context.Context, ids []da.ID, _ da.Namespace) ([]da.Proof, error) {
blobs, err := d.Get(ctx, ids, nil)
d.mu.Lock()
defer d.mu.Unlock()
if err != nil {
return nil, err
}
proofs := make([]da.Proof, len(blobs))
for i, blob := range blobs {
proofs[i] = d.getProof(ids[i], blob)
}
return proofs, nil
}
// Commit returns cryptographic Commitments for given blobs.
func (d *DummyDA) Commit(ctx context.Context, blobs []da.Blob, _ da.Namespace) ([]da.Commitment, error) {
commits := make([]da.Commitment, len(blobs))
for i, blob := range blobs {
commits[i] = d.getHash(blob)
}
return commits, nil
}
// Submit stores blobs in DA layer.
func (d *DummyDA) Submit(ctx context.Context, blobs []da.Blob, gasPrice float64, ns da.Namespace) ([]da.ID, error) {
return d.SubmitWithOptions(ctx, blobs, gasPrice, ns, nil)
}
// SubmitWithOptions stores blobs in DA layer (options are ignored).
func (d *DummyDA) SubmitWithOptions(ctx context.Context, blobs []da.Blob, gasPrice float64, _ da.Namespace, _ []byte) ([]da.ID, error) {
d.mu.Lock()
defer d.mu.Unlock()
ids := make([]da.ID, len(blobs))
d.height += 1
d.timestamps[d.height] = time.Now()
for i, blob := range blobs {
ids[i] = append(d.nextID(), d.getHash(blob)...)
d.data[d.height] = append(d.data[d.height], kvp{ids[i], blob})
}
return ids, nil
}
// Validate checks the Proofs for given IDs.
func (d *DummyDA) Validate(ctx context.Context, ids []da.ID, proofs []da.Proof, _ da.Namespace) ([]bool, error) {
if len(ids) != len(proofs) {
return nil, errors.New("number of IDs doesn't equal to number of proofs")
}
results := make([]bool, len(ids))
for i := 0; i < len(ids); i++ {
results[i] = ed25519.Verify(d.pubKey, ids[i][8:], proofs[i])
}
return results, nil
}
func (d *DummyDA) nextID() []byte {
return d.getID(d.height)
}
func (d *DummyDA) getID(cnt uint64) []byte {
id := make([]byte, 8)
binary.LittleEndian.PutUint64(id, cnt)
return id
}
func (d *DummyDA) getHash(blob []byte) []byte {
sha := sha256.Sum256(blob)
return sha[:]
}
func (d *DummyDA) getProof(id, blob []byte) []byte {
sign, _ := d.privKey.Sign(rand.Reader, d.getHash(blob), &ed25519.Options{})
return sign
}