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

Deploy native smart contract #433

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions account/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func CreateID(nonce []byte) (string, error) {
return SCHEME + ":" + METHOD + ":" + string(idstring), nil
}

func VerifyID(id string) bool {
if len(id) < 9 {
func VerifyID(method, id string) bool {
if len(id) < 9 || len(method) != 3 {
return false
}
if id[0:8] != "did:dna:" {
if id[0:8] != "did:"+method+":" {
return false
}
buf, err := base58.BitcoinEncoding.Decode([]byte(id[8:]))
Expand Down
4 changes: 2 additions & 2 deletions account/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCreate(t *testing.T) {

func TestVerify(t *testing.T) {
t.Log("verify", id)
if !VerifyID(id) {
if !VerifyID("dna", id) {
t.Error("error: failed")
}

Expand All @@ -56,7 +56,7 @@ func TestVerify(t *testing.T) {

for _, v := range invalid {
t.Log("verify", v)
if VerifyID(v) {
if VerifyID("dna", v) {
t.Error("error: passed")
}
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/sigsvr/handlers/sig_native_invoke_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ package handlers

import (
"encoding/json"
"testing"

"github.com/DNAProject/DNA/cmd/abi"
clisvrcom "github.com/DNAProject/DNA/cmd/sigsvr/common"
nutils "github.com/DNAProject/DNA/smartcontract/service/native/utils"
"github.com/DNAProject/DNA/smartcontract/service/native/common"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology-crypto/signature"
"testing"
)

func TestSigNativeInvokeTx(t *testing.T) {
Expand All @@ -46,7 +47,7 @@ func TestSigNativeInvokeTx(t *testing.T) {
invokeReq := &SigNativeInvokeTxReq{
GasPrice: 0,
GasLimit: 40000,
Address: nutils.GasContractAddress.ToHexString(),
Address: common.GasContractAddress.ToHexString(),
Method: "transfer",
Version: 0,
Params: []interface{}{
Expand Down
8 changes: 4 additions & 4 deletions cmd/utils/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import (
cutils "github.com/DNAProject/DNA/core/utils"
httpcom "github.com/DNAProject/DNA/http/base/common"
rpccommon "github.com/DNAProject/DNA/http/base/common"
common2 "github.com/DNAProject/DNA/smartcontract/service/native/common"
"github.com/DNAProject/DNA/smartcontract/service/native/gas"
"github.com/DNAProject/DNA/smartcontract/service/native/utils"
cstates "github.com/DNAProject/DNA/smartcontract/states"
"github.com/ontio/ontology-crypto/keypair"
sig "github.com/ontio/ontology-crypto/signature"
Expand Down Expand Up @@ -194,7 +194,7 @@ func ApproveTx(gasPrice, gasLimit uint64, asset string, from, to string, amount
switch strings.ToLower(asset) {
case ASSET_GAS:
version = VERSION_CONTRACT_GAS
contractAddr = utils.GasContractAddress
contractAddr = common2.GasContractAddress
default:
return nil, fmt.Errorf("Unsupport asset:%s", asset)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TransferTx(gasPrice, gasLimit uint64, asset, from, to string, amount uint64
switch strings.ToLower(asset) {
case ASSET_GAS:
version = VERSION_CONTRACT_GAS
contractAddr = utils.GasContractAddress
contractAddr = common2.GasContractAddress
default:
return nil, fmt.Errorf("unsupport asset:%s", asset)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func TransferFromTx(gasPrice, gasLimit uint64, asset, sender, from, to string, a
switch strings.ToLower(asset) {
case ASSET_GAS:
version = VERSION_CONTRACT_GAS
contractAddr = utils.GasContractAddress
contractAddr = common2.GasContractAddress
default:
return nil, fmt.Errorf("unsupport asset:%s", asset)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/utils/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ func ParseReturnValue(rawValue interface{}, rawReturnTypeStr string, vmtype payl
if !ok {
rawValues = append(rawValues, rawValue)
}
if vmtype == payload.NEOVM_TYPE {
return parseReturnNeoValueArray(rawValues, returnTypes)
} else {
if vmtype == payload.WASMVM_TYPE {
return parasReturnValueWasmArray(rawValues, returnTypes)
} else {
return parseReturnNeoValueArray(rawValues, returnTypes)
}

}
Expand Down
4 changes: 2 additions & 2 deletions consensus/vbft/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import (
"github.com/DNAProject/DNA/events"
"github.com/DNAProject/DNA/events/message"
p2pmsg "github.com/DNAProject/DNA/p2pserver/message/types"
common2 "github.com/DNAProject/DNA/smartcontract/service/native/common"
gover "github.com/DNAProject/DNA/smartcontract/service/native/governance"
ninit "github.com/DNAProject/DNA/smartcontract/service/native/init"
nutils "github.com/DNAProject/DNA/smartcontract/service/native/utils"
"github.com/DNAProject/DNA/validator/increment"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology-crypto/vrf"
Expand Down Expand Up @@ -2132,7 +2132,7 @@ func (self *Server) msgSendLoop() {

//creategovernaceTransaction invoke governance native contract commit_pos
func (self *Server) creategovernaceTransaction(blkNum uint32) (*types.Transaction, error) {
mutable := utils.BuildNativeTransaction(nutils.GovernanceContractAddress, gover.COMMIT_DPOS, []byte{})
mutable := utils.BuildNativeTransaction(common2.GovernanceContractAddress, gover.COMMIT_DPOS, []byte{})
mutable.Nonce = blkNum
tx, err := mutable.IntoImmutable()
return tx, err
Expand Down
10 changes: 5 additions & 5 deletions consensus/vbft/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (
"github.com/DNAProject/DNA/core/states"
scommon "github.com/DNAProject/DNA/core/store/common"
"github.com/DNAProject/DNA/core/store/overlaydb"
common2 "github.com/DNAProject/DNA/smartcontract/service/native/common"
gov "github.com/DNAProject/DNA/smartcontract/service/native/governance"
nutils "github.com/DNAProject/DNA/smartcontract/service/native/utils"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology-crypto/vrf"
)
Expand Down Expand Up @@ -138,7 +138,7 @@ func GetVbftConfigInfo(memdb *overlaydb.MemDB) (*config.VBFTConfig, error) {

//get preConfig
preCfg := new(gov.PreConfig)
data, err := GetStorageValue(memdb, ledger.DefLedger, nutils.GovernanceContractAddress, []byte(gov.PRE_CONFIG))
data, err := GetStorageValue(memdb, ledger.DefLedger, common2.GovernanceContractAddress, []byte(gov.PRE_CONFIG))
if err != nil && err != scommon.ErrNotFound {
return nil, err
}
Expand All @@ -162,7 +162,7 @@ func GetVbftConfigInfo(memdb *overlaydb.MemDB) (*config.VBFTConfig, error) {
MaxBlockChangeView: uint32(preCfg.Configuration.MaxBlockChangeView),
}
} else {
data, err := GetStorageValue(memdb, ledger.DefLedger, nutils.GovernanceContractAddress, []byte(gov.VBFT_CONFIG))
data, err := GetStorageValue(memdb, ledger.DefLedger, common2.GovernanceContractAddress, []byte(gov.VBFT_CONFIG))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,7 +195,7 @@ func GetPeersConfig(memdb *overlaydb.MemDB) ([]*config.VBFTPeerStakeInfo, error)
return nil, err
}
key := append([]byte(gov.PEER_POOL), viewBytes...)
data, err := GetStorageValue(memdb, ledger.DefLedger, nutils.GovernanceContractAddress, key)
data, err := GetStorageValue(memdb, ledger.DefLedger, common2.GovernanceContractAddress, key)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func GetStorageValue(memdb *overlaydb.MemDB, backend *ledger.Ledger, addr common
}

func GetGovernanceView(memdb *overlaydb.MemDB) (*gov.GovernanceView, error) {
value, err := GetStorageValue(memdb, ledger.DefLedger, nutils.GovernanceContractAddress, []byte(gov.GOVERNANCE_VIEW))
value, err := GetStorageValue(memdb, ledger.DefLedger, common2.GovernanceContractAddress, []byte(gov.GOVERNANCE_VIEW))
if err != nil {
return nil, err
}
Expand Down
63 changes: 43 additions & 20 deletions core/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/DNAProject/DNA/core/payload"
"github.com/DNAProject/DNA/core/types"
"github.com/DNAProject/DNA/core/utils"
common2 "github.com/DNAProject/DNA/smartcontract/service/native/common"
"github.com/DNAProject/DNA/smartcontract/service/native/gas"
"github.com/DNAProject/DNA/smartcontract/service/native/global_params"
"github.com/DNAProject/DNA/smartcontract/service/native/governance"
Expand Down Expand Up @@ -98,29 +99,33 @@ func BuildGenesisBlock(defaultBookkeeper []keypair.PublicKey, genesisConfig *con
//block
gas := newUtilityToken()
param := newParamContract()
oid := deployOntIDContract()
auth := deployAuthContract()
govConfigTx := newGovConfigTx()
did := deployBaseDIDContract()
auth := deployBaseAuthContract()
gov := deploygGovContract()
didInit := newDidInit("dna")
authInit := newAuthInit(common2.DIDContractAddress[:])

genesisBlock := &types.Block{
Header: genesisHeader,
Transactions: []*types.Transaction{
gas,
param,
oid,
did,
auth,
govConfigTx,
gov,
newUtilityInit(),
newParamInit(),
govConfig,
didInit,
authInit,
},
}
genesisBlock.RebuildMerkleRoot()
return genesisBlock, nil
}

func newUtilityToken() *types.Transaction {
mutable, err := utils.NewDeployTransaction(nutils.GasContractAddress[:], "GAS", "1.0",
mutable, err := utils.NewDeployTransaction(common2.GasContractAddress[:], "GAS", "1.0",
"DNA Dev Team", "[email protected]", "Blockchain Network Gas", payload.NEOVM_TYPE)
if err != nil {
panic("[NewDeployTransaction] construct genesis utility token transaction error ")
Expand All @@ -133,7 +138,7 @@ func newUtilityToken() *types.Transaction {
}

func newParamContract() *types.Transaction {
mutable, err := utils.NewDeployTransaction(nutils.ParamContractAddress[:],
mutable, err := utils.NewDeployTransaction(common2.ParamContractAddress[:],
"ParamConfig", "1.0", "DNA Dev Team", "[email protected]",
"Chain Global Environment Variables Manager ", payload.NEOVM_TYPE)
if err != nil {
Expand All @@ -146,9 +151,9 @@ func newParamContract() *types.Transaction {
return tx
}

func newGovConfigTx() *types.Transaction {
mutable, err := utils.NewDeployTransaction(nutils.GovernanceContractAddress[:], "CONFIG", "1.0",
"DNA Dev Team", "contact@ont.io", "Blockchain Network Consensus Config", payload.NEOVM_TYPE)
func deploygGovContract() *types.Transaction {
mutable, err := utils.NewDeployTransaction(common2.GovernanceContractAddress[:], "CONFIG", "1.0",
"DNA Dev Team", "contact@onchain.com", "Blockchain Network Consensus Config", payload.NEOVM_TYPE)
if err != nil {
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
}
Expand All @@ -159,9 +164,9 @@ func newGovConfigTx() *types.Transaction {
return tx
}

func deployAuthContract() *types.Transaction {
mutable, err := utils.NewDeployTransaction(nutils.AuthContractAddress[:], "AuthContract", "1.0",
"DNA Dev Team", "contact@ont.io", "Blockchain Network Authorization Contract", payload.NEOVM_TYPE)
func deployBaseAuthContract() *types.Transaction {
mutable, err := utils.NewDeployTransaction(common2.AuthContractAddress[:], "AuthContract", "1.0",
"DNA Dev Team", "contact@onchain.com", "Blockchain Network Authorization Contract", payload.NEOVM_TYPE)
if err != nil {
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
}
Expand All @@ -172,15 +177,15 @@ func deployAuthContract() *types.Transaction {
return tx
}

func deployOntIDContract() *types.Transaction {
mutable, err := utils.NewDeployTransaction(nutils.DIDContractAddress[:], "OID", "1.0",
"DNA Dev Team", "contact@ont.io", "Blockchain Network ONT ID", payload.NEOVM_TYPE)
func deployBaseDIDContract() *types.Transaction {
mutable, err := utils.NewDeployTransaction(common2.DIDContractAddress[:], "DID", "1.0",
"DNA Dev Team", "contact@onchain.com", "Blockchain Network DID", payload.NEOVM_TYPE)
if err != nil {
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
}
tx, err := mutable.IntoImmutable()
if err != nil {
panic("construct genesis ontid transaction error ")
panic("construct genesis DID transaction error ")
}
return tx
}
Expand Down Expand Up @@ -212,7 +217,7 @@ func newUtilityInit() *types.Transaction {
nutils.EncodeVarUint(args, part.value)
}

mutable := utils.BuildNativeTransaction(nutils.GasContractAddress, gas.INIT_NAME, args.Bytes())
mutable := utils.BuildNativeTransaction(common2.GasContractAddress, gas.INIT_NAME, args.Bytes())
tx, err := mutable.IntoImmutable()
if err != nil {
panic("construct genesis governing token transaction error ")
Expand Down Expand Up @@ -253,7 +258,7 @@ func newParamInit() *types.Transaction {
}
nutils.EncodeAddress(sink, addr)

mutable := utils.BuildNativeTransaction(nutils.ParamContractAddress, global_params.INIT_NAME, sink.Bytes())
mutable := utils.BuildNativeTransaction(common2.ParamContractAddress, global_params.INIT_NAME, sink.Bytes())
tx, err := mutable.IntoImmutable()
if err != nil {
panic("construct genesis governing token transaction error ")
Expand All @@ -262,10 +267,28 @@ func newParamInit() *types.Transaction {
}

func newGoverConfigInit(config []byte) *types.Transaction {
mutable := utils.BuildNativeTransaction(nutils.GovernanceContractAddress, governance.INIT_CONFIG, config)
mutable := utils.BuildNativeTransaction(common2.GovernanceContractAddress, governance.INIT_CONFIG, config)
tx, err := mutable.IntoImmutable()
if err != nil {
panic("construct genesis governing token transaction error ")
}
return tx
}

func newDidInit(didMethod string) *types.Transaction {
mutable := utils.BuildNativeTransaction(common2.DIDContractAddress, "initDID", []byte(didMethod))
tx, err := mutable.IntoImmutable()
if err != nil {
panic(fmt.Sprintf("construct did init transaction error: %s", err))
}
return tx
}

func newAuthInit(didBaseContract []byte) *types.Transaction {
mutable := utils.BuildNativeTransaction(common2.AuthContractAddress, "initAuth", didBaseContract)
tx, err := mutable.IntoImmutable()
if err != nil {
panic(fmt.Sprintf("construct auth init transaction error: %s", err))
}
return tx
}
Loading