Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
feat: Support delete document
Browse files Browse the repository at this point in the history
Update to latest sidetree-core and create test for delete document.

Closes #130

Signed-off-by: Sandra Vrtikapa <[email protected]>
  • Loading branch information
sandrask committed Feb 21, 2020
1 parent 5679b58 commit 562bb78
Show file tree
Hide file tree
Showing 21 changed files with 276 additions and 189 deletions.
55 changes: 52 additions & 3 deletions cmd/chaincode/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"encoding/json"
"fmt"
"runtime/debug"
"sort"

"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
ccapi "github.com/hyperledger/fabric/extensions/chaincode/api"
"github.com/trustbloc/sidetree-fabric/cmd/chaincode/cas"
)

var logger = NewLogger("doc")
var logger = flogging.MustGetLogger("doc")

const (
ccVersion = "v1"
Expand Down Expand Up @@ -191,9 +193,15 @@ func (cc *DocumentCC) queryByID(stub shim.ChaincodeStubInterface, args [][]byte)
}
}

// TODO: sort documents by blockchain time (block number, tx number within block, operation index within batch)
// sort documents by blockchain time (block number, tx number within block)
sorted, err := sortChronologically(operations)
if err != nil {
errMsg := fmt.Sprintf("failed to sort operations chronologically for id[%s]: %s", ID, err.Error())
logger.Errorf("[txID %s] %s", txID, errMsg)
return shim.Error(errMsg)
}

payload, err := json.Marshal(operations)
payload, err := json.Marshal(sorted)
if err != nil {
errMsg := fmt.Sprintf("failed to marshal documents: %s", err.Error())
logger.Errorf("[txID %s] %s", txID, errMsg)
Expand All @@ -203,6 +211,47 @@ func (cc *DocumentCC) queryByID(stub shim.ChaincodeStubInterface, args [][]byte)
return shim.Success(payload)
}

func sortChronologically(operations [][]byte) ([][]byte, error) {
if len(operations) <= 1 {
return operations, nil
}

ops := make([]*operation, len(operations))
for index, bytes := range operations {
op := &operation{}
err := json.Unmarshal(bytes, op)
if err != nil {
return nil, err
}
op.Index = index
ops[index] = op
}

sort.Slice(ops, func(i, j int) bool {
if ops[i].TransactionTime == ops[j].TransactionTime {
return ops[i].TransactionNumber < ops[j].TransactionNumber
}
return ops[i].TransactionTime < ops[j].TransactionTime
})

sorted := make([][]byte, len(operations))
for i, o := range ops {
sorted[i] = operations[o.Index]
}

return sorted, nil
}

type operation struct {
Index int

// The logical blockchain time that this operation was anchored on the blockchain - corresponds to block number
TransactionTime uint64 `json:"transactionTime"`

// The transaction number of the transaction this operation was batched within - corresponds to tx number within block
TransactionNumber uint64 `json:"transactionNumber"`
}

