frontend: implement SendTransaction

This commit is contained in:
George Tankersley
2019-01-22 19:05:49 +00:00
parent d08abe82b4
commit 0cb0bb51af
6 changed files with 208 additions and 14 deletions

37
frontend/rpc_client.go Normal file
View File

@@ -0,0 +1,37 @@
package frontend
import (
"net"
"github.com/btcsuite/btcd/rpcclient"
"github.com/pkg/errors"
ini "gopkg.in/ini.v1"
)
func NewZRPCFromConf(confPath string) (*rpcclient.Client, error) {
cfg, err := ini.Load(confPath)
if err != nil {
return nil, errors.Wrap(err, "failed to read config file")
}
rpcaddr := cfg.Section("").Key("rpcbind").String()
rpcport := cfg.Section("").Key("rpcport").String()
username := cfg.Section("").Key("rpcuser").String()
password := cfg.Section("").Key("rpcpassword").String()
return NewZRPCFromCreds(net.JoinHostPort(rpcaddr, rpcport), username, password)
}
func NewZRPCFromCreds(addr, username, password string) (*rpcclient.Client, error) {
// Connect to local zcash RPC server using HTTP POST mode.
connCfg := &rpcclient.ConnConfig{
Host: addr,
User: username,
Pass: password,
HTTPPostMode: true, // Zcash only supports HTTP POST mode
DisableTLS: true, // Zcash does not provide TLS by default
}
// Notice the notification parameter is nil since notifications are
// not supported in HTTP POST mode.
return rpcclient.New(connCfg, nil)
}

39
frontend/rpc_test.go Normal file
View File

@@ -0,0 +1,39 @@
package frontend
import (
"encoding/json"
"strconv"
"strings"
"testing"
)
// a well-formed raw transaction
const coinbaseTxHex = "0400008085202f89010000000000000000000000000000000000000" +
"000000000000000000000000000ffffffff03580101ffffffff0200ca9a3b000000001976a9146b" +
"9ae8c14e917966b0afdf422d32dbac40486d3988ac80b2e60e0000000017a9146708e6670db0b95" +
"0dac68031025cc5b63213a4918700000000000000000000000000000000000000"
func TestSendTransaction(t *testing.T) {
client, err := NewZRPCFromCreds("127.0.0.1:8232", "user", "password")
if err != nil {
t.Fatalf("Couldn't init JSON-RPC client: %v", err)
}
params := make([]json.RawMessage, 1)
params[0] = json.RawMessage("\"" + coinbaseTxHex + "\"")
_, err = client.RawRequest("sendrawtransaction", params)
if err == nil {
t.Fatal("somehow succeeded at sending a coinbase tx")
}
errParts := strings.SplitN(err.Error(), ":", 2)
errCode, err := strconv.ParseInt(errParts[0], 10, 64)
if err != nil {
t.Errorf("couldn't parse error code: %v", err)
}
errMsg := strings.TrimSpace(errParts[1])
if errCode != -26 || errMsg != "16: coinbase" {
t.Error("got the wrong errors")
}
}

View File

@@ -4,11 +4,17 @@ import (
"context"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/btcsuite/btcd/rpcclient"
"github.com/golang/protobuf/proto"
// blank import for sqlite driver support
_ "github.com/mattn/go-sqlite3"
"github.com/zcash-hackworks/lightwalletd/storage"
@@ -16,16 +22,16 @@ import (
)
var (
ErrNoImpl = errors.New("not yet implemented")
ErrUnspecified = errors.New("request for unspecified identifier")
)
// the service type
type SqlStreamer struct {
db *sql.DB
db *sql.DB
client *rpcclient.Client
}
func NewSQLiteStreamer(dbPath string) (walletrpc.CompactTxStreamerServer, error) {
func NewSQLiteStreamer(dbPath string, client *rpcclient.Client) (walletrpc.CompactTxStreamerServer, error) {
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?_busy_timeout=10000&cache=shared", dbPath))
db.SetMaxOpenConns(1)
if err != nil {
@@ -38,7 +44,7 @@ func NewSQLiteStreamer(dbPath string) (walletrpc.CompactTxStreamerServer, error)
return nil, err
}
return &SqlStreamer{db}, nil
return &SqlStreamer{db, client}, nil
}
func (s *SqlStreamer) GracefulStop() error {
@@ -147,6 +153,49 @@ func (s *SqlStreamer) GetTransaction(ctx context.Context, txf *walletrpc.TxFilte
return &walletrpc.RawTransaction{Data: txBytes}, nil
}
// SendTransaction forwards raw transaction bytes to a zcashd instance over JSON-RPC
func (s *SqlStreamer) SendTransaction(ctx context.Context, rawtx *walletrpc.RawTransaction) (*walletrpc.SendResponse, error) {
return nil, ErrNoImpl
// sendrawtransaction "hexstring" ( allowhighfees )
//
// Submits raw transaction (serialized, hex-encoded) to local node and network.
//
// Also see createrawtransaction and signrawtransaction calls.
//
// Arguments:
// 1. "hexstring" (string, required) The hex string of the raw transaction)
// 2. allowhighfees (boolean, optional, default=false) Allow high fees
//
// Result:
// "hex" (string) The transaction hash in hex
// Construct raw JSON-RPC params
params := make([]json.RawMessage, 1)
txHexString := hex.EncodeToString(rawtx.Data)
params[0] = json.RawMessage("\"" + txHexString + "\"")
result, rpcErr := s.client.RawRequest("sendrawtransaction", params)
var err error
var errCode int64
var errMsg string
// For some reason, the error responses are not JSON
if rpcErr != nil {
errParts := strings.SplitN(rpcErr.Error(), ":", 2)
errMsg = strings.TrimSpace(errParts[1])
errCode, err = strconv.ParseInt(errParts[0], 10, 32)
if err != nil {
// This should never happen. We can't panic here, but it's that class of error.
// This is why we need integration testing to work better than regtest currently does. TODO.
return nil, errors.New("SendTransaction couldn't parse error code")
}
} else {
errMsg = string(result)
}
// TODO these are called Error but they aren't at the moment.
// A success will return code 0 and message txhash.
return &walletrpc.SendResponse{
ErrorCode: int32(errCode),
ErrorMessage: errMsg,
}, nil
}