Skip to content

Commit 0040383

Browse files
committed
feat(cli): add PRT consensus to deploy application
1 parent a0f3ba3 commit 0040383

2 files changed

Lines changed: 54 additions & 17 deletions

File tree

cmd/cartesi-rollups-cli/root/deploy/application.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ func runDeployApplication(cmd *cobra.Command, args []string) {
221221
application.EpochLength = res.Deployment.EpochLength
222222
application.DataAvailability = res.Deployment.DataAvailability
223223
application.IInputBoxBlock = res.Deployment.IInputBoxBlock
224+
225+
case *ethutil.PRTApplicationDeploymentResult:
226+
application.IApplicationAddress = res.ApplicationResult.ApplicationAddress
227+
application.IConsensusAddress = res.ApplicationResult.Deployment.Consensus
228+
application.IInputBoxAddress = res.ApplicationResult.Deployment.InputBoxAddress
229+
application.TemplateHash = res.ApplicationResult.Deployment.TemplateHash
230+
application.EpochLength = res.ApplicationResult.Deployment.EpochLength
231+
application.DataAvailability = res.ApplicationResult.Deployment.DataAvailability
232+
application.IInputBoxBlock = res.ApplicationResult.Deployment.IInputBoxBlock
224233
default:
225234
panic("unimplemented deployment type\n")
226235
}
@@ -447,11 +456,11 @@ func buildPrtApplicationDeployment(
447456
client *ethclient.Client,
448457
txOpts *bind.TransactOpts,
449458
) (
450-
*ethutil.PRTDeployment,
459+
*ethutil.PRTApplicationDeployment,
451460
error,
452461
) {
453462
var err error
454-
request := &ethutil.PRTDeployment{}
463+
request := &ethutil.PRTApplicationDeployment{}
455464
if !cmd.Flags().Changed("prt-factory") {
456465
request.FactoryAddress, err = config.GetContractsPrtFactoryAddress()
457466
} else {
@@ -461,7 +470,7 @@ func buildPrtApplicationDeployment(
461470
return nil, fmt.Errorf("error on parameter factory: %w", err)
462471
}
463472

464-
request.App, err = buildApplicationOnlyDeploymentWithoutConsensus(cmd, args, client, txOpts)
473+
request.Application, err = buildApplicationOnlyDeploymentWithoutConsensus(cmd, args, client, txOpts)
465474
if err != nil {
466475
return nil, fmt.Errorf("error on application: %w", err)
467476
}

pkg/ethutil/prt.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package ethutil
44

55
import (
66
"context"
7+
"encoding/hex"
78
"fmt"
89
"math/big"
910

@@ -15,25 +16,41 @@ import (
1516
"github.com/ethereum/go-ethereum/ethclient"
1617
)
1718

18-
type PRTDeployment struct {
19-
App *ApplicationDeployment
19+
type PRTApplicationDeployment struct {
2020
FactoryAddress common.Address
21+
TemplateHash common.Hash
22+
Salt SaltBytes
23+
24+
Application *ApplicationDeployment
2125
}
2226

23-
type PRTDeploymentResult struct {
27+
type PRTApplicationDeploymentResult struct {
28+
Deployment *PRTApplicationDeployment
29+
ApplicationResult *ApplicationDeploymentResult
2430
}
2531

26-
func (me *PRTDeployment) String() string {
32+
func (me *PRTApplicationDeployment) String() string {
2733
result := ""
28-
result += me.App.String()
29-
if me.App.Verbose {
34+
result += fmt.Sprintf("PRT application deployment:\n")
35+
result += fmt.Sprintf("\tapplication owner: %v\n", me.Application.OwnerAddress)
36+
if me.Application.Verbose {
3037
result += fmt.Sprintf("\tPRT factory address: %v\n", me.FactoryAddress)
31-
result += fmt.Sprintf("\tPRT consensus address: %v\n", me.App.Consensus)
38+
result += fmt.Sprintf("\tAPP factory address: %v\n", me.Application.FactoryAddress)
39+
result += fmt.Sprintf("\ttemplate hash: %v\n", me.Application.TemplateHash)
40+
result += fmt.Sprintf("\tdata availability: 0x%v\n", hex.EncodeToString(me.Application.DataAvailability))
41+
result += fmt.Sprintf("\tsalt: %v\n", me.Application.Salt)
3242
}
3343
return result
3444
}
3545

36-
func (me *PRTDeployment) deployPRT(
46+
func (me *PRTApplicationDeploymentResult) String() string {
47+
result := ""
48+
result += fmt.Sprintf("\tapplication address: %v\n", me.ApplicationResult.ApplicationAddress)
49+
result += fmt.Sprintf("\tconsensus address: %v\n", me.ApplicationResult.Deployment.Consensus)
50+
return result
51+
}
52+
53+
func (me *PRTApplicationDeployment) deployPRT(
3754
ctx context.Context,
3855
client *ethclient.Client,
3956
txOpts *bind.TransactOpts,
@@ -45,7 +62,7 @@ func (me *PRTDeployment) deployPRT(
4562
if err != nil {
4663
return zero, fmt.Errorf("failed to instantiate contract: %v", err)
4764
}
48-
tx, err := factory.NewDaveConsensus(txOpts, applicationAddress, me.App.TemplateHash, me.App.Salt)
65+
tx, err := factory.NewDaveConsensus(txOpts, applicationAddress, me.TemplateHash, me.Salt)
4966
if err != nil {
5067
return zero, fmt.Errorf("transaction failed: %v", err)
5168
}
@@ -72,19 +89,29 @@ func (me *PRTDeployment) deployPRT(
7289
}
7390

7491
// Do the consensus/application dance based on: https://github.com/cartesi/dave/blob/v1.0.0/cartesi-rollups/contracts/cannonfile.prod-instance.toml
75-
func (me *PRTDeployment) Deploy(
92+
func (me *PRTApplicationDeployment) Deploy(
7693
ctx context.Context,
7794
client *ethclient.Client,
7895
txOpts *bind.TransactOpts,
7996
) (common.Address, IApplicationDeploymentResult, error) {
8097
zero := common.Address{}
98+
result := &PRTApplicationDeploymentResult{}
99+
result.Deployment = me
81100

82-
applicationAddress, result, err := me.App.Deploy(ctx, client, txOpts)
101+
var err error
102+
applicationAddress, appResult, err := me.Application.Deploy(ctx, client, txOpts)
83103
if err != nil {
84104
return zero, nil, err
85105
}
86106

87-
me.App.Consensus, err = me.deployPRT(ctx, client, txOpts, applicationAddress)
107+
switch appRes := appResult.(type) {
108+
case *ApplicationDeploymentResult:
109+
result.ApplicationResult = appRes
110+
default:
111+
panic("Application deployment returned an impossible type.")
112+
}
113+
114+
consensus, err := me.deployPRT(ctx, client, txOpts, applicationAddress)
88115
if err != nil {
89116
return zero, nil, fmt.Errorf("failed to deploy PRT contract: %w", err)
90117
}
@@ -96,7 +123,7 @@ func (me *PRTDeployment) Deploy(
96123

97124
_, err = sendTransaction(ctx, client, txOpts, big.NewInt(0), GasLimit,
98125
func(txOpts *bind.TransactOpts) (*types.Transaction, error) {
99-
return application.MigrateToOutputsMerkleRootValidator(txOpts, me.App.Consensus)
126+
return application.MigrateToOutputsMerkleRootValidator(txOpts, consensus)
100127
},
101128
)
102129
if err != nil {
@@ -112,9 +139,10 @@ func (me *PRTDeployment) Deploy(
112139
return zero, nil, fmt.Errorf("failed to create a self hosted application: execution reverted")
113140
}
114141

142+
result.ApplicationResult.Deployment.Consensus = consensus
115143
return applicationAddress, result, nil
116144
}
117145

118-
func (me *PRTDeployment) GetFactoryAddress() common.Address {
146+
func (me *PRTApplicationDeployment) GetFactoryAddress() common.Address {
119147
return me.FactoryAddress
120148
}

0 commit comments

Comments
 (0)