Skip to content

Commit 0f1c9ca

Browse files
committed
Merge branch 'master' of https://github.com/DNAProject/DNA
2 parents 7454578 + 0ab4c7d commit 0f1c9ca

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,48 @@
11
package program
22

33
import (
4-
"bytes"
4+
. "DNA/common"
55
"DNA/vm"
6+
"bytes"
67
"math/big"
7-
. "DNA/common"
88
)
99

1010
type ProgramBuilder struct {
1111
buffer bytes.Buffer
12-
1312
}
1413

15-
func NewProgramBuilder() *ProgramBuilder{
14+
func NewProgramBuilder() *ProgramBuilder {
1615
return &ProgramBuilder{
17-
//TODO: add sync pool for create ProgramBuilder
16+
//TODO: add sync pool for create ProgramBuilder
1817
}
1918
}
2019

21-
func (pb *ProgramBuilder) AddOp(op vm.OpCode){
20+
func (pb *ProgramBuilder) AddOp(op vm.OpCode) {
2221
pb.buffer.WriteByte(byte(op))
2322
}
2423

25-
func (pb *ProgramBuilder) AddCodes(codes []byte){
24+
func (pb *ProgramBuilder) AddCodes(codes []byte) {
2625
pb.buffer.Write(codes)
2726
}
2827

29-
func (pb *ProgramBuilder) PushNumber(number *big.Int){
28+
func (pb *ProgramBuilder) PushNumber(number *big.Int) {
3029
if number.Cmp(big.NewInt(-1)) == 0 {
31-
pb.AddOp(vm.NEGATE)
30+
pb.AddOp(vm.PUSHM1)
3231
return
3332
}
34-
if number.Cmp(big.NewInt(0)) == 0{
33+
if number.Cmp(big.NewInt(0)) == 0 {
3534
pb.AddOp(vm.PUSH0)
3635
return
3736
}
38-
if number.Cmp(big.NewInt(0)) == 1 && number.Cmp(big.NewInt(16)) <= 0{
37+
if number.Cmp(big.NewInt(0)) == 1 && number.Cmp(big.NewInt(16)) <= 0 {
3938
pb.AddOp(vm.OpCode(byte(vm.PUSH1) - 1 + number.Bytes()[0]))
4039
return
4140
}
4241
pb.PushData(number.Bytes())
4342
}
4443

45-
46-
func (pb *ProgramBuilder) PushData(data []byte){
47-
if data == nil{
44+
func (pb *ProgramBuilder) PushData(data []byte) {
45+
if data == nil {
4846
return //TODO: add error
4947
}
5048

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

71-
func (pb *ProgramBuilder) ToArray() []byte{
69+
func (pb *ProgramBuilder) ToArray() []byte {
7270
return pb.buffer.Bytes()
7371
}
74-
75-
76-
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package program
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"testing"
7+
)
8+
9+
func TestProgramBuilder_PushData(t *testing.T) {
10+
for i := int64(-20); i <= 26; i++ {
11+
pb := NewProgramBuilder()
12+
testx := big.NewInt(i)
13+
pb.PushNumber(testx)
14+
fmt.Printf("%d=%#v\n", i, pb.ToArray())
15+
}
16+
17+
}

core/transaction/TransactionBuilder.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ func NewTransferAssetTransaction(inputs []*UTXOTxInput, outputs []*TxOutput) (*T
5252

5353
assetRegPayload := &payload.TransferAsset{}
5454

55-
return &Transaction{
56-
UTXOInputs: []*UTXOTxInput{},
57-
BalanceInputs: []*BalanceTxInput{},
58-
Attributes: []*TxAttribute{},
59-
TxType: TransferAsset,
60-
Payload: assetRegPayload,
61-
Programs: []*program.Program{},
62-
}, nil
55+
return &Transaction{
56+
TxType: TransferAsset,
57+
Payload: assetRegPayload,
58+
Attributes: []*TxAttribute{},
59+
UTXOInputs: inputs,
60+
BalanceInputs: []*BalanceTxInput{},
61+
Outputs: outputs,
62+
Programs: []*program.Program{},
63+
}, nil
6364
}
6465

6566
//initial a new transaction with record payload

0 commit comments

Comments
 (0)