Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
add allowance flag for provers
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey committed Nov 30, 2023
1 parent 4a67e50 commit b69acad
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/flags/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ var (
Value: 16,
Category: proverCategory,
}
Allowance = &cli.StringFlag{
Name: "prover.allowance",
Usage: "Amount to approve TaikoL1 contract for TaikoToken usage",
Category: proverCategory,
}
)

// All prover flags.
Expand Down Expand Up @@ -210,4 +215,5 @@ var ProverFlags = MergeFlags(CommonFlags, []cli.Flag{
DatabasePath,
DatabaseCacheSize,
ProverAssignmentHookAddress,
Allowance,
})
12 changes: 12 additions & 0 deletions prover/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Config struct {
MaxBlockSlippage uint64
DatabasePath string
DatabaseCacheSize uint64
Allowance *big.Int
}

// NewConfigFromCliContext creates a new config instance from command line flags.
Expand Down Expand Up @@ -105,6 +106,16 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
proveBlockMaxTxGasTipCap = new(big.Int).SetUint64(c.Uint64(flags.ProveBlockMaxTxGasTipCap.Name))
}

var allowance *big.Int = common.Big0
if c.IsSet(flags.Allowance.Name) {
amt, ok := new(big.Int).SetString(c.String(flags.Allowance.Name), 10)
if !ok {
return nil, fmt.Errorf("error setting allowance config value: %v", c.String(flags.Allowance.Name))
}

allowance = amt
}

return &Config{
L1WsEndpoint: c.String(flags.L1WSEndpoint.Name),
L1HttpEndpoint: c.String(flags.L1HTTPEndpoint.Name),
Expand Down Expand Up @@ -144,5 +155,6 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
MaxProposedIn: c.Uint64(flags.MaxProposedIn.Name),
DatabasePath: c.String(flags.DatabasePath.Name),
DatabaseCacheSize: c.Uint64(flags.DatabaseCacheSize.Name),
Allowance: allowance,
}, nil
}
4 changes: 4 additions & 0 deletions prover/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
l2HttpEndpoint = os.Getenv("L2_EXECUTION_ENGINE_HTTP_ENDPOINT")
taikoL1 = os.Getenv("TAIKO_L1_ADDRESS")
taikoL2 = os.Getenv("TAIKO_L2_ADDRESS")
allowance = "10000000000000000000000000000000000000000000000000"
rpcTimeout = 5 * time.Second
minTierFee = 1024
)
Expand Down Expand Up @@ -57,6 +58,7 @@ func (s *ProverTestSuite) TestNewConfigFromCliContextGuardianProver() {
s.Equal(uint64(128), c.DatabaseCacheSize)
s.Equal(uint64(100), c.MaxProposedIn)
s.Equal(os.Getenv("ASSIGNMENT_HOOK_ADDRESS"), c.AssignmentHookAddress.String())
s.Equal(allowance, c.Allowance.String())

return err
}
Expand Down Expand Up @@ -88,6 +90,7 @@ func (s *ProverTestSuite) TestNewConfigFromCliContextGuardianProver() {
"--" + flags.DatabasePath.Name, "dbPath",
"--" + flags.DatabaseCacheSize.Name, "128",
"--" + flags.MaxProposedIn.Name, "100",
"--" + flags.Allowance.Name, allowance,
}))
}

Expand Down Expand Up @@ -161,6 +164,7 @@ func (s *ProverTestSuite) SetupApp() *cli.App {
&cli.Uint64Flag{Name: flags.DatabaseCacheSize.Name},
&cli.Uint64Flag{Name: flags.MaxProposedIn.Name},
&cli.StringFlag{Name: flags.ProverAssignmentHookAddress.Name},
&cli.StringFlag{Name: flags.Allowance.Name},
}
app.Action = func(ctx *cli.Context) error {
_, err := NewConfigFromCliContext(ctx)
Expand Down
64 changes: 64 additions & 0 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,74 @@ func InitFromConfig(ctx context.Context, p *Prover, cfg *Config) (err error) {
if p.IsGuardianProver() {
proverServerOpts.ProverPrivateKey = p.cfg.GuardianProverPrivateKey
}

if p.srv, err = server.New(proverServerOpts); err != nil {
return err
}

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

return nil
}

func (p *Prover) setApprovalAmount() error {
if p.cfg.Allowance.Cmp(common.Big0) != 1 {
log.Info("skipping setting approval, approval amount not set")
return nil
}

// check allowance
allowance, err := p.rpc.TaikoToken.Allowance(
&bind.CallOpts{},
p.proverAddress,
p.cfg.TaikoL1Address,
)
if err != nil {
return err
}

if allowance.Cmp(p.cfg.Allowance) == 1 {
log.Info("skipping setting approval, allowance already greater or equal",
"allowance", allowance.String(),
"approvalAmount", p.cfg.Allowance.String(),
)
return nil
}

opts, err := bind.NewKeyedTransactorWithChainID(
p.cfg.L1ProverPrivKey,
p.rpc.L1ChainID,
)
if err != nil {
return err
}

amt := new(big.Int).Exp(big.NewInt(1_000_000_000), big.NewInt(18), nil)

log.Info("prover approving taikoL1 for taiko token", "amt", amt.Uint64())

tx, err := p.rpc.TaikoToken.Approve(
opts,
p.cfg.TaikoL1Address,
amt,
)
if err != nil {
return err
}

receipt, err := rpc.WaitReceipt(context.Background(), p.rpc.L1, tx)
if err != nil {
return err
}

log.Info("prover approved taikoL1 for taiko token", "txHash", receipt.TxHash.Hex())

if receipt.Status != 1 {
return errors.New("unsuccessful transaction to approve taikol1")
}

return nil
}

Expand Down

0 comments on commit b69acad

Please sign in to comment.