-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(input-reader): Add input-reader to node Supervisor
- Loading branch information
Showing
5 changed files
with
180 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package inputreader | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/cartesi/rollups-node/pkg/contracts/inputbox" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
) | ||
|
||
// InputBox Wrapper | ||
type InputBoxInputSource struct { | ||
inputbox *inputbox.InputBox | ||
} | ||
|
||
func NewInputBoxInputSource( | ||
inputBoxAddress common.Address, | ||
client *ethclient.Client, | ||
) (*InputBoxInputSource, error) { | ||
inputbox, err := inputbox.NewInputBox(inputBoxAddress, client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &InputBoxInputSource{ | ||
inputbox: inputbox, | ||
}, nil | ||
} | ||
|
||
func (i *InputBoxInputSource) RetrieveInputs( | ||
opts *bind.FilterOpts, | ||
appContract []common.Address, | ||
index []*big.Int, | ||
) ([]*inputbox.InputBoxInputAdded, error) { | ||
|
||
itr, err := i.inputbox.FilterInputAdded(opts, appContract, index) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer itr.Close() | ||
|
||
var events []*inputbox.InputBoxInputAdded | ||
for itr.Next() { | ||
inputAddedEvent := itr.Event | ||
events = append(events, inputAddedEvent) | ||
} | ||
err = itr.Error() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return events, nil | ||
} |
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,76 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package inputreader | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cartesi/rollups-node/internal/repository" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
) | ||
|
||
// Service to manage InputReader lifecycle | ||
type InputReaderService struct { | ||
blockchainHttpEndpoint string | ||
postgresEndpoint string | ||
inputBoxAddress common.Address | ||
inputBoxBlockNumber uint64 | ||
applicationAddress common.Address | ||
} | ||
|
||
func NewInputReaderService( | ||
blockchainHttpEndpoint string, | ||
postgresEndpoint string, | ||
inputBoxAddress common.Address, | ||
inputBoxBlockNumber uint64, | ||
applicationAddress common.Address, | ||
) InputReaderService { | ||
return InputReaderService{ | ||
blockchainHttpEndpoint: blockchainHttpEndpoint, | ||
postgresEndpoint: postgresEndpoint, | ||
inputBoxAddress: inputBoxAddress, | ||
inputBoxBlockNumber: inputBoxBlockNumber, | ||
applicationAddress: applicationAddress, | ||
} | ||
} | ||
|
||
func (s InputReaderService) Start( | ||
ctx context.Context, | ||
ready chan<- struct{}, | ||
) error { | ||
|
||
db, err := repository.Connect(ctx, s.postgresEndpoint) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
client, err := ethclient.DialContext(ctx, s.blockchainHttpEndpoint) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
inputBoxWrapper, err := NewInputBoxInputSource(s.inputBoxAddress, client) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
reader := newInputReader( | ||
client, | ||
inputBoxWrapper, | ||
db, | ||
s.inputBoxAddress, | ||
s.inputBoxBlockNumber, | ||
s.applicationAddress, | ||
) | ||
|
||
return reader.Start(ctx, ready) | ||
} | ||
|
||
func (s InputReaderService) String() string { | ||
return "input-reader" | ||
} |
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.