Skip to content
On this page

btcWallet中的数据库设计

Bucket相关

顶层Bucket

key列表: mgrVersionName mgrCreateDateName Bucket列表: 地址相关的

1. waddrmgrNamespaceKey

1.1 mainBucketName
    stores the encrypted crypto keys that encrypt all other generated keys
1.2 syncBucketName
    stores the current sync state of the root manager.
1.3 scopeBucketName
    		scopeBucketNme is the name of the top-level bucket within the
            hierarchy. It maps: purpose || coinType to a new sub-bucket that
            will house a scoped address manager. All buckets below are a child
            of this bucket:
            
            scopeBucket -> scope -> acctBucketName //account id=>dbDefaultAccountRow
            scopeBucket -> scope -> addrBucketName //addressID Hash=>dbAddressRow
            scopeBucket -> scope -> usedAddrBucketName // 一个地址是否被使用
            scopeBucket -> scope -> addrAcctIdxBucketName //addressID hash => account id
            scopeBucket -> scope -> acctNameIdxBucketName //accountName => account_id
            scopeBucket -> scope -> acctIDIdxBucketName //account_id => accountName
            scopeBucket -> scope -> metaBucket //metaData
            scopeBucket -> scope -> metaBucket -> lastAccountNameKey //manager中的最后一个account
            scopeBucket -> scope -> coinTypePrivKey //后面这两个代码没看到
            scopeBucket -> scope -> coinTypePubKey

目前已知的Scope有KeyScopeBIP0044,KeyScopeBIP0049Plus等 从这里也看出比特币的Key是树形结构,

1.4 scopeSchemaBucketName
scopeSchemaBucket is the name of the bucket that maps a particular
manager scope to the type of addresses that should be derived for
particular branches during key derivation.
go
	// KeyScopeBIP0049Plus is the key scope of our modified BIP0049
	// derivation. We say this is BIP0049 "plus", as we'll actually use
	// p2wkh change all change addresses.
	KeyScopeBIP0049Plus = KeyScope{
		Purpose: 49,
		Coin:    0,
	}

	// KeyScopeBIP0084 is the key scope for BIP0084 derivation. BIP0084
	// will be used to derive all p2wkh addresses.
	KeyScopeBIP0084 = KeyScope{
		Purpose: 84,
		Coin:    0,
	}

	// KeyScopeBIP0044 is the key scope for BIP0044 derivation. Legacy
	// wallets will only be able to use this key scope, and no keys beyond
	// it.
	KeyScopeBIP0044 = KeyScope{
		Purpose: 44,
		Coin:    0,
	}

	// DefaultKeyScopes is the set of default key scopes that will be
	// created by the root manager upon initial creation.
	DefaultKeyScopes = []KeyScope{
		KeyScopeBIP0049Plus,
		KeyScopeBIP0084,
		KeyScopeBIP0044,
	}

	// ScopeAddrMap is a map from the default key scopes to the scope
	// address schema for each scope type. This will be consulted during
	// the initial creation of the root key manager.
	ScopeAddrMap = map[KeyScope]ScopeAddrSchema{
		KeyScopeBIP0049Plus: {
			ExternalAddrType: NestedWitnessPubKey,
			InternalAddrType: WitnessPubKey,
		},
		KeyScopeBIP0084: {
			ExternalAddrType: WitnessPubKey,
			InternalAddrType: WitnessPubKey,
		},
		KeyScopeBIP0044: {
			InternalAddrType: PubKeyHash,
			ExternalAddrType: PubKeyHash,
		},
	}

Tx相关的
2. wtxmgrNamespaceKey

Tx相关

buckets

bucketBlocks         = []byte("b")
bucketTxRecords      = []byte("t")
bucketCredits        = []byte("c")
bucketUnspent        = []byte("u")
bucketDebits         = []byte("d")
bucketUnmined        = []byte("m")
bucketUnminedCredits = []byte("mc")
bucketUnminedInputs  = []byte("mi")
  1. bucketBlocks 存储某个块有哪些Tx,没有考虑分叉 bucketBlocks: blockNumber=>blockHash+blockTime+TxCount+[ TxHash1,TxHash2...]

  2. bucketTxRecords 存储序列化的Tx,已经被打包上链的, TxHash+blockNumber+blockHash=>SerializedTx

  3. bucketCredits 存储未花费的UTXO,或者已花费,但是还没有确认的,这些都是我关注的 Txhash+blockNumber+blockHash+Index(outpoint中)=>UTXO Amount[8个字节]+其他信息 其他信息: v[8]第0位表示是否已消费 1 表示已消费 v[8]第1位表示是否是找零 1 为找零 如果已经消费,那么第9个字节后还会有TxHash+blockNumber+blockHash+Index 表示这个UTXO在哪里被消费了.

  4. bucketUnspent 存储需要我关注的未消费的UTXO,一旦该UTXO被消费,就会删除相关记录 存储outPoint=>blockNumer+blocHash 该outpoint产生的block

  5. bucketDebits 这个需要解释清楚 记录钱包中一笔被消费的UTXO, debit啥意思呢 Txhash+blockNumber+blockHash+Index(outpoint中)=>Amount[8字节]+Txhash+blockNumber+blockHash+Index

  6. bucketUnmined 存储进入memPool,但是还未被打包的交易 TxHash=>ReceivedTime(8字节)+SeralizedTx

  7. bucketUnminedCredits 存储 outpoint=>UTXO Amount+change 参考bucketCredits

  8. bucketUnminedInputs 保存已经消费的UTXO,但是还未被打包或者正在被打包 这些UTXO已经被进入mempool的Tx消费了. outpoint=>[TxHash1,TxHash2] TxHash1,TxHash2可能会消费这个outpoint