-
Notifications
You must be signed in to change notification settings - Fork 209
/
ethereum.go
915 lines (811 loc) · 27.9 KB
/
ethereum.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ethereum
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/go-resty/resty/v2"
"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-common/pkg/ffresty"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-common/pkg/log"
"github.com/hyperledger/firefly-common/pkg/wsclient"
"github.com/hyperledger/firefly-signer/pkg/abi"
"github.com/hyperledger/firefly-signer/pkg/ffi2abi"
"github.com/hyperledger/firefly/internal/blockchain/common"
"github.com/hyperledger/firefly/internal/cache"
"github.com/hyperledger/firefly/internal/coreconfig"
"github.com/hyperledger/firefly/internal/coremsgs"
"github.com/hyperledger/firefly/internal/metrics"
"github.com/hyperledger/firefly/pkg/blockchain"
"github.com/hyperledger/firefly/pkg/core"
)
const (
broadcastBatchEventSignature = "BatchPin(address,uint256,string,bytes32,bytes32,string,bytes32[])"
)
type Ethereum struct {
ctx context.Context
cancelCtx context.CancelFunc
topic string
prefixShort string
prefixLong string
capabilities *blockchain.Capabilities
callbacks common.BlockchainCallbacks
client *resty.Client
fftmClient *resty.Client
streams *streamManager
streamID string
wsconn wsclient.WSClient
closed chan struct{}
addressResolver *addressResolver
metrics metrics.Manager
ethconnectConf config.Section
subs common.FireflySubscriptions
cache cache.CInterface
}
type eventStreamWebsocket struct {
Topic string `json:"topic"`
}
type queryOutput struct {
Output interface{} `json:"output"`
}
type ethWSCommandPayload struct {
Type string `json:"type"`
Topic string `json:"topic,omitempty"`
BatchNumber int64 `json:"batchNumber,omitempty"`
}
type ethError struct {
Error string `json:"error,omitempty"`
}
type Location struct {
Address string `json:"address"`
}
type ListenerCheckpoint struct {
Block int64 `json:"block"`
TransactionIndex int64 `json:"transactionIndex"`
LogIndex int64 `json:"logIndex"`
}
type ListenerStatus struct {
Checkpoint ListenerCheckpoint `json:"checkpoint"`
Catchup bool `json:"catchup"`
}
type EthconnectMessageRequest struct {
Headers EthconnectMessageHeaders `json:"headers,omitempty"`
To string `json:"to"`
From string `json:"from,omitempty"`
Method *abi.Entry `json:"method"`
Params []interface{} `json:"params"`
}
type EthconnectMessageHeaders struct {
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
}
type FFIGenerationInput struct {
ABI *abi.ABI `json:"abi,omitempty"`
}
var addressVerify = regexp.MustCompile("^[0-9a-f]{40}$")
func (e *Ethereum) Name() string {
return "ethereum"
}
func (e *Ethereum) VerifierType() core.VerifierType {
return core.VerifierTypeEthAddress
}
func (e *Ethereum) Init(ctx context.Context, cancelCtx context.CancelFunc, conf config.Section, metrics metrics.Manager, cacheManager cache.Manager) (err error) {
e.InitConfig(conf)
ethconnectConf := e.ethconnectConf
addressResolverConf := conf.SubSection(AddressResolverConfigKey)
fftmConf := conf.SubSection(FFTMConfigKey)
e.ctx = log.WithLogField(ctx, "proto", "ethereum")
e.cancelCtx = cancelCtx
e.metrics = metrics
e.capabilities = &blockchain.Capabilities{}
e.callbacks = common.NewBlockchainCallbacks()
e.subs = common.NewFireflySubscriptions()
if addressResolverConf.GetString(AddressResolverURLTemplate) != "" {
if e.addressResolver, err = newAddressResolver(ctx, addressResolverConf, cacheManager); err != nil {
return err
}
}
if ethconnectConf.GetString(ffresty.HTTPConfigURL) == "" {
return i18n.NewError(ctx, coremsgs.MsgMissingPluginConfig, "url", "blockchain.ethereum.ethconnect")
}
e.client = ffresty.New(e.ctx, ethconnectConf)
if fftmConf.GetString(ffresty.HTTPConfigURL) != "" {
e.fftmClient = ffresty.New(e.ctx, fftmConf)
}
e.topic = ethconnectConf.GetString(EthconnectConfigTopic)
if e.topic == "" {
return i18n.NewError(ctx, coremsgs.MsgMissingPluginConfig, "topic", "blockchain.ethereum.ethconnect")
}
e.prefixShort = ethconnectConf.GetString(EthconnectPrefixShort)
e.prefixLong = ethconnectConf.GetString(EthconnectPrefixLong)
wsConfig := wsclient.GenerateConfig(ethconnectConf)
if wsConfig.WSKeyPath == "" {
wsConfig.WSKeyPath = "/ws"
}
e.wsconn, err = wsclient.New(ctx, wsConfig, nil, e.afterConnect)
if err != nil {
return err
}
cache, err := cacheManager.GetCache(
cache.NewCacheConfig(
ctx,
coreconfig.CacheBlockchainLimit,
coreconfig.CacheBlockchainTTL,
"",
),
)
if err != nil {
return err
}
e.cache = cache
e.streams = newStreamManager(e.client, e.cache)
batchSize := ethconnectConf.GetUint(EthconnectConfigBatchSize)
batchTimeout := uint(ethconnectConf.GetDuration(EthconnectConfigBatchTimeout).Milliseconds())
stream, err := e.streams.ensureEventStream(e.ctx, e.topic, batchSize, batchTimeout)
if err != nil {
return err
}
e.streamID = stream.ID
log.L(e.ctx).Infof("Event stream: %s (topic=%s)", e.streamID, e.topic)
e.closed = make(chan struct{})
go e.eventLoop()
return nil
}
func (e *Ethereum) SetHandler(namespace string, handler blockchain.Callbacks) {
e.callbacks.SetHandler(namespace, handler)
}
func (e *Ethereum) SetOperationHandler(namespace string, handler core.OperationCallbacks) {
e.callbacks.SetOperationalHandler(namespace, handler)
}
func (e *Ethereum) Start() (err error) {
return e.wsconn.Connect()
}
func (e *Ethereum) Capabilities() *blockchain.Capabilities {
return e.capabilities
}
func (e *Ethereum) AddFireflySubscription(ctx context.Context, namespace *core.Namespace, location *fftypes.JSONAny, firstEvent string) (string, error) {
ethLocation, err := parseContractLocation(ctx, location)
if err != nil {
return "", err
}
version, err := e.GetNetworkVersion(ctx, location)
if err != nil {
return "", err
}
sub, err := e.streams.ensureFireFlySubscription(ctx, namespace.Name, version, ethLocation.Address, firstEvent, e.streamID, batchPinEventABI)
if err != nil {
return "", err
}
e.subs.AddSubscription(ctx, namespace, version, sub.ID, nil)
return sub.ID, nil
}
func (e *Ethereum) RemoveFireflySubscription(ctx context.Context, subID string) {
// Don't actually delete the subscription from ethconnect, as this may be called while processing
// events from the subscription (and handling that scenario cleanly could be difficult for ethconnect).
// TODO: can old subscriptions be somehow cleaned up later?
e.subs.RemoveSubscription(ctx, subID)
}
func (e *Ethereum) afterConnect(ctx context.Context, w wsclient.WSClient) error {
// Send a subscribe to our topic after each connect/reconnect
b, _ := json.Marshal(ðWSCommandPayload{
Type: "listen",
Topic: e.topic,
})
err := w.Send(ctx, b)
if err == nil {
b, _ = json.Marshal(ðWSCommandPayload{
Type: "listenreplies",
})
err = w.Send(ctx, b)
}
return err
}
func ethHexFormatB32(b *fftypes.Bytes32) string {
if b == nil {
return "0x0000000000000000000000000000000000000000000000000000000000000000"
}
return "0x" + hex.EncodeToString(b[0:32])
}
func (e *Ethereum) parseBlockchainEvent(ctx context.Context, msgJSON fftypes.JSONObject) *blockchain.Event {
sBlockNumber := msgJSON.GetString("blockNumber")
sTransactionHash := msgJSON.GetString("transactionHash")
blockNumber := msgJSON.GetInt64("blockNumber")
txIndex := msgJSON.GetInt64("transactionIndex")
logIndex := msgJSON.GetInt64("logIndex")
dataJSON := msgJSON.GetObject("data")
signature := msgJSON.GetString("signature")
name := strings.SplitN(signature, "(", 2)[0]
timestampStr := msgJSON.GetString("timestamp")
timestamp, err := fftypes.ParseTimeString(timestampStr)
if err != nil {
log.L(ctx).Errorf("Blockchain event is not valid - missing timestamp: %+v", msgJSON)
return nil // move on
}
if sBlockNumber == "" || sTransactionHash == "" {
log.L(ctx).Errorf("Blockchain event is not valid - missing data: %+v", msgJSON)
return nil // move on
}
delete(msgJSON, "data")
return &blockchain.Event{
BlockchainTXID: sTransactionHash,
Source: e.Name(),
Name: name,
ProtocolID: fmt.Sprintf("%.12d/%.6d/%.6d", blockNumber, txIndex, logIndex),
Output: dataJSON,
Info: msgJSON,
Timestamp: timestamp,
Location: e.buildEventLocationString(msgJSON),
Signature: signature,
}
}
func (e *Ethereum) handleBatchPinEvent(ctx context.Context, location *fftypes.JSONAny, subInfo *common.SubscriptionInfo, msgJSON fftypes.JSONObject) (err error) {
event := e.parseBlockchainEvent(ctx, msgJSON)
if event == nil {
return nil // move on
}
authorAddress := event.Output.GetString("author")
nsOrAction := event.Output.GetString("action")
if nsOrAction == "" {
nsOrAction = event.Output.GetString("namespace")
}
params := &common.BatchPinParams{
UUIDs: event.Output.GetString("uuids"),
BatchHash: event.Output.GetString("batchHash"),
PayloadRef: event.Output.GetString("payloadRef"),
Contexts: event.Output.GetStringArray("contexts"),
}
authorAddress, err = e.NormalizeSigningKey(ctx, authorAddress)
if err != nil {
log.L(ctx).Errorf("BatchPin event is not valid - bad from address (%s): %+v", err, msgJSON)
return nil // move on
}
verifier := &core.VerifierRef{
Type: core.VerifierTypeEthAddress,
Value: authorAddress,
}
return e.callbacks.BatchPinOrNetworkAction(ctx, nsOrAction, subInfo, location, event, verifier, params)
}
func (e *Ethereum) handleContractEvent(ctx context.Context, msgJSON fftypes.JSONObject) (err error) {
subName, err := e.streams.getSubscriptionName(ctx, msgJSON.GetString("subId"))
if err != nil {
return err
}
namespace := common.GetNamespaceFromSubName(subName)
event := e.parseBlockchainEvent(ctx, msgJSON)
if event != nil {
err = e.callbacks.BlockchainEvent(ctx, namespace, &blockchain.EventWithSubscription{
Event: *event,
Subscription: msgJSON.GetString("subId"),
})
}
return err
}
func (e *Ethereum) handleReceipt(ctx context.Context, reply fftypes.JSONObject) {
l := log.L(ctx)
headers := reply.GetObject("headers")
requestID := headers.GetString("requestId")
replyType := headers.GetString("type")
txHash := reply.GetString("transactionHash")
message := reply.GetString("errorMessage")
if requestID == "" || replyType == "" {
l.Errorf("Reply cannot be processed - missing fields: %+v", reply)
return
}
var updateType core.OpStatus
switch replyType {
case "TransactionSuccess":
updateType = core.OpStatusSucceeded
case "TransactionUpdate":
updateType = core.OpStatusPending
default:
updateType = core.OpStatusFailed
}
l.Infof("Received operation update: status=%s request=%s tx=%s message=%s", updateType, requestID, txHash, message)
e.callbacks.OperationUpdate(ctx, e, requestID, updateType, txHash, message, reply)
}
func (e *Ethereum) buildEventLocationString(msgJSON fftypes.JSONObject) string {
return fmt.Sprintf("address=%s", msgJSON.GetString("address"))
}
func (e *Ethereum) handleMessageBatch(ctx context.Context, messages []interface{}) error {
for i, msgI := range messages {
msgMap, ok := msgI.(map[string]interface{})
if !ok {
log.L(ctx).Errorf("Message cannot be parsed as JSON: %+v", msgI)
return nil // Swallow this and move on
}
msgJSON := fftypes.JSONObject(msgMap)
logger := log.L(ctx).WithField("ethmsgidx", i)
eventCtx, done := context.WithCancel(log.WithLogger(ctx, logger))
signature := msgJSON.GetString("signature")
sub := msgJSON.GetString("subId")
logger.Infof("Received '%s' message on '%s'", signature, sub)
logger.Tracef("Message: %+v", msgJSON)
// Matches one of the active FireFly BatchPin subscriptions
if subInfo := e.subs.GetSubscription(sub); subInfo != nil {
location, err := encodeContractLocation(ctx, &Location{
Address: msgJSON.GetString("address"),
})
if err != nil {
done()
return err
}
firstColon := strings.Index(signature, ":")
if firstColon >= 0 {
signature = signature[firstColon+1:]
}
switch signature {
case broadcastBatchEventSignature:
if err := e.handleBatchPinEvent(eventCtx, location, subInfo, msgJSON); err != nil {
done()
return err
}
default:
log.L(ctx).Infof("Ignoring event with unknown signature: %s", signature)
}
} else {
// Subscription not recognized - assume it's from a custom contract listener
// (event manager will reject it if it's not)
if err := e.handleContractEvent(eventCtx, msgJSON); err != nil {
done()
return err
}
}
done()
}
return nil
}
func (e *Ethereum) eventLoop() {
defer e.wsconn.Close()
defer close(e.closed)
l := log.L(e.ctx).WithField("role", "event-loop")
ctx := log.WithLogger(e.ctx, l)
for {
select {
case <-ctx.Done():
l.Debugf("Event loop exiting (context cancelled)")
return
case msgBytes, ok := <-e.wsconn.Receive():
if !ok {
l.Debugf("Event loop exiting (receive channel closed). Terminating server!")
e.cancelCtx()
return
}
var msgParsed interface{}
err := json.Unmarshal(msgBytes, &msgParsed)
if err != nil {
l.Errorf("Message cannot be parsed as JSON: %s\n%s", err, string(msgBytes))
continue // Swallow this and move on
}
switch msgTyped := msgParsed.(type) {
case []interface{}:
err = e.handleMessageBatch(ctx, msgTyped)
if err == nil {
ack, _ := json.Marshal(ðWSCommandPayload{
Type: "ack",
Topic: e.topic,
})
err = e.wsconn.Send(ctx, ack)
}
case map[string]interface{}:
isBatch := false
if batchNumber, ok := msgTyped["batchNumber"].(float64); ok {
if events, ok := msgTyped["events"].([]interface{}); ok {
// FFTM delivery with a batch number to use in the ack
isBatch = true
err = e.handleMessageBatch(ctx, events)
if err == nil {
ack, _ := json.Marshal(ðWSCommandPayload{
Type: "ack",
Topic: e.topic,
BatchNumber: int64(batchNumber),
})
err = e.wsconn.Send(ctx, ack)
}
}
}
if !isBatch {
e.handleReceipt(ctx, fftypes.JSONObject(msgTyped))
}
default:
l.Errorf("Message unexpected: %+v", msgTyped)
continue
}
if err != nil {
l.Errorf("Event loop exiting (%s). Terminating server!", err)
e.cancelCtx()
return
}
}
}
}
func validateEthAddress(ctx context.Context, key string) (string, error) {
keyLower := strings.ToLower(key)
keyNoHexPrefix := strings.TrimPrefix(keyLower, "0x")
if addressVerify.MatchString(keyNoHexPrefix) {
return "0x" + keyNoHexPrefix, nil
}
return "", i18n.NewError(ctx, coremsgs.MsgInvalidEthAddress)
}
func (e *Ethereum) NormalizeSigningKey(ctx context.Context, key string) (string, error) {
resolved, err := validateEthAddress(ctx, key)
if err != nil && e.addressResolver != nil {
resolved, err := e.addressResolver.NormalizeSigningKey(ctx, key)
if err == nil {
log.L(ctx).Infof("Key '%s' resolved to '%s'", key, resolved)
}
return resolved, err
}
return resolved, err
}
func wrapError(ctx context.Context, errRes *ethError, res *resty.Response, err error) error {
if errRes != nil && errRes.Error != "" {
return i18n.WrapError(ctx, err, coremsgs.MsgEthconnectRESTErr, errRes.Error)
}
return ffresty.WrapRestErr(ctx, res, err, coremsgs.MsgEthconnectRESTErr)
}
func (e *Ethereum) buildEthconnectRequestBody(ctx context.Context, messageType, address, signingKey string, abi *abi.Entry, requestID string, input []interface{}, options map[string]interface{}) (map[string]interface{}, error) {
headers := EthconnectMessageHeaders{
Type: messageType,
}
if requestID != "" {
headers.ID = requestID
}
body := map[string]interface{}{
"headers": headers,
"to": address,
"method": abi,
"params": input,
}
if signingKey != "" {
body["from"] = signingKey
}
for k, v := range options {
// Set the new field if it's not already set. Do not allow overriding of existing fields
if _, ok := body[k]; !ok {
body[k] = v
} else {
return nil, i18n.NewError(ctx, coremsgs.MsgOverrideExistingFieldCustomOption, k)
}
}
return body, nil
}
func (e *Ethereum) invokeContractMethod(ctx context.Context, address, signingKey string, abi *abi.Entry, requestID string, input []interface{}, options map[string]interface{}) error {
if e.metrics.IsMetricsEnabled() {
e.metrics.BlockchainTransaction(address, abi.Name)
}
messageType := "SendTransaction"
body, err := e.buildEthconnectRequestBody(ctx, messageType, address, signingKey, abi, requestID, input, options)
if err != nil {
return err
}
client := e.fftmClient
if client == nil {
client = e.client
}
var resErr ethError
res, err := client.R().
SetContext(ctx).
SetBody(body).
SetError(&resErr).
Post("/")
if err != nil || !res.IsSuccess() {
return wrapError(ctx, &resErr, res, err)
}
return nil
}
func (e *Ethereum) queryContractMethod(ctx context.Context, address string, abi *abi.Entry, input []interface{}, options map[string]interface{}) (*resty.Response, error) {
if e.metrics.IsMetricsEnabled() {
e.metrics.BlockchainQuery(address, abi.Name)
}
messageType := "Query"
body, err := e.buildEthconnectRequestBody(ctx, messageType, address, "", abi, "", input, options)
if err != nil {
return nil, err
}
var resErr ethError
res, err := e.client.R().
SetContext(ctx).
SetBody(body).
SetError(&resErr).
Post("/")
if err != nil || !res.IsSuccess() {
return res, wrapError(ctx, &resErr, res, err)
}
return res, nil
}
func (e *Ethereum) SubmitBatchPin(ctx context.Context, nsOpID, networkNamespace, signingKey string, batch *blockchain.BatchPin, location *fftypes.JSONAny) error {
ethLocation, err := parseContractLocation(ctx, location)
if err != nil {
return err
}
ethHashes := make([]string, len(batch.Contexts))
for i, v := range batch.Contexts {
ethHashes[i] = ethHexFormatB32(v)
}
var uuids fftypes.Bytes32
copy(uuids[0:16], (*batch.TransactionID)[:])
copy(uuids[16:32], (*batch.BatchID)[:])
version, err := e.GetNetworkVersion(ctx, location)
if err != nil {
return err
}
var input []interface{}
var method *abi.Entry
if version == 1 {
method = batchPinMethodABIV1
input = []interface{}{
networkNamespace,
ethHexFormatB32(&uuids),
ethHexFormatB32(batch.BatchHash),
batch.BatchPayloadRef,
ethHashes,
}
} else {
method = batchPinMethodABI
input = []interface{}{
ethHexFormatB32(&uuids),
ethHexFormatB32(batch.BatchHash),
batch.BatchPayloadRef,
ethHashes,
}
}
return e.invokeContractMethod(ctx, ethLocation.Address, signingKey, method, nsOpID, input, nil)
}
func (e *Ethereum) SubmitNetworkAction(ctx context.Context, nsOpID string, signingKey string, action core.NetworkActionType, location *fftypes.JSONAny) error {
ethLocation, err := parseContractLocation(ctx, location)
if err != nil {
return err
}
version, err := e.GetNetworkVersion(ctx, location)
if err != nil {
return err
}
var input []interface{}
var method *abi.Entry
if version == 1 {
method = batchPinMethodABIV1
input = []interface{}{
blockchain.FireFlyActionPrefix + action,
ethHexFormatB32(nil),
ethHexFormatB32(nil),
"",
[]string{},
}
} else {
method = networkActionMethodABI
input = []interface{}{
blockchain.FireFlyActionPrefix + action,
"",
}
}
return e.invokeContractMethod(ctx, ethLocation.Address, signingKey, method, nsOpID, input, nil)
}
func (e *Ethereum) InvokeContract(ctx context.Context, nsOpID string, signingKey string, location *fftypes.JSONAny, method *fftypes.FFIMethod, input map[string]interface{}, options map[string]interface{}) error {
ethereumLocation, err := parseContractLocation(ctx, location)
if err != nil {
return err
}
abi, orderedInput, err := e.prepareRequest(ctx, method, input)
if err != nil {
return err
}
return e.invokeContractMethod(ctx, ethereumLocation.Address, signingKey, abi, nsOpID, orderedInput, options)
}
func (e *Ethereum) QueryContract(ctx context.Context, location *fftypes.JSONAny, method *fftypes.FFIMethod, input map[string]interface{}, options map[string]interface{}) (interface{}, error) {
ethereumLocation, err := parseContractLocation(ctx, location)
if err != nil {
return nil, err
}
abi, orderedInput, err := e.prepareRequest(ctx, method, input)
if err != nil {
return nil, err
}
res, err := e.queryContractMethod(ctx, ethereumLocation.Address, abi, orderedInput, options)
if err != nil || !res.IsSuccess() {
return nil, err
}
output := &queryOutput{}
if err = json.Unmarshal(res.Body(), output); err != nil {
return nil, err
}
return output, nil
}
func (e *Ethereum) NormalizeContractLocation(ctx context.Context, location *fftypes.JSONAny) (result *fftypes.JSONAny, err error) {
parsed, err := parseContractLocation(ctx, location)
if err != nil {
return nil, err
}
return encodeContractLocation(ctx, parsed)
}
func parseContractLocation(ctx context.Context, location *fftypes.JSONAny) (*Location, error) {
ethLocation := Location{}
if err := json.Unmarshal(location.Bytes(), ðLocation); err != nil {
return nil, i18n.NewError(ctx, coremsgs.MsgContractLocationInvalid, err)
}
if ethLocation.Address == "" {
return nil, i18n.NewError(ctx, coremsgs.MsgContractLocationInvalid, "'address' not set")
}
return ðLocation, nil
}
func encodeContractLocation(ctx context.Context, location *Location) (result *fftypes.JSONAny, err error) {
location.Address, err = validateEthAddress(ctx, location.Address)
if err != nil {
return nil, err
}
normalized, err := json.Marshal(location)
if err == nil {
result = fftypes.JSONAnyPtrBytes(normalized)
}
return result, err
}
func (e *Ethereum) AddContractListener(ctx context.Context, listener *core.ContractListenerInput) (err error) {
var location *Location
if listener.Location != nil {
location, err = parseContractLocation(ctx, listener.Location)
if err != nil {
return err
}
}
abi, err := ffi2abi.ConvertFFIEventDefinitionToABI(ctx, &listener.Event.FFIEventDefinition)
if err != nil {
return i18n.WrapError(ctx, err, coremsgs.MsgContractParamInvalid)
}
subName := fmt.Sprintf("ff-sub-%s-%s", listener.Namespace, listener.ID)
firstEvent := string(core.SubOptsFirstEventNewest)
if listener.Options != nil {
firstEvent = listener.Options.FirstEvent
}
result, err := e.streams.createSubscription(ctx, location, e.streamID, subName, firstEvent, abi)
if err != nil {
return err
}
listener.BackendID = result.ID
return nil
}
func (e *Ethereum) DeleteContractListener(ctx context.Context, subscription *core.ContractListener) error {
return e.streams.deleteSubscription(ctx, subscription.BackendID)
}
func (e *Ethereum) GetContractListenerStatus(ctx context.Context, subID string) (status interface{}, err error) {
sub, err := e.streams.getSubscription(ctx, subID)
if err != nil {
return nil, err
}
checkpoint := &ListenerStatus{
Catchup: sub.Catchup,
Checkpoint: ListenerCheckpoint{
Block: sub.Checkpoint.Block,
TransactionIndex: sub.Checkpoint.TransactionIndex,
LogIndex: sub.Checkpoint.LogIndex,
},
}
return checkpoint, nil
}
func (e *Ethereum) GetFFIParamValidator(ctx context.Context) (fftypes.FFIParamValidator, error) {
return &ffi2abi.ParamValidator{}, nil
}
func (e *Ethereum) GenerateEventSignature(ctx context.Context, event *fftypes.FFIEventDefinition) string {
abi, err := ffi2abi.ConvertFFIEventDefinitionToABI(ctx, event)
if err != nil {
return ""
}
return ffi2abi.ABIMethodToSignature(abi)
}
func (e *Ethereum) prepareRequest(ctx context.Context, method *fftypes.FFIMethod, input map[string]interface{}) (*abi.Entry, []interface{}, error) {
orderedInput := make([]interface{}, len(method.Params))
abi, err := ffi2abi.ConvertFFIMethodToABI(ctx, method)
if err != nil {
return abi, orderedInput, err
}
for i, ffiParam := range method.Params {
orderedInput[i] = input[ffiParam.Name]
}
return abi, orderedInput, nil
}
func (e *Ethereum) getContractAddress(ctx context.Context, instancePath string) (string, error) {
res, err := e.client.R().
SetContext(ctx).
Get(instancePath)
if err != nil || !res.IsSuccess() {
return "", ffresty.WrapRestErr(ctx, res, err, coremsgs.MsgEthconnectRESTErr)
}
var output map[string]string
if err = json.Unmarshal(res.Body(), &output); err != nil {
return "", err
}
return output["address"], nil
}
func (e *Ethereum) GenerateFFI(ctx context.Context, generationRequest *fftypes.FFIGenerationRequest) (*fftypes.FFI, error) {
var input FFIGenerationInput
err := json.Unmarshal(generationRequest.Input.Bytes(), &input)
if err != nil {
return nil, i18n.WrapError(ctx, err, coremsgs.MsgFFIGenerationFailed, "unable to deserialize JSON as ABI")
}
if len(*input.ABI) == 0 {
return nil, i18n.NewError(ctx, coremsgs.MsgFFIGenerationFailed, "ABI is empty")
}
return ffi2abi.ConvertABIToFFI(ctx, generationRequest.Namespace, generationRequest.Name, generationRequest.Version, generationRequest.Description, input.ABI)
}
func (e *Ethereum) GetNetworkVersion(ctx context.Context, location *fftypes.JSONAny) (version int, err error) {
ethLocation, err := parseContractLocation(ctx, location)
if err != nil {
return 0, err
}
cacheKey := "version:" + ethLocation.Address
if cachedValue := e.cache.GetInt(cacheKey); cachedValue != 0 {
return cachedValue, nil
}
version, err = e.queryNetworkVersion(ctx, ethLocation.Address)
if err == nil {
e.cache.SetInt(cacheKey, version)
}
return version, err
}
func (e *Ethereum) queryNetworkVersion(ctx context.Context, address string) (version int, err error) {
res, err := e.queryContractMethod(ctx, address, networkVersionMethodABI, []interface{}{}, nil)
if err != nil || !res.IsSuccess() {
// "Call failed" is interpreted as "method does not exist, default to version 1"
if strings.Contains(err.Error(), "FFEC100148") {
return 1, nil
}
return 0, err
}
output := &queryOutput{}
if err = json.Unmarshal(res.Body(), output); err != nil {
return 0, err
}
switch result := output.Output.(type) {
case string:
version, err = strconv.Atoi(result)
default:
err = i18n.NewError(ctx, coremsgs.MsgBadNetworkVersion, output.Output)
}
return version, err
}
func (e *Ethereum) GetAndConvertDeprecatedContractConfig(ctx context.Context) (location *fftypes.JSONAny, fromBlock string, err error) {
// Old config (attributes under "ethconnect")
address := e.ethconnectConf.GetString(EthconnectConfigInstanceDeprecated)
if address != "" {
log.L(ctx).Warnf("The %s.%s config key has been deprecated. Please use namespaces.predefined[].multiparty.contract[].location.address instead",
EthconnectConfigKey, EthconnectConfigInstanceDeprecated)
} else {
return nil, "", i18n.NewError(ctx, coremsgs.MsgMissingPluginConfig, "instance", "blockchain.ethereum.ethconnect")
}
fromBlock = e.ethconnectConf.GetString(EthconnectConfigFromBlockDeprecated)
if fromBlock != "" {
log.L(ctx).Warnf("The %s.%s config key has been deprecated. Please use namespaces.predefined[].multiparty.contract[].location.firstEvent instead",
EthconnectConfigKey, EthconnectConfigFromBlockDeprecated)
}
// Backwards compatibility from when instance path was not a contract address
if strings.HasPrefix(strings.ToLower(address), "/contracts/") {
address, err = e.getContractAddress(ctx, address)
if err != nil {
return nil, "", err
}
} else if strings.HasPrefix(address, "/instances/") {
address = strings.Replace(address, "/instances/", "", 1)
}
location, err = encodeContractLocation(ctx, &Location{
Address: address,
})
return location, fromBlock, err
}