2018-03-09 15:55:25 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2018-03-17 11:53:21 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-02-17 12:04:25 +00:00
|
|
|
"os"
|
2018-03-17 11:53:21 +00:00
|
|
|
"testing"
|
2018-03-09 15:55:25 +00:00
|
|
|
"time"
|
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/config"
|
2020-01-14 12:32:07 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/block"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2020-01-14 12:32:07 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/smartcontract"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2020-02-17 12:04:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/emit"
|
2019-12-03 14:54:47 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
2020-02-28 16:01:07 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/wallet"
|
2019-10-15 09:52:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
var privNetKeys = []string{
|
|
|
|
"KxyjQ8eUa4FHt3Gvioyt1Wz29cTUrE4eTqX3yFSk1YFCsPL8uNsY",
|
|
|
|
"KzfPUYDC9n2yf4fK5ro4C8KMcdeXtFuEnStycbZgX3GomiUsvX6W",
|
|
|
|
"KzgWE3u3EDp13XPXXuTKZxeJ3Gi8Bsm8f9ijY3ZsCKKRvZUo1Cdn",
|
|
|
|
"L2oEXKRAAMiPEZukwR5ho2S6SMeQLhcK9mF71ZnF7GvT8dU4Kkgz",
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestChain should be called before newBlock invocation to properly setup
|
|
|
|
// global state.
|
|
|
|
func newTestChain(t *testing.T) *Blockchain {
|
2020-02-29 14:24:37 +00:00
|
|
|
unitTestNetCfg, err := config.Load("../../config", config.ModeUnitTestNet)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-30 07:43:05 +00:00
|
|
|
chain, err := NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-07 17:47:48 +00:00
|
|
|
go chain.Run()
|
2019-10-15 09:52:10 +00:00
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2020-02-29 14:24:37 +00:00
|
|
|
func (bc *Blockchain) newBlock(txs ...*transaction.Transaction) *block.Block {
|
|
|
|
lastBlock := bc.topBlock.Load().(*block.Block)
|
|
|
|
return newBlock(bc.config, lastBlock.Index+1, lastBlock.Hash(), txs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBlock(cfg config.ProtocolConfiguration, index uint32, prev util.Uint256, txs ...*transaction.Transaction) *block.Block {
|
|
|
|
validators, _ := getValidators(cfg)
|
2019-10-15 09:52:10 +00:00
|
|
|
vlen := len(validators)
|
|
|
|
valScript, _ := smartcontract.CreateMultiSigRedeemScript(
|
|
|
|
vlen-(vlen-1)/3,
|
|
|
|
validators,
|
|
|
|
)
|
2019-12-09 14:14:10 +00:00
|
|
|
witness := transaction.Witness{
|
2019-10-15 09:52:10 +00:00
|
|
|
VerificationScript: valScript,
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
b := &block.Block{
|
2020-01-15 08:29:50 +00:00
|
|
|
Base: block.Base{
|
2018-03-09 15:55:25 +00:00
|
|
|
Version: 0,
|
2020-02-29 14:24:37 +00:00
|
|
|
PrevHash: prev,
|
2019-10-15 09:52:10 +00:00
|
|
|
Timestamp: uint32(time.Now().UTC().Unix()) + index,
|
2018-03-09 15:55:25 +00:00
|
|
|
Index: index,
|
|
|
|
ConsensusData: 1111,
|
2019-10-15 09:52:10 +00:00
|
|
|
NextConsensus: witness.ScriptHash(),
|
|
|
|
Script: witness,
|
2018-03-09 15:55:25 +00:00
|
|
|
},
|
|
|
|
Transactions: txs,
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
_ = b.RebuildMerkleRoot()
|
2018-03-25 10:45:54 +00:00
|
|
|
|
2019-10-15 09:52:10 +00:00
|
|
|
invScript := make([]byte, 0)
|
|
|
|
for _, wif := range privNetKeys {
|
|
|
|
pKey, err := keys.NewPrivateKeyFromWIF(wif)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-15 10:32:40 +00:00
|
|
|
b := b.GetHashableData()
|
2020-01-16 15:30:16 +00:00
|
|
|
sig := pKey.Sign(b)
|
|
|
|
if len(sig) != 64 {
|
|
|
|
panic("wrong signature length")
|
2019-10-15 09:52:10 +00:00
|
|
|
}
|
2019-12-03 14:54:47 +00:00
|
|
|
invScript = append(invScript, byte(opcode.PUSHBYTES64))
|
2019-10-15 09:52:10 +00:00
|
|
|
invScript = append(invScript, sig...)
|
|
|
|
}
|
|
|
|
b.Script.InvocationScript = invScript
|
2018-03-09 15:55:25 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-02-29 14:16:13 +00:00
|
|
|
func (bc *Blockchain) genBlocks(n int) ([]*block.Block, error) {
|
2020-01-14 12:32:07 +00:00
|
|
|
blocks := make([]*block.Block, n)
|
2020-02-29 14:24:37 +00:00
|
|
|
lastHash := bc.topBlock.Load().(*block.Block).Hash()
|
2018-03-17 11:53:21 +00:00
|
|
|
for i := 0; i < n; i++ {
|
2020-02-29 14:24:37 +00:00
|
|
|
blocks[i] = newBlock(bc.config, uint32(i)+1, lastHash, newMinerTX())
|
2020-02-29 14:16:13 +00:00
|
|
|
if err := bc.AddBlock(blocks[i]); err != nil {
|
|
|
|
return blocks, err
|
|
|
|
}
|
2020-02-29 14:24:37 +00:00
|
|
|
lastHash = blocks[i].Hash()
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
2020-02-29 14:16:13 +00:00
|
|
|
return blocks, nil
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 17:02:09 +00:00
|
|
|
func newMinerTX() *transaction.Transaction {
|
2018-03-09 15:55:25 +00:00
|
|
|
return &transaction.Transaction{
|
2019-10-10 17:02:09 +00:00
|
|
|
Type: transaction.MinerType,
|
|
|
|
Data: &transaction.MinerTX{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
func getDecodedBlock(t *testing.T, i int) *block.Block {
|
2018-03-17 11:53:21 +00:00
|
|
|
data, err := getBlockData(i)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
b, err := hex.DecodeString(data["raw"].(string))
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
block := &block.Block{}
|
2019-09-16 16:31:49 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
block.DecodeBinary(r)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, r.Err)
|
2018-03-17 11:53:21 +00:00
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlockData(i int) (map[string]interface{}, error) {
|
|
|
|
b, err := ioutil.ReadFile(fmt.Sprintf("test_data/block_%d.json", i))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var data map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
|
|
|
|
func newDumbBlock() *block.Block {
|
|
|
|
return &block.Block{
|
2020-01-15 08:29:50 +00:00
|
|
|
Base: block.Base{
|
2020-01-14 12:32:07 +00:00
|
|
|
Version: 0,
|
|
|
|
PrevHash: hash.Sha256([]byte("a")),
|
|
|
|
MerkleRoot: hash.Sha256([]byte("b")),
|
|
|
|
Timestamp: uint32(100500),
|
|
|
|
Index: 1,
|
|
|
|
ConsensusData: 1111,
|
|
|
|
NextConsensus: hash.Hash160([]byte("a")),
|
|
|
|
Script: transaction.Witness{
|
|
|
|
VerificationScript: []byte{0x51}, // PUSH1
|
|
|
|
InvocationScript: []byte{0x61}, // NOP
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Transactions: []*transaction.Transaction{
|
|
|
|
{Type: transaction.MinerType},
|
|
|
|
{Type: transaction.IssueType},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 12:04:25 +00:00
|
|
|
|
2020-02-27 12:59:17 +00:00
|
|
|
func getInvocationScript(data []byte, priv *keys.PrivateKey) []byte {
|
|
|
|
signature := priv.Sign(data)
|
|
|
|
return append([]byte{byte(opcode.PUSHBYTES64)}, signature...)
|
|
|
|
}
|
|
|
|
|
2020-02-17 12:04:25 +00:00
|
|
|
// This function generates "../rpc/testdata/testblocks.acc" file which contains data
|
|
|
|
// for RPC unit tests.
|
|
|
|
// To generate new "../rpc/testdata/testblocks.acc", follow the steps:
|
|
|
|
// 1. Rename the function
|
|
|
|
// 2. Add specific test-case into "neo-go/pkg/core/blockchain_test.go"
|
|
|
|
// 3. Run tests with `$ make test`
|
|
|
|
func _(t *testing.T) {
|
2020-02-26 12:37:53 +00:00
|
|
|
const prefix = "../rpc/server/testdata/"
|
|
|
|
|
2020-02-17 12:04:25 +00:00
|
|
|
bc := newTestChain(t)
|
|
|
|
n := 50
|
2020-02-29 14:16:13 +00:00
|
|
|
_, err := bc.genBlocks(n)
|
|
|
|
require.NoError(t, err)
|
2020-02-17 12:04:25 +00:00
|
|
|
|
|
|
|
tx1 := newMinerTX()
|
|
|
|
|
2020-02-26 12:37:53 +00:00
|
|
|
avm, err := ioutil.ReadFile(prefix + "test_contract.avm")
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-17 12:04:25 +00:00
|
|
|
|
|
|
|
var props smartcontract.PropertyState
|
|
|
|
script := io.NewBufBinWriter()
|
|
|
|
emit.Bytes(script.BinWriter, []byte("Da contract dat hallos u"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("joe@example.com"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("Random Guy"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("0.99"))
|
|
|
|
emit.Bytes(script.BinWriter, []byte("Helloer"))
|
|
|
|
props |= smartcontract.HasStorage
|
|
|
|
emit.Int(script.BinWriter, int64(props))
|
|
|
|
emit.Int(script.BinWriter, int64(5))
|
|
|
|
params := make([]byte, 1)
|
|
|
|
params[0] = byte(7)
|
|
|
|
emit.Bytes(script.BinWriter, params)
|
|
|
|
emit.Bytes(script.BinWriter, avm)
|
|
|
|
emit.Syscall(script.BinWriter, "Neo.Contract.Create")
|
|
|
|
txScript := script.Bytes()
|
|
|
|
|
|
|
|
tx2 := transaction.NewInvocationTX(txScript, util.Fixed8FromFloat(100))
|
|
|
|
|
2020-02-29 14:24:37 +00:00
|
|
|
block := bc.newBlock(tx1, tx2)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, bc.AddBlock(block))
|
2020-02-17 12:04:25 +00:00
|
|
|
|
2020-02-18 08:09:07 +00:00
|
|
|
script = io.NewBufBinWriter()
|
|
|
|
emit.String(script.BinWriter, "testvalue")
|
|
|
|
emit.String(script.BinWriter, "testkey")
|
|
|
|
emit.Int(script.BinWriter, 2)
|
|
|
|
emit.Opcode(script.BinWriter, opcode.PACK)
|
|
|
|
emit.String(script.BinWriter, "Put")
|
|
|
|
emit.AppCall(script.BinWriter, hash.Hash160(avm), false)
|
|
|
|
|
|
|
|
tx3 := transaction.NewInvocationTX(script.Bytes(), util.Fixed8FromFloat(100))
|
2020-02-26 12:37:53 +00:00
|
|
|
|
|
|
|
tx4 := transaction.NewContractTX()
|
|
|
|
h, err := util.Uint256DecodeStringBE("6da730b566db183bfceb863b780cd92dee2b497e5a023c322c1eaca81cf9ad7a")
|
|
|
|
require.NoError(t, err)
|
|
|
|
tx4.AddInput(&transaction.Input{
|
|
|
|
PrevHash: h,
|
|
|
|
PrevIndex: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
// multisig address which possess all NEO
|
|
|
|
scriptHash, err := util.Uint160DecodeStringBE("be48d3a3f5d10013ab9ffee489706078714f1ea2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
priv, err := keys.NewPrivateKeyFromWIF(privNetKeys[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
tx4.AddOutput(&transaction.Output{
|
|
|
|
AssetID: GoverningTokenID(),
|
|
|
|
Amount: util.Fixed8FromInt64(1000),
|
|
|
|
ScriptHash: priv.GetScriptHash(),
|
|
|
|
Position: 0,
|
|
|
|
})
|
|
|
|
tx4.AddOutput(&transaction.Output{
|
|
|
|
AssetID: GoverningTokenID(),
|
|
|
|
Amount: util.Fixed8FromInt64(99999000),
|
|
|
|
ScriptHash: scriptHash,
|
|
|
|
Position: 1,
|
|
|
|
})
|
|
|
|
tx4.Data = new(transaction.ContractTX)
|
|
|
|
|
|
|
|
validators, err := getValidators(bc.config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
rawScript, err := smartcontract.CreateMultiSigRedeemScript(len(bc.config.StandbyValidators)/2+1, validators)
|
|
|
|
require.NoError(t, err)
|
|
|
|
data := tx4.GetSignedPart()
|
|
|
|
|
|
|
|
var invoc []byte
|
|
|
|
for i := range privNetKeys {
|
|
|
|
priv, err := keys.NewPrivateKeyFromWIF(privNetKeys[i])
|
|
|
|
require.NoError(t, err)
|
2020-02-27 12:59:17 +00:00
|
|
|
invoc = append(invoc, getInvocationScript(data, priv)...)
|
2020-02-26 12:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tx4.Scripts = []transaction.Witness{{
|
|
|
|
InvocationScript: invoc,
|
|
|
|
VerificationScript: rawScript,
|
|
|
|
}}
|
|
|
|
|
|
|
|
b := bc.newBlock(newMinerTX(), tx3, tx4)
|
2020-02-18 08:09:07 +00:00
|
|
|
require.NoError(t, bc.AddBlock(b))
|
|
|
|
|
2020-02-27 12:59:17 +00:00
|
|
|
priv1, err := keys.NewPrivateKeyFromWIF(privNetKeys[1])
|
|
|
|
require.NoError(t, err)
|
|
|
|
tx5 := transaction.NewContractTX()
|
|
|
|
tx5.Data = new(transaction.ContractTX)
|
|
|
|
tx5.AddInput(&transaction.Input{
|
|
|
|
PrevHash: tx4.Hash(),
|
|
|
|
PrevIndex: 0,
|
|
|
|
})
|
|
|
|
tx5.AddOutput(&transaction.Output{
|
|
|
|
AssetID: GoverningTokenID(),
|
|
|
|
Amount: util.Fixed8FromInt64(1000),
|
|
|
|
ScriptHash: priv1.GetScriptHash(),
|
|
|
|
})
|
|
|
|
|
2020-02-28 16:01:07 +00:00
|
|
|
acc, err := wallet.NewAccountFromWIF(priv.WIF())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, acc.SignTx(tx5))
|
2020-02-27 12:59:17 +00:00
|
|
|
b = bc.newBlock(newMinerTX(), tx5)
|
|
|
|
require.NoError(t, bc.AddBlock(b))
|
|
|
|
|
2020-02-26 12:37:53 +00:00
|
|
|
outStream, err := os.Create(prefix + "testblocks.acc")
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-17 12:04:25 +00:00
|
|
|
defer outStream.Close()
|
|
|
|
|
|
|
|
writer := io.NewBinWriterFromIO(outStream)
|
|
|
|
|
|
|
|
count := bc.BlockHeight() + 1
|
|
|
|
writer.WriteU32LE(count - 1)
|
|
|
|
|
|
|
|
for i := 1; i < int(count); i++ {
|
|
|
|
bh := bc.GetHeaderHash(i)
|
|
|
|
b, err := bc.GetBlock(bh)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-17 12:04:25 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
b.EncodeBinary(buf.BinWriter)
|
|
|
|
bytes := buf.Bytes()
|
2020-02-26 14:51:30 +00:00
|
|
|
writer.WriteU32LE(uint32(len(bytes)))
|
2020-02-17 12:04:25 +00:00
|
|
|
writer.WriteBytes(bytes)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, writer.Err)
|
2020-02-17 12:04:25 +00:00
|
|
|
}
|
|
|
|
}
|