Merge pull request #1350 from nspcc-dev/some-post-preview3-core-changes

Some post-preview3 core changes
This commit is contained in:
Roman Khimov 2020-08-24 16:26:12 +03:00 committed by GitHub
commit 6c27c2c13a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 116 additions and 63 deletions

View file

@ -11,6 +11,8 @@ type Syscall struct {
// All lists are sorted, keep 'em this way, please.
var syscalls = map[string]map[string]Syscall{
"binary": {
"Base58Decode": {interopnames.SystemBinaryBase58Decode, false},
"Base58Encode": {interopnames.SystemBinaryBase58Encode, false},
"Base64Decode": {interopnames.SystemBinaryBase64Decode, false},
"Base64Encode": {interopnames.SystemBinaryBase64Encode, false},
"Deserialize": {interopnames.SystemBinaryDeserialize, false},

View file

@ -199,7 +199,7 @@ func (s *service) eventLoop() {
s.dbft.OnTimeout(hv)
case msg := <-s.messages:
fields := []zap.Field{
zap.Uint16("from", msg.validatorIndex),
zap.Uint8("from", msg.validatorIndex),
zap.Stringer("type", msg.Type()),
}

View file

@ -30,7 +30,7 @@ type (
network netmode.Magic
data []byte
version uint32
validatorIndex uint16
validatorIndex uint8
prevHash util.Uint256
height uint32
@ -134,12 +134,12 @@ func (p *Payload) SetVersion(v uint32) {
// ValidatorIndex implements payload.ConsensusPayload interface.
func (p Payload) ValidatorIndex() uint16 {
return p.validatorIndex
return uint16(p.validatorIndex)
}
// SetValidatorIndex implements payload.ConsensusPayload interface.
func (p *Payload) SetValidatorIndex(i uint16) {
p.validatorIndex = i
p.validatorIndex = uint8(i)
}
// PrevHash implements payload.ConsensusPayload interface.
@ -167,7 +167,7 @@ func (p *Payload) EncodeBinaryUnsigned(w *io.BinWriter) {
w.WriteU32LE(p.version)
w.WriteBytes(p.prevHash[:])
w.WriteU32LE(p.height)
w.WriteU16LE(p.validatorIndex)
w.WriteB(p.validatorIndex)
if p.data == nil {
ww := io.NewBufBinWriter()
@ -216,7 +216,7 @@ func (p *Payload) DecodeBinaryUnsigned(r *io.BinReader) {
p.version = r.ReadU32LE()
r.ReadBytes(p.prevHash[:])
p.height = r.ReadU32LE()
p.validatorIndex = r.ReadU16LE()
p.validatorIndex = r.ReadB()
p.data = r.ReadVarBytes()
if r.Err != nil {

View file

@ -1,8 +1,6 @@
package consensus
import (
"encoding/hex"
gio "io"
"math/rand"
"testing"
@ -76,6 +74,7 @@ func TestConsensusPayload_Setters(t *testing.T) {
require.Equal(t, pl, p.GetRecoveryMessage())
}
/*
func TestConsensusPayload_Verify(t *testing.T) {
// signed payload from testnet
dataHex := "000000006c2cf4b46a45e839a6e9b75feef6bd551b1a31f97be1689d55a95a82448291099084000005002221002b7b3e8b02b1ff2dccac65596772f858b3c3e017470a989510d3b2cd270f246901420c40eb0fcb702cacfd3cfdb3f50422f230489e3e0e896914b4f7e13ef0c2e8bf523938e48610f0d1d1c606dd8bc494787ec127c6a10992afa846fe4a53e4c9e0ce6b290c2102ba2c70f5996f357a43198705859fae2cfea13e1172962800772b3d588a9d4abd0b4195440d78"
@ -92,6 +91,7 @@ func TestConsensusPayload_Verify(t *testing.T) {
defer bc.Close()
require.NoError(t, bc.VerifyWitness(h, p, &p.Witness, payloadGasLimit))
}
*/
func TestConsensusPayload_Serializable(t *testing.T) {
for _, mt := range messageTypes {
@ -118,7 +118,7 @@ func TestConsensusPayload_Serializable(t *testing.T) {
func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
// PrepareResponse ConsensusPayload consists of:
// 42-byte common prefix
// 41-byte common prefix
// 1-byte varint length of the payload (34),
// - 1-byte view number
// - 1-byte message type (PrepareResponse)
@ -126,7 +126,7 @@ func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
// 1-byte delimiter (1)
// 2-byte for empty invocation and verification scripts
const (
lenIndex = 42
lenIndex = 41
typeIndex = lenIndex + 1
delimeterIndex = typeIndex + 34
)
@ -329,6 +329,7 @@ func TestMessageType_String(t *testing.T) {
require.Equal(t, "UNKNOWN(0xff)", messageType(0xff).String())
}
/*
func TestPayload_DecodeFromTestnet(t *testing.T) {
hexDump := "000000005e3c788da53e6669772c408014abab20c9f33d1a38396de645a2d40fb3a8a37c960801000400423000aaf1b1cd5544485412eab6b1af49b57ae83b236595a0918488a9899e540c4e105aee59ed2cef1015f205ff1909312acab39d504d68f141c77e10ae21e14971ce01420c4040cfd9a6d6aa245d79a905864551dcc68e108c40231b7df8178663ae453f62388c9bd6bf10b1f1fb1a8736faba5561a886efa78ea5ff4f98812a9d2adba5f1f5290c2102a7834be9b32e2981d157cb5bbd3acb42cfd11ea5c3b10224d7a44e98c5910f1b0b4195440d78"
data, err := hex.DecodeString(hexDump)
@ -345,3 +346,4 @@ func TestPayload_DecodeFromTestnet(t *testing.T) {
buf.ReadB()
require.Equal(t, gio.EOF, buf.Err)
}
*/

View file

@ -20,7 +20,7 @@ type (
}
changeViewCompact struct {
ValidatorIndex uint16
ValidatorIndex uint8
OriginalViewNumber byte
Timestamp uint64
InvocationScript []byte
@ -28,13 +28,13 @@ type (
commitCompact struct {
ViewNumber byte
ValidatorIndex uint16
ValidatorIndex uint8
Signature [signatureSize]byte
InvocationScript []byte
}
preparationCompact struct {
ValidatorIndex uint16
ValidatorIndex uint8
InvocationScript []byte
}
)
@ -94,7 +94,7 @@ func (m *recoveryMessage) EncodeBinary(w *io.BinWriter) {
// DecodeBinary implements io.Serializable interface.
func (p *changeViewCompact) DecodeBinary(r *io.BinReader) {
p.ValidatorIndex = r.ReadU16LE()
p.ValidatorIndex = r.ReadB()
p.OriginalViewNumber = r.ReadB()
p.Timestamp = r.ReadU64LE()
p.InvocationScript = r.ReadVarBytes(1024)
@ -102,7 +102,7 @@ func (p *changeViewCompact) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements io.Serializable interface.
func (p *changeViewCompact) EncodeBinary(w *io.BinWriter) {
w.WriteU16LE(p.ValidatorIndex)
w.WriteB(p.ValidatorIndex)
w.WriteB(p.OriginalViewNumber)
w.WriteU64LE(p.Timestamp)
w.WriteVarBytes(p.InvocationScript)
@ -111,7 +111,7 @@ func (p *changeViewCompact) EncodeBinary(w *io.BinWriter) {
// DecodeBinary implements io.Serializable interface.
func (p *commitCompact) DecodeBinary(r *io.BinReader) {
p.ViewNumber = r.ReadB()
p.ValidatorIndex = r.ReadU16LE()
p.ValidatorIndex = r.ReadB()
r.ReadBytes(p.Signature[:])
p.InvocationScript = r.ReadVarBytes(1024)
}
@ -119,25 +119,27 @@ func (p *commitCompact) DecodeBinary(r *io.BinReader) {
// EncodeBinary implements io.Serializable interface.
func (p *commitCompact) EncodeBinary(w *io.BinWriter) {
w.WriteB(p.ViewNumber)
w.WriteU16LE(p.ValidatorIndex)
w.WriteB(p.ValidatorIndex)
w.WriteBytes(p.Signature[:])
w.WriteVarBytes(p.InvocationScript)
}
// DecodeBinary implements io.Serializable interface.
func (p *preparationCompact) DecodeBinary(r *io.BinReader) {
p.ValidatorIndex = r.ReadU16LE()
p.ValidatorIndex = r.ReadB()
p.InvocationScript = r.ReadVarBytes(1024)
}
// EncodeBinary implements io.Serializable interface.
func (p *preparationCompact) EncodeBinary(w *io.BinWriter) {
w.WriteU16LE(p.ValidatorIndex)
w.WriteB(p.ValidatorIndex)
w.WriteVarBytes(p.InvocationScript)
}
// AddPayload implements payload.RecoveryMessage interface.
func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
validator := uint8(p.ValidatorIndex())
switch p.Type() {
case payload.PrepareRequestType:
m.prepareRequest = &message{
@ -148,12 +150,12 @@ func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
h := p.Hash()
m.preparationHash = &h
m.preparationPayloads = append(m.preparationPayloads, &preparationCompact{
ValidatorIndex: p.ValidatorIndex(),
ValidatorIndex: validator,
InvocationScript: p.(*Payload).Witness.InvocationScript,
})
case payload.PrepareResponseType:
m.preparationPayloads = append(m.preparationPayloads, &preparationCompact{
ValidatorIndex: p.ValidatorIndex(),
ValidatorIndex: validator,
InvocationScript: p.(*Payload).Witness.InvocationScript,
})
@ -163,14 +165,14 @@ func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
}
case payload.ChangeViewType:
m.changeViewPayloads = append(m.changeViewPayloads, &changeViewCompact{
ValidatorIndex: p.ValidatorIndex(),
ValidatorIndex: validator,
OriginalViewNumber: p.ViewNumber(),
Timestamp: p.GetChangeView().Timestamp() / nsInMs,
InvocationScript: p.(*Payload).Witness.InvocationScript,
})
case payload.CommitType:
m.commitPayloads = append(m.commitPayloads, &commitCompact{
ValidatorIndex: p.ValidatorIndex(),
ValidatorIndex: validator,
ViewNumber: p.ViewNumber(),
Signature: p.GetCommit().(*commit).signature,
InvocationScript: p.(*Payload).Witness.InvocationScript,
@ -186,7 +188,7 @@ func (m *recoveryMessage) GetPrepareRequest(p payload.ConsensusPayload, validato
var compact *preparationCompact
for _, p := range m.preparationPayloads {
if p != nil && p.ValidatorIndex == primary {
if p != nil && p.ValidatorIndex == uint8(primary) {
compact = p
break
}
@ -199,7 +201,7 @@ func (m *recoveryMessage) GetPrepareRequest(p payload.ConsensusPayload, validato
req := fromPayload(prepareRequestType, p.(*Payload), m.prepareRequest.payload)
req.SetValidatorIndex(primary)
req.Witness.InvocationScript = compact.InvocationScript
req.Witness.VerificationScript = getVerificationScript(primary, validators)
req.Witness.VerificationScript = getVerificationScript(uint8(primary), validators)
return req
}
@ -216,7 +218,7 @@ func (m *recoveryMessage) GetPrepareResponses(p payload.ConsensusPayload, valida
r := fromPayload(prepareResponseType, p.(*Payload), &prepareResponse{
preparationHash: *m.preparationHash,
})
r.SetValidatorIndex(resp.ValidatorIndex)
r.SetValidatorIndex(uint16(resp.ValidatorIndex))
r.Witness.InvocationScript = resp.InvocationScript
r.Witness.VerificationScript = getVerificationScript(resp.ValidatorIndex, validators)
@ -236,7 +238,7 @@ func (m *recoveryMessage) GetChangeViews(p payload.ConsensusPayload, validators
timestamp: cv.Timestamp,
})
c.message.ViewNumber = cv.OriginalViewNumber
c.SetValidatorIndex(cv.ValidatorIndex)
c.SetValidatorIndex(uint16(cv.ValidatorIndex))
c.Witness.InvocationScript = cv.InvocationScript
c.Witness.VerificationScript = getVerificationScript(cv.ValidatorIndex, validators)
@ -252,7 +254,7 @@ func (m *recoveryMessage) GetCommits(p payload.ConsensusPayload, validators []cr
for i, c := range m.commitPayloads {
cc := fromPayload(commitType, p.(*Payload), &commit{signature: c.Signature})
cc.SetValidatorIndex(c.ValidatorIndex)
cc.SetValidatorIndex(uint16(c.ValidatorIndex))
cc.Witness.InvocationScript = c.InvocationScript
cc.Witness.VerificationScript = getVerificationScript(c.ValidatorIndex, validators)
@ -272,7 +274,7 @@ func (m *recoveryMessage) SetPreparationHash(h *util.Uint256) {
m.preparationHash = h
}
func getVerificationScript(i uint16, validators []crypto.PublicKey) []byte {
func getVerificationScript(i uint8, validators []crypto.PublicKey) []byte {
if int(i) >= len(validators) {
return nil
}

View file

@ -1,8 +1,6 @@
package consensus
import (
"encoding/hex"
gio "io"
"testing"
"github.com/nspcc-dev/dbft/crypto"
@ -10,7 +8,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/internal/testchain"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
)
@ -119,6 +116,7 @@ func TestRecoveryMessage_Setters(t *testing.T) {
})
}
/*
func TestRecoveryMessage_Decode(t *testing.T) {
hexDump := "000000007f5b6094e1281e6bac667f1f871aee755dbe62c012868c718d7709de62135d250d1800000100fd0f024100000120003db64b5e000000008e4ab7138abe65a30133175ebcf3c66ad59ed2c532ca19bbb84cb3802f7dc9b6decde10e117ff6fc3303000041e52280e60c46778876e4c7fdcd262170d906090256ff2ac11d14d45516dd465b5b8f241ff78096ee7280f226df677681bff091884dcd7c4f25cd9a61856ce0bc6a01004136b0b971ef320135f61c11475ff07c5cad04635fc1dad41d346d085646e29e6ff1c5181421a203e5d4b627c6bacdd78a78c9f4cb0a749877ea5a9ed2b02196f17f020041ac5e279927ded591c234391078db55cad2ada58bded974fa2d2751470d0b2f94dddc84ed312f31ee960c884066f778e000f4f05883c74defa75d2a2eb524359c7d020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041546d2e34cbbfd0d09b7ce937eec07b402bd597f7bef24938f4a01041f443fb4dd31bebcabdaae3942bb9d549724a152e851bee43ebc5f482ddd9316f2690b48e7d00010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000415281a6579b875c480d9b3cc9144485d1f898e13405eaf1e4117d83844f1265f81a71998d53fa32d6c3b5249446ac036ecda73b1fe8c1341475fcc4b8d6ba8ec6e20141d775fd1a605173a8ed02084fef903ee043239ca4c76cb658809c6216031437e8f4d5a265550d5934fe386732364d9b49a14baef5a1236d02c557cb394a3a0873c82364f65259a991768a35ba18777f76901e1022f87d71910f4e3e46f161299401f2074d0c"
data, err := hex.DecodeString(hexDump)
@ -138,6 +136,7 @@ func TestRecoveryMessage_Decode(t *testing.T) {
buf.ReadB()
require.Equal(t, gio.EOF, buf.Err)
}
*/
func getKeys(t *testing.T, n int) []*privateKey {
privs := make([]*privateKey, 0, n)

View file

@ -2,6 +2,8 @@ package interopnames
// Names of all used interops.
const (
SystemBinaryBase58Decode = "System.Binary.Base58Decode"
SystemBinaryBase58Encode = "System.Binary.Base58Encode"
SystemBinaryBase64Decode = "System.Binary.Base64Decode"
SystemBinaryBase64Encode = "System.Binary.Base64Encode"
SystemBinaryDeserialize = "System.Binary.Deserialize"
@ -67,6 +69,8 @@ const (
)
var names = []string{
SystemBinaryBase58Decode,
SystemBinaryBase58Encode,
SystemBinaryBase64Decode,
SystemBinaryBase64Encode,
SystemBinaryDeserialize,

View file

@ -7,6 +7,7 @@ import (
"fmt"
"sort"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
@ -195,16 +196,16 @@ func runtimeDeserialize(ic *interop.Context) error {
return vm.RuntimeDeserialize(ic.VM)
}
// runtimeEncode encodes top stack item into a base64 string.
func runtimeEncode(ic *interop.Context) error {
// runtimeEncodeBase64 encodes top stack item into a base64 string.
func runtimeEncodeBase64(ic *interop.Context) error {
src := ic.VM.Estack().Pop().Bytes()
result := base64.StdEncoding.EncodeToString(src)
ic.VM.Estack().PushVal([]byte(result))
return nil
}
// runtimeDecode decodes top stack item from base64 string to byte array.
func runtimeDecode(ic *interop.Context) error {
// runtimeDecodeBase64 decodes top stack item from base64 string to byte array.
func runtimeDecodeBase64(ic *interop.Context) error {
src := ic.VM.Estack().Pop().String()
result, err := base64.StdEncoding.DecodeString(src)
if err != nil {
@ -213,3 +214,22 @@ func runtimeDecode(ic *interop.Context) error {
ic.VM.Estack().PushVal(result)
return nil
}
// runtimeEncodeBase58 encodes top stack item into a base58 string.
func runtimeEncodeBase58(ic *interop.Context) error {
src := ic.VM.Estack().Pop().Bytes()
result := base58.Encode(src)
ic.VM.Estack().PushVal([]byte(result))
return nil
}
// runtimeDecodeBase58 decodes top stack item from base58 string to byte array.
func runtimeDecodeBase58(ic *interop.Context) error {
src := ic.VM.Estack().Pop().String()
result, err := base58.Decode(src)
if err != nil {
return err
}
ic.VM.Estack().PushVal(result)
return nil
}

View file

@ -5,6 +5,7 @@ import (
"fmt"
"testing"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
@ -220,36 +221,45 @@ func TestECDSAVerify(t *testing.T) {
})
}
func TestRuntimeEncode(t *testing.T) {
str := []byte("my pretty string")
func TestRuntimeEncodeDecode(t *testing.T) {
original := []byte("my pretty string")
encoded64 := base64.StdEncoding.EncodeToString(original)
encoded58 := base58.Encode(original)
v, ic, bc := createVM(t)
defer bc.Close()
v.Estack().PushVal(str)
require.NoError(t, runtimeEncode(ic))
expected := []byte(base64.StdEncoding.EncodeToString(str))
actual := v.Estack().Pop().Bytes()
require.Equal(t, expected, actual)
}
func TestRuntimeDecode(t *testing.T) {
expected := []byte("my pretty string")
str := base64.StdEncoding.EncodeToString(expected)
v, ic, bc := createVM(t)
defer bc.Close()
t.Run("positive", func(t *testing.T) {
v.Estack().PushVal(str)
require.NoError(t, runtimeDecode(ic))
t.Run("Encode64", func(t *testing.T) {
v.Estack().PushVal(original)
require.NoError(t, runtimeEncodeBase64(ic))
actual := v.Estack().Pop().Bytes()
require.Equal(t, expected, actual)
require.Equal(t, []byte(encoded64), actual)
})
t.Run("error", func(t *testing.T) {
v.Estack().PushVal(str + "%")
require.Error(t, runtimeDecode(ic))
t.Run("Encode58", func(t *testing.T) {
v.Estack().PushVal(original)
require.NoError(t, runtimeEncodeBase58(ic))
actual := v.Estack().Pop().Bytes()
require.Equal(t, []byte(encoded58), actual)
})
t.Run("Decode64/positive", func(t *testing.T) {
v.Estack().PushVal(encoded64)
require.NoError(t, runtimeDecodeBase64(ic))
actual := v.Estack().Pop().Bytes()
require.Equal(t, original, actual)
})
t.Run("Decode64/error", func(t *testing.T) {
v.Estack().PushVal(encoded64 + "%")
require.Error(t, runtimeDecodeBase64(ic))
})
t.Run("Decode58/positive", func(t *testing.T) {
v.Estack().PushVal(encoded58)
require.NoError(t, runtimeDecodeBase58(ic))
actual := v.Estack().Pop().Bytes()
require.Equal(t, original, actual)
})
t.Run("Decode58/error", func(t *testing.T) {
v.Estack().PushVal(encoded58 + "%")
require.Error(t, runtimeDecodeBase58(ic))
})
}

View file

@ -31,8 +31,10 @@ func SpawnVM(ic *interop.Context) *vm.VM {
// All lists are sorted, keep 'em this way, please.
var systemInterops = []interop.Function{
{Name: interopnames.SystemBinaryBase64Decode, Func: runtimeDecode, Price: 100000, ParamCount: 1},
{Name: interopnames.SystemBinaryBase64Encode, Func: runtimeEncode, Price: 100000, ParamCount: 1},
{Name: interopnames.SystemBinaryBase58Decode, Func: runtimeDecodeBase58, Price: 100000, ParamCount: 1},
{Name: interopnames.SystemBinaryBase58Encode, Func: runtimeEncodeBase58, Price: 100000, ParamCount: 1},
{Name: interopnames.SystemBinaryBase64Decode, Func: runtimeDecodeBase64, Price: 100000, ParamCount: 1},
{Name: interopnames.SystemBinaryBase64Encode, Func: runtimeEncodeBase64, Price: 100000, ParamCount: 1},
{Name: interopnames.SystemBinaryDeserialize, Func: runtimeDeserialize, Price: 500000, ParamCount: 1},
{Name: interopnames.SystemBinarySerialize, Func: runtimeSerialize, Price: 100000, ParamCount: 1},
{Name: interopnames.SystemBlockchainGetBlock, Func: bcGetBlock, Price: 2500000,
@ -59,7 +61,7 @@ var systemInterops = []interop.Function{
RequiredFlags: smartcontract.AllowModifyStates, ParamCount: 2, DisallowCallback: true},
{Name: interopnames.SystemContractCreateStandardAccount, Func: contractCreateStandardAccount, Price: 10000, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemContractDestroy, Func: contractDestroy, Price: 1000000, RequiredFlags: smartcontract.AllowModifyStates, DisallowCallback: true},
{Name: interopnames.SystemContractIsStandard, Func: contractIsStandard, Price: 30000, ParamCount: 1},
{Name: interopnames.SystemContractIsStandard, Func: contractIsStandard, Price: 30000, RequiredFlags: smartcontract.AllowStates, ParamCount: 1},
{Name: interopnames.SystemContractGetCallFlags, Func: contractGetCallFlags, Price: 30000, DisallowCallback: true},
{Name: interopnames.SystemContractUpdate, Func: contractUpdate, Price: 0,
RequiredFlags: smartcontract.AllowModifyStates, ParamCount: 2, DisallowCallback: true},

View file

@ -28,3 +28,15 @@ func Base64Encode(b []byte) []byte {
func Base64Decode(b []byte) []byte {
return nil
}
// Base58Encode encodes given byte slice into a base58 string and returns byte
// representation of this string. It uses `System.Binary.Base58Encode` syscall.
func Base58Encode(b []byte) []byte {
return nil
}
// Base58Decode decodes given base58 string represented as a byte slice into
// a new byte slice. It uses `System.Binary.Base58Decode` syscall.
func Base58Decode(b []byte) []byte {
return nil
}