Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain: make a genesis block according to forks #246

Merged
merged 8 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions blockchain/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/kaiachain/kaia/blockchain/state"
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/blockchain/types/accountkey"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/common/hexutil"
"github.com/kaiachain/kaia/common/math"
Expand Down Expand Up @@ -307,16 +308,29 @@ func (g *Genesis) ToBlock(baseStateRoot common.Hash, db database.DBManager) *typ
db = database.NewMemoryDBManager()
}
stateDB, _ := state.New(baseStateRoot, state.NewDatabase(db), nil, nil)
rules := params.Rules{}
if g.Config != nil {
rules = g.Config.Rules(new(big.Int).SetUint64(g.Number))
}
for addr, account := range g.Alloc {
if len(account.Code) != 0 {
originalCode := stateDB.GetCode(addr)
originalCode := stateDB.GetCode(addr)
switch _, ok := types.ParseDelegation(account.Code); {
case ok && rules.IsPrague:
// EOA with code. Usually SetCodeTx creates it. Unit tests may create it here in the genesis.
stateDB.SetCodeToEOA(addr, account.Code, rules)
case len(account.Code) == 0 && len(account.Storage) != 0 && rules.IsPrague:
// Represents an EOA that had code then nullified with another SetCodeTx. Unit tests may create it here in the genesis.
stateDB.CreateEOA(addr, false, accountkey.NewAccountKeyLegacy())
case len(account.Code) != 0:
// Regular genesis smart contract account.
stateDB.CreateSmartContractAccount(addr, params.CodeFormatEVM, rules)
stateDB.SetCode(addr, account.Code)
// If originalCode is not nil,
// just update the code and don't change the other states
if originalCode != nil {
logger.Warn("this address already has a not nil code, now the code of this address has been changed", "addr", addr.String())
continue
}
}
// If account.Code is nil and originalCode is not nil,
// just update the code and don't change the other states
if len(account.Code) != 0 && originalCode != nil {
logger.Warn("this address already has a not nil code, now the code of this address has been changed", "addr", addr.String())
continue
Comment on lines +333 to +337
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this can be checked first before L316-322.

Copy link
Contributor Author

@ulbqb ulbqb Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't do that. I want to do continue after setting code if an original code exists.

}
for key, value := range account.Storage {
stateDB.SetState(addr, key, value)
Expand Down
136 changes: 136 additions & 0 deletions blockchain/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ import (
"fmt"
"math/big"
"reflect"
"strings"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/kaiachain/kaia/blockchain/state"
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/blockchain/types/account"
"github.com/kaiachain/kaia/blockchain/vm"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/common/hexutil"
"github.com/kaiachain/kaia/consensus/gxhash"
"github.com/kaiachain/kaia/params"
"github.com/kaiachain/kaia/storage/database"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestDefaultGenesisBlock tests the genesis block generation functions: DefaultGenesisBlock, DefaultKairosGenesisBlock
Expand Down Expand Up @@ -371,6 +376,137 @@ func TestGenesisRestoreState(t *testing.T) {
assert.True(t, ok)
}

// TestCodeInfo tests the genesis accounts
func TestGenesisAccount(t *testing.T) {
// test account address
addr := common.HexToAddress("0x0100000000000000000000000000000000000000")

// accumulate forks from Kairos config
v := reflect.ValueOf(*params.KairosChainConfig.Copy())
forks := map[string]*big.Int{"EmptyForkCompatibleBlock": big.NewInt(0)}
for i := 0; i < v.NumField(); i++ {
if strings.HasSuffix(v.Type().Field(i).Name, "CompatibleBlock") {
forks[v.Type().Field(i).Name] = v.Field(i).Interface().(*big.Int)
}
}

tcs := map[string]struct {
account GenesisAccount
test func(*testing.T, params.Rules, params.VmVersion, bool, account.Account)
}{
"simple EOA": {
account: GenesisAccount{
Balance: big.NewInt(0),
},
test: func(t *testing.T, r params.Rules, vmv params.VmVersion, ok bool, acc account.Account) {
require.False(t, ok)
require.Equal(t, params.VmVersion0, vmv)
require.Equal(t, account.ExternallyOwnedAccountType, acc.Type())
},
},
"simple SCA": {
account: GenesisAccount{
Balance: big.NewInt(0),
Code: hexutil.MustDecode("0x00"),
},
test: func(t *testing.T, r params.Rules, vmv params.VmVersion, ok bool, acc account.Account) {
if !r.IsIstanbul {
require.True(t, ok)
require.Equal(t, params.VmVersion0, vmv)
require.Equal(t, account.SmartContractAccountType, acc.Type())
} else if r.IsIstanbul {
require.True(t, ok)
require.Equal(t, params.VmVersion1, vmv)
require.Equal(t, account.SmartContractAccountType, acc.Type())
}
},
},
"account with delegation code": {
account: GenesisAccount{
Balance: big.NewInt(0),
Code: types.AddressToDelegation(common.HexToAddress("0x000000000000000000000000000000000000000")),
},
test: func(t *testing.T, r params.Rules, vmv params.VmVersion, ok bool, acc account.Account) {
if !r.IsIstanbul {
require.True(t, ok)
require.Equal(t, params.VmVersion0, vmv)
require.Equal(t, account.SmartContractAccountType, acc.Type())
} else if r.IsIstanbul && !r.IsPrague {
require.True(t, ok)
require.Equal(t, params.VmVersion1, vmv)
require.Equal(t, account.SmartContractAccountType, acc.Type())
} else {
require.True(t, ok)
require.Equal(t, params.VmVersion1, vmv)
require.Equal(t, account.ExternallyOwnedAccountType, acc.Type())
}
},
},
"account with empty code but non-empty storage": {
account: GenesisAccount{
Balance: big.NewInt(0),
Storage: map[common.Hash]common.Hash{{1}: {1}},
},
test: func(t *testing.T, r params.Rules, vmv params.VmVersion, ok bool, acc account.Account) {
if !r.IsPrague {
require.True(t, ok)
require.Equal(t, params.VmVersion0, vmv)
require.Equal(t, account.SmartContractAccountType, acc.Type())
} else {
require.False(t, ok)
require.Equal(t, params.VmVersion0, vmv)
require.Equal(t, account.ExternallyOwnedAccountType, acc.Type())
}
},
},
}

// test all cases for each fork
for tcn, tc := range tcs {
for fork, n := range forks {
t.Run(fmt.Sprintf("%s fork %s", tcn, fork), func(t *testing.T) {
genesis := &Genesis{
Config: &params.ChainConfig{
ChainID: new(big.Int).SetUint64(1),
},
Alloc: GenesisAlloc{
addr: tc.account,
},
}

// make config for fork
for f2, n2 := range forks {
var n3 *big.Int
if n == nil {
n3 = big.NewInt(0)
} else if n2 != nil && n.Cmp(n2) >= 0 {
n3 = big.NewInt(0)
}
r := reflect.ValueOf(genesis.Config)
f := reflect.Indirect(r).FieldByName(f2)
if f.Kind() != reflect.Invalid {
f.Set(reflect.ValueOf(n3))
}
}

genesis.Config.SetDefaultsForGenesis()
genesis.Governance = SetGenesisGovernance(genesis)
InitDeriveSha(genesis.Config)

db := database.NewMemoryDBManager()
gblock := genesis.ToBlock(common.Hash{}, db)
stateDB, _ := state.New(gblock.Root(), state.NewDatabase(db), nil, nil)

rules := genesis.Config.Rules(new(big.Int).SetUint64(genesis.Number))
vmVersion, ok := stateDB.GetVmVersion(addr)
account := stateDB.GetAccount(addr)

tc.test(t, rules, vmVersion, ok, account)
})
}
}
}

func genMainnetGenesisBlock() *Genesis {
genesis := DefaultGenesisBlock()
genesis.Config = params.MainnetChainConfig.Copy()
Expand Down
Loading