forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_service.go
96 lines (83 loc) · 2.72 KB
/
static_service.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"errors"
"net/http"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/bilgin-kocak/subnet-evm/core"
"github.com/ethereum/go-ethereum/common"
)
var (
errNoGenesisData = errors.New("no genesis data provided")
errNoConfig = errors.New("no config provided in genesis")
errNoChainID = errors.New("no chain ID provided in genesis config")
errInvalidChainID = errors.New("chainID must be greater than 0")
errNoAlloc = errors.New("no alloc table provided in genesis")
)
// StaticService defines the static API services exposed by the evm
type StaticService struct{}
func CreateStaticService() *StaticService {
return &StaticService{}
}
// BuildGenesisArgs are arguments for BuildGenesis
type BuildGenesisArgs struct {
GenesisData *core.Genesis `json:"genesisData"`
Encoding formatting.Encoding `json:"encoding"`
}
// BuildGenesisReply is the reply from BuildGenesis
type BuildGenesisReply struct {
GenesisBytes string `json:"genesisBytes"`
Encoding formatting.Encoding `json:"encoding"`
}
// BuildGenesis constructs a genesis file.
func (ss *StaticService) BuildGenesis(_ *http.Request, args *BuildGenesisArgs, reply *BuildGenesisReply) error {
// check for critical fields first
switch {
case args.GenesisData == nil:
return errNoGenesisData
case args.GenesisData.Config == nil:
return errNoConfig
case args.GenesisData.Config.ChainID == nil:
return errNoChainID
case args.GenesisData.Config.ChainID.Cmp(common.Big1) == -1:
return errInvalidChainID
case args.GenesisData.Alloc == nil:
return errNoAlloc
}
bytes, err := args.GenesisData.MarshalJSON()
if err != nil {
return err
}
bytesStr, err := formatting.Encode(args.Encoding, bytes)
if err != nil {
return err
}
reply.GenesisBytes = bytesStr
reply.Encoding = args.Encoding
return nil
}
// DecodeGenesisArgs are arguments for DecodeGenesis
type DecodeGenesisArgs struct {
GenesisBytes string `json:"genesisBytes"`
Encoding formatting.Encoding `json:"encoding"`
}
// DecodeGenesisReply is the reply from DecodeGenesis
type DecodeGenesisReply struct {
Genesis *core.Genesis `json:"genesisData"`
Encoding formatting.Encoding `json:"encoding"`
}
// BuildGenesis constructs a genesis file.
func (ss *StaticService) DecodeGenesis(_ *http.Request, args *DecodeGenesisArgs, reply *DecodeGenesisReply) error {
bytesStr, err := formatting.Decode(args.Encoding, args.GenesisBytes)
if err != nil {
return err
}
genesis := core.Genesis{}
if err := genesis.UnmarshalJSON(bytesStr); err != nil {
return err
}
reply.Genesis = &genesis
reply.Encoding = args.Encoding
return nil
}