From d67a0e207c41aeee7d578bbe94b0314eccc640d3 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 29 Jan 2015 00:11:19 -0600 Subject: [PATCH] Update for recent ScriptBuilder API change. --- mining.go | 18 ++++++++++++++---- rpcserver.go | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/mining.go b/mining.go index 9fc02f89..4f78db9c 100644 --- a/mining.go +++ b/mining.go @@ -200,7 +200,7 @@ func mergeTxStore(txStoreA btcchain.TxStore, txStoreB btcchain.TxStore) { // signature script of the coinbase transaction of a new block. In particular, // it starts with the block height that is required by version 2 blocks and adds // the extra nonce as well as additional coinbase flags. -func standardCoinbaseScript(nextBlockHeight int64, extraNonce uint64) []byte { +func standardCoinbaseScript(nextBlockHeight int64, extraNonce uint64) ([]byte, error) { return btcscript.NewScriptBuilder().AddInt64(nextBlockHeight). AddUint64(extraNonce).AddData([]byte(coinbaseFlags)).Script() } @@ -223,8 +223,12 @@ func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int64, addr btcutil return nil, err } } else { + var err error scriptBuilder := btcscript.NewScriptBuilder() - pkScript = scriptBuilder.AddOp(btcscript.OP_TRUE).Script() + pkScript, err = scriptBuilder.AddOp(btcscript.OP_TRUE).Script() + if err != nil { + return nil, err + } } tx := btcwire.NewMsgTx() @@ -439,7 +443,10 @@ func NewBlockTemplate(mempool *txMemPool, payToAddress btcutil.Address) (*BlockT // same value to the same public key address would otherwise be an // identical transaction for block version 1). extraNonce := uint64(0) - coinbaseScript := standardCoinbaseScript(nextBlockHeight, extraNonce) + coinbaseScript, err := standardCoinbaseScript(nextBlockHeight, extraNonce) + if err != nil { + return nil, err + } coinbaseTx, err := createCoinbaseTx(coinbaseScript, nextBlockHeight, payToAddress) if err != nil { @@ -841,7 +848,10 @@ func UpdateBlockTime(msgBlock *btcwire.MsgBlock, bManager *blockManager) error { // height. It also recalculates and updates the new merkle root that results // from changing the coinbase script. func UpdateExtraNonce(msgBlock *btcwire.MsgBlock, blockHeight int64, extraNonce uint64) error { - coinbaseScript := standardCoinbaseScript(blockHeight, extraNonce) + coinbaseScript, err := standardCoinbaseScript(blockHeight, extraNonce) + if err != nil { + return err + } if len(coinbaseScript) > btcchain.MaxCoinbaseScriptLen { return fmt.Errorf("coinbase transaction script length "+ "of %d is out of range (min: %d, max: %d)", diff --git a/rpcserver.go b/rpcserver.go index 0215e36c..a1a000ed 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -92,8 +92,8 @@ var ( // overhead of creating a new object on every invocation for constant // data. gbtCoinbaseAux = &btcjson.GetBlockTemplateResultAux{ - Flags: hex.EncodeToString(btcscript.NewScriptBuilder(). - AddData([]byte(coinbaseFlags)).Script()), + Flags: hex.EncodeToString(builderScript(btcscript. + NewScriptBuilder().AddData([]byte(coinbaseFlags)))), } // gbtCapabilities describes additional capabilities returned with a @@ -210,6 +210,18 @@ var rpcAskWallet = map[string]struct{}{ // Commands that are temporarily unimplemented. var rpcUnimplemented = map[string]struct{}{} +// builderScript is a convenience function which is used to for hard-coded +// scripts built with the script builder. Any errors are converted to a panic +// since it is only, and must only, be used with hard-coded, and therefore, +// known good, scripts. +func builderScript(builder *btcscript.ScriptBuilder) []byte { + script, err := builder.Script() + if err != nil { + panic(err) + } + return script +} + // workStateBlockInfo houses information about how to reconstruct a block given // its template and signature script. type workStateBlockInfo struct {