diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 355184706..07e6ffe45 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" "testing" "time" @@ -17,6 +18,7 @@ import ( "github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/util" + "github.com/CityOfZion/neo-go/pkg/vm/emit" "github.com/CityOfZion/neo-go/pkg/vm/opcode" "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" @@ -163,3 +165,78 @@ func newDumbBlock() *block.Block { }, } } + +// 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) { + bc := newTestChain(t) + n := 50 + blocks := makeBlocks(n) + + for i := 0; i < len(blocks); i++ { + if err := bc.AddBlock(blocks[i]); err != nil { + t.Fatal(err) + } + } + + tx1 := newMinerTX() + + avm, err := ioutil.ReadFile("../rpc/testdata/test_contract.avm") + if err != nil { + t.Fatal(err) + } + + 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)) + + block := newBlock(uint32(n+1), tx1, tx2) + if err := bc.AddBlock(block); err != nil { + t.Fatal(err) + } + + outStream, err := os.Create("../rpc/testdata/testblocks.acc") + if err != nil { + t.Fatal(err) + } + 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) + if err != nil { + t.Fatal(err) + } + buf := io.NewBufBinWriter() + b.EncodeBinary(buf.BinWriter) + bytes := buf.Bytes() + writer.WriteBytes(bytes) + if writer.Err != nil { + t.Fatal(err) + } + } +} diff --git a/pkg/rpc/server_helper_test.go b/pkg/rpc/server_helper_test.go index b1fd3b491..5451d4e1b 100644 --- a/pkg/rpc/server_helper_test.go +++ b/pkg/rpc/server_helper_test.go @@ -189,6 +189,12 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu go chain.Run() + // File "./testdata/testblocks.acc" was generated by function core._ + // ("neo-go/pkg/core/helper_test.go"). + // To generate new "./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` f, err := os.Open("testdata/testblocks.acc") require.Nil(t, err) br := io.NewBinReaderFromIO(f) diff --git a/pkg/rpc/testdata/test_contract.avm b/pkg/rpc/testdata/test_contract.avm new file mode 100755 index 000000000..10193d3de --- /dev/null +++ b/pkg/rpc/testdata/test_contract.avm @@ -0,0 +1 @@ +QÅk Hello, world!hNeo.Runtime.Logaluf \ No newline at end of file