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

Fix and clean unit test #313

Merged
merged 7 commits into from
May 21, 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
4 changes: 2 additions & 2 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
go-version: 1.22.x
- name: Run build
run: make build
# - name: Run tests
# run: make test
- name: Run tests
run: make test
13 changes: 0 additions & 13 deletions node/derivation/batch_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (bi *BatchInfo) TxNum() uint64 {

// ParseBatch This method is externally referenced for parsing Batch
func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch) error {
//var rollupData BatchInfo
bi.root = batch.PostStateRoot
bi.withdrawalRoot = batch.WithdrawRoot
bi.skippedL1MessageBitmap = new(big.Int).SetBytes(batch.SkippedL1MessageBitmap[:])
Expand Down Expand Up @@ -156,18 +155,6 @@ func encodeTransactions(txs []*eth.Transaction) [][]byte {
return enc
}

func decodeTransactions(txsBytes [][]byte) []*eth.Transaction {
var txs []*eth.Transaction
for _, txByte := range txsBytes {
var tx eth.Transaction
if err := tx.UnmarshalBinary(txByte); err != nil {
panic(err)
}
txs = append(txs, &tx)
}
return txs
}

type txQueue struct {
txs eth.Transactions
pointer int
Expand Down
97 changes: 0 additions & 97 deletions node/derivation/derivation_test.go
Original file line number Diff line number Diff line change
@@ -1,107 +1,15 @@
package derivation

import (
"context"
"crypto/ecdsa"
"math/big"
"os"
"reflect"
"strings"
"testing"

"github.com/scroll-tech/go-ethereum/accounts/abi/bind"
"github.com/scroll-tech/go-ethereum/accounts/abi/bind/backends"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/common/hexutil"
"github.com/scroll-tech/go-ethereum/core"
"github.com/scroll-tech/go-ethereum/core/rawdb"
"github.com/scroll-tech/go-ethereum/eth"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/ethclient/authclient"
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/require"
tmlog "github.com/tendermint/tendermint/libs/log"

"morph-l2/bindings/bindings"
"morph-l2/bindings/predeploys"
"morph-l2/node/types"
)

func TestCompareBlock(t *testing.T) {
eClient, err := ethclient.Dial("http://localhost:7545")
require.NoError(t, err)
l2Client, err := ethclient.Dial("http://localhost:8545")
blockNumber, err := eClient.BlockNumber(context.Background())
require.NoError(t, err)
for i := 0; i < int(blockNumber); i++ {
block, err := l2Client.BlockByNumber(context.Background(), big.NewInt(int64(i)))
require.NoError(t, err)
dBlock, err := eClient.BlockByNumber(context.Background(), big.NewInt(int64(i)))
require.True(t, reflect.DeepEqual(block.Header(), dBlock.Header()))
}
}

func testNewDerivationClient(t *testing.T) *Derivation {
ctx := context.Background()
l1Client, err := ethclient.Dial("http://localhost:9545")
addr := common.HexToAddress("0x6900000000000000000000000000000000000010")
require.NoError(t, err)
var secret [32]byte
jwtSecret := common.FromHex(strings.TrimSpace("688f5d737bad920bdfb2fc2f488d6b6209eebda1dae949a8de91398d932c517a"))
require.True(t, len(jwtSecret) == 32)
copy(secret[:], jwtSecret)
aClient, err := authclient.DialContext(context.Background(), "http://localhost:7551", secret)
require.NoError(t, err)
eClient, err := ethclient.Dial("http://localhost:7545")
require.NoError(t, err)
msgPasser, err := bindings.NewL2ToL1MessagePasser(predeploys.L2ToL1MessagePasserAddr, eClient)
require.NoError(t, err)
logger := tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout))
baseHttp := NewBasicHTTPClient("http://localhost:3500", logger)
l1BeaconClient := NewL1BeaconClient(baseHttp)
d := Derivation{
ctx: ctx,
l1Client: l1Client,
RollupContractAddress: addr,
confirmations: rpc.BlockNumber(5),
l2Client: types.NewRetryableClient(aClient, eClient, tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout))),
validator: nil,
latestDerivation: 9,
fetchBlockRange: 100,
pollInterval: 1,
l1BeaconClient: l1BeaconClient,
L2ToL1MessagePasser: msgPasser,
}
return &d
}

func TestFetchRollupData(t *testing.T) {
d := testNewDerivationClient(t)
_, err := d.fetchRollupDataByTxHash(common.HexToHash("0xf2eaf0c8c121751a544046bc5358db521dc0b8dce660c674ad64c8f1a2a12c1b"), 390)
require.NoError(t, err)
}

func TestFetchWithdrawalRoot(t *testing.T) {
d := testNewDerivationClient(t)
bn, err := d.l2Client.BlockNumber(context.Background())
require.NoError(t, err)
_, err = d.L2ToL1MessagePasser.MessageRoot(&bind.CallOpts{
BlockNumber: big.NewInt(int64(bn)),
})
require.NoError(t, err)
}

