-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
218 lines (184 loc) · 6.53 KB
/
main.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
package main
import (
"context"
"crypto/ecdsa"
"crypto/rand"
"math/big"
"os"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
"github.com/joho/godotenv"
)
func main() {
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, true))
glogger.Verbosity(log.LevelInfo)
log.SetDefault(log.NewLogger(glogger))
err := godotenv.Load("../.env")
if err != nil {
log.Crit("failed to load .env file", "err", err)
}
privateKey, err := crypto.HexToECDSA(os.Getenv("PRIVATE_KEY"))
if err != nil {
log.Crit("failed to create private key", "err", err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Crit("failed to cast public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
rpcClient, err := rpc.Dial(os.Getenv("RPC_PROVIDER_URL"))
if err != nil {
log.Crit("failed to connect to network", "err", err)
}
ethClient := ethclient.NewClient(rpcClient)
gethClient := gethclient.New(rpcClient)
pointEvaluationPrecompileAddress := common.HexToAddress("0x0A")
blob := randBlob()
sideCar := makeSidecar([]kzg4844.Blob{blob})
versionedHash := sideCar.BlobHashes()[0]
point := randFieldElement()
commitment := sideCar.Commitments[0]
proof, claim, err := kzg4844.ComputeProof(blob, point)
if err != nil {
log.Crit("failed to create KZG proof at point", "err", err)
}
var calldata []byte
calldata = append(calldata, versionedHash.Bytes()...)
calldata = append(calldata, point[:]...)
calldata = append(calldata, claim[:]...)
calldata = append(calldata, commitment[:]...)
calldata = append(calldata, proof[:]...)
chainID, err := ethClient.NetworkID(context.Background())
if err != nil {
log.Crit("failed to get network ID", "err", err)
}
nonce, err := ethClient.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Crit("failed to get pending nonce", "err", err)
}
gasTipCap, err := ethClient.SuggestGasTipCap(context.Background())
if err != nil {
log.Crit("failed to get suggest gas tip cap", "err", err)
}
gasFeeCap, err := ethClient.SuggestGasPrice(context.Background())
if err != nil {
log.Crit("failed to get suggest gas price", "err", err)
}
msg := ethereum.CallMsg{
From: fromAddress,
To: &pointEvaluationPrecompileAddress,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Value: big.NewInt(0),
Data: calldata,
}
gasLimitWithoutAccessList, err := ethClient.EstimateGas(context.Background(), msg)
if err != nil {
log.Crit("failed to estimate gas", "err", err)
}
// Explicitly set a gas limit to prevent the "insufficient funds for gas * price + value" error.
// Because if msg.Gas remains unset, CreateAccessList defaults to using RPCGasCap(), which can be excessively high.
msg.Gas = gasLimitWithoutAccessList * 3
accessList, gasLimitWithAccessList, errStr, rpcErr := gethClient.CreateAccessList(context.Background(), msg)
if rpcErr != nil {
log.Crit("CreateAccessList RPC error", "error", rpcErr)
}
if errStr != "" {
log.Crit("CreateAccessList reported error", "error", errStr)
}
// Fine-tune accessList because the 'to' address is automatically included in the access list by the Ethereum protocol: https://github.com/ethereum/go-ethereum/blob/v1.13.13/core/state/statedb.go#L1322
// This function returns a recalculated gas estimation based on the adjusted access list.
accessList, gasLimitWithAccessList = finetuneAccessList(accessList, gasLimitWithAccessList, msg.To)
dynamicFeeTx := &types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
Gas: gasLimitWithAccessList,
To: &pointEvaluationPrecompileAddress,
Value: big.NewInt(0),
Data: calldata,
}
if accessList != nil {
dynamicFeeTx.AccessList = *accessList
}
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
log.Crit("failed to create transactor", "chainID", chainID, "err", err)
}
signedTx, err := auth.Signer(auth.From, types.NewTx(dynamicFeeTx))
if err != nil {
log.Crit("failed to sign the transaction", "err", err)
}
err = ethClient.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Crit("failed to send transaction", "err", err)
}
log.Info("transaction sent", "txHash", signedTx.Hash().Hex())
}
func makeSidecar(blobs []kzg4844.Blob) *types.BlobTxSidecar {
var (
commitments []kzg4844.Commitment
proofs []kzg4844.Proof
)
for _, blob := range blobs {
c, _ := kzg4844.BlobToCommitment(blob)
p, _ := kzg4844.ComputeBlobProof(blob, c)
commitments = append(commitments, c)
proofs = append(proofs, p)
}
return &types.BlobTxSidecar{
Blobs: blobs,
Commitments: commitments,
Proofs: proofs,
}
}
func randBlob() kzg4844.Blob {
var blob kzg4844.Blob
for i := 0; i < len(blob); i += gokzg4844.SerializedScalarSize {
fieldElementBytes := randFieldElement()
copy(blob[i:i+gokzg4844.SerializedScalarSize], fieldElementBytes[:])
}
return blob
}
func randFieldElement() [32]byte {
bytes := make([]byte, 32)
_, err := rand.Read(bytes)
if err != nil {
panic("failed to get random field element")
}
var r fr.Element
r.SetBytes(bytes)
return gokzg4844.SerializeScalar(r)
}
func finetuneAccessList(accessList *types.AccessList, gasLimitWithAccessList uint64, to *common.Address) (*types.AccessList, uint64) {
if accessList == nil || to == nil {
return accessList, gasLimitWithAccessList
}
var newAccessList types.AccessList
for _, entry := range *accessList {
if entry.Address == *to && len(entry.StorageKeys) <= 24 {
// Based on: https://arxiv.org/pdf/2312.06574.pdf
// We remove the address and respective storage keys as long as the number of storage keys <= 24.
// This removal helps in preventing double-counting of the 'to' address in access list calculations.
gasLimitWithAccessList -= 2400
// Each storage key saves 100 gas units.
gasLimitWithAccessList += uint64(100 * len(entry.StorageKeys))
} else {
// Otherwise, keep the entry in the new access list.
newAccessList = append(newAccessList, entry)
}
}
return &newAccessList, gasLimitWithAccessList
}