forked from bnb-chain/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrlp_encode_alloc.go-ini
More file actions
108 lines (91 loc) · 2.71 KB
/
rlp_encode_alloc.go-ini
File metadata and controls
108 lines (91 loc) · 2.71 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/big"
"sort"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
)
// Account represents an Ethereum account allocation
type Account struct {
Balance string `json:"balance"`
}
// Genesis represents the structure of genesis.json
type Genesis struct {
Alloc map[string]Account `json:"alloc"`
}
// AccountRLP represents the RLP structure for an Ethereum genesis account
type AccountRLP struct {
Nonce uint64
Balance *big.Int
CodeHash []byte
}
func main() {
// Read the genesis.json file
fileData, err := ioutil.ReadFile("genesis.json")
if err != nil {
log.Fatalf("Error reading genesis.json: %v", err)
}
// Parse JSON
var genesis Genesis
err = json.Unmarshal(fileData, &genesis)
if err != nil {
log.Fatalf("Error parsing JSON: %v", err)
}
// Convert alloc map into a sorted slice
var allocList []struct {
Address common.Address
Account AccountRLP
}
for address, account := range genesis.Alloc {
// Convert balance string to *big.Int
balance := new(big.Int)
_, success := balance.SetString(account.Balance, 10) // Base 10 conversion
if !success {
log.Fatalf("Failed to convert balance %s for address %s", account.Balance, address)
}
// Convert hex address string to common.Address
addr := common.HexToAddress(address)
// Define Ethereum RLP account format
accountData := AccountRLP{
Nonce: 0, // Genesis accounts have nonce 0
Balance: balance, // Correct big.Int balance
CodeHash: common.Hash{}.Bytes(), // Empty code
}
// Append to list
allocList = append(allocList, struct {
Address common.Address
Account AccountRLP
}{Address: addr, Account: accountData})
}
// Sort the list by address to ensure deterministic output
sort.Slice(allocList, func(i, j int) bool {
return bytes.Compare(allocList[i].Address.Bytes(), allocList[j].Address.Bytes()) < 0
})
// RLP Encode the sorted alloc list
var encodedAlloc bytes.Buffer
err = rlp.Encode(&encodedAlloc, allocList)
if err != nil {
log.Fatalf("RLP Encoding failed: %v", err)
}
// Convert bytes to Go-compatible string (hex escapes)
encodedBytes := encodedAlloc.Bytes()
var goByteString strings.Builder
goByteString.WriteString(`const mainnetAllocData = "`)
for _, b := range encodedBytes {
goByteString.WriteString(fmt.Sprintf("\\x%02x", b))
}
goByteString.WriteString(`"`)
// Save to file
outputFile := "encoded_genesis_alloc.txt"
err = ioutil.WriteFile(outputFile, []byte(goByteString.String()), 0644)
if err != nil {
log.Fatalf("Error writing to file: %v", err)
}
fmt.Printf("✅ RLP Encoded Go Byte String saved to %s\n", outputFile)
}