Commit 17494c8a authored by vicotor's avatar vicotor

update block

parent c26224df
......@@ -7,6 +7,7 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/exchain/go-exchain/exchain"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"github.com/exchain/go-exchain/exchain/wrapper"
"github.com/exchain/go-exchain/metadb"
"github.com/exchain/go-exchain/metadb/memdb"
"github.com/exchain/go-exchain/op-service/grouptask"
......@@ -72,7 +73,7 @@ func NewChainDB(database metadb.Database) ChainDB {
}
type chainData struct {
block *nebulav1.Block
block *wrapper.BlkWrapper
receipts *nebulav1.TransactionReceiptList
}
......@@ -245,11 +246,12 @@ func (m *chaindb) chainDataSaveTask() {
// 6. save block accounts
// 7. save chain height
block := data.block
log.Infof("save block with block number %d", block.Header.Height)
blockHash := common.BytesToHash(block.Header.Hash)
blockHeight := uint256.NewInt(block.Header.Height)
header := block.Header
log.Infof("save block with block number %d", block.Height())
blockHash := block.Hash()
blockHeight := uint256.NewInt(block.Height())
header := block.Header()
{
t1 := time.Now()
// save blockHash -> number.Bytes()
......@@ -281,7 +283,7 @@ func (m *chaindb) chainDataSaveTask() {
t1 := time.Now()
// save block body
bodyk := blockBodyKey(blockHeight)
dbody, err := proto.Marshal(block)
dbody, err := proto.Marshal(block.Block())
if err != nil {
log.Error("save block body", "err", err)
panic(fmt.Sprintf("save block body with err %v", err))
......@@ -324,7 +326,7 @@ func (m *chaindb) chainDataSaveTask() {
}
t1 := time.Now()
batch := m.database.NewBatch()
number := block.Header.Height
number := block.Height()
{
sequence := func(receipts []*nebulav1.TransactionReceipt) []interface{} {
s := make([]interface{}, len(receipts))
......@@ -466,18 +468,19 @@ func (m *chaindb) cacheBlockHeader(header *nebulav1.BlockHeader) error {
return nil
}
func (m *chaindb) cacheBlock(block *nebulav1.Block) error {
k := blockBodyKey(uint256.NewInt(block.Header.Height))
m.cache.Set(k, block)
func (m *chaindb) cacheBlock(wBlk *wrapper.BlkWrapper) error {
k := blockBodyKey(uint256.NewInt(wBlk.Height()))
m.cache.Set(k, wBlk.Block())
return nil
}
func (m *chaindb) cacheBlockNumber(block *nebulav1.Block) error {
k := blockNumKey(common.BytesToHash(block.Header.Hash))
number := uint256.NewInt(block.Header.Height)
func (m *chaindb) cacheBlockNumber(wBlk *wrapper.BlkWrapper) error {
k := blockNumKey(wBlk.Hash())
number := uint256.NewInt(wBlk.Height())
m.cache.Set(k, number)
return nil
}
func (m *chaindb) cacheBlockTxsInfo(txs *nebulav1.TransactionList, rs *nebulav1.TransactionReceiptList) error {
if txs == nil || rs == nil {
return nil
......@@ -572,8 +575,10 @@ func (m *chaindb) SaveBlockHeader(header *nebulav1.BlockHeader) error {
}
func (m *chaindb) SaveBlockData(block *nebulav1.Block, rs *nebulav1.TransactionReceiptList) error {
wblk := wrapper.NewBlkWrapper(block)
m.cacheBlockHeader(block.Header)
m.cacheBlock(block)
m.cacheBlock(wblk)
m.cacheBlockNumber(wblk)
wg := sync.WaitGroup{}
......@@ -589,7 +594,7 @@ func (m *chaindb) SaveBlockData(block *nebulav1.Block, rs *nebulav1.TransactionR
wg.Wait()
data := chainData{
block: block,
block: wblk,
receipts: rs,
}
m.toStoreChainData(data)
......
......@@ -14,15 +14,14 @@ type Engine interface {
}
type PayloadParams struct {
ParentRoot *common.Hash
Timestamp uint64
Proposer common.Address
L1Info L1BlockInfo
Transactions nebulav1.TransactionList
Signer Signer
}
type ExecutionResult struct {
ParentRoot common.Hash
Payload *nebulav1.Block
Receipts nebulav1.TransactionReceiptList
Payload *nebulav1.Block
Receipts nebulav1.TransactionReceiptList
}
......@@ -65,19 +65,20 @@ func (g *GenesisBlock) Commit(db metadb.Database) (err error) {
func (g *GenesisBlock) ToBlock() *nebulav1.Block {
blk := &nebulav1.Block{
Header: &nebulav1.BlockHeader{
Height: 0,
ParentHash: common.Hash{}.Bytes(),
Timestamp: g.Timestamp,
Proposer: genesisSigner.Bytes(),
L1Hash: common.Hash{}.Bytes(),
AppRoot: common.Hash{}.Bytes(),
Height: 0,
ParentHash: common.Hash{}.Bytes(),
Timestamp: g.Timestamp,
Proposer: genesisSigner.Bytes(),
AppRoot: common.Hash{}.Bytes(),
L1Hash: common.Hash{}.Bytes(),
L1Height: 0,
SequenceNumber: 0,
},
Transactions: &nebulav1.TransactionList{},
}
hash := BlockHash(blk)
signature, _ := crypto.Sign(hash.Bytes(), genesisSignerKey)
blk.Header.Signatures = signature
blk.Header.Hash = hash.Bytes()
blk.Header.Signature = signature
return blk
}
......@@ -102,8 +103,9 @@ func BlockHash(blk *nebulav1.Block) common.Hash {
data := make([]byte, 0)
data = append(data, uint256.NewInt(blk.Header.Height).Bytes()...)
data = append(data, uint256.NewInt(blk.Header.Timestamp).Bytes()...)
data = append(data, blk.Header.ParentHash...)
data = append(data, blk.Header.L1Hash...)
data = append(data, uint256.NewInt(blk.Header.L1Height).Bytes()...)
data = append(data, blk.Header.ParentHash...)
data = append(data, blk.Header.AppRoot...)
data = append(data, blk.Header.Proposer...)
txdata, _ := proto.Marshal(blk.Transactions)
......
package exchain
import (
"github.com/ethereum/go-ethereum/common"
)
// L1BlockInfo presents the information stored in a L1Block.setL1BlockValues call
type L1BlockInfo struct {
Number uint64
Time uint64
BlockHash common.Hash
// Not strictly a piece of L1 information. Represents the number of L2 blocks since the start of the epoch,
// i.e. when the actual L1 info was first introduced.
SequenceNumber uint64
// BatcherAddr version 0 is just the address with 0 padding to the left.
BatcherAddr common.Address
}
......@@ -2,10 +2,10 @@ package mockengine
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/exchain/go-exchain/exchain"
"github.com/exchain/go-exchain/exchain/chaindb"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"github.com/exchain/go-exchain/exchain/wrapper"
log "github.com/sirupsen/logrus"
)
......@@ -22,13 +22,14 @@ func (m MockEngine) NewPayload(params exchain.PayloadParams) (exchain.ExecutionR
if err != nil {
return exchain.ExecutionResult{}, err
}
root := params.ParentRoot
wParent := wrapper.NewBlkWrapper(parent)
header := &nebulav1.BlockHeader{
Height: parent.Header.Height + 1,
ParentHash: parent.Header.Hash,
ParentHash: wParent.Hash().Bytes(),
Timestamp: params.Timestamp,
L1Hash: params.ParentRoot.Bytes(),
Proposer: params.Proposer.Bytes(),
L1Hash: params.L1Info.BlockHash.Bytes(),
L1Height: params.L1Info.Number,
AppRoot: make([]byte, 0),
}
receipts, err := m.ProcessTx(header, params.Transactions)
......@@ -38,7 +39,6 @@ func (m MockEngine) NewPayload(params exchain.PayloadParams) (exchain.ExecutionR
}
result := exchain.ExecutionResult{
ParentRoot: *root,
Payload: &nebulav1.Block{
Header: header,
Transactions: &params.Transactions,
......@@ -62,9 +62,8 @@ func (m MockEngine) ProcessPayload(block *nebulav1.Block) (exchain.ExecutionResu
}
return exchain.ExecutionResult{
ParentRoot: common.BytesToHash(block.Header.L1Hash),
Payload: block,
Receipts: receipts,
Payload: block,
Receipts: receipts,
}, nil
}
......
......@@ -27,14 +27,15 @@ type BlockHeader struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Height uint64 `protobuf:"varint,1,opt,name=height,proto3" json:"height,omitempty"`
Hash []byte `protobuf:"bytes,2,opt,name=hash,proto3" json:"hash,omitempty"`
ParentHash []byte `protobuf:"bytes,3,opt,name=parent_hash,json=parentHash,proto3" json:"parent_hash,omitempty"`
Timestamp uint64 `protobuf:"varint,4,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
L1Hash []byte `protobuf:"bytes,5,opt,name=l1_hash,json=l1Hash,proto3" json:"l1_hash,omitempty"`
Proposer []byte `protobuf:"bytes,6,opt,name=proposer,proto3" json:"proposer,omitempty"`
AppRoot []byte `protobuf:"bytes,7,opt,name=app_root,json=appRoot,proto3" json:"app_root,omitempty"` // used to verify block is valid.
Signatures []byte `protobuf:"bytes,8,opt,name=signatures,proto3" json:"signatures,omitempty"` // used to verify block is mint by proposer.
Height uint64 `protobuf:"varint,1,opt,name=height,proto3" json:"height,omitempty"`
ParentHash []byte `protobuf:"bytes,2,opt,name=parent_hash,json=parentHash,proto3" json:"parent_hash,omitempty"`
Timestamp uint64 `protobuf:"varint,3,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
L1Height uint64 `protobuf:"varint,4,opt,name=l1_height,json=l1Height,proto3" json:"l1_height,omitempty"`
L1Hash []byte `protobuf:"bytes,5,opt,name=l1_hash,json=l1Hash,proto3" json:"l1_hash,omitempty"`
SequenceNumber uint64 `protobuf:"varint,6,opt,name=sequence_number,json=sequenceNumber,proto3" json:"sequence_number,omitempty"`
Proposer []byte `protobuf:"bytes,7,opt,name=proposer,proto3" json:"proposer,omitempty"`
AppRoot []byte `protobuf:"bytes,8,opt,name=app_root,json=appRoot,proto3" json:"app_root,omitempty"` // used to verify block is valid.
Signature []byte `protobuf:"bytes,9,opt,name=signature,proto3" json:"signature,omitempty"` // used to verify block is mint by proposer.
}
func (x *BlockHeader) Reset() {
......@@ -76,13 +77,6 @@ func (x *BlockHeader) GetHeight() uint64 {
return 0
}
func (x *BlockHeader) GetHash() []byte {
if x != nil {
return x.Hash
}
return nil
}
func (x *BlockHeader) GetParentHash() []byte {
if x != nil {
return x.ParentHash
......@@ -97,6 +91,13 @@ func (x *BlockHeader) GetTimestamp() uint64 {
return 0
}
func (x *BlockHeader) GetL1Height() uint64 {
if x != nil {
return x.L1Height
}
return 0
}
func (x *BlockHeader) GetL1Hash() []byte {
if x != nil {
return x.L1Hash
......@@ -104,6 +105,13 @@ func (x *BlockHeader) GetL1Hash() []byte {
return nil
}
func (x *BlockHeader) GetSequenceNumber() uint64 {
if x != nil {
return x.SequenceNumber
}
return 0
}
func (x *BlockHeader) GetProposer() []byte {
if x != nil {
return x.Proposer
......@@ -118,9 +126,9 @@ func (x *BlockHeader) GetAppRoot() []byte {
return nil
}
func (x *BlockHeader) GetSignatures() []byte {
func (x *BlockHeader) GetSignature() []byte {
if x != nil {
return x.Signatures
return x.Signature
}
return nil
}
......@@ -191,21 +199,24 @@ var file_nebula_v1_block_proto_rawDesc = []byte{
0x62, 0x75, 0x6c, 0x61, 0x2f, 0x65, 0x78, 0x74, 0x2f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1b, 0x6e, 0x65, 0x62, 0x75, 0x6c, 0x61, 0x2f, 0x76,
0x31, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x22, 0xe8, 0x01, 0x0a, 0x0b, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x48, 0x65, 0x61,
0x6f, 0x74, 0x6f, 0x22, 0x98, 0x02, 0x0a, 0x0b, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x48, 0x65, 0x61,
0x64, 0x65, 0x72, 0x12, 0x16, 0x0a, 0x06, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x18, 0x01, 0x20,
0x01, 0x28, 0x04, 0x52, 0x06, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x68,
0x61, 0x73, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x68, 0x61, 0x73, 0x68, 0x12,
0x1f, 0x0a, 0x0b, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x68, 0x61, 0x73, 0x68, 0x18, 0x03,
0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x48, 0x61, 0x73, 0x68,
0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x04, 0x20,
0x01, 0x28, 0x04, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x12, 0x17,
0x0a, 0x07, 0x6c, 0x31, 0x5f, 0x68, 0x61, 0x73, 0x68, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52,
0x06, 0x6c, 0x31, 0x48, 0x61, 0x73, 0x68, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x72, 0x6f, 0x70, 0x6f,
0x73, 0x65, 0x72, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, 0x70, 0x72, 0x6f, 0x70, 0x6f,
0x73, 0x65, 0x72, 0x12, 0x19, 0x0a, 0x08, 0x61, 0x70, 0x70, 0x5f, 0x72, 0x6f, 0x6f, 0x74, 0x18,
0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x61, 0x70, 0x70, 0x52, 0x6f, 0x6f, 0x74, 0x12, 0x1e,
0x0a, 0x0a, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x18, 0x08, 0x20, 0x01,
0x28, 0x0c, 0x52, 0x0a, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x22, 0x87,
0x01, 0x28, 0x04, 0x52, 0x06, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x12, 0x1f, 0x0a, 0x0b, 0x70,
0x61, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x68, 0x61, 0x73, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c,
0x52, 0x0a, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x48, 0x61, 0x73, 0x68, 0x12, 0x1c, 0x0a, 0x09,
0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52,
0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x12, 0x1b, 0x0a, 0x09, 0x6c, 0x31,
0x5f, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, 0x6c,
0x31, 0x48, 0x65, 0x69, 0x67, 0x68, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x6c, 0x31, 0x5f, 0x68, 0x61,
0x73, 0x68, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x6c, 0x31, 0x48, 0x61, 0x73, 0x68,
0x12, 0x27, 0x0a, 0x0f, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x65, 0x5f, 0x6e, 0x75, 0x6d,
0x62, 0x65, 0x72, 0x18, 0x06, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0e, 0x73, 0x65, 0x71, 0x75, 0x65,
0x6e, 0x63, 0x65, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x72, 0x6f,
0x70, 0x6f, 0x73, 0x65, 0x72, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, 0x70, 0x72, 0x6f,
0x70, 0x6f, 0x73, 0x65, 0x72, 0x12, 0x19, 0x0a, 0x08, 0x61, 0x70, 0x70, 0x5f, 0x72, 0x6f, 0x6f,
0x74, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x61, 0x70, 0x70, 0x52, 0x6f, 0x6f, 0x74,
0x12, 0x1c, 0x0a, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x18, 0x09, 0x20,
0x01, 0x28, 0x0c, 0x52, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x65, 0x22, 0x87,
0x01, 0x0a, 0x05, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x12, 0x36, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64,
0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1e, 0x2e, 0x65, 0x78, 0x63, 0x68, 0x61,
0x69, 0x6e, 0x2e, 0x6e, 0x65, 0x62, 0x75, 0x6c, 0x61, 0x2e, 0x76, 0x31, 0x2e, 0x42, 0x6c, 0x6f,
......
......@@ -15,13 +15,14 @@ option php_namespace = "ExChain\\Nebula\\v1";
message BlockHeader {
uint64 height = 1;
bytes hash = 2;
bytes parent_hash = 3;
uint64 timestamp = 4;
bytes parent_hash = 2;
uint64 timestamp = 3;
uint64 l1_height = 4;
bytes l1_hash = 5;
bytes proposer = 6;
bytes app_root = 7; // used to verify block is valid.
bytes signatures = 8; // used to verify block is mint by proposer.
uint64 sequence_number = 6;
bytes proposer = 7;
bytes app_root = 8; // used to verify block is valid.
bytes signature = 9; // used to verify block is mint by proposer.
}
message Block {
......
......@@ -32,7 +32,6 @@ func TestMakeBlock(txcount int) *nebulav1.Block {
block := new(nebulav1.Block)
block.Header = new(nebulav1.BlockHeader)
block.Header.Height = uint64(rand.Intn(1000000))
block.Header.Hash = getHash()
block.Header.ParentHash = getHash()
block.Header.Proposer = getAddr()
block.Header.Timestamp = uint64(time.Now().UnixNano() / 1000 / 1000)
......
package wrapper
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"github.com/golang/protobuf/proto"
)
type BlkWrapper struct {
blk *nebulav1.Block
hash common.Hash
}
func NewBlkWrapper(blk *nebulav1.Block) *BlkWrapper {
return &BlkWrapper{blk: blk}
}
func (b *BlkWrapper) Hash() common.Hash {
if b.hash == (common.Hash{}) {
b.hash = b.calcHash()
}
return b.hash
}
func (b *BlkWrapper) Clone() *nebulav1.Block {
return proto.Clone(b.blk).(*nebulav1.Block)
}
func (b *BlkWrapper) Block() *nebulav1.Block {
return b.blk
}
func (b *BlkWrapper) Height() uint64 {
return b.blk.Header.Height
}
func (b *BlkWrapper) Header() *nebulav1.BlockHeader {
return b.blk.Header
}
func (b *BlkWrapper) calcHash() common.Hash {
nblk := b.Clone()
nblk.Header.Signature = nil
data, _ := proto.Marshal(nblk)
return crypto.Keccak256Hash(data)
}
......@@ -3,6 +3,7 @@ package interopgen
import (
"errors"
"fmt"
"github.com/exchain/go-exchain/exchain/wrapper"
"math/big"
"github.com/exchain/go-exchain/op-deployer/pkg/deployer/opcm"
......@@ -323,8 +324,8 @@ func CompleteL2(l2Host *script.Host, cfg *L2Config, l1Block *types.Block, deploy
// todo: vicotor add the allocs to the genesis block.
//l2Genesis.AllocInfo = allocs.Accounts
l2GenesisBlock := l2Genesis.ToBlock()
rollupCfg, err := deployCfg.RollupConfig(l1Block.Header(), common.BytesToHash(l2GenesisBlock.Header.Hash), l2GenesisBlock.Header.Height)
wblk := wrapper.NewBlkWrapper(l2GenesisBlock)
rollupCfg, err := deployCfg.RollupConfig(l1Block.Header(), wblk.Hash(), wblk.Height())
if err != nil {
return nil, fmt.Errorf("failed to build L2 rollup config: %w", err)
}
......
......@@ -3,8 +3,8 @@ package genesis
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
chaingenesis "github.com/exchain/go-exchain/exchain/genesis"
"github.com/exchain/go-exchain/exchain/wrapper"
"github.com/exchain/go-exchain/metadb/groupdb"
"time"
......@@ -213,7 +213,8 @@ var Subcommands = cli.Commands{
}
l2GenesisBlock := l2Genesis.ToBlock()
rollupConfig, err := config.RollupConfig(l1StartBlock.Header(), common.BytesToHash(l2GenesisBlock.Header.Hash), l2GenesisBlock.Header.Height)
wblk := wrapper.NewBlkWrapper(l2GenesisBlock)
rollupConfig, err := config.RollupConfig(l1StartBlock.Header(), wblk.Hash(), wblk.Height())
if err != nil {
return err
}
......
......@@ -25,9 +25,9 @@ func AttributesMatchBlock(rollupCfg *rollup.Config, attrs *eth.PayloadAttributes
if attrs.Param.Timestamp != block.Timestamp {
return fmt.Errorf("timestamp field does not match. expected: %v. got: %v", uint64(attrs.Param.Timestamp), block.Timestamp)
}
if err := checkParentBeaconBlockRootMatch(attrs.Param.ParentRoot, envelope.ParentBeaconBlockRoot); err != nil {
return err
}
//if err := checkParentBeaconBlockRootMatch(attrs.Param.ParentRoot, envelope.ParentBeaconBlockRoot); err != nil {
// return err
//}
return nil
}
......
......@@ -3,12 +3,11 @@ package derive
import (
"context"
"fmt"
"github.com/exchain/go-exchain/exchain"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/exchain/go-exchain/exchain"
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-service/eth"
......@@ -54,7 +53,7 @@ func (ba *FetchingAttributesBuilder) TestSkipL1OriginCheck() {
// A crit=true error means the input arguments are inconsistent or invalid.
func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Context, l2Parent eth.L2BlockRef, epoch eth.BlockID) (attrs *eth.PayloadAttributes, err error) {
var l1Info eth.BlockInfo
var depositTxs []hexutil.Bytes
var depositTxs []*nebulav1.Transaction_DepositTx
var seqNumber uint64
sysConfig, err := ba.l2.SystemConfigByL2Hash(ctx, l2Parent.Hash)
......@@ -76,7 +75,7 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
epoch, info.ParentHash(), l2Parent.L1Origin))
}
deposits, err := DeriveDeposits(receipts, ba.rollupCfg.DepositContractAddress)
deposits, err := DeriveDepositsForExchain(receipts, ba.rollupCfg.DepositContractAddress)
if err != nil {
// deposits may never be ignored. Failing to process them is a critical error.
return nil, NewCriticalError(fmt.Errorf("failed to derive some deposits: %w", err))
......@@ -109,26 +108,26 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
l2Parent, nextL2Time, eth.ToBlockID(l1Info), l1Info.Time()))
}
var upgradeTxs []hexutil.Bytes
if ba.rollupCfg.IsEcotoneActivationBlock(nextL2Time) {
upgradeTxs, err = EcotoneNetworkUpgradeTransactions()
if err != nil {
return nil, NewCriticalError(fmt.Errorf("failed to build ecotone network upgrade txs: %w", err))
}
}
//var upgradeTxs []hexutil.Bytes
//if ba.rollupCfg.IsEcotoneActivationBlock(nextL2Time) {
// upgradeTxs, err = EcotoneNetworkUpgradeTransactions()
// if err != nil {
// return nil, NewCriticalError(fmt.Errorf("failed to build ecotone network upgrade txs: %w", err))
// }
//}
if ba.rollupCfg.IsFjordActivationBlock(nextL2Time) {
fjord, err := FjordNetworkUpgradeTransactions()
if err != nil {
return nil, NewCriticalError(fmt.Errorf("failed to build fjord network upgrade txs: %w", err))
}
upgradeTxs = append(upgradeTxs, fjord...)
}
//if ba.rollupCfg.IsFjordActivationBlock(nextL2Time) {
// fjord, err := FjordNetworkUpgradeTransactions()
// if err != nil {
// return nil, NewCriticalError(fmt.Errorf("failed to build fjord network upgrade txs: %w", err))
// }
// upgradeTxs = append(upgradeTxs, fjord...)
//}
l1InfoTx, err := L1InfoDepositBytes(ba.rollupCfg, sysConfig, seqNumber, l1Info, nextL2Time)
if err != nil {
return nil, NewCriticalError(fmt.Errorf("failed to create l1InfoTx: %w", err))
}
//l1InfoTx, err := L1InfoDepositBytes(ba.rollupCfg, sysConfig, seqNumber, l1Info, nextL2Time)
//if err != nil {
// return nil, NewCriticalError(fmt.Errorf("failed to create l1InfoTx: %w", err))
//}
var afterForceIncludeTxs []hexutil.Bytes
if ba.rollupCfg.IsInterop(nextL2Time) {
......@@ -139,33 +138,68 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
afterForceIncludeTxs = append(afterForceIncludeTxs, depositsCompleteTx)
}
txs := make([]hexutil.Bytes, 0, 1+len(depositTxs)+len(afterForceIncludeTxs)+len(upgradeTxs))
txs = append(txs, l1InfoTx)
txs = append(txs, depositTxs...)
txs = append(txs, afterForceIncludeTxs...)
txs = append(txs, upgradeTxs...)
//var withdrawals *types.Withdrawals
//if ba.rollupCfg.IsCanyon(nextL2Time) {
// withdrawals = &types.Withdrawals{}
//}
var parentBeaconRoot *common.Hash
if ba.rollupCfg.IsEcotone(nextL2Time) {
parentBeaconRoot = l1Info.ParentBeaconRoot()
if parentBeaconRoot == nil { // default to zero hash if there is no beacon-block-root available
parentBeaconRoot = new(common.Hash)
}
//var parentBeaconRoot *common.Hash
//if ba.rollupCfg.IsEcotone(nextL2Time) {
// parentBeaconRoot = l1Info.ParentBeaconRoot()
// if parentBeaconRoot == nil { // default to zero hash if there is no beacon-block-root available
// parentBeaconRoot = new(common.Hash)
// }
//}
protocolTx := &nebulav1.Transaction_ProtocolTx{
ProtocolTx: &nebulav1.ProtocolTransaction{
L1Number: l1Info.NumberU64(),
L1BlockHash: l1Info.Hash().Bytes(),
L1Time: l1Info.Time(),
SequenceNumber: seqNumber,
BatcherAddr: sysConfig.BatcherAddr.Bytes(),
},
}
txs := make([]*nebulav1.Transaction, 0, len(depositTxs)+1)
txs = append(txs, &nebulav1.Transaction{
TxType: nebulav1.TxType_ProtocolTx,
User: common.Address{}.String(),
Nonce: exchain.GetNonce().Bytes(),
Proxy: false,
Signature: &nebulav1.Signature{
R: make([]byte, 32),
S: make([]byte, 32),
V: 0,
},
Tx: protocolTx,
})
for _, deposit := range depositTxs {
txs = append(txs, &nebulav1.Transaction{
TxType: nebulav1.TxType_DepositTx,
User: common.Address{}.String(),
Nonce: exchain.GetNonce().Bytes(),
Proxy: false,
Signature: &nebulav1.Signature{
R: make([]byte, 32),
S: make([]byte, 32),
V: 0,
},
Tx: deposit,
})
}
// todo: vicotor implement it.
r := &eth.PayloadAttributes{
Param: &exchain.PayloadParams{
ParentRoot: parentBeaconRoot,
Timestamp: nextL2Time,
Proposer: common.Address{},
L1Info: exchain.L1BlockInfo{
BlockHash: l1Info.Hash(),
Number: l1Info.NumberU64(),
Time: l1Info.Time(),
SequenceNumber: seqNumber,
BatcherAddr: sysConfig.BatcherAddr,
},
Timestamp: nextL2Time,
Proposer: common.Address{},
Transactions: nebulav1.TransactionList{
Txs: make([]*nebulav1.Transaction, 0),
Txs: txs,
},
Signer: nil,
},
......
......@@ -2,6 +2,8 @@ package derive
import (
"fmt"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"math/big"
"github.com/hashicorp/go-multierror"
......@@ -49,3 +51,53 @@ func DeriveDeposits(receipts []*types.Receipt, depositContractAddr common.Addres
}
return encodedTxs, result
}
func DeriveDepositsForExchain(receipts []*types.Receipt, depositContractAddr common.Address) ([]*nebulav1.Transaction_DepositTx, error) {
var result error
userDeposits, err := UserDeposits(receipts, depositContractAddr)
if err != nil {
result = multierror.Append(result, err)
}
depositTxs := make([]*nebulav1.Transaction_DepositTx, 0)
for _, tx := range userDeposits {
var mint *nebulav1.DepositTransaction
var transfer *nebulav1.DepositTransaction
if tx.Mint != nil {
// mint to from.
mint = &nebulav1.DepositTransaction{
SourceHash: tx.SourceHash.Bytes(),
User: tx.From.Bytes(),
Coin: common.Address{}.Bytes(),
Amount: tx.Mint.Bytes(),
}
}
if tx.To != nil {
// then do transfer tx
transfer = &nebulav1.DepositTransaction{
SourceHash: tx.SourceHash.Bytes(),
User: tx.To.Bytes(),
Coin: common.Address{}.Bytes(),
Amount: tx.Value.Bytes(),
}
}
if mint != nil && transfer != nil {
actMint := new(big.Int).SetBytes(mint.Amount)
actTransfer := new(big.Int).SetBytes(transfer.Amount)
mint.Amount = new(big.Int).Sub(actMint, actTransfer).Bytes()
}
if mint != nil {
depositTxs = append(depositTxs, &nebulav1.Transaction_DepositTx{
DepositTx: mint,
})
}
if transfer != nil {
depositTxs = append(depositTxs, &nebulav1.Transaction_DepositTx{
DepositTx: transfer,
})
}
}
return depositTxs, result
}
package derive
import (
"encoding/binary"
"fmt"
"github.com/ethereum/go-ethereum/common"
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-service/eth"
......@@ -21,24 +21,9 @@ func PayloadToBlockRef(rollupCfg *rollup.Config, payload *eth.ExecutionPayload)
l1Origin = genesis.L1
sequenceNumber = 0
} else {
txs := payload.Transactions()
if len(txs) == 0 {
return eth.L2BlockRef{}, fmt.Errorf("l2 block is missing L1 info, block hash: %s", payload.BlockHash)
}
tx := txs[0]
if tx.TxType != nebulav1.TxType_ProtocolTx {
return eth.L2BlockRef{}, fmt.Errorf("first payload tx has unexpected tx type: %d", tx.TxType)
}
content := tx.GetProtocolTx()
if content == nil {
return eth.L2BlockRef{}, fmt.Errorf("first payload tx has unexpected tx type: %d", tx.TxType)
}
info, err := L1BlockInfoFromNebula(rollupCfg, uint64(payload.Timestamp), content)
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}
sequenceNumber = info.SequenceNumber
header := payload.Payload.Header
l1Origin = eth.BlockID{Hash: common.BytesToHash(header.L1Hash), Number: header.L1Height}
sequenceNumber = header.SequenceNumber
}
return eth.L2BlockRef{
......@@ -73,18 +58,9 @@ func PayloadToSystemConfig(rollupCfg *rollup.Config, payload *eth.ExecutionPaylo
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 {
return eth.SystemConfig{}, fmt.Errorf("failed to parse L1 info deposit tx from L2 block: %w", err)
}
if isEcotoneButNotFirstBlock(rollupCfg, uint64(payload.Timestamp)) {
// Translate Ecotone values back into encoded scalar if needed.
// We do not know if it was derived from a v0 or v1 scalar,
// but v1 is fine, a 0 blob nebula fee has the same effect.
info.L1FeeScalar[0] = 1
binary.BigEndian.PutUint32(info.L1FeeScalar[24:28], info.BlobBaseFeeScalar)
binary.BigEndian.PutUint32(info.L1FeeScalar[28:32], info.BaseFeeScalar)
}
r := eth.SystemConfig{
BatcherAddr: info.BatcherAddr,
Overhead: info.L1FeeOverhead,
......
......@@ -3,6 +3,7 @@ package engine
import (
"context"
"fmt"
"github.com/exchain/go-exchain/exchain/wrapper"
"time"
"github.com/exchain/go-exchain/op-node/rollup"
......@@ -50,7 +51,7 @@ func (eq *EngDeriver) onBuildStart(ev BuildStartEvent) {
}
eq.emitter.Emit(fcEvent)
hash := result.Payload.Header.Hash
hash := wrapper.NewBlkWrapper(result.Payload).Hash()
eq.emitter.Emit(BuildStartedEvent{
Info: eth.PayloadInfo{ID: eth.PayloadID(hash[len(hash)-8:]), Timestamp: uint64(ev.Attributes.Attributes.Param.Timestamp)},
BuildStarted: buildStartTime,
......@@ -58,7 +59,7 @@ func (eq *EngDeriver) onBuildStart(ev BuildStartEvent) {
DerivedFrom: ev.Attributes.DerivedFrom,
Parent: ev.Attributes.Parent,
Envelope: &eth.ExecutionPayloadEnvelope{
ParentBeaconBlockRoot: &result.ParentRoot,
ParentBeaconBlockRoot: nil,
ExecutionPayload: eth.NewExecutePayload(result.Payload),
},
})
......
......@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/exchain/go-exchain/exchain/wrapper"
"time"
"github.com/ethereum/go-ethereum/common"
......@@ -230,7 +231,7 @@ func (ev TryUpdateEngineEvent) getBlockProcessingMetrics() []interface{} {
payload := ev.Envelope.ExecutionPayload.Payload
logValues := []interface{}{
"hash", payload.Header.Hash,
"hash", wrapper.NewBlkWrapper(payload).Hash(),
"number", uint64(payload.Header.Height),
"app_root", hexutil.Bytes(payload.Header.AppRoot),
"timestamp", uint64(payload.Header.Timestamp),
......
......@@ -8,6 +8,7 @@ import (
"fmt"
"github.com/exchain/go-exchain/exchain"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"github.com/exchain/go-exchain/exchain/wrapper"
"github.com/golang/protobuf/proto"
"io"
"math"
......@@ -238,9 +239,10 @@ type ExecutionPayload struct {
}
func NewExecutePayload(payload *nebulav1.Block) *ExecutionPayload {
wblk := wrapper.NewBlkWrapper(payload)
return &ExecutionPayload{
Payload: payload,
BlockHash: common.BytesToHash(payload.Header.Hash),
BlockHash: wblk.Hash(),
BlockNumber: uint64(payload.Header.Height),
ParentHash: common.BytesToHash(payload.Header.ParentHash),
Timestamp: uint64(payload.Header.Timestamp),
......@@ -272,7 +274,7 @@ func (payload *ExecutionPayload) UnmarshalData(length uint32, i io.Reader) error
}
func (payload *ExecutionPayload) ID() BlockID {
return BlockID{Hash: common.BytesToHash(payload.Payload.Header.Hash), Number: uint64(payload.Payload.Header.Height)}
return BlockID{Hash: payload.BlockHash, Number: uint64(payload.Payload.Header.Height)}
}
func (payload *ExecutionPayload) ParentID() BlockID {
......@@ -298,7 +300,8 @@ func (payload *ExecutionPayload) CanyonBlock() bool {
func (envelope *ExecutionPayloadEnvelope) CheckBlockHash() (actual common.Hash, ok bool) {
// todo: vicotor implement this.
// recalc the block hash, and compare it to payload.Header.Hash.
return common.BytesToHash(envelope.ExecutionPayload.Payload.Header.Hash), true
wblk := wrapper.NewBlkWrapper(envelope.ExecutionPayload.Payload)
return wblk.Hash(), true
}
// todo: vicotor remove it.
......
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