From f94d9b0e11eed0bdd4d28b04c72e72accd764230 Mon Sep 17 00:00:00 2001 From: Marston Connell <34043723+TheMarstonConnell@users.noreply.github.com> Date: Tue, 6 Dec 2022 13:06:35 -0500 Subject: [PATCH 1/4] fix for strays --- upgrades/v1.2.0.md | 70 +++++++++++++++++ .../keeper/msg_server_cancel_contract.go | 76 +++++++++++++++++-- x/storage/keeper/msg_server_claim_stray.go | 2 +- x/storage/keeper/msg_server_sign_contract.go | 9 ++- 4 files changed, 145 insertions(+), 12 deletions(-) create mode 100644 upgrades/v1.2.0.md diff --git a/upgrades/v1.2.0.md b/upgrades/v1.2.0.md new file mode 100644 index 000000000..5ba183d18 --- /dev/null +++ b/upgrades/v1.2.0.md @@ -0,0 +1,70 @@ +# Jackal v1.2.0 Upgrade Guide + +## Installing New Binary + +Clone the Jackal repo + +``` +git clone https://github.com/JackalLabs/canine-chain.git +cd canine-chain +``` +Checkout the upgrade version + +``` +git fetch +git checkout v1.2.0 +``` + +Build the binary + +``` +make build +``` + +The resulting binary will be `canine-chain/build/canined` + +## Starting the Blockchain + +### Cosmovisor Method + +Ensure your chain is at the following block height: TODO + +**If you halted your chain early, this may not be the case. I recommend following the Canined upgrade below any situation where your node is not already at the halt height.** + +Copy the new binary in place of the existing genesis cosmovisor binary + +``` +cp $HOME/canine-chain/build/canined $HOME/.canine/cosmovisor/genesis/bin/canined +``` + +### Traditonal Method +Set the halt height in the canined app config +``` +sed -i.bak 's/halt-height = 0/halt-height = TODO/' $HOME/.canine/config/app.toml +``` + +Wait until the planned upgrade time (approx. Dec 19 2022, 15:00 UTC) + +At the uprgade time, run the binary to ensure the node syncs to the halt height + +``` +canined start +``` + +Once it stops due to the halt-height, replace the old canined binary with the new one: + +``` +cp $HOME/canine-chain/build/canined $(which canined) +``` + +Remove the halt-height from the config: + +``` +sed -i.bak 's/halt-height = TODO/halt-height = 0/' $HOME/.canine/config/app.toml +``` + +Restart the node: + +``` +canined start +``` diff --git a/x/storage/keeper/msg_server_cancel_contract.go b/x/storage/keeper/msg_server_cancel_contract.go index 49a2dfa75..c1b820181 100644 --- a/x/storage/keeper/msg_server_cancel_contract.go +++ b/x/storage/keeper/msg_server_cancel_contract.go @@ -2,8 +2,10 @@ package keeper import ( "context" + "crypto/sha256" "encoding/json" "fmt" + "io" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -14,27 +16,87 @@ import ( func (k msgServer) CancelContract(goCtx context.Context, msg *types.MsgCancelContract) (*types.MsgCancelContractResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - deal, found := k.GetActiveDeals(ctx, msg.Cid) + root := msg.Cid + + h := sha256.New() + _, err := io.WriteString(h, fmt.Sprintf("%s%d", root, 0)) + if err != nil { + return nil, err + } + hashName := h.Sum(nil) + + left, err := MakeCid(hashName) + if err != nil { + return nil, err + } + h = sha256.New() + _, err = io.WriteString(h, fmt.Sprintf("%s%d", root, 1)) + if err != nil { + return nil, err + } + hashName = h.Sum(nil) + + right, err := MakeCid(hashName) + if err != nil { + return nil, err + } + + var fid string + + d, found := k.GetActiveDeals(ctx, root) + if !found { + s, found := k.GetStrays(ctx, root) + if !found { + return nil, fmt.Errorf("can't find contract") + } + k.RemoveStrays(ctx, s.Cid) + } else { + k.RemoveActiveDeals(ctx, d.Cid) + } + + d, found = k.GetActiveDeals(ctx, left) + if !found { + s, found := k.GetStrays(ctx, left) + if !found { + return nil, fmt.Errorf("can't find contract") + } + k.RemoveStrays(ctx, s.Cid) + } else { + k.RemoveActiveDeals(ctx, d.Cid) + } + + d, found = k.GetActiveDeals(ctx, right) if !found { - return nil, fmt.Errorf("can't find contract") + s, found := k.GetStrays(ctx, right) + if !found { + return nil, fmt.Errorf("can't find contract") + } else { + fid = s.Fid + k.RemoveStrays(ctx, s.Cid) + } + } else { + fid = d.Fid + k.RemoveActiveDeals(ctx, d.Cid) } - if deal.Creator != msg.Creator { + if d.Creator != msg.Creator { return nil, fmt.Errorf("you don't own this deal") } - ftc, found := k.GetFidCid(ctx, deal.Fid) + ftc, found := k.GetFidCid(ctx, fid) if !found { return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, "no fid found") } + var ncids []string - err := json.Unmarshal([]byte(ftc.Cids), &ncids) + err = json.Unmarshal([]byte(ftc.Cids), &ncids) if err != nil { return nil, err } + cids := make([]string, 0) for _, v := range ncids { - if v != msg.Cid { + if v != root && v != left && v != right { cids = append(cids, v) } } @@ -46,7 +108,5 @@ func (k msgServer) CancelContract(goCtx context.Context, msg *types.MsgCancelCon k.SetFidCid(ctx, ftc) - k.RemoveActiveDeals(ctx, deal.Cid) - return &types.MsgCancelContractResponse{}, nil } diff --git a/x/storage/keeper/msg_server_claim_stray.go b/x/storage/keeper/msg_server_claim_stray.go index efbfb03f4..c43a63ec5 100644 --- a/x/storage/keeper/msg_server_claim_stray.go +++ b/x/storage/keeper/msg_server_claim_stray.go @@ -35,7 +35,7 @@ func (k msgServer) ClaimStray(goCtx context.Context, msg *types.MsgClaimStray) ( Signee: stray.Signee, Provider: msg.Creator, Startblock: fmt.Sprintf("%d", ctx.BlockHeight()), - Endblock: fmt.Sprintf("%d", ctx.BlockHeight()), + Endblock: "0", Filesize: stray.Filesize, Proofverified: "false", Blocktoprove: fmt.Sprintf("%d", ctx.BlockHeight()/1024), diff --git a/x/storage/keeper/msg_server_sign_contract.go b/x/storage/keeper/msg_server_sign_contract.go index 00ecdc2d7..d26f33c3b 100644 --- a/x/storage/keeper/msg_server_sign_contract.go +++ b/x/storage/keeper/msg_server_sign_contract.go @@ -41,7 +41,7 @@ func (k msgServer) SignContract(goCtx context.Context, msg *types.MsgSignContrac Signee: contract.Signee, Provider: contract.Creator, Startblock: fmt.Sprintf("%d", ctx.BlockHeight()), - Endblock: fmt.Sprintf("%d", ctx.BlockHeight()), + Endblock: "0", Filesize: contract.Filesize, Proofverified: "false", Blocktoprove: fmt.Sprintf("%d", pieceToStart), @@ -86,13 +86,16 @@ func (k msgServer) SignContract(goCtx context.Context, msg *types.MsgSignContrac for i := 0; i < 2; i++ { h := sha256.New() - _, err := io.WriteString(h, fmt.Sprintf("%s%s%d", contract.Creator, contract.Fid, i)) + _, err := io.WriteString(h, fmt.Sprintf("%s%d", contract.Cid, i)) if err != nil { return nil, err } hashName := h.Sum(nil) - scid := fmt.Sprintf("%x", hashName) + scid, err := MakeCid(hashName) + if err != nil { + return nil, err + } newContract := types.Strays{ Cid: scid, From fd6ab1dba8eeebb050d961a2ec261b501c4f8150 Mon Sep 17 00:00:00 2001 From: Marston Connell <34043723+TheMarstonConnell@users.noreply.github.com> Date: Tue, 6 Dec 2022 19:05:33 -0500 Subject: [PATCH 2/4] fixed test case --- x/storage/keeper/msg_server_contracts_test.go | 204 +++++++++++++++++- 1 file changed, 193 insertions(+), 11 deletions(-) diff --git a/x/storage/keeper/msg_server_contracts_test.go b/x/storage/keeper/msg_server_contracts_test.go index 961a571e4..b5967cbd7 100644 --- a/x/storage/keeper/msg_server_contracts_test.go +++ b/x/storage/keeper/msg_server_contracts_test.go @@ -1,9 +1,13 @@ package keeper_test import ( + "crypto/sha256" "encoding/json" + "fmt" + "io" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/jackalLabs/canine-chain/x/storage/keeper" "github.com/jackalLabs/canine-chain/x/storage/types" ) @@ -328,12 +332,38 @@ func (suite *KeeperTestSuite) TestCancelContract() { { name: "invalid_deal_owner", preRun: func() *types.MsgCancelContract { + dcid := "testownercid" + h := sha256.New() + _, err := io.WriteString(h, dcid) + suite.Require().NoError(err) + hashName := h.Sum(nil) + + dcid, err = keeper.MakeCid(hashName) + suite.Require().NoError(err) + d := types.ActiveDeals{ - Cid: "100", + Cid: dcid, Creator: user.String(), } sKeeper.SetActiveDeals(suite.ctx, d) - _, found := sKeeper.GetActiveDeals(suite.ctx, "100") + + for i := 0; i < 2; i++ { + h := sha256.New() + _, err := io.WriteString(h, fmt.Sprintf("%s%d", d.Cid, i)) + suite.Require().NoError(err) + hashName := h.Sum(nil) + + scid, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + k := types.ActiveDeals{ + Cid: scid, + Creator: user.String(), + } + sKeeper.SetActiveDeals(suite.ctx, k) + } + + _, found := sKeeper.GetActiveDeals(suite.ctx, d.Cid) suite.Require().True(found) return &types.MsgCancelContract{ Creator: "foo", @@ -347,7 +377,38 @@ func (suite *KeeperTestSuite) TestCancelContract() { { name: "fid_not_found", preRun: func() *types.MsgCancelContract { - d, found := sKeeper.GetActiveDeals(suite.ctx, "100") + dcid := "testownercid" + h := sha256.New() + _, err := io.WriteString(h, dcid) + suite.Require().NoError(err) + hashName := h.Sum(nil) + + dcid, err = keeper.MakeCid(hashName) + suite.Require().NoError(err) + + d := types.ActiveDeals{ + Cid: dcid, + Creator: user.String(), + } + sKeeper.SetActiveDeals(suite.ctx, d) + + for i := 0; i < 2; i++ { + h := sha256.New() + _, err := io.WriteString(h, fmt.Sprintf("%s%d", d.Cid, i)) + suite.Require().NoError(err) + hashName := h.Sum(nil) + + scid, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + k := types.ActiveDeals{ + Cid: scid, + Creator: user.String(), + } + sKeeper.SetActiveDeals(suite.ctx, k) + } + + d, found := sKeeper.GetActiveDeals(suite.ctx, d.Cid) suite.Require().True(found) d.Fid = "100" sKeeper.SetActiveDeals(suite.ctx, d) @@ -363,34 +424,155 @@ func (suite *KeeperTestSuite) TestCancelContract() { { name: "invalid_cid_json", preRun: func() *types.MsgCancelContract { + dcid := "testownercid" + h := sha256.New() + _, err := io.WriteString(h, dcid) + suite.Require().NoError(err) + hashName := h.Sum(nil) + + dcid, err = keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids := []string{dcid} + fmt.Println(dcid) + + d := types.ActiveDeals{ + Cid: dcid, + Creator: user.String(), + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetActiveDeals(suite.ctx, d) + + h = sha256.New() + _, err = io.WriteString(h, fmt.Sprintf("%s%d", dcid, 0)) + suite.Require().NoError(err) + hashName = h.Sum(nil) + + left, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids = append(cids, left) + k := types.ActiveDeals{ + Cid: left, + Creator: user.String(), + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetActiveDeals(suite.ctx, k) + + h = sha256.New() + _, err = io.WriteString(h, fmt.Sprintf("%s%d", dcid, 1)) + suite.Require().NoError(err) + hashName = h.Sum(nil) + + right, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids = append(cids, right) + k = types.ActiveDeals{ + Cid: right, + Creator: user.String(), + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetActiveDeals(suite.ctx, k) + + b, err := json.Marshal(cids) + suite.Require().NoError(err) + ftc := types.FidCid{ - Fid: "100", - Cids: "100", + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + Cids: string(b), } sKeeper.SetFidCid(suite.ctx, ftc) + + suite.Require().NoError(err) + + deals := sKeeper.GetAllActiveDeals(suite.ctx) + for _, v := range deals { + fmt.Println(v) + } + return &types.MsgCancelContract{ Creator: user.String(), - Cid: ftc.Cids, + Cid: right, } }, expErr: true, - expErrMsg: "cannot unmarshal number into Go value of type []string", + expErrMsg: "can't find contract", }, { name: "successfully_cancelled_contract", preRun: func() *types.MsgCancelContract { - ncids := []string{"abc", "def", "foo", "123_bar"} - b, err := json.Marshal(ncids) + dcid := "testownercid" + h := sha256.New() + _, err := io.WriteString(h, dcid) + suite.Require().NoError(err) + hashName := h.Sum(nil) + + dcid, err = keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids := []string{dcid} + fmt.Println(dcid) + + d := types.ActiveDeals{ + Cid: dcid, + Creator: user.String(), + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetActiveDeals(suite.ctx, d) + + h = sha256.New() + _, err = io.WriteString(h, fmt.Sprintf("%s%d", dcid, 0)) suite.Require().NoError(err) + hashName = h.Sum(nil) + + left, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids = append(cids, left) + k := types.ActiveDeals{ + Cid: left, + Creator: user.String(), + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetActiveDeals(suite.ctx, k) + + h = sha256.New() + _, err = io.WriteString(h, fmt.Sprintf("%s%d", dcid, 1)) + suite.Require().NoError(err) + hashName = h.Sum(nil) + + right, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids = append(cids, right) + k = types.ActiveDeals{ + Cid: right, + Creator: user.String(), + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetActiveDeals(suite.ctx, k) + + b, err := json.Marshal(cids) + suite.Require().NoError(err) + ftc := types.FidCid{ - Fid: "100", + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", Cids: string(b), } sKeeper.SetFidCid(suite.ctx, ftc) + + suite.Require().NoError(err) + + deals := sKeeper.GetAllActiveDeals(suite.ctx) + for _, v := range deals { + fmt.Println(v) + } + return &types.MsgCancelContract{ Creator: user.String(), - Cid: "100", + Cid: d.Cid, } }, expErr: false, From dc215e36a729adf6006e330fa8f6bf1c1757e612 Mon Sep 17 00:00:00 2001 From: Marston Connell <34043723+TheMarstonConnell@users.noreply.github.com> Date: Tue, 6 Dec 2022 19:08:03 -0500 Subject: [PATCH 3/4] lint --- x/storage/keeper/msg_server_cancel_contract.go | 6 +++--- x/storage/keeper/msg_server_contracts_test.go | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/x/storage/keeper/msg_server_cancel_contract.go b/x/storage/keeper/msg_server_cancel_contract.go index c1b820181..81f823f33 100644 --- a/x/storage/keeper/msg_server_cancel_contract.go +++ b/x/storage/keeper/msg_server_cancel_contract.go @@ -70,10 +70,10 @@ func (k msgServer) CancelContract(goCtx context.Context, msg *types.MsgCancelCon s, found := k.GetStrays(ctx, right) if !found { return nil, fmt.Errorf("can't find contract") - } else { - fid = s.Fid - k.RemoveStrays(ctx, s.Cid) } + fid = s.Fid + k.RemoveStrays(ctx, s.Cid) + } else { fid = d.Fid k.RemoveActiveDeals(ctx, d.Cid) diff --git a/x/storage/keeper/msg_server_contracts_test.go b/x/storage/keeper/msg_server_contracts_test.go index b5967cbd7..08c9f583a 100644 --- a/x/storage/keeper/msg_server_contracts_test.go +++ b/x/storage/keeper/msg_server_contracts_test.go @@ -11,6 +11,8 @@ import ( "github.com/jackalLabs/canine-chain/x/storage/types" ) +const tst = "testownercid" + func (suite *KeeperTestSuite) TestPostContracts() { suite.SetupSuite() msgSrvr, sKeeper, goCtx := setupMsgServer(suite) @@ -332,7 +334,7 @@ func (suite *KeeperTestSuite) TestCancelContract() { { name: "invalid_deal_owner", preRun: func() *types.MsgCancelContract { - dcid := "testownercid" + dcid := tst h := sha256.New() _, err := io.WriteString(h, dcid) suite.Require().NoError(err) @@ -377,7 +379,7 @@ func (suite *KeeperTestSuite) TestCancelContract() { { name: "fid_not_found", preRun: func() *types.MsgCancelContract { - dcid := "testownercid" + dcid := tst h := sha256.New() _, err := io.WriteString(h, dcid) suite.Require().NoError(err) @@ -424,7 +426,7 @@ func (suite *KeeperTestSuite) TestCancelContract() { { name: "invalid_cid_json", preRun: func() *types.MsgCancelContract { - dcid := "testownercid" + dcid := tst h := sha256.New() _, err := io.WriteString(h, dcid) suite.Require().NoError(err) @@ -503,7 +505,7 @@ func (suite *KeeperTestSuite) TestCancelContract() { { name: "successfully_cancelled_contract", preRun: func() *types.MsgCancelContract { - dcid := "testownercid" + dcid := tst h := sha256.New() _, err := io.WriteString(h, dcid) suite.Require().NoError(err) From 20b0124baa5f671ee372f7e9b6da2fc8f2dbdeea Mon Sep 17 00:00:00 2001 From: Marston Connell <34043723+TheMarstonConnell@users.noreply.github.com> Date: Tue, 6 Dec 2022 19:22:20 -0500 Subject: [PATCH 4/4] cancel contract of strays --- .../keeper/msg_server_cancel_contract.go | 7 +- x/storage/keeper/msg_server_contracts_test.go | 75 +++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/x/storage/keeper/msg_server_cancel_contract.go b/x/storage/keeper/msg_server_cancel_contract.go index 81f823f33..6b0dc2862 100644 --- a/x/storage/keeper/msg_server_cancel_contract.go +++ b/x/storage/keeper/msg_server_cancel_contract.go @@ -77,10 +77,9 @@ func (k msgServer) CancelContract(goCtx context.Context, msg *types.MsgCancelCon } else { fid = d.Fid k.RemoveActiveDeals(ctx, d.Cid) - } - - if d.Creator != msg.Creator { - return nil, fmt.Errorf("you don't own this deal") + if d.Creator != msg.Creator { + return nil, fmt.Errorf("you don't own this deal") + } } ftc, found := k.GetFidCid(ctx, fid) diff --git a/x/storage/keeper/msg_server_contracts_test.go b/x/storage/keeper/msg_server_contracts_test.go index 08c9f583a..b6eb72980 100644 --- a/x/storage/keeper/msg_server_contracts_test.go +++ b/x/storage/keeper/msg_server_contracts_test.go @@ -579,6 +579,81 @@ func (suite *KeeperTestSuite) TestCancelContract() { }, expErr: false, }, + + { + name: "successfully_cancelled_contract_with_strays", + preRun: func() *types.MsgCancelContract { + dcid := tst + h := sha256.New() + _, err := io.WriteString(h, dcid) + suite.Require().NoError(err) + hashName := h.Sum(nil) + + dcid, err = keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids := []string{dcid} + fmt.Println(dcid) + + d := types.Strays{ + Cid: dcid, + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetStrays(suite.ctx, d) + + h = sha256.New() + _, err = io.WriteString(h, fmt.Sprintf("%s%d", dcid, 0)) + suite.Require().NoError(err) + hashName = h.Sum(nil) + + left, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids = append(cids, left) + k := types.Strays{ + Cid: left, + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetStrays(suite.ctx, k) + + h = sha256.New() + _, err = io.WriteString(h, fmt.Sprintf("%s%d", dcid, 1)) + suite.Require().NoError(err) + hashName = h.Sum(nil) + + right, err := keeper.MakeCid(hashName) + suite.Require().NoError(err) + + cids = append(cids, right) + k = types.Strays{ + Cid: right, + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + } + sKeeper.SetStrays(suite.ctx, k) + + b, err := json.Marshal(cids) + suite.Require().NoError(err) + + ftc := types.FidCid{ + Fid: "jklf1j3p63s42w7ywaczlju626st55mzu5z39w2rx9x", + Cids: string(b), + } + sKeeper.SetFidCid(suite.ctx, ftc) + + suite.Require().NoError(err) + + deals := sKeeper.GetAllStrays(suite.ctx) + for _, v := range deals { + fmt.Println(v) + } + + return &types.MsgCancelContract{ + Creator: user.String(), + Cid: d.Cid, + } + }, + expErr: false, + }, } for _, tc := range cases {