Skip to content

Commit

Permalink
Merge pull request #595 from OffchainLabs/kovan
Browse files Browse the repository at this point in the history
Add Kovan Support
  • Loading branch information
edfelten committed Oct 12, 2020
2 parents ef13f46 + a5650f6 commit 4d99801
Show file tree
Hide file tree
Showing 27 changed files with 176 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ type TimeGetterMock struct {
blockIdFunc func(ctx context.Context, height *common.TimeBlocks) (*common.BlockId, error)
}

func (m *TimeGetterMock) CurrentBlockId(context.Context) (*common.BlockId, error) {
return nil, errors.New("unsupported method")
}

func (m *TimeGetterMock) BlockIdForHeight(ctx context.Context, height *common.TimeBlocks) (*common.BlockId, error) {
return m.blockIdFunc(ctx, height)
}
Expand Down
6 changes: 5 additions & 1 deletion packages/arb-tx-aggregator/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func (m *Server) GetTxInBlockAtIndexResults(res *evm.BlockInfo, index uint64) (*
}

func (m *Server) GetBlock(ctx context.Context, height uint64) (*types.Block, error) {
l1BlockInfo, err := m.Client.BlockInfoByNumber(ctx, new(big.Int).SetUint64(height))
if err != nil {
return nil, err
}
header, err := m.GetBlockHeaderByNumber(ctx, height)
if err != nil {
return nil, err
Expand All @@ -250,7 +254,7 @@ func (m *Server) GetBlock(ctx context.Context, height uint64) (*types.Block, err
transactions := make([]*types.Transaction, 0, len(results))
receipts := make([]*types.Receipt, 0, len(results))
for _, res := range results {
receipt := res.ToEthReceipt(common.NewHashFromEth(header.Hash()))
receipt := res.ToEthReceipt(common.NewHashFromEth(l1BlockInfo.Hash))
receipts = append(receipts, receipt)
tx, err := GetTransaction(res.IncomingRequest)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions packages/arb-tx-aggregator/aggregator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package aggregator
import (
"bytes"
errors2 "github.com/pkg/errors"
"math/big"
"net/http"
"strconv"

Expand Down Expand Up @@ -195,11 +196,11 @@ func (m *RPCServer) BlockHash(
args *evm.BlockHashArgs,
reply *evm.BlockHashReply,
) error {
header, err := m.srv.GetBlockHeaderByNumber(r.Context(), args.Height)
blockInfo, err := m.srv.Client.BlockInfoByNumber(r.Context(), new(big.Int).SetUint64(args.Height))
if err != nil {
return err
}
reply.Hash = hexutil.Encode(header.Hash().Bytes())
reply.Hash = hexutil.Encode(blockInfo.Hash.Bytes())
return nil
}

Expand Down
5 changes: 2 additions & 3 deletions packages/arb-tx-aggregator/cmd/arb-test-case/arb-test-case.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package main

import (
"context"
"github.com/offchainlabs/arbitrum/packages/arb-validator-core/ethutils"
"log"

"github.com/ethereum/go-ethereum/ethclient"

"github.com/offchainlabs/arbitrum/packages/arb-avm-cpp/cmachine"
"github.com/offchainlabs/arbitrum/packages/arb-util/arbos"
"github.com/offchainlabs/arbitrum/packages/arb-util/common"
Expand All @@ -42,7 +41,7 @@ func main() {
func generateTestCase(ethURL string, rollupAddress common.Address, contract string) error {
ctx := context.Background()

ethclint, err := ethclient.Dial(ethURL)
ethclint, err := ethutils.NewRPCEthClient(ethURL)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ package main
import (
"context"
"flag"
"github.com/offchainlabs/arbitrum/packages/arb-evm/message"
"github.com/offchainlabs/arbitrum/packages/arb-tx-aggregator/rpc"
"github.com/offchainlabs/arbitrum/packages/arb-validator-core/ethutils"
"log"
"os"
"path/filepath"
"time"

"github.com/ethereum/go-ethereum/ethclient"

utils2 "github.com/offchainlabs/arbitrum/packages/arb-tx-aggregator/utils"
"github.com/offchainlabs/arbitrum/packages/arb-util/common"
"github.com/offchainlabs/arbitrum/packages/arb-validator-core/arbbridge"
Expand Down Expand Up @@ -69,7 +69,7 @@ func main() {
log.Fatal(err)
}

ethclint, err := ethclient.Dial(rollupArgs.EthURL)
ethclint, err := ethutils.NewRPCEthClient(rollupArgs.EthURL)
if err != nil {
log.Fatal(err)
}
Expand All @@ -86,6 +86,8 @@ func main() {
contractFile := filepath.Join(rollupArgs.ValidatorFolder, "contract.mexe")
dbPath := filepath.Join(rollupArgs.ValidatorFolder, "checkpoint_db")

log.Println("Launching aggregator for chain", rollupArgs.Address, "with chain id", message.ChainAddressToID(rollupArgs.Address))

if err := rpc.LaunchAggregator(
context.Background(),
ethclint,
Expand Down
13 changes: 9 additions & 4 deletions packages/arb-tx-aggregator/web3/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *Server) GetBlockByHash(ctx context.Context, blockHashRaw hexutil.Bytes,
// If we can't get the header, return nil
return nil, nil
}
return s.getBlock(header, includeTxData)
return s.getBlock(header, blockHash, includeTxData)
}

func (s *Server) GetBlockByNumber(ctx context.Context, blockNum *rpc.BlockNumber, includeTxData bool) (*GetBlockResult, error) {
Expand All @@ -172,7 +172,12 @@ func (s *Server) GetBlockByNumber(ctx context.Context, blockNum *rpc.BlockNumber
// If we can't get the header, return nil
return nil, err
}
return s.getBlock(header, includeTxData)
l1BlockInfo, err := s.srv.Client.BlockInfoByNumber(ctx, new(big.Int).SetUint64(height))
if err != nil {
return nil, err
}

return s.getBlock(header, l1BlockInfo.Hash, includeTxData)
}

func (s *Server) GetTransactionByHash(txHash hexutil.Bytes) (*TransactionResult, error) {
Expand Down Expand Up @@ -315,7 +320,7 @@ func (s *Server) getTransactionByBlockAndIndex(height uint64, index hexutil.Uint
return s.makeTransactionResult(txRes)
}

func (s *Server) getBlock(header *types.Header, includeTxData bool) (*GetBlockResult, error) {
func (s *Server) getBlock(header *types.Header, blockHash common.Hash, includeTxData bool) (*GetBlockResult, error) {
block, err := s.srv.BlockInfo(header.Number.Uint64())
if err != nil {
return nil, err
Expand Down Expand Up @@ -355,7 +360,7 @@ func (s *Server) getBlock(header *types.Header, includeTxData bool) (*GetBlockRe
uncles := make([]hexutil.Bytes, 0)
return &GetBlockResult{
Number: (*hexutil.Big)(header.Number),
Hash: header.Hash().Bytes(),
Hash: blockHash.Bytes(),
ParentHash: header.ParentHash.Bytes(),
MixDigest: header.MixDigest.Bytes(),
Nonce: &header.Nonce,
Expand Down
1 change: 0 additions & 1 deletion packages/arb-validator-core/arbbridge/arbClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type MaybeBlockId struct {
}

type ChainTimeGetter interface {
CurrentBlockId(ctx context.Context) (*common.BlockId, error)
BlockIdForHeight(ctx context.Context, height *common.TimeBlocks) (*common.BlockId, error)
TimestampForBlockHash(ctx context.Context, hash common.Hash) (*big.Int, error)
}
Expand Down
38 changes: 18 additions & 20 deletions packages/arb-validator-core/ethbridge/arbClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/offchainlabs/arbitrum/packages/arb-util/common"
"github.com/offchainlabs/arbitrum/packages/arb-validator-core/arbbridge"
)
Expand Down Expand Up @@ -83,13 +82,13 @@ func (c *EthArbClient) subscribeBlockHeadersAfter(ctx context.Context, prevBlock
defer close(blockIdChan)

for {
var nextHeader *types.Header
var blockInfo *ethutils.BlockInfo
fetchErrorCount := 0
for {
var err error
targetHeight := new(big.Int).Add(prevBlockId.Height.AsInt(), big.NewInt(1))
nextHeader, err = c.client.HeaderByNumber(ctx, targetHeight)
if err == nil && nextHeader.Number.Cmp(targetHeight) == 0 {
blockInfo, err = c.client.BlockInfoByNumber(ctx, targetHeight)
if err == nil && (*big.Int)(blockInfo.Number).Cmp(targetHeight) == 0 {
// Got next header
break
}
Expand All @@ -115,13 +114,16 @@ func (c *EthArbClient) subscribeBlockHeadersAfter(ctx context.Context, prevBlock
time.Sleep(headerRetryDelay)
}

if nextHeader.ParentHash != prevBlockId.HeaderHash.ToEthHash() {
if blockInfo.ParentHash != prevBlockId.HeaderHash.ToEthHash() {
blockIdChan <- arbbridge.MaybeBlockId{Err: reorgError}
return
}

prevBlockId = getBlockID(nextHeader)
blockIdChan <- arbbridge.MaybeBlockId{BlockId: prevBlockId, Timestamp: new(big.Int).SetUint64(nextHeader.Time)}
prevBlockId = &common.BlockId{
Height: common.NewTimeBlocks((*big.Int)(blockInfo.Number)),
HeaderHash: common.NewHashFromEth(blockInfo.Hash),
}
blockIdChan <- arbbridge.MaybeBlockId{BlockId: prevBlockId, Timestamp: new(big.Int).SetUint64(uint64(blockInfo.Time))}
}
}()
return nil
Expand Down Expand Up @@ -155,23 +157,19 @@ func (c *EthArbClient) GetBalance(ctx context.Context, account common.Address) (
return c.client.BalanceAt(ctx, account.ToEthAddress(), nil)
}

func (c *EthArbClient) CurrentBlockId(ctx context.Context) (*common.BlockId, error) {
header, err := c.client.HeaderByNumber(ctx, nil)
if err != nil {
return nil, err
}
return getBlockID(header), nil
}

func (c *EthArbClient) BlockIdForHeight(ctx context.Context, height *common.TimeBlocks) (*common.BlockId, error) {
header, err := c.client.HeaderByNumber(ctx, height.AsInt())
var num *big.Int
if height != nil {
num = height.AsInt()
}
blockInfo, err := c.client.BlockInfoByNumber(ctx, num)
if err != nil {
return nil, err
}
if header == nil {
return nil, errors.New("couldn't get header at height")
}
return getBlockID(header), nil
return &common.BlockId{
Height: common.NewTimeBlocks((*big.Int)(blockInfo.Number)),
HeaderHash: common.NewHashFromEth(blockInfo.Hash),
}, nil
}

func (c *EthArbClient) TimestampForBlockHash(ctx context.Context, hash common.Hash) (*big.Int, error) {
Expand Down
7 changes: 0 additions & 7 deletions packages/arb-validator-core/ethbridge/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ func (a ArbAddresses) ArbFactoryAddress() common.Address {
return common.NewAddressFromEth(ethcommon.HexToAddress(a.ArbFactory))
}

func getBlockID(header *types.Header) *common.BlockId {
return &common.BlockId{
Height: common.NewTimeBlocks(header.Number),
HeaderHash: common.NewHashFromEth(header.Hash()),
}
}

func getLogBlockID(ethLog types.Log) *common.BlockId {
return &common.BlockId{
Height: common.NewTimeBlocks(new(big.Int).SetUint64(ethLog.BlockNumber)),
Expand Down
73 changes: 73 additions & 0 deletions packages/arb-validator-core/ethutils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package ethutils

import (
"context"
"encoding/json"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"math/big"

"github.com/ethereum/go-ethereum"
Expand All @@ -31,8 +36,76 @@ type EthClient interface {
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)

HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
BlockInfoByNumber(ctx context.Context, number *big.Int) (*BlockInfo, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error)
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
}

type RPCEthClient struct {
*ethclient.Client
rpc *rpc.Client
}

type BlockInfo struct {
Hash common.Hash `json:"hash"`
ParentHash common.Hash `json:"parentHash"`
Time hexutil.Uint64 `json:"timestamp"`
Number *hexutil.Big `json:"number"`
}

func NewRPCEthClient(url string) (*RPCEthClient, error) {
ethcl, err := ethclient.Dial(url)
if err != nil {
return nil, err
}

rpccl, err := rpc.Dial(url)
if err != nil {
return nil, err
}

return &RPCEthClient{
Client: ethcl,
rpc: rpccl,
}, nil
}

func (r *RPCEthClient) BlockInfoByNumber(ctx context.Context, number *big.Int) (*BlockInfo, error) {
var raw json.RawMessage
var numParam string
if number != nil {
numParam = hexutil.EncodeBig(number)
} else {
numParam = "latest"
}
if err := r.rpc.CallContext(ctx, &raw, "eth_getBlockByNumber", numParam, false); err != nil {
return nil, err
}
if len(raw) == 0 {
return nil, ethereum.NotFound
}
var ret BlockInfo
if err := json.Unmarshal(raw, &ret); err != nil {
return nil, err
}
return &ret, nil
}

type SimulatedEthClient struct {
*backends.SimulatedBackend
}

func (r *SimulatedEthClient) BlockInfoByNumber(ctx context.Context, number *big.Int) (*BlockInfo, error) {
header, err := r.SimulatedBackend.HeaderByNumber(ctx, number)
if err != nil {
return nil, err
}
return &BlockInfo{
Hash: header.Hash(),
ParentHash: header.ParentHash,
Time: hexutil.Uint64(header.Time),
Number: (*hexutil.Big)(header.Number),
}, nil
}
2 changes: 1 addition & 1 deletion packages/arb-validator-core/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func CalculateCatchupFetch(ctx context.Context, start *big.Int, clnt arbbridge.ChainTimeGetter, maxReorg *big.Int) (*big.Int, error) {
currentLocalHeight := start
currentOnChain, err := clnt.CurrentBlockId(ctx)
currentOnChain, err := clnt.BlockIdForHeight(ctx, nil)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (lis *ValidatorChainListener) AssertionPrepared(
continue
}
lis.Lock()
currentTime, err := stakingKey.client.CurrentBlockId(ctx)
currentTime, err := stakingKey.client.BlockIdForHeight(ctx, nil)
if err != nil {
log.Println("Validator couldn't get time")
break
Expand Down
2 changes: 1 addition & 1 deletion packages/arb-validator/chainobserver/chainObserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (chain *ChainObserver) HandleNotification(ctx context.Context, event arbbri
}

func (chain *ChainObserver) UpdateAssumedValidBlock(ctx context.Context, clnt arbbridge.ChainTimeGetter, assumedValidDepth int64) error {
latestL1BlockId, err := clnt.CurrentBlockId(ctx)
latestL1BlockId, err := clnt.BlockIdForHeight(ctx, nil)
if err != nil {
return errors2.Wrap(err, "Getting current block header")
}
Expand Down
Loading

0 comments on commit 4d99801

Please sign in to comment.