OSDN Git Service

Feat(BVM): change block height and amount func (#1905)
authorxuexiansong <s.xue.xian.c@gmail.com>
Tue, 20 Apr 2021 13:55:02 +0000 (21:55 +0800)
committerGitHub <noreply@github.com>
Tue, 20 Apr 2021 13:55:02 +0000 (21:55 +0800)
* Feat(BVM): change block height and amount func

* Feat(BVM): change block height and amount func

blockchain/query/annotated.go
protocol/vm/assemble.go
protocol/vm/introspection.go
protocol/vm/pushdata.go
protocol/vm/pushdata_test.go
protocol/vm/splice.go
protocol/vm/vmutil/builder.go
protocol/vm/vmutil/script.go
protocol/vm/vmutil/script_test.go

index c1bb4fd..04277f3 100644 (file)
@@ -75,7 +75,7 @@ type AnnotatedAsset struct {
        IssuanceProgram   chainjson.HexBytes `json:"issue_program"`
        RawDefinitionByte chainjson.HexBytes `json:"raw_definition_byte"`
        Definition        *json.RawMessage   `json:"definition"`
-       LimitHeight       int64              `json:"limit_height"`
+       LimitHeight       uint64              `json:"limit_height"`
 }
 
 //AnnotatedSigner means an annotated signer for asset.
index e524280..040745e 100644 (file)
@@ -79,7 +79,7 @@ func Assemble(s string) (res []byte, err error) {
                        if err != nil {
                                return nil, err
                        }
-                       res = append(res, PushdataBytes(bytes)...)
+                       res = append(res, PushDataBytes(bytes)...)
                } else if len(token) >= 2 && token[0] == '\'' && token[len(token)-1] == '\'' {
                        bytes := make([]byte, 0, len(token)-2)
                        var b int
@@ -90,9 +90,9 @@ func Assemble(s string) (res []byte, err error) {
                                bytes = append(bytes, token[i])
                                b++
                        }
-                       res = append(res, PushdataBytes(bytes)...)
+                       res = append(res, PushDataBytes(bytes)...)
                } else if num, ok := checked.NewUInt256(token); ok {
-                       res = append(res, PushdataBytes(BigIntBytes(num))...)
+                       res = append(res, PushDataBytes(BigIntBytes(num))...)
                } else {
                        return nil, errors.Wrap(ErrToken, token)
                }
index 7a05623..36b71c7 100644 (file)
@@ -25,13 +25,16 @@ func opCheckOutput(vm *virtualMachine) error {
        if err != nil {
                return err
        }
-       amount, err := vm.popInt64(true)
+       amountInt, err := vm.popBigInt(true)
        if err != nil {
                return err
        }
-       if amount < 0 {
+
+       amount, overflow := amountInt.Uint64WithOverflow()
+       if overflow {
                return ErrBadValue
        }
+
        index, err := vm.popInt64(true)
        if err != nil {
                return err
@@ -44,7 +47,7 @@ func opCheckOutput(vm *virtualMachine) error {
                return ErrContext
        }
 
-       ok, err := vm.context.CheckOutput(uint64(index), uint64(amount), assetID, uint64(vmVersion), code, vm.expansionReserved)
+       ok, err := vm.context.CheckOutput(uint64(index), amount, assetID, uint64(vmVersion), code, vm.expansionReserved)
        if err != nil {
                return err
        }
@@ -72,7 +75,8 @@ func opAmount(vm *virtualMachine) error {
        if vm.context.Amount == nil {
                return ErrContext
        }
-       return vm.pushInt64(int64(*vm.context.Amount), true)
+
+       return vm.pushBigInt(uint256.NewInt().SetUint64(*vm.context.Amount), true)
 }
 
 func opProgram(vm *virtualMachine) error {
index 0edf329..1d28e35 100644 (file)
@@ -32,7 +32,8 @@ func opNop(vm *virtualMachine) error {
        return vm.applyCost(1)
 }
 
-func PushdataBytes(in []byte) []byte {
+// PushDataBytes push bytes to stack
+func PushDataBytes(in []byte) []byte {
        l := len(in)
        if l == 0 {
                return []byte{byte(OP_0)}
@@ -53,12 +54,13 @@ func PushdataBytes(in []byte) []byte {
        return append([]byte{byte(OP_PUSHDATA4), b[0], b[1], b[2], b[3]}, in...)
 }
 
-func PushdataInt64(n int64) []byte {
+// PushDataInt64 push int64 to stack
+func PushDataInt64(n int64) []byte {
        if n == 0 {
                return []byte{byte(OP_0)}
        }
        if n >= 1 && n <= 16 {
                return []byte{uint8(OP_1) + uint8(n) - 1}
        }
-       return PushdataBytes(Int64Bytes(n))
+       return PushDataBytes(Int64Bytes(n))
 }
index ac01b99..964685b 100644 (file)
@@ -134,14 +134,14 @@ func TestPushDataBytes(t *testing.T) {
        }
 
        for _, c := range cases {
-               got := PushdataBytes(c.data)
+               got := PushDataBytes(c.data)
 
                dl := len(c.data)
                if dl > 10 {
                        dl = 10
                }
                if !bytes.Equal(got, c.want) {
-                       t.Errorf("PushdataBytes(%x...) = %x...[%d] want %x...[%d]", c.data[:dl], got[:dl], len(got), c.want[:dl], len(c.want))
+                       t.Errorf("PushDataBytes(%x...) = %x...[%d] want %x...[%d]", c.data[:dl], got[:dl], len(got), c.want[:dl], len(c.want))
                }
        }
 }
@@ -179,10 +179,10 @@ func TestPushdataInt64(t *testing.T) {
        }
 
        for _, c := range cases {
-               got := PushdataInt64(c.num)
+               got := PushDataInt64(c.num)
 
                if !bytes.Equal(got, c.want) {
-                       t.Errorf("PushdataInt64(%d) = %x want %x", c.num, got, c.want)
+                       t.Errorf("PushDataInt64(%d) = %x want %x", c.num, got, c.want)
                }
        }
 }
index 560defb..13a7cc6 100644 (file)
@@ -166,5 +166,5 @@ func opCatpushdata(vm *virtualMachine) error {
                return err
        }
        vm.deferCost(-lens)
-       return vm.push(append(a, PushdataBytes(b)...), true)
+       return vm.push(append(a, PushDataBytes(b)...), true)
 }
index f2ffb85..56b6be2 100644 (file)
@@ -28,13 +28,13 @@ func NewBuilder() *Builder {
 
 // AddInt64 adds a pushdata instruction for an integer value.
 func (b *Builder) AddInt64(n int64) *Builder {
-       b.program = append(b.program, vm.PushdataInt64(n)...)
+       b.program = append(b.program, vm.PushDataInt64(n)...)
        return b
 }
 
 // AddData adds a pushdata instruction for a given byte string.
 func (b *Builder) AddData(data []byte) *Builder {
-       b.program = append(b.program, vm.PushdataBytes(data)...)
+       b.program = append(b.program, vm.PushDataBytes(data)...)
        return b
 }
 
index 2de83d6..c09cfdf 100644 (file)
@@ -136,18 +136,23 @@ func checkMultiSigParams(nrequired, npubkeys int64) error {
 
 // GetIssuanceProgramRestrictHeight return issuance program restrict height
 // if height invalid return 0
-func GetIssuanceProgramRestrictHeight(program []byte) int64 {
+func GetIssuanceProgramRestrictHeight(program []byte) uint64 {
        insts, err := vm.ParseProgram(program)
        if err != nil {
                return 0
        }
 
        if len(insts) >= 4 && insts[0].IsPushdata() && insts[1].Op == vm.OP_BLOCKHEIGHT && insts[2].Op == vm.OP_GREATERTHAN && insts[3].Op == vm.OP_VERIFY {
-               height, err := vm.AsInt64(insts[0].Data)
+               heightInt, err := vm.AsBigInt(insts[0].Data)
                if err != nil {
                        return 0
                }
 
+               height, overflow := heightInt.Uint64WithOverflow()
+               if overflow {
+                       return 0
+               }
+
                return height
        }
        return 0
index 4a4df8c..1184323 100644 (file)
@@ -192,7 +192,7 @@ func TestP2SPMultiSigProgramWithHeight(t *testing.T) {
 func TestGetIssuanceProgramRestrictHeight(t *testing.T) {
        tests := []struct {
                issuanceProgram string
-               wantHeight      int64
+               wantHeight      uint64
        }{
                {
                        issuanceProgram: "",
@@ -206,6 +206,10 @@ func TestGetIssuanceProgramRestrictHeight(t *testing.T) {
                        issuanceProgram: "01c8cda069ae20f44dd85be89de08b0f894476ccc7b3eebcf0a288c79504fa7e4c8033f5b7338020c86dc682ce3ecac64e165d9b5f8cca9ee05bd0d4df07adbfd11251ad7e88f1685152ad",
                        wantHeight:      200,
                },
+               {
+                       issuanceProgram: "08c8c8c8c8c8c8c8c8cda069ae20f44dd85be89de08b0f894476ccc7b3eebcf0a288c79504fa7e4c8033f5b7338020c86dc682ce3ecac64e165d9b5f8cca9ee05bd0d4df07adbfd11251ad7e88f1685152ad",
+                       wantHeight:      14468034567615334600,
+               },
        }
 
        for i, test := range tests {