-
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
3 changed files
with
155 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// (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 | ||
blockchainWsEndpoint string | ||
postgresEndpoint string | ||
inputBoxAddress common.Address | ||
inputBoxBlockNumber uint64 | ||
applicationAddress common.Address | ||
} | ||
|
||
func NewInputReaderService( | ||
blockchainHttpEndpoint string, | ||
blockchainWsEndpoint string, | ||
postgresEndpoint string, | ||
inputBoxAddress common.Address, | ||
inputBoxBlockNumber uint64, | ||
applicationAddress common.Address, | ||
) InputReaderService { | ||
return InputReaderService{ | ||
blockchainHttpEndpoint: blockchainHttpEndpoint, | ||
blockchainWsEndpoint: blockchainWsEndpoint, | ||
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 | ||
} | ||
defer client.Close() | ||
|
||
wsClient, err := ethclient.DialContext(ctx, s.blockchainWsEndpoint) | ||
if err != nil { | ||
return err | ||
} | ||
defer wsClient.Close() | ||
|
||
inputBoxWrapper, err := NewInputBoxInputSource(s.inputBoxAddress, client) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
reader := newInputReader( | ||
client, | ||
wsClient, | ||
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