func newSimulatedBackend(key *ecdsa.PrivateKey) (*backends.SimulatedBackend, ethdb.Database) {
var gasLimit uint64 = 9_000_000
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
genAlloc := make(core.GenesisAlloc)
genAlloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(9223372036854775807)}
db := rawdb.NewMemoryDatabase()
sim := backends.NewSimulatedBackendWithDatabase(db, genAlloc, gasLimit)
return sim, db
}

func TestDecodeBatch(t *testing.T) {
abi, err := bindings.RollupMetaData.GetAbi()
require.NoError(t, err)
Expand All @@ -118,11 +26,6 @@ func TestDecodeBatch(t *testing.T) {
PrevStateRoot [32]uint8 "json:\"prevStateRoot\""
PostStateRoot [32]uint8 "json:\"postStateRoot\""
WithdrawalRoot [32]uint8 "json:\"withdrawalRoot\""
Signature struct {
Version *big.Int "json:\"version\""
Signers []*big.Int "json:\"signers\""
Signature []uint8 "json:\"signature\""
} "json:\"signature\""
})

var chunks []hexutil.Bytes
Expand Down
37 changes: 0 additions & 37 deletions node/sync/deposit_log.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package sync

import (
"errors"
"fmt"
"math/big"

"github.com/hashicorp/go-multierror"
"github.com/scroll-tech/go-ethereum/common"
eth "github.com/scroll-tech/go-ethereum/core/types"
Expand Down Expand Up @@ -58,37 +55,3 @@ func (c *BridgeClient) deriveFromReceipt(receipts []*eth.Receipt) ([]types.L1Mes
}
return out, result
}

type relayMessageData struct {
nonce *big.Int
sender common.Address
target common.Address
value *big.Int
minGasLimit *big.Int
message []byte
}

func unpackRelayMessage(data []byte) (*relayMessageData, error) {
abi := L2CrossDomainMessengerABI
method, ok := abi.Methods["relayMessage"]
if !ok {
return nil, errors.New("can not find the method of relayMessage")
}
args := method.Inputs
unpacked, err := args.Unpack(data[4:])
if err != nil {
return nil, err
}
if len(unpacked) != 6 {
return nil, errors.New("wrong unpacked value length")
}

relayMessage := new(relayMessageData)
relayMessage.nonce = unpacked[0].(*big.Int)
relayMessage.sender = unpacked[1].(common.Address)
relayMessage.target = unpacked[2].(common.Address)
relayMessage.value = unpacked[3].(*big.Int)
relayMessage.minGasLimit = unpacked[4].(*big.Int)
relayMessage.message = unpacked[5].([]byte)
return relayMessage, nil
}
35 changes: 0 additions & 35 deletions node/sync/deposit_log_test.go

This file was deleted.

10 changes: 1 addition & 9 deletions node/types/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@ import (
)

func TestBlobFromSealedTxPayload(t *testing.T) {
sealedTxPayload := rand.Bytes(30)
sealedTxPayload := rand.Bytes(31)
_, err := MakeBlobCanonical(sealedTxPayload)
require.Error(t, err)

sealedTxPayload = rand.Bytes(MaxBlobBytesSize + 1)
_, err = MakeBlobCanonical(sealedTxPayload)
require.Error(t, err)

sealedTxPayload = rand.Bytes(31)
_, err = MakeBlobCanonical(sealedTxPayload)
require.NoError(t, err)

sealedTxPayload = rand.Bytes(MaxBlobBytesSize)
Expand Down
2 changes: 1 addition & 1 deletion node/types/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestChunks_ConstructBlobPayload(t *testing.T) {
blobBytes := chunks.ConstructBlobPayload()

expectedBytes := make([]byte, 62+50)
copy(expectedBytes, []byte{0x0, 0x2})
copy(expectedBytes, []byte{0x0, 0x3})
chunk0Size := make([]byte, 4)
binary.BigEndian.PutUint32(chunk0Size, 20)
chunk2Size := make([]byte, 4)
Expand Down
11 changes: 6 additions & 5 deletions node/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ func TestValidator_ChallengeState(t *testing.T) {
require.NoError(t, err)
sim.Commit()
v := Validator{
cli: sim,
privateKey: key,
l1ChainID: big.NewInt(1),
contract: rollup,
cli: sim,
privateKey: key,
l1ChainID: big.NewInt(1),
contract: rollup,
challengeEnable: true,
}
err = v.ChallengeState(10)
log.Info("addr:", addr)
require.EqualError(t, err, "execution reverted: Batch not exist")
require.EqualError(t, err, "execution reverted: caller challenger allowed")
}

func newSimulatedBackend(key *ecdsa.PrivateKey) (*backends.SimulatedBackend, ethdb.Database) {
Expand Down
Loading