WIP - publish statediffs to IPFS

This commit is contained in:
Elizabeth Engelman 2018-12-13 11:33:38 -06:00
parent 3a2600a718
commit ae4c889dc7
5 changed files with 274 additions and 8 deletions

View File

@ -0,0 +1,58 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains a batch of utility type declarations used by the tests. As the node
// operates on unique types, a lot of them are needed to check various features.
package ipfs
import (
"context"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/fsrepo"
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
)
type Adder interface {
Add(node ipld.Node) error
}
type adder struct {
n *core.IpfsNode
ctx context.Context
}
func (a adder) Add(node ipld.Node) error {
return a.n.DAG.Add(a.n.Context(), node) // For some reason DAG.Add method is not being exposed by the ipld.DAGService
}
func NewAdder(repoPath string) (*adder, error) {
r, err := fsrepo.Open(repoPath)
if err != nil {
return nil, err
}
ctx := context.Background()
cfg := &core.BuildCfg{
Online: false,
Repo: r,
}
ipfsNode, err := core.NewNode(ctx, cfg)
if err != nil {
return nil, err
}
return &adder{n: ipfsNode, ctx: ctx}, nil
}

View File

@ -0,0 +1,80 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains a batch of utility type declarations used by the tests. As the node
// operates on unique types, a lot of them are needed to check various features.
package ipfs
import (
"bytes"
"encoding/gob"
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
"github.com/ethereum/go-ethereum/statediff/builder"
)
const (
EthStateDiffCode = 0x99 // Register custom codec for state diff?
)
type DagPutter interface {
DagPut(sd *builder.StateDiff) (string, error)
}
type dagPutter struct {
Adder
}
func NewDagPutter(adder Adder) *dagPutter {
return &dagPutter{Adder: adder}
}
func (bhdp *dagPutter) DagPut(sd *builder.StateDiff) (string, error) {
nd, err := bhdp.getNode(sd)
if err != nil {
return "", err
}
err = bhdp.Add(nd)
if err != nil {
return "", err
}
return nd.Cid().String(), nil
}
func (bhdp *dagPutter) getNode(sd *builder.StateDiff) (ipld.Node, error) {
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
err := enc.Encode(sd)
if err != nil {
return nil, err
}
raw := buff.Bytes()
cid, err := RawToCid(EthStateDiffCode, raw)
if err != nil {
return nil, err
}
return &StateDiffNode{
StateDiff: sd,
cid: cid,
rawdata: raw,
}, nil
}

View File

@ -0,0 +1,38 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains a batch of utility type declarations used by the tests. As the node
// operates on unique types, a lot of them are needed to check various features.
package ipfs
import (
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
)
func RawToCid(codec uint64, raw []byte) (*cid.Cid, error) {
c, err := cid.Prefix{
Codec: codec,
Version: 1,
MhType: mh.KECCAK_256,
MhLength: -1,
}.Sum(raw)
if err != nil {
return nil, err
}
return &c, nil
}

View File

@ -0,0 +1,78 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains a batch of utility type declarations used by the tests. As the node
// operates on unique types, a lot of them are needed to check various features.
package ipfs
import (
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
"github.com/ethereum/go-ethereum/statediff/builder"
)
type StateDiffNode struct {
*builder.StateDiff
cid *cid.Cid
rawdata []byte
}
func (sdn *StateDiffNode) RawData() []byte {
return sdn.rawdata
}
func (sdn *StateDiffNode) Cid() cid.Cid {
return *sdn.cid
}
func (sdn StateDiffNode) String() string {
return sdn.cid.String()
}
func (sdn StateDiffNode) Loggable() map[string]interface{} {
return sdn.cid.Loggable()
}
func (sdn StateDiffNode) Resolve(path []string) (interface{}, []string, error) {
panic("implement me")
}
func (sdn StateDiffNode) Tree(path string, depth int) []string {
panic("implement me")
}
func (sdn StateDiffNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
panic("implement me")
}
func (sdn StateDiffNode) Copy() ipld.Node {
panic("implement me")
}
func (sdn StateDiffNode) Links() []*ipld.Link {
panic("implement me")
}
func (sdn StateDiffNode) Stat() (*ipld.NodeStat, error) {
panic("implement me")
}
func (sdn StateDiffNode) Size() (uint64, error) {
panic("implement me")
}

View File

@ -20,13 +20,14 @@
package publisher
import (
"os"
"encoding/csv"
"time"
"github.com/ethereum/go-ethereum/statediff"
"github.com/ethereum/go-ethereum/statediff/builder"
"github.com/ethereum/go-ethereum/statediff/publisher/ipfs"
"os"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/statediff/builder"
"github.com/ethereum/go-ethereum/statediff"
"time"
)
type Publisher interface {
@ -34,6 +35,7 @@ type Publisher interface {
}
type publisher struct {
ipfs.DagPutter
Config statediff.Config
}
@ -54,7 +56,12 @@ var (
)
func NewPublisher(config statediff.Config) (*publisher, error) {
adder, err := ipfs.NewAdder(config.Path)
if err != nil {
return nil, err
}
return &publisher{
DagPutter: ipfs.NewDagPutter(adder),
Config: config,
}, nil
}
@ -63,6 +70,12 @@ func (p *publisher) PublishStateDiff(sd *builder.StateDiff) (string, error) {
switch p.Config.Mode {
case statediff.CSV:
return "", p.publishStateDiffToCSV(*sd)
case statediff.IPLD:
cidStr, err := p.DagPut(sd)
if err != nil {
return "", err
}
return cidStr, err
default:
return "", p.publishStateDiffToCSV(*sd)
}
@ -184,4 +197,3 @@ func formatAccountDiffIncremental(accountDiff builder.AccountDiffIncremental, sd
}
return formattedAccountData
}