func (m funcMap) String() string {
str := ""
i := 0
Expand Down
77 changes: 70 additions & 7 deletions cmd/chaincode/doc/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package doc
import (
"crypto"
"encoding/base64"
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -183,6 +184,66 @@ func TestQueryError(t *testing.T) {
assert.Contains(t, err.Error(), testErr.Error())
}

func TestQuery_SortError(t *testing.T) {
stub := prepareStub()

testPayload := getOperationBytes(getCreateOperation())

address, err := invoke(stub, [][]byte{[]byte(write), testPayload})
assert.Nil(t, err)
assert.NotNil(t, address)

address, err = invoke(stub, [][]byte{[]byte(write), []byte("invalid json")})
assert.Nil(t, err)
assert.NotNil(t, address)

payload, err := invoke(stub, [][]byte{[]byte(queryByID), []byte(testID)})
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "unexpected end of JSON input")
assert.Nil(t, payload)
}

func TestSort(t *testing.T) {
var operations [][]byte

delete := &testOperation{ID: testID, Type: "delete", TransactionTime: 2, TransactionNumber: 1}
update := &testOperation{ID: testID, Type: "update", TransactionTime: 1, TransactionNumber: 7}
create := &testOperation{ID: testID, Type: "create", TransactionTime: 1, TransactionNumber: 1}

operations = append(operations, getOperationBytes(delete))
operations = append(operations, getOperationBytes(update))
operations = append(operations, getOperationBytes(create))

result, err := sortChronologically(operations)
require.NoError(t, err)

var first testOperation
err = json.Unmarshal(result[0], &first)
require.NoError(t, err)
require.Equal(t, create.Type, first.Type)

var second testOperation
err = json.Unmarshal(result[1], &second)
require.NoError(t, err)
require.Equal(t, update.Type, second.Type)

var third testOperation
err = json.Unmarshal(result[2], &third)
require.NoError(t, err)
require.Equal(t, delete.Type, third.Type)
}

func TestSortError(t *testing.T) {
var operations [][]byte
operations = append(operations, []byte("invalid json"))
operations = append(operations, []byte("invalid json"))

result, err := sortChronologically(operations)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid character")
require.Nil(t, result)
}

func TestWarmup(t *testing.T) {

stub := prepareStub()
Expand Down Expand Up @@ -254,7 +315,7 @@ func invoke(stub *mocks.MockStub, args [][]byte) ([]byte, error) {
return res.Payload, nil
}

func getOperationBytes(op *Operation) []byte {
func getOperationBytes(op *testOperation) []byte {

bytes, err := docutil.MarshalCanonical(op)
if err != nil {
Expand All @@ -264,12 +325,14 @@ func getOperationBytes(op *Operation) []byte {
return bytes
}

func getCreateOperation() *Operation {
return &Operation{ID: testID, Type: "create"}
func getCreateOperation() *testOperation {
return &testOperation{ID: testID, Type: "create", TransactionTime: 1, TransactionNumber: 1}
}

// Operation defines sample operation
type Operation struct {
Type string `json:"type"`
ID string `json:"id"`
// testOperation defines sample operation with smaallest subset of information
type testOperation struct {
Type string `json:"type"`
ID string `json:"id"`
TransactionTime uint64 `json:"transactionTime"`
TransactionNumber uint64 `json:"transactionNumber"`
}
48 changes: 0 additions & 48 deletions cmd/chaincode/doc/logger.go

This file was deleted.

20 changes: 0 additions & 20 deletions cmd/chaincode/doc/logger_test.go

This file was deleted.

48 changes: 0 additions & 48 deletions cmd/chaincode/txn/logger.go

This file was deleted.

20 changes: 0 additions & 20 deletions cmd/chaincode/txn/logger_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/chaincode/txn/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
ccapi "github.com/hyperledger/fabric/extensions/chaincode/api"
"github.com/trustbloc/sidetree-fabric/cmd/chaincode/cas"
)

var logger = NewLogger("sidetreetxncc")
var logger = flogging.MustGetLogger("sidetreetxncc")

const (
ccVersion = "v1"
Expand Down
4 changes: 2 additions & 2 deletions cmd/peer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c/go.mod h1:ot55KksVO/lnRDVVNDF14AtdS7CSs//NE20WU6x/eiw=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81 h1:iant8lATTlHYUWVdL3llKZuGgZUHBL4q7JUkLmcBQXk=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200214144924-3e7aa7825416 h1:t9FawNEHc0R1PTYdDhWd/Q643KAN9IpJZMdlu2FlBxM=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200214144924-3e7aa7825416/go.mod h1:qJ7oOPveEqrxTsO4KJsSHUM/Yivym221Dvd+IUq1V1U=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200220212110-5676c1827fc3 h1:3T1YFMcoLJl0r7c945BF7MFm9MDMCase+JEBZV/4pcU=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200220212110-5676c1827fc3/go.mod h1:qJ7oOPveEqrxTsO4KJsSHUM/Yivym221Dvd+IUq1V1U=
github.com/ugorji/go v1.1.1/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/spf13/viper2015 v1.3.2
github.com/stretchr/testify v1.4.0
github.com/trustbloc/fabric-peer-ext v0.0.0
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200214144924-3e7aa7825416
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200220212110-5676c1827fc3
)

replace github.com/hyperledger/fabric => github.com/trustbloc/fabric-mod v0.1.2-0.20200211211900-62c249e072e2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c/go.mod h1:ot55KksVO/lnRDVVNDF14AtdS7CSs//NE20WU6x/eiw=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81 h1:iant8lATTlHYUWVdL3llKZuGgZUHBL4q7JUkLmcBQXk=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200214144924-3e7aa7825416 h1:t9FawNEHc0R1PTYdDhWd/Q643KAN9IpJZMdlu2FlBxM=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200214144924-3e7aa7825416/go.mod h1:qJ7oOPveEqrxTsO4KJsSHUM/Yivym221Dvd+IUq1V1U=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200220212110-5676c1827fc3 h1:3T1YFMcoLJl0r7c945BF7MFm9MDMCase+JEBZV/4pcU=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200220212110-5676c1827fc3/go.mod h1:qJ7oOPveEqrxTsO4KJsSHUM/Yivym221Dvd+IUq1V1U=
github.com/ugorji/go v1.1.1/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
Expand Down
Loading

0 comments on commit 562bb78

Please sign in to comment.