transaction: add version check for publish

-Version check done before reading NeedStorage flag
-Add testify according to review
This commit is contained in:
Vsevolod Brekelov 2019-08-27 19:58:50 +03:00
parent e93a2192b1
commit abc5833853
4 changed files with 78 additions and 19 deletions

View file

@ -1,8 +1,27 @@
package transaction
import (
"bytes"
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
)
var (
// https://neotracker.io/tx/2c6a45547b3898318e400e541628990a07acb00f3b9a15a8e966ae49525304da
rawClaimTX = "020004bc67ba325d6412ff4c55b10f7e9afb54bbb2228d201b37363c3d697ac7c198f70300591cd454d7318d2087c0196abfbbd1573230380672f0f0cd004dcb4857e58cbd010031bcfbed573f5318437e95edd603922a4455ff3326a979fdd1c149a84c4cb0290000b51eb6159c58cac4fe23d90e292ad2bcb7002b0da2c474e81e1889c0649d2c490000000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c603b555f00000000005d9de59d99c0d1f6ed1496444473f4a0b538302f014140456349cec43053009accdb7781b0799c6b591c812768804ab0a0b56b5eae7a97694227fcd33e70899c075848b2cee8fae733faac6865b484d3f7df8949e2aadb232103945fae1ed3c31d778f149192b76734fcc951b400ba3598faa81ff92ebe477eacac"
// https://neotracker.io/tx/fe4b3af60677204c57e573a57bdc97bc5059b05ad85b1474f84431f88d910f64
rawInvocationTX = "d101590400b33f7114839c33710da24cf8e7d536b8d244f3991cf565c8146063795d3b9b3cd55aef026eae992b91063db0db53c1087472616e7366657267c5cc1cb5392019e2cc4e6d6b5ea54c8d4b6d11acf166cb072961424c54f6000000000000000001206063795d3b9b3cd55aef026eae992b91063db0db0000014140c6a131c55ca38995402dff8e92ac55d89cbed4b98dfebbcb01acbc01bd78fa2ce2061be921b8999a9ab79c2958875bccfafe7ce1bbbaf1f56580815ea3a4feed232102d41ddce2c97be4c9aa571b8a32cbc305aa29afffbcae71b0ef568db0e93929aaac"
// https://neoscan-testnet.io/transaction/5467A1FC8723CEFFA8E5EE59399B02EEA1DF6FBAA53768C6704B90B960D223FA
rawPublishTX = "d000fd3f01746b4c04000000004c04000000004c040000000061681e416e745368617265732e426c6f636b636861696e2e476574486569676874681d416e745368617265732e426c6f636b636861696e2e476574426c6f636b744c0400000000948c6c766b947275744c0402000000936c766b9479744c0400000000948c6c766b9479681d416e745368617265732e4865616465722e47657454696d657374616d70a0744c0401000000948c6c766b947275744c0401000000948c6c766b9479641b004c0400000000744c0402000000948c6c766b947275623000744c0401000000936c766b9479744c0400000000936c766b9479ac744c0402000000948c6c766b947275620300744c0402000000948c6c766b947961748c6c766b946d748c6c766b946d748c6c766b946d746c768c6b946d746c768c6b946d746c768c6b946d6c75660302050001044c6f636b0c312e302d70726576696577310a4572696b205a68616e67126572696b40616e747368617265732e6f7267234c6f636b20796f75722061737365747320756e74696c20612074696d657374616d702e00014e23ac4c4851f93407d4c59e1673171f39859db9e7cac72540cd3cc1ae0cca87000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6000ebcaaa0d00000067f97110a66136d38badc7b9f88eab013027ce49014140c298da9f06d5687a0bb87ea3bba188b7dcc91b9667ea5cb71f6fdefe388f42611df29be9b2d6288655b9f2188f46796886afc3b37d8b817599365d9e161ecfb62321034b44ed9c8a88fb2497b6b57206cc08edd42c5614bd1fee790e5b795dee0f4e11ac"
)
func decodeTransaction(rawTX string, t *testing.T) *Transaction {
b, err1 := hex.DecodeString(rawTX)
assert.Nil(t, err1)
tx := &Transaction{}
err2 := tx.DecodeBinary(bytes.NewReader(b))
assert.Nil(t, err2)
return tx
}

