Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Apr 10, 2019
1 parent 8ffd79d commit 8556e9f
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 95 deletions.
67 changes: 37 additions & 30 deletions cmd/gofs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import (
"text/tabwriter"
"time"

"github.com/gochain-io/gochain/v3/common/hexutil"
cid "github.com/ipfs/go-cid"

"github.com/gochain-io/gofs"

"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/core/types"
"github.com/gochain-io/gochain/v3/crypto"
"github.com/gochain-io/web3"
cid "github.com/ipfs/go-cid"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -175,8 +174,8 @@ func main() {
},
},
{
Name: "events",
Usage: "Get Pinned events from filtered logs.",
Name: "pins",
Usage: "Query for pins.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "hash",
Expand All @@ -191,12 +190,12 @@ func main() {
Usage: "Ending block number.",
},
cli.StringFlag{
Name: "cid",
Usage: "CID to filter on.",
Name: "cids",
Usage: "Comma separated CIDs to filter on.",
},
cli.StringFlag{
Name: "user",
Usage: "User to filter on.",
Name: "users",
Usage: "Comma separated users to filter on.",
},
},
Action: func(c *cli.Context) error {
Expand All @@ -222,16 +221,27 @@ func main() {
if c.IsSet("to") {
f.To = big.NewInt(c.Int64("to"))
}
f.CID = c.String("cid")
if c.IsSet("user") {
if a, err := parseAddress(c.String("user")); err != nil {
return fmt.Errorf("invalid user: %v", err)
} else {
f.User = &a
if c.IsSet("cids") {
for _, s := range c.StringSlice("cids") {
ci, err := cid.Parse(s)
if err != nil {
return fmt.Errorf("invalid cid %s: %v", s, err)
}
f.CIDs = append(f.CIDs, ci)
}

}
if c.IsSet("users") {
for _, s := range c.StringSlice("user") {
a, err := parseAddress(s)
if err != nil {
return fmt.Errorf("invalid user: %v", err)
}
f.Users = append(f.Users, a)
}
}

return Events(ctx, rpc, contract, f)
return Pins(ctx, rpc, contract, f)
},
},
}
Expand Down Expand Up @@ -338,26 +348,23 @@ func Status(ctx context.Context, apiURL, ci string) error {
return nil
}

func Events(ctx context.Context, rpcURL string, contract common.Address, f gofs.EventFilter) error {
events, err := gofs.Events(ctx, rpcURL, contract, f)
func Pins(ctx context.Context, rpcURL string, contract common.Address, f gofs.EventFilter) error {
pins, err := gofs.Pins(ctx, rpcURL, contract, f)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintln(w, "Block\tTx\tLog\tRemoved\tCID\tGBH\t")
for _, e := range events {
ci, err := cid.Parse(e.Cid)
if err != nil {
return fmt.Errorf("invalid cid %s: %v", hexutil.Encode(e.Cid), err)
}
fmt.Fprintln(w, "Block\tTx\tLog\tRemoved\tCID\tGBH\tUser\t")
for _, p := range pins {
fmt.Fprintf(w,
"%d\t%d\t%d\t%t\t%s\t%d\t\n",
e.Raw.BlockNumber, //TODO why are all these idxs 0?
e.Raw.TxIndex,
e.Raw.Index,
e.Raw.Removed,
ci.String(),
e.Gbh,
"%d\t%d\t%d\t%t\t%s\t%s\t%s\t\n",
p.BlNum,
p.TxNum,
p.LogNum,
p.Removed,
p.CID.String(),
p.GBH,
p.User.Hex(),
)
}
w.Flush()
Expand Down
10 changes: 0 additions & 10 deletions contracts/GOFSPinner.abi
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,8 @@
},
{
"indexed": true,
"name": "cidIdx",
"type": "bytes"
},
{
"indexed": false,
"name": "cid",
"type": "bytes"
},
{
"indexed": false,
"name": "gbh",
"type": "uint256"
}
],
"name": "Pinned",
Expand Down
2 changes: 1 addition & 1 deletion contracts/GOFSPinner.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x608060405234801561001057600080fd5b506040516020806105808339810180604052602081101561003057600080fd5b505160008054600160a060020a03191633179055600155436002556105268061005a6000396000f3fe60806040526004361061005b577c010000000000000000000000000000000000000000000000000000000060003504632c4e722e811461006057806334fcf4371461008757806393b862fe146100c5578063f905c15a1461016d575b600080fd5b34801561006c57600080fd5b50610075610182565b60408051918252519081900360200190f35b34801561009357600080fd5b506100b1600480360360208110156100aa57600080fd5b5035610188565b604080519115158252519081900360200190f35b6100b1600480360360408110156100db57600080fd5b8101906020810181356401000000008111156100f657600080fd5b82018360208201111561010857600080fd5b8035906020019184600183028401116401000000008311171561012a57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295505091359250610202915050565b34801561017957600080fd5b506100756104d2565b60015481565b6000805473ffffffffffffffffffffffffffffffffffffffff1633146101f9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260228152602001806104d96022913960400191505060405180910390fd5b60019190915590565b600082600081518110151561021357fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916601260f860020a021480156102a6575082600181518110151561026757fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916602060f860020a02145b1561031257604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f56657273696f6e203020434944206e6f7420616c6c6f77656400000000000000604482015290519081900360640190fd5b60015482023481111561038657604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600e60248201527f56616c756520746f6f206c6f772e000000000000000000000000000000000000604482015290519081900360640190fd5b60405133903483900380156108fc02916000818181858888f193505050501580156103b5573d6000803e3d6000fd5b50836040518082805190602001908083835b602083106103e65780518252601f1990920191602091820191016103c7565b6001836020036101000a03801982511681845116808217855250505050505090500191505060405180910390203373ffffffffffffffffffffffffffffffffffffffff167f4d6efb9bd2f38e3cafaf5d932ce16579df65d7653204e2cb666e874edaafb68e86866040518080602001838152602001828103825284818151815260200191508051906020019080838360005b83811015610490578181015183820152602001610478565b50505050905090810190601f1680156104bd5780820380516001836020036101000a031916815260200191505b50935050505060405180910390a35092915050565b6002548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea165627a7a72305820f6a793ec8355f43396dd0fe2814a38e083283282be8135324bc19930d75393f70029
0x608060405234801561001057600080fd5b506040516020806104ef8339810180604052602081101561003057600080fd5b505160008054600160a060020a03191633179055600155436002556104958061005a6000396000f3fe60806040526004361061005b577c010000000000000000000000000000000000000000000000000000000060003504632c4e722e811461006057806334fcf4371461008757806393b862fe146100c5578063f905c15a1461016d575b600080fd5b34801561006c57600080fd5b50610075610182565b60408051918252519081900360200190f35b34801561009357600080fd5b506100b1600480360360208110156100aa57600080fd5b5035610188565b604080519115158252519081900360200190f35b6100b1600480360360408110156100db57600080fd5b8101906020810181356401000000008111156100f657600080fd5b82018360208201111561010857600080fd5b8035906020019184600183028401116401000000008311171561012a57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295505091359250610202915050565b34801561017957600080fd5b50610075610441565b60015481565b6000805473ffffffffffffffffffffffffffffffffffffffff1633146101f9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260228152602001806104486022913960400191505060405180910390fd5b60019190915590565b600082600081518110151561021357fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916601260f860020a021480156102a6575082600181518110151561026757fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916602060f860020a02145b1561031257604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f56657273696f6e203020434944206e6f7420616c6c6f77656400000000000000604482015290519081900360640190fd5b60015482023481111561038657604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600e60248201527f56616c756520746f6f206c6f772e000000000000000000000000000000000000604482015290519081900360640190fd5b60405133903483900380156108fc02916000818181858888f193505050501580156103b5573d6000803e3d6000fd5b50836040518082805190602001908083835b602083106103e65780518252601f1990920191602091820191016103c7565b5181516020939093036101000a60001901801990911692169190911790526040519201829003822093503392507f6f488ce79215cfcbd7d03c6e0cf105b3d9ee23884121bebe21744e7a38fc13b29160009150a35092915050565b6002548156fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea165627a7a7230582075948529d7bf4e2f367753f983faac1529d4115e8d07eb05d3767a0f7f833c860029
10 changes: 0 additions & 10 deletions contracts/Pinner.abi
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,8 @@
},
{
"indexed": true,
"name": "cidIdx",
"type": "bytes"
},
{
"indexed": false,
"name": "cid",
"type": "bytes"
},
{
"indexed": false,
"name": "gbh",
"type": "uint256"
}
],
"name": "Pinned",
Expand Down
2 changes: 1 addition & 1 deletion contracts/owned.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x6080604052348015600f57600080fd5b5060008054600160a060020a03191633179055603580602f6000396000f3fe6080604052600080fdfea165627a7a7230582021f92166fec503bbbf7e744be3ba1ea5f0bf8f456fe32a457e043ced758478220029
0x6080604052348015600f57600080fd5b5060008054600160a060020a03191633179055603580602f6000396000f3fe6080604052600080fdfea165627a7a7230582080ce8d081142b8b78bf28c3af9e9aa76fb361ee6ade6011ee576f08cfc3e10a00029
5 changes: 3 additions & 2 deletions contracts/pinner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Pinner {

function pin(bytes calldata cid, uint gbh) external payable returns (bool);

event Pinned(address indexed user, bytes indexed cidIdx, bytes cid, uint gbh);
event Pinned(address indexed user, bytes indexed cid);
}


