Skip to content

Commit

Permalink
updates and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Apr 8, 2019
1 parent 25c32cc commit 2cf27a9
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 23 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ COMMANDS:
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--url value gofs api url (default: "https://gofs.io/api/v0/")
--help, -h show help
--version, -v print the version
--url value GOFS API URL. (default: "https://gofs.io/api/v0/") [$GOFS_API]
--contract value Contract address. (default: "0x0000000000000000000000000000000000001234") [$GOFS_CONTRACT]
--rpc value RPC URL. (default: "https://rpc.gochain.io") [$GOFS_RPC]
--help, -h show help
--version, -v print the version
```
40 changes: 27 additions & 13 deletions cmd/gofs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package main

import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/gochain-io/gochain/v3/crypto"

"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/core/types"
"github.com/gochain-io/web3"
Expand Down Expand Up @@ -67,7 +71,11 @@ func main() {
cli.Uint64Flag{
Name: "duration, d",
Usage: "Storage duration in hours.",
Value: 1,
},
cli.StringFlag{
Name: "private-key, pk",
Usage: "Private key.",
EnvVar: "WEB3_PRIVATE_KEY",
},
},
Action: func(c *cli.Context) error {
Expand All @@ -79,18 +87,23 @@ func main() {
if dur == 0 {
return fmt.Errorf("duration missing or invalid")
}
contract, err := parseContract(c)
contract, err := parseContract(contract)
if err != nil {
return err
}
return Pin(ctx, rpc, contract, cid, dur)
pkStr := c.String("private-key")
pk, err := crypto.HexToECDSA(strings.TrimPrefix(pkStr, "0x"))
if err != nil {
return fmt.Errorf("invalid private key %q: %v", pkStr, err)
}
return Pin(ctx, rpc, contract, pk, cid, dur)
},
},
{
Name: "rate",
Usage: "Get the current storage rate in wei per GigaByteHour.",
Action: func(c *cli.Context) error {
contract, err := parseContract(c)
contract, err := parseContract(contract)
if err != nil {
return err
}
Expand Down Expand Up @@ -121,7 +134,7 @@ func main() {
if size == 0 {
return fmt.Errorf("size missing or invalid")
}
contract, err := parseContract(c)
contract, err := parseContract(contract)
if err != nil {
return err
}
Expand All @@ -148,7 +161,7 @@ func main() {
},
{
Name: "status",
Usage: "Get the current storage cost in wei per GigaByteHour.",
Usage: "Get the current storage status for a CID.",
Action: func(c *cli.Context) error {
cid := c.Args().First()
if cid == "" {
Expand All @@ -157,25 +170,26 @@ func main() {
return Status(ctx, api, cid)
},
},
//TODO gofs logs CID (filter logs, start/end blocks)
//TODO by user too? do we need to index user in the event?
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
}
}

func parseContract(c *cli.Context) (common.Address, error) {
contract := c.String("contract")
func parseContract(contract string) (common.Address, error) {
if !common.IsHexAddress(contract) {
return common.Address{}, fmt.Errorf("invalid hex contract address: %s", contract)
}
return common.HexToAddress(contract), nil
}

func Pin(ctx context.Context, url string, contract common.Address, ci string, dur uint64) error {
h, r, err := gofs.Pin(ctx, url, contract, ci, dur)
func Pin(ctx context.Context, url string, contract common.Address, pk *ecdsa.PrivateKey, ci string, dur uint64) error {
h, r, err := gofs.Pin(ctx, url, contract, pk, ci, dur)
if err != nil {
return fmt.Errorf("failed to get receipt for tx %s: %v", h.Hex(), err)
return fmt.Errorf("failed to pin: %v", err)
}
switch r.Status {
case types.ReceiptStatusFailed:
Expand Down Expand Up @@ -210,7 +224,7 @@ func Cost(ctx context.Context, rpcurl string, contract common.Address, size, dur
}

func costStr(size uint64, dur uint64, cost *big.Int) string {
return fmt.Sprintf("%d GBs for %s: %s", size, time.Duration(dur)*time.Hour, web3.WeiAsBase(cost))
return fmt.Sprintf("%d GBs for %s: %s GO", size, time.Duration(dur)*time.Hour, web3.WeiAsBase(cost))
}

func Rate(ctx context.Context, rpcurl string, contract common.Address) error {
Expand All @@ -219,7 +233,7 @@ func Rate(ctx context.Context, rpcurl string, contract common.Address) error {
return err
}
//TODO friendlier units?
fmt.Printf("Current storage rate: %d wei per GigaByteHour.", rate)
fmt.Printf("Current storage rate: %d wei per GigaByteHour.\n\n", rate)

fmt.Println("Cost:")
for _, vals := range []struct {
Expand Down
11 changes: 11 additions & 0 deletions contracts/GOFSPinner.abi
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"name": "_rate",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
Expand Down
2 changes: 1 addition & 1 deletion contracts/GOFSPinner.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x608060405260008054600160a060020a03191633179055610473806100256000396000f3fe608060405260043610610050577c010000000000000000000000000000000000000000000000000000000060003504632c4e722e811461005557806334fcf4371461007c57806393b862fe146100ba575b600080fd5b34801561006157600080fd5b5061006a610162565b60408051918252519081900360200190f35b34801561008857600080fd5b506100a66004803603602081101561009f57600080fd5b5035610168565b604080519115158252519081900360200190f35b6100a6600480360360408110156100d057600080fd5b8101906020810181356401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955050913592506101e2915050565b60015481565b6000805473ffffffffffffffffffffffffffffffffffffffff1633146101d9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260228152602001806104266022913960400191505060405180910390fd5b60019190915590565b60008260008151811015156101f357fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916601260f860020a02148015610286575082600181518110151561024757fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916602060f860020a02145b156102f257604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f56657273696f6e203020434944206e6f7420616c6c6f77656400000000000000604482015290519081900360640190fd5b60015434101561036357604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600e60248201527f56616c756520746f6f206c6f772e000000000000000000000000000000000000604482015290519081900360640190fd5b6001546040513391840280156108fc02916000818181858888f19350505050158015610393573d6000803e3d6000fd5b50826040518082805190602001908083835b602083106103c45780518252601f1990920191602091820191016103a5565b51815160209384036101000a60001901801990921691161790526040805192909401829003822088835293519395507fa0d90cefad6d4f81c3b726e011627beb8a079c24c0215d178fcb1225ae9ea6d494509083900301919050a29291505056fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea165627a7a7230582073bcd17531526090d9463769d8b279bd71f4416c02da561fab7c51b7516835630029
0x608060405234801561001057600080fd5b506040516020806104cc8339810180604052602081101561003057600080fd5b505160008054600160a060020a03191633179055600155610476806100566000396000f3fe608060405260043610610050577c010000000000000000000000000000000000000000000000000000000060003504632c4e722e811461005557806334fcf4371461007c57806393b862fe146100ba575b600080fd5b34801561006157600080fd5b5061006a610162565b60408051918252519081900360200190f35b34801561008857600080fd5b506100a66004803603602081101561009f57600080fd5b5035610168565b604080519115158252519081900360200190f35b6100a6600480360360408110156100d057600080fd5b8101906020810181356401000000008111156100eb57600080fd5b8201836020820111156100fd57600080fd5b8035906020019184600183028401116401000000008311171561011f57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955050913592506101e2915050565b60015481565b6000805473ffffffffffffffffffffffffffffffffffffffff1633146101d9576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260228152602001806104296022913960400191505060405180910390fd5b60019190915590565b60008260008151811015156101f357fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916601260f860020a02148015610286575082600181518110151561024757fe5b90602001015160f860020a900460f860020a027effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916602060f860020a02145b156102f257604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f56657273696f6e203020434944206e6f7420616c6c6f77656400000000000000604482015290519081900360640190fd5b60015482023481111561036657604080517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600e60248201527f56616c756520746f6f206c6f772e000000000000000000000000000000000000604482015290519081900360640190fd5b60405133903483900380156108fc02916000818181858888f19350505050158015610395573d6000803e3d6000fd5b50836040518082805190602001908083835b602083106103c65780518252601f1990920191602091820191016103a7565b51815160209384036101000a60001901801990921691161790526040805192909401829003822089835293519395507fa0d90cefad6d4f81c3b726e011627beb8a079c24c0215d178fcb1225ae9ea6d494509083900301919050a2509291505056fe4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6e2ea165627a7a72305820c6d199375800fc101d4fd433c816f468330b871e139be1d830ec0f6488f7f8000029
2 changes: 1 addition & 1 deletion contracts/owned.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x6080604052348015600f57600080fd5b5060008054600160a060020a03191633179055603580602f6000396000f3fe6080604052600080fdfea165627a7a72305820f4f95fe5b361dc91fc18602cbb5f234bdf3e919ec163d5d4459b252d4aa7db840029
0x6080604052348015600f57600080fd5b5060008054600160a060020a03191633179055603580602f6000396000f3fe6080604052600080fdfea165627a7a72305820dfbd1b8db7bc849bc5a5977282e315280530860c5c7460ff8fd9751d7573ae0d0029
10 changes: 8 additions & 2 deletions contracts/pinner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ contract GOFSPinner is Pinner, owned {
// Rate in wei per GigaByteHour.
uint public rate; //TODO initial rate? static? constructor?

constructor(uint _rate) public {
rate = _rate;
}

function setRate(uint _rate) public onlyOwner returns (bool) {
rate = _rate;
}
Expand All @@ -45,11 +49,13 @@ contract GOFSPinner is Pinner, owned {
!(cid[0] == 0x12 && cid[1] == 0x20),
"Version 0 CID not allowed"
);
uint cost = gbh*rate;
require(
msg.value >= rate,
msg.value >= cost,
"Value too low."
);
msg.sender.transfer(gbh*rate);
// refund excess
msg.sender.transfer(msg.value - cost);
emit Pinned(cid, gbh);
}
}
22 changes: 19 additions & 3 deletions gofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package gofs

import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"os"
"time"

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

"github.com/gochain-io/gochain/v3/crypto"

"github.com/gochain-io/gochain/v3/accounts/abi/bind"
"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/core/types"
Expand Down Expand Up @@ -66,10 +71,10 @@ func AddFile(ctx context.Context, url, path string) (AddResponse, error) {
return NewClient(url).Add(ctx, f)
}

func Pin(ctx context.Context, url string, contract common.Address, ci string, dur uint64) (common.Hash, *types.Receipt, error) {
func Pin(ctx context.Context, url string, contract common.Address, pk *ecdsa.PrivateKey, ci string, dur uint64) (common.Hash, *types.Receipt, error) {
cid, err := cid.Parse(ci)
if err != nil {
return common.Hash{}, nil, err
return common.Hash{}, nil, fmt.Errorf("invalid cid %q: %v", ci, err)
}
if cid.Version() == 0 {
return common.Hash{}, nil, errors.New("version 0 CID not supported")
Expand All @@ -82,8 +87,19 @@ func Pin(ctx context.Context, url string, contract common.Address, ci string, du
if err != nil {
return common.Hash{}, nil, err
}
rate, err := p.Rate(&bind.CallOpts{Context: ctx})
if err != nil {
return common.Hash{}, nil, err
}
cost := new(big.Int).Mul(rate, big.NewInt(int64(dur)))
opts := &bind.TransactOpts{
Context: ctx,
From: crypto.PubkeyToAddress(pk.PublicKey),
Signer: func(s types.Signer, _ common.Address, tx *types.Transaction) (*types.Transaction, error) {
return types.SignTx(tx, s, pk)
},
GasLimit: 50000,
Value: cost,
}
tx, err := p.Pin(opts, cid.Bytes(), big.NewInt(int64(dur)))
if err != nil {
Expand All @@ -102,7 +118,7 @@ func WaitForReceipt(ctx context.Context, client *goclient.Client, hash common.Ha
receipt, err := client.TransactionReceipt(ctx, hash)
if err == nil && receipt != nil {
return receipt, nil
} else if err != nil {
} else if err != nil && err != gochain.NotFound {
return nil, err
}
select {
Expand Down

0 comments on commit 2cf27a9

Please sign in to comment.