This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
transact_server.go
150 lines (129 loc) · 4.84 KB
/
transact_server.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
package rpctransact
import (
"fmt"
"time"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/bcm"
"github.com/hyperledger/burrow/execution"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/txs"
"github.com/hyperledger/burrow/txs/payload"
"golang.org/x/net/context"
)
// This is probably silly
const maxBroadcastSyncTimeout = time.Hour
type transactServer struct {
UnimplementedTransactServer
stateSnapshot func() (acmstate.Reader, error)
blockchain bcm.BlockchainInfo
transactor *execution.Transactor
txCodec txs.Codec
logger *logging.Logger
}
func NewTransactServer(stateSnapshotter func() (acmstate.Reader, error), blockchain bcm.BlockchainInfo,
transactor *execution.Transactor, txCodec txs.Codec, logger *logging.Logger) TransactServer {
return &transactServer{
stateSnapshot: stateSnapshotter,
blockchain: blockchain,
transactor: transactor,
txCodec: txCodec,
logger: logger.WithScope("NewTransactServer()"),
}
}
func (ts *transactServer) BroadcastTxSync(ctx context.Context, param *TxEnvelopeParam) (*exec.TxExecution, error) {
const errHeader = "BroadcastTxSync():"
if param.Timeout == 0 {
param.Timeout = maxBroadcastSyncTimeout
}
ctx, cancel := context.WithTimeout(ctx, param.Timeout)
defer cancel()
txEnv := param.GetEnvelope(ts.transactor.BlockchainInfo.ChainID())
if txEnv == nil {
return nil, fmt.Errorf("%s no transaction envelope or payload provided", errHeader)
}
return ts.transactor.BroadcastTxSync(ctx, txEnv)
}
func (ts *transactServer) BroadcastTxAsync(ctx context.Context, param *TxEnvelopeParam) (*txs.Receipt, error) {
const errHeader = "BroadcastTxAsync():"
if param.Timeout == 0 {
param.Timeout = maxBroadcastSyncTimeout
}
txEnv := param.GetEnvelope(ts.transactor.BlockchainInfo.ChainID())
if txEnv == nil {
return nil, fmt.Errorf("%s no transaction envelope or payload provided", errHeader)
}
return ts.transactor.BroadcastTxAsync(ctx, txEnv)
}
func (ts *transactServer) SignTx(ctx context.Context, param *TxEnvelopeParam) (*TxEnvelope, error) {
txEnv := param.GetEnvelope(ts.transactor.BlockchainInfo.ChainID())
if txEnv == nil {
return nil, fmt.Errorf("no transaction envelope or payload provided")
}
txEnv, err := ts.transactor.SignTx(txEnv)
if err != nil {
return nil, err
}
return &TxEnvelope{
Envelope: txEnv,
}, nil
}
func (ts *transactServer) FormulateTx(ctx context.Context, param *payload.Any) (*TxEnvelope, error) {
txEnv := txs.EnvelopeFromAny(ts.transactor.BlockchainInfo.ChainID(), param)
if txEnv == nil {
return nil, fmt.Errorf("no payload provided to FormulateTx")
}
return &TxEnvelope{
Envelope: txEnv,
}, nil
}
func (ts *transactServer) CallTxSync(ctx context.Context, param *payload.CallTx) (*exec.TxExecution, error) {
return ts.BroadcastTxSync(ctx, &TxEnvelopeParam{Payload: param.Any()})
}
func (ts *transactServer) CallTxAsync(ctx context.Context, param *payload.CallTx) (*txs.Receipt, error) {
return ts.BroadcastTxAsync(ctx, &TxEnvelopeParam{Payload: param.Any()})
}
func (ts *transactServer) CallTxSim(ctx context.Context, param *payload.CallTx) (*exec.TxExecution, error) {
if param.Address == nil {
return nil, fmt.Errorf("CallSim requires a non-nil address from which to retrieve code")
}
// Get a consistent state view for duration of simulated call
st, err := ts.stateSnapshot()
if err != nil {
return nil, err
}
return execution.CallSim(st, ts.blockchain, param.Input.Address, *param.Address, param.Data, ts.logger)
}
func (ts *transactServer) CallCodeSim(ctx context.Context, param *CallCodeParam) (*exec.TxExecution, error) {
// Get a consistent state view for duration of simulated call
st, err := ts.stateSnapshot()
if err != nil {
return nil, err
}
return execution.CallCodeSim(st, ts.blockchain, param.FromAddress, param.FromAddress, param.Code, param.Data,
ts.logger)
}
func (ts *transactServer) SendTxSync(ctx context.Context, param *payload.SendTx) (*exec.TxExecution, error) {
return ts.BroadcastTxSync(ctx, &TxEnvelopeParam{Payload: param.Any()})
}
func (ts *transactServer) SendTxAsync(ctx context.Context, param *payload.SendTx) (*txs.Receipt, error) {
return ts.BroadcastTxAsync(ctx, &TxEnvelopeParam{Payload: param.Any()})
}
func (ts *transactServer) NameTxSync(ctx context.Context, param *payload.NameTx) (*exec.TxExecution, error) {
return ts.BroadcastTxSync(ctx, &TxEnvelopeParam{Payload: param.Any()})
}
func (ts *transactServer) NameTxAsync(ctx context.Context, param *payload.NameTx) (*txs.Receipt, error) {
return ts.BroadcastTxAsync(ctx, &TxEnvelopeParam{Payload: param.Any()})
}
func (te *TxEnvelopeParam) GetEnvelope(chainID string) *txs.Envelope {
if te == nil {
return nil
}
if te.Envelope != nil {
return te.Envelope
}
if te.Payload != nil {
return txs.EnvelopeFromAny(chainID, te.Payload)
}
return nil
}