forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 136
/
taiko_l1_origin.go
92 lines (76 loc) 路 2.71 KB
/
taiko_l1_origin.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
package rawdb
import (
"bytes"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
)
var (
// Database key prefix for L2 block's L1Origin.
l1OriginPrefix = []byte("TKO:L1O")
headL1OriginKey = []byte("TKO:LastL1O")
)
// l1OriginKey calculates the L1Origin key.
// l1OriginPrefix + l2HeaderHash -> l1OriginKey
func l1OriginKey(blockID *big.Int) []byte {
data, _ := (*math.HexOrDecimal256)(blockID).MarshalText()
return append(l1OriginPrefix, data...)
}
//go:generate go run github.com/fjl/gencodec -type L1Origin -field-override l1OriginMarshaling -out gen_taiko_l1_origin.go
// L1Origin represents a L1Origin of a L2 block.
type L1Origin struct {
BlockID *big.Int `json:"blockID" gencodec:"required"`
L2BlockHash common.Hash `json:"l2BlockHash"`
L1BlockHeight *big.Int `json:"l1BlockHeight" gencodec:"required"`
L1BlockHash common.Hash `json:"l1BlockHash" gencodec:"required"`
}
type l1OriginMarshaling struct {
BlockID *math.HexOrDecimal256
L1BlockHeight *math.HexOrDecimal256
}
// WriteL1Origin stores a L1Origin into the database.
func WriteL1Origin(db ethdb.KeyValueWriter, blockID *big.Int, l1Origin *L1Origin) {
data, err := rlp.EncodeToBytes(l1Origin)
if err != nil {
log.Crit("Failed to encode L1Origin", "err", err)
}
if err := db.Put(l1OriginKey(blockID), data); err != nil {
log.Crit("Failed to store L1Origin", "err", err)
}
}
// ReadL1Origin retrieves the given L2 block's L1Origin from database.
func ReadL1Origin(db ethdb.KeyValueReader, blockID *big.Int) (*L1Origin, error) {
data, _ := db.Get(l1OriginKey(blockID))
if len(data) == 0 {
return nil, nil
}
l1Origin := new(L1Origin)
if err := rlp.Decode(bytes.NewReader(data), l1Origin); err != nil {
return nil, fmt.Errorf("invalid L1Origin RLP bytes: %w", err)
}
return l1Origin, nil
}
// WriteHeadL1Origin stores the given L1Origin as the last L1Origin.
func WriteHeadL1Origin(db ethdb.KeyValueWriter, blockID *big.Int) {
data, _ := (*math.HexOrDecimal256)(blockID).MarshalText()
if err := db.Put(headL1OriginKey, data); err != nil {
log.Crit("Failed to store head L1Origin", "error", err)
}
}
// ReadHeadL1Origin retrieves the last L1Origin from database.
func ReadHeadL1Origin(db ethdb.KeyValueReader) (*big.Int, error) {
data, _ := db.Get(headL1OriginKey)
if len(data) == 0 {
return nil, nil
}
blockID := new(math.HexOrDecimal256)
if err := blockID.UnmarshalText(data); err != nil {
log.Error("Unmarshal L1Origin unmarshal error", "error", err)
return nil, fmt.Errorf("invalid L1Origin unmarshal: %w", err)
}
return (*big.Int)(blockID), nil
}