-
Notifications
You must be signed in to change notification settings - Fork 2
/
execute_tx.go
86 lines (77 loc) · 2.49 KB
/
execute_tx.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
package abci
import (
"fmt"
"strings"
"github.com/klyed/hivesmartchain/consensus/tendermint/codes"
"github.com/klyed/hivesmartchain/execution"
"github.com/klyed/hivesmartchain/execution/errors"
"github.com/klyed/hivesmartchain/logging"
"github.com/klyed/hivesmartchain/logging/structure"
"github.com/klyed/hivesmartchain/txs"
"github.com/tendermint/tendermint/abci/types"
)
// Attempt to execute a transaction using ABCI conventions and codes
func ExecuteTx(logHeader string, executor execution.Executor, txDecoder txs.Decoder, txBytes []byte) types.ResponseCheckTx {
logf := func(format string, args ...interface{}) string {
return fmt.Sprintf("%s: "+format, append([]interface{}{logHeader}, args...)...)
}
txEnv, err := txDecoder.DecodeTx(txBytes)
if err != nil {
return types.ResponseCheckTx{
Code: codes.EncodingErrorCode,
Log: logf("Decoding error: %s", err),
}
}
txe, err := executor.Execute(txEnv)
if err != nil {
ex := errors.AsException(err)
return types.ResponseCheckTx{
Code: codes.TxExecutionErrorCode,
Log: logf("Could not execute transaction: %s, error: %v", txEnv, ex.Exception),
}
}
tags := []types.EventAttribute{{Key: []byte(structure.TxHashKey), Value: []byte(txEnv.Tx.Hash().String())}}
if txe.Receipt.CreatesContract {
tags = append(tags, types.EventAttribute{
Key: []byte("created_contract_address"),
Value: []byte(txe.Receipt.ContractAddress.String()),
})
}
events := []types.Event{{Type: "ExecuteTx", Attributes: tags}}
bs, err := txe.Receipt.Encode()
if err != nil {
return types.ResponseCheckTx{
Code: codes.EncodingErrorCode,
Events: events,
Log: logf("Could not serialise receipt: %s", err),
}
}
return types.ResponseCheckTx{
Code: codes.TxExecutionSuccessCode,
Events: events,
Log: logf("Execution success - TxExecution in data"),
Data: bs,
}
}
// Some ABCI type helpers
func WithEvents(logger *logging.Logger, events []types.Event) *logging.Logger {
for _, e := range events {
values := make([]string, 0, len(e.Attributes))
for _, kvp := range e.Attributes {
values = append(values, fmt.Sprintf("%s:%s", string(kvp.Key), string(kvp.Value)))
}
logger = logger.With(e.Type, strings.Join(values, ","))
}
return logger
}
func DeliverTxFromCheckTx(ctr types.ResponseCheckTx) types.ResponseDeliverTx {
return types.ResponseDeliverTx{
Code: ctr.Code,
Log: ctr.Log,
Data: ctr.Data,
Events: ctr.Events,
GasUsed: ctr.GasUsed,
GasWanted: ctr.GasWanted,
Info: ctr.Info,
}
}