/
client.go
213 lines (181 loc) · 6.9 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package filecoin
import (
"context"
"fmt"
"net/http"
filaddress "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/crypto"
filclient "github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/ipfs/go-cid"
"github.com/renproject/multichain/api/account"
"github.com/renproject/multichain/api/address"
"github.com/renproject/pack"
)
const (
// AuthorizationKey is the header key used for authorization
AuthorizationKey = "Authorization"
// DefaultClientRPCURL is the RPC URL used by default, to interact with the
// filecoin lotus node.
DefaultClientRPCURL = "http://127.0.0.1:1234/rpc/v0"
// DefaultClientAuthToken is the auth token used to instantiate the lotus
// client. A valid lotus auth token is required to write messages to the
// filecoin storage. To do read-only queries, auth token is not required.
DefaultClientAuthToken = ""
)
// ClientOptions are used to parameterise the behaviour of the Client.
type ClientOptions struct {
RPCURL string
AuthToken string
}
// DefaultClientOptions returns ClientOptions with the default settings. These
// settings are valid for use with the default local deployment of the
// multichain. In production, the rpc-url and authentication token should be
// changed.
func DefaultClientOptions() ClientOptions {
return ClientOptions{
RPCURL: DefaultClientRPCURL,
AuthToken: DefaultClientAuthToken,
}
}
// WithRPCURL returns a modified version of the options with the given API
// rpc-url
func (opts ClientOptions) WithRPCURL(rpcURL pack.String) ClientOptions {
opts.RPCURL = string(rpcURL)
return opts
}
// WithAuthToken returns a modified version of the options with the given
// authentication token.
func (opts ClientOptions) WithAuthToken(authToken pack.String) ClientOptions {
opts.AuthToken = string(authToken)
return opts
}
// Client holds options to connect to a filecoin lotus node, and the underlying
// RPC client instance.
type Client struct {
opts ClientOptions
node v0api.FullNode
closer jsonrpc.ClientCloser
}
// NewClient creates and returns a new JSON-RPC client to the Filecoin node
func NewClient(opts ClientOptions) (*Client, error) {
requestHeaders := make(http.Header)
if opts.AuthToken != DefaultClientAuthToken {
requestHeaders.Add(AuthorizationKey, opts.AuthToken)
}
node, closer, err := filclient.NewFullNodeRPCV0(context.Background(), opts.RPCURL, requestHeaders)
if err != nil {
return nil, err
}
return &Client{opts, node, closer}, nil
}
// LatestBlock returns the block number at the current chain head.
func (client *Client) LatestBlock(ctx context.Context) (pack.U64, error) {
headTipset, err := client.node.ChainHead(ctx)
if err != nil {
return pack.NewU64(0), fmt.Errorf("get chain head: %v", err)
}
if headTipset.Height() < 0 {
return pack.NewU64(0), fmt.Errorf("unexpected chain head, expected > 0, got: %v", headTipset.Height())
}
return pack.NewU64(uint64(headTipset.Height())), nil
}
// Tx returns the transaction uniquely identified by the given transaction
// hash. It also returns the number of confirmations for the transaction.
func (client *Client) Tx(ctx context.Context, txID pack.Bytes) (account.Tx, pack.U64, error) {
// parse the transaction ID to a message ID
msgID, err := cid.Parse([]byte(txID))
if err != nil {
return nil, pack.NewU64(0), fmt.Errorf("parsing txid: %v", err)
}
// lookup message receipt to get its height
messageLookup, err := client.node.StateSearchMsg(ctx, msgID)
if err != nil {
return nil, pack.NewU64(0), fmt.Errorf("searching state for txid: %v", err)
}
if messageLookup == nil {
return nil, pack.NewU64(0), fmt.Errorf("searching state for txid %v: not found", msgID)
}
if messageLookup.Receipt.ExitCode.IsError() {
return nil, pack.NewU64(0), fmt.Errorf("executing transaction: %v", messageLookup.Receipt.ExitCode.String())
}
if !messageLookup.Message.Equals(msgID) {
return nil, pack.U64(0), fmt.Errorf("searching state for txid: expected %v, got %v", msgID, messageLookup.Message)
}
// get the most recent tipset and its height
chainHead, err := client.LatestBlock(ctx)
if err != nil {
return nil, pack.NewU64(0), err
}
confs := uint64(chainHead) - uint64(messageLookup.Height) + 1
if confs < 0 {
return nil, pack.NewU64(0), fmt.Errorf("get chain head: negative confirmations")
}
// get the message
msg, err := client.node.ChainGetMessage(ctx, msgID)
if err != nil {
return nil, pack.NewU64(0), fmt.Errorf("getting txid %v from chain: %v", msgID, err)
}
// support `Send` method for now
if msg.Method != builtin.MethodSend {
return nil, pack.NewU64(0), fmt.Errorf("unsupport tx method, expect `%v`, got `%v`", builtin.MethodSend, msg.Method)
}
return &Tx{msg: *msg}, pack.NewU64(confs), nil
}
// SubmitTx to the underlying blockchain network.
func (client *Client) SubmitTx(ctx context.Context, tx account.Tx) error {
switch tx := tx.(type) {
case *Tx:
// construct crypto.Signature
signature := crypto.Signature{
Type: crypto.SigTypeSecp256k1,
Data: tx.signature.Bytes(),
}
// construct types.SignedMessage
signedMessage := types.SignedMessage{
Message: tx.msg,
Signature: signature,
}
// submit transaction to mempool
msgID, err := client.node.MpoolPush(ctx, &signedMessage)
if err != nil {
return fmt.Errorf("pushing txid %v to mpool: %v", msgID, err)
}
return nil
default:
return fmt.Errorf("expected type %T, got type %T", new(Tx), tx)
}
}
// AccountNonce returns the current nonce of the account. This is the nonce to
// be used while building a new transaction.
func (client *Client) AccountNonce(ctx context.Context, addr address.Address) (pack.U256, error) {
filAddr, err := filaddress.NewFromString(string(addr))
if err != nil {
return pack.U256{}, fmt.Errorf("bad address '%v': %v", addr, err)
}
actor, err := client.node.StateGetActor(ctx, filAddr, types.NewTipSetKey(cid.Undef))
if err != nil {
return pack.U256{}, fmt.Errorf("searching state for addr %v: %v", addr, err)
}
return pack.NewU256FromU64(pack.NewU64(actor.Nonce)), nil
}
// AccountBalance returns the account balancee for a given address.
func (client *Client) AccountBalance(ctx context.Context, addr address.Address) (pack.U256, error) {
filAddr, err := filaddress.NewFromString(string(addr))
if err != nil {
return pack.U256{}, fmt.Errorf("bad address '%v': %v", addr, err)
}
actor, err := client.node.StateGetActor(ctx, filAddr, types.NewTipSetKey(cid.Undef))
if err != nil {
return pack.U256{}, fmt.Errorf("searching state for addr %v: %v", addr, err)
}
balance := actor.Balance.Int
// If the balance exceeds `MaxU256`, return an error.
if pack.MaxU256.Int().Cmp(balance) == -1 {
return pack.U256{}, fmt.Errorf("balance %v for %v exceeds MaxU256", balance.String(), addr)
}
return pack.NewU256FromInt(balance), nil
}