-
Notifications
You must be signed in to change notification settings - Fork 22
/
monomer_test.go
242 lines (209 loc) · 7.26 KB
/
monomer_test.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package monomer_test
import (
"math/big"
"strconv"
"testing"
"time"
bfttypes "github.com/cometbft/cometbft/types"
cosmossecp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
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"
"github.com/polymerdao/monomer"
"github.com/polymerdao/monomer/testutils"
"github.com/stretchr/testify/require"
)
func TestChainID(t *testing.T) {
n := int64(12345)
id := monomer.ChainID(n)
require.Equal(t, strconv.FormatInt(n, 10), id.String())
bigInt := big.NewInt(n)
require.Equal(t, bigInt, id.Big())
require.Equal(t, (*hexutil.Big)(bigInt), id.HexBig())
}
func newTestHeader() *monomer.Header {
return &monomer.Header{
ChainID: 12345,
Height: 67890,
Time: uint64(time.Now().Unix()),
StateRoot: common.HexToHash("0x1"),
ParentHash: common.HexToHash("0x2"),
GasLimit: 3000000,
Hash: common.HexToHash("0x3"),
}
}
func TestToComet(t *testing.T) {
header := newTestHeader()
cometHeader := header.ToComet()
require.Equal(t, &bfttypes.Header{
ChainID: header.ChainID.String(),
Height: int64(header.Height),
Time: time.Unix(int64(header.Time), 0),
AppHash: header.StateRoot.Bytes(),
LastBlockID: bfttypes.BlockID{Hash: header.ParentHash.Bytes()},
}, cometHeader)
}
func TestToEth(t *testing.T) {
header := newTestHeader()
ethHeader := header.ToEth()
require.Equal(t, ðtypes.Header{
ParentHash: header.ParentHash,
Root: header.StateRoot,
Number: big.NewInt(int64(header.Height)),
GasLimit: header.GasLimit,
MixDigest: common.Hash{},
Time: header.Time,
UncleHash: ethtypes.EmptyUncleHash,
ReceiptHash: ethtypes.EmptyReceiptsHash,
BaseFee: common.Big0,
WithdrawalsHash: ðtypes.EmptyWithdrawalsHash,
Difficulty: common.Big0,
}, ethHeader)
}
func TestBlockNewBlock(t *testing.T) {
block := monomer.NewBlock(newTestHeader(), bfttypes.Txs{})
ethBlock, err := block.ToEth()
require.NoError(t, err)
newBlock, err := monomer.SetHeader(block)
require.NoError(t, err)
require.Equal(t, ethBlock.Hash(), newBlock.Header.Hash)
}
func TestBlockSetHeader(t *testing.T) {
block := monomer.NewBlock(newTestHeader(), bfttypes.Txs{})
ethBlock, err := block.ToEth()
require.NoError(t, err)
newBlock, err := monomer.SetHeader(block)
require.NoError(t, err)
require.Equal(t, ethBlock.Hash(), newBlock.Header.Hash)
}
func TestBlockMakeBlock(t *testing.T) {
header := newTestHeader()
emptyTxs := bfttypes.Txs{}
block := monomer.NewBlock(header, emptyTxs)
ethBlock, err := block.ToEth()
require.NoError(t, err)
newBlock, err := monomer.MakeBlock(header, emptyTxs)
require.NoError(t, err)
require.Equal(t, ethBlock.Hash(), newBlock.Header.Hash)
}
func TestBlockToEth(t *testing.T) {
l1InfoTx, depositTx, cosmosEthTx := testutils.GenerateEthTxs(t)
block := testutils.GenerateBlockFromEthTxs(t,
l1InfoTx,
[]*ethtypes.Transaction{depositTx},
[]*ethtypes.Transaction{cosmosEthTx},
)
ethTxs, err := monomer.AdaptCosmosTxsToEthTxs(block.Txs)
require.NoError(t, err)
actualEthBlock, err := block.ToEth()
require.NoError(t, err)
require.Equal(t, ethtypes.NewBlock(
block.Header.ToEth(),
ðtypes.Body{
Transactions: ethTxs,
// op-node version requires non-nil withdrawals when it derives attributes from L1,
// so unsafe block consolidation will fail if we have nil withdrawals here.
Withdrawals: []*ethtypes.Withdrawal{},
},
[]*ethtypes.Receipt{},
trie.NewStackTrie(nil),
).Hash(), actualEthBlock.Hash())
}
// TestBlockToEthFailsWithWrongTxs tests the behavior of the Block.ToEth() method when provided with incorrect transactions.
// It creates a new block with a test header and a list of transactions containing two byte slices.
// The ToEth() method is then called on the new block, and the returned value and error are captured.
// The test asserts that an error is returned, indicating that the conversion to Ethereum format failed.
// This test is used to ensure the correct handling of invalid transactions in the Block.ToEth() method.
func TestBlockToEthFailsWithWrongTxs(t *testing.T) {
newBlock := monomer.NewBlock(newTestHeader(), bfttypes.Txs{[]byte("transaction1"), []byte("transaction2")})
_, err := newBlock.ToEth()
require.Error(t, err)
}
func TestBlockToCometLikeBlock(t *testing.T) {
l1InfoTx, depositTx, cosmosEthTx := testutils.GenerateEthTxs(t)
block := testutils.GenerateBlockFromEthTxs(t,
l1InfoTx,
[]*ethtypes.Transaction{depositTx},
[]*ethtypes.Transaction{cosmosEthTx},
)
require.Equal(t, &bfttypes.Block{
Header: bfttypes.Header{
ChainID: block.Header.ChainID.String(),
Time: time.Unix(int64(block.Header.Time), 0),
Height: int64(block.Header.Height),
AppHash: block.Header.StateRoot.Bytes(),
LastBlockID: bfttypes.BlockID{
Hash: block.Header.ParentHash.Bytes(),
},
},
Data: bfttypes.Data{
Txs: block.Txs,
},
}, block.ToCometLikeBlock())
}
func newPayloadAttributes() *monomer.PayloadAttributes {
return &monomer.PayloadAttributes{
Timestamp: uint64(time.Now().Unix()),
PrevRandao: [32]byte{1, 2, 3, 4},
SuggestedFeeRecipient: common.HexToAddress("0x5"),
NoTxPool: false,
GasLimit: 3000000,
ParentHash: common.HexToHash("0x2"),
CosmosTxs: bfttypes.Txs{[]byte("transaction1"), []byte("transaction2")},
}
}
func TestPayloadAttributesID(t *testing.T) {
tests := []struct {
name string
noTxPool bool
expected *engine.PayloadID
}{
{
name: "NoTxPool is false",
},
{
name: "NoTxPool is true",
noTxPool: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pa := newPayloadAttributes()
pa.NoTxPool = test.noTxPool
id := pa.ID()
require.NotNil(t, id)
require.Equal(t, id, pa.ID())
})
}
}
func TestPayloadAttributesValidForkchoiceUpdateResult(t *testing.T) {
headBlockHash := common.HexToHash("0x1")
payloadID := &engine.PayloadID{}
result := monomer.ValidForkchoiceUpdateResult(&headBlockHash, payloadID)
require.Equal(t, &opeth.ForkchoiceUpdatedResult{
PayloadStatus: opeth.PayloadStatusV1{
Status: opeth.ExecutionValid,
LatestValidHash: &headBlockHash,
},
PayloadID: payloadID,
}, result)
}
func TestCosmosETHAddress(t *testing.T) {
privKey, err := secp256k1.GeneratePrivateKey()
require.NoError(t, err)
pubKey := privKey.PubKey().ToECDSA()
got := monomer.PubkeyToCosmosETHAddress(pubKey)
wantBytes := (&cosmossecp256k1.PubKey{
Key: privKey.PubKey().SerializeCompressed(), // https://github.com/cosmos/cosmos-sdk/blob/346044afd0ecd4738c13993d2ac75da8e242266d/crypto/keys/secp256k1/secp256k1.go#L44-L45
}).Address().Bytes()
require.Equal(t, wantBytes, common.Address(got).Bytes())
// We have to use the `cosmos` hrp here because sdk.AccAddress.String() uses the global SDK config variable that uses the `cosmos` hrp.
gotEncoded, err := got.Encode("cosmos")
require.NoError(t, err)
require.Equal(t, sdk.AccAddress(wantBytes).String(), gotEncoded)
}