Expand All @@ -33,6 +33,7 @@ contract owned {
}
}

//TODO killable, and then refund full amount when dead?
contract GOFSPinner is Pinner, owned {
// Rate in wei per GigaByteHour.
uint public rate;
Expand Down Expand Up @@ -61,6 +62,6 @@ contract GOFSPinner is Pinner, owned {
);
// refund excess
msg.sender.transfer(msg.value - cost);
emit Pinned(msg.sender, cid, cid, gbh);
emit Pinned(msg.sender, cid);
}
}
94 changes: 73 additions & 21 deletions gofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ import (
"strings"
"time"

"github.com/gochain-io/gochain/v3/accounts/abi"

"github.com/gochain-io/gochain/v3"
"github.com/gochain-io/gochain/v3/accounts/abi"
"github.com/gochain-io/gochain/v3/accounts/abi/bind"
"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/common/hexutil"
"github.com/gochain-io/gochain/v3/core/types"
"github.com/gochain-io/gochain/v3/crypto"
"github.com/gochain-io/gochain/v3/goclient"
cid "github.com/ipfs/go-cid"
)

var pinnerABI abi.ABI
var (
pinnerABI abi.ABI
pinInputs abi.Arguments
)

func init() {
parsed, err := abi.JSON(strings.NewReader(PinnerABI))
if err != nil {
panic(fmt.Sprintf("failed to parse generated abi: %v", err))
}
pinnerABI = parsed
pinInputs = pinnerABI.Methods["pin"].Inputs
}

