core: add 'codeFilePath' optional field to allow loading predefined account code from a file

This commit is contained in:
Jared Wasinger 2018-08-21 14:06:55 -04:00
parent a063fe9b2d
commit 471b46d8c0

View file

@ -5,6 +5,8 @@ package core
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -39,6 +41,7 @@ func (g GenesisAccount) MarshalJSON() ([]byte, error) {
func (g *GenesisAccount) UnmarshalJSON(input []byte) error { func (g *GenesisAccount) UnmarshalJSON(input []byte) error {
type GenesisAccount struct { type GenesisAccount struct {
Code *hexutil.Bytes `json:"code,omitempty"` Code *hexutil.Bytes `json:"code,omitempty"`
CodeFilePath *string `json:"codeFilePath,omitempty"`
Storage map[storageJSON]storageJSON `json:"storage,omitempty"` Storage map[storageJSON]storageJSON `json:"storage,omitempty"`
Balance *math.HexOrDecimal256 `json:"balance" gencodec:"required"` Balance *math.HexOrDecimal256 `json:"balance" gencodec:"required"`
Nonce *math.HexOrDecimal64 `json:"nonce,omitempty"` Nonce *math.HexOrDecimal64 `json:"nonce,omitempty"`
@ -50,6 +53,15 @@ func (g *GenesisAccount) UnmarshalJSON(input []byte) error {
} }
if dec.Code != nil { if dec.Code != nil {
g.Code = *dec.Code g.Code = *dec.Code
} else if dec.CodeFilePath != nil {
// Check that the file exists
// create an io.Reader with the content of the file and get it as []byte
code, err := ioutil.ReadFile(*dec.CodeFilePath)
if err != nil {
panic(fmt.Sprintf("Could not open file: %v", err))
}
g.Code = code
} }
if dec.Storage != nil { if dec.Storage != nil {
g.Storage = make(map[common.Hash]common.Hash, len(dec.Storage)) g.Storage = make(map[common.Hash]common.Hash, len(dec.Storage))