Skip to content

Commit

Permalink
Merge 7a84192 into 2349b0c
Browse files Browse the repository at this point in the history
  • Loading branch information
elvis88 committed Apr 18, 2019
2 parents 2349b0c + 7a84192 commit 0b888d9
Show file tree
Hide file tree
Showing 32 changed files with 665 additions and 620 deletions.
12 changes: 4 additions & 8 deletions accountmanager/accountmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,16 +1198,12 @@ func (am *AccountManager) process(accountManagerContext *types.AccountManagerCon
action := accountManagerContext.Action
number := accountManagerContext.Number

if action.Type() != types.Transfer && action.Recipient() != common.Name(sysName) {
return ErrToNameInvalid
}

//transfer
if action.Value().Cmp(big.NewInt(0)) > 0 {
if action.Type() == types.CreateAccount || action.Type() == types.DestroyAsset {
if action.Recipient() != common.Name(sysName) {
return ErrToNameInvalid
}
} else if action.Type() != types.Transfer {
return ErrAmountValueInvalid
}

if err := am.TransferAsset(action.Sender(), action.Recipient(), action.AssetID(), action.Value()); err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions accountmanager/accountmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,12 +1529,12 @@ func TestAccountManager_Process(t *testing.T) {
panic("rlp payload err")
}

action := types.NewAction(types.IssueAsset, common.Name("a123456789aeee"), common.Name("a123456789aeee"), 1, 1, 0, big.NewInt(0), payload)
action1 := types.NewAction(types.IncreaseAsset, common.Name("a123456789aeee"), common.Name("a123456789aeee"), 1, 1, 2, big.NewInt(0), payload2)
action2 := types.NewAction(types.UpdateAsset, common.Name("a123456789aeee"), common.Name("a123456789addd"), 1, 1, 2, big.NewInt(0), payload1)
action := types.NewAction(types.IssueAsset, common.Name("a123456789aeee"), common.Name(sysName), 1, 1, 0, big.NewInt(0), payload)
action1 := types.NewAction(types.IncreaseAsset, common.Name("a123456789aeee"), common.Name(sysName), 1, 1, 2, big.NewInt(0), payload2)
action2 := types.NewAction(types.UpdateAsset, common.Name("a123456789aeee"), common.Name(sysName), 1, 1, 2, big.NewInt(0), payload1)
action3 := types.NewAction(types.CreateAccount, common.Name("a123456789aeee"), common.Name(sysName), 1, 1, 2, big.NewInt(10), payload3)
action4 := types.NewAction(types.UpdateAccount, common.Name("a123456789addd"), common.Name("a123456789addd"), 1, 1, 2, big.NewInt(0), payload4)
action5 := types.NewAction(types.UpdateAccountAuthor, common.Name("a123456789addd"), common.Name("a123456789addd"), 1, 1, 2, big.NewInt(0), payload5)
action4 := types.NewAction(types.UpdateAccount, common.Name("a123456789addd"), common.Name(sysName), 1, 1, 2, big.NewInt(0), payload4)
action5 := types.NewAction(types.UpdateAccountAuthor, common.Name("a123456789addd"), common.Name(sysName), 1, 1, 2, big.NewInt(0), payload5)

//action5 := types.NewAction(types.DeleteAccount, common.Name("123asdf2"), common.Name("123asdf2"), 1, 1, 2, big.NewInt(0), pubkey1[:])
//action6 := types.NewAction(types.Transfer, common.Name("a123456789aeee"), common.Name("a123456789aeee"), 1, 1, 2, big.NewInt(1), pubkey1[:])
Expand Down Expand Up @@ -1616,7 +1616,7 @@ func TestAccountManager_Process(t *testing.T) {
if err != nil {
panic("rlp payload err")
}
action6 := types.NewAction(types.UpdateAccountAuthor, common.Name("a123456789addd"), common.Name("a123456789addd"), 1, 1, 2, big.NewInt(0), payload6)
action6 := types.NewAction(types.UpdateAccountAuthor, common.Name("a123456789addd"), common.Name(sysName), 1, 1, 2, big.NewInt(0), payload6)

tests = []struct {
name string
Expand Down Expand Up @@ -1770,7 +1770,7 @@ func TestAccountManager_SubAccount(t *testing.T) {
}

a10 := &AccountAction{
AccountName: common.Name("bbbbbbb"),
AccountName: common.Name("bbbbbb"),
Founder: common.Name(""),
ChargeRatio: 10,
PublicKey: pubkey,
Expand Down
1 change: 1 addition & 0 deletions asset/asset_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type AssetObject struct {
Owner common.Name `json:"owner,omitempty"`
AddIssue *big.Int `json:"addIssue"`
UpperLimit *big.Int `json:"upperLimit"`
Contract common.Name `json:"contract,omitempty"`
}

func NewAssetObject(assetName string, number uint64, symbol string, amount *big.Int, dec uint64, founder common.Name, owner common.Name, limit *big.Int) (*AssetObject, error) {
Expand Down
2 changes: 1 addition & 1 deletion asset/asset_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Test_newAssetObject(t *testing.T) {
wantErr bool
}{
// TODO: Add test cases.
{"normal", args{"ft", "ft", big.NewInt(2), 18, common.Name(""), common.Name("a123"), big.NewInt(999999)}, &AssetObject{0, 0, "ft", "ft", big.NewInt(2), 18, common.Name(""), common.Name("a123"), big.NewInt(2), big.NewInt(999999)}, false},
{"normal", args{"ft", "ft", big.NewInt(2), 18, common.Name(""), common.Name("a123"), big.NewInt(999999)}, &AssetObject{0, 0, "ft", "ft", big.NewInt(2), 18, common.Name(""), common.Name("a123"), big.NewInt(2), big.NewInt(999999), common.Name("")}, false},
{"shortname", args{"z", "z", big.NewInt(2), 18, common.Name("a123"), common.Name("a123"), big.NewInt(999999)}, nil, true},
{"longname", args{"ftt0123456789ftt12", "zz", big.NewInt(2), 18, common.Name("a123"), common.Name("a123"), big.NewInt(999999)}, nil, true},
{"emptyname", args{"", "z", big.NewInt(2), 18, common.Name("a123"), common.Name("a123"), big.NewInt(999999)}, nil, true},
Expand Down
12 changes: 8 additions & 4 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"math/big"
mrand "math/rand"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -114,7 +115,10 @@ func NewBlockChain(db fdb.Database, vmConfig vm.Config, chainConfig *params.Chai
futureBlocks: futureBlocks,
badBlocks: badBlocks,
senderCacher: senderCacher,
fcontroller: NewForkController(defaultForkConfig, chainConfig),
fcontroller: NewForkController(&ForkConfig{
ForkBlockNum: chainConfig.ForkedCfg.ForkBlockNum,
Forkpercentage: chainConfig.ForkedCfg.Forkpercentage,
}, chainConfig),
}

bc.genesisBlock = bc.GetBlockByNumber(0)
Expand Down Expand Up @@ -325,7 +329,7 @@ func (bc *BlockChain) insert(batch fdb.Batch, block *types.Block) {
// store cur block
// bc.currentBlock.Store(block)

if block.Coinbase().String() == bc.chainConfig.SysName.String() {
if strings.Compare(block.Coinbase().String(), bc.chainConfig.SysName) == 0 {
rawdb.WriteIrreversibleNumber(batch, block.NumberU64())
bc.irreversibleNumber.Store(block.NumberU64())
}
Expand Down Expand Up @@ -536,7 +540,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
currentBlock := bc.CurrentBlock()
localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64())

reorg := externTd.Cmp(localTd) > 0 || (block.Coinbase().String() == bc.chainConfig.SysName.String())
reorg := externTd.Cmp(localTd) > 0 || strings.Compare(block.Coinbase().String(), bc.chainConfig.SysName) == 0
if !reorg && externTd.Cmp(localTd) == 0 {
// Split same-difficulty blocks by number, then at random
reorg = block.NumberU64() < currentBlock.NumberU64() || (block.NumberU64() == currentBlock.NumberU64() && mrand.Float64() < 0.5)
Expand Down Expand Up @@ -715,7 +719,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []*types.Log, error)

func (bc *BlockChain) insertSideChain(block *types.Block) ([]*types.Log, error) {
var systemBlock bool
if block.Coinbase().String() == bc.chainConfig.SysName.String() {
if strings.Compare(block.Coinbase().String(), bc.chainConfig.SysName) == 0 {
systemBlock = true
}

Expand Down
2 changes: 1 addition & 1 deletion blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestSystemForkChain(t *testing.T) {

allCadidates1 = append(allCadidates1, allCadidates...)
allCadidates1 = append(allCadidates1, "syscadidate0")
allCadidates1 = append(allCadidates1, params.DefaultChainconfig.SysName.String())
allCadidates1 = append(allCadidates1, params.DefaultChainconfig.SysName)

allHeaderTimes1 = append(allHeaderTimes1, allHeaderTimes...)
allHeaderTimes1 = append(allHeaderTimes1, allHeaderTimes[len(allHeaderTimes)-1]+1000*uint64(time.Millisecond)*3*7)
Expand Down
4 changes: 2 additions & 2 deletions blockchain/forkcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewForkController(cfg *ForkConfig, chaincfg *params.ChainConfig) *ForkContr
func (fc *ForkController) getForkInfo(statedb *state.StateDB) (ForkInfo, error) {
info := ForkInfo{}

infoBytes, err := statedb.Get(fc.chainCfg.SysName.String(), forkInfo)
infoBytes, err := statedb.Get(fc.chainCfg.ChainName, forkInfo)
if err != nil {
return info, err
}
Expand All @@ -84,7 +84,7 @@ func (fc *ForkController) putForkInfo(info ForkInfo, statedb *state.StateDB) err
return err
}

statedb.Put(fc.chainCfg.SysName.String(), forkInfo, infoBytes)
statedb.Put(fc.chainCfg.ChainName, forkInfo, infoBytes)
return nil
}

Expand Down
108 changes: 6 additions & 102 deletions blockchain/gen_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,106 +16,10 @@

package blockchain

import (
"encoding/json"
"math/big"
// func (g *Genesis) MarshalJSON() ([]byte, error) {
// return json.Marshal(g)
// }

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
am "github.com/fractalplatform/fractal/accountmanager"
"github.com/fractalplatform/fractal/asset"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/consensus/dpos"
"github.com/fractalplatform/fractal/params"
)

func (g Genesis) MarshalJSON() ([]byte, error) {
type genesisJSON struct {
Config *params.ChainConfig `json:"config"`
Dpos *dpos.Config `json:"dpos"`
Nonce math.HexOrDecimal64 `json:"nonce"`
Timestamp math.HexOrDecimal64 `json:"timestamp"`
ExtraData hexutil.Bytes `json:"extraData"`
GasLimit math.HexOrDecimal64 `json:"gasLimit"`
Difficulty *math.HexOrDecimal256 `json:"difficulty"`
Mixhash common.Hash `json:"mixHash"`
Coinbase common.Name `json:"coinbase"`
AllocAccounts []*GenesisAccount `json:"allocAccounts"`
AllocAssets []*asset.AssetObject `json:"allocAssets"`
AccountNameLevel *am.Config `json:"accountNameLevel"`
AssetNameLevel *asset.Config `json:"assetNameLevel"`
}
var enc genesisJSON
enc.Config = g.Config
enc.Dpos = g.Dpos
enc.Timestamp = math.HexOrDecimal64(g.Timestamp)
enc.ExtraData = g.ExtraData
enc.GasLimit = math.HexOrDecimal64(g.GasLimit)
enc.Difficulty = (*math.HexOrDecimal256)(g.Difficulty)
enc.Coinbase = g.Coinbase
enc.AllocAccounts = g.AllocAccounts
enc.AllocAssets = g.AllocAssets
enc.AccountNameLevel = g.AccountNameLevel
enc.AssetNameLevel = g.AssetNameLevel
return json.Marshal(&enc)
}

func (g *Genesis) UnmarshalJSON(input []byte) error {
type genesisJSON struct {
Config *params.ChainConfig `json:"config"`
Dpos *dpos.Config `json:"dpos"`
Nonce *math.HexOrDecimal64 `json:"nonce"`
Timestamp *math.HexOrDecimal64 `json:"timestamp"`
ExtraData *hexutil.Bytes `json:"extraData"`
GasLimit *math.HexOrDecimal64 `json:"gasLimit"`
Difficulty *math.HexOrDecimal256 `json:"difficulty"`
Mixhash *common.Hash `json:"mixHash"`
Coinbase common.Name `json:"coinbase"`
AllocAccounts []*GenesisAccount `json:"allocAccounts"`
AllocAssets []*asset.AssetObject `json:"allocAssets"`
AccountNameLevel *am.Config `json:"accountNameLevel"`
AssetNameLevel *asset.Config `json:"assetNameLevel"`
}
var dec genesisJSON
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.Config != nil {
g.Config = dec.Config
}
if dec.Dpos != nil {
g.Dpos = dec.Dpos
}
if dec.Timestamp != nil {
g.Timestamp = uint64(*dec.Timestamp)
}
if dec.ExtraData != nil {
g.ExtraData = *dec.ExtraData
}
if dec.GasLimit != nil {
g.GasLimit = uint64(*dec.GasLimit)
}
if dec.Difficulty != nil {
g.Difficulty = (*big.Int)(dec.Difficulty)
}
if len(dec.Coinbase) > 0 {
g.Coinbase = dec.Coinbase
}
if len(dec.AllocAccounts) > 0 {
g.AllocAccounts = dec.AllocAccounts
}

if len(dec.AllocAssets) > 0 {
g.AllocAssets = dec.AllocAssets
}

if dec.AccountNameLevel != nil {
g.AccountNameLevel = dec.AccountNameLevel
}

if dec.AssetNameLevel != nil {
g.AssetNameLevel = dec.AssetNameLevel
}

return nil
}
// func (g *Genesis) UnmarshalJSON(input []byte) error {
// return json.Unmarshal(input, &g)
// }
57 changes: 8 additions & 49 deletions blockchain/gen_genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package blockchain

import (
"bytes"
"encoding/json"
"fmt"
"testing"
Expand All @@ -24,64 +25,22 @@ import (
func TestGenGenesis(t *testing.T) {
genesis := DefaultGenesis()

j, err := genesis.MarshalJSON()
j, err := json.Marshal(genesis)
if err != nil {
t.Fatal(fmt.Sprintf("genesis marshal --- %v", err))
}

if err := genesis.UnmarshalJSON(j); err != nil {
//fmt.Println(string(j))

if err := json.Unmarshal(j, &genesis); err != nil {
t.Fatal(fmt.Sprintf("genesis Unmarshal --- %v", err))
}
_, err = genesis.MarshalJSON()
nj, err := json.Marshal(genesis)
if err != nil {
t.Fatal(fmt.Sprintf("genesis marshal --- %v", err))
}

s := `{
"config": {
"chainId": 1,
"sysName": "ftsystemio",
"sysToken": "fractalfoundation"
},
"dpos": {
"MaxURLLen": 512,
"UnitStake": 1000,
"CadidateMinQuantity": 10000,
"VoterMinQuantity": 1,
"ActivatedMinQuantity": 1000000,
"BlockInterval": 3000000000,
"BlockFrequency": 6,
"CadidateScheduleSize": 21,
"DelayEcho": 0,
"AccountName": "fdpos",
"SystemName": "ft",
"ExtraBlockRewardUnit": 1,
"BlockReward": 20
},
"timestamp": "0x0",
"extraData": "0x5a302047656e6573697320426c6f636b",
"gasLimit": "0x5f5e100",
"difficulty": "0x20000",
"coinbase": "systemio",
"allocAccounts": [{
"name": "ftsystemio",
"pubKey": "0x047db227d7094ce215c3a0f57e1bcc732551fe351f94249471934567e0f5dc1bf795962b8cccb87a2eb56b29fbe37d614e2f4c3c45b789ae4f1f51f4cb21972ffd"
}],
"allocAssets": [{
"assetname": "ftperfoundation",
"symbol": "ft",
"amount": 100000000,
"decimals": 18,
"owner": "ftsystemio"
}]
}`
if err := json.Unmarshal([]byte(s), genesis); err != nil {
t.Fatal(fmt.Sprintf("genesis unmarshal --- %v", err))
}

j, err = json.Marshal(genesis)
if err != nil {
t.Fatal(fmt.Sprintf("genesis marshal --- %v", err))
if bytes.Compare(j, nj) != 0 {
t.Fatal(fmt.Sprintf("genesis mismatch --- %v", err))
}

}

0 comments on commit 0b888d9

Please sign in to comment.