Skip to content

Commit

Permalink
feat: add GetIDs function (and basic test)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzdybal committed Oct 2, 2023
1 parent 8b6cbe1 commit dee3c76
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 18 deletions.
49 changes: 43 additions & 6 deletions da_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package da_test

import (
"testing"

"bytes"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/rollkit/go-da"
)

func TestDummyDA(t *testing.T) {
dummy := NewDummyDA()
t.Run("ExecuteDA", func(t *testing.T) {
ExecuteDATest(t, dummy)
t.Run("Basic DA test", func(t *testing.T) {
BasicDATest(t, dummy)
})
t.Run("CheckErrors", func(t *testing.T) {
t.Run("Get IDs and all data", func(t *testing.T) {
GetIDsTest(t, dummy)
})
t.Run("Check Errors", func(t *testing.T) {
CheckErrors(t, dummy)
})
}
Expand All @@ -22,7 +26,7 @@ func TestDummyDA(t *testing.T) {
type Blob = da.Blob
type ID = da.ID

func ExecuteDATest(t *testing.T, da da.DA) {
func BasicDATest(t *testing.T, da da.DA) {
msg1 := []byte("message 1")
msg2 := []byte("message 2")

Expand Down Expand Up @@ -90,3 +94,36 @@ func CheckErrors(t *testing.T, da da.DA) {
assert.Error(t, err)
assert.Empty(t, blob)
}

func GetIDsTest(t *testing.T, da da.DA) {
msgs := [][]byte{[]byte("msg1"), []byte("msg2"), []byte("msg3")}

ids, proofs, err := da.Submit(msgs)
assert.NoError(t, err)
assert.Len(t, ids, len(msgs))
assert.Len(t, proofs, len(msgs))

found := false
end := time.Now().Add(1 * time.Second)
for i := uint64(1); !found && !time.Now().After(end); i++ {
ret, err := da.GetIDs(i)
if err != nil {
break
}
if len(ret) > 0 {
blobs, err := da.Get(ret)
assert.NoError(t, err)

if len(blobs) == len(msgs) {
found = true
for b := 0; b < len(blobs); b++ {
if bytes.Compare(blobs[b], msgs[b]) != 0 {
found = false
}
}
}
}
}

assert.True(t, found)
}
45 changes: 33 additions & 12 deletions dummy_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package da_test

import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"sync/atomic"

"github.com/rollkit/go-da"
)
Expand All @@ -16,15 +16,19 @@ import (
// 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 {
data map[string][]byte
data map[uint64][]kvp
privKey ed25519.PrivateKey
pubKey ed25519.PublicKey
cnt uint64
height uint64
}

type kvp struct {
key, value []byte
}

func NewDummyDA() *DummyDA {
da := &DummyDA{
data: make(map[string][]byte),
data: make(map[uint64][]kvp),
}
da.pubKey, da.privKey, _ = ed25519.GenerateKey(rand.Reader)
return da
Expand All @@ -35,17 +39,31 @@ var _ da.DA = &DummyDA{}
func (d *DummyDA) Get(ids []da.ID) ([]da.Blob, error) {
blobs := make([]da.Blob, len(ids))
for i, id := range ids {
blob, ok := d.data[string(id)]
if !ok {
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.Compare(d.data[height][j].key, id) == 0 {
blobs[i] = d.data[height][j].value
found = true
}
}
if !found {
return nil, errors.New("no blob for given ID")
}
blobs[i] = blob
}
return blobs, nil
}

func (d *DummyDA) GetIDs(height uint64) ([]da.ID, error) {
panic("not implemented!")
kvps := d.data[height]
ids := make([]da.ID, len(kvps))
for i, kv := range kvps {
ids[i] = kv.key
}
return ids, nil
}

func (d *DummyDA) Commit(blobs []da.Blob) ([]da.Commitment, error) {
Expand All @@ -59,12 +77,12 @@ func (d *DummyDA) Commit(blobs []da.Blob) ([]da.Commitment, error) {
func (d *DummyDA) Submit(blobs []da.Blob) ([]da.ID, []da.Proof, error) {
ids := make([]da.ID, len(blobs))
proofs := make([]da.Proof, len(blobs))
d.height += 1
for i, blob := range blobs {
id := d.nextID()
ids[i] = append(id, d.getHash(blob)...)
ids[i] = append(d.nextID(), d.getHash(blob)...)
proofs[i] = d.getProof(ids[i], blob)

d.data[string(ids[i])] = blob
d.data[d.height] = append(d.data[d.height], kvp{ids[i], blob})
}

return ids, proofs, nil
Expand All @@ -82,7 +100,10 @@ func (d *DummyDA) Validate(ids []da.ID, proofs []da.Proof) ([]bool, error) {
}

func (d *DummyDA) nextID() []byte {
cnt := atomic.AddUint64(&d.cnt, 1)
return d.getID(d.height)
}

func (d *DummyDA) getID(cnt uint64) []byte {
id := make([]byte, 8)
binary.LittleEndian.PutUint64(id, cnt)
return id
Expand Down

0 comments on commit dee3c76

Please sign in to comment.