WIP - publish statediffs to IPFS
This commit is contained in:
parent
3a2600a718
commit
ae4c889dc7
58
statediff/publisher/ipfs/adder.go
Normal file
58
statediff/publisher/ipfs/adder.go
Normal 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
|
||||||
|
}
|
80
statediff/publisher/ipfs/dag_putter.go
Normal file
80
statediff/publisher/ipfs/dag_putter.go
Normal 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
|
||||||
|
}
|
38
statediff/publisher/ipfs/helpers.go
Normal file
38
statediff/publisher/ipfs/helpers.go
Normal 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
|
||||||
|
}
|
78
statediff/publisher/ipfs/node.go
Normal file
78
statediff/publisher/ipfs/node.go
Normal 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")
|
||||||
|
}
|
@ -20,13 +20,14 @@
|
|||||||
package publisher
|
package publisher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"encoding/csv"
|
"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"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"github.com/ethereum/go-ethereum/statediff/builder"
|
"time"
|
||||||
"github.com/ethereum/go-ethereum/statediff"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Publisher interface {
|
type Publisher interface {
|
||||||
@ -34,6 +35,7 @@ type Publisher interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type publisher struct {
|
type publisher struct {
|
||||||
|
ipfs.DagPutter
|
||||||
Config statediff.Config
|
Config statediff.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,15 +49,20 @@ var (
|
|||||||
"storageDiffPaths",
|
"storageDiffPaths",
|
||||||
}
|
}
|
||||||
|
|
||||||
timeStampFormat = "20060102150405.00000"
|
timeStampFormat = "20060102150405.00000"
|
||||||
deletedAccountAction = "deleted"
|
deletedAccountAction = "deleted"
|
||||||
createdAccountAction = "created"
|
createdAccountAction = "created"
|
||||||
updatedAccountAction = "updated"
|
updatedAccountAction = "updated"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPublisher(config statediff.Config) (*publisher, error) {
|
func NewPublisher(config statediff.Config) (*publisher, error) {
|
||||||
|
adder, err := ipfs.NewAdder(config.Path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &publisher{
|
return &publisher{
|
||||||
Config: config,
|
DagPutter: ipfs.NewDagPutter(adder),
|
||||||
|
Config: config,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,6 +70,12 @@ func (p *publisher) PublishStateDiff(sd *builder.StateDiff) (string, error) {
|
|||||||
switch p.Config.Mode {
|
switch p.Config.Mode {
|
||||||
case statediff.CSV:
|
case statediff.CSV:
|
||||||
return "", p.publishStateDiffToCSV(*sd)
|
return "", p.publishStateDiffToCSV(*sd)
|
||||||
|
case statediff.IPLD:
|
||||||
|
cidStr, err := p.DagPut(sd)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return cidStr, err
|
||||||
default:
|
default:
|
||||||
return "", p.publishStateDiffToCSV(*sd)
|
return "", p.publishStateDiffToCSV(*sd)
|
||||||
}
|
}
|
||||||
@ -94,7 +107,7 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) error {
|
|||||||
data = append(data, row)
|
data = append(data, row)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, value := range data{
|
for _, value := range data {
|
||||||
err := writer.Write(value)
|
err := writer.Write(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -184,4 +197,3 @@ func formatAccountDiffIncremental(accountDiff builder.AccountDiffIncremental, sd
|
|||||||
}
|
}
|
||||||
return formattedAccountData
|
return formattedAccountData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user