github.com/datachainlab/burrow@v0.25.0/rpc/rpctransact/transact_server.go (about) 1 package rpctransact 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/hyperledger/burrow/execution" 8 "github.com/hyperledger/burrow/execution/exec" 9 "github.com/hyperledger/burrow/txs" 10 "github.com/hyperledger/burrow/txs/payload" 11 "golang.org/x/net/context" 12 ) 13 14 // This is probably silly 15 const maxBroadcastSyncTimeout = time.Hour 16 17 type transactServer struct { 18 transactor *execution.Transactor 19 txCodec txs.Codec 20 } 21 22 func NewTransactServer(transactor *execution.Transactor, txCodec txs.Codec) TransactServer { 23 return &transactServer{ 24 transactor: transactor, 25 txCodec: txCodec, 26 } 27 } 28 29 func (ts *transactServer) BroadcastTxSync(ctx context.Context, param *TxEnvelopeParam) (*exec.TxExecution, error) { 30 const errHeader = "BroadcastTxSync():" 31 if param.Timeout == 0 { 32 param.Timeout = maxBroadcastSyncTimeout 33 } 34 ctx, cancel := context.WithTimeout(ctx, param.Timeout) 35 defer cancel() 36 txEnv := param.GetEnvelope(ts.transactor.BlockchainInfo.ChainID()) 37 if txEnv == nil { 38 return nil, fmt.Errorf("%s no transaction envelope or payload provided", errHeader) 39 } 40 return ts.transactor.BroadcastTxSync(ctx, txEnv) 41 } 42 43 func (ts *transactServer) BroadcastTxAsync(ctx context.Context, param *TxEnvelopeParam) (*txs.Receipt, error) { 44 const errHeader = "BroadcastTxAsync():" 45 if param.Timeout == 0 { 46 param.Timeout = maxBroadcastSyncTimeout 47 } 48 txEnv := param.GetEnvelope(ts.transactor.BlockchainInfo.ChainID()) 49 if txEnv == nil { 50 return nil, fmt.Errorf("%s no transaction envelope or payload provided", errHeader) 51 } 52 return ts.transactor.BroadcastTxAsync(ctx, txEnv) 53 } 54 55 func (ts *transactServer) SignTx(ctx context.Context, param *TxEnvelopeParam) (*TxEnvelope, error) { 56 txEnv := param.GetEnvelope(ts.transactor.BlockchainInfo.ChainID()) 57 if txEnv == nil { 58 return nil, fmt.Errorf("no transaction envelope or payload provided") 59 } 60 txEnv, err := ts.transactor.SignTx(txEnv) 61 if err != nil { 62 return nil, err 63 } 64 return &TxEnvelope{ 65 Envelope: txEnv, 66 }, nil 67 } 68 69 func (ts *transactServer) FormulateTx(ctx context.Context, param *payload.Any) (*TxEnvelope, error) { 70 txEnv := txs.EnvelopeFromAny(ts.transactor.BlockchainInfo.ChainID(), param) 71 if txEnv == nil { 72 return nil, fmt.Errorf("no payload provided to FormulateTx") 73 } 74 return &TxEnvelope{ 75 Envelope: txEnv, 76 }, nil 77 } 78 79 func (ts *transactServer) CallTxSync(ctx context.Context, param *payload.CallTx) (*exec.TxExecution, error) { 80 return ts.BroadcastTxSync(ctx, &TxEnvelopeParam{Payload: param.Any()}) 81 } 82 83 func (ts *transactServer) CallTxAsync(ctx context.Context, param *payload.CallTx) (*txs.Receipt, error) { 84 return ts.BroadcastTxAsync(ctx, &TxEnvelopeParam{Payload: param.Any()}) 85 } 86 87 func (ts *transactServer) CallTxSim(ctx context.Context, param *payload.CallTx) (*exec.TxExecution, error) { 88 if param.Address == nil { 89 return nil, fmt.Errorf("CallSim requires a non-nil address from which to retrieve code") 90 } 91 return ts.transactor.CallSim(param.Input.Address, *param.Address, param.Data) 92 } 93 94 func (ts *transactServer) CallCodeSim(ctx context.Context, param *CallCodeParam) (*exec.TxExecution, error) { 95 return ts.transactor.CallCodeSim(param.FromAddress, param.Code, param.Data) 96 } 97 98 func (ts *transactServer) SendTxSync(ctx context.Context, param *payload.SendTx) (*exec.TxExecution, error) { 99 return ts.BroadcastTxSync(ctx, &TxEnvelopeParam{Payload: param.Any()}) 100 } 101 102 func (ts *transactServer) SendTxAsync(ctx context.Context, param *payload.SendTx) (*txs.Receipt, error) { 103 return ts.BroadcastTxAsync(ctx, &TxEnvelopeParam{Payload: param.Any()}) 104 } 105 106 func (ts *transactServer) NameTxSync(ctx context.Context, param *payload.NameTx) (*exec.TxExecution, error) { 107 return ts.BroadcastTxSync(ctx, &TxEnvelopeParam{Payload: param.Any()}) 108 } 109 110 func (ts *transactServer) NameTxAsync(ctx context.Context, param *payload.NameTx) (*txs.Receipt, error) { 111 return ts.BroadcastTxAsync(ctx, &TxEnvelopeParam{Payload: param.Any()}) 112 } 113 114 func (te *TxEnvelopeParam) GetEnvelope(chainID string) *txs.Envelope { 115 if te == nil { 116 return nil 117 } 118 if te.Envelope != nil { 119 return te.Envelope 120 } 121 if te.Payload != nil { 122 return txs.EnvelopeFromAny(chainID, te.Payload) 123 } 124 return nil 125 }