From 109b0d6eedc17d21c959a355d07d9db2eeef8000 Mon Sep 17 00:00:00 2001 From: Colt Date: Fri, 18 Aug 2017 14:37:19 +0800 Subject: [PATCH] clean the entry's data structure --- protocol/bc/bc.pb.go | 47 ++-------------------------- protocol/bc/bc.proto | 11 ++----- protocol/bc/coinbase.go | 4 +-- protocol/bc/entry_test.go | 2 +- protocol/bc/timerange.go | 20 ------------ protocol/bc/tx.go | 12 -------- protocol/validation/validation.go | 56 ++++++++++++++++------------------ protocol/validation/validation_test.go | 7 ----- 8 files changed, 33 insertions(+), 126 deletions(-) delete mode 100644 protocol/bc/timerange.go diff --git a/protocol/bc/bc.pb.go b/protocol/bc/bc.pb.go index e0515cc2..8e428547 100644 --- a/protocol/bc/bc.pb.go +++ b/protocol/bc/bc.pb.go @@ -22,7 +22,6 @@ It has these top-level messages: Nonce Output Retirement - TimeRange Issuance Spend */ @@ -476,9 +475,8 @@ func (m *Nonce) GetWitnessAnchoredId() *Hash { type Coinbase struct { Program *Program `protobuf:"bytes,1,opt,name=program" json:"program,omitempty"` - BlockId *Hash `protobuf:"bytes,2,opt,name=block_id,json=blockId" json:"block_id,omitempty"` - ExtHash *Hash `protobuf:"bytes,3,opt,name=ext_hash,json=extHash" json:"ext_hash,omitempty"` - WitnessArguments [][]byte `protobuf:"bytes,4,rep,name=witness_arguments,json=witnessArguments,proto3" json:"witness_arguments,omitempty"` + ExtHash *Hash `protobuf:"bytes,2,opt,name=ext_hash,json=extHash" json:"ext_hash,omitempty"` + WitnessArguments [][]byte `protobuf:"bytes,3,rep,name=witness_arguments,json=witnessArguments,proto3" json:"witness_arguments,omitempty"` } func (m *Coinbase) Reset() { *m = Coinbase{} } @@ -493,13 +491,6 @@ func (m *Coinbase) GetProgram() *Program { return nil } -func (m *Coinbase) GetBlockId() *Hash { - if m != nil { - return m.BlockId - } - return nil -} - func (m *Coinbase) GetExtHash() *Hash { if m != nil { return m.ExtHash @@ -602,38 +593,6 @@ func (m *Retirement) GetOrdinal() uint64 { return 0 } -type TimeRange struct { - MinTimeMs uint64 `protobuf:"varint,1,opt,name=min_time_ms,json=minTimeMs" json:"min_time_ms,omitempty"` - MaxTimeMs uint64 `protobuf:"varint,2,opt,name=max_time_ms,json=maxTimeMs" json:"max_time_ms,omitempty"` - ExtHash *Hash `protobuf:"bytes,3,opt,name=ext_hash,json=extHash" json:"ext_hash,omitempty"` -} - -func (m *TimeRange) Reset() { *m = TimeRange{} } -func (m *TimeRange) String() string { return proto.CompactTextString(m) } -func (*TimeRange) ProtoMessage() {} -func (*TimeRange) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{13} } - -func (m *TimeRange) GetMinTimeMs() uint64 { - if m != nil { - return m.MinTimeMs - } - return 0 -} - -func (m *TimeRange) GetMaxTimeMs() uint64 { - if m != nil { - return m.MaxTimeMs - } - return 0 -} - -func (m *TimeRange) GetExtHash() *Hash { - if m != nil { - return m.ExtHash - } - return nil -} - type Issuance struct { AnchorId *Hash `protobuf:"bytes,1,opt,name=anchor_id,json=anchorId" json:"anchor_id,omitempty"` Value *AssetAmount `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"` @@ -792,7 +751,7 @@ func init() { proto.RegisterType((*Nonce)(nil), "bc.Nonce") proto.RegisterType((*Output)(nil), "bc.Output") proto.RegisterType((*Retirement)(nil), "bc.Retirement") - proto.RegisterType((*TimeRange)(nil), "bc.TimeRange") + proto.RegisterType((*Coinbase)(nil), "bc.Coinbase") proto.RegisterType((*Issuance)(nil), "bc.Issuance") proto.RegisterType((*Spend)(nil), "bc.Spend") } diff --git a/protocol/bc/bc.proto b/protocol/bc/bc.proto index 81cbbb9e..0494ac95 100644 --- a/protocol/bc/bc.proto +++ b/protocol/bc/bc.proto @@ -86,9 +86,8 @@ message Nonce { message Coinbase { Program program = 1; - Hash block_id = 2; - Hash ext_hash = 3; - repeated bytes witness_arguments = 4; + Hash ext_hash = 2; + repeated bytes witness_arguments = 3; } message Output { @@ -106,12 +105,6 @@ message Retirement { uint64 ordinal = 4; } -message TimeRange { - uint64 min_time_ms = 1; - uint64 max_time_ms = 2; - Hash ext_hash = 3; -} - message Issuance { Hash anchor_id = 1; AssetAmount value = 2; diff --git a/protocol/bc/coinbase.go b/protocol/bc/coinbase.go index 8c50c85a..6e1e7803 100644 --- a/protocol/bc/coinbase.go +++ b/protocol/bc/coinbase.go @@ -5,13 +5,11 @@ import "io" func (Coinbase) typ() string { return "coinbase1" } func (n *Coinbase) writeForHash(w io.Writer) { mustWriteForHash(w, n.Program) - mustWriteForHash(w, n.BlockId) } // NewCoinbase creates a new Coinbase. -func NewCoinbase(p *Program, trID *Hash) *Coinbase { +func NewCoinbase(p *Program) *Coinbase { return &Coinbase{ Program: p, - BlockId: trID, } } diff --git a/protocol/bc/entry_test.go b/protocol/bc/entry_test.go index 51b41a00..cc7b6766 100644 --- a/protocol/bc/entry_test.go +++ b/protocol/bc/entry_test.go @@ -13,7 +13,7 @@ func BenchmarkEntryID(b *testing.B) { NewIssuance(nil, &AssetAmount{}, &Hash{}, 0), NewTxHeader(1, nil, &Hash{}, uint64(time.Now().Unix()), uint64(time.Now().Unix())), m, - NewNonce(&Program{Code: []byte{1}, VmVersion: 1}, nil), + NewNonce(&Program{Code: []byte{1}, VmVersion: 1}), NewOutput(&ValueSource{}, &Program{Code: []byte{1}, VmVersion: 1}, &Hash{}, 0), NewRetirement(&ValueSource{}, &Hash{}, 1), NewSpend(&Hash{}, &Hash{}, 0), diff --git a/protocol/bc/timerange.go b/protocol/bc/timerange.go deleted file mode 100644 index fc35bd53..00000000 --- a/protocol/bc/timerange.go +++ /dev/null @@ -1,20 +0,0 @@ -package bc - -import "io" - -// TimeRange denotes a time range. It satisfies the Entry interface. - -func (TimeRange) typ() string { return "timerange1" } -func (tr *TimeRange) writeForHash(w io.Writer) { - mustWriteForHash(w, tr.MinTimeMs) - mustWriteForHash(w, tr.MaxTimeMs) - mustWriteForHash(w, tr.ExtHash) -} - -// NewTimeRange creates a new TimeRange. -func NewTimeRange(minTimeMS, maxTimeMS uint64) *TimeRange { - return &TimeRange{ - MinTimeMs: minTimeMS, - MaxTimeMs: maxTimeMS, - } -} diff --git a/protocol/bc/tx.go b/protocol/bc/tx.go index 1a139196..dd678dc8 100644 --- a/protocol/bc/tx.go +++ b/protocol/bc/tx.go @@ -34,18 +34,6 @@ var ( ErrMissingEntry = errors.New("missing entry") ) -func (tx *Tx) TimeRange(id Hash) (*TimeRange, error) { - e, ok := tx.Entries[id] - if !ok || e == nil { - return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes()) - } - tr, ok := e.(*TimeRange) - if !ok { - return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e) - } - return tr, nil -} - func (tx *Tx) Output(id Hash) (*Output, error) { e, ok := tx.Entries[id] if !ok || e == nil { diff --git a/protocol/validation/validation.go b/protocol/validation/validation.go index 9b224047..414f229a 100644 --- a/protocol/validation/validation.go +++ b/protocol/validation/validation.go @@ -65,29 +65,27 @@ type validationState struct { } var ( - errBadTimeRange = errors.New("bad time range") - errEmptyResults = errors.New("transaction has no results") - errMismatchedAssetID = errors.New("mismatched asset id") - errMismatchedBlock = errors.New("mismatched block") - errMismatchedMerkleRoot = errors.New("mismatched merkle root") - errMismatchedPosition = errors.New("mismatched value source/dest positions") - errMismatchedReference = errors.New("mismatched reference") - errMismatchedValue = errors.New("mismatched value") - errMisorderedBlockHeight = errors.New("misordered block height") - errMisorderedBlockTime = errors.New("misordered block time") - errMissingField = errors.New("missing required field") - errNoGas = errors.New("no gas input") - errNoPrevBlock = errors.New("no previous block") - errNoSource = errors.New("no source for value") - errNonemptyExtHash = errors.New("non-empty extension hash") - errOverflow = errors.New("arithmetic overflow/underflow") - errPosition = errors.New("invalid source or destination position") - errTxVersion = errors.New("invalid transaction version") - errUnbalanced = errors.New("unbalanced") - errUntimelyTransaction = errors.New("block timestamp outside transaction time range") - errVersionRegression = errors.New("version regression") - errWrongBlockchain = errors.New("wrong blockchain") - errZeroTime = errors.New("timerange has one or two bounds set to zero") + errEmptyResults = errors.New("transaction has no results") + errMismatchedAssetID = errors.New("mismatched asset id") + errMismatchedBlock = errors.New("mismatched block") + errMismatchedMerkleRoot = errors.New("mismatched merkle root") + errMismatchedPosition = errors.New("mismatched value source/dest positions") + errMismatchedReference = errors.New("mismatched reference") + errMismatchedValue = errors.New("mismatched value") + errMisorderedBlockHeight = errors.New("misordered block height") + errMisorderedBlockTime = errors.New("misordered block time") + errMissingField = errors.New("missing required field") + errNoGas = errors.New("no gas input") + errNoPrevBlock = errors.New("no previous block") + errNoSource = errors.New("no source for value") + errNonemptyExtHash = errors.New("non-empty extension hash") + errOverflow = errors.New("arithmetic overflow/underflow") + errPosition = errors.New("invalid source or destination position") + errTxVersion = errors.New("invalid transaction version") + errUnbalanced = errors.New("unbalanced") + errUntimelyTransaction = errors.New("block timestamp outside transaction time range") + errVersionRegression = errors.New("version regression") + errWrongCoinbaseTransaction = errors.New("wrong coinbase transaction") ) func checkValid(vs *validationState, e bc.Entry) (err error) { @@ -103,13 +101,6 @@ func checkValid(vs *validationState, e bc.Entry) (err error) { switch e := e.(type) { case *bc.TxHeader: - // This does only part of the work of validating a tx header. The - // block-related parts of tx validation are in ValidateBlock. - if e.MaxTimeMs > 0 { - if e.MaxTimeMs < e.MinTimeMs { - return errors.WithDetailf(errBadTimeRange, "min time %d, max time %d", e.MinTimeMs, e.MaxTimeMs) - } - } for i, resID := range e.ResultIds { resultEntry := vs.tx.Entries[*resID] @@ -131,6 +122,11 @@ func checkValid(vs *validationState, e bc.Entry) (err error) { } } + case *bc.Coinbase: + if vs.block == nil || len(vs.block.Transactions) == 0 || vs.block.Transactions[0] != vs.tx { + return errWrongCoinbaseTransaction + } + case *bc.Mux: parity := make(map[bc.AssetID]int64) for i, src := range e.Sources { diff --git a/protocol/validation/validation_test.go b/protocol/validation/validation_test.go index 7e07262a..0e85cf07 100644 --- a/protocol/validation/validation_test.go +++ b/protocol/validation/validation_test.go @@ -186,13 +186,6 @@ func TestTxValidation(t *testing.T) { }, }, { - desc: "misordered tx time range", - f: func() { - tx.MinTimeMs = tx.MaxTimeMs + 1 - }, - err: errBadTimeRange, - }, - { desc: "empty tx results", f: func() { tx.ResultIds = nil -- 2.11.0