-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathblockchain.go
121 lines (96 loc) · 3.74 KB
/
blockchain.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
//go:generate nodejs ../resources/blockchain/scripts/generateAbi.js --contract-package singularitynet-platform-contracts --contract-name MultiPartyEscrow --go-package blockchain --output-file multi_party_escrow.go
//go:generate nodejs ../resources/blockchain/scripts/generateAbi.js --contract-package singularitynet-platform-contracts --contract-name Registry --go-package blockchain --output-file registry.go
package blockchain
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"
"github.com/singnet/snet-daemon/config"
log "github.com/sirupsen/logrus"
"math/big"
)
var (
// HashPrefix32Bytes is an Ethereum signature prefix: see https://github.com/ethereum/go-ethereum/blob/bf468a81ec261745b25206b2a596eb0ee0a24a74/internal/ethapi/api.go#L361
HashPrefix32Bytes = []byte("\x19Ethereum Signed Message:\n32")
hashPrefix42Bytes = []byte("\x19Ethereum Signed Message:\n420x")
)
type jobInfo struct {
jobAddressBytes []byte
jobSignatureBytes []byte
}
type Processor struct {
enabled bool
ethClient *ethclient.Client
rawClient *rpc.Client
sigHasher func([]byte) []byte
privateKey *ecdsa.PrivateKey
address string
jobCompletionQueue chan *jobInfo
escrowContractAddress common.Address
registryContractAddress common.Address
multiPartyEscrow *MultiPartyEscrow
}
// NewProcessor creates a new blockchain processor
func NewProcessor(metadata *ServiceMetadata) (Processor, error) {
// TODO(aiden) accept configuration as a parameter
p := Processor{
jobCompletionQueue: make(chan *jobInfo, 1000),
enabled: config.GetBool(config.BlockchainEnabledKey),
}
if !p.enabled {
return p, nil
}
// Setup ethereum client
if ethclients, err := GetEthereumClient(); err != nil {
return p, errors.Wrap(err, "error creating RPC client")
} else {
p.rawClient = ethclients.RawClient
p.ethClient = ethclients.EthClient
}
// TODO: if address is not in config, try to load it using network
//TODO: Read this from github
p.escrowContractAddress = metadata.GetMpeAddress()
if mpe, err := NewMultiPartyEscrow(p.escrowContractAddress, p.ethClient); err != nil {
return p, errors.Wrap(err, "error instantiating MultiPartyEscrow contract")
} else {
p.multiPartyEscrow = mpe
}
// set local signature hash creator
p.sigHasher = func(i []byte) []byte {
return crypto.Keccak256(HashPrefix32Bytes, crypto.Keccak256(i))
}
return p, nil
}
func (processor *Processor) Enabled() (enabled bool) {
return processor.enabled
}
func (processor *Processor) EscrowContractAddress() common.Address {
return processor.escrowContractAddress
}
func (processor *Processor) MultiPartyEscrow() *MultiPartyEscrow {
return processor.multiPartyEscrow
}
func (processor *Processor) CurrentBlock() (currentBlock *big.Int, err error) {
// We have to do a raw call because the standard method of ethClient.HeaderByNumber(ctx, nil) errors on
// unmarshaling the response currently. See https://github.com/ethereum/go-ethereum/issues/3230
var currentBlockHex string
if err = processor.rawClient.CallContext(context.Background(), ¤tBlockHex, "eth_blockNumber"); err != nil {
log.WithError(err).Error("error determining current block")
return nil, fmt.Errorf("error determining current block: %v", err)
}
currentBlockBytes := common.FromHex(currentBlockHex)
currentBlock = new(big.Int).SetBytes(currentBlockBytes)
return
}
func (processor *Processor) HasIdentity() bool {
return processor.address != ""
}
func (processor *Processor) Close() {
processor.ethClient.Close()
processor.rawClient.Close()
}