Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
filippo.io/edwards25519 v1.1.0
github.com/aws/aws-sdk-go-v2 v0.17.0
github.com/code-payments/code-vm-indexer v1.2.0
github.com/code-payments/ocp-protobuf-api v0.6.0
github.com/code-payments/ocp-protobuf-api v0.7.0
github.com/emirpasic/gods v1.12.0
github.com/envoyproxy/protoc-gen-validate v1.2.1
github.com/golang/protobuf v1.5.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/code-payments/code-vm-indexer v1.2.0 h1:rSHpBMiT9BKgmKcXg/VIoi/h0t7jNxGx07Qz59m+6Q0=
github.com/code-payments/code-vm-indexer v1.2.0/go.mod h1:vn91YN2qNqb+gGJeZe2+l+TNxVmEEiRHXXnIn2Y40h8=
github.com/code-payments/ocp-protobuf-api v0.6.0 h1:dv/QWox20Z8RRHEwPSlWMAJPDMIvkzw97FESZArS8WA=
github.com/code-payments/ocp-protobuf-api v0.6.0/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
github.com/code-payments/ocp-protobuf-api v0.7.0 h1:pHIVYXmDus32LEzaj92qDWKYrPawuzBIJ+Xlzzf9udg=
github.com/code-payments/ocp-protobuf-api v0.7.0/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down
4 changes: 1 addition & 3 deletions ocp/data/swap/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"sync"
"time"

"github.com/code-payments/ocp-server/ocp/data/swap"
"github.com/code-payments/ocp-server/database/query"
"github.com/code-payments/ocp-server/pointer"
"github.com/code-payments/ocp-server/ocp/data/swap"
)

