Commit 4d1c917e authored by vicotor's avatar vicotor

fix bug for genesis

parent 1b908768
{
"0x905D5E8F7db76bCA91fdcA0990be7263dfD23335": {
"signerProxy": "0x",
"assets": {
"usdc": {
"balance": "0x20000000000",
"frozen": "0x10"
}
}
}
}
...@@ -116,8 +116,8 @@ func LoadGenesisAllocs(allocsPath string) (GenesisAlloc, error) { ...@@ -116,8 +116,8 @@ func LoadGenesisAllocs(allocsPath string) (GenesisAlloc, error) {
} }
defer f.Close() defer f.Close()
var out = make(GenesisAlloc) var out = make(GenesisAlloc)
if err := json.NewDecoder(f).Decode(out); err != nil { if err := json.NewDecoder(f).Decode(&out); err != nil {
return nil, fmt.Errorf("failed to json-decode forge allocs %q: %w", allocsPath, err) return nil, fmt.Errorf("failed to json-decode genesis allocs %q: %w", allocsPath, err)
} }
return out, nil return out, nil
} }
...@@ -62,3 +62,14 @@ func TestGenesisBlock_ToBlock(t *testing.T) { ...@@ -62,3 +62,14 @@ func TestGenesisBlock_ToBlock(t *testing.T) {
block := gen.ToBlock() block := gen.ToBlock()
fmt.Println("genesis block is ", block.String()) fmt.Println("genesis block is ", block.String())
} }
func TestLoadGenesisAllocs(t *testing.T) {
genesis, err := LoadGenesisAllocs("alloc.json")
if err != nil {
t.Fatal("load genesis allocs failed", "err", err)
}
for addr, info := range genesis {
data, _ := info.MarshalJSON()
fmt.Printf("user[%s]={%s}\n", addr.String(), string(data))
}
}
...@@ -169,7 +169,7 @@ var Subcommands = cli.Commands{ ...@@ -169,7 +169,7 @@ var Subcommands = cli.Commands{
} }
config.SetDeployments(deployments) config.SetDeployments(deployments)
var l2Allocs chaingenesis.GenesisAlloc var l2Allocs = make(chaingenesis.GenesisAlloc)
if l2AllocsPath := ctx.String(l2AllocsFlag.Name); l2AllocsPath != "" { if l2AllocsPath := ctx.String(l2AllocsFlag.Name); l2AllocsPath != "" {
l2Allocs, err = chaingenesis.LoadGenesisAllocs(l2AllocsPath) l2Allocs, err = chaingenesis.LoadGenesisAllocs(l2AllocsPath)
if err != nil { if err != nil {
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
...@@ -308,6 +309,18 @@ func L1BlockInfoFromBytes(rollupCfg *rollup.Config, l2BlockTime uint64, data []b ...@@ -308,6 +309,18 @@ func L1BlockInfoFromBytes(rollupCfg *rollup.Config, l2BlockTime uint64, data []b
return &info, info.unmarshalBinaryBedrock(data) return &info, info.unmarshalBinaryBedrock(data)
} }
// L1BlockInfoFromBytes is the inverse of L1InfoDeposit, to see where the L2 chain is derived from
func L1BlockInfoFromNebula(rollupCfg *rollup.Config, l2BlockTime uint64, tx *nebulav1.ProtocolTransaction) (*L1BlockInfo, error) {
info := &L1BlockInfo{
Number: tx.L1Number,
Time: tx.L1Time,
BlockHash: common.BytesToHash(tx.L1BlockHash),
SequenceNumber: tx.SequenceNumber,
BatcherAddr: common.BytesToAddress(tx.BatcherAddr),
}
return info, nil
}
// L1InfoDeposit creates a L1 Info deposit transaction based on the L1 block, // L1InfoDeposit creates a L1 Info deposit transaction based on the L1 block,
// and the L2 block-height difference with the start of the epoch. // and the L2 block-height difference with the start of the epoch.
func L1InfoDeposit(rollupCfg *rollup.Config, sysCfg eth.SystemConfig, seqNumber uint64, block eth.BlockInfo, l2Timestamp uint64) (*types.DepositTx, error) { func L1InfoDeposit(rollupCfg *rollup.Config, sysCfg eth.SystemConfig, seqNumber uint64, block eth.BlockInfo, l2Timestamp uint64) (*types.DepositTx, error) {
......
...@@ -3,6 +3,7 @@ package derive ...@@ -3,6 +3,7 @@ package derive
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"github.com/exchain/go-exchain/op-node/rollup" "github.com/exchain/go-exchain/op-node/rollup"
"github.com/exchain/go-exchain/op-service/eth" "github.com/exchain/go-exchain/op-service/eth"
) )
...@@ -20,22 +21,22 @@ func PayloadToBlockRef(rollupCfg *rollup.Config, payload *eth.ExecutionPayload) ...@@ -20,22 +21,22 @@ func PayloadToBlockRef(rollupCfg *rollup.Config, payload *eth.ExecutionPayload)
l1Origin = genesis.L1 l1Origin = genesis.L1
sequenceNumber = 0 sequenceNumber = 0
} else { } else {
// todo: vicotor implement this. txs := payload.Transactions()
//if len(payload.Transactions) == 0 { if len(txs) == 0 {
// return eth.L2BlockRef{}, fmt.Errorf("l2 block is missing L1 info deposit tx, block hash: %s", payload.BlockHash) return eth.L2BlockRef{}, fmt.Errorf("l2 block is missing L1 info, block hash: %s", payload.BlockHash)
//} }
//var tx types.Transaction tx := txs[0]
//if err := tx.UnmarshalBinary(payload.Transactions[0]); err != nil { if tx.TxType != nebulav1.TxType_ProtocolTx {
// return eth.L2BlockRef{}, fmt.Errorf("failed to decode first tx to read l1 info from: %w", err) return eth.L2BlockRef{}, fmt.Errorf("first payload tx has unexpected tx type: %d", tx.TxType)
//} }
//if tx.Type() != types.DepositTxType { content := tx.GetProtocolTx()
// return eth.L2BlockRef{}, fmt.Errorf("first payload tx has unexpected tx type: %d", tx.Type()) if content == nil {
//} return eth.L2BlockRef{}, fmt.Errorf("first payload tx has unexpected tx type: %d", tx.TxType)
info, err := L1BlockInfoFromBytes(rollupCfg, uint64(payload.Timestamp), tx.Data()) }
//if err != nil { info, err := L1BlockInfoFromNebula(rollupCfg, uint64(payload.Timestamp), content)
// return eth.L2BlockRef{}, fmt.Errorf("failed to parse L1 info deposit tx from L2 block: %w", err) if err != nil {
//} return eth.L2BlockRef{}, fmt.Errorf("failed to parse L1 info deposit tx from L2 block: %w", err)
//l1Origin = eth.BlockID{Hash: info.BlockHash, Number: info.Number} }
l1Origin = eth.BlockID{Hash: info.BlockHash, Number: info.Number} l1Origin = eth.BlockID{Hash: info.BlockHash, Number: info.Number}
sequenceNumber = info.SequenceNumber sequenceNumber = info.SequenceNumber
} }
...@@ -59,9 +60,20 @@ func PayloadToSystemConfig(rollupCfg *rollup.Config, payload *eth.ExecutionPaylo ...@@ -59,9 +60,20 @@ func PayloadToSystemConfig(rollupCfg *rollup.Config, payload *eth.ExecutionPaylo
} }
return rollupCfg.Genesis.SystemConfig, nil return rollupCfg.Genesis.SystemConfig, nil
} }
// todo: vicotor implement this. txs := payload.Transactions()
data := []byte{} if len(txs) == 0 {
info, err := L1BlockInfoFromBytes(rollupCfg, uint64(payload.Timestamp), data) return eth.SystemConfig{}, fmt.Errorf("l2 block is missing L1 info, block hash: %s", payload.BlockHash)
}
tx := txs[0]
if tx.TxType != nebulav1.TxType_ProtocolTx {
return eth.SystemConfig{}, fmt.Errorf("first payload tx has unexpected tx type: %d", tx.TxType)
}
content := tx.GetProtocolTx()
if content == nil {
return eth.SystemConfig{}, fmt.Errorf("first payload tx has unexpected tx type: %d", tx.TxType)
}
info, err := L1BlockInfoFromNebula(rollupCfg, uint64(payload.Timestamp), content)
//info, err := L1BlockInfoFromBytes(rollupCfg, uint64(payload.Timestamp), data)
if err != nil { if err != nil {
return eth.SystemConfig{}, fmt.Errorf("failed to parse L1 info deposit tx from L2 block: %w", err) return eth.SystemConfig{}, fmt.Errorf("failed to parse L1 info deposit tx from L2 block: %w", err)
} }
......
...@@ -37,11 +37,11 @@ func (eq *EngDeriver) onBuildStart(ev BuildStartEvent) { ...@@ -37,11 +37,11 @@ func (eq *EngDeriver) onBuildStart(ev BuildStartEvent) {
eq.emitter.Emit(rollup.CriticalErrorEvent{Err: err}) // make the node exit, things are very wrong. eq.emitter.Emit(rollup.CriticalErrorEvent{Err: err}) // make the node exit, things are very wrong.
return return
} }
//fc := eth.ForkchoiceState{ fc := eth.ForkchoiceState{
// HeadBlockHash: fcEvent.UnsafeL2Head.Hash, HeadBlockHash: fcEvent.UnsafeL2Head.Hash,
// SafeBlockHash: fcEvent.SafeL2Head.Hash, SafeBlockHash: fcEvent.SafeL2Head.Hash,
// FinalizedBlockHash: fcEvent.FinalizedL2Head.Hash, FinalizedBlockHash: fcEvent.FinalizedL2Head.Hash,
//} }
buildStartTime := time.Now() buildStartTime := time.Now()
result, err := startPayload(context.TODO(), eq.ec.engine, fc, ev.Attributes.Attributes) result, err := startPayload(context.TODO(), eq.ec.engine, fc, ev.Attributes.Attributes)
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment