Skip to content

Commit

Permalink
modernize commonCC.go
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed Jan 25, 2024
1 parent 1d778a0 commit 0d414bd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
10 changes: 6 additions & 4 deletions commonCC.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
)

type CommonChaincode struct {
Name string
Channel string
CCAPI shim.ChaincodeStubInterface // chaincode API
Name string
TxID string
ChannelID string
CCAPI shim.ChaincodeStubInterface // chaincode API
}

func (cc *CommonChaincode) Prepare(ccAPI shim.ChaincodeStubInterface) {
cc.CCAPI = ccAPI
cc.Channel = ccAPI.GetChannelID()
cc.ChannelID = ccAPI.GetChannelID()
cc.TxID = ccAPI.GetTxID()
}

// GetChaincodeID return empty for if no record.
Expand Down
55 changes: 55 additions & 0 deletions contract-api/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package contract_api

import (
"github.com/hyperledger/fabric-chaincode-go/pkg/cid"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
"github.com/hyperledger/fabric-contract-api-go/metadata"
)

type ExtendedContract contractapi.Contract

func (c *ExtendedContract) GetIgnoredFunctions() []string {
return []string{
"GetClientIdentity", "GetStub",
}
}
func (c *ExtendedContract) GetStub() shim.ChaincodeStubInterface {
var context = c.TransactionContextHandler.(*contractapi.TransactionContext)
return context.GetStub()
}
func (c *ExtendedContract) GetClientIdentity() cid.ClientIdentity {
var context = c.TransactionContextHandler.(*contractapi.TransactionContext)
return context.GetClientIdentity()
}

func (c *ExtendedContract) GetInfo() metadata.InfoMetadata {
return c.Info
}

func (c *ExtendedContract) GetUnknownTransaction() interface{} {
return c.UnknownTransaction
}

func (c *ExtendedContract) GetBeforeTransaction() interface{} {
return c.BeforeTransaction
}

func (c *ExtendedContract) GetAfterTransaction() interface{} {
return c.AfterTransaction
}

// GetName returns the name of the contract
func (c *ExtendedContract) GetName() string {
return c.Name
}

// GetTransactionContextHandler returns the current transaction context set for
// the contract. If none has been set then TransactionContext will be returned
func (c *ExtendedContract) GetTransactionContextHandler() contractapi.SettableTransactionContextInterface {
if c.TransactionContextHandler == nil {
return new(contractapi.TransactionContext)
}

return c.TransactionContextHandler
}
18 changes: 18 additions & 0 deletions contract-api/contract_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package contract_api

import (
"testing"
)

type StupidContract struct {
ExtendedContract
}

func (c *StupidContract) Ping() {

}
func TestBuild(t *testing.T) {

Start(NewChaincode(&StupidContract{}))

}
4 changes: 2 additions & 2 deletions contract-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.18

require (
github.com/davidkhala/goutils v1.6.0
github.com/hyperledger/fabric-chaincode-go v0.0.0-20230731094759-d626e9ab09b9
github.com/hyperledger/fabric-contract-api-go v1.2.2
github.com/stretchr/testify v1.8.4
)

require (
Expand All @@ -17,14 +19,12 @@ require (
github.com/gobuffalo/packd v1.0.2 // indirect
github.com/gobuffalo/packr v1.30.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hyperledger/fabric-chaincode-go v0.0.0-20230731094759-d626e9ab09b9 // indirect
github.com/hyperledger/fabric-protos-go v0.3.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down
15 changes: 13 additions & 2 deletions contract-api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ import (
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)

func Start(chaincode *contractapi.ContractChaincode) {
var err = chaincode.Start()
switch err.Error() {
case "'CORE_CHAINCODE_ID_NAME' must be set":
println("[dev mode]", err.Error())
break
default:
goutils.PanicError(err)
}

}
func NewChaincode(contracts ...contractapi.ContractInterface) *contractapi.ContractChaincode {
cc, err := contractapi.NewChaincode(contracts...)
chaincode, err := contractapi.NewChaincode(contracts...)
goutils.PanicError(err)
return cc
return chaincode
}

0 comments on commit 0d414bd

Please sign in to comment.