Skip to content

Commit

Permalink
fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Nov 25, 2022
1 parent ceb4e9e commit 075c282
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
36 changes: 18 additions & 18 deletions client/core/bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion client/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 0 additions & 7 deletions client/rpcserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 075c282

Please sign in to comment.