-
Notifications
You must be signed in to change notification settings - Fork 6
/
monomer.go
208 lines (185 loc) · 6.71 KB
/
monomer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package monomer
import (
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
"hash"
"math/big"
"strconv"
"time"
abcitypes "github.com/cometbft/cometbft/abci/types"
bftbytes "github.com/cometbft/cometbft/libs/bytes"
bfttypes "github.com/cometbft/cometbft/types"
opeth "github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
)
type Application interface {
Info(context.Context, *abcitypes.RequestInfo) (*abcitypes.ResponseInfo, error)
Query(context.Context, *abcitypes.RequestQuery) (*abcitypes.ResponseQuery, error)
CheckTx(context.Context, *abcitypes.RequestCheckTx) (*abcitypes.ResponseCheckTx, error)
InitChain(context.Context, *abcitypes.RequestInitChain) (*abcitypes.ResponseInitChain, error)
FinalizeBlock(context.Context, *abcitypes.RequestFinalizeBlock) (*abcitypes.ResponseFinalizeBlock, error)
Commit(context.Context, *abcitypes.RequestCommit) (*abcitypes.ResponseCommit, error)
RollbackToHeight(context.Context, uint64) error
}
type ChainID uint64
func (id ChainID) String() string {
return strconv.FormatUint(uint64(id), 10)
}
func (id ChainID) HexBig() *hexutil.Big {
return (*hexutil.Big)(new(big.Int).SetUint64(uint64(id)))
}
type Header struct {
ChainID ChainID `json:"chain_id"`
Height int64 `json:"height"`
Time uint64 `json:"time"`
ParentHash common.Hash `json:"parentHash"`
// state after txs from the *previous* block
AppHash []byte `json:"app_hash"`
GasLimit uint64 `json:"gasLimit"`
Hash common.Hash `json:"hash"`
}
func (h *Header) ToComet() *bfttypes.Header {
return &bfttypes.Header{
ChainID: h.ChainID.String(),
Height: h.Height,
Time: time.Unix(int64(h.Time), 0),
AppHash: h.AppHash,
}
}
type Block struct {
Header *Header `json:"header"`
Txs bfttypes.Txs `json:"txs"`
}
// Hash returns a unique hash of the block, used as the block identifier
func (b *Block) Hash() common.Hash {
if b.Header.Hash == (common.Hash{}) {
// We exclude the tx commitment.
// TODO better hashing technique than using Ethereum's.
b.Header.Hash = (ðtypes.Header{
ParentHash: b.Header.ParentHash,
Root: common.BytesToHash(b.Header.AppHash), // TODO actually take the keccak
Number: big.NewInt(b.Header.Height),
GasLimit: b.Header.GasLimit,
MixDigest: common.Hash{},
Time: b.Header.Time,
UncleHash: ethtypes.EmptyUncleHash,
ReceiptHash: ethtypes.EmptyReceiptsHash,
BaseFee: common.Big0,
WithdrawalsHash: ðtypes.EmptyWithdrawalsHash,
}).Hash()
}
return b.Header.Hash
}
// This trick is played by the eth rpc server too. Instead of constructing
// an actual eth block, simply create a map with the right keys so the client
// can unmarshal it into a block
func (b *Block) ToEthLikeBlock(txs ethtypes.Transactions, inclTxs bool) map[string]any {
excessBlobGas := hexutil.Uint64(0)
blockGasUsed := hexutil.Uint64(0)
result := map[string]any{
// These are the ones that make sense to polymer.
"parentHash": b.Header.ParentHash,
"stateRoot": common.BytesToHash(b.Header.AppHash),
"number": (*hexutil.Big)(big.NewInt(b.Header.Height)),
"gasLimit": hexutil.Uint64(b.Header.GasLimit),
"mixHash": common.Hash{},
"timestamp": hexutil.Uint64(b.Header.Time),
"hash": b.Hash(),
// these are required fields that need to be part of the header or
// the eth client will complain during unmarshalling
"sha3Uncles": ethtypes.EmptyUncleHash,
"receiptsRoot": ethtypes.EmptyReceiptsHash,
"baseFeePerGas": (*hexutil.Big)(common.Big0),
"difficulty": (*hexutil.Big)(common.Big0),
"extraData": []byte{},
"gasUsed": hexutil.Uint64(0),
"logsBloom": ethtypes.Bloom(make([]byte, ethtypes.BloomByteLength)),
"withdrawalsRoot": ethtypes.EmptyWithdrawalsHash,
"withdrawals": ethtypes.Withdrawals{},
"blobGasUsed": &blockGasUsed,
"excessBlobGas": &excessBlobGas,
"parentBeaconBlockRoot": common.Hash{},
"transactionsRoot": ethtypes.DeriveSha(txs, trie.NewStackTrie(nil)),
}
if inclTxs {
result["transactions"] = txs
}
return result
}
func (b *Block) ToCometLikeBlock() *bfttypes.Block {
return &bfttypes.Block{
Header: bfttypes.Header{
ChainID: b.Header.ChainID.String(),
Time: time.Unix(int64(b.Header.Time), 0),
Height: b.Header.Height,
AppHash: bftbytes.HexBytes(b.Header.AppHash),
},
}
}
type PayloadAttributes struct {
Timestamp uint64
PrevRandao [32]byte
SuggestedFeeRecipient common.Address
Withdrawals *ethtypes.Withdrawals
NoTxPool bool
GasLimit uint64
ParentBeaconBlockRoot *common.Hash
ParentHash common.Hash
Height int64
Transactions []hexutil.Bytes
CosmosTxs bfttypes.Txs
id *engine.PayloadID
}
// ID returns a PaylodID (a hash) from a PayloadAttributes when it's applied to a head block.
// Hashing does not conform to go-ethereum/miner/payload_building.go
// PayloadID is only calculated once, and cached for future calls.
func (p *PayloadAttributes) ID() *engine.PayloadID {
if p.id != nil {
return p.id
}
hasher := sha256.New()
hashData(hasher, p.ParentHash[:])
hashDataAsBinary(hasher, p.Timestamp)
hashData(hasher, p.PrevRandao[:])
hashData(hasher, p.SuggestedFeeRecipient[:])
hashDataAsBinary(hasher, p.GasLimit)
if p.NoTxPool || len(p.Transactions) == 0 {
hashDataAsBinary(hasher, p.NoTxPool)
hashDataAsBinary(hasher, uint64(len(p.Transactions)))
for _, txData := range p.CosmosTxs {
hashData(hasher, txData)
}
}
var out engine.PayloadID
copy(out[:], hasher.Sum(nil)[:8])
p.id = &out
return &out
}
func hashData(h hash.Hash, data []byte) {
// We know hash.Hash should never return an error, so a panic is fine.
if _, err := h.Write(data); err != nil {
panic(fmt.Errorf("hash data: %v", err))
}
}
func hashDataAsBinary(h hash.Hash, data any) {
// We know hash.Hash should never return an error, so a panic is fine.
if err := binary.Write(h, binary.BigEndian, data); err != nil {
panic(fmt.Errorf("hash data as binary: %v", err))
}
}
// ValidForkchoiceUpdateResult returns a valid ForkchoiceUpdateResult with given head block hash.
func ValidForkchoiceUpdateResult(headBlockHash *common.Hash, id *engine.PayloadID) *opeth.ForkchoiceUpdatedResult {
return &opeth.ForkchoiceUpdatedResult{
PayloadStatus: opeth.PayloadStatusV1{
Status: opeth.ExecutionValid,
LatestValidHash: headBlockHash,
},
PayloadID: id,
}
}