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

noto: add check for duplicate states #174

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 domains/noto/internal/msgs/en_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ var (
MsgSignatureDoesNotMatch = ffe("PD200017", "Signature for '%s' did not match: expected=%s actual=%s")
MsgStateWrongOwner = ffe("PD200018", "State '%s' is not owned by '%s'")
MsgUnrecognizedEndorsement = ffe("PD200019", "Unrecognized endorsement request: %s")
MsgDuplicateStateInList = ffe("PD200020", "Duplicate state in list %s[%d] (%s)")
)
8 changes: 8 additions & 0 deletions domains/noto/internal/noto/e2e_noto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ func TestNoto(t *testing.T) {
assert.ErrorContains(t, rpcerr.Error(), "PD200009")
assert.True(t, boolResult)

coins, err = noto.FindCoins(ctx, "{}")
require.NoError(t, err)
require.Len(t, coins, 1)

log.L(ctx).Infof("Transfer 150 from notary (should fail)")
rpcerr = rpc.CallRPC(ctx, &boolResult, "testbed_invoke", &tktypes.PrivateContractInvoke{
From: notaryName,
Expand All @@ -203,6 +207,10 @@ func TestNoto(t *testing.T) {
require.NotNil(t, rpcerr)
assert.ErrorContains(t, rpcerr.Error(), "PD200005")

coins, err = noto.FindCoins(ctx, "{}")
require.NoError(t, err)
require.Len(t, coins, 1)

log.L(ctx).Infof("Transfer 50 from notary to recipient1")
rpcerr = rpc.CallRPC(ctx, &boolResult, "testbed_invoke", &tktypes.PrivateContractInvoke{
From: notaryName,
Expand Down
19 changes: 12 additions & 7 deletions domains/noto/internal/noto/noto.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,24 @@ func (n *Noto) recoverSignature(ctx context.Context, payload ethtypes.HexBytes0x

func (n *Noto) parseCoinList(ctx context.Context, label string, states []*pb.EndorsableState) ([]*types.NotoCoin, []*pb.StateRef, *big.Int, error) {
var err error
statesUsed := make(map[string]bool)
coins := make([]*types.NotoCoin, len(states))
refs := make([]*pb.StateRef, len(states))
total := big.NewInt(0)
for i, input := range states {
if input.SchemaId != n.coinSchema.Id {
return nil, nil, nil, i18n.NewError(ctx, msgs.MsgUnknownSchema, input.SchemaId)
for i, state := range states {
if state.SchemaId != n.coinSchema.Id {
return nil, nil, nil, i18n.NewError(ctx, msgs.MsgUnknownSchema, state.SchemaId)
}
if statesUsed[state.Id] {
return nil, nil, nil, i18n.NewError(ctx, msgs.MsgDuplicateStateInList, label, i, state.Id)
}
if coins[i], err = n.unmarshalCoin(input.StateDataJson); err != nil {
return nil, nil, nil, i18n.NewError(ctx, msgs.MsgInvalidListInput, label, i, input.Id, err)
statesUsed[state.Id] = true
if coins[i], err = n.unmarshalCoin(state.StateDataJson); err != nil {
return nil, nil, nil, i18n.NewError(ctx, msgs.MsgInvalidListInput, label, i, state.Id, err)
}
refs[i] = &pb.StateRef{
SchemaId: input.SchemaId,
Id: input.Id,
SchemaId: state.SchemaId,
Id: state.Id,
}
total = total.Add(total, coins[i].Amount.BigInt())
}
Expand Down
1 change: 1 addition & 0 deletions domains/noto/internal/noto/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (n *Noto) prepareInputs(ctx context.Context, owner ethtypes.Address0xHex, a
stateRefs := []*pb.StateRef{}
coins := []*types.NotoCoin{}
for {
// TODO: make this configurable
queryBuilder := query.NewQueryBuilder().
Limit(10).
Sort(".created").
Expand Down