-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(claimer): WIP rework of claimer in GO
- Loading branch information
Showing
11 changed files
with
388 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-claimer/root" | ||
) | ||
|
||
func main() { | ||
err := root.Cmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package root | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cartesi/rollups-node/internal/claimer" | ||
"github.com/cartesi/rollups-node/internal/config" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// Should be overridden during the final release build with ldflags | ||
// to contain the actual version number | ||
buildVersion = "devel" | ||
) | ||
|
||
const ( | ||
CMD_NAME = "claimer2" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: CMD_NAME, | ||
Short: "Runs Claimer", | ||
Long: `Runs Claimer in standalone mode`, | ||
Run: run, | ||
} | ||
|
||
func run(cmd *cobra.Command, args []string) { | ||
ctx := context.Background() | ||
c := config.FromEnv() | ||
|
||
claimer, err := claimer.Create(&claimer.CreateInfo{ | ||
Auth: c.Auth, | ||
BlockchainHttpEndpoint: c.BlockchainHttpEndpoint, | ||
PostgresEndpoint: c.PostgresEndpoint, | ||
Context: ctx, | ||
PollInterval: 1000 * time.Millisecond, | ||
}) | ||
cobra.CheckErr(err) | ||
claimer.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package claimer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"math/big" | ||
"time" | ||
|
||
signtx "github.com/cartesi/rollups-node/internal/aws-kms-signtx" | ||
conf "github.com/cartesi/rollups-node/internal/config" | ||
repo "github.com/cartesi/rollups-node/internal/repository" | ||
"github.com/cartesi/rollups-node/pkg/contracts/iconsensus" | ||
"github.com/cartesi/rollups-node/pkg/ethutil" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
aws_cfg "github.com/aws/aws-sdk-go-v2/config" | ||
aws_kms "github.com/aws/aws-sdk-go-v2/service/kms" | ||
) | ||
|
||
type CreateInfo struct { | ||
Auth conf.Auth | ||
BlockchainHttpEndpoint conf.Redacted[string] | ||
Context context.Context | ||
EthConn ethclient.Client | ||
PollInterval time.Duration | ||
PostgresEndpoint conf.Redacted[string] | ||
} | ||
|
||
type Service struct { | ||
consensus iconsensus.IConsensus | ||
context context.Context | ||
dbConn *repo.Database | ||
ethConn *ethclient.Client | ||
signer *bind.TransactOpts | ||
ticker *time.Ticker | ||
} | ||
|
||
func createSigner(ctx context.Context, auth conf.Auth, chainID *big.Int) (*bind.TransactOpts, error) { | ||
switch auth := auth.(type) { | ||
case conf.AuthMnemonic: | ||
privateKey, err := ethutil.MnemonicToPrivateKey( | ||
auth.Mnemonic.Value, uint32(auth.AccountIndex.Value)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bind.NewKeyedTransactorWithChainID(privateKey, chainID) | ||
case conf.AuthAWS: | ||
awsc, err := aws_cfg.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
kms := aws_kms.NewFromConfig(awsc, func(o *aws_kms.Options) { | ||
o.BaseEndpoint = aws.String(auth.EndpointURL.Value) | ||
}) | ||
return signtx.CreateAWSTransactOpts(ctx, kms, | ||
aws.String(auth.KeyID.Value), types.NewEIP155Signer(chainID)) | ||
} | ||
return nil, fmt.Errorf("error: unimplemented authentication method") | ||
} | ||
|
||
func Create(ci *CreateInfo) (s *Service, err error) { | ||
ethconn, err := ethclient.Dial(ci.BlockchainHttpEndpoint.Value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
chainid, err := ethconn.NetworkID(ci.Context) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signer, err := createSigner(ci.Context, ci.Auth, chainid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dbconn, err := repo.Connect(ci.Context, ci.PostgresEndpoint.Value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Service{ | ||
context: ci.Context, | ||
dbConn: dbconn, | ||
ethConn: ethconn, | ||
signer: signer, | ||
ticker: time.NewTicker(ci.PollInterval), | ||
}, nil | ||
} | ||
|
||
func (s *Service) onTick() error { | ||
claims, err := s.dbConn.GetComputedClaims(s.context) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
slog.Info("claimer: polling database for computed claims:", "found", len(claims)) | ||
for _, claim := range(claims) { | ||
// TODO: detect duplicates? | ||
|
||
slog.Info("claimer: submitting:", "claim", claim) | ||
instance, err := iconsensus.NewIConsensus(claim.IConsensusAddress, s.ethConn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tx, err := instance.SubmitClaim(s.signer, claim.AppAddress, claim.LastBlock, claim.Hash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
slog.Info("claimer: got receipt", "transaction_hash", tx.Hash()) | ||
err = s.dbConn.SetClaimAsSubmitted(s.context, claim.ID, tx.Hash()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// TODO: implement quit channel | ||
func (s *Service) Start() error { | ||
for { | ||
select { | ||
case <-s.ticker.C: | ||
err := s.onTick() | ||
if err != nil { | ||
slog.Error("failed: ", "cause", err) | ||
} | ||
// TODO: recreate DBConn on error | ||
// TODO: recreate EthConn on error | ||
_ = err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.