Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/cheque cashout bug #356

Merged
merged 151 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
151 commits
Select commit Hold shift + click to select a range
7627be3
feat: accesss key
imstevez Jul 24, 2023
a57905b
feat: access-key module & access-key cmds
imstevez Jul 27, 2023
959d35d
fix: remove redunt error condition
imstevez Jul 27, 2023
4ddc476
opt: handle wrapper error and remove unused bucket type define in acc…
imstevez Jul 27, 2023
a791ada
fix: return an explicit error instead of panic and optimize the error…
Shawn-Huang-Tron Jul 28, 2023
92436c7
feat: add daemon check before execute accesskey commands
imstevez Aug 7, 2023
3082ff1
optmize: access-key store prefix
imstevez Aug 7, 2023
5e72e40
optmize: not found error
imstevez Aug 8, 2023
3962044
feat: add s3 signature
turingczz Aug 8, 2023
7d25946
chore:
turingczz Aug 8, 2023
d7ad67d
chore:
turingczz Aug 9, 2023
21d4705
chore:
turingczz Aug 9, 2023
7d0c76f
chore:
turingczz Aug 9, 2023
25baad4
chore:
turingczz Aug 9, 2023
7711c82
feat: add store
turingczz Aug 10, 2023
f4324c0
chore:
turingczz Aug 10, 2023
5c57bec
feat: s3 access-key, server, handlers, statestore, filestore
imstevez Aug 10, 2023
d434b63
chore:
turingczz Aug 10, 2023
3d9d343
optmize: code structure
imstevez Aug 11, 2023
a4e6f56
style: s3 code structure
imstevez Aug 11, 2023
9a1bf42
Merge branch 's3_auth' into feat/s3-compatible-api
imstevez Aug 11, 2023
865daf9
style: code structure
imstevez Aug 11, 2023
6299ba8
optmize: code structure
imstevez Aug 12, 2023
7aa2ca5
feat: add multiple context lock
imstevez Aug 14, 2023
ee3e5f8
feat: check auth
turingczz Aug 14, 2023
44da310
chore:
turingczz Aug 14, 2023
1784c49
chore:
turingczz Aug 14, 2023
84dd056
feat: add bucket service
turingczz Aug 15, 2023
bdc1837
chore:
turingczz Aug 15, 2023
29d9ce3
Merge remote-tracking branch 'origin/feat/s3-compatible-api' into s3-…
turingczz Aug 15, 2023
d30af9e
mod: update bucket lock
turingczz Aug 15, 2023
e4f6109
chore:
turingczz Aug 15, 2023
5e4a427
chore:
turingczz Aug 15, 2023
6b3c0c1
chore:
turingczz Aug 15, 2023
4faa010
del s3d
turingczz Aug 15, 2023
8176acc
chore: s3 req & rsp structure
imstevez Aug 15, 2023
6874753
chore:
turingczz Aug 16, 2023
0b4e596
feat: add multibase commands (#342)
Shawn-Huang-Tron Aug 16, 2023
fbbb5e1
feat: add pubBucket api
turingczz Aug 17, 2023
3a572cb
feat: add more bucket api
turingczz Aug 17, 2023
3ce88b3
chore:
turingczz Aug 17, 2023
0eaf129
chore:
turingczz Aug 17, 2023
0b87d68
feat: add request and response
turingczz Aug 17, 2023
3e84557
chore:
turingczz Aug 17, 2023
1cab398
feat: server build
imstevez Aug 18, 2023
bd25a00
chore: check acl
turingczz Aug 21, 2023
0fab872
chore
turingczz Aug 22, 2023
7d83079
chore: adjust bucket url
turingczz Aug 22, 2023
a869bfd

imstevez Aug 23, 2023
7996a1e
feat: add auth middleware
imstevez Aug 23, 2023
3b817d4
feat: adjust code structure
imstevez Aug 23, 2023
366a184
optmize: code structure & auth bug
imstevez Aug 23, 2023
ea2bb72
feat: put object
imstevez Aug 23, 2023
a689141
chore:
turingczz Aug 24, 2023
98a169f
mod: mod bucket parse req
turingczz Aug 24, 2023
4df4864
optmize: adjust place of response error
imstevez Aug 24, 2023
0ecff79
chore: mig sig 01
imstevez Aug 24, 2023
1c5ac9d
chore: clear sig
imstevez Aug 24, 2023
c492fad
chore: mig sig 02
imstevez Aug 24, 2023
a839315
chore: mig sig 03
imstevez Aug 24, 2023
691e63c
chore: mig sig 04
imstevez Aug 24, 2023
ed1a503
optmize: refractor codes
imstevez Aug 24, 2023
0a5e0d6
optmize: rename auth to sign
imstevez Aug 24, 2023
fe0156c
optmize: code structure & h.name
imstevez Aug 24, 2023
0c135c3
fix: h.name
imstevez Aug 24, 2023
17118db
feat: put-object
imstevez Aug 24, 2023
be278a4
feat: multipart
imstevez Aug 24, 2023
5fa266e
fix: nslock key
imstevez Aug 24, 2023
fbe0ec5
chore: rename s3 constructor file name
imstevez Aug 24, 2023
cb34bf1
fix: tidy example go-ipfs-as-a-library go mod
imstevez Aug 26, 2023
bc0f8e7
Merge branch 'fix/exp-go-mod' into s3-compatible-api-auth-2
imstevez Aug 26, 2023
860a7fc
chore: change default s3 server address to local
imstevez Aug 26, 2023
d38caf1
mod: add object api
turingczz Aug 28, 2023
dfda6e2
Merge branch 's3-compatible-api-auth-2' of github.com:bittorrent/go-b…
turingczz Aug 28, 2023
2a61167
feat: s3-compatible-api - 1. add start option and configure; 2. optmi…
imstevez Aug 29, 2023
f21eb32
Merge branch 's3-compatible-api-auth-2' into s3-compatible-api-z
turingczz Aug 30, 2023
71ee7b3
merge: object
turingczz Aug 30, 2023
83889ee
chore: add object lock
turingczz Aug 30, 2023
278fe64
chore: of delete objs
turingczz Aug 30, 2023
73ca8c8
chore:
turingczz Aug 30, 2023
bbfcb97
fix: list objects bug
imstevez Aug 30, 2023
bbed9cd
chore: rename ListObjetV1Handler to ListObjectHandler, rename BTFS-Ha…
imstevez Aug 30, 2023
d34b4c6
refractor: bucket service
imstevez Aug 31, 2023
d0d92c9
refactor: object service
imstevez Aug 31, 2023
714aa30
refractor: refract object service
imstevez Sep 1, 2023
8097ebb
refractor: handlers
imstevez Sep 1, 2023
b7eca10
refractor: bucket handler
imstevez Sep 2, 2023
187f740
refractor: bucket handler
imstevez Sep 2, 2023
ec4bb37
refractor: response
imstevez Sep 3, 2023
ac30bbe
refractor: response func
imstevez Sep 4, 2023
2fb46c1
refractor: response
imstevez Sep 4, 2023
59577be
refractor: object
imstevez Sep 4, 2023
1bbb036
feat: add backup and recovery command (#348)
Shawn-Huang-Tron Sep 5, 2023
12f68bf
refractor: object
imstevez Sep 6, 2023
00c7227
refractor: objects
imstevez Sep 6, 2023
c2b877e
refractor: btf api add timeout & add cid refs to enable referred cid…
imstevez Sep 6, 2023
0e8707d
ref: fix delete object remove body
imstevez Sep 6, 2023
102db6b
ref: format code
imstevez Sep 6, 2023
f5153f3
fix: routers
imstevez Sep 7, 2023
f086009
fix: add cors header
imstevez Sep 7, 2023
23b3caf
fix: router option
imstevez Sep 7, 2023
ac5145c
feat: add delete objects handler
imstevez Sep 7, 2023
0410494
refractor: multipart
imstevez Sep 8, 2023
4d3d6b7
ref: multipart
imstevez Sep 8, 2023
df7cb3d
fix: multipart etag calculation
imstevez Sep 8, 2023
568e64b
chore: add min part size todo
imstevez Sep 8, 2023
8dbe3c7
chore: upgrade 'github.com/anacrolix/torrent' from v1.47.0 to v1.52.5
imstevez Sep 9, 2023
bcc55bf
opt: comment and amz header
imstevez Sep 9, 2023
510e7fe
opt: code
imstevez Sep 10, 2023
b5f5e5f
opt: preflight cache max age
imstevez Sep 11, 2023
6c71fb5
feat: bucket response add acl header
imstevez Sep 11, 2023
dbb474d
opt: change cid-list header to cid
imstevez Sep 11, 2023
fe87cab
fix: required check exlude unknow location
imstevez Sep 12, 2023
6a2ac3a
fix: get object acl
imstevez Sep 13, 2023
3e1def6
ref: requests
imstevez Sep 18, 2023
c62f651
ref: complete refractor
imstevez Sep 19, 2023
a18e40e
fix: args parse
imstevez Sep 19, 2023
962a5ad
fix: get object unlock
imstevez Sep 19, 2023
cd8ef90
fix: object acl writer
imstevez Sep 19, 2023
5a6bdc5
fix: delete objects error
imstevez Sep 19, 2023
39b0fdd
fix: Sign handler name
imstevez Sep 19, 2023
9fc88cd
fix: object name escape
imstevez Sep 20, 2023
40735b2
fix: copy source validate
imstevez Sep 20, 2023
7c6bb5b
chore: fix
turingczz Sep 20, 2023
7034f17
opt: s3 log
imstevez Sep 20, 2023
75a8e21
opt: s3 api log
imstevez Sep 20, 2023
95d2aac
fix: allow Cache-Control header in PutObject and CopyObject Action
imstevez Sep 21, 2023
81db736
feat: log details (#349)
Shawn-Huang-Tron Sep 21, 2023
e0cba7a
merge: s3-compatible-api
imstevez Sep 21, 2023
16db48a
opt: add access-key command taglines
imstevez Sep 21, 2023
0804939
Merge branch 'feat/s3-compatible-api' into pre_2.3.3
imstevez Sep 21, 2023
f8b2589
opt: add accesskey command description
imstevez Sep 21, 2023
ecfa625
Merge branch 'feat/s3-compatible-api' into pre_2.3.3
imstevez Sep 21, 2023
ce2eacc
chore: add out of FixChequeCashOutCmd
turingczz Sep 21, 2023
6ff651d
chore:
turingczz Sep 21, 2023
88c3a68
chore
turingczz Sep 22, 2023
85fb908
chore:
turingczz Sep 22, 2023
b05bc79
chore:
turingczz Sep 22, 2023
2736d71
chore:
turingczz Sep 22, 2023
08c2a19
chore
turingczz Sep 22, 2023
b9aedc2
Merge branch 'fix/cash-out-bug' into fix/cheque-cashout-bug
turingczz Sep 22, 2023
fd09b33
chore: add accesskey commands test path
imstevez Sep 22, 2023
08cc667
chore:
turingczz Sep 22, 2023
5aca67c
chore: add accesskey test path
imstevez Sep 22, 2023
9ffb273
chore: add accesskey test path
imstevez Sep 22, 2023
5913d93
chore:
turingczz Sep 22, 2023
585c449
chore:
turingczz Sep 22, 2023
71f6bcc
chore:
turingczz Sep 22, 2023
6def651
Merge remote-tracking branch 'origin/pre_2.3.3' into fix/cheque-casho…
turingczz Sep 22, 2023
a812b31
chore:
turingczz Sep 22, 2023
ce0e663
Merge branch 'release_2.3.3' into fix/cheque-cashout-bug
laocheng-cheng Sep 22, 2023
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
1 change: 1 addition & 0 deletions cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ If the user need to start multiple nodes on the same machine, the configuration
spin.Analytics(api, cctx.ConfigRoot, node, version.CurrentVersionNumber, hValue)
spin.Hosts(node, env)
spin.Contracts(node, req, env, nodepb.ContractStat_HOST.String())
spin.RestartFixChequeCashOut()
}

// Give the user some immediate feedback when they hit C-c
Expand Down
25 changes: 20 additions & 5 deletions core/commands/cheque/cheque.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ type ListChequeRet struct {
Len int
}

type fixCheque struct {
PeerID string
Token string
Beneficiary string
Vault string
TotalCashedAmount *big.Int
FixCashedAmount *big.Int
}

type ListFixChequeRet struct {
FixCheques []fixCheque
Len int
}

type ReceiveCheque struct {
PeerID string
Token common.Address
Expand Down Expand Up @@ -65,11 +79,12 @@ var ChequeCmd = &cmds.Command{
Vault services include issue cheque to peer, receive cheque and store operations.`,
},
Subcommands: map[string]*cmds.Command{
"cash": CashChequeCmd,
"cashstatus": ChequeCashStatusCmd,
"cashlist": ChequeCashListCmd,
"price": StorePriceCmd,
"price-all": StorePriceAllCmd,
"cash": CashChequeCmd,
"cashstatus": ChequeCashStatusCmd,
"cashlist": ChequeCashListCmd,
"price": StorePriceCmd,
"price-all": StorePriceAllCmd,
"fix_cheque_cashout": FixChequeCashOutCmd,

"send": SendChequeCmd,
"sendlist": ListSendChequesCmd,
Expand Down
75 changes: 75 additions & 0 deletions core/commands/cheque/fix_cheque_cashout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cheque

import (
"fmt"
cmds "github.com/bittorrent/go-btfs-cmds"
"github.com/bittorrent/go-btfs/chain"
"github.com/bittorrent/go-btfs/chain/tokencfg"
"github.com/bittorrent/go-btfs/utils"
"github.com/google/martian/log"
"golang.org/x/net/context"
"io"
)

var FixChequeCashOutCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List cheque(s) received from peers.",
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
err := utils.CheckSimpleMode(env)
if err != nil {
return err
}

listRet := ListFixChequeRet{}
listRet.FixCheques = make([]fixCheque, 0)

for _, tokenAddr := range tokencfg.MpTokenAddr {
cheques, err := chain.SettleObject.SwapService.LastReceivedCheques(tokenAddr)
if err != nil {
return err
}

for k, v := range cheques {
totalCashOutAmount, newCashOutAmount, err := chain.SettleObject.CashoutService.AdjustCashCheque(
context.Background(), v.Vault, v.Beneficiary, tokenAddr, false)
if err != nil {
return err
}
if newCashOutAmount != nil && newCashOutAmount.Uint64() > 0 {
var record fixCheque
record.PeerID = k
record.Token = v.Token.String()
record.Beneficiary = v.Beneficiary.String()
record.Vault = v.Vault.String()
record.TotalCashedAmount = totalCashOutAmount
record.FixCashedAmount = newCashOutAmount

listRet.FixCheques = append(listRet.FixCheques, record)
}
}
}
listRet.Len = len(listRet.FixCheques)

log.Infof("FixChequeCashOutCmd, listRet = %+v", listRet)

return cmds.EmitOnce(res, &listRet)
},
Type: ListFixChequeRet{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ListFixChequeRet) error {
fmt.Fprintf(w, "fix: \n\t%-55s\t%-46s\t%-46s\t%-46s\tfix_cash_amount: \n", "peerID:", "vault:", "beneficiary:", "total_cash_amount:")
for iter := 0; iter < out.Len; iter++ {
fmt.Fprintf(w, "\t%-55s\t%-46s\t%-46s\t%d\t%d \n",
out.FixCheques[iter].PeerID,
out.FixCheques[iter].Vault,
out.FixCheques[iter].Beneficiary,
out.FixCheques[iter].TotalCashedAmount.Uint64(),
out.FixCheques[iter].FixCashedAmount.Uint64(),
)
}

return nil
}),
},
}
15 changes: 8 additions & 7 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ func TestCommands(t *testing.T) {
"/bittorrent/scrape",
"/bittorrent/metainfo",
"/bittorrent/bencode",
"/multibase",
"/multibase/encode",
"/multibase/decode",
"/multibase/transcode",
"/multibase/list",
"/backup",
"/recovery",
"/accesskey",
"/accesskey/generate",
"/accesskey/enable",
Expand All @@ -350,13 +357,7 @@ func TestCommands(t *testing.T) {
"/accesskey/delete",
"/accesskey/get",
"/accesskey/list",
"/multibase",
"/multibase/encode",
"/multibase/decode",
"/multibase/transcode",
"/multibase/list",
"/backup",
"/recovery",
"/cheque/fix_cheque_cashout",
}

cmdSet := make(map[string]struct{})
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/go-bindata/go-bindata/v3 v3.1.3
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.3
github.com/google/martian v2.1.0+incompatible
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.7.3
github.com/hashicorp/go-multierror v1.1.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down
16 changes: 12 additions & 4 deletions settlement/swap/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ func (m *addressbookMock) PutVault(peer string, vault common.Address) error {
}

type cashoutMock struct {
cashCheque func(ctx context.Context, vault, recipient common.Address, token common.Address) (common.Hash, error)
cashoutStatus func(ctx context.Context, vaultAddress common.Address, token common.Address) (*vault.CashoutStatus, error)
cashoutResults func() ([]vault.CashOutResult, error)
hasCashoutAction func(ctx context.Context, peer common.Address, token common.Address) (bool, error)
cashCheque func(ctx context.Context, vault, recipient common.Address, token common.Address) (common.Hash, error)
cashoutStatus func(ctx context.Context, vaultAddress common.Address, token common.Address) (*vault.CashoutStatus, error)
cashoutResults func() ([]vault.CashOutResult, error)
hasCashoutAction func(ctx context.Context, peer common.Address, token common.Address) (bool, error)
adjustCashCheque func(ctx context.Context, vaultAddress, recipient common.Address, token common.Address, restartPassFlag bool) (totalCashOutAmount, newCashOutAmount *big.Int, err error)
restartFixChequeCashOut func()
}

func (m *cashoutMock) CashCheque(ctx context.Context, vault, recipient common.Address, token common.Address) (common.Hash, error) {
Expand All @@ -142,6 +144,12 @@ func (m *cashoutMock) CashoutResults() ([]vault.CashOutResult, error) {
func (m *cashoutMock) HasCashoutAction(ctx context.Context, peer common.Address, token common.Address) (bool, error) {
return m.hasCashoutAction(ctx, peer, token)
}
func (m *cashoutMock) AdjustCashCheque(ctx context.Context, vaultAddress, recipient common.Address, token common.Address, restartPassFlag bool) (totalCashOutAmount, newCashOutAmount *big.Int, err error) {
return m.adjustCashCheque(ctx, vaultAddress, recipient, token, restartPassFlag)
}
func (m *cashoutMock) RestartFixChequeCashOut() {
m.restartFixChequeCashOut()
}
func TestReceiveCheque(t *testing.T) {
store := mockstore.NewStateStore()
vaultService := mockvault.NewVault(
Expand Down
157 changes: 157 additions & 0 deletions settlement/swap/vault/cashout.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type CashoutService interface {
CashCheque(ctx context.Context, vault, recipient common.Address, token common.Address) (common.Hash, error)
// CashoutStatus gets the status of the latest cashout transaction for the vault
CashoutStatus(ctx context.Context, vaultAddress common.Address, token common.Address) (*CashoutStatus, error)
AdjustCashCheque(ctx context.Context, vaultAddress, recipient common.Address, token common.Address, restartPassFlag bool) (totalCashOutAmount, newCashOutAmount *big.Int, err error)
HasCashoutAction(ctx context.Context, peer common.Address, token common.Address) (bool, error)
CashoutResults() ([]CashOutResult, error)
RestartFixChequeCashOut()
}

type cashoutService struct {
Expand Down Expand Up @@ -178,6 +180,10 @@ func (s *cashoutService) CashoutResults() ([]CashOutResult, error) {

// CashCheque sends a cashout transaction for the last cheque of the vault
func (s *cashoutService) CashCheque(ctx context.Context, vault, recipient common.Address, token common.Address) (common.Hash, error) {
if RestartFixCashOutStatusLock {
return common.Hash{}, errors.New("Just started, it can not cash cheque, you will wait for about 40s to do it. ")
}

cheque, err := s.chequeStore.LastReceivedCheque(vault, token)
if err != nil {
return common.Hash{}, err
Expand Down Expand Up @@ -215,6 +221,19 @@ func (s *cashoutService) CashCheque(ctx context.Context, vault, recipient common
return common.Hash{}, err
}

// 1.add cash out status
cashOutStateInfo := CashOutStatusStoreInfo{
Token: token,
Vault: cheque.Vault,
Beneficiary: cheque.Beneficiary,
CumulativePayout: cheque.CumulativePayout,
TxHash: txHash.String(),
}
err = s.AddCashOutStatusStore(cashOutStateInfo)
if err != nil {
return common.Hash{}, err
}

// WaitForReceipt takes long time
go func() {
defer func() {
Expand All @@ -223,6 +242,13 @@ func (s *cashoutService) CashCheque(ctx context.Context, vault, recipient common
}
}()
s.storeCashResult(context.Background(), vault, txHash, cheque, token)

// 2.delete cash out status
err = s.DeleteCashOutStatusStore(cashOutStateInfo)
if err != nil {
log.Errorf("delete cashout status, err = %v", err)
return
}
}()
return txHash, nil
}
Expand Down Expand Up @@ -304,6 +330,137 @@ func (s *cashoutService) storeCashResult(ctx context.Context, vault common.Addre
return nil
}

// AdjustCashCheque .
func (s *cashoutService) AdjustCashCheque(ctx context.Context, vaultAddress, recipient common.Address, token common.Address, restartPassFlag bool) (totalCashOutAmount, newCashOutAmount *big.Int, err error) {
if RestartFixCashOutStatusLock {
if !restartPassFlag {
return nil, nil, errors.New("Just started, it can not fix cash out status, you will wait for about 40s to do it. ")
}
}

// 1.totalReceivedCashed
totalReceivedCashed := big.NewInt(0)
err = s.store.Get(tokencfg.AddToken(statestore.TotalReceivedCashedKey, token), &totalReceivedCashed)
if err != nil && err != storage.ErrNotFound {
return nil, nil, err
}

// 2.alreadyPaidOut in renter contract
// blockchain calls below
contract := newVaultContractMuti(vaultAddress, s.transactionService)
alreadyPaidOutOnline, err := contract.PaidOut(ctx, recipient, token)
if err != nil {
return nil, nil, err
}

// 3.compare it to fix.
diff := big.NewInt(0).Sub(alreadyPaidOutOnline, totalReceivedCashed)
log.Infof("AdjustCashCheque: diff > 0, vault=%s, recipient=%s, online=%s, local=%s, diff=%s",
vaultAddress.String(), recipient.String(),
alreadyPaidOutOnline.String(), totalReceivedCashed.String(), diff.String(),
)

if diff.Cmp(big.NewInt(0)) > 0 {
cashResult, err := s.fixStoreCashResult(vaultAddress, diff, token)
if err != nil {
return nil, nil, err
}
newCashOutAmount = cashResult.Amount
}

return alreadyPaidOutOnline, newCashOutAmount, nil
}

func (s *cashoutService) RestartFixChequeCashOut() {
if RestartFixCashOutStatusLock {
list, err := s.GetAllCashOutStatusStore()
if err != nil {
log.Infof("RestartFixChequeCashOut: GetAllCashOutStatusStore err = %v", err)
return
}

if len(list) > 0 {
log.Infof("wait 30s, for fixing cash out status")

// wait 30s, for online cashing out ok.
time.Sleep(time.Second * RestartWaitCashOutOnlineTime)

for _, v := range list {
_, _, err := s.AdjustCashCheque(context.Background(), v.Vault, v.Beneficiary, v.Token, true)
if err != nil {
log.Infof("RestartFixChequeCashOut: AdjustCashCheque err = %v, info = %+v", err, v)
continue
}

err = s.DeleteCashOutStatusStore(v)
if err != nil {
log.Infof("RestartFixChequeCashOut: DeleteCashOutStatusStore err = %v, info = %+v", err, v)
continue
}
}
}
RestartFixCashOutStatusLock = false
}
return
}

func (s *cashoutService) fixStoreCashResult(vault common.Address, shouldPaidOut *big.Int, token common.Address) (cashResult *CashOutResult, err error) {
txHash := common.Hash{} //fix txHash: 0x0000...
cashResult = &CashOutResult{
TxHash: txHash,
Vault: vault,
Token: token,
Amount: shouldPaidOut,
CashTime: time.Now().Unix(),
Status: "success",
}

totalReceivedCashed := big.NewInt(0)
if err = s.store.Get(tokencfg.AddToken(statestore.TotalReceivedCashedKey, token), &totalReceivedCashed); err == nil || err == storage.ErrNotFound {
totalReceivedCashed = totalReceivedCashed.Add(totalReceivedCashed, shouldPaidOut)
err := s.store.Put(tokencfg.AddToken(statestore.TotalReceivedCashedKey, token), totalReceivedCashed)
if err != nil {
log.Infof("fixStoreCashResult:put totalReceivedCashdKey err:%+v", err)
}
}

totalDailyReceivedCashed := big.NewInt(0)
if err = s.store.Get(statestore.GetTodayTotalDailyReceivedCashedKey(token), &totalDailyReceivedCashed); err == nil || err == storage.ErrNotFound {
totalDailyReceivedCashed = totalDailyReceivedCashed.Add(totalDailyReceivedCashed, shouldPaidOut)
err := s.store.Put(statestore.GetTodayTotalDailyReceivedCashedKey(token), totalDailyReceivedCashed)
if err != nil {
log.Infof("fixStoreCashResult:put totalReceivedDailyCashdKey err:%+v", err)
}
}

// update TotalReceivedCountCashed
uncashed := 0
err = s.store.Get(statestore.PeerReceivedUncashRecordsCountKey(vault, token), &uncashed)
if err != nil {
log.Infof("fixStoreCashResult:put totalReceivedCountCashed err:%+v", err)
} else {
cashedCount := 0
err := s.store.Get(tokencfg.AddToken(statestore.TotalReceivedCashedCountKey, token), &cashedCount)
if err == nil || err == storage.ErrNotFound {
err := s.store.Put(tokencfg.AddToken(statestore.TotalReceivedCashedCountKey, token), cashedCount+uncashed)
if err != nil {
log.Infof("fixStoreCashResult:put totalReceivedCashedConuntKey err:%+v", err)
} else {
err := s.store.Put(statestore.PeerReceivedUncashRecordsCountKey(vault, token), 0)
if err != nil {
log.Infof("fixStoreCashResult:put totalReceivedCashedConuntKey err:%+v", err)
}
}
}
}

err = s.store.Put(statestore.CashoutResultKey(vault), &cashResult)
if err != nil {
log.Infof("fixStoreCashResult:put cashoutResultKey err:%+v", err)
}
return
}

// CashoutStatus gets the status of the latest cashout transaction for the vault
func (s *cashoutService) CashoutStatus(ctx context.Context, vaultAddress common.Address, token common.Address) (*CashoutStatus, error) {
cheque, err := s.chequeStore.LastReceivedCheque(vaultAddress, token)
Expand Down
Loading
Loading