forked from hyperledger-archives/fabric-chaincode-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethservice.go
788 lines (667 loc) · 24.6 KB
/
ethservice.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fab3
import (
"bytes"
"crypto/sha256"
"encoding/asn1"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"net/http"
"strconv"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric-chaincode-evm/event"
"github.com/hyperledger/fabric-chaincode-evm/fab3/types"
)
var ZeroAddress = make([]byte, 20)
//go:generate counterfeiter -o ../mocks/fab3/mockchannelclient.go --fake-name MockChannelClient ./ ChannelClient
type ChannelClient interface {
Query(request channel.Request, options ...channel.RequestOption) (channel.Response, error)
Execute(request channel.Request, options ...channel.RequestOption) (channel.Response, error)
}
//go:generate counterfeiter -o ../mocks/fab3/mockledgerclient.go --fake-name MockLedgerClient ./ LedgerClient
type LedgerClient interface {
QueryInfo(options ...ledger.RequestOption) (*fab.BlockchainInfoResponse, error)
QueryBlock(blockNumber uint64, options ...ledger.RequestOption) (*common.Block, error)
QueryBlockByTxID(txid fab.TransactionID, options ...ledger.RequestOption) (*common.Block, error)
QueryBlockByHash(blockHash []byte, options ...ledger.RequestOption) (*common.Block, error)
}
//go:generate counterfeiter -o ../mocks/fab3/mockethservice.go --fake-name MockEthService ./ EthService
// EthService is the rpc server implementation. Each function is an
// implementation of one ethereum json-rpc
// https://github.com/ethereum/wiki/wiki/JSON-RPC
//
// Arguments and return values are formatted as HEX value encoding
// https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
//
// gorilla RPC is the receiver of these functions, they must all take three
// pointers, and return a single error
//
// see godoc for RegisterService(receiver interface{}, name string) error
//
type EthService interface {
GetCode(r *http.Request, arg *string, reply *string) error
Call(r *http.Request, args *types.EthArgs, reply *string) error
SendTransaction(r *http.Request, args *types.EthArgs, reply *string) error
GetTransactionReceipt(r *http.Request, arg *string, reply *types.TxReceipt) error
Accounts(r *http.Request, arg *string, reply *[]string) error
EstimateGas(r *http.Request, args *types.EthArgs, reply *string) error
GetBalance(r *http.Request, p *[]string, reply *string) error
GetBlockByNumber(r *http.Request, p *[]interface{}, reply *types.Block) error
BlockNumber(r *http.Request, _ *interface{}, reply *string) error
GetTransactionByHash(r *http.Request, txID *string, reply *types.Transaction) error
GetTransactionCount(r *http.Request, _ *interface{}, reply *string) error
GetLogs(*http.Request, *types.GetLogsArgs, *[]types.Log) error
}
type ethService struct {
channelClient ChannelClient
ledgerClient LedgerClient
channelID string
ccid string
logger *zap.SugaredLogger
}
func NewEthService(channelClient ChannelClient, ledgerClient LedgerClient, channelID string, ccid string, logger *zap.SugaredLogger) EthService {
return ðService{channelClient: channelClient, ledgerClient: ledgerClient, channelID: channelID, ccid: ccid, logger: logger.Named("ethservice")}
}
func (s *ethService) GetCode(r *http.Request, arg *string, reply *string) error {
strippedAddr := strip0x(*arg)
response, err := s.query(s.ccid, "getCode", [][]byte{[]byte(strippedAddr)})
if err != nil {
return fmt.Errorf("Failed to query the ledger: %s", err)
}
*reply = string(response.Payload)
return nil
}
func (s *ethService) Call(r *http.Request, args *types.EthArgs, reply *string) error {
response, err := s.query(s.ccid, strip0x(args.To), [][]byte{[]byte(strip0x(args.Data))})
if err != nil {
return fmt.Errorf("Failed to query the ledger: %s", err)
}
// Clients expect the prefix to present in responses
*reply = "0x" + hex.EncodeToString(response.Payload)
return nil
}
func (s *ethService) SendTransaction(r *http.Request, args *types.EthArgs, reply *string) error {
if args.To == "" {
args.To = hex.EncodeToString(ZeroAddress)
}
response, err := s.channelClient.Execute(channel.Request{
ChaincodeID: s.ccid,
Fcn: strip0x(args.To),
Args: [][]byte{[]byte(strip0x(args.Data))},
})
if err != nil {
return fmt.Errorf("Failed to execute transaction: %s", err)
}
*reply = string(response.TransactionID)
return nil
}
func (s *ethService) GetTransactionReceipt(r *http.Request, txID *string, reply *types.TxReceipt) error {
logger := s.logger.With("method", "GetTransactionReceipt")
strippedTxID := strip0x(*txID)
block, err := s.ledgerClient.QueryBlockByTxID(fab.TransactionID(strippedTxID))
if err != nil {
return fmt.Errorf("Failed to query the ledger: %s", err)
}
blkHeader := block.GetHeader()
transactionsFilter := block.GetMetadata().GetMetadata()[common.BlockMetadataIndex_TRANSACTIONS_FILTER]
receipt := types.TxReceipt{
TransactionHash: "0x" + strippedTxID,
BlockHash: "0x" + hex.EncodeToString(blockHash(blkHeader)),
BlockNumber: "0x" + strconv.FormatUint(blkHeader.GetNumber(), 16),
GasUsed: 0,
CumulativeGasUsed: 0,
}
index, txPayload, err := findTransaction(strippedTxID, block.GetData().GetData())
if err != nil {
return fmt.Errorf("Failed parsing the transactions in the block: %s", err)
}
receipt.TransactionIndex = index
indexU, _ := strconv.ParseUint(strip0x(index), 16, 64)
// for fabric transactions, 0 is valid, 1 is invalid, the opposite of how ethereum
receipt.Status = "0x" + strconv.FormatUint(((1+uint64(transactionsFilter[indexU]))%2), 16)
to, _, respPayload, err := getTransactionInformation(txPayload)
if to != "" {
callee, err := hex.DecodeString(to)
if err != nil {
return fmt.Errorf("Failed to decode to address: %s", err)
}
if bytes.Equal(callee, ZeroAddress) {
receipt.ContractAddress = "0x" + string(respPayload.GetResponse().GetPayload())
} else {
receipt.To = "0x" + to
}
}
txLogs, err := fabricEventToEVMLogs(logger, respPayload.Events, receipt.BlockNumber, receipt.TransactionHash,
receipt.TransactionIndex, receipt.BlockHash, types.AddressFilter{}, types.TopicsFilter{})
if err != nil {
return errors.Wrap(err, "failed to get EVM Logs out of fabric event")
}
receipt.Logs = txLogs
*reply = receipt
return nil
}
func (s *ethService) Accounts(r *http.Request, arg *string, reply *[]string) error {
response, err := s.query(s.ccid, "account", [][]byte{})
if err != nil {
return fmt.Errorf("Failed to query the ledger: %s", err)
}
*reply = []string{"0x" + strings.ToLower(string(response.Payload))}
return nil
}
// EstimateGas accepts the same arguments as Call but all arguments are
// optional. This implementation ignores all arguments and returns a zero
// estimate.
//
// The intention is to estimate how much gas is necessary to allow a transaction
// to complete.
//
// EVM-chaincode does not require gas to run transactions. The chaincode will
// give enough gas per transaction.
func (s *ethService) EstimateGas(r *http.Request, _ *types.EthArgs, reply *string) error {
s.logger.Debug("EstimateGas called")
*reply = "0x0"
return nil
}
// GetBalance takes an address and a block, but this implementation
// does not check or use either parameter.
//
// Always returns zero.
func (s *ethService) GetBalance(r *http.Request, p *[]string, reply *string) error {
s.logger.Debug("GetBalance called")
*reply = "0x0"
return nil
}
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getblockbynumber
func (s *ethService) GetBlockByNumber(r *http.Request, p *[]interface{}, reply *types.Block) error {
s.logger.Debug("Received a request for GetBlockByNumber")
params := *p
s.logger.Debug("Params are : ", params)
// handle params
// must have two params
numParams := len(params)
if numParams != 2 {
return fmt.Errorf("need 2 params, got %q", numParams)
}
// first arg is string of block to get
number, ok := params[0].(string)
if !ok {
s.logger.Debugf("Incorrect argument received: %#v", params[0])
return fmt.Errorf("Incorrect first parameter sent, must be string")
}
// second arg is bool for full txn or hash txn
fullTransactions, ok := params[1].(bool)
if !ok {
return fmt.Errorf("Incorrect second parameter sent, must be boolean")
}
parsedNumber, err := s.parseBlockNum(strip0x(number))
if err != nil {
return err
}
block, err := s.ledgerClient.QueryBlock(parsedNumber)
if err != nil {
return fmt.Errorf("Failed to query the ledger: %v", err)
}
blkHeader := block.GetHeader()
blockHash := "0x" + hex.EncodeToString(blockHash(blkHeader))
blockNumber := "0x" + strconv.FormatUint(parsedNumber, 16)
// each data is a txn
data := block.GetData().GetData()
txns := make([]interface{}, len(data))
// drill into the block to find the transaction ids it contains
for index, transactionData := range data {
if transactionData == nil {
continue
}
payload, chdr, err := getChannelHeaderandPayloadFromTransactionData(transactionData)
if err != nil {
return err
}
// returning full transactions is unimplemented,
// so the hash-only case is the only case.
s.logger.Debug("block has transaction hash:", chdr.TxId)
if fullTransactions {
txn := types.Transaction{
BlockHash: blockHash,
BlockNumber: blockNumber,
TransactionIndex: "0x" + strconv.FormatUint(uint64(index), 16),
Hash: "0x" + chdr.TxId,
}
to, input, _, err := getTransactionInformation(payload)
if err != nil {
return err
}
txn.To = "0x" + to
txn.Input = "0x" + input
txns[index] = txn
} else {
txns[index] = "0x" + chdr.TxId
}
}
blk := types.Block{
Number: blockNumber,
Hash: blockHash,
ParentHash: "0x" + hex.EncodeToString(blkHeader.GetPreviousHash()),
Transactions: txns,
}
s.logger.Debug("asked for block", number, "found block", blk)
*reply = blk
return nil
}
func (s *ethService) BlockNumber(r *http.Request, _ *interface{}, reply *string) error {
blockNumber, err := s.parseBlockNum("latest")
if err != nil {
return fmt.Errorf("failed to get latest block number: %s", err)
}
*reply = "0x" + strconv.FormatUint(uint64(blockNumber), 16)
return nil
}
// GetTransactionByHash takes a TransactionID as a string and returns the
// details of the transaction.
//
// The implementation of this function follows the EVM ChainCode implementation
// of Invoke.
//
// Since this takes only one string, we can have gorilla verify that it has
// received only a single string, and skip our own verification.
//
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gettransactionbyhash
func (s *ethService) GetTransactionByHash(r *http.Request, txID *string, reply *types.Transaction) error {
strippedTxId := strip0x(*txID)
if strippedTxId == "" {
return fmt.Errorf("txID was empty")
}
s.logger.Debug("GetTransactionByHash", strippedTxId) // logging input to function
txn := types.Transaction{
Hash: "0x" + strippedTxId,
}
block, err := s.ledgerClient.QueryBlockByTxID(fab.TransactionID(strippedTxId))
if err != nil {
return fmt.Errorf("Failed to query the ledger: %s", err)
}
blkHeader := block.GetHeader()
txn.BlockHash = "0x" + hex.EncodeToString(blockHash(blkHeader))
txn.BlockNumber = "0x" + strconv.FormatUint(blkHeader.GetNumber(), 16)
index, txPayload, err := findTransaction(strippedTxId, block.GetData().GetData())
if err != nil {
return fmt.Errorf("Failed to parse through transactions in the block: %s", err)
}
txn.TransactionIndex = index
to, input, _, err := getTransactionInformation(txPayload)
if err != nil {
return err
}
if to != "" {
txn.To = "0x" + to
}
if input != "" {
txn.Input = "0x" + input
}
*reply = txn
return nil
}
// GetTransactionCount will always return 0 regardless of the provided parameters.
//
// This implmentation does not match the Ethereum JSON RPC API definition of eth_getTransactionCount.
// This has been stubbed out in order to have Truffle IDE support.
func (s *ethService) GetTransactionCount(r *http.Request, _ *interface{}, reply *string) error {
*reply = "0x0"
return nil
}
// GetLogs returns matching logs in range FromBlock to ToBlock. If BlockHash is specified, the
// single matching block is searched for logs.
func (s *ethService) GetLogs(r *http.Request, args *types.GetLogsArgs, logs *[]types.Log) error {
logger := s.logger.With("method", "GetLogs")
logger.Debug("parameters", args)
var from, to uint64
var err error
if args.BlockHash != "" {
hash, err := hex.DecodeString(args.BlockHash)
if err != nil {
return errors.Wrap(err, "failed to parse the block hash")
}
block, err := s.ledgerClient.QueryBlockByHash(hash)
if err != nil {
return errors.Wrap(err, "failed to find block by block hash")
}
blockNumber := block.Header.Number
from = blockNumber
to = blockNumber
} else {
// set defaults *after* checking for input conflicts and validating
if args.FromBlock == "" {
args.FromBlock = "latest"
}
if args.ToBlock == "" {
args.ToBlock = "latest"
}
from, err = s.parseBlockNum(args.FromBlock)
if err != nil {
return errors.Wrap(err, "failed to parse the block number")
}
// check if both from and to are the same to avoid doing two
// queries to the fabric network.
if args.FromBlock == args.ToBlock {
to = from
} else {
to, err = s.parseBlockNum(args.ToBlock)
if err != nil {
return errors.Wrap(err, "failed to parse the block number")
}
}
if from > to {
return fmt.Errorf("fromBlock number greater than toBlock number")
}
}
var txLogs []types.Log
logger.Debugw("checking blocks for logs", "from", from, "to", to)
for blockNumber := from; blockNumber <= to; blockNumber++ {
logger = logger.With("block-number", blockNumber)
logger.Debug("handling single block")
block, err := s.ledgerClient.QueryBlock(blockNumber)
if err != nil {
return errors.Wrap(err, "failed to query the ledger")
}
blockHeader := block.GetHeader()
blockHash := "0x" + hex.EncodeToString(blockHash(blockHeader))
blockData := block.GetData().GetData()
transactionsFilter := block.GetMetadata().GetMetadata()[common.BlockMetadataIndex_TRANSACTIONS_FILTER]
logger.Debug("handling ", len(blockData), " transactions in block")
for transactionIndex, transactionData := range blockData {
logger = logger.With("transaction-index", transactionIndex)
// check validity of transaction
if (transactionsFilter[transactionIndex] == 1) || (transactionData == nil) {
continue
}
// start processing the transaction
payload, chdr, err := getChannelHeaderandPayloadFromTransactionData(transactionData)
if err != nil {
return errors.Wrap(err, "failed to unmarshal the transaction")
}
// only process transactions
if chdr.Type != int32(common.HeaderType_ENDORSER_TRANSACTION) {
logger.Debug("skipping non-ENDORSER_TRANSACTION")
continue
}
transactionHash := "0x" + chdr.TxId
logger.Debug("transaction ", transactionIndex, " has hash ", transactionHash)
var respPayload *peer.ChaincodeAction
_, _, respPayload, err = getTransactionInformation(payload)
if err != nil {
return errors.Wrap(err, "failed to unmarshal the transaction details")
}
blkNumber := "0x" + strconv.FormatUint(blockNumber, 16)
transactionIndexStr := "0x" + strconv.FormatUint(uint64(transactionIndex), 16)
logs, err := fabricEventToEVMLogs(logger, respPayload.Events, blkNumber, transactionHash,
transactionIndexStr, blockHash, args.Address, args.Topics)
if err != nil {
return errors.Wrap(err, "failed to get EVM Logs out of fabric event")
}
txLogs = append(txLogs, logs...)
}
}
logger.Debug("returning logs", txLogs)
*logs = txLogs
return nil
}
func (s *ethService) query(ccid, function string, queryArgs [][]byte) (channel.Response, error) {
return s.channelClient.Query(channel.Request{
ChaincodeID: ccid,
Fcn: function,
Args: queryArgs,
})
}
// https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter
func (s *ethService) parseBlockNum(input string) (uint64, error) {
// check if it's one of the named-blocks
switch input {
case "latest":
// latest
// qscc GetChainInfo, for a BlockchainInfo
// from that take the height
// using the height, call GetBlockByNumber
blockchainInfo, err := s.ledgerClient.QueryInfo()
if err != nil {
s.logger.Debug(err)
return 0, fmt.Errorf("failed to query the ledger: %v", err)
}
// height is the block being worked on now, we want the previous block
topBlockNumber := blockchainInfo.BCI.GetHeight() - 1
return topBlockNumber, nil
case "earliest":
return 0, nil
case "pending":
return 0, fmt.Errorf("unsupported: fabric does not have the concept of in-progress blocks being visible")
default:
return strconv.ParseUint(input, 16, 64)
}
}
func strip0x(addr string) string {
//Not checking for malformed addresses just stripping `0x` prefix where applicable
if len(addr) > 2 && addr[0:2] == "0x" {
return addr[2:]
}
return addr
}
func getPayloads(txActions *peer.TransactionAction) (*peer.ChaincodeProposalPayload, *peer.ChaincodeAction, error) {
// TODO: pass in the tx type (in what follows we're assuming the type is ENDORSER_TRANSACTION)
ccPayload := &peer.ChaincodeActionPayload{}
err := proto.Unmarshal(txActions.Payload, ccPayload)
if err != nil {
return nil, nil, err
}
if ccPayload.Action == nil || ccPayload.Action.ProposalResponsePayload == nil {
return nil, nil, fmt.Errorf("no payload in ChaincodeActionPayload")
}
ccProposalPayload := &peer.ChaincodeProposalPayload{}
err = proto.Unmarshal(ccPayload.ChaincodeProposalPayload, ccProposalPayload)
if err != nil {
return nil, nil, err
}
pRespPayload := &peer.ProposalResponsePayload{}
err = proto.Unmarshal(ccPayload.Action.ProposalResponsePayload, pRespPayload)
if err != nil {
return nil, nil, err
}
if pRespPayload.Extension == nil {
return nil, nil, fmt.Errorf("response payload is missing extension")
}
respPayload := &peer.ChaincodeAction{}
err = proto.Unmarshal(pRespPayload.Extension, respPayload)
if err != nil {
return ccProposalPayload, nil, err
}
return ccProposalPayload, respPayload, nil
}
// getTransactionInformation takes a payload
// It returns if available the To, Input, the Response Payload of the transaction in the payload, otherwise it returns an error
func getTransactionInformation(payload *common.Payload) (string, string, *peer.ChaincodeAction, error) {
txActions := &peer.Transaction{}
err := proto.Unmarshal(payload.GetData(), txActions)
if err != nil {
return "", "", nil, err
}
ccPropPayload, respPayload, err := getPayloads(txActions.GetActions()[0])
if err != nil {
return "", "", nil, fmt.Errorf("Failed to unmarshal transaction: %s", err)
}
invokeSpec := &peer.ChaincodeInvocationSpec{}
err = proto.Unmarshal(ccPropPayload.GetInput(), invokeSpec)
if err != nil {
return "", "", nil, fmt.Errorf("Failed to unmarshal transaction: %s", err)
}
// callee, input data is standard case, also handle getcode & account cases
args := invokeSpec.GetChaincodeSpec().GetInput().Args
if len(args) != 2 || string(args[0]) == "getCode" {
// no more data available to fill the transaction
return "", "", respPayload, nil
}
// At this point, this is either an EVM Contract Deploy,
// or an EVM Contract Invoke. We don't care about the
// specific case, fill in the fields directly.
// First arg is to and second arg is the input data
return string(args[0]), string(args[1]), respPayload, nil
}
// findTransaction takes in the txId and block data from block.GetData().GetData() where block is of type *common.Block
// It returns the index of the transaction, transaction payload, otherwise it returns an error
func findTransaction(txID string, blockData [][]byte) (string, *common.Payload, error) {
for index, transactionData := range blockData {
if transactionData == nil { // can a data be empty? Is this an error?
continue
}
payload, chdr, err := getChannelHeaderandPayloadFromTransactionData(transactionData)
if err != nil {
return "", &common.Payload{}, err
}
// early exit to try next transaction
if txID != chdr.TxId {
// transaction does not match, go to next
continue
}
return "0x" + strconv.FormatUint(uint64(index), 16), payload, nil
}
return "", &common.Payload{}, nil
}
func fabricEventToEVMLogs(logger *zap.SugaredLogger, events []byte, blocknumber, txhash, txindex, blockhash string,
af types.AddressFilter, tf types.TopicsFilter) ([]types.Log, error) {
if len(events) == 0 {
return nil, nil
}
chaincodeEvent := &peer.ChaincodeEvent{}
err := proto.Unmarshal(events, chaincodeEvent)
if err != nil {
return nil, errors.Wrap(err, "failed to decode chaincode event")
}
var eventMsgs []event.Event
err = json.Unmarshal(chaincodeEvent.Payload, &eventMsgs)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal chaincode event payload")
}
var txLogs []types.Log
LOG_EVENT:
for i, logEvent := range eventMsgs {
if len(af) != 0 {
foundMatch := false
// if no address, empty range, skipped, present but empty address field results in no match
for _, address := range af {
logger.Debugw("trying address match", "matcherAddress", address, "eventAddress", logEvent.Address)
if logEvent.Address == address {
foundMatch = true
break
}
}
if !foundMatch {
continue LOG_EVENT // no match, move to next logEvent
}
}
// If we have more matchers than things to match against, we cannot match.
if len(tf) > len(logEvent.Topics) {
continue LOG_EVENT
}
logger.Debug("checking for topics")
// Check match for each topic. This implementation matches behavior we have observed from other implementations.
//
// [] "anything"
// [A] "A in first position (and anything after)"
// [null, B] "anything in first position AND B in second position (and anything after)"
// [A, B] "A in first position AND B in second position (and anything after)"
// [[A, B], [A, B]] "(A OR B) in first position AND (A OR B) in second position (and anything after)"
//
// null matchers can be used to force an event to have at least that many topics
// [] "anything"
// [null] "anything with at least one topic"
// [null, null] "anything with two or more topics"
for i, topicFilter := range tf {
// if filter is empty it matches automatically.
if len(topicFilter) == 0 {
continue
}
eventTopic := logEvent.Topics[i]
foundMatch := false
for _, topic := range topicFilter {
logger.Debugw("matching Topic ", "matcherTopic", topic, "eventTopic", eventTopic)
if topic == eventTopic || topic == "" {
foundMatch = true
break
}
}
if !foundMatch {
// if we didn't find a match, no use in checking any of the other topics
continue LOG_EVENT
}
}
logger.Debug("finished checking for topics")
var topics []string
for _, topic := range logEvent.Topics {
topics = append(topics, "0x"+topic)
}
log := types.Log{
Address: "0x" + logEvent.Address,
Topics: topics,
BlockNumber: blocknumber,
TxHash: txhash,
TxIndex: txindex,
BlockHash: blockhash,
Index: "0x" + strconv.FormatUint(uint64(i), 16),
}
if logEvent.Data != "" {
log.Data = "0x" + logEvent.Data
}
txLogs = append(txLogs, log)
}
return txLogs, nil
}
func getChannelHeaderandPayloadFromTransactionData(transactionData []byte) (*common.Payload, *common.ChannelHeader, error) {
env := &common.Envelope{}
if err := proto.Unmarshal(transactionData, env); err != nil {
return nil, nil, err
}
payload := &common.Payload{}
if err := proto.Unmarshal(env.GetPayload(), payload); err != nil {
return nil, nil, err
}
chdr := &common.ChannelHeader{}
if err := proto.Unmarshal(payload.GetHeader().GetChannelHeader(), chdr); err != nil {
return nil, nil, err
}
return payload, chdr, nil
}
type asn1Header struct {
Number int64
PreviousHash []byte
DataHash []byte
}
// Bytes returns the ASN.1 marshaled representation of the block header.
func blockHash(b *common.BlockHeader) []byte {
asn1Header := asn1Header{
PreviousHash: b.PreviousHash,
DataHash: b.DataHash,
}
if b.Number > uint64(math.MaxInt64) {
panic(fmt.Errorf("Golang does not currently support encoding uint64 to asn1"))
} else {
asn1Header.Number = int64(b.Number)
}
result, err := asn1.Marshal(asn1Header)
if err != nil {
// Errors should only arise for types which cannot be encoded, since the
// BlockHeader type is known a-priori to contain only encodable types, an
// error here is fatal and should not be propogated
panic(err)
}
h := sha256.New()
h.Write(result)
return h.Sum(nil)
}