Skip to content

Commit

Permalink
forbidden new api when height is not reached (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
siovanus committed May 22, 2020
1 parent a82eb22 commit ead2a01
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 12 deletions.
26 changes: 15 additions & 11 deletions smartcontract/service/native/ontid/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package ontid

import (
"github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/smartcontract/service/native"
"github.com/ontio/ontology/smartcontract/service/native/utils"
)
Expand All @@ -36,22 +37,31 @@ func RegisterIDContract(srvc *native.NativeService) {
srvc.Register("changeRecovery", changeRecovery)
srvc.Register("setRecovery", setRecovery)
srvc.Register("updateRecovery", updateRecovery)
srvc.Register("removeRecovery", removeRecovery)
srvc.Register("addKey", addKey)
srvc.Register("addKeyByIndex", addKeyByIndex)
srvc.Register("removeKey", removeKey)
srvc.Register("removeKeyByIndex", removeKeyByIndex)
srvc.Register("addKeyByController", addKeyByController)
srvc.Register("removeKeyByController", removeKeyByController)
srvc.Register("addKeyByRecovery", addKeyByRecovery)
srvc.Register("removeKeyByRecovery", removeKeyByRecovery)
srvc.Register("regIDWithAttributes", regIdWithAttributes)
srvc.Register("addAttributes", addAttributes)
srvc.Register("addAttributesByIndex", addAttributesByIndex)
srvc.Register("removeAttribute", removeAttribute)
srvc.Register("removeAttributeByIndex", removeAttributeByIndex)
srvc.Register("addAttributesByController", addAttributesByController)
srvc.Register("removeAttributeByController", removeAttributeByController)
srvc.Register("verifySignature", verifySignature)
srvc.Register("verifyController", verifyController)
srvc.Register("getPublicKeys", GetPublicKeys)
srvc.Register("getKeyState", GetKeyState)
srvc.Register("getAttributes", GetAttributes)
srvc.Register("getDDO", GetDDO)
if srvc.Height < config.GetNewOntIdHeight() {
return
}
srvc.Register("removeRecovery", removeRecovery)
srvc.Register("addKeyByIndex", addKeyByIndex)
srvc.Register("removeKeyByIndex", removeKeyByIndex)
srvc.Register("addAttributesByIndex", addAttributesByIndex)
srvc.Register("removeAttributeByIndex", removeAttributeByIndex)
srvc.Register("addNewAuthKey", addNewAuthKey)
srvc.Register("addNewAuthKeyByRecovery", addNewAuthKeyByRecovery)
srvc.Register("addNewAuthKeyByController", addNewAuthKeyByController)
Expand All @@ -67,15 +77,9 @@ func RegisterIDContract(srvc *native.NativeService) {
srvc.Register("addContext", addContext)
srvc.Register("removeContext", removeContext)
srvc.Register("addProof", addProof)
srvc.Register("verifySignature", verifySignature)
srvc.Register("verifyController", verifyController)
srvc.Register("getPublicKeys", GetPublicKeys)
srvc.Register("getPublicKeysJson", GetPublicKeysJson)
srvc.Register("getKeyState", GetKeyState)
srvc.Register("getAttributesJson", GetAttributesJson)
srvc.Register("getAttributes", GetAttributes)
srvc.Register("getAttributeByKey", GetAttributeByKey)
srvc.Register("getDDO", GetDDO)
srvc.Register("getServiceJson", GetServiceJson)
srvc.Register("getControllerJson", GetControllerJson)
srvc.Register("getDocumentJson", GetDocumentJson)
Expand Down
177 changes: 176 additions & 1 deletion smartcontract/service/native/ontid/ontid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ import (
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology/account"
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/smartcontract/service/native"
"github.com/ontio/ontology/smartcontract/service/native/testsuite"
"github.com/ontio/ontology/smartcontract/service/native/utils"
"github.com/stretchr/testify/assert"
)

func init() {
Init()
}

func testcase(t *testing.T, f func(t *testing.T, n *native.NativeService)) {
testsuite.InvokeNativeContract(t, utils.OntIDContractAddress,
func(n *native.NativeService) ([]byte, error) {
Expand All @@ -56,6 +62,11 @@ func TestOwnerSize(t *testing.T) {
testcase(t, CaseOwnerSize)
}

func TestNewApiFork(t *testing.T) {
testcase(t, CaseBeforeNewApiFork)
testcase(t, CaseAfterNewApiFork)
}

// Register id with account acc
func regID(n *native.NativeService, id string, a *account.Account) error {
// make arguments
Expand Down Expand Up @@ -343,7 +354,7 @@ func CaseOwnerSize(t *testing.T, n *native.NativeService) {
}

func CaseGetDocument(t *testing.T, n *native.NativeService) {
n.Height = 10000000
n.Height = config.GetNewOntIdHeight()
// 1. register ID
id0, _ := account.GenerateID()
a0 := account.NewAccount("")
Expand Down Expand Up @@ -486,3 +497,167 @@ func CaseGetDocument(t *testing.T, n *native.NativeService) {
}
fmt.Println(string(res))
}

func CaseBeforeNewApiFork(t *testing.T, n *native.NativeService) {
// 1. register ID
id0, _ := account.GenerateID()
a0 := account.NewAccount("")
id1, _ := account.GenerateID()
a1 := account.NewAccount("")
id2, _ := account.GenerateID()
a2 := account.NewAccount("")
if err := regID(n, id0, a0); err != nil {
t.Fatal("register ID error", err)
}
if err := regID(n, id1, a1); err != nil {
t.Fatal("register ID error", err)
}
if err := regID(n, id2, a2); err != nil {
t.Fatal("register ID error", err)
}

// 2. add context before fork height
var contexts = [][]byte{[]byte("https://www.w3.org/ns0/did/v1"), []byte("https://ontid.ont.io0/did/v1"), []byte("https://ontid.ont.io0/did/v1")}
context := &Context{
OntId: []byte(id0),
Contexts: contexts,
Index: 1,
}
sink := common.NewZeroCopySink(nil)
context.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err := n.NativeCall(utils.OntIDContractAddress, "addContext", sink.Bytes())
assert.Contains(t, err.Error(), "doesn't support this function")

// 3. add auth key before fork height
newPublicKey := &NewPublicKey{
key: keypair.SerializePublicKey(a2.PublicKey),
controller: []byte(id2),
}
authKeyParam := &AddNewAuthKeyParam{
OntId: []byte(id0),
NewPublicKey: newPublicKey,
SignIndex: 1,
}

sink.Reset()
authKeyParam.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err = n.NativeCall(utils.OntIDContractAddress, "addNewAuthKey", sink.Bytes())
assert.Contains(t, err.Error(), "doesn't support this function")

// 4. set auth key before fork height
setAuthKeyParam := &SetAuthKeyParam{
OntId: []byte(id0),
Index: 1,
SignIndex: 1,
}

sink.Reset()
setAuthKeyParam.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err = n.NativeCall(utils.OntIDContractAddress, "setAuthKey", sink.Bytes())
assert.Contains(t, err.Error(), "doesn't support this function")

// 5. add service before fork height
service := &ServiceParam{
OntId: []byte(id0),
ServiceId: []byte("someService"),
Type: []byte("sss"),
ServiceEndpint: []byte("http;;s;s;s;;s"),
Index: 1,
}

sink.Reset()
service.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err = n.NativeCall(utils.OntIDContractAddress, "addService", sink.Bytes())
assert.Contains(t, err.Error(), "doesn't support this function")

// 6. get document before fork height
_, err = n.NativeCall(utils.OntIDContractAddress, "getDocumentJson", sink.Bytes())
assert.Contains(t, err.Error(), "doesn't support this function")
}

func CaseAfterNewApiFork(t *testing.T, n *native.NativeService) {
n.Height = config.GetNewOntIdHeight()
// 1. register ID
id0, _ := account.GenerateID()
a0 := account.NewAccount("")
id1, _ := account.GenerateID()
a1 := account.NewAccount("")
id2, _ := account.GenerateID()
a2 := account.NewAccount("")
if err := regID(n, id0, a0); err != nil {
t.Fatal("register ID error", err)
}
if err := regID(n, id1, a1); err != nil {
t.Fatal("register ID error", err)
}
if err := regID(n, id2, a2); err != nil {
t.Fatal("register ID error", err)
}

// 2. add context after fork height
var contexts = [][]byte{[]byte("https://www.w3.org/ns0/did/v1"), []byte("https://ontid.ont.io0/did/v1"), []byte("https://ontid.ont.io0/did/v1")}
context := &Context{
OntId: []byte(id0),
Contexts: contexts,
Index: 1,
}
sink := common.NewZeroCopySink(nil)
context.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err := n.NativeCall(utils.OntIDContractAddress, "addContext", sink.Bytes())
assert.NoError(t, err)

// 3. add auth key after fork height
newPublicKey := &NewPublicKey{
key: keypair.SerializePublicKey(a2.PublicKey),
controller: []byte(id2),
}
authKeyParam := &AddNewAuthKeyParam{
OntId: []byte(id0),
NewPublicKey: newPublicKey,
SignIndex: 1,
}

sink.Reset()
authKeyParam.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err = n.NativeCall(utils.OntIDContractAddress, "addNewAuthKey", sink.Bytes())
assert.NoError(t, err)

// 4. set auth key after fork height
setAuthKeyParam := &SetAuthKeyParam{
OntId: []byte(id0),
Index: 1,
SignIndex: 1,
}

sink.Reset()
setAuthKeyParam.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err = n.NativeCall(utils.OntIDContractAddress, "setAuthKey", sink.Bytes())
assert.NoError(t, err)

// 5. add service after fork height
service := &ServiceParam{
OntId: []byte(id0),
ServiceId: []byte("someService"),
Type: []byte("sss"),
ServiceEndpint: []byte("http;;s;s;s;;s"),
Index: 1,
}

sink.Reset()
service.Serialization(sink)
n.Tx.SignedAddr = []common.Address{a0.Address}
_, err = n.NativeCall(utils.OntIDContractAddress, "addService", sink.Bytes())
assert.NoError(t, err)

// 6. get document after fork height
res, err := n.NativeCall(utils.OntIDContractAddress, "getDocumentJson", sink.Bytes())
assert.NoError(t, err)
fmt.Println(string(res))
}

0 comments on commit ead2a01

Please sign in to comment.