OSDN Git Service

update master (#487)
[bytom/bytom.git] / blockchain / txbuilder / txbuilder.go
old mode 100644 (file)
new mode 100755 (executable)
index d3ace5f..e8c791c
@@ -6,23 +6,31 @@ import (
        "context"
        "time"
 
+       log "github.com/sirupsen/logrus"
+
        "github.com/bytom/crypto/ed25519/chainkd"
        "github.com/bytom/errors"
        "github.com/bytom/math/checked"
        "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/legacy"
-
-       log "github.com/sirupsen/logrus"
+       "github.com/bytom/protocol/bc/types"
 )
 
+// errors
 var (
-       ErrBadRefData          = errors.New("transaction reference data does not match previous template's reference data")
-       ErrBadTxInputIdx       = errors.New("unsigned tx missing input")
+       //ErrBadRefData means invalid reference data
+       ErrBadRefData = errors.New("transaction reference data does not match previous template's reference data")
+       //ErrBadTxInputIdx means unsigned tx input
+       ErrBadTxInputIdx = errors.New("unsigned tx missing input")
+       //ErrBadWitnessComponent means invalid witness component
        ErrBadWitnessComponent = errors.New("invalid witness component")
-       ErrBadAmount           = errors.New("bad asset amount")
-       ErrBlankCheck          = errors.New("unsafe transaction: leaves assets free to control")
-       ErrAction              = errors.New("errors occurred in one or more actions")
-       ErrMissingFields       = errors.New("required field is missing")
+       //ErrBadAmount means invalid asset amount
+       ErrBadAmount = errors.New("bad asset amount")
+       //ErrBlankCheck means unsafe transaction
+       ErrBlankCheck = errors.New("unsafe transaction: leaves assets free to control")
+       //ErrAction means errors occurred in actions
+       ErrAction = errors.New("errors occurred in one or more actions")
+       //ErrMissingFields means missing required fields
+       ErrMissingFields = errors.New("required field is missing")
 )
 
 // Build builds or adds on to a transaction.
@@ -30,21 +38,20 @@ var (
 // Build partners then satisfy and consume inputs and destinations.
 // The final party must ensure that the transaction is
 // balanced before calling finalize.
-func Build(ctx context.Context, tx *legacy.TxData, actions []Action, maxTime time.Time) (*Template, error) {
+func Build(ctx context.Context, tx *types.TxData, actions []Action, maxTime time.Time, timeRange uint64) (*Template, error) {
        builder := TemplateBuilder{
-               base:    tx,
-               maxTime: maxTime,
+               base:      tx,
+               maxTime:   maxTime,
+               timeRange: timeRange,
        }
 
        // Build all of the actions, updating the builder.
        var errs []error
        for i, action := range actions {
                err := action.Build(ctx, &builder)
-
-               log.WithFields(log.Fields{"action": action, "error": err}).Info("Loop tx's action")
                if err != nil {
-                       err = errors.WithData(err, "index", i)
-                       errs = append(errs, err)
+                       log.WithFields(log.Fields{"action index": i, "error": err}).Error("Loop tx's action")
+                       errs = append(errs, errors.WithDetailf(err, "action index %v", i))
                }
        }
 
@@ -72,20 +79,28 @@ func Build(ctx context.Context, tx *legacy.TxData, actions []Action, maxTime tim
        return tpl, nil
 }
 
-
+// Sign will try to sign all the witness
 func Sign(ctx context.Context, tpl *Template, xpubs []chainkd.XPub, auth string, signFn SignFunc) error {
        for i, sigInst := range tpl.SigningInstructions {
-               for j, sw := range sigInst.SignatureWitnesses {
-                       err := sw.sign(ctx, tpl, uint32(i), xpubs, auth, signFn)
-                       if err != nil {
-                               return errors.WithDetailf(err, "adding signature(s) to witness component %d of input %d", j, i)
+               for j, wc := range sigInst.WitnessComponents {
+                       switch sw := wc.(type) {
+                       case *SignatureWitness:
+                               err := sw.sign(ctx, tpl, uint32(i), xpubs, auth, signFn)
+                               if err != nil {
+                                       return errors.WithDetailf(err, "adding signature(s) to signature witness component %d of input %d", j, i)
+                               }
+                       case *RawTxSigWitness:
+                               err := sw.sign(ctx, tpl, uint32(i), xpubs, auth, signFn)
+                               if err != nil {
+                                       return errors.WithDetailf(err, "adding signature(s) to raw-signature witness component %d of input %d", j, i)
+                               }
                        }
                }
        }
        return materializeWitnesses(tpl)
 }
 
-func checkBlankCheck(tx *legacy.TxData) error {
+func checkBlankCheck(tx *types.TxData) error {
        assetMap := make(map[bc.AssetID]int64)
        var ok bool
        for _, in := range tx.Inputs {