diff --git a/client/core/bond.go b/client/core/bond.go index afa7ff8179..d4cc85b6e0 100644 --- a/client/core/bond.go +++ b/client/core/bond.go @@ -19,7 +19,6 @@ import ( ) const ( - bondAssetSymbol = "dcr" // Hard-coded to Decred for bond, for now // lockTimeLimit is an upper limit on the allowable bond lockTime. lockTimeLimit = 120 * 24 * time.Hour ) @@ -173,14 +172,14 @@ func (c *Core) refundExpiredBonds(ctx context.Context) { } } -func (c *Core) preValidateBond(dc *dexConnection, bond *asset.Bond) (*msgjson.PreValidateBondResult, error) { +func (c *Core) preValidateBond(dc *dexConnection, bond *asset.Bond) error { if len(dc.acct.encKey) == 0 { - return nil, fmt.Errorf("uninitialized account") + return fmt.Errorf("uninitialized account") } pkBytes := dc.acct.pubKey() if len(pkBytes) == 0 { - return nil, fmt.Errorf("account keys not decrypted") + return fmt.Errorf("account keys not decrypted") } assetID, bondCoin := bond.AssetID, bond.CoinID @@ -199,7 +198,7 @@ func (c *Core) preValidateBond(dc *dexConnection, bond *asset.Bond) (*msgjson.Pr preBondRes := new(msgjson.PreValidateBondResult) err := dc.signAndRequest(preBond, msgjson.PreValidateBondRoute, preBondRes, DefaultResponseTimeout) if err != nil { - return nil, codedError(registerErr, err) + return codedError(registerErr, err) } // Check the response signature. @@ -208,11 +207,16 @@ func (c *Core) preValidateBond(dc *dexConnection, bond *asset.Bond) (*msgjson.Pr c.log.Warnf("postbond: DEX signature validation error: %v", err) } if !bytes.Equal(preBondRes.BondID, bondCoin) { - return nil, fmt.Errorf("server reported bond coin ID %v, expected %v", bondCoinStr, + return fmt.Errorf("server reported bond coin ID %v, expected %v", bondCoinStr, coinIDString(assetID, preBondRes.BondID)) } - return preBondRes, nil + if preBondRes.Amount != bond.Amount { + return newError(bondTimeErr, "pre-validated bond amount is not the desired amount: %d != %d", + preBondRes.Amount, bond.Amount) + } + + return nil } func (c *Core) postBond(dc *dexConnection, bond *asset.Bond) (*msgjson.PostBondResult, error) { @@ -512,7 +516,6 @@ func (c *Core) PostBond(form *PostBondForm) (*PostBondResult, error) { c.connMtx.Lock() c.conns[dc.acct.host] = dc c.connMtx.Unlock() - return &PostBondResult{ /* no new bond */ }, nil } // dc.acct is now configured with encKey, privKey, and id for a new @@ -585,14 +588,9 @@ func (c *Core) PostBond(form *PostBondForm) (*PostBondResult, error) { } // Do prevalidatebond with the *unsigned* txn. - pbr, err := c.preValidateBond(dc, bond) - if err != nil { + if err = c.preValidateBond(dc, bond); err != nil { return nil, err } - if pbr.Amount != bondAsset.Amt { - return nil, newError(bondTimeErr, "pre-validated bond amount is not the desired amount: %d != %d", - pbr.Amount, bondAsset.Amt) - } reqConfs := bondAsset.Confs bondCoinStr := coinIDString(bond.AssetID, bond.CoinID) @@ -633,23 +631,27 @@ func (c *Core) PostBond(form *PostBondForm) (*PostBondResult, error) { return nil, fmt.Errorf("failed to store account %v for dex %v: %w", dc.acct.id, host, err) } + c.connMtx.Lock() + c.conns[dc.acct.host] = dc + c.connMtx.Unlock() } dc.acct.authMtx.Lock() dc.acct.pendingBonds = append(dc.acct.pendingBonds, dbBond) dc.acct.authMtx.Unlock() + success = true // no errors after this + // Broadcast the bond and start waiting for confs. c.log.Infof("Broadcasting bond %v (%s) with lock time %v, data = %x.\n\n"+ "BACKUP refund tx paying to current wallet: %x\n\n", bondCoinStr, unbip(bond.AssetID), lockTime, bond.Data, bond.RedeemTx) if bondCoinCast, err := wallet.SendTransaction(bond.SignedTx); err != nil { - c.log.Warnf("Failed to broadcast bond txn: %v") + c.log.Warnf("Failed to broadcast bond txn (%v). Tx bytes: %x", bond.SignedTx) } else if !bytes.Equal(bond.CoinID, bondCoinCast) { c.log.Warnf("Broadcasted bond %v; was expecting %v!", coinIDString(bond.AssetID, bondCoinCast), bondCoinStr) } - // TODO: return/unlock spent coins? c.updateAssetBalance(bond.AssetID) @@ -662,8 +664,6 @@ func (c *Core) PostBond(form *PostBondForm) (*PostBondResult, error) { // when to expect their account to be marked paid by the server. c.monitorBondConfs(dc, bond, reqConfs) - success = true - return &PostBondResult{BondID: bondCoinStr, ReqConfirms: uint16(reqConfs)}, err } diff --git a/client/core/types.go b/client/core/types.go index 91b8d8390a..f7e10663fb 100644 --- a/client/core/types.go +++ b/client/core/types.go @@ -163,7 +163,7 @@ type SupportedAsset struct { // } // PostBondForm is information necessary to post a new bond for a new or -// existing DEX account at he specified DEX address. +// existing DEX account at the specified DEX address. type PostBondForm struct { Addr string `json:"host"` AppPass encode.PassBytes `json:"appPass"` diff --git a/client/rpcserver/types.go b/client/rpcserver/types.go index 2f23858db2..5a5e99fd06 100644 --- a/client/rpcserver/types.go +++ b/client/rpcserver/types.go @@ -442,13 +442,6 @@ func parsePostBondArgs(params *RawParams) (*core.PostBondForm, error) { if err != nil { return nil, err } - lockTime := time.Unix(int64(lockTimeEpoch), 0) - if time.Now().Before(lockTime) { - return nil, fmt.Errorf("locktime already passed") - } - if time.Until(lockTime) > 120*24*time.Hour { - return nil, fmt.Errorf("locktime is longer than 120 days") - } } var cert []byte if len(params.Args) > 4 {