Skip to content

Commit

Permalink
修改获取Cid错误
Browse files Browse the repository at this point in the history
  • Loading branch information
myxtype committed Apr 8, 2021
1 parent c09c54d commit 2a02bdd
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 47 deletions.
5 changes: 3 additions & 2 deletions chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestClient_ChainGetMessage(t *testing.T) {
c := testClient()

id, err := cid.Parse("bafy2bzacebrx3sb5do2b7cqgsnys2lkxtdq3pvjtgmdt2wclwmrtjeraa7x3q")
id, err := cid.Parse("bafy2bzacebrnc5tactfdeddxmpiyy5wppfc4gyc45zscwymn4r2pm4uwmasx4")
if err != nil {
t.Error(err)
}
Expand All @@ -21,6 +21,7 @@ func TestClient_ChainGetMessage(t *testing.T) {
}

t.Log(msg)
t.Log(msg.Cid().String())
}

// 获取当前头部高度
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestClient_ChainHead(t *testing.T) {
func TestClient_ChainGetTipSetByHeight(t *testing.T) {
c := testClient()

ts, err := c.ChainGetTipSetByHeight(context.Background(), 1201, nil)
ts, err := c.ChainGetTipSetByHeight(context.Background(), 652243, nil)
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// The Lotus Node
// The default token is in ~/.lotus/token
func testClient() *Client {
return NewClient("http://127.0.0.1:1234/rpc/v0", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBbGxvdyI6WyJyZWFkIiwid3JpdGUiLCJzaWduIiwiYWRtaW4iXX0.cF__3r_0IR9KwZ2nLkqcBW8vuPePruZieJAVvTAoUA4")
return New("https://1lB5G4SmGdSTikOo7l6vYlsktdd:b58884915362a99b4fc18c2bf8af8358@filecoin.infura.io")
}

// 测试RpcClient
Expand Down
24 changes: 19 additions & 5 deletions examples/local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"context"
"encoding/hex"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/myxtype/filecoin-client"
"github.com/myxtype/filecoin-client/local"
"github.com/myxtype/filecoin-client/types"
"github.com/shopspring/decimal"
)

func main() {
// 设置网络类型
address.CurrentNetwork = address.Mainnet

// 生产新的地址
// 新地址有转入fil才激活,不然没法用
ki, addr, err := local.WalletNew(types.KTSecp256k1)
if err != nil {
panic(err)
Expand All @@ -26,19 +30,31 @@ func main() {

to, _ := address.NewFromString("f1yfi4yslez2hz3ori5grvv3xdo3xkibc4v6xjusy")

// 转移0.001FIL到f1yfi4yslez2hz3ori5grvv3xdo3xkibc4v6xjusy
msg := &types.Message{
Version: 0,
To: to,
From: *addr,
Nonce: 0,
Value: decimal.NewFromInt(10000),
Value: filecoin.FromFil(decimal.NewFromFloat(0.001)),
GasLimit: 0,
GasFeeCap: decimal.NewFromInt(10000),
GasPremium: decimal.NewFromInt(10000),
GasFeeCap: abi.NewTokenAmount(10000),
GasPremium: abi.NewTokenAmount(10000),
Method: 0,
Params: nil,
}

client := filecoin.New("https://1lB5G4SmGdSTikOo7l6vYlsktdd:b58884915362a99b4fc18c2bf8af8358@filecoin.infura.io")

// 最大手续费0.0001 FIL
//maxFee := filecoin.FromFil(decimal.NewFromFloat(0.0001))

// 估算GasLimit
//msg, err = client.GasEstimateMessageGas(context.Background(), msg, &types.MessageSendSpec{MaxFee: maxFee}, nil)
//if err != nil {
// panic(err)
//}

// 离线签名
s, err := local.WalletSignMessage(types.KTSecp256k1, ki.PrivateKey, msg)
if err != nil {
Expand All @@ -53,8 +69,6 @@ func main() {
panic(err)
}

client := filecoin.New("https://1lB5G4SmGdSTikOo7l6vYlsktdd:b58884915362a99b4fc18c2bf8af8358@filecoin.infura.io")

mid, err := client.MpoolPush(context.Background(), s)
if err != nil {
panic(err)
Expand Down
86 changes: 86 additions & 0 deletions examples/recharge/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"context"
"github.com/ipfs/go-cid"
"github.com/myxtype/filecoin-client"
"github.com/myxtype/filecoin-client/types"
)

// 简单的充值逻辑
func main() {
client := filecoin.New("https://1lB5G4SmGdSTikOo7l6vYlsktdd:b58884915362a99b4fc18c2bf8af8358@filecoin.infura.io")

job := &RechargeFilJob{}

// 处理区块652243
job.mapHeight(client, 652243)

// todo
}

// 充值处理任务
type RechargeFilJob struct {
}

func (job *RechargeFilJob) mapHeight(c *filecoin.Client, height int64) error {
ts, err := c.ChainGetTipSetByHeight(context.Background(), height, nil)
if err != nil {
return err
}
for _, n := range ts.Cids {
bm, err := c.ChainGetBlockMessages(context.Background(), n)
if err != nil {
return err
}

// BlsMessages
for _, msg := range bm.BlsMessages {
err := job.handleMessage(msg.Cid(), msg)
if err != nil {
return err
}
}

// SecpkMessages
for _, msg := range bm.SecpkMessages {
err := job.handleSecpkMessage(msg)
if err != nil {
return err
}
}
}
return nil
}

func (job *RechargeFilJob) handleSecpkMessage(msg *types.SignedMessage) error {
return job.handleMessage(msg.Cid(), msg.Message)
}

func (job *RechargeFilJob) handleMessage(msgCid cid.Cid, msg *types.Message) error {
// 发送交易并且值大于0
if msg.Method != 0 || msg.Value.IsZero() {
return nil
}

value := filecoin.ToFil(msg.Value)

println(msgCid.String(), value.String())

// todo

return nil
}

// 结果如下:
/*
bafy2bzacedv5q3nam6icb4qwuftxw3nazn33iu3vd6zvwkcr6ljgfbkjmow62 5.002
bafy2bzacecb52i5423svvxgyfz7dvfvl4wssi4vraiyilkckomytplvbtxbgo 1.676583814823873898
bafy2bzaceabqawv4iwnjn4xusseqgxenprq36w6xxyveryflr3e7dpfmroprw 9.999853889870997446
bafy2bzacedvdkwm7js4qjgzyiqdzcoy3kgf7fdooowzbchj4rnzlw6wuk4bp6 3.978744820695744384
bafy2bzaced7tmalo62e74xsveb2ahmb6metcb6kwo5jq35neic3xlzwphm7am 3100
bafy2bzacecdykvlsswqlrkgohqsvr4cwv7rph4hzabbfg2p4iqixcm3iv62ly 220
bafy2bzaceakfahxbzfvson5ylx7nsa2velqfy37xmdz5bycyxds4rsyefittu 220
bafy2bzacec3rlx3yoezxzpnzyzehu5kenyjoso2gogkkpiw5wwbjv7hcjmy5y 9.99
bafy2bzacebrnc5tactfdeddxmpiyy5wppfc4gyc45zscwymn4r2pm4uwmasx4 17.90844787
*/
11 changes: 7 additions & 4 deletions mpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filecoin
import (
"context"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/myxtype/filecoin-client/types"
"github.com/shopspring/decimal"
"testing"
Expand All @@ -15,20 +16,22 @@ func TestClient_MpoolPush(t *testing.T) {
from, _ := address.NewFromString("t1e3soclcq34tq7wmykp7xkkmpkzjnghumm3syyay")
to, _ := address.NewFromString("t1r6egk7djfy7krbw7zdswbgdhep4hge5fecwmsoi")

// 转移0.001FIL
msg := &types.Message{
Version: 0,
To: to,
From: from,
Nonce: 0,
Value: FromFil(decimal.NewFromFloat(1)),
Value: FromFil(decimal.NewFromFloat(0.001)),
GasLimit: 0,
GasFeeCap: decimal.Zero,
GasPremium: decimal.Zero,
GasFeeCap: abi.NewTokenAmount(0),
GasPremium: abi.NewTokenAmount(0),
Method: 0,
Params: nil,
}

maxFee := FromFil(decimal.NewFromFloat(0.00001))
// 最大手续费0.0001 FIL
maxFee := FromFil(decimal.NewFromFloat(0.0001))
msg, err := c.GasEstimateMessageGas(context.Background(), msg, &types.MessageSendSpec{MaxFee: maxFee}, nil)
if err != nil {
t.Error(err)
Expand Down
20 changes: 6 additions & 14 deletions types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/filecoin-project/go-state-types/abi"
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/shopspring/decimal"
cbg "github.com/whyrusleeping/cbor-gen"
"io"
)
Expand All @@ -18,10 +17,10 @@ type Message struct {
To address.Address `json:"To"`
From address.Address `json:"From"`
Nonce uint64 `json:"Nonce"`
Value decimal.Decimal `json:"Value"`
Value abi.TokenAmount `json:"Value"`
GasLimit int64 `json:"GasLimit"`
GasFeeCap decimal.Decimal `json:"GasFeeCap"`
GasPremium decimal.Decimal `json:"GasPremium"`
GasFeeCap abi.TokenAmount `json:"GasFeeCap"`
GasPremium abi.TokenAmount `json:"GasPremium"`
Method uint64 `json:"Method"`
Params []byte `json:"Params"`
}
Expand All @@ -34,10 +33,6 @@ func (m *Message) Receiver() address.Address {
return m.To
}

func (m *Message) ValueReceived() abi.TokenAmount {
return abi.NewTokenAmount(m.Value.IntPart())
}

func (m *Message) Serialize() ([]byte, error) {
buf := new(bytes.Buffer)
if err := m.MarshalCBOR(buf); err != nil {
Expand Down Expand Up @@ -131,8 +126,7 @@ func (t *Message) MarshalCBOR(w io.Writer) error {
}

// t.Value (big.Int) (struct)
tValue := abi.NewTokenAmount(t.Value.IntPart())
if err := tValue.MarshalCBOR(w); err != nil {
if err := t.Value.MarshalCBOR(w); err != nil {
return err
}

Expand All @@ -148,14 +142,12 @@ func (t *Message) MarshalCBOR(w io.Writer) error {
}

// t.GasFeeCap (big.Int) (struct)
tGasFeeCap := abi.NewTokenAmount(t.GasFeeCap.IntPart())
if err := tGasFeeCap.MarshalCBOR(w); err != nil {
if err := t.GasFeeCap.MarshalCBOR(w); err != nil {
return err
}

// t.GasPremium (big.Int) (struct)
tGasPremium := abi.NewTokenAmount(t.GasPremium.IntPart())
if err := tGasPremium.MarshalCBOR(w); err != nil {
if err := t.GasPremium.MarshalCBOR(w); err != nil {
return err
}

Expand Down
78 changes: 78 additions & 0 deletions types/signed_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package types

import (
"bytes"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen"
"io"
)

type SignedMessage struct {
Message *Message `json:"Message"`
Signature *crypto.Signature `json:"Signature"`
}

func (sm *SignedMessage) Cid() cid.Cid {
if sm.Signature.Type == crypto.SigTypeBLS {
return sm.Message.Cid()
}

sb, err := sm.ToStorageBlock()
if err != nil {
panic(err)
}

return sb.Cid()
}

func (sm *SignedMessage) ToStorageBlock() (block.Block, error) {
if sm.Signature.Type == crypto.SigTypeBLS {
return sm.Message.ToStorageBlock()
}

data, err := sm.Serialize()
if err != nil {
return nil, err
}

c, err := abi.CidBuilder.Sum(data)
if err != nil {
return nil, err
}

return block.NewBlockWithCid(data, c)
}

func (sm *SignedMessage) Serialize() ([]byte, error) {
buf := new(bytes.Buffer)
if err := sm.MarshalCBOR(buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

var lengthBufSignedMessage = []byte{130}

func (t *SignedMessage) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write(lengthBufSignedMessage); err != nil {
return err
}

// t.Message (types.Message) (struct)
if err := t.Message.MarshalCBOR(w); err != nil {
return err
}

// t.Signature (crypto.Signature) (struct)
if err := t.Signature.MarshalCBOR(w); err != nil {
return err
}
return nil
}
9 changes: 2 additions & 7 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"
"github.com/shopspring/decimal"
"time"
Expand Down Expand Up @@ -36,12 +36,7 @@ type ObjStat struct {
}

type MessageSendSpec struct {
MaxFee decimal.Decimal `json:"MaxFee"`
}

type SignedMessage struct {
Message *Message `json:"Message"`
Signature *crypto.Signature `json:"Signature"`
MaxFee abi.TokenAmount `json:"MaxFee"`
}

type BlockMessages struct {
Expand Down
Loading

0 comments on commit 2a02bdd

Please sign in to comment.