forked from xuperchain/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactor.go
394 lines (341 loc) · 12.3 KB
/
transactor.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
// Copyright 2017 Monax Industries Limited
//
// 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 execution
import (
"context"
"fmt"
"runtime/debug"
"time"
"bytes"
acm "github.com/hyperledger/burrow/account"
"github.com/hyperledger/burrow/account/state"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/blockchain"
"github.com/hyperledger/burrow/consensus/tendermint/codes"
"github.com/hyperledger/burrow/event"
exe_events "github.com/hyperledger/burrow/execution/events"
"github.com/hyperledger/burrow/execution/evm"
evm_events "github.com/hyperledger/burrow/execution/evm/events"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
"github.com/hyperledger/burrow/txs"
abci_types "github.com/tendermint/abci/types"
"github.com/tendermint/go-wire"
)
const BlockingTimeoutSeconds = 30
type Call struct {
Return []byte
GasUsed uint64
}
// Transactor is the controller/middleware for the v0 RPC
type Transactor struct {
tip blockchain.Tip
eventEmitter event.Emitter
broadcastTxAsync func(tx txs.Tx, callback func(res *abci_types.Response)) error
logger *logging.Logger
}
func NewTransactor(tip blockchain.Tip, eventEmitter event.Emitter,
broadcastTxAsync func(tx txs.Tx, callback func(res *abci_types.Response)) error,
logger *logging.Logger) *Transactor {
return &Transactor{
tip: tip,
eventEmitter: eventEmitter,
broadcastTxAsync: broadcastTxAsync,
logger: logger.With(structure.ComponentKey, "Transactor"),
}
}
// Run a contract's code on an isolated and unpersisted state
// Cannot be used to create new contracts
func (trans *Transactor) Call(reader state.Reader, fromAddress, toAddress acm.Address,
data []byte) (call *Call, err error) {
if evm.RegisteredNativeContract(toAddress.Word256()) {
return nil, fmt.Errorf("attempt to call native contract at address "+
"%X, but native contracts can not be called directly. Use a deployed "+
"contract that calls the native function instead", toAddress)
}
// This was being run against CheckTx cache, need to understand the reasoning
callee, err := state.GetMutableAccount(reader, toAddress)
if err != nil {
return nil, err
}
if callee == nil {
return nil, fmt.Errorf("account %s does not exist", toAddress)
}
caller := acm.ConcreteAccount{Address: fromAddress}.MutableAccount()
txCache := state.NewCache(reader)
params := vmParams(trans.tip)
vmach := evm.NewVM(txCache, params, caller.Address(), nil, trans.logger.WithScope("Call"))
vmach.SetPublisher(trans.eventEmitter)
gas := params.GasLimit
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic from VM in simulated call: %v\n%s", r, debug.Stack())
}
}()
ret, err := vmach.Call(caller, callee, callee.Code(), data, 0, &gas)
if err != nil {
return nil, err
}
gasUsed := params.GasLimit - gas
return &Call{Return: ret, GasUsed: gasUsed}, nil
}
// Run the given code on an isolated and unpersisted state
// Cannot be used to create new contracts.
func (trans *Transactor) CallCode(reader state.Reader, fromAddress acm.Address, code, data []byte) (*Call, error) {
// This was being run against CheckTx cache, need to understand the reasoning
callee := acm.ConcreteAccount{Address: fromAddress}.MutableAccount()
caller := acm.ConcreteAccount{Address: fromAddress}.MutableAccount()
txCache := state.NewCache(reader)
params := vmParams(trans.tip)
vmach := evm.NewVM(txCache, params, caller.Address(), nil, trans.logger.WithScope("CallCode"))
gas := params.GasLimit
ret, err := vmach.Call(caller, callee, code, data, 0, &gas)
if err != nil {
return nil, err
}
gasUsed := params.GasLimit - gas
return &Call{Return: ret, GasUsed: gasUsed}, nil
}
func (trans *Transactor) BroadcastTxAsync(tx txs.Tx, callback func(res *abci_types.Response)) error {
return trans.broadcastTxAsync(tx, callback)
}
// Broadcast a transaction and waits for a response from the mempool. Transactions to BroadcastTx will block during
// various mempool operations (managed by Tendermint) including mempool Reap, Commit, and recheckTx.
func (trans *Transactor) BroadcastTx(tx txs.Tx) (*txs.Receipt, error) {
trans.logger.Trace.Log("method", "BroadcastTx",
"tx_hash", tx.Hash(trans.tip.ChainID()),
"tx", tx.String())
responseCh := make(chan *abci_types.Response, 1)
err := trans.BroadcastTxAsync(tx, func(res *abci_types.Response) {
responseCh <- res
})
if err != nil {
return nil, err
}
response := <-responseCh
checkTxResponse := response.GetCheckTx()
if checkTxResponse == nil {
return nil, fmt.Errorf("application did not return CheckTx response")
}
switch checkTxResponse.Code {
case codes.TxExecutionSuccessCode:
receipt := new(txs.Receipt)
err := wire.ReadBinaryBytes(checkTxResponse.Data, receipt)
if err != nil {
return nil, fmt.Errorf("could not deserialise transaction receipt: %s", err)
}
return receipt, nil
default:
return nil, fmt.Errorf("error returned by Tendermint in BroadcastTxSync "+
"ABCI code: %v, ABCI log: %v", checkTxResponse.Code, checkTxResponse.Log)
}
}
// Orders calls to BroadcastTx using lock (waits for response from core before releasing)
func (trans *Transactor) Transact(sequentialSigningAccount *SequentialSigningAccount, address *acm.Address, data []byte,
gasLimit, fee uint64) (*txs.Receipt, error) {
// Use the get the freshest sequence numbers from mempool state and hold the lock until we get a response from
// CheckTx
inputAccount, unlock, err := sequentialSigningAccount.Lock()
if err != nil {
return nil, err
}
defer unlock()
callTx, _, err := trans.formulateCallTx(inputAccount, address, data, gasLimit, fee)
if err != nil {
return nil, err
}
// Got ourselves a tx.
err = callTx.Sign(trans.tip.ChainID(), inputAccount)
if err != nil {
return nil, err
}
return trans.BroadcastTx(callTx)
}
func (trans *Transactor) TransactAndHold(sequentialSigningAccount *SequentialSigningAccount, address *acm.Address, data []byte, gasLimit,
fee uint64) (*evm_events.EventDataCall, error) {
inputAccount, unlock, err := sequentialSigningAccount.Lock()
if err != nil {
return nil, err
}
defer unlock()
callTx, expectedReceipt, err := trans.formulateCallTx(inputAccount, address, data, gasLimit, fee)
if err != nil {
return nil, err
}
subID, err := event.GenerateSubscriptionID()
if err != nil {
return nil, err
}
// We want non-blocking on the first event received (but buffer the value),
// after which we want to block (and then discard the value - see below)
ch := make(chan *evm_events.EventDataCall, 1)
err = evm_events.SubscribeAccountCall(context.Background(), trans.eventEmitter, subID, expectedReceipt.ContractAddress,
expectedReceipt.TxHash, 0, ch)
if err != nil {
return nil, err
}
// Will clean up callback goroutine and subscription in pubsub
defer trans.eventEmitter.UnsubscribeAll(context.Background(), subID)
receipt, err := trans.BroadcastTx(callTx)
if err != nil {
return nil, err
}
if !bytes.Equal(receipt.TxHash, expectedReceipt.TxHash) {
return nil, fmt.Errorf("BroadcastTx received TxHash %X but %X was expected",
receipt.TxHash, expectedReceipt.TxHash)
}
timer := time.NewTimer(BlockingTimeoutSeconds * time.Second)
defer timer.Stop()
select {
case <-timer.C:
return nil, fmt.Errorf("transaction timed out TxHash: %X", expectedReceipt.TxHash)
case eventDataCall := <-ch:
if eventDataCall.Exception != "" {
return nil, fmt.Errorf("error when transacting: " + eventDataCall.Exception)
} else {
return eventDataCall, nil
}
}
}
func (trans *Transactor) formulateCallTx(inputAccount *SigningAccount, address *acm.Address, data []byte,
gasLimit, fee uint64) (*txs.CallTx, *txs.Receipt, error) {
txInput := &txs.TxInput{
Address: inputAccount.Address(),
Amount: fee,
Sequence: inputAccount.Sequence() + 1,
PublicKey: inputAccount.PublicKey(),
}
tx := &txs.CallTx{
Input: txInput,
Address: address,
GasLimit: gasLimit,
Fee: fee,
Data: data,
}
// Got ourselves a tx.
err := tx.Sign(trans.tip.ChainID(), inputAccount)
if err != nil {
return nil, nil, err
}
receipt := txs.GenerateReceipt(trans.tip.ChainID(), tx)
return tx, &receipt, nil
}
func (trans *Transactor) Send(sequentialSigningAccount *SequentialSigningAccount, toAddress acm.Address,
amount uint64) (*txs.Receipt, error) {
inputAccount, unlock, err := sequentialSigningAccount.Lock()
if err != nil {
return nil, err
}
defer unlock()
sendTx, _, err := trans.formulateSendTx(inputAccount, toAddress, amount)
if err != nil {
return nil, err
}
return trans.BroadcastTx(sendTx)
}
func (trans *Transactor) SendAndHold(sequentialSigningAccount *SequentialSigningAccount, toAddress acm.Address,
amount uint64) (*txs.Receipt, error) {
inputAccount, unlock, err := sequentialSigningAccount.Lock()
if err != nil {
return nil, err
}
defer unlock()
sendTx, expectedReceipt, err := trans.formulateSendTx(inputAccount, toAddress, amount)
if err != nil {
return nil, err
}
subID, err := event.GenerateSubscriptionID()
if err != nil {
return nil, err
}
wc := make(chan *txs.SendTx)
err = exe_events.SubscribeAccountOutputSendTx(context.Background(), trans.eventEmitter, subID, toAddress,
expectedReceipt.TxHash, wc)
if err != nil {
return nil, err
}
defer trans.eventEmitter.UnsubscribeAll(context.Background(), subID)
receipt, err := trans.BroadcastTx(sendTx)
if err != nil {
return nil, err
}
if !bytes.Equal(receipt.TxHash, expectedReceipt.TxHash) {
return nil, fmt.Errorf("BroadcastTx received TxHash %X but %X was expected",
receipt.TxHash, expectedReceipt.TxHash)
}
timer := time.NewTimer(BlockingTimeoutSeconds * time.Second)
defer timer.Stop()
select {
case <-timer.C:
return nil, fmt.Errorf("transaction timed out TxHash: %X", expectedReceipt.TxHash)
case sendTx := <-wc:
// This is a double check - we subscribed to this tx's hash so something has gone wrong if the amounts don't match
if sendTx.Inputs[0].Amount == amount {
return expectedReceipt, nil
}
return nil, fmt.Errorf("received SendTx but hash doesn't seem to match what we subscribed to, "+
"received SendTx: %v which does not match receipt on sending: %v", sendTx, expectedReceipt)
}
}
func (trans *Transactor) formulateSendTx(inputAccount *SigningAccount, toAddress acm.Address,
amount uint64) (*txs.SendTx, *txs.Receipt, error) {
sendTx := txs.NewSendTx()
txInput := &txs.TxInput{
Address: inputAccount.Address(),
Amount: amount,
Sequence: inputAccount.Sequence() + 1,
PublicKey: inputAccount.PublicKey(),
}
sendTx.Inputs = append(sendTx.Inputs, txInput)
txOutput := &txs.TxOutput{Address: toAddress, Amount: amount}
sendTx.Outputs = append(sendTx.Outputs, txOutput)
err := sendTx.Sign(trans.tip.ChainID(), inputAccount)
if err != nil {
return nil, nil, err
}
receipt := txs.GenerateReceipt(trans.tip.ChainID(), sendTx)
return sendTx, &receipt, nil
}
func (trans *Transactor) TransactNameReg(sequentialSigningAccount *SequentialSigningAccount, name, data string, amount,
fee uint64) (*txs.Receipt, error) {
inputAccount, unlock, err := sequentialSigningAccount.Lock()
if err != nil {
return nil, err
}
defer unlock()
// Formulate and sign
tx := txs.NewNameTxWithSequence(inputAccount.PublicKey(), name, data, amount, fee, inputAccount.Sequence()+1)
err = tx.Sign(trans.tip.ChainID(), inputAccount)
if err != nil {
return nil, err
}
return trans.BroadcastTx(tx)
}
// Sign a transaction
func (trans *Transactor) SignTx(tx txs.Tx, signingAccounts []acm.AddressableSigner) (txs.Tx, error) {
// more checks?
err := tx.Sign(trans.tip.ChainID(), signingAccounts...)
if err != nil {
return nil, err
}
return tx, nil
}
func vmParams(tip blockchain.Tip) evm.Params {
return evm.Params{
BlockHeight: tip.LastBlockHeight(),
BlockHash: binary.LeftPadWord256(tip.LastBlockHash()),
BlockTime: tip.LastBlockTime().Unix(),
GasLimit: GasLimit,
}
}