Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record restful interface #420

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func BytesToInt16(b []byte) int16 {
return int16(tmp)
}

func BytesToInt(b [] byte) []int {
i := make([]int, len(b))
for k,v := range b {
i[k] = int(v)
}
return i
}

func IsEqualBytes(b1 []byte, b2 []byte) bool {
len1 := len(b1)
len2 := len(b2)
Expand All @@ -72,6 +80,15 @@ func HexToBytes(value string) ([]byte, error) {
return hex.DecodeString(value)
}

func ToArrayReverse(arr []byte) []byte {
l := len(arr)
x := make([]byte, 0)
for i := l - 1; i >= 0 ;i--{
x = append(x, arr[i])
}
return x
}

func BytesReverse(u []byte) []byte {
for i, j := 0, len(u)-1; i < j; i, j = i+1, j-1 {
u[i], u[j] = u[j], u[i]
Expand Down
26 changes: 26 additions & 0 deletions common/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package common

import "math/big"

const (
HashLength = 32
)

type Hash [HashLength]byte

func BytesToHash(b []byte) Hash {
var h Hash
h.SetBytes(b)
return h
}

func (h *Hash) SetBytes(b []byte) {
if len(b) > len(h) {
b = b[len(b)-HashLength:]
}
copy(h[HashLength-len(b):], b)
}

func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
func (h Hash) Bytes() []byte { return h[:] }
11 changes: 11 additions & 0 deletions common/serialization/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,14 @@ func ReadBool(reader io.Reader) (bool, error) {
err := binary.Read(reader, binary.LittleEndian, &x)
return x, err
}

func WriteByte(writer io.Writer, val byte) error {
err := binary.Write(writer, binary.LittleEndian, val)
return err
}

func ReadByte(reader io.Reader) (byte, error) {
var x byte
err := binary.Read(reader, binary.LittleEndian, &x)
return x, err
}
21 changes: 21 additions & 0 deletions common/uint160.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,24 @@ func ToScriptHash(address string) (Uint160, error) {

return ph, nil
}

func (u *Uint160) SetBytes(b []byte) {
if len(b) > len(u) {
b = b[len(b)-UINT160SIZE:]
}
copy(u[UINT160SIZE-len(b):], b)
}

func BytesToUint160(b []byte) Uint160 {
u := new(Uint160)
u.SetBytes(b)
return *u
}

func BigToUint160(b *big.Int) Uint160 {
return BytesToUint160(b.Bytes())
}

func (u *Uint160) Big() *big.Int {
return new(big.Int).SetBytes(u.ToArray()[:])
}
8 changes: 8 additions & 0 deletions core/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
. "DNA/errors"
"errors"
"io"
"bytes"
)

//AssetType
Expand Down Expand Up @@ -98,3 +99,10 @@ func (a *Asset) Deserialize(r io.Reader) error {
}
return nil
}


func (a *Asset) ToArray() ([]byte) {
b := new(bytes.Buffer)
a.Serialize(b)
return b.Bytes()
}
41 changes: 27 additions & 14 deletions core/code/FunctionCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,41 @@ type FunctionCode struct {
ParameterTypes []ContractParameterType

// Contract return type list
ReturnTypes []ContractParameterType
ReturnType ContractParameterType

codeHash Uint160
}

// method of SerializableData
func (fc *FunctionCode) Serialize(w io.Writer) error {
err := serialization.WriteVarBytes(w,ContractParameterTypeToByte(fc.ParameterTypes))
err := serialization.WriteByte(w, byte(fc.ReturnType))
if err != nil {
return err
}
err = serialization.WriteVarBytes(w, ContractParameterTypeToByte(fc.ParameterTypes))
if err != nil {
return err
}

err = serialization.WriteVarBytes(w,fc.Code)
if err != nil {
return err
}

return nil
}

// method of SerializableData
func (fc *FunctionCode) Deserialize(r io.Reader) error {
p,err := serialization.ReadVarBytes(r)
returnType, err := serialization.ReadByte(r)
if err != nil {
return err
}
fc.ParameterTypes = ByteToContractParameterType(p)
fc.ReturnType = ContractParameterType(returnType)

parameterTypes, err := serialization.ReadVarBytes(r)
if err != nil {
return err
}
fc.ParameterTypes = ByteToContractParameterType(parameterTypes)

fc.Code,err = serialization.ReadVarBytes(r)
if err != nil {
Expand All @@ -65,18 +75,21 @@ func (fc *FunctionCode) GetParameterTypes() []ContractParameterType {

// method of ICode
// Get the list of return value
func (fc *FunctionCode) GetReturnTypes() []ContractParameterType {
return fc.ReturnTypes
func (fc *FunctionCode) GetReturnType() ContractParameterType {
return fc.ReturnType
}

// method of ICode
// Get the hash of the smart contract
func (fc *FunctionCode) CodeHash() Uint160 {
hash,err := ToCodeHash(fc.Code)
if err != nil {
log.Debug( fmt.Sprintf("[FunctionCode] ToCodeHash err=%s",err) )
return Uint160{0}
u160 := Uint160{}
if fc.codeHash == u160 {
u160, err := ToCodeHash(fc.Code)
if err != nil {
log.Debug( fmt.Sprintf("[FunctionCode] ToCodeHash err=%s",err) )
return u160
}
fc.codeHash = u160
}

return hash
return fc.codeHash
}
19 changes: 9 additions & 10 deletions core/contract/contract.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package contract

import (
"bytes"
"errors"
"io"

. "DNA/common"
"DNA/common/serialization"
. "DNA/errors"
"DNA/vm"
"bytes"
"errors"
"io"
"DNA/vm/avm"
)

//Contract address is the hash of contract program .
//which be used to control asset or indicate the smart contract address
//which be used to control asset or indicate the smart contract address �?

//Contract include the program codes with parameters which can be executed on specific evnrioment
type Contract struct {
Expand All @@ -35,7 +34,7 @@ func (c *Contract) IsStandard() bool {
if len(c.Code) != 35 {
return false
}
if c.Code[0] != 33 || c.Code[34] != byte(vm.CHECKSIG) {
if c.Code[0] != 33 || c.Code[34] != byte(avm.CHECKSIG) {
return false
}
return true
Expand All @@ -49,10 +48,10 @@ func (c *Contract) IsMultiSigContract() bool {
if len(c.Code) < 37 {
return false
}
if c.Code[i] > byte(vm.PUSH16) {
if c.Code[i] > byte(avm.PUSH16) {
return false
}
if c.Code[i] < byte(vm.PUSH1) && c.Code[i] != 1 && c.Code[i] != 2 {
if c.Code[i] < byte(avm.PUSH1) && c.Code[i] != 1 && c.Code[i] != 2 {
return false
}

Expand Down Expand Up @@ -111,7 +110,7 @@ func (c *Contract) IsMultiSigContract() bool {
break
}

if c.Code[i] != byte(vm.CHECKMULTISIG) {
if c.Code[i] != byte(avm.CHECKMULTISIG) {
return false
}
i++
Expand Down
6 changes: 3 additions & 3 deletions core/contract/contractBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
pg "DNA/core/contract/program"
"DNA/crypto"
. "DNA/errors"
"DNA/vm"
"math/big"
"sort"
"DNA/vm/avm"
)

//create a Single Singature contract for owner
Expand Down Expand Up @@ -43,7 +43,7 @@ func CreateSignatureRedeemScript(pubkey *crypto.PubKey) ([]byte, error) {
}
sb := pg.NewProgramBuilder()
sb.PushData(temp)
sb.AddOp(vm.CHECKSIG)
sb.AddOp(avm.CHECKSIG)
return sb.ToArray(), nil
}

Expand Down Expand Up @@ -90,6 +90,6 @@ func CreateMultiSigRedeemScript(m int, pubkeys []*crypto.PubKey) ([]byte, error)
}

sb.PushNumber(big.NewInt(int64(len(pubkeys))))
sb.AddOp(vm.CHECKMULTISIG)
sb.AddOp(avm.CHECKMULTISIG)
return sb.ToArray(), nil
}
5 changes: 5 additions & 0 deletions core/contract/contractParameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ const (
Hash160
Hash256
ByteArray
PublicKey
String
Object
Array = 0x10
Void = 0xff
)
20 changes: 10 additions & 10 deletions core/contract/program/ProgramBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package program

import (
. "DNA/common"
"DNA/vm"
"bytes"
"math/big"
"DNA/vm/avm"
)

type ProgramBuilder struct {
Expand All @@ -13,11 +13,11 @@ type ProgramBuilder struct {

func NewProgramBuilder() *ProgramBuilder {
return &ProgramBuilder{
//TODO: add sync pool for create ProgramBuilder
//TODO: add sync pool for create ProgramBuilder
}
}

func (pb *ProgramBuilder) AddOp(op vm.OpCode) {
func (pb *ProgramBuilder) AddOp(op avm.OpCode) {
pb.buffer.WriteByte(byte(op))
}

Expand All @@ -27,15 +27,15 @@ func (pb *ProgramBuilder) AddCodes(codes []byte) {

func (pb *ProgramBuilder) PushNumber(number *big.Int) {
if number.Cmp(big.NewInt(-1)) == 0 {
pb.AddOp(vm.PUSHM1)
pb.AddOp(avm.PUSHM1)
return
}
if number.Cmp(big.NewInt(0)) == 0 {
pb.AddOp(vm.PUSH0)
pb.AddOp(avm.PUSH0)
return
}
if number.Cmp(big.NewInt(0)) == 1 && number.Cmp(big.NewInt(16)) <= 0 {
pb.AddOp(vm.OpCode(byte(vm.PUSH1) - 1 + number.Bytes()[0]))
pb.AddOp(avm.OpCode(byte(avm.PUSH1) - 1 + number.Bytes()[0]))
return
}
pb.PushData(number.Bytes())
Expand All @@ -46,20 +46,20 @@ func (pb *ProgramBuilder) PushData(data []byte) {
return //TODO: add error
}

if len(data) <= int(vm.PUSHBYTES75) {
if len(data) <= int(avm.PUSHBYTES75) {
pb.buffer.WriteByte(byte(len(data)))
pb.buffer.Write(data[0:len(data)])
} else if len(data) < 0x100 {
pb.AddOp(vm.PUSHDATA1)
pb.AddOp(avm.PUSHDATA1)
pb.buffer.WriteByte(byte(len(data)))
pb.buffer.Write(data[0:len(data)])
} else if len(data) < 0x10000 {
pb.AddOp(vm.PUSHDATA2)
pb.AddOp(avm.PUSHDATA2)
dataByte := IntToBytes(len(data))
pb.buffer.Write(dataByte[0:2])
pb.buffer.Write(data[0:len(data)])
} else {
pb.AddOp(vm.PUSHDATA4)
pb.AddOp(avm.PUSHDATA4)
dataByte := IntToBytes(len(data))
pb.buffer.Write(dataByte[0:4])
pb.buffer.Write(data[0:len(data)])
Expand Down
11 changes: 9 additions & 2 deletions core/ledger/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"DNA/core/transaction/payload"
"DNA/crypto"
. "DNA/errors"
"DNA/vm"
"DNA/vm/avm"
"io"
"time"
"bytes"
)

const BlockVersion uint32 = 0
Expand Down Expand Up @@ -116,6 +117,12 @@ func (b *Block) GetMessage() []byte {
return sig.GetHashData(b)
}

func (b *Block) ToArray() ([]byte) {
bf := new(bytes.Buffer)
b.Serialize(bf)
return bf.Bytes()
}

func (b *Block) GetProgramHashes() ([]Uint160, error) {

return b.Blockdata.GetProgramHashes()
Expand Down Expand Up @@ -164,7 +171,7 @@ func GenesisBlockInit(defaultBookKeeper []*crypto.PubKey) (*Block, error) {
NextBookKeeper: nextBookKeeper,
Program: &program.Program{
Code: []byte{},
Parameter: []byte{byte(vm.PUSHT)},
Parameter: []byte{byte(avm.PUSHT)},
},
}
//transaction
Expand Down
Loading