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
111 lines (94 loc) · 3.58 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
package rpctransact
import (
"fmt"
"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"
)
type transactServer struct {
transactor *execution.Transactor
txCodec txs.Codec
}
func NewTransactServer(transactor *execution.Transactor, txCodec txs.Codec) TransactServer {
return &transactServer{
transactor: transactor,
txCodec: txCodec,
}
}
func (ts *transactServer) BroadcastTxSync(ctx context.Context, param *TxEnvelopeParam) (*exec.TxExecution, error) {
txEnv := param.GetEnvelope(ts.transactor.Tip.ChainID())
if txEnv == nil {
return nil, fmt.Errorf("no transaction envelope or payload provided")
}
return ts.transactor.BroadcastTxSync(ctx, txEnv)
}
func (ts *transactServer) BroadcastTxAsync(ctx context.Context, param *TxEnvelopeParam) (*txs.Receipt, error) {
txEnv := param.GetEnvelope(ts.transactor.Tip.ChainID())
if txEnv == nil {
return nil, fmt.Errorf("no transaction envelope or payload provided")
}
return ts.transactor.BroadcastTxAsync(txEnv)
}
func (ts *transactServer) SignTx(ctx context.Context, param *TxEnvelopeParam) (*TxEnvelope, error) {
txEnv := param.GetEnvelope(ts.transactor.Tip.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.Tip.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")
}
return ts.transactor.CallSim(param.Input.Address, *param.Address, param.Data)
}
func (ts *transactServer) CallCodeSim(ctx context.Context, param *CallCodeParam) (*exec.TxExecution, error) {
return ts.transactor.CallCodeSim(param.FromAddress, param.Code, param.Data)
}
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
}