func Rate(ctx context.Context, rpcURL string, contract common.Address) (*big.Int, error) {
Expand Down Expand Up @@ -148,12 +152,33 @@ type EventFilter struct {
From *big.Int
To *big.Int

User *common.Address //TODO slice?
CID string //TODO slice? cid.CID here?
Users []common.Address
CIDs []cid.Cid
}

type PinInputs struct {
CID []byte
GBH *big.Int
}

func UnpackPinInputs(data []byte) (pi PinInputs, err error) {
err = pinInputs.Unpack(&pinInputs, data)
return
}

type PinReceipt struct {
User common.Address
CID cid.Cid
GBH *big.Int
Tx *types.Transaction
BlNum uint64
TxNum uint
LogNum uint
Removed bool
}

// Events returns Pinned events for the given EventFilter.
func Events(ctx context.Context, rpcURL string, contract common.Address, filter EventFilter) ([]*PinnerPinned, error) {
// Pins returns Pinned events for the given EventFilter.
func Pins(ctx context.Context, rpcURL string, contract common.Address, filter EventFilter) ([]PinReceipt, error) {
gc, err := goclient.Dial(rpcURL)
if err != nil {
return nil, err
Expand All @@ -170,20 +195,22 @@ func Events(ctx context.Context, rpcURL string, contract common.Address, filter
q.ToBlock = filter.To
}
var userTopic []common.Hash
if filter.User != nil {
userTopic = []common.Hash{filter.User.Hash()}
if len(filter.Users) > 0 {
userTopic = make([]common.Hash, len(filter.Users))
for i := range filter.Users {
userTopic[i] = filter.Users[i].Hash()
}

}
var cidTopic []common.Hash
if filter.CID != "" {
ci, err := cid.Parse(filter.CID)
if err != nil {
return nil, fmt.Errorf("invalid cid %q: %v", filter.CID, err)
}
if ci.Version() == 0 {
return nil, errors.New("version 0 CID not supported")
if len(filter.CIDs) > 0 {
cidTopic = make([]common.Hash, len(filter.CIDs))
for i, ci := range filter.CIDs {
if ci.Version() == 0 {
return nil, errors.New("version 0 CID not supported")
}
cidTopic[i] = crypto.Keccak256Hash(ci.Bytes())
}
cidTopic = []common.Hash{crypto.Keccak256Hash(ci.Bytes())}
}
q.Topics = [][]common.Hash{{pinnerABI.Events["Pinned"].Id()}, userTopic, cidTopic}
logs, err := gc.FilterLogs(ctx, q)
Expand All @@ -192,14 +219,39 @@ func Events(ctx context.Context, rpcURL string, contract common.Address, filter
}

bc := bind.NewBoundContract(contract, pinnerABI, nil, nil, nil)
pinned := make([]*PinnerPinned, len(logs))
for i, l := range logs {
var pins []PinReceipt
for _, l := range logs {
var event PinnerPinned
if err := bc.UnpackLog(&event, "Pinned", l); err != nil {
return nil, err
}
pinned[i] = &event
tx, _, err := gc.TransactionByHash(ctx, l.TxHash)
if err != nil {
return nil, fmt.Errorf("failed to get tx %s: %v", l.TxHash, err)
}
from, err := gc.TransactionSender(ctx, tx, l.BlockHash, 0)
if err != nil {
return nil, fmt.Errorf("failed to look up tx sender %s: %v", l.TxHash, err)
}
pi, err := UnpackPinInputs(tx.Data())
if err != nil {
return nil, fmt.Errorf("failed to unpack pin inputs %s: %v", hexutil.Encode(tx.Data()), err)
}
ci, err := cid.Parse(pi.CID)
if err != nil {
return nil, fmt.Errorf("failed to parse CID %s: %v", hexutil.Encode(pi.CID), err)
}
pins = append(pins, PinReceipt{
User: from,
CID: ci,
GBH: pi.GBH,
Tx: tx,
BlNum: l.BlockNumber,
TxNum: l.TxIndex,
LogNum: l.Index,
Removed: l.Removed,
})
}

return pinned, nil
return pins, nil
}
Loading

0 comments on commit 8556e9f

Please sign in to comment.