Skip to content

Commit

Permalink
feat(protocol): enable whitelisting provers (disabled for now) (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik committed Nov 19, 2022
1 parent 7ce3973 commit 9d44a45
Show file tree
Hide file tree
Showing 43 changed files with 983 additions and 51 deletions.
19 changes: 18 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/cyberhorsey/errors v0.0.0-20220929234051-087d6d8bb841
github.com/ethereum/go-ethereum v1.10.25
github.com/joho/godotenv v1.4.0
github.com/labstack/echo/v4 v4.9.1
github.com/pkg/errors v0.9.1
github.com/pressly/goose v2.7.0+incompatible
github.com/pressly/goose/v3 v3.7.0
Expand All @@ -26,8 +27,10 @@ require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.9.4 // indirect
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/containerd v1.6.8 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -41,13 +44,19 @@ require (
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/labstack/echo-contrib v0.13.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
Expand All @@ -56,17 +65,25 @@ require (
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
107 changes: 107 additions & 0 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/protocol/contracts/L1/LibData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ library LibData {
// block id => parent hash => fork choice
mapping(uint256 => mapping(bytes32 => ForkChoice)) forkChoices;
mapping(bytes32 => uint256) commits;
mapping(address => bool) provers; // Whitelisted provers
uint64 genesisHeight;
uint64 latestFinalizedHeight;
uint64 latestFinalizedId;
Expand Down
25 changes: 24 additions & 1 deletion packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
using SafeCastUpgradeable for uint256;

LibData.State public state;
uint256[45] private __gap;
uint256[44] private __gap;

function init(
address _addressManager,
Expand Down Expand Up @@ -134,6 +134,29 @@ contract TaikoL1 is EssentialContract, IHeaderSync, V1Events {
);
}

/**
* Add or remove a prover from the whitelist.
*
* @param prover The prover to be added or removed.
* @param whitelisted True to add; remove otherwise.
*/
function whitelistProver(
address prover,
bool whitelisted
) public onlyOwner {
V1Proving.whitelistProver(state, prover, whitelisted);
}

/**
* Return whether a prover is whitelisted.
*
* @param prover The prover.
* @return True if the prover is whitelisted, false otherwise.
*/
function isProverWhitelisted(address prover) public view returns (bool) {
return V1Proving.isProverWhitelisted(state, prover);
}

/// @notice Finalize up to N blocks.
/// @param maxBlocks Max number of blocks to finalize.
function finalizeBlocks(uint256 maxBlocks) external nonReentrant {
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/contracts/L1/v1/V1Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ abstract contract V1Events {
uint64 provenAt,
address prover
);

event ProverWhitelisted(address indexed prover, bool whitelisted);
}
36 changes: 34 additions & 2 deletions packages/protocol/contracts/L1/v1/V1Proving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ library V1Proving {
address prover
);

event ProverWhitelisted(address indexed prover, bool whitelisted);

modifier onlyWhitelistedProver(LibData.State storage s) {
if (LibConstants.K_WHITELIST_PROVERS) {
require(s.provers[msg.sender], "L1:whitelist");
}
_;
}

function proveBlock(
LibData.State storage s,
AddressResolver resolver,
uint256 blockIndex,
bytes[] calldata inputs
) public {
) public onlyWhitelistedProver(s) {
// Check and decode inputs
require(inputs.length == 3, "L1:inputs:size");
Evidence memory evidence = abi.decode(inputs[0], (Evidence));
Expand Down Expand Up @@ -126,7 +135,7 @@ library V1Proving {
AddressResolver resolver,
uint256 blockIndex,
bytes[] calldata inputs
) public {
) public onlyWhitelistedProver(s) {
// Check and decode inputs
require(inputs.length == 3, "L1:inputs:size");
Evidence memory evidence = abi.decode(inputs[0], (Evidence));
Expand Down Expand Up @@ -182,6 +191,29 @@ library V1Proving {
);
}

function whitelistProver(
LibData.State storage s,
address prover,
bool enabled
) public {
require(LibConstants.K_WHITELIST_PROVERS, "L1:featureDisabled");
require(
prover != address(0) && s.provers[prover] != enabled,
"L1:precondition"
);

s.provers[prover] = enabled;
emit ProverWhitelisted(prover, enabled);
}

function isProverWhitelisted(
LibData.State storage s,
address prover
) public view returns (bool) {
require(LibConstants.K_WHITELIST_PROVERS, "L1:featureDisabled");
return s.provers[prover];
}

function _proveBlock(
LibData.State storage s,
AddressResolver resolver,
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/contracts/libs/LibConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ library LibConstants {

bytes32 public constant V1_INVALIDATE_BLOCK_LOG_TOPIC =
keccak256("BlockInvalidated(bytes32)");

bool public constant K_WHITELIST_PROVERS = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// ╱╱╰╯╰╯╰┻┻╯╰┻━━╯╰━━━┻╯╰┻━━┻━━╯
pragma solidity ^0.8.9;


contract TestBadReceiver {
receive() external payable {
revert("can not send to this contract");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ contract TestLibBridgeRetry is EssentialContract {
EssentialContract._init(_addressManager);
}

function retryMessage(IBridge.Message calldata message, bool lastAttempt)
public
payable
{
function retryMessage(
IBridge.Message calldata message,
bool lastAttempt
) public payable {
LibBridgeRetry.retryMessage(
state,
AddressResolver(this),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity ^0.8.9;
import "../../bridge/IBridge.sol";

contract TestMessageSender {

bytes32 signal = 0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab;
bytes32 signal =
0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab;

function sendMessage(
IBridge.Message calldata message
Expand Down
4 changes: 3 additions & 1 deletion packages/relayer/.default.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
HTTP_PORT=4102
PROMETHEUS_HTTP_PORT=6060
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_DATABASE=relayer
Expand All @@ -16,4 +17,5 @@ MYSQL_MAX_OPEN_CONNS=
MYSQL_CONN_MAX_LIFETIME_IN_MS=
NUM_GOROUTINES=20
SUBSCRIPTION_BACKOFF_IN_SECONDS=3
CONFIRMATIONS_BEFORE_PROCESSING=15
CONFIRMATIONS_BEFORE_PROCESSING=15
CORS_ORIGINS=*
4 changes: 3 additions & 1 deletion packages/relayer/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ output:

linters:
enable:
- errcheck
- funlen
- gocognit
- gocritic
Expand All @@ -21,6 +22,7 @@ linters:
- gosec
- gosimple
- lll
- unused
- whitespace
- wsl

Expand All @@ -29,7 +31,7 @@ linters-settings:
lines: 116
statements: 48
gocognit:
min-complexity: 35
min-complexity: 37

issues:
exclude-rules:
Expand Down
22 changes: 22 additions & 0 deletions packages/relayer/bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package relayer

import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/taikochain/taiko-mono/packages/relayer/contracts"
)

type Bridge interface {
WatchMessageSent(
opts *bind.WatchOpts,
sink chan<- *contracts.BridgeMessageSent,
signal [][32]byte,
) (event.Subscription, error)
FilterMessageSent(opts *bind.FilterOpts, signal [][32]byte) (*contracts.BridgeMessageSentIterator, error)
GetMessageStatus(opts *bind.CallOpts, signal [32]byte) (uint8, error)
ProcessMessage(opts *bind.TransactOpts, message contracts.IBridgeMessage, proof []byte) (*types.Transaction, error)
IsMessageReceived(opts *bind.CallOpts, signal [32]byte, srcChainId *big.Int, proof []byte) (bool, error) // nolint
}
7 changes: 7 additions & 0 deletions packages/relayer/caller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package relayer

import "context"

type Caller interface {
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
}
20 changes: 18 additions & 2 deletions packages/relayer/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"github.com/labstack/echo/v4"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/taikochain/taiko-mono/packages/relayer"
"github.com/taikochain/taiko-mono/packages/relayer/db"
"github.com/taikochain/taiko-mono/packages/relayer/http"
"github.com/taikochain/taiko-mono/packages/relayer/indexer"
"github.com/taikochain/taiko-mono/packages/relayer/repo"
"gorm.io/driver/mysql"
Expand All @@ -36,6 +38,7 @@ var (
"MYSQL_HOST",
"RELAYER_ECDSA_KEY",
"CONFIRMATIONS_BEFORE_PROCESSING",
"PROMETHEUS_HTTP_PORT",
}

defaultBlockBatchSize = 2
Expand All @@ -44,7 +47,7 @@ var (
defaultConfirmations = 15
)

func Run(mode relayer.Mode, layer relayer.Layer) {
func Run(mode relayer.Mode, watchMode relayer.WatchMode, layer relayer.Layer) {
if err := loadAndValidateEnv(); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -77,6 +80,13 @@ func Run(mode relayer.Mode, layer relayer.Layer) {
log.Fatal(err)
}

srv, err := http.NewServer(http.NewServerOpts{
Echo: echo.New(),
})
if err != nil {
log.Fatal(err)
}

indexers, closeFunc, err := makeIndexers(layer, db)
if err != nil {
sqlDB.Close()
Expand All @@ -90,12 +100,18 @@ func Run(mode relayer.Mode, layer relayer.Layer) {

for _, i := range indexers {
go func(i *indexer.Service) {
if err := i.FilterThenSubscribe(context.Background(), mode); err != nil {
if err := i.FilterThenSubscribe(context.Background(), mode, watchMode); err != nil {
log.Fatal(err)
}
}(i)
}

go func() {
if err := srv.Start(fmt.Sprintf(":%v", os.Getenv("HTTP_PORT"))); err != nil {
log.Fatal(err)
}
}()

<-forever
}

Expand Down
9 changes: 8 additions & 1 deletion packages/relayer/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ func main() {
both: watch l1 => l2 and l2 => l1 bridge messages
`)

watchModePtr := flag.String("watch-mode", string(relayer.FilterAndSubscribeWatchMode), `watch mode to run in.
options:
filter: only filter previous messages
subscribe: only subscribe to new messages
filter-and-subscribe: catch up on all previous messages, then subscribe to new messages
`)

flag.Parse()

if !relayer.IsInSlice(relayer.Mode(*modePtr), relayer.Modes) {
Expand All @@ -33,5 +40,5 @@ func main() {
log.Fatal("mode not valid")
}

cli.Run(relayer.Mode(*modePtr), relayer.Layer(*layersPtr))
cli.Run(relayer.Mode(*modePtr), relayer.WatchMode(*watchModePtr), relayer.Layer(*layersPtr))
}
9 changes: 9 additions & 0 deletions packages/relayer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ var (
Both Layer = "both"
Layers = []Layer{L1, L2, Both}
)

type WatchMode string

var (
FilterWatchMode WatchMode = "filter"
SubscribeWatchMode WatchMode = "subscribe"
FilterAndSubscribeWatchMode WatchMode = "filter-and-subscribe"
WatchModes = []WatchMode{FilterWatchMode, SubscribeWatchMode}
)
Loading

0 comments on commit 9d44a45

Please sign in to comment.