Skip to content

Commit

Permalink
ipfs: package refactor
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed May 12, 2023
1 parent 53f174d commit af85c05
Show file tree
Hide file tree
Showing 24 changed files with 341 additions and 303 deletions.
2 changes: 1 addition & 1 deletion api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/ipfs"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/dvote/util"
Expand Down
6 changes: 3 additions & 3 deletions api/census_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"go.vocdoni.io/dvote/api/censusdb"
"go.vocdoni.io/dvote/crypto/ethereum"
"go.vocdoni.io/dvote/crypto/zk"
"go.vocdoni.io/dvote/data"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/db/metadb"
"go.vocdoni.io/dvote/httprouter"
Expand All @@ -39,7 +39,7 @@ func TestCensus(t *testing.T) {
qt.Assert(t, err, qt.IsNil)
censusDB := censusdb.NewCensusDB(db)

storage := data.MockIPFS(t)
storage := ipfs.MockIPFS(t)
app := vochain.TestBaseApplication(t)
api.Attach(app, nil, nil, storage, censusDB)
qt.Assert(t, api.EnableHandlers(CensusHandler), qt.IsNil)
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestCensusZk(t *testing.T) {
qt.Assert(t, err, qt.IsNil)
censusDB := censusdb.NewCensusDB(db)

storage := data.MockIPFS(t)
storage := ipfs.MockIPFS(t)
app := vochain.TestBaseApplication(t)
api.Attach(app, nil, nil, storage, censusDB)
qt.Assert(t, api.EnableHandlers(CensusHandler), qt.IsNil)
Expand Down
2 changes: 1 addition & 1 deletion api/censusdb/censusdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/google/uuid"
"go.vocdoni.io/dvote/censustree"
"go.vocdoni.io/dvote/data/compressor"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/ipfs"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/proto/build/go/models"
)
Expand Down
2 changes: 1 addition & 1 deletion api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/httprouter"
"go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/ipfs"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/statedb"
"go.vocdoni.io/dvote/util"
Expand Down
2 changes: 1 addition & 1 deletion apiclient/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"go.vocdoni.io/dvote/api"
"go.vocdoni.io/dvote/ipfs"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/types"
indexertypes "go.vocdoni.io/dvote/vochain/indexer/indexertypes"
"go.vocdoni.io/proto/build/go/models"
Expand Down
2 changes: 1 addition & 1 deletion apiclient/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"time"

"go.vocdoni.io/dvote/api"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/ipfs"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/proto/build/go/models"
Expand Down
4 changes: 2 additions & 2 deletions benchmark/zk_census_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"go.vocdoni.io/dvote/crypto/zk"
"go.vocdoni.io/dvote/crypto/zk/circuit"
"go.vocdoni.io/dvote/crypto/zk/prover"
"go.vocdoni.io/dvote/data"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/db/metadb"
"go.vocdoni.io/dvote/httprouter"
Expand Down Expand Up @@ -45,7 +45,7 @@ func BenchmarkZkCensus(b *testing.B) {
qt.Assert(b, err, qt.IsNil)
censusDB := censusdb.NewCensusDB(db)

storage := data.MockIPFS(b)
storage := ipfs.MockIPFS(b)

vocapp := vochain.TestBaseApplication(b)
vocApi.Attach(vocapp, nil, nil, storage, censusDB)
Expand Down
23 changes: 12 additions & 11 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,61 @@ package data

import (
"context"
"errors"
"fmt"

"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/metrics"
"go.vocdoni.io/dvote/types"
)

// Storage is the interface that wraps the basic methods for a distributed data storage provider.
type Storage interface {
Init(d *types.DataStore) error
Publish(ctx context.Context, o []byte) (string, error)
Publish(ctx context.Context, data []byte) (string, error)
Retrieve(ctx context.Context, id string, maxSize int64) ([]byte, error)
Pin(ctx context.Context, path string) error
Unpin(ctx context.Context, path string) error
ListPins(ctx context.Context) (map[string]string, error)
URIprefix() string
Stats(ctx context.Context) map[string]interface{}
CollectMetrics(ctx context.Context, ma *metrics.Agent) error

// TODO(mvdan): Temporary until we rethink Init/Start/etc.
Stop() error
}

// StorageID is the type for the different storage providers.
// Currently only IPFS is supported.
type StorageID int

const (
// IPFS is the InterPlanetary File System.
IPFS StorageID = iota + 1
BZZ
)

// StorageIDFromString returns the Storage identifier from a string.
func StorageIDFromString(i string) StorageID {
switch i {
case "IPFS":
return IPFS
case "BZZ":
return BZZ
default:
return -1
}
}

// IPFSNewConfig returns a new DataStore configuration for IPFS.
func IPFSNewConfig(path string) *types.DataStore {
datastore := new(types.DataStore)
datastore.Datadir = path
return datastore
}

// TODO(mvdan): This is really a Start, not an Init. Rethink this.

// Init returns a new Storage instance of type `t`.
func Init(t StorageID, d *types.DataStore) (Storage, error) {
switch t {
case IPFS:
s := new(IPFSHandle)
s := new(ipfs.Handler)
err := s.Init(d)
return s, err
default:
return nil, errors.New("bad storage type or DataStore specification")
return nil, fmt.Errorf("bad storage type or DataStore specification")
}
}
2 changes: 1 addition & 1 deletion data/datamocktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"golang.org/x/exp/maps"

"go.vocdoni.io/dvote/ipfs"
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/metrics"
"go.vocdoni.io/dvote/test/testcommon/testutil"
"go.vocdoni.io/dvote/types"
Expand Down
41 changes: 5 additions & 36 deletions ipfs/cid.go → data/ipfs/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ import (

"github.com/ipfs/boxo/ipld/merkledag"

"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
"go.vocdoni.io/dvote/log"
)

const (
// ChunkerTypeSize is the chunker type used by IPFS to calculate to build the DAG.
ChunkerTypeSize = "size-262144"
)
var dAGbuilder = ihelper.DagBuilderParams{}

// dAG returns a new, thread-safe, dummy DAGService.
func dAG() ipld.DAGService {
Expand All @@ -34,7 +29,9 @@ func dAG() ipld.DAGService {

// bserv returns a new, thread-safe, mock BlockService.
func bserv() blockservice.BlockService {
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
bstore := blockstore.NewBlockstore(
dssync.MutexWrap(ds.NewMapDatastore()),
blockstore.NoPrefix())
return blockservice.New(bstore, offline.Exchange(bstore))
}

Expand All @@ -53,19 +50,7 @@ func CalculateCIDv1json(data []byte) string {
log.Errorw(err, "could not create chunk")
}

format := ipfscid.V1Builder{
Codec: uint64(multicodec.DagJson),
MhType: uint64(multihash.SHA2_256),
}
params := ihelper.DagBuilderParams{
Dagserv: dAG(),
RawLeaves: false,
Maxlinks: ihelper.DefaultLinksPerBlock,
NoCopy: false,
CidBuilder: &format,
}

dbh, err := params.New(chnk)
dbh, err := dAGbuilder.New(chnk)
if err != nil {
log.Errorw(err, "could not create dag builder")
}
Expand Down Expand Up @@ -93,21 +78,5 @@ func CIDequals(cid1, cid2 string) bool {
log.Errorw(err, "could not decode cid2 "+cid2)
return false
}

// skip version length
/* b := c1.Bytes()
_, n1, _ := varint.FromUvarint(b)
// skip codec length
_, n2, _ := varint.FromUvarint(b[n1:])
log.Warnf("==1==> %x", b[n1+n2:])
b = c2.Bytes()
_, n1, _ = varint.FromUvarint(b)
// skip codec length
_, n2, _ = varint.FromUvarint(b[n1:])
log.Warnf("==2==> %x", b[n1+n2:])
log.Warnf("cid1=%s cid2=%s | cid1hash=%s cid2hash=%s", c1.String(), c2.String(), c1.Hash().String(), c2.Hash().String())
*/
return c1.Hash().String() == c2.Hash().String()
}
Loading

0 comments on commit af85c05

Please sign in to comment.