View file

@ -19,6 +19,7 @@ type PublishTX struct {
Author string
Email string
Description string
Version uint8 // Version of the parent struct Transaction. Used in reading NeedStorage flag.
}
// DecodeBinary implements the Payload interface.
@ -38,7 +39,11 @@ func (tx *PublishTX) DecodeBinary(r io.Reader) error {
br.ReadLE(&rtype)
tx.ReturnType = smartcontract.ParamType(rtype)
br.ReadLE(&tx.NeedStorage)
if tx.Version >= 1 {
br.ReadLE(&tx.NeedStorage)
} else {
tx.NeedStorage = false
}
tx.Name = br.ReadString()
tx.CodeVersion = br.ReadString()

View file

@ -93,7 +93,6 @@ func (t *Transaction) DecodeBinary(r io.Reader) error {
for i := 0; i < int(lenAttrs); i++ {
t.Attributes[i] = &Attribute{}
if err := t.Attributes[i].DecodeBinary(r); err != nil {
// @TODO: remove this when TX attribute decode bug is solved.
log.Warnf("failed to decode TX %s", t.hash)
return err
}
@ -158,7 +157,7 @@ func (t *Transaction) decodeData(r io.Reader) error {
t.Data = &EnrollmentTX{}
return t.Data.(*EnrollmentTX).DecodeBinary(r)
case PublishType:
t.Data = &PublishTX{}
t.Data = &PublishTX{Version: t.Version}
return t.Data.(*PublishTX).DecodeBinary(r)
case StateType:
t.Data = &StateTX{}

View file

@ -6,6 +6,7 @@ import (
"testing"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/smartcontract"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
@ -42,14 +43,7 @@ func TestWitnessEncodeDecode(t *testing.T) {
}
func TestDecodeEncodeClaimTX(t *testing.T) {
b, err := hex.DecodeString(rawClaimTX)
if err != nil {
t.Fatal(err)
}
tx := &Transaction{}
if err := tx.DecodeBinary(bytes.NewReader(b)); err != nil {
t.Fatal(err)
}
tx := decodeTransaction(rawClaimTX, t)
assert.Equal(t, tx.Type, ClaimType)
assert.IsType(t, tx.Data, &ClaimTX{})
claimTX := tx.Data.(*ClaimTX)
@ -78,14 +72,7 @@ func TestDecodeEncodeClaimTX(t *testing.T) {
}
func TestDecodeEncodeInvocationTX(t *testing.T) {
b, err := hex.DecodeString(rawInvocationTX)
if err != nil {
t.Fatal(err)
}
tx := &Transaction{}
if err := tx.DecodeBinary(bytes.NewReader(b)); err != nil {
t.Fatal(err)
}
tx := decodeTransaction(rawInvocationTX, t)
assert.Equal(t, tx.Type, InvocationType)
assert.IsType(t, tx.Data, &InvocationTX{})
@ -109,3 +96,52 @@ func TestDecodeEncodeInvocationTX(t *testing.T) {
}
assert.Equal(t, rawInvocationTX, hex.EncodeToString(buf.Bytes()))
}
func TestDecodePublishTX(t *testing.T) {
expectedTXData := &PublishTX{}
expectedTXData.Name = "Lock"
expectedTXData.Author = "Erik Zhang"
expectedTXData.Email = "erik@antshares.org"
expectedTXData.Description = "Lock your assets until a timestamp."
expectedTXData.CodeVersion = "1.0-preview1"
expectedTXData.NeedStorage = false
expectedTXData.ReturnType = 1
paramList := []smartcontract.ParamType{
smartcontract.IntegerType,
smartcontract.ByteArrayType,
smartcontract.SignatureType,
}
expectedTXData.ParamList = paramList
expectedTXData.Script = []uint8{0x74, 0x6b, 0x4c, 0x4, 0x0, 0x0, 0x0, 0x0, 0x4c, 0x4, 0x0, 0x0, 0x0, 0x0, 0x4c, 0x4,
0x0, 0x0, 0x0, 0x0, 0x61, 0x68, 0x1e, 0x41, 0x6e, 0x74, 0x53, 0x68, 0x61, 0x72, 0x65, 0x73, 0x2e, 0x42, 0x6c,
0x6f, 0x63, 0x6b, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x74, 0x48, 0x65, 0x69, 0x67, 0x68, 0x74,
0x68, 0x1d, 0x41, 0x6e, 0x74, 0x53, 0x68, 0x61, 0x72, 0x65, 0x73, 0x2e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x63,
0x68, 0x61, 0x69, 0x6e, 0x2e, 0x47, 0x65, 0x74, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x74, 0x4c, 0x4, 0x0, 0x0, 0x0,
0x0, 0x94, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x72, 0x75, 0x74, 0x4c, 0x4, 0x2, 0x0, 0x0, 0x0, 0x93, 0x6c, 0x76,
0x6b, 0x94, 0x79, 0x74, 0x4c, 0x4, 0x0, 0x0, 0x0, 0x0, 0x94, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x79, 0x68, 0x1d,
0x41, 0x6e, 0x74, 0x53, 0x68, 0x61, 0x72, 0x65, 0x73, 0x2e, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x47,
0x65, 0x74, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0xa0, 0x74, 0x4c, 0x4, 0x1, 0x0, 0x0, 0x0,
0x94, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x72, 0x75, 0x74, 0x4c, 0x4, 0x1, 0x0, 0x0, 0x0, 0x94, 0x8c, 0x6c, 0x76,
0x6b, 0x94, 0x79, 0x64, 0x1b, 0x0, 0x4c, 0x4, 0x0, 0x0, 0x0, 0x0, 0x74, 0x4c, 0x4, 0x2, 0x0, 0x0, 0x0, 0x94,
0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x72, 0x75, 0x62, 0x30, 0x0, 0x74, 0x4c, 0x4, 0x1, 0x0, 0x0, 0x0, 0x93, 0x6c,
0x76, 0x6b, 0x94, 0x79, 0x74, 0x4c, 0x4, 0x0, 0x0, 0x0, 0x0, 0x93, 0x6c, 0x76, 0x6b, 0x94, 0x79, 0xac, 0x74,
0x4c, 0x4, 0x2, 0x0, 0x0, 0x0, 0x94, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x72, 0x75, 0x62, 0x3, 0x0, 0x74, 0x4c, 0x4,
0x2, 0x0, 0x0, 0x0, 0x94, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x79, 0x61, 0x74, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x6d,
0x74, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x6d, 0x74, 0x8c, 0x6c, 0x76, 0x6b, 0x94, 0x6d, 0x74, 0x6c, 0x76, 0x8c,
0x6b, 0x94, 0x6d, 0x74, 0x6c, 0x76, 0x8c, 0x6b, 0x94, 0x6d, 0x74, 0x6c, 0x76, 0x8c, 0x6b, 0x94, 0x6d, 0x6c,
0x75, 0x66}
expectedTX := &Transaction{}
expectedTX.Type = PublishType
expectedTX.Version = 0
expectedTX.Data = expectedTXData
actualTX := decodeTransaction(rawPublishTX, t)
assert.Equal(t, expectedTX.Data, actualTX.Data)
assert.Equal(t, expectedTX.Type, actualTX.Type)
assert.Equal(t, expectedTX.Version, actualTX.Version)
}