Skip to content

Commit

Permalink
Add auto deployed contracts to stackState.json
Browse files Browse the repository at this point in the history
Signed-off-by: Nicko Guyer <[email protected]>
  • Loading branch information
nguyer committed Apr 26, 2022
1 parent 54ef823 commit fdc6c78
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 171 deletions.
4 changes: 2 additions & 2 deletions cmd/deploy_ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ solc --combined-json abi,bin contract.sol > contract.json
return err
}
}
contractAddress, err := stackManager.DeployContract(filename, selectedContractName, 0, args[2:])
location, err := stackManager.DeployContract(filename, selectedContractName, 0, args[2:])
if err != nil {
return err
}
fmt.Print(contractAddress)
fmt.Print(location)
return nil
},
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ This command will start a stack and run it in the background.
if spin != nil {
spin.Start()
}
if err := stackManager.StartStack(verbose, &startOptions); err != nil {
messages, err := stackManager.StartStack(verbose, &startOptions)
if err != nil {
return err
}
if spin != nil {
spin.Stop()
}
fmt.Print("\n\n")
for _, message := range messages {
fmt.Printf("%s\n\n", message)
}
for _, member := range stackManager.Stack.Members {
fmt.Printf("Web UI for member '%v': http://127.0.0.1:%v/ui\n", member.ID, member.ExposedFireflyPort)
if stackManager.Stack.SandboxEnabled {
Expand Down
4 changes: 2 additions & 2 deletions internal/blockchain/blockchain_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
type IBlockchainProvider interface {
WriteConfig(options *types.InitOptions) error
FirstTimeSetup() error
DeployFireFlyContract() (*core.BlockchainConfig, error)
DeployFireFlyContract() (*core.BlockchainConfig, *types.ContractDeploymentResult, error)
PreStart() error
PostStart() error
GetDockerServiceDefinitions() []*docker.ServiceDefinition
GetFireflyConfig(stack *types.Stack, member *types.Member) (blockchainConfig *core.BlockchainConfig, coreConfig *core.OrgConfig)
Reset() error
GetContracts(filename string, extraArgs []string) ([]string, error)
DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (interface{}, error)
DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (*types.ContractDeploymentResult, error)
CreateAccount(args []string) (interface{}, error)
ParseAccount(interface{}) interface{}
}
16 changes: 11 additions & 5 deletions internal/blockchain/ethereum/besu/besu_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (p *BesuProvider) PostStart() error {
return nil
}

func (p *BesuProvider) DeployFireFlyContract() (*core.BlockchainConfig, error) {
func (p *BesuProvider) DeployFireFlyContract() (*core.BlockchainConfig, *types.ContractDeploymentResult, error) {
return ethconnect.DeployFireFlyContract(p.Stack, p.Log, p.Verbose)
}

Expand Down Expand Up @@ -236,14 +236,20 @@ func (p *BesuProvider) GetContracts(filename string, extraArgs []string) ([]stri
return contractNames, err
}

func (p *BesuProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (interface{}, error) {
func (p *BesuProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (*types.ContractDeploymentResult, error) {
contractAddres, err := ethconnect.DeployCustomContract(member, filename, contractName)
if err != nil {
return nil, err
}
return map[string]string{
"address": contractAddres,
}, nil
result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: contractName,
Location: map[string]string{
"address": contractAddres,
},
},
}
return result, nil
}

func (p *BesuProvider) CreateAccount(args []string) (interface{}, error) {
Expand Down
25 changes: 16 additions & 9 deletions internal/blockchain/ethereum/ethconnect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func DeprecatedRegisterContract(member *types.Member, contract *ethereum.Compile
return nil
}

func DeployFireFlyContract(s *types.Stack, log log.Logger, verbose bool) (*core.BlockchainConfig, error) {
func DeployFireFlyContract(s *types.Stack, log log.Logger, verbose bool) (*core.BlockchainConfig, *types.ContractDeploymentResult, error) {
var containerName string
var firstNonExternalMember *types.Member
for _, member := range s.Members {
Expand All @@ -293,40 +293,47 @@ func DeployFireFlyContract(s *types.Stack, log log.Logger, verbose bool) (*core.
}
}
if containerName == "" {
return nil, errors.New("unable to extract contracts from container - no valid firefly core containers found in stack")
return nil, nil, errors.New("unable to extract contracts from container - no valid firefly core containers found in stack")
}
log.Info("extracting smart contracts")

if err := ethereum.ExtractContracts(containerName, "/firefly/contracts", s.RuntimeDir, verbose); err != nil {
return nil, err
return nil, nil, err
}

var fireflyContract *ethereum.CompiledContract
contracts, err := ethereum.ReadContractJSON(filepath.Join(s.RuntimeDir, "contracts", "Firefly.json"))
if err != nil {
return nil, err
return nil, nil, err
}

fireflyContract, ok := contracts.Contracts["Firefly.sol:Firefly"]
if !ok {
fireflyContract, ok = contracts.Contracts["Firefly"]
fireflyContract, ok = contracts.Contracts["FireFly"]
if !ok {
return nil, fmt.Errorf("unable to find compiled FireFly contract")
return nil, nil, fmt.Errorf("unable to find compiled FireFly contract")
}
}

log.Info(fmt.Sprintf("deploying firefly contract via '%s'", firstNonExternalMember.ID))
contractAddress, err := deployContract(firstNonExternalMember, fireflyContract, map[string]string{})
if err != nil {
return nil, err
return nil, nil, err
}
return &core.BlockchainConfig{
blockchainConfig := &core.BlockchainConfig{
Ethereum: &core.EthereumConfig{
Ethconnect: &core.EthconnectConfig{
Instance: contractAddress,
},
},
}, nil
}
result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: "FireFly",
Location: map[string]string{"address": contractAddress},
},
}
return blockchainConfig, result, nil
}

func DeployCustomContract(member *types.Member, filename, contractName string) (string, error) {
Expand Down
17 changes: 12 additions & 5 deletions internal/blockchain/ethereum/geth/geth_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (p *GethProvider) unlockAccount(address, password string) error {
return nil
}

func (p *GethProvider) DeployFireFlyContract() (*core.BlockchainConfig, error) {
func (p *GethProvider) DeployFireFlyContract() (*core.BlockchainConfig, *types.ContractDeploymentResult, error) {
return ethconnect.DeployFireFlyContract(p.Stack, p.Log, p.Verbose)
}

Expand Down Expand Up @@ -218,14 +218,21 @@ func (p *GethProvider) GetContracts(filename string, extraArgs []string) ([]stri
return contractNames, err
}

func (p *GethProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (interface{}, error) {
func (p *GethProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (*types.ContractDeploymentResult, error) {
contractAddres, err := ethconnect.DeployCustomContract(member, filename, contractName)
if err != nil {
return nil, err
}
return map[string]string{
"address": contractAddres,
}, nil

result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: contractName,
Location: map[string]string{
"address": contractAddres,
},
},
}
return result, nil
}

func (p *GethProvider) CreateAccount(args []string) (interface{}, error) {
Expand Down
70 changes: 43 additions & 27 deletions internal/blockchain/fabric/fabric_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,59 +112,69 @@ func (p *FabricProvider) FirstTimeSetup() error {
return nil
}

func (p *FabricProvider) DeployFireFlyContract() (*core.BlockchainConfig, error) {
func (p *FabricProvider) DeployFireFlyContract() (*core.BlockchainConfig, *types.ContractDeploymentResult, error) {
// No config patch YAML required for Fabric, as the chaincode name is pre-determined
return nil, p.deploySmartContracts()
result, err := p.deploySmartContracts()
return nil, result, err
}

func (p *FabricProvider) deploySmartContracts() error {
func (p *FabricProvider) deploySmartContracts() (*types.ContractDeploymentResult, error) {
packageFilename := path.Join(p.Stack.RuntimeDir, "contracts", "firefly_fabric.tar.gz")
chaincode := "firefly"
channel := "firefly"
version := "1.0"

if err := p.extractChaincode(); err != nil {
return err
return nil, err
}

if err := p.createChannel(); err != nil {
return err
return nil, err
}

if err := p.joinChannel(); err != nil {
return err
return nil, err
}

if err := p.installChaincode(packageFilename); err != nil {
return err
return nil, err
}

res, err := p.queryInstalled()
if err != nil {
return err
return nil, err
}
if len(res.InstalledChaincodes) == 0 {
return fmt.Errorf("failed to find installed chaincode")
return nil, fmt.Errorf("failed to find installed chaincode")
}

if err := p.approveChaincode(channel, chaincode, version, res.InstalledChaincodes[0].PackageID); err != nil {
return err
return nil, err
}

if err := p.commitChaincode(channel, chaincode, version); err != nil {
return err
return nil, err
}

p.Log.Info("registering identities")
for _, m := range p.Stack.Members {
account, err := p.registerIdentity(m, m.OrgName)
if err != nil {
return err
return nil, err
}
p.Stack.State.Accounts = append(p.Stack.State.Accounts, account)
}

return nil
result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: "FireFly",
Location: map[string]string{
"channel": channel,
"chaincode": chaincode,
},
},
}
return result, nil
}

func (p *FabricProvider) PreStart() error {
Expand Down Expand Up @@ -446,30 +456,30 @@ func (p *FabricProvider) GetContracts(filename string, extraArgs []string) ([]st
return []string{filename}, nil
}

func (p *FabricProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (interface{}, error) {
func (p *FabricProvider) DeployContract(filename, contractName string, member *types.Member, extraArgs []string) (*types.ContractDeploymentResult, error) {
filename, err := filepath.Abs(filename)
if err != nil {
return "", err
return nil, err
}
switch {
case len(extraArgs) < 1:
return "", fmt.Errorf("channel not set. usage: ff deploy <stack_name> <filename> <channel> <chaincode> <version>")
return nil, fmt.Errorf("channel not set. usage: ff deploy <stack_name> <filename> <channel> <chaincode> <version>")
case len(extraArgs) < 2:
return "", fmt.Errorf("chaincode not set. usage: ff deploy <stack_name> <filename> <channel> <chaincode> <version>")
return nil, fmt.Errorf("chaincode not set. usage: ff deploy <stack_name> <filename> <channel> <chaincode> <version>")
case len(extraArgs) < 3:
return "", fmt.Errorf("version not set. usage: ff deploy <stack_name> <filename> <channel> <chaincode> <version>")
return nil, fmt.Errorf("version not set. usage: ff deploy <stack_name> <filename> <channel> <chaincode> <version>")
}
channel := extraArgs[0]
chaincode := extraArgs[1]
version := extraArgs[2]

if err := p.installChaincode(filename); err != nil {
return "", err
return nil, err
}

res, err := p.queryInstalled()
if err != nil {
return "", err
return nil, err
}

chaincodeInstalled := false
Expand All @@ -483,20 +493,26 @@ func (p *FabricProvider) DeployContract(filename, contractName string, member *t
}

if !chaincodeInstalled {
return "", fmt.Errorf("failed to find installed chaincode")
return nil, fmt.Errorf("failed to find installed chaincode")
}

if err := p.approveChaincode(channel, chaincode, version, packageID); err != nil {
return "", err
return nil, err
}

if err := p.commitChaincode(channel, chaincode, version); err != nil {
return "", err
return nil, err
}
return map[string]string{
"channel": channel,
"chaincode": chaincode,
}, nil
result := &types.ContractDeploymentResult{
DeployedContract: &types.DeployedContract{
Name: "FireFly",
Location: map[string]string{
"channel": channel,
"chaincode": chaincode,
},
},
}
return result, nil
}

func (p *FabricProvider) CreateAccount(args []string) (interface{}, error) {
Expand Down
Loading

0 comments on commit fdc6c78

Please sign in to comment.