mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
genesis block writing to db
This commit is contained in:
parent
1d93088980
commit
cb1f0db4a5
10 changed files with 178 additions and 19 deletions
|
|
@ -37,7 +37,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
cli "gopkg.in/urfave/cli.v1"
|
||||
"gopkg.in/urfave/cli.v1"
|
||||
)
|
||||
|
||||
var runCommand = cli.Command{
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ func initGenesis(ctx *cli.Context) error {
|
|||
if err != nil {
|
||||
utils.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
_, hash, err := core.SetupGenesisBlock(chaindb, genesis)
|
||||
_, hash, err := core.SetupGenesisBlock(chaindb, genesis, nil)
|
||||
if err != nil {
|
||||
utils.Fatalf("Failed to write genesis block: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1219,7 +1219,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
|
|||
var err error
|
||||
chainDb = MakeChainDatabase(ctx, stack)
|
||||
|
||||
config, _, err := core.SetupGenesisBlock(chainDb, MakeGenesis(ctx))
|
||||
config, _, err := core.SetupGenesisBlock(chainDb, MakeGenesis(ctx), nil)
|
||||
if err != nil {
|
||||
Fatalf("%v", err)
|
||||
}
|
||||
|
|
|
|||
137
core/genesis.go
137
core/genesis.go
|
|
@ -25,6 +25,7 @@ import (
|
|||
"math/big"
|
||||
"strings"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/common/math"
|
||||
|
|
@ -34,6 +35,8 @@ import (
|
|||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"database/sql"
|
||||
"github.com/ethereum/go-ethereum/shyftdb"
|
||||
)
|
||||
|
||||
//go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
|
||||
|
|
@ -72,6 +75,7 @@ func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error {
|
|||
*ga = make(GenesisAlloc)
|
||||
for addr, a := range m {
|
||||
(*ga)[common.Address(addr)] = a
|
||||
fmt.Println("GENESIS +++++++++", a)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -149,7 +153,56 @@ func (e *GenesisMismatchError) Error() string {
|
|||
// error is a *params.ConfigCompatError and the new, unwritten config is returned.
|
||||
//
|
||||
// The returned chain configuration is never nil.
|
||||
func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig, common.Hash, error) {
|
||||
//func SetupShyftGenesisBlock(sqldb *sql.DB,gen *Genesis) (common.Hash) {
|
||||
// // Just commit the new block if there is no stored genesis block.
|
||||
// //stored := GetCanonicalHash(db, 0)
|
||||
// //if (stored == common.Hash{}) {
|
||||
// // if genesis == nil {
|
||||
// // log.Info("Writing default main-net genesis block")
|
||||
// // genesis = DefaultGenesisBlock()
|
||||
// // } else {
|
||||
// // log.Info("Writing custom genesis block")
|
||||
// // }
|
||||
// //
|
||||
// //shyftBlock := gen.ShyftCommit(sqldb)
|
||||
// log.Info("Setup Shyft Gen")
|
||||
// return shyftBlock.Hash()
|
||||
//}
|
||||
|
||||
// SetupGenesisBlock writes or updates the genesis block in db.
|
||||
// The block that will be used is:
|
||||
//
|
||||
// genesis == nil genesis != nil
|
||||
// +------------------------------------------
|
||||
// db has no genesis | main-net default | genesis
|
||||
// db has genesis | from DB | genesis (if compatible)
|
||||
//
|
||||
// The stored chain configuration will be updated if it is compatible (i.e. does not
|
||||
// specify a fork block below the local head block). In case of a conflict, the
|
||||
// error is a *params.ConfigCompatError and the new, unwritten config is returned.
|
||||
//
|
||||
// The returned chain configuration is never nil.
|
||||
func WriteShyftGen(sqldb *sql.DB, gen *Genesis) {
|
||||
|
||||
if sqldb == nil {
|
||||
fmt.Println("NIL POSTGRES")
|
||||
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
||||
blockExplorerDb, _ := sql.Open("postgres", connStr)
|
||||
sqldb = blockExplorerDb
|
||||
}
|
||||
|
||||
for k, v := range gen.Alloc {
|
||||
addr := k.String()
|
||||
fmt.Println(addr,v.Balance)
|
||||
fmt.Println("*************")
|
||||
|
||||
sqlStatement := `INSERT INTO accounts(addr, balance) VALUES(($1), ($2)) RETURNING addr`
|
||||
insertErr := sqldb.QueryRow(sqlStatement, addr, v.Balance.String()).Scan(&addr)
|
||||
fmt.Println(insertErr)
|
||||
}
|
||||
}
|
||||
|
||||
func SetupGenesisBlock(db ethdb.Database, genesis *Genesis, sqldb *sql.DB) (*params.ChainConfig, common.Hash, error) {
|
||||
if genesis != nil && genesis.Config == nil {
|
||||
return params.AllEthashProtocolChanges, common.Hash{}, errGenesisNoConfig
|
||||
}
|
||||
|
|
@ -162,6 +215,8 @@ func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig
|
|||
genesis = DefaultGenesisBlock()
|
||||
} else {
|
||||
log.Info("Writing custom genesis block")
|
||||
|
||||
WriteShyftGen(sqldb, genesis)
|
||||
}
|
||||
block, err := genesis.Commit(db)
|
||||
return genesis.Config, block.Hash(), err
|
||||
|
|
@ -219,6 +274,75 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
|
|||
}
|
||||
}
|
||||
|
||||
// ToBlock creates the genesis block and writes state of a genesis specification
|
||||
// to the given database (or discards it if nil).
|
||||
func (ga *Genesis) ToShyftBlock(sqldb *sql.DB) *types.Block {
|
||||
//actual return
|
||||
fmt.Println("++++++++++++++++++++++++++++++++++++")
|
||||
fmt.Println(ga.Alloc)
|
||||
//for addr, account := range g.Alloc {
|
||||
// fmt.Println(addr, account.Balance)
|
||||
// //sqldb.AddBalance(addr, account.Balance)
|
||||
// //sqldb.SetCode(addr, account.Code)
|
||||
// //sqldb.SetNonce(addr, account.Nonce)
|
||||
//}
|
||||
|
||||
head := &types.Header{
|
||||
Number: new(big.Int).SetUint64(ga.Number),
|
||||
Nonce: types.EncodeNonce(ga.Nonce),
|
||||
Time: new(big.Int).SetUint64(ga.Timestamp),
|
||||
ParentHash: ga.ParentHash,
|
||||
Extra: ga.ExtraData,
|
||||
GasLimit: ga.GasLimit,
|
||||
GasUsed: ga.GasUsed,
|
||||
Difficulty: ga.Difficulty,
|
||||
MixDigest: ga.Mixhash,
|
||||
Coinbase: ga.Coinbase,
|
||||
}
|
||||
//if g.GasLimit == 0 {
|
||||
// head.GasLimit = params.GenesisGasLimit
|
||||
//}
|
||||
//if g.Difficulty == nil {
|
||||
// head.Difficulty = params.GenesisDifficulty
|
||||
//}
|
||||
//statedb.Commit(false)
|
||||
//statedb.Database().TrieDB().Commit(root, true)
|
||||
|
||||
return types.NewBlock(head, nil, nil, nil)
|
||||
|
||||
}
|
||||
//@NOTE:SHYFT
|
||||
func (ga *Genesis) ShyftCommit(sqldb *sql.DB) (*types.Block) {
|
||||
fmt.Println("********************************* GENESIS")
|
||||
shyftBlock := ga.ToShyftBlock(sqldb)
|
||||
//if shyftBlock.Number().Sign() != 0 {
|
||||
// return nil
|
||||
//}
|
||||
//if err := WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty); err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
if err := shyftdb.WriteBlock(sqldb, shyftBlock); err != nil {
|
||||
return nil
|
||||
}
|
||||
//if err := WriteBlockReceipts(db, block.Hash(), block.NumberU64(), nil); err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//if err := WriteCanonicalHash(db, block.Hash(), block.NumberU64()); err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//if err := WriteHeadBlockHash(db, block.Hash()); err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//if err := WriteHeadHeaderHash(db, block.Hash()); err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//config := g.Config
|
||||
//if config == nil {
|
||||
// config = params.AllEthashProtocolChanges
|
||||
//}
|
||||
return shyftBlock
|
||||
}
|
||||
|
||||
// ToBlock creates the genesis block and writes state of a genesis specification
|
||||
// to the given database (or discards it if nil).
|
||||
func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
|
||||
|
|
@ -343,6 +467,17 @@ func DefaultRinkebyGenesisBlock() *Genesis {
|
|||
Alloc: decodePrealloc(rinkebyAllocData),
|
||||
}
|
||||
}
|
||||
// DefaultShyftGenesisBlock returns the Shyft network genesis block.
|
||||
func DefaultShyftGenesisBlock() *Genesis {
|
||||
return &Genesis{
|
||||
Config: params.ShyftNetworkChainConfig,
|
||||
Timestamp: 1492009146,
|
||||
ExtraData: hexutil.MustDecode("0x52657370656374206d7920617574686f7269746168207e452e436172746d616e42eb768f2244c8811c63729a21a3569731535f067ffc57839b00206d1ad20c69a1981b489f772031b279182d99e65703f0076e4812653aab85fca0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
|
||||
GasLimit: 4700000,
|
||||
Difficulty: big.NewInt(1),
|
||||
Alloc: decodePrealloc(shyftAllocData),
|
||||
}
|
||||
}
|
||||
|
||||
// DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must
|
||||
// be seeded with the
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -62,7 +62,7 @@ func TestSetupGenesis(t *testing.T) {
|
|||
{
|
||||
name: "genesis without ChainConfig",
|
||||
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
||||
return SetupGenesisBlock(db, new(Genesis))
|
||||
return SetupGenesisBlock(db, new(Genesis), nil)
|
||||
},
|
||||
wantErr: errGenesisNoConfig,
|
||||
wantConfig: params.AllEthashProtocolChanges,
|
||||
|
|
@ -70,7 +70,7 @@ func TestSetupGenesis(t *testing.T) {
|
|||
{
|
||||
name: "no block in DB, genesis == nil",
|
||||
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
||||
return SetupGenesisBlock(db, nil)
|
||||
return SetupGenesisBlock(db, nil, nil)
|
||||
},
|
||||
wantHash: params.MainnetGenesisHash,
|
||||
wantConfig: params.MainnetChainConfig,
|
||||
|
|
@ -79,7 +79,7 @@ func TestSetupGenesis(t *testing.T) {
|
|||
name: "mainnet block in DB, genesis == nil",
|
||||
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
||||
DefaultGenesisBlock().MustCommit(db)
|
||||
return SetupGenesisBlock(db, nil)
|
||||
return SetupGenesisBlock(db, nil, nil)
|
||||
},
|
||||
wantHash: params.MainnetGenesisHash,
|
||||
wantConfig: params.MainnetChainConfig,
|
||||
|
|
@ -88,7 +88,7 @@ func TestSetupGenesis(t *testing.T) {
|
|||
name: "custom block in DB, genesis == nil",
|
||||
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
||||
customg.MustCommit(db)
|
||||
return SetupGenesisBlock(db, nil)
|
||||
return SetupGenesisBlock(db, nil, nil)
|
||||
},
|
||||
wantHash: customghash,
|
||||
wantConfig: customg.Config,
|
||||
|
|
@ -97,7 +97,7 @@ func TestSetupGenesis(t *testing.T) {
|
|||
name: "custom block in DB, genesis == testnet",
|
||||
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
||||
customg.MustCommit(db)
|
||||
return SetupGenesisBlock(db, DefaultTestnetGenesisBlock())
|
||||
return SetupGenesisBlock(db, DefaultTestnetGenesisBlock(), nil)
|
||||
},
|
||||
wantErr: &GenesisMismatchError{Stored: customghash, New: params.TestnetGenesisHash},
|
||||
wantHash: params.TestnetGenesisHash,
|
||||
|
|
@ -107,7 +107,7 @@ func TestSetupGenesis(t *testing.T) {
|
|||
name: "compatible config in DB",
|
||||
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
|
||||
oldcustomg.MustCommit(db)
|
||||
return SetupGenesisBlock(db, &customg)
|
||||
return SetupGenesisBlock(db, &customg, nil)
|
||||
},
|
||||
wantHash: customghash,
|
||||
wantConfig: customg.Config,
|
||||
|
|
@ -126,7 +126,7 @@ func TestSetupGenesis(t *testing.T) {
|
|||
bc.InsertChain(blocks)
|
||||
bc.CurrentBlock()
|
||||
// This should return a compatibility error.
|
||||
return SetupGenesisBlock(db, &customg)
|
||||
return SetupGenesisBlock(db, &customg, nil)
|
||||
},
|
||||
wantHash: customghash,
|
||||
wantConfig: customg.Config,
|
||||
|
|
|
|||
|
|
@ -105,6 +105,8 @@ func (s *Ethereum) AddLesServer(ls LesServer) {
|
|||
// New creates a new Ethereum object (including the
|
||||
// initialisation of the common Ethereum object)
|
||||
func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||
fmt.Println("check//////////")
|
||||
|
||||
if config.SyncMode == downloader.LightSync {
|
||||
return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum")
|
||||
}
|
||||
|
|
@ -115,22 +117,26 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
//for k, v := range &config.Genesis.Alloc {
|
||||
// fmt.Printf("key[%s] value[%s]\n", k, v)
|
||||
//}
|
||||
|
||||
// @NOTE:shyft instantiate BlockExplorerDB here
|
||||
// @TODO: Create Genesis Block
|
||||
// @NOTE:shyft instantiate BlockExplorerDB here?
|
||||
connStr := "user=postgres dbname=shyftdb sslmode=disable"
|
||||
blockExplorerDb, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stopDbUpgrade := upgradeDeduplicateData(chainDb)
|
||||
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis)
|
||||
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis, blockExplorerDb)
|
||||
//core.SetupShyftGenesisBlock(blockExplorerDb, config.Genesis)
|
||||
if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
|
||||
return nil, genesisErr
|
||||
}
|
||||
log.Info("Initialised chain configuration", "config", chainConfig)
|
||||
fmt.Println(config.GasPrice)
|
||||
|
||||
eth := &Ethereum{
|
||||
config: config,
|
||||
chainDb: chainDb,
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis)
|
||||
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis, nil)
|
||||
if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat {
|
||||
return nil, genesisErr
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
||||
|
||||
"database/sql"
|
||||
|
||||
"log"
|
||||
|
|
@ -83,11 +83,20 @@ type SendAndReceive struct {
|
|||
Balance string
|
||||
}
|
||||
|
||||
//func WriteGen(sqldb *sql.DB, genesis *core.Genesis) error {
|
||||
// for k, v := range genesis.Alloc {
|
||||
// addr := k.String()
|
||||
// fmt.Println(addr,v.Balance)
|
||||
// fmt.Println("*************")
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
//WriteBlock writes to block info to sql db
|
||||
func WriteBlock(sqldb *sql.DB, block *types.Block) error {
|
||||
|
||||
fmt.Println("******************")
|
||||
coinbase := block.Header().Coinbase.String()
|
||||
number := block.Header().Number.String()
|
||||
|
||||
sqlStatement := `INSERT INTO blocks(hash, coinbase, number) VALUES(($1), ($2), ($3)) RETURNING number`
|
||||
qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number).Scan(&number)
|
||||
if qerr != nil {
|
||||
|
|
@ -306,6 +315,8 @@ func WriteBalanceHelper(sqldb *sql.DB, tx *types.Transaction) (SendAndReceive, s
|
|||
// }
|
||||
//}
|
||||
|
||||
|
||||
|
||||
///////////
|
||||
// Getters
|
||||
//////////
|
||||
|
|
|
|||
4
vendor/vendor.json
vendored
4
vendor/vendor.json
vendored
|
|
@ -417,6 +417,10 @@
|
|||
"revision": "ed27b6fd65218132ee50cd95f38474a3d8a2cd12",
|
||||
"revisionTime": "2016-06-18T19:32:21Z"
|
||||
},
|
||||
{
|
||||
"path": "github.com/shyft/shyft_go-ethereum/core",
|
||||
"revision": ""
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "mGbTYZ8dHVTiPTTJu3ktp+84pPI=",
|
||||
"path": "github.com/stretchr/testify/assert",
|
||||
|
|
|
|||
Loading…
Reference in a new issue