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(database): Add Epoch and Snapshot tables #548

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 0 additions & 11 deletions cmd/cartesi-rollups-cli/root/app/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var (
templateHash string
inputBoxDeploymentBlockNumber uint64
snapshotUri string
epochLength uint64
status string
)

Expand Down Expand Up @@ -72,14 +71,6 @@ func init() {
"Application snapshot URI",
)

Cmd.Flags().Uint64VarP(
&epochLength,
"epoch-length",
"e",
1,
"Application epoch length in blocks",
)

Cmd.Flags().StringVarP(
&status,
"status",
Expand Down Expand Up @@ -113,8 +104,6 @@ func run(cmd *cobra.Command, args []string) {
ContractAddress: common.HexToAddress(applicationAddress),
TemplateHash: common.HexToHash(templateHash),
LastProcessedBlock: inputBoxDeploymentBlockNumber,
SnapshotURI: snapshotUri,
EpochLength: epochLength,
Status: applicationStatus,
}

Expand Down
1 change: 0 additions & 1 deletion docs/cli/cartesi-rollups-cli_app_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ cartesi-rollups-cli app add -a 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -n 10

```
-a, --address string Application contract address
-e, --epoch-length uint Application epoch length in blocks (default 1)
-h, --help help for add
-n, --inputbox-block-number uint InputBox deployment block number
-u, --snapshot-uri string Application snapshot URI
Expand Down
66 changes: 31 additions & 35 deletions internal/evmreader/evmreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log/slog"
"math/big"

"github.com/cartesi/rollups-node/internal/node/model"
. "github.com/cartesi/rollups-node/internal/node/model"
"github.com/cartesi/rollups-node/pkg/contracts/inputbox"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -18,62 +18,58 @@ import (
"github.com/ethereum/go-ethereum/rpc"
)

type (
Address = common.Address
Application = model.Application
Input = model.Input
DefaultBlock = model.DefaultBlock
NodePersistentConfig = model.NodePersistentConfig
InputBoxInputAdded = inputbox.InputBoxInputAdded
FilterOpts = bind.FilterOpts
Context = context.Context
Header = types.Header
Subscription = ethereum.Subscription
)

// Interface for Input reading
type InputSource interface {
// Wrapper for FilterInputAdded(), which is automatically generated
// by go-ethereum and cannot be used for testing
RetrieveInputs(
opts *FilterOpts,
appContract []Address,
opts *bind.FilterOpts,
appContract []common.Address,
index []*big.Int,
) ([]InputBoxInputAdded, error)
) ([]inputbox.InputBoxInputAdded, error)
}

// Interface for the node repository
type EvmReaderRepository interface {
InsertInputsAndUpdateLastProcessedBlock(
ctx Context,
ctx context.Context,
inputs []Input,
blockNumber uint64,
appAddress Address,
appAddress common.Address,
) error
GetAllRunningApplications(
ctx context.Context,
) ([]Application, error)
GetNodeConfig(
ctx context.Context,
) (*NodePersistentConfig, error)
GetEpoch(
ctx context.Context,
indexKey uint64,
appAddressKey common.Address,
) (*Epoch, error)
InsertEpoch(
ctx context.Context,
epoch *Epoch,
) (uint64, error)
}

// EthClient mimics part of ethclient.Client functions to narrow down the
// interface needed by the EvmReader. It must be bound to an HTTP endpoint
type EthClient interface {
HeaderByNumber(
ctx Context,
ctx context.Context,
number *big.Int,
) (*Header, error)
) (*types.Header, error)
}

// EthWsClient mimics part of ethclient.Client functions to narrow down the
// interface needed by the EvmReader. It must be bound to a WS endpoint
type EthWsClient interface {
SubscribeNewHead(
ctx Context,
ch chan<- *Header,
) (Subscription, error)
ctx context.Context,
ch chan<- *types.Header,
) (ethereum.Subscription, error)
}

type SubscriptionError struct {
Expand Down Expand Up @@ -134,10 +130,10 @@ func (r *EvmReader) Run(
// Watch for new blocks and reads new inputs based on the
// default block configuration, which have not been processed yet.
func (r *EvmReader) watchForNewBlocks(
ctx Context,
ctx context.Context,
ready chan<- struct{},
) error {
headers := make(chan *Header)
headers := make(chan *types.Header)
sub, err := r.wsClient.SubscribeNewHead(ctx, headers)
if err != nil {
return fmt.Errorf("could not start subscription: %v", err)
Expand Down Expand Up @@ -167,7 +163,7 @@ func (r *EvmReader) watchForNewBlocks(
}

// Check if is there new Inputs for all running Applications
func (r *EvmReader) checkForNewInputs(ctx Context) error {
func (r *EvmReader) checkForNewInputs(ctx context.Context) error {

// Get All Applications
apps, err := r.repository.GetAllRunningApplications(ctx)
Expand Down Expand Up @@ -245,19 +241,19 @@ func (r *EvmReader) classifyApplicationsByLastProcessedInput(
// Fetch the most recent header up till the
// given default block
func (r *EvmReader) fetchMostRecentHeader(
ctx Context,
ctx context.Context,
defaultBlock DefaultBlock,
) (*types.Header, error) {

var defaultBlockNumber int64
switch defaultBlock {
case model.DefaultBlockStatusPending:
case DefaultBlockStatusPending:
defaultBlockNumber = rpc.PendingBlockNumber.Int64()
case model.DefaultBlockStatusLatest:
case DefaultBlockStatusLatest:
defaultBlockNumber = rpc.LatestBlockNumber.Int64()
case model.DefaultBlockStatusFinalized:
case DefaultBlockStatusFinalized:
defaultBlockNumber = rpc.FinalizedBlockNumber.Int64()
case model.DefaultBlockStatusSafe:
case DefaultBlockStatusSafe:
defaultBlockNumber = rpc.SafeBlockNumber.Int64()
default:
return nil, fmt.Errorf("Default block '%v' not supported", defaultBlock)
Expand All @@ -284,9 +280,9 @@ func (r *EvmReader) readInputs(
endBlock uint64,
apps []Application,
) error {
filter := []Address{}
filter := []common.Address{}

var inputsMap = make(map[Address][]Input)
var inputsMap = make(map[common.Address][]Input)
for _, app := range apps {
filter = append(filter, app.ContractAddress)
inputsMap[app.ContractAddress] = []Input{}
Expand All @@ -310,7 +306,7 @@ func (r *EvmReader) readInputs(
slog.Debug("received input ", "app", event.AppContract, "index", event.Index)
input := Input{
Index: event.Index.Uint64(),
CompletionStatus: model.InputStatusNone,
CompletionStatus: InputStatusNone,
RawData: event.Input,
BlockNumber: event.Raw.BlockNumber,
AppAddress: event.AppContract,
Expand Down
Loading
Loading