From: nsz Date: Sun, 29 Jan 2012 19:00:26 +0000 (+0100) Subject: fix balance check in server logic X-Git-Url: http://nsz.repo.hu/git/?p=epoint;a=commitdiff_plain;h=5e9fc897c292d0c51e263f6d8a4f6cd31da131cc;hp=a6d3c7c07507062519a125e3f0f10d8a212fd483 fix balance check in server logic --- diff --git a/pkg/server/server.go b/pkg/server/server.go index 93d8da2..8be10b2 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -429,6 +429,10 @@ func handleDebit(w *work) (c []byte, err error) { return } +func isIssuer(c *document.Cert) bool { + return c.Issuer == c.Holder +} + func newDebitCert(w *work) (*document.DebitCert, error) { cert := new(document.DebitCert) cert.Holder = w.draft.Drawer @@ -461,6 +465,7 @@ func newDebitCert(w *work) (*document.DebitCert, error) { // internal error return nil, err } + // TODO: make sure oldcert and newcert cannot become inconsistent // TODO: this is a hack oldcert, err := document.ToCert(iv) if err != nil { @@ -473,8 +478,8 @@ func newDebitCert(w *work) (*document.DebitCert, error) { if cert.Balance <= -IntLimit { return nil, fmt.Errorf("balance limit exceeded: %d", cert.Balance) } - if oldcert.Balance > 0 && cert.Balance < 0 { - return nil, fmt.Errorf("insufficient funds: %d", oldcert.Balance) + if !isIssuer(&cert.Cert) && cert.Balance < 0 { + return nil, fmt.Errorf("insufficient funds: %d, draft: %d", oldcert.Balance, cert.Difference) } cert.LastDebitSerial = oldcert.LastDebitSerial cert.LastCreditSerial = oldcert.LastCreditSerial @@ -536,6 +541,10 @@ func newCreditCert(w *work) (*document.CreditCert, error) { if cert.Balance >= IntLimit { return nil, fmt.Errorf("balance limit exceeded: %d", cert.Balance) } + // sanity check + if isIssuer(&cert.Cert) && cert.Balance > 0 { + return nil, fmt.Errorf("internal error") + } cert.LastDebitSerial = oldcert.LastDebitSerial cert.LastCreditSerial = oldcert.LastCreditSerial if _, ok := iv.(*document.DebitCert); ok {