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

feat(client:go): add batch create contract #689

Merged
merged 3 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
107 changes: 107 additions & 0 deletions clients/tfchain-client-go/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,113 @@ func (s *Substrate) CreateNodeContract(identity Identity, node uint32, body stri
return s.GetContractWithHash(node, h)
}

// BatchCreateContractData struct for batch create contract
type BatchCreateContractData struct {
Node uint32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add json tags please? We need them for the web3 proxy.

Body string
Hash string
PublicIPs uint32
SolutionProviderID *uint64

// for name contracts. if set the contract is assumed to be a name contract
// and other fields are ignored
Name string

hexHash HexHash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field will be private, it doesn't start with upper case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was intentional as it is generated internally and not provided by user, should it be public?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't return this object then no there is no use to it.

}

// BatchAllCreateContract creates a batch of contracts for deployments atomically.
// transaction will rollback on error
func (s *Substrate) BatchAllCreateContract(identity Identity, contractData []BatchCreateContractData) ([]uint64, error) {
contracts, _, err := s.batchCreateContract(identity, contractData, "Utility.batch_all")
return contracts, err
}

// BatchCreateContract creates a batch of contracts for deployments non-atomically.
// on error returns the created contracts, the first failing contract index and the error message.
func (s *Substrate) BatchCreateContract(identity Identity, contractData []BatchCreateContractData) ([]uint64, *int, error) {
return s.batchCreateContract(identity, contractData, "Utility.batch")
}

func (s *Substrate) batchCreateContract(identity Identity, contractData []BatchCreateContractData, batchType string) ([]uint64, *int, error) {
cl, meta, err := s.GetClient()
if err != nil {
return nil, nil, err
}

calls := make([]types.Call, 0)
for i, contract := range contractData {
if contract.Name != "" {
c, err := types.NewCall(meta, "SmartContractModule.create_name_contract",
contract.Name,
)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create call")
}
calls = append(calls, c)
continue
}

var providerID types.OptionU64
if contract.SolutionProviderID != nil {
providerID = types.NewOptionU64(types.U64(*contract.SolutionProviderID))
}

h := NewHexHash(contract.Hash)
c, err := types.NewCall(meta, "SmartContractModule.create_node_contract",
contract.Node, h, contract.Body, contract.PublicIPs, providerID,
)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create call")
}

calls = append(calls, c)
contractData[i].hexHash = h
}
batchCall, err := types.NewCall(meta, batchType, calls)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create batch call")
}

resp, err := s.Call(cl, meta, identity, batchCall)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to create contracts")
}

var failingContract *int
if batchType == "Utility.batch" && resp.Events != nil && resp.Events.Utility_BatchInterrupted != nil {
i := int(resp.Events.Utility_BatchInterrupted[0].Index)
failingContract = &i
}

contracts := make([]uint64, 0)
for i, contract := range contractData {
if failingContract != nil && *failingContract == i {
break
}

var contractID uint64
var err error

if contract.Name != "" {
contractID, err = s.GetContractIDByNameRegistration(contract.Name)
} else {
contractID, err = s.GetContractWithHash(contract.Node, contract.hexHash)
}

if err != nil {
failingContract = &i
return nil, failingContract, err
}
contracts = append(contracts, contractID)
}

if failingContract != nil {
err = errors.New("failed to create contracts")
}
return contracts, failingContract, err
}

// CreateNameContract creates a contract for deployment
func (s *Substrate) CreateNameContract(identity Identity, name string) (uint64, error) {
cl, meta, err := s.GetClient()
Expand Down
145 changes: 145 additions & 0 deletions clients/tfchain-client-go/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,148 @@ func TestCancelBatch(t *testing.T) {
err = cl.BatchCancelContract(identity, []uint64{contractID_1, contractID_2})
require.NoError(t, err)
}

func TestCreateBatch(t *testing.T) {
var nodeID uint32

cl := startLocalConnection(t)
defer cl.Close()

identity, err := NewIdentityFromSr25519Phrase(BobMnemonics)
require.NoError(t, err)

farmID, twinID := assertCreateFarm(t, cl)

nodeID = assertCreateNode(t, cl, farmID, twinID, identity)
require.NoError(t, err)

t.Run("batch all succeeded", func(t *testing.T) {
contracts := []BatchCreateContractData{{
Node: nodeID,
Body: "",
Hash: "adf503a0e0160ba74804c27494f87255",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Node: nodeID,
Body: "",
Hash: "d20454ba65dfd4577af63d142c622cdc",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Name: "test",
}}

c, err := cl.BatchAllCreateContract(identity, contracts)
require.NoError(t, err)
require.Len(t, c, 3)

_, err = cl.GetContract(c[0])
require.NoError(t, err)

_, err = cl.GetContract(c[1])
require.NoError(t, err)

_, err = cl.GetContract(c[2])
require.NoError(t, err)

err = cl.BatchCancelContract(identity, c)
require.NoError(t, err)
})
t.Run("batch all failed", func(t *testing.T) {
// second contract already exists (same hash)
contracts := []BatchCreateContractData{{
Node: nodeID,
Body: "",
Hash: "adf503a0e0160ba74804c27494f87250",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Node: nodeID,
Body: "",
Hash: "adf503a0e0160ba74804c27494f87250",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Name: "test",
}}

c, err := cl.BatchAllCreateContract(identity, contracts)
require.Error(t, err)
require.Len(t, c, 0)

hash := NewHexHash(contracts[0].Hash)

// first contract should be rolled back
_, err = cl.GetContractWithHash(nodeID, hash)
require.Error(t, err)
})
t.Run("batch succeeded", func(t *testing.T) {
contracts := []BatchCreateContractData{{
Node: nodeID,
Body: "",
Hash: "adf503a0e0160ba74804c27494f87251",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Node: nodeID,
Body: "",
Hash: "d20454ba65dfd4577af63d142c622cda",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Name: "test",
}}

c, index, err := cl.BatchCreateContract(identity, contracts)
require.NoError(t, err)
require.Len(t, c, 3)
require.Nil(t, index)

_, err = cl.GetContract(c[0])
require.NoError(t, err)

_, err = cl.GetContract(c[1])
require.NoError(t, err)

_, err = cl.GetContract(c[2])
require.NoError(t, err)

err = cl.BatchCancelContract(identity, c)
require.NoError(t, err)
})

t.Run("batch failed", func(t *testing.T) {
// second contract already exists (same hash)
contracts := []BatchCreateContractData{{
Node: nodeID,
Body: "",
Hash: "adf503a0e0160ba74804c27494f87253",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Node: nodeID,
Body: "",
Hash: "adf503a0e0160ba74804c27494f87253",
PublicIPs: 0,
SolutionProviderID: nil,
}, {
Name: "test",
}}

c, index, err := cl.BatchCreateContract(identity, contracts)
require.Error(t, err)
require.NotNil(t, index)
require.Equal(t, *index, 1)
require.Len(t, c, 1)

// first contract should be created
_, err = cl.GetContract(c[0])
require.NoError(t, err)

// third contract should not be created
_, err = cl.GetContractIDByNameRegistration(contracts[2].Name)
require.Error(t, err)
})

}