Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/DNAProject/DNA
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamfly281 committed May 16, 2017
2 parents 7454578 + 0ab4c7d commit 0f1c9ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
31 changes: 13 additions & 18 deletions core/contract/program/ProgramBuilder.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
package program

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

type ProgramBuilder struct {
buffer bytes.Buffer

}

func NewProgramBuilder() *ProgramBuilder{
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 vm.OpCode) {
pb.buffer.WriteByte(byte(op))
}

func (pb *ProgramBuilder) AddCodes(codes []byte){
func (pb *ProgramBuilder) AddCodes(codes []byte) {
pb.buffer.Write(codes)
}

func (pb *ProgramBuilder) PushNumber(number *big.Int){
func (pb *ProgramBuilder) PushNumber(number *big.Int) {
if number.Cmp(big.NewInt(-1)) == 0 {
pb.AddOp(vm.NEGATE)
pb.AddOp(vm.PUSHM1)
return
}
if number.Cmp(big.NewInt(0)) == 0{
if number.Cmp(big.NewInt(0)) == 0 {
pb.AddOp(vm.PUSH0)
return
}
if number.Cmp(big.NewInt(0)) == 1 && number.Cmp(big.NewInt(16)) <= 0{
if number.Cmp(big.NewInt(0)) == 1 && number.Cmp(big.NewInt(16)) <= 0 {
pb.AddOp(vm.OpCode(byte(vm.PUSH1) - 1 + number.Bytes()[0]))
return
}
pb.PushData(number.Bytes())
}


func (pb *ProgramBuilder) PushData(data []byte){
if data == nil{
func (pb *ProgramBuilder) PushData(data []byte) {
if data == nil {
return //TODO: add error
}

Expand All @@ -68,9 +66,6 @@ func (pb *ProgramBuilder) PushData(data []byte){
}
}

func (pb *ProgramBuilder) ToArray() []byte{
func (pb *ProgramBuilder) ToArray() []byte {
return pb.buffer.Bytes()
}



17 changes: 17 additions & 0 deletions core/contract/program/ProgramBuilder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package program

import (
"fmt"
"math/big"
"testing"
)

func TestProgramBuilder_PushData(t *testing.T) {
for i := int64(-20); i <= 26; i++ {
pb := NewProgramBuilder()
testx := big.NewInt(i)
pb.PushNumber(testx)
fmt.Printf("%d=%#v\n", i, pb.ToArray())
}

}
17 changes: 9 additions & 8 deletions core/transaction/TransactionBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ func NewTransferAssetTransaction(inputs []*UTXOTxInput, outputs []*TxOutput) (*T

assetRegPayload := &payload.TransferAsset{}

return &Transaction{
UTXOInputs: []*UTXOTxInput{},
BalanceInputs: []*BalanceTxInput{},
Attributes: []*TxAttribute{},
TxType: TransferAsset,
Payload: assetRegPayload,
Programs: []*program.Program{},
}, nil
return &Transaction{
TxType: TransferAsset,
Payload: assetRegPayload,
Attributes: []*TxAttribute{},
UTXOInputs: inputs,
BalanceInputs: []*BalanceTxInput{},
Outputs: outputs,
Programs: []*program.Program{},
}, nil
}

//initial a new transaction with record payload
Expand Down

0 comments on commit 0f1c9ca

Please sign in to comment.