diff --git a/token/services/tokens/tokens.go b/token/services/tokens/tokens.go index f758f8bf8..aee3615d9 100644 --- a/token/services/tokens/tokens.go +++ b/token/services/tokens/tokens.go @@ -358,9 +358,9 @@ func (t *Tokens) extractActions(tmsID token.TMSID, txID string, request *token.R if err != nil { return nil, nil, errors.WithMessagef(err, "failed to get request's outputs") } - toSpend, toAppend := t.parse(auth, txID, md, is, os, auditorFlag, precision, graphHiding) + toSpend, toAppend, err := t.parse(auth, txID, md, is, os, auditorFlag, precision, graphHiding) logger.Debugf("transaction [%s] parsed [%d] inputs and [%d] outputs", txID, len(toSpend), len(toAppend)) - return toSpend, toAppend, nil + return toSpend, toAppend, err } // parse returns the tokens to store and spend as the result of a transaction @@ -373,7 +373,7 @@ func (t *Tokens) parse( auditorFlag bool, precision uint64, graphHiding bool, -) (toSpend []*token2.ID, toAppend []TokenToAppend) { +) (toSpend []*token2.ID, toAppend []TokenToAppend, err error) { if graphHiding { ids := md.SpentTokenID() logger.Debugf("transaction [%s] with graph hiding, delete inputs [%v]", txID, ids) @@ -394,13 +394,14 @@ func (t *Tokens) parse( // parse the outputs for _, output := range os.Outputs() { - // get token in the clear - if len(output.LedgerOutput) == 0 { - logger.Debugf("transaction [%s] without graph hiding, delete input [%d]", txID, output.Index) - toSpend = append(toSpend, &token2.ID{TxId: txID, Index: output.Index}) + + // if this is a redeem, then skip + if len(output.Token.Owner) == 0 { + logger.Debugf("output [%s:%d] is a redeem", txID, output.Index) continue } + // process the output to identify the relations with the current TMS issuerFlag := !output.Issuer.IsNone() && auth.Issued(output.Issuer, &output.Token) ownerWalletID, ids, mine := auth.IsMine(&output.Token) if logger.IsEnabledFor(zapcore.DebugLevel) { @@ -421,8 +422,7 @@ func (t *Tokens) parse( ownerType, ownerIdentity, err := auth.OwnerType(output.Token.Owner) if err != nil { - logger.Errorf("could not unmarshal identity when storing token: %s", err.Error()) - continue + return nil, nil, errors.Wrapf(err, "failed to extract owner type for token [%s:%d]", txID, output.Index) } tta := TokenToAppend{ diff --git a/token/services/tokens/tokens_test.go b/token/services/tokens/tokens_test.go index 458425d7a..6f9164154 100644 --- a/token/services/tokens/tokens_test.go +++ b/token/services/tokens/tokens_test.go @@ -63,7 +63,8 @@ func TestParse(t *testing.T) { } output1 := &token.Output{ Token: token2.Token{ - Type: "TOK", + Type: "TOK", + Owner: []byte("alice"), }, ActionIndex: 0, Index: 0, @@ -75,7 +76,8 @@ func TestParse(t *testing.T) { is := token.NewInputStream(qsMock{}, []*token.Input{input1}, 64) os := token.NewOutputStream([]*token.Output{output1}, 64) - spend, store := tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false) + spend, store, err := tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false) + assert.NoError(t, err) assert.Len(t, spend, 1) assert.Equal(t, "in", spend[0].TxId) @@ -91,11 +93,12 @@ func TestParse(t *testing.T) { assert.Equal(t, uint64(64), store[0].precision) assert.Equal(t, output1.Type, store[0].tok.Type) - // no ledger output -> spend - output1.LedgerOutput = []byte{} + // no owner, then a redeemed token + output1.Token.Owner = []byte{} os = token.NewOutputStream([]*token.Output{output1}, 64) - spend, store = tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false) - assert.Len(t, spend, 2) + spend, store, err = tokens.parse(&authMock{}, "tx1", md, is, os, false, 64, false) + assert.NoError(t, err) + assert.Len(t, spend, 1) assert.Len(t, store, 0) // transfer with several inputs and outputs @@ -121,7 +124,8 @@ func TestParse(t *testing.T) { } output1 = &token.Output{ Token: token2.Token{ - Type: "TOK", + Type: "TOK", + Owner: []byte("alice"), }, ActionIndex: 0, Index: 0, @@ -132,7 +136,8 @@ func TestParse(t *testing.T) { } output2 := &token.Output{ Token: token2.Token{ - Type: "TOK", + Type: "TOK", + Owner: []byte("bob"), }, ActionIndex: 0, Index: 1, @@ -144,7 +149,8 @@ func TestParse(t *testing.T) { is = token.NewInputStream(qsMock{}, []*token.Input{input1, input2}, 64) os = token.NewOutputStream([]*token.Output{output1, output2}, 64) - spend, store = tokens.parse(&authMock{}, "tx2", md, is, os, false, 64, false) + spend, store, err = tokens.parse(&authMock{}, "tx2", md, is, os, false, 64, false) + assert.NoError(t, err) assert.Len(t, spend, 2) assert.Equal(t, "in1", spend[0].TxId) assert.Equal(t, uint64(1), spend[0].Index)