Sitelet https://github.com/DAD-Chain/dad-go/commit/981f363a4bb8ae330a654534d896256394926f6f
Skip to content

Commit 981f363

Browse files
author
tomdd
committed
add vmType check (#1100)
1 parent d2f1e84 commit 981f363

22 files changed

Lines changed: 183 additions & 284 deletions

‎cmd/utils/ont.go‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,10 @@ func DeployContract(
605605
if err != nil {
606606
return "", fmt.Errorf("hex.DecodeString error:%s", err)
607607
}
608-
mutable := NewDeployCodeTransaction(gasPrice, gasLimit, c, vmtype, cname, cversion, cauthor, cemail, cdesc)
608+
mutable, err := NewDeployCodeTransaction(gasPrice, gasLimit, c, vmtype, cname, cversion, cauthor, cemail, cdesc)
609+
if err != nil {
610+
return "", err
611+
}
609612

610613
err = SignTransaction(signer, mutable)
611614
if err != nil {
@@ -634,7 +637,10 @@ func PrepareDeployContract(
634637
if err != nil {
635638
return nil, fmt.Errorf("hex.DecodeString error:%s", err)
636639
}
637-
mutable := NewDeployCodeTransaction(0, 0, c, vmtype, cname, cversion, cauthor, cemail, cdesc)
640+
mutable, err := NewDeployCodeTransaction(0, 0, c, vmtype, cname, cversion, cauthor, cemail, cdesc)
641+
if err != nil {
642+
return nil, fmt.Errorf("NewDeployCodeTransaction error:%s", err)
643+
}
638644
tx, _ := mutable.IntoImmutable()
639645
var buffer bytes.Buffer
640646
err = tx.Serialize(&buffer)
@@ -776,17 +782,12 @@ func PrepareInvokeNativeContract(
776782

777783
//NewDeployCodeTransaction return a smart contract deploy transaction instance
778784
func NewDeployCodeTransaction(gasPrice, gasLimit uint64, code []byte, vmType payload.VmType,
779-
cname, cversion, cauthor, cemail, cdesc string) *types.MutableTransaction {
785+
cname, cversion, cauthor, cemail, cdesc string) (*types.MutableTransaction, error) {
780786

781-
deployPayload := &payload.DeployCode{
782-
Code: code,
783-
Name: cname,
784-
Version: cversion,
785-
Author: cauthor,
786-
Email: cemail,
787-
Description: cdesc,
787+
deployPayload, err := payload.NewDeployCode(code, vmType, cname, cversion, cauthor, cemail, cdesc)
788+
if err != nil {
789+
return nil, err
788790
}
789-
deployPayload.SetVmType(vmType)
790791
tx := &types.MutableTransaction{
791792
Version: VERSION_TRANSACTION,
792793
TxType: types.Deploy,
@@ -796,7 +797,7 @@ func NewDeployCodeTransaction(gasPrice, gasLimit uint64, code []byte, vmType pay
796797
GasLimit: gasLimit,
797798
Sigs: make([]types.Sig, 0, 0),
798799
}
799-
return tx
800+
return tx, nil
800801
}
801802

802803
//ParseNeoVMContractReturnTypeBool return bool value of smart contract execute code.

‎core/genesis/genesis.go‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ func BuildGenesisBlock(defaultBookkeeper []keypair.PublicKey, genesisConfig *con
120120
}
121121

122122
func newGoverningToken() *types.Transaction {
123-
mutable := utils.NewDeployTransaction(nutils.OntContractAddress[:], "ONT", "1.0",
123+
mutable, err := utils.NewDeployTransaction(nutils.OntContractAddress[:], "ONT", "1.0",
124124
"dad-go Team", "contact@ont.io", "dad-go Network ONT Token", payload.NEOVM_TYPE)
125+
if err != nil {
126+
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
127+
}
125128
tx, err := mutable.IntoImmutable()
126129
if err != nil {
127130
panic("construct genesis governing token transaction error ")
@@ -130,8 +133,11 @@ func newGoverningToken() *types.Transaction {
130133
}
131134

132135
func newUtilityToken() *types.Transaction {
133-
mutable := utils.NewDeployTransaction(nutils.OngContractAddress[:], "ONG", "1.0",
136+
mutable, err := utils.NewDeployTransaction(nutils.OngContractAddress[:], "ONG", "1.0",
134137
"dad-go Team", "contact@ont.io", "dad-go Network ONG Token", payload.NEOVM_TYPE)
138+
if err != nil {
139+
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
140+
}
135141
tx, err := mutable.IntoImmutable()
136142
if err != nil {
137143
panic("construct genesis utility token transaction error ")
@@ -140,9 +146,12 @@ func newUtilityToken() *types.Transaction {
140146
}
141147

142148
func newParamContract() *types.Transaction {
143-
mutable := utils.NewDeployTransaction(nutils.ParamContractAddress[:],
149+
mutable, err := utils.NewDeployTransaction(nutils.ParamContractAddress[:],
144150
"ParamConfig", "1.0", "dad-go Team", "contact@ont.io",
145151
"Chain Global Environment Variables Manager ", payload.NEOVM_TYPE)
152+
if err != nil {
153+
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
154+
}
146155
tx, err := mutable.IntoImmutable()
147156
if err != nil {
148157
panic("construct genesis param transaction error ")
@@ -151,8 +160,11 @@ func newParamContract() *types.Transaction {
151160
}
152161

153162
func newGovConfigTx() *types.Transaction {
154-
mutable := utils.NewDeployTransaction(nutils.GovernanceContractAddress[:], "CONFIG", "1.0",
163+
mutable, err := utils.NewDeployTransaction(nutils.GovernanceContractAddress[:], "CONFIG", "1.0",
155164
"dad-go Team", "contact@ont.io", "dad-go Network Consensus Config", payload.NEOVM_TYPE)
165+
if err != nil {
166+
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
167+
}
156168
tx, err := mutable.IntoImmutable()
157169
if err != nil {
158170
panic("construct genesis config transaction error ")
@@ -161,8 +173,11 @@ func newGovConfigTx() *types.Transaction {
161173
}
162174

163175
func deployAuthContract() *types.Transaction {
164-
mutable := utils.NewDeployTransaction(nutils.AuthContractAddress[:], "AuthContract", "1.0",
176+
mutable, err := utils.NewDeployTransaction(nutils.AuthContractAddress[:], "AuthContract", "1.0",
165177
"dad-go Team", "contact@ont.io", "dad-go Network Authorization Contract", payload.NEOVM_TYPE)
178+
if err != nil {
179+
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
180+
}
166181
tx, err := mutable.IntoImmutable()
167182
if err != nil {
168183
panic("construct genesis auth transaction error ")
@@ -171,8 +186,11 @@ func deployAuthContract() *types.Transaction {
171186
}
172187

173188
func deployOntIDContract() *types.Transaction {
174-
mutable := utils.NewDeployTransaction(nutils.OntIDContractAddress[:], "OID", "1.0",
189+
mutable, err := utils.NewDeployTransaction(nutils.OntIDContractAddress[:], "OID", "1.0",
175190
"dad-go Team", "contact@ont.io", "dad-go Network ONT ID", payload.NEOVM_TYPE)
191+
if err != nil {
192+
panic("[NewDeployTransaction] construct genesis governing token transaction error ")
193+
}
176194
tx, err := mutable.IntoImmutable()
177195
if err != nil {
178196
panic("construct genesis ontid transaction error ")

‎core/payload/bookkeeper.go‎

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"io"
2424

2525
"github.com/ontio/dad-go-crypto/keypair"
26-
"github.com/ontio/dad-go/common/serialization"
26+
"github.com/ontio/dad-go/common"
2727
)
2828

2929
const BookkeeperPayloadVersion byte = 0x00
@@ -43,57 +43,48 @@ type Bookkeeper struct {
4343
Issuer keypair.PublicKey
4444
}
4545

46-
// Serialize serialize Bookkeeper into io.Writer
47-
func (self *Bookkeeper) Serialize(w io.Writer) error {
48-
err := serialization.WriteVarBytes(w, keypair.SerializePublicKey(self.PubKey))
49-
if err != nil {
50-
return fmt.Errorf("[Bookkeeper], serializing PubKey failed: %s", err)
51-
}
52-
err = serialization.WriteVarBytes(w, []byte{byte(self.Action)})
53-
if err != nil {
54-
return fmt.Errorf("[Bookkeeper], serializing Action failed: %s", err)
55-
}
56-
err = serialization.WriteVarBytes(w, self.Cert)
57-
if err != nil {
58-
return fmt.Errorf("[Bookkeeper], serializing Cert failed: %s", err)
46+
func (self *Bookkeeper) Serialization(sink *common.ZeroCopySink) {
47+
sink.WriteVarBytes(keypair.SerializePublicKey(self.PubKey))
48+
sink.WriteByte(byte(self.Action))
49+
sink.WriteVarBytes(self.Cert)
50+
sink.WriteVarBytes(keypair.SerializePublicKey(self.Issuer))
51+
}
52+
func (self *Bookkeeper) Deserialization(source *common.ZeroCopySource) error {
53+
pubKey, _, irregular, eof := source.NextVarBytes()
54+
if irregular {
55+
return common.ErrIrregularData
5956
}
60-
err = serialization.WriteVarBytes(w, keypair.SerializePublicKey(self.Issuer))
61-
if err != nil {
62-
return fmt.Errorf("[Bookkeeper], serializing Issuer failed: %s", err)
57+
if eof {
58+
return io.ErrUnexpectedEOF
6359
}
64-
return nil
65-
}
66-
67-
// Deserialize deserialize Bookkeeper from io.Reader
68-
func (self *Bookkeeper) Deserialize(r io.Reader) error {
69-
buf, err := serialization.ReadVarBytes(r)
60+
var err error
61+
self.PubKey, err = keypair.DeserializePublicKey(pubKey)
7062
if err != nil {
7163
return fmt.Errorf("[Bookkeeper], deserializing PubKey failed: %s", err)
7264
}
73-
self.PubKey, err = keypair.DeserializePublicKey(buf)
74-
if err != nil {
75-
return fmt.Errorf("[Bookkeeper], deserializing PubKey failed: %s", err)
65+
action, eof := source.NextByte()
66+
if eof {
67+
return io.ErrUnexpectedEOF
7668
}
77-
78-
var p [1]byte
79-
_, err = io.ReadFull(r, p[:])
80-
if err != nil {
81-
return fmt.Errorf("[Bookkeeper], deserializing Action failed: %s", err)
69+
self.Action = BookkeeperAction(action)
70+
cert, _, irregular, eof := source.NextVarBytes()
71+
if irregular {
72+
return common.ErrIrregularData
8273
}
83-
self.Action = BookkeeperAction(p[0])
84-
self.Cert, err = serialization.ReadVarBytes(r)
85-
if err != nil {
86-
return fmt.Errorf("[Bookkeeper], deserializing Cert failed: %s", err)
74+
if eof {
75+
return io.ErrUnexpectedEOF
8776
}
88-
89-
buf, err = serialization.ReadVarBytes(r)
90-
if err != nil {
91-
return fmt.Errorf("[Bookkeeper], deserializing Issuer failed: %s", err)
77+
self.Cert = cert
78+
issuer, _, irregular, eof := source.NextVarBytes()
79+
if irregular {
80+
return common.ErrIrregularData
9281
}
93-
self.Issuer, err = keypair.DeserializePublicKey(buf)
82+
if eof {
83+
return io.ErrUnexpectedEOF
84+
}
85+
self.Issuer, err = keypair.DeserializePublicKey(issuer)
9486
if err != nil {
9587
return fmt.Errorf("[Bookkeeper], deserializing Issuer failed: %s", err)
9688
}
97-
9889
return nil
9990
}
File renamed without changes.

0 commit comments

Comments
 (0)