Skip to content

Commit 48aa8a6

Browse files
authored
Merge pull request #16 from hyle-team/dev/submit-endpoint-update
Dev/submit-endpoint-update
2 parents 7efc9a8 + f7a465a commit 48aa8a6

File tree

19 files changed

+197
-128
lines changed

19 files changed

+197
-128
lines changed

cmd/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# /cmd
22

33
## Description
4-
Contains the command-line interface (CLI) for the project
4+
Contains the command-line interface (CLI) for the project.
5+
Command-line interface for project is designed to help user to prepare and then run the service.
56

67
## Components
78
- `root.go`: Contains the main entry point for the CLI
@@ -12,6 +13,7 @@ Contains the command-line interface (CLI) for the project
1213
Some of the commands require the mandatory or optional flags to be passed. See the [Flags](#flags) section for more details about specific flag definition and usage.
1314

1415
### Database Migrations
16+
At the start of server user has to migrate up his db to have possibility to process deposits in right way.
1517
Commands:
1618
- `tss-svc service migrate up`: Migrates the database schema to the latest version
1719
- `tss-svc service migrate down`: Rolls back the database schema to the previous version
@@ -20,6 +22,10 @@ Required flags:
2022
- `--config` (can be omitted if the default config file path is used)
2123

2224
### Run server
25+
Service can be run into two modes: keygen and signing.
26+
- Signing mode offers user to take part in signing sessions and proceed incoming deposits.
27+
- Keygen mode is designed to generate user`s shares used in signing process.
28+
2329
Commands:
2430
- `tss-svc service run keygen`: Runs the TSS service in the keygen mode
2531
- `tss-svc service run signing`: Runs the TSS service in the sign mode
@@ -54,6 +60,15 @@ Optional flags:
5460
- `tss-svc helpers generate transaction`: Generates a new transaction based on the given data.
5561
It is used for resharing purposes. Should be investigated further.
5662

63+
### Parsing
64+
Commands:
65+
- `tss-svc helpers parse address-btc [x-cord] [y-cord]`: Parses btc address from given point
66+
- `tss-svc helpers parse address-eth [x-cord] [y-cord]`: Parses eth address from given point
67+
- `tss-svc helpers parse pubkey [x-cord] [y-cord]`: Parses public key from given point
68+
69+
Optional flags:
70+
- `--network` (Network type (mainnet/testnet), mainnet is used by default)
71+
5772
## Flags
5873
- `--config` (`-c`): Specifies the path to the configuration file. By default, the config file path is set to `config.yaml`. See [Configuration](../docs/04_configuration.md) for more details
5974
- `--output` (`-o`): Specifies the data output type for the command.

cmd/service/run/sign.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func runSigningService(ctx context.Context, cfg config.Config, wg *sync.WaitGrou
7979
fetcher,
8080
p2p.NewBroadcaster(cfg.Parties()),
8181
account.CosmosAddress(),
82+
connector,
8283
)
8384

8485
// API servers spin-up
@@ -172,7 +173,6 @@ func runSigningService(ctx context.Context, cfg config.Config, wg *sync.WaitGrou
172173
depositAcceptorSession := bridge.NewDepositAcceptorSession(
173174
cfg.Parties(),
174175
fetcher,
175-
clientsRepo,
176176
db,
177177
logger.WithField("component", "deposit_acceptor_session"),
178178
)

internal/api/common/common.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package common
33
import (
44
validation "github.com/go-ozzo/ozzo-validation/v4"
55
apiTypes "github.com/hyle-team/tss-svc/internal/api/types"
6+
"github.com/hyle-team/tss-svc/internal/bridge/chains"
67
"github.com/hyle-team/tss-svc/internal/bridge/clients"
78
database "github.com/hyle-team/tss-svc/internal/db"
89
"github.com/hyle-team/tss-svc/internal/types"
@@ -24,6 +25,11 @@ func ValidateIdentifier(identifier *types.DepositIdentifier, client clients.Clie
2425
return errors.New("invalid transaction hash")
2526
}
2627

28+
// If chain type is Zano event index always is 0
29+
if client.Type() == chains.TypeZano {
30+
identifier.TxNonce = 0
31+
}
32+
2733
return nil
2834
}
2935

@@ -43,20 +49,20 @@ func ToStatusResponse(d *database.Deposit) *apiTypes.CheckWithdrawalResponse {
4349

4450
result.TransferData = &types.TransferData{
4551
Sender: d.Depositor,
46-
Receiver: *d.Receiver,
47-
DepositAmount: *d.DepositAmount,
48-
WithdrawalAmount: *d.WithdrawalAmount,
49-
DepositAsset: *d.DepositToken,
50-
WithdrawalAsset: *d.WithdrawalToken,
51-
IsWrappedAsset: *d.IsWrappedToken,
52-
DepositBlock: *d.DepositBlock,
52+
Receiver: d.Receiver,
53+
DepositAmount: d.DepositAmount,
54+
WithdrawalAmount: d.WithdrawalAmount,
55+
DepositAsset: d.DepositToken,
56+
WithdrawalAsset: d.WithdrawalToken,
57+
IsWrappedAsset: d.IsWrappedToken,
58+
DepositBlock: d.DepositBlock,
5359
Signature: d.Signature,
5460
}
5561

5662
if d.WithdrawalTxHash != nil {
5763
result.WithdrawalIdentifier = &types.WithdrawalIdentifier{
5864
TxHash: *d.WithdrawalTxHash,
59-
ChainId: *d.WithdrawalChainId,
65+
ChainId: d.WithdrawalChainId,
6066
}
6167
}
6268

internal/api/ctx/ctx.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ctx
22

33
import (
44
"context"
5+
coreConnector "github.com/hyle-team/tss-svc/internal/core/connector"
56

67
"github.com/hyle-team/tss-svc/internal/bridge"
78
bridgeTypes "github.com/hyle-team/tss-svc/internal/bridge/clients"
@@ -14,12 +15,13 @@ import (
1415
type ctxKey int
1516

1617
const (
17-
dbKey ctxKey = iota
18-
loggerKey ctxKey = iota
19-
clientsKey ctxKey = iota
20-
processorKey ctxKey = iota
21-
broadcasterKey ctxKey = iota
22-
selfKey ctxKey = iota
18+
dbKey ctxKey = iota
19+
loggerKey ctxKey = iota
20+
clientsKey ctxKey = iota
21+
processorKey ctxKey = iota
22+
broadcasterKey ctxKey = iota
23+
selfKey ctxKey = iota
24+
coreConnectorKey ctxKey = iota
2325
)
2426

2527
func DBProvider(q db.DepositsQ) func(context.Context) context.Context {
@@ -84,3 +86,11 @@ func SelfProvider(self core.Address) func(context.Context) context.Context {
8486
func Self(ctx context.Context) core.Address {
8587
return ctx.Value(selfKey).(core.Address)
8688
}
89+
90+
func CoreConnectorProvider(connector *coreConnector.Connector) func(context.Context) context.Context {
91+
return func(ctx context.Context) context.Context { return context.WithValue(ctx, coreConnectorKey, connector) }
92+
}
93+
94+
func CoreConnector(ctx context.Context) *coreConnector.Connector {
95+
return ctx.Value(coreConnectorKey).(*coreConnector.Connector)
96+
}

internal/api/grpc/submit_withdraw.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package grpc
22

33
import (
44
"context"
5-
65
"github.com/hyle-team/tss-svc/internal/api/common"
76
"github.com/hyle-team/tss-svc/internal/api/ctx"
87
"github.com/hyle-team/tss-svc/internal/bridge"
@@ -22,10 +21,11 @@ func (Implementation) SubmitWithdrawal(ctxt context.Context, identifier *types.D
2221
}
2322

2423
var (
25-
clientsRepo = ctx.Clients(ctxt)
26-
data = ctx.DB(ctxt)
27-
logger = ctx.Logger(ctxt)
28-
processor = ctx.Fetcher(ctxt)
24+
clientsRepo = ctx.Clients(ctxt)
25+
data = ctx.DB(ctxt)
26+
logger = ctx.Logger(ctxt)
27+
processor = ctx.Fetcher(ctxt)
28+
coreConnector = ctx.CoreConnector(ctxt)
2929
)
3030

3131
client, err := clientsRepo.Client(identifier.ChainId)
@@ -36,12 +36,12 @@ func (Implementation) SubmitWithdrawal(ctxt context.Context, identifier *types.D
3636
return nil, status.Error(codes.InvalidArgument, err.Error())
3737
}
3838

39-
exists, err := data.Exists(db.ToExistenceCheck(identifier, client.Type()))
39+
d, err := coreConnector.GetDepositInfo(identifier)
4040
if err != nil {
41-
logger.WithError(err).Error("failed to check if deposit exists")
41+
logger.WithError(err).Error("error checking deposit info on core")
4242
return nil, ErrInternal
4343
}
44-
if exists {
44+
if d != nil {
4545
return nil, ErrTxAlreadySubmitted
4646
}
4747

@@ -51,13 +51,30 @@ func (Implementation) SubmitWithdrawal(ctxt context.Context, identifier *types.D
5151
TxNonce: int(identifier.TxNonce),
5252
}
5353

54-
deposit, err := processor.FetchDeposit(id)
54+
deposit, err := data.Get(id)
55+
if err != nil {
56+
logger.WithError(err).Error("error getting deposit")
57+
return nil, ErrInternal
58+
}
59+
if deposit != nil {
60+
return nil, status.Error(codes.AlreadyExists, "deposit already exists")
61+
}
62+
63+
deposit, err = processor.FetchDeposit(id)
5564
if err != nil {
5665
if clients.IsPendingDepositError(err) {
5766
return nil, ErrDepositPending
5867
}
5968
if clients.IsInvalidDepositError(err) {
60-
// TODO: insert in db
69+
deposit = &db.Deposit{
70+
DepositIdentifier: id,
71+
WithdrawalStatus: types.WithdrawalStatus_WITHDRAWAL_STATUS_INVALID,
72+
}
73+
if _, err = data.Insert(*deposit); err != nil {
74+
logger.WithError(err).Error("failed to process deposit")
75+
return nil, ErrInternal
76+
}
77+
6178
return nil, status.Error(codes.InvalidArgument, "invalid deposit")
6279
}
6380

internal/api/server.go

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"context"
5+
coreConnector "github.com/hyle-team/tss-svc/internal/core/connector"
56
"net"
67
"net/http"
78
"time"
@@ -46,6 +47,7 @@ func NewServer(
4647
processor *bridge.DepositFetcher,
4748
broadcaster *p2p.Broadcaster,
4849
self core.Address,
50+
connector *coreConnector.Connector,
4951
) *Server {
5052
return &Server{
5153
grpc: grpc,
@@ -59,6 +61,7 @@ func NewServer(
5961
ctx.FetcherProvider(processor),
6062
ctx.BroadcasterProvider(broadcaster),
6163
ctx.SelfProvider(self),
64+
ctx.CoreConnectorProvider(connector),
6265
},
6366
}
6467
}

internal/bridge/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Signing data validation: using the provided deposit data and the signing data, t
7171

7272
### Zano network
7373
Signing data construction: according to the provided deposit data, the [`emit_asset`](https://docs.zano.org/docs/build/rpc-api/wallet-rpc-api/emit_asset/) request is sent to the Zano wallet RPC server, and the resulting `VerifiedTxID` field is a ready-to-sign data.
74-
74+
7575
Signing data validation: using the provided deposit data and provided additional data from the `emit_asset` response by the proposer, the constructor has the ability to decrypt transaction details using [`decrypt_tx_details`](https://docs.zano.org/docs/build/rpc-api/daemon-rpc-api/decrypt_tx_details) method.
7676
Constructor validates:
7777
- if the provided `VerifiedTxID` matches the `decrypt_tx_details` response;

internal/bridge/acceptor_session.go

+31-17
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"context"
55
"fmt"
66

7-
bridgeTypes "github.com/hyle-team/tss-svc/internal/bridge/clients"
7+
"github.com/hyle-team/tss-svc/internal/bridge/clients"
88
"github.com/hyle-team/tss-svc/internal/core"
99
"github.com/hyle-team/tss-svc/internal/db"
1010
"github.com/hyle-team/tss-svc/internal/p2p"
11+
1112
"github.com/hyle-team/tss-svc/internal/types"
1213
"github.com/pkg/errors"
1314
"gitlab.com/distributed_lab/logan/v3"
@@ -28,7 +29,6 @@ type DepositAcceptorSession struct {
2829
fetcher *DepositFetcher
2930
data db.DepositsQ
3031
logger *logan.Entry
31-
clients bridgeTypes.Repository
3232

3333
distributors map[core.Address]struct{}
3434

@@ -38,7 +38,6 @@ type DepositAcceptorSession struct {
3838
func NewDepositAcceptorSession(
3939
distributors []p2p.Party,
4040
fetcher *DepositFetcher,
41-
clients bridgeTypes.Repository,
4241
data db.DepositsQ,
4342
logger *logan.Entry,
4443
) *DepositAcceptorSession {
@@ -52,7 +51,6 @@ func NewDepositAcceptorSession(
5251
msgs: make(chan distributedDeposit, 100),
5352
data: data,
5453
logger: logger,
55-
clients: clients,
5654
distributors: distributorsMap,
5755
}
5856
}
@@ -68,27 +66,43 @@ func (d *DepositAcceptorSession) Run(ctx context.Context) {
6866
case msg := <-d.msgs:
6967
d.logger.Info(fmt.Sprintf("received deposit from %s", msg.Distributor))
7068

71-
client, err := d.clients.Client(msg.Identifier.ChainId)
72-
if err != nil {
73-
d.logger.Error("got unsupported chain identifier")
74-
continue
69+
id := db.DepositIdentifier{
70+
ChainId: msg.Identifier.ChainId,
71+
TxHash: msg.Identifier.TxHash,
72+
TxNonce: int(msg.Identifier.TxNonce),
7573
}
7674

77-
if exists, err := d.data.Exists(db.ToExistenceCheck(msg.Identifier, client.Type())); err != nil {
75+
deposit, err := d.data.Get(db.DepositIdentifier{
76+
TxHash: msg.Identifier.TxHash,
77+
TxNonce: int(msg.Identifier.TxNonce),
78+
ChainId: msg.Identifier.ChainId,
79+
})
80+
if err != nil {
7881
d.logger.WithError(err).Error("failed to check if deposit exists")
7982
continue
80-
} else if exists {
81-
d.logger.Info("deposit already exists")
83+
} else if deposit != nil {
84+
d.logger.Warn("deposit already exists")
8285
continue
8386
}
8487

85-
deposit, err := d.fetcher.FetchDeposit(db.DepositIdentifier{
86-
ChainId: msg.Identifier.ChainId,
87-
TxHash: msg.Identifier.TxHash,
88-
TxNonce: int(msg.Identifier.TxNonce),
89-
})
88+
deposit, err = d.fetcher.FetchDeposit(id)
9089
if err != nil {
91-
// TODO: checkout err type
90+
if clients.IsPendingDepositError(err) {
91+
d.logger.Warn("deposit still pending")
92+
continue
93+
}
94+
if clients.IsInvalidDepositError(err) {
95+
deposit = &db.Deposit{
96+
DepositIdentifier: id,
97+
WithdrawalStatus: types.WithdrawalStatus_WITHDRAWAL_STATUS_INVALID,
98+
}
99+
if _, err = d.data.Insert(*deposit); err != nil {
100+
d.logger.WithError(err).Error("failed to process deposit")
101+
continue
102+
}
103+
d.logger.Warn("invalid deposit")
104+
continue
105+
}
92106
d.logger.WithError(err).Error("failed to fetch deposit")
93107
continue
94108
}

internal/bridge/clients/bitcoin/withdraw.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ const (
2020
)
2121

2222
func (c *Client) CreateUnsignedWithdrawalTx(deposit db.Deposit, changeAddr string) (*wire.MsgTx, [][]byte, error) {
23-
amount, set := new(big.Int).SetString(*deposit.WithdrawalAmount, 10)
23+
amount, set := new(big.Int).SetString(deposit.WithdrawalAmount, 10)
2424
if !set {
2525
return nil, nil, errors.New("failed to parse amount")
2626
}
27-
receiverAddr, err := btcutil.DecodeAddress(*deposit.Receiver, c.chain.Params)
27+
receiverAddr, err := btcutil.DecodeAddress(deposit.Receiver, c.chain.Params)
2828
if err != nil {
2929
return nil, nil, errors.Wrap(err, "failed to decode receiver address")
3030
}

internal/bridge/clients/evm/operations/erc20.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ type WithdrawERC20Content struct {
2222
}
2323

2424
func NewWithdrawERC20Content(data db.Deposit) (*WithdrawERC20Content, error) {
25-
destinationChainID, ok := new(big.Int).SetString(*data.WithdrawalChainId, 10)
25+
destinationChainID, ok := new(big.Int).SetString(data.WithdrawalChainId, 10)
2626
if !ok {
2727
return nil, errors.New("invalid chains id")
2828
}
2929

30-
withdrawalAmount, ok := new(big.Int).SetString(*data.WithdrawalAmount, 10)
30+
withdrawalAmount, ok := new(big.Int).SetString(data.WithdrawalAmount, 10)
3131
if !ok {
3232
return nil, errors.New("invalid withdrawal amount")
3333
}
3434

35-
if !common.IsHexAddress(*data.Receiver) {
35+
if !common.IsHexAddress(data.Receiver) {
3636
return nil, errors.New("invalid destination address")
3737
}
3838

3939
return &WithdrawERC20Content{
4040
Amount: ToBytes32(withdrawalAmount.Bytes()),
41-
Receiver: hexutil.MustDecode(*data.Receiver),
41+
Receiver: hexutil.MustDecode(data.Receiver),
4242
TxHash: hexutil.MustDecode(data.TxHash),
4343
TxNonce: IntToBytes32(data.TxNonce),
4444
ChainID: ToBytes32(destinationChainID.Bytes()),
45-
DestinationTokenAddress: common.HexToAddress(*data.WithdrawalToken).Bytes(),
46-
IsWrapped: BoolToBytes(*data.IsWrappedToken),
45+
DestinationTokenAddress: common.HexToAddress(data.WithdrawalToken).Bytes(),
46+
IsWrapped: BoolToBytes(data.IsWrappedToken),
4747
}, nil
4848
}
4949

0 commit comments

Comments
 (0)