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

feat(ecocredit): add tx origin duplication check to CreateBatch and MintBatchCredits #1112

Merged
merged 9 commits into from
May 23, 2022
Prev Previous commit
Next Next commit
feat: add origin tx check to create batch
  • Loading branch information
aleem1314 committed May 19, 2022
commit 75c04aee8398fba6381769d86cfa837fd1e792e1
23 changes: 23 additions & 0 deletions x/ecocredit/server/core/create_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/math"
Expand Down Expand Up @@ -40,6 +41,17 @@ func (k Keeper) CreateBatch(ctx context.Context, req *core.MsgCreateBatch) (*cor
return nil, err
}

if req.OriginTx != nil {
originTx, err := k.stateStore.BatchOrigTxTable().Get(ctx, req.OriginTx.Id)
if err != nil && !ormerrors.IsNotFound(err) {
return nil, err
}

if originTx != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("credits already issued with tx id: %s", req.OriginTx.Id)
}
}

aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
batchSeqNo, err := k.getBatchSeqNo(ctx, projectInfo.Key)
if err != nil {
return nil, err
Expand Down Expand Up @@ -132,6 +144,17 @@ func (k Keeper) CreateBatch(ctx context.Context, req *core.MsgCreateBatch) (*cor
return nil, err
}

if req.OriginTx != nil {
if err = k.stateStore.BatchOrigTxTable().Insert(ctx, &api.BatchOrigTx{
TxId: req.OriginTx.Id,
Typ: req.OriginTx.Typ,
Note: req.Note,
BatchDenom: batchDenom,
}); err != nil {
return nil, err
}
}

if err = sdkCtx.EventManager().EmitTypedEvent(&core.EventCreateBatch{
BatchDenom: batchDenom,
}); err != nil {
Expand Down
82 changes: 82 additions & 0 deletions x/ecocredit/server/core/create_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,88 @@ func TestCreateBatch_ProjectNotFound(t *testing.T) {
assert.ErrorContains(t, err, "not found")
}

func TestCreateBatch_WithOriginTx_Valid(t *testing.T) {
t.Parallel()
s := setupBase(t)
batchTestSetup(t, s.ctx, s.stateStore, s.addr)
_, _, addr2 := testdata.KeyTestPubAddr()

blockTime, err := time.Parse("2006-01-02", "2049-01-30")
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved
assert.NilError(t, err)
s.sdkCtx = s.sdkCtx.WithBlockTime(blockTime)
s.ctx = types.WrapSDKContext(s.sdkCtx)

start, end := time.Now(), time.Now()
_, err = s.k.CreateBatch(s.ctx, &core.MsgCreateBatch{
Issuer: s.addr.String(),
ProjectId: "C01-001",
Issuance: []*core.BatchIssuance{
{
Recipient: s.addr.String(),
TradableAmount: "10",
RetiredAmount: "5.3",
},
{
Recipient: addr2.String(),
TradableAmount: "2.4",
RetiredAmount: "3.4",
},
},
Metadata: "",
StartDate: &start,
EndDate: &end,
OriginTx: &core.OriginTx{
Typ: "Ethereum",
Id: "210985091248",
},
})
assert.NilError(t, err)
}

func TestCreateBatch_WithOriginTx_InValid(t *testing.T) {
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
s := setupBase(t)
batchTestSetup(t, s.ctx, s.stateStore, s.addr)
_, _, addr2 := testdata.KeyTestPubAddr()

blockTime, err := time.Parse("2006-01-02", "2049-01-30")
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved
assert.NilError(t, err)
s.sdkCtx = s.sdkCtx.WithBlockTime(blockTime)
s.ctx = types.WrapSDKContext(s.sdkCtx)

start, end := time.Now(), time.Now()
batch := &core.MsgCreateBatch{
Issuer: s.addr.String(),
ProjectId: "C01-001",
Issuance: []*core.BatchIssuance{
{
Recipient: s.addr.String(),
TradableAmount: "10",
RetiredAmount: "5.3",
},
{
Recipient: addr2.String(),
TradableAmount: "2.4",
RetiredAmount: "3.4",
},
},
Metadata: "",
StartDate: &start,
EndDate: &end,
OriginTx: &core.OriginTx{
Typ: "Ethereum",
Id: "210985091248",
},
}

_, err = s.k.CreateBatch(s.ctx, batch)
assert.NilError(t, err)

// create credit batch with same tx origin id
_, err = s.k.CreateBatch(s.ctx, batch)
assert.ErrorContains(t, err, "credits already issued")
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
}

// creates a class "C01", with a single class issuer, and a project "C01-001"
func batchTestSetup(t *testing.T, ctx context.Context, ss api.StateStore, addr types.AccAddress) (classId, projectId string) {
classId, projectId = "C01", "C01-001"
Expand Down