type ById []*swap.Record
Expand Down Expand Up @@ -43,7 +42,6 @@ func (s *store) Save(_ context.Context, data *swap.Record) error {

data.Version++

item.TransactionSignature = pointer.StringCopy(data.TransactionSignature)
item.TransactionBlob = data.TransactionBlob
item.State = data.State
item.Version = data.Version
Expand Down
41 changes: 20 additions & 21 deletions ocp/data/swap/postgres/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@ import (

"github.com/jmoiron/sqlx"

"github.com/code-payments/ocp-server/ocp/data/swap"
pgutil "github.com/code-payments/ocp-server/database/postgres"
q "github.com/code-payments/ocp-server/database/query"
"github.com/code-payments/ocp-server/pointer"
"github.com/code-payments/ocp-server/ocp/data/swap"
)

const (
tableName = "ocp__core_swap"
)

type model struct {
Id sql.NullInt64 `db:"id"`
SwapId string `db:"swap_id"`
Owner string `db:"owner"`
FromMint string `db:"from_mint"`
ToMint string `db:"to_mint"`
Amount uint64 `db:"amount"`
FundingId string `db:"funding_id"`
FundingSource uint8 `db:"funding_source"`
Nonce string `db:"nonce"`
Blockhash string `db:"blockhash"`
ProofSignature string `db:"proof_signature"`
TransactionSignature sql.NullString `db:"transaction_signature"`
TransactionBlob []byte `db:"transaction_blob"`
State uint8 `db:"state"`
Version uint64 `db:"version"`
CreatedAt time.Time `db:"created_at"`
Id sql.NullInt64 `db:"id"`
SwapId string `db:"swap_id"`
Owner string `db:"owner"`
FromMint string `db:"from_mint"`
ToMint string `db:"to_mint"`
Amount uint64 `db:"amount"`
FundingId string `db:"funding_id"`
FundingSource uint8 `db:"funding_source"`
Nonce string `db:"nonce"`
Blockhash string `db:"blockhash"`
ProofSignature string `db:"proof_signature"`
TransactionSignature string `db:"transaction_signature"`
TransactionBlob []byte `db:"transaction_blob"`
State uint8 `db:"state"`
Version uint64 `db:"version"`
CreatedAt time.Time `db:"created_at"`
}

func toModel(obj *swap.Record) (*model, error) {
Expand All @@ -57,7 +56,7 @@ func toModel(obj *swap.Record) (*model, error) {
Nonce: obj.Nonce,
Blockhash: obj.Blockhash,
ProofSignature: obj.ProofSignature,
TransactionSignature: sql.NullString{String: *pointer.StringOrDefault(obj.TransactionSignature, ""), Valid: obj.TransactionSignature != nil},
TransactionSignature: obj.TransactionSignature,
TransactionBlob: obj.TransactionBlob,
State: uint8(obj.State),
Version: obj.Version,
Expand All @@ -78,7 +77,7 @@ func fromModel(m *model) *swap.Record {
Nonce: m.Nonce,
Blockhash: m.Blockhash,
ProofSignature: m.ProofSignature,
TransactionSignature: pointer.StringIfValid(m.TransactionSignature.Valid, m.TransactionSignature.String),
TransactionSignature: m.TransactionSignature,
TransactionBlob: m.TransactionBlob,
State: swap.State(m.State),
Version: m.Version,
Expand All @@ -94,7 +93,7 @@ func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {

ON CONFLICT (swap_id)
DO UPDATE
SET transaction_signature = $11, transaction_blob = $12, state = $13, version = ` + tableName + `.version + 1
SET transaction_blob = $12, state = $13, version = ` + tableName + `.version + 1
WHERE ` + tableName + `.swap_id = $1 AND ` + tableName + `.version = $14

RETURNING
Expand Down
14 changes: 4 additions & 10 deletions ocp/data/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package swap
import (
"errors"
"time"

"github.com/code-payments/ocp-server/pointer"
)

type State uint8
Expand Down Expand Up @@ -48,7 +46,7 @@ type Record struct {

ProofSignature string

TransactionSignature *string
TransactionSignature string
TransactionBlob []byte

State State
Expand Down Expand Up @@ -78,7 +76,7 @@ func (r *Record) Clone() Record {

ProofSignature: r.ProofSignature,

TransactionSignature: pointer.StringCopy(r.TransactionSignature),
TransactionSignature: r.TransactionSignature,
TransactionBlob: r.TransactionBlob,

State: r.State,
Expand Down Expand Up @@ -108,7 +106,7 @@ func (r *Record) CopyTo(dst *Record) {

dst.ProofSignature = r.ProofSignature

dst.TransactionSignature = pointer.StringCopy(r.TransactionSignature)
dst.TransactionSignature = r.TransactionSignature
dst.TransactionBlob = r.TransactionBlob

dst.State = r.State
Expand Down Expand Up @@ -159,14 +157,10 @@ func (r *Record) Validate() error {
return errors.New("proof signature is required")
}

if r.TransactionSignature != nil && len(*r.TransactionSignature) == 0 {
if len(r.TransactionSignature) == 0 {
return errors.New("transaction signature is empty")
}

if len(r.TransactionBlob) != 0 && r.TransactionSignature == nil {
return errors.New("transaction signature is missing")
}

return nil
}

Expand Down
15 changes: 6 additions & 9 deletions ocp/data/swap/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/code-payments/ocp-server/ocp/data/swap"
"github.com/code-payments/ocp-server/database/query"
"github.com/code-payments/ocp-server/pointer"
"github.com/code-payments/ocp-server/ocp/data/swap"
)

func RunTests(t *testing.T, s swap.Store, teardown func()) {
Expand Down Expand Up @@ -58,7 +57,7 @@ func testRoundTrip(t *testing.T, s swap.Store) {

ProofSignature: "test_proof_signature",

TransactionSignature: pointer.String("test_transaction_signature"),
TransactionSignature: "test_transaction_signature",
TransactionBlob: []byte("test_transaction_blob"),

State: swap.StateFinalized,
Expand Down Expand Up @@ -107,7 +106,7 @@ func testUpdateHappyPath(t *testing.T, s swap.Store) {

ProofSignature: "test_proof_signature",

TransactionSignature: nil,
TransactionSignature: "test_transaction_signature",
TransactionBlob: nil,

State: swap.StateCreated,
Expand All @@ -119,7 +118,6 @@ func testUpdateHappyPath(t *testing.T, s swap.Store) {
assert.EqualValues(t, 1, expected.Id)
assert.EqualValues(t, 1, expected.Version)

expected.TransactionSignature = pointer.String("test_transaction_signature")
expected.TransactionBlob = []byte("transaction_blob")
expected.State = swap.StateFinalized

Expand Down Expand Up @@ -155,7 +153,7 @@ func testUpdateStaleRecord(t *testing.T, s swap.Store) {

ProofSignature: "test_proof_signature",

TransactionSignature: pointer.String("test_transaction_signature"),
TransactionSignature: "test_transaction_signature",
TransactionBlob: []byte("test_transaction_blob"),

State: swap.StateFinalized,
Expand All @@ -169,7 +167,6 @@ func testUpdateStaleRecord(t *testing.T, s swap.Store) {

stale := expected.Clone()
expected.State = swap.StateUnknown
expected.TransactionSignature = nil
expected.TransactionBlob = nil
stale.Version -= 1

Expand Down Expand Up @@ -214,7 +211,7 @@ func testGetAllByOwnerAndState(t *testing.T, s swap.Store) {

ProofSignature: fmt.Sprintf("test_proof_signature_%d", i),

TransactionSignature: pointer.String(fmt.Sprintf("test_transaction_signature_%d", i)),
TransactionSignature: fmt.Sprintf("test_transaction_signature_%d", i),
TransactionBlob: []byte(fmt.Sprintf("test_transaction_blob_%d", i)),

State: swap.State(i % int(swap.StateCancelled+1)),
Expand Down Expand Up @@ -281,7 +278,7 @@ func testGetAllByState(t *testing.T, s swap.Store) {

ProofSignature: fmt.Sprintf("test_proof_signature_%d", i),

TransactionSignature: pointer.String(fmt.Sprintf("test_transaction_signature_%d", i)),
TransactionSignature: fmt.Sprintf("test_transaction_signature_%d", i),
TransactionBlob: []byte(fmt.Sprintf("test_transaction_blob_%d", i)),

State: state,
Expand Down
6 changes: 3 additions & 3 deletions ocp/rpc/transaction/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const (
defaultDisableSwaps = false

SubmitIntentTimeoutConfigEnvName = envConfigPrefix + "SUBMIT_INTENT_TIMEOUT"
defaultSubmitIntentTimeout = 5 * time.Second
defaultSubmitIntentTimeout = 15 * time.Second

SwapTimeoutConfigEnvName = envConfigPrefix + "SWAP_TIMEOUT"
defaultSwapTimeout = 120 * time.Second
defaultSwapTimeout = time.Minute

ClientReceiveTimeoutConfigEnvName = envConfigPrefix + "CLIENT_RECEIVE_TIMEOUT"
defaultClientReceiveTimeout = time.Second
defaultClientReceiveTimeout = 2 * time.Second

FeeCollectorOwnerPublicKeyConfigEnvName = envConfigPrefix + "FEE_COLLECTOR_OWNER_PUBLIC_KEY"
defaultFeeCollectorPublicKey = "invalid" // Ensure something valid is set
Expand Down
77 changes: 11 additions & 66 deletions ocp/rpc/transaction/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func handleSubmitIntentStructuredError(streamer transactionpb.Transaction_Submit
return streamer.Send(errResp)
}

func handleStartSwapError(streamer transactionpb.Transaction_StartSwapServer, err error) error {
func handleStatefulSwapError(streamer transactionpb.Transaction_StatefulSwapServer, err error) error {
// gRPC status errors are passed through as is
if _, ok := status.FromError(err); ok {
return err
Expand All @@ -300,24 +300,24 @@ func handleStartSwapError(streamer transactionpb.Transaction_StartSwapServer, er
// Case 1: Errors that map to a Code error response
switch err.(type) {
case SwapValidationError:
return handleStartSwapStructuredError(
return handleStatefulSwapStructuredError(
streamer,
transactionpb.StartSwapResponse_Error_INVALID_SWAP,
transactionpb.StatefulSwapResponse_Error_INVALID_SWAP,
toReasonStringErrorDetails(err),
)
case SwapDeniedError:
return handleStartSwapStructuredError(
return handleStatefulSwapStructuredError(
streamer,
transactionpb.StartSwapResponse_Error_DENIED,
transactionpb.StatefulSwapResponse_Error_DENIED,
toDeniedErrorDetails(err),
)
}

switch err {
case ErrInvalidSignature:
return handleStartSwapStructuredError(
return handleStatefulSwapStructuredError(
streamer,
transactionpb.StartSwapResponse_Error_SIGNATURE_ERROR,
transactionpb.StatefulSwapResponse_Error_SIGNATURE_ERROR,
toReasonStringErrorDetails(err),
)
case ErrNotImplemented:
Expand All @@ -336,65 +336,10 @@ func handleStartSwapError(streamer transactionpb.Transaction_StartSwapServer, er
return status.Error(codes.Internal, "rpc server failure")
}

func handleStartSwapStructuredError(streamer transactionpb.Transaction_StartSwapServer, code transactionpb.StartSwapResponse_Error_Code, errorDetails ...*transactionpb.ErrorDetails) error {
errResp := &transactionpb.StartSwapResponse{
Response: &transactionpb.StartSwapResponse_Error_{
Error: &transactionpb.StartSwapResponse_Error{
Code: code,
ErrorDetails: errorDetails,
},
},
}
return streamer.Send(errResp)
}

func handleSwapError(streamer transactionpb.Transaction_SwapServer, err error) error {
// gRPC status errors are passed through as is
if _, ok := status.FromError(err); ok {
return err
}

// Case 1: Errors that map to a Code error response
switch err.(type) {
case SwapValidationError:
return handleSwapStructuredError(
streamer,
transactionpb.SwapResponse_Error_INVALID_SWAP,
toReasonStringErrorDetails(err),
)
case SwapDeniedError:
return handleSwapStructuredError(
streamer,
transactionpb.SwapResponse_Error_DENIED,
toDeniedErrorDetails(err),
)
}

switch err {
case ErrInvalidSignature:
return handleSwapStructuredError(
streamer,
transactionpb.SwapResponse_Error_SIGNATURE_ERROR,
toReasonStringErrorDetails(err),
)
case ErrNotImplemented:
return status.Error(codes.Unimplemented, err.Error())
}

// Case 2: Errors that map to gRPC status errors
switch err {
case ErrTimedOutReceivingRequest, context.DeadlineExceeded:
return status.Error(codes.DeadlineExceeded, err.Error())
case context.Canceled:
return status.Error(codes.Canceled, err.Error())
}
return status.Error(codes.Internal, "rpc server failure")
}

func handleSwapStructuredError(streamer transactionpb.Transaction_SwapServer, code transactionpb.SwapResponse_Error_Code, errorDetails ...*transactionpb.ErrorDetails) error {
errResp := &transactionpb.SwapResponse{
Response: &transactionpb.SwapResponse_Error_{
Error: &transactionpb.SwapResponse_Error{
func handleStatefulSwapStructuredError(streamer transactionpb.Transaction_StatefulSwapServer, code transactionpb.StatefulSwapResponse_Error_Code, errorDetails ...*transactionpb.ErrorDetails) error {
errResp := &transactionpb.StatefulSwapResponse{
Response: &transactionpb.StatefulSwapResponse_Error_{
Error: &transactionpb.StatefulSwapResponse_Error{
Code: code,
ErrorDetails: errorDetails,
},
Expand Down
Loading
Loading