Skip to content

Commit

Permalink
refactor: data fee (#40)
Browse files Browse the repository at this point in the history
* refactor: upload fee

* feat: new cli params for tx fee

* test: always enable market
  • Loading branch information
MiniFrenchBread authored Jul 31, 2024
1 parent 4936814 commit a5b9576
Show file tree
Hide file tree
Showing 20 changed files with 1,947 additions and 821 deletions.
11 changes: 11 additions & 0 deletions cmd/kv_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"math"
"math/big"
"time"

zg_common "github.com/0glabs/0g-storage-client/common"
Expand Down Expand Up @@ -37,6 +38,8 @@ var (
finalityRequired bool
taskSize uint

fee float64

timeout time.Duration
}

Expand Down Expand Up @@ -77,6 +80,8 @@ func init() {

kvWriteCmd.Flags().DurationVar(&kvWriteArgs.timeout, "timeout", 0, "cli task timeout, 0 for no timeout")

kvWriteCmd.Flags().Float64Var(&kvWriteArgs.fee, "fee", 0, "fee paid in a0gi")

rootCmd.AddCommand(kvWriteCmd)
}

Expand All @@ -96,11 +101,17 @@ func kvWrite(*cobra.Command, []string) {
logrus.WithError(err).Fatal("Failed to create flow contract")
}

var fee *big.Int
if kvWriteArgs.fee > 0 {
feeInA0GI := big.NewFloat(kvWriteArgs.fee)
fee, _ = feeInA0GI.Mul(feeInA0GI, big.NewFloat(1e18)).Int(nil)
}
opt := transfer.UploadOption{
FinalityRequired: kvWriteArgs.finalityRequired,
TaskSize: kvWriteArgs.taskSize,
ExpectedReplica: kvWriteArgs.expectedReplica,
SkipTx: kvWriteArgs.skipTx,
Fee: fee,
}

var clients []*node.ZgsClient
Expand Down
14 changes: 12 additions & 2 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"math/big"
"time"

zg_common "github.com/0glabs/0g-storage-client/common"
Expand Down Expand Up @@ -35,6 +36,8 @@ var (
finalityRequired bool
taskSize uint

fee float64

timeout time.Duration
}

Expand Down Expand Up @@ -69,6 +72,8 @@ func init() {

uploadCmd.Flags().DurationVar(&uploadArgs.timeout, "timeout", 0, "cli task timeout, 0 for no timeout")

uploadCmd.Flags().Float64Var(&uploadArgs.fee, "fee", 0, "fee paid in a0gi")

rootCmd.AddCommand(uploadCmd)
}

Expand All @@ -87,13 +92,18 @@ func upload(*cobra.Command, []string) {
if err != nil {
logrus.WithError(err).Fatal("Failed to create flow contract")
}

var fee *big.Int
if uploadArgs.fee > 0 {
feeInA0GI := big.NewFloat(uploadArgs.fee)
fee, _ = feeInA0GI.Mul(feeInA0GI, big.NewFloat(1e18)).Int(nil)
}
opt := transfer.UploadOption{
Tags: hexutil.MustDecode(uploadArgs.tags),
FinalityRequired: uploadArgs.finalityRequired,
TaskSize: uploadArgs.taskSize,
ExpectedReplica: uploadArgs.expectedReplica,
SkipTx: uploadArgs.skipTx,
Fee: fee,
}

file, err := core.Open(uploadArgs.file)
Expand All @@ -118,7 +128,7 @@ func upload(*cobra.Command, []string) {
defer client.Close()
}

uploader, err := transfer.NewUploader(flow, clients, zg_common.LogOption{Logger: logrus.StandardLogger()})
uploader, err := transfer.NewUploader(ctx, flow, clients, zg_common.LogOption{Logger: logrus.StandardLogger()})
if err != nil {
logrus.WithError(err).Fatal("Failed to initialize uploader")
}
Expand Down
39 changes: 21 additions & 18 deletions contract/contract.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package contract

import (
"context"
"fmt"
"math/big"

"github.com/0glabs/0g-storage-client/common/blockchain"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/openweb3/web3go"
Expand All @@ -13,6 +15,7 @@ import (
type FlowContract struct {
*blockchain.Contract
*Flow
clientWithSigner *web3go.Client
}

func NewFlowContract(flowAddress common.Address, clientWithSigner *web3go.Client) (*FlowContract, error) {
Expand All @@ -28,7 +31,23 @@ func NewFlowContract(flowAddress common.Address, clientWithSigner *web3go.Client
return nil, err
}

return &FlowContract{contract, flow}, nil
return &FlowContract{contract, flow, clientWithSigner}, nil
}

func (f *FlowContract) GetMarketContract(ctx context.Context) (*Market, error) {
marketAddr, err := f.Market(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}

backend, _ := f.clientWithSigner.ToClientForContract()

market, err := NewMarket(marketAddr, backend)
if err != nil {
return nil, err
}

return market, nil
}

func (submission Submission) String() string {
Expand Down Expand Up @@ -58,23 +77,7 @@ func (submission Submission) Root() common.Hash {
return root
}

const (
LIFETIME_MONTHES = 3
BYTES_PER_SECTOR = 256
ANNUAL_ZGS_TOKENS_PER_GB = 10
GB = 1024 * 1024 * 1024
MONTH_PER_YEAR = 12
)

var pricePerSector = new(big.Int).Div(
new(big.Int).Mul(
big.NewInt(LIFETIME_MONTHES*BYTES_PER_SECTOR*ANNUAL_ZGS_TOKENS_PER_GB/MONTH_PER_YEAR),
new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil), // ether: 10^18
),
big.NewInt(GB),
)

func (submission Submission) Fee() *big.Int {
func (submission Submission) Fee(pricePerSector *big.Int) *big.Int {
var sectors int64
for _, node := range submission.Nodes {
sectors += 1 << node.Height.Int64()
Expand Down
575 changes: 83 additions & 492 deletions contract/flow.go

Large diffs are not rendered by default.

Loading

0 comments on commit a5b9576

Please sign in to comment.